detect: convert unittest into fail/pass api

pull/14990/head
Victor Julien 5 months ago
parent 25a8559cd4
commit 565c1831a0

@ -4263,7 +4263,6 @@ end:
* any other packet of the stream */ * any other packet of the stream */
static int SigTestDropFlow03(void) static int SigTestDropFlow03(void)
{ {
int result = 0;
Flow f; Flow f;
HtpState *http_state = NULL; HtpState *http_state = NULL;
uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n" uint8_t http_buf1[] = "POST /one HTTP/1.0\r\n"
@ -4282,7 +4281,6 @@ static int SigTestDropFlow03(void)
TcpSession ssn; TcpSession ssn;
Packet *p1 = NULL; Packet *p1 = NULL;
Packet *p2 = NULL; Packet *p2 = NULL;
Signature *s = NULL;
ThreadVars tv; ThreadVars tv;
DetectEngineThreadCtx *det_ctx = NULL; DetectEngineThreadCtx *det_ctx = NULL;
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
@ -4314,107 +4312,62 @@ static int SigTestDropFlow03(void)
StreamTcpInitConfig(true); StreamTcpInitConfig(true);
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, "drop tcp any any -> any 80 " Signature *s = DetectEngineAppendSig(de_ctx, "drop tcp any any -> any 80 "
"(msg:\"Test proto match\"; uricontent:\"one\";" "(msg:\"Test proto match\"; uricontent:\"one\";"
"sid:1;)"); "sid:1;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
/* the no inspection flag should be set after the first sig gets triggered, /* the no inspection flag should be set after the first sig gets triggered,
* so the second packet should not match the next sig (because of no inspection) */ * so the second packet should not match the next sig (because of no inspection) */
s = de_ctx->sig_list->next = SigInit(de_ctx, "alert tcp any any -> any 80 " s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any 80 "
"(msg:\"Test proto match\"; uricontent:\"two\";" "(msg:\"Test proto match\"; uricontent:\"two\";"
"sid:2;)"); "sid:2;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end;
}
SigGroupBuild(de_ctx); SigGroupBuild(de_ctx);
DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx); DetectEngineThreadCtxInit(&tv, (void *)de_ctx, (void *)&det_ctx);
int r = AppLayerParserParse( int r = AppLayerParserParse(
NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, http_buf1, http_buf1_len); NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, http_buf1, http_buf1_len);
if (r != 0) { FAIL_IF(r != 0);
printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
goto end;
}
http_state = f.alstate; http_state = f.alstate;
if (http_state == NULL) { FAIL_IF_NULL(http_state);
printf("no http state: ");
goto end;
}
/* do detect */ /* do detect */
SigMatchSignatures(&tv, de_ctx, det_ctx, p1); SigMatchSignatures(&tv, de_ctx, det_ctx, p1);
FAIL_IF(!PacketAlertCheck(p1, 1));
if (!PacketAlertCheck(p1, 1)) { FAIL_IF(!(p1->flow->flags & FLOW_ACTION_DROP));
printf("sig 1 didn't alert on p1, but it should: ");
goto end;
}
if ( !(p1->flow->flags & FLOW_ACTION_DROP)) {
printf("sig 1 alerted but flow was not flagged correctly: ");
goto end;
}
/* Second part.. Let's feed with another packet */ /* Second part.. Let's feed with another packet */
if (StreamTcpCheckFlowDrops(p2) == 1) { FAIL_IF_NOT(StreamTcpCheckFlowDrops(p2) == 1);
SCLogDebug("This flow/stream triggered a drop rule");
DecodeSetNoPacketInspectionFlag(p2);
StreamTcpDisableAppLayer(p2->flow);
p2->action |= ACTION_DROP;
/* return the segments to the pool */
StreamTcpSessionPktFree(p2);
}
SCLogDebug("This flow/stream triggered a drop rule");
DecodeSetNoPacketInspectionFlag(p2);
StreamTcpDisableAppLayer(p2->flow);
p2->action |= ACTION_DROP;
/* return the segments to the pool */
StreamTcpSessionPktFree(p2);
if ( !(p2->flags & PKT_NOPACKET_INSPECTION)) { FAIL_IF(!(p2->flags & PKT_NOPACKET_INSPECTION));
printf("The packet was not flagged with no-inspection: ");
goto end;
}
r = AppLayerParserParse( r = AppLayerParserParse(
NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, http_buf2, http_buf2_len); NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, http_buf2, http_buf2_len);
if (r != 0) { FAIL_IF(r != 0);
printf("toserver chunk 2 returned %" PRId32 ", expected 0: ", r);
goto end;
}
/* do detect */ /* do detect */
SigMatchSignatures(&tv, de_ctx, det_ctx, p2); SigMatchSignatures(&tv, de_ctx, det_ctx, p2);
if (PacketAlertCheck(p2, 1)) { FAIL_IF(PacketAlertCheck(p2, 1));
printf("sig 1 alerted, but it should not since the no pkt inspection should be set: "); FAIL_IF(PacketAlertCheck(p2, 2));
goto end; FAIL_IF(!(PacketTestAction(p2, ACTION_DROP)));
}
if (PacketAlertCheck(p2, 2)) {
printf("sig 2 alerted, but it should not since the no pkt inspection should be set: ");
goto end;
}
if (!(PacketTestAction(p2, ACTION_DROP))) { AppLayerParserThreadCtxFree(alp_tctx);
printf("A \"drop\" action should be set from the flow to the packet: "); DetectEngineThreadCtxDeinit(&tv, det_ctx);
goto end; DetectEngineCtxFree(de_ctx);
}
result = 1;
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (det_ctx != NULL)
DetectEngineThreadCtxDeinit(&tv, det_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
StreamTcpFreeConfig(true); StreamTcpFreeConfig(true);
FLOW_DESTROY(&f); FLOW_DESTROY(&f);
@ -4425,7 +4378,7 @@ end:
/* Restore mode to IDS */ /* Restore mode to IDS */
EngineModeSetIDS(); EngineModeSetIDS();
StatsThreadCleanup(&tv.stats); StatsThreadCleanup(&tv.stats);
return result; PASS;
} }
/** \test ICMP packet shouldn't be matching port based sig /** \test ICMP packet shouldn't be matching port based sig

Loading…
Cancel
Save