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