detect-icode: convert unittests to FAIL/PASS APIs

Bug: #4045
pull/6629/head
Modupe Falodun 4 years ago committed by Victor Julien
parent 97801c795b
commit 2a800d572c

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2020 Open Information Security Foundation /* Copyright (C) 2007-2021 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -379,15 +379,13 @@ static bool PrefilterICodeIsPrefilterable(const Signature *s)
*/ */
static int DetectICodeParseTest01(void) static int DetectICodeParseTest01(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, "8");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, "8"); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->mode == DETECT_ICODE_EQ);
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_EQ) DetectICodeFree(NULL, icd);
result = 1;
DetectICodeFree(NULL, icd); PASS;
}
return result;
} }
/** /**
@ -396,15 +394,13 @@ static int DetectICodeParseTest01(void)
*/ */
static int DetectICodeParseTest02(void) static int DetectICodeParseTest02(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, ">8");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, ">8"); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->mode == DETECT_ICODE_GT);
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_GT) DetectICodeFree(NULL, icd);
result = 1;
DetectICodeFree(NULL, icd); PASS;
}
return result;
} }
/** /**
@ -413,15 +409,13 @@ static int DetectICodeParseTest02(void)
*/ */
static int DetectICodeParseTest03(void) static int DetectICodeParseTest03(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, "<8");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, "<8"); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->mode == DETECT_ICODE_LT);
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_LT) DetectICodeFree(NULL, icd);
result = 1;
DetectICodeFree(NULL, icd); PASS;
}
return result;
} }
/** /**
@ -430,15 +424,14 @@ static int DetectICodeParseTest03(void)
*/ */
static int DetectICodeParseTest04(void) static int DetectICodeParseTest04(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, "8<>20");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, "8<>20"); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->code2 == 20);
if (icd->code1 == 8 && icd->code2 == 20 && icd->mode == DETECT_ICODE_RN) FAIL_IF_NOT(icd->mode == DETECT_ICODE_RN);
result = 1; DetectICodeFree(NULL, icd);
DetectICodeFree(NULL, icd);
} PASS;
return result;
} }
/** /**
@ -447,15 +440,13 @@ static int DetectICodeParseTest04(void)
*/ */
static int DetectICodeParseTest05(void) static int DetectICodeParseTest05(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, " 8 ");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, " 8 "); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->mode == DETECT_ICODE_EQ);
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_EQ) DetectICodeFree(NULL, icd);
result = 1;
DetectICodeFree(NULL, icd); PASS;
}
return result;
} }
/** /**
@ -464,15 +455,13 @@ static int DetectICodeParseTest05(void)
*/ */
static int DetectICodeParseTest06(void) static int DetectICodeParseTest06(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, " > 8 ");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, " > 8 "); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->mode == DETECT_ICODE_GT);
if (icd->code1 == 8 && icd->mode == DETECT_ICODE_GT) DetectICodeFree(NULL, icd);
result = 1;
DetectICodeFree(NULL, icd); PASS;
}
return result;
} }
/** /**
@ -481,15 +470,14 @@ static int DetectICodeParseTest06(void)
*/ */
static int DetectICodeParseTest07(void) static int DetectICodeParseTest07(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, " 8 <> 20 ");
int result = 0; FAIL_IF_NULL(icd);
icd = DetectICodeParse(NULL, " 8 <> 20 "); FAIL_IF_NOT(icd->code1 == 8);
if (icd != NULL) { FAIL_IF_NOT(icd->code2 == 20);
if (icd->code1 == 8 && icd->code2 == 20 && icd->mode == DETECT_ICODE_RN) FAIL_IF_NOT(icd->mode == DETECT_ICODE_RN);
result = 1; DetectICodeFree(NULL, icd);
DetectICodeFree(NULL, icd);
} PASS;
return result;
} }
/** /**
@ -497,12 +485,11 @@ static int DetectICodeParseTest07(void)
*/ */
static int DetectICodeParseTest08(void) static int DetectICodeParseTest08(void)
{ {
DetectICodeData *icd = NULL; DetectICodeData *icd = DetectICodeParse(NULL, "> 8 <> 20");
icd = DetectICodeParse(NULL, "> 8 <> 20"); FAIL_IF_NOT_NULL(icd);
if (icd == NULL)
return 1;
DetectICodeFree(NULL, icd); DetectICodeFree(NULL, icd);
return 0; PASS;
} }
/** /**
@ -517,7 +504,6 @@ static int DetectICodeMatchTest01(void)
Signature *s = NULL; Signature *s = NULL;
ThreadVars th_v; ThreadVars th_v;
DetectEngineThreadCtx *det_ctx; DetectEngineThreadCtx *det_ctx;
int result = 0;
memset(&th_v, 0, sizeof(th_v)); memset(&th_v, 0, sizeof(th_v));
@ -526,70 +512,42 @@ static int DetectICodeMatchTest01(void)
p->icmpv4h->code = 10; p->icmpv4h->code = 10;
DetectEngineCtx *de_ctx = DetectEngineCtxInit(); DetectEngineCtx *de_ctx = DetectEngineCtxInit();
if (de_ctx == NULL) { FAIL_IF_NULL(de_ctx);
goto end;
}
de_ctx->flags |= DE_QUIET; de_ctx->flags |= DE_QUIET;
s = de_ctx->sig_list = SigInit(de_ctx,"alert icmp any any -> any any (icode:10; sid:1;)"); s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any (icode:10; sid:1;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:<15; sid:2;)"); s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any (icode:<15; sid:2;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:>20; sid:3;)"); s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any (icode:>20; sid:3;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:8<>20; sid:4;)"); s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any (icode:8<>20; sid:4;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
s = s->next = SigInit(de_ctx,"alert icmp any any -> any any (icode:20<>8; sid:5;)"); s = DetectEngineAppendSig(de_ctx, "alert icmp any any -> any any (icode:20<>8; sid:5;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
SigGroupBuild(de_ctx); SigGroupBuild(de_ctx);
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx); DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
SigMatchSignatures(&th_v, de_ctx, det_ctx, p); SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
if (PacketAlertCheck(p, 1) == 0) {
SCLogDebug("sid 1 did not alert, but should have");
goto cleanup;
} else if (PacketAlertCheck(p, 2) == 0) {
SCLogDebug("sid 2 did not alert, but should have");
goto cleanup;
} else if (PacketAlertCheck(p, 3)) {
SCLogDebug("sid 3 alerted, but should not have");
goto cleanup;
} else if (PacketAlertCheck(p, 4) == 0) {
SCLogDebug("sid 4 did not alert, but should have");
goto cleanup;
} else if (PacketAlertCheck(p, 5) == 0) {
SCLogDebug("sid 5 did not alert, but should have");
goto cleanup;
}
result = 1; FAIL_IF(PacketAlertCheck(p, 1) == 0);
FAIL_IF(PacketAlertCheck(p, 2) == 0);
cleanup: FAIL_IF(PacketAlertCheck(p, 3));
SigGroupCleanup(de_ctx); FAIL_IF(PacketAlertCheck(p, 4) == 0);
SigCleanSignatures(de_ctx); FAIL_IF(PacketAlertCheck(p, 5) == 0);
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
DetectEngineCtxFree(de_ctx); DetectEngineCtxFree(de_ctx);
UTHFreePackets(&p, 1); UTHFreePackets(&p, 1);
end:
return result; PASS;
} }
/** /**

Loading…
Cancel
Save