Coverage Report

Created: 2026-08-14 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/runmode-unittests.c
Line
Count
Source
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"
38
#include "detect-engine-dcepayload.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"
74
#include "util-classification-config.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-bloomfilter.h"
89
#include "util-bloomfilter-counting.h"
90
#include "util-pool.h"
91
#include "util-byte.h"
92
#include "util-proto-name.h"
93
#include "util-macset.h"
94
#include "util-memrchr.h"
95
96
#include "util-mpm-ac.h"
97
#include "util-mpm-hs.h"
98
99
#include "conf.h"
100
#include "conf-yaml-loader.h"
101
#include "tmqh-flow.h"
102
#include "defrag.h"
103
#include "detect-engine-siggroup.h"
104
105
#include "util-streaming-buffer.h"
106
#include "util-lua.h"
107
#include "util-luajit.h"
108
#include "tm-modules.h"
109
#include "tmqh-packetpool.h"
110
#include "decode-chdlc.h"
111
#include "decode-geneve.h"
112
#include "decode-nsh.h"
113
#include "decode-raw.h"
114
#include "decode-vntag.h"
115
#include "decode-vxlan.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
{
134
    UTHRegisterTests();
135
    StreamTcpRegisterTests();
136
    SigRegisterTests();
137
    SCReputationRegisterTests();
138
    TmModuleRegisterTests();
139
    SigTableRegisterTests();
140
    HashTableRegisterTests();
141
    HashListTableRegisterTests();
142
    BloomFilterRegisterTests();
143
    BloomFilterCountingRegisterTests();
144
    PoolRegisterTests();
145
    ByteRegisterTests();
146
    MpmRegisterTests();
147
    FlowBitRegisterTests();
148
    HostBitRegisterTests();
149
    IPPairBitRegisterTests();
150
    StatsRegisterTests();
151
    DecodeEthernetRegisterTests();
152
    DecodeCHDLCRegisterTests();
153
    DecodePPPRegisterTests();
154
    DecodeVLANRegisterTests();
155
    DecodeVNTagRegisterTests();
156
    DecodeGeneveRegisterTests();
157
    DecodeVXLANRegisterTests();
158
    DecodeRawRegisterTests();
159
    DecodePPPOERegisterTests();
160
    DecodeICMPV4RegisterTests();
161
    DecodeICMPV6RegisterTests();
162
    DecodeIPV4RegisterTests();
163
    DecodeIPV6RegisterTests();
164
    DecodeTCPRegisterTests();
165
    DecodeUDPV4RegisterTests();
166
    DecodeGRERegisterTests();
167
    DecodeESPRegisterTests();
168
    DecodeMPLSRegisterTests();
169
    DecodeNSHRegisterTests();
170
    AppLayerProtoDetectUnittestsRegister();
171
    ConfRegisterTests();
172
    ConfYamlRegisterTests();
173
    TmqhFlowRegisterTests();
174
    FlowRegisterTests();
175
    HostRegisterUnittests();
176
    IPPairRegisterUnittests();
177
    SCSigRegisterSignatureOrderingTests();
178
    SCRadixRegisterTests();
179
    DefragRegisterTests();
180
    SigGroupHeadRegisterTests();
181
    SCHInfoRegisterTests();
182
    SCRuleVarsRegisterTests();
183
    AppLayerParserRegisterUnittests();
184
    ThreadMacrosRegisterTests();
185
    UtilSpmSearchRegistertests();
186
    UtilActionRegisterTests();
187
    Base64RegisterTests();
188
    SCClassConfRegisterTests();
189
    SCThresholdConfRegisterTests();
190
    SCRConfRegisterTests();
191
    PayloadRegisterTests();
192
    DcePayloadRegisterTests();
193
#ifdef PROFILING
194
    SCProfilingRegisterTests();
195
#endif
196
    DeStateRegisterTests();
197
    MemcmpRegisterTests();
198
    DetectEngineRegisterTests();
199
    SCLogRegisterTests();
200
    MagicRegisterTests();
201
    UtilMiscRegisterTests();
202
    DetectAddressTests();
203
    DetectProtoTests();
204
    DetectPortTests();
205
    DetectEngineAlertRegisterTests();
206
    SCAtomicRegisterTests();
207
    MemrchrRegisterTests();
208
    AppLayerUnittestsRegister();
209
    MimeDecRegisterTests();
210
    StreamingBufferRegisterTests();
211
    MacSetRegisterTests();
212
#ifdef OS_WIN32
213
    Win32SyscallRegisterTests();
214
#endif
215
#ifdef WINDIVERT
216
    SourceWinDivertRegisterTests();
217
#endif
218
    SCProtoNameRegisterTests();
219
    UtilCIDRTests();
220
    OutputJsonStatsRegisterTests();
221
}
222
#endif
223
224
/**
225
 * Run or list unittests
226
 *
227
 * \param list_unittests If set to 1, list unittests. Run them if set to 0.
228
 * \param regex_arg A regular expression to select unittests to run
229
 *
230
 * This function is terminal and will call exit after being called.
231
 */
232
233
void RunUnittests(int list_unittests, const char *regex_arg)
234
0
{
235
#ifdef UNITTESTS
236
    /* Initializations for global vars, queues, etc (memsets, mutex init..) */
237
    GlobalsInitPreConfig();
238
    EngineModeSetIDS();
239
240
#ifdef HAVE_LUAJIT
241
    if (LuajitSetupStatesPool() != 0) {
242
        exit(EXIT_FAILURE);
243
    }
244
#endif
245
246
    default_packet_size = DEFAULT_PACKET_SIZE;
247
    /* load the pattern matchers */
248
    MpmTableSetup();
249
    SpmTableSetup();
250
251
    StorageInit();
252
    AppLayerSetup();
253
254
    /* hardcoded initialization code */
255
    SigTableSetup(); /* load the rule keywords */
256
    TmqhSetup();
257
258
    TagInitCtx();
259
260
    /* test and initialize the unit testing subsystem */
261
    if (regex_arg == NULL) {
262
        regex_arg = ".*";
263
        UtRunSelftest(regex_arg); /* inits and cleans up again */
264
    }
265
    UtInitialize();
266
267
    RegisterAllModules();
268
269
    HostBitInitCtx();
270
271
    StorageFinalize();
272
273
    AppLayerHtpEnableRequestBodyCallback();
274
    AppLayerHtpNeedFileInspection();
275
276
    RegisterUnittests();
277
278
    if (list_unittests) {
279
        UtListTests(regex_arg);
280
    } else {
281
        /* global packet pool */
282
        extern uint16_t max_pending_packets;
283
        max_pending_packets = 128;
284
        PacketPoolInit();
285
286
        uint32_t failed = UtRunTests(regex_arg);
287
        PacketPoolDestroy();
288
        UtCleanup();
289
#ifdef BUILD_HYPERSCAN
290
        MpmHSGlobalCleanup();
291
#endif
292
        if (failed) {
293
            exit(EXIT_FAILURE);
294
        }
295
    }
296
297
#ifdef HAVE_LUAJIT
298
    LuajitFreeStatesPool();
299
#endif
300
301
    exit(EXIT_SUCCESS);
302
#else
303
    FatalError("Unittests are not build-in");
304
0
#endif /* UNITTESTS */
305
0
}