suricata
util-unittest.h
Go to the documentation of this file.
1/* Copyright (C) 2007-2021 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Victor Julien <victor@inliniac.net>
22 * \author Breno Silva <breno.silva@gmail.com>
23 *
24 * Unit test framework
25 */
26
27/**
28 * \addtogroup Testing
29 *
30 * @{
31 */
32
33#ifndef SURICATA_UTIL_UNITTEST_H
34#define SURICATA_UTIL_UNITTEST_H
35
36#ifdef UNITTESTS
37
38typedef struct UtTest_
39{
40 const char *name;
41 int(*TestFn)(void);
42
43 struct UtTest_ *next;
44
46
47void UtRegisterTest(const char *name, int(*TestFn)(void));
48uint32_t UtRunTests(const char *regex_arg);
49void UtInitialize(void);
50void UtCleanup(void);
51int UtRunSelftest (const char *regex_arg);
52void UtListTests(const char *regex_arg);
53void UtRunModeRegister(void);
54
55extern int unittests_fatal;
56
57/**
58 * \brief Fail a test.
59 */
60#define FAIL do { \
61 if (unittests_fatal) { \
62 BUG_ON(1); \
63 } else { \
64 return 0; \
65 } \
66 } while (0)
67
68/**
69 * \brief Fail a test if expression evaluates to true.
70 */
71#define FAIL_IF(expr) do { \
72 if (unittests_fatal) { \
73 BUG_ON(expr); \
74 } else if (expr) { \
75 return 0; \
76 } \
77 } while (0)
78
79/**
80 * \brief Fail a test if expression evaluates to false.
81 */
82#define FAIL_IF_NOT(expr) do { \
83 FAIL_IF(!(expr)); \
84 } while (0)
85
86/**
87 * \brief Fail a test if expression evaluates to NULL.
88 */
89#define FAIL_IF_NULL(expr) do { \
90 FAIL_IF(NULL == expr); \
91 } while (0)
92
93/**
94 * \brief Fail a test if expression evaluates to non-NULL.
95 */
96#define FAIL_IF_NOT_NULL(expr) do { \
97 FAIL_IF(NULL != expr); \
98 } while (0)
99
100/**
101 * \brief Pass the test.
102 *
103 * Only to be used at the end of a function instead of "return 1."
104 */
105#define PASS do { \
106 return 1; \
107 } while (0)
108
109#endif
110
111#endif /* SURICATA_UTIL_UNITTEST_H */
112
113/**
114 * @}
115 */
int UtRunSelftest(const char *regex_arg)
Run self tests.
struct UtTest_ UtTest
int unittests_fatal
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
void UtCleanup(void)
Cleanup unit test list.
void UtRunModeRegister(void)
void UtInitialize(void)
Initialize unit test list.
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
void UtListTests(const char *regex_arg)
List all registered unit tests.
const char * regex_arg
const char * name
struct UtTest_ * next
int(* TestFn)(void)
const char * name