suricata
runmode-unittests.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2022 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 "runmode-unittests.h"
25 #include "util-unittest.h"
26 
27 #include "util-debug.h"
28 #ifdef UNITTESTS
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-alert.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-enip.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-htp.h"
62 #include "app-layer-ftp.h"
63 #include "app-layer-ssl.h"
64 #include "app-layer-ssh.h"
65 #include "app-layer-smtp.h"
66 
67 #include "util-action.h"
68 #include "util-radix-tree.h"
69 #include "util-host-os-info.h"
70 #include "util-cidr.h"
71 #include "util-unittest-helper.h"
72 #include "util-time.h"
73 #include "util-rule-vars.h"
75 #include "util-threshold-config.h"
76 #include "util-reference-config.h"
77 #include "util-profiling.h"
78 #include "util-magic.h"
79 #include "util-memcmp.h"
80 #include "util-misc.h"
81 #include "util-signal.h"
82 
83 #include "reputation.h"
84 #include "util-atomic.h"
85 #include "util-spm.h"
86 #include "util-hash.h"
87 #include "util-hashlist.h"
88 #include "util-pool.h"
89 #include "util-byte.h"
90 #include "util-proto-name.h"
91 #include "util-macset.h"
92 #include "util-memrchr.h"
93 
94 #include "util-mpm-ac.h"
95 #include "util-mpm-hs.h"
96 
97 #include "conf.h"
98 #include "conf-yaml-loader.h"
99 #include "tmqh-flow.h"
100 #include "defrag.h"
101 #include "detect-engine-siggroup.h"
102 
103 #include "util-streaming-buffer.h"
104 #include "util-lua.h"
105 #include "util-luajit.h"
106 #include "tm-modules.h"
107 #include "tmqh-packetpool.h"
108 #include "decode-chdlc.h"
109 #include "decode-geneve.h"
110 #include "decode-nsh.h"
111 #include "decode-pppoe.h"
112 #include "decode-raw.h"
113 #include "decode-vntag.h"
114 #include "decode-vxlan.h"
115 #include "decode-pppoe.h"
116 
117 #include "output-json-stats.h"
118 
119 #ifdef OS_WIN32
120 #include "win32-syscall.h"
121 #endif
122 
123 #ifdef WINDIVERT
124 #include "source-windivert.h"
125 #endif
126 
127 #endif /* UNITTESTS */
128 
129 void TmqhSetup (void);
130 
131 #ifdef UNITTESTS
132 static void RegisterUnittests(void)
133 {
191 #ifdef PROFILING
193 #endif
202  DetectPortTests();
210 #ifdef OS_WIN32
211  Win32SyscallRegisterTests();
212 #endif
213 #ifdef WINDIVERT
214  SourceWinDivertRegisterTests();
215 #endif
217  UtilCIDRTests();
219 }
220 #endif
221 
222 /**
223  * Run or list unittests
224  *
225  * \param list_unittests If set to 1, list unittests. Run them if set to 0.
226  * \param regex_arg A regular expression to select unittests to run
227  *
228  * This function is terminal and will call exit after being called.
229  */
230 
231 void RunUnittests(int list_unittests, const char *regex_arg)
232 {
233 #ifdef UNITTESTS
234  /* Initializations for global vars, queues, etc (memsets, mutex init..) */
237 
238 #ifdef HAVE_LUAJIT
239  if (LuajitSetupStatesPool() != 0) {
240  exit(EXIT_FAILURE);
241  }
242 #endif
243 
245  /* load the pattern matchers */
246  MpmTableSetup();
247  SpmTableSetup();
248 
249  StorageInit();
250  AppLayerSetup();
251 
252  /* hardcoded initialization code */
253  SigTableSetup(); /* load the rule keywords */
254  TmqhSetup();
255 
256  TagInitCtx();
257 
258  UtInitialize();
259 
261 
262  HostBitInitCtx();
263 
264  StorageFinalize();
265  /* test and initialize the unit testing subsystem */
266  if (regex_arg == NULL){
267  regex_arg = ".*";
268  UtRunSelftest(regex_arg); /* inits and cleans up again */
269  }
270 
273 
274  RegisterUnittests();
275 
276  if (list_unittests) {
278  } else {
279  /* global packet pool */
280  extern uint16_t max_pending_packets;
281  max_pending_packets = 128;
282  PacketPoolInit();
283 
284  uint32_t failed = UtRunTests(regex_arg);
286  UtCleanup();
287 #ifdef BUILD_HYPERSCAN
289 #endif
290  if (failed) {
291  exit(EXIT_FAILURE);
292  }
293  }
294 
295 #ifdef HAVE_LUAJIT
296  LuajitFreeStatesPool();
297 #endif
298 
299  exit(EXIT_SUCCESS);
300 #else
301  FatalError("Unittests are not build-in");
302 #endif /* UNITTESTS */
303 }
util-byte.h
host.h
decode-raw.h
SCProtoNameRegisterTests
void SCProtoNameRegisterTests(void)
Definition: util-proto-name.c:503
TagInitCtx
void TagInitCtx(void)
Definition: detect-engine-tag.c:52
DecodeUDPV4RegisterTests
void DecodeUDPV4RegisterTests(void)
Definition: decode-udp.c:221
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:516
detect-engine.h
decode-vxlan.h
detect-engine-proto.h
win32-syscall.h
DetectAddressTests
void DetectAddressTests(void)
DecodeESPRegisterTests
void DecodeESPRegisterTests(void)
Definition: decode-esp.c:196
app-layer-ssh.h
StorageInit
void StorageInit(void)
Definition: util-storage.c:68
MagicRegisterTests
void MagicRegisterTests(void)
Definition: util-magic.c:581
util-hashlist.h
detect-engine-siggroup.h
decode-pppoe.h
stream-tcp.h
runmode-unittests.h
TmqhSetup
void TmqhSetup(void)
Definition: tm-queuehandlers.c:39
regex_arg
const char * regex_arg
Definition: runmode-unittests.h:27
FlowRegisterTests
void FlowRegisterTests(void)
Function to register the Flow Unitests.
Definition: flow.c:1444
PoolRegisterTests
void PoolRegisterTests(void)
Definition: util-pool.c:706
SigTableSetup
void SigTableSetup(void)
Definition: detect-engine-register.c:473
DecodePPPRegisterTests
void DecodePPPRegisterTests(void)
Definition: decode-ppp.c:385
util-lua.h
IPPairRegisterUnittests
void IPPairRegisterUnittests(void)
Definition: ippair.c:761
ippair-bit.h
DcePayloadRegisterTests
void DcePayloadRegisterTests(void)
Definition: detect-engine-dcepayload.c:748
util-macset.h
UtListTests
void UtListTests(const char *regex_arg)
List all registered unit tests.
Definition: util-unittest.c:157
util-hash.h
MpmRegisterTests
void MpmRegisterTests(void)
Definition: util-mpm.c:575
TmqhFlowRegisterTests
void TmqhFlowRegisterTests(void)
Definition: tmqh-flow.c:406
UtRunSelftest
int UtRunSelftest(const char *regex_arg)
Run self tests.
Definition: util-unittest.c:323
RegisterAllModules
void RegisterAllModules(void)
Definition: suricata.c:897
flow-bit.h
tm-modules.h
TmModuleRegisterTests
void TmModuleRegisterTests(void)
register all unittests for the tm modules
Definition: tm-modules.c:155
DecodeNSHRegisterTests
void DecodeNSHRegisterTests(void)
Definition: decode-nsh.c:283
SCSigRegisterSignatureOrderingTests
void SCSigRegisterSignatureOrderingTests(void)
Definition: detect-engine-sigorder.c:1973
AppLayerUnittestsRegister
void AppLayerUnittestsRegister(void)
Definition: app-layer.c:2947
GlobalsInitPreConfig
void GlobalsInitPreConfig(void)
Definition: suricata.c:361
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:1993
HostBitInitCtx
void HostBitInitCtx(void)
Definition: host-bit.c:49
tmqh-packetpool.h
util-unittest.h
util-unittest-helper.h
SigGroupHeadRegisterTests
void SigGroupHeadRegisterTests(void)
Definition: detect-engine-siggroup.c:1089
PacketPoolInit
void PacketPoolInit(void)
Definition: tmqh-packetpool.c:246
SigRegisterTests
void SigRegisterTests(void)
Definition: detect.c:4961
OutputJsonStatsRegisterTests
void OutputJsonStatsRegisterTests(void)
Definition: output-json-stats.c:131
SigTableRegisterTests
void SigTableRegisterTests(void)
Definition: detect-engine-register.c:739
UtilSpmSearchRegistertests
void UtilSpmSearchRegistertests(void)
Definition: util-spm.c:2657
FlowBitRegisterTests
void FlowBitRegisterTests(void)
Definition: flow-bit.c:342
DecodeMPLSRegisterTests
void DecodeMPLSRegisterTests(void)
Definition: decode-mpls.c:342
util-memcmp.h
detect-engine-enip.h
util-signal.h
DecodeEthernetRegisterTests
void DecodeEthernetRegisterTests(void)
Registers Ethernet unit tests.
Definition: decode-ethernet.c:176
AppLayerSetup
int AppLayerSetup(void)
Setup the app layer.
Definition: app-layer.c:1032
app-layer-detect-proto.h
util-cidr.h
app-layer-htp.h
ByteRegisterTests
void ByteRegisterTests(void)
Definition: util-byte.c:1034
SCRadixRegisterTests
void SCRadixRegisterTests(void)
Definition: util-radix-tree.c:3787
util-debug.h
DecodeCHDLCRegisterTests
void DecodeCHDLCRegisterTests(void)
Registers Ethernet unit tests.
Definition: decode-chdlc.c:105
EngineModeSetIDS
void EngineModeSetIDS(void)
Definition: suricata.c:248
DecodeVNTagRegisterTests
void DecodeVNTagRegisterTests(void)
Definition: decode-vntag.c:172
SpmTableSetup
void SpmTableSetup(void)
Definition: util-spm.c:122
UtInitialize
void UtInitialize(void)
Initialize unit test list.
Definition: util-unittest.c:260
UTHRegisterTests
void UTHRegisterTests(void)
Definition: util-unittest-helper.c:1123
SCRuleVarsRegisterTests
void SCRuleVarsRegisterTests(void)
Definition: util-rule-vars.c:400
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:474
HashTableRegisterTests
void HashTableRegisterTests(void)
Definition: util-hash.c:417
util-reference-config.h
detect-engine-mpm.h
DefragRegisterTests
void DefragRegisterTests(void)
Definition: defrag.c:3170
pkt-var.h
SCHInfoRegisterTests
void SCHInfoRegisterTests(void)
Definition: util-host-os-info.c:1610
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
StreamTcpRegisterTests
void StreamTcpRegisterTests(void)
Definition: stream-tcp.c:3405
DecodeICMPV6RegisterTests
void DecodeICMPV6RegisterTests(void)
Registers ICMPV6 unit tests.
Definition: decode-icmpv6.c:1537
conf.h
detect-engine-alert.h
util-magic.h
StorageFinalize
int StorageFinalize(void)
Definition: util-storage.c:138
util-radix-tree.h
source-windivert.h
decode-nsh.h
SCAtomicRegisterTests
void SCAtomicRegisterTests(void)
Definition: util-atomic.c:67
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:529
reputation.h
SCLogRegisterTests
void SCLogRegisterTests(void)
Definition: util-debug.c:1821
max_pending_packets
uint16_t max_pending_packets
Definition: suricata.c:183
util-action.h
UtRunTests
uint32_t UtRunTests(const char *regex_arg)
Run all registered unittests.
Definition: util-unittest.c:188
util-mpm-ac.h
util-proto-name.h
defrag.h
MpmTableSetup
void MpmTableSetup(void)
Definition: util-mpm.c:225
MacSetRegisterTests
void MacSetRegisterTests(void)
Definition: util-macset.c:421
DEFAULT_PACKET_SIZE
#define DEFAULT_PACKET_SIZE
Definition: decode.h:691
DetectEngineRegisterTests
void DetectEngineRegisterTests(void)
Definition: detect-engine.c:5064
util-host-os-info.h
UtilCIDRTests
void UtilCIDRTests(void)
Definition: util-cidr.c:149
HostBitRegisterTests
void HostBitRegisterTests(void)
Definition: host-bit.c:508
default_packet_size
uint32_t default_packet_size
Definition: decode.c:77
DecodeVLANRegisterTests
void DecodeVLANRegisterTests(void)
Definition: decode-vlan.c:257
detect-fast-pattern.h
Base64RegisterTests
void Base64RegisterTests(void)
Definition: util-base64.c:495
ConfYamlRegisterTests
void ConfYamlRegisterTests(void)
Definition: conf-yaml-loader.c:1056
flow-manager.h
suricata-common.h
output-json-stats.h
ConfRegisterTests
void ConfRegisterTests(void)
Definition: conf.c:1462
util-streaming-buffer.h
MimeDecRegisterTests
void MimeDecRegisterTests(void)
Definition: util-decode-mime.c:3572
util-spm.h
DecodePPPOERegisterTests
void DecodePPPOERegisterTests(void)
Registers PPPOE unit tests.
Definition: decode-pppoe.c:540
util-classification-config.h
FatalError
#define FatalError(...)
Definition: util-debug.h:502
decode-geneve.h
SCClassConfRegisterTests
void SCClassConfRegisterTests(void)
This function registers unit tests for Classification Config API.
Definition: util-classification-config.c:790
UtCleanup
void UtCleanup(void)
Cleanup unit test list.
Definition: util-unittest.c:269
detect-engine-sigorder.h
AppLayerProtoDetectUnittestsRegister
void AppLayerProtoDetectUnittestsRegister(void)
Register unittests for app layer proto detection module.
Definition: app-layer-detect-proto.c:3630
DecodeVXLANRegisterTests
void DecodeVXLANRegisterTests(void)
Definition: decode-vxlan.c:270
unix-manager.h
SCRConfRegisterTests
void SCRConfRegisterTests(void)
This function registers unit tests for Reference Config API.
Definition: util-reference-config.c:784
HashListTableRegisterTests
void HashListTableRegisterTests(void)
Definition: util-hashlist.c:498
DecodeGRERegisterTests
void DecodeGRERegisterTests(void)
this function registers unit tests for GRE decoder
Definition: decode-gre.c:395
UtilMiscRegisterTests
void UtilMiscRegisterTests(void)
Definition: util-misc.c:773
detect-parse.h
DecodeICMPV4RegisterTests
void DecodeICMPV4RegisterTests(void)
Registers ICMPV4 unit test.
Definition: decode-icmpv4.c:793
RunUnittests
void RunUnittests(int list_unittests, const char *regex_arg)
Definition: runmode-unittests.c:231
SCReputationRegisterTests
void SCReputationRegisterTests(void)
Definition: reputation.c:174
decode-chdlc.h
SCThresholdConfRegisterTests
void SCThresholdConfRegisterTests(void)
This function registers unit tests for Classification Config API.
Definition: util-threshold-config.c:2575
HostRegisterUnittests
void HostRegisterUnittests(void)
Definition: host.c:723
tmqh-flow.h
DecodeIPV4RegisterTests
void DecodeIPV4RegisterTests(void)
Definition: decode-ipv4.c:1555
DetectEngineAlertRegisterTests
void DetectEngineAlertRegisterTests(void)
Registers Detect Engine Alert unit tests.
Definition: detect-engine-alert.c:71
util-mpm-hs.h
SCProfilingRegisterTests
void SCProfilingRegisterTests(void)
Definition: util-profiling.c:1403
IPPairBitRegisterTests
void IPPairBitRegisterTests(void)
Definition: ippair-bit.c:492
app-layer-smtp.h
DetectPortTests
void DetectPortTests(void)
UtilActionRegisterTests
void UtilActionRegisterTests(void)
Definition: util-action.c:427
util-misc.h
PacketPoolDestroy
void PacketPoolDestroy(void)
Definition: tmqh-packetpool.c:277
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:351
util-luajit.h
decode-vntag.h
DeStateRegisterTests
void DeStateRegisterTests(void)
Definition: detect-engine-state.c:1447
detect-engine-dcepayload.h
util-pool.h
DecodeGeneveRegisterTests
void DecodeGeneveRegisterTests(void)
Definition: decode-geneve.c:490
flow-var.h
util-memrchr.h
app-layer-ssl.h
DecodeIPV6RegisterTests
void DecodeIPV6RegisterTests(void)
this function registers unit tests for IPV6 decoder
Definition: decode-ipv6.c:901
detect-engine-address.h
StatsRegisterTests
void StatsRegisterTests(void)
Definition: counters.c:1584
util-threshold-config.h
host-bit.h
app-layer.h
StreamingBufferRegisterTests
void StreamingBufferRegisterTests(void)
Definition: util-streaming-buffer.c:2371
PayloadRegisterTests
void PayloadRegisterTests(void)
Definition: detect-engine-payload.c:1142
MpmHSGlobalCleanup
void MpmHSGlobalCleanup(void)
DecodeRawRegisterTests
void DecodeRawRegisterTests(void)
Registers Raw unit tests.
Definition: decode-raw.c:217
DetectProtoTests
void DetectProtoTests(void)
this function registers unit tests for DetectProto
Definition: detect-engine-proto.c:394