unittests: clean up memory for http urilen tests

pull/14025/head
Victor Julien 10 months ago committed by Victor Julien
parent 346cfe25a1
commit f64989cfd0

@ -502,24 +502,21 @@ end:
/** \test Check a signature with given urilen */ /** \test Check a signature with given urilen */
static int DetectUrilenSigTest01(void) static int DetectUrilenSigTest01(void)
{ {
int result = 0;
Flow f; Flow f;
uint8_t httpbuf1[] = "POST /suricata HTTP/1.0\r\n" uint8_t httpbuf1[] = "POST /suricata HTTP/1.0\r\n"
"Host: foo.bar.tld\r\n" "Host: foo.bar.tld\r\n"
"\r\n"; "\r\n";
uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
TcpSession ssn; TcpSession ssn;
Packet *p = NULL;
Signature *s = NULL;
ThreadVars th_v; ThreadVars th_v;
DetectEngineThreadCtx *det_ctx; DetectEngineThreadCtx *det_ctx = NULL;
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
memset(&th_v, 0, sizeof(th_v)); memset(&th_v, 0, sizeof(th_v));
memset(&f, 0, sizeof(f)); memset(&f, 0, sizeof(f));
memset(&ssn, 0, sizeof(ssn)); memset(&ssn, 0, sizeof(ssn));
p = UTHBuildPacket(NULL, 0, IPPROTO_TCP); Packet *p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
FLOW_INITIALIZE(&f); FLOW_INITIALIZE(&f);
f.protoctx = (void *)&ssn; f.protoctx = (void *)&ssn;
@ -535,68 +532,42 @@ static int DetectUrilenSigTest01(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, Signature *s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
"alert tcp any any -> any any "
"(msg:\"Testing urilen\"; " "(msg:\"Testing urilen\"; "
"urilen: <5; sid:1;)"); "urilen: <5; sid:1;)");
if (s == NULL) { FAIL_IF_NULL(s);
goto end; s = DetectEngineAppendSig(de_ctx, "alert tcp any any -> any any "
}
s = s->next = SigInit(de_ctx,
"alert tcp any any -> any any "
"(msg:\"Testing http_method\"; " "(msg:\"Testing http_method\"; "
"urilen: >5; sid:2;)"); "urilen: >5; sid:2;)");
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);
int r = AppLayerParserParse( int r = AppLayerParserParse(
NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1); NULL, alp_tctx, &f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1);
if (r != 0) { FAIL_IF(r != 0);
SCLogDebug("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
goto end;
}
HtpState *htp_state = f.alstate; HtpState *htp_state = f.alstate;
if (htp_state == NULL) { FAIL_IF_NULL(htp_state);
SCLogDebug("no http state: ");
goto end;
}
SigMatchSignatures(&th_v, de_ctx, det_ctx, p); SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
if ((PacketAlertCheck(p, 1))) { FAIL_IF(PacketAlertCheck(p, 1));
printf("sid 1 alerted, but should not have: \n"); FAIL_IF(!PacketAlertCheck(p, 2));
goto end;
}
if (!PacketAlertCheck(p, 2)) {
printf("sid 2 did not alerted, but should have: \n");
goto end;
}
result = 1; UTHFreePackets(&p, 1);
FLOW_DESTROY(&f);
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx); AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL) SigGroupCleanup(de_ctx); DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
if (de_ctx != NULL) SigCleanSignatures(de_ctx); DetectEngineCtxFree(de_ctx);
if (de_ctx != NULL) DetectEngineCtxFree(de_ctx);
StreamTcpFreeConfig(true); StreamTcpFreeConfig(true);
FLOW_DESTROY(&f); StatsThreadCleanup(&th_v);
UTHFreePackets(&p, 1); PASS;
return result;
} }
/** /**

Loading…
Cancel
Save