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-dcerpc.h"
62 #include "app-layer-dcerpc-udp.h"
63 #include "app-layer-htp.h"
64 #include "app-layer-ftp.h"
65 #include "app-layer-ssl.h"
66 #include "app-layer-ssh.h"
67 #include "app-layer-smtp.h"
68 
69 #include "util-action.h"
70 #include "util-radix-tree.h"
71 #include "util-host-os-info.h"
72 #include "util-cidr.h"
73 #include "util-unittest-helper.h"
74 #include "util-time.h"
75 #include "util-rule-vars.h"
77 #include "util-threshold-config.h"
78 #include "util-reference-config.h"
79 #include "util-profiling.h"
80 #include "util-magic.h"
81 #include "util-memcmp.h"
82 #include "util-misc.h"
83 #include "util-signal.h"
84 
85 #include "reputation.h"
86 #include "util-atomic.h"
87 #include "util-spm.h"
88 #include "util-hash.h"
89 #include "util-hashlist.h"
90 #include "util-bloomfilter.h"
92 #include "util-pool.h"
93 #include "util-byte.h"
94 #include "util-proto-name.h"
95 #include "util-memrchr.h"
96 
97 #include "util-mpm-ac.h"
98 #include "util-mpm-hs.h"
99 
100 #include "util-decode-asn1.h"
101 
102 #include "conf.h"
103 #include "conf-yaml-loader.h"
104 #include "tmqh-flow.h"
105 #include "defrag.h"
106 #include "detect-engine-siggroup.h"
107 
108 #include "util-streaming-buffer.h"
109 #include "util-lua.h"
110 
111 #ifdef OS_WIN32
112 #include "win32-syscall.h"
113 #endif
114 
115 #ifdef WINDIVERT
116 #include "source-windivert.h"
117 #endif
118 
119 #ifdef HAVE_NSS
120 #include <prinit.h>
121 #include <nss.h>
122 #endif
123 
124 #endif /* UNITTESTS */
125 
126 void TmqhSetup (void);
127 
128 #ifdef UNITTESTS
129 static void RegisterUnittests(void)
130 {
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) {
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 
SCProtoNameInit
void SCProtoNameInit()
Function to load the protocol names from the specified protocol file.
Definition: util-proto-name.c:39
util-byte.h
host.h
app-layer-dcerpc.h
TagInitCtx
void TagInitCtx(void)
Definition: detect-engine-tag.c:51
app-layer-dcerpc-udp.h
DecodeUDPV4RegisterTests
void DecodeUDPV4RegisterTests(void)
Definition: decode-udp.c:211
ippair.h
AppLayerHtpNeedFileInspection
void AppLayerHtpNeedFileInspection(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request file...
Definition: app-layer-htp.c:505
detect-engine.h
detect-engine-proto.h
SCReferenceConfInit
void SCReferenceConfInit(void)
Definition: util-reference-config.c:56
win32-syscall.h
app-layer-ssh.h
StorageInit
void StorageInit(void)
Definition: util-storage.c:67
MagicRegisterTests
void MagicRegisterTests(void)
Definition: util-magic.c:655
util-hashlist.h
detect-engine-siggroup.h
stream-tcp.h
detect-engine-modbus.h
BloomFilterCountingRegisterTests
void BloomFilterCountingRegisterTests(void)
Definition: util-bloomfilter-counting.c:393
runmode-unittests.h
TmqhSetup
void TmqhSetup(void)
Definition: tm-queuehandlers.c:39
regex_arg
const char * regex_arg
Definition: runmode-unittests.h:28
FlowRegisterTests
void FlowRegisterTests(void)
Function to register the Flow Unitests.
Definition: flow.c:1362
PoolRegisterTests
void PoolRegisterTests(void)
Definition: util-pool.c:715
SigTableSetup
void SigTableSetup(void)
Definition: detect-engine-register.c:399
DecodePPPRegisterTests
void DecodePPPRegisterTests(void)
Definition: decode-ppp.c:311
util-lua.h
IPPairRegisterUnittests
void IPPairRegisterUnittests(void)
Definition: ippair.c:781
ippair-bit.h
SC_ERR_NOT_SUPPORTED
@ SC_ERR_NOT_SUPPORTED
Definition: util-error.h:257
DcePayloadRegisterTests
void DcePayloadRegisterTests(void)
Definition: detect-engine-dcepayload.c:3703
UtListTests
void UtListTests(const char *regex_arg)
List all registered unit tests.
Definition: util-unittest.c:160
util-hash.h
MpmRegisterTests
void MpmRegisterTests(void)
Definition: util-mpm.c:627
TmqhFlowRegisterTests
void TmqhFlowRegisterTests(void)
Definition: tmqh-flow.c:381
UtRunSelftest
int UtRunSelftest(const char *regex_arg)
Run self tests.
Definition: util-unittest.c:322
RegisterAllModules
void RegisterAllModules(void)
Definition: suricata.c:851
flow-bit.h
TmModuleRegisterTests
void TmModuleRegisterTests(void)
register all unittests for the tm modules
Definition: tm-modules.c:165
util-bloomfilter-counting.h
SCSigRegisterSignatureOrderingTests
void SCSigRegisterSignatureOrderingTests(void)
Definition: detect-engine-sigorder.c:2147
AppLayerUnittestsRegister
void AppLayerUnittestsRegister(void)
Definition: app-layer.c:2626
GlobalsInitPreConfig
void GlobalsInitPreConfig(void)
Definition: suricata.c:330
MemrchrRegisterTests
void MemrchrRegisterTests(void)
Definition: util-memrchr.c:63
app-layer-ftp.h
detect-engine-payload.h
AppLayerParserRegisterUnittests
void AppLayerParserRegisterUnittests(void)
Definition: app-layer-parser.c:1854
HostBitInitCtx
void HostBitInitCtx(void)
Definition: host-bit.c:49
util-unittest.h
util-unittest-helper.h
SigGroupHeadRegisterTests
void SigGroupHeadRegisterTests(void)
Definition: detect-engine-siggroup.c:1205
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:370
SigRegisterTests
void SigRegisterTests(void)
Definition: detect.c:5323
SigTableRegisterTests
void SigTableRegisterTests(void)
Definition: detect-engine-register.c:590
UtilSpmSearchRegistertests
void UtilSpmSearchRegistertests(void)
Definition: util-spm.c:2671
FlowBitRegisterTests
void FlowBitRegisterTests(void)
Definition: flow-bit.c:402
DecodeMPLSRegisterTests
void DecodeMPLSRegisterTests(void)
Definition: decode-mpls.c:377
util-memcmp.h
util-signal.h
DecodeEthernetRegisterTests
void DecodeEthernetRegisterTests(void)
Registers Ethernet unit tests.
Definition: decode-ethernet.c:220
AppLayerSetup
int AppLayerSetup(void)
Setup the app layer.
Definition: app-layer.c:798
app-layer-detect-proto.h
util-cidr.h
app-layer-htp.h
ByteRegisterTests
void ByteRegisterTests(void)
Definition: util-byte.c:772
SCRadixRegisterTests
void SCRadixRegisterTests(void)
Definition: util-radix-tree.c:4046
SpmTableSetup
void SpmTableSetup(void)
Definition: util-spm.c:117
UtInitialize
void UtInitialize(void)
Initialize unit test list.
Definition: util-unittest.c:256
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1180
SCRuleVarsRegisterTests
void SCRuleVarsRegisterTests(void)
Definition: util-rule-vars.c:520
SCLogRegisterTests
void SCLogRegisterTests()
Definition: util-debug.c:1671
AppLayerHtpEnableRequestBodyCallback
void AppLayerHtpEnableRequestBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
Definition: app-layer-htp.c:463
HashTableRegisterTests
void HashTableRegisterTests(void)
Definition: util-hash.c:427
util-reference-config.h
detect-engine-mpm.h
DefragRegisterTests
void DefragRegisterTests(void)
Definition: defrag.c:2484
DetectAddressTests
void DetectAddressTests(void)
Definition: detect-engine-address.c:4804
pkt-var.h
SCHInfoRegisterTests
void SCHInfoRegisterTests(void)
Definition: util-host-os-info.c:1605
util-bloomfilter.h
detect-engine-port.h
util-atomic.h
util-time.h
app-layer-parser.h
detect-engine-tag.h
util-profiling.h
util-rule-vars.h
conf-yaml-loader.h
DecodeICMPV6RegisterTests
void DecodeICMPV6RegisterTests(void)
Registers ICMPV6 unit tests.
Definition: decode-icmpv6.c:1585
conf.h
util-magic.h
StorageFinalize
int StorageFinalize(void)
Definition: util-storage.c:139
util-radix-tree.h
source-windivert.h
SCAtomicRegisterTests
void SCAtomicRegisterTests(void)
Definition: util-atomic.c:66
detect-engine-state.h
Data structures and function prototypes for keeping state for the detection engine.
flow-timeout.h
DecodeTCPRegisterTests
void DecodeTCPRegisterTests(void)
Definition: decode-tcp.c:553
reputation.h
util-action.h
UtRunTests
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
Definition: util-unittest.c:190
util-mpm-ac.h
util-proto-name.h
defrag.h
MpmTableSetup
void MpmTableSetup(void)
Definition: util-mpm.c:279
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
DEFAULT_PACKET_SIZE
#define DEFAULT_PACKET_SIZE
Definition: decode.h:615
BloomFilterRegisterTests
void BloomFilterRegisterTests(void)
Definition: util-bloomfilter.c:275
util-host-os-info.h
HostBitRegisterTests
void HostBitRegisterTests(void)
Definition: host-bit.c:509
default_packet_size
uint32_t default_packet_size
Definition: decode.c:71
DecodeVLANRegisterTests
void DecodeVLANRegisterTests(void)
Definition: decode-vlan.c:316
util-decode-asn1.h
detect-fast-pattern.h
DetectPortTests
void DetectPortTests(void)
Definition: detect-engine-port.c:2435
ConfYamlRegisterTests
void ConfYamlRegisterTests(void)
Definition: conf-yaml-loader.c:956
flow-manager.h
suricata-common.h
ConfRegisterTests
void ConfRegisterTests(void)
Definition: conf.c:1467
util-streaming-buffer.h
MimeDecRegisterTests
void MimeDecRegisterTests(void)
Definition: util-decode-mime.c:3147
util-spm.h
SCLogError
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
DecodePPPOERegisterTests
void DecodePPPOERegisterTests(void)
Registers PPPOE unit tests.
Definition: decode-pppoe.c:457
util-classification-config.h
DecodeAsn1RegisterTests
void DecodeAsn1RegisterTests(void)
Definition: util-decode-asn1.c:888
SCClassConfInit
void SCClassConfInit(void)
Definition: util-classification-config.c:63
CIDRInit
void CIDRInit(void)
Definition: util-cidr.c:31
SCClassConfRegisterTests
void SCClassConfRegisterTests(void)
This function registers unit tests for Classification Config API.
Definition: util-classification-config.c:820
UtCleanup
void UtCleanup(void)
Cleanup unit test list.
Definition: util-unittest.c:265
detect-engine-sigorder.h
AppLayerProtoDetectUnittestsRegister
void AppLayerProtoDetectUnittestsRegister(void)
Register unittests for app layer proto detection module.
Definition: app-layer-detect-proto.c:3618
DecodeVXLANRegisterTests
void DecodeVXLANRegisterTests(void)
Definition: decode-vxlan.c:262
unix-manager.h
SCRConfRegisterTests
void SCRConfRegisterTests(void)
This function registers unit tests for Reference Config API.
Definition: util-reference-config.c:783
StreamTcpRegisterTests
void StreamTcpRegisterTests(void)
Definition: stream-tcp.c:10624
HashListTableRegisterTests
void HashListTableRegisterTests(void)
Definition: util-hashlist.c:501
max_pending_packets
int max_pending_packets
Definition: suricata.c:212
DecodeGRERegisterTests
void DecodeGRERegisterTests(void)
this function registers unit tests for GRE decoder
Definition: decode-gre.c:397
UtilMiscRegisterTests
void UtilMiscRegisterTests(void)
Definition: util-misc.c:1174
detect-parse.h
DecodeICMPV4RegisterTests
void DecodeICMPV4RegisterTests(void)
Registers ICMPV4 unit test.
Definition: decode-icmpv4.c:799
RunUnittests
void RunUnittests(int list_unittests, const char *regex_arg)
Definition: runmode-unittests.c:221
SCReputationRegisterTests
void SCReputationRegisterTests(void)
Definition: reputation.c:147
SCThresholdConfRegisterTests
void SCThresholdConfRegisterTests(void)
This function registers unit tests for Classification Config API.
Definition: util-threshold-config.c:2729
HostRegisterUnittests
void HostRegisterUnittests(void)
Definition: host.c:738
tmqh-flow.h
DecodeIPV4RegisterTests
void DecodeIPV4RegisterTests(void)
Definition: decode-ipv4.c:1643
util-mpm-hs.h
SCProfilingRegisterTests
void SCProfilingRegisterTests(void)
Definition: util-profiling.c:1449
IPPairBitRegisterTests
void IPPairBitRegisterTests(void)
Definition: ippair-bit.c:493
DetectEngineInspectModbusRegisterTests
void DetectEngineInspectModbusRegisterTests(void)
Definition: detect-engine-modbus.c:1444
app-layer-smtp.h
UtilActionRegisterTests
void UtilActionRegisterTests(void)
Definition: util-action.c:1596
util-misc.h
PacketPoolDestroy
void PacketPoolDestroy(void)
Definition: tmqh-packetpool.c:407
flow.h
ThreadMacrosRegisterTests
void ThreadMacrosRegisterTests(void)
this function registers unit tests for DetectId
Definition: threads.c:144
MemcmpRegisterTests
void MemcmpRegisterTests(void)
Definition: util-memcmp.c:384
DeStateRegisterTests
void DeStateRegisterTests(void)
Definition: detect-engine-state.c:1358
detect-engine-dcepayload.h
util-pool.h
flow-var.h
DetectEngineRegisterTests
void DetectEngineRegisterTests()
Definition: detect-engine.c:4422
util-memrchr.h
app-layer-ssl.h
DecodeIPV6RegisterTests
void DecodeIPV6RegisterTests(void)
this function registers unit tests for IPV6 decoder
Definition: decode-ipv6.c:912
detect-engine-address.h
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1559
util-threshold-config.h
host-bit.h
app-layer.h
StreamingBufferRegisterTests
void StreamingBufferRegisterTests(void)
Definition: util-streaming-buffer.c:1477
PayloadRegisterTests
void PayloadRegisterTests(void)
Definition: detect-engine-payload.c:1317
MpmHSGlobalCleanup
void MpmHSGlobalCleanup(void)
DecodeRawRegisterTests
void DecodeRawRegisterTests(void)
Registers Raw unit tests.
Definition: decode-raw.c:230
DetectProtoTests
void DetectProtoTests(void)
this function registers unit tests for DetectProto
Definition: detect-engine-proto.c:579