unit-tests: Add test for host_create_netmask()
authorTobias Brunner <tobias@strongswan.org>
Wed, 24 Jul 2013 08:33:06 +0000 (10:33 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 24 Jul 2013 14:17:21 +0000 (16:17 +0200)
src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/test_host.c [new file with mode: 0644]
src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_runner.h

index f175aa4..585f9c1 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_array.c test_ecdsa.c test_rsa.c test_host.c
 
 test_runner_CFLAGS = \
   -I$(top_srcdir)/src/libstrongswan \
diff --git a/src/libstrongswan/tests/test_host.c b/src/libstrongswan/tests/test_host.c
new file mode 100644 (file)
index 0000000..bcc0301
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
+ * 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"
+
+#include <networking/host.h>
+
+/*******************************************************************************
+ * host_create_netmask
+ */
+
+static void verify_netmask(chunk_t addr, int mask)
+{
+       int byte, bit;
+
+       for (byte = 0; byte < addr.len; byte++)
+       {
+               for (bit = 7; bit >= 0; bit--)
+               {
+                       int val = (addr.ptr[byte] >> bit) & 0x01;
+                       if (mask-- > 0)
+                       {
+                               ck_assert_int_eq(val, 1);
+                       }
+                       else
+                       {
+                               ck_assert_int_eq(val, 0);
+                       }
+               }
+       }
+}
+
+static void test_create_netmask(int family)
+{
+       host_t *netmask;
+       int i, len = (family == AF_INET) ? 32 : 128;
+
+       netmask = host_create_netmask(family, -1);
+       ck_assert(netmask == NULL);
+       for (i = 0; i <= len; i++)
+       {
+               netmask = host_create_netmask(family, i);
+               verify_netmask(netmask->get_address(netmask), i);
+               netmask->destroy(netmask);
+       }
+       netmask = host_create_netmask(family, len + 1);
+       ck_assert(netmask == NULL);
+}
+
+START_TEST(test_create_netmask_v4)
+{
+       test_create_netmask(AF_INET);
+}
+END_TEST
+
+START_TEST(test_create_netmask_v6)
+{
+       test_create_netmask(AF_INET6);
+}
+END_TEST
+
+START_TEST(test_create_netmask_other)
+{
+       host_t *netmask;
+
+       netmask = host_create_netmask(AF_UNSPEC, 0);
+       ck_assert(netmask == NULL);
+}
+END_TEST
+
+Suite *host_suite_create()
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("host");
+
+       tc = tcase_create("host_create_netmask");
+       tcase_add_test(tc, test_create_netmask_v4);
+       tcase_add_test(tc, test_create_netmask_v6);
+       tcase_add_test(tc, test_create_netmask_other);
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index b9e1901..e7a04fd 100644 (file)
@@ -82,6 +82,7 @@ int main()
        srunner_add_suite(sr, identification_suite_create());
        srunner_add_suite(sr, threading_suite_create());
        srunner_add_suite(sr, utils_suite_create());
+       srunner_add_suite(sr, host_suite_create());
        srunner_add_suite(sr, vectors_suite_create());
        if (lib->plugins->has_feature(lib->plugins,
                                                                  PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA)))
index 34aa0cf..e938175 100644 (file)
@@ -33,5 +33,6 @@ Suite *utils_suite_create();
 Suite *vectors_suite_create();
 Suite *ecdsa_suite_create();
 Suite *rsa_suite_create();
+Suite *host_suite_create();
 
 #endif /** TEST_RUNNER_H_ */