]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
tests: Add printf encoding/decoding module tests
authorJouni Malinen <j@w1.fi>
Sun, 25 May 2014 17:35:55 +0000 (20:35 +0300)
committerJouni Malinen <j@w1.fi>
Mon, 26 May 2014 14:21:44 +0000 (17:21 +0300)
This replaces tests/test-printf.c.

Signed-off-by: Jouni Malinen <j@w1.fi>
src/utils/utils_module_tests.c [moved from tests/test-printf.c with 59% similarity]
tests/Makefile
wpa_supplicant/Makefile
wpa_supplicant/wpas_module_tests.c

similarity index 59%
rename from tests/test-printf.c
rename to src/utils/utils_module_tests.c
index a8a2e258ed27e3bd692b81ab3956de5c771f8f08..2611c42a6c31ee1c218a554e086830293909ac0a 100644 (file)
@@ -1,25 +1,25 @@
 /*
- * printf format routines - test program
- * Copyright (c) 2012, Jouni Malinen <j@w1.fi>
+ * utils module tests
+ * Copyright (c) 2014, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
  */
 
 #include "utils/includes.h"
-#include "utils/os.h"
+
 #include "utils/common.h"
 
 
-struct test_data {
+struct printf_test_data {
        u8 *data;
        size_t len;
        char *encoded;
 };
 
-static const struct test_data tests[] = {
+static const struct printf_test_data printf_tests[] = {
        { (u8 *) "abcde", 5, "abcde" },
-       { (u8 *) "a\0b\nc\ed\re\tf", 11, "a\\0b\\nc\\ed\\re\\tf" },
+       { (u8 *) "a\0b\nc\ed\re\tf\"\\", 13, "a\\0b\\nc\\ed\\re\\tf\\\"\\\\" },
        { (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,
@@ -32,15 +32,7 @@ static const struct test_data tests[] = {
 };
 
 
-static void print_hex(const u8 *data, size_t len)
-{
-       size_t i;
-       for (i = 0; i < len; i++)
-               printf(" %02x", data[i]);
-}
-
-
-int main(int argc, char *argv[])
+static int printf_encode_decode_tests(void)
 {
        int i;
        size_t binlen;
@@ -48,36 +40,47 @@ int main(int argc, char *argv[])
        u8 bin[100];
        int errors = 0;
 
-       for (i = 0; tests[i].data; i++) {
-               const struct test_data *test = &tests[i];
-               printf("%d:", i);
-               print_hex(test->data, test->len);
+       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];
                printf_encode(buf, sizeof(buf), test->data, test->len);
-               printf(" -> \"%s\"\n", buf);
+               wpa_printf(MSG_INFO, "%d: -> \"%s\"", i, buf);
 
                binlen = printf_decode(bin, sizeof(bin), buf);
                if (binlen != test->len ||
                    os_memcmp(bin, test->data, binlen) != 0) {
-                       printf("Error in decoding#1:");
-                       print_hex(bin, binlen);
-                       printf("\n");
+                       wpa_hexdump(MSG_ERROR, "Error in decoding#1",
+                                   bin, binlen);
                        errors++;
                }
 
                binlen = printf_decode(bin, sizeof(bin), test->encoded);
                if (binlen != test->len ||
                    os_memcmp(bin, test->data, binlen) != 0) {
-                       printf("Error in decoding#2:");
-                       print_hex(bin, binlen);
-                       printf("\n");
+                       wpa_hexdump(MSG_ERROR, "Error in decoding#2",
+                                   bin, binlen);
                        errors++;
                }
        }
 
        if (errors) {
-               printf("%d test(s) failed\n", errors);
+               wpa_printf(MSG_ERROR, "%d printf test(s) failed", errors);
                return -1;
        }
 
        return 0;
 }
+
+
+int utils_module_tests(void)
+{
+       int ret = 0;
+
+       wpa_printf(MSG_INFO, "utils module tests");
+
+       if (printf_encode_decode_tests() < 0)
+               ret = -1;
+
+       return ret;
+}
index 33fec20d1c51fb637eb4a15c539b8f76773f54d4..6fed9fe480320d69281655be44613ab36d8b454e 100644 (file)
@@ -1,6 +1,5 @@
 TESTS=test-base64 test-md4 test-md5 test-milenage test-ms_funcs \
        test-bitfield \
-       test-printf \
        test-rsa-sig-ver \
        test-sha1 \
        test-sha256 test-aes test-asn1 test-x509 test-x509v3 test-list test-rc4
@@ -73,9 +72,6 @@ test-milenage: test-milenage.o $(LIBS)
 test-ms_funcs: test-ms_funcs.o $(LIBS)
        $(LDO) $(LDFLAGS) -o $@ $^ $(LLIBS)
 
-test-printf: test-printf.o $(LIBS)
-       $(LDO) $(LDFLAGS) -o $@ $^ $(LLIBS)
-
 test-rc4: test-rc4.o $(LIBS)
        $(LDO) $(LDFLAGS) -o $@ $^ $(LLIBS)
 
@@ -102,7 +98,6 @@ run-tests: $(TESTS)
        ./test-md4
        ./test-md5
        ./test-milenage
-       ./test-printf
        ./test-rsa-sig-ver
        ./test-sha1
        ./test-sha256
index 49f79078b415eb310fc920d7a576671519df298e..c0913e0ac6685a6396a83a6254838b191595736b 100644 (file)
@@ -100,6 +100,7 @@ OBJS_c += ../src/utils/os_$(CONFIG_OS).o
 ifdef CONFIG_MODULE_TESTS
 CFLAGS += -DCONFIG_MODULE_TESTS
 OBJS += wpas_module_tests.o
+OBJS += ../src/utils/utils_module_tests.o
 ifdef CONFIG_WPS
 OBJS += ../src/wps/wps_module_tests.o
 endif
index 86b70a947d45f644ff61dbec63cea8db06e01305..38493d43860084d849e0cb92fe1b13c03d5e51aa 100644 (file)
@@ -86,5 +86,11 @@ int wpas_module_tests(void)
        }
 #endif /* CONFIG_WPS */
 
+       {
+               int utils_module_tests(void);
+               if (utils_module_tests() < 0)
+                       ret = -1;
+       }
+
        return ret;
 }