15b9c547cSRui Paulo /* 25b9c547cSRui Paulo * common module tests 3*325151a3SRui 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" 125b9c547cSRui Paulo #include "ieee802_11_common.h" 13*325151a3SRui Paulo #include "ieee802_11_defs.h" 14*325151a3SRui Paulo #include "gas.h" 155b9c547cSRui Paulo #include "wpa_common.h" 165b9c547cSRui Paulo 175b9c547cSRui Paulo 185b9c547cSRui Paulo struct ieee802_11_parse_test_data { 195b9c547cSRui Paulo u8 *data; 205b9c547cSRui Paulo size_t len; 215b9c547cSRui Paulo ParseRes result; 225b9c547cSRui Paulo int count; 235b9c547cSRui Paulo }; 245b9c547cSRui Paulo 255b9c547cSRui Paulo static const struct ieee802_11_parse_test_data parse_tests[] = { 265b9c547cSRui Paulo { (u8 *) "", 0, ParseOK, 0 }, 275b9c547cSRui Paulo { (u8 *) " ", 1, ParseFailed, 0 }, 285b9c547cSRui Paulo { (u8 *) "\xff\x00", 2, ParseUnknown, 1 }, 295b9c547cSRui Paulo { (u8 *) "\xff\x01", 2, ParseFailed, 0 }, 305b9c547cSRui Paulo { (u8 *) "\xdd\x03\x01\x02\x03", 5, ParseUnknown, 1 }, 315b9c547cSRui Paulo { (u8 *) "\xdd\x04\x01\x02\x03\x04", 6, ParseUnknown, 1 }, 325b9c547cSRui Paulo { (u8 *) "\xdd\x04\x00\x50\xf2\x02", 6, ParseUnknown, 1 }, 335b9c547cSRui Paulo { (u8 *) "\xdd\x05\x00\x50\xf2\x02\x02", 7, ParseOK, 1 }, 345b9c547cSRui Paulo { (u8 *) "\xdd\x05\x00\x50\xf2\x02\xff", 7, ParseUnknown, 1 }, 355b9c547cSRui Paulo { (u8 *) "\xdd\x04\x00\x50\xf2\xff", 6, ParseUnknown, 1 }, 365b9c547cSRui Paulo { (u8 *) "\xdd\x04\x50\x6f\x9a\xff", 6, ParseUnknown, 1 }, 375b9c547cSRui Paulo { (u8 *) "\xdd\x04\x00\x90\x4c\x33", 6, ParseOK, 1 }, 385b9c547cSRui Paulo { (u8 *) "\xdd\x04\x00\x90\x4c\xff\xdd\x04\x00\x90\x4c\x33", 12, 395b9c547cSRui Paulo ParseUnknown, 2 }, 405b9c547cSRui Paulo { (u8 *) "\x10\x01\x00\x21\x00", 5, ParseOK, 2 }, 415b9c547cSRui Paulo { (u8 *) "\x24\x00", 2, ParseOK, 1 }, 425b9c547cSRui Paulo { (u8 *) "\x38\x00", 2, ParseOK, 1 }, 435b9c547cSRui Paulo { (u8 *) "\x54\x00", 2, ParseOK, 1 }, 445b9c547cSRui Paulo { (u8 *) "\x5a\x00", 2, ParseOK, 1 }, 455b9c547cSRui Paulo { (u8 *) "\x65\x00", 2, ParseOK, 1 }, 465b9c547cSRui Paulo { (u8 *) "\x65\x12\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11", 475b9c547cSRui Paulo 20, ParseOK, 1 }, 485b9c547cSRui Paulo { (u8 *) "\x6e\x00", 2, ParseOK, 1 }, 495b9c547cSRui Paulo { (u8 *) "\xc7\x00", 2, ParseOK, 1 }, 505b9c547cSRui Paulo { (u8 *) "\xc7\x01\x00", 3, ParseOK, 1 }, 51*325151a3SRui Paulo { (u8 *) "\x03\x00\x2a\x00\x36\x00\x37\x00\x38\x00\x2d\x00\x3d\x00\xbf\x00\xc0\x00", 52*325151a3SRui Paulo 18, ParseOK, 9 }, 53*325151a3SRui Paulo { (u8 *) "\x8b\x00", 2, ParseOK, 1 }, 54*325151a3SRui Paulo { (u8 *) "\xdd\x04\x00\x90\x4c\x04", 6, ParseUnknown, 1 }, 555b9c547cSRui Paulo { NULL, 0, ParseOK, 0 } 565b9c547cSRui Paulo }; 575b9c547cSRui Paulo 585b9c547cSRui Paulo static int ieee802_11_parse_tests(void) 595b9c547cSRui Paulo { 605b9c547cSRui Paulo int i, ret = 0; 615b9c547cSRui Paulo 625b9c547cSRui Paulo wpa_printf(MSG_INFO, "ieee802_11_parse tests"); 635b9c547cSRui Paulo 645b9c547cSRui Paulo for (i = 0; parse_tests[i].data; i++) { 655b9c547cSRui Paulo const struct ieee802_11_parse_test_data *test; 665b9c547cSRui Paulo struct ieee802_11_elems elems; 675b9c547cSRui Paulo ParseRes res; 685b9c547cSRui Paulo 695b9c547cSRui Paulo test = &parse_tests[i]; 705b9c547cSRui Paulo res = ieee802_11_parse_elems(test->data, test->len, &elems, 1); 715b9c547cSRui Paulo if (res != test->result || 725b9c547cSRui Paulo ieee802_11_ie_count(test->data, test->len) != test->count) { 735b9c547cSRui Paulo wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed", 745b9c547cSRui Paulo i); 755b9c547cSRui Paulo ret = -1; 765b9c547cSRui Paulo } 775b9c547cSRui Paulo } 785b9c547cSRui Paulo 795b9c547cSRui Paulo if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL) 805b9c547cSRui Paulo { 815b9c547cSRui Paulo wpa_printf(MSG_ERROR, 825b9c547cSRui Paulo "ieee802_11_vendor_ie_concat test failed"); 835b9c547cSRui Paulo ret = -1; 845b9c547cSRui Paulo } 855b9c547cSRui Paulo 865b9c547cSRui Paulo return ret; 875b9c547cSRui Paulo } 885b9c547cSRui Paulo 895b9c547cSRui Paulo 905b9c547cSRui Paulo struct rsn_ie_parse_test_data { 915b9c547cSRui Paulo u8 *data; 925b9c547cSRui Paulo size_t len; 935b9c547cSRui Paulo int result; 945b9c547cSRui Paulo }; 955b9c547cSRui Paulo 965b9c547cSRui Paulo static const struct rsn_ie_parse_test_data rsn_parse_tests[] = { 975b9c547cSRui Paulo { (u8 *) "", 0, -1 }, 985b9c547cSRui Paulo { (u8 *) "\x30\x00", 2, -1 }, 995b9c547cSRui Paulo { (u8 *) "\x30\x02\x01\x00", 4, 0 }, 1005b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x00", 4, -2 }, 1015b9c547cSRui Paulo { (u8 *) "\x30\x02\x02\x00", 4, -2 }, 1025b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x01", 4, -2 }, 1035b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x00\x00", 5, -2 }, 1045b9c547cSRui Paulo { (u8 *) "\x30\x03\x01\x00\x00", 5, -3 }, 1055b9c547cSRui Paulo { (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 }, 1065b9c547cSRui Paulo { (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 }, 1075b9c547cSRui Paulo { (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 }, 1085b9c547cSRui Paulo { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 }, 1095b9c547cSRui Paulo { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 }, 1105b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04", 1115b9c547cSRui Paulo 14, 0 }, 1125b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04", 1135b9c547cSRui Paulo 14, -4 }, 1145b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06", 1155b9c547cSRui Paulo 14, -1 }, 1165b9c547cSRui Paulo { (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08", 1175b9c547cSRui Paulo 18, 0 }, 1185b9c547cSRui Paulo { (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00", 1195b9c547cSRui Paulo 15, -7 }, 1205b9c547cSRui Paulo { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00", 1215b9c547cSRui Paulo 16, -6 }, 1225b9c547cSRui Paulo { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01", 1235b9c547cSRui Paulo 16, -6 }, 1245b9c547cSRui Paulo { (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01", 1255b9c547cSRui Paulo 20, 0 }, 1265b9c547cSRui 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", 1275b9c547cSRui Paulo 24, 0 }, 1285b9c547cSRui Paulo { (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00", 1295b9c547cSRui Paulo 21, 0 }, 1305b9c547cSRui Paulo { (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00", 1315b9c547cSRui Paulo 22, 0 }, 1325b9c547cSRui 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", 1335b9c547cSRui Paulo 24, 0 }, 1345b9c547cSRui 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", 1355b9c547cSRui Paulo 24, -9 }, 1365b9c547cSRui 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", 1375b9c547cSRui Paulo 28, -10 }, 1385b9c547cSRui 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", 1395b9c547cSRui Paulo 28, 0 }, 1405b9c547cSRui 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", 1415b9c547cSRui Paulo 30, 0 }, 1425b9c547cSRui Paulo { NULL, 0, 0 } 1435b9c547cSRui Paulo }; 1445b9c547cSRui Paulo 1455b9c547cSRui Paulo static int rsn_ie_parse_tests(void) 1465b9c547cSRui Paulo { 1475b9c547cSRui Paulo int i, ret = 0; 1485b9c547cSRui Paulo 1495b9c547cSRui Paulo wpa_printf(MSG_INFO, "rsn_ie_parse tests"); 1505b9c547cSRui Paulo 1515b9c547cSRui Paulo for (i = 0; rsn_parse_tests[i].data; i++) { 1525b9c547cSRui Paulo const struct rsn_ie_parse_test_data *test; 1535b9c547cSRui Paulo struct wpa_ie_data data; 1545b9c547cSRui Paulo 1555b9c547cSRui Paulo test = &rsn_parse_tests[i]; 1565b9c547cSRui Paulo if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) != 1575b9c547cSRui Paulo test->result) { 1585b9c547cSRui Paulo wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i); 1595b9c547cSRui Paulo ret = -1; 1605b9c547cSRui Paulo } 1615b9c547cSRui Paulo } 1625b9c547cSRui Paulo 1635b9c547cSRui Paulo return ret; 1645b9c547cSRui Paulo } 1655b9c547cSRui Paulo 1665b9c547cSRui Paulo 167*325151a3SRui Paulo static int gas_tests(void) 168*325151a3SRui Paulo { 169*325151a3SRui Paulo struct wpabuf *buf; 170*325151a3SRui Paulo 171*325151a3SRui Paulo wpa_printf(MSG_INFO, "gas tests"); 172*325151a3SRui Paulo gas_anqp_set_len(NULL); 173*325151a3SRui Paulo 174*325151a3SRui Paulo buf = wpabuf_alloc(1); 175*325151a3SRui Paulo if (buf == NULL) 176*325151a3SRui Paulo return -1; 177*325151a3SRui Paulo gas_anqp_set_len(buf); 178*325151a3SRui Paulo wpabuf_free(buf); 179*325151a3SRui Paulo 180*325151a3SRui Paulo buf = wpabuf_alloc(20); 181*325151a3SRui Paulo if (buf == NULL) 182*325151a3SRui Paulo return -1; 183*325151a3SRui Paulo wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC); 184*325151a3SRui Paulo wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_REQ); 185*325151a3SRui Paulo wpabuf_put_u8(buf, 0); 186*325151a3SRui Paulo wpabuf_put_be32(buf, 0); 187*325151a3SRui Paulo wpabuf_put_u8(buf, 0); 188*325151a3SRui Paulo gas_anqp_set_len(buf); 189*325151a3SRui Paulo wpabuf_free(buf); 190*325151a3SRui Paulo 191*325151a3SRui Paulo return 0; 192*325151a3SRui Paulo } 193*325151a3SRui Paulo 194*325151a3SRui Paulo 1955b9c547cSRui Paulo int common_module_tests(void) 1965b9c547cSRui Paulo { 1975b9c547cSRui Paulo int ret = 0; 1985b9c547cSRui Paulo 1995b9c547cSRui Paulo wpa_printf(MSG_INFO, "common module tests"); 2005b9c547cSRui Paulo 2015b9c547cSRui Paulo if (ieee802_11_parse_tests() < 0 || 202*325151a3SRui Paulo gas_tests() < 0 || 2035b9c547cSRui Paulo rsn_ie_parse_tests() < 0) 2045b9c547cSRui Paulo ret = -1; 2055b9c547cSRui Paulo 2065b9c547cSRui Paulo return ret; 2075b9c547cSRui Paulo } 208