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