unit-tests: Don't assert failures for unreadable settings files as root
[strongswan.git] / src / libstrongswan / tests / test_suite.h
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 * Copyright (C) 2013 Martin Willi
5 * Copyright (C) 2013 revosec AG
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup test_suite test_suite
20 * @{ @ingroup libtest
21 */
22
23 #ifndef TEST_SUITE_H_
24 #define TEST_SUITE_H_
25
26 #define _GNU_SOURCE
27 #include <setjmp.h>
28
29 #include <library.h>
30 #include <utils/debug.h>
31 #include <utils/backtrace.h>
32 #include <collections/array.h>
33
34 typedef struct test_suite_t test_suite_t;
35 typedef struct test_case_t test_case_t;
36 typedef struct test_function_t test_function_t;
37 typedef struct test_fixture_t test_fixture_t;
38
39 /**
40 * Default timeout for a single test function
41 */
42 #define TEST_FUNCTION_DEFAULT_TIMEOUT 2
43
44 /**
45 * Test function implementation
46 */
47 typedef void (*test_function_cb_t)(int);
48
49 /**
50 * Fixture for a test case.
51 */
52 typedef void (*test_fixture_cb_t)(void);
53
54 /**
55 * A test suite; a collection of test cases with fixtures
56 */
57 struct test_suite_t {
58 /** name of the test suite */
59 const char *name;
60 /** test cases registered, as test_case_t* */
61 array_t *tcases;
62 };
63
64 /**
65 * A test case; multiple test functions using the same fixtures
66 */
67 struct test_case_t {
68 /** name of the test case */
69 const char *name;
70 /** tests registered, as test_function_t */
71 array_t *functions;
72 /** fixture for tests, as test_fixture_t */
73 array_t *fixtures;
74 /** timeout for each function, in s */
75 int timeout;
76 };
77
78 /**
79 * A test function, with optional loop setup
80 */
81 struct test_function_t {
82 /** name of test function */
83 char *name;
84 /** tests function registered, test_function_t* */
85 test_function_cb_t cb;
86 /** start for loop test */
87 int start;
88 /** end for loop test */
89 int end;
90 };
91
92 /**
93 * Registered fixture for a test case
94 */
95 struct test_fixture_t {
96 test_fixture_cb_t setup;
97 test_fixture_cb_t teardown;
98 };
99
100 /**
101 * Create a new test suite
102 *
103 * @param name name of the test suite
104 * @return test suite
105 */
106 test_suite_t* test_suite_create(const char *name);
107
108 /**
109 * Create a new test case
110 *
111 * @param name name of test case
112 * @return test case
113 */
114 test_case_t* test_case_create(const char *name);
115
116 /**
117 * Add a setup/teardown function to the test case
118 *
119 * @param tcase test case to add a fixture to
120 * @param setup setup function called before each test
121 * @param teardown cleanup function called after each test
122 */
123 void test_case_add_checked_fixture(test_case_t *tcase, test_fixture_cb_t setup,
124 test_fixture_cb_t teardown);
125
126 /**
127 * Add a test function to a test case, with a name, looped several times
128 *
129 * @param name name of the test case
130 * @param tcase test case to add test function to
131 * @param cb callback function to invoke for test
132 * @param start start of loop counter
133 * @param end end of loop counter
134 */
135 void test_case_add_test_name(test_case_t *tcase, char *name,
136 test_function_cb_t cb, int start, int end);
137
138 /**
139 * Add a test function to a test case
140 *
141 * @param tcase test case to add test function to
142 * @param cb callback function to invoke for test
143 */
144 #define test_case_add_test(tcase, cb) \
145 test_case_add_test_name(tcase, #cb, cb, 0, 1)
146
147 /**
148 * Add a test function to a test case, looped several times
149 *
150 * @param tcase test case to add test function to
151 * @param cb callback function to invoke for test
152 * @param start start of loop counter
153 * @param end end of loop counter
154 */
155 #define test_case_add_loop_test(tcase, cb, start, end) \
156 test_case_add_test_name(tcase, #cb, cb, start, end)
157
158 /**
159 * Set a custom timeout for test functions in a test case
160 *
161 * @param tcase test case to set timeout for
162 * @param s test timeout in s
163 */
164 void test_case_set_timeout(test_case_t *tcase, int s);
165
166 /**
167 * Add a test function to a test case, looped several times
168 *
169 * @param suite test suite to add test case to
170 * @param tcase test case to add
171 */
172 void test_suite_add_case(test_suite_t *suite, test_case_t *tcase);
173
174 /**
175 * sigjmp restore point used by test_restore_point
176 */
177 extern sigjmp_buf test_restore_point_env;
178
179 /**
180 * Set or return from an execution restore point
181 *
182 * This call sets a restore execution point and returns TRUE after it has
183 * been set up. On test failure, the execution is returned to the restore point
184 * and FALSE is returned to indicate test failure.
185 *
186 * @return TRUE if restore point set, FALSE when restored
187 */
188 #define test_restore_point() (sigsetjmp(test_restore_point_env, 1) == 0)
189
190 /**
191 * Set up signal handlers for test cases
192 */
193 void test_setup_handler();
194
195 /**
196 * Set up a timeout to let a test fail
197 *
198 * @param s timeout, 0 to disable timeout
199 */
200 void test_setup_timeout(int s);
201
202 /**
203 * Get info about a test failure
204 *
205 * @param msg buffer receiving failure info
206 * @param len size of msg buffer
207 * @param file pointer receiving source code file
208 * @return source code line number
209 */
210 int test_failure_get(char *msg, int len, const char **file);
211
212 /**
213 * Get a backtrace for a failure.
214 *
215 * @return allocated backtrace of test failure, if any
216 */
217 backtrace_t *test_failure_backtrace();
218
219 /**
220 * Let a test fail and set a message using vprintf style arguments.
221 *
222 * @param file source code file name
223 * @param line source code line number
224 * @param fmt printf format string
225 * @param args argument list for fmt
226 */
227 void test_fail_vmsg(const char *file, int line, char *fmt, va_list args);
228
229 /**
230 * Let a test fail and set a message using printf style arguments.
231 *
232 * @param file source code file name
233 * @param line source code line number
234 * @param fmt printf format string
235 * @param ... arguments for fmt
236 */
237 void test_fail_msg(const char *file, int line, char *fmt, ...);
238
239 /**
240 * Check if two integers equal, fail test if not
241 *
242 * @param a first integer
243 * @param b second integer
244 */
245 #define test_int_eq(a, b) \
246 ({ \
247 typeof(a) _a = a; \
248 typeof(b) _b = b; \
249 if (_a != _b) \
250 { \
251 test_fail_msg(__FILE__, __LINE__, #a " != " #b " (%d != %d)", _a, _b); \
252 } \
253 })
254
255 /**
256 * Check if two strings equal, fail test if not
257 *
258 * @param a first string
259 * @param b second string
260 */
261 #define test_str_eq(a, b) \
262 ({ \
263 char* _a = (char*)a; \
264 char* _b = (char*)b; \
265 if (!_a || !_b || !streq(_a, _b)) \
266 { \
267 test_fail_msg(__FILE__, __LINE__, \
268 #a " != " #b " (\"%s\" != \"%s\")", _a, _b); \
269 } \
270 })
271
272 /**
273 * Check if two chunks are equal, fail test if not
274 *
275 * @param a first chunk
276 * @param b second chunk
277 */
278 #define test_chunk_eq(a, b) \
279 ({ \
280 chunk_t _a = (chunk_t)a; \
281 chunk_t _b = (chunk_t)b; \
282 if (_a.len != _b.len || !memeq(a.ptr, b.ptr, a.len)) \
283 { \
284 test_fail_msg(__FILE__, __LINE__, \
285 #a " != " #b " (\"%#B\" != \"%#B\")", &_a, &_b); \
286 } \
287 })
288
289 /**
290 * Check if a statement evaluates to TRUE, fail test if not
291 *
292 * @param x statement to evaluate
293 */
294 #define test_assert(x) \
295 ({ \
296 if (!(x)) \
297 { \
298 test_fail_msg(__FILE__, __LINE__, #x); \
299 } \
300 })
301
302 /**
303 * Check if a statement evaluates to TRUE, fail and print a message if not
304 *
305 * @param x statement to evaluate
306 * @param fmt message format string
307 * @param ... fmt printf arguments
308 */
309 #define test_assert_msg(x, fmt, ...) \
310 ({ \
311 if (!(x)) \
312 { \
313 test_fail_msg(__FILE__, __LINE__, #x ": " fmt, ##__VA_ARGS__); \
314 } \
315 })
316
317
318
319 /* "check unit testing" compatibility */
320 #define Suite test_suite_t
321 #define TCase test_case_t
322 #define ck_assert_int_eq test_int_eq
323 #define ck_assert test_assert
324 #define ck_assert_msg test_assert_msg
325 #define ck_assert_str_eq test_str_eq
326 #define ck_assert_chunk_eq test_chunk_eq
327 #define fail(fmt, ...) test_fail_msg(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
328 #define fail_if(x, fmt, ...) \
329 ({ \
330 if (x) \
331 { \
332 test_fail_msg(__FILE__, __LINE__, #x ": " fmt, ##__VA_ARGS__); \
333 } \
334 })
335 #define fail_unless test_assert_msg
336 #define suite_create test_suite_create
337 #define tcase_create test_case_create
338 #define tcase_add_checked_fixture test_case_add_checked_fixture
339 #define tcase_add_test test_case_add_test
340 #define tcase_add_loop_test test_case_add_loop_test
341 #define tcase_set_timeout test_case_set_timeout
342 #define suite_add_tcase test_suite_add_case
343 #define START_TEST(name) static void name (int _i) {
344 #define END_TEST }
345 #define START_SETUP(name) static void name() {
346 #define END_SETUP }
347 #define START_TEARDOWN(name) static void name() {
348 #define END_TEARDOWN }
349
350 #endif /** TEST_SUITE_H_ @}*/