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.
This commit is contained in:
Tobias Brunner 2013-11-05 18:29:40 +01:00
parent 70f4461359
commit d6032bff8b
12 changed files with 236 additions and 138 deletions

View 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"

View 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

View 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");
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;
}

View 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");
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;
}

View 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;
}

View 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;
}

View 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");
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;
}

View 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");
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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -14,17 +14,10 @@
* 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)

View 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;
}