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