Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / test / core / http / httpscli_test.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include "src/core/lib/http/httpcli.h"
20
21 #include <string.h>
22
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security_constants.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/log.h>
27 #include <grpc/support/string_util.h>
28 #include <grpc/support/sync.h>
29
30 #include "src/core/lib/gpr/env.h"
31 #include "src/core/lib/iomgr/iomgr.h"
32 #include "src/core/lib/security/security_connector/ssl_utils.h"
33 #include "test/core/util/port.h"
34 #include "test/core/util/subprocess.h"
35 #include "test/core/util/test_config.h"
36
37 static int g_done = 0;
38 static grpc_httpcli_context g_context;
39 static gpr_mu* g_mu;
40 static grpc_polling_entity g_pops;
41
42 static grpc_millis n_seconds_time(int seconds) {
43   return grpc_timespec_to_millis_round_up(
44       grpc_timeout_seconds_to_deadline(seconds));
45 }
46
47 static void on_finish(void* arg, grpc_error* error) {
48   const char* expect =
49       "<html><head><title>Hello world!</title></head>"
50       "<body><p>This is a test</p></body></html>";
51   grpc_http_response* response = static_cast<grpc_http_response*>(arg);
52   GPR_ASSERT(response);
53   gpr_log(GPR_INFO, "response status=%d error=%s", response->status,
54           grpc_error_string(error));
55   GPR_ASSERT(response->status == 200);
56   GPR_ASSERT(response->body_length == strlen(expect));
57   GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
58   gpr_mu_lock(g_mu);
59   g_done = 1;
60   GPR_ASSERT(GRPC_LOG_IF_ERROR(
61       "pollset_kick",
62       grpc_pollset_kick(grpc_polling_entity_pollset(&g_pops), nullptr)));
63   gpr_mu_unlock(g_mu);
64 }
65
66 static void test_get(int port) {
67   grpc_httpcli_request req;
68   char* host;
69   grpc_core::ExecCtx exec_ctx;
70
71   g_done = 0;
72   gpr_log(GPR_INFO, "test_get");
73
74   gpr_asprintf(&host, "localhost:%d", port);
75   gpr_log(GPR_INFO, "requesting from %s", host);
76
77   memset(&req, 0, sizeof(req));
78   req.host = host;
79   req.ssl_host_override = const_cast<char*>("foo.test.google.fr");
80   req.http.path = const_cast<char*>("/get");
81   req.handshaker = &grpc_httpcli_ssl;
82
83   grpc_http_response response;
84   memset(&response, 0, sizeof(response));
85   grpc_resource_quota* resource_quota = grpc_resource_quota_create("test_get");
86   grpc_httpcli_get(
87       &g_context, &g_pops, resource_quota, &req, n_seconds_time(15),
88       GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx),
89       &response);
90   grpc_resource_quota_unref_internal(resource_quota);
91   gpr_mu_lock(g_mu);
92   while (!g_done) {
93     grpc_pollset_worker* worker = nullptr;
94     GPR_ASSERT(GRPC_LOG_IF_ERROR(
95         "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops),
96                                           &worker, n_seconds_time(1))));
97     gpr_mu_unlock(g_mu);
98     grpc_core::ExecCtx::Get()->Flush();
99     gpr_mu_lock(g_mu);
100   }
101   gpr_mu_unlock(g_mu);
102   gpr_free(host);
103   grpc_http_response_destroy(&response);
104 }
105
106 static void test_post(int port) {
107   grpc_httpcli_request req;
108   char* host;
109   grpc_core::ExecCtx exec_ctx;
110
111   g_done = 0;
112   gpr_log(GPR_INFO, "test_post");
113
114   gpr_asprintf(&host, "localhost:%d", port);
115   gpr_log(GPR_INFO, "posting to %s", host);
116
117   memset(&req, 0, sizeof(req));
118   req.host = host;
119   req.ssl_host_override = const_cast<char*>("foo.test.google.fr");
120   req.http.path = const_cast<char*>("/post");
121   req.handshaker = &grpc_httpcli_ssl;
122
123   grpc_http_response response;
124   memset(&response, 0, sizeof(response));
125   grpc_resource_quota* resource_quota = grpc_resource_quota_create("test_post");
126   grpc_httpcli_post(
127       &g_context, &g_pops, resource_quota, &req, "hello", 5, n_seconds_time(15),
128       GRPC_CLOSURE_CREATE(on_finish, &response, grpc_schedule_on_exec_ctx),
129       &response);
130   grpc_resource_quota_unref_internal(resource_quota);
131   gpr_mu_lock(g_mu);
132   while (!g_done) {
133     grpc_pollset_worker* worker = nullptr;
134     GPR_ASSERT(GRPC_LOG_IF_ERROR(
135         "pollset_work", grpc_pollset_work(grpc_polling_entity_pollset(&g_pops),
136                                           &worker, n_seconds_time(1))));
137     gpr_mu_unlock(g_mu);
138     grpc_core::ExecCtx::Get()->Flush();
139     gpr_mu_lock(g_mu);
140   }
141   gpr_mu_unlock(g_mu);
142   gpr_free(host);
143   grpc_http_response_destroy(&response);
144 }
145
146 static void destroy_pops(void* p, grpc_error* error) {
147   grpc_pollset_destroy(
148       grpc_polling_entity_pollset(static_cast<grpc_polling_entity*>(p)));
149 }
150
151 int main(int argc, char** argv) {
152   grpc_closure destroyed;
153   gpr_subprocess* server;
154   char* me = argv[0];
155   char* lslash = strrchr(me, '/');
156   char* args[5];
157   int port = grpc_pick_unused_port_or_die();
158   int arg_shift = 0;
159   /* figure out where we are */
160   char* root;
161   if (lslash != nullptr) {
162     /* Hack for bazel target */
163     if (static_cast<unsigned>(lslash - me) >= (sizeof("http") - 1) &&
164         strncmp(me + (lslash - me) - sizeof("http") + 1, "http",
165                 sizeof("http") - 1) == 0) {
166       lslash = me + (lslash - me) - sizeof("http");
167     }
168     root = static_cast<char*>(
169         gpr_malloc(static_cast<size_t>(lslash - me + sizeof("/../.."))));
170     memcpy(root, me, static_cast<size_t>(lslash - me));
171     memcpy(root + (lslash - me), "/../..", sizeof("/../.."));
172   } else {
173     root = gpr_strdup(".");
174   }
175
176   GPR_ASSERT(argc <= 2);
177   if (argc == 2) {
178     args[0] = gpr_strdup(argv[1]);
179   } else {
180     arg_shift = 1;
181     gpr_asprintf(&args[0], "%s/test/core/http/python_wrapper.sh", root);
182     gpr_asprintf(&args[1], "%s/test/core/http/test_server.py", root);
183   }
184
185   /* Set the environment variable for the SSL certificate file */
186   char* pem_file;
187   gpr_asprintf(&pem_file, "%s/src/core/tsi/test_creds/ca.pem", root);
188   GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, pem_file);
189   gpr_free(pem_file);
190
191   /* start the server */
192   args[1 + arg_shift] = const_cast<char*>("--port");
193   gpr_asprintf(&args[2 + arg_shift], "%d", port);
194   args[3 + arg_shift] = const_cast<char*>("--ssl");
195   server = gpr_subprocess_create(4 + arg_shift, (const char**)args);
196   GPR_ASSERT(server);
197   gpr_free(args[0]);
198   if (arg_shift) gpr_free(args[1]);
199   gpr_free(args[2 + arg_shift]);
200   gpr_free(root);
201
202   gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
203                                gpr_time_from_seconds(5, GPR_TIMESPAN)));
204
205   grpc::testing::TestEnvironment env(argc, argv);
206   grpc_init();
207   grpc_httpcli_context_init(&g_context);
208   grpc_pollset* pollset =
209       static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
210   grpc_pollset_init(pollset, &g_mu);
211   g_pops = grpc_polling_entity_create_from_pollset(pollset);
212
213   test_get(port);
214   test_post(port);
215
216   {
217     grpc_core::ExecCtx exec_ctx;
218     grpc_httpcli_context_destroy(&g_context);
219     GRPC_CLOSURE_INIT(&destroyed, destroy_pops, &g_pops,
220                       grpc_schedule_on_exec_ctx);
221     grpc_pollset_shutdown(grpc_polling_entity_pollset(&g_pops), &destroyed);
222   }
223   grpc_shutdown();
224
225   gpr_free(grpc_polling_entity_pollset(&g_pops));
226
227   gpr_subprocess_destroy(server);
228
229   return 0;
230 }