From 34a57d0346afe95e11104462c30dc468b0cb0b89 Mon Sep 17 00:00:00 2001 From: Yash Tibrewal Date: Mon, 23 Oct 2017 15:33:21 -0700 Subject: rename all test core files to cc and a lot of C++ style conversions --- test/core/http/format_request_test.c | 149 ----------------- test/core/http/format_request_test.cc | 152 +++++++++++++++++ test/core/http/httpcli_test.c | 209 ----------------------- test/core/http/httpcli_test.cc | 211 +++++++++++++++++++++++ test/core/http/httpscli_test.c | 212 ------------------------ test/core/http/httpscli_test.cc | 214 ++++++++++++++++++++++++ test/core/http/parser_test.c | 299 --------------------------------- test/core/http/parser_test.cc | 304 ++++++++++++++++++++++++++++++++++ test/core/http/request_fuzzer.c | 42 ----- test/core/http/request_fuzzer.cc | 42 +++++ test/core/http/response_fuzzer.c | 41 ----- test/core/http/response_fuzzer.cc | 41 +++++ 12 files changed, 964 insertions(+), 952 deletions(-) delete mode 100644 test/core/http/format_request_test.c create mode 100644 test/core/http/format_request_test.cc delete mode 100644 test/core/http/httpcli_test.c create mode 100644 test/core/http/httpcli_test.cc delete mode 100644 test/core/http/httpscli_test.c create mode 100644 test/core/http/httpscli_test.cc delete mode 100644 test/core/http/parser_test.c create mode 100644 test/core/http/parser_test.cc delete mode 100644 test/core/http/request_fuzzer.c create mode 100644 test/core/http/request_fuzzer.cc delete mode 100644 test/core/http/response_fuzzer.c create mode 100644 test/core/http/response_fuzzer.cc (limited to 'test/core/http') diff --git a/test/core/http/format_request_test.c b/test/core/http/format_request_test.c deleted file mode 100644 index 0279a1b5e3..0000000000 --- a/test/core/http/format_request_test.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/http/format_request.h" - -#include - -#include -#include "test/core/util/test_config.h" - -static void test_format_get_request(void) { - grpc_http_header hdr = {"x-yz", "abc"}; - grpc_httpcli_request req; - grpc_slice slice; - - memset(&req, 0, sizeof(req)); - req.host = "example.com"; - req.http.path = "/index.html"; - req.http.hdr_count = 1; - req.http.hdrs = &hdr; - - slice = grpc_httpcli_format_get_request(&req); - - GPR_ASSERT(0 == grpc_slice_str_cmp(slice, - "GET /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "\r\n")); - - grpc_slice_unref(slice); -} - -static void test_format_post_request(void) { - grpc_http_header hdr = {"x-yz", "abc"}; - grpc_httpcli_request req; - grpc_slice slice; - char body_bytes[] = "fake body"; - size_t body_len = 9; - - memset(&req, 0, sizeof(req)); - req.host = "example.com"; - req.http.path = "/index.html"; - req.http.hdr_count = 1; - req.http.hdrs = &hdr; - - slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); - - GPR_ASSERT(0 == grpc_slice_str_cmp(slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "Content-Type: text/plain\r\n" - "Content-Length: 9\r\n" - "\r\n" - "fake body")); - - grpc_slice_unref(slice); -} - -static void test_format_post_request_no_body(void) { - grpc_http_header hdr = {"x-yz", "abc"}; - grpc_httpcli_request req; - grpc_slice slice; - - memset(&req, 0, sizeof(req)); - req.host = "example.com"; - req.http.path = "/index.html"; - req.http.hdr_count = 1; - req.http.hdrs = &hdr; - - slice = grpc_httpcli_format_post_request(&req, NULL, 0); - - GPR_ASSERT(0 == grpc_slice_str_cmp(slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT - "\r\n" - "x-yz: abc\r\n" - "\r\n")); - - grpc_slice_unref(slice); -} - -static void test_format_post_request_content_type_override(void) { - grpc_http_header hdrs[2]; - grpc_httpcli_request req; - grpc_slice slice; - char body_bytes[] = "fake%20body"; - size_t body_len = 11; - - hdrs[0].key = "x-yz"; - hdrs[0].value = "abc"; - hdrs[1].key = "Content-Type"; - hdrs[1].value = "application/x-www-form-urlencoded"; - memset(&req, 0, sizeof(req)); - req.host = "example.com"; - req.http.path = "/index.html"; - req.http.hdr_count = 2; - req.http.hdrs = hdrs; - - slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); - - GPR_ASSERT(0 == grpc_slice_str_cmp( - slice, - "POST /index.html HTTP/1.0\r\n" - "Host: example.com\r\n" - "Connection: close\r\n" - "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" - "x-yz: abc\r\n" - "Content-Type: application/x-www-form-urlencoded\r\n" - "Content-Length: 11\r\n" - "\r\n" - "fake%20body")); - - grpc_slice_unref(slice); -} - -int main(int argc, char **argv) { - grpc_test_init(argc, argv); - - test_format_get_request(); - test_format_post_request(); - test_format_post_request_no_body(); - test_format_post_request_content_type_override(); - - return 0; -} diff --git a/test/core/http/format_request_test.cc b/test/core/http/format_request_test.cc new file mode 100644 index 0000000000..b2d903458d --- /dev/null +++ b/test/core/http/format_request_test.cc @@ -0,0 +1,152 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/http/format_request.h" + +#include + +#include +#include "test/core/util/test_config.h" + +static void test_format_get_request(void) { + grpc_http_header hdr = {const_cast("x-yz"), + const_cast("abc")}; + grpc_httpcli_request req; + grpc_slice slice; + + memset(&req, 0, sizeof(req)); + req.host = const_cast("example.com"); + req.http.path = const_cast("/index.html"); + req.http.hdr_count = 1; + req.http.hdrs = &hdr; + + slice = grpc_httpcli_format_get_request(&req); + + GPR_ASSERT(0 == grpc_slice_str_cmp(slice, + "GET /index.html HTTP/1.0\r\n" + "Host: example.com\r\n" + "Connection: close\r\n" + "User-Agent: " GRPC_HTTPCLI_USER_AGENT + "\r\n" + "x-yz: abc\r\n" + "\r\n")); + + grpc_slice_unref(slice); +} + +static void test_format_post_request(void) { + grpc_http_header hdr = {const_cast("x-yz"), + const_cast("abc")}; + grpc_httpcli_request req; + grpc_slice slice; + char body_bytes[] = "fake body"; + size_t body_len = 9; + + memset(&req, 0, sizeof(req)); + req.host = const_cast("example.com"); + req.http.path = const_cast("/index.html"); + req.http.hdr_count = 1; + req.http.hdrs = &hdr; + + slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); + + GPR_ASSERT(0 == grpc_slice_str_cmp(slice, + "POST /index.html HTTP/1.0\r\n" + "Host: example.com\r\n" + "Connection: close\r\n" + "User-Agent: " GRPC_HTTPCLI_USER_AGENT + "\r\n" + "x-yz: abc\r\n" + "Content-Type: text/plain\r\n" + "Content-Length: 9\r\n" + "\r\n" + "fake body")); + + grpc_slice_unref(slice); +} + +static void test_format_post_request_no_body(void) { + grpc_http_header hdr = {const_cast("x-yz"), + const_cast("abc")}; + grpc_httpcli_request req; + grpc_slice slice; + + memset(&req, 0, sizeof(req)); + req.host = const_cast("example.com"); + req.http.path = const_cast("/index.html"); + req.http.hdr_count = 1; + req.http.hdrs = &hdr; + + slice = grpc_httpcli_format_post_request(&req, NULL, 0); + + GPR_ASSERT(0 == grpc_slice_str_cmp(slice, + "POST /index.html HTTP/1.0\r\n" + "Host: example.com\r\n" + "Connection: close\r\n" + "User-Agent: " GRPC_HTTPCLI_USER_AGENT + "\r\n" + "x-yz: abc\r\n" + "\r\n")); + + grpc_slice_unref(slice); +} + +static void test_format_post_request_content_type_override(void) { + grpc_http_header hdrs[2]; + grpc_httpcli_request req; + grpc_slice slice; + char body_bytes[] = "fake%20body"; + size_t body_len = 11; + + hdrs[0].key = const_cast("x-yz"); + hdrs[0].value = const_cast("abc"); + hdrs[1].key = const_cast("Content-Type"); + hdrs[1].value = const_cast("application/x-www-form-urlencoded"); + memset(&req, 0, sizeof(req)); + req.host = const_cast("example.com"); + req.http.path = const_cast("/index.html"); + req.http.hdr_count = 2; + req.http.hdrs = hdrs; + + slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len); + + GPR_ASSERT(0 == grpc_slice_str_cmp( + slice, + "POST /index.html HTTP/1.0\r\n" + "Host: example.com\r\n" + "Connection: close\r\n" + "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n" + "x-yz: abc\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: 11\r\n" + "\r\n" + "fake%20body")); + + grpc_slice_unref(slice); +} + +int main(int argc, char **argv) { + grpc_test_init(argc, argv); + + test_format_get_request(); + test_format_post_request(); + test_format_post_request_no_body(); + test_format_post_request_content_type_override(); + + return 0; +} diff --git a/test/core/http/httpcli_test.c b/test/core/http/httpcli_test.c deleted file mode 100644 index cc1c16d695..0000000000 --- a/test/core/http/httpcli_test.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/http/httpcli.h" - -#include - -#include -#include -#include -#include -#include -#include -#include "src/core/lib/iomgr/iomgr.h" -#include "test/core/util/port.h" -#include "test/core/util/test_config.h" - -static int g_done = 0; -static grpc_httpcli_context g_context; -static gpr_mu *g_mu; -static grpc_polling_entity g_pops; - -static grpc_millis n_seconds_time(int seconds) { - return grpc_timespec_to_millis_round_up( - grpc_timeout_seconds_to_deadline(seconds)); -} - -static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { - const char *expect = - "Hello world!" - "

This is a test

"; - grpc_http_response *response = arg; - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - GPR_ASSERT(response->body_length == strlen(expect)); - GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); - gpr_mu_lock(g_mu); - g_done = 1; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_kick", - grpc_pollset_kick(exec_ctx, grpc_polling_entity_pollset(&g_pops), NULL))); - gpr_mu_unlock(g_mu); -} - -static void test_get(int port) { - grpc_httpcli_request req; - char *host; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - - g_done = 0; - gpr_log(GPR_INFO, "test_get"); - - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "requesting from %s", host); - - memset(&req, 0, sizeof(req)); - req.host = host; - req.http.path = "/get"; - req.handshaker = &grpc_httpcli_plaintext; - - grpc_http_response response; - memset(&response, 0, sizeof(response)); - grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get"); - grpc_httpcli_get( - &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15), - GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), - &response); - grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); - gpr_mu_lock(g_mu); - while (!g_done) { - grpc_pollset_worker *worker = NULL; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_work", - grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &worker, n_seconds_time(1)))); - gpr_mu_unlock(g_mu); - grpc_exec_ctx_finish(&exec_ctx); - gpr_mu_lock(g_mu); - } - gpr_mu_unlock(g_mu); - gpr_free(host); - grpc_http_response_destroy(&response); -} - -static void test_post(int port) { - grpc_httpcli_request req; - char *host; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - - g_done = 0; - gpr_log(GPR_INFO, "test_post"); - - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "posting to %s", host); - - memset(&req, 0, sizeof(req)); - req.host = host; - req.http.path = "/post"; - req.handshaker = &grpc_httpcli_plaintext; - - grpc_http_response response; - memset(&response, 0, sizeof(response)); - grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post"); - grpc_httpcli_post( - &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5, - n_seconds_time(15), - GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), - &response); - grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); - gpr_mu_lock(g_mu); - while (!g_done) { - grpc_pollset_worker *worker = NULL; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_work", - grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &worker, n_seconds_time(1)))); - gpr_mu_unlock(g_mu); - grpc_exec_ctx_finish(&exec_ctx); - gpr_mu_lock(g_mu); - } - gpr_mu_unlock(g_mu); - gpr_free(host); - grpc_http_response_destroy(&response); -} - -static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) { - grpc_pollset_destroy(exec_ctx, grpc_polling_entity_pollset(p)); -} - -int main(int argc, char **argv) { - grpc_closure destroyed; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - gpr_subprocess *server; - char *me = argv[0]; - char *lslash = strrchr(me, '/'); - char *args[4]; - int port = grpc_pick_unused_port_or_die(); - int arg_shift = 0; - /* figure out where we are */ - char *root; - if (lslash) { - root = gpr_malloc((size_t)(lslash - me + 1)); - memcpy(root, me, (size_t)(lslash - me)); - root[lslash - me] = 0; - } else { - root = gpr_strdup("."); - } - - GPR_ASSERT(argc <= 2); - if (argc == 2) { - args[0] = gpr_strdup(argv[1]); - } else { - arg_shift = 1; - gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root); - gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root); - } - - /* start the server */ - args[1 + arg_shift] = "--port"; - gpr_asprintf(&args[2 + arg_shift], "%d", port); - server = gpr_subprocess_create(3 + arg_shift, (const char **)args); - GPR_ASSERT(server); - gpr_free(args[0]); - if (arg_shift) gpr_free(args[1]); - gpr_free(args[2 + arg_shift]); - gpr_free(root); - - gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(5, GPR_TIMESPAN))); - - grpc_test_init(argc, argv); - grpc_init(); - grpc_httpcli_context_init(&g_context); - grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size()); - grpc_pollset_init(pollset, &g_mu); - g_pops = grpc_polling_entity_create_from_pollset(pollset); - - test_get(port); - test_post(port); - - grpc_httpcli_context_destroy(&exec_ctx, &g_context); - GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops, - grpc_schedule_on_exec_ctx); - grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &destroyed); - grpc_exec_ctx_finish(&exec_ctx); - grpc_shutdown(); - - gpr_free(grpc_polling_entity_pollset(&g_pops)); - - gpr_subprocess_destroy(server); - - return 0; -} diff --git a/test/core/http/httpcli_test.cc b/test/core/http/httpcli_test.cc new file mode 100644 index 0000000000..925f63decc --- /dev/null +++ b/test/core/http/httpcli_test.cc @@ -0,0 +1,211 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/http/httpcli.h" + +#include + +#include +#include +#include +#include +#include +#include +#include "src/core/lib/iomgr/iomgr.h" +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" + +static int g_done = 0; +static grpc_httpcli_context g_context; +static gpr_mu *g_mu; +static grpc_polling_entity g_pops; + +static grpc_millis n_seconds_time(int seconds) { + return grpc_timespec_to_millis_round_up( + grpc_timeout_seconds_to_deadline(seconds)); +} + +static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { + const char *expect = + "Hello world!" + "

This is a test

"; + grpc_http_response *response = static_cast(arg); + GPR_ASSERT(response); + GPR_ASSERT(response->status == 200); + GPR_ASSERT(response->body_length == strlen(expect)); + GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); + gpr_mu_lock(g_mu); + g_done = 1; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_kick", + grpc_pollset_kick(exec_ctx, grpc_polling_entity_pollset(&g_pops), NULL))); + gpr_mu_unlock(g_mu); +} + +static void test_get(int port) { + grpc_httpcli_request req; + char *host; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + + g_done = 0; + gpr_log(GPR_INFO, "test_get"); + + gpr_asprintf(&host, "localhost:%d", port); + gpr_log(GPR_INFO, "requesting from %s", host); + + memset(&req, 0, sizeof(req)); + req.host = host; + req.http.path = const_cast("/get"); + req.handshaker = &grpc_httpcli_plaintext; + + grpc_http_response response; + memset(&response, 0, sizeof(response)); + grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get"); + grpc_httpcli_get( + &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15), + GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), + &response); + grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); + gpr_mu_lock(g_mu); + while (!g_done) { + grpc_pollset_worker *worker = NULL; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_work", + grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &worker, n_seconds_time(1)))); + gpr_mu_unlock(g_mu); + grpc_exec_ctx_finish(&exec_ctx); + gpr_mu_lock(g_mu); + } + gpr_mu_unlock(g_mu); + gpr_free(host); + grpc_http_response_destroy(&response); +} + +static void test_post(int port) { + grpc_httpcli_request req; + char *host; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + + g_done = 0; + gpr_log(GPR_INFO, "test_post"); + + gpr_asprintf(&host, "localhost:%d", port); + gpr_log(GPR_INFO, "posting to %s", host); + + memset(&req, 0, sizeof(req)); + req.host = host; + req.http.path = const_cast("/post"); + req.handshaker = &grpc_httpcli_plaintext; + + grpc_http_response response; + memset(&response, 0, sizeof(response)); + grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post"); + grpc_httpcli_post( + &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5, + n_seconds_time(15), + GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), + &response); + grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); + gpr_mu_lock(g_mu); + while (!g_done) { + grpc_pollset_worker *worker = NULL; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_work", + grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &worker, n_seconds_time(1)))); + gpr_mu_unlock(g_mu); + grpc_exec_ctx_finish(&exec_ctx); + gpr_mu_lock(g_mu); + } + gpr_mu_unlock(g_mu); + gpr_free(host); + grpc_http_response_destroy(&response); +} + +static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) { + grpc_pollset_destroy(exec_ctx, grpc_polling_entity_pollset( + static_cast(p))); +} + +int main(int argc, char **argv) { + grpc_closure destroyed; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + gpr_subprocess *server; + char *me = argv[0]; + char *lslash = strrchr(me, '/'); + char *args[4]; + int port = grpc_pick_unused_port_or_die(); + int arg_shift = 0; + /* figure out where we are */ + char *root; + if (lslash) { + root = static_cast(gpr_malloc((size_t)(lslash - me + 1))); + memcpy(root, me, (size_t)(lslash - me)); + root[lslash - me] = 0; + } else { + root = gpr_strdup("."); + } + + GPR_ASSERT(argc <= 2); + if (argc == 2) { + args[0] = gpr_strdup(argv[1]); + } else { + arg_shift = 1; + gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root); + gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root); + } + + /* start the server */ + args[1 + arg_shift] = const_cast("--port"); + gpr_asprintf(&args[2 + arg_shift], "%d", port); + server = gpr_subprocess_create(3 + arg_shift, (const char **)args); + GPR_ASSERT(server); + gpr_free(args[0]); + if (arg_shift) gpr_free(args[1]); + gpr_free(args[2 + arg_shift]); + gpr_free(root); + + gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(5, GPR_TIMESPAN))); + + grpc_test_init(argc, argv); + grpc_init(); + grpc_httpcli_context_init(&g_context); + grpc_pollset *pollset = + static_cast(gpr_zalloc(grpc_pollset_size())); + grpc_pollset_init(pollset, &g_mu); + g_pops = grpc_polling_entity_create_from_pollset(pollset); + + test_get(port); + test_post(port); + + grpc_httpcli_context_destroy(&exec_ctx, &g_context); + GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops, + grpc_schedule_on_exec_ctx); + grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &destroyed); + grpc_exec_ctx_finish(&exec_ctx); + grpc_shutdown(); + + gpr_free(grpc_polling_entity_pollset(&g_pops)); + + gpr_subprocess_destroy(server); + + return 0; +} diff --git a/test/core/http/httpscli_test.c b/test/core/http/httpscli_test.c deleted file mode 100644 index f8a3cfdd76..0000000000 --- a/test/core/http/httpscli_test.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/http/httpcli.h" - -#include - -#include -#include -#include -#include -#include -#include -#include "src/core/lib/iomgr/iomgr.h" -#include "test/core/util/port.h" -#include "test/core/util/test_config.h" - -static int g_done = 0; -static grpc_httpcli_context g_context; -static gpr_mu *g_mu; -static grpc_polling_entity g_pops; - -static grpc_millis n_seconds_time(int seconds) { - return grpc_timespec_to_millis_round_up( - grpc_timeout_seconds_to_deadline(seconds)); -} - -static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { - const char *expect = - "Hello world!" - "

This is a test

"; - grpc_http_response *response = arg; - GPR_ASSERT(response); - GPR_ASSERT(response->status == 200); - GPR_ASSERT(response->body_length == strlen(expect)); - GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); - gpr_mu_lock(g_mu); - g_done = 1; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_kick", - grpc_pollset_kick(exec_ctx, grpc_polling_entity_pollset(&g_pops), NULL))); - gpr_mu_unlock(g_mu); -} - -static void test_get(int port) { - grpc_httpcli_request req; - char *host; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - - g_done = 0; - gpr_log(GPR_INFO, "test_get"); - - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "requesting from %s", host); - - memset(&req, 0, sizeof(req)); - req.host = host; - req.ssl_host_override = "foo.test.google.fr"; - req.http.path = "/get"; - req.handshaker = &grpc_httpcli_ssl; - - grpc_http_response response; - memset(&response, 0, sizeof(response)); - grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get"); - grpc_httpcli_get( - &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15), - GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), - &response); - grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); - gpr_mu_lock(g_mu); - while (!g_done) { - grpc_pollset_worker *worker = NULL; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_work", - grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &worker, n_seconds_time(1)))); - gpr_mu_unlock(g_mu); - grpc_exec_ctx_finish(&exec_ctx); - gpr_mu_lock(g_mu); - } - gpr_mu_unlock(g_mu); - gpr_free(host); - grpc_http_response_destroy(&response); -} - -static void test_post(int port) { - grpc_httpcli_request req; - char *host; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - - g_done = 0; - gpr_log(GPR_INFO, "test_post"); - - gpr_asprintf(&host, "localhost:%d", port); - gpr_log(GPR_INFO, "posting to %s", host); - - memset(&req, 0, sizeof(req)); - req.host = host; - req.ssl_host_override = "foo.test.google.fr"; - req.http.path = "/post"; - req.handshaker = &grpc_httpcli_ssl; - - grpc_http_response response; - memset(&response, 0, sizeof(response)); - grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post"); - grpc_httpcli_post( - &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5, - n_seconds_time(15), - GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), - &response); - grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); - gpr_mu_lock(g_mu); - while (!g_done) { - grpc_pollset_worker *worker = NULL; - GPR_ASSERT(GRPC_LOG_IF_ERROR( - "pollset_work", - grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &worker, n_seconds_time(1)))); - gpr_mu_unlock(g_mu); - grpc_exec_ctx_finish(&exec_ctx); - gpr_mu_lock(g_mu); - } - gpr_mu_unlock(g_mu); - gpr_free(host); - grpc_http_response_destroy(&response); -} - -static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) { - grpc_pollset_destroy(exec_ctx, grpc_polling_entity_pollset(p)); -} - -int main(int argc, char **argv) { - grpc_closure destroyed; - grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - gpr_subprocess *server; - char *me = argv[0]; - char *lslash = strrchr(me, '/'); - char *args[5]; - int port = grpc_pick_unused_port_or_die(); - int arg_shift = 0; - /* figure out where we are */ - char *root; - if (lslash) { - root = gpr_malloc((size_t)(lslash - me + 1)); - memcpy(root, me, (size_t)(lslash - me)); - root[lslash - me] = 0; - } else { - root = gpr_strdup("."); - } - - GPR_ASSERT(argc <= 2); - if (argc == 2) { - args[0] = gpr_strdup(argv[1]); - } else { - arg_shift = 1; - gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root); - gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root); - } - - /* start the server */ - args[1 + arg_shift] = "--port"; - gpr_asprintf(&args[2 + arg_shift], "%d", port); - args[3 + arg_shift] = "--ssl"; - server = gpr_subprocess_create(4 + arg_shift, (const char **)args); - GPR_ASSERT(server); - gpr_free(args[0]); - if (arg_shift) gpr_free(args[1]); - gpr_free(args[2 + arg_shift]); - gpr_free(root); - - gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), - gpr_time_from_seconds(5, GPR_TIMESPAN))); - - grpc_test_init(argc, argv); - grpc_init(); - grpc_httpcli_context_init(&g_context); - grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size()); - grpc_pollset_init(pollset, &g_mu); - g_pops = grpc_polling_entity_create_from_pollset(pollset); - - test_get(port); - test_post(port); - - grpc_httpcli_context_destroy(&exec_ctx, &g_context); - GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops, - grpc_schedule_on_exec_ctx); - grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops), - &destroyed); - grpc_exec_ctx_finish(&exec_ctx); - grpc_shutdown(); - - gpr_free(grpc_polling_entity_pollset(&g_pops)); - - gpr_subprocess_destroy(server); - - return 0; -} diff --git a/test/core/http/httpscli_test.cc b/test/core/http/httpscli_test.cc new file mode 100644 index 0000000000..179b3a720b --- /dev/null +++ b/test/core/http/httpscli_test.cc @@ -0,0 +1,214 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/http/httpcli.h" + +#include + +#include +#include +#include +#include +#include +#include +#include "src/core/lib/iomgr/iomgr.h" +#include "test/core/util/port.h" +#include "test/core/util/test_config.h" + +static int g_done = 0; +static grpc_httpcli_context g_context; +static gpr_mu *g_mu; +static grpc_polling_entity g_pops; + +static grpc_millis n_seconds_time(int seconds) { + return grpc_timespec_to_millis_round_up( + grpc_timeout_seconds_to_deadline(seconds)); +} + +static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { + const char *expect = + "Hello world!" + "

This is a test

"; + grpc_http_response *response = static_cast(arg); + GPR_ASSERT(response); + GPR_ASSERT(response->status == 200); + GPR_ASSERT(response->body_length == strlen(expect)); + GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); + gpr_mu_lock(g_mu); + g_done = 1; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_kick", + grpc_pollset_kick(exec_ctx, grpc_polling_entity_pollset(&g_pops), NULL))); + gpr_mu_unlock(g_mu); +} + +static void test_get(int port) { + grpc_httpcli_request req; + char *host; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + + g_done = 0; + gpr_log(GPR_INFO, "test_get"); + + gpr_asprintf(&host, "localhost:%d", port); + gpr_log(GPR_INFO, "requesting from %s", host); + + memset(&req, 0, sizeof(req)); + req.host = host; + req.ssl_host_override = const_cast("foo.test.google.fr"); + req.http.path = const_cast("/get"); + req.handshaker = &grpc_httpcli_ssl; + + grpc_http_response response; + memset(&response, 0, sizeof(response)); + grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get"); + grpc_httpcli_get( + &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15), + GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), + &response); + grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); + gpr_mu_lock(g_mu); + while (!g_done) { + grpc_pollset_worker *worker = NULL; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_work", + grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &worker, n_seconds_time(1)))); + gpr_mu_unlock(g_mu); + grpc_exec_ctx_finish(&exec_ctx); + gpr_mu_lock(g_mu); + } + gpr_mu_unlock(g_mu); + gpr_free(host); + grpc_http_response_destroy(&response); +} + +static void test_post(int port) { + grpc_httpcli_request req; + char *host; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + + g_done = 0; + gpr_log(GPR_INFO, "test_post"); + + gpr_asprintf(&host, "localhost:%d", port); + gpr_log(GPR_INFO, "posting to %s", host); + + memset(&req, 0, sizeof(req)); + req.host = host; + req.ssl_host_override = const_cast("foo.test.google.fr"); + req.http.path = const_cast("/post"); + req.handshaker = &grpc_httpcli_ssl; + + grpc_http_response response; + memset(&response, 0, sizeof(response)); + grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post"); + grpc_httpcli_post( + &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5, + n_seconds_time(15), + GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx), + &response); + grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); + gpr_mu_lock(g_mu); + while (!g_done) { + grpc_pollset_worker *worker = NULL; + GPR_ASSERT(GRPC_LOG_IF_ERROR( + "pollset_work", + grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &worker, n_seconds_time(1)))); + gpr_mu_unlock(g_mu); + grpc_exec_ctx_finish(&exec_ctx); + gpr_mu_lock(g_mu); + } + gpr_mu_unlock(g_mu); + gpr_free(host); + grpc_http_response_destroy(&response); +} + +static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) { + grpc_pollset_destroy(exec_ctx, grpc_polling_entity_pollset( + static_cast(p))); +} + +int main(int argc, char **argv) { + grpc_closure destroyed; + grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; + gpr_subprocess *server; + char *me = argv[0]; + char *lslash = strrchr(me, '/'); + char *args[5]; + int port = grpc_pick_unused_port_or_die(); + int arg_shift = 0; + /* figure out where we are */ + char *root; + if (lslash) { + root = static_cast(gpr_malloc((size_t)(lslash - me + 1))); + memcpy(root, me, (size_t)(lslash - me)); + root[lslash - me] = 0; + } else { + root = gpr_strdup("."); + } + + GPR_ASSERT(argc <= 2); + if (argc == 2) { + args[0] = gpr_strdup(argv[1]); + } else { + arg_shift = 1; + gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root); + gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root); + } + + /* start the server */ + args[1 + arg_shift] = const_cast("--port"); + gpr_asprintf(&args[2 + arg_shift], "%d", port); + args[3 + arg_shift] = const_cast("--ssl"); + server = gpr_subprocess_create(4 + arg_shift, (const char **)args); + GPR_ASSERT(server); + gpr_free(args[0]); + if (arg_shift) gpr_free(args[1]); + gpr_free(args[2 + arg_shift]); + gpr_free(root); + + gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), + gpr_time_from_seconds(5, GPR_TIMESPAN))); + + grpc_test_init(argc, argv); + grpc_init(); + grpc_httpcli_context_init(&g_context); + grpc_pollset *pollset = + static_cast(gpr_zalloc(grpc_pollset_size())); + grpc_pollset_init(pollset, &g_mu); + g_pops = grpc_polling_entity_create_from_pollset(pollset); + + test_get(port); + test_post(port); + + grpc_httpcli_context_destroy(&exec_ctx, &g_context); + GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops, + grpc_schedule_on_exec_ctx); + grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops), + &destroyed); + grpc_exec_ctx_finish(&exec_ctx); + grpc_shutdown(); + + gpr_free(grpc_polling_entity_pollset(&g_pops)); + + gpr_subprocess_destroy(server); + + return 0; +} diff --git a/test/core/http/parser_test.c b/test/core/http/parser_test.c deleted file mode 100644 index a7044c03bc..0000000000 --- a/test/core/http/parser_test.c +++ /dev/null @@ -1,299 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include "src/core/lib/http/parser.h" - -#include -#include - -#include -#include -#include -#include -#include "test/core/util/slice_splitter.h" -#include "test/core/util/test_config.h" - -static void test_request_succeeds(grpc_slice_split_mode split_mode, - char *request_text, char *expect_method, - grpc_http_version expect_version, - char *expect_path, char *expect_body, ...) { - grpc_http_parser parser; - grpc_slice input_slice = grpc_slice_from_copied_string(request_text); - size_t num_slices; - size_t i; - grpc_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); - grpc_slice_unref(input_slice); - - 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], NULL) == - GRPC_ERROR_NONE); - grpc_slice_unref(slices[i]); - } - GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); - - GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type); - 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) == request.body_length); - GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length)); - } else { - GPR_ASSERT(request.body_length == 0); - } - - va_start(args, expect_body); - i = 0; - for (;;) { - char *expect_key; - char *expect_value; - expect_key = va_arg(args, char *); - if (!expect_key) break; - GPR_ASSERT(i < request.hdr_count); - expect_value = va_arg(args, char *); - GPR_ASSERT(expect_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 == 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_text, - int expect_status, char *expect_body, ...) { - grpc_http_parser parser; - grpc_slice input_slice = grpc_slice_from_copied_string(response_text); - size_t num_slices; - size_t i; - grpc_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); - grpc_slice_unref(input_slice); - - 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], NULL) == - GRPC_ERROR_NONE); - grpc_slice_unref(slices[i]); - } - GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); - - GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type); - GPR_ASSERT(expect_status == response.status); - if (expect_body != NULL) { - GPR_ASSERT(strlen(expect_body) == response.body_length); - GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length)); - } else { - GPR_ASSERT(response.body_length == 0); - } - - va_start(args, expect_body); - i = 0; - for (;;) { - char *expect_key; - char *expect_value; - expect_key = va_arg(args, char *); - if (!expect_key) break; - GPR_ASSERT(i < response.hdr_count); - expect_value = va_arg(args, char *); - GPR_ASSERT(expect_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 == 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_text) { - grpc_http_parser parser; - grpc_slice input_slice = grpc_slice_from_copied_string(response_text); - size_t num_slices; - size_t i; - grpc_slice *slices; - 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); - grpc_slice_unref(input_slice); - - grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); - - for (i = 0; i < num_slices; i++) { - if (GRPC_ERROR_NONE == error) { - error = grpc_http_parser_parse(&parser, slices[i], NULL); - } - grpc_slice_unref(slices[i]); - } - if (GRPC_ERROR_NONE == error) { - error = grpc_http_parser_eof(&parser); - } - GPR_ASSERT(error != GRPC_ERROR_NONE); - GRPC_ERROR_UNREF(error); - - grpc_http_response_destroy(&response); - grpc_http_parser_destroy(&parser); - gpr_free(slices); -} - -static void test_request_fails(grpc_slice_split_mode split_mode, - char *request_text) { - grpc_http_parser parser; - grpc_slice input_slice = grpc_slice_from_copied_string(request_text); - size_t num_slices; - size_t i; - grpc_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); - grpc_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], NULL); - } - grpc_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) { - size_t i; - const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY, - GRPC_SLICE_SPLIT_ONE_BYTE}; - char *tmp1, *tmp2; - - grpc_test_init(argc, argv); - - for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { - test_succeeds(split_modes[i], - "HTTP/1.0 200 OK\r\n" - "xyz: abc\r\n" - "\r\n" - "hello world!", - 200, "hello world!", "xyz", "abc", NULL); - test_succeeds(split_modes[i], - "HTTP/1.0 404 Not Found\r\n" - "\r\n", - 404, NULL, NULL); - test_succeeds(split_modes[i], - "HTTP/1.1 200 OK\r\n" - "xyz: abc\r\n" - "\r\n" - "hello world!", - 200, "hello world!", "xyz", "abc", NULL); - test_succeeds(split_modes[i], - "HTTP/1.1 200 OK\n" - "\n" - "abc", - 200, "abc", NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/1.0\r\n" - "\r\n", - "GET", GRPC_HTTP_HTTP10, "/", NULL, NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/1.0\r\n" - "\r\n" - "xyz", - "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/1.1\r\n" - "\r\n" - "xyz", - "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/2.0\r\n" - "\r\n" - "xyz", - "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/1.0\r\n" - "xyz: abc\r\n" - "\r\n" - "xyz", - "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc", - NULL); - test_request_succeeds(split_modes[i], - "GET / HTTP/1.0\n" - "\n" - "xyz", - "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL); - test_fails(split_modes[i], "HTTP/1.0\r\n"); - test_fails(split_modes[i], "HTTP/1.2\r\n"); - test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n"); - test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n"); - test_fails(split_modes[i], - "HTTP/1.0 200 OK\r\n" - "xyz: abc\r\n" - " def\r\n" - "\r\n" - "hello world!"); - 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); - tmp1[2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1] = 0; - gpr_asprintf(&tmp2, "HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1); - test_fails(split_modes[i], tmp2); - gpr_free(tmp1); - gpr_free(tmp2); - } - - return 0; -} diff --git a/test/core/http/parser_test.cc b/test/core/http/parser_test.cc new file mode 100644 index 0000000000..9eff49122c --- /dev/null +++ b/test/core/http/parser_test.cc @@ -0,0 +1,304 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "src/core/lib/http/parser.h" + +#include +#include + +#include +#include +#include +#include +#include "test/core/util/slice_splitter.h" +#include "test/core/util/test_config.h" + +static void test_request_succeeds(grpc_slice_split_mode split_mode, + const char *request_text, + const char *expect_method, + grpc_http_version expect_version, + const char *expect_path, + const char *expect_body, ...) { + grpc_http_parser parser; + grpc_slice input_slice = grpc_slice_from_copied_string(request_text); + size_t num_slices; + size_t i; + grpc_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); + grpc_slice_unref(input_slice); + + 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], NULL) == + GRPC_ERROR_NONE); + grpc_slice_unref(slices[i]); + } + GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); + + GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type); + 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) == request.body_length); + GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length)); + } else { + GPR_ASSERT(request.body_length == 0); + } + + va_start(args, expect_body); + i = 0; + for (;;) { + char *expect_key; + char *expect_value; + expect_key = va_arg(args, char *); + if (!expect_key) break; + GPR_ASSERT(i < request.hdr_count); + expect_value = va_arg(args, char *); + GPR_ASSERT(expect_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 == 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, + const char *response_text, int expect_status, + const char *expect_body, ...) { + grpc_http_parser parser; + grpc_slice input_slice = grpc_slice_from_copied_string(response_text); + size_t num_slices; + size_t i; + grpc_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); + grpc_slice_unref(input_slice); + + 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], NULL) == + GRPC_ERROR_NONE); + grpc_slice_unref(slices[i]); + } + GPR_ASSERT(grpc_http_parser_eof(&parser) == GRPC_ERROR_NONE); + + GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type); + GPR_ASSERT(expect_status == response.status); + if (expect_body != NULL) { + GPR_ASSERT(strlen(expect_body) == response.body_length); + GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length)); + } else { + GPR_ASSERT(response.body_length == 0); + } + + va_start(args, expect_body); + i = 0; + for (;;) { + char *expect_key; + char *expect_value; + expect_key = va_arg(args, char *); + if (!expect_key) break; + GPR_ASSERT(i < response.hdr_count); + expect_value = va_arg(args, char *); + GPR_ASSERT(expect_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 == 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, + const char *response_text) { + grpc_http_parser parser; + grpc_slice input_slice = grpc_slice_from_copied_string(response_text); + size_t num_slices; + size_t i; + grpc_slice *slices; + 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); + grpc_slice_unref(input_slice); + + grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); + + for (i = 0; i < num_slices; i++) { + if (GRPC_ERROR_NONE == error) { + error = grpc_http_parser_parse(&parser, slices[i], NULL); + } + grpc_slice_unref(slices[i]); + } + if (GRPC_ERROR_NONE == error) { + error = grpc_http_parser_eof(&parser); + } + GPR_ASSERT(error != GRPC_ERROR_NONE); + GRPC_ERROR_UNREF(error); + + grpc_http_response_destroy(&response); + grpc_http_parser_destroy(&parser); + gpr_free(slices); +} + +static void test_request_fails(grpc_slice_split_mode split_mode, + const char *request_text) { + grpc_http_parser parser; + grpc_slice input_slice = grpc_slice_from_copied_string(request_text); + size_t num_slices; + size_t i; + grpc_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); + grpc_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], NULL); + } + grpc_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) { + size_t i; + const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY, + GRPC_SLICE_SPLIT_ONE_BYTE}; + char *tmp1, *tmp2; + + grpc_test_init(argc, argv); + + for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) { + test_succeeds(split_modes[i], + "HTTP/1.0 200 OK\r\n" + "xyz: abc\r\n" + "\r\n" + "hello world!", + 200, "hello world!", "xyz", "abc", NULL); + test_succeeds(split_modes[i], + "HTTP/1.0 404 Not Found\r\n" + "\r\n", + 404, NULL, NULL); + test_succeeds(split_modes[i], + "HTTP/1.1 200 OK\r\n" + "xyz: abc\r\n" + "\r\n" + "hello world!", + 200, "hello world!", "xyz", "abc", NULL); + test_succeeds(split_modes[i], + "HTTP/1.1 200 OK\n" + "\n" + "abc", + 200, "abc", NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/1.0\r\n" + "\r\n", + "GET", GRPC_HTTP_HTTP10, "/", NULL, NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/1.0\r\n" + "\r\n" + "xyz", + "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/1.1\r\n" + "\r\n" + "xyz", + "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/2.0\r\n" + "\r\n" + "xyz", + "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/1.0\r\n" + "xyz: abc\r\n" + "\r\n" + "xyz", + "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc", + NULL); + test_request_succeeds(split_modes[i], + "GET / HTTP/1.0\n" + "\n" + "xyz", + "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL); + test_fails(split_modes[i], "HTTP/1.0\r\n"); + test_fails(split_modes[i], "HTTP/1.2\r\n"); + test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n"); + test_fails(split_modes[i], "HTTP/1.0 200 OK\n"); + test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n"); + test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n"); + test_fails(split_modes[i], + "HTTP/1.0 200 OK\r\n" + "xyz: abc\r\n" + " def\r\n" + "\r\n" + "hello world!"); + 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 = + static_cast(gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH)); + memset(tmp1, 'a', 2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1); + tmp1[2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1] = 0; + gpr_asprintf(&tmp2, "HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1); + test_fails(split_modes[i], tmp2); + gpr_free(tmp1); + gpr_free(tmp2); + } + + return 0; +} diff --git a/test/core/http/request_fuzzer.c b/test/core/http/request_fuzzer.c deleted file mode 100644 index aefe9eb0f9..0000000000 --- a/test/core/http/request_fuzzer.c +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include -#include - -#include - -#include "src/core/lib/http/parser.h" - -bool squelch = true; -bool leak_check = true; - -int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - grpc_http_parser parser; - grpc_http_request request; - memset(&request, 0, sizeof(request)); - grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); - grpc_slice slice = grpc_slice_from_copied_buffer((const char *)data, size); - GRPC_ERROR_UNREF(grpc_http_parser_parse(&parser, slice, NULL)); - GRPC_ERROR_UNREF(grpc_http_parser_eof(&parser)); - grpc_slice_unref(slice); - grpc_http_parser_destroy(&parser); - grpc_http_request_destroy(&request); - return 0; -} diff --git a/test/core/http/request_fuzzer.cc b/test/core/http/request_fuzzer.cc new file mode 100644 index 0000000000..aefe9eb0f9 --- /dev/null +++ b/test/core/http/request_fuzzer.cc @@ -0,0 +1,42 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include + +#include + +#include "src/core/lib/http/parser.h" + +bool squelch = true; +bool leak_check = true; + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + grpc_http_parser parser; + grpc_http_request request; + memset(&request, 0, sizeof(request)); + grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request); + grpc_slice slice = grpc_slice_from_copied_buffer((const char *)data, size); + GRPC_ERROR_UNREF(grpc_http_parser_parse(&parser, slice, NULL)); + GRPC_ERROR_UNREF(grpc_http_parser_eof(&parser)); + grpc_slice_unref(slice); + grpc_http_parser_destroy(&parser); + grpc_http_request_destroy(&request); + return 0; +} diff --git a/test/core/http/response_fuzzer.c b/test/core/http/response_fuzzer.c deleted file mode 100644 index d5bb67500d..0000000000 --- a/test/core/http/response_fuzzer.c +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#include -#include - -#include - -#include "src/core/lib/http/parser.h" - -bool squelch = true; -bool leak_check = true; - -int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - grpc_http_parser parser; - grpc_http_response response; - memset(&response, 0, sizeof(response)); - grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); - grpc_slice slice = grpc_slice_from_copied_buffer((const char *)data, size); - GRPC_ERROR_UNREF(grpc_http_parser_parse(&parser, slice, NULL)); - GRPC_ERROR_UNREF(grpc_http_parser_eof(&parser)); - grpc_slice_unref(slice); - grpc_http_parser_destroy(&parser); - grpc_http_response_destroy(&response); - return 0; -} diff --git a/test/core/http/response_fuzzer.cc b/test/core/http/response_fuzzer.cc new file mode 100644 index 0000000000..d5bb67500d --- /dev/null +++ b/test/core/http/response_fuzzer.cc @@ -0,0 +1,41 @@ +/* + * + * Copyright 2015 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include + +#include + +#include "src/core/lib/http/parser.h" + +bool squelch = true; +bool leak_check = true; + +int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { + grpc_http_parser parser; + grpc_http_response response; + memset(&response, 0, sizeof(response)); + grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response); + grpc_slice slice = grpc_slice_from_copied_buffer((const char *)data, size); + GRPC_ERROR_UNREF(grpc_http_parser_parse(&parser, slice, NULL)); + GRPC_ERROR_UNREF(grpc_http_parser_eof(&parser)); + grpc_slice_unref(slice); + grpc_http_parser_destroy(&parser); + grpc_http_response_destroy(&response); + return 0; +} -- cgit v1.2.3