15b9c547cSRui Paulo /*
25b9c547cSRui Paulo * utils module tests
35b9c547cSRui 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"
124bc52338SCy Schubert #include "utils/const_time.h"
13325151a3SRui Paulo #include "common/ieee802_11_defs.h"
145b9c547cSRui Paulo #include "utils/bitfield.h"
155b9c547cSRui Paulo #include "utils/ext_password.h"
165b9c547cSRui Paulo #include "utils/trace.h"
175b9c547cSRui Paulo #include "utils/base64.h"
18325151a3SRui Paulo #include "utils/ip_addr.h"
19325151a3SRui Paulo #include "utils/eloop.h"
2085732ac8SCy Schubert #include "utils/json.h"
21780fb4a2SCy Schubert #include "utils/module_tests.h"
225b9c547cSRui Paulo
235b9c547cSRui Paulo
245b9c547cSRui Paulo struct printf_test_data {
255b9c547cSRui Paulo u8 *data;
265b9c547cSRui Paulo size_t len;
275b9c547cSRui Paulo char *encoded;
285b9c547cSRui Paulo };
295b9c547cSRui Paulo
305b9c547cSRui Paulo static const struct printf_test_data printf_tests[] = {
315b9c547cSRui Paulo { (u8 *) "abcde", 5, "abcde" },
325b9c547cSRui Paulo { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
335b9c547cSRui Paulo { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
345b9c547cSRui Paulo { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
355b9c547cSRui Paulo { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
365b9c547cSRui Paulo "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
375b9c547cSRui Paulo { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
385b9c547cSRui Paulo "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
395b9c547cSRui Paulo { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
405b9c547cSRui Paulo "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
415b9c547cSRui Paulo { NULL, 0, NULL }
425b9c547cSRui Paulo };
435b9c547cSRui Paulo
445b9c547cSRui Paulo
printf_encode_decode_tests(void)455b9c547cSRui Paulo static int printf_encode_decode_tests(void)
465b9c547cSRui Paulo {
475b9c547cSRui Paulo int i;
485b9c547cSRui Paulo size_t binlen;
495b9c547cSRui Paulo char buf[100];
505b9c547cSRui Paulo u8 bin[100];
515b9c547cSRui Paulo int errors = 0;
52325151a3SRui Paulo int array[10];
535b9c547cSRui Paulo
545b9c547cSRui Paulo wpa_printf(MSG_INFO, "printf encode/decode tests");
555b9c547cSRui Paulo
565b9c547cSRui Paulo for (i = 0; printf_tests[i].data; i++) {
575b9c547cSRui Paulo const struct printf_test_data *test = &printf_tests[i];
585b9c547cSRui Paulo printf_encode(buf, sizeof(buf), test->data, test->len);
595b9c547cSRui Paulo wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
605b9c547cSRui Paulo
615b9c547cSRui Paulo binlen = printf_decode(bin, sizeof(bin), buf);
625b9c547cSRui Paulo if (binlen != test->len ||
635b9c547cSRui Paulo os_memcmp(bin, test->data, binlen) != 0) {
645b9c547cSRui Paulo wpa_hexdump(MSG_ERROR, "Error in decoding#1",
655b9c547cSRui Paulo bin, binlen);
665b9c547cSRui Paulo errors++;
675b9c547cSRui Paulo }
685b9c547cSRui Paulo
695b9c547cSRui Paulo binlen = printf_decode(bin, sizeof(bin), test->encoded);
705b9c547cSRui Paulo if (binlen != test->len ||
715b9c547cSRui Paulo os_memcmp(bin, test->data, binlen) != 0) {
725b9c547cSRui Paulo wpa_hexdump(MSG_ERROR, "Error in decoding#2",
735b9c547cSRui Paulo bin, binlen);
745b9c547cSRui Paulo errors++;
755b9c547cSRui Paulo }
765b9c547cSRui Paulo }
775b9c547cSRui Paulo
785b9c547cSRui Paulo buf[5] = 'A';
795b9c547cSRui Paulo printf_encode(buf, 5, (const u8 *) "abcde", 5);
805b9c547cSRui Paulo if (buf[5] != 'A') {
815b9c547cSRui Paulo wpa_printf(MSG_ERROR, "Error in bounds checking#1");
825b9c547cSRui Paulo errors++;
835b9c547cSRui Paulo }
845b9c547cSRui Paulo
855b9c547cSRui Paulo for (i = 5; i < 10; i++) {
865b9c547cSRui Paulo buf[i] = 'A';
875b9c547cSRui Paulo printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
885b9c547cSRui Paulo if (buf[i] != 'A') {
895b9c547cSRui Paulo wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
905b9c547cSRui Paulo i);
915b9c547cSRui Paulo errors++;
925b9c547cSRui Paulo }
935b9c547cSRui Paulo }
945b9c547cSRui Paulo
955b9c547cSRui Paulo if (printf_decode(bin, 3, "abcde") != 2)
965b9c547cSRui Paulo errors++;
975b9c547cSRui Paulo
985b9c547cSRui Paulo if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
995b9c547cSRui Paulo errors++;
1005b9c547cSRui Paulo
101325151a3SRui Paulo if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
102325151a3SRui Paulo errors++;
103325151a3SRui Paulo
1045b9c547cSRui Paulo if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
1055b9c547cSRui Paulo errors++;
1065b9c547cSRui Paulo
107325151a3SRui Paulo array[0] = 10;
108325151a3SRui Paulo array[1] = 10;
109325151a3SRui Paulo array[2] = 5;
110325151a3SRui Paulo array[3] = 10;
111325151a3SRui Paulo array[4] = 5;
112325151a3SRui Paulo array[5] = 0;
113325151a3SRui Paulo if (int_array_len(array) != 5)
114325151a3SRui Paulo errors++;
115325151a3SRui Paulo int_array_sort_unique(array);
116325151a3SRui Paulo if (int_array_len(array) != 2)
117325151a3SRui Paulo errors++;
118325151a3SRui Paulo
1195b9c547cSRui Paulo if (errors) {
1205b9c547cSRui Paulo wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
1215b9c547cSRui Paulo return -1;
1225b9c547cSRui Paulo }
1235b9c547cSRui Paulo
1245b9c547cSRui Paulo return 0;
1255b9c547cSRui Paulo }
1265b9c547cSRui Paulo
1275b9c547cSRui Paulo
bitfield_tests(void)1285b9c547cSRui Paulo static int bitfield_tests(void)
1295b9c547cSRui Paulo {
1305b9c547cSRui Paulo struct bitfield *bf;
1315b9c547cSRui Paulo int i;
1325b9c547cSRui Paulo int errors = 0;
1335b9c547cSRui Paulo
1345b9c547cSRui Paulo wpa_printf(MSG_INFO, "bitfield tests");
1355b9c547cSRui Paulo
1365b9c547cSRui Paulo bf = bitfield_alloc(123);
1375b9c547cSRui Paulo if (bf == NULL)
1385b9c547cSRui Paulo return -1;
1395b9c547cSRui Paulo
1405b9c547cSRui Paulo for (i = 0; i < 123; i++) {
1415b9c547cSRui Paulo if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
1425b9c547cSRui Paulo errors++;
1435b9c547cSRui Paulo if (i > 0 && bitfield_is_set(bf, i - 1))
1445b9c547cSRui Paulo errors++;
1455b9c547cSRui Paulo bitfield_set(bf, i);
1465b9c547cSRui Paulo if (!bitfield_is_set(bf, i))
1475b9c547cSRui Paulo errors++;
1485b9c547cSRui Paulo bitfield_clear(bf, i);
1495b9c547cSRui Paulo if (bitfield_is_set(bf, i))
1505b9c547cSRui Paulo errors++;
1515b9c547cSRui Paulo }
1525b9c547cSRui Paulo
1535b9c547cSRui Paulo for (i = 123; i < 200; i++) {
1545b9c547cSRui Paulo if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
1555b9c547cSRui Paulo errors++;
1565b9c547cSRui Paulo if (i > 0 && bitfield_is_set(bf, i - 1))
1575b9c547cSRui Paulo errors++;
1585b9c547cSRui Paulo bitfield_set(bf, i);
1595b9c547cSRui Paulo if (bitfield_is_set(bf, i))
1605b9c547cSRui Paulo errors++;
1615b9c547cSRui Paulo bitfield_clear(bf, i);
1625b9c547cSRui Paulo if (bitfield_is_set(bf, i))
1635b9c547cSRui Paulo errors++;
1645b9c547cSRui Paulo }
1655b9c547cSRui Paulo
1665b9c547cSRui Paulo for (i = 0; i < 123; i++) {
1675b9c547cSRui Paulo if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
1685b9c547cSRui Paulo errors++;
1695b9c547cSRui Paulo bitfield_set(bf, i);
1705b9c547cSRui Paulo if (!bitfield_is_set(bf, i))
1715b9c547cSRui Paulo errors++;
1725b9c547cSRui Paulo }
1735b9c547cSRui Paulo
1745b9c547cSRui Paulo for (i = 0; i < 123; i++) {
1755b9c547cSRui Paulo if (!bitfield_is_set(bf, i))
1765b9c547cSRui Paulo errors++;
1775b9c547cSRui Paulo bitfield_clear(bf, i);
1785b9c547cSRui Paulo if (bitfield_is_set(bf, i))
1795b9c547cSRui Paulo errors++;
1805b9c547cSRui Paulo }
1815b9c547cSRui Paulo
1825b9c547cSRui Paulo for (i = 0; i < 123; i++) {
1835b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != i)
1845b9c547cSRui Paulo errors++;
1855b9c547cSRui Paulo bitfield_set(bf, i);
1865b9c547cSRui Paulo }
1875b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != -1)
1885b9c547cSRui Paulo errors++;
1895b9c547cSRui Paulo for (i = 0; i < 123; i++) {
1905b9c547cSRui Paulo if (!bitfield_is_set(bf, i))
1915b9c547cSRui Paulo errors++;
1925b9c547cSRui Paulo bitfield_clear(bf, i);
1935b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != i)
1945b9c547cSRui Paulo errors++;
1955b9c547cSRui Paulo bitfield_set(bf, i);
1965b9c547cSRui Paulo }
1975b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != -1)
1985b9c547cSRui Paulo errors++;
1995b9c547cSRui Paulo
2005b9c547cSRui Paulo bitfield_free(bf);
2015b9c547cSRui Paulo
2025b9c547cSRui Paulo bf = bitfield_alloc(8);
2035b9c547cSRui Paulo if (bf == NULL)
2045b9c547cSRui Paulo return -1;
2055b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != 0)
2065b9c547cSRui Paulo errors++;
2075b9c547cSRui Paulo for (i = 0; i < 8; i++)
2085b9c547cSRui Paulo bitfield_set(bf, i);
2095b9c547cSRui Paulo if (bitfield_get_first_zero(bf) != -1)
2105b9c547cSRui Paulo errors++;
2115b9c547cSRui Paulo bitfield_free(bf);
2125b9c547cSRui Paulo
2135b9c547cSRui Paulo if (errors) {
2145b9c547cSRui Paulo wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
2155b9c547cSRui Paulo return -1;
2165b9c547cSRui Paulo }
2175b9c547cSRui Paulo
2185b9c547cSRui Paulo return 0;
2195b9c547cSRui Paulo }
2205b9c547cSRui Paulo
2215b9c547cSRui Paulo
int_array_tests(void)2225b9c547cSRui Paulo static int int_array_tests(void)
2235b9c547cSRui Paulo {
2245b9c547cSRui Paulo int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
2255b9c547cSRui Paulo int test2[] = { 1, -1, 0 };
2265b9c547cSRui Paulo int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
2275b9c547cSRui Paulo int test3_res[] = { -1, 1, 2, 3, 4, 0 };
2285b9c547cSRui Paulo int errors = 0;
229*c1d255d3SCy Schubert size_t len;
2305b9c547cSRui Paulo
2315b9c547cSRui Paulo wpa_printf(MSG_INFO, "int_array tests");
2325b9c547cSRui Paulo
2335b9c547cSRui Paulo if (int_array_len(test1) != 6 ||
2345b9c547cSRui Paulo int_array_len(test2) != 2)
2355b9c547cSRui Paulo errors++;
2365b9c547cSRui Paulo
2375b9c547cSRui Paulo int_array_sort_unique(test3);
2385b9c547cSRui Paulo len = int_array_len(test3_res);
2395b9c547cSRui Paulo if (int_array_len(test3) != len)
2405b9c547cSRui Paulo errors++;
2415b9c547cSRui Paulo else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
2425b9c547cSRui Paulo errors++;
2435b9c547cSRui Paulo
2445b9c547cSRui Paulo if (errors) {
2455b9c547cSRui Paulo wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
2465b9c547cSRui Paulo return -1;
2475b9c547cSRui Paulo }
2485b9c547cSRui Paulo
2495b9c547cSRui Paulo return 0;
2505b9c547cSRui Paulo }
2515b9c547cSRui Paulo
2525b9c547cSRui Paulo
ext_password_tests(void)2535b9c547cSRui Paulo static int ext_password_tests(void)
2545b9c547cSRui Paulo {
2555b9c547cSRui Paulo struct ext_password_data *data;
2565b9c547cSRui Paulo int ret = 0;
2575b9c547cSRui Paulo struct wpabuf *pw;
2585b9c547cSRui Paulo
2595b9c547cSRui Paulo wpa_printf(MSG_INFO, "ext_password tests");
2605b9c547cSRui Paulo
2615b9c547cSRui Paulo data = ext_password_init("unknown", "foo");
2625b9c547cSRui Paulo if (data != NULL)
2635b9c547cSRui Paulo return -1;
2645b9c547cSRui Paulo
2655b9c547cSRui Paulo data = ext_password_init("test", NULL);
2665b9c547cSRui Paulo if (data == NULL)
2675b9c547cSRui Paulo return -1;
2685b9c547cSRui Paulo pw = ext_password_get(data, "foo");
2695b9c547cSRui Paulo if (pw != NULL)
2705b9c547cSRui Paulo ret = -1;
2715b9c547cSRui Paulo ext_password_free(pw);
2725b9c547cSRui Paulo
2735b9c547cSRui Paulo ext_password_deinit(data);
2745b9c547cSRui Paulo
2755b9c547cSRui Paulo pw = ext_password_get(NULL, "foo");
2765b9c547cSRui Paulo if (pw != NULL)
2775b9c547cSRui Paulo ret = -1;
2785b9c547cSRui Paulo ext_password_free(pw);
2795b9c547cSRui Paulo
2805b9c547cSRui Paulo return ret;
2815b9c547cSRui Paulo }
2825b9c547cSRui Paulo
2835b9c547cSRui Paulo
trace_tests(void)2845b9c547cSRui Paulo static int trace_tests(void)
2855b9c547cSRui Paulo {
2865b9c547cSRui Paulo wpa_printf(MSG_INFO, "trace tests");
2875b9c547cSRui Paulo
2885b9c547cSRui Paulo wpa_trace_show("test backtrace");
2895b9c547cSRui Paulo wpa_trace_dump_funcname("test funcname", trace_tests);
2905b9c547cSRui Paulo
2915b9c547cSRui Paulo return 0;
2925b9c547cSRui Paulo }
2935b9c547cSRui Paulo
2945b9c547cSRui Paulo
base64_tests(void)2955b9c547cSRui Paulo static int base64_tests(void)
2965b9c547cSRui Paulo {
2975b9c547cSRui Paulo int errors = 0;
2985b9c547cSRui Paulo unsigned char *res;
299*c1d255d3SCy Schubert char *res2;
3005b9c547cSRui Paulo size_t res_len;
3015b9c547cSRui Paulo
3025b9c547cSRui Paulo wpa_printf(MSG_INFO, "base64 tests");
3035b9c547cSRui Paulo
304*c1d255d3SCy Schubert res2 = base64_encode("", ~0, &res_len);
305*c1d255d3SCy Schubert if (res2) {
306*c1d255d3SCy Schubert errors++;
307*c1d255d3SCy Schubert os_free(res2);
308*c1d255d3SCy Schubert }
309*c1d255d3SCy Schubert
310*c1d255d3SCy Schubert res2 = base64_encode("=", 1, &res_len);
311*c1d255d3SCy Schubert if (!res2 || res_len != 5 || res2[0] != 'P' || res2[1] != 'Q' ||
312*c1d255d3SCy Schubert res2[2] != '=' || res2[3] != '=' || res2[4] != '\n')
313*c1d255d3SCy Schubert errors++;
314*c1d255d3SCy Schubert os_free(res2);
315*c1d255d3SCy Schubert
316*c1d255d3SCy Schubert res2 = base64_encode("=", 1, NULL);
317*c1d255d3SCy Schubert if (!res2 || res2[0] != 'P' || res2[1] != 'Q' ||
318*c1d255d3SCy Schubert res2[2] != '=' || res2[3] != '=' || res2[4] != '\n')
319*c1d255d3SCy Schubert errors++;
320*c1d255d3SCy Schubert os_free(res2);
321*c1d255d3SCy Schubert
322*c1d255d3SCy Schubert res = base64_decode("", 0, &res_len);
3235b9c547cSRui Paulo if (res) {
3245b9c547cSRui Paulo errors++;
3255b9c547cSRui Paulo os_free(res);
3265b9c547cSRui Paulo }
3275b9c547cSRui Paulo
328*c1d255d3SCy Schubert res = base64_decode("a", 1, &res_len);
3295b9c547cSRui Paulo if (res) {
3305b9c547cSRui Paulo errors++;
3315b9c547cSRui Paulo os_free(res);
3325b9c547cSRui Paulo }
3335b9c547cSRui Paulo
334*c1d255d3SCy Schubert res = base64_decode("====", 4, &res_len);
3355b9c547cSRui Paulo if (res) {
3365b9c547cSRui Paulo errors++;
3375b9c547cSRui Paulo os_free(res);
3385b9c547cSRui Paulo }
3395b9c547cSRui Paulo
340*c1d255d3SCy Schubert res = base64_decode("PQ==", 4, &res_len);
3415b9c547cSRui Paulo if (!res || res_len != 1 || res[0] != '=')
3425b9c547cSRui Paulo errors++;
3435b9c547cSRui Paulo os_free(res);
3445b9c547cSRui Paulo
345*c1d255d3SCy Schubert res = base64_decode("P.Q-=!=*", 8, &res_len);
3465b9c547cSRui Paulo if (!res || res_len != 1 || res[0] != '=')
3475b9c547cSRui Paulo errors++;
3485b9c547cSRui Paulo os_free(res);
3495b9c547cSRui Paulo
3505b9c547cSRui Paulo if (errors) {
3515b9c547cSRui Paulo wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
3525b9c547cSRui Paulo return -1;
3535b9c547cSRui Paulo }
3545b9c547cSRui Paulo
3555b9c547cSRui Paulo return 0;
3565b9c547cSRui Paulo }
3575b9c547cSRui Paulo
3585b9c547cSRui Paulo
common_tests(void)3595b9c547cSRui Paulo static int common_tests(void)
3605b9c547cSRui Paulo {
361325151a3SRui Paulo char buf[3], longbuf[100];
3625b9c547cSRui Paulo u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
3635b9c547cSRui Paulo u8 bin[3];
3645b9c547cSRui Paulo int errors = 0;
3655b9c547cSRui Paulo struct wpa_freq_range_list ranges;
366325151a3SRui Paulo size_t len;
367325151a3SRui Paulo const char *txt;
368325151a3SRui Paulo u8 ssid[255];
3695b9c547cSRui Paulo
3705b9c547cSRui Paulo wpa_printf(MSG_INFO, "common tests");
3715b9c547cSRui Paulo
3725b9c547cSRui Paulo if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
3735b9c547cSRui Paulo errors++;
3745b9c547cSRui Paulo
3755b9c547cSRui Paulo if (wpa_scnprintf(buf, 0, "hello") != 0 ||
3765b9c547cSRui Paulo wpa_scnprintf(buf, 3, "hello") != 2)
3775b9c547cSRui Paulo errors++;
3785b9c547cSRui Paulo
3795b9c547cSRui Paulo if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
3805b9c547cSRui Paulo wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
3815b9c547cSRui Paulo errors++;
3825b9c547cSRui Paulo
3835b9c547cSRui Paulo if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
3845b9c547cSRui Paulo merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
3855b9c547cSRui Paulo errors++;
3865b9c547cSRui Paulo
3875b9c547cSRui Paulo if (dup_binstr(NULL, 0) != NULL)
3885b9c547cSRui Paulo errors++;
3895b9c547cSRui Paulo
3905b9c547cSRui Paulo if (freq_range_list_includes(NULL, 0) != 0)
3915b9c547cSRui Paulo errors++;
3925b9c547cSRui Paulo
3935b9c547cSRui Paulo os_memset(&ranges, 0, sizeof(ranges));
3945b9c547cSRui Paulo if (freq_range_list_parse(&ranges, "") != 0 ||
3955b9c547cSRui Paulo freq_range_list_includes(&ranges, 0) != 0 ||
3965b9c547cSRui Paulo freq_range_list_str(&ranges) != NULL)
3975b9c547cSRui Paulo errors++;
3985b9c547cSRui Paulo
3995b9c547cSRui Paulo if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
4005b9c547cSRui Paulo utf8_unescape("a", 1, NULL, 0) != 0 ||
4015b9c547cSRui Paulo utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
4025b9c547cSRui Paulo utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
4035b9c547cSRui Paulo utf8_unescape("abc", 3, buf, 3) != 3)
4045b9c547cSRui Paulo errors++;
4055b9c547cSRui Paulo
4065b9c547cSRui Paulo if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
4075b9c547cSRui Paulo errors++;
4085b9c547cSRui Paulo
4095b9c547cSRui Paulo if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
4105b9c547cSRui Paulo errors++;
4115b9c547cSRui Paulo
4125b9c547cSRui Paulo if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
4135b9c547cSRui Paulo utf8_escape("a", 1, NULL, 0) != 0 ||
4145b9c547cSRui Paulo utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
4155b9c547cSRui Paulo utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
4165b9c547cSRui Paulo utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
4175b9c547cSRui Paulo utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
4185b9c547cSRui Paulo utf8_escape("abc", 3, buf, 3) != 3)
4195b9c547cSRui Paulo errors++;
4205b9c547cSRui Paulo
4215b9c547cSRui Paulo if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
4225b9c547cSRui Paulo errors++;
4235b9c547cSRui Paulo
424325151a3SRui Paulo os_memset(ssid, 0, sizeof(ssid));
425325151a3SRui Paulo txt = wpa_ssid_txt(ssid, sizeof(ssid));
426325151a3SRui Paulo len = os_strlen(txt);
427325151a3SRui Paulo /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
428325151a3SRui Paulo if (len != SSID_MAX_LEN * 4) {
429325151a3SRui Paulo wpa_printf(MSG_ERROR,
430325151a3SRui Paulo "Unexpected wpa_ssid_txt() result with too long SSID");
431325151a3SRui Paulo errors++;
432325151a3SRui Paulo }
433325151a3SRui Paulo
434325151a3SRui Paulo if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
435325151a3SRui Paulo wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
436325151a3SRui Paulo os_strcmp(longbuf, "01-0") != 0)
437325151a3SRui Paulo errors++;
438325151a3SRui Paulo
4395b9c547cSRui Paulo if (errors) {
4405b9c547cSRui Paulo wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
4415b9c547cSRui Paulo return -1;
4425b9c547cSRui Paulo }
4435b9c547cSRui Paulo
4445b9c547cSRui Paulo return 0;
4455b9c547cSRui Paulo }
4465b9c547cSRui Paulo
4475b9c547cSRui Paulo
os_tests(void)448325151a3SRui Paulo static int os_tests(void)
449325151a3SRui Paulo {
450325151a3SRui Paulo int errors = 0;
451325151a3SRui Paulo void *ptr;
452325151a3SRui Paulo os_time_t t;
453325151a3SRui Paulo
454325151a3SRui Paulo wpa_printf(MSG_INFO, "os tests");
455325151a3SRui Paulo
456325151a3SRui Paulo ptr = os_calloc((size_t) -1, (size_t) -1);
457325151a3SRui Paulo if (ptr) {
458325151a3SRui Paulo errors++;
459325151a3SRui Paulo os_free(ptr);
460325151a3SRui Paulo }
461325151a3SRui Paulo ptr = os_calloc((size_t) 2, (size_t) -1);
462325151a3SRui Paulo if (ptr) {
463325151a3SRui Paulo errors++;
464325151a3SRui Paulo os_free(ptr);
465325151a3SRui Paulo }
466325151a3SRui Paulo ptr = os_calloc((size_t) -1, (size_t) 2);
467325151a3SRui Paulo if (ptr) {
468325151a3SRui Paulo errors++;
469325151a3SRui Paulo os_free(ptr);
470325151a3SRui Paulo }
471325151a3SRui Paulo
472325151a3SRui Paulo ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
473325151a3SRui Paulo if (ptr) {
474325151a3SRui Paulo errors++;
475325151a3SRui Paulo os_free(ptr);
476325151a3SRui Paulo }
477325151a3SRui Paulo
478325151a3SRui Paulo os_sleep(1, 1);
479325151a3SRui Paulo
480325151a3SRui Paulo if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
481325151a3SRui Paulo os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
482325151a3SRui Paulo os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
483325151a3SRui Paulo os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
484325151a3SRui Paulo os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
485325151a3SRui Paulo os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
486325151a3SRui Paulo os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
487325151a3SRui Paulo os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
488325151a3SRui Paulo os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
489325151a3SRui Paulo os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
490325151a3SRui Paulo os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
491325151a3SRui Paulo os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
492325151a3SRui Paulo os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
493325151a3SRui Paulo os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
494325151a3SRui Paulo errors++;
495325151a3SRui Paulo
496325151a3SRui Paulo if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
497325151a3SRui Paulo os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
498325151a3SRui Paulo os_unsetenv("hwsim_test_env") != 0)
499325151a3SRui Paulo errors++;
500325151a3SRui Paulo
501325151a3SRui Paulo if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
502325151a3SRui Paulo errors++;
503325151a3SRui Paulo
504325151a3SRui Paulo if (errors) {
505325151a3SRui Paulo wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
506325151a3SRui Paulo return -1;
507325151a3SRui Paulo }
508325151a3SRui Paulo
509325151a3SRui Paulo return 0;
510325151a3SRui Paulo }
511325151a3SRui Paulo
512325151a3SRui Paulo
wpabuf_tests(void)513325151a3SRui Paulo static int wpabuf_tests(void)
514325151a3SRui Paulo {
515325151a3SRui Paulo int errors = 0;
516325151a3SRui Paulo void *ptr;
517325151a3SRui Paulo struct wpabuf *buf;
518325151a3SRui Paulo
519325151a3SRui Paulo wpa_printf(MSG_INFO, "wpabuf tests");
520325151a3SRui Paulo
521325151a3SRui Paulo ptr = os_malloc(100);
522325151a3SRui Paulo if (ptr) {
523325151a3SRui Paulo buf = wpabuf_alloc_ext_data(ptr, 100);
524325151a3SRui Paulo if (buf) {
525325151a3SRui Paulo if (wpabuf_resize(&buf, 100) < 0)
526325151a3SRui Paulo errors++;
527325151a3SRui Paulo else
528325151a3SRui Paulo wpabuf_put(buf, 100);
529325151a3SRui Paulo wpabuf_free(buf);
530325151a3SRui Paulo } else {
531325151a3SRui Paulo errors++;
532325151a3SRui Paulo os_free(ptr);
533325151a3SRui Paulo }
534325151a3SRui Paulo } else {
535325151a3SRui Paulo errors++;
536325151a3SRui Paulo }
537325151a3SRui Paulo
538325151a3SRui Paulo buf = wpabuf_alloc(100);
539325151a3SRui Paulo if (buf) {
540325151a3SRui Paulo struct wpabuf *buf2;
541325151a3SRui Paulo
542325151a3SRui Paulo wpabuf_put(buf, 100);
543325151a3SRui Paulo if (wpabuf_resize(&buf, 100) < 0)
544325151a3SRui Paulo errors++;
545325151a3SRui Paulo else
546325151a3SRui Paulo wpabuf_put(buf, 100);
547325151a3SRui Paulo buf2 = wpabuf_concat(buf, NULL);
548325151a3SRui Paulo if (buf2 != buf)
549325151a3SRui Paulo errors++;
550325151a3SRui Paulo wpabuf_free(buf2);
551325151a3SRui Paulo } else {
552325151a3SRui Paulo errors++;
553325151a3SRui Paulo }
554325151a3SRui Paulo
555325151a3SRui Paulo buf = NULL;
556325151a3SRui Paulo buf = wpabuf_zeropad(buf, 10);
557325151a3SRui Paulo if (buf != NULL)
558325151a3SRui Paulo errors++;
559325151a3SRui Paulo
560325151a3SRui Paulo if (errors) {
561325151a3SRui Paulo wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
562325151a3SRui Paulo return -1;
563325151a3SRui Paulo }
564325151a3SRui Paulo
565325151a3SRui Paulo return 0;
566325151a3SRui Paulo }
567325151a3SRui Paulo
568325151a3SRui Paulo
ip_addr_tests(void)569325151a3SRui Paulo static int ip_addr_tests(void)
570325151a3SRui Paulo {
571325151a3SRui Paulo int errors = 0;
572325151a3SRui Paulo struct hostapd_ip_addr addr;
573325151a3SRui Paulo char buf[100];
574325151a3SRui Paulo
575325151a3SRui Paulo wpa_printf(MSG_INFO, "ip_addr tests");
576325151a3SRui Paulo
577325151a3SRui Paulo if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
578325151a3SRui Paulo addr.af != AF_INET ||
579325151a3SRui Paulo hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
580325151a3SRui Paulo hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
581325151a3SRui Paulo hostapd_ip_txt(&addr, buf, 0) != NULL ||
582325151a3SRui Paulo hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
583325151a3SRui Paulo errors++;
584325151a3SRui Paulo
585325151a3SRui Paulo if (hostapd_parse_ip_addr("::", &addr) != 0 ||
586325151a3SRui Paulo addr.af != AF_INET6 ||
587325151a3SRui Paulo hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
588325151a3SRui Paulo hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
589325151a3SRui Paulo errors++;
590325151a3SRui Paulo
591325151a3SRui Paulo if (errors) {
592325151a3SRui Paulo wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
593325151a3SRui Paulo return -1;
594325151a3SRui Paulo }
595325151a3SRui Paulo
596325151a3SRui Paulo return 0;
597325151a3SRui Paulo }
598325151a3SRui Paulo
599325151a3SRui Paulo
600325151a3SRui Paulo struct test_eloop {
601325151a3SRui Paulo unsigned int magic;
602325151a3SRui Paulo int close_in_timeout;
603325151a3SRui Paulo int pipefd1[2];
604325151a3SRui Paulo int pipefd2[2];
605325151a3SRui Paulo };
606325151a3SRui Paulo
607325151a3SRui Paulo
608325151a3SRui Paulo static void eloop_tests_start(int close_in_timeout);
609325151a3SRui Paulo
610325151a3SRui Paulo
eloop_test_read_2(int sock,void * eloop_ctx,void * sock_ctx)611325151a3SRui Paulo static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
612325151a3SRui Paulo {
613325151a3SRui Paulo struct test_eloop *t = eloop_ctx;
614325151a3SRui Paulo ssize_t res;
615325151a3SRui Paulo char buf[10];
616325151a3SRui Paulo
617325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
618325151a3SRui Paulo
619325151a3SRui Paulo if (t->magic != 0x12345678) {
620325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
621325151a3SRui Paulo __func__, t->magic);
622325151a3SRui Paulo }
623325151a3SRui Paulo
624325151a3SRui Paulo if (t->pipefd2[0] != sock) {
625325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
626325151a3SRui Paulo __func__, sock, t->pipefd2[0]);
627325151a3SRui Paulo }
628325151a3SRui Paulo
629325151a3SRui Paulo res = read(sock, buf, sizeof(buf));
630325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
631325151a3SRui Paulo __func__, sock, (int) res);
632325151a3SRui Paulo }
633325151a3SRui Paulo
634325151a3SRui Paulo
eloop_test_read_2_wrong(int sock,void * eloop_ctx,void * sock_ctx)635325151a3SRui Paulo static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
636325151a3SRui Paulo {
637325151a3SRui Paulo struct test_eloop *t = eloop_ctx;
638325151a3SRui Paulo
639325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
640325151a3SRui Paulo
641325151a3SRui Paulo if (t->magic != 0x12345678) {
642325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
643325151a3SRui Paulo __func__, t->magic);
644325151a3SRui Paulo }
645325151a3SRui Paulo
646325151a3SRui Paulo if (t->pipefd2[0] != sock) {
647325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
648325151a3SRui Paulo __func__, sock, t->pipefd2[0]);
649325151a3SRui Paulo }
650325151a3SRui Paulo
651325151a3SRui Paulo /*
652325151a3SRui Paulo * This is expected to block due to the original socket with data having
653325151a3SRui Paulo * been closed and no new data having been written to the new socket
654325151a3SRui Paulo * with the same fd. To avoid blocking the process during test, skip the
655325151a3SRui Paulo * read here.
656325151a3SRui Paulo */
657325151a3SRui Paulo wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
658325151a3SRui Paulo __func__);
659325151a3SRui Paulo }
660325151a3SRui Paulo
661325151a3SRui Paulo
reopen_pipefd2(struct test_eloop * t)662325151a3SRui Paulo static void reopen_pipefd2(struct test_eloop *t)
663325151a3SRui Paulo {
664325151a3SRui Paulo if (t->pipefd2[0] < 0) {
665325151a3SRui Paulo wpa_printf(MSG_INFO, "pipefd2 had been closed");
666325151a3SRui Paulo } else {
667325151a3SRui Paulo int res;
668325151a3SRui Paulo
669325151a3SRui Paulo wpa_printf(MSG_INFO, "close pipefd2");
670325151a3SRui Paulo eloop_unregister_read_sock(t->pipefd2[0]);
671325151a3SRui Paulo close(t->pipefd2[0]);
672325151a3SRui Paulo t->pipefd2[0] = -1;
673325151a3SRui Paulo close(t->pipefd2[1]);
674325151a3SRui Paulo t->pipefd2[1] = -1;
675325151a3SRui Paulo
676325151a3SRui Paulo res = pipe(t->pipefd2);
677325151a3SRui Paulo if (res < 0) {
678325151a3SRui Paulo wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
679325151a3SRui Paulo t->pipefd2[0] = -1;
680325151a3SRui Paulo t->pipefd2[1] = -1;
681325151a3SRui Paulo return;
682325151a3SRui Paulo }
683325151a3SRui Paulo
684325151a3SRui Paulo wpa_printf(MSG_INFO,
685325151a3SRui Paulo "re-register pipefd2 with new sockets %d,%d",
686325151a3SRui Paulo t->pipefd2[0], t->pipefd2[1]);
687325151a3SRui Paulo eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
688325151a3SRui Paulo t, NULL);
689325151a3SRui Paulo }
690325151a3SRui Paulo }
691325151a3SRui Paulo
692325151a3SRui Paulo
eloop_test_read_1(int sock,void * eloop_ctx,void * sock_ctx)693325151a3SRui Paulo static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
694325151a3SRui Paulo {
695325151a3SRui Paulo struct test_eloop *t = eloop_ctx;
696325151a3SRui Paulo ssize_t res;
697325151a3SRui Paulo char buf[10];
698325151a3SRui Paulo
699325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
700325151a3SRui Paulo
701325151a3SRui Paulo if (t->magic != 0x12345678) {
702325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
703325151a3SRui Paulo __func__, t->magic);
704325151a3SRui Paulo }
705325151a3SRui Paulo
706325151a3SRui Paulo if (t->pipefd1[0] != sock) {
707325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
708325151a3SRui Paulo __func__, sock, t->pipefd1[0]);
709325151a3SRui Paulo }
710325151a3SRui Paulo
711325151a3SRui Paulo res = read(sock, buf, sizeof(buf));
712325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
713325151a3SRui Paulo __func__, sock, (int) res);
714325151a3SRui Paulo
715325151a3SRui Paulo if (!t->close_in_timeout)
716325151a3SRui Paulo reopen_pipefd2(t);
717325151a3SRui Paulo }
718325151a3SRui Paulo
719325151a3SRui Paulo
eloop_test_cb(void * eloop_data,void * user_ctx)720325151a3SRui Paulo static void eloop_test_cb(void *eloop_data, void *user_ctx)
721325151a3SRui Paulo {
722325151a3SRui Paulo struct test_eloop *t = eloop_data;
723325151a3SRui Paulo
724325151a3SRui Paulo wpa_printf(MSG_INFO, "%s", __func__);
725325151a3SRui Paulo
726325151a3SRui Paulo if (t->magic != 0x12345678) {
727325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
728325151a3SRui Paulo __func__, t->magic);
729325151a3SRui Paulo }
730325151a3SRui Paulo
731325151a3SRui Paulo if (t->close_in_timeout)
732325151a3SRui Paulo reopen_pipefd2(t);
733325151a3SRui Paulo }
734325151a3SRui Paulo
735325151a3SRui Paulo
eloop_test_timeout(void * eloop_data,void * user_ctx)736325151a3SRui Paulo static void eloop_test_timeout(void *eloop_data, void *user_ctx)
737325151a3SRui Paulo {
738325151a3SRui Paulo struct test_eloop *t = eloop_data;
739325151a3SRui Paulo int next_run = 0;
740325151a3SRui Paulo
741325151a3SRui Paulo wpa_printf(MSG_INFO, "%s", __func__);
742325151a3SRui Paulo
743325151a3SRui Paulo if (t->magic != 0x12345678) {
744325151a3SRui Paulo wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
745325151a3SRui Paulo __func__, t->magic);
746325151a3SRui Paulo }
747325151a3SRui Paulo
748325151a3SRui Paulo if (t->pipefd1[0] >= 0) {
749325151a3SRui Paulo wpa_printf(MSG_INFO, "pipefd1 had not been closed");
750325151a3SRui Paulo eloop_unregister_read_sock(t->pipefd1[0]);
751325151a3SRui Paulo close(t->pipefd1[0]);
752325151a3SRui Paulo t->pipefd1[0] = -1;
753325151a3SRui Paulo close(t->pipefd1[1]);
754325151a3SRui Paulo t->pipefd1[1] = -1;
755325151a3SRui Paulo }
756325151a3SRui Paulo
757325151a3SRui Paulo if (t->pipefd2[0] >= 0) {
758325151a3SRui Paulo wpa_printf(MSG_INFO, "pipefd2 had not been closed");
759325151a3SRui Paulo eloop_unregister_read_sock(t->pipefd2[0]);
760325151a3SRui Paulo close(t->pipefd2[0]);
761325151a3SRui Paulo t->pipefd2[0] = -1;
762325151a3SRui Paulo close(t->pipefd2[1]);
763325151a3SRui Paulo t->pipefd2[1] = -1;
764325151a3SRui Paulo }
765325151a3SRui Paulo
766325151a3SRui Paulo next_run = t->close_in_timeout;
767325151a3SRui Paulo t->magic = 0;
768325151a3SRui Paulo wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
769325151a3SRui Paulo os_free(t);
770325151a3SRui Paulo
771325151a3SRui Paulo if (next_run)
772325151a3SRui Paulo eloop_tests_start(0);
773325151a3SRui Paulo }
774325151a3SRui Paulo
775325151a3SRui Paulo
eloop_tests_start(int close_in_timeout)776325151a3SRui Paulo static void eloop_tests_start(int close_in_timeout)
777325151a3SRui Paulo {
778325151a3SRui Paulo struct test_eloop *t;
779325151a3SRui Paulo int res;
780325151a3SRui Paulo
781325151a3SRui Paulo t = os_zalloc(sizeof(*t));
782325151a3SRui Paulo if (!t)
783325151a3SRui Paulo return;
784325151a3SRui Paulo t->magic = 0x12345678;
785325151a3SRui Paulo t->close_in_timeout = close_in_timeout;
786325151a3SRui Paulo
787325151a3SRui Paulo wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
788325151a3SRui Paulo t, close_in_timeout);
789325151a3SRui Paulo
790325151a3SRui Paulo res = pipe(t->pipefd1);
791325151a3SRui Paulo if (res < 0) {
792325151a3SRui Paulo wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
793325151a3SRui Paulo os_free(t);
794325151a3SRui Paulo return;
795325151a3SRui Paulo }
796325151a3SRui Paulo
797325151a3SRui Paulo res = pipe(t->pipefd2);
798325151a3SRui Paulo if (res < 0) {
799325151a3SRui Paulo wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
800325151a3SRui Paulo close(t->pipefd1[0]);
801325151a3SRui Paulo close(t->pipefd1[1]);
802325151a3SRui Paulo os_free(t);
803325151a3SRui Paulo return;
804325151a3SRui Paulo }
805325151a3SRui Paulo
806325151a3SRui Paulo wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
807325151a3SRui Paulo t->pipefd1[0], t->pipefd1[1],
808325151a3SRui Paulo t->pipefd2[0], t->pipefd2[1]);
809325151a3SRui Paulo
810325151a3SRui Paulo eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
811325151a3SRui Paulo eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
812325151a3SRui Paulo eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
813325151a3SRui Paulo eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
814325151a3SRui Paulo
815325151a3SRui Paulo if (write(t->pipefd1[1], "HELLO", 5) < 0)
816325151a3SRui Paulo wpa_printf(MSG_INFO, "write: %s", strerror(errno));
817325151a3SRui Paulo if (write(t->pipefd2[1], "TEST", 4) < 0)
818325151a3SRui Paulo wpa_printf(MSG_INFO, "write: %s", strerror(errno));
819325151a3SRui Paulo os_sleep(0, 50000);
820325151a3SRui Paulo wpa_printf(MSG_INFO, "waiting for eloop callbacks");
821325151a3SRui Paulo }
822325151a3SRui Paulo
823325151a3SRui Paulo
eloop_tests_run(void * eloop_data,void * user_ctx)824325151a3SRui Paulo static void eloop_tests_run(void *eloop_data, void *user_ctx)
825325151a3SRui Paulo {
826325151a3SRui Paulo eloop_tests_start(1);
827325151a3SRui Paulo }
828325151a3SRui Paulo
829325151a3SRui Paulo
eloop_tests(void)830325151a3SRui Paulo static int eloop_tests(void)
831325151a3SRui Paulo {
832325151a3SRui Paulo wpa_printf(MSG_INFO, "schedule eloop tests to be run");
833325151a3SRui Paulo
834325151a3SRui Paulo /*
835325151a3SRui Paulo * Cannot return error from these without a significant design change,
836325151a3SRui Paulo * so for now, run the tests from a scheduled timeout and require
837325151a3SRui Paulo * separate verification of the results from the debug log.
838325151a3SRui Paulo */
839325151a3SRui Paulo eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
840325151a3SRui Paulo
841325151a3SRui Paulo return 0;
842325151a3SRui Paulo }
843325151a3SRui Paulo
844325151a3SRui Paulo
84585732ac8SCy Schubert #ifdef CONFIG_JSON
84685732ac8SCy Schubert struct json_test_data {
84785732ac8SCy Schubert const char *json;
84885732ac8SCy Schubert const char *tree;
84985732ac8SCy Schubert };
85085732ac8SCy Schubert
85185732ac8SCy Schubert static const struct json_test_data json_test_cases[] = {
85285732ac8SCy Schubert { "{}", "[1:OBJECT:]" },
85385732ac8SCy Schubert { "[]", "[1:ARRAY:]" },
85485732ac8SCy Schubert { "{", NULL },
85585732ac8SCy Schubert { "[", NULL },
85685732ac8SCy Schubert { "}", NULL },
85785732ac8SCy Schubert { "]", NULL },
85885732ac8SCy Schubert { "[[]]", "[1:ARRAY:][2:ARRAY:]" },
85985732ac8SCy Schubert { "{\"t\":\"test\"}", "[1:OBJECT:][2:STRING:t]" },
86085732ac8SCy Schubert { "{\"t\":123}", "[1:OBJECT:][2:NUMBER:t]" },
86185732ac8SCy Schubert { "{\"t\":true}", "[1:OBJECT:][2:BOOLEAN:t]" },
86285732ac8SCy Schubert { "{\"t\":false}", "[1:OBJECT:][2:BOOLEAN:t]" },
86385732ac8SCy Schubert { "{\"t\":null}", "[1:OBJECT:][2:NULL:t]" },
86485732ac8SCy Schubert { "{\"t\":truetrue}", NULL },
86585732ac8SCy Schubert { "\"test\"", "[1:STRING:]" },
86685732ac8SCy Schubert { "123", "[1:NUMBER:]" },
86785732ac8SCy Schubert { "true", "[1:BOOLEAN:]" },
86885732ac8SCy Schubert { "false", "[1:BOOLEAN:]" },
86985732ac8SCy Schubert { "null", "[1:NULL:]" },
87085732ac8SCy Schubert { "truetrue", NULL },
87185732ac8SCy Schubert { " {\t\n\r\"a\"\n:\r1\n,\n\"b\":3\n}\n",
87285732ac8SCy Schubert "[1:OBJECT:][2:NUMBER:a][2:NUMBER:b]" },
87385732ac8SCy Schubert { ",", NULL },
87485732ac8SCy Schubert { "{,}", NULL },
87585732ac8SCy Schubert { "[,]", NULL },
87685732ac8SCy Schubert { ":", NULL },
87785732ac8SCy Schubert { "{:}", NULL },
87885732ac8SCy Schubert { "[:]", NULL },
87985732ac8SCy Schubert { "{ \"\\u005c\" : \"\\u005c\" }", "[1:OBJECT:][2:STRING:\\]" },
88085732ac8SCy Schubert { "[{},{}]", "[1:ARRAY:][2:OBJECT:][2:OBJECT:]" },
88185732ac8SCy Schubert { "[1,2]", "[1:ARRAY:][2:NUMBER:][2:NUMBER:]" },
88285732ac8SCy Schubert { "[\"1\",\"2\"]", "[1:ARRAY:][2:STRING:][2:STRING:]" },
88385732ac8SCy Schubert { "[true,false]", "[1:ARRAY:][2:BOOLEAN:][2:BOOLEAN:]" },
88485732ac8SCy Schubert };
88585732ac8SCy Schubert #endif /* CONFIG_JSON */
88685732ac8SCy Schubert
88785732ac8SCy Schubert
json_tests(void)88885732ac8SCy Schubert static int json_tests(void)
88985732ac8SCy Schubert {
89085732ac8SCy Schubert #ifdef CONFIG_JSON
89185732ac8SCy Schubert unsigned int i;
89285732ac8SCy Schubert struct json_token *root;
89385732ac8SCy Schubert char buf[1000];
89485732ac8SCy Schubert
89585732ac8SCy Schubert wpa_printf(MSG_INFO, "JSON tests");
89685732ac8SCy Schubert
89785732ac8SCy Schubert for (i = 0; i < ARRAY_SIZE(json_test_cases); i++) {
89885732ac8SCy Schubert const struct json_test_data *test = &json_test_cases[i];
89985732ac8SCy Schubert int res = 0;
90085732ac8SCy Schubert
90185732ac8SCy Schubert root = json_parse(test->json, os_strlen(test->json));
90285732ac8SCy Schubert if ((root && !test->tree) || (!root && test->tree)) {
90385732ac8SCy Schubert wpa_printf(MSG_INFO, "JSON test %u failed", i);
90485732ac8SCy Schubert res = -1;
90585732ac8SCy Schubert } else if (root) {
90685732ac8SCy Schubert json_print_tree(root, buf, sizeof(buf));
90785732ac8SCy Schubert if (os_strcmp(buf, test->tree) != 0) {
90885732ac8SCy Schubert wpa_printf(MSG_INFO,
90985732ac8SCy Schubert "JSON test %u tree mismatch: %s %s",
91085732ac8SCy Schubert i, buf, test->tree);
91185732ac8SCy Schubert res = -1;
91285732ac8SCy Schubert }
91385732ac8SCy Schubert }
91485732ac8SCy Schubert json_free(root);
91585732ac8SCy Schubert if (res < 0)
91685732ac8SCy Schubert return -1;
91785732ac8SCy Schubert
91885732ac8SCy Schubert }
91985732ac8SCy Schubert #endif /* CONFIG_JSON */
92085732ac8SCy Schubert return 0;
92185732ac8SCy Schubert }
92285732ac8SCy Schubert
92385732ac8SCy Schubert
const_time_tests(void)9244bc52338SCy Schubert static int const_time_tests(void)
9254bc52338SCy Schubert {
9264bc52338SCy Schubert struct const_time_fill_msb_test {
9274bc52338SCy Schubert unsigned int val;
9284bc52338SCy Schubert unsigned int expected;
9294bc52338SCy Schubert } const_time_fill_msb_tests[] = {
9304bc52338SCy Schubert { 0, 0 },
9314bc52338SCy Schubert { 1, 0 },
9324bc52338SCy Schubert { 2, 0 },
933*c1d255d3SCy Schubert { 1U << (sizeof(unsigned int) * 8 - 1), ~0 },
9344bc52338SCy Schubert { ~0 - 1, ~0 },
9354bc52338SCy Schubert { ~0, ~0 }
9364bc52338SCy Schubert };
9374bc52338SCy Schubert struct const_time_is_zero_test {
9384bc52338SCy Schubert unsigned int val;
9394bc52338SCy Schubert unsigned int expected;
9404bc52338SCy Schubert } const_time_is_zero_tests[] = {
9414bc52338SCy Schubert { 0, ~0 },
9424bc52338SCy Schubert { 1, 0 },
9434bc52338SCy Schubert { 2, 0 },
944*c1d255d3SCy Schubert { 1U << (sizeof(unsigned int) * 8 - 1), 0 },
9454bc52338SCy Schubert { ~0 - 1, 0 },
9464bc52338SCy Schubert { ~0, 0 }
9474bc52338SCy Schubert };
9484bc52338SCy Schubert struct const_time_eq_test {
9494bc52338SCy Schubert unsigned int a;
9504bc52338SCy Schubert unsigned int b;
9514bc52338SCy Schubert unsigned int expected;
9524bc52338SCy Schubert unsigned int expected_u8;
9534bc52338SCy Schubert } const_time_eq_tests[] = {
9544bc52338SCy Schubert { 0, 1, 0, 0 },
9554bc52338SCy Schubert { 1, 2, 0, 0 },
9564bc52338SCy Schubert { 1, 1, ~0, 0xff },
9574bc52338SCy Schubert { ~0, ~0, ~0, 0xff },
9584bc52338SCy Schubert { ~0, ~0 - 1, 0, 0 },
9594bc52338SCy Schubert { 0, 0, ~0, 0xff }
9604bc52338SCy Schubert };
9614bc52338SCy Schubert struct const_time_eq_bin_test {
9624bc52338SCy Schubert u8 *a;
9634bc52338SCy Schubert u8 *b;
9644bc52338SCy Schubert size_t len;
9654bc52338SCy Schubert unsigned int expected;
9664bc52338SCy Schubert } const_time_eq_bin_tests[] = {
9674bc52338SCy Schubert { (u8 *) "", (u8 *) "", 0, ~0 },
9684bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "abcde", 5, ~0 },
9694bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "Abcde", 5, 0 },
9704bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "aBcde", 5, 0 },
9714bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "abCde", 5, 0 },
9724bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "abcDe", 5, 0 },
9734bc52338SCy Schubert { (u8 *) "abcde", (u8 *) "abcdE", 5, 0 },
9744bc52338SCy Schubert { (u8 *) "\x00", (u8 *) "\x01", 1, 0 },
9754bc52338SCy Schubert { (u8 *) "\x00", (u8 *) "\x80", 1, 0 },
9764bc52338SCy Schubert { (u8 *) "\x00", (u8 *) "\x00", 1, ~0 }
9774bc52338SCy Schubert };
9784bc52338SCy Schubert struct const_time_select_test {
9794bc52338SCy Schubert unsigned int mask;
9804bc52338SCy Schubert unsigned int true_val;
9814bc52338SCy Schubert unsigned int false_val;
9824bc52338SCy Schubert unsigned int expected;
9834bc52338SCy Schubert } const_time_select_tests[] = {
9844bc52338SCy Schubert { ~0, ~0, ~0, ~0 },
9854bc52338SCy Schubert { 0, ~0, ~0, ~0 },
9864bc52338SCy Schubert { ~0, ~0, 0, ~0 },
9874bc52338SCy Schubert { 0, ~0, 0, 0 },
9884bc52338SCy Schubert { ~0, 0xaaaaaaaa, 0x55555555, 0xaaaaaaaa },
9894bc52338SCy Schubert { 0, 0xaaaaaaaa, 0x55555555, 0x55555555 },
9904bc52338SCy Schubert { ~0, 3, 3, 3 },
9914bc52338SCy Schubert { 0, 3, 3, 3 },
9924bc52338SCy Schubert { ~0, 1, 2, 1 },
9934bc52338SCy Schubert { 0, 1, 2, 2 }
9944bc52338SCy Schubert };
9954bc52338SCy Schubert struct const_time_select_int_test {
9964bc52338SCy Schubert unsigned int mask;
9974bc52338SCy Schubert int true_val;
9984bc52338SCy Schubert int false_val;
9994bc52338SCy Schubert int expected;
10004bc52338SCy Schubert } const_time_select_int_tests[] = {
10014bc52338SCy Schubert { ~0, -128, 127, -128 },
10024bc52338SCy Schubert { 0, -128, 127, 127 },
10034bc52338SCy Schubert { ~0, -2147483648, 2147483647, -2147483648 },
10044bc52338SCy Schubert { 0, -2147483648, 2147483647, 2147483647 },
10054bc52338SCy Schubert { ~0, 0, 0, 0 },
10064bc52338SCy Schubert { 0, 0, 0, 0 },
10074bc52338SCy Schubert { ~0, -1, 1, -1 },
10084bc52338SCy Schubert { 0, -1, 1, 1 }
10094bc52338SCy Schubert };
10104bc52338SCy Schubert struct const_time_select_u8_test {
10114bc52338SCy Schubert u8 mask;
10124bc52338SCy Schubert u8 true_val;
10134bc52338SCy Schubert u8 false_val;
10144bc52338SCy Schubert u8 expected;
10154bc52338SCy Schubert } const_time_select_u8_tests[] = {
10164bc52338SCy Schubert { ~0, ~0, ~0, ~0 },
10174bc52338SCy Schubert { 0, ~0, ~0, ~0 },
10184bc52338SCy Schubert { ~0, ~0, 0, ~0 },
10194bc52338SCy Schubert { 0, ~0, 0, 0 },
10204bc52338SCy Schubert { ~0, 0xaa, 0x55, 0xaa },
10214bc52338SCy Schubert { 0, 0xaa, 0x55, 0x55 },
10224bc52338SCy Schubert { ~0, 1, 2, 1 },
10234bc52338SCy Schubert { 0, 1, 2, 2 }
10244bc52338SCy Schubert };
10254bc52338SCy Schubert struct const_time_select_s8_test {
10264bc52338SCy Schubert u8 mask;
10274bc52338SCy Schubert s8 true_val;
10284bc52338SCy Schubert s8 false_val;
10294bc52338SCy Schubert s8 expected;
10304bc52338SCy Schubert } const_time_select_s8_tests[] = {
10314bc52338SCy Schubert { ~0, -128, 127, -128 },
10324bc52338SCy Schubert { 0, -128, 127, 127 },
10334bc52338SCy Schubert { ~0, 0, 0, 0 },
10344bc52338SCy Schubert { 0, 0, 0, 0 },
10354bc52338SCy Schubert { ~0, -1, 1, -1 },
10364bc52338SCy Schubert { 0, -1, 1, 1 }
10374bc52338SCy Schubert };
10384bc52338SCy Schubert struct const_time_select_bin_test {
10394bc52338SCy Schubert u8 mask;
10404bc52338SCy Schubert u8 *true_val;
10414bc52338SCy Schubert u8 *false_val;
10424bc52338SCy Schubert size_t len;
10434bc52338SCy Schubert u8 *expected;
10444bc52338SCy Schubert } const_time_select_bin_tests[] = {
10454bc52338SCy Schubert { ~0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "abcde" },
10464bc52338SCy Schubert { 0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "ABCDE" },
10474bc52338SCy Schubert { ~0, (u8 *) "", (u8 *) "", 0, (u8 *) "" },
10484bc52338SCy Schubert { 0, (u8 *) "", (u8 *) "", 0, (u8 *) "" }
10494bc52338SCy Schubert };
10504bc52338SCy Schubert struct const_time_memcmp_test {
10514bc52338SCy Schubert char *a;
10524bc52338SCy Schubert char *b;
10534bc52338SCy Schubert size_t len;
10544bc52338SCy Schubert int expected;
10554bc52338SCy Schubert } const_time_memcmp_tests[] = {
10564bc52338SCy Schubert { "abcde", "abcde", 5, 0 },
10574bc52338SCy Schubert { "abcde", "bbcde", 5, -1 },
10584bc52338SCy Schubert { "bbcde", "abcde", 5, 1 },
10594bc52338SCy Schubert { "accde", "abcde", 5, 1 },
10604bc52338SCy Schubert { "abcee", "abcde", 5, 1 },
10614bc52338SCy Schubert { "abcdf", "abcde", 5, 1 },
10624bc52338SCy Schubert { "cbcde", "aXXXX", 5, 2 },
10634bc52338SCy Schubert { "a", "d", 1, -3 },
10644bc52338SCy Schubert { "", "", 0, 0 }
10654bc52338SCy Schubert };
10664bc52338SCy Schubert unsigned int i;
10674bc52338SCy Schubert int ret = 0;
10684bc52338SCy Schubert
10694bc52338SCy Schubert wpa_printf(MSG_INFO, "constant time tests");
10704bc52338SCy Schubert
10714bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_fill_msb_tests); i++) {
10724bc52338SCy Schubert struct const_time_fill_msb_test *test;
10734bc52338SCy Schubert
10744bc52338SCy Schubert test = &const_time_fill_msb_tests[i];
10754bc52338SCy Schubert if (const_time_fill_msb(test->val) != test->expected) {
10764bc52338SCy Schubert wpa_printf(MSG_ERROR,
10774bc52338SCy Schubert "const_time_fill_msb(0x%x) test failed",
10784bc52338SCy Schubert test->val);
10794bc52338SCy Schubert ret = -1;
10804bc52338SCy Schubert }
10814bc52338SCy Schubert }
10824bc52338SCy Schubert
10834bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_is_zero_tests); i++) {
10844bc52338SCy Schubert struct const_time_is_zero_test *test;
10854bc52338SCy Schubert
10864bc52338SCy Schubert test = &const_time_is_zero_tests[i];
10874bc52338SCy Schubert if (const_time_is_zero(test->val) != test->expected) {
10884bc52338SCy Schubert wpa_printf(MSG_ERROR,
10894bc52338SCy Schubert "const_time_is_zero(0x%x) test failed",
10904bc52338SCy Schubert test->val);
10914bc52338SCy Schubert ret = -1;
10924bc52338SCy Schubert }
10934bc52338SCy Schubert }
10944bc52338SCy Schubert
10954bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_eq_tests); i++) {
10964bc52338SCy Schubert struct const_time_eq_test *test;
10974bc52338SCy Schubert
10984bc52338SCy Schubert test = &const_time_eq_tests[i];
10994bc52338SCy Schubert if (const_time_eq(test->a, test->b) != test->expected) {
11004bc52338SCy Schubert wpa_printf(MSG_ERROR,
11014bc52338SCy Schubert "const_time_eq(0x%x,0x%x) test failed",
11024bc52338SCy Schubert test->a, test->b);
11034bc52338SCy Schubert ret = -1;
11044bc52338SCy Schubert }
11054bc52338SCy Schubert if (const_time_eq_u8(test->a, test->b) != test->expected_u8) {
11064bc52338SCy Schubert wpa_printf(MSG_ERROR,
11074bc52338SCy Schubert "const_time_eq_u8(0x%x,0x%x) test failed",
11084bc52338SCy Schubert test->a, test->b);
11094bc52338SCy Schubert ret = -1;
11104bc52338SCy Schubert }
11114bc52338SCy Schubert }
11124bc52338SCy Schubert
11134bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_eq_bin_tests); i++) {
11144bc52338SCy Schubert struct const_time_eq_bin_test *test;
11154bc52338SCy Schubert
11164bc52338SCy Schubert test = &const_time_eq_bin_tests[i];
11174bc52338SCy Schubert if (const_time_eq_bin(test->a, test->b, test->len) !=
11184bc52338SCy Schubert test->expected) {
11194bc52338SCy Schubert wpa_printf(MSG_ERROR,
11204bc52338SCy Schubert "const_time_eq_bin(len=%u) test failed",
11214bc52338SCy Schubert (unsigned int) test->len);
11224bc52338SCy Schubert ret = -1;
11234bc52338SCy Schubert }
11244bc52338SCy Schubert }
11254bc52338SCy Schubert
11264bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_select_tests); i++) {
11274bc52338SCy Schubert struct const_time_select_test *test;
11284bc52338SCy Schubert
11294bc52338SCy Schubert test = &const_time_select_tests[i];
11304bc52338SCy Schubert if (const_time_select(test->mask, test->true_val,
11314bc52338SCy Schubert test->false_val) != test->expected) {
11324bc52338SCy Schubert wpa_printf(MSG_ERROR,
11334bc52338SCy Schubert "const_time_select(0x%x,0x%x,0x%x) test failed",
11344bc52338SCy Schubert test->mask, test->true_val, test->false_val);
11354bc52338SCy Schubert ret = -1;
11364bc52338SCy Schubert }
11374bc52338SCy Schubert }
11384bc52338SCy Schubert
11394bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_select_int_tests); i++) {
11404bc52338SCy Schubert struct const_time_select_int_test *test;
11414bc52338SCy Schubert
11424bc52338SCy Schubert test = &const_time_select_int_tests[i];
11434bc52338SCy Schubert if (const_time_select_int(test->mask, test->true_val,
11444bc52338SCy Schubert test->false_val) != test->expected) {
11454bc52338SCy Schubert wpa_printf(MSG_ERROR,
11464bc52338SCy Schubert "const_time_select_int(0x%x,%d,%d) test failed",
11474bc52338SCy Schubert test->mask, test->true_val, test->false_val);
11484bc52338SCy Schubert ret = -1;
11494bc52338SCy Schubert }
11504bc52338SCy Schubert }
11514bc52338SCy Schubert
11524bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_select_u8_tests); i++) {
11534bc52338SCy Schubert struct const_time_select_u8_test *test;
11544bc52338SCy Schubert
11554bc52338SCy Schubert test = &const_time_select_u8_tests[i];
11564bc52338SCy Schubert if (const_time_select_u8(test->mask, test->true_val,
11574bc52338SCy Schubert test->false_val) != test->expected) {
11584bc52338SCy Schubert wpa_printf(MSG_ERROR,
11594bc52338SCy Schubert "const_time_select_u8(0x%x,0x%x,0x%x) test failed",
11604bc52338SCy Schubert test->mask, test->true_val, test->false_val);
11614bc52338SCy Schubert ret = -1;
11624bc52338SCy Schubert }
11634bc52338SCy Schubert }
11644bc52338SCy Schubert
11654bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_select_s8_tests); i++) {
11664bc52338SCy Schubert struct const_time_select_s8_test *test;
11674bc52338SCy Schubert
11684bc52338SCy Schubert test = &const_time_select_s8_tests[i];
11694bc52338SCy Schubert if (const_time_select_s8(test->mask, test->true_val,
11704bc52338SCy Schubert test->false_val) != test->expected) {
11714bc52338SCy Schubert wpa_printf(MSG_ERROR,
11724bc52338SCy Schubert "const_time_select_s8(0x%x,0x%x,0x%x) test failed",
11734bc52338SCy Schubert test->mask, test->true_val, test->false_val);
11744bc52338SCy Schubert ret = -1;
11754bc52338SCy Schubert }
11764bc52338SCy Schubert }
11774bc52338SCy Schubert
11784bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_select_bin_tests); i++) {
11794bc52338SCy Schubert struct const_time_select_bin_test *test;
11804bc52338SCy Schubert u8 dst[100];
11814bc52338SCy Schubert
11824bc52338SCy Schubert test = &const_time_select_bin_tests[i];
11834bc52338SCy Schubert const_time_select_bin(test->mask, test->true_val,
11844bc52338SCy Schubert test->false_val, test->len, dst);
11854bc52338SCy Schubert if (os_memcmp(dst, test->expected, test->len) != 0) {
11864bc52338SCy Schubert wpa_printf(MSG_ERROR,
11874bc52338SCy Schubert "const_time_select_bin(0x%x,%u) test failed",
11884bc52338SCy Schubert test->mask, (unsigned int) test->len);
11894bc52338SCy Schubert ret = -1;
11904bc52338SCy Schubert }
11914bc52338SCy Schubert }
11924bc52338SCy Schubert
11934bc52338SCy Schubert for (i = 0; i < ARRAY_SIZE(const_time_memcmp_tests); i++) {
11944bc52338SCy Schubert struct const_time_memcmp_test *test;
11954bc52338SCy Schubert int res;
11964bc52338SCy Schubert
11974bc52338SCy Schubert test = &const_time_memcmp_tests[i];
11984bc52338SCy Schubert res = const_time_memcmp(test->a, test->b, test->len);
11994bc52338SCy Schubert if (res != test->expected) {
12004bc52338SCy Schubert wpa_printf(MSG_ERROR,
12014bc52338SCy Schubert "const_time_memcmp(%s,%s,%d) test failed (%d != %d)",
12024bc52338SCy Schubert test->a, test->b, (int) test->len,
12034bc52338SCy Schubert res, test->expected);
12044bc52338SCy Schubert ret = -1;
12054bc52338SCy Schubert }
12064bc52338SCy Schubert }
12074bc52338SCy Schubert
12084bc52338SCy Schubert return ret;
12094bc52338SCy Schubert }
12104bc52338SCy Schubert
12114bc52338SCy Schubert
utils_module_tests(void)12125b9c547cSRui Paulo int utils_module_tests(void)
12135b9c547cSRui Paulo {
12145b9c547cSRui Paulo int ret = 0;
12155b9c547cSRui Paulo
12165b9c547cSRui Paulo wpa_printf(MSG_INFO, "utils module tests");
12175b9c547cSRui Paulo
12185b9c547cSRui Paulo if (printf_encode_decode_tests() < 0 ||
12195b9c547cSRui Paulo ext_password_tests() < 0 ||
12205b9c547cSRui Paulo trace_tests() < 0 ||
12215b9c547cSRui Paulo bitfield_tests() < 0 ||
12225b9c547cSRui Paulo base64_tests() < 0 ||
12235b9c547cSRui Paulo common_tests() < 0 ||
1224325151a3SRui Paulo os_tests() < 0 ||
1225325151a3SRui Paulo wpabuf_tests() < 0 ||
1226325151a3SRui Paulo ip_addr_tests() < 0 ||
1227325151a3SRui Paulo eloop_tests() < 0 ||
122885732ac8SCy Schubert json_tests() < 0 ||
12294bc52338SCy Schubert const_time_tests() < 0 ||
12305b9c547cSRui Paulo int_array_tests() < 0)
12315b9c547cSRui Paulo ret = -1;
12325b9c547cSRui Paulo
12335b9c547cSRui Paulo return ret;
12345b9c547cSRui Paulo }
1235