xref: /freebsd/contrib/wpa/src/utils/utils_module_tests.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*
2  * utils module tests
3  * Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
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"
16 
17 
18 struct printf_test_data {
19 	u8 *data;
20 	size_t len;
21 	char *encoded;
22 };
23 
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" },
35 	{ NULL, 0, NULL }
36 };
37 
38 
39 static int printf_encode_decode_tests(void)
40 {
41 	int i;
42 	size_t binlen;
43 	char buf[100];
44 	u8 bin[100];
45 	int errors = 0;
46 
47 	wpa_printf(MSG_INFO, "printf encode/decode tests");
48 
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);
53 
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",
58 				    bin, binlen);
59 			errors++;
60 		}
61 
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",
66 				    bin, binlen);
67 			errors++;
68 		}
69 	}
70 
71 	buf[5] = 'A';
72 	printf_encode(buf, 5, (const u8 *) "abcde", 5);
73 	if (buf[5] != 'A') {
74 		wpa_printf(MSG_ERROR, "Error in bounds checking#1");
75 		errors++;
76 	}
77 
78 	for (i = 5; i < 10; i++) {
79 		buf[i] = 'A';
80 		printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
81 		if (buf[i] != 'A') {
82 			wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
83 				   i);
84 			errors++;
85 		}
86 	}
87 
88 	if (printf_decode(bin, 3, "abcde") != 2)
89 		errors++;
90 
91 	if (printf_decode(bin, 3, "\\xa") != 1 || bin[0] != 10)
92 		errors++;
93 
94 	if (printf_decode(bin, 3, "\\a") != 1 || bin[0] != 'a')
95 		errors++;
96 
97 	if (errors) {
98 		wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
99 		return -1;
100 	}
101 
102 	return 0;
103 }
104 
105 
106 static int bitfield_tests(void)
107 {
108 	struct bitfield *bf;
109 	int i;
110 	int errors = 0;
111 
112 	wpa_printf(MSG_INFO, "bitfield tests");
113 
114 	bf = bitfield_alloc(123);
115 	if (bf == NULL)
116 		return -1;
117 
118 	for (i = 0; i < 123; i++) {
119 		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
120 			errors++;
121 		if (i > 0 && bitfield_is_set(bf, i - 1))
122 			errors++;
123 		bitfield_set(bf, i);
124 		if (!bitfield_is_set(bf, i))
125 			errors++;
126 		bitfield_clear(bf, i);
127 		if (bitfield_is_set(bf, i))
128 			errors++;
129 	}
130 
131 	for (i = 123; i < 200; i++) {
132 		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
133 			errors++;
134 		if (i > 0 && bitfield_is_set(bf, i - 1))
135 			errors++;
136 		bitfield_set(bf, i);
137 		if (bitfield_is_set(bf, i))
138 			errors++;
139 		bitfield_clear(bf, i);
140 		if (bitfield_is_set(bf, i))
141 			errors++;
142 	}
143 
144 	for (i = 0; i < 123; i++) {
145 		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
146 			errors++;
147 		bitfield_set(bf, i);
148 		if (!bitfield_is_set(bf, i))
149 			errors++;
150 	}
151 
152 	for (i = 0; i < 123; i++) {
153 		if (!bitfield_is_set(bf, i))
154 			errors++;
155 		bitfield_clear(bf, i);
156 		if (bitfield_is_set(bf, i))
157 			errors++;
158 	}
159 
160 	for (i = 0; i < 123; i++) {
161 		if (bitfield_get_first_zero(bf) != i)
162 			errors++;
163 		bitfield_set(bf, i);
164 	}
165 	if (bitfield_get_first_zero(bf) != -1)
166 		errors++;
167 	for (i = 0; i < 123; i++) {
168 		if (!bitfield_is_set(bf, i))
169 			errors++;
170 		bitfield_clear(bf, i);
171 		if (bitfield_get_first_zero(bf) != i)
172 			errors++;
173 		bitfield_set(bf, i);
174 	}
175 	if (bitfield_get_first_zero(bf) != -1)
176 		errors++;
177 
178 	bitfield_free(bf);
179 
180 	bf = bitfield_alloc(8);
181 	if (bf == NULL)
182 		return -1;
183 	if (bitfield_get_first_zero(bf) != 0)
184 		errors++;
185 	for (i = 0; i < 8; i++)
186 		bitfield_set(bf, i);
187 	if (bitfield_get_first_zero(bf) != -1)
188 		errors++;
189 	bitfield_free(bf);
190 
191 	if (errors) {
192 		wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
193 		return -1;
194 	}
195 
196 	return 0;
197 }
198 
199 
200 static int int_array_tests(void)
201 {
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 };
206 	int errors = 0;
207 	int len;
208 
209 	wpa_printf(MSG_INFO, "int_array tests");
210 
211 	if (int_array_len(test1) != 6 ||
212 	    int_array_len(test2) != 2)
213 		errors++;
214 
215 	int_array_sort_unique(test3);
216 	len = int_array_len(test3_res);
217 	if (int_array_len(test3) != len)
218 		errors++;
219 	else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
220 		errors++;
221 
222 	if (errors) {
223 		wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
224 		return -1;
225 	}
226 
227 	return 0;
228 }
229 
230 
231 static int ext_password_tests(void)
232 {
233 	struct ext_password_data *data;
234 	int ret = 0;
235 	struct wpabuf *pw;
236 
237 	wpa_printf(MSG_INFO, "ext_password tests");
238 
239 	data = ext_password_init("unknown", "foo");
240 	if (data != NULL)
241 		return -1;
242 
243 	data = ext_password_init("test", NULL);
244 	if (data == NULL)
245 		return -1;
246 	pw = ext_password_get(data, "foo");
247 	if (pw != NULL)
248 		ret = -1;
249 	ext_password_free(pw);
250 
251 	ext_password_deinit(data);
252 
253 	pw = ext_password_get(NULL, "foo");
254 	if (pw != NULL)
255 		ret = -1;
256 	ext_password_free(pw);
257 
258 	return ret;
259 }
260 
261 
262 static int trace_tests(void)
263 {
264 	wpa_printf(MSG_INFO, "trace tests");
265 
266 	wpa_trace_show("test backtrace");
267 	wpa_trace_dump_funcname("test funcname", trace_tests);
268 
269 	return 0;
270 }
271 
272 
273 static int base64_tests(void)
274 {
275 	int errors = 0;
276 	unsigned char *res;
277 	size_t res_len;
278 
279 	wpa_printf(MSG_INFO, "base64 tests");
280 
281 	res = base64_encode((const unsigned char *) "", ~0, &res_len);
282 	if (res) {
283 		errors++;
284 		os_free(res);
285 	}
286 
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')
290 		errors++;
291 	os_free(res);
292 
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')
296 		errors++;
297 	os_free(res);
298 
299 	res = base64_decode((const unsigned char *) "", 0, &res_len);
300 	if (res) {
301 		errors++;
302 		os_free(res);
303 	}
304 
305 	res = base64_decode((const unsigned char *) "a", 1, &res_len);
306 	if (res) {
307 		errors++;
308 		os_free(res);
309 	}
310 
311 	res = base64_decode((const unsigned char *) "====", 4, &res_len);
312 	if (res) {
313 		errors++;
314 		os_free(res);
315 	}
316 
317 	res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
318 	if (!res || res_len != 1 || res[0] != '=')
319 		errors++;
320 	os_free(res);
321 
322 	res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
323 	if (!res || res_len != 1 || res[0] != '=')
324 		errors++;
325 	os_free(res);
326 
327 	if (errors) {
328 		wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
329 		return -1;
330 	}
331 
332 	return 0;
333 }
334 
335 
336 static int common_tests(void)
337 {
338 	char buf[3];
339 	u8 addr[ETH_ALEN] = { 1, 2, 3, 4, 5, 6 };
340 	u8 bin[3];
341 	int errors = 0;
342 	struct wpa_freq_range_list ranges;
343 
344 	wpa_printf(MSG_INFO, "common tests");
345 
346 	if (hwaddr_mask_txt(buf, 3, addr, addr) != -1)
347 		errors++;
348 
349 	if (wpa_scnprintf(buf, 0, "hello") != 0 ||
350 	    wpa_scnprintf(buf, 3, "hello") != 2)
351 		errors++;
352 
353 	if (wpa_snprintf_hex(buf, 0, addr, ETH_ALEN) != 0 ||
354 	    wpa_snprintf_hex(buf, 3, addr, ETH_ALEN) != 2)
355 		errors++;
356 
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)
359 		errors++;
360 
361 	if (dup_binstr(NULL, 0) != NULL)
362 		errors++;
363 
364 	if (freq_range_list_includes(NULL, 0) != 0)
365 		errors++;
366 
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)
371 		errors++;
372 
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)
378 		errors++;
379 
380 	if (utf8_unescape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
381 		errors++;
382 
383 	if (utf8_unescape("\\b", 2, buf, sizeof(buf)) != 1 || buf[0] != 'b')
384 		errors++;
385 
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)
393 		errors++;
394 
395 	if (utf8_escape("a", 0, buf, sizeof(buf)) != 1 || buf[0] != 'a')
396 		errors++;
397 
398 	if (errors) {
399 		wpa_printf(MSG_ERROR, "%d common test(s) failed", errors);
400 		return -1;
401 	}
402 
403 	return 0;
404 }
405 
406 
407 int utils_module_tests(void)
408 {
409 	int ret = 0;
410 
411 	wpa_printf(MSG_INFO, "utils module tests");
412 
413 	if (printf_encode_decode_tests() < 0 ||
414 	    ext_password_tests() < 0 ||
415 	    trace_tests() < 0 ||
416 	    bitfield_tests() < 0 ||
417 	    base64_tests() < 0 ||
418 	    common_tests() < 0 ||
419 	    int_array_tests() < 0)
420 		ret = -1;
421 
422 	return ret;
423 }
424