diff options
Diffstat (limited to 'test/core/http/parser_test.c')
-rw-r--r-- | test/core/http/parser_test.c | 162 |
1 files changed, 84 insertions, 78 deletions
diff --git a/test/core/http/parser_test.c b/test/core/http/parser_test.c index 7fdf60cc2b..d645d2879c 100644 --- a/test/core/http/parser_test.c +++ b/test/core/http/parser_test.c @@ -44,38 +44,39 @@ #include "test/core/util/test_config.h" static void test_request_succeeds(grpc_slice_split_mode split_mode, - char *request, char *expect_method, + char *request_text, char *expect_method, grpc_http_version expect_version, char *expect_path, char *expect_body, ...) { grpc_http_parser parser; - gpr_slice input_slice = gpr_slice_from_copied_string(request); + gpr_slice input_slice = gpr_slice_from_copied_string(request_text); size_t num_slices; size_t i; gpr_slice *slices; va_list args; + grpc_http_request request; + memset(&request, 0, sizeof(request)); grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); gpr_slice_unref(input_slice); - grpc_http_parser_init(&parser); + grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); for (i = 0; i < num_slices; i++) { - GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i])); + GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]) == GRPC_ERROR_NONE); gpr_slice_unref(slices[i]); } - GPR_ASSERT(grpc_http_parser_eof(&parser)); + GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type); - GPR_ASSERT(0 == strcmp(expect_method, parser.http.request.method)); - GPR_ASSERT(0 == strcmp(expect_path, parser.http.request.path)); - GPR_ASSERT(expect_version == parser.http.request.version); + GPR_ASSERT(0 == strcmp(expect_method, request.method)); + GPR_ASSERT(0 == strcmp(expect_path, request.path)); + GPR_ASSERT(expect_version == request.version); if (expect_body != NULL) { - GPR_ASSERT(strlen(expect_body) == parser.http.request.body_length); - GPR_ASSERT(0 == memcmp(expect_body, parser.http.request.body, - parser.http.request.body_length)); + GPR_ASSERT(strlen(expect_body) == request.body_length); + GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length)); } else { - GPR_ASSERT(parser.http.request.body_length == 0); + GPR_ASSERT(request.body_length == 0); } va_start(args, expect_body); @@ -85,48 +86,50 @@ static void test_request_succeeds(grpc_slice_split_mode split_mode, char *expect_value; expect_key = va_arg(args, char *); if (!expect_key) break; - GPR_ASSERT(i < parser.http.request.hdr_count); + GPR_ASSERT(i < request.hdr_count); expect_value = va_arg(args, char *); GPR_ASSERT(expect_value); - GPR_ASSERT(0 == strcmp(expect_key, parser.http.request.hdrs[i].key)); - GPR_ASSERT(0 == strcmp(expect_value, parser.http.request.hdrs[i].value)); + GPR_ASSERT(0 == strcmp(expect_key, request.hdrs[i].key)); + GPR_ASSERT(0 == strcmp(expect_value, request.hdrs[i].value)); i++; } va_end(args); - GPR_ASSERT(i == parser.http.request.hdr_count); + GPR_ASSERT(i == request.hdr_count); + grpc_http_request_destroy(&request); grpc_http_parser_destroy(&parser); gpr_free(slices); } -static void test_succeeds(grpc_slice_split_mode split_mode, char *response, +static void test_succeeds(grpc_slice_split_mode split_mode, char *response_text, int expect_status, char *expect_body, ...) { grpc_http_parser parser; - gpr_slice input_slice = gpr_slice_from_copied_string(response); + gpr_slice input_slice = gpr_slice_from_copied_string(response_text); size_t num_slices; size_t i; gpr_slice *slices; va_list args; + grpc_http_response response; + memset(&response, 0, sizeof(response)); grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); gpr_slice_unref(input_slice); - grpc_http_parser_init(&parser); + grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); for (i = 0; i < num_slices; i++) { - GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i])); + GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]) == GRPC_ERROR_NONE); gpr_slice_unref(slices[i]); } - GPR_ASSERT(grpc_http_parser_eof(&parser)); + GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type); - GPR_ASSERT(expect_status == parser.http.response.status); + GPR_ASSERT(expect_status == response.status); if (expect_body != NULL) { - GPR_ASSERT(strlen(expect_body) == parser.http.response.body_length); - GPR_ASSERT(0 == memcmp(expect_body, parser.http.response.body, - parser.http.response.body_length)); + GPR_ASSERT(strlen(expect_body) == response.body_length); + GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length)); } else { - GPR_ASSERT(parser.http.response.body_length == 0); + GPR_ASSERT(response.body_length == 0); } va_start(args, expect_body); @@ -136,77 +139,84 @@ static void test_succeeds(grpc_slice_split_mode split_mode, char *response, char *expect_value; expect_key = va_arg(args, char *); if (!expect_key) break; - GPR_ASSERT(i < parser.http.response.hdr_count); + GPR_ASSERT(i < response.hdr_count); expect_value = va_arg(args, char *); GPR_ASSERT(expect_value); - GPR_ASSERT(0 == strcmp(expect_key, parser.http.response.hdrs[i].key)); - GPR_ASSERT(0 == strcmp(expect_value, parser.http.response.hdrs[i].value)); + GPR_ASSERT(0 == strcmp(expect_key, response.hdrs[i].key)); + GPR_ASSERT(0 == strcmp(expect_value, response.hdrs[i].value)); i++; } va_end(args); - GPR_ASSERT(i == parser.http.response.hdr_count); + GPR_ASSERT(i == response.hdr_count); + grpc_http_response_destroy(&response); grpc_http_parser_destroy(&parser); gpr_free(slices); } -static void test_fails(grpc_slice_split_mode split_mode, char *response) { +static void test_fails(grpc_slice_split_mode split_mode, char *response_text) { grpc_http_parser parser; - gpr_slice input_slice = gpr_slice_from_copied_string(response); + gpr_slice input_slice = gpr_slice_from_copied_string(response_text); size_t num_slices; size_t i; gpr_slice *slices; - int done = 0; + grpc_error *error = GRPC_ERROR_NONE; + grpc_http_response response; + memset(&response, 0, sizeof(response)); grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); gpr_slice_unref(input_slice); - grpc_http_parser_init(&parser); + grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); for (i = 0; i < num_slices; i++) { - if (!done && !grpc_http_parser_parse(&parser, slices[i])) { - done = 1; + if (GRPC_ERROR_NONE == error) { + error = grpc_http_parser_parse(&parser, slices[i]); } gpr_slice_unref(slices[i]); } - if (!done && !grpc_http_parser_eof(&parser)) { - done = 1; + if (GRPC_ERROR_NONE == error) { + error = grpc_http_parser_eof(&parser); } - GPR_ASSERT(done); + GPR_ASSERT(error != GRPC_ERROR_NONE); + GRPC_ERROR_UNREF(error); + grpc_http_response_destroy(&response); grpc_http_parser_destroy(&parser); gpr_free(slices); } -static const uint8_t failed_test1[] = { - 0x9e, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x4a, - 0x48, 0x54, 0x54, 0x30, 0x32, 0x16, 0xa, 0x2f, 0x48, 0x20, - 0x31, 0x2e, 0x31, 0x20, 0x32, 0x30, 0x31, 0x54, 0x54, 0xb9, - 0x32, 0x31, 0x2e, 0x20, 0x32, 0x30, 0x20, -}; - -typedef struct { - const char *name; - const uint8_t *data; - size_t length; -} failed_test; - -#define FAILED_TEST(name) \ - { #name, name, sizeof(name) } - -failed_test failed_tests[] = { - FAILED_TEST(failed_test1), -}; - -static void test_doesnt_crash(failed_test t) { - gpr_log(GPR_DEBUG, "Run previously failed test: %s", t.name); - grpc_http_parser p; - grpc_http_parser_init(&p); - gpr_slice slice = - gpr_slice_from_copied_buffer((const char *)t.data, t.length); - grpc_http_parser_parse(&p, slice); - gpr_slice_unref(slice); - grpc_http_parser_destroy(&p); +static void test_request_fails(grpc_slice_split_mode split_mode, + char *request_text) { + grpc_http_parser parser; + gpr_slice input_slice = gpr_slice_from_copied_string(request_text); + size_t num_slices; + size_t i; + gpr_slice *slices; + grpc_error *error = GRPC_ERROR_NONE; + grpc_http_request request; + memset(&request, 0, sizeof(request)); + + grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices); + gpr_slice_unref(input_slice); + + grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); + + for (i = 0; i < num_slices; i++) { + if (error == GRPC_ERROR_NONE) { + error = grpc_http_parser_parse(&parser, slices[i]); + } + gpr_slice_unref(slices[i]); + } + if (error == GRPC_ERROR_NONE) { + error = grpc_http_parser_eof(&parser); + } + GPR_ASSERT(error != GRPC_ERROR_NONE); + GRPC_ERROR_UNREF(error); + + grpc_http_request_destroy(&request); + grpc_http_parser_destroy(&parser); + gpr_free(slices); } int main(int argc, char **argv) { @@ -217,10 +227,6 @@ int main(int argc, char **argv) { grpc_test_init(argc, argv); - for (i = 0; i < GPR_ARRAY_SIZE(failed_tests); i++) { - test_doesnt_crash(failed_tests[i]); - } - for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { test_succeeds(split_modes[i], "HTTP/1.0 200 OK\r\n" @@ -286,12 +292,12 @@ int main(int argc, char **argv) { " def\r\n" "\r\n" "hello world!"); - test_fails(split_modes[i], "GET\r\n"); - test_fails(split_modes[i], "GET /\r\n"); - test_fails(split_modes[i], "GET / HTTP/0.0\r\n"); - test_fails(split_modes[i], "GET / ____/1.0\r\n"); - test_fails(split_modes[i], "GET / HTTP/1.2\r\n"); - test_fails(split_modes[i], "GET / HTTP/1.0\n"); + test_request_fails(split_modes[i], "GET\r\n"); + test_request_fails(split_modes[i], "GET /\r\n"); + test_request_fails(split_modes[i], "GET / HTTP/0.0\r\n"); + test_request_fails(split_modes[i], "GET / ____/1.0\r\n"); + test_request_fails(split_modes[i], "GET / HTTP/1.2\r\n"); + test_request_fails(split_modes[i], "GET / HTTP/1.0\n"); tmp1 = gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH); memset(tmp1, 'a', 2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1); |