unit-tests: Use some include magic to define test suite constructors
authorMartin Willi <martin@revosec.ch>
Mon, 4 Nov 2013 15:41:22 +0000 (16:41 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:07 +0000 (10:31 +0100)
Avoid editing of several files when creating test suites by using a single
header file to define test suite constructor functions.

src/libstrongswan/tests/test_runner.c
src/libstrongswan/tests/test_runner.h

index 14db14a..3b2bdac 100644 (file)
@@ -15,7 +15,7 @@
  * for more details.
  */
 
-#include "test_runner.h"
+#include "test_suite.h"
 
 #include <library.h>
 #include <plugins/plugin_feature.h>
@@ -51,17 +51,12 @@ static bool load_plugins()
        return lib->plugins->load(lib->plugins, PLUGINS);
 }
 
-/**
- * Check if a specific feature is available, return falg if so
- */
-static int check_feature(plugin_feature_t feature, int flag)
-{
-       if (lib->plugins->has_feature(lib->plugins, feature))
-       {
-               return flag;
-       }
-       return 0;
-}
+/* 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
@@ -69,10 +64,18 @@ static int check_feature(plugin_feature_t feature, int flag)
 static array_t *load_suites()
 {
        array_t *suites;
-       enum {
-               OTEST_RSA = (1<<0),
-               OTEST_ECDSA = (1<<1),
-       } otest = 0;
+       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;
 
        library_init(NULL);
 
@@ -85,43 +88,29 @@ static array_t *load_suites()
        }
        lib->plugins->status(lib->plugins, LEVEL_CTRL);
 
-       /* we have to build the test suite array without leak detective, so
-        * separate plugin checks and suite creation */
-       otest |= check_feature(PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA), OTEST_RSA);
-       otest |= check_feature(PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_ECDSA), OTEST_ECDSA);
-
-       library_deinit();
+       if (lib->leak_detective)
+       {
+               old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
+       }
 
        suites = array_create(0, 0);
 
-       array_insert(suites, -1, bio_reader_suite_create());
-       array_insert(suites, -1, bio_writer_suite_create());
-       array_insert(suites, -1, chunk_suite_create());
-       array_insert(suites, -1, enum_suite_create());
-       array_insert(suites, -1, enumerator_suite_create());
-       array_insert(suites, -1, linked_list_suite_create());
-       array_insert(suites, -1, linked_list_enumerator_suite_create());
-       array_insert(suites, -1, hashtable_suite_create());
-       array_insert(suites, -1, array_suite_create());
-       array_insert(suites, -1, identification_suite_create());
-       array_insert(suites, -1, threading_suite_create());
-       array_insert(suites, -1, watcher_suite_create());
-       array_insert(suites, -1, stream_suite_create());
-       array_insert(suites, -1, utils_suite_create());
-       array_insert(suites, -1, host_suite_create());
-       array_insert(suites, -1, vectors_suite_create());
-       array_insert(suites, -1, pen_suite_create());
-       array_insert(suites, -1, asn1_suite_create());
-       array_insert(suites, -1, printf_suite_create());
-       if (otest & OTEST_RSA)
+       for (i = 0; i < countof(constructors); i++)
        {
-               array_insert(suites, -1, rsa_suite_create());
+               if (constructors[i].feature.type == 0 ||
+                       lib->plugins->has_feature(lib->plugins, constructors[i].feature))
+               {
+                       array_insert(suites, -1, constructors[i].suite());
+               }
        }
-       if (otest & OTEST_ECDSA)
+
+       if (lib->leak_detective)
        {
-               array_insert(suites, -1, ecdsa_suite_create());
+               lib->leak_detective->set_state(lib->leak_detective, old);
        }
 
+       library_deinit();
+
        return suites;
 }
 
index 4358836..6316692 100644 (file)
  * for more details.
  */
 
-#ifndef TEST_RUNNER_H_
-#define TEST_RUNNER_H_
-
-#include <test_suite.h>
-
-Suite *bio_reader_suite_create();
-Suite *bio_writer_suite_create();
-Suite *chunk_suite_create();
-Suite *enum_suite_create();
-Suite *enumerator_suite_create();
-Suite *linked_list_suite_create();
-Suite *linked_list_enumerator_suite_create();
-Suite *hashtable_suite_create();
-Suite *array_suite_create();
-Suite *identification_suite_create();
-Suite *threading_suite_create();
-Suite *watcher_suite_create();
-Suite *stream_suite_create();
-Suite *utils_suite_create();
-Suite *vectors_suite_create();
-Suite *ecdsa_suite_create();
-Suite *rsa_suite_create();
-Suite *host_suite_create();
-Suite *printf_suite_create();
-Suite *pen_suite_create();
-Suite *asn1_suite_create();
-
-#endif /** TEST_RUNNER_H_ */
+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)