]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
http: add tests for header folding
authorVictor Julien <victor@inliniac.net>
Thu, 19 Apr 2018 14:31:43 +0000 (16:31 +0200)
committerVictor Julien <victor@inliniac.net>
Wed, 9 May 2018 13:11:08 +0000 (15:11 +0200)
To test for https://github.com/OISF/libhtp/issues/159

src/app-layer-htp.c

index 8608bb8002f73e3391f19d04d96e6a1dca845029..abff03c6be7091bac3893fdbd0dd771f85df37a9 100644 (file)
@@ -2956,6 +2956,107 @@ static int HTPParserTest01(void)
     PASS;
 }
 
+/** \test Test folding in 1 read case */
+static int HTPParserTest01b(void)
+{
+    uint8_t httpbuf1[] = "POST / HTTP/1.0\r\nUser-Agent:\r\n Victor/1.0\r\n\r\nPost"
+                         " Data is c0oL!";
+    uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
+
+    TcpSession ssn;
+    memset(&ssn, 0, sizeof(ssn));
+
+    AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
+    FAIL_IF_NULL(alp_tctx);
+
+    Flow *f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80);
+    FAIL_IF_NULL(f);
+    f->protoctx = &ssn;
+    f->proto = IPPROTO_TCP;
+    f->alproto = ALPROTO_HTTP;
+
+    StreamTcpInitConfig(TRUE);
+
+    uint8_t flags =STREAM_TOSERVER|STREAM_START|STREAM_EOF;
+    int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP, flags,
+            httpbuf1, httplen1);
+    FAIL_IF(r != 0);
+
+    HtpState *htp_state = f->alstate;
+    FAIL_IF_NULL(htp_state);
+
+    htp_tx_t *tx = HTPStateGetTx(htp_state, 0);
+    FAIL_IF_NULL(tx);
+
+    htp_header_t *h =  htp_table_get_index(tx->request_headers, 0, NULL);
+    FAIL_IF_NULL(h);
+
+    FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
+    FAIL_IF(tx->request_method_number != HTP_M_POST);
+    FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_0);
+
+    AppLayerParserThreadCtxFree(alp_tctx);
+    StreamTcpFreeConfig(TRUE);
+    UTHFreeFlow(f);
+    PASS;
+}
+
+/** \test Test folding in 1byte per read case */
+static int HTPParserTest01c(void)
+{
+    uint8_t httpbuf1[] = "POST / HTTP/1.0\r\nUser-Agent:\r\n Victor/1.0\r\n\r\nPost"
+                         " Data is c0oL!";
+    uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */
+
+    TcpSession ssn;
+    memset(&ssn, 0, sizeof(ssn));
+
+    AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc();
+    FAIL_IF_NULL(alp_tctx);
+
+    Flow *f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80);
+    FAIL_IF_NULL(f);
+    f->protoctx = &ssn;
+    f->proto = IPPROTO_TCP;
+    f->alproto = ALPROTO_HTTP;
+
+    StreamTcpInitConfig(TRUE);
+
+    uint32_t u;
+    for (u = 0; u < httplen1; u++) {
+        uint8_t flags = 0;
+
+        if (u == 0)
+            flags = STREAM_TOSERVER|STREAM_START;
+        else if (u == (httplen1 - 1))
+            flags = STREAM_TOSERVER|STREAM_EOF;
+        else
+            flags = STREAM_TOSERVER;
+
+        int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP, flags,
+                                &httpbuf1[u], 1);
+        FAIL_IF(r != 0);
+    }
+
+    HtpState *htp_state = f->alstate;
+    FAIL_IF_NULL(htp_state);
+
+    htp_tx_t *tx = HTPStateGetTx(htp_state, 0);
+    FAIL_IF_NULL(tx);
+
+    htp_header_t *h =  htp_table_get_index(tx->request_headers, 0, NULL);
+    FAIL_IF_NULL(h);
+
+    FAIL_IF(strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0"));
+    FAIL_IF(tx->request_method_number != HTP_M_POST);
+    FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_0);
+
+    AppLayerParserThreadCtxFree(alp_tctx);
+    StreamTcpFreeConfig(TRUE);
+    UTHFreeFlow(f);
+    PASS;
+}
+
 /** \test Test case where chunks are sent in smaller chunks and check the
  *        response of the parser from HTP library. */
 static int HTPParserTest01a(void)
@@ -6543,6 +6644,8 @@ void HTPParserRegisterTests(void)
 #ifdef UNITTESTS
     UtRegisterTest("HTPParserTest01", HTPParserTest01);
     UtRegisterTest("HTPParserTest01a", HTPParserTest01a);
+    UtRegisterTest("HTPParserTest01b", HTPParserTest01b);
+    UtRegisterTest("HTPParserTest01c", HTPParserTest01c);
     UtRegisterTest("HTPParserTest02", HTPParserTest02);
     UtRegisterTest("HTPParserTest03", HTPParserTest03);
     UtRegisterTest("HTPParserTest04", HTPParserTest04);