unit-tests: Separate test runner to a library, reusable by other tests
authorMartin Willi <martin@revosec.ch>
Tue, 5 Nov 2013 13:40:03 +0000 (14:40 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:07 +0000 (10:31 +0100)
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.

src/libstrongswan/tests/Makefile.am
src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_runner.h
src/libstrongswan/tests/tests.c [new file with mode: 0644]
src/libstrongswan/tests/tests.h [new file with mode: 0644]

index fd85ee2..d785f24 100644 (file)
@@ -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)
 
-test_runner_SOURCES = \
-  test_runner.c test_runner.h test_suite.c test_suite.h \
+tests_SOURCES = tests.h tests.c \
   suites/test_linked_list.c \
   suites/test_enumerator.c \
   suites/test_linked_list_enumerator.c \
@@ -26,13 +41,14 @@ test_runner_SOURCES = \
   suites/test_asn1.c \
   suites/test_printf.c
 
-test_runner_CFLAGS = \
+tests_CFLAGS = \
   -I$(top_srcdir)/src/libstrongswan \
+  -I$(top_srcdir)/src/libstrongswan/tests \
   -DPLUGINDIR=\""$(top_builddir)/src/libstrongswan/plugins\"" \
   -DPLUGINS=\""${s_plugins}\"" \
   @COVERAGE_CFLAGS@
 
-test_runner_LDFLAGS = @COVERAGE_LDFLAGS@
-test_runner_LDADD = \
+tests_LDFLAGS = @COVERAGE_LDFLAGS@
+tests_LDADD = \
   $(top_builddir)/src/libstrongswan/libstrongswan.la \
-  $(PTHREADLIB)
+  libtest.la
index 3b2bdac..5aa65ba 100644 (file)
@@ -15,7 +15,7 @@
  * for more details.
  */
 
-#include "test_suite.h"
+#include "test_runner.h"
 
 #include <library.h>
 #include <plugins/plugin_feature.h>
 /**
  * Load plugins from builddir
  */
-static bool load_plugins()
+static bool load_plugins(char *plugindirs[], char *plugins)
 {
        enumerator_t *enumerator;
        char *name, path[PATH_MAX], dir[64];
+       int i;
 
-       enumerator = enumerator_create_token(PLUGINS, " ", "");
+       enumerator = enumerator_create_token(plugins, " ", "");
        while (enumerator->enumerate(enumerator, &name))
        {
                snprintf(dir, sizeof(dir), "%s", name);
                translate(dir, "-", "_");
-               snprintf(path, sizeof(path), "%s/%s/.libs", PLUGINDIR, dir);
-               lib->plugins->add_path(lib->plugins, path);
+               for (i = 0; plugindirs[i]; i++)
+               {
+                       snprintf(path, sizeof(path), "%s/%s/.libs", plugindirs[i], dir);
+                       lib->plugins->add_path(lib->plugins, path);
+               }
        }
        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
  */
-static array_t *load_suites()
+static array_t *load_suites(test_configuration_t configs[],
+                                                       char *plugindirs[], char *plugins)
 {
        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;
        int i;
 
@@ -81,7 +69,7 @@ static array_t *load_suites()
 
        test_setup_handler();
 
-       if (!load_plugins())
+       if (!load_plugins(plugindirs, plugins))
        {
                library_deinit();
                return NULL;
@@ -95,12 +83,12 @@ static array_t *load_suites()
 
        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 ||
-                       lib->plugins->has_feature(lib->plugins, constructors[i].feature))
+               if (configs[i].feature.type == 0 ||
+                       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
  */
-static bool pre_test()
+static bool pre_test(char *plugindirs[], char *plugins)
 {
        library_init(NULL);
 
@@ -200,7 +188,7 @@ static bool pre_test()
                lib->leak_detective->set_report_cb(lib->leak_detective,
                                                                                   NULL, NULL, NULL);
        }
-       if (!load_plugins())
+       if (!load_plugins(plugindirs, plugins))
        {
                library_deinit();
                return FALSE;
@@ -333,7 +321,7 @@ static void print_failures(array_t *failures)
 /**
  * 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;
        test_function_t *tfun;
@@ -352,7 +340,7 @@ static bool run_case(test_case_t *tcase)
 
                for (i = tfun->start; i < tfun->end; i++)
                {
-                       if (pre_test())
+                       if (pre_test(plugindirs, plugins))
                        {
                                bool ok = FALSE, leaks = FALSE;
 
@@ -418,7 +406,7 @@ static bool run_case(test_case_t *tcase)
 /**
  * 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;
        test_case_t *tcase;
@@ -429,7 +417,7 @@ static bool run_suite(test_suite_t *suite)
        enumerator = array_create_enumerator(suite->tcases);
        while (enumerator->enumerate(enumerator, &tcase))
        {
-               if (run_case(tcase))
+               if (run_case(tcase, plugindirs, plugins))
                {
                        passed++;
                }
@@ -447,7 +435,11 @@ static bool run_suite(test_suite_t *suite)
        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;
        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) */
        dup2(2, 1);
 
-       suites = load_suites();
+       suites = load_suites(configs, plugindirs, plugins);
        if (!suites)
        {
                return EXIT_FAILURE;
@@ -468,7 +460,7 @@ int main(int argc, char *argv[])
        enumerator = array_create_enumerator(suites);
        while (enumerator->enumerate(enumerator, &suite))
        {
-               if (run_suite(suite))
+               if (run_suite(suite, plugindirs, plugins))
                {
                        passed++;
                }
index 6316692..86b6f18 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Copyright (C) 2013 Tobias Brunner
- * Hochschule fuer Technik Rapperswil
+ * 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
  * 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)
+#include "test_suite.h"
+
+#include <plugins/plugin_feature.h>
+
+typedef struct test_configuration_t test_configuration_t;
+
+/**
+ * Test configuration, suite constructor with plugin dependency
+ */
+struct test_configuration_t {
+
+       /**
+        * Constructor function to create suite.
+        */
+       test_suite_t *(*suite)();
+
+       /**
+        * Plugin feature this test suite depends on
+        */
+       plugin_feature_t feature;
+};
+
+/**
+ * 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);
diff --git a/src/libstrongswan/tests/tests.c b/src/libstrongswan/tests/tests.c
new file mode 100644 (file)
index 0000000..a32f384
--- /dev/null
@@ -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);
+}
diff --git a/src/libstrongswan/tests/tests.h b/src/libstrongswan/tests/tests.h
new file mode 100644 (file)
index 0000000..6316692
--- /dev/null
@@ -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)