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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
#include "sntptest.h"
extern "C" {
#include "kod_management.h"
#include "main.h"
#include "networking.h"
#include "ntp.h"
};
class mainTest : public sntptest {
protected:
::testing::AssertionResult LfpEquality(const l_fp &expected, const l_fp &actual) {
if (L_ISEQU(&expected, &actual)) {
return ::testing::AssertionSuccess();
} else {
return ::testing::AssertionFailure()
<< " expected: " << lfptoa(&expected, FRACTION_PREC)
<< " (" << expected.l_ui << "." << expected.l_uf << ")"
<< " but was: " << lfptoa(&actual, FRACTION_PREC)
<< " (" << actual.l_ui << "." << actual.l_uf << ")";
}
}
};
TEST_F(mainTest, GenerateUnauthenticatedPacket) {
pkt testpkt;
timeval xmt;
GETTIMEOFDAY(&xmt, NULL);
xmt.tv_sec += JAN_1970;
EXPECT_EQ(LEN_PKT_NOMAC,
generate_pkt(&testpkt, &xmt, 0, NULL));
EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
EXPECT_EQ(8, testpkt.ppoll);
l_fp expected_xmt, actual_xmt;
TVTOTS(&xmt, &expected_xmt);
NTOHL_FP(&testpkt.xmt, &actual_xmt);
EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
}
TEST_F(mainTest, GenerateAuthenticatedPacket) {
key testkey;
testkey.next = NULL;
testkey.key_id = 30;
testkey.key_len = 9;
memcpy(testkey.key_seq, "123456789", testkey.key_len);
memcpy(testkey.type, "MD5", 3);
pkt testpkt;
timeval xmt;
GETTIMEOFDAY(&xmt, NULL);
xmt.tv_sec += JAN_1970;
const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
EXPECT_EQ(EXPECTED_PKTLEN,
generate_pkt(&testpkt, &xmt, testkey.key_id, &testkey));
EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
EXPECT_EQ(8, testpkt.ppoll);
l_fp expected_xmt, actual_xmt;
TVTOTS(&xmt, &expected_xmt);
NTOHL_FP(&testpkt.xmt, &actual_xmt);
EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
EXPECT_EQ(testkey.key_id, ntohl(testpkt.exten[0]));
char expected_mac[MAX_MD5_LEN];
ASSERT_EQ(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac.
make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac));
EXPECT_TRUE(memcmp(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4) == 0);
}
TEST_F(mainTest, OffsetCalculationPositiveOffset) {
pkt rpkt;
rpkt.precision = -16; // 0,000015259
rpkt.rootdelay = HTONS_FP(DTOUFP(0.125));
rpkt.rootdisp = HTONS_FP(DTOUFP(0.25));
// Synch Distance: (0.125+0.25)/2.0 == 0.1875
l_fp reftime;
get_systime(&reftime);
HTONL_FP(&reftime, &rpkt.reftime);
l_fp tmp;
// T1 - Originate timestamp
tmp.l_ui = 1000000000UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.org);
// T2 - Receive timestamp
tmp.l_ui = 1000000001UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.rec);
// T3 - Transmit timestamp
tmp.l_ui = 1000000002UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.xmt);
// T4 - Destination timestamp as standard timeval
tmp.l_ui = 1000000001UL;
tmp.l_uf = 0UL;
timeval dst;
TSTOTV(&tmp, &dst);
dst.tv_sec -= JAN_1970;
double offset, precision, synch_distance;
offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
EXPECT_DOUBLE_EQ(1.25, offset);
EXPECT_DOUBLE_EQ(1. / ULOGTOD(16), precision);
// 1.1250150000000001 ?
EXPECT_DOUBLE_EQ(1.125015, synch_distance);
}
TEST_F(mainTest, OffsetCalculationNegativeOffset) {
pkt rpkt;
rpkt.precision = -1;
rpkt.rootdelay = HTONS_FP(DTOUFP(0.5));
rpkt.rootdisp = HTONS_FP(DTOUFP(0.5));
// Synch Distance is (0.5+0.5)/2.0, or 0.5
l_fp reftime;
get_systime(&reftime);
HTONL_FP(&reftime, &rpkt.reftime);
l_fp tmp;
// T1 - Originate timestamp
tmp.l_ui = 1000000001UL;
tmp.l_uf = 0UL;
HTONL_FP(&tmp, &rpkt.org);
// T2 - Receive timestamp
tmp.l_ui = 1000000000UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.rec);
// T3 - Transmit timestamp
tmp.l_ui = 1000000001UL;
tmp.l_uf = 2147483648UL;
HTONL_FP(&tmp, &rpkt.xmt);
// T4 - Destination timestamp as standard timeval
tmp.l_ui = 1000000003UL;
tmp.l_uf = 0UL;
timeval dst;
TSTOTV(&tmp, &dst);
dst.tv_sec -= JAN_1970;
double offset, precision, synch_distance;
offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &synch_distance);
EXPECT_DOUBLE_EQ(-1, offset);
EXPECT_DOUBLE_EQ(1. / ULOGTOD(1), precision);
EXPECT_DOUBLE_EQ(1.3333483333333334, synch_distance);
}
TEST_F(mainTest, HandleUnusableServer) {
pkt rpkt;
sockaddr_u host;
int rpktl;
ZERO(rpkt);
ZERO(host);
rpktl = SERVER_UNUSEABLE;
EXPECT_EQ(-1, handle_pkt(rpktl, &rpkt, &host, ""));
}
TEST_F(mainTest, HandleUnusablePacket) {
pkt rpkt;
sockaddr_u host;
int rpktl;
ZERO(rpkt);
ZERO(host);
rpktl = PACKET_UNUSEABLE;
EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
}
TEST_F(mainTest, HandleServerAuthenticationFailure) {
pkt rpkt;
sockaddr_u host;
int rpktl;
ZERO(rpkt);
ZERO(host);
rpktl = SERVER_AUTH_FAIL;
EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
}
TEST_F(mainTest, HandleKodDemobilize) {
const char * HOSTNAME = "192.0.2.1";
const char * REASON = "DENY";
pkt rpkt;
sockaddr_u host;
int rpktl;
kod_entry * entry;
rpktl = KOD_DEMOBILIZE;
ZERO(rpkt);
memcpy(&rpkt.refid, REASON, 4);
ZERO(host);
host.sa4.sin_family = AF_INET;
host.sa4.sin_addr.s_addr = inet_addr(HOSTNAME);
// Test that the KOD-entry is added to the database.
kod_init_kod_db("/dev/null", TRUE);
EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, HOSTNAME));
ASSERT_EQ(1, search_entry(HOSTNAME, &entry));
EXPECT_TRUE(memcmp(REASON, entry->type, 4) == 0);
}
TEST_F(mainTest, HandleKodRate) {
pkt rpkt;
sockaddr_u host;
int rpktl;
ZERO(rpkt);
ZERO(host);
rpktl = KOD_RATE;
EXPECT_EQ(1, handle_pkt(rpktl, &rpkt, &host, ""));
}
TEST_F(mainTest, HandleCorrectPacket) {
pkt rpkt;
sockaddr_u host;
int rpktl;
l_fp now;
// We don't want our testing code to actually change the system clock.
ASSERT_FALSE(ENABLED_OPT(STEP));
ASSERT_FALSE(ENABLED_OPT(SLEW));
get_systime(&now);
HTONL_FP(&now, &rpkt.reftime);
HTONL_FP(&now, &rpkt.org);
HTONL_FP(&now, &rpkt.rec);
HTONL_FP(&now, &rpkt.xmt);
rpktl = LEN_PKT_NOMAC;
ZERO(host);
AF(&host) = AF_INET;
EXPECT_EQ(0, handle_pkt(rpktl, &rpkt, &host, ""));
}
/* packetHandling.cpp */
|