detect/http-client-body: code cleanups and test cleanups

pull/3632/head
Victor Julien 7 years ago
parent 645acb1089
commit 3413757027

@ -331,12 +331,11 @@ static int RunTest (struct TestSteps *steps, const char *sig, const char *yaml)
{
TcpSession ssn;
Flow f;
Packet *p = NULL;
ThreadVars th_v;
DetectEngineCtx *de_ctx = NULL;
DetectEngineThreadCtx *det_ctx = NULL;
int result = 0;
AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
FAIL_IF_NULL(alp_tctx);
memset(&th_v, 0, sizeof(th_v));
memset(&f, 0, sizeof(f));
memset(&ssn, 0, sizeof(ssn));
@ -352,9 +351,9 @@ static int RunTest (struct TestSteps *steps, const char *sig, const char *yaml)
StreamTcpInitConfig(TRUE);
de_ctx = DetectEngineCtxInit();
if (de_ctx == NULL)
goto end;
DetectEngineCtx *de_ctx = DetectEngineCtxInit();
FAIL_IF_NULL(de_ctx);
de_ctx->flags |= DE_QUIET;
FLOW_INITIALIZE(&f);
f.protoctx = (void *)&ssn;
@ -363,75 +362,52 @@ static int RunTest (struct TestSteps *steps, const char *sig, const char *yaml)
f.alproto = ALPROTO_HTTP;
SCLogDebug("sig %s", sig);
DetectEngineAppendSig(de_ctx, (char *)sig);
de_ctx->flags |= DE_QUIET;
if (de_ctx->sig_list == NULL)
goto end;
Signature *s = DetectEngineAppendSig(de_ctx, (char *)sig);
FAIL_IF_NULL(s);
SigGroupBuild(de_ctx);
DetectEngineThreadCtxInit(&th_v, (void *)de_ctx, (void *)&det_ctx);
FAIL_IF_NULL(det_ctx);
struct TestSteps *b = steps;
int i = 0;
while (b->input != NULL) {
SCLogDebug("chunk %p %d", b, i);
p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
if (p == NULL)
goto end;
Packet *p = UTHBuildPacket(NULL, 0, IPPROTO_TCP);
FAIL_IF_NULL(p);
p->flow = &f;
p->flowflags = (b->direction == STREAM_TOSERVER) ? FLOW_PKT_TOSERVER : FLOW_PKT_TOCLIENT;
p->flowflags |= FLOW_PKT_ESTABLISHED;
p->flags |= PKT_HAS_FLOW|PKT_STREAM_EST;
FLOWLOCK_WRLOCK(&f);
int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_HTTP,
b->direction, (uint8_t *)b->input,
b->input_size ? b->input_size : strlen((const char *)b->input));
if (r != 0) {
printf("toserver chunk %d returned %" PRId32 ", expected 0: ", i+1, r);
result = 0;
FLOWLOCK_UNLOCK(&f);
goto end;
}
FLOWLOCK_UNLOCK(&f);
FAIL_IF_NOT(r == 0);
/* do detect */
SigMatchSignatures(&th_v, de_ctx, det_ctx, p);
int match = PacketAlertCheck(p, 1);
if (b->expect != match) {
printf("rule matching mismatch: ");
goto end;
}
FAIL_IF_NOT (b->expect == match);
UTHFreePackets(&p, 1);
p = NULL;
b++;
i++;
}
result = 1;
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
AppLayerParserThreadCtxFree(alp_tctx);
DetectEngineCtxFree(de_ctx);
StreamTcpFreeConfig(TRUE);
FLOW_DESTROY(&f);
UTHFreePackets(&p, 1);
if (yaml) {
HtpConfigRestoreBackup();
ConfRestoreContextBackup();
}
return result;
PASS;
}
static int DetectEngineHttpClientBodyTest01(void)

@ -60,7 +60,6 @@
static int DetectHttpClientBodySetup(DetectEngineCtx *, Signature *, const char *);
static void DetectHttpClientBodyRegisterTests(void);
static void DetectHttpClientBodyFree(void *);
static void DetectHttpClientBodySetupCallback(const DetectEngineCtx *de_ctx,
Signature *s);
static int g_http_client_body_buffer_id = 0;
@ -73,11 +72,8 @@ void DetectHttpClientBodyRegister(void)
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].name = "http_client_body";
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].desc = "content modifier to match only on HTTP request-body";
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].url = DOC_URL DOC_VERSION "/rules/http-keywords.html#http-client-body";
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Match = NULL;
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Setup = DetectHttpClientBodySetup;
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].Free = DetectHttpClientBodyFree;
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].RegisterTests = DetectHttpClientBodyRegisterTests;
sigmatch_table[DETECT_AL_HTTP_CLIENT_BODY].flags |= SIGMATCH_NOOPT ;
DetectAppLayerMpmRegister("http_client_body", SIG_FLAG_TOSERVER, 2,
@ -127,27 +123,6 @@ int DetectHttpClientBodySetup(DetectEngineCtx *de_ctx, Signature *s, const char
ALPROTO_HTTP);
}
/**
* \brief The function to free the http_client_body data.
*
* \param ptr Pointer to the http_client_body.
*/
void DetectHttpClientBodyFree(void *ptr)
{
SCEnter();
DetectContentData *hcbd = (DetectContentData *)ptr;
if (hcbd == NULL)
SCReturn;
if (hcbd->content != NULL)
SCFree(hcbd->content);
SpmDestroyCtx(hcbd->spm_ctx);
SCFree(hcbd);
SCReturn;
}
/************************************Unittests*********************************/
#ifdef UNITTESTS
@ -186,8 +161,6 @@ static int DetectHttpClientBodyTest01(void)
}
end:
SigGroupCleanup(de_ctx);
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
@ -214,8 +187,6 @@ static int DetectHttpClientBodyTest02(void)
result = 1;
end:
SigGroupCleanup(de_ctx);
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
@ -242,8 +213,6 @@ static int DetectHttpClientBodyTest03(void)
result = 1;
end:
SigGroupCleanup(de_ctx);
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
@ -270,8 +239,6 @@ static int DetectHttpClientBodyTest04(void)
result = 1;
end:
SigGroupCleanup(de_ctx);
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
@ -298,8 +265,6 @@ static int DetectHttpClientBodyTest05(void)
result = 1;
end:
SigGroupCleanup(de_ctx);
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
@ -395,10 +360,6 @@ static int DetectHttpClientBodyTest06(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -523,10 +484,6 @@ static int DetectHttpClientBodyTest07(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -655,10 +612,6 @@ static int DetectHttpClientBodyTest08(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -787,10 +740,6 @@ static int DetectHttpClientBodyTest09(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -919,10 +868,6 @@ static int DetectHttpClientBodyTest10(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -1023,10 +968,6 @@ static int DetectHttpClientBodyTest11(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -1126,10 +1067,6 @@ static int DetectHttpClientBodyTest12(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -1229,10 +1166,6 @@ static int DetectHttpClientBodyTest13(void)
end:
if (alp_tctx != NULL)
AppLayerParserThreadCtxFree(alp_tctx);
if (de_ctx != NULL)
SigGroupCleanup(de_ctx);
if (de_ctx != NULL)
SigCleanSignatures(de_ctx);
if (de_ctx != NULL)
DetectEngineCtxFree(de_ctx);
@ -1458,7 +1391,6 @@ end:
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
}
if (de_ctx != NULL) {
SigGroupCleanup(de_ctx);
DetectEngineCtxFree(de_ctx);
}
@ -1717,7 +1649,6 @@ end:
DetectEngineThreadCtxDeinit(&th_v, (void *)det_ctx);
}
if (de_ctx != NULL) {
SigGroupCleanup(de_ctx);
DetectEngineCtxFree(de_ctx);
}
@ -1727,20 +1658,6 @@ end:
return result;
}
static int DetectHttpClientBodyTest22(void)
{
DetectEngineCtx *de_ctx = NULL;
@ -1791,7 +1708,6 @@ static int DetectHttpClientBodyTest22(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -1845,7 +1761,6 @@ static int DetectHttpClientBodyTest23(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -1899,7 +1814,6 @@ static int DetectHttpClientBodyTest24(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -1955,7 +1869,6 @@ static int DetectHttpClientBodyTest25(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2012,7 +1925,6 @@ static int DetectHttpClientBodyTest26(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2038,7 +1950,6 @@ static int DetectHttpClientBodyTest27(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2094,7 +2005,6 @@ static int DetectHttpClientBodyTest28(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2138,7 +2048,6 @@ static int DetectHttpClientBodyTest29(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2182,7 +2091,6 @@ static int DetectHttpClientBodyTest30(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2206,7 +2114,6 @@ static int DetectHttpClientBodyTest31(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2230,7 +2137,6 @@ static int DetectHttpClientBodyTest32(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2254,7 +2160,6 @@ static int DetectHttpClientBodyTest33(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2305,7 +2210,6 @@ static int DetectHttpClientBodyTest34(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2356,7 +2260,6 @@ static int DetectHttpClientBodyTest35(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}
@ -2407,7 +2310,6 @@ static int DetectHttpClientBodyTest36(void)
result = 1;
end:
SigCleanSignatures(de_ctx);
DetectEngineCtxFree(de_ctx);
return result;
}

Loading…
Cancel
Save