unit-tests: load plugins in test-runner from build directory
[strongswan.git] / src / libstrongswan / tests / test_runner.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <unistd.h>
17
18 #include "test_runner.h"
19
20 #include <library.h>
21 #include <plugins/plugin_feature.h>
22
23 #include <dirent.h>
24
25 /**
26 * Load plugins from builddir
27 */
28 static bool load_plugins()
29 {
30 enumerator_t *enumerator;
31 char *name, path[PATH_MAX], dir[64];
32 bool success = TRUE;
33
34 enumerator = enumerator_create_token(PLUGINS, " ", "");
35 while (enumerator->enumerate(enumerator, &name))
36 {
37 snprintf(dir, sizeof(dir), "%s", name);
38 translate(dir, "-", "_");
39 snprintf(path, sizeof(path), "%s/%s/.libs", PLUGINDIR, dir);
40 if (!lib->plugins->load(lib->plugins, path, name))
41 {
42 success = FALSE;
43 break;
44 }
45 }
46 enumerator->destroy(enumerator);
47
48 return success;
49 }
50
51 int main()
52 {
53 SRunner *sr;
54 int nf;
55
56 /* test cases are forked and there is no cleanup, so disable leak detective.
57 * if test_suite.h is included leak detective is enabled in test cases */
58 setenv("LEAK_DETECTIVE_DISABLE", "1", 1);
59 /* redirect all output to stderr (to redirect make's stdout to /dev/null) */
60 dup2(2, 1);
61
62 library_init(NULL);
63
64 /* use non-blocking RNG to generate keys fast */
65 lib->settings->set_default_str(lib->settings,
66 "libstrongswan.plugins.random.random",
67 lib->settings->get_str(lib->settings,
68 "libstrongswan.plugins.random.urandom", "/dev/urandom"));
69
70 if (!load_plugins())
71 {
72 library_deinit();
73 return EXIT_FAILURE;
74 }
75
76 sr = srunner_create(NULL);
77 srunner_add_suite(sr, bio_reader_suite_create());
78 srunner_add_suite(sr, bio_writer_suite_create());
79 srunner_add_suite(sr, chunk_suite_create());
80 srunner_add_suite(sr, enum_suite_create());
81 srunner_add_suite(sr, enumerator_suite_create());
82 srunner_add_suite(sr, linked_list_suite_create());
83 srunner_add_suite(sr, linked_list_enumerator_suite_create());
84 srunner_add_suite(sr, hashtable_suite_create());
85 srunner_add_suite(sr, identification_suite_create());
86 srunner_add_suite(sr, threading_suite_create());
87 srunner_add_suite(sr, utils_suite_create());
88 srunner_add_suite(sr, vectors_suite_create());
89 if (lib->plugins->has_feature(lib->plugins,
90 PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA)))
91 {
92 srunner_add_suite(sr, rsa_suite_create());
93 }
94 if (lib->plugins->has_feature(lib->plugins,
95 PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_ECDSA)))
96 {
97 srunner_add_suite(sr, ecdsa_suite_create());
98 }
99
100 srunner_run_all(sr, CK_NORMAL);
101 nf = srunner_ntests_failed(sr);
102
103 srunner_free(sr);
104 library_deinit();
105
106 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
107 }