1 /* $NetBSD: clocktime.c,v 1.1.1.3 2015/10/23 17:47:45 christos Exp $ */ 2 3 #include "config.h" 4 5 #include "ntp_calendar.h" 6 #include "ntp_stdlib.h" 7 8 #include "unity.h" 9 #include "test-libntp.h" 10 11 void setUp(void); 12 void tearDown(void); 13 void test_CurrentYear(void); 14 void test_CurrentYearFuzz(void); 15 void test_TimeZoneOffset(void); 16 void test_WrongYearStart(void); 17 void test_PreviousYear(void); 18 void test_NextYear(void); 19 void test_NoReasonableConversion(void); 20 int isLE(u_int32 diff,u_int32 actual); 21 void test_AlwaysInLimit(void); 22 23 24 /* --------------------------------------------------------------------- 25 * test fixture 26 * 27 * The clocktimeTest uses the NTP calendar feature to use a mockup 28 * function for getting the current system time, so the tests are not 29 * dependent on the actual system time. 30 */ 31 32 void 33 setUp() 34 { 35 ntpcal_set_timefunc(timefunc); 36 settime(2000, 1, 1, 0, 0, 0); 37 } 38 39 void 40 tearDown() 41 { 42 ntpcal_set_timefunc(NULL); 43 } 44 45 /* --------------------------------------------------------------------- 46 * test cases 47 */ 48 49 void 50 test_CurrentYear(void) { 51 /* Timestamp: 2010-06-24 12:50:00Z */ 52 const u_int32 timestamp = 3486372600UL; 53 const u_int32 expected = timestamp; /* exactly the same. */ 54 55 const int yday=175, hour=12, minute=50, second=0, tzoff=0; 56 57 u_long yearstart=0; 58 u_int32 actual; 59 60 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 61 &yearstart, &actual)); 62 TEST_ASSERT_EQUAL(expected, actual); 63 } 64 65 void 66 test_CurrentYearFuzz(void) { 67 /* 68 * Timestamp (rec_ui) is: 2010-06-24 12:50:00 69 * Time sent into function is 12:00:00. 70 * 71 * Since the fuzz is rather small, we should get a NTP 72 * timestamp for the 12:00:00 time. 73 */ 74 75 const u_int32 timestamp = 3486372600UL; /* 2010-06-24 12:50:00Z */ 76 const u_int32 expected = 3486369600UL; /* 2010-06-24 12:00:00Z */ 77 78 const int yday=175, hour=12, minute=0, second=0, tzoff=0; 79 80 u_long yearstart=0; 81 u_int32 actual; 82 83 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 84 &yearstart, &actual)); 85 TEST_ASSERT_EQUAL(expected, actual); 86 } 87 88 void 89 test_TimeZoneOffset(void) { 90 /* 91 * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800 92 * (which is 2010-06-24 04:00:00Z) 93 * 94 * Time sent into function is 04:00:00 +0800 95 */ 96 const u_int32 timestamp = 3486369600UL; 97 const u_int32 expected = timestamp; 98 99 const int yday=175, hour=4, minute=0, second=0, tzoff=8; 100 101 u_long yearstart=0; 102 u_int32 actual; 103 104 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 105 &yearstart, &actual)); 106 TEST_ASSERT_EQUAL(expected, actual); 107 } 108 109 void 110 test_WrongYearStart(void) { 111 /* 112 * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z 113 * Time sent into function is 11:00:00. 114 * Yearstart sent into function is the yearstart of 2009! 115 */ 116 const u_int32 timestamp = 3471418800UL; 117 const u_int32 expected = timestamp; 118 119 const int yday=2, hour=11, minute=0, second=0, tzoff=0; 120 121 u_long yearstart = 302024100UL; /* Yearstart of 2009. */ 122 u_int32 actual; 123 124 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 125 &yearstart, &actual)); 126 TEST_ASSERT_EQUAL(expected, actual); 127 } 128 129 void 130 test_PreviousYear(void) { 131 /* 132 * Timestamp is: 2010-01-01 01:00:00Z 133 * Time sent into function is 23:00:00 134 * (which is meant to be 2009-12-31 23:00:00Z) 135 */ 136 const u_int32 timestamp = 3471296400UL; 137 const u_int32 expected = 3471289200UL; 138 139 const int yday=365, hour=23, minute=0, second=0, tzoff=0; 140 141 u_long yearstart = 0; 142 u_int32 actual; 143 144 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 145 &yearstart, &actual)); 146 TEST_ASSERT_EQUAL(expected, actual); 147 } 148 149 void 150 test_NextYear(void) { 151 /* 152 * Timestamp is: 2009-12-31 23:00:00Z 153 * Time sent into function is 01:00:00 154 * (which is meant to be 2010-01-01 01:00:00Z) 155 */ 156 const u_int32 timestamp = 3471289200UL; 157 const u_int32 expected = 3471296400UL; 158 159 const int yday=1, hour=1, minute=0, second=0, tzoff=0; 160 u_long yearstart = 0; 161 u_int32 actual; 162 163 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp, 164 &yearstart, &actual)); 165 TEST_ASSERT_EQUAL(expected, actual); 166 } 167 168 void 169 test_NoReasonableConversion(void) { 170 /* Timestamp is: 2010-01-02 11:00:00Z */ 171 const u_int32 timestamp = 3471418800UL; 172 173 const int yday=100, hour=12, minute=0, second=0, tzoff=0; 174 u_long yearstart = 0; 175 u_int32 actual; 176 177 TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp, 178 &yearstart, &actual)); 179 } 180 181 182 int/*BOOL*/ 183 isLE(u_int32 diff,u_int32 actual){ 184 if(diff <= actual){ 185 return TRUE; 186 } 187 else return FALSE; 188 } 189 190 191 void 192 test_AlwaysInLimit(void) { 193 /* Timestamp is: 2010-01-02 11:00:00Z */ 194 const u_int32 timestamp = 3471418800UL; 195 const u_short prime_incs[] = { 127, 151, 163, 179 }; 196 int cyc; 197 int yday; 198 u_char whichprime; 199 u_short ydayinc; 200 int hour; 201 int minute; 202 u_long yearstart; 203 u_int32 actual; 204 u_int32 diff; 205 206 yearstart = 0; 207 for (cyc = 0; cyc < 5; cyc++) { 208 settime(1900 + cyc * 65, 1, 1, 0, 0, 0); 209 for (yday = -26000; yday < 26000; yday += ydayinc) { 210 whichprime = abs(yday) % COUNTOF(prime_incs); 211 ydayinc = prime_incs[whichprime]; 212 for (hour = -204; hour < 204; hour += 2) { 213 for (minute = -60; minute < 60; minute++) { 214 clocktime(yday, hour, minute, 30, 0, 215 timestamp, &yearstart, &actual); 216 diff = actual - timestamp; 217 if (diff >= 0x80000000UL) 218 diff = ~diff + 1; 219 TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY))); 220 } 221 } 222 } 223 } 224 } 225