5 #include <mach/clock_types.h>
9 #include <darwintest.h>
10 #include <darwintest_utils.h>
13 #include <sys/types.h>
15 #include <uuid/uuid.h>
19 * This test expects the entitlement or root privileges for a process to
20 * set the time using settimeofday syscall.
23 #define DAY 86400 //1 day in sec
25 T_DECL(settime_32089962_not_entitled_root
,
26 "Verify that root privileges can allow to change the time",
27 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
29 struct timeval settimeofdaytime
;
30 struct timeval adj_time
;
34 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
37 /* test settimeofday */
38 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
39 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
44 T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
),NULL
);
46 /* test ntp_adjtime */
47 memset(&ntptime
, 0, sizeof(ntptime
));
48 ntptime
.modes
|= MOD_STATUS
;
49 ntptime
.status
= TIME_OK
;
51 T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
);
54 T_DECL(settime_32089962_not_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))
58 struct timeval settimeofdaytime
;
59 struct timeval adj_time
;
64 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
67 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
);
69 /* test settimeofday */
70 #if TARGET_OS_EMBEDDED
71 T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
);
73 res
= settimeofday(&settimeofdaytime
, NULL
);
74 T_ASSERT_EQ(res
, -1, NULL
);
80 res
= adjtime(&adj_time
, NULL
);
81 T_ASSERT_EQ(res
, -1, NULL
);
83 /* test ntp_adjtime */
84 memset(&ntptime
, 0, sizeof(ntptime
));
85 ntptime
.modes
|= MOD_STATUS
;
86 ntptime
.status
= TIME_OK
;
87 res
= ntp_adjtime(&ntptime
);
88 T_ASSERT_EQ(res
, -1, NULL
);
91 T_DECL(settimeofday_29193041_not_entitled_root
,
92 "Verify that root privileges can allow to change the time",
93 T_META_ASROOT(true), T_META_CHECK_LEAKS(false))
99 T_SKIP("settimeofday_root_29193041 test requires root privileges to run.");
102 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
104 /* increment the time of one day */
105 new_time
= time
.tv_sec
+ DAY
;
107 time
.tv_sec
= new_time
;
110 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
112 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
114 /* expext to be past new_time */
115 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time changed with root and without entitlement");
118 T_QUIET
;T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
121 T_DECL(settimeofday_29193041_not_entitled_not_root
,
122 "Verify that the \"com.apple.settime\" entitlement can allow to change the time",
123 T_META_ASROOT(false), T_META_CHECK_LEAKS(false))
129 T_SKIP("settimeofday_29193041 test requires no root privileges to run.");
132 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
134 /* increment the time of one day */
135 new_time
= time
.tv_sec
+ DAY
;
137 time
.tv_sec
= new_time
;
140 #if TARGET_OS_EMBEDDED
141 T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
143 int res
= settimeofday(&time
, NULL
);
144 T_ASSERT_EQ(res
, -1, NULL
);
147 T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
);
149 #if TARGET_OS_EMBEDDED
150 /* expext to be past new_time */
151 T_EXPECT_GE_LONG(time
.tv_sec
, new_time
, "Time successfully changed without root and without entitlement");
153 T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
);
155 T_EXPECT_LT_LONG(time
.tv_sec
, new_time
, "Not permitted to change time without root and without entitlement");