diff --git a/src/libstrongswan/tests/suites/test_asn1.c b/src/libstrongswan/tests/suites/test_asn1.c index d0cd7e6e4..ac7c5519e 100644 --- a/src/libstrongswan/tests/suites/test_asn1.c +++ b/src/libstrongswan/tests/suites/test_asn1.c @@ -335,8 +335,8 @@ START_TEST(test_asn1_length) /* largest chunk on 32 bit system */ a = chunk_from_chars(0x04, 0x84, 0xff, 0xff, 0xff, 0xf9, 0xaa); - a.len = 4294967295; - ck_assert(asn1_length(&a) == 4294967289); + a.len = 4294967295U; + ck_assert(asn1_length(&a) == 4294967289U); } END_TEST diff --git a/src/libstrongswan/tests/test_runner.c b/src/libstrongswan/tests/test_runner.c index 5ec4198e7..1f11050f4 100644 --- a/src/libstrongswan/tests/test_runner.c +++ b/src/libstrongswan/tests/test_runner.c @@ -26,6 +26,7 @@ #include #include #include +#include /** * Get a tty color escape character for stderr @@ -114,13 +115,13 @@ static void filter_suites(array_t *loaded) * Load all available test suites, or optionally only selected ones. */ static array_t *load_suites(test_configuration_t configs[], - test_runner_init_t init) + test_runner_init_t init, char *cfg) { array_t *suites; bool old = FALSE; int i; - library_init(NULL, "test-runner"); + library_init(cfg, "test-runner"); test_setup_handler(); @@ -226,12 +227,12 @@ static bool call_fixture(test_case_t *tcase, bool up) /** * Test initialization, initializes libstrongswan for the next run */ -static bool pre_test(test_runner_init_t init) +static bool pre_test(test_runner_init_t init, char *cfg) { level_t level = LEVEL_SILENT; char *verbosity; - library_init(NULL, "test-runner"); + library_init(cfg, "test-runner"); /* use non-blocking RNG to generate keys fast */ lib->settings->set_default_str(lib->settings, @@ -395,7 +396,7 @@ static void print_failures(array_t *failures) /** * Run a single test case with fixtures */ -static bool run_case(test_case_t *tcase, test_runner_init_t init) +static bool run_case(test_case_t *tcase, test_runner_init_t init, char *cfg) { enumerator_t *enumerator; test_function_t *tfun; @@ -414,7 +415,7 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init) for (i = tfun->start; i < tfun->end; i++) { - if (pre_test(init)) + if (pre_test(init, cfg)) { bool ok = FALSE; int leaks = 0; @@ -483,7 +484,7 @@ static bool run_case(test_case_t *tcase, test_runner_init_t init) /** * Run a single test suite */ -static bool run_suite(test_suite_t *suite, test_runner_init_t init) +static bool run_suite(test_suite_t *suite, test_runner_init_t init, char *cfg) { enumerator_t *enumerator; test_case_t *tcase; @@ -494,7 +495,7 @@ static bool run_suite(test_suite_t *suite, test_runner_init_t init) enumerator = array_create_enumerator(suite->tcases); while (enumerator->enumerate(enumerator, &tcase)) { - if (run_case(tcase, init)) + if (run_case(tcase, init, cfg)) { passed++; } @@ -522,11 +523,14 @@ int test_runner_run(const char *name, test_configuration_t configs[], test_suite_t *suite; enumerator_t *enumerator; int passed = 0, result; + char *cfg; /* redirect all output to stderr (to redirect make's stdout to /dev/null) */ dup2(2, 1); - suites = load_suites(configs, init); + cfg = getenv("TESTS_STRONGSWAN_CONF"); + + suites = load_suites(configs, init, cfg); if (!suites) { return EXIT_FAILURE; @@ -537,7 +541,7 @@ int test_runner_run(const char *name, test_configuration_t configs[], enumerator = array_create_enumerator(suites); while (enumerator->enumerate(enumerator, &suite)) { - if (run_suite(suite, init)) + if (run_suite(suite, init, cfg)) { passed++; } diff --git a/src/libstrongswan/tests/test_runner.h b/src/libstrongswan/tests/test_runner.h index 643b622e5..de87a1f0f 100644 --- a/src/libstrongswan/tests/test_runner.h +++ b/src/libstrongswan/tests/test_runner.h @@ -64,7 +64,13 @@ struct test_configuration_t { /** * Run test configuration. * - * The configs array must be terminated with a NULL element. + * The configs array must be terminated with a NULL element. The following + * environment variables are currently supported: + * + * - TESTS_VERBOSITY: Numerical loglevel for debug log + * - TESTS_STRONGSWAN_CONF: Specify a path to a custom strongswan.conf + * - TESTS_SUITES: Run specific test suites only + * - TESTS_REDUCED_KEYLENGTHS: Test minimal keylengths for public key tests only * * @param name name of test runner * @param config test suite constructors with dependencies diff --git a/src/libstrongswan/tests/test_suite.h b/src/libstrongswan/tests/test_suite.h index c44f149f5..2b1a64c2d 100644 --- a/src/libstrongswan/tests/test_suite.h +++ b/src/libstrongswan/tests/test_suite.h @@ -269,6 +269,23 @@ void test_fail_msg(const char *file, int line, char *fmt, ...); } \ }) +/** + * Check if two chunks are equal, fail test if not + * + * @param a first chunk + * @param b second chunk + */ +#define test_chunk_eq(a, b) \ +({ \ + chunk_t _a = (chunk_t)a; \ + chunk_t _b = (chunk_t)b; \ + if (_a.len != _b.len || !memeq(a.ptr, b.ptr, a.len)) \ + { \ + test_fail_msg(__FILE__, __LINE__, \ + #a " != " #b " (\"%#B\" != \"%#B\")", &_a, &_b); \ + } \ +}) + /** * Check if a statement evaluates to TRUE, fail test if not * @@ -306,6 +323,7 @@ void test_fail_msg(const char *file, int line, char *fmt, ...); #define ck_assert test_assert #define ck_assert_msg test_assert_msg #define ck_assert_str_eq test_str_eq +#define ck_assert_chunk_eq test_chunk_eq #define fail(fmt, ...) test_fail_msg(__FILE__, __LINE__, fmt, ##__VA_ARGS__) #define fail_if(x, fmt, ...) \ ({ \ diff --git a/src/libstrongswan/tests/tests.c b/src/libstrongswan/tests/tests.c index 9f2adfd15..d95ddd9d5 100644 --- a/src/libstrongswan/tests/tests.c +++ b/src/libstrongswan/tests/tests.c @@ -35,8 +35,14 @@ static bool test_runner_init(bool init) { if (init) { - plugin_loader_add_plugindirs(PLUGINDIR, PLUGINS); - if (!lib->plugins->load(lib->plugins, PLUGINS)) + char *plugins, *plugindir; + + plugins = lib->settings->get_str(lib->settings, + "tests.load", PLUGINS); + plugindir = lib->settings->get_str(lib->settings, + "tests.plugindir", PLUGINDIR); + plugin_loader_add_plugindirs(plugindir, plugins); + if (!lib->plugins->load(lib->plugins, plugins)) { return FALSE; }