]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/utils/test.h
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libstrongswan / utils / test.h
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 /**
18 * @defgroup test test
19 * @{ @ingroup utils
20 */
21
22 #ifndef TEST_H_
23 #define TEST_H_
24
25 #include "collections/hashtable.h"
26
27 /**
28 * Register a (possibly static) function so that it can be called from tests.
29 *
30 * @param name name (namespace/function)
31 * @param fn function to register (set to NULL to unregister)
32 */
33 void testable_function_register(char *name, void *fn);
34
35 /**
36 * Find a previously registered testable function.
37 *
38 * @param name name (namespace/function)
39 * @return function, NULL if not found
40 */
41 void* testable_function_get(char *name);
42
43 /**
44 * Macro to automatically register/unregister a function that can be called
45 * from tests.
46 *
47 * @note The constructor has a priority set so that it runs after the
48 * constructor that creates the hashtable. The destructor, on the other hand,
49 * does not have a priority set, as test coverage would report that function as
50 * untested otherwise.
51 *
52 * @param ns namespace
53 * @param fn function to register
54 */
55 #define EXPORT_FUNCTION_FOR_TESTS(ns, fn) \
56 static void testable_function_register_##fn() __attribute__ ((constructor)); \
57 static void testable_function_register_##fn() \
58 { \
59 testable_function_register(#ns "/" #fn, fn); \
60 } \
61 static void testable_function_unregister_##fn() __attribute__ ((destructor)); \
62 static void testable_function_unregister_##fn() \
63 { \
64 testable_function_register(#ns "/" #fn, NULL); \
65 }
66
67 /**
68 * Import a registered function so that it can be called from tests.
69 *
70 * @param ns namespace of the function
71 * @param name name of the function
72 * @param ret return type of the function
73 * @param ... arguments of the function
74 */
75 #define IMPORT_FUNCTION_FOR_TESTS(ns, name, ret, ...) \
76 static ret (*TEST_##ns##name)(__VA_ARGS__);
77
78 /**
79 * Call a registered function from tests.
80 *
81 * @param ns namespace of the function
82 * @param name name of the function
83 * @param ... arguments for the function
84 */
85 #define TEST_FUNCTION(ns, name, ...) \
86 ({ \
87 TEST_##ns##name = testable_function_get( #ns "/" #name); \
88 if (!TEST_##ns##name) \
89 { \
90 test_fail_msg(__FILE__, __LINE__, "function " #name " (" #ns ") not found"); \
91 } \
92 TEST_##ns##name(__VA_ARGS__); \
93 })
94
95 #endif /** TEST_H_ @}*/