utils_module_tests.c revision b5d893b5dec601a58c3ce0fc9e5d6da3816ce97a
154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org/*
254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org * utils module tests
354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org *
554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org * This software may be distributed under the terms of the BSD license.
654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org * See README for more details.
754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org */
854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org#include "utils/includes.h"
1054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
1154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org#include "utils/common.h"
1254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org#include "utils/bitfield.h"
1354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org#include "utils/ext_password.h"
1454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org#include "utils/trace.h"
1554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
1654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
1754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.orgstruct printf_test_data {
1854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	u8 *data;
1954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	size_t len;
2054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	char *encoded;
2154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org};
2254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
2354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.orgstatic const struct printf_test_data printf_tests[] = {
2454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "abcde", 5, "abcde" },
2554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
2654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
2754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "\n\n\n", 3, "\n\12\x0a" },
2854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
2954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	  "\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
3054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
3154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	  "\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
3254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
3354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	  "\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
3454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	{ NULL, 0, NULL }
3554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org};
3654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
3754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
3854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.orgstatic int printf_encode_decode_tests(void)
3954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org{
4054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	int i;
4154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	size_t binlen;
4254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	char buf[100];
4354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	u8 bin[100];
4454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	int errors = 0;
4554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
4654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	wpa_printf(MSG_INFO, "printf encode/decode tests");
4754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
4854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	for (i = 0; printf_tests[i].data; i++) {
4954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		const struct printf_test_data *test = &printf_tests[i];
5054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		printf_encode(buf, sizeof(buf), test->data, test->len);
5154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
5254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
5354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		binlen = printf_decode(bin, sizeof(bin), buf);
5454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		if (binlen != test->len ||
5554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		    os_memcmp(bin, test->data, binlen) != 0) {
5654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			wpa_hexdump(MSG_ERROR, "Error in decoding#1",
5754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org				    bin, binlen);
5854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			errors++;
5954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		}
6054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
6154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		binlen = printf_decode(bin, sizeof(bin), test->encoded);
6254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		if (binlen != test->len ||
6354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		    os_memcmp(bin, test->data, binlen) != 0) {
6454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			wpa_hexdump(MSG_ERROR, "Error in decoding#2",
6554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org				    bin, binlen);
6654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			errors++;
6754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		}
6854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	}
6954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
7054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	buf[5] = 'A';
7154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	printf_encode(buf, 5, (const u8 *) "abcde", 5);
7254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	if (buf[5] != 'A') {
7354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		wpa_printf(MSG_ERROR, "Error in bounds checking#1");
7454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		errors++;
7554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	}
7654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
7754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	for (i = 5; i < 10; i++) {
7854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		buf[i] = 'A';
7954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
8054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		if (buf[i] != 'A') {
8154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
8254ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org				   i);
8354ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org			errors++;
8454ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		}
8554ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	}
8654ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
8754ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	if (errors) {
8854ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
8954ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org		return -1;
9054ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org	}
9154ad9296e15baf36bbc485d9fe1878ba4a28ebe2jiayl@webrtc.org
92	return 0;
93}
94
95
96static int bitfield_tests(void)
97{
98	struct bitfield *bf;
99	int i;
100	int errors = 0;
101
102	wpa_printf(MSG_INFO, "bitfield tests");
103
104	bf = bitfield_alloc(123);
105	if (bf == NULL)
106		return -1;
107
108	for (i = 0; i < 123; i++) {
109		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
110			errors++;
111		if (i > 0 && bitfield_is_set(bf, i - 1))
112			errors++;
113		bitfield_set(bf, i);
114		if (!bitfield_is_set(bf, i))
115			errors++;
116		bitfield_clear(bf, i);
117		if (bitfield_is_set(bf, i))
118			errors++;
119	}
120
121	for (i = 123; i < 200; i++) {
122		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
123			errors++;
124		if (i > 0 && bitfield_is_set(bf, i - 1))
125			errors++;
126		bitfield_set(bf, i);
127		if (bitfield_is_set(bf, i))
128			errors++;
129		bitfield_clear(bf, i);
130		if (bitfield_is_set(bf, i))
131			errors++;
132	}
133
134	for (i = 0; i < 123; i++) {
135		if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
136			errors++;
137		bitfield_set(bf, i);
138		if (!bitfield_is_set(bf, i))
139			errors++;
140	}
141
142	for (i = 0; i < 123; i++) {
143		if (!bitfield_is_set(bf, i))
144			errors++;
145		bitfield_clear(bf, i);
146		if (bitfield_is_set(bf, i))
147			errors++;
148	}
149
150	for (i = 0; i < 123; i++) {
151		if (bitfield_get_first_zero(bf) != i)
152			errors++;
153		bitfield_set(bf, i);
154	}
155	if (bitfield_get_first_zero(bf) != -1)
156		errors++;
157	for (i = 0; i < 123; i++) {
158		if (!bitfield_is_set(bf, i))
159			errors++;
160		bitfield_clear(bf, 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
168	bitfield_free(bf);
169
170	if (errors) {
171		wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
172		return -1;
173	}
174
175	return 0;
176}
177
178
179static int int_array_tests(void)
180{
181	int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
182	int test2[] = { 1, -1, 0 };
183	int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
184	int test3_res[] = { -1, 1, 2, 3, 4, 0 };
185	int errors = 0;
186	int len;
187
188	wpa_printf(MSG_INFO, "int_array tests");
189
190	if (int_array_len(test1) != 6 ||
191	    int_array_len(test2) != 2)
192		errors++;
193
194	int_array_sort_unique(test3);
195	len = int_array_len(test3_res);
196	if (int_array_len(test3) != len)
197		errors++;
198	else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
199		errors++;
200
201	if (errors) {
202		wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
203		return -1;
204	}
205
206	return 0;
207}
208
209
210static int ext_password_tests(void)
211{
212	struct ext_password_data *data;
213	int ret = 0;
214	struct wpabuf *pw;
215
216	wpa_printf(MSG_INFO, "ext_password tests");
217
218	data = ext_password_init("unknown", "foo");
219	if (data != NULL)
220		return -1;
221
222	data = ext_password_init("test", NULL);
223	if (data == NULL)
224		return -1;
225	pw = ext_password_get(data, "foo");
226	if (pw != NULL)
227		ret = -1;
228	ext_password_free(pw);
229
230	ext_password_deinit(data);
231
232	pw = ext_password_get(NULL, "foo");
233	if (pw != NULL)
234		ret = -1;
235	ext_password_free(pw);
236
237	return ret;
238}
239
240
241static int trace_tests(void)
242{
243	wpa_printf(MSG_INFO, "trace tests");
244
245	wpa_trace_show("test backtrace");
246	wpa_trace_dump_funcname("test funcname", trace_tests);
247
248	return 0;
249}
250
251
252int utils_module_tests(void)
253{
254	int ret = 0;
255
256	wpa_printf(MSG_INFO, "utils module tests");
257
258	if (printf_encode_decode_tests() < 0 ||
259	    ext_password_tests() < 0 ||
260	    trace_tests() < 0 ||
261	    bitfield_tests() < 0 ||
262	    int_array_tests() < 0)
263		ret = -1;
264
265	return ret;
266}
267