]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_clocktime.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_clocktime.cpp
1 #include "g_libntptest.h"
2
3 // ---------------------------------------------------------------------
4 // test fixture
5 //
6 // The clocktimeTest uses the NTP calendar feature to use a mockup
7 // function for getting the current system time, so the tests are not
8 // dependent on the actual system time.
9
10 class clocktimeTest : public libntptest {
11         virtual void SetUp();
12         virtual void TearDown();
13 };
14
15 void clocktimeTest::SetUp()
16 {
17     ntpcal_set_timefunc(timefunc);
18     settime(2000, 1, 1, 0, 0, 0);
19 }
20
21 void clocktimeTest::TearDown()
22 {
23     ntpcal_set_timefunc(NULL);
24 }
25
26 // ---------------------------------------------------------------------
27 // test cases
28
29 TEST_F(clocktimeTest, CurrentYear) {
30         // Timestamp: 2010-06-24 12:50:00Z
31         const u_int32 timestamp = 3486372600UL;
32         const u_int32 expected  = timestamp; // exactly the same.
33
34         const int yday=175, hour=12, minute=50, second=0, tzoff=0;
35
36         u_long yearstart=0;
37         u_int32 actual;
38
39         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
40                                                   &yearstart, &actual));
41         EXPECT_EQ(expected, actual);
42 }
43
44 TEST_F(clocktimeTest, CurrentYearFuzz) {
45         /* 
46          * Timestamp (rec_ui) is: 2010-06-24 12:50:00
47          * Time sent into function is 12:00:00.
48          *
49          * Since the fuzz is rather small, we should get a NTP
50          * timestamp for the 12:00:00 time.
51          */
52
53         const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
54         const u_int32 expected  = 3486369600UL; // 2010-06-24 12:00:00Z
55
56         const int yday=175, hour=12, minute=0, second=0, tzoff=0;
57
58         u_long yearstart=0;
59         u_int32 actual;
60
61         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
62                                                   &yearstart, &actual));
63         EXPECT_EQ(expected, actual);
64 }
65
66 TEST_F(clocktimeTest, TimeZoneOffset) {
67         /*
68          * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
69          * (which is 2010-06-24 04:00:00Z)
70          *
71          * Time sent into function is 04:00:00 +0800
72          */
73         const u_int32 timestamp = 3486369600UL;
74         const u_int32 expected  = timestamp;
75
76         const int yday=175, hour=4, minute=0, second=0, tzoff=8;
77
78         u_long yearstart=0;
79         u_int32 actual;
80
81         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
82                                                   &yearstart, &actual));
83         EXPECT_EQ(expected, actual);
84 }
85
86 TEST_F(clocktimeTest, WrongYearStart) {
87         /* 
88          * Timestamp (rec_ui) is: 2010-01-02 11:00:00Z
89          * Time sent into function is 11:00:00.
90          * Yearstart sent into function is the yearstart of 2009!
91          */
92         const u_int32 timestamp = 3471418800UL;
93         const u_int32 expected  = timestamp;
94
95         const int yday=2, hour=11, minute=0, second=0, tzoff=0;
96
97         u_long yearstart = 302024100UL; // Yearstart of 2009.
98         u_int32 actual;
99
100         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
101                                                   &yearstart, &actual));
102         EXPECT_EQ(expected, actual);
103 }
104
105 TEST_F(clocktimeTest, PreviousYear) {
106         /*
107          * Timestamp is: 2010-01-01 01:00:00Z
108          * Time sent into function is 23:00:00
109          * (which is meant to be 2009-12-31 23:00:00Z)
110          */
111         const u_int32 timestamp = 3471296400UL;
112         const u_int32 expected  = 3471289200UL;
113
114         const int yday=365, hour=23, minute=0, second=0, tzoff=0;
115
116         u_long yearstart = 0;
117         u_int32 actual;
118
119         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
120                                                   &yearstart, &actual));
121         EXPECT_EQ(expected, actual);
122 }
123
124 TEST_F(clocktimeTest, NextYear) {
125         /*
126          * Timestamp is: 2009-12-31 23:00:00Z
127          * Time sent into function is 01:00:00
128          * (which is meant to be 2010-01-01 01:00:00Z)
129          */
130         const u_int32 timestamp = 3471289200UL;
131         const u_int32 expected  = 3471296400UL;
132
133         const int yday=1, hour=1, minute=0, second=0, tzoff=0;
134         u_long yearstart = 0;
135         u_int32 actual;
136
137         ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
138                                                   &yearstart, &actual));
139         EXPECT_EQ(expected, actual);
140 }
141
142 TEST_F(clocktimeTest, NoReasonableConversion) {
143         /* Timestamp is: 2010-01-02 11:00:00Z */
144         const u_int32 timestamp = 3471418800UL;
145         
146         const int yday=100, hour=12, minute=0, second=0, tzoff=0;
147         u_long yearstart = 0;
148         u_int32 actual;
149
150         ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
151                                                    &yearstart, &actual));
152 }
153
154 TEST_F(clocktimeTest, AlwaysInLimit) {
155         /* Timestamp is: 2010-01-02 11:00:00Z */
156         const u_int32 timestamp = 3471418800UL;
157         const u_short prime_incs[] = { 127, 151, 163, 179 };
158         int     cyc;
159         int     yday;
160         u_char  whichprime;
161         u_short ydayinc;
162         int     hour;
163         int     minute;
164         int     second;
165         u_long  yearstart;
166         u_int32 actual;
167         u_int32 diff;
168
169         yearstart = 0;
170         for (cyc = 0; cyc < 5; cyc++) {
171                 settime(1900 + cyc * 65, 1, 1, 0, 0, 0);
172                 for (yday = -26000; yday < 26000; yday += ydayinc) {
173                         whichprime = abs(yday) % COUNTOF(prime_incs);
174                         ydayinc = prime_incs[whichprime];
175                         for (hour = -204; hour < 204; hour += 2) {
176                                 for (minute = -60; minute < 60; minute++) {
177                                         clocktime(yday, hour, minute, 30, 0,
178                                                   timestamp, &yearstart, &actual);
179                                         diff = actual - timestamp;
180                                         if (diff >= 0x80000000UL)
181                                                 diff = ~diff + 1;
182                                         ASSERT_LE(diff, (183u * SECSPERDAY));
183                                 }
184                         }
185                 }
186         }
187 }