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