utils_module_tests.c revision 76cd2cc44b62e858f1897ce58f4ce7d0174e8839
1/*
2 * utils module tests
3 * Copyright (c) 2014, 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
16
17struct printf_test_data {
18	u8 *data;
19	size_t len;
20	char *encoded;
21};
22
23static const struct printf_test_data printf_tests[] = {
24	{ (u8 *) "abcde", 5, "abcde" },
25	{ (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
26	{ (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
27	{ (u8 *) "\n\n\n", 3, "\n\12\x0a" },
28	{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
29	  "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
30	{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
31	  "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
32	{ (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
33	  "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
34	{ NULL, 0, NULL }
35};
36
37
38static int printf_encode_decode_tests(void)
39{
40	int i;
41	size_t binlen;
42	char buf[100];
43	u8 bin[100];
44	int errors = 0;
45
46	wpa_printf(MSG_INFO, "printf encode/decode tests");
47
48	for (i = 0; printf_tests[i].data; i++) {
49		const struct printf_test_data *test = &printf_tests[i];
50		printf_encode(buf, sizeof(buf), test->data, test->len);
51		wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
52
53		binlen = printf_decode(bin, sizeof(bin), buf);
54		if (binlen != test->len ||
55		    os_memcmp(bin, test->data, binlen) != 0) {
56			wpa_hexdump(MSG_ERROR, "Error in decoding#1",
57				    bin, binlen);
58			errors++;
59		}
60
61		binlen = printf_decode(bin, sizeof(bin), test->encoded);
62		if (binlen != test->len ||
63		    os_memcmp(bin, test->data, binlen) != 0) {
64			wpa_hexdump(MSG_ERROR, "Error in decoding#2",
65				    bin, binlen);
66			errors++;
67		}
68	}
69
70	if (errors) {
71		wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
72		return -1;
73	}
74
75	return 0;
76}
77
78
79static int bitfield_tests(void)
80{
81	struct bitfield *bf;
82	int i;
83	int errors = 0;
84
85	wpa_printf(MSG_INFO, "bitfield tests");
86
87	bf = bitfield_alloc(123);
88	if (bf == NULL)
89		return -1;
90
91	for (i = 0; i < 123; i++) {
92		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
93			errors++;
94		if (i > 0 && bitfield_is_set(bf, i - 1))
95			errors++;
96		bitfield_set(bf, i);
97		if (!bitfield_is_set(bf, i))
98			errors++;
99		bitfield_clear(bf, i);
100		if (bitfield_is_set(bf, i))
101			errors++;
102	}
103
104	for (i = 123; i < 200; i++) {
105		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
106			errors++;
107		if (i > 0 && bitfield_is_set(bf, i - 1))
108			errors++;
109		bitfield_set(bf, i);
110		if (bitfield_is_set(bf, i))
111			errors++;
112		bitfield_clear(bf, i);
113		if (bitfield_is_set(bf, i))
114			errors++;
115	}
116
117	for (i = 0; i < 123; i++) {
118		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
119			errors++;
120		bitfield_set(bf, i);
121		if (!bitfield_is_set(bf, i))
122			errors++;
123	}
124
125	for (i = 0; i < 123; i++) {
126		if (!bitfield_is_set(bf, i))
127			errors++;
128		bitfield_clear(bf, i);
129		if (bitfield_is_set(bf, i))
130			errors++;
131	}
132
133	for (i = 0; i < 123; i++) {
134		if (bitfield_get_first_zero(bf) != i)
135			errors++;
136		bitfield_set(bf, i);
137	}
138	if (bitfield_get_first_zero(bf) != -1)
139		errors++;
140	for (i = 0; i < 123; i++) {
141		if (!bitfield_is_set(bf, i))
142			errors++;
143		bitfield_clear(bf, i);
144		if (bitfield_get_first_zero(bf) != i)
145			errors++;
146		bitfield_set(bf, i);
147	}
148	if (bitfield_get_first_zero(bf) != -1)
149		errors++;
150
151	bitfield_free(bf);
152
153	if (errors) {
154		wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
155		return -1;
156	}
157
158	return 0;
159}
160
161
162static int int_array_tests(void)
163{
164	int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
165	int test2[] = { 1, -1, 0 };
166	int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
167	int test3_res[] = { -1, 1, 2, 3, 4, 0 };
168	int errors = 0;
169	int len;
170
171	wpa_printf(MSG_INFO, "int_array tests");
172
173	if (int_array_len(test1) != 6 ||
174	    int_array_len(test2) != 2)
175		errors++;
176
177	int_array_sort_unique(test3);
178	len = int_array_len(test3_res);
179	if (int_array_len(test3) != len)
180		errors++;
181	else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
182		errors++;
183
184	if (errors) {
185		wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
186		return -1;
187	}
188
189	return 0;
190}
191
192
193static int ext_password_tests(void)
194{
195	struct ext_password_data *data;
196	int ret = 0;
197	struct wpabuf *pw;
198
199	wpa_printf(MSG_INFO, "ext_password tests");
200
201	data = ext_password_init("unknown", "foo");
202	if (data != NULL)
203		return -1;
204
205	data = ext_password_init("test", NULL);
206	if (data == NULL)
207		return -1;
208	pw = ext_password_get(data, "foo");
209	if (pw != NULL)
210		ret = -1;
211	ext_password_free(pw);
212
213	ext_password_deinit(data);
214
215	pw = ext_password_get(NULL, "foo");
216	if (pw != NULL)
217		ret = -1;
218	ext_password_free(pw);
219
220	return ret;
221}
222
223
224static int trace_tests(void)
225{
226	wpa_printf(MSG_INFO, "trace tests");
227
228	wpa_trace_show("test backtrace");
229	wpa_trace_dump_funcname("test funcname", trace_tests);
230
231	return 0;
232}
233
234
235int utils_module_tests(void)
236{
237	int ret = 0;
238
239	wpa_printf(MSG_INFO, "utils module tests");
240
241	if (printf_encode_decode_tests() < 0 ||
242	    ext_password_tests() < 0 ||
243	    trace_tests() < 0 ||
244	    bitfield_tests() < 0 ||
245	    int_array_tests() < 0)
246		ret = -1;
247
248	return ret;
249}
250