5 #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_not_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
; 
  27         /* test settimeofday */ 
  28         T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
); 
  29         T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
); 
  34         T_ASSERT_POSIX_ZERO(adjtime(&adj_time
, NULL
), NULL
); 
  36         /* test ntp_adjtime */ 
  37         memset(&ntptime
, 0, sizeof(ntptime
)); 
  38         ntptime
.modes 
|= MOD_STATUS
; 
  39         ntptime
.status 
= TIME_OK
; 
  41         T_ASSERT_EQ(ntp_adjtime(&ntptime
), TIME_OK
, NULL
); 
  44 T_DECL(settime_32089962_not_entitled_not_root
, 
  45     "Verify that the \"com.apple.settime\" entitlement can allow to change the time", 
  46     T_META_ASROOT(false), T_META_CHECK_LEAKS(false)) 
  48         struct timeval settimeofdaytime
; 
  49         struct timeval adj_time
; 
  54                 T_SKIP("settimeofday_29193041 test requires no root privileges to run."); 
  57         T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&settimeofdaytime
, NULL
), NULL
); 
  59         /* test settimeofday */ 
  60 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 
  61         T_ASSERT_POSIX_ZERO(settimeofday(&settimeofdaytime
, NULL
), NULL
); 
  63         res 
= settimeofday(&settimeofdaytime
, NULL
); 
  64         T_ASSERT_EQ(res
, -1, NULL
); 
  70         res 
= adjtime(&adj_time
, NULL
); 
  71         T_ASSERT_EQ(res
, -1, NULL
); 
  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
); 
  81 T_DECL(settimeofday_29193041_not_entitled_root
, 
  82     "Verify that root privileges can allow to change the time", 
  83     T_META_ASROOT(true), T_META_CHECK_LEAKS(false)) 
  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 without entitlement"); 
 104         T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
); 
 107 T_DECL(settimeofday_29193041_not_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 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 
 127         T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
); 
 129         int res 
= settimeofday(&time
, NULL
); 
 130         T_ASSERT_EQ(res
, -1, NULL
); 
 133         T_QUIET
; T_ASSERT_POSIX_ZERO(gettimeofday(&time
, NULL
), NULL
); 
 135 #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR) 
 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"); 
 139         T_QUIET
; T_ASSERT_POSIX_ZERO(settimeofday(&time
, NULL
), NULL
); 
 141         T_EXPECT_LT_LONG(time
.tv_sec
, new_time
, "Not permitted to change time without root and without entitlement");