]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
unittests: add exception to detect engine setup
authorVictor Julien <victor@inliniac.net>
Tue, 20 Jan 2015 10:25:02 +0000 (11:25 +0100)
committerVictor Julien <victor@inliniac.net>
Thu, 19 Mar 2015 16:52:29 +0000 (17:52 +0100)
Add code to allow for unittests not following the complete api.

Update replace tests as they don't use the unittests runmode that
powers the workaround based on RunmodeIsUnittests().

src/detect-engine.c
src/detect-replace.c

index b81fe74ef785453004388c9e76499933909c7876..7e0f57138aff00f9c81bb42ef62a9572d4efba65 100644 (file)
@@ -1559,8 +1559,17 @@ TmEcode DetectEngineThreadCtxInit(ThreadVars *tv, void *initdata, void **data)
     det_ctx->tv = tv;
     det_ctx->de_ctx = DetectEngineGetCurrent();
     if (det_ctx->de_ctx == NULL) {
+#ifdef UNITTESTS
+        if (RunmodeIsUnittests()) {
+            det_ctx->de_ctx = (DetectEngineCtx *)initdata;
+        } else {
+            DetectEngineThreadCtxDeinit(tv, det_ctx);
+            return TM_ECODE_FAILED;
+        }
+#else
         DetectEngineThreadCtxDeinit(tv, det_ctx);
         return TM_ECODE_FAILED;
+#endif
     }
 
     if (ThreadCtxDoInit(det_ctx->de_ctx, det_ctx) != TM_ECODE_OK) {
@@ -1705,7 +1714,12 @@ TmEcode DetectEngineThreadCtxDeinit(ThreadVars *tv, void *data)
     }
 
     DetectEngineThreadCtxDeinitKeywords(det_ctx->de_ctx, det_ctx);
+#ifdef UNITTESTS
+    if (!RunmodeIsUnittests() || det_ctx->de_ctx->ref_cnt > 0)
+        DetectEngineDeReference(&det_ctx->de_ctx);
+#else
     DetectEngineDeReference(&det_ctx->de_ctx);
+#endif
     SCFree(det_ctx);
 
     return TM_ECODE_OK;
index 6031c31184f65c01d18106069bff0c8ab6470806..3bf725a992e1769a353cc72dd12faa50731569f7 100644 (file)
@@ -261,9 +261,11 @@ int DetectReplaceLongPatternMatchTest(uint8_t *raw_eth_pkt, uint16_t pktsize, ch
     }
 
     SigGroupBuild(de_ctx);
-    DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
+    DetectEngineAddToMaster(de_ctx);
+    DetectEngineThreadCtxInit(&th_v, NULL, (void *)&det_ctx);
 
     SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
+    DetectEngineMoveToFreeList(de_ctx);
 
     if (PacketAlertCheck(p, sid) != 1) {
         SCLogDebug("replace: no alert on sig %d", sid);
@@ -281,14 +283,9 @@ int DetectReplaceLongPatternMatchTest(uint8_t *raw_eth_pkt, uint16_t pktsize, ch
 end:
     if (dtv.app_tctx != NULL)
         AppLayerDestroyCtxThread(dtv.app_tctx);
-    if (de_ctx != NULL)
-    {
-        SigGroupCleanup(de_ctx);
-        SigCleanSignatures(de_ctx);
-        if (det_ctx != NULL)
-            DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
-        DetectEngineCtxFree(de_ctx);
-    }
+    if (det_ctx != NULL)
+        DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
+    DetectEnginePruneFreeList();
     PACKET_RECYCLE(p);
     FlowShutdown();
     SCFree(p);