]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
detect-replace: Convert unittests to FAIL/PASS API
authorGabriel Lima Luz <gabriel.luz.lima@hotmail.com>
Mon, 24 Oct 2022 14:04:31 +0000 (11:04 -0300)
committerVictor Julien <vjulien@oisf.net>
Thu, 27 Oct 2022 08:26:30 +0000 (10:26 +0200)
Ticket: 4054

src/detect-replace.c

index 06117058b34d37297482185ea17912c129c8c93a..b17b0f4b23742545c2c07ffaca581c7d73a8b9e1 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2011-2014 Open Information Security Foundation
+/* Copyright (C) 2011-2022 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -421,7 +421,8 @@ static int DetectReplaceMatchTest01(void)
                 " content:\"big\"; replace:\"pig\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"this is a pig test\"; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -433,7 +434,8 @@ static int DetectReplaceMatchTest02(void)
                 " content:\"th\"; offset: 4; replace:\"TH\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"THis\"; offset:4; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -445,7 +447,8 @@ static int DetectReplaceMatchTest03(void)
                 " content:\"th\"; replace:\"TH\"; offset: 4; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"THis\"; offset:4; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -457,7 +460,8 @@ static int DetectReplaceMatchTest04(void)
                 " content:\"th\"; replace:\"TH\"; content:\"patter\"; replace:\"matter\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"THis\"; content:\"matterns\"; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -469,7 +473,8 @@ static int DetectReplaceMatchTest05(void)
                 " content:\"th\"; replace:\"TH\"; content:\"nutella\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"TH\"; sid:2;)";
-    return !DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -482,7 +487,8 @@ static int DetectReplaceMatchTest06(void)
                 " content:\"nutella\"; replace:\"commode\"; content:\"this is\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"commode\"; sid:2;)";
-    return !DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -494,7 +500,8 @@ static int DetectReplaceMatchTest07(void)
                 " content:\"BiG\"; nocase; replace:\"pig\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"this is a pig test\"; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -506,7 +513,8 @@ static int DetectReplaceMatchTest08(void)
                 " content:\"big\"; depth:17; replace:\"pig\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"this is a pig test\"; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -518,7 +526,8 @@ static int DetectReplaceMatchTest09(void)
                 " content:\"big\"; depth:16; replace:\"pig\"; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"this is a pig test\"; sid:2;)";
-    return !DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -530,7 +539,8 @@ static int DetectReplaceMatchTest10(void)
                 " content:\"big\"; depth:17; replace:\"pig\"; offset: 14; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"pig\"; depth:17; offset:14; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -542,7 +552,9 @@ static int DetectReplaceMatchTest11(void)
                 " content:\"big\"; replace:\"pig\"; content:\"to\"; within: 11; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"pig\"; depth:17; offset:14; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -554,7 +566,8 @@ static int DetectReplaceMatchTest12(void)
                 " content:\"big\"; replace:\"pig\"; content:\"to\"; within: 4; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"pig\"; depth:17; offset:14; sid:2;)";
-    return !DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -566,7 +579,8 @@ static int DetectReplaceMatchTest13(void)
                 " content:\"big\"; replace:\"pig\"; content:\"test\"; distance: 1; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"pig\"; depth:17; offset:14; sid:2;)";
-    return DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -578,7 +592,8 @@ static int DetectReplaceMatchTest14(void)
                 " content:\"big\"; replace:\"pig\"; content:\"test\"; distance: 2; sid:1;)";
     const char *sig_rep = "alert tcp any any -> any any (msg:\"replace worked\";"
                 " content:\"pig\"; depth:17; offset:14; sid:2;)";
-    return !DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2);
+    FAIL_IF(DetectReplaceLongPatternMatchTestWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 /**
@@ -590,7 +605,8 @@ static int DetectReplaceMatchTest15(void)
                 " content:\"com\"; replace:\"org\"; sid:1;)";
     const char *sig_rep = "alert udp any any -> any any (msg:\"replace worked\";"
                 " content:\"twimg|03|org\"; sid:2;)";
-    return DetectReplaceLongPatternMatchTestUDPWrp(sig, 1, sig_rep, 2);
+    FAIL_IF_NOT(DetectReplaceLongPatternMatchTestUDPWrp(sig, 1, sig_rep, 2));
+    PASS;
 }
 
 
@@ -602,30 +618,17 @@ static int DetectReplaceParseTest01(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert udp any any -> any any "
-                               "(msg:\"test\"; content:\"doh\"; replace:\"; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx,
+            "alert udp any any -> any any "
+            "(msg:\"test\"; content:\"doh\"; replace:\"; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -636,30 +639,17 @@ static int DetectReplaceParseTest02(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert http any any -> any any "
-                               "(msg:\"test\"; content:\"doh\"; replace:\"bon\"; sid:238012;)");
-    if (de_ctx->sig_list == NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NULL(DetectEngineAppendSig(de_ctx,
+            "alert http any any -> any any "
+            "(msg:\"test\"; content:\"doh\"; replace:\"bon\"; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -671,30 +661,18 @@ static int DetectReplaceParseTest03(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
 
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert tcp any any -> any any "
-                               "(msg:\"test\"; content:\"doh\"; replace:\"don\"; http_header; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx,
+            "alert tcp any any -> any any "
+            "(msg:\"test\"; content:\"doh\"; replace:\"don\"; http_header; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -705,30 +683,16 @@ static int DetectReplaceParseTest04(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert tcp any any -> any any "
-                               "(msg:\"test\"; replace:\"don\"; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
+                                                   "(msg:\"test\"; replace:\"don\"; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -739,30 +703,17 @@ static int DetectReplaceParseTest05(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert tcp any any -> any any "
-                               "(msg:\"test\"; replace:\"don\"; content:\"doh\"; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx,
+            "alert tcp any any -> any any "
+            "(msg:\"test\"; replace:\"don\"; content:\"doh\"; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -773,30 +724,17 @@ static int DetectReplaceParseTest06(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert tcp any any -> any any "
-                               "(msg:\"test\"; content:\"don\"; replace:\"donut\"; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(DetectEngineAppendSig(de_ctx,
+            "alert tcp any any -> any any "
+            "(msg:\"test\"; content:\"don\"; replace:\"donut\"; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**
@@ -807,30 +745,18 @@ static int DetectReplaceParseTest07(void)
     int run_mode_backup = run_mode;
     run_mode = RUNMODE_NFQ;
 
-    DetectEngineCtx *de_ctx = NULL;
-    int result = 1;
-
-    de_ctx = DetectEngineCtxInit();
-    if (de_ctx == NULL)
-        goto end;
+    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
+    FAIL_IF_NULL(de_ctx);
 
     de_ctx->flags |= DE_QUIET;
-    de_ctx->sig_list = SigInit(de_ctx,
-                               "alert tcp any any -> any any "
-                               "(msg:\"test\"; content:\"don\"; replace:\"dou\"; content:\"jpg\"; http_header; sid:238012;)");
-    if (de_ctx->sig_list != NULL) {
-        result = 0;
-        goto end;
-    }
+    FAIL_IF_NOT_NULL(
+            DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
+                                          "(msg:\"test\"; content:\"don\"; replace:\"dou\"; "
+                                          "content:\"jpg\"; http_header; sid:238012;)"));
 
- end:
     run_mode = run_mode_backup;
-
-    SigGroupCleanup(de_ctx);
-    SigCleanSignatures(de_ctx);
     DetectEngineCtxFree(de_ctx);
-
-    return result;
+    PASS;
 }
 
 /**