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 pcre *parse_regex;
48 static pcre_extra *parse_regex_study;
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  const char *eb;
128  int eo;
129  int opts = PCRE_CASELESS;;
130 
131  if(regex_arg == NULL)
132  return -1;
133 
134  parse_regex = pcre_compile(regex_arg, opts, &eb, &eo, NULL);
135  if(parse_regex == NULL)
136  {
137  printf("pcre compile of \"%s\" failed at offset %" PRId32 ": %s\n", regex_arg, eo, eb);
138  goto error;
139  }
140 
141  parse_regex_study = pcre_study(parse_regex, 0, &eb);
142  if(eb != NULL)
143  {
144  printf("pcre study failed: %s\n", eb);
145  goto error;
146  }
147 
148  return 1;
149 
150 error:
151  return -1;
152 }
153 
154 #define MAX_SUBSTRINGS 30
155 
156 /** \brief List all registered unit tests.
157  *
158  * \param regex_arg Regular expression to limit listed tests.
159  */
160 void UtListTests(const char *regex_arg)
161 {
162  UtTest *ut;
163  int ret = 0, rcomp = 0;
164  int ov[MAX_SUBSTRINGS];
165 
166  rcomp = UtRegex(regex_arg);
167 
168  for (ut = ut_list; ut != NULL; ut = ut->next) {
169  if (rcomp == 1) {
170  ret = pcre_exec(parse_regex, parse_regex_study, ut->name,
171  strlen(ut->name), 0, 0, ov, MAX_SUBSTRINGS);
172  if (ret >= 1) {
173  printf("%s\n", ut->name);
174  }
175  }
176  else {
177  printf("%s\n", ut->name);
178  }
179  }
180 }
181 
182 /** \brief Run all registered unittests.
183  *
184  * \param regex_arg The regular expression
185  *
186  * \retval 0 all successful
187  * \retval result number of tests that failed
188  */
189 
190 uint32_t UtRunTests(const char *regex_arg)
191 {
192  UtTest *ut;
193  uint32_t good = 0, bad = 0, matchcnt = 0;
194  int ret = 0, rcomp = 0;
195  int ov[MAX_SUBSTRINGS];
196 
199 
200  rcomp = UtRegex(regex_arg);
201 
202  if(rcomp == 1){
203  for (ut = ut_list; ut != NULL; ut = ut->next) {
204  ret = pcre_exec(parse_regex, parse_regex_study, ut->name, strlen(ut->name), 0, 0, ov, MAX_SUBSTRINGS);
205  if( ret >= 1 ) {
206  printf("Test %-60.60s : ", ut->name);
207  matchcnt++;
208  fflush(stdout); /* flush so in case of a segv we see the testname */
209 
210  /* reset the time */
213 
214  ret = ut->TestFn();
215 
216  if (StreamTcpMemuseCounter() != 0) {
217  printf("STREAM MEMORY IN USE %"PRIu64"\n", StreamTcpMemuseCounter());
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  return bad;
251 }
252 /**
253  * \brief Initialize unit test list
254  */
255 
256 void UtInitialize(void)
257 {
258  ut_list = NULL;
259 }
260 
261 /**
262  * \brief Cleanup unit test list
263  */
264 
265 void UtCleanup(void)
266 {
267 
268  UtTest *tmp = ut_list, *otmp;
269 
270  while (tmp != NULL) {
271  otmp = tmp->next;
272  SCFree(tmp);
273  tmp = otmp;
274  }
275 
276  ut_list = NULL;
277 }
278 
280 {
282  "unittest",
283  "Unittest mode",
284  NULL);
285 
286  return;
287 }
288 
289 /*
290  * unittests for the unittests code
291  */
292 
293 /** \brief True test
294  *
295  * \retval 1 True
296  * \retval 0 False
297  */
298 static int UtSelftestTrue(void)
299 {
300  if (1)return 1;
301  else return 0;
302 }
303 
304 /** \brief False test
305  *
306  * \retval 1 False
307  * \retval 0 True
308  */
309 static int UtSelftestFalse(void)
310 {
311  if (0)return 0;
312  else return 1;
313 }
314 
315 /** \brief Run self tests
316  *
317  * \param regex_arg The regular expression
318  *
319  * \retval 0 all successful
320  */
321 
322 int UtRunSelftest (const char *regex_arg)
323 {
324  printf("* Running Unittesting subsystem selftests...\n");
325 
326  UtInitialize();
327 
328  UtRegisterTest("true", UtSelftestTrue);
329  UtRegisterTest("false", UtSelftestFalse);
330 
331  int ret = UtRunTests(regex_arg);
332  if (ret == 0)
333  printf("* Done running Unittesting subsystem selftests...\n");
334  else
335  printf("* ERROR running Unittesting subsystem selftests failed...\n");
336 
337  UtCleanup();
338  return 0;
339 }
340 #endif /* UNITTESTS */
341 
342 /**
343  * @}
344  */
void TimeSetToCurrentTime(void)
set the time to "gettimeofday" meant for testing
Definition: util-time.c:135
struct UtTest_ * next
Definition: util-unittest.h:43
int unittests_fatal
Definition: util-unittest.c:52
#define unlikely(expr)
Definition: util-optimize.h:35
void UtListTests(const char *regex_arg)
List all registered unit tests.
const char * name
Definition: util-unittest.h:40
int UtRunSelftest(const char *regex_arg)
Run self tests.
uint64_t StreamTcpMemuseCounter(void)
Definition: stream-tcp.c:156
void StreamTcpReassembleInitMemuse(void)
void TimeModeSetOffline(void)
Definition: util-time.c:96
void UtInitialize(void)
Initialize unit test list.
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
uint64_t StreamTcpReassembleMemuseGlobalCounter(void)
#define SCMalloc(a)
Definition: util-mem.h:222
void StreamTcpInitMemuse(void)
Definition: stream-tcp.c:123
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
#define SCFree(a)
Definition: util-mem.h:322
void UtRunModeRegister(void)
const char * regex_arg
void RunModeRegisterNewRunMode(enum RunModes runmode, const char *name, const char *description, int(*RunModeFunc)(void))
Registers a new runmode.
Definition: runmodes.c:419
#define MAX_SUBSTRINGS
void UtCleanup(void)
Cleanup unit test list.
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
int(* TestFn)(void)
Definition: util-unittest.h:41