3 #include "ntp_calendar.h"
4 #include "ntp_stdlib.h"
7 #include "test-libntp.h"
11 void test_CurrentYear(void);
12 void test_CurrentYearFuzz(void);
13 void test_TimeZoneOffset(void);
14 void test_WrongYearStart(void);
15 void test_PreviousYear(void);
16 void test_NextYear(void);
17 void test_NoReasonableConversion(void);
18 int isLE(u_int32 diff,u_int32 actual);
19 void test_AlwaysInLimit(void);
22 /* ---------------------------------------------------------------------
25 * The clocktimeTest uses the NTP calendar feature to use a mockup
26 * function for getting the current system time, so the tests are not
27 * dependent on the actual system time.
33 ntpcal_set_timefunc(timefunc);
34 settime(2000, 1, 1, 0, 0, 0);
42 ntpcal_set_timefunc(NULL);
47 /* ---------------------------------------------------------------------
52 test_CurrentYear(void)
54 /* Timestamp: 2010-06-24 12:50:00Z */
55 const u_int32 timestamp = 3486372600UL;
56 const u_int32 expected = timestamp; /* exactly the same. */
58 const int yday=175, hour=12, minute=50, second=0, tzoff=0;
63 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
64 timestamp, &yearstart, &actual));
65 TEST_ASSERT_EQUAL(expected, actual);
71 test_CurrentYearFuzz(void)
74 * Timestamp (rec_ui) is: 2010-06-24 12:50:00
75 * Time sent into function is 12:00:00.
77 * Since the fuzz is rather small, we should get a NTP
78 * timestamp for the 12:00:00 time.
81 const u_int32 timestamp = 3486372600UL; /* 2010-06-24 12:50:00Z */
82 const u_int32 expected = 3486369600UL; /* 2010-06-24 12:00:00Z */
84 const int yday=175, hour=12, minute=0, second=0, tzoff=0;
89 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
90 timestamp, &yearstart, &actual));
91 TEST_ASSERT_EQUAL(expected, actual);
97 test_TimeZoneOffset(void)
100 * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
101 * (which is 2010-06-24 04:00:00Z)
103 * Time sent into function is 04:00:00 +0800
105 const u_int32 timestamp = 3486369600UL;
106 const u_int32 expected = timestamp;
108 const int yday=175, hour=4, minute=0, second=0, tzoff=8;
113 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
114 &yearstart, &actual));
115 TEST_ASSERT_EQUAL(expected, actual);
119 test_WrongYearStart(void)
122 * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
123 * Time sent into function is 11:00:00.
124 * Yearstart sent into function is the yearstart of 2009!
126 const u_int32 timestamp = 3471418800UL;
127 const u_int32 expected = timestamp;
129 const int yday=2, hour=11, minute=0, second=0, tzoff=0;
131 u_long yearstart = 302024100UL; /* Yearstart of 2009. */
134 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
135 &yearstart, &actual));
136 TEST_ASSERT_EQUAL(expected, actual);
140 test_PreviousYear(void)
143 * Timestamp is: 2010-01-01 01:00:00Z
144 * Time sent into function is 23:00:00
145 * (which is meant to be 2009-12-31 23:00:00Z)
147 const u_int32 timestamp = 3471296400UL;
148 const u_int32 expected = 3471289200UL;
150 const int yday=365, hour=23, minute=0, second=0, tzoff=0;
152 u_long yearstart = 0;
155 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
156 &yearstart, &actual));
157 TEST_ASSERT_EQUAL(expected, actual);
164 * Timestamp is: 2009-12-31 23:00:00Z
165 * Time sent into function is 01:00:00
166 * (which is meant to be 2010-01-01 01:00:00Z)
168 const u_int32 timestamp = 3471289200UL;
169 const u_int32 expected = 3471296400UL;
171 const int yday=1, hour=1, minute=0, second=0, tzoff=0;
172 u_long yearstart = 0;
175 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
176 timestamp, &yearstart, &actual));
177 TEST_ASSERT_EQUAL(expected, actual);
183 test_NoReasonableConversion(void)
185 /* Timestamp is: 2010-01-02 11:00:00Z */
186 const u_int32 timestamp = 3471418800UL;
188 const int yday=100, hour=12, minute=0, second=0, tzoff=0;
189 u_long yearstart = 0;
192 TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff,
193 timestamp, &yearstart, &actual));
200 isLE(u_int32 diff,u_int32 actual)
203 if (diff <= actual) {
211 test_AlwaysInLimit(void)
213 /* Timestamp is: 2010-01-02 11:00:00Z */
214 const u_int32 timestamp = 3471418800UL;
215 const u_short prime_incs[] = { 127, 151, 163, 179 };
227 for (cyc = 0; cyc < 5; cyc++) {
228 settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
229 for (yday = -26000; yday < 26000; yday += ydayinc) {
230 whichprime = abs(yday) % COUNTOF(prime_incs);
231 ydayinc = prime_incs[whichprime];
232 for (hour = -204; hour < 204; hour += 2) {
233 for (minute = -60; minute < 60; minute++) {
234 clocktime(yday, hour, minute, 30, 0,
235 timestamp, &yearstart,
237 diff = actual - timestamp;
238 if (diff >= 0x80000000UL)
240 TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY)));