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/module_tests.h"
22 struct printf_test_data {
28 static const struct printf_test_data printf_tests[] = {
29 { (u8 *) "abcde", 5, "abcde" },
30 { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
31 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
32 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
33 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
34 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
35 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
36 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
37 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
38 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
43 static int printf_encode_decode_tests(void)
52 wpa_printf(MSG_INFO, "printf encode/decode tests");
54 for (i = 0; printf_tests[i].data; i++) {
55 const struct printf_test_data *test = &printf_tests[i];
56 printf_encode(buf, sizeof(buf), test->data, test->len);
57 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
59 binlen = printf_decode(bin, sizeof(bin), buf);
60 if (binlen != test->len ||
61 os_memcmp(bin, test->data, binlen) != 0) {
62 wpa_hexdump(MSG_ERROR, "Error in decoding#1",
67 binlen = printf_decode(bin, sizeof(bin), test->encoded);
68 if (binlen != test->len ||
69 os_memcmp(bin, test->data, binlen) != 0) {
70 wpa_hexdump(MSG_ERROR, "Error in decoding#2",
77 printf_encode(buf, 5, (const u8 *) "abcde", 5);
79 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
83 for (i = 5; i < 10; i++) {
85 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
87 wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
93 if (printf_decode(bin, 3, "abcde") != 2)
96 if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
99 if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
102 if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
111 if (int_array_len(array) != 5)
113 int_array_sort_unique(array);
114 if (int_array_len(array) != 2)
118 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
126 static int bitfield_tests(void)
132 wpa_printf(MSG_INFO, "bitfield tests");
134 bf = bitfield_alloc(123);
138 for (i = 0; i < 123; i++) {
139 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
141 if (i > 0 && bitfield_is_set(bf, i - 1))
144 if (!bitfield_is_set(bf, i))
146 bitfield_clear(bf, i);
147 if (bitfield_is_set(bf, i))
151 for (i = 123; i < 200; i++) {
152 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
154 if (i > 0 && bitfield_is_set(bf, i - 1))
157 if (bitfield_is_set(bf, i))
159 bitfield_clear(bf, i);
160 if (bitfield_is_set(bf, i))
164 for (i = 0; i < 123; i++) {
165 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
168 if (!bitfield_is_set(bf, i))
172 for (i = 0; i < 123; i++) {
173 if (!bitfield_is_set(bf, i))
175 bitfield_clear(bf, i);
176 if (bitfield_is_set(bf, i))
180 for (i = 0; i < 123; i++) {
181 if (bitfield_get_first_zero(bf) != i)
185 if (bitfield_get_first_zero(bf) != -1)
187 for (i = 0; i < 123; i++) {
188 if (!bitfield_is_set(bf, i))
190 bitfield_clear(bf, i);
191 if (bitfield_get_first_zero(bf) != i)
195 if (bitfield_get_first_zero(bf) != -1)
200 bf = bitfield_alloc(8);
203 if (bitfield_get_first_zero(bf) != 0)
205 for (i = 0; i < 8; i++)
207 if (bitfield_get_first_zero(bf) != -1)
212 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
220 static int int_array_tests(void)
222 int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
223 int test2[] = { 1, -1, 0 };
224 int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
225 int test3_res[] = { -1, 1, 2, 3, 4, 0 };
229 wpa_printf(MSG_INFO, "int_array tests");
231 if (int_array_len(test1) != 6 ||
232 int_array_len(test2) != 2)
235 int_array_sort_unique(test3);
236 len = int_array_len(test3_res);
237 if (int_array_len(test3) != len)
239 else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
243 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
251 static int ext_password_tests(void)
253 struct ext_password_data *data;
257 wpa_printf(MSG_INFO, "ext_password tests");
259 data = ext_password_init("unknown", "foo");
263 data = ext_password_init("test", NULL);
266 pw = ext_password_get(data, "foo");
269 ext_password_free(pw);
271 ext_password_deinit(data);
273 pw = ext_password_get(NULL, "foo");
276 ext_password_free(pw);
282 static int trace_tests(void)
284 wpa_printf(MSG_INFO, "trace tests");
286 wpa_trace_show("test backtrace");
287 wpa_trace_dump_funcname("test funcname", trace_tests);
293 static int base64_tests(void)
299 wpa_printf(MSG_INFO, "base64 tests");
301 res = base64_encode((const unsigned char *) "", ~0, &res_len);
307 res = base64_encode((const unsigned char *) "=", 1, &res_len);
308 if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
309 res[2] != '=' || res[3] != '=' || res[4] != '\n')
313 res = base64_encode((const unsigned char *) "=", 1, NULL);
314 if (!res || res[0] != 'P' || res[1] != 'Q' ||
315 res[2] != '=' || res[3] != '=' || res[4] != '\n')
319 res = base64_decode((const unsigned char *) "", 0, &res_len);
325 res = base64_decode((const unsigned char *) "a", 1, &res_len);
331 res = base64_decode((const unsigned char *) "====", 4, &res_len);
337 res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
338 if (!res || res_len != 1 || res[0] != '=')
342 res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
343 if (!res || res_len != 1 || res[0] != '=')
348 wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
356 static int common_tests(void)
358 char buf[3], longbuf[100];
359 u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
362 struct wpa_freq_range_list ranges;
367 wpa_printf(MSG_INFO, "common tests");
369 if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
372 if (wpa_scnprintf(buf, 0, "hello") != 0 ||
373 wpa_scnprintf(buf, 3, "hello") != 2)
376 if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
377 wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
380 if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
381 merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
384 if (dup_binstr(NULL, 0) != NULL)
387 if (freq_range_list_includes(NULL, 0) != 0)
390 os_memset(&ranges, 0, sizeof(ranges));
391 if (freq_range_list_parse(&ranges, "") != 0 ||
392 freq_range_list_includes(&ranges, 0) != 0 ||
393 freq_range_list_str(&ranges) != NULL)
396 if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
397 utf8_unescape("a", 1, NULL, 0) != 0 ||
398 utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
399 utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
400 utf8_unescape("abc", 3, buf, 3) != 3)
403 if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
406 if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
409 if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
410 utf8_escape("a", 1, NULL, 0) != 0 ||
411 utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
412 utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
413 utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
414 utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
415 utf8_escape("abc", 3, buf, 3) != 3)
418 if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
421 os_memset(ssid, 0, sizeof(ssid));
422 txt = wpa_ssid_txt(ssid, sizeof(ssid));
423 len = os_strlen(txt);
424 /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
425 if (len != SSID_MAX_LEN * 4) {
426 wpa_printf(MSG_ERROR,
427 "Unexpected wpa_ssid_txt() result with too long SSID");
431 if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
432 wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
433 os_strcmp(longbuf, "01-0") != 0)
437 wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
445 static int os_tests(void)
451 wpa_printf(MSG_INFO, "os tests");
453 ptr = os_calloc((size_t) -1, (size_t) -1);
458 ptr = os_calloc((size_t) 2, (size_t) -1);
463 ptr = os_calloc((size_t) -1, (size_t) 2);
469 ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
477 if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
478 os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
479 os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
480 os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
481 os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
482 os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
483 os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
484 os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
485 os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
486 os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
487 os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
488 os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
489 os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
490 os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
493 if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
494 os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
495 os_unsetenv("hwsim_test_env") != 0)
498 if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
502 wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
510 static int wpabuf_tests(void)
516 wpa_printf(MSG_INFO, "wpabuf tests");
518 ptr = os_malloc(100);
520 buf = wpabuf_alloc_ext_data(ptr, 100);
522 if (wpabuf_resize(&buf, 100) < 0)
525 wpabuf_put(buf, 100);
535 buf = wpabuf_alloc(100);
539 wpabuf_put(buf, 100);
540 if (wpabuf_resize(&buf, 100) < 0)
543 wpabuf_put(buf, 100);
544 buf2 = wpabuf_concat(buf, NULL);
553 buf = wpabuf_zeropad(buf, 10);
558 wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
566 static int ip_addr_tests(void)
569 struct hostapd_ip_addr addr;
572 wpa_printf(MSG_INFO, "ip_addr tests");
574 if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
575 addr.af != AF_INET ||
576 hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
577 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
578 hostapd_ip_txt(&addr, buf, 0) != NULL ||
579 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
582 if (hostapd_parse_ip_addr("::", &addr) != 0 ||
583 addr.af != AF_INET6 ||
584 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
585 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
589 wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
599 int close_in_timeout;
605 static void eloop_tests_start(int close_in_timeout);
608 static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
610 struct test_eloop *t = eloop_ctx;
614 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
616 if (t->magic != 0x12345678) {
617 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
621 if (t->pipefd2[0] != sock) {
622 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
623 __func__, sock, t->pipefd2[0]);
626 res = read(sock, buf, sizeof(buf));
627 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
628 __func__, sock, (int) res);
632 static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
634 struct test_eloop *t = eloop_ctx;
636 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
638 if (t->magic != 0x12345678) {
639 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
643 if (t->pipefd2[0] != sock) {
644 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
645 __func__, sock, t->pipefd2[0]);
649 * This is expected to block due to the original socket with data having
650 * been closed and no new data having been written to the new socket
651 * with the same fd. To avoid blocking the process during test, skip the
654 wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
659 static void reopen_pipefd2(struct test_eloop *t)
661 if (t->pipefd2[0] < 0) {
662 wpa_printf(MSG_INFO, "pipefd2 had been closed");
666 wpa_printf(MSG_INFO, "close pipefd2");
667 eloop_unregister_read_sock(t->pipefd2[0]);
668 close(t->pipefd2[0]);
670 close(t->pipefd2[1]);
673 res = pipe(t->pipefd2);
675 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
682 "re-register pipefd2 with new sockets %d,%d",
683 t->pipefd2[0], t->pipefd2[1]);
684 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
690 static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
692 struct test_eloop *t = eloop_ctx;
696 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
698 if (t->magic != 0x12345678) {
699 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
703 if (t->pipefd1[0] != sock) {
704 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
705 __func__, sock, t->pipefd1[0]);
708 res = read(sock, buf, sizeof(buf));
709 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
710 __func__, sock, (int) res);
712 if (!t->close_in_timeout)
717 static void eloop_test_cb(void *eloop_data, void *user_ctx)
719 struct test_eloop *t = eloop_data;
721 wpa_printf(MSG_INFO, "%s", __func__);
723 if (t->magic != 0x12345678) {
724 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
728 if (t->close_in_timeout)
733 static void eloop_test_timeout(void *eloop_data, void *user_ctx)
735 struct test_eloop *t = eloop_data;
738 wpa_printf(MSG_INFO, "%s", __func__);
740 if (t->magic != 0x12345678) {
741 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
745 if (t->pipefd1[0] >= 0) {
746 wpa_printf(MSG_INFO, "pipefd1 had not been closed");
747 eloop_unregister_read_sock(t->pipefd1[0]);
748 close(t->pipefd1[0]);
750 close(t->pipefd1[1]);
754 if (t->pipefd2[0] >= 0) {
755 wpa_printf(MSG_INFO, "pipefd2 had not been closed");
756 eloop_unregister_read_sock(t->pipefd2[0]);
757 close(t->pipefd2[0]);
759 close(t->pipefd2[1]);
763 next_run = t->close_in_timeout;
765 wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
769 eloop_tests_start(0);
773 static void eloop_tests_start(int close_in_timeout)
775 struct test_eloop *t;
778 t = os_zalloc(sizeof(*t));
781 t->magic = 0x12345678;
782 t->close_in_timeout = close_in_timeout;
784 wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
785 t, close_in_timeout);
787 res = pipe(t->pipefd1);
789 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
794 res = pipe(t->pipefd2);
796 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
797 close(t->pipefd1[0]);
798 close(t->pipefd1[1]);
803 wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
804 t->pipefd1[0], t->pipefd1[1],
805 t->pipefd2[0], t->pipefd2[1]);
807 eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
808 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
809 eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
810 eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
812 if (write(t->pipefd1[1], "HELLO", 5) < 0)
813 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
814 if (write(t->pipefd2[1], "TEST", 4) < 0)
815 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
817 wpa_printf(MSG_INFO, "waiting for eloop callbacks");
821 static void eloop_tests_run(void *eloop_data, void *user_ctx)
823 eloop_tests_start(1);
827 static int eloop_tests(void)
829 wpa_printf(MSG_INFO, "schedule eloop tests to be run");
832 * Cannot return error from these without a significant design change,
833 * so for now, run the tests from a scheduled timeout and require
834 * separate verification of the results from the debug log.
836 eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
842 int utils_module_tests(void)
846 wpa_printf(MSG_INFO, "utils module tests");
848 if (printf_encode_decode_tests() < 0 ||
849 ext_password_tests() < 0 ||
851 bitfield_tests() < 0 ||
852 base64_tests() < 0 ||
853 common_tests() < 0 ||
855 wpabuf_tests() < 0 ||
856 ip_addr_tests() < 0 ||
858 int_array_tests() < 0)