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"
21 struct printf_test_data {
27 static const struct printf_test_data printf_tests[] = {
28 { (u8 *) "abcde", 5, "abcde" },
29 { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
30 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
31 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
32 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
33 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
34 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
35 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
36 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
37 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
42 static int printf_encode_decode_tests(void)
51 wpa_printf(MSG_INFO, "printf encode/decode tests");
53 for (i = 0; printf_tests[i].data; i++) {
54 const struct printf_test_data *test = &printf_tests[i];
55 printf_encode(buf, sizeof(buf), test->data, test->len);
56 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
58 binlen = printf_decode(bin, sizeof(bin), buf);
59 if (binlen != test->len ||
60 os_memcmp(bin, test->data, binlen) != 0) {
61 wpa_hexdump(MSG_ERROR, "Error in decoding#1",
66 binlen = printf_decode(bin, sizeof(bin), test->encoded);
67 if (binlen != test->len ||
68 os_memcmp(bin, test->data, binlen) != 0) {
69 wpa_hexdump(MSG_ERROR, "Error in decoding#2",
76 printf_encode(buf, 5, (const u8 *) "abcde", 5);
78 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
82 for (i = 5; i < 10; i++) {
84 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
86 wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
92 if (printf_decode(bin, 3, "abcde") != 2)
95 if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
98 if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
101 if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
110 if (int_array_len(array) != 5)
112 int_array_sort_unique(array);
113 if (int_array_len(array) != 2)
117 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
125 static int bitfield_tests(void)
131 wpa_printf(MSG_INFO, "bitfield tests");
133 bf = bitfield_alloc(123);
137 for (i = 0; i < 123; i++) {
138 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
140 if (i > 0 && bitfield_is_set(bf, i - 1))
143 if (!bitfield_is_set(bf, i))
145 bitfield_clear(bf, i);
146 if (bitfield_is_set(bf, i))
150 for (i = 123; i < 200; i++) {
151 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
153 if (i > 0 && bitfield_is_set(bf, i - 1))
156 if (bitfield_is_set(bf, i))
158 bitfield_clear(bf, i);
159 if (bitfield_is_set(bf, i))
163 for (i = 0; i < 123; i++) {
164 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
167 if (!bitfield_is_set(bf, i))
171 for (i = 0; i < 123; i++) {
172 if (!bitfield_is_set(bf, i))
174 bitfield_clear(bf, i);
175 if (bitfield_is_set(bf, i))
179 for (i = 0; i < 123; i++) {
180 if (bitfield_get_first_zero(bf) != i)
184 if (bitfield_get_first_zero(bf) != -1)
186 for (i = 0; i < 123; i++) {
187 if (!bitfield_is_set(bf, i))
189 bitfield_clear(bf, i);
190 if (bitfield_get_first_zero(bf) != i)
194 if (bitfield_get_first_zero(bf) != -1)
199 bf = bitfield_alloc(8);
202 if (bitfield_get_first_zero(bf) != 0)
204 for (i = 0; i < 8; i++)
206 if (bitfield_get_first_zero(bf) != -1)
211 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
219 static int int_array_tests(void)
221 int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
222 int test2[] = { 1, -1, 0 };
223 int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
224 int test3_res[] = { -1, 1, 2, 3, 4, 0 };
228 wpa_printf(MSG_INFO, "int_array tests");
230 if (int_array_len(test1) != 6 ||
231 int_array_len(test2) != 2)
234 int_array_sort_unique(test3);
235 len = int_array_len(test3_res);
236 if (int_array_len(test3) != len)
238 else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
242 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
250 static int ext_password_tests(void)
252 struct ext_password_data *data;
256 wpa_printf(MSG_INFO, "ext_password tests");
258 data = ext_password_init("unknown", "foo");
262 data = ext_password_init("test", NULL);
265 pw = ext_password_get(data, "foo");
268 ext_password_free(pw);
270 ext_password_deinit(data);
272 pw = ext_password_get(NULL, "foo");
275 ext_password_free(pw);
281 static int trace_tests(void)
283 wpa_printf(MSG_INFO, "trace tests");
285 wpa_trace_show("test backtrace");
286 wpa_trace_dump_funcname("test funcname", trace_tests);
292 static int base64_tests(void)
298 wpa_printf(MSG_INFO, "base64 tests");
300 res = base64_encode((const unsigned char *) "", ~0, &res_len);
306 res = base64_encode((const unsigned char *) "=", 1, &res_len);
307 if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
308 res[2] != '=' || res[3] != '=' || res[4] != '\n')
312 res = base64_encode((const unsigned char *) "=", 1, NULL);
313 if (!res || res[0] != 'P' || res[1] != 'Q' ||
314 res[2] != '=' || res[3] != '=' || res[4] != '\n')
318 res = base64_decode((const unsigned char *) "", 0, &res_len);
324 res = base64_decode((const unsigned char *) "a", 1, &res_len);
330 res = base64_decode((const unsigned char *) "====", 4, &res_len);
336 res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
337 if (!res || res_len != 1 || res[0] != '=')
341 res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
342 if (!res || res_len != 1 || res[0] != '=')
347 wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
355 static int common_tests(void)
357 char buf[3], longbuf[100];
358 u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
361 struct wpa_freq_range_list ranges;
366 wpa_printf(MSG_INFO, "common tests");
368 if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
371 if (wpa_scnprintf(buf, 0, "hello") != 0 ||
372 wpa_scnprintf(buf, 3, "hello") != 2)
375 if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
376 wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
379 if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
380 merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
383 if (dup_binstr(NULL, 0) != NULL)
386 if (freq_range_list_includes(NULL, 0) != 0)
389 os_memset(&ranges, 0, sizeof(ranges));
390 if (freq_range_list_parse(&ranges, "") != 0 ||
391 freq_range_list_includes(&ranges, 0) != 0 ||
392 freq_range_list_str(&ranges) != NULL)
395 if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
396 utf8_unescape("a", 1, NULL, 0) != 0 ||
397 utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
398 utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
399 utf8_unescape("abc", 3, buf, 3) != 3)
402 if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
405 if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
408 if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
409 utf8_escape("a", 1, NULL, 0) != 0 ||
410 utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
411 utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
412 utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
413 utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
414 utf8_escape("abc", 3, buf, 3) != 3)
417 if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
420 os_memset(ssid, 0, sizeof(ssid));
421 txt = wpa_ssid_txt(ssid, sizeof(ssid));
422 len = os_strlen(txt);
423 /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
424 if (len != SSID_MAX_LEN * 4) {
425 wpa_printf(MSG_ERROR,
426 "Unexpected wpa_ssid_txt() result with too long SSID");
430 if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
431 wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
432 os_strcmp(longbuf, "01-0") != 0)
436 wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
444 static int os_tests(void)
450 wpa_printf(MSG_INFO, "os tests");
452 ptr = os_calloc((size_t) -1, (size_t) -1);
457 ptr = os_calloc((size_t) 2, (size_t) -1);
462 ptr = os_calloc((size_t) -1, (size_t) 2);
468 ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
476 if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
477 os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
478 os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
479 os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
480 os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
481 os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
482 os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
483 os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
484 os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
485 os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
486 os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
487 os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
488 os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
489 os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
492 if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
493 os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
494 os_unsetenv("hwsim_test_env") != 0)
497 if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
501 wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
509 static int wpabuf_tests(void)
515 wpa_printf(MSG_INFO, "wpabuf tests");
517 ptr = os_malloc(100);
519 buf = wpabuf_alloc_ext_data(ptr, 100);
521 if (wpabuf_resize(&buf, 100) < 0)
524 wpabuf_put(buf, 100);
534 buf = wpabuf_alloc(100);
538 wpabuf_put(buf, 100);
539 if (wpabuf_resize(&buf, 100) < 0)
542 wpabuf_put(buf, 100);
543 buf2 = wpabuf_concat(buf, NULL);
552 buf = wpabuf_zeropad(buf, 10);
557 wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
565 static int ip_addr_tests(void)
568 struct hostapd_ip_addr addr;
571 wpa_printf(MSG_INFO, "ip_addr tests");
573 if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
574 addr.af != AF_INET ||
575 hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
576 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
577 hostapd_ip_txt(&addr, buf, 0) != NULL ||
578 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
581 if (hostapd_parse_ip_addr("::", &addr) != 0 ||
582 addr.af != AF_INET6 ||
583 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
584 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
588 wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
598 int close_in_timeout;
604 static void eloop_tests_start(int close_in_timeout);
607 static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
609 struct test_eloop *t = eloop_ctx;
613 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
615 if (t->magic != 0x12345678) {
616 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
620 if (t->pipefd2[0] != sock) {
621 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
622 __func__, sock, t->pipefd2[0]);
625 res = read(sock, buf, sizeof(buf));
626 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
627 __func__, sock, (int) res);
631 static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
633 struct test_eloop *t = eloop_ctx;
635 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
637 if (t->magic != 0x12345678) {
638 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
642 if (t->pipefd2[0] != sock) {
643 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
644 __func__, sock, t->pipefd2[0]);
648 * This is expected to block due to the original socket with data having
649 * been closed and no new data having been written to the new socket
650 * with the same fd. To avoid blocking the process during test, skip the
653 wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
658 static void reopen_pipefd2(struct test_eloop *t)
660 if (t->pipefd2[0] < 0) {
661 wpa_printf(MSG_INFO, "pipefd2 had been closed");
665 wpa_printf(MSG_INFO, "close pipefd2");
666 eloop_unregister_read_sock(t->pipefd2[0]);
667 close(t->pipefd2[0]);
669 close(t->pipefd2[1]);
672 res = pipe(t->pipefd2);
674 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
681 "re-register pipefd2 with new sockets %d,%d",
682 t->pipefd2[0], t->pipefd2[1]);
683 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
689 static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
691 struct test_eloop *t = eloop_ctx;
695 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
697 if (t->magic != 0x12345678) {
698 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
702 if (t->pipefd1[0] != sock) {
703 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
704 __func__, sock, t->pipefd1[0]);
707 res = read(sock, buf, sizeof(buf));
708 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
709 __func__, sock, (int) res);
711 if (!t->close_in_timeout)
716 static void eloop_test_cb(void *eloop_data, void *user_ctx)
718 struct test_eloop *t = eloop_data;
720 wpa_printf(MSG_INFO, "%s", __func__);
722 if (t->magic != 0x12345678) {
723 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
727 if (t->close_in_timeout)
732 static void eloop_test_timeout(void *eloop_data, void *user_ctx)
734 struct test_eloop *t = eloop_data;
737 wpa_printf(MSG_INFO, "%s", __func__);
739 if (t->magic != 0x12345678) {
740 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
744 if (t->pipefd1[0] >= 0) {
745 wpa_printf(MSG_INFO, "pipefd1 had not been closed");
746 eloop_unregister_read_sock(t->pipefd1[0]);
747 close(t->pipefd1[0]);
749 close(t->pipefd1[1]);
753 if (t->pipefd2[0] >= 0) {
754 wpa_printf(MSG_INFO, "pipefd2 had not been closed");
755 eloop_unregister_read_sock(t->pipefd2[0]);
756 close(t->pipefd2[0]);
758 close(t->pipefd2[1]);
762 next_run = t->close_in_timeout;
764 wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
768 eloop_tests_start(0);
772 static void eloop_tests_start(int close_in_timeout)
774 struct test_eloop *t;
777 t = os_zalloc(sizeof(*t));
780 t->magic = 0x12345678;
781 t->close_in_timeout = close_in_timeout;
783 wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
784 t, close_in_timeout);
786 res = pipe(t->pipefd1);
788 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
793 res = pipe(t->pipefd2);
795 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
796 close(t->pipefd1[0]);
797 close(t->pipefd1[1]);
802 wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
803 t->pipefd1[0], t->pipefd1[1],
804 t->pipefd2[0], t->pipefd2[1]);
806 eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
807 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
808 eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
809 eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
811 if (write(t->pipefd1[1], "HELLO", 5) < 0)
812 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
813 if (write(t->pipefd2[1], "TEST", 4) < 0)
814 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
816 wpa_printf(MSG_INFO, "waiting for eloop callbacks");
820 static void eloop_tests_run(void *eloop_data, void *user_ctx)
822 eloop_tests_start(1);
826 static int eloop_tests(void)
828 wpa_printf(MSG_INFO, "schedule eloop tests to be run");
831 * Cannot return error from these without a significant design change,
832 * so for now, run the tests from a scheduled timeout and require
833 * separate verification of the results from the debug log.
835 eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
841 int utils_module_tests(void)
845 wpa_printf(MSG_INFO, "utils module tests");
847 if (printf_encode_decode_tests() < 0 ||
848 ext_password_tests() < 0 ||
850 bitfield_tests() < 0 ||
851 base64_tests() < 0 ||
852 common_tests() < 0 ||
854 wpabuf_tests() < 0 ||
855 ip_addr_tests() < 0 ||
857 int_array_tests() < 0)