2012-08-07 12:30:13 +02:00
|
|
|
/*
|
2014-05-25 19:35:55 +02:00
|
|
|
* utils module tests
|
2015-02-28 10:25:48 +01:00
|
|
|
* Copyright (c) 2014-2015, Jouni Malinen <j@w1.fi>
|
2012-08-07 12:30:13 +02:00
|
|
|
*
|
|
|
|
* This software may be distributed under the terms of the BSD license.
|
|
|
|
* See README for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils/includes.h"
|
2014-05-25 19:35:55 +02:00
|
|
|
|
2012-08-07 12:30:13 +02:00
|
|
|
#include "utils/common.h"
|
2014-05-25 21:17:12 +02:00
|
|
|
#include "utils/bitfield.h"
|
2014-05-26 22:57:32 +02:00
|
|
|
#include "utils/ext_password.h"
|
2014-05-26 23:03:06 +02:00
|
|
|
#include "utils/trace.h"
|
2015-02-28 10:25:48 +01:00
|
|
|
#include "utils/base64.h"
|
2012-08-07 12:30:13 +02:00
|
|
|
|
|
|
|
|
2014-05-25 19:35:55 +02:00
|
|
|
struct printf_test_data {
|
2012-08-07 12:30:13 +02:00
|
|
|
u8 *data;
|
|
|
|
size_t len;
|
|
|
|
char *encoded;
|
|
|
|
};
|
|
|
|
|
2014-05-25 19:35:55 +02:00
|
|
|
static const struct printf_test_data printf_tests[] = {
|
2012-08-07 12:30:13 +02:00
|
|
|
{ (u8 *) "abcde", 5, "abcde" },
|
2014-05-25 19:35:55 +02:00
|
|
|
{ (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
|
2012-08-07 12:30:13 +02:00
|
|
|
{ (u8 *) "\x00\x31\x00\x32\x00\x39", 6, "\\x001\\0002\\09" },
|
|
|
|
{ (u8 *) "\n\n\n", 3, "\n\12\x0a" },
|
|
|
|
{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
|
|
|
|
"\\xc3\\xa5\xc3\\xa4\\xc3\\xb6\\xc3\\x85\\xc3\\x84\\xc3\\x96" },
|
|
|
|
{ (u8 *) "\303\245\303\244\303\266\303\205\303\204\303\226", 12,
|
|
|
|
"\\303\\245\\303\\244\\303\\266\\303\\205\\303\\204\\303\\226" },
|
|
|
|
{ (u8 *) "\xe5\xe4\xf6\xc5\xc4\xd6", 6,
|
|
|
|
"\\xe5\\xe4\\xf6\\xc5\\xc4\\xd6" },
|
|
|
|
{ NULL, 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-25 19:35:55 +02:00
|
|
|
static int printf_encode_decode_tests(void)
|
2012-08-07 12:30:13 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t binlen;
|
|
|
|
char buf[100];
|
|
|
|
u8 bin[100];
|
|
|
|
int errors = 0;
|
|
|
|
|
2014-05-25 19:35:55 +02:00
|
|
|
wpa_printf(MSG_INFO, "printf encode/decode tests");
|
|
|
|
|
|
|
|
for (i = 0; printf_tests[i].data; i++) {
|
|
|
|
const struct printf_test_data *test = &printf_tests[i];
|
2012-08-07 12:30:13 +02:00
|
|
|
printf_encode(buf, sizeof(buf), test->data, test->len);
|
2014-05-25 19:35:55 +02:00
|
|
|
wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
|
2012-08-07 12:30:13 +02:00
|
|
|
|
|
|
|
binlen = printf_decode(bin, sizeof(bin), buf);
|
|
|
|
if (binlen != test->len ||
|
|
|
|
os_memcmp(bin, test->data, binlen) != 0) {
|
2014-05-25 19:35:55 +02:00
|
|
|
wpa_hexdump(MSG_ERROR, "Error in decoding#1",
|
|
|
|
bin, binlen);
|
2012-08-07 12:30:13 +02:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
binlen = printf_decode(bin, sizeof(bin), test->encoded);
|
|
|
|
if (binlen != test->len ||
|
|
|
|
os_memcmp(bin, test->data, binlen) != 0) {
|
2014-05-25 19:35:55 +02:00
|
|
|
wpa_hexdump(MSG_ERROR, "Error in decoding#2",
|
|
|
|
bin, binlen);
|
2012-08-07 12:30:13 +02:00
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 14:49:54 +02:00
|
|
|
buf[5] = 'A';
|
|
|
|
printf_encode(buf, 5, (const u8 *) "abcde", 5);
|
|
|
|
if (buf[5] != 'A') {
|
|
|
|
wpa_printf(MSG_ERROR, "Error in bounds checking#1");
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 5; i < 10; i++) {
|
|
|
|
buf[i] = 'A';
|
|
|
|
printf_encode(buf, i, (const u8 *) "\xdd\xdd\xdd\xdd\xdd", 5);
|
|
|
|
if (buf[i] != 'A') {
|
|
|
|
wpa_printf(MSG_ERROR, "Error in bounds checking#2(%d)",
|
|
|
|
i);
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 12:30:13 +02:00
|
|
|
if (errors) {
|
2014-05-25 19:35:55 +02:00
|
|
|
wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
|
2012-08-07 12:30:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-25 19:35:55 +02:00
|
|
|
|
|
|
|
|
2014-05-25 21:17:12 +02:00
|
|
|
static int bitfield_tests(void)
|
|
|
|
{
|
|
|
|
struct bitfield *bf;
|
|
|
|
int i;
|
|
|
|
int errors = 0;
|
|
|
|
|
|
|
|
wpa_printf(MSG_INFO, "bitfield tests");
|
|
|
|
|
|
|
|
bf = bitfield_alloc(123);
|
|
|
|
if (bf == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < 123; i++) {
|
|
|
|
if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
|
|
|
|
errors++;
|
|
|
|
if (i > 0 && bitfield_is_set(bf, i - 1))
|
|
|
|
errors++;
|
|
|
|
bitfield_set(bf, i);
|
|
|
|
if (!bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
bitfield_clear(bf, i);
|
|
|
|
if (bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 123; i < 200; i++) {
|
|
|
|
if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
|
|
|
|
errors++;
|
|
|
|
if (i > 0 && bitfield_is_set(bf, i - 1))
|
|
|
|
errors++;
|
|
|
|
bitfield_set(bf, i);
|
|
|
|
if (bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
bitfield_clear(bf, i);
|
|
|
|
if (bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 123; i++) {
|
|
|
|
if (bitfield_is_set(bf, i) || bitfield_is_set(bf, i + 1))
|
|
|
|
errors++;
|
|
|
|
bitfield_set(bf, i);
|
|
|
|
if (!bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 123; i++) {
|
|
|
|
if (!bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
bitfield_clear(bf, i);
|
|
|
|
if (bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 123; i++) {
|
|
|
|
if (bitfield_get_first_zero(bf) != i)
|
|
|
|
errors++;
|
|
|
|
bitfield_set(bf, i);
|
|
|
|
}
|
|
|
|
if (bitfield_get_first_zero(bf) != -1)
|
|
|
|
errors++;
|
|
|
|
for (i = 0; i < 123; i++) {
|
|
|
|
if (!bitfield_is_set(bf, i))
|
|
|
|
errors++;
|
|
|
|
bitfield_clear(bf, i);
|
|
|
|
if (bitfield_get_first_zero(bf) != i)
|
|
|
|
errors++;
|
|
|
|
bitfield_set(bf, i);
|
|
|
|
}
|
|
|
|
if (bitfield_get_first_zero(bf) != -1)
|
|
|
|
errors++;
|
|
|
|
|
|
|
|
bitfield_free(bf);
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
wpa_printf(MSG_ERROR, "%d bitfield test(s) failed", errors);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-25 21:30:46 +02:00
|
|
|
static int int_array_tests(void)
|
|
|
|
{
|
|
|
|
int test1[] = { 1, 2, 3, 4, 5, 6, 0 };
|
|
|
|
int test2[] = { 1, -1, 0 };
|
|
|
|
int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
|
|
|
|
int test3_res[] = { -1, 1, 2, 3, 4, 0 };
|
|
|
|
int errors = 0;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
wpa_printf(MSG_INFO, "int_array tests");
|
|
|
|
|
|
|
|
if (int_array_len(test1) != 6 ||
|
|
|
|
int_array_len(test2) != 2)
|
|
|
|
errors++;
|
|
|
|
|
|
|
|
int_array_sort_unique(test3);
|
|
|
|
len = int_array_len(test3_res);
|
|
|
|
if (int_array_len(test3) != len)
|
|
|
|
errors++;
|
|
|
|
else if (os_memcmp(test3, test3_res, len * sizeof(int)) != 0)
|
|
|
|
errors++;
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
wpa_printf(MSG_ERROR, "%d int_array test(s) failed", errors);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-26 22:57:32 +02:00
|
|
|
static int ext_password_tests(void)
|
|
|
|
{
|
|
|
|
struct ext_password_data *data;
|
|
|
|
int ret = 0;
|
|
|
|
struct wpabuf *pw;
|
|
|
|
|
|
|
|
wpa_printf(MSG_INFO, "ext_password tests");
|
|
|
|
|
|
|
|
data = ext_password_init("unknown", "foo");
|
|
|
|
if (data != NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
data = ext_password_init("test", NULL);
|
|
|
|
if (data == NULL)
|
|
|
|
return -1;
|
|
|
|
pw = ext_password_get(data, "foo");
|
|
|
|
if (pw != NULL)
|
|
|
|
ret = -1;
|
|
|
|
ext_password_free(pw);
|
|
|
|
|
|
|
|
ext_password_deinit(data);
|
|
|
|
|
|
|
|
pw = ext_password_get(NULL, "foo");
|
|
|
|
if (pw != NULL)
|
|
|
|
ret = -1;
|
|
|
|
ext_password_free(pw);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-26 23:03:06 +02:00
|
|
|
static int trace_tests(void)
|
|
|
|
{
|
|
|
|
wpa_printf(MSG_INFO, "trace tests");
|
|
|
|
|
|
|
|
wpa_trace_show("test backtrace");
|
|
|
|
wpa_trace_dump_funcname("test funcname", trace_tests);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-28 10:25:48 +01:00
|
|
|
static int base64_tests(void)
|
|
|
|
{
|
|
|
|
int errors = 0;
|
|
|
|
unsigned char *res;
|
|
|
|
size_t res_len;
|
|
|
|
|
|
|
|
wpa_printf(MSG_INFO, "base64 tests");
|
|
|
|
|
|
|
|
res = base64_encode((const unsigned char *) "", ~0, &res_len);
|
|
|
|
if (res) {
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = base64_encode((const unsigned char *) "=", 1, &res_len);
|
|
|
|
if (!res || res_len != 5 || res[0] != 'P' || res[1] != 'Q' ||
|
|
|
|
res[2] != '=' || res[3] != '=' || res[4] != '\n')
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
|
|
|
|
res = base64_encode((const unsigned char *) "=", 1, NULL);
|
|
|
|
if (!res || res[0] != 'P' || res[1] != 'Q' ||
|
|
|
|
res[2] != '=' || res[3] != '=' || res[4] != '\n')
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
|
|
|
|
res = base64_decode((const unsigned char *) "", 0, &res_len);
|
|
|
|
if (res) {
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = base64_decode((const unsigned char *) "a", 1, &res_len);
|
|
|
|
if (res) {
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = base64_decode((const unsigned char *) "====", 4, &res_len);
|
|
|
|
if (res) {
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = base64_decode((const unsigned char *) "PQ==", 4, &res_len);
|
|
|
|
if (!res || res_len != 1 || res[0] != '=')
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
|
|
|
|
res = base64_decode((const unsigned char *) "P.Q-=!=*", 8, &res_len);
|
|
|
|
if (!res || res_len != 1 || res[0] != '=')
|
|
|
|
errors++;
|
|
|
|
os_free(res);
|
|
|
|
|
|
|
|
if (errors) {
|
|
|
|
wpa_printf(MSG_ERROR, "%d base64 test(s) failed", errors);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-25 19:35:55 +02:00
|
|
|
int utils_module_tests(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
wpa_printf(MSG_INFO, "utils module tests");
|
|
|
|
|
2014-05-25 21:30:46 +02:00
|
|
|
if (printf_encode_decode_tests() < 0 ||
|
2014-05-26 22:57:32 +02:00
|
|
|
ext_password_tests() < 0 ||
|
2014-05-26 23:03:06 +02:00
|
|
|
trace_tests() < 0 ||
|
2014-05-25 21:30:46 +02:00
|
|
|
bitfield_tests() < 0 ||
|
2015-02-28 10:25:48 +01:00
|
|
|
base64_tests() < 0 ||
|
2014-05-25 21:30:46 +02:00
|
|
|
int_array_tests() < 0)
|
2014-05-25 21:17:12 +02:00
|
|
|
ret = -1;
|
2014-05-25 19:35:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|