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 "utils/const_time.h"
13 #include "common/ieee802_11_defs.h"
14 #include "utils/bitfield.h"
15 #include "utils/ext_password.h"
16 #include "utils/trace.h"
17 #include "utils/base64.h"
18 #include "utils/ip_addr.h"
19 #include "utils/eloop.h"
20 #include "utils/json.h"
21 #include "utils/module_tests.h"
24 struct printf_test_data {
30 static const struct printf_test_data printf_tests[] = {
31 { (u8 *) "abcde", 5, "abcde" },
32 { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
33 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
34 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
35 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
36 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
37 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
38 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
39 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
40 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
45 static int printf_encode_decode_tests(void)
54 wpa_printf(MSG_INFO, "printf encode/decode tests");
56 for (i = 0; printf_tests[i].data; i++) {
57 const struct printf_test_data *test = &printf_tests[i];
58 printf_encode(buf, sizeof(buf), test->data, test->len);
59 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
61 binlen = printf_decode(bin, sizeof(bin), buf);
62 if (binlen != test->len ||
63 os_memcmp(bin, test->data, binlen) != 0) {
64 wpa_hexdump(MSG_ERROR, "Error in decoding#1",
69 binlen = printf_decode(bin, sizeof(bin), test->encoded);
70 if (binlen != test->len ||
71 os_memcmp(bin, test->data, binlen) != 0) {
72 wpa_hexdump(MSG_ERROR, "Error in decoding#2",
79 printf_encode(buf, 5, (const u8 *) "abcde", 5);
81 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
85 for (i = 5; i < 10; i++) {
87 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
89 wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
95 if (printf_decode(bin, 3, "abcde") != 2)
98 if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
101 if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
104 if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
113 if (int_array_len(array) != 5)
115 int_array_sort_unique(array);
116 if (int_array_len(array) != 2)
120 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
128 static int bitfield_tests(void)
134 wpa_printf(MSG_INFO, "bitfield tests");
136 bf = bitfield_alloc(123);
140 for (i = 0; i < 123; i++) {
141 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
143 if (i > 0 && bitfield_is_set(bf, i - 1))
146 if (!bitfield_is_set(bf, i))
148 bitfield_clear(bf, i);
149 if (bitfield_is_set(bf, i))
153 for (i = 123; i < 200; i++) {
154 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
156 if (i > 0 && bitfield_is_set(bf, i - 1))
159 if (bitfield_is_set(bf, i))
161 bitfield_clear(bf, i);
162 if (bitfield_is_set(bf, i))
166 for (i = 0; i < 123; i++) {
167 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
170 if (!bitfield_is_set(bf, i))
174 for (i = 0; i < 123; i++) {
175 if (!bitfield_is_set(bf, i))
177 bitfield_clear(bf, i);
178 if (bitfield_is_set(bf, i))
182 for (i = 0; i < 123; i++) {
183 if (bitfield_get_first_zero(bf) != i)
187 if (bitfield_get_first_zero(bf) != -1)
189 for (i = 0; i < 123; i++) {
190 if (!bitfield_is_set(bf, i))
192 bitfield_clear(bf, i);
193 if (bitfield_get_first_zero(bf) != i)
197 if (bitfield_get_first_zero(bf) != -1)
202 bf = bitfield_alloc(8);
205 if (bitfield_get_first_zero(bf) != 0)
207 for (i = 0; i < 8; i++)
209 if (bitfield_get_first_zero(bf) != -1)
214 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
222 static int int_array_tests(void)
224 int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
225 int test2[] = { 1, -1, 0 };
226 int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
227 int test3_res[] = { -1, 1, 2, 3, 4, 0 };
231 wpa_printf(MSG_INFO, "int_array tests");
233 if (int_array_len(test1) != 6 ||
234 int_array_len(test2) != 2)
237 int_array_sort_unique(test3);
238 len = int_array_len(test3_res);
239 if (int_array_len(test3) != len)
241 else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
245 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
253 static int ext_password_tests(void)
255 struct ext_password_data *data;
259 wpa_printf(MSG_INFO, "ext_password tests");
261 data = ext_password_init("unknown", "foo");
265 data = ext_password_init("test", NULL);
268 pw = ext_password_get(data, "foo");
271 ext_password_free(pw);
273 ext_password_deinit(data);
275 pw = ext_password_get(NULL, "foo");
278 ext_password_free(pw);
284 static int trace_tests(void)
286 wpa_printf(MSG_INFO, "trace tests");
288 wpa_trace_show("test backtrace");
289 wpa_trace_dump_funcname("test funcname", trace_tests);
295 static int base64_tests(void)
301 wpa_printf(MSG_INFO, "base64 tests");
303 res = base64_encode((const unsigned char *) "", ~0, &res_len);
309 res = base64_encode((const unsigned char *) "=", 1, &res_len);
310 if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
311 res[2] != '=' || res[3] != '=' || res[4] != '\n')
315 res = base64_encode((const unsigned char *) "=", 1, NULL);
316 if (!res || res[0] != 'P' || res[1] != 'Q' ||
317 res[2] != '=' || res[3] != '=' || res[4] != '\n')
321 res = base64_decode((const unsigned char *) "", 0, &res_len);
327 res = base64_decode((const unsigned char *) "a", 1, &res_len);
333 res = base64_decode((const unsigned char *) "====", 4, &res_len);
339 res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
340 if (!res || res_len != 1 || res[0] != '=')
344 res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
345 if (!res || res_len != 1 || res[0] != '=')
350 wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
358 static int common_tests(void)
360 char buf[3], longbuf[100];
361 u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
364 struct wpa_freq_range_list ranges;
369 wpa_printf(MSG_INFO, "common tests");
371 if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
374 if (wpa_scnprintf(buf, 0, "hello") != 0 ||
375 wpa_scnprintf(buf, 3, "hello") != 2)
378 if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
379 wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
382 if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
383 merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
386 if (dup_binstr(NULL, 0) != NULL)
389 if (freq_range_list_includes(NULL, 0) != 0)
392 os_memset(&ranges, 0, sizeof(ranges));
393 if (freq_range_list_parse(&ranges, "") != 0 ||
394 freq_range_list_includes(&ranges, 0) != 0 ||
395 freq_range_list_str(&ranges) != NULL)
398 if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
399 utf8_unescape("a", 1, NULL, 0) != 0 ||
400 utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
401 utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
402 utf8_unescape("abc", 3, buf, 3) != 3)
405 if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
408 if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
411 if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
412 utf8_escape("a", 1, NULL, 0) != 0 ||
413 utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
414 utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
415 utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
416 utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
417 utf8_escape("abc", 3, buf, 3) != 3)
420 if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
423 os_memset(ssid, 0, sizeof(ssid));
424 txt = wpa_ssid_txt(ssid, sizeof(ssid));
425 len = os_strlen(txt);
426 /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
427 if (len != SSID_MAX_LEN * 4) {
428 wpa_printf(MSG_ERROR,
429 "Unexpected wpa_ssid_txt() result with too long SSID");
433 if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
434 wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
435 os_strcmp(longbuf, "01-0") != 0)
439 wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
447 static int os_tests(void)
453 wpa_printf(MSG_INFO, "os tests");
455 ptr = os_calloc((size_t) -1, (size_t) -1);
460 ptr = os_calloc((size_t) 2, (size_t) -1);
465 ptr = os_calloc((size_t) -1, (size_t) 2);
471 ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
479 if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
480 os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
481 os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
482 os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
483 os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
484 os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
485 os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
486 os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
487 os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
488 os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
489 os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
490 os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
491 os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
492 os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
495 if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
496 os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
497 os_unsetenv("hwsim_test_env") != 0)
500 if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
504 wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
512 static int wpabuf_tests(void)
518 wpa_printf(MSG_INFO, "wpabuf tests");
520 ptr = os_malloc(100);
522 buf = wpabuf_alloc_ext_data(ptr, 100);
524 if (wpabuf_resize(&buf, 100) < 0)
527 wpabuf_put(buf, 100);
537 buf = wpabuf_alloc(100);
541 wpabuf_put(buf, 100);
542 if (wpabuf_resize(&buf, 100) < 0)
545 wpabuf_put(buf, 100);
546 buf2 = wpabuf_concat(buf, NULL);
555 buf = wpabuf_zeropad(buf, 10);
560 wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
568 static int ip_addr_tests(void)
571 struct hostapd_ip_addr addr;
574 wpa_printf(MSG_INFO, "ip_addr tests");
576 if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
577 addr.af != AF_INET ||
578 hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
579 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
580 hostapd_ip_txt(&addr, buf, 0) != NULL ||
581 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
584 if (hostapd_parse_ip_addr("::", &addr) != 0 ||
585 addr.af != AF_INET6 ||
586 hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
587 hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
591 wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
601 int close_in_timeout;
607 static void eloop_tests_start(int close_in_timeout);
610 static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
612 struct test_eloop *t = eloop_ctx;
616 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
618 if (t->magic != 0x12345678) {
619 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
623 if (t->pipefd2[0] != sock) {
624 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
625 __func__, sock, t->pipefd2[0]);
628 res = read(sock, buf, sizeof(buf));
629 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
630 __func__, sock, (int) res);
634 static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
636 struct test_eloop *t = eloop_ctx;
638 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
640 if (t->magic != 0x12345678) {
641 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
645 if (t->pipefd2[0] != sock) {
646 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
647 __func__, sock, t->pipefd2[0]);
651 * This is expected to block due to the original socket with data having
652 * been closed and no new data having been written to the new socket
653 * with the same fd. To avoid blocking the process during test, skip the
656 wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
661 static void reopen_pipefd2(struct test_eloop *t)
663 if (t->pipefd2[0] < 0) {
664 wpa_printf(MSG_INFO, "pipefd2 had been closed");
668 wpa_printf(MSG_INFO, "close pipefd2");
669 eloop_unregister_read_sock(t->pipefd2[0]);
670 close(t->pipefd2[0]);
672 close(t->pipefd2[1]);
675 res = pipe(t->pipefd2);
677 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
684 "re-register pipefd2 with new sockets %d,%d",
685 t->pipefd2[0], t->pipefd2[1]);
686 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
692 static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
694 struct test_eloop *t = eloop_ctx;
698 wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
700 if (t->magic != 0x12345678) {
701 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
705 if (t->pipefd1[0] != sock) {
706 wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
707 __func__, sock, t->pipefd1[0]);
710 res = read(sock, buf, sizeof(buf));
711 wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
712 __func__, sock, (int) res);
714 if (!t->close_in_timeout)
719 static void eloop_test_cb(void *eloop_data, void *user_ctx)
721 struct test_eloop *t = eloop_data;
723 wpa_printf(MSG_INFO, "%s", __func__);
725 if (t->magic != 0x12345678) {
726 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
730 if (t->close_in_timeout)
735 static void eloop_test_timeout(void *eloop_data, void *user_ctx)
737 struct test_eloop *t = eloop_data;
740 wpa_printf(MSG_INFO, "%s", __func__);
742 if (t->magic != 0x12345678) {
743 wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
747 if (t->pipefd1[0] >= 0) {
748 wpa_printf(MSG_INFO, "pipefd1 had not been closed");
749 eloop_unregister_read_sock(t->pipefd1[0]);
750 close(t->pipefd1[0]);
752 close(t->pipefd1[1]);
756 if (t->pipefd2[0] >= 0) {
757 wpa_printf(MSG_INFO, "pipefd2 had not been closed");
758 eloop_unregister_read_sock(t->pipefd2[0]);
759 close(t->pipefd2[0]);
761 close(t->pipefd2[1]);
765 next_run = t->close_in_timeout;
767 wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
771 eloop_tests_start(0);
775 static void eloop_tests_start(int close_in_timeout)
777 struct test_eloop *t;
780 t = os_zalloc(sizeof(*t));
783 t->magic = 0x12345678;
784 t->close_in_timeout = close_in_timeout;
786 wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
787 t, close_in_timeout);
789 res = pipe(t->pipefd1);
791 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
796 res = pipe(t->pipefd2);
798 wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
799 close(t->pipefd1[0]);
800 close(t->pipefd1[1]);
805 wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
806 t->pipefd1[0], t->pipefd1[1],
807 t->pipefd2[0], t->pipefd2[1]);
809 eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
810 eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
811 eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
812 eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
814 if (write(t->pipefd1[1], "HELLO", 5) < 0)
815 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
816 if (write(t->pipefd2[1], "TEST", 4) < 0)
817 wpa_printf(MSG_INFO, "write: %s", strerror(errno));
819 wpa_printf(MSG_INFO, "waiting for eloop callbacks");
823 static void eloop_tests_run(void *eloop_data, void *user_ctx)
825 eloop_tests_start(1);
829 static int eloop_tests(void)
831 wpa_printf(MSG_INFO, "schedule eloop tests to be run");
834 * Cannot return error from these without a significant design change,
835 * so for now, run the tests from a scheduled timeout and require
836 * separate verification of the results from the debug log.
838 eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
845 struct json_test_data {
850 static const struct json_test_data json_test_cases[] = {
851 { "{}", "[1:OBJECT:]" },
852 { "[]", "[1:ARRAY:]" },
857 { "[[]]", "[1:ARRAY:][2:ARRAY:]" },
858 { "{\"t\":\"test\"}", "[1:OBJECT:][2:STRING:t]" },
859 { "{\"t\":123}", "[1:OBJECT:][2:NUMBER:t]" },
860 { "{\"t\":true}", "[1:OBJECT:][2:BOOLEAN:t]" },
861 { "{\"t\":false}", "[1:OBJECT:][2:BOOLEAN:t]" },
862 { "{\"t\":null}", "[1:OBJECT:][2:NULL:t]" },
863 { "{\"t\":truetrue}", NULL },
864 { "\"test\"", "[1:STRING:]" },
865 { "123", "[1:NUMBER:]" },
866 { "true", "[1:BOOLEAN:]" },
867 { "false", "[1:BOOLEAN:]" },
868 { "null", "[1:NULL:]" },
869 { "truetrue", NULL },
870 { " {\t\n\r\"a\"\n:\r1\n,\n\"b\":3\n}\n",
871 "[1:OBJECT:][2:NUMBER:a][2:NUMBER:b]" },
878 { "{ \"\\u005c\" : \"\\u005c\" }", "[1:OBJECT:][2:STRING:\\]" },
879 { "[{},{}]", "[1:ARRAY:][2:OBJECT:][2:OBJECT:]" },
880 { "[1,2]", "[1:ARRAY:][2:NUMBER:][2:NUMBER:]" },
881 { "[\"1\",\"2\"]", "[1:ARRAY:][2:STRING:][2:STRING:]" },
882 { "[true,false]", "[1:ARRAY:][2:BOOLEAN:][2:BOOLEAN:]" },
884 #endif /* CONFIG_JSON */
887 static int json_tests(void)
891 struct json_token *root;
894 wpa_printf(MSG_INFO, "JSON tests");
896 for (i = 0; i < ARRAY_SIZE(json_test_cases); i++) {
897 const struct json_test_data *test = &json_test_cases[i];
900 root = json_parse(test->json, os_strlen(test->json));
901 if ((root && !test->tree) || (!root && test->tree)) {
902 wpa_printf(MSG_INFO, "JSON test %u failed", i);
905 json_print_tree(root, buf, sizeof(buf));
906 if (os_strcmp(buf, test->tree) != 0) {
908 "JSON test %u tree mismatch: %s %s",
918 #endif /* CONFIG_JSON */
923 static int const_time_tests(void)
925 struct const_time_fill_msb_test {
927 unsigned int expected;
928 } const_time_fill_msb_tests[] = {
932 { 1 << (sizeof(unsigned int) * 8 - 1), ~0 },
936 struct const_time_is_zero_test {
938 unsigned int expected;
939 } const_time_is_zero_tests[] = {
943 { 1 << (sizeof(unsigned int) * 8 - 1), 0 },
947 struct const_time_eq_test {
950 unsigned int expected;
951 unsigned int expected_u8;
952 } const_time_eq_tests[] = {
956 { ~0, ~0, ~0, 0xff },
957 { ~0, ~0 - 1, 0, 0 },
960 struct const_time_eq_bin_test {
964 unsigned int expected;
965 } const_time_eq_bin_tests[] = {
966 { (u8 *) "", (u8 *) "", 0, ~0 },
967 { (u8 *) "abcde", (u8 *) "abcde", 5, ~0 },
968 { (u8 *) "abcde", (u8 *) "Abcde", 5, 0 },
969 { (u8 *) "abcde", (u8 *) "aBcde", 5, 0 },
970 { (u8 *) "abcde", (u8 *) "abCde", 5, 0 },
971 { (u8 *) "abcde", (u8 *) "abcDe", 5, 0 },
972 { (u8 *) "abcde", (u8 *) "abcdE", 5, 0 },
973 { (u8 *) "\x00", (u8 *) "\x01", 1, 0 },
974 { (u8 *) "\x00", (u8 *) "\x80", 1, 0 },
975 { (u8 *) "\x00", (u8 *) "\x00", 1, ~0 }
977 struct const_time_select_test {
979 unsigned int true_val;
980 unsigned int false_val;
981 unsigned int expected;
982 } const_time_select_tests[] = {
987 { ~0, 0xaaaaaaaa, 0x55555555, 0xaaaaaaaa },
988 { 0, 0xaaaaaaaa, 0x55555555, 0x55555555 },
994 struct const_time_select_int_test {
999 } const_time_select_int_tests[] = {
1000 { ~0, -128, 127, -128 },
1001 { 0, -128, 127, 127 },
1002 { ~0, -2147483648, 2147483647, -2147483648 },
1003 { 0, -2147483648, 2147483647, 2147483647 },
1009 struct const_time_select_u8_test {
1014 } const_time_select_u8_tests[] = {
1019 { ~0, 0xaa, 0x55, 0xaa },
1020 { 0, 0xaa, 0x55, 0x55 },
1024 struct const_time_select_s8_test {
1029 } const_time_select_s8_tests[] = {
1030 { ~0, -128, 127, -128 },
1031 { 0, -128, 127, 127 },
1037 struct const_time_select_bin_test {
1043 } const_time_select_bin_tests[] = {
1044 { ~0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "abcde" },
1045 { 0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "ABCDE" },
1046 { ~0, (u8 *) "", (u8 *) "", 0, (u8 *) "" },
1047 { 0, (u8 *) "", (u8 *) "", 0, (u8 *) "" }
1049 struct const_time_memcmp_test {
1054 } const_time_memcmp_tests[] = {
1055 { "abcde", "abcde", 5, 0 },
1056 { "abcde", "bbcde", 5, -1 },
1057 { "bbcde", "abcde", 5, 1 },
1058 { "accde", "abcde", 5, 1 },
1059 { "abcee", "abcde", 5, 1 },
1060 { "abcdf", "abcde", 5, 1 },
1061 { "cbcde", "aXXXX", 5, 2 },
1062 { "a", "d", 1, -3 },
1068 wpa_printf(MSG_INFO, "constant time tests");
1070 for (i = 0; i < ARRAY_SIZE(const_time_fill_msb_tests); i++) {
1071 struct const_time_fill_msb_test *test;
1073 test = &const_time_fill_msb_tests[i];
1074 if (const_time_fill_msb(test->val) != test->expected) {
1075 wpa_printf(MSG_ERROR,
1076 "const_time_fill_msb(0x%x) test failed",
1082 for (i = 0; i < ARRAY_SIZE(const_time_is_zero_tests); i++) {
1083 struct const_time_is_zero_test *test;
1085 test = &const_time_is_zero_tests[i];
1086 if (const_time_is_zero(test->val) != test->expected) {
1087 wpa_printf(MSG_ERROR,
1088 "const_time_is_zero(0x%x) test failed",
1094 for (i = 0; i < ARRAY_SIZE(const_time_eq_tests); i++) {
1095 struct const_time_eq_test *test;
1097 test = &const_time_eq_tests[i];
1098 if (const_time_eq(test->a, test->b) != test->expected) {
1099 wpa_printf(MSG_ERROR,
1100 "const_time_eq(0x%x,0x%x) test failed",
1104 if (const_time_eq_u8(test->a, test->b) != test->expected_u8) {
1105 wpa_printf(MSG_ERROR,
1106 "const_time_eq_u8(0x%x,0x%x) test failed",
1112 for (i = 0; i < ARRAY_SIZE(const_time_eq_bin_tests); i++) {
1113 struct const_time_eq_bin_test *test;
1115 test = &const_time_eq_bin_tests[i];
1116 if (const_time_eq_bin(test->a, test->b, test->len) !=
1118 wpa_printf(MSG_ERROR,
1119 "const_time_eq_bin(len=%u) test failed",
1120 (unsigned int) test->len);
1125 for (i = 0; i < ARRAY_SIZE(const_time_select_tests); i++) {
1126 struct const_time_select_test *test;
1128 test = &const_time_select_tests[i];
1129 if (const_time_select(test->mask, test->true_val,
1130 test->false_val) != test->expected) {
1131 wpa_printf(MSG_ERROR,
1132 "const_time_select(0x%x,0x%x,0x%x) test failed",
1133 test->mask, test->true_val, test->false_val);
1138 for (i = 0; i < ARRAY_SIZE(const_time_select_int_tests); i++) {
1139 struct const_time_select_int_test *test;
1141 test = &const_time_select_int_tests[i];
1142 if (const_time_select_int(test->mask, test->true_val,
1143 test->false_val) != test->expected) {
1144 wpa_printf(MSG_ERROR,
1145 "const_time_select_int(0x%x,%d,%d) test failed",
1146 test->mask, test->true_val, test->false_val);
1151 for (i = 0; i < ARRAY_SIZE(const_time_select_u8_tests); i++) {
1152 struct const_time_select_u8_test *test;
1154 test = &const_time_select_u8_tests[i];
1155 if (const_time_select_u8(test->mask, test->true_val,
1156 test->false_val) != test->expected) {
1157 wpa_printf(MSG_ERROR,
1158 "const_time_select_u8(0x%x,0x%x,0x%x) test failed",
1159 test->mask, test->true_val, test->false_val);
1164 for (i = 0; i < ARRAY_SIZE(const_time_select_s8_tests); i++) {
1165 struct const_time_select_s8_test *test;
1167 test = &const_time_select_s8_tests[i];
1168 if (const_time_select_s8(test->mask, test->true_val,
1169 test->false_val) != test->expected) {
1170 wpa_printf(MSG_ERROR,
1171 "const_time_select_s8(0x%x,0x%x,0x%x) test failed",
1172 test->mask, test->true_val, test->false_val);
1177 for (i = 0; i < ARRAY_SIZE(const_time_select_bin_tests); i++) {
1178 struct const_time_select_bin_test *test;
1181 test = &const_time_select_bin_tests[i];
1182 const_time_select_bin(test->mask, test->true_val,
1183 test->false_val, test->len, dst);
1184 if (os_memcmp(dst, test->expected, test->len) != 0) {
1185 wpa_printf(MSG_ERROR,
1186 "const_time_select_bin(0x%x,%u) test failed",
1187 test->mask, (unsigned int) test->len);
1192 for (i = 0; i < ARRAY_SIZE(const_time_memcmp_tests); i++) {
1193 struct const_time_memcmp_test *test;
1196 test = &const_time_memcmp_tests[i];
1197 res = const_time_memcmp(test->a, test->b, test->len);
1198 if (res != test->expected) {
1199 wpa_printf(MSG_ERROR,
1200 "const_time_memcmp(%s,%s,%d) test failed (%d != %d)",
1201 test->a, test->b, (int) test->len,
1202 res, test->expected);
1211 int utils_module_tests(void)
1215 wpa_printf(MSG_INFO, "utils module tests");
1217 if (printf_encode_decode_tests() < 0 ||
1218 ext_password_tests() < 0 ||
1219 trace_tests() < 0 ||
1220 bitfield_tests() < 0 ||
1221 base64_tests() < 0 ||
1222 common_tests() < 0 ||
1224 wpabuf_tests() < 0 ||
1225 ip_addr_tests() < 0 ||
1226 eloop_tests() < 0 ||
1228 const_time_tests() < 0 ||
1229 int_array_tests() < 0)