tests: Properly load plugins 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
33 enumerator = enumerator_create_token(PLUGINS, " ", "");
34 while (enumerator->enumerate(enumerator, &name))
35 {
36 snprintf(dir, sizeof(dir), "%s", name);
37 translate(dir, "-", "_");
38 snprintf(path, sizeof(path), "%s/%s/.libs", PLUGINDIR, dir);
39 lib->plugins->add_path(lib->plugins, path);
40 }
41 enumerator->destroy(enumerator);
42
43 return lib->plugins->load(lib->plugins, NULL, PLUGINS);
44 }
45
46 int main()
47 {
48 SRunner *sr;
49 int nf;
50
51 /* test cases are forked and there is no cleanup, so disable leak detective.
52 * if test_suite.h is included leak detective is enabled in test cases */
53 setenv("LEAK_DETECTIVE_DISABLE", "1", 1);
54 /* redirect all output to stderr (to redirect make's stdout to /dev/null) */
55 dup2(2, 1);
56
57 library_init(NULL);
58
59 /* use non-blocking RNG to generate keys fast */
60 lib->settings->set_default_str(lib->settings,
61 "libstrongswan.plugins.random.random",
62 lib->settings->get_str(lib->settings,
63 "libstrongswan.plugins.random.urandom", "/dev/urandom"));
64
65 if (!load_plugins())
66 {
67 library_deinit();
68 return EXIT_FAILURE;
69 }
70
71 sr = srunner_create(NULL);
72 srunner_add_suite(sr, bio_reader_suite_create());
73 srunner_add_suite(sr, bio_writer_suite_create());
74 srunner_add_suite(sr, chunk_suite_create());
75 srunner_add_suite(sr, enum_suite_create());
76 srunner_add_suite(sr, enumerator_suite_create());
77 srunner_add_suite(sr, linked_list_suite_create());
78 srunner_add_suite(sr, linked_list_enumerator_suite_create());
79 srunner_add_suite(sr, hashtable_suite_create());
80 srunner_add_suite(sr, identification_suite_create());
81 srunner_add_suite(sr, threading_suite_create());
82 srunner_add_suite(sr, utils_suite_create());
83 srunner_add_suite(sr, vectors_suite_create());
84 if (lib->plugins->has_feature(lib->plugins,
85 PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_RSA)))
86 {
87 srunner_add_suite(sr, rsa_suite_create());
88 }
89 if (lib->plugins->has_feature(lib->plugins,
90 PLUGIN_DEPENDS(PRIVKEY_GEN, KEY_ECDSA)))
91 {
92 srunner_add_suite(sr, ecdsa_suite_create());
93 }
94
95 srunner_run_all(sr, CK_NORMAL);
96 nf = srunner_ntests_failed(sr);
97
98 srunner_free(sr);
99 library_deinit();
100
101 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
102 }