unit-tests: support testing when leak-detective has not been enabled
[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) \
34 { \
35 if (lib->leak_detective->leaks(lib->leak_detective)) { \
36 lib->leak_detective->report(lib->leak_detective, TRUE); \
37 } \
38 ck_assert_int_eq(lib->leak_detective->leaks(lib->leak_detective), 0); \
39 } \
40 } \
41 while(0)
42
43 /**
44 * Extended versions of the START|END_TEST macros that use leak detective.
45 *
46 * Since each test case runs in its own fork of the test runner the stuff
47 * allocated before the test starts is not freed, so leak detective is disabled
48 * by default to prevent false positives. By enabling it right when the test
49 * starts we at least capture leaks created by the tested objects/functions and
50 * the test case itself. This allows writing test cases for cleanup functions.
51 *
52 * To define test fixture with possibly allocated/destroyed memory that is
53 * allocated/freed in a test case use the START|END_SETUP|TEARDOWN macros.
54 */
55 #undef START_TEST
56 #define START_TEST(name) \
57 static void name (int _i CK_ATTRIBUTE_UNUSED) \
58 { \
59 tcase_fn_start(""#name, __FILE__, __LINE__); \
60 dbg_default_set_level(LEVEL_SILENT); \
61 if (lib->leak_detective) \
62 { \
63 lib->leak_detective->set_state(lib->leak_detective, TRUE); \
64 }
65
66 #undef END_TEST
67 #define END_TEST \
68 if (!lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
69 { \
70 CHECK_FOR_LEAKS(); \
71 } \
72 }
73
74 /**
75 * Define a function to setup a test fixture that can be used with the above
76 * macros.
77 */
78 #define START_SETUP(name) \
79 static void name() \
80 { \
81 lib->set(lib, UNIT_TEST_FIXTURE_USED, (void*)TRUE); \
82 if (lib->leak_detective) \
83 { \
84 lib->leak_detective->set_state(lib->leak_detective, TRUE); \
85 }
86
87 /**
88 * End a setup function
89 */
90 #define END_SETUP }
91
92 /**
93 * Define a function to teardown a test fixture that can be used with the above
94 * macros.
95 */
96 #define START_TEARDOWN(name) \
97 static void name() \
98 {
99
100 /**
101 * End a teardown function
102 */
103 #define END_TEARDOWN \
104 if (lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
105 { \
106 CHECK_FOR_LEAKS(); \
107 } \
108 }
109
110 #endif /** TEST_UTILS_H_ */