6 #include <mach/clock_types.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
13 * This test expects the entitlement or root privileges for a process to
14 * set the time using settimeofday syscall.
17 #define DAY 86400 //1 day in sec
19 T_DECL(settime_32089962_entitled_root
,
20 "Verify that root privileges can allow to change the time",
21 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
23 struct timeval settimeofdaytime
;
24 struct timeval adj_time
;
28 T_SKIP("settime_32089962_entitled_root test requires root privileges to run.");
31 /* test settimeofday */
32 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
33 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
38 T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
), NULL
);
40 /* test ntp_adjtime */
41 memset(&ntptime
, 0, sizeof(ntptime
));
42 ntptime
.modes
|= MOD_STATUS
;
43 ntptime
.status
= TIME_OK
;
45 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
48 T_DECL(settime_32089962_entitled_not_root
,
49 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
50 T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
52 struct timeval settimeofdaytime
;
53 struct timeval adj_time
;
57 T_SKIP("settime_32089962_entitled_root test requires no root privileges to run.");
60 /* test settimeofday */
61 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
62 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
67 T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
), NULL
);
69 /* test ntp_adjtime */
70 memset(&ntptime
, 0, sizeof(ntptime
));
71 ntptime
.modes
|= MOD_STATUS
;
72 ntptime
.status
= TIME_OK
;
74 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
77 T_DECL(settimeofday_29193041_entitled_root
,
78 "Verify that root privileges can allow to change the time",
79 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
85 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
88 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
90 /* increment the time of one day */
91 new_time
= time
.tv_sec
+ DAY
;
93 time
.tv_sec
= new_time
;
96 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
98 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
100 /* expext to be past new_time */
101 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time changed with root and entitlement");
104 T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
107 T_DECL(settimeofday_29193041_entitled_not_root
,
108 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
109 T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
114 if (geteuid() == 0) {
115 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
118 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
120 /* increment the time of one day */
121 new_time
= time
.tv_sec
+ DAY
;
123 time
.tv_sec
= new_time
;
126 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
128 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
130 /* expext to be past new_time */
131 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time successfully changed without root and with entitlement");
134 T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);