unit-tests: Separate test runner to a library, reusable by other tests
Other users may make use of the noinst libtest.la helper library to implement unit tests. For libstrongswan, tests.[ch] provide the configuration for test runner to perform unit tests in a simple manner.
This commit is contained in:
@@ -1,9 +1,24 @@
|
|||||||
TESTS = test_runner
|
check_LTLIBRARIES = libtest.la
|
||||||
|
|
||||||
|
libtest_la_SOURCES = \
|
||||||
|
test_suite.c test_suite.h \
|
||||||
|
test_runner.c test_runner.h
|
||||||
|
|
||||||
|
libtest_la_CFLAGS = \
|
||||||
|
-I$(top_srcdir)/src/libstrongswan \
|
||||||
|
@COVERAGE_CFLAGS@
|
||||||
|
|
||||||
|
libtest_la_LDFLAGS = @COVERAGE_LDFLAGS@
|
||||||
|
libtest_la_LIBADD = \
|
||||||
|
$(top_builddir)/src/libstrongswan/libstrongswan.la \
|
||||||
|
$(PTHREADLIB)
|
||||||
|
|
||||||
|
|
||||||
|
TESTS = tests
|
||||||
|
|
||||||
check_PROGRAMS = $(TESTS)
|
check_PROGRAMS = $(TESTS)
|
||||||
|
|
||||||
test_runner_SOURCES = \
|
tests_SOURCES = tests.h tests.c \
|
||||||
test_runner.c test_runner.h test_suite.c test_suite.h \
|
|
||||||
suites/test_linked_list.c \
|
suites/test_linked_list.c \
|
||||||
suites/test_enumerator.c \
|
suites/test_enumerator.c \
|
||||||
suites/test_linked_list_enumerator.c \
|
suites/test_linked_list_enumerator.c \
|
||||||
@@ -26,13 +41,14 @@ test_runner_SOURCES = \
|
|||||||
suites/test_asn1.c \
|
suites/test_asn1.c \
|
||||||
suites/test_printf.c
|
suites/test_printf.c
|
||||||
|
|
||||||
test_runner_CFLAGS = \
|
tests_CFLAGS = \
|
||||||
-I$(top_srcdir)/src/libstrongswan \
|
-I$(top_srcdir)/src/libstrongswan \
|
||||||
|
-I$(top_srcdir)/src/libstrongswan/tests \
|
||||||
-DPLUGINDIR=\""$(top_builddir)/src/libstrongswan/plugins\"" \
|
-DPLUGINDIR=\""$(top_builddir)/src/libstrongswan/plugins\"" \
|
||||||
-DPLUGINS=\""${s_plugins}\"" \
|
-DPLUGINS=\""${s_plugins}\"" \
|
||||||
@COVERAGE_CFLAGS@
|
@COVERAGE_CFLAGS@
|
||||||
|
|
||||||
test_runner_LDFLAGS = @COVERAGE_LDFLAGS@
|
tests_LDFLAGS = @COVERAGE_LDFLAGS@
|
||||||
test_runner_LDADD = \
|
tests_LDADD = \
|
||||||
$(top_builddir)/src/libstrongswan/libstrongswan.la \
|
$(top_builddir)/src/libstrongswan/libstrongswan.la \
|
||||||
$(PTHREADLIB)
|
libtest.la
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
* for more details.
|
* for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "test_suite.h"
|
#include "test_runner.h"
|
||||||
|
|
||||||
#include <library.h>
|
#include <library.h>
|
||||||
#include <plugins/plugin_feature.h>
|
#include <plugins/plugin_feature.h>
|
||||||
@@ -33,47 +33,35 @@
|
|||||||
/**
|
/**
|
||||||
* Load plugins from builddir
|
* Load plugins from builddir
|
||||||
*/
|
*/
|
||||||
static bool load_plugins()
|
static bool load_plugins(char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
enumerator_t *enumerator;
|
enumerator_t *enumerator;
|
||||||
char *name, path[PATH_MAX], dir[64];
|
char *name, path[PATH_MAX], dir[64];
|
||||||
|
int i;
|
||||||
|
|
||||||
enumerator = enumerator_create_token(PLUGINS, " ", "");
|
enumerator = enumerator_create_token(plugins, " ", "");
|
||||||
while (enumerator->enumerate(enumerator, &name))
|
while (enumerator->enumerate(enumerator, &name))
|
||||||
{
|
{
|
||||||
snprintf(dir, sizeof(dir), "%s", name);
|
snprintf(dir, sizeof(dir), "%s", name);
|
||||||
translate(dir, "-", "_");
|
translate(dir, "-", "_");
|
||||||
snprintf(path, sizeof(path), "%s/%s/.libs", PLUGINDIR, dir);
|
for (i = 0; plugindirs[i]; i++)
|
||||||
lib->plugins->add_path(lib->plugins, path);
|
{
|
||||||
|
snprintf(path, sizeof(path), "%s/%s/.libs", plugindirs[i], dir);
|
||||||
|
lib->plugins->add_path(lib->plugins, path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
enumerator->destroy(enumerator);
|
enumerator->destroy(enumerator);
|
||||||
|
|
||||||
return lib->plugins->load(lib->plugins, PLUGINS);
|
return lib->plugins->load(lib->plugins, plugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* declare test suite constructors */
|
|
||||||
#define TEST_SUITE(x) test_suite_t* x();
|
|
||||||
#define TEST_SUITE_DEPEND(x, ...) TEST_SUITE(x)
|
|
||||||
#include "test_runner.h"
|
|
||||||
#undef TEST_SUITE
|
|
||||||
#undef TEST_SUITE_DEPEND
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all available test suites
|
* Load all available test suites
|
||||||
*/
|
*/
|
||||||
static array_t *load_suites()
|
static array_t *load_suites(test_configuration_t configs[],
|
||||||
|
char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
array_t *suites;
|
array_t *suites;
|
||||||
struct {
|
|
||||||
test_suite_t *(*suite)();
|
|
||||||
plugin_feature_t feature;
|
|
||||||
} constructors[] = {
|
|
||||||
#define TEST_SUITE(x) \
|
|
||||||
{ .suite = x, },
|
|
||||||
#define TEST_SUITE_DEPEND(x, type, args) \
|
|
||||||
{ .suite = x, .feature = PLUGIN_DEPENDS(type, args) },
|
|
||||||
#include "test_runner.h"
|
|
||||||
};
|
|
||||||
bool old = FALSE;
|
bool old = FALSE;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -81,7 +69,7 @@ static array_t *load_suites()
|
|||||||
|
|
||||||
test_setup_handler();
|
test_setup_handler();
|
||||||
|
|
||||||
if (!load_plugins())
|
if (!load_plugins(plugindirs, plugins))
|
||||||
{
|
{
|
||||||
library_deinit();
|
library_deinit();
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -95,12 +83,12 @@ static array_t *load_suites()
|
|||||||
|
|
||||||
suites = array_create(0, 0);
|
suites = array_create(0, 0);
|
||||||
|
|
||||||
for (i = 0; i < countof(constructors); i++)
|
for (i = 0; configs[i].suite; i++)
|
||||||
{
|
{
|
||||||
if (constructors[i].feature.type == 0 ||
|
if (configs[i].feature.type == 0 ||
|
||||||
lib->plugins->has_feature(lib->plugins, constructors[i].feature))
|
lib->plugins->has_feature(lib->plugins, configs[i].feature))
|
||||||
{
|
{
|
||||||
array_insert(suites, -1, constructors[i].suite());
|
array_insert(suites, -1, configs[i].suite());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +172,7 @@ 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()
|
static bool pre_test(char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
library_init(NULL);
|
library_init(NULL);
|
||||||
|
|
||||||
@@ -200,7 +188,7 @@ static bool pre_test()
|
|||||||
lib->leak_detective->set_report_cb(lib->leak_detective,
|
lib->leak_detective->set_report_cb(lib->leak_detective,
|
||||||
NULL, NULL, NULL);
|
NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
if (!load_plugins())
|
if (!load_plugins(plugindirs, plugins))
|
||||||
{
|
{
|
||||||
library_deinit();
|
library_deinit();
|
||||||
return FALSE;
|
return FALSE;
|
||||||
@@ -333,7 +321,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)
|
static bool run_case(test_case_t *tcase, char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
enumerator_t *enumerator;
|
enumerator_t *enumerator;
|
||||||
test_function_t *tfun;
|
test_function_t *tfun;
|
||||||
@@ -352,7 +340,7 @@ static bool run_case(test_case_t *tcase)
|
|||||||
|
|
||||||
for (i = tfun->start; i < tfun->end; i++)
|
for (i = tfun->start; i < tfun->end; i++)
|
||||||
{
|
{
|
||||||
if (pre_test())
|
if (pre_test(plugindirs, plugins))
|
||||||
{
|
{
|
||||||
bool ok = FALSE, leaks = FALSE;
|
bool ok = FALSE, leaks = FALSE;
|
||||||
|
|
||||||
@@ -418,7 +406,7 @@ static bool run_case(test_case_t *tcase)
|
|||||||
/**
|
/**
|
||||||
* Run a single test suite
|
* Run a single test suite
|
||||||
*/
|
*/
|
||||||
static bool run_suite(test_suite_t *suite)
|
static bool run_suite(test_suite_t *suite, char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
enumerator_t *enumerator;
|
enumerator_t *enumerator;
|
||||||
test_case_t *tcase;
|
test_case_t *tcase;
|
||||||
@@ -429,7 +417,7 @@ static bool run_suite(test_suite_t *suite)
|
|||||||
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))
|
if (run_case(tcase, plugindirs, plugins))
|
||||||
{
|
{
|
||||||
passed++;
|
passed++;
|
||||||
}
|
}
|
||||||
@@ -447,7 +435,11 @@ static bool run_suite(test_suite_t *suite)
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
/**
|
||||||
|
* See header.
|
||||||
|
*/
|
||||||
|
int test_runner_run(test_configuration_t configs[],
|
||||||
|
char *plugindirs[], char *plugins)
|
||||||
{
|
{
|
||||||
array_t *suites;
|
array_t *suites;
|
||||||
test_suite_t *suite;
|
test_suite_t *suite;
|
||||||
@@ -457,7 +449,7 @@ int main(int argc, char *argv[])
|
|||||||
/* 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();
|
suites = load_suites(configs, plugindirs, plugins);
|
||||||
if (!suites)
|
if (!suites)
|
||||||
{
|
{
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@@ -468,7 +460,7 @@ int main(int argc, char *argv[])
|
|||||||
enumerator = array_create_enumerator(suites);
|
enumerator = array_create_enumerator(suites);
|
||||||
while (enumerator->enumerate(enumerator, &suite))
|
while (enumerator->enumerate(enumerator, &suite))
|
||||||
{
|
{
|
||||||
if (run_suite(suite))
|
if (run_suite(suite, plugindirs, plugins))
|
||||||
{
|
{
|
||||||
passed++;
|
passed++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2013 Tobias Brunner
|
* Copyright (C) 2013 Martin Willi
|
||||||
* Hochschule fuer Technik Rapperswil
|
* Copyright (C) 2013 revosec AG
|
||||||
*
|
*
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
* 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
|
* under the terms of the GNU General Public License as published by the
|
||||||
@@ -13,24 +13,38 @@
|
|||||||
* for more details.
|
* for more details.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TEST_SUITE(bio_reader_suite_create)
|
#include "test_suite.h"
|
||||||
TEST_SUITE(bio_writer_suite_create)
|
|
||||||
TEST_SUITE(chunk_suite_create)
|
#include <plugins/plugin_feature.h>
|
||||||
TEST_SUITE(enum_suite_create)
|
|
||||||
TEST_SUITE(enumerator_suite_create)
|
typedef struct test_configuration_t test_configuration_t;
|
||||||
TEST_SUITE(linked_list_suite_create)
|
|
||||||
TEST_SUITE(linked_list_enumerator_suite_create)
|
/**
|
||||||
TEST_SUITE(hashtable_suite_create)
|
* Test configuration, suite constructor with plugin dependency
|
||||||
TEST_SUITE(array_suite_create)
|
*/
|
||||||
TEST_SUITE(identification_suite_create)
|
struct test_configuration_t {
|
||||||
TEST_SUITE(threading_suite_create)
|
|
||||||
TEST_SUITE(watcher_suite_create)
|
/**
|
||||||
TEST_SUITE(stream_suite_create)
|
* Constructor function to create suite.
|
||||||
TEST_SUITE(utils_suite_create)
|
*/
|
||||||
TEST_SUITE(vectors_suite_create)
|
test_suite_t *(*suite)();
|
||||||
TEST_SUITE_DEPEND(ecdsa_suite_create, PRIVKEY_GEN, KEY_ECDSA)
|
|
||||||
TEST_SUITE_DEPEND(rsa_suite_create, PRIVKEY_GEN, KEY_RSA)
|
/**
|
||||||
TEST_SUITE(host_suite_create)
|
* Plugin feature this test suite depends on
|
||||||
TEST_SUITE(printf_suite_create)
|
*/
|
||||||
TEST_SUITE(pen_suite_create)
|
plugin_feature_t feature;
|
||||||
TEST_SUITE(asn1_suite_create)
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run test configuration, loading plugins from plugin base directory.
|
||||||
|
*
|
||||||
|
* Both the configs and the plugindirs array must be terminated with a NULL
|
||||||
|
* element.
|
||||||
|
*
|
||||||
|
* @param configs test suite constructors with dependencies
|
||||||
|
* @param plugindirs base directories containing plugin directories to load
|
||||||
|
* @param plugins plugin names to load, space separated
|
||||||
|
* @return test result, EXIT_SUCCESS if all tests passed
|
||||||
|
*/
|
||||||
|
int test_runner_run(test_configuration_t config[],
|
||||||
|
char *plugindirs[], char *plugins);
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Martin Willi
|
||||||
|
* Copyright (C) 2013 revosec AG
|
||||||
|
*
|
||||||
|
* 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 <test_runner.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 char *plugindirs[] = {
|
||||||
|
PLUGINDIR,
|
||||||
|
NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
return test_runner_run(tests, plugindirs, PLUGINS);
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2013 Tobias Brunner
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
TEST_SUITE(bio_reader_suite_create)
|
||||||
|
TEST_SUITE(bio_writer_suite_create)
|
||||||
|
TEST_SUITE(chunk_suite_create)
|
||||||
|
TEST_SUITE(enum_suite_create)
|
||||||
|
TEST_SUITE(enumerator_suite_create)
|
||||||
|
TEST_SUITE(linked_list_suite_create)
|
||||||
|
TEST_SUITE(linked_list_enumerator_suite_create)
|
||||||
|
TEST_SUITE(hashtable_suite_create)
|
||||||
|
TEST_SUITE(array_suite_create)
|
||||||
|
TEST_SUITE(identification_suite_create)
|
||||||
|
TEST_SUITE(threading_suite_create)
|
||||||
|
TEST_SUITE(watcher_suite_create)
|
||||||
|
TEST_SUITE(stream_suite_create)
|
||||||
|
TEST_SUITE(utils_suite_create)
|
||||||
|
TEST_SUITE(vectors_suite_create)
|
||||||
|
TEST_SUITE_DEPEND(ecdsa_suite_create, PRIVKEY_GEN, KEY_ECDSA)
|
||||||
|
TEST_SUITE_DEPEND(rsa_suite_create, PRIVKEY_GEN, KEY_RSA)
|
||||||
|
TEST_SUITE(host_suite_create)
|
||||||
|
TEST_SUITE(printf_suite_create)
|
||||||
|
TEST_SUITE(pen_suite_create)
|
||||||
|
TEST_SUITE(asn1_suite_create)
|
||||||
Reference in New Issue
Block a user