xref: /freebsd/contrib/wpa/src/common/common_module_tests.c (revision 5b9c547c072b84410b50897cc53710c75b2f6b74)
1*5b9c547cSRui Paulo /*
2*5b9c547cSRui Paulo  * common module tests
3*5b9c547cSRui Paulo  * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
4*5b9c547cSRui Paulo  *
5*5b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
6*5b9c547cSRui Paulo  * See README for more details.
7*5b9c547cSRui Paulo  */
8*5b9c547cSRui Paulo 
9*5b9c547cSRui Paulo #include "utils/includes.h"
10*5b9c547cSRui Paulo 
11*5b9c547cSRui Paulo #include "utils/common.h"
12*5b9c547cSRui Paulo #include "ieee802_11_common.h"
13*5b9c547cSRui Paulo #include "wpa_common.h"
14*5b9c547cSRui Paulo 
15*5b9c547cSRui Paulo 
16*5b9c547cSRui Paulo struct ieee802_11_parse_test_data {
17*5b9c547cSRui Paulo 	u8 *data;
18*5b9c547cSRui Paulo 	size_t len;
19*5b9c547cSRui Paulo 	ParseRes result;
20*5b9c547cSRui Paulo 	int count;
21*5b9c547cSRui Paulo };
22*5b9c547cSRui Paulo 
23*5b9c547cSRui Paulo static const struct ieee802_11_parse_test_data parse_tests[] = {
24*5b9c547cSRui Paulo 	{ (u8 *) "", 0, ParseOK, 0 },
25*5b9c547cSRui Paulo 	{ (u8 *) " ", 1, ParseFailed, 0 },
26*5b9c547cSRui Paulo 	{ (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
27*5b9c547cSRui Paulo 	{ (u8 *) "\xff\x01", 2, ParseFailed, 0 },
28*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x03\x01\x02\x03", 5, ParseUnknown, 1 },
29*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x01\x02\x03\x04", 6, ParseUnknown, 1 },
30*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x50\xf2\x02", 6, ParseUnknown, 1 },
31*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\x02", 7, ParseOK, 1 },
32*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\xff", 7, ParseUnknown, 1 },
33*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x50\xf2\xff", 6, ParseUnknown, 1 },
34*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x50\x6f\x9a\xff", 6, ParseUnknown, 1 },
35*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x90\x4c\x33", 6, ParseOK, 1 },
36*5b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x90\x4c\xff\xdd\x04\x00\x90\x4c\x33", 12,
37*5b9c547cSRui Paulo 	  ParseUnknown, 2 },
38*5b9c547cSRui Paulo 	{ (u8 *) "\x10\x01\x00\x21\x00", 5, ParseOK, 2 },
39*5b9c547cSRui Paulo 	{ (u8 *) "\x24\x00", 2, ParseOK, 1 },
40*5b9c547cSRui Paulo 	{ (u8 *) "\x38\x00", 2, ParseOK, 1 },
41*5b9c547cSRui Paulo 	{ (u8 *) "\x54\x00", 2, ParseOK, 1 },
42*5b9c547cSRui Paulo 	{ (u8 *) "\x5a\x00", 2, ParseOK, 1 },
43*5b9c547cSRui Paulo 	{ (u8 *) "\x65\x00", 2, ParseOK, 1 },
44*5b9c547cSRui Paulo 	{ (u8 *) "\x65\x12\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11",
45*5b9c547cSRui Paulo 	  20, ParseOK, 1 },
46*5b9c547cSRui Paulo 	{ (u8 *) "\x6e\x00", 2, ParseOK, 1 },
47*5b9c547cSRui Paulo 	{ (u8 *) "\xc7\x00", 2, ParseOK, 1 },
48*5b9c547cSRui Paulo 	{ (u8 *) "\xc7\x01\x00", 3, ParseOK, 1 },
49*5b9c547cSRui Paulo 	{ NULL, 0, ParseOK, 0 }
50*5b9c547cSRui Paulo };
51*5b9c547cSRui Paulo 
52*5b9c547cSRui Paulo static int ieee802_11_parse_tests(void)
53*5b9c547cSRui Paulo {
54*5b9c547cSRui Paulo 	int i, ret = 0;
55*5b9c547cSRui Paulo 
56*5b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "ieee802_11_parse tests");
57*5b9c547cSRui Paulo 
58*5b9c547cSRui Paulo 	for (i = 0; parse_tests[i].data; i++) {
59*5b9c547cSRui Paulo 		const struct ieee802_11_parse_test_data *test;
60*5b9c547cSRui Paulo 		struct ieee802_11_elems elems;
61*5b9c547cSRui Paulo 		ParseRes res;
62*5b9c547cSRui Paulo 
63*5b9c547cSRui Paulo 		test = &parse_tests[i];
64*5b9c547cSRui Paulo 		res = ieee802_11_parse_elems(test->data, test->len, &elems, 1);
65*5b9c547cSRui Paulo 		if (res != test->result ||
66*5b9c547cSRui Paulo 		    ieee802_11_ie_count(test->data, test->len) != test->count) {
67*5b9c547cSRui Paulo 			wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed",
68*5b9c547cSRui Paulo 				   i);
69*5b9c547cSRui Paulo 			ret = -1;
70*5b9c547cSRui Paulo 		}
71*5b9c547cSRui Paulo 	}
72*5b9c547cSRui Paulo 
73*5b9c547cSRui Paulo 	if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL)
74*5b9c547cSRui Paulo 	{
75*5b9c547cSRui Paulo 		wpa_printf(MSG_ERROR,
76*5b9c547cSRui Paulo 			   "ieee802_11_vendor_ie_concat test failed");
77*5b9c547cSRui Paulo 		ret = -1;
78*5b9c547cSRui Paulo 	}
79*5b9c547cSRui Paulo 
80*5b9c547cSRui Paulo 	return ret;
81*5b9c547cSRui Paulo }
82*5b9c547cSRui Paulo 
83*5b9c547cSRui Paulo 
84*5b9c547cSRui Paulo struct rsn_ie_parse_test_data {
85*5b9c547cSRui Paulo 	u8 *data;
86*5b9c547cSRui Paulo 	size_t len;
87*5b9c547cSRui Paulo 	int result;
88*5b9c547cSRui Paulo };
89*5b9c547cSRui Paulo 
90*5b9c547cSRui Paulo static const struct rsn_ie_parse_test_data rsn_parse_tests[] = {
91*5b9c547cSRui Paulo 	{ (u8 *) "", 0, -1 },
92*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x00", 2, -1 },
93*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x01\x00", 4, 0 },
94*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x00", 4, -2 },
95*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x02\x00", 4, -2 },
96*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x01", 4, -2 },
97*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x00\x00", 5, -2 },
98*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x03\x01\x00\x00", 5, -3 },
99*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 },
100*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 },
101*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 },
102*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 },
103*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 },
104*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04",
105*5b9c547cSRui Paulo 	  14, 0 },
106*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04",
107*5b9c547cSRui Paulo 	  14, -4 },
108*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06",
109*5b9c547cSRui Paulo 	  14, -1 },
110*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08",
111*5b9c547cSRui Paulo 	  18, 0 },
112*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00",
113*5b9c547cSRui Paulo 	  15, -7 },
114*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00",
115*5b9c547cSRui Paulo 	  16, -6 },
116*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01",
117*5b9c547cSRui Paulo 	  16, -6 },
118*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01",
119*5b9c547cSRui Paulo 	  20, 0 },
120*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x01\x00\x0f\xac\x02",
121*5b9c547cSRui Paulo 	  24, 0 },
122*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00",
123*5b9c547cSRui Paulo 	  21, 0 },
124*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00",
125*5b9c547cSRui Paulo 	  22, 0 },
126*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00",
127*5b9c547cSRui Paulo 	  24, 0 },
128*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x16\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x01",
129*5b9c547cSRui Paulo 	  24, -9 },
130*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x00\x00\x00",
131*5b9c547cSRui Paulo 	  28, -10 },
132*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x1a\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x0f\xac\x06",
133*5b9c547cSRui Paulo 	  28, 0 },
134*5b9c547cSRui Paulo 	{ (u8 *) "\x30\x1c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00\x00\x00\x00\x0f\xac\x06\x01\x02",
135*5b9c547cSRui Paulo 	  30, 0 },
136*5b9c547cSRui Paulo 	{ NULL, 0, 0 }
137*5b9c547cSRui Paulo };
138*5b9c547cSRui Paulo 
139*5b9c547cSRui Paulo static int rsn_ie_parse_tests(void)
140*5b9c547cSRui Paulo {
141*5b9c547cSRui Paulo 	int i, ret = 0;
142*5b9c547cSRui Paulo 
143*5b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "rsn_ie_parse tests");
144*5b9c547cSRui Paulo 
145*5b9c547cSRui Paulo 	for (i = 0; rsn_parse_tests[i].data; i++) {
146*5b9c547cSRui Paulo 		const struct rsn_ie_parse_test_data *test;
147*5b9c547cSRui Paulo 		struct wpa_ie_data data;
148*5b9c547cSRui Paulo 
149*5b9c547cSRui Paulo 		test = &rsn_parse_tests[i];
150*5b9c547cSRui Paulo 		if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) !=
151*5b9c547cSRui Paulo 		    test->result) {
152*5b9c547cSRui Paulo 			wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i);
153*5b9c547cSRui Paulo 			ret = -1;
154*5b9c547cSRui Paulo 		}
155*5b9c547cSRui Paulo 	}
156*5b9c547cSRui Paulo 
157*5b9c547cSRui Paulo 	return ret;
158*5b9c547cSRui Paulo }
159*5b9c547cSRui Paulo 
160*5b9c547cSRui Paulo 
161*5b9c547cSRui Paulo int common_module_tests(void)
162*5b9c547cSRui Paulo {
163*5b9c547cSRui Paulo 	int ret = 0;
164*5b9c547cSRui Paulo 
165*5b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "common module tests");
166*5b9c547cSRui Paulo 
167*5b9c547cSRui Paulo 	if (ieee802_11_parse_tests() < 0 ||
168*5b9c547cSRui Paulo 	    rsn_ie_parse_tests() < 0)
169*5b9c547cSRui Paulo 		ret = -1;
170*5b9c547cSRui Paulo 
171*5b9c547cSRui Paulo 	return ret;
172*5b9c547cSRui Paulo }
173