]> git.ipfire.org Git - people/ms/strongswan.git/commitdiff
charon-tkm: Migrate tests to our own test runner
authorTobias Brunner <tobias@strongswan.org>
Tue, 5 Nov 2013 17:29:40 +0000 (18:29 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 27 Nov 2013 17:35:44 +0000 (18:35 +0100)
Due to problems with the external libraries tkm_init/deinit can't be
called for each test case.  Because of this leak detective has to be
disabled for these tests.

12 files changed:
src/charon-tkm/Makefile.am
src/charon-tkm/build_tests.gpr
src/charon-tkm/tests/chunk_map_tests.c
src/charon-tkm/tests/diffie_hellman_tests.c
src/charon-tkm/tests/id_manager_tests.c
src/charon-tkm/tests/kernel_sad_tests.c
src/charon-tkm/tests/keymat_tests.c
src/charon-tkm/tests/nonceg_tests.c
src/charon-tkm/tests/test_runner.c [deleted file]
src/charon-tkm/tests/tests.c [new file with mode: 0644]
src/charon-tkm/tests/tests.h [moved from src/charon-tkm/tests/test_runner.h with 66% similarity]
src/charon-tkm/tests/utils_tests.c

index c3da7d7f4eca6a7e34928ad12f077df3967515b8..c06864a57141af7d58222b93f9fedf1659c591bf 100644 (file)
@@ -21,6 +21,10 @@ BUILD_OPTS = \
        -cargs $(AM_CPPFLAGS) $(DEFS) \
        -largs $(LIBLD) $(LIBFL)
 
+TEST_OPTS = \
+       -cargs -DBUILDDIR=\"${abs_top_builddir}\" \
+       -largs -L$(OBJ)/libstrongswan/tests/.libs -ltest
+
 # plugins to enable
 PLUGINS = \
        kernel-netlink \
@@ -35,11 +39,11 @@ build_charon: build_charon.gpr src/charon-tkm.c
        @$(GPRBUILD) -p $< $(BUILD_OPTS)
 
 build_tests: build_tests.gpr
-       @$(GPRBUILD) -p $< $(BUILD_OPTS) -cargs @CHECK_CFLAGS@ -largs @CHECK_LIBS@
+       @$(GPRBUILD) -p $< $(BUILD_OPTS) $(TEST_OPTS)
 
 if UNITTESTS
 check: build_tests
-       @LD_LIBRARY_PATH=$(LIBPT) obj/test_runner
+       @LD_LIBRARY_PATH=$(LIBPT) obj/tests
 else
 check:
        @echo "reconfigure with --enable-unit-tests"
index 032c7969e56f14e53da0d5ee88dbcca6315309b0..a1976984d533a95ebbf268cc4370756afff1c850 100644 (file)
@@ -4,7 +4,7 @@ project Build_Tests is
 
    for Languages use ("Ada", "C");
    for Source_Dirs use ("src/ees", "src/ehandler", "src/tkm", "tests");
-   for Main use ("test_runner");
+   for Main use ("tests");
    for Object_Dir use Build_Common.Obj_Dir;
 
    package Compiler is
index 6deef9a80573e5ad7e3fbf5baa6d45dd382e570f..1283a787ca87a4ba68c0c05406da2c06825c987e 100644 (file)
@@ -14,7 +14,7 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
 
 #include "tkm_chunk_map.h"
 
@@ -48,11 +48,20 @@ START_TEST(test_chunk_map_handling)
 }
 END_TEST
 
-TCase *make_chunk_map_tests(void)
+Suite *make_chunk_map_tests()
 {
-       TCase *tc = tcase_create("Chunk map tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("chunk map");
+
+       tc = tcase_create("creating");
        tcase_add_test(tc, test_chunk_map_creation);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("handling");
        tcase_add_test(tc, test_chunk_map_handling);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
index ffe99614d4ad121019a190321e288d22e520a387..b46f82a7ee1f9995ce81a842070db136d070fb13 100644 (file)
@@ -14,7 +14,7 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
 
 #include "tkm_diffie_hellman.h"
 
@@ -49,11 +49,20 @@ START_TEST(test_dh_get_my_pubvalue)
 }
 END_TEST
 
-TCase *make_diffie_hellman_tests(void)
+Suite *make_diffie_hellman_tests()
 {
-       TCase *tc = tcase_create("Diffie-Hellman tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("Diffie-Hellman");
+
+       tc = tcase_create("creation");
        tcase_add_test(tc, test_dh_creation);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("get_my_pubvalue");
        tcase_add_test(tc, test_dh_get_my_pubvalue);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
index 15522f1189ded1232eb52b081afda007a239f5d2..8157496ca1dfdda85e8099c0d6aadf725e302d91 100644 (file)
@@ -14,7 +14,7 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
 
 #include "tkm_id_manager.h"
 
@@ -135,16 +135,28 @@ START_TEST(test_release_id_nonexistent)
 }
 END_TEST
 
-TCase *make_id_manager_tests(void)
+Suite *make_id_manager_tests()
 {
-       TCase *tc = tcase_create("Context id manager tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("context id manager");
+
+       tc = tcase_create("creation");
        tcase_add_test(tc, test_id_mgr_creation);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("acquire");
        tcase_add_test(tc, test_acquire_id);
        tcase_add_test(tc, test_acquire_id_invalid_kind);
        tcase_add_test(tc, test_acquire_id_same);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("release");
        tcase_add_test(tc, test_release_id);
        tcase_add_test(tc, test_release_id_invalid_kind);
        tcase_add_test(tc, test_release_id_nonexistent);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
index 11785602de5974a3bf10cac8892c59987aade18d..6f0b396d3058218750283e5c326e851cad35bddc 100644 (file)
@@ -14,7 +14,7 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
 
 #include "tkm_kernel_sad.h"
 
@@ -107,16 +107,31 @@ START_TEST(test_remove_nonexistent)
 }
 END_TEST
 
-TCase *make_kernel_sad_tests(void)
+Suite *make_kernel_sad_tests()
 {
-       TCase *tc = tcase_create("Kernel SAD tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("kernel SAD tests");
+
+       tc = tcase_create("creation");
        tcase_add_test(tc, test_sad_creation);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("insert");
        tcase_add_test(tc, test_insert);
        tcase_add_test(tc, test_insert_duplicate);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("get_esa_id");
        tcase_add_test(tc, test_get_esa_id);
        tcase_add_test(tc, test_get_esa_id_nonexistent);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("remove");
        tcase_add_test(tc, test_remove);
        tcase_add_test(tc, test_remove_nonexistent);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
index 2a7525d4e62925cdbf15db1a24a027c7c6004609..1982671d3dd612ca5c411e00a34cd2b5c616c3a5 100644 (file)
@@ -14,7 +14,8 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
+
 #include <daemon.h>
 #include <hydra.h>
 #include <config/proposal.h>
@@ -139,11 +140,20 @@ START_TEST(test_derive_child_keys)
 }
 END_TEST
 
-TCase *make_keymat_tests(void)
+Suite *make_keymat_tests()
 {
-       TCase *tc = tcase_create("Keymat tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("keymat");
+
+       tc = tcase_create("derive IKE keys");
        tcase_add_test(tc, test_derive_ike_keys);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("derive CHILD keys");
        tcase_add_test(tc, test_derive_child_keys);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
index 3a1effab8e3d81bbaad34409dfd5eed2dcf53b36..6f524cb2249a11dc4ee3faefc857b43284bca5fc 100644 (file)
@@ -14,7 +14,8 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
+
 #include <tkm/client.h>
 
 #include "tkm.h"
@@ -82,12 +83,24 @@ START_TEST(test_nonceg_get_nonce)
 }
 END_TEST
 
-TCase *make_nonceg_tests(void)
+Suite *make_nonceg_tests()
 {
-       TCase *tc = tcase_create("Nonce generator tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("nonce generator");
+
+       tc = tcase_create("creation");
        tcase_add_test(tc, test_nonceg_creation);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("allocate");
        tcase_add_test(tc, test_nonceg_allocate_nonce);
+       suite_add_tcase(s, tc);
+
+       tc = tcase_create("get");
        tcase_add_test(tc, test_nonceg_get_nonce);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }
diff --git a/src/charon-tkm/tests/test_runner.c b/src/charon-tkm/tests/test_runner.c
deleted file mode 100644 (file)
index 5ae0329..0000000
+++ /dev/null
@@ -1,84 +0,0 @@
-/*
- * Copyright (C) 2012 Reto Buerki
- * Copyright (C) 2012 Adrian-Ken Rueegsegger
- * 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 <library.h>
-#include <hydra.h>
-#include <daemon.h>
-
-#include "tkm.h"
-#include "tkm_nonceg.h"
-#include "tkm_diffie_hellman.h"
-#include "tkm_kernel_ipsec.h"
-#include "test_runner.h"
-
-int main(void)
-{
-       library_init(NULL);
-       libhydra_init("test_runner");
-       libcharon_init("test_runner");
-
-       lib->settings->set_int(lib->settings, "test_runner.filelog.stdout.default",
-                                                  1);
-       charon->load_loggers(charon, NULL, FALSE);
-
-       /* Register TKM specific plugins */
-       static plugin_feature_t features[] = {
-               PLUGIN_REGISTER(NONCE_GEN, tkm_nonceg_create),
-                       PLUGIN_PROVIDE(NONCE_GEN),
-               PLUGIN_REGISTER(DH, tkm_diffie_hellman_create),
-                       PLUGIN_PROVIDE(DH, MODP_3072_BIT),
-                       PLUGIN_PROVIDE(DH, MODP_4096_BIT),
-               PLUGIN_CALLBACK(kernel_ipsec_register, tkm_kernel_ipsec_create),
-                       PLUGIN_PROVIDE(CUSTOM, "kernel-ipsec"),
-       };
-       lib->plugins->add_static_features(lib->plugins, "tkm-tests", features,
-                       countof(features), TRUE);
-
-       if (!charon->initialize(charon, PLUGINS))
-       {
-               fprintf(stderr, "Unable to init charon");
-               return EXIT_FAILURE;
-       }
-
-       if (!tkm_init())
-       {
-               fprintf(stderr, "Could not connect to TKM, aborting tests\n");
-               return EXIT_FAILURE;
-       }
-
-       int number_failed;
-       Suite *s = suite_create("TKM tests");
-       suite_add_tcase(s, make_id_manager_tests());
-       suite_add_tcase(s, make_chunk_map_tests());
-       suite_add_tcase(s, make_utility_tests());
-       suite_add_tcase(s, make_nonceg_tests());
-       suite_add_tcase(s, make_diffie_hellman_tests());
-       suite_add_tcase(s, make_keymat_tests());
-       suite_add_tcase(s, make_kernel_sad_tests());
-
-       SRunner *sr = srunner_create(s);
-
-       srunner_run_all(sr, CK_NORMAL);
-       number_failed = srunner_ntests_failed(sr);
-
-       tkm_deinit();
-       libcharon_deinit();
-       libhydra_deinit();
-       library_deinit();
-       srunner_free(sr);
-
-       return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
-}
diff --git a/src/charon-tkm/tests/tests.c b/src/charon-tkm/tests/tests.c
new file mode 100644 (file)
index 0000000..189966e
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * Copyright (C) 2013 Tobias Brunner
+ * Copyright (C) 2012 Reto Buerki
+ * Copyright (C) 2012 Adrian-Ken Rueegsegger
+ * 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 <tests/test_runner.h>
+
+#include <library.h>
+#include <hydra.h>
+#include <daemon.h>
+
+#include "tkm.h"
+#include "tkm_nonceg.h"
+#include "tkm_diffie_hellman.h"
+#include "tkm_kernel_ipsec.h"
+
+/* declare test suite constructors */
+#define TEST_SUITE(x) test_suite_t* x();
+#define TEST_SUITE_DEPEND(x, ...) TEST_SUITE(x)
+#include "tests.h"
+#undef TEST_SUITE
+#undef TEST_SUITE_DEPEND
+
+static test_configuration_t tests[] = {
+#define TEST_SUITE(x) \
+       { .suite = x, },
+#define TEST_SUITE_DEPEND(x, type, args) \
+       { .suite = x, .feature = PLUGIN_DEPENDS(type, args) },
+#include "tests.h"
+       { .suite = NULL, }
+};
+
+static bool tkm_initialized = false;
+
+static bool test_runner_init(bool init)
+{
+       bool result = TRUE;
+
+       if (init)
+       {
+               libhydra_init("test_runner");
+               libcharon_init("test_runner");
+               lib->settings->set_int(lib->settings,
+                                                          "test_runner.filelog.stdout.default", 0);
+               charon->load_loggers(charon, NULL, FALSE);
+
+               /* Register TKM specific plugins */
+               static plugin_feature_t features[] = {
+                       PLUGIN_REGISTER(NONCE_GEN, tkm_nonceg_create),
+                               PLUGIN_PROVIDE(NONCE_GEN),
+                       PLUGIN_REGISTER(DH, tkm_diffie_hellman_create),
+                               PLUGIN_PROVIDE(DH, MODP_3072_BIT),
+                               PLUGIN_PROVIDE(DH, MODP_4096_BIT),
+                       PLUGIN_CALLBACK(kernel_ipsec_register, tkm_kernel_ipsec_create),
+                               PLUGIN_PROVIDE(CUSTOM, "kernel-ipsec"),
+               };
+               lib->plugins->add_static_features(lib->plugins, "tkm-tests", features,
+                                                                                 countof(features), TRUE);
+
+               plugin_loader_add_plugindirs(BUILDDIR "/src/libstrongswan/plugins",
+                                                                        PLUGINS);
+               plugin_loader_add_plugindirs(BUILDDIR "/src/libhydra/plugins",
+                                                                        PLUGINS);
+               plugin_loader_add_plugindirs(BUILDDIR "/src/libcharon/plugins",
+                                                                        PLUGINS);
+               if (charon->initialize(charon, PLUGINS))
+               {
+                       if (!tkm_initialized)
+                       {
+                               if (!tkm_init())
+                               {
+                                       return FALSE;
+                               }
+                               tkm_initialized = true;
+                       }
+                       return TRUE;
+               }
+               result = FALSE;
+       }
+
+       libcharon_deinit();
+       libhydra_deinit();
+       return result;
+}
+
+int main(int argc, char *argv[])
+{
+       bool result;
+
+       /* disable leak detective because of how tkm_init/deinit is called, which
+        * does not work otherwise due to limitations of the external libraries */
+       setenv("LEAK_DETECTIVE_DISABLE", "1", 1);
+
+       result = test_runner_run(tests, test_runner_init);
+       tkm_deinit();
+
+       return result;
+}
similarity index 66%
rename from src/charon-tkm/tests/test_runner.h
rename to src/charon-tkm/tests/tests.h
index 236a7f2a6a2b59c25afa917417b56622a7bf3792..fb5e96a9c1afbb2e254dce5b536fd50b2152e6f6 100644 (file)
  * for more details.
  */
 
-#ifndef TEST_RUNNER_H_
-#define TEST_RUNNER_H_
-
-#include <check.h>
-
-TCase *make_id_manager_tests(void);
-TCase *make_chunk_map_tests(void);
-TCase *make_utility_tests(void);
-TCase *make_nonceg_tests(void);
-TCase *make_diffie_hellman_tests(void);
-TCase *make_keymat_tests(void);
-TCase *make_kernel_sad_tests(void);
-
-#endif /** TEST_RUNNER_H_ */
+TEST_SUITE(make_id_manager_tests)
+TEST_SUITE(make_chunk_map_tests)
+TEST_SUITE(make_utility_tests)
+TEST_SUITE(make_nonceg_tests)
+TEST_SUITE(make_diffie_hellman_tests)
+TEST_SUITE(make_keymat_tests)
+TEST_SUITE(make_kernel_sad_tests)
index b3ead76332d1105e5ffb1bfdff515ef60add3a8c..0a4d6fbd27f32fb23e1ef25172ccc149efd02d63 100644 (file)
@@ -14,7 +14,8 @@
  * for more details.
  */
 
-#include <check.h>
+#include <tests/test_suite.h>
+
 #include <tkm/types.h>
 
 #include "tkm_utils.h"
@@ -53,11 +54,17 @@ START_TEST(test_chunk_to_sequence)
 }
 END_TEST
 
-TCase *make_utility_tests(void)
+Suite *make_utility_tests()
 {
-       TCase *tc = tcase_create("Utility tests");
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("utility tests");
+
+       tc = tcase_create("chunk<->sequence");
        tcase_add_test(tc, test_sequence_to_chunk);
        tcase_add_test(tc, test_chunk_to_sequence);
+       suite_add_tcase(s, tc);
 
-       return tc;
+       return s;
 }