xref: /xnu-11417.140.69/tests/settimeofday_29193041.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include <stdio.h>
2*43a90889SApple OSS Distributions #include <errno.h>
3*43a90889SApple OSS Distributions #include <string.h>
4*43a90889SApple OSS Distributions #include <unistd.h>
5*43a90889SApple OSS Distributions #include <mach/clock_types.h>
6*43a90889SApple OSS Distributions #include <sys/mman.h>
7*43a90889SApple OSS Distributions #include <sys/timex.h>
8*43a90889SApple OSS Distributions #include <spawn.h>
9*43a90889SApple OSS Distributions #include <darwintest.h>
10*43a90889SApple OSS Distributions #include <darwintest_utils.h>
11*43a90889SApple OSS Distributions 
12*43a90889SApple OSS Distributions /*
13*43a90889SApple OSS Distributions  * This test expects the entitlement or root privileges for a process to
14*43a90889SApple OSS Distributions  * set the time using settimeofday syscall.
15*43a90889SApple OSS Distributions  */
16*43a90889SApple OSS Distributions 
17*43a90889SApple OSS Distributions #define DAY 86400 //1 day in sec
18*43a90889SApple OSS Distributions 
19*43a90889SApple OSS Distributions T_DECL(settime_32089962_not_entitled_root,
20*43a90889SApple OSS Distributions     "Verify that root privileges can allow to change the time",
21*43a90889SApple OSS Distributions     T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
22*43a90889SApple OSS Distributions {
23*43a90889SApple OSS Distributions 	struct timeval settimeofdaytime;
24*43a90889SApple OSS Distributions 	struct timeval adj_time;
25*43a90889SApple OSS Distributions 	struct timex ntptime;
26*43a90889SApple OSS Distributions 
27*43a90889SApple OSS Distributions 	/* test settimeofday */
28*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL);
29*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL);
30*43a90889SApple OSS Distributions 
31*43a90889SApple OSS Distributions 	/* test adjtime */
32*43a90889SApple OSS Distributions 	adj_time.tv_sec = 1;
33*43a90889SApple OSS Distributions 	adj_time.tv_usec = 0;
34*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(adjtime(&adj_time, NULL), NULL);
35*43a90889SApple OSS Distributions 
36*43a90889SApple OSS Distributions 	/* test ntp_adjtime */
37*43a90889SApple OSS Distributions 	memset(&ntptime, 0, sizeof(ntptime));
38*43a90889SApple OSS Distributions 	ntptime.modes |= MOD_STATUS;
39*43a90889SApple OSS Distributions 	ntptime.status = TIME_OK;
40*43a90889SApple OSS Distributions 
41*43a90889SApple OSS Distributions 	T_ASSERT_EQ(ntp_adjtime(&ntptime), TIME_OK, NULL);
42*43a90889SApple OSS Distributions }
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions T_DECL(settime_32089962_not_entitled_not_root,
45*43a90889SApple OSS Distributions     "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
46*43a90889SApple OSS Distributions     T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
47*43a90889SApple OSS Distributions {
48*43a90889SApple OSS Distributions 	struct timeval settimeofdaytime;
49*43a90889SApple OSS Distributions 	struct timeval adj_time;
50*43a90889SApple OSS Distributions 	struct timex ntptime;
51*43a90889SApple OSS Distributions 	int res;
52*43a90889SApple OSS Distributions 
53*43a90889SApple OSS Distributions 	if (geteuid() == 0) {
54*43a90889SApple OSS Distributions 		T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
55*43a90889SApple OSS Distributions 	}
56*43a90889SApple OSS Distributions 
57*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime, NULL), NULL);
58*43a90889SApple OSS Distributions 
59*43a90889SApple OSS Distributions 	/* test settimeofday */
60*43a90889SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
61*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime, NULL), NULL);
62*43a90889SApple OSS Distributions #else
63*43a90889SApple OSS Distributions 	res = settimeofday(&settimeofdaytime, NULL);
64*43a90889SApple OSS Distributions 	T_ASSERT_EQ(res, -1, NULL);
65*43a90889SApple OSS Distributions #endif
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions 	/* test adjtime */
68*43a90889SApple OSS Distributions 	adj_time.tv_sec = 1;
69*43a90889SApple OSS Distributions 	adj_time.tv_usec = 0;
70*43a90889SApple OSS Distributions 	res = adjtime(&adj_time, NULL);
71*43a90889SApple OSS Distributions 	T_ASSERT_EQ(res, -1, NULL);
72*43a90889SApple OSS Distributions 
73*43a90889SApple OSS Distributions 	/* test ntp_adjtime */
74*43a90889SApple OSS Distributions 	memset(&ntptime, 0, sizeof(ntptime));
75*43a90889SApple OSS Distributions 	ntptime.modes |= MOD_STATUS;
76*43a90889SApple OSS Distributions 	ntptime.status = TIME_OK;
77*43a90889SApple OSS Distributions 	res = ntp_adjtime(&ntptime);
78*43a90889SApple OSS Distributions 	T_ASSERT_EQ(res, -1, NULL);
79*43a90889SApple OSS Distributions }
80*43a90889SApple OSS Distributions 
81*43a90889SApple OSS Distributions T_DECL(settimeofday_29193041_not_entitled_root,
82*43a90889SApple OSS Distributions     "Verify that root privileges can allow to change the time",
83*43a90889SApple OSS Distributions     T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
84*43a90889SApple OSS Distributions {
85*43a90889SApple OSS Distributions 	struct timeval time;
86*43a90889SApple OSS Distributions 	long new_time;
87*43a90889SApple OSS Distributions 
88*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
89*43a90889SApple OSS Distributions 
90*43a90889SApple OSS Distributions 	/* increment the time of one day */
91*43a90889SApple OSS Distributions 	new_time = time.tv_sec + DAY;
92*43a90889SApple OSS Distributions 
93*43a90889SApple OSS Distributions 	time.tv_sec = new_time;
94*43a90889SApple OSS Distributions 	time.tv_usec = 0;
95*43a90889SApple OSS Distributions 
96*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
97*43a90889SApple OSS Distributions 
98*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
99*43a90889SApple OSS Distributions 
100*43a90889SApple OSS Distributions 	/* expext to be past new_time */
101*43a90889SApple OSS Distributions 	T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time changed with root and without entitlement");
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions 	time.tv_sec -= DAY;
104*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
105*43a90889SApple OSS Distributions }
106*43a90889SApple OSS Distributions 
107*43a90889SApple OSS Distributions T_DECL(settimeofday_29193041_not_entitled_not_root,
108*43a90889SApple OSS Distributions     "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
109*43a90889SApple OSS Distributions     T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
110*43a90889SApple OSS Distributions {
111*43a90889SApple OSS Distributions 	struct timeval time;
112*43a90889SApple OSS Distributions 	long new_time;
113*43a90889SApple OSS Distributions 
114*43a90889SApple OSS Distributions 	if (geteuid() == 0) {
115*43a90889SApple OSS Distributions 		T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
116*43a90889SApple OSS Distributions 	}
117*43a90889SApple OSS Distributions 
118*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
119*43a90889SApple OSS Distributions 
120*43a90889SApple OSS Distributions 	/* increment the time of one day */
121*43a90889SApple OSS Distributions 	new_time = time.tv_sec + DAY;
122*43a90889SApple OSS Distributions 
123*43a90889SApple OSS Distributions 	time.tv_sec = new_time;
124*43a90889SApple OSS Distributions 	time.tv_usec = 0;
125*43a90889SApple OSS Distributions 
126*43a90889SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
127*43a90889SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
128*43a90889SApple OSS Distributions #else
129*43a90889SApple OSS Distributions 	int res = settimeofday(&time, NULL);
130*43a90889SApple OSS Distributions 	T_ASSERT_EQ(res, -1, NULL);
131*43a90889SApple OSS Distributions #endif
132*43a90889SApple OSS Distributions 
133*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(gettimeofday(&time, NULL), NULL);
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
136*43a90889SApple OSS Distributions 	/* expext to be past new_time */
137*43a90889SApple OSS Distributions 	T_EXPECT_GE_LONG(time.tv_sec, new_time, "Time successfully changed without root and without entitlement");
138*43a90889SApple OSS Distributions 	time.tv_sec -= DAY;
139*43a90889SApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(settimeofday(&time, NULL), NULL);
140*43a90889SApple OSS Distributions #else
141*43a90889SApple OSS Distributions 	T_EXPECT_LT_LONG(time.tv_sec, new_time, "Not permitted to change time without root and without entitlement");
142*43a90889SApple OSS Distributions #endif
143*43a90889SApple OSS Distributions }
144