diff --git a/src/app-layer-htp.c b/src/app-layer-htp.c index f3101fde709..4cbb47e411b 100644 --- a/src/app-layer-htp.c +++ b/src/app-layer-htp.c @@ -3057,21 +3057,18 @@ static int HTPParserTest01c(void) * response of the parser from HTP library. */ static int HTPParserTest01a(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = " POST / HTTP/1.0\r\nUser-Agent: Victor/1.0\r\n\r\nPost" " Data is c0oL!"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3089,46 +3086,32 @@ static int HTPParserTest01a(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + 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); - if (strcmp(bstr_util_strdup_to_c(h->value), "Victor/1.0") - || tx->request_method_number != HTP_M_POST || - tx->request_protocol_number != HTP_PROTOCOL_1_0) - { - printf("expected header value: Victor/1.0 and got %s: and expected" - " method: POST and got %s, expected protocol number HTTP/1.0" - " and got: %s \n", bstr_util_strdup_to_c(h->value), - bstr_util_strdup_to_c(tx->request_method), - bstr_util_strdup_to_c(tx->request_protocol)); - goto end; - } - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + 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); - return result; + PASS; } /** \test See how it deals with an incomplete request. */ static int HTPParserTest02(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "POST"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -3139,8 +3122,7 @@ static int HTPParserTest02(void) memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3149,16 +3131,10 @@ static int HTPParserTest02(void) int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1); - if (r != 0) { - printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); - goto end; - } + FAIL_IF(r != 0); http_state = f->alstate; - if (http_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(http_state); htp_tx_t *tx = HTPStateGetTx(http_state, 0); FAIL_IF_NULL(tx); @@ -3172,33 +3148,27 @@ static int HTPParserTest02(void) FAIL_IF(strcmp(method, "POST") != 0); SCFree(method); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test case where method is invalid and data is sent in smaller chunks * and check the response of the parser from HTP library. */ static int HTPParserTest03(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "HELLO / HTTP/1.0\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3213,92 +3183,66 @@ static int HTPParserTest03(void) else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + 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); - if (tx->request_method_number != HTP_M_UNKNOWN || - h != NULL || tx->request_protocol_number != HTP_PROTOCOL_1_0) - { - printf("expected method M_UNKNOWN and got %s: , expected protocol " - "HTTP/1.0 and got %s \n", bstr_util_strdup_to_c(tx->request_method), - bstr_util_strdup_to_c(tx->request_protocol)); - goto end; - } - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF_NOT_NULL(h); + FAIL_IF(tx->request_method_number != HTP_M_UNKNOWN); + FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_0); + + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test case where invalid data is sent and check the response of the * parser from HTP library. */ static int HTPParserTest04(void) { - int result = 0; Flow *f = NULL; HtpState *htp_state = NULL; uint8_t httpbuf1[] = "World!\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - int r = 0; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; StreamTcpInitConfig(true); - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, STREAM_TOSERVER | STREAM_START | STREAM_EOF, httpbuf1, httplen1); - if (r != 0) { - goto end; - } + FAIL_IF(r != 0); htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); - if (tx->request_method_number != HTP_M_UNKNOWN || - h != NULL || tx->request_protocol_number != HTP_PROTOCOL_0_9) - { - printf("expected method M_UNKNOWN and got %s: , expected protocol " - "NULL and got %s \n", bstr_util_strdup_to_c(tx->request_method), - bstr_util_strdup_to_c(tx->request_protocol)); - goto end; - } - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF_NULL(tx); + htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); + + FAIL_IF(h != NULL); + FAIL_IF(tx->request_method_number != HTP_M_UNKNOWN); + FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_0_9); + + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test both sides of a http stream mixed up to see if the HTP parser @@ -3454,7 +3398,7 @@ static int HTPParserTest06(void) FAIL_IF(tx->response_status_number != 200); FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_1); - htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); + htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); FAIL_IF_NULL(h); AppLayerParserThreadCtxFree(alp_tctx); @@ -3467,20 +3411,17 @@ static int HTPParserTest06(void) */ static int HTPParserTest07(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /awstats.pl?/migratemigrate%20=%20| HTTP/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3498,54 +3439,31 @@ static int HTPParserTest07(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref[] = "/awstats.pl?/migratemigrate = |"; size_t reflen = sizeof(ref) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; - HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } + FAIL_IF_NULL(tx); + HtpTxUserData *tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref, + bstr_len(tx_ud->request_uri_normalized)) != 0); + + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } #include "conf-yaml-loader.h" @@ -3554,7 +3472,6 @@ static int HTPParserTest07(void) */ static int HTPParserTest08(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /secondhouse/image/js/\%ce\%de\%ce\%fd_RentCity.js?v=2011.05.02 HTTP/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -3577,64 +3494,47 @@ libhtp:\n\ ConfYamlLoadString(input, strlen(input)); HTPConfigure(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; StreamTcpInitConfig(true); - uint8_t flags = 0; - flags = STREAM_TOSERVER|STREAM_START|STREAM_EOF; + uint8_t flags = STREAM_TOSERVER | STREAM_START | STREAM_EOF; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, httpbuf1, httplen1); - if (r != 0) { - printf("toserver chunk returned %" PRId32 ", expected" - " 0: ", r); - result = 0; - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, httpbuf1, httplen1); + FAIL_IF(r != 0); htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - result = 0; - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - PrintRawDataFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), - bstr_len(tx_ud->request_uri_normalized)); - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + PrintRawDataFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), + bstr_len(tx_ud->request_uri_normalized)); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); HtpConfigRestoreBackup(); UTHFreeFlow(f); - return result; + PASS; } /** \test Abort */ static int HTPParserTest09(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /secondhouse/image/js/\%ce\%de\%ce\%fd_RentCity.js?v=2011.05.02 HTTP/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -3657,76 +3557,61 @@ libhtp:\n\ ConfYamlLoadString(input, strlen(input)); HTPConfigure(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; StreamTcpInitConfig(true); - uint8_t flags = 0; - flags = STREAM_TOSERVER|STREAM_START|STREAM_EOF; + uint8_t flags = STREAM_TOSERVER | STREAM_START | STREAM_EOF; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, httpbuf1, httplen1); - if (r != 0) { - printf("toserver chunk returned %" PRId32 ", expected" - " 0: ", r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, httpbuf1, httplen1); + FAIL_IF(r != 0); htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); + HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - PrintRawDataFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), - bstr_len(tx_ud->request_uri_normalized)); - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + PrintRawDataFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), + bstr_len(tx_ud->request_uri_normalized)); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); HtpConfigRestoreBackup(); UTHFreeFlow(f); - return result; + PASS; } /** \test Host:www.google.com <- missing space between name:value (rfc violation) */ static int HTPParserTest10(void) { - int result = 0; + Flow *f = NULL; uint8_t httpbuf1[] = "GET / HTTP/1.0\r\nHost:www.google.com\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3744,77 +3629,48 @@ static int HTPParserTest10(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); - if (h == NULL) { - goto end; - } + FAIL_IF_NULL(h); char *name = bstr_util_strdup_to_c(h->name); - if (name == NULL) { - goto end; - } - - if (strcmp(name, "Host") != 0) { - printf("header name not \"Host\", instead \"%s\": ", name); - free(name); - goto end; - } - free(name); + FAIL_IF_NULL(name); + FAIL_IF(strcmp(name, "Host") != 0); char *value = bstr_util_strdup_to_c(h->value); - if (value == NULL) { - goto end; - } - - if (strcmp(value, "www.google.com") != 0) { - printf("header value not \"www.google.com\", instead \"%s\": ", value); - free(value); - goto end; - } - free(value); + FAIL_IF_NULL(value); + FAIL_IF(strcmp(value, "www.google.com") != 0); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + SCFree(name); + SCFree(value); + PASS; } /** \test double encoding in path */ static int HTPParserTest11(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /%2500 HTTP/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3832,70 +3688,48 @@ static int HTPParserTest11(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); + HtpTxUserData *tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); - if (tx != NULL && tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (4 != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be 2, is %"PRIuMAX, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); - if (bstr_ptr(tx_ud->request_uri_normalized)[0] != '/' || - bstr_ptr(tx_ud->request_uri_normalized)[1] != '%' || - bstr_ptr(tx_ud->request_uri_normalized)[2] != '0' || - bstr_ptr(tx_ud->request_uri_normalized)[3] != '0') - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\": "); - goto end; - } - } + FAIL_IF(bstr_len(tx_ud->request_uri_normalized) != 4); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[0] != '/'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[1] != '%'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[2] != '0'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[3] != '0'); + + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test double encoding in query */ static int HTPParserTest12(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /?a=%2500 HTTP/1.0\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3913,73 +3747,50 @@ static int HTPParserTest12(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (7 != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be 5, is %"PRIuMAX, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); - if (bstr_ptr(tx_ud->request_uri_normalized)[0] != '/' || - bstr_ptr(tx_ud->request_uri_normalized)[1] != '?' || - bstr_ptr(tx_ud->request_uri_normalized)[2] != 'a' || - bstr_ptr(tx_ud->request_uri_normalized)[3] != '=' || - bstr_ptr(tx_ud->request_uri_normalized)[4] != '%' || - bstr_ptr(tx_ud->request_uri_normalized)[5] != '0' || - bstr_ptr(tx_ud->request_uri_normalized)[6] != '0') - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\": "); - goto end; - } - } + FAIL_IF(bstr_len(tx_ud->request_uri_normalized) != 7); - result = 1; - end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[0] != '/'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[1] != '?'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[2] != 'a'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[3] != '='); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[4] != '%'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[5] != '0'); + FAIL_IF(bstr_ptr(tx_ud->request_uri_normalized)[6] != '0'); + + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Host:www.google.com0dName: Value0d0a <- missing space between name:value (rfc violation) */ static int HTPParserTest13(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET / HTTP/1.0\r\nHost:www.google.com\rName: Value\r\n\r\n"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -3997,65 +3808,36 @@ static int HTPParserTest13(void) else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } - + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); htp_header_t *h = htp_table_get_index(tx->request_headers, 0, NULL); - if (h == NULL) { - goto end; - } + FAIL_IF_NULL(h); char *name = bstr_util_strdup_to_c(h->name); - if (name == NULL) { - goto end; - } - - if (strcmp(name, "Host") != 0) { - printf("header name not \"Host\", instead \"%s\": ", name); - free(name); - goto end; - } - free(name); + FAIL_IF_NULL(name); + FAIL_IF(strcmp(name, "Host") != 0); char *value = bstr_util_strdup_to_c(h->value); - if (value == NULL) { - goto end; - } + FAIL_IF_NULL(value); + FAIL_IF(strcmp(value, "www.google.com\rName: Value") != 0); - if (strcmp(value, "www.google.com\rName: Value") != 0) { - printf("header value not \"www.google.com\", instead \""); - PrintRawUriFp(stdout, (uint8_t *)value, strlen(value)); - printf("\": "); - free(value); - goto end; - } - free(value); - - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + SCFree(name); + SCFree(value); + + PASS; } /** \test Test basic config */ static int HTPParserConfigTest01(void) { - int ret = 0; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4084,157 +3866,94 @@ libhtp:\n\ ConfNode *outputs; outputs = ConfGetNode("libhtp.default-config.personality"); - if (outputs == NULL) { - goto end; - } + FAIL_IF_NULL(outputs); outputs = ConfGetNode("libhtp.server-config"); - if (outputs == NULL) { - goto end; - } + FAIL_IF_NULL(outputs); ConfNode *node = TAILQ_FIRST(&outputs->head); - if (node == NULL) { - goto end; - } - if (strcmp(node->name, "0") != 0) { - goto end; - } + FAIL_IF_NULL(node); + FAIL_IF(strcmp(node->name, "0") != 0); node = TAILQ_FIRST(&node->head); - if (node == NULL) { - goto end; - } - if (strcmp(node->name, "apache-tomcat") != 0) { - goto end; - } + FAIL_IF_NULL(node); + FAIL_IF(strcmp(node->name, "apache-tomcat") != 0); int i = 0; ConfNode *n; ConfNode *node2 = ConfNodeLookupChild(node, "personality"); - if (node2 == NULL) { - goto end; - } - if (strcmp(node2->val, "Tomcat_6_0") != 0) { - goto end; - } + FAIL_IF_NULL(node2); + FAIL_IF(strcmp(node2->val, "Tomcat_6_0") != 0); node = ConfNodeLookupChild(node, "address"); - if (node == NULL) { - goto end; - } - TAILQ_FOREACH(n, &node->head, next) { - if (n == NULL) { - goto end; - } + FAIL_IF_NULL(node); + TAILQ_FOREACH (n, &node->head, next) { + FAIL_IF_NULL(n); switch(i) { case 0: - if (strcmp(n->name, "0") != 0) { - goto end; - } - if (strcmp(n->val, "192.168.1.0/24") != 0) { - goto end; - } + FAIL_IF(strcmp(n->name, "0") != 0); + FAIL_IF(strcmp(n->val, "192.168.1.0/24") != 0); break; case 1: - if (strcmp(n->name, "1") != 0) { - goto end; - } - if (strcmp(n->val, "127.0.0.0/8") != 0) { - goto end; - } + FAIL_IF(strcmp(n->name, "1") != 0); + FAIL_IF(strcmp(n->val, "127.0.0.0/8") != 0); break; case 2: - if (strcmp(n->name, "2") != 0) { - goto end; - } - if (strcmp(n->val, "::1") != 0) { - goto end; - } + FAIL_IF(strcmp(n->name, "2") != 0); + FAIL_IF(strcmp(n->val, "::1") != 0); break; default: - goto end; + FAIL; } i++; } outputs = ConfGetNode("libhtp.server-config"); - if (outputs == NULL) { - goto end; - } - + FAIL_IF_NULL(outputs); node = TAILQ_FIRST(&outputs->head); node = TAILQ_NEXT(node, next); - if (node == NULL) { - goto end; - } - if (strcmp(node->name, "1") != 0) { - goto end; - } + FAIL_IF_NULL(node); + FAIL_IF(strcmp(node->name, "1") != 0); node = TAILQ_FIRST(&node->head); - if (node == NULL) { - goto end; - } - if (strcmp(node->name, "iis7") != 0) { - goto end; - } + FAIL_IF_NULL(node); + FAIL_IF(strcmp(node->name, "iis7") != 0); node2 = ConfNodeLookupChild(node, "personality"); - if (node2 == NULL) { - goto end; - } - if (strcmp(node2->val, "IIS_7_0") != 0) { - goto end; - } + FAIL_IF_NULL(node2); + FAIL_IF(strcmp(node2->val, "IIS_7_0") != 0); node = ConfNodeLookupChild(node, "address"); - if (node == NULL) { - goto end; - } + FAIL_IF_NULL(node); i = 0; TAILQ_FOREACH(n, &node->head, next) { - if (n == NULL) { - goto end; - } + FAIL_IF_NULL(n); switch(i) { case 0: - if (strcmp(n->name, "0") != 0) { - goto end; - } - if (strcmp(n->val, "192.168.0.0/24") != 0) { - goto end; - } + FAIL_IF(strcmp(n->name, "0") != 0); + FAIL_IF(strcmp(n->val, "192.168.0.0/24") != 0); break; case 1: - if (strcmp(n->name, "1") != 0) { - goto end; - } - if (strcmp(n->val, "192.168.10.0/24") != 0) { - goto end; - } + FAIL_IF(strcmp(n->name, "1") != 0); + FAIL_IF(strcmp(n->val, "192.168.10.0/24") != 0); break; default: - goto end; + FAIL; } i++; } - ret = 1; - -end: ConfDeInit(); ConfRestoreContextBackup(); - return ret; + PASS; } /** \test Test config builds radix correctly */ static int HTPParserConfigTest02(void) { - int ret = 0; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4264,15 +3983,9 @@ libhtp:\n\ HTPConfigure(); - if (cfglist.cfg == NULL) { - printf("No default config created.\n"); - goto end; - } + FAIL_IF_NULL(cfglist.cfg); - if (cfgtree == NULL) { - printf("No config tree created.\n"); - goto end; - } + FAIL_IF_NULL(cfgtree); htp_cfg_t *htp = cfglist.cfg; uint8_t buf[128]; @@ -4280,57 +3993,35 @@ libhtp:\n\ void *user_data = NULL; addr = "192.168.10.42"; - if (inet_pton(AF_INET, addr, buf) == 1) { - (void)SCRadixFindKeyIPV4BestMatch(buf, cfgtree, &user_data); - if (user_data != NULL) { - HTPCfgRec *htp_cfg_rec = user_data; - htp = htp_cfg_rec->cfg; - SCLogDebug("LIBHTP using config: %p", htp); - } - if (htp == NULL) { - printf("Could not get config for: %s\n", addr); - goto end; - } - } - else { - printf("Failed to parse address: %s\n", addr); - goto end; - } + FAIL_IF(inet_pton(AF_INET, addr, buf) != 1); + (void)SCRadixFindKeyIPV4BestMatch(buf, cfgtree, &user_data); + FAIL_IF_NULL(user_data); + HTPCfgRec *htp_cfg_rec = user_data; + FAIL_IF_NULL(htp); + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); user_data = NULL; addr = "::1"; - if (inet_pton(AF_INET6, addr, buf) == 1) { - (void)SCRadixFindKeyIPV6BestMatch(buf, cfgtree, &user_data); - if (user_data != NULL) { - HTPCfgRec *htp_cfg_rec = user_data; - htp = htp_cfg_rec->cfg; - SCLogDebug("LIBHTP using config: %p", htp); - } - if (htp == NULL) { - printf("Could not get config for: %s\n", addr); - goto end; - } - } - else { - printf("Failed to parse address: %s\n", addr); - goto end; - } - - ret = 1; + FAIL_IF(inet_pton(AF_INET6, addr, buf) != 1); + (void)SCRadixFindKeyIPV6BestMatch(buf, cfgtree, &user_data); + FAIL_IF_NULL(user_data); + htp_cfg_rec = user_data; + FAIL_IF_NULL(htp); + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); -end: HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); HtpConfigRestoreBackup(); - return ret; + PASS; } /** \test Test traffic is handled by the correct htp config */ static int HTPParserConfigTest03(void) { - int result = 1; Flow *f = NULL; uint8_t httpbuf1[] = "POST / HTTP/1.0\r\nUser-Agent: Victor/1.0\r\n\r\nPost" " Data is c0oL!"; @@ -4338,8 +4029,7 @@ static int HTPParserConfigTest03(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4374,8 +4064,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -4384,15 +4073,12 @@ libhtp:\n\ void *user_data = NULL; (void)SCRadixFindKeyIPV4BestMatch((uint8_t *)f->dst.addr_data32, cfgtree, &user_data); - if (user_data != NULL) { - HTPCfgRec *htp_cfg_rec = user_data; - htp = htp_cfg_rec->cfg; - SCLogDebug("LIBHTP using config: %p", htp); - } - if (htp == NULL) { - printf("Could not get config for: %s\n", addr); - goto end; - } + FAIL_IF_NULL(user_data); + HTPCfgRec *htp_cfg_rec = user_data; + htp = htp_cfg_rec->cfg; + SCLogDebug("LIBHTP using config: %p", htp); + + FAIL_IF_NULL(htp); StreamTcpInitConfig(true); @@ -4404,47 +4090,24 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - result = 0; - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - result = 0; - goto end; - } + FAIL_IF_NULL(htp_state); - if (HTPStateGetTxCnt(htp_state) != 2) { - printf("HTPStateGetTxCnt(htp_state) failure\n"); - goto end; - } + FAIL_IF(HTPStateGetTxCnt(htp_state) != 2); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; - if (tx->cfg != htp) { - printf("wrong HTP config (%p instead of %p - default=%p): ", - tx->cfg, htp, cfglist.cfg); - goto end; - } + FAIL_IF_NULL(tx); + FAIL_IF(tx->cfg != htp); + tx = HTPStateGetTx(htp_state, 1); - if (tx == NULL) - goto end; - if (tx->cfg != htp) { - printf("wrong HTP config (%p instead of %p - default=%p): ", - tx->cfg, htp, cfglist.cfg); - goto end; - } + FAIL_IF_NULL(tx); + FAIL_IF(tx->cfg != htp); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -4452,7 +4115,7 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test %2f decoding in profile Apache_2_2 @@ -4658,7 +4321,6 @@ libhtp:\n\ */ static int HTPParserDecodingTest02(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /abc%2fdef HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n" @@ -4668,8 +4330,7 @@ static int HTPParserDecodingTest02(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4690,8 +4351,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -4706,105 +4366,51 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/abc/def"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); uint8_t ref2[] = "/abc/def?ghi/jkl"; reflen = sizeof(ref2) - 1; tx = HTPStateGetTx(htp_state, 1); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref2, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref2, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref2, + bstr_len(tx_ud->request_uri_normalized)) != 0); uint8_t ref3[] = "/abc/def?ghi%2fjkl"; reflen = sizeof(ref3) - 1; tx = HTPStateGetTx(htp_state, 2); - if (tx == NULL) - goto end; - tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX" (3): ", - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref3, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref3, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx); + tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref3, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -4812,7 +4418,7 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test %2f decoding in profile IDS with double-decode-* options @@ -4822,7 +4428,6 @@ libhtp:\n\ */ static int HTPParserDecodingTest03(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /abc%252fdef HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n" @@ -4831,8 +4436,7 @@ static int HTPParserDecodingTest03(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4853,8 +4457,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -4869,79 +4472,40 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } - htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + htp_state = f->alstate; + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/abc/def"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); uint8_t ref2[] = "/abc/def?ghi/jkl"; reflen = sizeof(ref2) - 1; tx = HTPStateGetTx(htp_state, 1); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); tx_ud = (HtpTxUserData *)htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref2, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref2, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref2, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -4949,14 +4513,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test http:// in query profile IDS */ static int HTPParserDecodingTest04(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /abc/def?a=http://www.abc.com/ HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n"; @@ -4964,8 +4527,7 @@ static int HTPParserDecodingTest04(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -4986,8 +4548,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5002,52 +4563,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/abc/def?a=http://www.abc.com/"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5055,14 +4591,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test \ char in query profile IDS. Bug 739 */ static int HTPParserDecodingTest05(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /index?id=\\\" HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n"; @@ -5070,8 +4605,7 @@ static int HTPParserDecodingTest05(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5092,8 +4626,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5108,52 +4641,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/index?id=\\\""; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5161,14 +4669,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test + char in query. Bug 1035 */ static int HTPParserDecodingTest06(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /put.php?ip=1.2.3.4&port=+6000 HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n"; @@ -5176,8 +4683,7 @@ static int HTPParserDecodingTest06(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5198,8 +4704,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5214,52 +4719,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/put.php?ip=1.2.3.4&port=+6000"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5267,14 +4747,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test + char in query. Bug 1035 */ static int HTPParserDecodingTest07(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET /put.php?ip=1.2.3.4&port=+6000 HTTP/1.1\r\nHost: www.domain.ltd\r\n\r\n"; @@ -5282,8 +4761,7 @@ static int HTPParserDecodingTest07(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5305,8 +4783,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5321,52 +4798,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/put.php?ip=1.2.3.4&port= 6000"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5374,14 +4826,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test 'proxy' URI normalization. Ticket 1008 */ static int HTPParserDecodingTest08(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET http://suricata-ids.org/blah/ HTTP/1.1\r\nHost: suricata-ids.org\r\n\r\n"; @@ -5389,8 +4840,7 @@ static int HTPParserDecodingTest08(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5409,8 +4859,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5425,52 +4874,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "/blah/"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5478,14 +4902,13 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test 'proxy' URI normalization. Ticket 1008 */ static int HTPParserDecodingTest09(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "GET http://suricata-ids.org/blah/ HTTP/1.1\r\nHost: suricata-ids.org\r\n\r\n"; @@ -5493,8 +4916,7 @@ static int HTPParserDecodingTest09(void) TcpSession ssn; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5514,8 +4936,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", addr, 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5530,52 +4951,27 @@ libhtp:\n\ else if (u == (httplen1 - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, &httpbuf1[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); uint8_t ref1[] = "http://suricata-ids.org/blah/"; size_t reflen = sizeof(ref1) - 1; htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL) - goto end; + FAIL_IF_NULL(tx); HtpTxUserData *tx_ud = (HtpTxUserData *) htp_tx_get_user_data(tx); - if (tx_ud != NULL && tx_ud->request_uri_normalized != NULL) { - if (reflen != bstr_len(tx_ud->request_uri_normalized)) { - printf("normalized uri len should be %"PRIuMAX", is %"PRIuMAX, - (uintmax_t)reflen, - (uintmax_t)bstr_len(tx_ud->request_uri_normalized)); - goto end; - } - - if (memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, - bstr_len(tx_ud->request_uri_normalized)) != 0) - { - printf("normalized uri \""); - PrintRawUriFp(stdout, bstr_ptr(tx_ud->request_uri_normalized), bstr_len(tx_ud->request_uri_normalized)); - printf("\" != \""); - PrintRawUriFp(stdout, ref1, reflen); - printf("\": "); - goto end; - } - } + FAIL_IF_NULL(tx_ud); + FAIL_IF_NULL(tx_ud->request_uri_normalized); + FAIL_IF(reflen != bstr_len(tx_ud->request_uri_normalized)); - result = 1; + FAIL_IF(memcmp(bstr_ptr(tx_ud->request_uri_normalized), ref1, + bstr_len(tx_ud->request_uri_normalized)) != 0); -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); @@ -5583,13 +4979,12 @@ libhtp:\n\ StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test BG box crash -- chunks are messed up. Observed for real. */ static int HTPBodyReassemblyTest01(void) { - int result = 0; HtpTxUserData htud; memset(&htud, 0x00, sizeof(htud)); HtpState hstate; @@ -5607,17 +5002,15 @@ static int HTPBodyReassemblyTest01(void) uint8_t chunk2[] = "POST /uri HTTP/1.1\r\nHost: hostname.com\r\nKeep-Alive: 115\r\nAccept-Charset: utf-8\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nConnection: keep-alive\r\nContent-length: 68102\r\nReferer: http://otherhost.com\r\nAccept-Encoding: gzip\r\nContent-Type: multipart/form-data; boundary=e5a320f21416a02493a0a6f561b1c494\r\nCookie: blah\r\nAccept-Language: us\r\n\r\n--e5a320f21416a02493a0a6f561b1c494\r\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"D2GUef.jpg\"\r"; int r = HtpBodyAppendChunk(&htud.request_body, chunk1, sizeof(chunk1) - 1); - BUG_ON(r != 0); + FAIL_IF(r != 0); r = HtpBodyAppendChunk(&htud.request_body, chunk2, sizeof(chunk2) - 1); - BUG_ON(r != 0); + FAIL_IF(r != 0); const uint8_t *chunks_buffer = NULL; uint32_t chunks_buffer_len = 0; HtpRequestBodyReassemble(&htud, &chunks_buffer, &chunks_buffer_len); - if (chunks_buffer == NULL) { - goto end; - } + FAIL_IF_NULL(chunks_buffer); #ifdef PRINT printf("REASSCHUNK START: \n"); PrintRawDataFp(stdout, chunks_buffer, chunks_buffer_len); @@ -5630,22 +5023,16 @@ static int HTPBodyReassemblyTest01(void) htud.tsflags |= HTP_BOUNDARY_SET; HtpRequestBodyHandleMultipart(&hstate, &htud, &tx, chunks_buffer, chunks_buffer_len, false); - if (htud.request_body.content_len_so_far != 669) { - printf("htud.request_body.content_len_so_far %"PRIu64": ", htud.request_body.content_len_so_far); - goto end; - } + FAIL_IF(htud.request_body.content_len_so_far != 669); FAIL_IF_NOT_NULL(htud.files_ts.head); - result = 1; -end: - return result; + PASS; } /** \test BG crash */ static int HTPSegvTest01(void) { - int result = 0; Flow *f = NULL; uint8_t httpbuf1[] = "POST /uri HTTP/1.1\r\nHost: hostname.com\r\nKeep-Alive: 115\r\nAccept-Charset: utf-8\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686; rv:9.0.1) Gecko/20100101 Firefox/9.0.1\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nConnection: keep-alive\r\nContent-length: 68102\r\nReferer: http://otherhost.com\r\nAccept-Encoding: gzip\r\nContent-Type: multipart/form-data; boundary=e5a320f21416a02493a0a6f561b1c494\r\nCookie: blah\r\nAccept-Language: us\r\n\r\n--e5a320f21416a02493a0a6f561b1c494\r\nContent-Disposition: form-data; name=\"uploadfile\"; filename=\"D2GUef.jpg\"\r"; uint32_t httplen1 = sizeof(httpbuf1) - 1; /* minus the \0 */ @@ -5675,8 +5062,7 @@ libhtp:\n\ memset(&ssn, 0, sizeof(ssn)); f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5686,39 +5072,25 @@ libhtp:\n\ SCLogDebug("\n>>>> processing chunk 1 <<<<\n"); int r = AppLayerParserParse( NULL, alp_tctx, f, ALPROTO_HTTP1, STREAM_TOSERVER | STREAM_START, httpbuf1, httplen1); - if (r != 0) { - printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); - goto end; - } + FAIL_IF(r != 0); SCLogDebug("\n>>>> processing chunk 1 again <<<<\n"); r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, STREAM_TOSERVER, httpbuf1, httplen1); - if (r != 0) { - printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); - goto end; - } + FAIL_IF(r != 0); http_state = f->alstate; - if (http_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(http_state); AppLayerDecoderEvents *decoder_events = AppLayerParserGetDecoderEvents(f->alparser); - if (decoder_events != NULL) { - printf("app events: "); - goto end; - } - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + FAIL_IF_NOT_NULL(decoder_events); + + AppLayerParserThreadCtxFree(alp_tctx); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); HtpConfigRestoreBackup(); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test really long request, this should result in HTTP_DECODER_EVENT_REQUEST_FIELD_TOO_LONG */ @@ -5820,13 +5192,11 @@ libhtp:\n\ * update to allow it */ static int HTPParserTest15(void) { - int result = 0; Flow *f = NULL; char *httpbuf = NULL; size_t len = 18887; TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; char input[] = "\ %YAML 1.1\n\ ---\n\ @@ -5851,8 +5221,8 @@ libhtp:\n\ HTPConfigure(); httpbuf = SCMalloc(len); - if (unlikely(httpbuf == NULL)) - goto end; + FAIL_IF_NULL(httpbuf); + memset(httpbuf, 0x00, len); /* create the request with a longer than 18k cookie */ @@ -5874,8 +5244,7 @@ libhtp:\n\ httpbuf[len - 1] = '\n'; f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5890,59 +5259,40 @@ libhtp:\n\ else if (u == (len - 1)) flags = STREAM_TOSERVER|STREAM_EOF; else flags = STREAM_TOSERVER; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, (uint8_t *)&httpbuf[u], 1); - if (r != 0) { - printf("toserver chunk %" PRIu32 " returned %" PRId32 ", expected" - " 0: ", u, r); - goto end; - } + int r = AppLayerParserParse( + NULL, alp_tctx, f, ALPROTO_HTTP1, flags, (uint8_t *)&httpbuf[u], 1); + FAIL_IF(r != 0); } htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL || tx->request_method_number != HTP_M_GET || tx->request_protocol_number != HTP_PROTOCOL_1_1) - { - printf("expected method M_GET and got %s: , expected protocol " - "HTTP/1.1 and got %s \n", bstr_util_strdup_to_c(tx->request_method), - bstr_util_strdup_to_c(tx->request_protocol)); - goto end; - } + FAIL_IF_NULL(tx); + FAIL_IF(tx->request_method_number != HTP_M_GET); + FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_1); void *txtmp = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, f->alstate, 0); AppLayerDecoderEvents *decoder_events = AppLayerParserGetEventsByTx(IPPROTO_TCP, ALPROTO_HTTP1, txtmp); - if (decoder_events != NULL) { - printf("app events: "); - goto end; - } + FAIL_IF_NOT_NULL(decoder_events); - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - if (httpbuf != NULL) - SCFree(httpbuf); + SCFree(httpbuf); HTPFreeConfig(); ConfDeInit(); ConfRestoreContextBackup(); HtpConfigRestoreBackup(); - return result; + PASS; } /** \test Test unusual delims in request line HTTP_DECODER_EVENT_REQUEST_FIELD_TOO_LONG */ static int HTPParserTest16(void) { - int result = 0; Flow *f = NULL; TcpSession ssn; - HtpState *htp_state = NULL; - int r = 0; + HtpState *htp_state = NULL; AppLayerParserThreadCtx *alp_tctx = AppLayerParserThreadCtxAlloc(); memset(&ssn, 0, sizeof(ssn)); @@ -5958,8 +5308,7 @@ static int HTPParserTest16(void) size_t len = sizeof(httpbuf) - 1; f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 80); - if (f == NULL) - goto end; + FAIL_IF_NULL(f); f->protoctx = &ssn; f->proto = IPPROTO_TCP; f->alproto = ALPROTO_HTTP1; @@ -5968,55 +5317,33 @@ static int HTPParserTest16(void) uint8_t flags = STREAM_TOSERVER|STREAM_START|STREAM_EOF; - r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, (uint8_t *)httpbuf, len); - if (r != 0) { - printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r); - goto end; - } + int r = AppLayerParserParse(NULL, alp_tctx, f, ALPROTO_HTTP1, flags, (uint8_t *)httpbuf, len); + FAIL_IF(r != 0); htp_state = f->alstate; - if (htp_state == NULL) { - printf("no http state: "); - goto end; - } + FAIL_IF_NULL(htp_state); htp_tx_t *tx = HTPStateGetTx(htp_state, 0); - if (tx == NULL || tx->request_method_number != HTP_M_GET || tx->request_protocol_number != HTP_PROTOCOL_1_1) - { - printf("expected method M_GET and got %s: , expected protocol " - "HTTP/1.1 and got %s \n", tx ? bstr_util_strdup_to_c(tx->request_method) : "tx null", - tx ? bstr_util_strdup_to_c(tx->request_protocol) : "tx null"); - goto end; - } + FAIL_IF_NULL(tx); + FAIL_IF(tx->request_method_number != HTP_M_GET); + FAIL_IF(tx->request_protocol_number != HTP_PROTOCOL_1_1); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION //these events are disabled during fuzzing as they are too noisy and consume much resource void *txtmp = AppLayerParserGetTx(IPPROTO_TCP, ALPROTO_HTTP1, f->alstate, 0); AppLayerDecoderEvents *decoder_events = AppLayerParserGetEventsByTx(IPPROTO_TCP, ALPROTO_HTTP1, txtmp); - if (decoder_events == NULL) { - printf("no app events: "); - goto end; - } - if (decoder_events->events[0] != HTTP_DECODER_EVENT_METHOD_DELIM_NON_COMPLIANT) { - printf("HTTP_DECODER_EVENT_METHOD_DELIM_NON_COMPLIANT not set: "); - goto end; - } + FAIL_IF_NULL(decoder_events); + FAIL_IF(decoder_events->events[0] != HTTP_DECODER_EVENT_METHOD_DELIM_NON_COMPLIANT); + FAIL_IF(decoder_events->events[1] != HTTP_DECODER_EVENT_URI_DELIM_NON_COMPLIANT); - if (decoder_events->events[1] != HTTP_DECODER_EVENT_URI_DELIM_NON_COMPLIANT) { - printf("HTTP_DECODER_EVENT_URI_DELIM_NON_COMPLIANT not set: "); - goto end; - } #endif - result = 1; -end: - if (alp_tctx != NULL) - AppLayerParserThreadCtxFree(alp_tctx); + AppLayerParserThreadCtxFree(alp_tctx); StreamTcpFreeConfig(true); UTHFreeFlow(f); - return result; + PASS; } /** \test Test response not HTTP