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/bitfield.h"
13 #include "utils/ext_password.h"
14 #include "utils/trace.h"
15 #include "utils/base64.h"
18 struct printf_test_data {
24 static const struct printf_test_data printf_tests[] = {
25 { (u8 *) "abcde", 5, "abcde" },
26 { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
27 { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
28 { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
29 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
30 "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
31 { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
32 "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
33 { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
34 "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
39 static int printf_encode_decode_tests(void)
47 wpa_printf(MSG_INFO, "printf encode/decode tests");
49 for (i = 0; printf_tests[i].data; i++) {
50 const struct printf_test_data *test = &printf_tests[i];
51 printf_encode(buf, sizeof(buf), test->data, test->len);
52 wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
54 binlen = printf_decode(bin, sizeof(bin), buf);
55 if (binlen != test->len ||
56 os_memcmp(bin, test->data, binlen) != 0) {
57 wpa_hexdump(MSG_ERROR, "Error in decoding#1",
62 binlen = printf_decode(bin, sizeof(bin), test->encoded);
63 if (binlen != test->len ||
64 os_memcmp(bin, test->data, binlen) != 0) {
65 wpa_hexdump(MSG_ERROR, "Error in decoding#2",
72 printf_encode(buf, 5, (const u8 *) "abcde", 5);
74 wpa_printf(MSG_ERROR, "Error in bounds checking#1");
78 for (i = 5; i < 10; i++) {
80 printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
82 wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
88 if (printf_decode(bin, 3, "abcde") != 2)
91 if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
94 if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
98 wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
106 static int bitfield_tests(void)
112 wpa_printf(MSG_INFO, "bitfield tests");
114 bf = bitfield_alloc(123);
118 for (i = 0; i < 123; i++) {
119 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
121 if (i > 0 && bitfield_is_set(bf, i - 1))
124 if (!bitfield_is_set(bf, i))
126 bitfield_clear(bf, i);
127 if (bitfield_is_set(bf, i))
131 for (i = 123; i < 200; i++) {
132 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
134 if (i > 0 && bitfield_is_set(bf, i - 1))
137 if (bitfield_is_set(bf, i))
139 bitfield_clear(bf, i);
140 if (bitfield_is_set(bf, i))
144 for (i = 0; i < 123; i++) {
145 if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
148 if (!bitfield_is_set(bf, i))
152 for (i = 0; i < 123; i++) {
153 if (!bitfield_is_set(bf, i))
155 bitfield_clear(bf, i);
156 if (bitfield_is_set(bf, i))
160 for (i = 0; i < 123; i++) {
161 if (bitfield_get_first_zero(bf) != i)
165 if (bitfield_get_first_zero(bf) != -1)
167 for (i = 0; i < 123; i++) {
168 if (!bitfield_is_set(bf, i))
170 bitfield_clear(bf, i);
171 if (bitfield_get_first_zero(bf) != i)
175 if (bitfield_get_first_zero(bf) != -1)
180 bf = bitfield_alloc(8);
183 if (bitfield_get_first_zero(bf) != 0)
185 for (i = 0; i < 8; i++)
187 if (bitfield_get_first_zero(bf) != -1)
192 wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
200 static int int_array_tests(void)
202 int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
203 int test2[] = { 1, -1, 0 };
204 int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
205 int test3_res[] = { -1, 1, 2, 3, 4, 0 };
209 wpa_printf(MSG_INFO, "int_array tests");
211 if (int_array_len(test1) != 6 ||
212 int_array_len(test2) != 2)
215 int_array_sort_unique(test3);
216 len = int_array_len(test3_res);
217 if (int_array_len(test3) != len)
219 else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
223 wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
231 static int ext_password_tests(void)
233 struct ext_password_data *data;
237 wpa_printf(MSG_INFO, "ext_password tests");
239 data = ext_password_init("unknown", "foo");
243 data = ext_password_init("test", NULL);
246 pw = ext_password_get(data, "foo");
249 ext_password_free(pw);
251 ext_password_deinit(data);
253 pw = ext_password_get(NULL, "foo");
256 ext_password_free(pw);
262 static int trace_tests(void)
264 wpa_printf(MSG_INFO, "trace tests");
266 wpa_trace_show("test backtrace");
267 wpa_trace_dump_funcname("test funcname", trace_tests);
273 static int base64_tests(void)
279 wpa_printf(MSG_INFO, "base64 tests");
281 res = base64_encode((const unsigned char *) "", ~0, &res_len);
287 res = base64_encode((const unsigned char *) "=", 1, &res_len);
288 if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
289 res[2] != '=' || res[3] != '=' || res[4] != '\n')
293 res = base64_encode((const unsigned char *) "=", 1, NULL);
294 if (!res || res[0] != 'P' || res[1] != 'Q' ||
295 res[2] != '=' || res[3] != '=' || res[4] != '\n')
299 res = base64_decode((const unsigned char *) "", 0, &res_len);
305 res = base64_decode((const unsigned char *) "a", 1, &res_len);
311 res = base64_decode((const unsigned char *) "====", 4, &res_len);
317 res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
318 if (!res || res_len != 1 || res[0] != '=')
322 res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
323 if (!res || res_len != 1 || res[0] != '=')
328 wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
336 static int common_tests(void)
339 u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
342 struct wpa_freq_range_list ranges;
344 wpa_printf(MSG_INFO, "common tests");
346 if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
349 if (wpa_scnprintf(buf, 0, "hello") != 0 ||
350 wpa_scnprintf(buf, 3, "hello") != 2)
353 if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
354 wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
357 if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
358 merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
361 if (dup_binstr(NULL, 0) != NULL)
364 if (freq_range_list_includes(NULL, 0) != 0)
367 os_memset(&ranges, 0, sizeof(ranges));
368 if (freq_range_list_parse(&ranges, "") != 0 ||
369 freq_range_list_includes(&ranges, 0) != 0 ||
370 freq_range_list_str(&ranges) != NULL)
373 if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
374 utf8_unescape("a", 1, NULL, 0) != 0 ||
375 utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
376 utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
377 utf8_unescape("abc", 3, buf, 3) != 3)
380 if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
383 if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
386 if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
387 utf8_escape("a", 1, NULL, 0) != 0 ||
388 utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
389 utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
390 utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
391 utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
392 utf8_escape("abc", 3, buf, 3) != 3)
395 if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
399 wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
407 int utils_module_tests(void)
411 wpa_printf(MSG_INFO, "utils module tests");
413 if (printf_encode_decode_tests() < 0 ||
414 ext_password_tests() < 0 ||
416 bitfield_tests() < 0 ||
417 base64_tests() < 0 ||
418 common_tests() < 0 ||
419 int_array_tests() < 0)