1/*
2 * common module tests
3 * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/module_tests.h"
13#include "ieee802_11_common.h"
14#include "ieee802_11_defs.h"
15#include "gas.h"
16#include "wpa_common.h"
17
18
19struct ieee802_11_parse_test_data {
20	u8 *data;
21	size_t len;
22	ParseRes result;
23	int count;
24};
25
26static const struct ieee802_11_parse_test_data parse_tests[] = {
27	{ (u8 *) "", 0, ParseOK, 0 },
28	{ (u8 *) " ", 1, ParseFailed, 0 },
29	{ (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
30	{ (u8 *) "\xff\x01", 2, ParseFailed, 0 },
31	{ (u8 *) "\xdd\x03\x01\x02\x03", 5, ParseUnknown, 1 },
32	{ (u8 *) "\xdd\x04\x01\x02\x03\x04", 6, ParseUnknown, 1 },
33	{ (u8 *) "\xdd\x04\x00\x50\xf2\x02", 6, ParseUnknown, 1 },
34	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\x02", 7, ParseOK, 1 },
35	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\xff", 7, ParseUnknown, 1 },
36	{ (u8 *) "\xdd\x04\x00\x50\xf2\xff", 6, ParseUnknown, 1 },
37	{ (u8 *) "\xdd\x04\x50\x6f\x9a\xff", 6, ParseUnknown, 1 },
38	{ (u8 *) "\xdd\x04\x00\x90\x4c\x33", 6, ParseOK, 1 },
39	{ (u8 *) "\xdd\x04\x00\x90\x4c\xff\xdd\x04\x00\x90\x4c\x33", 12,
40	  ParseUnknown, 2 },
41	{ (u8 *) "\x10\x01\x00\x21\x00", 5, ParseOK, 2 },
42	{ (u8 *) "\x24\x00", 2, ParseOK, 1 },
43	{ (u8 *) "\x38\x00", 2, ParseOK, 1 },
44	{ (u8 *) "\x54\x00", 2, ParseOK, 1 },
45	{ (u8 *) "\x5a\x00", 2, ParseOK, 1 },
46	{ (u8 *) "\x65\x00", 2, ParseOK, 1 },
47	{ (u8 *) "\x65\x12\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11",
48	  20, ParseOK, 1 },
49	{ (u8 *) "\x6e\x00", 2, ParseOK, 1 },
50	{ (u8 *) "\xc7\x00", 2, ParseOK, 1 },
51	{ (u8 *) "\xc7\x01\x00", 3, ParseOK, 1 },
52	{ (u8 *) "\x03\x00\x2a\x00\x36\x00\x37\x00\x38\x00\x2d\x00\x3d\x00\xbf\x00\xc0\x00",
53	  18, ParseOK, 9 },
54	{ (u8 *) "\x8b\x00", 2, ParseOK, 1 },
55	{ (u8 *) "\xdd\x04\x00\x90\x4c\x04", 6, ParseUnknown, 1 },
56	{ (u8 *) "\xed\x00", 2, ParseOK, 1 },
57	{ (u8 *) "\xef\x00", 2, ParseOK, 1 },
58	{ (u8 *) "\xef\x01\x11", 3, ParseOK, 1 },
59	{ (u8 *) "\xf0\x00", 2, ParseOK, 1 },
60	{ (u8 *) "\xf1\x00", 2, ParseOK, 1 },
61	{ (u8 *) "\xf1\x02\x11\x22", 4, ParseOK, 1 },
62	{ (u8 *) "\xf2\x00", 2, ParseOK, 1 },
63	{ (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
64	{ (u8 *) "\xff\x01\x00", 3, ParseUnknown, 1 },
65	{ (u8 *) "\xff\x01\x01", 3, ParseOK, 1 },
66	{ (u8 *) "\xff\x02\x01\x00", 4, ParseOK, 1 },
67	{ (u8 *) "\xff\x01\x02", 3, ParseOK, 1 },
68	{ (u8 *) "\xff\x04\x02\x11\x22\x33", 6, ParseOK, 1 },
69	{ (u8 *) "\xff\x01\x04", 3, ParseOK, 1 },
70	{ (u8 *) "\xff\x01\x05", 3, ParseOK, 1 },
71	{ (u8 *) "\xff\x0d\x05\x11\x22\x33\x44\x55\x55\x11\x22\x33\x44\x55\x55",
72	  15, ParseOK, 1 },
73	{ (u8 *) "\xff\x01\x06", 3, ParseOK, 1 },
74	{ (u8 *) "\xff\x02\x06\x00", 4, ParseOK, 1 },
75	{ (u8 *) "\xff\x01\x07", 3, ParseOK, 1 },
76	{ (u8 *) "\xff\x09\x07\x11\x22\x33\x44\x55\x66\x77\x88", 11,
77	  ParseOK, 1 },
78	{ (u8 *) "\xff\x01\x0c", 3, ParseOK, 1 },
79	{ (u8 *) "\xff\x02\x0c\x00", 4, ParseOK, 1 },
80	{ (u8 *) "\xff\x01\x0d", 3, ParseOK, 1 },
81	{ NULL, 0, ParseOK, 0 }
82};
83
84static int ieee802_11_parse_tests(void)
85{
86	int i, ret = 0;
87	struct wpabuf *buf;
88
89	wpa_printf(MSG_INFO, "ieee802_11_parse tests");
90
91	for (i = 0; parse_tests[i].data; i++) {
92		const struct ieee802_11_parse_test_data *test;
93		struct ieee802_11_elems elems;
94		ParseRes res;
95
96		test = &parse_tests[i];
97		res = ieee802_11_parse_elems(test->data, test->len, &elems, 1);
98		if (res != test->result ||
99		    ieee802_11_ie_count(test->data, test->len) != test->count) {
100			wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed",
101				   i);
102			ret = -1;
103		}
104	}
105
106	if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL)
107	{
108		wpa_printf(MSG_ERROR,
109			   "ieee802_11_vendor_ie_concat test failed");
110		ret = -1;
111	}
112
113	buf = ieee802_11_vendor_ie_concat((const u8 *) "\xdd\x05\x11\x22\x33\x44\x01\xdd\x05\x11\x22\x33\x44\x02\x00\x01",
114					  16, 0x11223344);
115	do {
116		const u8 *pos;
117
118		if (!buf) {
119			wpa_printf(MSG_ERROR,
120				   "ieee802_11_vendor_ie_concat test 2 failed");
121			ret = -1;
122			break;
123		}
124
125		if (wpabuf_len(buf) != 2) {
126			wpa_printf(MSG_ERROR,
127				   "ieee802_11_vendor_ie_concat test 3 failed");
128			ret = -1;
129			break;
130		}
131
132		pos = wpabuf_head(buf);
133		if (pos[0] != 0x01 || pos[1] != 0x02) {
134			wpa_printf(MSG_ERROR,
135				   "ieee802_11_vendor_ie_concat test 3 failed");
136			ret = -1;
137			break;
138		}
139	} while (0);
140	wpabuf_free(buf);
141
142	return ret;
143}
144
145
146struct rsn_ie_parse_test_data {
147	u8 *data;
148	size_t len;
149	int result;
150};
151
152static const struct rsn_ie_parse_test_data rsn_parse_tests[] = {
153	{ (u8 *) "", 0, -1 },
154	{ (u8 *) "\x30\x00", 2, -1 },
155	{ (u8 *) "\x30\x02\x01\x00", 4, 0 },
156	{ (u8 *) "\x30\x02\x00\x00", 4, -2 },
157	{ (u8 *) "\x30\x02\x02\x00", 4, -2 },
158	{ (u8 *) "\x30\x02\x00\x01", 4, -2 },
159	{ (u8 *) "\x30\x02\x00\x00\x00", 5, -2 },
160	{ (u8 *) "\x30\x03\x01\x00\x00", 5, -3 },
161	{ (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 },
162	{ (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 },
163	{ (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 },
164	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 },
165	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 },
166	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04",
167	  14, 0 },
168	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04",
169	  14, -4 },
170	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06",
171	  14, -1 },
172	{ (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08",
173	  18, 0 },
174	{ (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00",
175	  15, -7 },
176	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00",
177	  16, -6 },
178	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01",
179	  16, -6 },
180	{ (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01",
181	  20, 0 },
182	{ (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",
183	  24, 0 },
184	{ (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00",
185	  21, 0 },
186	{ (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00",
187	  22, 0 },
188	{ (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",
189	  24, 0 },
190	{ (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",
191	  24, -9 },
192	{ (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",
193	  28, -10 },
194	{ (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",
195	  28, 0 },
196	{ (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",
197	  30, 0 },
198	{ NULL, 0, 0 }
199};
200
201static int rsn_ie_parse_tests(void)
202{
203	int i, ret = 0;
204
205	wpa_printf(MSG_INFO, "rsn_ie_parse tests");
206
207	for (i = 0; rsn_parse_tests[i].data; i++) {
208		const struct rsn_ie_parse_test_data *test;
209		struct wpa_ie_data data;
210
211		test = &rsn_parse_tests[i];
212		if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) !=
213		    test->result) {
214			wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i);
215			ret = -1;
216		}
217	}
218
219	return ret;
220}
221
222
223static int gas_tests(void)
224{
225	struct wpabuf *buf;
226
227	wpa_printf(MSG_INFO, "gas tests");
228	gas_anqp_set_len(NULL);
229
230	buf = wpabuf_alloc(1);
231	if (buf == NULL)
232		return -1;
233	gas_anqp_set_len(buf);
234	wpabuf_free(buf);
235
236	buf = wpabuf_alloc(20);
237	if (buf == NULL)
238		return -1;
239	wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
240	wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_REQ);
241	wpabuf_put_u8(buf, 0);
242	wpabuf_put_be32(buf, 0);
243	wpabuf_put_u8(buf, 0);
244	gas_anqp_set_len(buf);
245	wpabuf_free(buf);
246
247	return 0;
248}
249
250
251int common_module_tests(void)
252{
253	int ret = 0;
254
255	wpa_printf(MSG_INFO, "common module tests");
256
257	if (ieee802_11_parse_tests() < 0 ||
258	    gas_tests() < 0 ||
259	    rsn_ie_parse_tests() < 0)
260		ret = -1;
261
262	return ret;
263}
264