]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/ntp/tests/libntp/clocktime.c
o Fix invalid TCP checksums with pf(4). [EN-16:02.pf]
[FreeBSD/releng/9.3.git] / contrib / ntp / tests / libntp / clocktime.c
1 #include "config.h"
2
3 #include "ntp_calendar.h"
4 #include "ntp_stdlib.h"
5
6 #include "unity.h"
7 #include "test-libntp.h"
8
9 void setUp(void);
10 void tearDown(void);
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);
20
21
22 /* ---------------------------------------------------------------------
23  * test fixture
24  *
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.
28  */
29
30 void
31 setUp()
32 {
33         ntpcal_set_timefunc(timefunc);
34         settime(2000, 1, 1, 0, 0, 0);
35
36         return;
37 }
38
39 void
40 tearDown()
41 {
42         ntpcal_set_timefunc(NULL);
43
44         return;
45 }
46
47 /* ---------------------------------------------------------------------
48  * test cases
49  */
50
51 void
52 test_CurrentYear(void)
53 {
54         /* Timestamp: 2010-06-24 12:50:00Z */
55         const u_int32 timestamp = 3486372600UL;
56         const u_int32 expected  = timestamp; /* exactly the same. */
57
58         const int yday=175, hour=12, minute=50, second=0, tzoff=0;
59
60         u_long yearstart = 0;
61         u_int32 actual;
62
63         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
64                                    timestamp, &yearstart, &actual));
65         TEST_ASSERT_EQUAL(expected, actual);
66
67         return;
68 }
69
70 void
71 test_CurrentYearFuzz(void)
72 {
73         /* 
74          * Timestamp (rec_ui) is: 2010-06-24 12:50:00
75          * Time sent into function is 12:00:00.
76          *
77          * Since the fuzz is rather small, we should get a NTP
78          * timestamp for the 12:00:00 time.
79          */
80
81         const u_int32 timestamp = 3486372600UL; /* 2010-06-24 12:50:00Z */
82         const u_int32 expected  = 3486369600UL; /* 2010-06-24 12:00:00Z */
83
84         const int yday=175, hour=12, minute=0, second=0, tzoff=0;
85
86         u_long yearstart=0;
87         u_int32 actual;
88
89         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
90                                    timestamp, &yearstart, &actual));
91         TEST_ASSERT_EQUAL(expected, actual);
92
93         return;
94 }
95
96 void
97 test_TimeZoneOffset(void)
98 {
99         /*
100          * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
101          * (which is 2010-06-24 04:00:00Z)
102          *
103          * Time sent into function is 04:00:00 +0800
104          */
105         const u_int32 timestamp = 3486369600UL;
106         const u_int32 expected  = timestamp;
107
108         const int yday=175, hour=4, minute=0, second=0, tzoff=8;
109
110         u_long yearstart=0;
111         u_int32 actual;
112
113         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
114                                                   &yearstart, &actual));
115         TEST_ASSERT_EQUAL(expected, actual);
116 }
117
118 void
119 test_WrongYearStart(void)
120 {
121         /* 
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!
125          */
126         const u_int32 timestamp = 3471418800UL;
127         const u_int32 expected  = timestamp;
128
129         const int yday=2, hour=11, minute=0, second=0, tzoff=0;
130
131         u_long yearstart = 302024100UL; /* Yearstart of 2009. */
132         u_int32 actual;
133
134         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
135                                                   &yearstart, &actual));
136         TEST_ASSERT_EQUAL(expected, actual);
137 }
138
139 void
140 test_PreviousYear(void)
141 {
142         /*
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)
146          */
147         const u_int32 timestamp = 3471296400UL;
148         const u_int32 expected  = 3471289200UL;
149
150         const int yday=365, hour=23, minute=0, second=0, tzoff=0;
151
152         u_long yearstart = 0;
153         u_int32 actual;
154
155         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
156                                                   &yearstart, &actual));
157         TEST_ASSERT_EQUAL(expected, actual);
158 }
159
160 void
161 test_NextYear(void)
162 {
163         /*
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)
167          */
168         const u_int32 timestamp = 3471289200UL;
169         const u_int32 expected  = 3471296400UL;
170
171         const int yday=1, hour=1, minute=0, second=0, tzoff=0;
172         u_long yearstart = 0;
173         u_int32 actual;
174
175         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff,
176                                    timestamp, &yearstart, &actual));
177         TEST_ASSERT_EQUAL(expected, actual);
178
179         return;
180 }
181
182 void
183 test_NoReasonableConversion(void)
184 {
185         /* Timestamp is: 2010-01-02 11:00:00Z */
186         const u_int32 timestamp = 3471418800UL;
187
188         const int yday=100, hour=12, minute=0, second=0, tzoff=0;
189         u_long yearstart = 0;
190         u_int32 actual;
191
192         TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff,
193                                     timestamp, &yearstart, &actual));
194
195         return;
196 }
197
198
199 int/*BOOL*/
200 isLE(u_int32 diff,u_int32 actual)
201 {
202
203         if (diff <= actual) {
204                 return TRUE;
205         }
206         else return FALSE;
207 }
208
209
210 void
211 test_AlwaysInLimit(void)
212 {
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 };
216         int     cyc;
217         int     yday;
218         u_char  whichprime;
219         u_short ydayinc;
220         int     hour;
221         int     minute;
222         u_long  yearstart;
223         u_int32 actual;
224         u_int32 diff;
225
226         yearstart = 0;
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,
236                                                   &actual);
237                                         diff = actual - timestamp;
238                                         if (diff >= 0x80000000UL)
239                                                 diff = ~diff + 1;
240                                         TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY)));
241                                 }
242                         }
243                 }
244         }
245         return;
246 }