Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/http/httpcli.h" |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include <grpc/grpc.h> |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/string_util.h> |
| 42 | #include <grpc/support/subprocess.h> |
| 43 | #include <grpc/support/sync.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 44 | #include "src/core/lib/iomgr/iomgr.h" |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 45 | #include "test/core/util/port.h" |
| 46 | #include "test/core/util/test_config.h" |
| 47 | |
| 48 | static int g_done = 0; |
| 49 | static grpc_httpcli_context g_context; |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 50 | static gpr_mu *g_mu; |
David Garcia Quintas | 2a50dfe | 2016-05-31 15:09:12 -0700 | [diff] [blame] | 51 | static grpc_polling_entity g_pops; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 52 | |
| 53 | static gpr_timespec n_seconds_time(int seconds) { |
Robbie Shade | ca7effc | 2017-01-17 09:14:29 -0500 | [diff] [blame] | 54 | return grpc_timeout_seconds_to_deadline(seconds); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 57 | static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) { |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 58 | const char *expect = |
| 59 | "<html><head><title>Hello world!</title></head>" |
| 60 | "<body><p>This is a test</p></body></html>"; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 61 | grpc_http_response *response = arg; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 62 | GPR_ASSERT(response); |
| 63 | GPR_ASSERT(response->status == 200); |
| 64 | GPR_ASSERT(response->body_length == strlen(expect)); |
| 65 | GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length)); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 66 | gpr_mu_lock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 67 | g_done = 1; |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 68 | GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 69 | "pollset_kick", |
| 70 | grpc_pollset_kick(grpc_polling_entity_pollset(&g_pops), NULL))); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 71 | gpr_mu_unlock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 74 | static void test_get(int port) { |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 75 | grpc_httpcli_request req; |
| 76 | char *host; |
| 77 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 78 | |
| 79 | g_done = 0; |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 80 | gpr_log(GPR_INFO, "test_get"); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 81 | |
| 82 | gpr_asprintf(&host, "localhost:%d", port); |
| 83 | gpr_log(GPR_INFO, "requesting from %s", host); |
| 84 | |
| 85 | memset(&req, 0, sizeof(req)); |
| 86 | req.host = host; |
| 87 | req.ssl_host_override = "foo.test.google.fr"; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 88 | req.http.path = "/get"; |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 89 | req.handshaker = &grpc_httpcli_ssl; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 90 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 91 | grpc_http_response response; |
| 92 | memset(&response, 0, sizeof(response)); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 93 | grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get"); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 94 | grpc_httpcli_get( |
| 95 | &exec_ctx, &g_context, &g_pops, resource_quota, &req, n_seconds_time(15), |
| 96 | grpc_closure_create(on_finish, &response, grpc_schedule_on_exec_ctx), |
| 97 | &response); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 98 | grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 99 | gpr_mu_lock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 100 | while (!g_done) { |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 101 | grpc_pollset_worker *worker = NULL; |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 102 | GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 103 | "pollset_work", |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 104 | grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), |
| 105 | &worker, gpr_now(GPR_CLOCK_MONOTONIC), |
| 106 | n_seconds_time(20)))); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 107 | gpr_mu_unlock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 108 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 109 | gpr_mu_lock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 110 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 111 | gpr_mu_unlock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 112 | gpr_free(host); |
Craig Tiller | bc5fcad | 2016-05-12 16:23:15 -0700 | [diff] [blame] | 113 | grpc_http_response_destroy(&response); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 116 | static void test_post(int port) { |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 117 | grpc_httpcli_request req; |
| 118 | char *host; |
| 119 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 120 | |
| 121 | g_done = 0; |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 122 | gpr_log(GPR_INFO, "test_post"); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 123 | |
| 124 | gpr_asprintf(&host, "localhost:%d", port); |
| 125 | gpr_log(GPR_INFO, "posting to %s", host); |
| 126 | |
| 127 | memset(&req, 0, sizeof(req)); |
| 128 | req.host = host; |
| 129 | req.ssl_host_override = "foo.test.google.fr"; |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 130 | req.http.path = "/post"; |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 131 | req.handshaker = &grpc_httpcli_ssl; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 132 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 133 | grpc_http_response response; |
| 134 | memset(&response, 0, sizeof(response)); |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 135 | grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post"); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 136 | grpc_httpcli_post( |
| 137 | &exec_ctx, &g_context, &g_pops, resource_quota, &req, "hello", 5, |
| 138 | n_seconds_time(15), |
| 139 | grpc_closure_create(on_finish, &response, grpc_schedule_on_exec_ctx), |
| 140 | &response); |
Craig Tiller | a59c16c | 2016-10-31 07:25:01 -0700 | [diff] [blame] | 141 | grpc_resource_quota_unref_internal(&exec_ctx, resource_quota); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 142 | gpr_mu_lock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 143 | while (!g_done) { |
Craig Tiller | d0a8ae1 | 2016-02-18 08:01:19 -0800 | [diff] [blame] | 144 | grpc_pollset_worker *worker = NULL; |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 145 | GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 146 | "pollset_work", |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 147 | grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops), |
| 148 | &worker, gpr_now(GPR_CLOCK_MONOTONIC), |
| 149 | n_seconds_time(20)))); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 150 | gpr_mu_unlock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 151 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 152 | gpr_mu_lock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 153 | } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 154 | gpr_mu_unlock(g_mu); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 155 | gpr_free(host); |
Craig Tiller | bc5fcad | 2016-05-12 16:23:15 -0700 | [diff] [blame] | 156 | grpc_http_response_destroy(&response); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Craig Tiller | c97065d | 2016-06-07 13:00:14 -0700 | [diff] [blame] | 159 | static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) { |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 160 | grpc_pollset_destroy(grpc_polling_entity_pollset(p)); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | int main(int argc, char **argv) { |
| 164 | grpc_closure destroyed; |
| 165 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 166 | gpr_subprocess *server; |
| 167 | char *me = argv[0]; |
| 168 | char *lslash = strrchr(me, '/'); |
| 169 | char *args[5]; |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 170 | int port = grpc_pick_unused_port_or_die(); |
Nicolas "Pixel" Noble | 2d86263 | 2016-03-03 23:25:22 +0100 | [diff] [blame] | 171 | int arg_shift = 0; |
| 172 | /* figure out where we are */ |
| 173 | char *root; |
| 174 | if (lslash) { |
| 175 | root = gpr_malloc((size_t)(lslash - me + 1)); |
| 176 | memcpy(root, me, (size_t)(lslash - me)); |
| 177 | root[lslash - me] = 0; |
| 178 | } else { |
| 179 | root = gpr_strdup("."); |
| 180 | } |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 181 | |
Nicolas "Pixel" Noble | c27a99b | 2015-12-15 02:46:16 +0100 | [diff] [blame] | 182 | GPR_ASSERT(argc <= 2); |
| 183 | if (argc == 2) { |
| 184 | args[0] = gpr_strdup(argv[1]); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 185 | } else { |
Nicolas "Pixel" Noble | 2d86263 | 2016-03-03 23:25:22 +0100 | [diff] [blame] | 186 | arg_shift = 1; |
| 187 | gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root); |
Matthew Iselin | 1824f05 | 2016-02-10 12:16:06 +1100 | [diff] [blame] | 188 | gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* start the server */ |
Nicolas "Pixel" Noble | 2d86263 | 2016-03-03 23:25:22 +0100 | [diff] [blame] | 192 | args[1 + arg_shift] = "--port"; |
| 193 | gpr_asprintf(&args[2 + arg_shift], "%d", port); |
| 194 | args[3 + arg_shift] = "--ssl"; |
| 195 | server = gpr_subprocess_create(4 + arg_shift, (const char **)args); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 196 | GPR_ASSERT(server); |
| 197 | gpr_free(args[0]); |
Nicolas "Pixel" Noble | 2d86263 | 2016-03-03 23:25:22 +0100 | [diff] [blame] | 198 | if (arg_shift) gpr_free(args[1]); |
| 199 | gpr_free(args[2 + arg_shift]); |
| 200 | gpr_free(root); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 201 | |
| 202 | gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 203 | gpr_time_from_seconds(5, GPR_TIMESPAN))); |
| 204 | |
| 205 | grpc_test_init(argc, argv); |
| 206 | grpc_init(); |
| 207 | grpc_httpcli_context_init(&g_context); |
Yuchen Zeng | 47de64c | 2017-02-22 19:04:38 -0800 | [diff] [blame] | 208 | grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size()); |
David Garcia Quintas | f72eb97 | 2016-05-03 18:28:09 -0700 | [diff] [blame] | 209 | grpc_pollset_init(pollset, &g_mu); |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 210 | g_pops = grpc_polling_entity_create_from_pollset(pollset); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 211 | |
Craig Tiller | ed2164d | 2015-12-08 22:03:36 -0800 | [diff] [blame] | 212 | test_get(port); |
| 213 | test_post(port); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 214 | |
Craig Tiller | 9e5ac1b | 2017-02-14 22:25:50 -0800 | [diff] [blame] | 215 | grpc_httpcli_context_destroy(&exec_ctx, &g_context); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 216 | grpc_closure_init(&destroyed, destroy_pops, &g_pops, |
| 217 | grpc_schedule_on_exec_ctx); |
David Garcia Quintas | 69ff63d | 2016-06-06 16:39:47 -0700 | [diff] [blame] | 218 | grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops), |
| 219 | &destroyed); |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 220 | grpc_exec_ctx_finish(&exec_ctx); |
| 221 | grpc_shutdown(); |
| 222 | |
David Garcia Quintas | c4d5112 | 2016-06-06 14:56:02 -0700 | [diff] [blame] | 223 | gpr_free(grpc_polling_entity_pollset(&g_pops)); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 224 | |
Craig Tiller | cc0535d | 2015-12-08 15:14:47 -0800 | [diff] [blame] | 225 | gpr_subprocess_destroy(server); |
| 226 | |
| 227 | return 0; |
| 228 | } |