]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/clocktime.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.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
37 void
38 tearDown()
39 {
40     ntpcal_set_timefunc(NULL);
41 }
42
43 /* ---------------------------------------------------------------------
44  * test cases
45  */
46
47 void
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. */
52
53         const int yday=175, hour=12, minute=50, second=0, tzoff=0;
54
55         u_long yearstart=0;
56         u_int32 actual;
57
58         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
59                                                   &yearstart, &actual));
60         TEST_ASSERT_EQUAL(expected, actual);
61 }
62
63 void
64 test_CurrentYearFuzz(void) {
65         /* 
66          * Timestamp (rec_ui) is: 2010-06-24 12:50:00
67          * Time sent into function is 12:00:00.
68          *
69          * Since the fuzz is rather small, we should get a NTP
70          * timestamp for the 12:00:00 time.
71          */
72
73         const u_int32 timestamp = 3486372600UL; /* 2010-06-24 12:50:00Z */
74         const u_int32 expected  = 3486369600UL; /* 2010-06-24 12:00:00Z */
75
76         const int yday=175, hour=12, minute=0, second=0, tzoff=0;
77
78         u_long yearstart=0;
79         u_int32 actual;
80
81         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
82                                                   &yearstart, &actual));
83         TEST_ASSERT_EQUAL(expected, actual);
84 }
85
86 void
87 test_TimeZoneOffset(void) {
88         /*
89          * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
90          * (which is 2010-06-24 04:00:00Z)
91          *
92          * Time sent into function is 04:00:00 +0800
93          */
94         const u_int32 timestamp = 3486369600UL;
95         const u_int32 expected  = timestamp;
96
97         const int yday=175, hour=4, minute=0, second=0, tzoff=8;
98
99         u_long yearstart=0;
100         u_int32 actual;
101
102         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
103                                                   &yearstart, &actual));
104         TEST_ASSERT_EQUAL(expected, actual);
105 }
106
107 void
108 test_WrongYearStart(void) {
109         /* 
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!
113          */
114         const u_int32 timestamp = 3471418800UL;
115         const u_int32 expected  = timestamp;
116
117         const int yday=2, hour=11, minute=0, second=0, tzoff=0;
118
119         u_long yearstart = 302024100UL; /* Yearstart of 2009. */
120         u_int32 actual;
121
122         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
123                                                   &yearstart, &actual));
124         TEST_ASSERT_EQUAL(expected, actual);
125 }
126
127 void
128 test_PreviousYear(void) {
129         /*
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)
133          */
134         const u_int32 timestamp = 3471296400UL;
135         const u_int32 expected  = 3471289200UL;
136
137         const int yday=365, hour=23, minute=0, second=0, tzoff=0;
138
139         u_long yearstart = 0;
140         u_int32 actual;
141
142         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
143                                                   &yearstart, &actual));
144         TEST_ASSERT_EQUAL(expected, actual);
145 }
146
147 void
148 test_NextYear(void) {
149         /*
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)
153          */
154         const u_int32 timestamp = 3471289200UL;
155         const u_int32 expected  = 3471296400UL;
156
157         const int yday=1, hour=1, minute=0, second=0, tzoff=0;
158         u_long yearstart = 0;
159         u_int32 actual;
160
161         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
162                                                   &yearstart, &actual));
163         TEST_ASSERT_EQUAL(expected, actual);
164 }
165
166 void
167 test_NoReasonableConversion(void) {
168         /* Timestamp is: 2010-01-02 11:00:00Z */
169         const u_int32 timestamp = 3471418800UL;
170         
171         const int yday=100, hour=12, minute=0, second=0, tzoff=0;
172         u_long yearstart = 0;
173         u_int32 actual;
174
175         TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
176                                                    &yearstart, &actual));
177 }
178
179
180 int/*BOOL*/
181 isLE(u_int32 diff,u_int32 actual){
182         if(diff <= actual){
183                 return TRUE;
184         }
185         else return FALSE;
186 }
187
188
189 void
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 };
194         int     cyc;
195         int     yday;
196         u_char  whichprime;
197         u_short ydayinc;
198         int     hour;
199         int     minute;
200         u_long  yearstart;
201         u_int32 actual;
202         u_int32 diff;
203
204         yearstart = 0;
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)
216                                                 diff = ~diff + 1;
217                                         TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY)));
218                                 }
219                         }
220                 }
221         }
222 }