unit-tests: Allow default test timeout to be configured via compile option
[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 #ifndef TEST_FUNCTION_DEFAULT_TIMEOUT
43 #define TEST_FUNCTION_DEFAULT_TIMEOUT 2
44 #endif
45
46 /**
47 * Test function implementation
48 */
49 typedef void (*test_function_cb_t)(int);
50
51 /**
52 * Fixture for a test case.
53 */
54 typedef void (*test_fixture_cb_t)(void);
55
56 /**
57 * A test suite; a collection of test cases with fixtures
58 */
59 struct test_suite_t {
60 /** name of the test suite */
61 const char *name;
62 /** test cases registered, as test_case_t* */
63 array_t *tcases;
64 };
65
66 /**
67 * A test case; multiple test functions using the same fixtures
68 */
69 struct test_case_t {
70 /** name of the test case */
71 const char *name;
72 /** tests registered, as test_function_t */
73 array_t *functions;
74 /** fixture for tests, as test_fixture_t */
75 array_t *fixtures;
76 /** timeout for each function, in s */
77 int timeout;
78 };
79
80 /**
81 * A test function, with optional loop setup
82 */
83 struct test_function_t {
84 /** name of test function */
85 char *name;
86 /** tests function registered, test_function_t* */
87 test_function_cb_t cb;
88 /** start for loop test */
89 int start;
90 /** end for loop test */
91 int end;
92 };
93
94 /**
95 * Registered fixture for a test case
96 */
97 struct test_fixture_t {
98 test_fixture_cb_t setup;
99 test_fixture_cb_t teardown;
100 };
101
102 /**
103 * Create a new test suite
104 *
105 * @param name name of the test suite
106 * @return test suite
107 */
108 test_suite_t* test_suite_create(const char *name);
109
110 /**
111 * Create a new test case
112 *
113 * @param name name of test case
114 * @return test case
115 */
116 test_case_t* test_case_create(const char *name);
117
118 /**
119 * Add a setup/teardown function to the test case
120 *
121 * @param tcase test case to add a fixture to
122 * @param setup setup function called before each test
123 * @param teardown cleanup function called after each test
124 */
125 void test_case_add_checked_fixture(test_case_t *tcase, test_fixture_cb_t setup,
126 test_fixture_cb_t teardown);
127
128 /**
129 * Add a test function to a test case, with a name, looped several times
130 *
131 * @param name name of the test case
132 * @param tcase test case to add test function to
133 * @param cb callback function to invoke for test
134 * @param start start of loop counter
135 * @param end end of loop counter
136 */
137 void test_case_add_test_name(test_case_t *tcase, char *name,
138 test_function_cb_t cb, int start, int end);
139
140 /**
141 * Add a test function to a test case
142 *
143 * @param tcase test case to add test function to
144 * @param cb callback function to invoke for test
145 */
146 #define test_case_add_test(tcase, cb) \
147 test_case_add_test_name(tcase, #cb, cb, 0, 1)
148
149 /**
150 * Add a test function to a test case, looped several times
151 *
152 * @param tcase test case to add test function to
153 * @param cb callback function to invoke for test
154 * @param start start of loop counter
155 * @param end end of loop counter
156 */
157 #define test_case_add_loop_test(tcase, cb, start, end) \
158 test_case_add_test_name(tcase, #cb, cb, start, end)
159
160 /**
161 * Set a custom timeout for test functions in a test case
162 *
163 * @param tcase test case to set timeout for
164 * @param s test timeout in s
165 */
166 void test_case_set_timeout(test_case_t *tcase, int s);
167
168 /**
169 * Add a test function to a test case, looped several times
170 *
171 * @param suite test suite to add test case to
172 * @param tcase test case to add
173 */
174 void test_suite_add_case(test_suite_t *suite, test_case_t *tcase);
175
176 /**
177 * sigjmp restore point used by test_restore_point
178 */
179 #ifdef WIN32
180 extern jmp_buf test_restore_point_env;
181 #else
182 extern sigjmp_buf test_restore_point_env;
183 #endif
184
185 /**
186 * Set or return from an execution restore point
187 *
188 * This call sets a restore execution point and returns TRUE after it has
189 * been set up. On test failure, the execution is returned to the restore point
190 * and FALSE is returned to indicate test failure.
191 *
192 * @return TRUE if restore point set, FALSE when restored
193 */
194 #ifdef WIN32
195 # define test_restore_point() (setjmp(test_restore_point_env) == 0)
196 #else
197 # define test_restore_point() (sigsetjmp(test_restore_point_env, 1) == 0)
198 #endif
199
200 /**
201 * Set up signal handlers for test cases
202 */
203 void test_setup_handler();
204
205 /**
206 * Set up a timeout to let a test fail
207 *
208 * @param s timeout, 0 to disable timeout
209 */
210 void test_setup_timeout(int s);
211
212 /**
213 * Get info about a test failure
214 *
215 * @param msg buffer receiving failure info
216 * @param len size of msg buffer
217 * @param file pointer receiving source code file
218 * @return source code line number
219 */
220 int test_failure_get(char *msg, int len, const char **file);
221
222 /**
223 * Get a backtrace for a failure.
224 *
225 * @return allocated backtrace of test failure, if any
226 */
227 backtrace_t *test_failure_backtrace();
228
229 /**
230 * Let a test fail and set a message using vprintf 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 args argument list for fmt
236 */
237 void test_fail_vmsg(const char *file, int line, char *fmt, va_list args);
238
239 /**
240 * Let a test fail and set a message using printf style arguments.
241 *
242 * @param file source code file name
243 * @param line source code line number
244 * @param fmt printf format string
245 * @param ... arguments for fmt
246 */
247 void test_fail_msg(const char *file, int line, char *fmt, ...);
248
249 /**
250 * Let a test fail if one of the worker threads has failed (only if called from
251 * the main thread).
252 */
253 void test_fail_if_worker_failed();
254
255 /**
256 * Check if two integers equal, fail test if not
257 *
258 * @param a first integer
259 * @param b second integer
260 */
261 #define test_int_eq(a, b) \
262 ({ \
263 typeof(a) _a = a; \
264 typeof(b) _b = b; \
265 test_fail_if_worker_failed(); \
266 if (_a != _b) \
267 { \
268 test_fail_msg(__FILE__, __LINE__, #a " != " #b " (%d != %d)", _a, _b); \
269 } \
270 })
271
272 /**
273 * Check if two strings equal, fail test if not
274 *
275 * @param a first string
276 * @param b second string
277 */
278 #define test_str_eq(a, b) \
279 ({ \
280 char* _a = (char*)a; \
281 char* _b = (char*)b; \
282 test_fail_if_worker_failed(); \
283 if (!_a || !_b || !streq(_a, _b)) \
284 { \
285 test_fail_msg(__FILE__, __LINE__, \
286 #a " != " #b " (\"%s\" != \"%s\")", _a, _b); \
287 } \
288 })
289
290 /**
291 * Check if two chunks are equal, fail test if not
292 *
293 * @param a first chunk
294 * @param b second chunk
295 */
296 #define test_chunk_eq(a, b) \
297 ({ \
298 chunk_t _a = (chunk_t)a; \
299 chunk_t _b = (chunk_t)b; \
300 test_fail_if_worker_failed(); \
301 if (_a.len != _b.len || !memeq(a.ptr, b.ptr, a.len)) \
302 { \
303 test_fail_msg(__FILE__, __LINE__, \
304 #a " != " #b " (\"%#B\" != \"%#B\")", &_a, &_b); \
305 } \
306 })
307
308 /**
309 * Check if a statement evaluates to TRUE, fail test if not
310 *
311 * @param x statement to evaluate
312 */
313 #define test_assert(x) \
314 ({ \
315 test_fail_if_worker_failed(); \
316 if (!(x)) \
317 { \
318 test_fail_msg(__FILE__, __LINE__, "%s", #x); \
319 } \
320 })
321
322 /**
323 * Check if a statement evaluates to TRUE, fail and print a message if not
324 *
325 * @param x statement to evaluate
326 * @param fmt message format string
327 * @param ... fmt printf arguments
328 */
329 #define test_assert_msg(x, fmt, ...) \
330 ({ \
331 test_fail_if_worker_failed(); \
332 if (!(x)) \
333 { \
334 test_fail_msg(__FILE__, __LINE__, "%s: " fmt, #x, ##__VA_ARGS__); \
335 } \
336 })
337
338
339
340 /* "check unit testing" compatibility */
341 #define Suite test_suite_t
342 #define TCase test_case_t
343 #define ck_assert_int_eq test_int_eq
344 #define ck_assert test_assert
345 #define ck_assert_msg test_assert_msg
346 #define ck_assert_str_eq test_str_eq
347 #define ck_assert_chunk_eq test_chunk_eq
348 #define fail(fmt, ...) test_fail_msg(__FILE__, __LINE__, fmt, ##__VA_ARGS__)
349 #define fail_if(x, fmt, ...) \
350 ({ \
351 test_fail_if_worker_failed(); \
352 if (x) \
353 { \
354 test_fail_msg(__FILE__, __LINE__, "%s : " fmt, #x, ##__VA_ARGS__); \
355 } \
356 })
357 #define fail_unless test_assert_msg
358 #define suite_create test_suite_create
359 #define tcase_create test_case_create
360 #define tcase_add_checked_fixture test_case_add_checked_fixture
361 #define tcase_add_test test_case_add_test
362 #define tcase_add_loop_test test_case_add_loop_test
363 #define tcase_set_timeout test_case_set_timeout
364 #define suite_add_tcase test_suite_add_case
365 #define START_TEST(name) static void name (int _i) {
366 #define END_TEST test_fail_if_worker_failed(); }
367 #define START_SETUP(name) static void name() {
368 #define END_SETUP test_fail_if_worker_failed(); }
369 #define START_TEARDOWN(name) static void name() {
370 #define END_TEARDOWN test_fail_if_worker_failed(); }
371
372 #endif /** TEST_SUITE_H_ @}*/