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" 12*780fb4a2SCy 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 }, 565b9c547cSRui Paulo { NULL, 0, ParseOK, 0 } 575b9c547cSRui Paulo }; 585b9c547cSRui Paulo 595b9c547cSRui Paulo static int ieee802_11_parse_tests(void) 605b9c547cSRui Paulo { 615b9c547cSRui Paulo int i, ret = 0; 625b9c547cSRui Paulo 635b9c547cSRui Paulo wpa_printf(MSG_INFO, "ieee802_11_parse tests"); 645b9c547cSRui Paulo 655b9c547cSRui Paulo for (i = 0; parse_tests[i].data; i++) { 665b9c547cSRui Paulo const struct ieee802_11_parse_test_data *test; 675b9c547cSRui Paulo struct ieee802_11_elems elems; 685b9c547cSRui Paulo ParseRes res; 695b9c547cSRui Paulo 705b9c547cSRui Paulo test = &parse_tests[i]; 715b9c547cSRui Paulo res = ieee802_11_parse_elems(test->data, test->len, &elems, 1); 725b9c547cSRui Paulo if (res != test->result || 735b9c547cSRui Paulo ieee802_11_ie_count(test->data, test->len) != test->count) { 745b9c547cSRui Paulo wpa_printf(MSG_ERROR, "ieee802_11_parse test %d failed", 755b9c547cSRui Paulo i); 765b9c547cSRui Paulo ret = -1; 775b9c547cSRui Paulo } 785b9c547cSRui Paulo } 795b9c547cSRui Paulo 805b9c547cSRui Paulo if (ieee802_11_vendor_ie_concat((const u8 *) "\x00\x01", 2, 0) != NULL) 815b9c547cSRui Paulo { 825b9c547cSRui Paulo wpa_printf(MSG_ERROR, 835b9c547cSRui Paulo "ieee802_11_vendor_ie_concat test failed"); 845b9c547cSRui Paulo ret = -1; 855b9c547cSRui Paulo } 865b9c547cSRui Paulo 875b9c547cSRui Paulo return ret; 885b9c547cSRui Paulo } 895b9c547cSRui Paulo 905b9c547cSRui Paulo 915b9c547cSRui Paulo struct rsn_ie_parse_test_data { 925b9c547cSRui Paulo u8 *data; 935b9c547cSRui Paulo size_t len; 945b9c547cSRui Paulo int result; 955b9c547cSRui Paulo }; 965b9c547cSRui Paulo 975b9c547cSRui Paulo static const struct rsn_ie_parse_test_data rsn_parse_tests[] = { 985b9c547cSRui Paulo { (u8 *) "", 0, -1 }, 995b9c547cSRui Paulo { (u8 *) "\x30\x00", 2, -1 }, 1005b9c547cSRui Paulo { (u8 *) "\x30\x02\x01\x00", 4, 0 }, 1015b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x00", 4, -2 }, 1025b9c547cSRui Paulo { (u8 *) "\x30\x02\x02\x00", 4, -2 }, 1035b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x01", 4, -2 }, 1045b9c547cSRui Paulo { (u8 *) "\x30\x02\x00\x00\x00", 5, -2 }, 1055b9c547cSRui Paulo { (u8 *) "\x30\x03\x01\x00\x00", 5, -3 }, 1065b9c547cSRui Paulo { (u8 *) "\x30\x06\x01\x00\x00\x00\x00\x00", 8, -1 }, 1075b9c547cSRui Paulo { (u8 *) "\x30\x06\x01\x00\x00\x0f\xac\x04", 8, 0 }, 1085b9c547cSRui Paulo { (u8 *) "\x30\x07\x01\x00\x00\x0f\xac\x04\x00", 9, -5 }, 1095b9c547cSRui Paulo { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x00", 10, -4 }, 1105b9c547cSRui Paulo { (u8 *) "\x30\x08\x01\x00\x00\x0f\xac\x04\x00\x01", 10, -4 }, 1115b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04", 1125b9c547cSRui Paulo 14, 0 }, 1135b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x00\x01\x00\x0f\xac\x04", 1145b9c547cSRui Paulo 14, -4 }, 1155b9c547cSRui Paulo { (u8 *) "\x30\x0c\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x06", 1165b9c547cSRui Paulo 14, -1 }, 1175b9c547cSRui Paulo { (u8 *) "\x30\x10\x01\x00\x00\x0f\xac\x04\x02\x00\x00\x0f\xac\x04\x00\x0f\xac\x08", 1185b9c547cSRui Paulo 18, 0 }, 1195b9c547cSRui Paulo { (u8 *) "\x30\x0d\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00", 1205b9c547cSRui Paulo 15, -7 }, 1215b9c547cSRui Paulo { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x00", 1225b9c547cSRui Paulo 16, -6 }, 1235b9c547cSRui Paulo { (u8 *) "\x30\x0e\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x00\x01", 1245b9c547cSRui Paulo 16, -6 }, 1255b9c547cSRui Paulo { (u8 *) "\x30\x12\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01", 1265b9c547cSRui Paulo 20, 0 }, 1275b9c547cSRui 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", 1285b9c547cSRui Paulo 24, 0 }, 1295b9c547cSRui Paulo { (u8 *) "\x30\x13\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00", 1305b9c547cSRui Paulo 21, 0 }, 1315b9c547cSRui Paulo { (u8 *) "\x30\x14\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x04\x01\x00\x00\x0f\xac\x01\x00\x00", 1325b9c547cSRui Paulo 22, 0 }, 1335b9c547cSRui 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", 1345b9c547cSRui Paulo 24, 0 }, 1355b9c547cSRui 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", 1365b9c547cSRui Paulo 24, -9 }, 1375b9c547cSRui 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", 1385b9c547cSRui Paulo 28, -10 }, 1395b9c547cSRui 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", 1405b9c547cSRui Paulo 28, 0 }, 1415b9c547cSRui 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", 1425b9c547cSRui Paulo 30, 0 }, 1435b9c547cSRui Paulo { NULL, 0, 0 } 1445b9c547cSRui Paulo }; 1455b9c547cSRui Paulo 1465b9c547cSRui Paulo static int rsn_ie_parse_tests(void) 1475b9c547cSRui Paulo { 1485b9c547cSRui Paulo int i, ret = 0; 1495b9c547cSRui Paulo 1505b9c547cSRui Paulo wpa_printf(MSG_INFO, "rsn_ie_parse tests"); 1515b9c547cSRui Paulo 1525b9c547cSRui Paulo for (i = 0; rsn_parse_tests[i].data; i++) { 1535b9c547cSRui Paulo const struct rsn_ie_parse_test_data *test; 1545b9c547cSRui Paulo struct wpa_ie_data data; 1555b9c547cSRui Paulo 1565b9c547cSRui Paulo test = &rsn_parse_tests[i]; 1575b9c547cSRui Paulo if (wpa_parse_wpa_ie_rsn(test->data, test->len, &data) != 1585b9c547cSRui Paulo test->result) { 1595b9c547cSRui Paulo wpa_printf(MSG_ERROR, "rsn_ie_parse test %d failed", i); 1605b9c547cSRui Paulo ret = -1; 1615b9c547cSRui Paulo } 1625b9c547cSRui Paulo } 1635b9c547cSRui Paulo 1645b9c547cSRui Paulo return ret; 1655b9c547cSRui Paulo } 1665b9c547cSRui Paulo 1675b9c547cSRui Paulo 168325151a3SRui Paulo static int gas_tests(void) 169325151a3SRui Paulo { 170325151a3SRui Paulo struct wpabuf *buf; 171325151a3SRui Paulo 172325151a3SRui Paulo wpa_printf(MSG_INFO, "gas tests"); 173325151a3SRui Paulo gas_anqp_set_len(NULL); 174325151a3SRui Paulo 175325151a3SRui Paulo buf = wpabuf_alloc(1); 176325151a3SRui Paulo if (buf == NULL) 177325151a3SRui Paulo return -1; 178325151a3SRui Paulo gas_anqp_set_len(buf); 179325151a3SRui Paulo wpabuf_free(buf); 180325151a3SRui Paulo 181325151a3SRui Paulo buf = wpabuf_alloc(20); 182325151a3SRui Paulo if (buf == NULL) 183325151a3SRui Paulo return -1; 184325151a3SRui Paulo wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC); 185325151a3SRui Paulo wpabuf_put_u8(buf, WLAN_PA_GAS_INITIAL_REQ); 186325151a3SRui Paulo wpabuf_put_u8(buf, 0); 187325151a3SRui Paulo wpabuf_put_be32(buf, 0); 188325151a3SRui Paulo wpabuf_put_u8(buf, 0); 189325151a3SRui Paulo gas_anqp_set_len(buf); 190325151a3SRui Paulo wpabuf_free(buf); 191325151a3SRui Paulo 192325151a3SRui Paulo return 0; 193325151a3SRui Paulo } 194325151a3SRui Paulo 195325151a3SRui Paulo 1965b9c547cSRui Paulo int common_module_tests(void) 1975b9c547cSRui Paulo { 1985b9c547cSRui Paulo int ret = 0; 1995b9c547cSRui Paulo 2005b9c547cSRui Paulo wpa_printf(MSG_INFO, "common module tests"); 2015b9c547cSRui Paulo 2025b9c547cSRui Paulo if (ieee802_11_parse_tests() < 0 || 203325151a3SRui Paulo gas_tests() < 0 || 2045b9c547cSRui Paulo rsn_ie_parse_tests() < 0) 2055b9c547cSRui Paulo ret = -1; 2065b9c547cSRui Paulo 2075b9c547cSRui Paulo return ret; 2085b9c547cSRui Paulo } 209