blob: 549411037e56b0e377ec252c44808a8dfe55365c [file] [log] [blame]
Craig Tillercc0535d2015-12-08 15:14:47 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillercc0535d2015-12-08 15:14:47 -08004 * 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 Tiller9533d042016-03-25 17:11:06 -070034#include "src/core/lib/http/httpcli.h"
Craig Tillercc0535d2015-12-08 15:14:47 -080035
36#include <string.h>
37
38#include <grpc/grpc.h>
Craig Tillercc0535d2015-12-08 15:14:47 -080039#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 Tiller9533d042016-03-25 17:11:06 -070044#include "src/core/lib/iomgr/iomgr.h"
Craig Tillercc0535d2015-12-08 15:14:47 -080045#include "test/core/util/port.h"
46#include "test/core/util/test_config.h"
47
48static int g_done = 0;
49static grpc_httpcli_context g_context;
Craig Tiller69b093b2016-02-25 19:04:07 -080050static gpr_mu *g_mu;
David Garcia Quintas2a50dfe2016-05-31 15:09:12 -070051static grpc_polling_entity g_pops;
Craig Tillercc0535d2015-12-08 15:14:47 -080052
53static gpr_timespec n_seconds_time(int seconds) {
Robbie Shadeca7effc2017-01-17 09:14:29 -050054 return grpc_timeout_seconds_to_deadline(seconds);
Craig Tillercc0535d2015-12-08 15:14:47 -080055}
56
Craig Tillerf707d622016-05-06 14:26:12 -070057static void on_finish(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
Craig Tillercc0535d2015-12-08 15:14:47 -080058 const char *expect =
59 "<html><head><title>Hello world!</title></head>"
60 "<body><p>This is a test</p></body></html>";
Craig Tillerf707d622016-05-06 14:26:12 -070061 grpc_http_response *response = arg;
Craig Tillercc0535d2015-12-08 15:14:47 -080062 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 Tiller69b093b2016-02-25 19:04:07 -080066 gpr_mu_lock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -080067 g_done = 1;
Craig Tillerc97065d2016-06-07 13:00:14 -070068 GPR_ASSERT(GRPC_LOG_IF_ERROR(
69 "pollset_kick",
70 grpc_pollset_kick(grpc_polling_entity_pollset(&g_pops), NULL)));
Craig Tiller69b093b2016-02-25 19:04:07 -080071 gpr_mu_unlock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -080072}
73
Craig Tillered2164d2015-12-08 22:03:36 -080074static void test_get(int port) {
Craig Tillercc0535d2015-12-08 15:14:47 -080075 grpc_httpcli_request req;
76 char *host;
77 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
78
79 g_done = 0;
Craig Tillered2164d2015-12-08 22:03:36 -080080 gpr_log(GPR_INFO, "test_get");
Craig Tillercc0535d2015-12-08 15:14:47 -080081
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 Iselin1824f052016-02-10 12:16:06 +110088 req.http.path = "/get";
Craig Tillered2164d2015-12-08 22:03:36 -080089 req.handshaker = &grpc_httpcli_ssl;
Craig Tillercc0535d2015-12-08 15:14:47 -080090
Craig Tillerf707d622016-05-06 14:26:12 -070091 grpc_http_response response;
92 memset(&response, 0, sizeof(response));
Craig Tiller20afa3d2016-10-17 14:52:14 -070093 grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_get");
Craig Tiller91031da2016-12-28 15:44:25 -080094 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 Tillera59c16c2016-10-31 07:25:01 -070098 grpc_resource_quota_unref_internal(&exec_ctx, resource_quota);
Craig Tiller69b093b2016-02-25 19:04:07 -080099 gpr_mu_lock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800100 while (!g_done) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800101 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700102 GPR_ASSERT(GRPC_LOG_IF_ERROR(
103 "pollset_work",
Craig Tillerc97065d2016-06-07 13:00:14 -0700104 grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
105 &worker, gpr_now(GPR_CLOCK_MONOTONIC),
106 n_seconds_time(20))));
Craig Tiller69b093b2016-02-25 19:04:07 -0800107 gpr_mu_unlock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800108 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800109 gpr_mu_lock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800110 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800111 gpr_mu_unlock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800112 gpr_free(host);
Craig Tillerbc5fcad2016-05-12 16:23:15 -0700113 grpc_http_response_destroy(&response);
Craig Tillercc0535d2015-12-08 15:14:47 -0800114}
115
Craig Tillered2164d2015-12-08 22:03:36 -0800116static void test_post(int port) {
Craig Tillercc0535d2015-12-08 15:14:47 -0800117 grpc_httpcli_request req;
118 char *host;
119 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
120
121 g_done = 0;
Craig Tillered2164d2015-12-08 22:03:36 -0800122 gpr_log(GPR_INFO, "test_post");
Craig Tillercc0535d2015-12-08 15:14:47 -0800123
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 Iselin1824f052016-02-10 12:16:06 +1100130 req.http.path = "/post";
Craig Tillered2164d2015-12-08 22:03:36 -0800131 req.handshaker = &grpc_httpcli_ssl;
Craig Tillercc0535d2015-12-08 15:14:47 -0800132
Craig Tillerf707d622016-05-06 14:26:12 -0700133 grpc_http_response response;
134 memset(&response, 0, sizeof(response));
Craig Tiller20afa3d2016-10-17 14:52:14 -0700135 grpc_resource_quota *resource_quota = grpc_resource_quota_create("test_post");
Craig Tiller91031da2016-12-28 15:44:25 -0800136 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 Tillera59c16c2016-10-31 07:25:01 -0700141 grpc_resource_quota_unref_internal(&exec_ctx, resource_quota);
Craig Tiller69b093b2016-02-25 19:04:07 -0800142 gpr_mu_lock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800143 while (!g_done) {
Craig Tillerd0a8ae12016-02-18 08:01:19 -0800144 grpc_pollset_worker *worker = NULL;
Craig Tiller1aee5362016-05-07 11:26:50 -0700145 GPR_ASSERT(GRPC_LOG_IF_ERROR(
146 "pollset_work",
Craig Tillerc97065d2016-06-07 13:00:14 -0700147 grpc_pollset_work(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
148 &worker, gpr_now(GPR_CLOCK_MONOTONIC),
149 n_seconds_time(20))));
Craig Tiller69b093b2016-02-25 19:04:07 -0800150 gpr_mu_unlock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800151 grpc_exec_ctx_finish(&exec_ctx);
Craig Tiller69b093b2016-02-25 19:04:07 -0800152 gpr_mu_lock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800153 }
Craig Tiller69b093b2016-02-25 19:04:07 -0800154 gpr_mu_unlock(g_mu);
Craig Tillercc0535d2015-12-08 15:14:47 -0800155 gpr_free(host);
Craig Tillerbc5fcad2016-05-12 16:23:15 -0700156 grpc_http_response_destroy(&response);
Craig Tillercc0535d2015-12-08 15:14:47 -0800157}
158
Craig Tillerc97065d2016-06-07 13:00:14 -0700159static void destroy_pops(grpc_exec_ctx *exec_ctx, void *p, grpc_error *error) {
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700160 grpc_pollset_destroy(grpc_polling_entity_pollset(p));
Craig Tillercc0535d2015-12-08 15:14:47 -0800161}
162
163int 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 Tillercc0535d2015-12-08 15:14:47 -0800170 int port = grpc_pick_unused_port_or_die();
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100171 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 Tillercc0535d2015-12-08 15:14:47 -0800181
Nicolas "Pixel" Noblec27a99b2015-12-15 02:46:16 +0100182 GPR_ASSERT(argc <= 2);
183 if (argc == 2) {
184 args[0] = gpr_strdup(argv[1]);
Craig Tillercc0535d2015-12-08 15:14:47 -0800185 } else {
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100186 arg_shift = 1;
187 gpr_asprintf(&args[0], "%s/../../tools/distrib/python_wrapper.sh", root);
Matthew Iselin1824f052016-02-10 12:16:06 +1100188 gpr_asprintf(&args[1], "%s/../../test/core/http/test_server.py", root);
Craig Tillercc0535d2015-12-08 15:14:47 -0800189 }
190
191 /* start the server */
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100192 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 Tillercc0535d2015-12-08 15:14:47 -0800196 GPR_ASSERT(server);
197 gpr_free(args[0]);
Nicolas "Pixel" Noble2d862632016-03-03 23:25:22 +0100198 if (arg_shift) gpr_free(args[1]);
199 gpr_free(args[2 + arg_shift]);
200 gpr_free(root);
Craig Tillercc0535d2015-12-08 15:14:47 -0800201
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 Zeng47de64c2017-02-22 19:04:38 -0800208 grpc_pollset *pollset = gpr_zalloc(grpc_pollset_size());
David Garcia Quintasf72eb972016-05-03 18:28:09 -0700209 grpc_pollset_init(pollset, &g_mu);
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700210 g_pops = grpc_polling_entity_create_from_pollset(pollset);
Craig Tillercc0535d2015-12-08 15:14:47 -0800211
Craig Tillered2164d2015-12-08 22:03:36 -0800212 test_get(port);
213 test_post(port);
Craig Tillercc0535d2015-12-08 15:14:47 -0800214
Craig Tiller9e5ac1b2017-02-14 22:25:50 -0800215 grpc_httpcli_context_destroy(&exec_ctx, &g_context);
Craig Tiller91031da2016-12-28 15:44:25 -0800216 grpc_closure_init(&destroyed, destroy_pops, &g_pops,
217 grpc_schedule_on_exec_ctx);
David Garcia Quintas69ff63d2016-06-06 16:39:47 -0700218 grpc_pollset_shutdown(&exec_ctx, grpc_polling_entity_pollset(&g_pops),
219 &destroyed);
Craig Tillercc0535d2015-12-08 15:14:47 -0800220 grpc_exec_ctx_finish(&exec_ctx);
221 grpc_shutdown();
222
David Garcia Quintasc4d51122016-06-06 14:56:02 -0700223 gpr_free(grpc_polling_entity_pollset(&g_pops));
Craig Tiller69b093b2016-02-25 19:04:07 -0800224
Craig Tillercc0535d2015-12-08 15:14:47 -0800225 gpr_subprocess_destroy(server);
226
227 return 0;
228}