/src/suricata8/src/runmode-unittests.c
Line | Count | Source |
1 | | /* Copyright (C) 2013-2024 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-fast-pattern.h" |
42 | | #include "flow.h" |
43 | | #include "flow-timeout.h" |
44 | | #include "flow-manager.h" |
45 | | #include "flow-var.h" |
46 | | #include "flow-bit.h" |
47 | | #include "tm-threads.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-radix4-tree.h" |
69 | | #include "util-radix6-tree.h" |
70 | | #include "util-host-os-info.h" |
71 | | #include "util-cidr.h" |
72 | | #include "util-coredump-config.h" |
73 | | #include "util-unittest-helper.h" |
74 | | #include "util-time.h" |
75 | | #include "util-rule-vars.h" |
76 | | #include "util-classification-config.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 | | #include "util-affinity.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-pool.h" |
92 | | #include "util-byte.h" |
93 | | #include "util-file.h" |
94 | | #include "util-proto-name.h" |
95 | | #include "util-macset.h" |
96 | | #include "util-flow-rate.h" |
97 | | #include "util-memrchr.h" |
98 | | |
99 | | #include "util-mpm-ac.h" |
100 | | #include "util-mpm-hs.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 | | #include "util-lua-common.h" |
111 | | #include "tm-modules.h" |
112 | | #include "tmqh-packetpool.h" |
113 | | #include "decode-chdlc.h" |
114 | | #include "decode-geneve.h" |
115 | | #include "decode-nsh.h" |
116 | | #include "decode-pppoe.h" |
117 | | #include "decode-raw.h" |
118 | | #include "decode-vntag.h" |
119 | | #include "decode-vxlan.h" |
120 | | #include "decode-pppoe.h" |
121 | | |
122 | | #include "output-json-stats.h" |
123 | | |
124 | | #ifdef OS_WIN32 |
125 | | #include "win32-syscall.h" |
126 | | #endif |
127 | | |
128 | | #ifdef WINDIVERT |
129 | | #include "source-windivert.h" |
130 | | #endif |
131 | | |
132 | | #endif /* UNITTESTS */ |
133 | | |
134 | | void TmqhSetup (void); |
135 | | |
136 | | #ifdef UNITTESTS |
137 | | static void RegisterUnittests(void) |
138 | | { |
139 | | UTHRegisterTests(); |
140 | | StreamTcpRegisterTests(); |
141 | | SigRegisterTests(); |
142 | | SCReputationRegisterTests(); |
143 | | TmModuleRegisterTests(); |
144 | | SigTableRegisterTests(); |
145 | | HashTableRegisterTests(); |
146 | | HashListTableRegisterTests(); |
147 | | PoolRegisterTests(); |
148 | | ByteRegisterTests(); |
149 | | MpmRegisterTests(); |
150 | | FlowBitRegisterTests(); |
151 | | HostBitRegisterTests(); |
152 | | IPPairBitRegisterTests(); |
153 | | StatsRegisterTests(); |
154 | | DecodeEthernetRegisterTests(); |
155 | | DecodeCHDLCRegisterTests(); |
156 | | DecodePPPRegisterTests(); |
157 | | DecodeVLANRegisterTests(); |
158 | | DecodeVNTagRegisterTests(); |
159 | | DecodeGeneveRegisterTests(); |
160 | | DecodeVXLANRegisterTests(); |
161 | | DecodeRawRegisterTests(); |
162 | | DecodePPPOERegisterTests(); |
163 | | DecodeICMPV4RegisterTests(); |
164 | | DecodeICMPV6RegisterTests(); |
165 | | DecodeIPV4RegisterTests(); |
166 | | DecodeIPV6RegisterTests(); |
167 | | DecodeTCPRegisterTests(); |
168 | | DecodeUDPV4RegisterTests(); |
169 | | DecodeGRERegisterTests(); |
170 | | DecodeESPRegisterTests(); |
171 | | DecodeMPLSRegisterTests(); |
172 | | DecodeNSHRegisterTests(); |
173 | | AppLayerProtoDetectUnittestsRegister(); |
174 | | SCConfRegisterTests(); |
175 | | SCConfYamlRegisterTests(); |
176 | | TmqhFlowRegisterTests(); |
177 | | FlowRegisterTests(); |
178 | | HostRegisterUnittests(); |
179 | | IPPairRegisterUnittests(); |
180 | | SCSigRegisterSignatureOrderingTests(); |
181 | | SCRadix4RegisterTests(); |
182 | | SCRadix6RegisterTests(); |
183 | | DefragRegisterTests(); |
184 | | SigGroupHeadRegisterTests(); |
185 | | SCHInfoRegisterTests(); |
186 | | SCRuleVarsRegisterTests(); |
187 | | AppLayerParserRegisterUnittests(); |
188 | | ThreadMacrosRegisterTests(); |
189 | | UtilSpmSearchRegistertests(); |
190 | | UtilActionRegisterTests(); |
191 | | SCClassConfRegisterTests(); |
192 | | SCThresholdConfRegisterTests(); |
193 | | SCRConfRegisterTests(); |
194 | | PayloadRegisterTests(); |
195 | | DcePayloadRegisterTests(); |
196 | | #ifdef PROFILING |
197 | | SCProfilingRegisterTests(); |
198 | | #endif |
199 | | DeStateRegisterTests(); |
200 | | MemcmpRegisterTests(); |
201 | | DetectEngineRegisterTests(); |
202 | | SCLogRegisterTests(); |
203 | | MagicRegisterTests(); |
204 | | UtilMiscRegisterTests(); |
205 | | ThreadingAffinityRegisterTests(); |
206 | | TmThreadsRegisterTests(); |
207 | | DetectAddressTests(); |
208 | | DetectProtoTests(); |
209 | | DetectPortTests(); |
210 | | DetectEngineAlertRegisterTests(); |
211 | | SCAtomicRegisterTests(); |
212 | | MemrchrRegisterTests(); |
213 | | AppLayerUnittestsRegister(); |
214 | | SCLuaCommonRegisterTests(); |
215 | | StreamingBufferRegisterTests(); |
216 | | MacSetRegisterTests(); |
217 | | FlowRateRegisterTests(); |
218 | | #ifdef OS_WIN32 |
219 | | Win32SyscallRegisterTests(); |
220 | | #endif |
221 | | #ifdef WINDIVERT |
222 | | SourceWinDivertRegisterTests(); |
223 | | #endif |
224 | | SCProtoNameRegisterTests(); |
225 | | FileRegisterTests(); |
226 | | UtilCIDRTests(); |
227 | | OutputJsonStatsRegisterTests(); |
228 | | CoredumpConfigRegisterTests(); |
229 | | } |
230 | | #endif |
231 | | |
232 | | /** |
233 | | * Run or list unittests |
234 | | * |
235 | | * \param list_unittests If set to 1, list unittests. Run them if set to 0. |
236 | | * \param regex_arg A regular expression to select unittests to run |
237 | | * |
238 | | * This function is terminal and will call exit after being called. |
239 | | */ |
240 | | |
241 | | void RunUnittests(int list_unittests, const char *regex_arg) |
242 | 0 | { |
243 | | #ifdef UNITTESTS |
244 | | /* Initializations for global vars, queues, etc (memsets, mutex init..) */ |
245 | | GlobalsInitPreConfig(); |
246 | | EngineModeSetIDS(); |
247 | | |
248 | | default_packet_size = DEFAULT_PACKET_SIZE; |
249 | | /* load the pattern matchers */ |
250 | | MpmTableSetup(); |
251 | | SpmTableSetup(); |
252 | | |
253 | | StorageInit(); |
254 | | AppLayerSetup(); |
255 | | |
256 | | /* hardcoded initialization code */ |
257 | | SigTableInit(); |
258 | | SigTableSetup(); /* load the rule keywords */ |
259 | | TmqhSetup(); |
260 | | |
261 | | TagInitCtx(); |
262 | | |
263 | | /* test and initialize the unit testing subsystem */ |
264 | | if (regex_arg == NULL) { |
265 | | regex_arg = ".*"; |
266 | | UtRunSelftest(regex_arg); /* inits and cleans up again */ |
267 | | } |
268 | | UtInitialize(); |
269 | | |
270 | | RegisterAllModules(); |
271 | | |
272 | | HostBitInitCtx(); |
273 | | |
274 | | StorageFinalize(); |
275 | | |
276 | | AppLayerHtpEnableRequestBodyCallback(); |
277 | | AppLayerHtpNeedFileInspection(); |
278 | | |
279 | | RegisterUnittests(); |
280 | | |
281 | | if (list_unittests) { |
282 | | UtListTests(regex_arg); |
283 | | } else { |
284 | | /* global packet pool */ |
285 | | extern uint32_t max_pending_packets; |
286 | | max_pending_packets = 128; |
287 | | PacketPoolInit(); |
288 | | |
289 | | uint32_t failed = UtRunTests(regex_arg); |
290 | | PacketPoolDestroy(); |
291 | | UtCleanup(); |
292 | | #ifdef BUILD_HYPERSCAN |
293 | | MpmHSGlobalCleanup(); |
294 | | #endif |
295 | | if (failed) { |
296 | | exit(EXIT_FAILURE); |
297 | | } |
298 | | } |
299 | | |
300 | | exit(EXIT_SUCCESS); |
301 | | #else |
302 | | FatalError("Unittests are not build-in"); |
303 | 0 | #endif /* UNITTESTS */ |
304 | 0 | } |