3 * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "common/ieee802_11_defs.h"
13 #include "utils/bitfield.h"
14 #include "utils/ext_password.h"
15 #include "utils/trace.h"
16 #include "utils/base64.h"
17 #include "utils/ip_addr.h"
18 #include "utils/eloop.h"
19 #include "utils/json.h"
20 #include "utils/module_tests.h"
23 struct printf_test_data {
29 static const struct printf_test_data printf_tests[] = {
30 { (u8 *) "abcde", 5, "abcde" },
31 { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
32 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
33 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
34 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
35 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
36 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
37 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
38 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
39 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
44 static int printf_encode_decode_tests(void)
53 wpa_printf(MSG_INFO, "printf encode/decode tests");
55 for (i = 0; printf_tests[i].data; i++) {
56 const struct printf_test_data *test = &printf_tests[i];
57 printf_encode(buf, sizeof(buf), test->data, test->len);
58 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
60 binlen = printf_decode(bin, sizeof(bin), buf);
61 if (binlen != test->len ||
62 os_memcmp(bin, test->data, binlen) != 0) {
63 wpa_hexdump(MSG_ERROR, "Error in decoding#1",
68 binlen = printf_decode(bin, sizeof(bin), test->encoded);
69 if (binlen != test->len ||
70 os_memcmp(bin, test->data, binlen) != 0) {
71 wpa_hexdump(MSG_ERROR, "Error in decoding#2",
78 printf_encode(buf, 5, (const u8 *) "abcde", 5);
80 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
84 for (i = 5; i < 10; i++) {
86 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
88 wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
94 if (printf_decode(bin, 3, "abcde") != 2)
97 if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
100 if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
103 if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
112 if (int_array_len(array) != 5)
114 int_array_sort_unique(array);
115 if (int_array_len(array) != 2)
119 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
127 static int bitfield_tests(void)
133 wpa_printf(MSG_INFO, "bitfield tests");
135 bf = bitfield_alloc(123);
139 for (i = 0; i < 123; i++) {
140 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
142 if (i > 0 && bitfield_is_set(bf, i - 1))
145 if (!bitfield_is_set(bf, i))
147 bitfield_clear(bf, i);
148 if (bitfield_is_set(bf, i))
152 for (i = 123; i < 200; i++) {
153 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
155 if (i > 0 && bitfield_is_set(bf, i - 1))
158 if (bitfield_is_set(bf, i))
160 bitfield_clear(bf, i);
161 if (bitfield_is_set(bf, i))
165 for (i = 0; i < 123; i++) {
166 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
169 if (!bitfield_is_set(bf, i))
173 for (i = 0; i < 123; i++) {
174 if (!bitfield_is_set(bf, i))
176 bitfield_clear(bf, i);
177 if (bitfield_is_set(bf, i))
181 for (i = 0; i < 123; i++) {
182 if (bitfield_get_first_zero(bf) != i)
186 if (bitfield_get_first_zero(bf) != -1)
188 for (i = 0; i < 123; i++) {
189 if (!bitfield_is_set(bf, i))
191 bitfield_clear(bf, i);
192 if (bitfield_get_first_zero(bf) != i)
196 if (bitfield_get_first_zero(bf) != -1)
201 bf = bitfield_alloc(8);
204 if (bitfield_get_first_zero(bf) != 0)
206 for (i = 0; i < 8; i++)
208 if (bitfield_get_first_zero(bf) != -1)
213 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
221 static int int_array_tests(void)
223 int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
224 int test2[] = { 1, -1, 0 };
225 int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
226 int test3_res[] = { -1, 1, 2, 3, 4, 0 };
230 wpa_printf(MSG_INFO, "int_array tests");
232 if (int_array_len(test1) != 6 ||
233 int_array_len(test2) != 2)
236 int_array_sort_unique(test3);
237 len = int_array_len(test3_res);
238 if (int_array_len(test3) != len)
240 else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
244 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
252 static int ext_password_tests(void)
254 struct ext_password_data *data;
258 wpa_printf(MSG_INFO, "ext_password tests");
260 data = ext_password_init("unknown", "foo");
264 data = ext_password_init("test", NULL);
267 pw = ext_password_get(data, "foo");
270 ext_password_free(pw);
272 ext_password_deinit(data);
274 pw = ext_password_get(NULL, "foo");
277 ext_password_free(pw);
283 static int trace_tests(void)
285 wpa_printf(MSG_INFO, "trace tests");
287 wpa_trace_show("test backtrace");
288 wpa_trace_dump_funcname("test funcname", trace_tests);
294 static int base64_tests(void)
300 wpa_printf(MSG_INFO, "base64 tests");
302 res = base64_encode((const unsigned char *) "", ~0, &res_len);
308 res = base64_encode((const unsigned char *) "=", 1, &res_len);
309 if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
310 res[2] != '=' || res[3] != '=' || res[4] != '\n')
314 res = base64_encode((const unsigned char *) "=", 1, NULL);
315 if (!res || res[0] != 'P' || res[1] != 'Q' ||
316 res[2] != '=' || res[3] != '=' || res[4] != '\n')
320 res = base64_decode((const unsigned char *) "", 0, &res_len);
326 res = base64_decode((const unsigned char *) "a", 1, &res_len);
332 res = base64_decode((const unsigned char *) "====", 4, &res_len);
338 res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
339 if (!res || res_len != 1 || res[0] != '=')
343 res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
344 if (!res || res_len != 1 || res[0] != '=')
349 wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
357 static int common_tests(void)
359 char buf[3], longbuf[100];
360 u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
363 struct wpa_freq_range_list ranges;
368 wpa_printf(MSG_INFO, "common tests");
370 if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
373 if (wpa_scnprintf(buf, 0, "hello") != 0 ||
374 wpa_scnprintf(buf, 3, "hello") != 2)
377 if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
378 wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
381 if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
382 merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
385 if (dup_binstr(NULL, 0) != NULL)
388 if (freq_range_list_includes(NULL, 0) != 0)
391 os_memset(&ranges, 0, sizeof(ranges));
392 if (freq_range_list_parse(&ranges, "") != 0 ||
393 freq_range_list_includes(&ranges, 0) != 0 ||
394 freq_range_list_str(&ranges) != NULL)
397 if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
398 utf8_unescape("a", 1, NULL, 0) != 0 ||
399 utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
400 utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
401 utf8_unescape("abc", 3, buf, 3) != 3)
404 if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
407 if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
410 if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
411 utf8_escape("a", 1, NULL, 0) != 0 ||
412 utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
413 utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
414 utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
415 utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
416 utf8_escape("abc", 3, buf, 3) != 3)
419 if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
422 os_memset(ssid, 0, sizeof(ssid));
423 txt = wpa_ssid_txt(ssid, sizeof(ssid));
424 len = os_strlen(txt);
425 /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
426 if (len != SSID_MAX_LEN * 4) {
427 wpa_printf(MSG_ERROR,
428 "Unexpected wpa_ssid_txt() result with too long SSID");
432 if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
433 wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
434 os_strcmp(longbuf, "01-0") != 0)
438 wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
446 static int os_tests(void)
452 wpa_printf(MSG_INFO, "os tests");
454 ptr = os_calloc((size_t) -1, (size_t) -1);
459 ptr = os_calloc((size_t) 2, (size_t) -1);
464 ptr = os_calloc((size_t) -1, (size_t) 2);
470 ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
478 if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
479 os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
480 os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
481 os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
482 os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
483 os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
484 os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
485 os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
486 os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
487 os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
488 os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
489 os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
490 os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
491 os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
494 if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
495 os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
496 os_unsetenv("hwsim_test_env") != 0)
499 if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
503 wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
511 static int wpabuf_tests(void)
517 wpa_printf(MSG_INFO, "wpabuf tests");
519 ptr = os_malloc(100);
521 buf = wpabuf_alloc_ext_data(ptr, 100);
523 if (wpabuf_resize(&buf, 100) < 0)
526 wpabuf_put(buf, 100);
536 buf = wpabuf_alloc(100);
540 wpabuf_put(buf, 100);
541 if (wpabuf_resize(&buf, 100) < 0)
544 wpabuf_put(buf, 100);
545 buf2 = wpabuf_concat(buf, NULL);
554 buf = wpabuf_zeropad(buf, 10);
559 wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
567 static int ip_addr_tests(void)
570 struct hostapd_ip_addr addr;
573 wpa_printf(MSG_INFO, "ip_addr tests");
575 if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
576 addr.af != AF_INET ||
577 hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
578 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
579 hostapd_ip_txt(&addr, buf, 0) != NULL ||
580 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
583 if (hostapd_parse_ip_addr("::", &addr) != 0 ||
584 addr.af != AF_INET6 ||
585 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
586 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
590 wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
600 int close_in_timeout;
606 static void eloop_tests_start(int close_in_timeout);
609 static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
611 struct test_eloop *t = eloop_ctx;
615 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
617 if (t->magic != 0x12345678) {
618 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
622 if (t->pipefd2[0] != sock) {
623 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
624 __func__, sock, t->pipefd2[0]);
627 res = read(sock, buf, sizeof(buf));
628 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
629 __func__, sock, (int) res);
633 static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
635 struct test_eloop *t = eloop_ctx;
637 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
639 if (t->magic != 0x12345678) {
640 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
644 if (t->pipefd2[0] != sock) {
645 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
646 __func__, sock, t->pipefd2[0]);
650 * This is expected to block due to the original socket with data having
651 * been closed and no new data having been written to the new socket
652 * with the same fd. To avoid blocking the process during test, skip the
655 wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
660 static void reopen_pipefd2(struct test_eloop *t)
662 if (t->pipefd2[0] < 0) {
663 wpa_printf(MSG_INFO, "pipefd2 had been closed");
667 wpa_printf(MSG_INFO, "close pipefd2");
668 eloop_unregister_read_sock(t->pipefd2[0]);
669 close(t->pipefd2[0]);
671 close(t->pipefd2[1]);
674 res = pipe(t->pipefd2);
676 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
683 "re-register pipefd2 with new sockets %d,%d",
684 t->pipefd2[0], t->pipefd2[1]);
685 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
691 static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
693 struct test_eloop *t = eloop_ctx;
697 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
699 if (t->magic != 0x12345678) {
700 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
704 if (t->pipefd1[0] != sock) {
705 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
706 __func__, sock, t->pipefd1[0]);
709 res = read(sock, buf, sizeof(buf));
710 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
711 __func__, sock, (int) res);
713 if (!t->close_in_timeout)
718 static void eloop_test_cb(void *eloop_data, void *user_ctx)
720 struct test_eloop *t = eloop_data;
722 wpa_printf(MSG_INFO, "%s", __func__);
724 if (t->magic != 0x12345678) {
725 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
729 if (t->close_in_timeout)
734 static void eloop_test_timeout(void *eloop_data, void *user_ctx)
736 struct test_eloop *t = eloop_data;
739 wpa_printf(MSG_INFO, "%s", __func__);
741 if (t->magic != 0x12345678) {
742 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
746 if (t->pipefd1[0] >= 0) {
747 wpa_printf(MSG_INFO, "pipefd1 had not been closed");
748 eloop_unregister_read_sock(t->pipefd1[0]);
749 close(t->pipefd1[0]);
751 close(t->pipefd1[1]);
755 if (t->pipefd2[0] >= 0) {
756 wpa_printf(MSG_INFO, "pipefd2 had not been closed");
757 eloop_unregister_read_sock(t->pipefd2[0]);
758 close(t->pipefd2[0]);
760 close(t->pipefd2[1]);
764 next_run = t->close_in_timeout;
766 wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
770 eloop_tests_start(0);
774 static void eloop_tests_start(int close_in_timeout)
776 struct test_eloop *t;
779 t = os_zalloc(sizeof(*t));
782 t->magic = 0x12345678;
783 t->close_in_timeout = close_in_timeout;
785 wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
786 t, close_in_timeout);
788 res = pipe(t->pipefd1);
790 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
795 res = pipe(t->pipefd2);
797 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
798 close(t->pipefd1[0]);
799 close(t->pipefd1[1]);
804 wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
805 t->pipefd1[0], t->pipefd1[1],
806 t->pipefd2[0], t->pipefd2[1]);
808 eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
809 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
810 eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
811 eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
813 if (write(t->pipefd1[1], "HELLO", 5) < 0)
814 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
815 if (write(t->pipefd2[1], "TEST", 4) < 0)
816 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
818 wpa_printf(MSG_INFO, "waiting for eloop callbacks");
822 static void eloop_tests_run(void *eloop_data, void *user_ctx)
824 eloop_tests_start(1);
828 static int eloop_tests(void)
830 wpa_printf(MSG_INFO, "schedule eloop tests to be run");
833 * Cannot return error from these without a significant design change,
834 * so for now, run the tests from a scheduled timeout and require
835 * separate verification of the results from the debug log.
837 eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
844 struct json_test_data {
849 static const struct json_test_data json_test_cases[] = {
850 { "{}", "[1:OBJECT:]" },
851 { "[]", "[1:ARRAY:]" },
856 { "[[]]", "[1:ARRAY:][2:ARRAY:]" },
857 { "{\"t\":\"test\"}", "[1:OBJECT:][2:STRING:t]" },
858 { "{\"t\":123}", "[1:OBJECT:][2:NUMBER:t]" },
859 { "{\"t\":true}", "[1:OBJECT:][2:BOOLEAN:t]" },
860 { "{\"t\":false}", "[1:OBJECT:][2:BOOLEAN:t]" },
861 { "{\"t\":null}", "[1:OBJECT:][2:NULL:t]" },
862 { "{\"t\":truetrue}", NULL },
863 { "\"test\"", "[1:STRING:]" },
864 { "123", "[1:NUMBER:]" },
865 { "true", "[1:BOOLEAN:]" },
866 { "false", "[1:BOOLEAN:]" },
867 { "null", "[1:NULL:]" },
868 { "truetrue", NULL },
869 { " {\t\n\r\"a\"\n:\r1\n,\n\"b\":3\n}\n",
870 "[1:OBJECT:][2:NUMBER:a][2:NUMBER:b]" },
877 { "{ \"\\u005c\" : \"\\u005c\" }", "[1:OBJECT:][2:STRING:\\]" },
878 { "[{},{}]", "[1:ARRAY:][2:OBJECT:][2:OBJECT:]" },
879 { "[1,2]", "[1:ARRAY:][2:NUMBER:][2:NUMBER:]" },
880 { "[\"1\",\"2\"]", "[1:ARRAY:][2:STRING:][2:STRING:]" },
881 { "[true,false]", "[1:ARRAY:][2:BOOLEAN:][2:BOOLEAN:]" },
883 #endif /* CONFIG_JSON */
886 static int json_tests(void)
890 struct json_token *root;
893 wpa_printf(MSG_INFO, "JSON tests");
895 for (i = 0; i < ARRAY_SIZE(json_test_cases); i++) {
896 const struct json_test_data *test = &json_test_cases[i];
899 root = json_parse(test->json, os_strlen(test->json));
900 if ((root && !test->tree) || (!root && test->tree)) {
901 wpa_printf(MSG_INFO, "JSON test %u failed", i);
904 json_print_tree(root, buf, sizeof(buf));
905 if (os_strcmp(buf, test->tree) != 0) {
907 "JSON test %u tree mismatch: %s %s",
917 #endif /* CONFIG_JSON */
922 int utils_module_tests(void)
926 wpa_printf(MSG_INFO, "utils module tests");
928 if (printf_encode_decode_tests() < 0 ||
929 ext_password_tests() < 0 ||
931 bitfield_tests() < 0 ||
932 base64_tests() < 0 ||
933 common_tests() < 0 ||
935 wpabuf_tests() < 0 ||
936 ip_addr_tests() < 0 ||
939 int_array_tests() < 0)