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 
38 typedef struct UtTest_
39 {
40  const char *name;
41  int(*TestFn)(void);
42 
43  struct UtTest_ *next;
44 
46 
47 void UtRegisterTest(const char *name, int(*TestFn)(void));
48 uint32_t UtRunTests(const char *regex_arg);
49 void UtInitialize(void);
50 void UtCleanup(void);
51 int UtRunSelftest (const char *regex_arg);
52 void UtListTests(const char *regex_arg);
53 void UtRunModeRegister(void);
54 
55 extern 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  */
UtTest_
Definition: util-unittest.h:39
UtTest
struct UtTest_ UtTest
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
regex_arg
const char * regex_arg
Definition: runmode-unittests.h:27
UtListTests
void UtListTests(const char *regex_arg)
List all registered unit tests.
Definition: util-unittest.c:157
UtRunSelftest
int UtRunSelftest(const char *regex_arg)
Run self tests.
Definition: util-unittest.c:321
unittests_fatal
int unittests_fatal
Definition: util-unittest.c:52
UtTest_::TestFn
int(* TestFn)(void)
Definition: util-unittest.h:41
UtInitialize
void UtInitialize(void)
Initialize unit test list.
Definition: util-unittest.c:260
UtRunTests
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
Definition: util-unittest.c:188
UtTest_::next
struct UtTest_ * next
Definition: util-unittest.h:43
UtCleanup
void UtCleanup(void)
Cleanup unit test list.
Definition: util-unittest.c:269
UtTest_::name
const char * name
Definition: util-unittest.h:40
UtRunModeRegister
void UtRunModeRegister(void)
Definition: util-unittest.c:283