suricata
runmode-unittests.c
Go to the documentation of this file.
1 /* Copyright (C) 2013 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 /** \file
19  *
20  * \author Eric Leblond <eric@regit.org>
21  */
22 
23 #include "suricata-common.h"
24 #include "config.h"
25 #include "util-unittest.h"
26 #include "runmode-unittests.h"
27 
28 #ifdef UNITTESTS
29 
30 #include "detect-parse.h"
31 #include "detect-engine.h"
32 #include "detect-engine-address.h"
33 #include "detect-engine-proto.h"
34 #include "detect-engine-port.h"
35 #include "detect-engine-mpm.h"
36 #include "detect-engine-sigorder.h"
37 #include "detect-engine-payload.h"
39 #include "detect-engine-state.h"
40 #include "detect-engine-tag.h"
41 #include "detect-engine-modbus.h"
42 #include "detect-fast-pattern.h"
43 #include "flow.h"
44 #include "flow-timeout.h"
45 #include "flow-manager.h"
46 #include "flow-var.h"
47 #include "flow-bit.h"
48 #include "pkt-var.h"
49 
50 #include "host.h"
51 #include "host-bit.h"
52 #include "ippair.h"
53 #include "ippair-bit.h"
54 #include "unix-manager.h"
55 
56 #include "stream-tcp.h"
57 
58 #include "app-layer-detect-proto.h"
59 #include "app-layer-parser.h"
60 #include "app-layer.h"
61 #include "app-layer-smb.h"
62 #include "app-layer-dcerpc.h"
63 #include "app-layer-dcerpc-udp.h"
64 #include "app-layer-htp.h"
65 #include "app-layer-ftp.h"
66 #include "app-layer-ssl.h"
67 #include "app-layer-ssh.h"
68 #include "app-layer-smtp.h"
69 
70 #include "util-action.h"
71 #include "util-radix-tree.h"
72 #include "util-host-os-info.h"
73 #include "util-cidr.h"
74 #include "util-unittest-helper.h"
75 #include "util-time.h"
76 #include "util-rule-vars.h"
78 #include "util-threshold-config.h"
79 #include "util-reference-config.h"
80 #include "util-profiling.h"
81 #include "util-magic.h"
82 #include "util-memcmp.h"
83 #include "util-misc.h"
84 #include "util-signal.h"
85 
86 #include "reputation.h"
87 #include "util-atomic.h"
88 #include "util-spm.h"
89 #include "util-hash.h"
90 #include "util-hashlist.h"
91 #include "util-bloomfilter.h"
93 #include "util-pool.h"
94 #include "util-byte.h"
95 #include "util-proto-name.h"
96 #include "util-memrchr.h"
97 
98 #include "util-mpm-ac.h"
99 #include "util-mpm-hs.h"
100 
101 #include "util-decode-asn1.h"
102 
103 #include "conf.h"
104 #include "conf-yaml-loader.h"
105 #include "tmqh-flow.h"
106 #include "defrag.h"
107 #include "detect-engine-siggroup.h"
108 
109 #include "util-streaming-buffer.h"
110 #include "util-lua.h"
111 
112 #ifdef OS_WIN32
113 #include "win32-syscall.h"
114 #endif
115 
116 #ifdef WINDIVERT
117 #include "source-windivert.h"
118 #endif
119 
120 #ifdef HAVE_NSS
121 #include <prinit.h>
122 #include <nss.h>
123 #endif
124 
125 #endif /* UNITTESTS */
126 
127 void TmqhSetup (void);
128 
129 #ifdef UNITTESTS
130 static void RegisterUnittests(void)
131 {
185 #ifdef PROFILING
187 #endif
197  DetectPortTests();
203 #ifdef OS_WIN32
204  Win32SyscallRegisterTests();
205 #endif
206 #ifdef WINDIVERT
207  SourceWinDivertRegisterTests();
208 #endif
209 }
210 #endif
211 
212 /**
213  * Run or list unittests
214  *
215  * \param list_unittests If set to 1, list unittests. Run them if set to 0.
216  * \param regex_arg A regular expression to select unittests to run
217  *
218  * This function is terminal and will call exit after being called.
219  */
220 
221 void RunUnittests(int list_unittests, const char *regex_arg)
222 {
223 #ifdef UNITTESTS
224  /* Initializations for global vars, queues, etc (memsets, mutex init..) */
226 
227 #ifdef HAVE_LUAJIT
228  if (LuajitSetupStatesPool() != 0) {
229  exit(EXIT_FAILURE);
230  }
231 #endif
232 
234  /* load the pattern matchers */
235  MpmTableSetup();
236  SpmTableSetup();
237 
238  StorageInit();
239  AppLayerSetup();
240 
241  /* hardcoded initialization code */
242  SigTableSetup(); /* load the rule keywords */
243  TmqhSetup();
244 
245  CIDRInit();
246 
247 #ifdef DBG_MEM_ALLOC
248  SCLogInfo("Memory used at startup: %"PRIdMAX, (intmax_t)global_mem);
249 #endif
250  SCProtoNameInit();
251 
252  TagInitCtx();
254  SCClassConfInit();
255 
256  UtInitialize();
257 
259 
260  HostBitInitCtx();
261 
262  StorageFinalize();
263  /* test and initialize the unittesting subsystem */
264  if (regex_arg == NULL){
265  regex_arg = ".*";
266  UtRunSelftest(regex_arg); /* inits and cleans up again */
267  }
268 
269 #ifdef HAVE_NSS
270  /* init NSS for hashing */
271  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
272  NSS_NoDB_Init(NULL);
273 #endif
274 
275 
278 
279  RegisterUnittests();
280 
281  if (list_unittests) {
282  UtListTests(regex_arg);
283  } else {
284  /* global packet pool */
285  extern intmax_t max_pending_packets;
286  max_pending_packets = 128;
287  PacketPoolInit();
288 
289  uint32_t failed = UtRunTests(regex_arg);
291  UtCleanup();
292 #ifdef BUILD_HYPERSCAN
294 #endif
295  if (failed) {
296  exit(EXIT_FAILURE);
297  }
298  }
299 
300 #ifdef HAVE_LUAJIT
301  LuajitFreeStatesPool();
302 #endif
303 #ifdef DBG_MEM_ALLOC
304  SCLogInfo("Total memory used (without SCFree()): %"PRIdMAX, (intmax_t)global_mem);
305 #endif
306 
307  exit(EXIT_SUCCESS);
308 #else
309  SCLogError(SC_ERR_NOT_SUPPORTED, "Unittests are not build-in");
310  exit(EXIT_FAILURE);
311 #endif /* UNITTESTS */
312 }
313 
void SpmTableSetup(void)
Definition: util-spm.c:115
void SigTableSetup(void)
void SCReferenceConfInit(void)
void MpmTableSetup(void)
Definition: util-mpm.c:284
void SCRuleVarsRegisterTests(void)
void BloomFilterCountingRegisterTests(void)
void IPPairBitRegisterTests(void)
Definition: ippair-bit.c:493
void SCHInfoRegisterTests(void)
void DetectProtoTests(void)
this function registers unit tests for DetectProto
void HostBitInitCtx(void)
Definition: host-bit.c:49
int StorageFinalize(void)
Definition: util-storage.c:139
void DefragRegisterTests(void)
Definition: defrag.c:2461
void SCAtomicRegisterTests(void)
Definition: util-atomic.c:66
void SCRConfRegisterTests(void)
This function registers unit tests for Reference Config API.
void DecodeRawRegisterTests(void)
Registers Raw unit tests.
Definition: decode-raw.c:229
void SCReputationRegisterTests(void)
Definition: reputation.c:2313
void UTHRegisterTests(void)
void RunUnittests(int list_unittests, const char *regex_arg)
void CIDRInit(void)
Definition: util-cidr.c:31
void UtListTests(const char *regex_arg)
List all registered unit tests.
void DecodeUDPV4RegisterTests(void)
Definition: decode-udp.c:199
void DecodePPPRegisterTests(void)
Definition: decode-ppp.c:310
void SCProtoNameInit()
Function to load the protocol names from the specified protocol file.
void UtilActionRegisterTests(void)
Definition: util-action.c:1596
void HostRegisterUnittests(void)
Definition: host.c:731
void PoolRegisterTests(void)
Definition: util-pool.c:721
void SCProfilingRegisterTests(void)
void PayloadRegisterTests(void)
void DetectPortTests(void)
void SCClassConfRegisterTests(void)
This function registers unit tests for Classification Config API.
int UtRunSelftest(const char *regex_arg)
Run self tests.
int max_pending_packets
Definition: suricata.c:215
int AppLayerSetup(void)
Setup the app layer.
Definition: app-layer.c:756
void RegisterAllModules(void)
Definition: suricata.c:865
void StreamTcpRegisterTests(void)
Definition: stream-tcp.c:10758
void UtilSpmSearchRegistertests(void)
Definition: util-spm.c:2669
void MpmHSGlobalCleanup(void)
void DecodeIPV4RegisterTests(void)
Definition: decode-ipv4.c:1636
void StreamingBufferRegisterTests(void)
void SCRadixRegisterTests(void)
void TmqhFlowRegisterTests(void)
Definition: tmqh-flow.c:423
void AppLayerHtpNeedFileInspection(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request file...
void DecodeICMPV6RegisterTests(void)
Registers ICMPV6 unit tests.
void UtInitialize(void)
Initialize unit test list.
void UtilMiscRegisterTests(void)
Definition: util-misc.c:1147
void FlowRegisterTests(void)
Function to register the Flow Unitests.
Definition: flow.c:1259
void SCClassConfInit(void)
Data structures and function prototypes for keeping state for the detection engine.
void AppLayerHtpEnableRequestBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
void AppLayerProtoDetectUnittestsRegister(void)
Register unittests for app layer proto detection module.
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
void SigTableRegisterTests(void)
void DecodeTCPRegisterTests(void)
Definition: decode-tcp.c:518
void DecodeMPLSRegisterTests(void)
Definition: decode-mpls.c:323
void GlobalsInitPreConfig(void)
Definition: suricata.c:324
void AppLayerUnittestsRegister(void)
Definition: app-layer.c:2593
void AppLayerParserRegisterUnittests(void)
void DecodeEthernetRegisterTests(void)
Registers Ethernet unit tests.
void TagInitCtx(void)
void DecodeVLANRegisterTests(void)
Definition: decode-vlan.c:316
void DecodeIPV6RegisterTests(void)
this function registers unit tests for IPV6 decoder
Definition: decode-ipv6.c:924
void MemcmpRegisterTests(void)
Definition: util-memcmp.c:384
void PacketPoolDestroy(void)
void DcePayloadRegisterTests(void)
void BloomFilterRegisterTests(void)
void PacketPoolInit(void)
void MemrchrRegisterTests(void)
Definition: util-memrchr.c:63
void MimeDecRegisterTests(void)
void MpmRegisterTests(void)
Definition: util-mpm.c:621
void HashListTableRegisterTests(void)
void DecodeGRERegisterTests(void)
this function registers unit tests for GRE decoder
Definition: decode-gre.c:403
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
void ByteRegisterTests(void)
Definition: util-byte.c:654
void DetectEngineInspectModbusRegisterTests(void)
void SigRegisterTests(void)
Definition: detect.c:5323
void StorageInit(void)
Definition: util-storage.c:67
void ConfYamlRegisterTests(void)
#define DEFAULT_PACKET_SIZE
Definition: decode.h:624
void ConfRegisterTests(void)
Definition: conf.c:1467
uint32_t default_packet_size
Definition: decode.h:627
void HashTableRegisterTests(void)
Definition: util-hash.c:427
void DetectAddressTests(void)
void DecodeAsn1RegisterTests(void)
void IPPairRegisterUnittests(void)
Definition: ippair.c:775
const char * regex_arg
void StatsRegisterTests(void)
Definition: counters.c:1576
void HostBitRegisterTests(void)
Definition: host-bit.c:509
void DecodePPPOERegisterTests(void)
Registers PPPOE unit tests.
Definition: decode-pppoe.c:455
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:144
void SigGroupHeadRegisterTests(void)
void TmModuleRegisterTests(void)
register all unittests for the tm modules
Definition: tm-modules.c:163
void DeStateRegisterTests(void)
void UtCleanup(void)
Cleanup unit test list.
void SCLogRegisterTests()
Definition: util-debug.c:1676
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
void DecodeICMPV4RegisterTests(void)
Registers ICMPV4 unit test.
void SCSigRegisterSignatureOrderingTests(void)
void SCThresholdConfRegisterTests(void)
This function registers unit tests for Classification Config API.
void MagicRegisterTests(void)
Definition: util-magic.c:661
void FlowBitRegisterTests(void)
Definition: flow-bit.c:402
void TmqhSetup(void)
void DetectEngineRegisterTests()