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);
40 ntpcal_set_timefunc(NULL);
43 /* ---------------------------------------------------------------------
48 test_CurrentYear(void) {
49 /* Timestamp: 2010-06-24 12:50:00Z */
50 const u_int32 timestamp = 3486372600UL;
51 const u_int32 expected = timestamp; /* exactly the same. */
53 const int yday=175, hour=12, minute=50, second=0, tzoff=0;
58 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
59 &yearstart, &actual));
60 TEST_ASSERT_EQUAL(expected, actual);
64 test_CurrentYearFuzz(void) {
66 * Timestamp (rec_ui) is: 2010-06-24 12:50:00
67 * Time sent into function is 12:00:00.
69 * Since the fuzz is rather small, we should get a NTP
70 * timestamp for the 12:00:00 time.
73 const u_int32 timestamp = 3486372600UL; /* 2010-06-24 12:50:00Z */
74 const u_int32 expected = 3486369600UL; /* 2010-06-24 12:00:00Z */
76 const int yday=175, hour=12, minute=0, second=0, tzoff=0;
81 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
82 &yearstart, &actual));
83 TEST_ASSERT_EQUAL(expected, actual);
87 test_TimeZoneOffset(void) {
89 * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
90 * (which is 2010-06-24 04:00:00Z)
92 * Time sent into function is 04:00:00 +0800
94 const u_int32 timestamp = 3486369600UL;
95 const u_int32 expected = timestamp;
97 const int yday=175, hour=4, minute=0, second=0, tzoff=8;
102 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
103 &yearstart, &actual));
104 TEST_ASSERT_EQUAL(expected, actual);
108 test_WrongYearStart(void) {
110 * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
111 * Time sent into function is 11:00:00.
112 * Yearstart sent into function is the yearstart of 2009!
114 const u_int32 timestamp = 3471418800UL;
115 const u_int32 expected = timestamp;
117 const int yday=2, hour=11, minute=0, second=0, tzoff=0;
119 u_long yearstart = 302024100UL; /* Yearstart of 2009. */
122 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
123 &yearstart, &actual));
124 TEST_ASSERT_EQUAL(expected, actual);
128 test_PreviousYear(void) {
130 * Timestamp is: 2010-01-01 01:00:00Z
131 * Time sent into function is 23:00:00
132 * (which is meant to be 2009-12-31 23:00:00Z)
134 const u_int32 timestamp = 3471296400UL;
135 const u_int32 expected = 3471289200UL;
137 const int yday=365, hour=23, minute=0, second=0, tzoff=0;
139 u_long yearstart = 0;
142 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
143 &yearstart, &actual));
144 TEST_ASSERT_EQUAL(expected, actual);
148 test_NextYear(void) {
150 * Timestamp is: 2009-12-31 23:00:00Z
151 * Time sent into function is 01:00:00
152 * (which is meant to be 2010-01-01 01:00:00Z)
154 const u_int32 timestamp = 3471289200UL;
155 const u_int32 expected = 3471296400UL;
157 const int yday=1, hour=1, minute=0, second=0, tzoff=0;
158 u_long yearstart = 0;
161 TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
162 &yearstart, &actual));
163 TEST_ASSERT_EQUAL(expected, actual);
167 test_NoReasonableConversion(void) {
168 /* Timestamp is: 2010-01-02 11:00:00Z */
169 const u_int32 timestamp = 3471418800UL;
171 const int yday=100, hour=12, minute=0, second=0, tzoff=0;
172 u_long yearstart = 0;
175 TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
176 &yearstart, &actual));
181 isLE(u_int32 diff,u_int32 actual){
190 test_AlwaysInLimit(void) {
191 /* Timestamp is: 2010-01-02 11:00:00Z */
192 const u_int32 timestamp = 3471418800UL;
193 const u_short prime_incs[] = { 127, 151, 163, 179 };
205 for (cyc = 0; cyc < 5; cyc++) {
206 settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
207 for (yday = -26000; yday < 26000; yday += ydayinc) {
208 whichprime = abs(yday) % COUNTOF(prime_incs);
209 ydayinc = prime_incs[whichprime];
210 for (hour = -204; hour < 204; hour += 2) {
211 for (minute = -60; minute < 60; minute++) {
212 clocktime(yday, hour, minute, 30, 0,
213 timestamp, &yearstart, &actual);
214 diff = actual - timestamp;
215 if (diff >= 0x80000000UL)
217 TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY)));