Merge branch 'unit-tests'

Bring some minor improvements to unit testing, including more flexible
configuration.
This commit is contained in:
Martin Willi
2014-04-30 17:23:55 +02:00
5 changed files with 49 additions and 15 deletions
+2 -2
View File
@@ -335,8 +335,8 @@ START_TEST(test_asn1_length)
/* largest chunk on 32 bit system */ /* largest chunk on 32 bit system */
a = chunk_from_chars(0x04, 0x84, 0xff, 0xff, 0xff, 0xf9, 0xaa); a = chunk_from_chars(0x04, 0x84, 0xff, 0xff, 0xff, 0xf9, 0xaa);
a.len = 4294967295; a.len = 4294967295U;
ck_assert(asn1_length(&a) == 4294967289); ck_assert(asn1_length(&a) == 4294967289U);
} }
END_TEST END_TEST
+14 -10
View File
@@ -26,6 +26,7 @@
#include <dirent.h> #include <dirent.h>
#include <unistd.h> #include <unistd.h>
#include <limits.h> #include <limits.h>
#include <stdlib.h>
/** /**
* Get a tty color escape character for stderr * 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. * Load all available test suites, or optionally only selected ones.
*/ */
static array_t *load_suites(test_configuration_t configs[], static array_t *load_suites(test_configuration_t configs[],
test_runner_init_t init) test_runner_init_t init, char *cfg)
{ {
array_t *suites; array_t *suites;
bool old = FALSE; bool old = FALSE;
int i; int i;
library_init(NULL, "test-runner"); library_init(cfg, "test-runner");
test_setup_handler(); 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 * 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; level_t level = LEVEL_SILENT;
char *verbosity; char *verbosity;
library_init(NULL, "test-runner"); library_init(cfg, "test-runner");
/* use non-blocking RNG to generate keys fast */ /* use non-blocking RNG to generate keys fast */
lib->settings->set_default_str(lib->settings, 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 * 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; enumerator_t *enumerator;
test_function_t *tfun; 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++) for (i = tfun->start; i < tfun->end; i++)
{ {
if (pre_test(init)) if (pre_test(init, cfg))
{ {
bool ok = FALSE; bool ok = FALSE;
int leaks = 0; 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 * 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; enumerator_t *enumerator;
test_case_t *tcase; 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); enumerator = array_create_enumerator(suite->tcases);
while (enumerator->enumerate(enumerator, &tcase)) while (enumerator->enumerate(enumerator, &tcase))
{ {
if (run_case(tcase, init)) if (run_case(tcase, init, cfg))
{ {
passed++; passed++;
} }
@@ -522,11 +523,14 @@ int test_runner_run(const char *name, test_configuration_t configs[],
test_suite_t *suite; test_suite_t *suite;
enumerator_t *enumerator; enumerator_t *enumerator;
int passed = 0, result; int passed = 0, result;
char *cfg;
/* redirect all output to stderr (to redirect make's stdout to /dev/null) */ /* redirect all output to stderr (to redirect make's stdout to /dev/null) */
dup2(2, 1); dup2(2, 1);
suites = load_suites(configs, init); cfg = getenv("TESTS_STRONGSWAN_CONF");
suites = load_suites(configs, init, cfg);
if (!suites) if (!suites)
{ {
return EXIT_FAILURE; return EXIT_FAILURE;
@@ -537,7 +541,7 @@ int test_runner_run(const char *name, test_configuration_t configs[],
enumerator = array_create_enumerator(suites); enumerator = array_create_enumerator(suites);
while (enumerator->enumerate(enumerator, &suite)) while (enumerator->enumerate(enumerator, &suite))
{ {
if (run_suite(suite, init)) if (run_suite(suite, init, cfg))
{ {
passed++; passed++;
} }
+7 -1
View File
@@ -64,7 +64,13 @@ struct test_configuration_t {
/** /**
* Run test configuration. * 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 name name of test runner
* @param config test suite constructors with dependencies * @param config test suite constructors with dependencies
+18
View File
@@ -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 * 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 test_assert
#define ck_assert_msg test_assert_msg #define ck_assert_msg test_assert_msg
#define ck_assert_str_eq test_str_eq #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(fmt, ...) test_fail_msg(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define fail_if(x, fmt, ...) \ #define fail_if(x, fmt, ...) \
({ \ ({ \
+8 -2
View File
@@ -35,8 +35,14 @@ static bool test_runner_init(bool init)
{ {
if (init) if (init)
{ {
plugin_loader_add_plugindirs(PLUGINDIR, PLUGINS); char *plugins, *plugindir;
if (!lib->plugins->load(lib->plugins, PLUGINS))
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; return FALSE;
} }