xref: /freebsd/contrib/wpa/src/common/common_module_tests.c (revision 85732ac8bccbc0adcf5a261ea1ffec8ca7b3a92d)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * common module tests
3325151a3SRui Paulo  * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo  *
55b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo  * See README for more details.
75b9c547cSRui Paulo  */
85b9c547cSRui Paulo 
95b9c547cSRui Paulo #include "utils/includes.h"
105b9c547cSRui Paulo 
115b9c547cSRui Paulo #include "utils/common.h"
12780fb4a2SCy Schubert #include "utils/module_tests.h"
135b9c547cSRui Paulo #include "ieee802_11_common.h"
14325151a3SRui Paulo #include "ieee802_11_defs.h"
15325151a3SRui Paulo #include "gas.h"
165b9c547cSRui Paulo #include "wpa_common.h"
175b9c547cSRui Paulo 
185b9c547cSRui Paulo 
195b9c547cSRui Paulo struct ieee802_11_parse_test_data {
205b9c547cSRui Paulo 	u8 *data;
215b9c547cSRui Paulo 	size_t len;
225b9c547cSRui Paulo 	ParseRes result;
235b9c547cSRui Paulo 	int count;
245b9c547cSRui Paulo };
255b9c547cSRui Paulo 
265b9c547cSRui Paulo static const struct ieee802_11_parse_test_data parse_tests[] = {
275b9c547cSRui Paulo 	{ (u8 *) "", 0, ParseOK, 0 },
285b9c547cSRui Paulo 	{ (u8 *) " ", 1, ParseFailed, 0 },
295b9c547cSRui Paulo 	{ (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
305b9c547cSRui Paulo 	{ (u8 *) "\xff\x01", 2, ParseFailed, 0 },
315b9c547cSRui Paulo 	{ (u8 *) "\xdd\x03\x01\x02\x03", 5, ParseUnknown, 1 },
325b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x01\x02\x03\x04", 6, ParseUnknown, 1 },
335b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x50\xf2\x02", 6, ParseUnknown, 1 },
345b9c547cSRui Paulo 	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\x02", 7, ParseOK, 1 },
355b9c547cSRui Paulo 	{ (u8 *) "\xdd\x05\x00\x50\xf2\x02\xff", 7, ParseUnknown, 1 },
365b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x50\xf2\xff", 6, ParseUnknown, 1 },
375b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x50\x6f\x9a\xff", 6, ParseUnknown, 1 },
385b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x90\x4c\x33", 6, ParseOK, 1 },
395b9c547cSRui Paulo 	{ (u8 *) "\xdd\x04\x00\x90\x4c\xff\xdd\x04\x00\x90\x4c\x33", 12,
405b9c547cSRui Paulo 	  ParseUnknown, 2 },
415b9c547cSRui Paulo 	{ (u8 *) "\x10\x01\x00\x21\x00", 5, ParseOK, 2 },
425b9c547cSRui Paulo 	{ (u8 *) "\x24\x00", 2, ParseOK, 1 },
435b9c547cSRui Paulo 	{ (u8 *) "\x38\x00", 2, ParseOK, 1 },
445b9c547cSRui Paulo 	{ (u8 *) "\x54\x00", 2, ParseOK, 1 },
455b9c547cSRui Paulo 	{ (u8 *) "\x5a\x00", 2, ParseOK, 1 },
465b9c547cSRui Paulo 	{ (u8 *) "\x65\x00", 2, ParseOK, 1 },
475b9c547cSRui Paulo 	{ (u8 *) "\x65\x12\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11",
485b9c547cSRui Paulo 	  20, ParseOK, 1 },
495b9c547cSRui Paulo 	{ (u8 *) "\x6e\x00", 2, ParseOK, 1 },
505b9c547cSRui Paulo 	{ (u8 *) "\xc7\x00", 2, ParseOK, 1 },
515b9c547cSRui Paulo 	{ (u8 *) "\xc7\x01\x00", 3, ParseOK, 1 },
52325151a3SRui Paulo 	{ (u8 *) "\x03\x00\x2a\x00\x36\x00\x37\x00\x38\x00\x2d\x00\x3d\x00\xbf\x00\xc0\x00",
53325151a3SRui Paulo 	  18, ParseOK, 9 },
54325151a3SRui Paulo 	{ (u8 *) "\x8b\x00", 2, ParseOK, 1 },
55325151a3SRui Paulo 	{ (u8 *) "\xdd\x04\x00\x90\x4c\x04", 6, ParseUnknown, 1 },
56*85732ac8SCy Schubert 	{ (u8 *) "\xed\x00", 2, ParseOK, 1 },
57*85732ac8SCy Schubert 	{ (u8 *) "\xef\x00", 2, ParseOK, 1 },
58*85732ac8SCy Schubert 	{ (u8 *) "\xef\x01\x11", 3, ParseOK, 1 },
59*85732ac8SCy Schubert 	{ (u8 *) "\xf0\x00", 2, ParseOK, 1 },
60*85732ac8SCy Schubert 	{ (u8 *) "\xf1\x00", 2, ParseOK, 1 },
61*85732ac8SCy Schubert 	{ (u8 *) "\xf1\x02\x11\x22", 4, ParseOK, 1 },
62*85732ac8SCy Schubert 	{ (u8 *) "\xf2\x00", 2, ParseOK, 1 },
63*85732ac8SCy Schubert 	{ (u8 *) "\xff\x00", 2, ParseUnknown, 1 },
64*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x00", 3, ParseUnknown, 1 },
65*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x01", 3, ParseOK, 1 },
66*85732ac8SCy Schubert 	{ (u8 *) "\xff\x02\x01\x00", 4, ParseOK, 1 },
67*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x02", 3, ParseOK, 1 },
68*85732ac8SCy Schubert 	{ (u8 *) "\xff\x04\x02\x11\x22\x33", 6, ParseOK, 1 },
69*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x04", 3, ParseOK, 1 },
70*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x05", 3, ParseOK, 1 },
71*85732ac8SCy Schubert 	{ (u8 *) "\xff\x0d\x05\x11\x22\x33\x44\x55\x55\x11\x22\x33\x44\x55\x55",
72*85732ac8SCy Schubert 	  15, ParseOK, 1 },
73*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x06", 3, ParseOK, 1 },
74*85732ac8SCy Schubert 	{ (u8 *) "\xff\x02\x06\x00", 4, ParseOK, 1 },
75*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x07", 3, ParseOK, 1 },
76*85732ac8SCy Schubert 	{ (u8 *) "\xff\x09\x07\x11\x22\x33\x44\x55\x66\x77\x88", 11,
77*85732ac8SCy Schubert 	  ParseOK, 1 },
78*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x0c", 3, ParseOK, 1 },
79*85732ac8SCy Schubert 	{ (u8 *) "\xff\x02\x0c\x00", 4, ParseOK, 1 },
80*85732ac8SCy Schubert 	{ (u8 *) "\xff\x01\x0d", 3, ParseOK, 1 },
815b9c547cSRui Paulo 	{ NULL, 0, ParseOK, 0 }
825b9c547cSRui Paulo };
835b9c547cSRui Paulo 
845b9c547cSRui Paulo static int ieee802_11_parse_tests(void)
855b9c547cSRui Paulo {
865b9c547cSRui Paulo 	int i, ret = 0;
87*85732ac8SCy Schubert 	struct wpabuf *buf;
885b9c547cSRui Paulo 
895b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "ieee802_11_parse tests");
905b9c547cSRui Paulo 
915b9c547cSRui Paulo 	for (i = 0; parse_tests[i].data; i++) {
925b9c547cSRui Paulo 		const struct ieee802_11_parse_test_data *test;
935b9c547cSRui Paulo 		struct ieee802_11_elems elems;
945b9c547cSRui Paulo 		ParseRes res;
955b9c547cSRui Paulo 
965b9c547cSRui Paulo 		test = &parse_tests[i];
975b9c547cSRui Paulo 		res = ieee802_11_parse_elems(test->data, test->len, &elems, 1);
985b9c547cSRui Paulo 		if (res != test->result ||
995b9c547cSRui Paulo 		    ieee802_11_ie_count(test->data, test->len) != test->count) {
1005b9c547cSRui Paulo 			wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed",
1015b9c547cSRui Paulo 				   i);
1025b9c547cSRui Paulo 			ret = -1;
1035b9c547cSRui Paulo 		}
1045b9c547cSRui Paulo 	}
1055b9c547cSRui Paulo 
1065b9c547cSRui Paulo 	if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL)
1075b9c547cSRui Paulo 	{
1085b9c547cSRui Paulo 		wpa_printf(MSG_ERROR,
1095b9c547cSRui Paulo 			   "ieee802_11_vendor_ie_concat test failed");
1105b9c547cSRui Paulo 		ret = -1;
1115b9c547cSRui Paulo 	}
1125b9c547cSRui Paulo 
113*85732ac8SCy Schubert 	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*85732ac8SCy Schubert 					  16, 0x11223344);
115*85732ac8SCy Schubert 	do {
116*85732ac8SCy Schubert 		const u8 *pos;
117*85732ac8SCy Schubert 
118*85732ac8SCy Schubert 		if (!buf) {
119*85732ac8SCy Schubert 			wpa_printf(MSG_ERROR,
120*85732ac8SCy Schubert 				   "ieee802_11_vendor_ie_concat test 2 failed");
121*85732ac8SCy Schubert 			ret = -1;
122*85732ac8SCy Schubert 			break;
123*85732ac8SCy Schubert 		}
124*85732ac8SCy Schubert 
125*85732ac8SCy Schubert 		if (wpabuf_len(buf) != 2) {
126*85732ac8SCy Schubert 			wpa_printf(MSG_ERROR,
127*85732ac8SCy Schubert 				   "ieee802_11_vendor_ie_concat test 3 failed");
128*85732ac8SCy Schubert 			ret = -1;
129*85732ac8SCy Schubert 			break;
130*85732ac8SCy Schubert 		}
131*85732ac8SCy Schubert 
132*85732ac8SCy Schubert 		pos = wpabuf_head(buf);
133*85732ac8SCy Schubert 		if (pos[0] != 0x01 || pos[1] != 0x02) {
134*85732ac8SCy Schubert 			wpa_printf(MSG_ERROR,
135*85732ac8SCy Schubert 				   "ieee802_11_vendor_ie_concat test 3 failed");
136*85732ac8SCy Schubert 			ret = -1;
137*85732ac8SCy Schubert 			break;
138*85732ac8SCy Schubert 		}
139*85732ac8SCy Schubert 	} while (0);
140*85732ac8SCy Schubert 	wpabuf_free(buf);
141*85732ac8SCy Schubert 
1425b9c547cSRui Paulo 	return ret;
1435b9c547cSRui Paulo }
1445b9c547cSRui Paulo 
1455b9c547cSRui Paulo 
1465b9c547cSRui Paulo struct rsn_ie_parse_test_data {
1475b9c547cSRui Paulo 	u8 *data;
1485b9c547cSRui Paulo 	size_t len;
1495b9c547cSRui Paulo 	int result;
1505b9c547cSRui Paulo };
1515b9c547cSRui Paulo 
1525b9c547cSRui Paulo static const struct rsn_ie_parse_test_data rsn_parse_tests[] = {
1535b9c547cSRui Paulo 	{ (u8 *) "", 0, -1 },
1545b9c547cSRui Paulo 	{ (u8 *) "\x30\x00", 2, -1 },
1555b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x01\x00", 4, 0 },
1565b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x00", 4, -2 },
1575b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x02\x00", 4, -2 },
1585b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x01", 4, -2 },
1595b9c547cSRui Paulo 	{ (u8 *) "\x30\x02\x00\x00\x00", 5, -2 },
1605b9c547cSRui Paulo 	{ (u8 *) "\x30\x03\x01\x00\x00", 5, -3 },
1615b9c547cSRui Paulo 	{ (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 },
1625b9c547cSRui Paulo 	{ (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 },
1635b9c547cSRui Paulo 	{ (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 },
1645b9c547cSRui Paulo 	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 },
1655b9c547cSRui Paulo 	{ (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 },
1665b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04",
1675b9c547cSRui Paulo 	  14, 0 },
1685b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04",
1695b9c547cSRui Paulo 	  14, -4 },
1705b9c547cSRui Paulo 	{ (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06",
1715b9c547cSRui Paulo 	  14, -1 },
1725b9c547cSRui Paulo 	{ (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08",
1735b9c547cSRui Paulo 	  18, 0 },
1745b9c547cSRui Paulo 	{ (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00",
1755b9c547cSRui Paulo 	  15, -7 },
1765b9c547cSRui Paulo 	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00",
1775b9c547cSRui Paulo 	  16, -6 },
1785b9c547cSRui Paulo 	{ (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01",
1795b9c547cSRui Paulo 	  16, -6 },
1805b9c547cSRui Paulo 	{ (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01",
1815b9c547cSRui Paulo 	  20, 0 },
1825b9c547cSRui 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",
1835b9c547cSRui Paulo 	  24, 0 },
1845b9c547cSRui Paulo 	{ (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00",
1855b9c547cSRui Paulo 	  21, 0 },
1865b9c547cSRui Paulo 	{ (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00",
1875b9c547cSRui Paulo 	  22, 0 },
1885b9c547cSRui 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",
1895b9c547cSRui Paulo 	  24, 0 },
1905b9c547cSRui 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",
1915b9c547cSRui Paulo 	  24, -9 },
1925b9c547cSRui 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",
1935b9c547cSRui Paulo 	  28, -10 },
1945b9c547cSRui 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",
1955b9c547cSRui Paulo 	  28, 0 },
1965b9c547cSRui 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",
1975b9c547cSRui Paulo 	  30, 0 },
1985b9c547cSRui Paulo 	{ NULL, 0, 0 }
1995b9c547cSRui Paulo };
2005b9c547cSRui Paulo 
2015b9c547cSRui Paulo static int rsn_ie_parse_tests(void)
2025b9c547cSRui Paulo {
2035b9c547cSRui Paulo 	int i, ret = 0;
2045b9c547cSRui Paulo 
2055b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "rsn_ie_parse tests");
2065b9c547cSRui Paulo 
2075b9c547cSRui Paulo 	for (i = 0; rsn_parse_tests[i].data; i++) {
2085b9c547cSRui Paulo 		const struct rsn_ie_parse_test_data *test;
2095b9c547cSRui Paulo 		struct wpa_ie_data data;
2105b9c547cSRui Paulo 
2115b9c547cSRui Paulo 		test = &rsn_parse_tests[i];
2125b9c547cSRui Paulo 		if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) !=
2135b9c547cSRui Paulo 		    test->result) {
2145b9c547cSRui Paulo 			wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i);
2155b9c547cSRui Paulo 			ret = -1;
2165b9c547cSRui Paulo 		}
2175b9c547cSRui Paulo 	}
2185b9c547cSRui Paulo 
2195b9c547cSRui Paulo 	return ret;
2205b9c547cSRui Paulo }
2215b9c547cSRui Paulo 
2225b9c547cSRui Paulo 
223325151a3SRui Paulo static int gas_tests(void)
224325151a3SRui Paulo {
225325151a3SRui Paulo 	struct wpabuf *buf;
226325151a3SRui Paulo 
227325151a3SRui Paulo 	wpa_printf(MSG_INFO, "gas tests");
228325151a3SRui Paulo 	gas_anqp_set_len(NULL);
229325151a3SRui Paulo 
230325151a3SRui Paulo 	buf = wpabuf_alloc(1);
231325151a3SRui Paulo 	if (buf == NULL)
232325151a3SRui Paulo 		return -1;
233325151a3SRui Paulo 	gas_anqp_set_len(buf);
234325151a3SRui Paulo 	wpabuf_free(buf);
235325151a3SRui Paulo 
236325151a3SRui Paulo 	buf = wpabuf_alloc(20);
237325151a3SRui Paulo 	if (buf == NULL)
238325151a3SRui Paulo 		return -1;
239325151a3SRui Paulo 	wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
240325151a3SRui Paulo 	wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_REQ);
241325151a3SRui Paulo 	wpabuf_put_u8(buf, 0);
242325151a3SRui Paulo 	wpabuf_put_be32(buf, 0);
243325151a3SRui Paulo 	wpabuf_put_u8(buf, 0);
244325151a3SRui Paulo 	gas_anqp_set_len(buf);
245325151a3SRui Paulo 	wpabuf_free(buf);
246325151a3SRui Paulo 
247325151a3SRui Paulo 	return 0;
248325151a3SRui Paulo }
249325151a3SRui Paulo 
250325151a3SRui Paulo 
2515b9c547cSRui Paulo int common_module_tests(void)
2525b9c547cSRui Paulo {
2535b9c547cSRui Paulo 	int ret = 0;
2545b9c547cSRui Paulo 
2555b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "common module tests");
2565b9c547cSRui Paulo 
2575b9c547cSRui Paulo 	if (ieee802_11_parse_tests() < 0 ||
258325151a3SRui Paulo 	    gas_tests() < 0 ||
2595b9c547cSRui Paulo 	    rsn_ie_parse_tests() < 0)
2605b9c547cSRui Paulo 		ret = -1;
2615b9c547cSRui Paulo 
2625b9c547cSRui Paulo 	return ret;
2635b9c547cSRui Paulo }
264