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