]>
git.saurik.com Git - apple/xnu.git/blob - tests/time.c
   1 #include <darwintest.h> 
   4 #include <sys/syslimits.h> 
   9 T_GLOBAL_META(T_META_CHECK_LEAKS(false)); 
  11 T_DECL(settimeofday
, "check setting and getting time of day", 
  14         struct timeval origtime 
= {}; 
  15         struct timezone origtz 
= {}; 
  16         int ret 
= gettimeofday(&origtime
, &origtz
); 
  17         T_ASSERT_POSIX_SUCCESS(ret
, "get current time with gettimeofday(2)"); 
  21          * bridgeOS is not allowed to set the time -- only the macOS side can. 
  23         T_SKIP("bridgeOS is not allowed to call settimeofday(2)"); 
  24 #endif /* TARGET_OS_BRIDGE */ 
  26         struct timeval newtime 
= {}; 
  29         ret 
= settimeofday(&newtime
, NULL
); 
  30         T_ASSERT_POSIX_SUCCESS(ret
, 
  31             "set time back 60 seconds with settimeofday(2)"); 
  33         ret 
= gettimeofday(&newtime
, NULL
); 
  34         T_ASSERT_POSIX_SUCCESS(ret
, "get new time with gettimeofday(2)"); 
  36         T_ASSERT_GT(origtime
.tv_sec
, newtime
.tv_sec
, 
  37             "new time should be before original time"); 
  41         ret 
= settimeofday(&newtime
, NULL
); 
  42         T_ASSERT_POSIX_SUCCESS(ret
, 
  43             "set time close to original value with gettimeofday(2)"); 
  46 static char tmppath
[PATH_MAX
] = ""; 
  51         if (tmppath
[0] != '\0') { 
  59         const char *tmpdir 
= getenv("TMPDIR"); 
  60         strlcat(tmppath
, tmpdir 
? tmpdir 
: "/tmp", sizeof(tmppath
)); 
  61         strlcat(tmppath
, "xnu_quick_test.XXXXX", sizeof(tmppath
)); 
  62         int fd 
= mkstemp(tmppath
); 
  63         T_ASSERT_POSIX_SUCCESS(fd
, "created temporary file at %s", tmppath
); 
  64         T_ATEND(cleanup_tmpfile
); 
  68 T_DECL(futimes
, "check that futimes updates file times", 
  69     T_META_RUN_CONCURRENTLY(true)) 
  71         int tmpfd 
= create_tmpfile(); 
  73         struct stat stbuf 
= {}; 
  74         int ret 
= fstat(tmpfd
, &stbuf
); 
  75         T_ASSERT_POSIX_SUCCESS(ret
, "get file metadata with fstat(2)"); 
  76         struct timeval amtimes
[2] = {}; 
  77         TIMESPEC_TO_TIMEVAL(&amtimes
[0], &stbuf
.st_atimespec
); 
  78         TIMESPEC_TO_TIMEVAL(&amtimes
[1], &stbuf
.st_mtimespec
); 
  80         amtimes
[0].tv_sec 
-= 120; 
  81         amtimes
[1].tv_sec 
-= 120; 
  83         ret 
= futimes(tmpfd
, amtimes
); 
  84         T_ASSERT_POSIX_SUCCESS(ret
, "update file times with utimes(2)"); 
  86         ret 
= fstat(tmpfd
, &stbuf
); 
  87         T_ASSERT_POSIX_SUCCESS(ret
, "get file metadata after update with fstat(2)"); 
  88         struct timeval newamtimes
[2] = {}; 
  89         TIMESPEC_TO_TIMEVAL(&newamtimes
[0], &stbuf
.st_atimespec
); 
  90         TIMESPEC_TO_TIMEVAL(&newamtimes
[1], &stbuf
.st_mtimespec
); 
  93          * Reading the metadata shouldn't count as an access. 
  95         T_ASSERT_EQ(amtimes
[0].tv_sec
, newamtimes
[0].tv_sec
, 
  96             "access time matches what was set"); 
  97         T_ASSERT_EQ(amtimes
[1].tv_sec
, newamtimes
[1].tv_sec
, 
  98             "modification time matches what was set");