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