xref: /xnu-8792.81.2/tests/time.c (revision 19c3b8c28c31cb8130e034cfb5df6bf9ba342d90)
1*19c3b8c2SApple OSS Distributions #include <darwintest.h>
2*19c3b8c2SApple OSS Distributions #include <darwintest_utils.h>
3*19c3b8c2SApple OSS Distributions #include <stdlib.h>
4*19c3b8c2SApple OSS Distributions #include <sys/stat.h>
5*19c3b8c2SApple OSS Distributions #include <sys/syslimits.h>
6*19c3b8c2SApple OSS Distributions #include <sys/time.h>
7*19c3b8c2SApple OSS Distributions #include <time.h>
8*19c3b8c2SApple OSS Distributions #include <unistd.h>
9*19c3b8c2SApple OSS Distributions 
10*19c3b8c2SApple OSS Distributions T_GLOBAL_META(T_META_CHECK_LEAKS(false));
11*19c3b8c2SApple OSS Distributions 
12*19c3b8c2SApple OSS Distributions T_DECL(settimeofday, "check setting and getting time of day",
13*19c3b8c2SApple OSS Distributions     T_META_ASROOT(true))
14*19c3b8c2SApple OSS Distributions {
15*19c3b8c2SApple OSS Distributions 	struct timeval origtime = {};
16*19c3b8c2SApple OSS Distributions 	struct timezone origtz = {};
17*19c3b8c2SApple OSS Distributions 	int ret = gettimeofday(&origtime, &origtz);
18*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get current time with gettimeofday(2)");
19*19c3b8c2SApple OSS Distributions 
20*19c3b8c2SApple OSS Distributions #if TARGET_OS_BRIDGE
21*19c3b8c2SApple OSS Distributions 	/*
22*19c3b8c2SApple OSS Distributions 	 * bridgeOS is not allowed to set the time -- only the macOS side can.
23*19c3b8c2SApple OSS Distributions 	 */
24*19c3b8c2SApple OSS Distributions 	T_SKIP("bridgeOS is not allowed to call settimeofday(2)");
25*19c3b8c2SApple OSS Distributions #endif /* TARGET_OS_BRIDGE */
26*19c3b8c2SApple OSS Distributions 
27*19c3b8c2SApple OSS Distributions 	struct timeval newtime = {};
28*19c3b8c2SApple OSS Distributions 	newtime = origtime;
29*19c3b8c2SApple OSS Distributions 	newtime.tv_sec -= 60;
30*19c3b8c2SApple OSS Distributions 	ret = settimeofday(&newtime, NULL);
31*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret,
32*19c3b8c2SApple OSS Distributions 	    "set time back 60 seconds with settimeofday(2)");
33*19c3b8c2SApple OSS Distributions 
34*19c3b8c2SApple OSS Distributions 	ret = gettimeofday(&newtime, NULL);
35*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get new time with gettimeofday(2)");
36*19c3b8c2SApple OSS Distributions 
37*19c3b8c2SApple OSS Distributions 	T_ASSERT_GT(origtime.tv_sec, newtime.tv_sec,
38*19c3b8c2SApple OSS Distributions 	    "new time should be before original time");
39*19c3b8c2SApple OSS Distributions 
40*19c3b8c2SApple OSS Distributions 	newtime = origtime;
41*19c3b8c2SApple OSS Distributions 	newtime.tv_sec += 1;
42*19c3b8c2SApple OSS Distributions 	ret = settimeofday(&newtime, NULL);
43*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret,
44*19c3b8c2SApple OSS Distributions 	    "set time close to original value with gettimeofday(2)");
45*19c3b8c2SApple OSS Distributions }
46*19c3b8c2SApple OSS Distributions 
47*19c3b8c2SApple OSS Distributions static char tmppath[PATH_MAX] = "";
48*19c3b8c2SApple OSS Distributions 
49*19c3b8c2SApple OSS Distributions static void
cleanup_tmpfile(void)50*19c3b8c2SApple OSS Distributions cleanup_tmpfile(void)
51*19c3b8c2SApple OSS Distributions {
52*19c3b8c2SApple OSS Distributions 	if (tmppath[0] != '\0') {
53*19c3b8c2SApple OSS Distributions 		unlink(tmppath);
54*19c3b8c2SApple OSS Distributions 	}
55*19c3b8c2SApple OSS Distributions }
56*19c3b8c2SApple OSS Distributions 
57*19c3b8c2SApple OSS Distributions static int
create_tmpfile(void)58*19c3b8c2SApple OSS Distributions create_tmpfile(void)
59*19c3b8c2SApple OSS Distributions {
60*19c3b8c2SApple OSS Distributions 	const char *tmpdir = dt_tmpdir();
61*19c3b8c2SApple OSS Distributions 	strlcat(tmppath, tmpdir ? tmpdir : "/tmp", sizeof(tmppath));
62*19c3b8c2SApple OSS Distributions 	strlcat(tmppath, "/xnu.futimes.XXXXX", sizeof(tmppath));
63*19c3b8c2SApple OSS Distributions 	T_LOG("creating temporary file at %s", tmppath);
64*19c3b8c2SApple OSS Distributions 	int fd = mkstemp(tmppath);
65*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "create temporary file");
66*19c3b8c2SApple OSS Distributions 	T_ATEND(cleanup_tmpfile);
67*19c3b8c2SApple OSS Distributions 	return fd;
68*19c3b8c2SApple OSS Distributions }
69*19c3b8c2SApple OSS Distributions 
70*19c3b8c2SApple OSS Distributions T_DECL(futimes, "check that futimes updates file times",
71*19c3b8c2SApple OSS Distributions     T_META_RUN_CONCURRENTLY(true))
72*19c3b8c2SApple OSS Distributions {
73*19c3b8c2SApple OSS Distributions 	int tmpfd = create_tmpfile();
74*19c3b8c2SApple OSS Distributions 
75*19c3b8c2SApple OSS Distributions 	struct stat stbuf = {};
76*19c3b8c2SApple OSS Distributions 	int ret = fstat(tmpfd, &stbuf);
77*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get file metadata with fstat(2)");
78*19c3b8c2SApple OSS Distributions 	struct timeval amtimes[2] = {};
79*19c3b8c2SApple OSS Distributions 	TIMESPEC_TO_TIMEVAL(&amtimes[0], &stbuf.st_atimespec);
80*19c3b8c2SApple OSS Distributions 	TIMESPEC_TO_TIMEVAL(&amtimes[1], &stbuf.st_mtimespec);
81*19c3b8c2SApple OSS Distributions 
82*19c3b8c2SApple OSS Distributions 	amtimes[0].tv_sec -= 120;
83*19c3b8c2SApple OSS Distributions 	amtimes[1].tv_sec -= 120;
84*19c3b8c2SApple OSS Distributions 
85*19c3b8c2SApple OSS Distributions 	ret = futimes(tmpfd, amtimes);
86*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "update file times with utimes(2)");
87*19c3b8c2SApple OSS Distributions 
88*19c3b8c2SApple OSS Distributions 	ret = fstat(tmpfd, &stbuf);
89*19c3b8c2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret, "get file metadata after update with fstat(2)");
90*19c3b8c2SApple OSS Distributions 	struct timeval newamtimes[2] = {};
91*19c3b8c2SApple OSS Distributions 	TIMESPEC_TO_TIMEVAL(&newamtimes[0], &stbuf.st_atimespec);
92*19c3b8c2SApple OSS Distributions 	TIMESPEC_TO_TIMEVAL(&newamtimes[1], &stbuf.st_mtimespec);
93*19c3b8c2SApple OSS Distributions 
94*19c3b8c2SApple OSS Distributions 	/*
95*19c3b8c2SApple OSS Distributions 	 * Reading the metadata shouldn't count as an access.
96*19c3b8c2SApple OSS Distributions 	 */
97*19c3b8c2SApple OSS Distributions 	T_ASSERT_EQ(amtimes[0].tv_sec, newamtimes[0].tv_sec,
98*19c3b8c2SApple OSS Distributions 	    "access time matches what was set");
99*19c3b8c2SApple OSS Distributions 	T_ASSERT_EQ(amtimes[1].tv_sec, newamtimes[1].tv_sec,
100*19c3b8c2SApple OSS Distributions 	    "modification time matches what was set");
101*19c3b8c2SApple OSS Distributions }
102