1 #include "g_libntptest.h"
3 // ---------------------------------------------------------------------
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.
10 class clocktimeTest : public libntptest {
12 virtual void TearDown();
15 void clocktimeTest::SetUp()
17 ntpcal_set_timefunc(timefunc);
18 settime(2000, 1, 1, 0, 0, 0);
21 void clocktimeTest::TearDown()
23 ntpcal_set_timefunc(NULL);
26 // ---------------------------------------------------------------------
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.
34 const int yday=175, hour=12, minute=50, second=0, tzoff=0;
39 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
40 &yearstart, &actual));
41 EXPECT_EQ(expected, actual);
44 TEST_F(clocktimeTest, CurrentYearFuzz) {
46 * Timestamp (rec_ui) is: 2010-06-24 12:50:00
47 * Time sent into function is 12:00:00.
49 * Since the fuzz is rather small, we should get a NTP
50 * timestamp for the 12:00:00 time.
53 const u_int32 timestamp = 3486372600UL; // 2010-06-24 12:50:00Z
54 const u_int32 expected = 3486369600UL; // 2010-06-24 12:00:00Z
56 const int yday=175, hour=12, minute=0, second=0, tzoff=0;
61 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
62 &yearstart, &actual));
63 EXPECT_EQ(expected, actual);
66 TEST_F(clocktimeTest, TimeZoneOffset) {
68 * Timestamp (rec_ui) is: 2010-06-24 12:00:00 +0800
69 * (which is 2010-06-24 04:00:00Z)
71 * Time sent into function is 04:00:00 +0800
73 const u_int32 timestamp = 3486369600UL;
74 const u_int32 expected = timestamp;
76 const int yday=175, hour=4, minute=0, second=0, tzoff=8;
81 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
82 &yearstart, &actual));
83 EXPECT_EQ(expected, actual);
86 TEST_F(clocktimeTest, WrongYearStart) {
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!
92 const u_int32 timestamp = 3471418800UL;
93 const u_int32 expected = timestamp;
95 const int yday=2, hour=11, minute=0, second=0, tzoff=0;
97 u_long yearstart = 302024100UL; // Yearstart of 2009.
100 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
101 &yearstart, &actual));
102 EXPECT_EQ(expected, actual);
105 TEST_F(clocktimeTest, PreviousYear) {
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)
111 const u_int32 timestamp = 3471296400UL;
112 const u_int32 expected = 3471289200UL;
114 const int yday=365, hour=23, minute=0, second=0, tzoff=0;
116 u_long yearstart = 0;
119 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
120 &yearstart, &actual));
121 EXPECT_EQ(expected, actual);
124 TEST_F(clocktimeTest, NextYear) {
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)
130 const u_int32 timestamp = 3471289200UL;
131 const u_int32 expected = 3471296400UL;
133 const int yday=1, hour=1, minute=0, second=0, tzoff=0;
134 u_long yearstart = 0;
137 ASSERT_TRUE(clocktime(yday, hour, minute, second, tzoff, timestamp,
138 &yearstart, &actual));
139 EXPECT_EQ(expected, actual);
142 TEST_F(clocktimeTest, NoReasonableConversion) {
143 /* Timestamp is: 2010-01-02 11:00:00Z */
144 const u_int32 timestamp = 3471418800UL;
146 const int yday=100, hour=12, minute=0, second=0, tzoff=0;
147 u_long yearstart = 0;
150 ASSERT_FALSE(clocktime(yday, hour, minute, second, tzoff, timestamp,
151 &yearstart, &actual));
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 };
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)
182 ASSERT_LE(diff, (183u * SECSPERDAY));