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