summaryrefslogtreecommitdiff
path: root/src/libstrongswan/tests/test_suite.h
blob: edf16f128d9a2ebe712eabac1dce21d25d9ece5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2013 Tobias Brunner
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#ifndef TEST_UTILS_H_
#define TEST_UTILS_H_

#include <check.h>
#include <library.h>
#include <utils/debug.h>

/**
 * Used to mark test cases that use test fixtures.
 */
#define UNIT_TEST_FIXTURE_USED "UNIT_TEST_FIXTURE_USED"

/**
 * Check for memory leaks and fail if any are encountered.
 */
#define CHECK_FOR_LEAKS() do \
{ \
	if (lib->leak_detective->leaks(lib->leak_detective)) { \
		lib->leak_detective->report(lib->leak_detective, TRUE); \
	} \
	ck_assert_int_eq(lib->leak_detective->leaks(lib->leak_detective), 0); \
} \
while(0)

/**
 * Extended versions of the START|END_TEST macros that use leak detective.
 *
 * Since each test case runs in its own fork of the test runner the stuff
 * allocated before the test starts is not freed, so leak detective is disabled
 * by default to prevent false positives.  By enabling it right when the test
 * starts we at least capture leaks created by the tested objects/functions and
 * the test case itself.  This allows writing test cases for cleanup functions.
 *
 * To define test fixture with possibly allocated/destroyed memory that is
 * allocated/freed in a test case use the START|END_SETUP|TEARDOWN macros.
 */
#undef START_TEST
#define START_TEST(name) \
static void name (int _i CK_ATTRIBUTE_UNUSED) \
{ \
	tcase_fn_start(""#name, __FILE__, __LINE__); \
	dbg_default_set_level(LEVEL_SILENT); \
	lib->leak_detective->set_state(lib->leak_detective, TRUE);

#undef END_TEST
#define END_TEST \
	if (!lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
	{ \
		CHECK_FOR_LEAKS(); \
	} \
}

/**
 * Define a function to setup a test fixture that can be used with the above
 * macros.
 */
#define START_SETUP(name) \
static void name() \
{ \
	lib->set(lib, UNIT_TEST_FIXTURE_USED, (void*)TRUE); \
	lib->leak_detective->set_state(lib->leak_detective, TRUE);

/**
 * End a setup function
 */
#define END_SETUP }

/**
 * Define a function to teardown a test fixture that can be used with the above
 * macros.
 */
#define START_TEARDOWN(name) \
static void name() \
{

/**
 * End a teardown function
 */
#define END_TEARDOWN \
	if (lib->get(lib, UNIT_TEST_FIXTURE_USED)) \
	{ \
		CHECK_FOR_LEAKS(); \
	} \
}

#endif /** TEST_UTILS_H_ */