]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/utils/test.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libstrongswan / utils / test.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "test.h"
18
19 #include <library.h>
20
21 /**
22 * A collection of testable functions
23 */
24 static hashtable_t *functions = NULL;
25
26 #ifndef WIN32
27 bool test_runner_available __attribute__((weak));
28 #endif
29
30 /**
31 * Check if we have libtest linkage and need testable functions
32 */
33 static bool has_libtest_linkage()
34 {
35 #ifdef WIN32
36 return dlsym(RTLD_DEFAULT, "test_runner_available");
37 #else
38 return test_runner_available;
39 #endif
40 }
41
42 /*
43 * Described in header.
44 */
45 void testable_function_register(char *name, void *fn)
46 {
47 bool old = FALSE;
48
49 if (lib && lib->leak_detective)
50 {
51 old = lib->leak_detective->set_state(lib->leak_detective, FALSE);
52 }
53
54 if (has_libtest_linkage())
55 {
56 if (!functions)
57 {
58 chunk_hash_seed();
59 functions = hashtable_create(hashtable_hash_str,
60 hashtable_equals_str, 8);
61 }
62 if (fn)
63 {
64 functions->put(functions, name, fn);
65 }
66 else
67 {
68 functions->remove(functions, name);
69 if (functions->get_count(functions) == 0)
70 {
71 functions->destroy(functions);
72 functions = NULL;
73 }
74 }
75 }
76
77 if (lib && lib->leak_detective)
78 {
79 lib->leak_detective->set_state(lib->leak_detective, old);
80 }
81 }
82
83 /*
84 * Described in header.
85 */
86 void* testable_function_get(char *name)
87 {
88 if (functions)
89 {
90 return functions->get(functions, name);
91 }
92 return NULL;
93 }