X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/clxutils/threadTest/timeThread.cpp diff --git a/SecurityTests/clxutils/threadTest/timeThread.cpp b/SecurityTests/clxutils/threadTest/timeThread.cpp new file mode 100644 index 00000000..ba060c65 --- /dev/null +++ b/SecurityTests/clxutils/threadTest/timeThread.cpp @@ -0,0 +1,99 @@ +/* quick time() and gmtime() tester. */ +/* As of Dec. 20, 2000, this test demonstrates that stdlib time() is thread-safe, but + * gmtime() is NOT thread safe. */ +#include +#include +#include "testParams.h" +#include +#include + +#define DO_CF_TIME 1 +#define DO_TIME_LOCK 1 + +#if DO_CF_TIME +int timeInit(TestParams *tp) +{ + return 0; +} + +#define INNER_LOOPS 100 + +int timeThread(TestParams *tp) +{ + CFAbsoluteTime cfTimes[INNER_LOOPS]; + + for(unsigned dex=0; dexnumLoops; dex++) { + if(tp->verbose) { + printf("timeThread loop %u\n", dex); + } + else if(!tp->quiet) { + printChar(tp->progressChar); + } + + for(unsigned innerLoop=0; innerLoopnumLoops); dex++) { + time_t nowTime; + struct tm nowTm; + nowTime = time(NULL); + #if DO_TIME_LOCK + timeLock.lock(); + #endif + nowTm = *gmtime(&nowTime); + #if DO_TIME_LOCK + timeLock.unlock(); + #endif + if(nowTime < baseTime) { + printf("\n***time() went backwards: base %d now %d\n", + (int)baseTime, (int)nowTime); + return 1; + } + if((nowTm.tm_year < baseTm.tm_year) || + (nowTm.tm_mon < baseTm.tm_mon) || + (nowTm.tm_mday < baseTm.tm_mday) || + /* careful, this overflows at midnight */ + (nowTm.tm_hour < baseTm.tm_hour)) { + printf("\n***gmtime() went backwards\n"); + printf(" baseTm y:%d m:%d d:%d h:%d m:%d\n", + baseTm.tm_year, baseTm.tm_mon, baseTm.tm_mday, + baseTm.tm_hour, baseTm.tm_min); + printf(" nowTm y:%d m:%d d:%d h:%d m:%d\n", + nowTm.tm_year, nowTm.tm_mon, nowTm.tm_mday, + nowTm.tm_hour, nowTm.tm_min); + return 1; + } + if(((dex % 100) == 0) && !tp->quiet) { + printChar(tp->progressChar); + } + } + return 0; +} + +#endif /* DO_CF_TIME */