edf16f128d9a2ebe712eabac1dce21d25d9ece5a
[strongswan.git] / src / libstrongswan / tests / test_suite.h
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 #ifndef TEST_UTILS_H_
17 #define TEST_UTILS_H_
18
19 #include <check.h>
20 #include <library.h>
21 #include <utils/debug.h>
22
23 /**
24 * Used to mark test cases that use test fixtures.
25 */
26 #define UNIT_TEST_FIXTURE_USED "UNIT_TEST_FIXTURE_USED"
27
28 /**
29 * Check for memory leaks and fail if any are encountered.
30 */
31 #define CHECK_FOR_LEAKS() do \
32 { \
33 if (lib->leak_detective->leaks(lib->leak_detective)) { \
34 lib->leak_detective->report(lib->leak_detective, TRUE); \
35 } \
36 ck_assert_int_eq(lib->leak_detective->leaks(lib->leak_detective), 0); \
37 } \
38 while(0)
39
40 /**
41 * Extended versions of the START|END_TEST macros that use leak detective.
42 *
43 * Since each test case runs in its own fork of the test runner the stuff
44 * allocated before the test starts is not freed, so leak detective is disabled
45 * by default to prevent false positives. By enabling it right when the test
46 * starts we at least capture leaks created by the tested objects/functions and
47 * the test case itself. This allows writing test cases for cleanup functions.
48 *
49 * To define test fixture with possibly allocated/destroyed memory that is
50 * allocated/freed in a test case use the START|END_SETUP|TEARDOWN macros.
51 */
52 #undef START_TEST
53 #define START_TEST(name) \
54 static void name (int _i CK_ATTRIBUTE_UNUSED) \
55 { \
56 tcase_fn_start(""#name, __FILE__, __LINE__); \
57 dbg_default_set_level(LEVEL_SILENT); \
58 lib->leak_detective->set_state(lib->leak_detective, TRUE);
59
60 #undef END_TEST
61 #define END_TEST \
62 if (!lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
63 { \
64 CHECK_FOR_LEAKS(); \
65 } \
66 }
67
68 /**
69 * Define a function to setup a test fixture that can be used with the above
70 * macros.
71 */
72 #define START_SETUP(name) \
73 static void name() \
74 { \
75 lib->set(lib, UNIT_TEST_FIXTURE_USED, (void*)TRUE); \
76 lib->leak_detective->set_state(lib->leak_detective, TRUE);
77
78 /**
79 * End a setup function
80 */
81 #define END_SETUP }
82
83 /**
84 * Define a function to teardown a test fixture that can be used with the above
85 * macros.
86 */
87 #define START_TEARDOWN(name) \
88 static void name() \
89 {
90
91 /**
92 * End a teardown function
93 */
94 #define END_TEARDOWN \
95 if (lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
96 { \
97 CHECK_FOR_LEAKS(); \
98 } \
99 }
100
101 #endif /** TEST_UTILS_H_ */