]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/clocktime.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[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
10 // ---------------------------------------------------------------------
11 // test fixture
12 //
13 // The clocktimeTest uses the NTP calendar feature to use a mockup
14 // function for getting the current system time, so the tests are not
15 // dependent on the actual system time.
16
17
18 void setUp()
19 {
20     ntpcal_set_timefunc(timefunc);
21     settime(2000, 1, 1, 0, 0, 0);
22 }
23
24 void tearDown()
25 {
26     ntpcal_set_timefunc(NULL);
27 }
28
29 // ---------------------------------------------------------------------
30 // test cases
31
32 void test_CurrentYear() {
33         // Timestamp: 2010-06-24 12:50:00Z
34         const u_int32 timestamp = 3486372600UL;
35         const u_int32 expected  = timestamp; // exactly the same.
36
37         const int yday=175, hour=12, minute=50, second=0, tzoff=0;
38
39         u_long yearstart=0;
40         u_int32 actual;
41
42         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
43                                                   &yearstart, &actual));
44         TEST_ASSERT_EQUAL(expected, actual);
45 }
46
47 void test_CurrentYearFuzz() {
48         /* 
49          * Timestamp (rec_ui) is: 2010-06-24 12:50:00
50          * Time sent into function is 12:00:00.
51          *
52          * Since the fuzz is rather small, we should get a NTP
53          * timestamp for the 12:00:00 time.
54          */
55
56         const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
57         const u_int32 expected  = 3486369600UL; // 2010-06-24 12:00:00Z
58
59         const int yday=175, hour=12, minute=0, second=0, tzoff=0;
60
61         u_long yearstart=0;
62         u_int32 actual;
63
64         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
65                                                   &yearstart, &actual));
66         TEST_ASSERT_EQUAL(expected, actual);
67 }
68
69 void test_TimeZoneOffset() {
70         /*
71          * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
72          * (which is 2010-06-24 04:00:00Z)
73          *
74          * Time sent into function is 04:00:00 +0800
75          */
76         const u_int32 timestamp = 3486369600UL;
77         const u_int32 expected  = timestamp;
78
79         const int yday=175, hour=4, minute=0, second=0, tzoff=8;
80
81         u_long yearstart=0;
82         u_int32 actual;
83
84         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
85                                                   &yearstart, &actual));
86         TEST_ASSERT_EQUAL(expected, actual);
87 }
88
89 void test_WrongYearStart() {
90         /* 
91          * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
92          * Time sent into function is 11:00:00.
93          * Yearstart sent into function is the yearstart of 2009!
94          */
95         const u_int32 timestamp = 3471418800UL;
96         const u_int32 expected  = timestamp;
97
98         const int yday=2, hour=11, minute=0, second=0, tzoff=0;
99
100         u_long yearstart = 302024100UL; // Yearstart of 2009.
101         u_int32 actual;
102
103         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
104                                                   &yearstart, &actual));
105         TEST_ASSERT_EQUAL(expected, actual);
106 }
107
108 void test_PreviousYear() {
109         /*
110          * Timestamp is: 2010-01-01 01:00:00Z
111          * Time sent into function is 23:00:00
112          * (which is meant to be 2009-12-31 23:00:00Z)
113          */
114         const u_int32 timestamp = 3471296400UL;
115         const u_int32 expected  = 3471289200UL;
116
117         const int yday=365, hour=23, minute=0, second=0, tzoff=0;
118
119         u_long yearstart = 0;
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 test_NextYear() {
128         /*
129          * Timestamp is: 2009-12-31 23:00:00Z
130          * Time sent into function is 01:00:00
131          * (which is meant to be 2010-01-01 01:00:00Z)
132          */
133         const u_int32 timestamp = 3471289200UL;
134         const u_int32 expected  = 3471296400UL;
135
136         const int yday=1, hour=1, minute=0, second=0, tzoff=0;
137         u_long yearstart = 0;
138         u_int32 actual;
139
140         TEST_ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
141                                                   &yearstart, &actual));
142         TEST_ASSERT_EQUAL(expected, actual);
143 }
144
145 void test_NoReasonableConversion() {
146         /* Timestamp is: 2010-01-02 11:00:00Z */
147         const u_int32 timestamp = 3471418800UL;
148         
149         const int yday=100, hour=12, minute=0, second=0, tzoff=0;
150         u_long yearstart = 0;
151         u_int32 actual;
152
153         TEST_ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
154                                                    &yearstart, &actual));
155 }
156
157 // *** FUNCTION isLE, to simulate gtest's ASSERT_LE using Unity's TEST_ASSERT_TRUE
158 //tehnically boolean
159 int isLE(u_int32 diff,u_int32 actual){
160         if(diff <= actual){
161                 return TRUE;
162         }
163         else return FALSE;
164 }
165
166
167 void test_AlwaysInLimit() {
168         /* Timestamp is: 2010-01-02 11:00:00Z */
169         const u_int32 timestamp = 3471418800UL;
170         const u_short prime_incs[] = { 127, 151, 163, 179 };
171         int     cyc;
172         int     yday;
173         u_char  whichprime;
174         u_short ydayinc;
175         int     hour;
176         int     minute;
177         int     second;
178         u_long  yearstart;
179         u_int32 actual;
180         u_int32 diff;
181
182         yearstart = 0;
183         for (cyc = 0; cyc < 5; cyc++) {
184                 settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
185                 for (yday = -26000; yday < 26000; yday += ydayinc) {
186                         whichprime = abs(yday) % COUNTOF(prime_incs);
187                         ydayinc = prime_incs[whichprime];
188                         for (hour = -204; hour < 204; hour += 2) {
189                                 for (minute = -60; minute < 60; minute++) {
190                                         clocktime(yday, hour, minute, 30, 0,
191                                                   timestamp, &yearstart, &actual);
192                                         diff = actual - timestamp;
193                                         if (diff >= 0x80000000UL)
194                                                 diff = ~diff + 1;
195                                         TEST_ASSERT_TRUE(isLE(diff, (183u * SECSPERDAY))); // adding new function to return TRUE if first number is less or equal the second
196                                         //TEST_ASSERT_LE(diff, (183u * SECSPERDAY));
197                                 }
198                         }
199                 }
200         }
201 }