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 "util-unittest.h"
25 #include "runmode-unittests.h"
26 
27 #ifdef UNITTESTS
28 
29 #include "detect-parse.h"
30 #include "detect-engine.h"
31 #include "detect-engine-address.h"
32 #include "detect-engine-proto.h"
33 #include "detect-engine-port.h"
34 #include "detect-engine-mpm.h"
35 #include "detect-engine-sigorder.h"
36 #include "detect-engine-payload.h"
37 #include "detect-engine-dcepayload.h"
38 #include "detect-engine-state.h"
39 #include "detect-engine-tag.h"
40 #include "detect-engine-modbus.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 "pkt-var.h"
48 
49 #include "host.h"
50 #include "host-bit.h"
51 #include "ippair.h"
52 #include "ippair-bit.h"
53 #include "unix-manager.h"
54 
55 #include "stream-tcp.h"
56 
57 #include "app-layer-detect-proto.h"
58 #include "app-layer-parser.h"
59 #include "app-layer.h"
60 #include "app-layer-dcerpc.h"
61 #include "app-layer-dcerpc-udp.h"
62 #include "app-layer-htp.h"
63 #include "app-layer-ftp.h"
64 #include "app-layer-ssl.h"
65 #include "app-layer-ssh.h"
66 #include "app-layer-smtp.h"
67 
68 #include "util-action.h"
69 #include "util-radix-tree.h"
70 #include "util-host-os-info.h"
71 #include "util-cidr.h"
72 #include "util-unittest-helper.h"
73 #include "util-time.h"
74 #include "util-rule-vars.h"
75 #include "util-classification-config.h"
76 #include "util-threshold-config.h"
77 #include "util-reference-config.h"
78 #include "util-profiling.h"
79 #include "util-magic.h"
80 #include "util-memcmp.h"
81 #include "util-misc.h"
82 #include "util-signal.h"
83 
84 #include "reputation.h"
85 #include "util-atomic.h"
86 #include "util-spm.h"
87 #include "util-hash.h"
88 #include "util-hashlist.h"
89 #include "util-bloomfilter.h"
90 #include "util-bloomfilter-counting.h"
91 #include "util-pool.h"
92 #include "util-byte.h"
93 #include "util-proto-name.h"
94 #include "util-macset.h"
95 #include "util-memrchr.h"
96 
97 #include "util-mpm-ac.h"
98 #include "util-mpm-hs.h"
99 
100 #include "conf.h"
101 #include "conf-yaml-loader.h"
102 #include "tmqh-flow.h"
103 #include "defrag.h"
104 #include "detect-engine-siggroup.h"
105 
106 #include "util-streaming-buffer.h"
107 #include "util-lua.h"
108 
109 #ifdef OS_WIN32
110 #include "win32-syscall.h"
111 #endif
112 
113 #ifdef WINDIVERT
114 #include "source-windivert.h"
115 #endif
116 
117 #ifdef HAVE_NSS
118 #include <prinit.h>
119 #include <nss.h>
120 #endif
121 
122 #endif /* UNITTESTS */
123 
124 void TmqhSetup (void);
125 
126 #ifdef UNITTESTS
RegisterUnittests(void)127 static void RegisterUnittests(void)
128 {
129     UTHRegisterTests();
130     StreamTcpRegisterTests();
131     SigRegisterTests();
132     SCReputationRegisterTests();
133     TmModuleRegisterTests();
134     SigTableRegisterTests();
135     HashTableRegisterTests();
136     HashListTableRegisterTests();
137     BloomFilterRegisterTests();
138     BloomFilterCountingRegisterTests();
139     PoolRegisterTests();
140     ByteRegisterTests();
141     MpmRegisterTests();
142     FlowBitRegisterTests();
143     HostBitRegisterTests();
144     IPPairBitRegisterTests();
145     StatsRegisterTests();
146     DecodeEthernetRegisterTests();
147     DecodeCHDLCRegisterTests();
148     DecodePPPRegisterTests();
149     DecodeVLANRegisterTests();
150     DecodeVNTagRegisterTests();
151     DecodeGeneveRegisterTests();
152     DecodeVXLANRegisterTests();
153     DecodeRawRegisterTests();
154     DecodePPPOERegisterTests();
155     DecodeICMPV4RegisterTests();
156     DecodeICMPV6RegisterTests();
157     DecodeIPV4RegisterTests();
158     DecodeIPV6RegisterTests();
159     DecodeTCPRegisterTests();
160     DecodeUDPV4RegisterTests();
161     DecodeGRERegisterTests();
162     DecodeMPLSRegisterTests();
163     AppLayerProtoDetectUnittestsRegister();
164     ConfRegisterTests();
165     ConfYamlRegisterTests();
166     TmqhFlowRegisterTests();
167     FlowRegisterTests();
168     HostRegisterUnittests();
169     IPPairRegisterUnittests();
170     SCSigRegisterSignatureOrderingTests();
171     SCRadixRegisterTests();
172     DefragRegisterTests();
173     SigGroupHeadRegisterTests();
174     SCHInfoRegisterTests();
175     SCRuleVarsRegisterTests();
176     AppLayerParserRegisterUnittests();
177     ThreadMacrosRegisterTests();
178     UtilSpmSearchRegistertests();
179     UtilActionRegisterTests();
180     SCClassConfRegisterTests();
181     SCThresholdConfRegisterTests();
182     SCRConfRegisterTests();
183     PayloadRegisterTests();
184     DcePayloadRegisterTests();
185 #ifdef PROFILING
186     SCProfilingRegisterTests();
187 #endif
188     DeStateRegisterTests();
189     MemcmpRegisterTests();
190     DetectEngineInspectModbusRegisterTests();
191     DetectEngineRegisterTests();
192     SCLogRegisterTests();
193     MagicRegisterTests();
194     UtilMiscRegisterTests();
195     DetectAddressTests();
196     DetectProtoTests();
197     DetectPortTests();
198     SCAtomicRegisterTests();
199     MemrchrRegisterTests();
200     AppLayerUnittestsRegister();
201     MimeDecRegisterTests();
202     StreamingBufferRegisterTests();
203     MacSetRegisterTests();
204 #ifdef OS_WIN32
205     Win32SyscallRegisterTests();
206 #endif
207 #ifdef WINDIVERT
208     SourceWinDivertRegisterTests();
209 #endif
210 }
211 #endif
212 
213 /**
214  * Run or list unittests
215  *
216  * \param list_unittests If set to 1, list unittests. Run them if set to 0.
217  * \param regex_arg A regular expression to select unittests to run
218  *
219  * This function is terminal and will call exit after being called.
220  */
221 
RunUnittests(int list_unittests,const char * regex_arg)222 void RunUnittests(int list_unittests, const char *regex_arg)
223 {
224 #ifdef UNITTESTS
225     /* Initializations for global vars, queues, etc (memsets, mutex init..) */
226     GlobalsInitPreConfig();
227 
228 #ifdef HAVE_LUAJIT
229     if (LuajitSetupStatesPool() != 0) {
230         exit(EXIT_FAILURE);
231     }
232 #endif
233 
234     default_packet_size = DEFAULT_PACKET_SIZE;
235     /* load the pattern matchers */
236     MpmTableSetup();
237     SpmTableSetup();
238 
239     StorageInit();
240     AppLayerSetup();
241 
242     /* hardcoded initialization code */
243     SigTableSetup(); /* load the rule keywords */
244     TmqhSetup();
245 
246     CIDRInit();
247 
248     SCProtoNameInit();
249 
250     TagInitCtx();
251     SCReferenceConfInit();
252     SCClassConfInit();
253 
254     UtInitialize();
255 
256     RegisterAllModules();
257 
258     HostBitInitCtx();
259 
260     StorageFinalize();
261    /* test and initialize the unittesting subsystem */
262     if (regex_arg == NULL){
263         regex_arg = ".*";
264         UtRunSelftest(regex_arg); /* inits and cleans up again */
265     }
266 
267 #ifdef HAVE_NSS
268     /* init NSS for hashing */
269     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
270     NSS_NoDB_Init(NULL);
271 #endif
272 
273 
274     AppLayerHtpEnableRequestBodyCallback();
275     AppLayerHtpNeedFileInspection();
276 
277     RegisterUnittests();
278 
279     if (list_unittests) {
280         UtListTests(regex_arg);
281     } else {
282         /* global packet pool */
283         extern intmax_t max_pending_packets;
284         max_pending_packets = 128;
285         PacketPoolInit();
286 
287         uint32_t failed = UtRunTests(regex_arg);
288         PacketPoolDestroy();
289         UtCleanup();
290 #ifdef BUILD_HYPERSCAN
291         MpmHSGlobalCleanup();
292 #endif
293         if (failed) {
294             exit(EXIT_FAILURE);
295         }
296     }
297 
298 #ifdef HAVE_LUAJIT
299     LuajitFreeStatesPool();
300 #endif
301 
302     exit(EXIT_SUCCESS);
303 #else
304     FatalError(SC_ERR_FATAL, "Unittests are not build-in");
305 #endif /* UNITTESTS */
306 }
307 
308