4 #include "networking.h"
5 #include "ntp_stdlib.h"
9 const char * Version = "stub unit test Version string";
11 /* Hacks into the key database. */
12 extern struct key* key_ptr;
16 void PrepareAuthenticationTest(int key_id,int key_len,const char* type,const void* key_seq);
17 void PrepareAuthenticationTestMD5(int key_id,int key_len,const void* key_seq);
20 void test_TooShortLength(void);
21 void test_LengthNotMultipleOfFour(void);
22 void test_TooShortExtensionFieldLength(void);
23 void test_UnauthenticatedPacketReject(void);
24 void test_CryptoNAKPacketReject(void);
25 void test_AuthenticatedPacketInvalid(void);
26 void test_AuthenticatedPacketUnknownKey(void);
27 void test_ServerVersionTooOld(void);
28 void test_ServerVersionTooNew(void);
29 void test_NonWantedMode(void);
30 void test_KoDRate(void);
31 void test_KoDDeny(void);
32 void test_RejectUnsyncedServer(void);
33 void test_RejectWrongResponseServerMode(void);
34 void test_AcceptNoSentPacketBroadcastMode(void);
35 void test_CorrectUnauthenticatedPacket(void);
36 void test_CorrectAuthenticatedPacketMD5(void);
37 void test_CorrectAuthenticatedPacketSHA1(void);
39 /* [Bug 2998] There are some issues whith the definition of 'struct pkt'
40 * when AUTOKEY is undefined -- the formal struct is too small to hold
41 * all the extension fields that are going to be tested. We have to make
42 * sure we have the extra bytes, or the test yield undefined results due
46 # define EXTRA_BUFSIZE 256
48 # define EXTRA_BUFSIZE 0
53 u_char b[sizeof(struct pkt) + EXTRA_BUFSIZE];
56 static union tpkt testpkt;
57 static union tpkt testspkt;
58 static sockaddr_u testsock;
63 PrepareAuthenticationTest(
71 snprintf(str, 25, "%d", key_id);
72 ActivateOption("-a", str);
75 key_ptr = emalloc(sizeof(struct key));
77 key_ptr->key_id = key_id;
78 key_ptr->key_len = key_len;
79 memcpy(key_ptr->type, "MD5", 3);
81 TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
83 memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
89 PrepareAuthenticationTestMD5(
95 PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
104 restoreKeyDb = false;
106 /* Initialize the test packet and socket,
107 * so they contain at least some valid data.
109 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
111 testpkt.p.stratum = STRATUM_REFCLOCK;
112 memcpy(&testpkt.p.refid, "GPS\0", 4);
114 /* Set the origin timestamp of the received packet to the
115 * same value as the transmit timestamp of the sent packet.
121 HTONL_FP(&tmp, &testpkt.p.org);
122 HTONL_FP(&tmp, &testspkt.p.xmt);
135 sntptest_destroy(); /* only on the final test!! if counter == 0 etc... */
140 test_TooShortLength(void)
142 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
143 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
144 MODE_SERVER, &testspkt.p, "UnitTest"));
145 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
146 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
147 MODE_BROADCAST, &testspkt.p, "UnitTest"));
152 test_LengthNotMultipleOfFour(void)
154 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
155 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 6,
156 MODE_SERVER, &testspkt.p, "UnitTest"));
157 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
158 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 3,
159 MODE_BROADCAST, &testspkt.p, "UnitTest"));
164 test_TooShortExtensionFieldLength(void)
166 /* [Bug 2998] We have to get around the formal specification of
167 * the extension field if AUTOKEY is undefined. (At least CLANG
168 * issues a warning in this case. It's just a warning, but
171 uint32_t * pe = testpkt.p.exten + 7;
173 /* The lower 16-bits are the length of the extension field.
174 * This lengths must be multiples of 4 bytes, which gives
175 * a minimum of 4 byte extension field length.
177 *pe = htonl(3); /* 3 bytes is too short. */
179 /* We send in a pkt_len of header size + 4 byte extension
180 * header + 24 byte MAC, this prevents the length error to
181 * be caught at an earlier stage
183 int pkt_len = LEN_PKT_NOMAC + 4 + 24;
185 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
186 process_pkt(&testpkt.p, &testsock, pkt_len,
187 MODE_SERVER, &testspkt.p, "UnitTest"));
192 test_UnauthenticatedPacketReject(void)
194 /* Activate authentication option */
195 ActivateOption("-a", "123");
196 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
198 int pkt_len = LEN_PKT_NOMAC;
200 /* We demand authentication, but no MAC header is present. */
201 TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
202 process_pkt(&testpkt.p, &testsock, pkt_len,
203 MODE_SERVER, &testspkt.p, "UnitTest"));
208 test_CryptoNAKPacketReject(void)
210 /* Activate authentication option */
211 ActivateOption("-a", "123");
212 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
214 int pkt_len = LEN_PKT_NOMAC + 4; /* + 4 byte MAC = Crypto-NAK */
216 TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
217 process_pkt(&testpkt.p, &testsock, pkt_len,
218 MODE_SERVER, &testspkt.p, "UnitTest"));
223 test_AuthenticatedPacketInvalid(void)
225 /* Activate authentication option */
226 PrepareAuthenticationTestMD5(50, 9, "123456789");
227 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
229 /* Prepare the packet. */
230 int pkt_len = LEN_PKT_NOMAC;
232 testpkt.p.exten[0] = htonl(50);
233 int mac_len = make_mac(&testpkt.p, pkt_len,
234 MAX_MD5_LEN, key_ptr,
235 &testpkt.p.exten[1]);
237 pkt_len += 4 + mac_len;
239 /* Now, alter the MAC so it becomes invalid. */
240 testpkt.p.exten[1] += 1;
242 TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
243 process_pkt(&testpkt.p, &testsock, pkt_len,
244 MODE_SERVER, &testspkt.p, "UnitTest"));
249 test_AuthenticatedPacketUnknownKey(void)
251 /* Activate authentication option */
252 PrepareAuthenticationTestMD5(30, 9, "123456789");
253 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
255 /* Prepare the packet. Note that the Key-ID expected is 30, but
256 * the packet has a key id of 50.
258 int pkt_len = LEN_PKT_NOMAC;
260 testpkt.p.exten[0] = htonl(50);
261 int mac_len = make_mac(&testpkt.p, pkt_len,
262 MAX_MD5_LEN, key_ptr,
263 &testpkt.p.exten[1]);
264 pkt_len += 4 + mac_len;
266 TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
267 process_pkt(&testpkt.p, &testsock, pkt_len,
268 MODE_SERVER, &testspkt.p, "UnitTest"));
273 test_ServerVersionTooOld(void)
275 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
277 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
280 TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) < NTP_OLDVERSION);
282 int pkt_len = LEN_PKT_NOMAC;
284 TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
285 process_pkt(&testpkt.p, &testsock, pkt_len,
286 MODE_SERVER, &testspkt.p, "UnitTest"));
291 test_ServerVersionTooNew(void)
293 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
295 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
298 TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) > NTP_VERSION);
300 int pkt_len = LEN_PKT_NOMAC;
302 TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
303 process_pkt(&testpkt.p, &testsock, pkt_len,
304 MODE_SERVER, &testspkt.p, "UnitTest"));
309 test_NonWantedMode(void)
311 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
313 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
317 /* The packet has a mode of MODE_CLIENT, but process_pkt expects
320 TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
321 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
322 MODE_SERVER, &testspkt.p, "UnitTest"));
330 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
332 testpkt.p.stratum = STRATUM_PKT_UNSPEC;
333 memcpy(&testpkt.p.refid, "RATE", 4);
335 TEST_ASSERT_EQUAL(KOD_RATE,
336 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
337 MODE_SERVER, &testspkt.p, "UnitTest"));
344 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
346 testpkt.p.stratum = STRATUM_PKT_UNSPEC;
347 memcpy(&testpkt.p.refid, "DENY", 4);
349 TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
350 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
351 MODE_SERVER, &testspkt.p, "UnitTest"));
356 test_RejectUnsyncedServer(void)
358 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
360 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
364 TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
365 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
366 MODE_SERVER, &testspkt.p, "UnitTest"));
371 test_RejectWrongResponseServerMode(void)
373 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
378 HTONL_FP(&tmp, &testpkt.p.org);
382 HTONL_FP(&tmp, &testspkt.p.xmt);
384 TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
385 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
386 MODE_SERVER, &testspkt.p, "UnitTest"));
391 test_AcceptNoSentPacketBroadcastMode(void)
393 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
395 testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
399 TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
400 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
401 MODE_BROADCAST, NULL, "UnitTest"));
406 test_CorrectUnauthenticatedPacket(void)
408 TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
410 TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
411 process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
412 MODE_SERVER, &testspkt.p, "UnitTest"));
417 test_CorrectAuthenticatedPacketMD5(void)
419 PrepareAuthenticationTestMD5(10, 15, "123456789abcdef");
420 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
422 int pkt_len = LEN_PKT_NOMAC;
424 /* Prepare the packet. */
425 testpkt.p.exten[0] = htonl(10);
426 int mac_len = make_mac(&testpkt.p, pkt_len,
427 MAX_MD5_LEN, key_ptr,
428 &testpkt.p.exten[1]);
430 pkt_len += 4 + mac_len;
432 TEST_ASSERT_EQUAL(pkt_len,
433 process_pkt(&testpkt.p, &testsock, pkt_len,
434 MODE_SERVER, &testspkt.p, "UnitTest"));
439 test_CorrectAuthenticatedPacketSHA1(void)
441 PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
442 TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
444 int pkt_len = LEN_PKT_NOMAC;
446 /* Prepare the packet. */
447 testpkt.p.exten[0] = htonl(20);
448 int mac_len = make_mac(&testpkt.p, pkt_len,
449 MAX_MAC_LEN, key_ptr,
450 &testpkt.p.exten[1]);
452 pkt_len += 4 + mac_len;
454 TEST_ASSERT_EQUAL(pkt_len,
455 process_pkt(&testpkt.p, &testsock, pkt_len,
456 MODE_SERVER, &testspkt.p, "UnitTest"));