]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
printf-hook: Add some basic printf() string/integer test functions
authorMartin Willi <martin@revosec.ch>
Fri, 27 Sep 2013 10:19:11 +0000 (12:19 +0200)
committerMartin Willi <martin@revosec.ch>
Fri, 11 Oct 2013 09:05:37 +0000 (11:05 +0200)
src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/test_printf.c [new file with mode: 0644]
src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_runner.h

index 585f9c16e71faea536c568d6c610f7c9a8fd8dcb..c3d41a1cd829acd0d118b27d5b976b2790ed464d 100644 (file)
@@ -7,7 +7,7 @@ test_runner_SOURCES = \
   test_linked_list.c test_enumerator.c test_linked_list_enumerator.c \
   test_bio_reader.c test_bio_writer.c test_chunk.c test_enum.c test_hashtable.c \
   test_identification.c test_threading.c test_utils.c test_vectors.c \
-  test_array.c test_ecdsa.c test_rsa.c test_host.c
+  test_array.c test_ecdsa.c test_rsa.c test_host.c test_printf.c
 
 test_runner_CFLAGS = \
   -I$(top_srcdir)/src/libstrongswan \
diff --git a/src/libstrongswan/tests/test_printf.c b/src/libstrongswan/tests/test_printf.c
new file mode 100644 (file)
index 0000000..2e42475
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2013 Martin Willi
+ * Copyright (C) 2013 revosec AG
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+#include "test_suite.h"
+
+static void verify(char *expected, char *format, ...)
+{
+       FILE *mem;
+       char buf[128];
+       va_list args;
+
+       va_start(args, format);
+       vsnprintf(buf, sizeof(buf), format, args);
+       ck_assert_str_eq(expected, buf);
+       va_end(args);
+
+       mem = fmemopen(buf, sizeof(buf), "w");
+       va_start(args, format);
+       vfprintf(mem, format, args);
+       va_end(args);
+       fclose(mem);
+       ck_assert_str_eq(expected, buf);
+}
+
+START_TEST(test_printf_strings)
+{
+       verify("a bc def", "%s %s %s", "a", "bc", "def");
+       verify("asd", "%.3s", "asdfg");
+       verify("asdf", "%.*s", (int)4, "asdfg");
+       verify("  asdf", "%6s", "asdf");
+       verify("  asdf", "%+6s", "asdf");
+       verify("asdf  ", "%-6s", "asdf");
+}
+END_TEST
+
+START_TEST(test_printf_unsigned)
+{
+       verify("1 23 456", "%u %lu %llu", 1, (u_long)23, (u_int64_t)456);
+       verify("65535 255", "%hu %hhu", 0x1ffff, 0x1ff);
+       verify("123456789", "%zu", (size_t)123456789);
+       verify("   12", "%5u", 12);
+       verify("12   ", "%-5u", 12);
+       verify("0012", "%04u", 12);
+       verify("0012", "%.4u", 12);
+}
+END_TEST
+
+START_TEST(test_printf_signed)
+{
+       verify("-1 -23 -456", "%d %ld %lld", -1, (long)-23, (int64_t)-456);
+       verify("-1 -1", "%hd %hhd", 0x1ffff, 0x1ff);
+       verify("123456789", "%zd", (ssize_t)123456789);
+       verify("  -12", "%5d", -12);
+       verify("-12  ", "%-5d", -12);
+       verify("-012", "%04d", -12);
+       verify("-0012", "%.4d", -12);
+}
+END_TEST
+
+START_TEST(test_printf_hex)
+{
+       verify("1 23 456", "%x %lx %llx", 1, (u_long)0x23, (u_int64_t)0x456);
+       verify("12abcdef 12ABCDEF", "%x %X", 0x12ABCDEF, 0x12ABCDEF);
+       verify("ffff ff", "%hx %hhx", 0x1ffff, 0x1ff);
+       verify("23456789", "%zx", (size_t)0x23456789);
+       verify("   ab", "%5x", 0xab);
+       verify("ab   ", "%-5x", 0xab);
+       verify("00ab", "%04x", 0xab);
+       verify("00ab", "%.4x", 0xab);
+}
+END_TEST
+
+Suite *printf_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("printf");
+
+       tc = tcase_create("strings");
+       tcase_add_test(tc, test_printf_strings);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("unsiged");
+       tcase_add_test(tc, test_printf_unsigned);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("siged");
+       tcase_add_test(tc, test_printf_signed);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("hex");
+       tcase_add_test(tc, test_printf_hex);
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index e7a04fd9a3d8c545024217c9330df073a0f9d399..f85858504ec5b7638b0b9837336ce7d63267ec4a 100644 (file)
@@ -84,6 +84,7 @@ int main()
        srunner_add_suite(sr, utils_suite_create());
        srunner_add_suite(sr, host_suite_create());
        srunner_add_suite(sr, vectors_suite_create());
+       srunner_add_suite(sr, printf_suite_create());
        if (lib->plugins->has_feature(lib->plugins,
                                                                  PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA)))
        {
index e9381756cb7fff29f2b70f8fd751fae717f1c035..6315abba75bef8bbf5f3a5a4b86f063e52dd395f 100644 (file)
@@ -34,5 +34,6 @@ Suite *vectors_suite_create();
 Suite *ecdsa_suite_create();
 Suite *rsa_suite_create();
 Suite *host_suite_create();
+Suite *printf_suite_create();
 
 #endif /** TEST_RUNNER_H_ */