1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt * utils module tests
3*a1157835SDaniel Fojt * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
4*a1157835SDaniel Fojt *
5*a1157835SDaniel Fojt * This software may be distributed under the terms of the BSD license.
6*a1157835SDaniel Fojt * See README for more details.
7*a1157835SDaniel Fojt */
8*a1157835SDaniel Fojt
9*a1157835SDaniel Fojt #include "utils/includes.h"
10*a1157835SDaniel Fojt
11*a1157835SDaniel Fojt #include "utils/common.h"
12*a1157835SDaniel Fojt #include "utils/const_time.h"
13*a1157835SDaniel Fojt #include "common/ieee802_11_defs.h"
14*a1157835SDaniel Fojt #include "utils/bitfield.h"
15*a1157835SDaniel Fojt #include "utils/ext_password.h"
16*a1157835SDaniel Fojt #include "utils/trace.h"
17*a1157835SDaniel Fojt #include "utils/base64.h"
18*a1157835SDaniel Fojt #include "utils/ip_addr.h"
19*a1157835SDaniel Fojt #include "utils/eloop.h"
20*a1157835SDaniel Fojt #include "utils/json.h"
21*a1157835SDaniel Fojt #include "utils/module_tests.h"
22*a1157835SDaniel Fojt
23*a1157835SDaniel Fojt
24*a1157835SDaniel Fojt struct printf_test_data {
25*a1157835SDaniel Fojt u8 *data;
26*a1157835SDaniel Fojt size_t len;
27*a1157835SDaniel Fojt char *encoded;
28*a1157835SDaniel Fojt };
29*a1157835SDaniel Fojt
30*a1157835SDaniel Fojt static const struct printf_test_data printf_tests[] = {
31*a1157835SDaniel Fojt { (u8 *) "abcde", 5, "abcde" },
32*a1157835SDaniel Fojt { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
33*a1157835SDaniel Fojt { (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
34*a1157835SDaniel Fojt { (u8 *) "\n\n\n", 3, "\n\12\x0a" },
35*a1157835SDaniel Fojt { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
36*a1157835SDaniel Fojt "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
37*a1157835SDaniel Fojt { (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
38*a1157835SDaniel Fojt "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
39*a1157835SDaniel Fojt { (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
40*a1157835SDaniel Fojt "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
41*a1157835SDaniel Fojt { NULL, 0, NULL }
42*a1157835SDaniel Fojt };
43*a1157835SDaniel Fojt
44*a1157835SDaniel Fojt
printf_encode_decode_tests(void)45*a1157835SDaniel Fojt static int printf_encode_decode_tests(void)
46*a1157835SDaniel Fojt {
47*a1157835SDaniel Fojt int i;
48*a1157835SDaniel Fojt size_t binlen;
49*a1157835SDaniel Fojt char buf[100];
50*a1157835SDaniel Fojt u8 bin[100];
51*a1157835SDaniel Fojt int errors = 0;
52*a1157835SDaniel Fojt int array[10];
53*a1157835SDaniel Fojt
54*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "printf encode/decode tests");
55*a1157835SDaniel Fojt
56*a1157835SDaniel Fojt for (i = 0; printf_tests[i].data; i++) {
57*a1157835SDaniel Fojt const struct printf_test_data *test = &printf_tests[i];
58*a1157835SDaniel Fojt printf_encode(buf, sizeof(buf), test->data, test->len);
59*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
60*a1157835SDaniel Fojt
61*a1157835SDaniel Fojt binlen = printf_decode(bin, sizeof(bin), buf);
62*a1157835SDaniel Fojt if (binlen != test->len ||
63*a1157835SDaniel Fojt os_memcmp(bin, test->data, binlen) != 0) {
64*a1157835SDaniel Fojt wpa_hexdump(MSG_ERROR, "Error in decoding#1",
65*a1157835SDaniel Fojt bin, binlen);
66*a1157835SDaniel Fojt errors++;
67*a1157835SDaniel Fojt }
68*a1157835SDaniel Fojt
69*a1157835SDaniel Fojt binlen = printf_decode(bin, sizeof(bin), test->encoded);
70*a1157835SDaniel Fojt if (binlen != test->len ||
71*a1157835SDaniel Fojt os_memcmp(bin, test->data, binlen) != 0) {
72*a1157835SDaniel Fojt wpa_hexdump(MSG_ERROR, "Error in decoding#2",
73*a1157835SDaniel Fojt bin, binlen);
74*a1157835SDaniel Fojt errors++;
75*a1157835SDaniel Fojt }
76*a1157835SDaniel Fojt }
77*a1157835SDaniel Fojt
78*a1157835SDaniel Fojt buf[5] = 'A';
79*a1157835SDaniel Fojt printf_encode(buf, 5, (const u8 *) "abcde", 5);
80*a1157835SDaniel Fojt if (buf[5] != 'A') {
81*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "Error in bounds checking#1");
82*a1157835SDaniel Fojt errors++;
83*a1157835SDaniel Fojt }
84*a1157835SDaniel Fojt
85*a1157835SDaniel Fojt for (i = 5; i < 10; i++) {
86*a1157835SDaniel Fojt buf[i] = 'A';
87*a1157835SDaniel Fojt printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
88*a1157835SDaniel Fojt if (buf[i] != 'A') {
89*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
90*a1157835SDaniel Fojt i);
91*a1157835SDaniel Fojt errors++;
92*a1157835SDaniel Fojt }
93*a1157835SDaniel Fojt }
94*a1157835SDaniel Fojt
95*a1157835SDaniel Fojt if (printf_decode(bin, 3, "abcde") != 2)
96*a1157835SDaniel Fojt errors++;
97*a1157835SDaniel Fojt
98*a1157835SDaniel Fojt if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
99*a1157835SDaniel Fojt errors++;
100*a1157835SDaniel Fojt
101*a1157835SDaniel Fojt if (printf_decode(bin, 3, "\\xq") != 1 || bin[0] != 'q')
102*a1157835SDaniel Fojt errors++;
103*a1157835SDaniel Fojt
104*a1157835SDaniel Fojt if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
105*a1157835SDaniel Fojt errors++;
106*a1157835SDaniel Fojt
107*a1157835SDaniel Fojt array[0] = 10;
108*a1157835SDaniel Fojt array[1] = 10;
109*a1157835SDaniel Fojt array[2] = 5;
110*a1157835SDaniel Fojt array[3] = 10;
111*a1157835SDaniel Fojt array[4] = 5;
112*a1157835SDaniel Fojt array[5] = 0;
113*a1157835SDaniel Fojt if (int_array_len(array) != 5)
114*a1157835SDaniel Fojt errors++;
115*a1157835SDaniel Fojt int_array_sort_unique(array);
116*a1157835SDaniel Fojt if (int_array_len(array) != 2)
117*a1157835SDaniel Fojt errors++;
118*a1157835SDaniel Fojt
119*a1157835SDaniel Fojt if (errors) {
120*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
121*a1157835SDaniel Fojt return -1;
122*a1157835SDaniel Fojt }
123*a1157835SDaniel Fojt
124*a1157835SDaniel Fojt return 0;
125*a1157835SDaniel Fojt }
126*a1157835SDaniel Fojt
127*a1157835SDaniel Fojt
bitfield_tests(void)128*a1157835SDaniel Fojt static int bitfield_tests(void)
129*a1157835SDaniel Fojt {
130*a1157835SDaniel Fojt struct bitfield *bf;
131*a1157835SDaniel Fojt int i;
132*a1157835SDaniel Fojt int errors = 0;
133*a1157835SDaniel Fojt
134*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "bitfield tests");
135*a1157835SDaniel Fojt
136*a1157835SDaniel Fojt bf = bitfield_alloc(123);
137*a1157835SDaniel Fojt if (bf == NULL)
138*a1157835SDaniel Fojt return -1;
139*a1157835SDaniel Fojt
140*a1157835SDaniel Fojt for (i = 0; i < 123; i++) {
141*a1157835SDaniel Fojt if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
142*a1157835SDaniel Fojt errors++;
143*a1157835SDaniel Fojt if (i > 0 && bitfield_is_set(bf, i - 1))
144*a1157835SDaniel Fojt errors++;
145*a1157835SDaniel Fojt bitfield_set(bf, i);
146*a1157835SDaniel Fojt if (!bitfield_is_set(bf, i))
147*a1157835SDaniel Fojt errors++;
148*a1157835SDaniel Fojt bitfield_clear(bf, i);
149*a1157835SDaniel Fojt if (bitfield_is_set(bf, i))
150*a1157835SDaniel Fojt errors++;
151*a1157835SDaniel Fojt }
152*a1157835SDaniel Fojt
153*a1157835SDaniel Fojt for (i = 123; i < 200; i++) {
154*a1157835SDaniel Fojt if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
155*a1157835SDaniel Fojt errors++;
156*a1157835SDaniel Fojt if (i > 0 && bitfield_is_set(bf, i - 1))
157*a1157835SDaniel Fojt errors++;
158*a1157835SDaniel Fojt bitfield_set(bf, i);
159*a1157835SDaniel Fojt if (bitfield_is_set(bf, i))
160*a1157835SDaniel Fojt errors++;
161*a1157835SDaniel Fojt bitfield_clear(bf, i);
162*a1157835SDaniel Fojt if (bitfield_is_set(bf, i))
163*a1157835SDaniel Fojt errors++;
164*a1157835SDaniel Fojt }
165*a1157835SDaniel Fojt
166*a1157835SDaniel Fojt for (i = 0; i < 123; i++) {
167*a1157835SDaniel Fojt if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
168*a1157835SDaniel Fojt errors++;
169*a1157835SDaniel Fojt bitfield_set(bf, i);
170*a1157835SDaniel Fojt if (!bitfield_is_set(bf, i))
171*a1157835SDaniel Fojt errors++;
172*a1157835SDaniel Fojt }
173*a1157835SDaniel Fojt
174*a1157835SDaniel Fojt for (i = 0; i < 123; i++) {
175*a1157835SDaniel Fojt if (!bitfield_is_set(bf, i))
176*a1157835SDaniel Fojt errors++;
177*a1157835SDaniel Fojt bitfield_clear(bf, i);
178*a1157835SDaniel Fojt if (bitfield_is_set(bf, i))
179*a1157835SDaniel Fojt errors++;
180*a1157835SDaniel Fojt }
181*a1157835SDaniel Fojt
182*a1157835SDaniel Fojt for (i = 0; i < 123; i++) {
183*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != i)
184*a1157835SDaniel Fojt errors++;
185*a1157835SDaniel Fojt bitfield_set(bf, i);
186*a1157835SDaniel Fojt }
187*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != -1)
188*a1157835SDaniel Fojt errors++;
189*a1157835SDaniel Fojt for (i = 0; i < 123; i++) {
190*a1157835SDaniel Fojt if (!bitfield_is_set(bf, i))
191*a1157835SDaniel Fojt errors++;
192*a1157835SDaniel Fojt bitfield_clear(bf, i);
193*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != i)
194*a1157835SDaniel Fojt errors++;
195*a1157835SDaniel Fojt bitfield_set(bf, i);
196*a1157835SDaniel Fojt }
197*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != -1)
198*a1157835SDaniel Fojt errors++;
199*a1157835SDaniel Fojt
200*a1157835SDaniel Fojt bitfield_free(bf);
201*a1157835SDaniel Fojt
202*a1157835SDaniel Fojt bf = bitfield_alloc(8);
203*a1157835SDaniel Fojt if (bf == NULL)
204*a1157835SDaniel Fojt return -1;
205*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != 0)
206*a1157835SDaniel Fojt errors++;
207*a1157835SDaniel Fojt for (i = 0; i < 8; i++)
208*a1157835SDaniel Fojt bitfield_set(bf, i);
209*a1157835SDaniel Fojt if (bitfield_get_first_zero(bf) != -1)
210*a1157835SDaniel Fojt errors++;
211*a1157835SDaniel Fojt bitfield_free(bf);
212*a1157835SDaniel Fojt
213*a1157835SDaniel Fojt if (errors) {
214*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
215*a1157835SDaniel Fojt return -1;
216*a1157835SDaniel Fojt }
217*a1157835SDaniel Fojt
218*a1157835SDaniel Fojt return 0;
219*a1157835SDaniel Fojt }
220*a1157835SDaniel Fojt
221*a1157835SDaniel Fojt
int_array_tests(void)222*a1157835SDaniel Fojt static int int_array_tests(void)
223*a1157835SDaniel Fojt {
224*a1157835SDaniel Fojt int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
225*a1157835SDaniel Fojt int test2[] = { 1, -1, 0 };
226*a1157835SDaniel Fojt int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
227*a1157835SDaniel Fojt int test3_res[] = { -1, 1, 2, 3, 4, 0 };
228*a1157835SDaniel Fojt int errors = 0;
229*a1157835SDaniel Fojt int len;
230*a1157835SDaniel Fojt
231*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "int_array tests");
232*a1157835SDaniel Fojt
233*a1157835SDaniel Fojt if (int_array_len(test1) != 6 ||
234*a1157835SDaniel Fojt int_array_len(test2) != 2)
235*a1157835SDaniel Fojt errors++;
236*a1157835SDaniel Fojt
237*a1157835SDaniel Fojt int_array_sort_unique(test3);
238*a1157835SDaniel Fojt len = int_array_len(test3_res);
239*a1157835SDaniel Fojt if (int_array_len(test3) != len)
240*a1157835SDaniel Fojt errors++;
241*a1157835SDaniel Fojt else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
242*a1157835SDaniel Fojt errors++;
243*a1157835SDaniel Fojt
244*a1157835SDaniel Fojt if (errors) {
245*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
246*a1157835SDaniel Fojt return -1;
247*a1157835SDaniel Fojt }
248*a1157835SDaniel Fojt
249*a1157835SDaniel Fojt return 0;
250*a1157835SDaniel Fojt }
251*a1157835SDaniel Fojt
252*a1157835SDaniel Fojt
ext_password_tests(void)253*a1157835SDaniel Fojt static int ext_password_tests(void)
254*a1157835SDaniel Fojt {
255*a1157835SDaniel Fojt struct ext_password_data *data;
256*a1157835SDaniel Fojt int ret = 0;
257*a1157835SDaniel Fojt struct wpabuf *pw;
258*a1157835SDaniel Fojt
259*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "ext_password tests");
260*a1157835SDaniel Fojt
261*a1157835SDaniel Fojt data = ext_password_init("unknown", "foo");
262*a1157835SDaniel Fojt if (data != NULL)
263*a1157835SDaniel Fojt return -1;
264*a1157835SDaniel Fojt
265*a1157835SDaniel Fojt data = ext_password_init("test", NULL);
266*a1157835SDaniel Fojt if (data == NULL)
267*a1157835SDaniel Fojt return -1;
268*a1157835SDaniel Fojt pw = ext_password_get(data, "foo");
269*a1157835SDaniel Fojt if (pw != NULL)
270*a1157835SDaniel Fojt ret = -1;
271*a1157835SDaniel Fojt ext_password_free(pw);
272*a1157835SDaniel Fojt
273*a1157835SDaniel Fojt ext_password_deinit(data);
274*a1157835SDaniel Fojt
275*a1157835SDaniel Fojt pw = ext_password_get(NULL, "foo");
276*a1157835SDaniel Fojt if (pw != NULL)
277*a1157835SDaniel Fojt ret = -1;
278*a1157835SDaniel Fojt ext_password_free(pw);
279*a1157835SDaniel Fojt
280*a1157835SDaniel Fojt return ret;
281*a1157835SDaniel Fojt }
282*a1157835SDaniel Fojt
283*a1157835SDaniel Fojt
trace_tests(void)284*a1157835SDaniel Fojt static int trace_tests(void)
285*a1157835SDaniel Fojt {
286*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "trace tests");
287*a1157835SDaniel Fojt
288*a1157835SDaniel Fojt wpa_trace_show("test backtrace");
289*a1157835SDaniel Fojt wpa_trace_dump_funcname("test funcname", trace_tests);
290*a1157835SDaniel Fojt
291*a1157835SDaniel Fojt return 0;
292*a1157835SDaniel Fojt }
293*a1157835SDaniel Fojt
294*a1157835SDaniel Fojt
base64_tests(void)295*a1157835SDaniel Fojt static int base64_tests(void)
296*a1157835SDaniel Fojt {
297*a1157835SDaniel Fojt int errors = 0;
298*a1157835SDaniel Fojt unsigned char *res;
299*a1157835SDaniel Fojt size_t res_len;
300*a1157835SDaniel Fojt
301*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "base64 tests");
302*a1157835SDaniel Fojt
303*a1157835SDaniel Fojt res = base64_encode((const unsigned char *) "", ~0, &res_len);
304*a1157835SDaniel Fojt if (res) {
305*a1157835SDaniel Fojt errors++;
306*a1157835SDaniel Fojt os_free(res);
307*a1157835SDaniel Fojt }
308*a1157835SDaniel Fojt
309*a1157835SDaniel Fojt res = base64_encode((const unsigned char *) "=", 1, &res_len);
310*a1157835SDaniel Fojt if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
311*a1157835SDaniel Fojt res[2] != '=' || res[3] != '=' || res[4] != '\n')
312*a1157835SDaniel Fojt errors++;
313*a1157835SDaniel Fojt os_free(res);
314*a1157835SDaniel Fojt
315*a1157835SDaniel Fojt res = base64_encode((const unsigned char *) "=", 1, NULL);
316*a1157835SDaniel Fojt if (!res || res[0] != 'P' || res[1] != 'Q' ||
317*a1157835SDaniel Fojt res[2] != '=' || res[3] != '=' || res[4] != '\n')
318*a1157835SDaniel Fojt errors++;
319*a1157835SDaniel Fojt os_free(res);
320*a1157835SDaniel Fojt
321*a1157835SDaniel Fojt res = base64_decode((const unsigned char *) "", 0, &res_len);
322*a1157835SDaniel Fojt if (res) {
323*a1157835SDaniel Fojt errors++;
324*a1157835SDaniel Fojt os_free(res);
325*a1157835SDaniel Fojt }
326*a1157835SDaniel Fojt
327*a1157835SDaniel Fojt res = base64_decode((const unsigned char *) "a", 1, &res_len);
328*a1157835SDaniel Fojt if (res) {
329*a1157835SDaniel Fojt errors++;
330*a1157835SDaniel Fojt os_free(res);
331*a1157835SDaniel Fojt }
332*a1157835SDaniel Fojt
333*a1157835SDaniel Fojt res = base64_decode((const unsigned char *) "====", 4, &res_len);
334*a1157835SDaniel Fojt if (res) {
335*a1157835SDaniel Fojt errors++;
336*a1157835SDaniel Fojt os_free(res);
337*a1157835SDaniel Fojt }
338*a1157835SDaniel Fojt
339*a1157835SDaniel Fojt res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
340*a1157835SDaniel Fojt if (!res || res_len != 1 || res[0] != '=')
341*a1157835SDaniel Fojt errors++;
342*a1157835SDaniel Fojt os_free(res);
343*a1157835SDaniel Fojt
344*a1157835SDaniel Fojt res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
345*a1157835SDaniel Fojt if (!res || res_len != 1 || res[0] != '=')
346*a1157835SDaniel Fojt errors++;
347*a1157835SDaniel Fojt os_free(res);
348*a1157835SDaniel Fojt
349*a1157835SDaniel Fojt if (errors) {
350*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
351*a1157835SDaniel Fojt return -1;
352*a1157835SDaniel Fojt }
353*a1157835SDaniel Fojt
354*a1157835SDaniel Fojt return 0;
355*a1157835SDaniel Fojt }
356*a1157835SDaniel Fojt
357*a1157835SDaniel Fojt
common_tests(void)358*a1157835SDaniel Fojt static int common_tests(void)
359*a1157835SDaniel Fojt {
360*a1157835SDaniel Fojt char buf[3], longbuf[100];
361*a1157835SDaniel Fojt u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
362*a1157835SDaniel Fojt u8 bin[3];
363*a1157835SDaniel Fojt int errors = 0;
364*a1157835SDaniel Fojt struct wpa_freq_range_list ranges;
365*a1157835SDaniel Fojt size_t len;
366*a1157835SDaniel Fojt const char *txt;
367*a1157835SDaniel Fojt u8 ssid[255];
368*a1157835SDaniel Fojt
369*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "common tests");
370*a1157835SDaniel Fojt
371*a1157835SDaniel Fojt if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
372*a1157835SDaniel Fojt errors++;
373*a1157835SDaniel Fojt
374*a1157835SDaniel Fojt if (wpa_scnprintf(buf, 0, "hello") != 0 ||
375*a1157835SDaniel Fojt wpa_scnprintf(buf, 3, "hello") != 2)
376*a1157835SDaniel Fojt errors++;
377*a1157835SDaniel Fojt
378*a1157835SDaniel Fojt if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
379*a1157835SDaniel Fojt wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
380*a1157835SDaniel Fojt errors++;
381*a1157835SDaniel Fojt
382*a1157835SDaniel Fojt if (merge_byte_arrays(bin, 3, addr, ETH_ALEN, NULL, 0) != 3 ||
383*a1157835SDaniel Fojt merge_byte_arrays(bin, 3, NULL, 0, addr, ETH_ALEN) != 3)
384*a1157835SDaniel Fojt errors++;
385*a1157835SDaniel Fojt
386*a1157835SDaniel Fojt if (dup_binstr(NULL, 0) != NULL)
387*a1157835SDaniel Fojt errors++;
388*a1157835SDaniel Fojt
389*a1157835SDaniel Fojt if (freq_range_list_includes(NULL, 0) != 0)
390*a1157835SDaniel Fojt errors++;
391*a1157835SDaniel Fojt
392*a1157835SDaniel Fojt os_memset(&ranges, 0, sizeof(ranges));
393*a1157835SDaniel Fojt if (freq_range_list_parse(&ranges, "") != 0 ||
394*a1157835SDaniel Fojt freq_range_list_includes(&ranges, 0) != 0 ||
395*a1157835SDaniel Fojt freq_range_list_str(&ranges) != NULL)
396*a1157835SDaniel Fojt errors++;
397*a1157835SDaniel Fojt
398*a1157835SDaniel Fojt if (utf8_unescape(NULL, 0, buf, sizeof(buf)) != 0 ||
399*a1157835SDaniel Fojt utf8_unescape("a", 1, NULL, 0) != 0 ||
400*a1157835SDaniel Fojt utf8_unescape("a\\", 2, buf, sizeof(buf)) != 0 ||
401*a1157835SDaniel Fojt utf8_unescape("abcde", 5, buf, sizeof(buf)) != 0 ||
402*a1157835SDaniel Fojt utf8_unescape("abc", 3, buf, 3) != 3)
403*a1157835SDaniel Fojt errors++;
404*a1157835SDaniel Fojt
405*a1157835SDaniel Fojt if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
406*a1157835SDaniel Fojt errors++;
407*a1157835SDaniel Fojt
408*a1157835SDaniel Fojt if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
409*a1157835SDaniel Fojt errors++;
410*a1157835SDaniel Fojt
411*a1157835SDaniel Fojt if (utf8_escape(NULL, 0, buf, sizeof(buf)) != 0 ||
412*a1157835SDaniel Fojt utf8_escape("a", 1, NULL, 0) != 0 ||
413*a1157835SDaniel Fojt utf8_escape("abcde", 5, buf, sizeof(buf)) != 0 ||
414*a1157835SDaniel Fojt utf8_escape("a\\bcde", 6, buf, sizeof(buf)) != 0 ||
415*a1157835SDaniel Fojt utf8_escape("ab\\cde", 6, buf, sizeof(buf)) != 0 ||
416*a1157835SDaniel Fojt utf8_escape("abc\\de", 6, buf, sizeof(buf)) != 0 ||
417*a1157835SDaniel Fojt utf8_escape("abc", 3, buf, 3) != 3)
418*a1157835SDaniel Fojt errors++;
419*a1157835SDaniel Fojt
420*a1157835SDaniel Fojt if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
421*a1157835SDaniel Fojt errors++;
422*a1157835SDaniel Fojt
423*a1157835SDaniel Fojt os_memset(ssid, 0, sizeof(ssid));
424*a1157835SDaniel Fojt txt = wpa_ssid_txt(ssid, sizeof(ssid));
425*a1157835SDaniel Fojt len = os_strlen(txt);
426*a1157835SDaniel Fojt /* Verify that SSID_MAX_LEN * 4 buffer limit is enforced. */
427*a1157835SDaniel Fojt if (len != SSID_MAX_LEN * 4) {
428*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
429*a1157835SDaniel Fojt "Unexpected wpa_ssid_txt() result with too long SSID");
430*a1157835SDaniel Fojt errors++;
431*a1157835SDaniel Fojt }
432*a1157835SDaniel Fojt
433*a1157835SDaniel Fojt if (wpa_snprintf_hex_sep(longbuf, 0, addr, ETH_ALEN, '-') != 0 ||
434*a1157835SDaniel Fojt wpa_snprintf_hex_sep(longbuf, 5, addr, ETH_ALEN, '-') != 3 ||
435*a1157835SDaniel Fojt os_strcmp(longbuf, "01-0") != 0)
436*a1157835SDaniel Fojt errors++;
437*a1157835SDaniel Fojt
438*a1157835SDaniel Fojt if (errors) {
439*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
440*a1157835SDaniel Fojt return -1;
441*a1157835SDaniel Fojt }
442*a1157835SDaniel Fojt
443*a1157835SDaniel Fojt return 0;
444*a1157835SDaniel Fojt }
445*a1157835SDaniel Fojt
446*a1157835SDaniel Fojt
os_tests(void)447*a1157835SDaniel Fojt static int os_tests(void)
448*a1157835SDaniel Fojt {
449*a1157835SDaniel Fojt int errors = 0;
450*a1157835SDaniel Fojt void *ptr;
451*a1157835SDaniel Fojt os_time_t t;
452*a1157835SDaniel Fojt
453*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "os tests");
454*a1157835SDaniel Fojt
455*a1157835SDaniel Fojt ptr = os_calloc((size_t) -1, (size_t) -1);
456*a1157835SDaniel Fojt if (ptr) {
457*a1157835SDaniel Fojt errors++;
458*a1157835SDaniel Fojt os_free(ptr);
459*a1157835SDaniel Fojt }
460*a1157835SDaniel Fojt ptr = os_calloc((size_t) 2, (size_t) -1);
461*a1157835SDaniel Fojt if (ptr) {
462*a1157835SDaniel Fojt errors++;
463*a1157835SDaniel Fojt os_free(ptr);
464*a1157835SDaniel Fojt }
465*a1157835SDaniel Fojt ptr = os_calloc((size_t) -1, (size_t) 2);
466*a1157835SDaniel Fojt if (ptr) {
467*a1157835SDaniel Fojt errors++;
468*a1157835SDaniel Fojt os_free(ptr);
469*a1157835SDaniel Fojt }
470*a1157835SDaniel Fojt
471*a1157835SDaniel Fojt ptr = os_realloc_array(NULL, (size_t) -1, (size_t) -1);
472*a1157835SDaniel Fojt if (ptr) {
473*a1157835SDaniel Fojt errors++;
474*a1157835SDaniel Fojt os_free(ptr);
475*a1157835SDaniel Fojt }
476*a1157835SDaniel Fojt
477*a1157835SDaniel Fojt os_sleep(1, 1);
478*a1157835SDaniel Fojt
479*a1157835SDaniel Fojt if (os_mktime(1969, 1, 1, 1, 1, 1, &t) == 0 ||
480*a1157835SDaniel Fojt os_mktime(1971, 0, 1, 1, 1, 1, &t) == 0 ||
481*a1157835SDaniel Fojt os_mktime(1971, 13, 1, 1, 1, 1, &t) == 0 ||
482*a1157835SDaniel Fojt os_mktime(1971, 1, 0, 1, 1, 1, &t) == 0 ||
483*a1157835SDaniel Fojt os_mktime(1971, 1, 32, 1, 1, 1, &t) == 0 ||
484*a1157835SDaniel Fojt os_mktime(1971, 1, 1, -1, 1, 1, &t) == 0 ||
485*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 24, 1, 1, &t) == 0 ||
486*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 1, -1, 1, &t) == 0 ||
487*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 1, 60, 1, &t) == 0 ||
488*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 1, 1, -1, &t) == 0 ||
489*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 1, 1, 61, &t) == 0 ||
490*a1157835SDaniel Fojt os_mktime(1971, 1, 1, 1, 1, 1, &t) != 0 ||
491*a1157835SDaniel Fojt os_mktime(2020, 1, 2, 3, 4, 5, &t) != 0 ||
492*a1157835SDaniel Fojt os_mktime(2015, 12, 31, 23, 59, 59, &t) != 0)
493*a1157835SDaniel Fojt errors++;
494*a1157835SDaniel Fojt
495*a1157835SDaniel Fojt if (os_setenv("hwsim_test_env", "test value", 0) != 0 ||
496*a1157835SDaniel Fojt os_setenv("hwsim_test_env", "test value 2", 1) != 0 ||
497*a1157835SDaniel Fojt os_unsetenv("hwsim_test_env") != 0)
498*a1157835SDaniel Fojt errors++;
499*a1157835SDaniel Fojt
500*a1157835SDaniel Fojt if (os_file_exists("/this-file-does-not-exists-hwsim") != 0)
501*a1157835SDaniel Fojt errors++;
502*a1157835SDaniel Fojt
503*a1157835SDaniel Fojt if (errors) {
504*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d os test(s) failed", errors);
505*a1157835SDaniel Fojt return -1;
506*a1157835SDaniel Fojt }
507*a1157835SDaniel Fojt
508*a1157835SDaniel Fojt return 0;
509*a1157835SDaniel Fojt }
510*a1157835SDaniel Fojt
511*a1157835SDaniel Fojt
wpabuf_tests(void)512*a1157835SDaniel Fojt static int wpabuf_tests(void)
513*a1157835SDaniel Fojt {
514*a1157835SDaniel Fojt int errors = 0;
515*a1157835SDaniel Fojt void *ptr;
516*a1157835SDaniel Fojt struct wpabuf *buf;
517*a1157835SDaniel Fojt
518*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "wpabuf tests");
519*a1157835SDaniel Fojt
520*a1157835SDaniel Fojt ptr = os_malloc(100);
521*a1157835SDaniel Fojt if (ptr) {
522*a1157835SDaniel Fojt buf = wpabuf_alloc_ext_data(ptr, 100);
523*a1157835SDaniel Fojt if (buf) {
524*a1157835SDaniel Fojt if (wpabuf_resize(&buf, 100) < 0)
525*a1157835SDaniel Fojt errors++;
526*a1157835SDaniel Fojt else
527*a1157835SDaniel Fojt wpabuf_put(buf, 100);
528*a1157835SDaniel Fojt wpabuf_free(buf);
529*a1157835SDaniel Fojt } else {
530*a1157835SDaniel Fojt errors++;
531*a1157835SDaniel Fojt os_free(ptr);
532*a1157835SDaniel Fojt }
533*a1157835SDaniel Fojt } else {
534*a1157835SDaniel Fojt errors++;
535*a1157835SDaniel Fojt }
536*a1157835SDaniel Fojt
537*a1157835SDaniel Fojt buf = wpabuf_alloc(100);
538*a1157835SDaniel Fojt if (buf) {
539*a1157835SDaniel Fojt struct wpabuf *buf2;
540*a1157835SDaniel Fojt
541*a1157835SDaniel Fojt wpabuf_put(buf, 100);
542*a1157835SDaniel Fojt if (wpabuf_resize(&buf, 100) < 0)
543*a1157835SDaniel Fojt errors++;
544*a1157835SDaniel Fojt else
545*a1157835SDaniel Fojt wpabuf_put(buf, 100);
546*a1157835SDaniel Fojt buf2 = wpabuf_concat(buf, NULL);
547*a1157835SDaniel Fojt if (buf2 != buf)
548*a1157835SDaniel Fojt errors++;
549*a1157835SDaniel Fojt wpabuf_free(buf2);
550*a1157835SDaniel Fojt } else {
551*a1157835SDaniel Fojt errors++;
552*a1157835SDaniel Fojt }
553*a1157835SDaniel Fojt
554*a1157835SDaniel Fojt buf = NULL;
555*a1157835SDaniel Fojt buf = wpabuf_zeropad(buf, 10);
556*a1157835SDaniel Fojt if (buf != NULL)
557*a1157835SDaniel Fojt errors++;
558*a1157835SDaniel Fojt
559*a1157835SDaniel Fojt if (errors) {
560*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d wpabuf test(s) failed", errors);
561*a1157835SDaniel Fojt return -1;
562*a1157835SDaniel Fojt }
563*a1157835SDaniel Fojt
564*a1157835SDaniel Fojt return 0;
565*a1157835SDaniel Fojt }
566*a1157835SDaniel Fojt
567*a1157835SDaniel Fojt
ip_addr_tests(void)568*a1157835SDaniel Fojt static int ip_addr_tests(void)
569*a1157835SDaniel Fojt {
570*a1157835SDaniel Fojt int errors = 0;
571*a1157835SDaniel Fojt struct hostapd_ip_addr addr;
572*a1157835SDaniel Fojt char buf[100];
573*a1157835SDaniel Fojt
574*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "ip_addr tests");
575*a1157835SDaniel Fojt
576*a1157835SDaniel Fojt if (hostapd_parse_ip_addr("1.2.3.4", &addr) != 0 ||
577*a1157835SDaniel Fojt addr.af != AF_INET ||
578*a1157835SDaniel Fojt hostapd_ip_txt(NULL, buf, sizeof(buf)) != NULL ||
579*a1157835SDaniel Fojt hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
580*a1157835SDaniel Fojt hostapd_ip_txt(&addr, buf, 0) != NULL ||
581*a1157835SDaniel Fojt hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
582*a1157835SDaniel Fojt errors++;
583*a1157835SDaniel Fojt
584*a1157835SDaniel Fojt if (hostapd_parse_ip_addr("::", &addr) != 0 ||
585*a1157835SDaniel Fojt addr.af != AF_INET6 ||
586*a1157835SDaniel Fojt hostapd_ip_txt(&addr, buf, 1) != buf || buf[0] != '\0' ||
587*a1157835SDaniel Fojt hostapd_ip_txt(&addr, buf, sizeof(buf)) != buf)
588*a1157835SDaniel Fojt errors++;
589*a1157835SDaniel Fojt
590*a1157835SDaniel Fojt if (errors) {
591*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%d ip_addr test(s) failed", errors);
592*a1157835SDaniel Fojt return -1;
593*a1157835SDaniel Fojt }
594*a1157835SDaniel Fojt
595*a1157835SDaniel Fojt return 0;
596*a1157835SDaniel Fojt }
597*a1157835SDaniel Fojt
598*a1157835SDaniel Fojt
599*a1157835SDaniel Fojt struct test_eloop {
600*a1157835SDaniel Fojt unsigned int magic;
601*a1157835SDaniel Fojt int close_in_timeout;
602*a1157835SDaniel Fojt int pipefd1[2];
603*a1157835SDaniel Fojt int pipefd2[2];
604*a1157835SDaniel Fojt };
605*a1157835SDaniel Fojt
606*a1157835SDaniel Fojt
607*a1157835SDaniel Fojt static void eloop_tests_start(int close_in_timeout);
608*a1157835SDaniel Fojt
609*a1157835SDaniel Fojt
eloop_test_read_2(int sock,void * eloop_ctx,void * sock_ctx)610*a1157835SDaniel Fojt static void eloop_test_read_2(int sock, void *eloop_ctx, void *sock_ctx)
611*a1157835SDaniel Fojt {
612*a1157835SDaniel Fojt struct test_eloop *t = eloop_ctx;
613*a1157835SDaniel Fojt ssize_t res;
614*a1157835SDaniel Fojt char buf[10];
615*a1157835SDaniel Fojt
616*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
617*a1157835SDaniel Fojt
618*a1157835SDaniel Fojt if (t->magic != 0x12345678) {
619*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
620*a1157835SDaniel Fojt __func__, t->magic);
621*a1157835SDaniel Fojt }
622*a1157835SDaniel Fojt
623*a1157835SDaniel Fojt if (t->pipefd2[0] != sock) {
624*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
625*a1157835SDaniel Fojt __func__, sock, t->pipefd2[0]);
626*a1157835SDaniel Fojt }
627*a1157835SDaniel Fojt
628*a1157835SDaniel Fojt res = read(sock, buf, sizeof(buf));
629*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
630*a1157835SDaniel Fojt __func__, sock, (int) res);
631*a1157835SDaniel Fojt }
632*a1157835SDaniel Fojt
633*a1157835SDaniel Fojt
eloop_test_read_2_wrong(int sock,void * eloop_ctx,void * sock_ctx)634*a1157835SDaniel Fojt static void eloop_test_read_2_wrong(int sock, void *eloop_ctx, void *sock_ctx)
635*a1157835SDaniel Fojt {
636*a1157835SDaniel Fojt struct test_eloop *t = eloop_ctx;
637*a1157835SDaniel Fojt
638*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
639*a1157835SDaniel Fojt
640*a1157835SDaniel Fojt if (t->magic != 0x12345678) {
641*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
642*a1157835SDaniel Fojt __func__, t->magic);
643*a1157835SDaniel Fojt }
644*a1157835SDaniel Fojt
645*a1157835SDaniel Fojt if (t->pipefd2[0] != sock) {
646*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
647*a1157835SDaniel Fojt __func__, sock, t->pipefd2[0]);
648*a1157835SDaniel Fojt }
649*a1157835SDaniel Fojt
650*a1157835SDaniel Fojt /*
651*a1157835SDaniel Fojt * This is expected to block due to the original socket with data having
652*a1157835SDaniel Fojt * been closed and no new data having been written to the new socket
653*a1157835SDaniel Fojt * with the same fd. To avoid blocking the process during test, skip the
654*a1157835SDaniel Fojt * read here.
655*a1157835SDaniel Fojt */
656*a1157835SDaniel Fojt wpa_printf(MSG_ERROR, "%s: FAIL - should not have called this function",
657*a1157835SDaniel Fojt __func__);
658*a1157835SDaniel Fojt }
659*a1157835SDaniel Fojt
660*a1157835SDaniel Fojt
reopen_pipefd2(struct test_eloop * t)661*a1157835SDaniel Fojt static void reopen_pipefd2(struct test_eloop *t)
662*a1157835SDaniel Fojt {
663*a1157835SDaniel Fojt if (t->pipefd2[0] < 0) {
664*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipefd2 had been closed");
665*a1157835SDaniel Fojt } else {
666*a1157835SDaniel Fojt int res;
667*a1157835SDaniel Fojt
668*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "close pipefd2");
669*a1157835SDaniel Fojt eloop_unregister_read_sock(t->pipefd2[0]);
670*a1157835SDaniel Fojt close(t->pipefd2[0]);
671*a1157835SDaniel Fojt t->pipefd2[0] = -1;
672*a1157835SDaniel Fojt close(t->pipefd2[1]);
673*a1157835SDaniel Fojt t->pipefd2[1] = -1;
674*a1157835SDaniel Fojt
675*a1157835SDaniel Fojt res = pipe(t->pipefd2);
676*a1157835SDaniel Fojt if (res < 0) {
677*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
678*a1157835SDaniel Fojt t->pipefd2[0] = -1;
679*a1157835SDaniel Fojt t->pipefd2[1] = -1;
680*a1157835SDaniel Fojt return;
681*a1157835SDaniel Fojt }
682*a1157835SDaniel Fojt
683*a1157835SDaniel Fojt wpa_printf(MSG_INFO,
684*a1157835SDaniel Fojt "re-register pipefd2 with new sockets %d,%d",
685*a1157835SDaniel Fojt t->pipefd2[0], t->pipefd2[1]);
686*a1157835SDaniel Fojt eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2_wrong,
687*a1157835SDaniel Fojt t, NULL);
688*a1157835SDaniel Fojt }
689*a1157835SDaniel Fojt }
690*a1157835SDaniel Fojt
691*a1157835SDaniel Fojt
eloop_test_read_1(int sock,void * eloop_ctx,void * sock_ctx)692*a1157835SDaniel Fojt static void eloop_test_read_1(int sock, void *eloop_ctx, void *sock_ctx)
693*a1157835SDaniel Fojt {
694*a1157835SDaniel Fojt struct test_eloop *t = eloop_ctx;
695*a1157835SDaniel Fojt ssize_t res;
696*a1157835SDaniel Fojt char buf[10];
697*a1157835SDaniel Fojt
698*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: sock=%d", __func__, sock);
699*a1157835SDaniel Fojt
700*a1157835SDaniel Fojt if (t->magic != 0x12345678) {
701*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
702*a1157835SDaniel Fojt __func__, t->magic);
703*a1157835SDaniel Fojt }
704*a1157835SDaniel Fojt
705*a1157835SDaniel Fojt if (t->pipefd1[0] != sock) {
706*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected sock %d != %d",
707*a1157835SDaniel Fojt __func__, sock, t->pipefd1[0]);
708*a1157835SDaniel Fojt }
709*a1157835SDaniel Fojt
710*a1157835SDaniel Fojt res = read(sock, buf, sizeof(buf));
711*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: sock=%d --> res=%d",
712*a1157835SDaniel Fojt __func__, sock, (int) res);
713*a1157835SDaniel Fojt
714*a1157835SDaniel Fojt if (!t->close_in_timeout)
715*a1157835SDaniel Fojt reopen_pipefd2(t);
716*a1157835SDaniel Fojt }
717*a1157835SDaniel Fojt
718*a1157835SDaniel Fojt
eloop_test_cb(void * eloop_data,void * user_ctx)719*a1157835SDaniel Fojt static void eloop_test_cb(void *eloop_data, void *user_ctx)
720*a1157835SDaniel Fojt {
721*a1157835SDaniel Fojt struct test_eloop *t = eloop_data;
722*a1157835SDaniel Fojt
723*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s", __func__);
724*a1157835SDaniel Fojt
725*a1157835SDaniel Fojt if (t->magic != 0x12345678) {
726*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
727*a1157835SDaniel Fojt __func__, t->magic);
728*a1157835SDaniel Fojt }
729*a1157835SDaniel Fojt
730*a1157835SDaniel Fojt if (t->close_in_timeout)
731*a1157835SDaniel Fojt reopen_pipefd2(t);
732*a1157835SDaniel Fojt }
733*a1157835SDaniel Fojt
734*a1157835SDaniel Fojt
eloop_test_timeout(void * eloop_data,void * user_ctx)735*a1157835SDaniel Fojt static void eloop_test_timeout(void *eloop_data, void *user_ctx)
736*a1157835SDaniel Fojt {
737*a1157835SDaniel Fojt struct test_eloop *t = eloop_data;
738*a1157835SDaniel Fojt int next_run = 0;
739*a1157835SDaniel Fojt
740*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s", __func__);
741*a1157835SDaniel Fojt
742*a1157835SDaniel Fojt if (t->magic != 0x12345678) {
743*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s: unexpected magic 0x%x",
744*a1157835SDaniel Fojt __func__, t->magic);
745*a1157835SDaniel Fojt }
746*a1157835SDaniel Fojt
747*a1157835SDaniel Fojt if (t->pipefd1[0] >= 0) {
748*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipefd1 had not been closed");
749*a1157835SDaniel Fojt eloop_unregister_read_sock(t->pipefd1[0]);
750*a1157835SDaniel Fojt close(t->pipefd1[0]);
751*a1157835SDaniel Fojt t->pipefd1[0] = -1;
752*a1157835SDaniel Fojt close(t->pipefd1[1]);
753*a1157835SDaniel Fojt t->pipefd1[1] = -1;
754*a1157835SDaniel Fojt }
755*a1157835SDaniel Fojt
756*a1157835SDaniel Fojt if (t->pipefd2[0] >= 0) {
757*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipefd2 had not been closed");
758*a1157835SDaniel Fojt eloop_unregister_read_sock(t->pipefd2[0]);
759*a1157835SDaniel Fojt close(t->pipefd2[0]);
760*a1157835SDaniel Fojt t->pipefd2[0] = -1;
761*a1157835SDaniel Fojt close(t->pipefd2[1]);
762*a1157835SDaniel Fojt t->pipefd2[1] = -1;
763*a1157835SDaniel Fojt }
764*a1157835SDaniel Fojt
765*a1157835SDaniel Fojt next_run = t->close_in_timeout;
766*a1157835SDaniel Fojt t->magic = 0;
767*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "%s - free(%p)", __func__, t);
768*a1157835SDaniel Fojt os_free(t);
769*a1157835SDaniel Fojt
770*a1157835SDaniel Fojt if (next_run)
771*a1157835SDaniel Fojt eloop_tests_start(0);
772*a1157835SDaniel Fojt }
773*a1157835SDaniel Fojt
774*a1157835SDaniel Fojt
eloop_tests_start(int close_in_timeout)775*a1157835SDaniel Fojt static void eloop_tests_start(int close_in_timeout)
776*a1157835SDaniel Fojt {
777*a1157835SDaniel Fojt struct test_eloop *t;
778*a1157835SDaniel Fojt int res;
779*a1157835SDaniel Fojt
780*a1157835SDaniel Fojt t = os_zalloc(sizeof(*t));
781*a1157835SDaniel Fojt if (!t)
782*a1157835SDaniel Fojt return;
783*a1157835SDaniel Fojt t->magic = 0x12345678;
784*a1157835SDaniel Fojt t->close_in_timeout = close_in_timeout;
785*a1157835SDaniel Fojt
786*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "starting eloop tests (%p) (close_in_timeout=%d)",
787*a1157835SDaniel Fojt t, close_in_timeout);
788*a1157835SDaniel Fojt
789*a1157835SDaniel Fojt res = pipe(t->pipefd1);
790*a1157835SDaniel Fojt if (res < 0) {
791*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
792*a1157835SDaniel Fojt os_free(t);
793*a1157835SDaniel Fojt return;
794*a1157835SDaniel Fojt }
795*a1157835SDaniel Fojt
796*a1157835SDaniel Fojt res = pipe(t->pipefd2);
797*a1157835SDaniel Fojt if (res < 0) {
798*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipe: %s", strerror(errno));
799*a1157835SDaniel Fojt close(t->pipefd1[0]);
800*a1157835SDaniel Fojt close(t->pipefd1[1]);
801*a1157835SDaniel Fojt os_free(t);
802*a1157835SDaniel Fojt return;
803*a1157835SDaniel Fojt }
804*a1157835SDaniel Fojt
805*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "pipe fds: %d,%d %d,%d",
806*a1157835SDaniel Fojt t->pipefd1[0], t->pipefd1[1],
807*a1157835SDaniel Fojt t->pipefd2[0], t->pipefd2[1]);
808*a1157835SDaniel Fojt
809*a1157835SDaniel Fojt eloop_register_read_sock(t->pipefd1[0], eloop_test_read_1, t, NULL);
810*a1157835SDaniel Fojt eloop_register_read_sock(t->pipefd2[0], eloop_test_read_2, t, NULL);
811*a1157835SDaniel Fojt eloop_register_timeout(0, 0, eloop_test_cb, t, NULL);
812*a1157835SDaniel Fojt eloop_register_timeout(0, 200000, eloop_test_timeout, t, NULL);
813*a1157835SDaniel Fojt
814*a1157835SDaniel Fojt if (write(t->pipefd1[1], "HELLO", 5) < 0)
815*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "write: %s", strerror(errno));
816*a1157835SDaniel Fojt if (write(t->pipefd2[1], "TEST", 4) < 0)
817*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "write: %s", strerror(errno));
818*a1157835SDaniel Fojt os_sleep(0, 50000);
819*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "waiting for eloop callbacks");
820*a1157835SDaniel Fojt }
821*a1157835SDaniel Fojt
822*a1157835SDaniel Fojt
eloop_tests_run(void * eloop_data,void * user_ctx)823*a1157835SDaniel Fojt static void eloop_tests_run(void *eloop_data, void *user_ctx)
824*a1157835SDaniel Fojt {
825*a1157835SDaniel Fojt eloop_tests_start(1);
826*a1157835SDaniel Fojt }
827*a1157835SDaniel Fojt
828*a1157835SDaniel Fojt
eloop_tests(void)829*a1157835SDaniel Fojt static int eloop_tests(void)
830*a1157835SDaniel Fojt {
831*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "schedule eloop tests to be run");
832*a1157835SDaniel Fojt
833*a1157835SDaniel Fojt /*
834*a1157835SDaniel Fojt * Cannot return error from these without a significant design change,
835*a1157835SDaniel Fojt * so for now, run the tests from a scheduled timeout and require
836*a1157835SDaniel Fojt * separate verification of the results from the debug log.
837*a1157835SDaniel Fojt */
838*a1157835SDaniel Fojt eloop_register_timeout(0, 0, eloop_tests_run, NULL, NULL);
839*a1157835SDaniel Fojt
840*a1157835SDaniel Fojt return 0;
841*a1157835SDaniel Fojt }
842*a1157835SDaniel Fojt
843*a1157835SDaniel Fojt
844*a1157835SDaniel Fojt #ifdef CONFIG_JSON
845*a1157835SDaniel Fojt struct json_test_data {
846*a1157835SDaniel Fojt const char *json;
847*a1157835SDaniel Fojt const char *tree;
848*a1157835SDaniel Fojt };
849*a1157835SDaniel Fojt
850*a1157835SDaniel Fojt static const struct json_test_data json_test_cases[] = {
851*a1157835SDaniel Fojt { "{}", "[1:OBJECT:]" },
852*a1157835SDaniel Fojt { "[]", "[1:ARRAY:]" },
853*a1157835SDaniel Fojt { "{", NULL },
854*a1157835SDaniel Fojt { "[", NULL },
855*a1157835SDaniel Fojt { "}", NULL },
856*a1157835SDaniel Fojt { "]", NULL },
857*a1157835SDaniel Fojt { "[[]]", "[1:ARRAY:][2:ARRAY:]" },
858*a1157835SDaniel Fojt { "{\"t\":\"test\"}", "[1:OBJECT:][2:STRING:t]" },
859*a1157835SDaniel Fojt { "{\"t\":123}", "[1:OBJECT:][2:NUMBER:t]" },
860*a1157835SDaniel Fojt { "{\"t\":true}", "[1:OBJECT:][2:BOOLEAN:t]" },
861*a1157835SDaniel Fojt { "{\"t\":false}", "[1:OBJECT:][2:BOOLEAN:t]" },
862*a1157835SDaniel Fojt { "{\"t\":null}", "[1:OBJECT:][2:NULL:t]" },
863*a1157835SDaniel Fojt { "{\"t\":truetrue}", NULL },
864*a1157835SDaniel Fojt { "\"test\"", "[1:STRING:]" },
865*a1157835SDaniel Fojt { "123", "[1:NUMBER:]" },
866*a1157835SDaniel Fojt { "true", "[1:BOOLEAN:]" },
867*a1157835SDaniel Fojt { "false", "[1:BOOLEAN:]" },
868*a1157835SDaniel Fojt { "null", "[1:NULL:]" },
869*a1157835SDaniel Fojt { "truetrue", NULL },
870*a1157835SDaniel Fojt { " {\t\n\r\"a\"\n:\r1\n,\n\"b\":3\n}\n",
871*a1157835SDaniel Fojt "[1:OBJECT:][2:NUMBER:a][2:NUMBER:b]" },
872*a1157835SDaniel Fojt { ",", NULL },
873*a1157835SDaniel Fojt { "{,}", NULL },
874*a1157835SDaniel Fojt { "[,]", NULL },
875*a1157835SDaniel Fojt { ":", NULL },
876*a1157835SDaniel Fojt { "{:}", NULL },
877*a1157835SDaniel Fojt { "[:]", NULL },
878*a1157835SDaniel Fojt { "{ \"\\u005c\" : \"\\u005c\" }", "[1:OBJECT:][2:STRING:\\]" },
879*a1157835SDaniel Fojt { "[{},{}]", "[1:ARRAY:][2:OBJECT:][2:OBJECT:]" },
880*a1157835SDaniel Fojt { "[1,2]", "[1:ARRAY:][2:NUMBER:][2:NUMBER:]" },
881*a1157835SDaniel Fojt { "[\"1\",\"2\"]", "[1:ARRAY:][2:STRING:][2:STRING:]" },
882*a1157835SDaniel Fojt { "[true,false]", "[1:ARRAY:][2:BOOLEAN:][2:BOOLEAN:]" },
883*a1157835SDaniel Fojt };
884*a1157835SDaniel Fojt #endif /* CONFIG_JSON */
885*a1157835SDaniel Fojt
886*a1157835SDaniel Fojt
json_tests(void)887*a1157835SDaniel Fojt static int json_tests(void)
888*a1157835SDaniel Fojt {
889*a1157835SDaniel Fojt #ifdef CONFIG_JSON
890*a1157835SDaniel Fojt unsigned int i;
891*a1157835SDaniel Fojt struct json_token *root;
892*a1157835SDaniel Fojt char buf[1000];
893*a1157835SDaniel Fojt
894*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "JSON tests");
895*a1157835SDaniel Fojt
896*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(json_test_cases); i++) {
897*a1157835SDaniel Fojt const struct json_test_data *test = &json_test_cases[i];
898*a1157835SDaniel Fojt int res = 0;
899*a1157835SDaniel Fojt
900*a1157835SDaniel Fojt root = json_parse(test->json, os_strlen(test->json));
901*a1157835SDaniel Fojt if ((root && !test->tree) || (!root && test->tree)) {
902*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "JSON test %u failed", i);
903*a1157835SDaniel Fojt res = -1;
904*a1157835SDaniel Fojt } else if (root) {
905*a1157835SDaniel Fojt json_print_tree(root, buf, sizeof(buf));
906*a1157835SDaniel Fojt if (os_strcmp(buf, test->tree) != 0) {
907*a1157835SDaniel Fojt wpa_printf(MSG_INFO,
908*a1157835SDaniel Fojt "JSON test %u tree mismatch: %s %s",
909*a1157835SDaniel Fojt i, buf, test->tree);
910*a1157835SDaniel Fojt res = -1;
911*a1157835SDaniel Fojt }
912*a1157835SDaniel Fojt }
913*a1157835SDaniel Fojt json_free(root);
914*a1157835SDaniel Fojt if (res < 0)
915*a1157835SDaniel Fojt return -1;
916*a1157835SDaniel Fojt
917*a1157835SDaniel Fojt }
918*a1157835SDaniel Fojt #endif /* CONFIG_JSON */
919*a1157835SDaniel Fojt return 0;
920*a1157835SDaniel Fojt }
921*a1157835SDaniel Fojt
922*a1157835SDaniel Fojt
const_time_tests(void)923*a1157835SDaniel Fojt static int const_time_tests(void)
924*a1157835SDaniel Fojt {
925*a1157835SDaniel Fojt struct const_time_fill_msb_test {
926*a1157835SDaniel Fojt unsigned int val;
927*a1157835SDaniel Fojt unsigned int expected;
928*a1157835SDaniel Fojt } const_time_fill_msb_tests[] = {
929*a1157835SDaniel Fojt { 0, 0 },
930*a1157835SDaniel Fojt { 1, 0 },
931*a1157835SDaniel Fojt { 2, 0 },
932*a1157835SDaniel Fojt { 1 << (sizeof(unsigned int) * 8 - 1), ~0 },
933*a1157835SDaniel Fojt { ~0 - 1, ~0 },
934*a1157835SDaniel Fojt { ~0, ~0 }
935*a1157835SDaniel Fojt };
936*a1157835SDaniel Fojt struct const_time_is_zero_test {
937*a1157835SDaniel Fojt unsigned int val;
938*a1157835SDaniel Fojt unsigned int expected;
939*a1157835SDaniel Fojt } const_time_is_zero_tests[] = {
940*a1157835SDaniel Fojt { 0, ~0 },
941*a1157835SDaniel Fojt { 1, 0 },
942*a1157835SDaniel Fojt { 2, 0 },
943*a1157835SDaniel Fojt { 1 << (sizeof(unsigned int) * 8 - 1), 0 },
944*a1157835SDaniel Fojt { ~0 - 1, 0 },
945*a1157835SDaniel Fojt { ~0, 0 }
946*a1157835SDaniel Fojt };
947*a1157835SDaniel Fojt struct const_time_eq_test {
948*a1157835SDaniel Fojt unsigned int a;
949*a1157835SDaniel Fojt unsigned int b;
950*a1157835SDaniel Fojt unsigned int expected;
951*a1157835SDaniel Fojt unsigned int expected_u8;
952*a1157835SDaniel Fojt } const_time_eq_tests[] = {
953*a1157835SDaniel Fojt { 0, 1, 0, 0 },
954*a1157835SDaniel Fojt { 1, 2, 0, 0 },
955*a1157835SDaniel Fojt { 1, 1, ~0, 0xff },
956*a1157835SDaniel Fojt { ~0, ~0, ~0, 0xff },
957*a1157835SDaniel Fojt { ~0, ~0 - 1, 0, 0 },
958*a1157835SDaniel Fojt { 0, 0, ~0, 0xff }
959*a1157835SDaniel Fojt };
960*a1157835SDaniel Fojt struct const_time_eq_bin_test {
961*a1157835SDaniel Fojt u8 *a;
962*a1157835SDaniel Fojt u8 *b;
963*a1157835SDaniel Fojt size_t len;
964*a1157835SDaniel Fojt unsigned int expected;
965*a1157835SDaniel Fojt } const_time_eq_bin_tests[] = {
966*a1157835SDaniel Fojt { (u8 *) "", (u8 *) "", 0, ~0 },
967*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "abcde", 5, ~0 },
968*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "Abcde", 5, 0 },
969*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "aBcde", 5, 0 },
970*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "abCde", 5, 0 },
971*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "abcDe", 5, 0 },
972*a1157835SDaniel Fojt { (u8 *) "abcde", (u8 *) "abcdE", 5, 0 },
973*a1157835SDaniel Fojt { (u8 *) "\x00", (u8 *) "\x01", 1, 0 },
974*a1157835SDaniel Fojt { (u8 *) "\x00", (u8 *) "\x80", 1, 0 },
975*a1157835SDaniel Fojt { (u8 *) "\x00", (u8 *) "\x00", 1, ~0 }
976*a1157835SDaniel Fojt };
977*a1157835SDaniel Fojt struct const_time_select_test {
978*a1157835SDaniel Fojt unsigned int mask;
979*a1157835SDaniel Fojt unsigned int true_val;
980*a1157835SDaniel Fojt unsigned int false_val;
981*a1157835SDaniel Fojt unsigned int expected;
982*a1157835SDaniel Fojt } const_time_select_tests[] = {
983*a1157835SDaniel Fojt { ~0, ~0, ~0, ~0 },
984*a1157835SDaniel Fojt { 0, ~0, ~0, ~0 },
985*a1157835SDaniel Fojt { ~0, ~0, 0, ~0 },
986*a1157835SDaniel Fojt { 0, ~0, 0, 0 },
987*a1157835SDaniel Fojt { ~0, 0xaaaaaaaa, 0x55555555, 0xaaaaaaaa },
988*a1157835SDaniel Fojt { 0, 0xaaaaaaaa, 0x55555555, 0x55555555 },
989*a1157835SDaniel Fojt { ~0, 3, 3, 3 },
990*a1157835SDaniel Fojt { 0, 3, 3, 3 },
991*a1157835SDaniel Fojt { ~0, 1, 2, 1 },
992*a1157835SDaniel Fojt { 0, 1, 2, 2 }
993*a1157835SDaniel Fojt };
994*a1157835SDaniel Fojt struct const_time_select_int_test {
995*a1157835SDaniel Fojt unsigned int mask;
996*a1157835SDaniel Fojt int true_val;
997*a1157835SDaniel Fojt int false_val;
998*a1157835SDaniel Fojt int expected;
999*a1157835SDaniel Fojt } const_time_select_int_tests[] = {
1000*a1157835SDaniel Fojt { ~0, -128, 127, -128 },
1001*a1157835SDaniel Fojt { 0, -128, 127, 127 },
1002*a1157835SDaniel Fojt { ~0, -2147483648, 2147483647, -2147483648 },
1003*a1157835SDaniel Fojt { 0, -2147483648, 2147483647, 2147483647 },
1004*a1157835SDaniel Fojt { ~0, 0, 0, 0 },
1005*a1157835SDaniel Fojt { 0, 0, 0, 0 },
1006*a1157835SDaniel Fojt { ~0, -1, 1, -1 },
1007*a1157835SDaniel Fojt { 0, -1, 1, 1 }
1008*a1157835SDaniel Fojt };
1009*a1157835SDaniel Fojt struct const_time_select_u8_test {
1010*a1157835SDaniel Fojt u8 mask;
1011*a1157835SDaniel Fojt u8 true_val;
1012*a1157835SDaniel Fojt u8 false_val;
1013*a1157835SDaniel Fojt u8 expected;
1014*a1157835SDaniel Fojt } const_time_select_u8_tests[] = {
1015*a1157835SDaniel Fojt { ~0, ~0, ~0, ~0 },
1016*a1157835SDaniel Fojt { 0, ~0, ~0, ~0 },
1017*a1157835SDaniel Fojt { ~0, ~0, 0, ~0 },
1018*a1157835SDaniel Fojt { 0, ~0, 0, 0 },
1019*a1157835SDaniel Fojt { ~0, 0xaa, 0x55, 0xaa },
1020*a1157835SDaniel Fojt { 0, 0xaa, 0x55, 0x55 },
1021*a1157835SDaniel Fojt { ~0, 1, 2, 1 },
1022*a1157835SDaniel Fojt { 0, 1, 2, 2 }
1023*a1157835SDaniel Fojt };
1024*a1157835SDaniel Fojt struct const_time_select_s8_test {
1025*a1157835SDaniel Fojt u8 mask;
1026*a1157835SDaniel Fojt s8 true_val;
1027*a1157835SDaniel Fojt s8 false_val;
1028*a1157835SDaniel Fojt s8 expected;
1029*a1157835SDaniel Fojt } const_time_select_s8_tests[] = {
1030*a1157835SDaniel Fojt { ~0, -128, 127, -128 },
1031*a1157835SDaniel Fojt { 0, -128, 127, 127 },
1032*a1157835SDaniel Fojt { ~0, 0, 0, 0 },
1033*a1157835SDaniel Fojt { 0, 0, 0, 0 },
1034*a1157835SDaniel Fojt { ~0, -1, 1, -1 },
1035*a1157835SDaniel Fojt { 0, -1, 1, 1 }
1036*a1157835SDaniel Fojt };
1037*a1157835SDaniel Fojt struct const_time_select_bin_test {
1038*a1157835SDaniel Fojt u8 mask;
1039*a1157835SDaniel Fojt u8 *true_val;
1040*a1157835SDaniel Fojt u8 *false_val;
1041*a1157835SDaniel Fojt size_t len;
1042*a1157835SDaniel Fojt u8 *expected;
1043*a1157835SDaniel Fojt } const_time_select_bin_tests[] = {
1044*a1157835SDaniel Fojt { ~0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "abcde" },
1045*a1157835SDaniel Fojt { 0, (u8 *) "abcde", (u8 *) "ABCDE", 5, (u8 *) "ABCDE" },
1046*a1157835SDaniel Fojt { ~0, (u8 *) "", (u8 *) "", 0, (u8 *) "" },
1047*a1157835SDaniel Fojt { 0, (u8 *) "", (u8 *) "", 0, (u8 *) "" }
1048*a1157835SDaniel Fojt };
1049*a1157835SDaniel Fojt struct const_time_memcmp_test {
1050*a1157835SDaniel Fojt char *a;
1051*a1157835SDaniel Fojt char *b;
1052*a1157835SDaniel Fojt size_t len;
1053*a1157835SDaniel Fojt int expected;
1054*a1157835SDaniel Fojt } const_time_memcmp_tests[] = {
1055*a1157835SDaniel Fojt { "abcde", "abcde", 5, 0 },
1056*a1157835SDaniel Fojt { "abcde", "bbcde", 5, -1 },
1057*a1157835SDaniel Fojt { "bbcde", "abcde", 5, 1 },
1058*a1157835SDaniel Fojt { "accde", "abcde", 5, 1 },
1059*a1157835SDaniel Fojt { "abcee", "abcde", 5, 1 },
1060*a1157835SDaniel Fojt { "abcdf", "abcde", 5, 1 },
1061*a1157835SDaniel Fojt { "cbcde", "aXXXX", 5, 2 },
1062*a1157835SDaniel Fojt { "a", "d", 1, -3 },
1063*a1157835SDaniel Fojt { "", "", 0, 0 }
1064*a1157835SDaniel Fojt };
1065*a1157835SDaniel Fojt unsigned int i;
1066*a1157835SDaniel Fojt int ret = 0;
1067*a1157835SDaniel Fojt
1068*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "constant time tests");
1069*a1157835SDaniel Fojt
1070*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_fill_msb_tests); i++) {
1071*a1157835SDaniel Fojt struct const_time_fill_msb_test *test;
1072*a1157835SDaniel Fojt
1073*a1157835SDaniel Fojt test = &const_time_fill_msb_tests[i];
1074*a1157835SDaniel Fojt if (const_time_fill_msb(test->val) != test->expected) {
1075*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1076*a1157835SDaniel Fojt "const_time_fill_msb(0x%x) test failed",
1077*a1157835SDaniel Fojt test->val);
1078*a1157835SDaniel Fojt ret = -1;
1079*a1157835SDaniel Fojt }
1080*a1157835SDaniel Fojt }
1081*a1157835SDaniel Fojt
1082*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_is_zero_tests); i++) {
1083*a1157835SDaniel Fojt struct const_time_is_zero_test *test;
1084*a1157835SDaniel Fojt
1085*a1157835SDaniel Fojt test = &const_time_is_zero_tests[i];
1086*a1157835SDaniel Fojt if (const_time_is_zero(test->val) != test->expected) {
1087*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1088*a1157835SDaniel Fojt "const_time_is_zero(0x%x) test failed",
1089*a1157835SDaniel Fojt test->val);
1090*a1157835SDaniel Fojt ret = -1;
1091*a1157835SDaniel Fojt }
1092*a1157835SDaniel Fojt }
1093*a1157835SDaniel Fojt
1094*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_eq_tests); i++) {
1095*a1157835SDaniel Fojt struct const_time_eq_test *test;
1096*a1157835SDaniel Fojt
1097*a1157835SDaniel Fojt test = &const_time_eq_tests[i];
1098*a1157835SDaniel Fojt if (const_time_eq(test->a, test->b) != test->expected) {
1099*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1100*a1157835SDaniel Fojt "const_time_eq(0x%x,0x%x) test failed",
1101*a1157835SDaniel Fojt test->a, test->b);
1102*a1157835SDaniel Fojt ret = -1;
1103*a1157835SDaniel Fojt }
1104*a1157835SDaniel Fojt if (const_time_eq_u8(test->a, test->b) != test->expected_u8) {
1105*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1106*a1157835SDaniel Fojt "const_time_eq_u8(0x%x,0x%x) test failed",
1107*a1157835SDaniel Fojt test->a, test->b);
1108*a1157835SDaniel Fojt ret = -1;
1109*a1157835SDaniel Fojt }
1110*a1157835SDaniel Fojt }
1111*a1157835SDaniel Fojt
1112*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_eq_bin_tests); i++) {
1113*a1157835SDaniel Fojt struct const_time_eq_bin_test *test;
1114*a1157835SDaniel Fojt
1115*a1157835SDaniel Fojt test = &const_time_eq_bin_tests[i];
1116*a1157835SDaniel Fojt if (const_time_eq_bin(test->a, test->b, test->len) !=
1117*a1157835SDaniel Fojt test->expected) {
1118*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1119*a1157835SDaniel Fojt "const_time_eq_bin(len=%u) test failed",
1120*a1157835SDaniel Fojt (unsigned int) test->len);
1121*a1157835SDaniel Fojt ret = -1;
1122*a1157835SDaniel Fojt }
1123*a1157835SDaniel Fojt }
1124*a1157835SDaniel Fojt
1125*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_select_tests); i++) {
1126*a1157835SDaniel Fojt struct const_time_select_test *test;
1127*a1157835SDaniel Fojt
1128*a1157835SDaniel Fojt test = &const_time_select_tests[i];
1129*a1157835SDaniel Fojt if (const_time_select(test->mask, test->true_val,
1130*a1157835SDaniel Fojt test->false_val) != test->expected) {
1131*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1132*a1157835SDaniel Fojt "const_time_select(0x%x,0x%x,0x%x) test failed",
1133*a1157835SDaniel Fojt test->mask, test->true_val, test->false_val);
1134*a1157835SDaniel Fojt ret = -1;
1135*a1157835SDaniel Fojt }
1136*a1157835SDaniel Fojt }
1137*a1157835SDaniel Fojt
1138*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_select_int_tests); i++) {
1139*a1157835SDaniel Fojt struct const_time_select_int_test *test;
1140*a1157835SDaniel Fojt
1141*a1157835SDaniel Fojt test = &const_time_select_int_tests[i];
1142*a1157835SDaniel Fojt if (const_time_select_int(test->mask, test->true_val,
1143*a1157835SDaniel Fojt test->false_val) != test->expected) {
1144*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1145*a1157835SDaniel Fojt "const_time_select_int(0x%x,%d,%d) test failed",
1146*a1157835SDaniel Fojt test->mask, test->true_val, test->false_val);
1147*a1157835SDaniel Fojt ret = -1;
1148*a1157835SDaniel Fojt }
1149*a1157835SDaniel Fojt }
1150*a1157835SDaniel Fojt
1151*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_select_u8_tests); i++) {
1152*a1157835SDaniel Fojt struct const_time_select_u8_test *test;
1153*a1157835SDaniel Fojt
1154*a1157835SDaniel Fojt test = &const_time_select_u8_tests[i];
1155*a1157835SDaniel Fojt if (const_time_select_u8(test->mask, test->true_val,
1156*a1157835SDaniel Fojt test->false_val) != test->expected) {
1157*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1158*a1157835SDaniel Fojt "const_time_select_u8(0x%x,0x%x,0x%x) test failed",
1159*a1157835SDaniel Fojt test->mask, test->true_val, test->false_val);
1160*a1157835SDaniel Fojt ret = -1;
1161*a1157835SDaniel Fojt }
1162*a1157835SDaniel Fojt }
1163*a1157835SDaniel Fojt
1164*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_select_s8_tests); i++) {
1165*a1157835SDaniel Fojt struct const_time_select_s8_test *test;
1166*a1157835SDaniel Fojt
1167*a1157835SDaniel Fojt test = &const_time_select_s8_tests[i];
1168*a1157835SDaniel Fojt if (const_time_select_s8(test->mask, test->true_val,
1169*a1157835SDaniel Fojt test->false_val) != test->expected) {
1170*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1171*a1157835SDaniel Fojt "const_time_select_s8(0x%x,0x%x,0x%x) test failed",
1172*a1157835SDaniel Fojt test->mask, test->true_val, test->false_val);
1173*a1157835SDaniel Fojt ret = -1;
1174*a1157835SDaniel Fojt }
1175*a1157835SDaniel Fojt }
1176*a1157835SDaniel Fojt
1177*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_select_bin_tests); i++) {
1178*a1157835SDaniel Fojt struct const_time_select_bin_test *test;
1179*a1157835SDaniel Fojt u8 dst[100];
1180*a1157835SDaniel Fojt
1181*a1157835SDaniel Fojt test = &const_time_select_bin_tests[i];
1182*a1157835SDaniel Fojt const_time_select_bin(test->mask, test->true_val,
1183*a1157835SDaniel Fojt test->false_val, test->len, dst);
1184*a1157835SDaniel Fojt if (os_memcmp(dst, test->expected, test->len) != 0) {
1185*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1186*a1157835SDaniel Fojt "const_time_select_bin(0x%x,%u) test failed",
1187*a1157835SDaniel Fojt test->mask, (unsigned int) test->len);
1188*a1157835SDaniel Fojt ret = -1;
1189*a1157835SDaniel Fojt }
1190*a1157835SDaniel Fojt }
1191*a1157835SDaniel Fojt
1192*a1157835SDaniel Fojt for (i = 0; i < ARRAY_SIZE(const_time_memcmp_tests); i++) {
1193*a1157835SDaniel Fojt struct const_time_memcmp_test *test;
1194*a1157835SDaniel Fojt int res;
1195*a1157835SDaniel Fojt
1196*a1157835SDaniel Fojt test = &const_time_memcmp_tests[i];
1197*a1157835SDaniel Fojt res = const_time_memcmp(test->a, test->b, test->len);
1198*a1157835SDaniel Fojt if (res != test->expected) {
1199*a1157835SDaniel Fojt wpa_printf(MSG_ERROR,
1200*a1157835SDaniel Fojt "const_time_memcmp(%s,%s,%d) test failed (%d != %d)",
1201*a1157835SDaniel Fojt test->a, test->b, (int) test->len,
1202*a1157835SDaniel Fojt res, test->expected);
1203*a1157835SDaniel Fojt ret = -1;
1204*a1157835SDaniel Fojt }
1205*a1157835SDaniel Fojt }
1206*a1157835SDaniel Fojt
1207*a1157835SDaniel Fojt return ret;
1208*a1157835SDaniel Fojt }
1209*a1157835SDaniel Fojt
1210*a1157835SDaniel Fojt
utils_module_tests(void)1211*a1157835SDaniel Fojt int utils_module_tests(void)
1212*a1157835SDaniel Fojt {
1213*a1157835SDaniel Fojt int ret = 0;
1214*a1157835SDaniel Fojt
1215*a1157835SDaniel Fojt wpa_printf(MSG_INFO, "utils module tests");
1216*a1157835SDaniel Fojt
1217*a1157835SDaniel Fojt if (printf_encode_decode_tests() < 0 ||
1218*a1157835SDaniel Fojt ext_password_tests() < 0 ||
1219*a1157835SDaniel Fojt trace_tests() < 0 ||
1220*a1157835SDaniel Fojt bitfield_tests() < 0 ||
1221*a1157835SDaniel Fojt base64_tests() < 0 ||
1222*a1157835SDaniel Fojt common_tests() < 0 ||
1223*a1157835SDaniel Fojt os_tests() < 0 ||
1224*a1157835SDaniel Fojt wpabuf_tests() < 0 ||
1225*a1157835SDaniel Fojt ip_addr_tests() < 0 ||
1226*a1157835SDaniel Fojt eloop_tests() < 0 ||
1227*a1157835SDaniel Fojt json_tests() < 0 ||
1228*a1157835SDaniel Fojt const_time_tests() < 0 ||
1229*a1157835SDaniel Fojt int_array_tests() < 0)
1230*a1157835SDaniel Fojt ret = -1;
1231*a1157835SDaniel Fojt
1232*a1157835SDaniel Fojt return ret;
1233*a1157835SDaniel Fojt }
1234