xref: /freebsd/contrib/ntp/sntp/tests/packetProcessing.c (revision 9034852c84a13f0e3b5527e1c886ca94b2863b2b)
1276da39aSCy Schubert #include "config.h"
2276da39aSCy Schubert #include "sntptest.h"
3276da39aSCy Schubert #include "networking.h"
4276da39aSCy Schubert #include "ntp_stdlib.h"
5276da39aSCy Schubert #include "unity.h"
6276da39aSCy Schubert 
7*9034852cSGleb Smirnoff 
8276da39aSCy Schubert const char * Version = "stub unit test Version string";
9276da39aSCy Schubert 
10276da39aSCy Schubert // Hacks into the key database.
11276da39aSCy Schubert extern struct key* key_ptr;
12276da39aSCy Schubert extern int key_cnt;
13276da39aSCy Schubert 
14276da39aSCy Schubert 
15*9034852cSGleb Smirnoff void PrepareAuthenticationTest(int key_id,int key_len,const char* type,const void* key_seq);
16*9034852cSGleb Smirnoff void PrepareAuthenticationTestMD5(int key_id,int key_len,const void* key_seq);
17*9034852cSGleb Smirnoff void setUp(void);
18*9034852cSGleb Smirnoff void tearDown(void);
19*9034852cSGleb Smirnoff void test_TooShortLength(void);
20*9034852cSGleb Smirnoff void test_LengthNotMultipleOfFour(void);
21*9034852cSGleb Smirnoff void test_TooShortExtensionFieldLength(void);
22*9034852cSGleb Smirnoff void test_UnauthenticatedPacketReject(void);
23*9034852cSGleb Smirnoff void test_CryptoNAKPacketReject(void);
24*9034852cSGleb Smirnoff void test_AuthenticatedPacketInvalid(void);
25*9034852cSGleb Smirnoff void test_AuthenticatedPacketUnknownKey(void);
26*9034852cSGleb Smirnoff void test_ServerVersionTooOld(void);
27*9034852cSGleb Smirnoff void test_ServerVersionTooNew(void);
28*9034852cSGleb Smirnoff void test_NonWantedMode(void);
29*9034852cSGleb Smirnoff void test_KoDRate(void);
30*9034852cSGleb Smirnoff void test_KoDDeny(void);
31*9034852cSGleb Smirnoff void test_RejectUnsyncedServer(void);
32*9034852cSGleb Smirnoff void test_RejectWrongResponseServerMode(void);
33*9034852cSGleb Smirnoff void test_AcceptNoSentPacketBroadcastMode(void);
34*9034852cSGleb Smirnoff void test_CorrectUnauthenticatedPacket(void);
35*9034852cSGleb Smirnoff void test_CorrectAuthenticatedPacketMD5(void);
36*9034852cSGleb Smirnoff void test_CorrectAuthenticatedPacketSHA1(void);
37*9034852cSGleb Smirnoff 
38*9034852cSGleb Smirnoff 
39276da39aSCy Schubert static struct pkt testpkt;
40276da39aSCy Schubert static struct pkt testspkt;
41276da39aSCy Schubert static sockaddr_u testsock;
42276da39aSCy Schubert bool restoreKeyDb;
43276da39aSCy Schubert 
44*9034852cSGleb Smirnoff 
45*9034852cSGleb Smirnoff void
46*9034852cSGleb Smirnoff PrepareAuthenticationTest(int key_id,
47276da39aSCy Schubert 							   int key_len,
48276da39aSCy Schubert 							   const char* type,
49276da39aSCy Schubert 							   const void* key_seq) {
50276da39aSCy Schubert 	char str[25];
51*9034852cSGleb Smirnoff 	snprintf(str, 25, "%d", key_id);
52276da39aSCy Schubert 	ActivateOption("-a", str);
53276da39aSCy Schubert 
54276da39aSCy Schubert 	key_cnt = 1;
55*9034852cSGleb Smirnoff 	key_ptr = emalloc(sizeof(struct key));
56276da39aSCy Schubert 	key_ptr->next = NULL;
57276da39aSCy Schubert 	key_ptr->key_id = key_id;
58276da39aSCy Schubert 	key_ptr->key_len = key_len;
59276da39aSCy Schubert 	memcpy(key_ptr->type, "MD5", 3);
60276da39aSCy Schubert 
61276da39aSCy Schubert 	TEST_ASSERT_TRUE(key_len < sizeof(key_ptr->key_seq));
62276da39aSCy Schubert 
63276da39aSCy Schubert 	memcpy(key_ptr->key_seq, key_seq, key_ptr->key_len);
64276da39aSCy Schubert 	restoreKeyDb = true;
65276da39aSCy Schubert }
66276da39aSCy Schubert 
67*9034852cSGleb Smirnoff 
68*9034852cSGleb Smirnoff void
69*9034852cSGleb Smirnoff PrepareAuthenticationTestMD5(int key_id,
70276da39aSCy Schubert 							   int key_len,
71276da39aSCy Schubert 							   const void* key_seq) {
72276da39aSCy Schubert 	PrepareAuthenticationTest(key_id, key_len, "MD5", key_seq);
73276da39aSCy Schubert }
74276da39aSCy Schubert 
75*9034852cSGleb Smirnoff 
76*9034852cSGleb Smirnoff void
77*9034852cSGleb Smirnoff setUp(void) {
78276da39aSCy Schubert 
79276da39aSCy Schubert 	sntptest();
80276da39aSCy Schubert 	restoreKeyDb = false;
81276da39aSCy Schubert 
82276da39aSCy Schubert 	/* Initialize the test packet and socket,
83276da39aSCy Schubert 	 * so they contain at least some valid data. */
84276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING, NTP_VERSION,
85276da39aSCy Schubert 										MODE_SERVER);
86276da39aSCy Schubert 	testpkt.stratum = STRATUM_REFCLOCK;
87276da39aSCy Schubert 	memcpy(&testpkt.refid, "GPS\0", 4);
88276da39aSCy Schubert 
89276da39aSCy Schubert 	/* Set the origin timestamp of the received packet to the
90276da39aSCy Schubert 	 * same value as the transmit timestamp of the sent packet. */
91276da39aSCy Schubert 	l_fp tmp;
92276da39aSCy Schubert 	tmp.l_ui = 1000UL;
93276da39aSCy Schubert 	tmp.l_uf = 0UL;
94276da39aSCy Schubert 
95276da39aSCy Schubert 	HTONL_FP(&tmp, &testpkt.org);
96276da39aSCy Schubert 	HTONL_FP(&tmp, &testspkt.xmt);
97276da39aSCy Schubert }
98276da39aSCy Schubert 
99*9034852cSGleb Smirnoff 
100*9034852cSGleb Smirnoff void
101*9034852cSGleb Smirnoff tearDown(void) {
102276da39aSCy Schubert 
103276da39aSCy Schubert 	if (restoreKeyDb) {
104276da39aSCy Schubert 		key_cnt = 0;
105276da39aSCy Schubert 		free(key_ptr);
106276da39aSCy Schubert 		key_ptr = NULL;
107276da39aSCy Schubert 	}
108276da39aSCy Schubert 
109276da39aSCy Schubert 	sntptest_destroy(); //only on the final test!! if counter == 0 etc...
110276da39aSCy Schubert }
111276da39aSCy Schubert 
112276da39aSCy Schubert 
113276da39aSCy Schubert 
114*9034852cSGleb Smirnoff void
115*9034852cSGleb Smirnoff test_TooShortLength(void) {
116276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
117276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
118276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
119276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
120276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC - 1,
121276da39aSCy Schubert 						  MODE_BROADCAST, &testspkt, "UnitTest"));
122276da39aSCy Schubert }
123276da39aSCy Schubert 
124*9034852cSGleb Smirnoff 
125*9034852cSGleb Smirnoff void
126*9034852cSGleb Smirnoff test_LengthNotMultipleOfFour(void) {
127276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
128276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 6,
129276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
130276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
131276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC + 3,
132276da39aSCy Schubert 						  MODE_BROADCAST, &testspkt, "UnitTest"));
133276da39aSCy Schubert }
134276da39aSCy Schubert 
135*9034852cSGleb Smirnoff 
136*9034852cSGleb Smirnoff void
137*9034852cSGleb Smirnoff test_TooShortExtensionFieldLength(void) {
138276da39aSCy Schubert 	/* The lower 16-bits are the length of the extension field.
139276da39aSCy Schubert 	 * This lengths must be multiples of 4 bytes, which gives
140276da39aSCy Schubert 	 * a minimum of 4 byte extension field length. */
141276da39aSCy Schubert 	testpkt.exten[7] = htonl(3); // 3 bytes is too short.
142276da39aSCy Schubert 
143276da39aSCy Schubert 	/* We send in a pkt_len of header size + 4 byte extension
144276da39aSCy Schubert 	 * header + 24 byte MAC, this prevents the length error to
145276da39aSCy Schubert 	 * be caught at an earlier stage */
146276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC + 4 + 24;
147276da39aSCy Schubert 
148276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
149276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
150276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
151276da39aSCy Schubert }
152276da39aSCy Schubert 
153*9034852cSGleb Smirnoff 
154*9034852cSGleb Smirnoff void
155*9034852cSGleb Smirnoff test_UnauthenticatedPacketReject(void) {
156276da39aSCy Schubert 	//sntptest();
157276da39aSCy Schubert 	// Activate authentication option
158276da39aSCy Schubert 	ActivateOption("-a", "123");
159276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
160276da39aSCy Schubert 
161276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
162276da39aSCy Schubert 
163276da39aSCy Schubert 	// We demand authentication, but no MAC header is present.
164276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
165276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
166276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
167276da39aSCy Schubert }
168276da39aSCy Schubert 
169*9034852cSGleb Smirnoff 
170*9034852cSGleb Smirnoff void
171*9034852cSGleb Smirnoff test_CryptoNAKPacketReject(void) {
172276da39aSCy Schubert 	// Activate authentication option
173276da39aSCy Schubert 	ActivateOption("-a", "123");
174276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
175276da39aSCy Schubert 
176276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC + 4; // + 4 byte MAC = Crypto-NAK
177276da39aSCy Schubert 
178276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
179276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
180276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
181276da39aSCy Schubert }
182276da39aSCy Schubert 
183*9034852cSGleb Smirnoff 
184*9034852cSGleb Smirnoff void
185*9034852cSGleb Smirnoff test_AuthenticatedPacketInvalid(void) {
186276da39aSCy Schubert 	// Activate authentication option
187276da39aSCy Schubert 	PrepareAuthenticationTestMD5(50, 9, "123456789");
188276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
189276da39aSCy Schubert 
190276da39aSCy Schubert 	// Prepare the packet.
191276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
192276da39aSCy Schubert 
193276da39aSCy Schubert 	testpkt.exten[0] = htonl(50);
194276da39aSCy Schubert 	int mac_len = make_mac((char*)&testpkt, pkt_len,
195276da39aSCy Schubert 						   MAX_MD5_LEN, key_ptr,
196276da39aSCy Schubert 						   (char*)&testpkt.exten[1]);
197276da39aSCy Schubert 
198276da39aSCy Schubert 	pkt_len += 4 + mac_len;
199276da39aSCy Schubert 
200276da39aSCy Schubert 	// Now, alter the MAC so it becomes invalid.
201276da39aSCy Schubert 	testpkt.exten[1] += 1;
202276da39aSCy Schubert 
203276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
204276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
205276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
206276da39aSCy Schubert }
207276da39aSCy Schubert 
208*9034852cSGleb Smirnoff 
209*9034852cSGleb Smirnoff void
210*9034852cSGleb Smirnoff test_AuthenticatedPacketUnknownKey(void) {
211276da39aSCy Schubert 	// Activate authentication option
212276da39aSCy Schubert 	PrepareAuthenticationTestMD5(30, 9, "123456789");
213276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
214276da39aSCy Schubert 
215276da39aSCy Schubert 	// Prepare the packet. Observe that the Key-ID expected is 30,
216276da39aSCy Schubert 	// but the packet has a key id of 50.
217276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
218276da39aSCy Schubert 
219276da39aSCy Schubert 	testpkt.exten[0] = htonl(50);
220276da39aSCy Schubert 	int mac_len = make_mac((char*)&testpkt, pkt_len,
221276da39aSCy Schubert 						   MAX_MD5_LEN, key_ptr,
222276da39aSCy Schubert 						   (char*)&testpkt.exten[1]);
223276da39aSCy Schubert 	pkt_len += 4 + mac_len;
224276da39aSCy Schubert 
225276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_AUTH_FAIL,
226276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
227276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
228276da39aSCy Schubert }
229276da39aSCy Schubert 
230*9034852cSGleb Smirnoff 
231*9034852cSGleb Smirnoff void
232*9034852cSGleb Smirnoff test_ServerVersionTooOld(void) {
233276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
234276da39aSCy Schubert 
235276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
236276da39aSCy Schubert 										NTP_OLDVERSION - 1,
237276da39aSCy Schubert 										MODE_CLIENT);
238276da39aSCy Schubert 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.li_vn_mode) < NTP_OLDVERSION);
239276da39aSCy Schubert 
240276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
241276da39aSCy Schubert 
242276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
243276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
244276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
245276da39aSCy Schubert }
246276da39aSCy Schubert 
247*9034852cSGleb Smirnoff 
248*9034852cSGleb Smirnoff void
249*9034852cSGleb Smirnoff test_ServerVersionTooNew(void) {
250276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
251276da39aSCy Schubert 
252276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
253276da39aSCy Schubert 										NTP_VERSION + 1,
254276da39aSCy Schubert 										MODE_CLIENT);
255276da39aSCy Schubert 	TEST_ASSERT_TRUE(PKT_VERSION(testpkt.li_vn_mode) > NTP_VERSION);
256276da39aSCy Schubert 
257276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
258276da39aSCy Schubert 
259276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
260276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
261276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
262276da39aSCy Schubert }
263276da39aSCy Schubert 
264*9034852cSGleb Smirnoff 
265*9034852cSGleb Smirnoff void
266*9034852cSGleb Smirnoff test_NonWantedMode(void) {
267276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
268276da39aSCy Schubert 
269276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
270276da39aSCy Schubert 										NTP_VERSION,
271276da39aSCy Schubert 										MODE_CLIENT);
272276da39aSCy Schubert 
273276da39aSCy Schubert 	// The packet has a mode of MODE_CLIENT, but process_pkt expects MODE_SERVER
274276da39aSCy Schubert 
275276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
276276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
277276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
278276da39aSCy Schubert }
279276da39aSCy Schubert 
280*9034852cSGleb Smirnoff 
281276da39aSCy Schubert /* Tests bug 1597 */
282*9034852cSGleb Smirnoff void
283*9034852cSGleb Smirnoff test_KoDRate(void) {
284276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
285276da39aSCy Schubert 
286276da39aSCy Schubert 	testpkt.stratum = STRATUM_PKT_UNSPEC;
287276da39aSCy Schubert 	memcpy(&testpkt.refid, "RATE", 4);
288276da39aSCy Schubert 
289276da39aSCy Schubert 	TEST_ASSERT_EQUAL(KOD_RATE,
290276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
291276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
292276da39aSCy Schubert }
293276da39aSCy Schubert 
294*9034852cSGleb Smirnoff 
295*9034852cSGleb Smirnoff void
296*9034852cSGleb Smirnoff test_KoDDeny(void) {
297276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
298276da39aSCy Schubert 
299276da39aSCy Schubert 	testpkt.stratum = STRATUM_PKT_UNSPEC;
300276da39aSCy Schubert 	memcpy(&testpkt.refid, "DENY", 4);
301276da39aSCy Schubert 
302276da39aSCy Schubert 	TEST_ASSERT_EQUAL(KOD_DEMOBILIZE,
303276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
304276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
305276da39aSCy Schubert }
306276da39aSCy Schubert 
307*9034852cSGleb Smirnoff 
308*9034852cSGleb Smirnoff void
309*9034852cSGleb Smirnoff test_RejectUnsyncedServer(void) {
310276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
311276da39aSCy Schubert 
312276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOTINSYNC,
313276da39aSCy Schubert 										NTP_VERSION,
314276da39aSCy Schubert 										MODE_SERVER);
315276da39aSCy Schubert 
316276da39aSCy Schubert 	TEST_ASSERT_EQUAL(SERVER_UNUSEABLE,
317276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
318276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
319276da39aSCy Schubert }
320276da39aSCy Schubert 
321*9034852cSGleb Smirnoff 
322*9034852cSGleb Smirnoff void
323*9034852cSGleb Smirnoff test_RejectWrongResponseServerMode(void) {
324276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
325276da39aSCy Schubert 
326276da39aSCy Schubert 	l_fp tmp;
327276da39aSCy Schubert 	tmp.l_ui = 1000UL;
328276da39aSCy Schubert 	tmp.l_uf = 0UL;
329276da39aSCy Schubert 	HTONL_FP(&tmp, &testpkt.org);
330276da39aSCy Schubert 
331276da39aSCy Schubert 	tmp.l_ui = 2000UL;
332276da39aSCy Schubert 	tmp.l_uf = 0UL;
333276da39aSCy Schubert 	HTONL_FP(&tmp, &testspkt.xmt);
334276da39aSCy Schubert 
335276da39aSCy Schubert 	TEST_ASSERT_EQUAL(PACKET_UNUSEABLE,
336276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
337276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
338276da39aSCy Schubert }
339276da39aSCy Schubert 
340*9034852cSGleb Smirnoff 
341*9034852cSGleb Smirnoff void
342*9034852cSGleb Smirnoff test_AcceptNoSentPacketBroadcastMode(void) {
343276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
344276da39aSCy Schubert 
345276da39aSCy Schubert 	testpkt.li_vn_mode = PKT_LI_VN_MODE(LEAP_NOWARNING,
346276da39aSCy Schubert 					    NTP_VERSION,
347276da39aSCy Schubert 					    MODE_BROADCAST);
348276da39aSCy Schubert 
349276da39aSCy Schubert 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
350276da39aSCy Schubert 		  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
351276da39aSCy Schubert 			      MODE_BROADCAST, NULL, "UnitTest"));
352276da39aSCy Schubert }
353276da39aSCy Schubert 
354*9034852cSGleb Smirnoff 
355*9034852cSGleb Smirnoff void
356*9034852cSGleb Smirnoff test_CorrectUnauthenticatedPacket(void) {
357276da39aSCy Schubert 	TEST_ASSERT_FALSE(ENABLED_OPT(AUTHENTICATION));
358276da39aSCy Schubert 
359276da39aSCy Schubert 	TEST_ASSERT_EQUAL(LEN_PKT_NOMAC,
360276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, LEN_PKT_NOMAC,
361276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
362276da39aSCy Schubert }
363276da39aSCy Schubert 
364*9034852cSGleb Smirnoff 
365*9034852cSGleb Smirnoff void
366*9034852cSGleb Smirnoff test_CorrectAuthenticatedPacketMD5(void) {
367276da39aSCy Schubert 	PrepareAuthenticationTestMD5(10, 15, "123456789abcdef");
368276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
369276da39aSCy Schubert 
370276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
371276da39aSCy Schubert 
372276da39aSCy Schubert 	// Prepare the packet.
373276da39aSCy Schubert 	testpkt.exten[0] = htonl(10);
374276da39aSCy Schubert 	int mac_len = make_mac((char*)&testpkt, pkt_len,
375276da39aSCy Schubert 						   MAX_MD5_LEN, key_ptr,
376276da39aSCy Schubert 						   (char*)&testpkt.exten[1]);
377276da39aSCy Schubert 
378276da39aSCy Schubert 	pkt_len += 4 + mac_len;
379276da39aSCy Schubert 
380276da39aSCy Schubert 	TEST_ASSERT_EQUAL(pkt_len,
381276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
382276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
383276da39aSCy Schubert 
384276da39aSCy Schubert }
385276da39aSCy Schubert 
386*9034852cSGleb Smirnoff 
387*9034852cSGleb Smirnoff void
388*9034852cSGleb Smirnoff test_CorrectAuthenticatedPacketSHA1(void) {
389276da39aSCy Schubert 	PrepareAuthenticationTest(20, 15, "SHA1", "abcdefghijklmno");
390276da39aSCy Schubert 	TEST_ASSERT_TRUE(ENABLED_OPT(AUTHENTICATION));
391276da39aSCy Schubert 
392276da39aSCy Schubert 	int pkt_len = LEN_PKT_NOMAC;
393276da39aSCy Schubert 
394276da39aSCy Schubert 	// Prepare the packet.
395276da39aSCy Schubert 	testpkt.exten[0] = htonl(20);
396276da39aSCy Schubert 	int mac_len = make_mac((char*)&testpkt, pkt_len,
397276da39aSCy Schubert 						   MAX_MAC_LEN, key_ptr,
398276da39aSCy Schubert 						   (char*)&testpkt.exten[1]);
399276da39aSCy Schubert 
400276da39aSCy Schubert 	pkt_len += 4 + mac_len;
401276da39aSCy Schubert 
402276da39aSCy Schubert 	TEST_ASSERT_EQUAL(pkt_len,
403276da39aSCy Schubert 			  process_pkt(&testpkt, &testsock, pkt_len,
404276da39aSCy Schubert 						  MODE_SERVER, &testspkt, "UnitTest"));
405276da39aSCy Schubert }
406