aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/packetProcessing.c
blob: 660b5b6e20f15916fbb6a77046d99899a7c609c6 (plain) (blame)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "config.h"

#include "sntptest.h"
#include "networking.h"
#include "ntp_stdlib.h"
#include "unity.h"


const char * Version = "stub unit test Version string";

/* Hacks into the key database. */
extern struct key* key_ptr;
extern int key_cnt;


void PrepareAuthenticationTest(int key_id,int key_len,const char* type,const void* key_seq);
void PrepareAuthenticationTestMD5(int key_id,int key_len,const void* key_seq);
void setUp(void);
void tearDown(void);
void test_TooShortLength(void);
void test_LengthNotMultipleOfFour(void);
void test_TooShortExtensionFieldLength(void);
void test_UnauthenticatedPacketReject(void);
void test_CryptoNAKPacketReject(void);
void test_AuthenticatedPacketInvalid(void);
void test_AuthenticatedPacketUnknownKey(void);
void test_ServerVersionTooOld(void);
void test_ServerVersionTooNew(void);
void test_NonWantedMode(void);
void test_KoDRate(void);
void test_KoDDeny(void);
void test_RejectUnsyncedServer(void);
void test_RejectWrongResponseServerMode(void);
void test_AcceptNoSentPacketBroadcastMode(void);
void test_CorrectUnauthenticatedPacket(void);
void test_CorrectAuthenticatedPacketMD5(void);
void test_CorrectAuthenticatedPacketSHA1(void);

/* [Bug 2998] There are some issues whith the definition of 'struct pkt'
 * when AUTOKEY is undefined -- the formal struct is too small to hold
 * all the extension fields that are going to be tested. We have to make
 * sure we have the extra bytes, or the test yield undefined results due
 * to buffer overrun. 
 */
#ifndef AUTOKEY
# define EXTRA_BUFSIZE 256
#else
# define EXTRA_BUFSIZE 0
#endif

union tpkt {
	struct pkt p;
	u_char     b[sizeof(struct pkt) + EXTRA_BUFSIZE];
}; 

static union tpkt testpkt;
static union tpkt testspkt;
static sockaddr_u testsock;
bool restoreKeyDb;


void
PrepareAuthenticationTest(
	int		key_id,
	int		key_len,
	const char *	type,
	const void *	key_seq
	)
{
	char str[25];
	snprintf(str, 25, "%d", key_id);
	ActivateOption("-a", str);

	key_cnt = 1;
	key_ptr = emalloc(sizeof(struct key));
	key_ptr->next = NULL;
	key_ptr->key_id = key_id;
	key_ptr->key_len = key_len;
	memcpy(key_ptr->type, "MD5", 3);

	TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));

	memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
	restoreKeyDb = true;
}


void
PrepareAuthenticationTestMD5(
	int 		key_id,
	int 		key_len,
	const void *	key_seq
	)
{
	PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
}


void
setUp(void)
{

	sntptest();
	restoreKeyDb = false;

	/* Initialize the test packet and socket,
	 * so they contain at least some valid data.
	 */
	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
										MODE_SERVER);
	testpkt.p.stratum = STRATUM_REFCLOCK;
	memcpy(&testpkt.p.refid, "GPS\0", 4);

	/* Set the origin timestamp of the received packet to the
	 * same value as the transmit timestamp of the sent packet.
	 */
	l_fp tmp;
	tmp.l_ui = 1000UL;
	tmp.l_uf = 0UL;

	HTONL_FP(&tmp, &testpkt.p.org);
	HTONL_FP(&tmp, &testspkt.p.xmt);
}


void
tearDown(void)
{	
	if (restoreKeyDb) {
		key_cnt = 0;
		free(key_ptr);
		key_ptr = NULL;
	}

	sntptest_destroy(); /* only on the final test!! if counter == 0 etc... */
}


void
test_TooShortLength(void)
{
	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC - 1,
				      MODE_BROADCAST, &testspkt.p, "UnitTest"));
}


void
test_LengthNotMultipleOfFour(void)
{
	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 6,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC + 3,
				      MODE_BROADCAST, &testspkt.p, "UnitTest"));
}


void
test_TooShortExtensionFieldLength(void)
{
	/* [Bug 2998] We have to get around the formal specification of
	 * the extension field if AUTOKEY is undefined. (At least CLANG
	 * issues a warning in this case. It's just a warning, but
	 * still...
	 */
	uint32_t * pe = testpkt.p.exten + 7;
	
	/* The lower 16-bits are the length of the extension field.
	 * This lengths must be multiples of 4 bytes, which gives
	 * a minimum of 4 byte extension field length.
	 */
	*pe = htonl(3); /* 3 bytes is too short. */

	/* We send in a pkt_len of header size + 4 byte extension
	 * header + 24 byte MAC, this prevents the length error to
	 * be caught at an earlier stage
	 */
	int pkt_len = LEN_PKT_NOMAC + 4 + 24;

	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_UnauthenticatedPacketReject(void)
{
	/* Activate authentication option */
	ActivateOption("-a", "123");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));

	int pkt_len = LEN_PKT_NOMAC;

	/* We demand authentication, but no MAC header is present. */
	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_CryptoNAKPacketReject(void)
{
	/* Activate authentication option */
	ActivateOption("-a", "123");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));

	int pkt_len = LEN_PKT_NOMAC + 4; /* + 4 byte MAC = Crypto-NAK */

	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_AuthenticatedPacketInvalid(void)
{
	/* Activate authentication option */
	PrepareAuthenticationTestMD5(50, 9, "123456789");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
	
	/* Prepare the packet. */
	int pkt_len = LEN_PKT_NOMAC;

	testpkt.p.exten[0] = htonl(50);
	int mac_len = make_mac(&testpkt.p, pkt_len,
			       MAX_MD5_LEN, key_ptr,
			       &testpkt.p.exten[1]);

	pkt_len += 4 + mac_len;

	/* Now, alter the MAC so it becomes invalid. */
	testpkt.p.exten[1] += 1;

	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_AuthenticatedPacketUnknownKey(void)
{
	/* Activate authentication option */
	PrepareAuthenticationTestMD5(30, 9, "123456789");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
	
	/* Prepare the packet. Note that the Key-ID expected is 30, but
	 * the packet has a key id of 50.
	 */
	int pkt_len = LEN_PKT_NOMAC;

	testpkt.p.exten[0] = htonl(50);
	int mac_len = make_mac(&testpkt.p, pkt_len,
			       MAX_MD5_LEN, key_ptr,
			       &testpkt.p.exten[1]);
	pkt_len += 4 + mac_len;

	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_ServerVersionTooOld(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					      NTP_OLDVERSION - 1,
					      MODE_CLIENT);
	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) < NTP_OLDVERSION);

	int pkt_len = LEN_PKT_NOMAC;
	
	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_ServerVersionTooNew(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					      NTP_VERSION + 1,
					      MODE_CLIENT);
	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.p.li_vn_mode) > NTP_VERSION);

	int pkt_len = LEN_PKT_NOMAC;

	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_NonWantedMode(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					      NTP_VERSION,
					      MODE_CLIENT);

	/* The packet has a mode of MODE_CLIENT, but process_pkt expects
	 * MODE_SERVER
	 */
	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


/* Tests bug 1597 */
void
test_KoDRate(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.stratum = STRATUM_PKT_UNSPEC;
	memcpy(&testpkt.p.refid, "RATE", 4);

	TEST_ASSERT_EQUAL(KOD_RATE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_KoDDeny(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.stratum = STRATUM_PKT_UNSPEC;
	memcpy(&testpkt.p.refid, "DENY", 4);

	TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_RejectUnsyncedServer(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
					      NTP_VERSION,
					      MODE_SERVER);

	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_RejectWrongResponseServerMode(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	l_fp tmp;
	tmp.l_ui = 1000UL;
	tmp.l_uf = 0UL;
	HTONL_FP(&tmp, &testpkt.p.org);

	tmp.l_ui = 2000UL;
	tmp.l_uf = 0UL;
	HTONL_FP(&tmp, &testspkt.p.xmt);

	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_AcceptNoSentPacketBroadcastMode(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	testpkt.p.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
					      NTP_VERSION,
					      MODE_BROADCAST);

	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
		  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
			      MODE_BROADCAST, NULL, "UnitTest"));
}


void
test_CorrectUnauthenticatedPacket(void)
{
	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));

	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
			  process_pkt(&testpkt.p, &testsock, LEN_PKT_NOMAC,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_CorrectAuthenticatedPacketMD5(void)
{
	PrepareAuthenticationTestMD5(10, 15, "123456789abcdef");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));

	int pkt_len = LEN_PKT_NOMAC;

	/* Prepare the packet. */
	testpkt.p.exten[0] = htonl(10);
	int mac_len = make_mac(&testpkt.p, pkt_len,
			       MAX_MD5_LEN, key_ptr,
			       &testpkt.p.exten[1]);

	pkt_len += 4 + mac_len;

	TEST_ASSERT_EQUAL(pkt_len,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}


void
test_CorrectAuthenticatedPacketSHA1(void)
{
	PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));

	int pkt_len = LEN_PKT_NOMAC;

	/* Prepare the packet. */
	testpkt.p.exten[0] = htonl(20);
	int mac_len = make_mac(&testpkt.p, pkt_len,
			       MAX_MAC_LEN, key_ptr,
			       &testpkt.p.exten[1]);

	pkt_len += 4 + mac_len;

	TEST_ASSERT_EQUAL(pkt_len,
			  process_pkt(&testpkt.p, &testsock, pkt_len,
				      MODE_SERVER, &testspkt.p, "UnitTest"));
}