suricata
util-unittest.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2010 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  * \defgroup Testing Testing
29  *
30  * \brief Unit testing support functions.
31  *
32  * @{
33  */
34 
35 #include "suricata-common.h"
36 #include "runmodes.h"
37 #include "util-debug.h"
38 #include "conf.h"
39 
40 #include "stream-tcp.h"
41 #include "stream-tcp-reassemble.h"
42 
43 #ifdef UNITTESTS
44 
45 static pcre2_code *parse_regex;
46 static pcre2_match_data *parse_regex_match;
47 
48 static UtTest *ut_list;
49 
51 
52 /**
53  * \brief Allocate UtTest list member
54  *
55  * \retval ut Pointer to UtTest
56  */
57 
58 static UtTest *UtAllocTest(void)
59 {
60  UtTest *ut = SCMalloc(sizeof(UtTest));
61  if (unlikely(ut == NULL))
62  return NULL;
63 
64  memset(ut, 0, sizeof(UtTest));
65 
66  return ut;
67 }
68 
69 /**
70  * \brief Append test in UtTest list
71  *
72  * \param list Pointer to the start of the IP packet
73  * \param test Pointer to unit test
74  *
75  * \retval 0 Function always returns zero
76  */
77 
78 static int UtAppendTest(UtTest **list, UtTest *test)
79 {
80  if (*list == NULL) {
81  *list = test;
82  } else {
83  UtTest *tmp = *list;
84 
85  while (tmp->next != NULL) {
86  tmp = tmp->next;
87  }
88  tmp->next = test;
89  }
90 
91  return 0;
92 }
93 
94 /**
95  * \brief Register unit test
96  *
97  * \param name Unit test name
98  * \param TestFn Unit test function
99  */
100 
101 void UtRegisterTest(const char *name, int(*TestFn)(void))
102 {
103  UtTest *ut = UtAllocTest();
104  if (ut == NULL)
105  return;
106 
107  ut->name = name;
108  ut->TestFn = TestFn;
109  ut->next = NULL;
110 
111  /* append */
112  UtAppendTest(&ut_list, ut);
113 }
114 
115 /**
116  * \brief Compile a regex to run a specific unit test
117  *
118  * \param regex_arg The regular expression
119  *
120  * \retval 1 Regex compiled
121  * \retval -1 Regex error
122  */
123 static int UtRegex (const char *regex_arg)
124 {
125  int en;
126  PCRE2_SIZE eo;
127  int opts = PCRE2_CASELESS;
128 
129  if(regex_arg == NULL)
130  return -1;
131 
132  parse_regex =
133  pcre2_compile((PCRE2_SPTR8)regex_arg, PCRE2_ZERO_TERMINATED, opts, &en, &eo, NULL);
134  if(parse_regex == NULL)
135  {
136  PCRE2_UCHAR errbuffer[256];
137  pcre2_get_error_message(en, errbuffer, sizeof(errbuffer));
138  SCLogError("pcre2 compile of \"%s\" failed at "
139  "offset %d: %s",
140  regex_arg, (int)eo, errbuffer);
141  goto error;
142  }
143  parse_regex_match = pcre2_match_data_create_from_pattern(parse_regex, NULL);
144 
145  return 1;
146 
147 error:
148  return -1;
149 }
150 
151 /** \brief List all registered unit tests.
152  *
153  * \param regex_arg Regular expression to limit listed tests.
154  */
155 void UtListTests(const char *regex_arg)
156 {
157  UtTest *ut;
158  int ret = 0, rcomp = 0;
159 
160  rcomp = UtRegex(regex_arg);
161 
162  for (ut = ut_list; ut != NULL; ut = ut->next) {
163  if (rcomp == 1) {
164  ret = pcre2_match(parse_regex, (PCRE2_SPTR8)ut->name, strlen(ut->name), 0, 0,
165  parse_regex_match, NULL);
166  if (ret >= 1) {
167  printf("%s\n", ut->name);
168  }
169  }
170  else {
171  printf("%s\n", ut->name);
172  }
173  }
174  pcre2_code_free(parse_regex);
175  pcre2_match_data_free(parse_regex_match);
176 }
177 
178 /** \brief Run all registered unittests.
179  *
180  * \param regex_arg The regular expression
181  *
182  * \retval 0 all successful
183  * \retval result number of tests that failed
184  */
185 
186 uint32_t UtRunTests(const char *regex_arg)
187 {
188  UtTest *ut;
189  uint32_t good = 0, bad = 0, matchcnt = 0;
190  int ret = 0, rcomp = 0;
191 
194 
195  rcomp = UtRegex(regex_arg);
196 
197  if(rcomp == 1){
198  for (ut = ut_list; ut != NULL; ut = ut->next) {
199  ret = pcre2_match(parse_regex, (PCRE2_SPTR8)ut->name, strlen(ut->name), 0, 0,
200  parse_regex_match, NULL);
201  if( ret >= 1 ) {
202  printf("Test %-60.60s : ", ut->name);
203  matchcnt++;
204  fflush(stdout); /* flush so in case of a segv we see the testname */
205 
206  /* reset the time */
209 
210  ret = ut->TestFn();
211 
212  if (StreamTcpMemuseCounter() != 0) {
213  printf("STREAM MEMORY IN USE %"PRIu64"\n", StreamTcpMemuseCounter());
214  ret = 0;
215  }
216  if (FlowGetMemuse() != 0) {
217  printf("FLOW MEMORY IN USE %"PRIu64"\n", FlowGetMemuse());
218  ret = 0;
219  }
220 
222  printf("STREAM REASSEMBLY MEMORY IN USE %"PRIu64"\n", StreamTcpReassembleMemuseGlobalCounter());
223  ret = 0;
224  }
225 
226  printf("%s\n", ret ? "pass" : "FAILED");
227 
228  if (!ret) {
229  if (unittests_fatal == 1) {
230  fprintf(stderr, "ERROR: unittest failed.\n");
231  exit(EXIT_FAILURE);
232  }
233  bad++;
234  } else {
235  good++;
236  }
237  }
238  }
239  if(matchcnt > 0){
240  printf("==== TEST RESULTS ====\n");
241  printf("PASSED: %" PRIu32 "\n", good);
242  printf("FAILED: %" PRIu32 "\n", bad);
243  printf("======================\n");
244  } else {
245  SCLogInfo("UtRunTests: regex provided regex_arg: %s did not match any tests",regex_arg);
246  }
247  } else {
248  SCLogInfo("UtRunTests: pcre compilation failed");
249  }
250  pcre2_code_free(parse_regex);
251  pcre2_match_data_free(parse_regex_match);
252  return bad;
253 }
254 /**
255  * \brief Initialize unit test list
256  */
257 
258 void UtInitialize(void)
259 {
260  ut_list = NULL;
261 }
262 
263 /**
264  * \brief Cleanup unit test list
265  */
266 
267 void UtCleanup(void)
268 {
269 
270  UtTest *tmp = ut_list, *otmp;
271 
272  while (tmp != NULL) {
273  otmp = tmp->next;
274  SCFree(tmp);
275  tmp = otmp;
276  }
277 
278  ut_list = NULL;
279 }
280 
282 {
283  RunModeRegisterNewRunMode(RUNMODE_UNITTEST, "unittest", "Unittest mode", NULL, NULL);
284 }
285 
286 /*
287  * unittests for the unittests code
288  */
289 
290 /** \brief True test
291  *
292  * \retval 1 True
293  * \retval 0 False
294  */
295 static int UtSelftestTrue(void)
296 {
297  if (1)return 1;
298  else
299  return 0;
300 }
301 
302 /** \brief False test
303  *
304  * \retval 1 False
305  * \retval 0 True
306  */
307 static int UtSelftestFalse(void)
308 {
309  if (0)return 0;
310  else
311  return 1;
312 }
313 
314 /** \brief Run self tests
315  *
316  * \param regex_arg The regular expression
317  *
318  * \retval 0 all successful
319  */
320 
321 int UtRunSelftest (const char *regex_arg)
322 {
323  printf("* Running Unittesting subsystem selftests...\n");
324 
325  UtInitialize();
326 
327  UtRegisterTest("true", UtSelftestTrue);
328  UtRegisterTest("false", UtSelftestFalse);
329 
330  int ret = UtRunTests(regex_arg);
331  if (ret == 0)
332  printf("* Done running Unittesting subsystem selftests...\n");
333  else
334  printf("* ERROR running Unittesting subsystem selftests failed...\n");
335 
336  UtCleanup();
337  return 0;
338 }
339 #endif /* UNITTESTS */
340 
341 /**
342  * @}
343  */
UtTest_
Definition: util-unittest.h:39
stream-tcp.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
regex_arg
const char * regex_arg
Definition: runmode-unittests.h:27
name
const char * name
Definition: detect-engine-proto.c:47
StreamTcpInitMemuse
void StreamTcpInitMemuse(void)
Definition: stream-tcp.c:231
UtListTests
void UtListTests(const char *regex_arg)
List all registered unit tests.
Definition: util-unittest.c:155
UtRunSelftest
int UtRunSelftest(const char *regex_arg)
Run self tests.
Definition: util-unittest.c:321
FlowGetMemuse
uint64_t FlowGetMemuse(void)
Definition: flow.c:126
unittests_fatal
int unittests_fatal
Definition: util-unittest.c:50
stream-tcp-reassemble.h
UtTest_::TestFn
int(* TestFn)(void)
Definition: util-unittest.h:41
util-debug.h
TimeSetToCurrentTime
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:140
UtInitialize
void UtInitialize(void)
Initialize unit test list.
Definition: util-unittest.c:258
RUNMODE_UNITTEST
@ RUNMODE_UNITTEST
Definition: runmodes.h:41
StreamTcpReassembleInitMemuse
void StreamTcpReassembleInitMemuse(void)
Definition: stream-tcp-reassemble.c:105
conf.h
RunModeRegisterNewRunMode
void RunModeRegisterNewRunMode(enum SCRunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void), int(*RunModeIsIPSEnabled)(void))
Registers a new runmode.
Definition: runmodes.c:481
UtRunTests
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
Definition: util-unittest.c:186
runmodes.h
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:232
UtTest_::next
struct UtTest_ * next
Definition: util-unittest.h:43
TimeModeSetOffline
void TimeModeSetOffline(void)
Definition: util-time.c:105
suricata-common.h
UtCleanup
void UtCleanup(void)
Cleanup unit test list.
Definition: util-unittest.c:267
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
UtTest_::name
const char * name
Definition: util-unittest.h:40
StreamTcpMemuseCounter
uint64_t StreamTcpMemuseCounter(void)
Definition: stream-tcp.c:262
StreamTcpReassembleMemuseGlobalCounter
uint64_t StreamTcpReassembleMemuseGlobalCounter(void)
Definition: stream-tcp-reassemble.c:150
UtRunModeRegister
void UtRunModeRegister(void)
Definition: util-unittest.c:281