From d6032bff8b8461aa7714fed61ba82bf63ca0b605 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Tue, 5 Nov 2013 18:29:40 +0100 Subject: [PATCH] charon-tkm: Migrate tests to our own test runner 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. --- src/charon-tkm/Makefile.am | 8 +- src/charon-tkm/build_tests.gpr | 2 +- src/charon-tkm/tests/chunk_map_tests.c | 21 +++- src/charon-tkm/tests/diffie_hellman_tests.c | 21 +++- src/charon-tkm/tests/id_manager_tests.c | 20 +++- src/charon-tkm/tests/kernel_sad_tests.c | 23 +++- src/charon-tkm/tests/keymat_tests.c | 22 +++- src/charon-tkm/tests/nonceg_tests.c | 27 +++-- src/charon-tkm/tests/test_runner.c | 84 ------------- src/charon-tkm/tests/tests.c | 110 ++++++++++++++++++ .../tests/{test_runner.h => tests.h} | 21 ++-- src/charon-tkm/tests/utils_tests.c | 15 ++- 12 files changed, 236 insertions(+), 138 deletions(-) delete mode 100644 src/charon-tkm/tests/test_runner.c create mode 100644 src/charon-tkm/tests/tests.c rename src/charon-tkm/tests/{test_runner.h => tests.h} (66%) diff --git a/src/charon-tkm/Makefile.am b/src/charon-tkm/Makefile.am index c3da7d7f4..c06864a57 100644 --- a/src/charon-tkm/Makefile.am +++ b/src/charon-tkm/Makefile.am @@ -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" diff --git a/src/charon-tkm/build_tests.gpr b/src/charon-tkm/build_tests.gpr index 032c7969e..a1976984d 100644 --- a/src/charon-tkm/build_tests.gpr +++ b/src/charon-tkm/build_tests.gpr @@ -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 diff --git a/src/charon-tkm/tests/chunk_map_tests.c b/src/charon-tkm/tests/chunk_map_tests.c index 6deef9a80..1283a787c 100644 --- a/src/charon-tkm/tests/chunk_map_tests.c +++ b/src/charon-tkm/tests/chunk_map_tests.c @@ -14,7 +14,7 @@ * for more details. */ -#include +#include #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"); - tcase_add_test(tc, test_chunk_map_creation); - tcase_add_test(tc, test_chunk_map_handling); + Suite *s; + TCase *tc; - return 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 s; } diff --git a/src/charon-tkm/tests/diffie_hellman_tests.c b/src/charon-tkm/tests/diffie_hellman_tests.c index ffe99614d..b46f82a7e 100644 --- a/src/charon-tkm/tests/diffie_hellman_tests.c +++ b/src/charon-tkm/tests/diffie_hellman_tests.c @@ -14,7 +14,7 @@ * for more details. */ -#include +#include #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"); - tcase_add_test(tc, test_dh_creation); - tcase_add_test(tc, test_dh_get_my_pubvalue); + Suite *s; + TCase *tc; - return 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 s; } diff --git a/src/charon-tkm/tests/id_manager_tests.c b/src/charon-tkm/tests/id_manager_tests.c index 15522f118..8157496ca 100644 --- a/src/charon-tkm/tests/id_manager_tests.c +++ b/src/charon-tkm/tests/id_manager_tests.c @@ -14,7 +14,7 @@ * for more details. */ -#include +#include #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; } diff --git a/src/charon-tkm/tests/kernel_sad_tests.c b/src/charon-tkm/tests/kernel_sad_tests.c index 11785602d..6f0b396d3 100644 --- a/src/charon-tkm/tests/kernel_sad_tests.c +++ b/src/charon-tkm/tests/kernel_sad_tests.c @@ -14,7 +14,7 @@ * for more details. */ -#include +#include #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; } diff --git a/src/charon-tkm/tests/keymat_tests.c b/src/charon-tkm/tests/keymat_tests.c index 2a7525d4e..1982671d3 100644 --- a/src/charon-tkm/tests/keymat_tests.c +++ b/src/charon-tkm/tests/keymat_tests.c @@ -14,7 +14,8 @@ * for more details. */ -#include +#include + #include #include #include @@ -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"); - tcase_add_test(tc, test_derive_ike_keys); - tcase_add_test(tc, test_derive_child_keys); + Suite *s; + TCase *tc; - return 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 s; } diff --git a/src/charon-tkm/tests/nonceg_tests.c b/src/charon-tkm/tests/nonceg_tests.c index 3a1effab8..6f524cb22 100644 --- a/src/charon-tkm/tests/nonceg_tests.c +++ b/src/charon-tkm/tests/nonceg_tests.c @@ -14,7 +14,8 @@ * for more details. */ -#include +#include + #include #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"); - tcase_add_test(tc, test_nonceg_creation); - tcase_add_test(tc, test_nonceg_allocate_nonce); - tcase_add_test(tc, test_nonceg_get_nonce); + Suite *s; + TCase *tc; - return 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 s; } diff --git a/src/charon-tkm/tests/test_runner.c b/src/charon-tkm/tests/test_runner.c deleted file mode 100644 index 5ae032935..000000000 --- a/src/charon-tkm/tests/test_runner.c +++ /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 . - * - * 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 -#include -#include - -#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 index 000000000..189966eae --- /dev/null +++ b/src/charon-tkm/tests/tests.c @@ -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 . + * + * 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 + +#include +#include +#include + +#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; +} diff --git a/src/charon-tkm/tests/test_runner.h b/src/charon-tkm/tests/tests.h similarity index 66% rename from src/charon-tkm/tests/test_runner.h rename to src/charon-tkm/tests/tests.h index 236a7f2a6..fb5e96a9c 100644 --- a/src/charon-tkm/tests/test_runner.h +++ b/src/charon-tkm/tests/tests.h @@ -14,17 +14,10 @@ * for more details. */ -#ifndef TEST_RUNNER_H_ -#define TEST_RUNNER_H_ - -#include - -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) diff --git a/src/charon-tkm/tests/utils_tests.c b/src/charon-tkm/tests/utils_tests.c index b3ead7633..0a4d6fbd2 100644 --- a/src/charon-tkm/tests/utils_tests.c +++ b/src/charon-tkm/tests/utils_tests.c @@ -14,7 +14,8 @@ * for more details. */ -#include +#include + #include #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; }