blob: 3590207b6ac8e3918023a736e1459d42f60efed6 [file] [log] [blame]
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -07001/*
2 *
Craig Tiller19482442016-01-25 09:59:20 -08003 * Copyright 2015-2016, Google Inc.
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -07004 * 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
34#include "test/core/end2end/end2end_tests.h"
35
36#include <string.h>
37
38#include "src/core/channel/channel_args.h"
39#include "src/core/channel/client_channel.h"
40#include "src/core/channel/client_uchannel.h"
41#include "src/core/channel/connected_channel.h"
42#include "src/core/channel/http_client_filter.h"
43#include "src/core/channel/http_server_filter.h"
44#include "src/core/client_config/resolver_registry.h"
45#include "src/core/iomgr/tcp_client.h"
46#include "src/core/surface/channel.h"
47#include "src/core/surface/server.h"
48#include "src/core/transport/chttp2_transport.h"
49#include <grpc/support/alloc.h>
50#include <grpc/support/host_port.h>
51#include <grpc/support/log.h>
52#include <grpc/support/string_util.h>
53#include <grpc/support/sync.h>
54#include <grpc/support/thd.h>
55#include <grpc/support/useful.h>
56#include "test/core/util/port.h"
57#include "test/core/util/test_config.h"
58
59typedef struct {
60 grpc_connector base;
61 gpr_refcount refs;
62
63 grpc_closure *notify;
64 grpc_connect_in_args args;
65 grpc_connect_out_args *result;
66
67 grpc_endpoint *tcp;
68
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070069 grpc_closure connected;
70} connector;
71
72static void connector_ref(grpc_connector *con) {
73 connector *c = (connector *)con;
74 gpr_ref(&c->refs);
75}
76
77static void connector_unref(grpc_exec_ctx *exec_ctx, grpc_connector *con) {
78 connector *c = (connector *)con;
79 if (gpr_unref(&c->refs)) {
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070080 gpr_free(c);
81 }
82}
83
Craig Tiller6c396862016-01-28 13:53:40 -080084static void connected(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070085 connector *c = arg;
86 grpc_closure *notify;
87 grpc_endpoint *tcp = c->tcp;
88 if (tcp != NULL) {
Craig Tillerb2b42612015-11-20 12:02:17 -080089 c->result->transport =
90 grpc_create_chttp2_transport(exec_ctx, c->args.channel_args, tcp, 1);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070091 grpc_chttp2_transport_start_reading(exec_ctx, c->result->transport, NULL,
92 0);
93 GPR_ASSERT(c->result->transport);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -070094 } else {
95 memset(c->result, 0, sizeof(*c->result));
96 }
97 notify = c->notify;
98 c->notify = NULL;
99 notify->cb(exec_ctx, notify->cb_arg, 1);
100}
101
102static void connector_shutdown(grpc_exec_ctx *exec_ctx, grpc_connector *con) {}
103
104static void connector_connect(grpc_exec_ctx *exec_ctx, grpc_connector *con,
105 const grpc_connect_in_args *args,
106 grpc_connect_out_args *result,
107 grpc_closure *notify) {
108 connector *c = (connector *)con;
109 GPR_ASSERT(c->notify == NULL);
110 GPR_ASSERT(notify->cb);
111 c->notify = notify;
112 c->args = *args;
113 c->result = result;
114 c->tcp = NULL;
115 grpc_closure_init(&c->connected, connected, c);
116 grpc_tcp_client_connect(exec_ctx, &c->connected, &c->tcp,
117 args->interested_parties, args->addr, args->addr_len,
118 args->deadline);
119}
120
121static const grpc_connector_vtable connector_vtable = {
122 connector_ref, connector_unref, connector_shutdown, connector_connect};
123
124typedef struct {
125 grpc_subchannel_factory base;
126 gpr_refcount refs;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700127 grpc_channel_args *merge_args;
128 grpc_channel *master;
129 grpc_subchannel **sniffed_subchannel;
130} subchannel_factory;
131
132static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
133 subchannel_factory *f = (subchannel_factory *)scf;
134 gpr_ref(&f->refs);
135}
136
137static void subchannel_factory_unref(grpc_exec_ctx *exec_ctx,
138 grpc_subchannel_factory *scf) {
139 subchannel_factory *f = (subchannel_factory *)scf;
140 if (gpr_unref(&f->refs)) {
141 GRPC_CHANNEL_INTERNAL_UNREF(exec_ctx, f->master, "subchannel_factory");
142 grpc_channel_args_destroy(f->merge_args);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700143 gpr_free(f);
144 }
145}
146
147static grpc_subchannel *subchannel_factory_create_subchannel(
148 grpc_exec_ctx *exec_ctx, grpc_subchannel_factory *scf,
149 grpc_subchannel_args *args) {
150 subchannel_factory *f = (subchannel_factory *)scf;
151 connector *c = gpr_malloc(sizeof(*c));
152 grpc_channel_args *final_args =
153 grpc_channel_args_merge(args->args, f->merge_args);
154 grpc_subchannel *s;
155 memset(c, 0, sizeof(*c));
156 c->base.vtable = &connector_vtable;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700157 gpr_ref_init(&c->refs, 1);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700158 args->args = final_args;
Craig Tiller8cdba662016-01-22 20:01:55 -0800159 s = grpc_subchannel_create(exec_ctx, &c->base, args);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700160 grpc_connector_unref(exec_ctx, &c->base);
161 grpc_channel_args_destroy(final_args);
Craig Tillerf036a642015-12-01 17:00:40 -0800162 if (*f->sniffed_subchannel) {
163 GRPC_SUBCHANNEL_UNREF(exec_ctx, *f->sniffed_subchannel, "sniffed");
164 }
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700165 *f->sniffed_subchannel = s;
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800166 GRPC_SUBCHANNEL_REF(s, "sniffed");
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700167 return s;
168}
169
170static const grpc_subchannel_factory_vtable test_subchannel_factory_vtable = {
171 subchannel_factory_ref, subchannel_factory_unref,
172 subchannel_factory_create_subchannel};
173
174/* The evil twin of grpc_insecure_channel_create. It allows the test to use the
175 * custom-built sniffing subchannel_factory */
176grpc_channel *channel_create(const char *target, const grpc_channel_args *args,
177 grpc_subchannel **sniffed_subchannel) {
178 grpc_channel *channel = NULL;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700179 grpc_resolver *resolver;
180 subchannel_factory *f;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700181 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700182
Craig Tillerb2b42612015-11-20 12:02:17 -0800183 channel =
Craig Tiller178edfa2016-02-17 20:54:46 -0800184 grpc_channel_create(&exec_ctx, target, args, GRPC_CLIENT_CHANNEL, NULL);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700185
186 f = gpr_malloc(sizeof(*f));
187 f->sniffed_subchannel = sniffed_subchannel;
188 f->base.vtable = &test_subchannel_factory_vtable;
189 gpr_ref_init(&f->refs, 1);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700190 f->merge_args = grpc_channel_args_copy(args);
191 f->master = channel;
192 GRPC_CHANNEL_INTERNAL_REF(f->master, "test_subchannel_factory");
193 resolver = grpc_resolver_create(target, &f->base);
194 if (!resolver) {
195 return NULL;
196 }
197
198 grpc_client_channel_set_resolver(
199 &exec_ctx, grpc_channel_get_channel_stack(channel), resolver);
200 GRPC_RESOLVER_UNREF(&exec_ctx, resolver, "test_create");
201 grpc_subchannel_factory_unref(&exec_ctx, &f->base);
202
203 grpc_exec_ctx_finish(&exec_ctx);
204
205 return channel;
206}
207
208typedef struct micro_fullstack_fixture_data {
209 char *localaddr;
210 grpc_channel *master_channel;
211 grpc_subchannel *sniffed_subchannel;
212} micro_fullstack_fixture_data;
213
214static grpc_end2end_test_fixture chttp2_create_fixture_micro_fullstack(
215 grpc_channel_args *client_args, grpc_channel_args *server_args) {
216 grpc_end2end_test_fixture f;
217 int port = grpc_pick_unused_port_or_die();
218 micro_fullstack_fixture_data *ffd =
219 gpr_malloc(sizeof(micro_fullstack_fixture_data));
220 memset(&f, 0, sizeof(f));
Craig Tillerf036a642015-12-01 17:00:40 -0800221 memset(ffd, 0, sizeof(*ffd));
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700222
223 gpr_join_host_port(&ffd->localaddr, "127.0.0.1", port);
224
225 f.fixture_data = ffd;
226 f.cq = grpc_completion_queue_create(NULL);
227
228 return f;
229}
230
Craig Tiller4f7080c2015-11-20 17:09:08 -0800231grpc_connectivity_state g_state = GRPC_CHANNEL_IDLE;
Craig Tiller50ec2672015-11-27 21:45:11 -0800232grpc_pollset_set g_interested_parties;
Craig Tiller4f7080c2015-11-20 17:09:08 -0800233
Craig Tiller6c396862016-01-28 13:53:40 -0800234static void state_changed(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller4f7080c2015-11-20 17:09:08 -0800235 if (g_state != GRPC_CHANNEL_READY) {
Craig Tillerab33b482015-11-21 08:11:04 -0800236 grpc_subchannel_notify_on_state_change(
Craig Tiller1d881fb2015-12-01 07:39:04 -0800237 exec_ctx, arg, &g_interested_parties, &g_state,
238 grpc_closure_create(state_changed, arg));
Craig Tiller4f7080c2015-11-20 17:09:08 -0800239 }
240}
241
Craig Tiller6c396862016-01-28 13:53:40 -0800242static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *arg, bool success) {
Craig Tiller27e5aa42015-11-24 16:28:54 -0800243 grpc_pollset_destroy(arg);
244}
245
Craig Tiller4f7080c2015-11-20 17:09:08 -0800246static grpc_connected_subchannel *connect_subchannel(grpc_subchannel *c) {
247 grpc_pollset pollset;
248 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
249 grpc_pollset_init(&pollset);
Craig Tiller50ec2672015-11-27 21:45:11 -0800250 grpc_pollset_set_init(&g_interested_parties);
251 grpc_pollset_set_add_pollset(&exec_ctx, &g_interested_parties, &pollset);
Craig Tiller1d881fb2015-12-01 07:39:04 -0800252 grpc_subchannel_notify_on_state_change(&exec_ctx, c, &g_interested_parties,
253 &g_state,
Craig Tillerab33b482015-11-21 08:11:04 -0800254 grpc_closure_create(state_changed, c));
Craig Tiller4f7080c2015-11-20 17:09:08 -0800255 grpc_exec_ctx_flush(&exec_ctx);
256 gpr_mu_lock(GRPC_POLLSET_MU(&pollset));
257 while (g_state != GRPC_CHANNEL_READY) {
258 grpc_pollset_worker worker;
Craig Tiller11beb9a2015-11-24 10:29:32 -0800259 grpc_pollset_work(&exec_ctx, &pollset, &worker,
260 gpr_now(GPR_CLOCK_MONOTONIC),
Craig Tillerab33b482015-11-21 08:11:04 -0800261 GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
Craig Tiller4f7080c2015-11-20 17:09:08 -0800262 gpr_mu_unlock(GRPC_POLLSET_MU(&pollset));
263 grpc_exec_ctx_flush(&exec_ctx);
264 gpr_mu_lock(GRPC_POLLSET_MU(&pollset));
265 }
Craig Tiller27e5aa42015-11-24 16:28:54 -0800266 grpc_pollset_shutdown(&exec_ctx, &pollset,
267 grpc_closure_create(destroy_pollset, &pollset));
Craig Tiller50ec2672015-11-27 21:45:11 -0800268 grpc_pollset_set_destroy(&g_interested_parties);
Craig Tiller4f7080c2015-11-20 17:09:08 -0800269 gpr_mu_unlock(GRPC_POLLSET_MU(&pollset));
Craig Tiller4f7080c2015-11-20 17:09:08 -0800270 grpc_exec_ctx_finish(&exec_ctx);
271 return grpc_subchannel_get_connected_subchannel(c);
272}
273
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700274static void chttp2_init_client_micro_fullstack(grpc_end2end_test_fixture *f,
275 grpc_channel_args *client_args) {
276 micro_fullstack_fixture_data *ffd = f->fixture_data;
277 grpc_connectivity_state conn_state;
Craig Tiller4f7080c2015-11-20 17:09:08 -0800278 grpc_connected_subchannel *connected;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700279 char *ipv4_localaddr;
280
281 gpr_asprintf(&ipv4_localaddr, "ipv4:%s", ffd->localaddr);
282 ffd->master_channel =
283 channel_create(ipv4_localaddr, client_args, &ffd->sniffed_subchannel);
284 gpr_free(ipv4_localaddr);
285 gpr_log(GPR_INFO, "MASTER CHANNEL %p ", ffd->master_channel);
286 /* the following will block. That's ok for this test */
287 conn_state = grpc_channel_check_connectivity_state(ffd->master_channel,
288 1 /* try to connect */);
289 GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE);
290
291 /* here sniffed_subchannel should be ready to use */
292 GPR_ASSERT(conn_state == GRPC_CHANNEL_IDLE);
293 GPR_ASSERT(ffd->sniffed_subchannel != NULL);
Craig Tiller4f7080c2015-11-20 17:09:08 -0800294
295 connected = connect_subchannel(ffd->sniffed_subchannel);
Craig Tillerb2b42612015-11-20 12:02:17 -0800296 f->client = grpc_client_uchannel_create(ffd->sniffed_subchannel, client_args);
Craig Tiller4f7080c2015-11-20 17:09:08 -0800297 grpc_client_uchannel_set_connected_subchannel(f->client, connected);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700298 gpr_log(GPR_INFO, "CHANNEL WRAPPING SUBCHANNEL: %p(%p)", f->client,
299 ffd->sniffed_subchannel);
300
301 GPR_ASSERT(f->client);
302}
303
304static void chttp2_init_server_micro_fullstack(grpc_end2end_test_fixture *f,
305 grpc_channel_args *server_args) {
306 micro_fullstack_fixture_data *ffd = f->fixture_data;
307 if (f->server) {
308 grpc_server_destroy(f->server);
309 }
310 f->server = grpc_server_create(server_args, NULL);
311 grpc_server_register_completion_queue(f->server, f->cq, NULL);
312 GPR_ASSERT(grpc_server_add_insecure_http2_port(f->server, ffd->localaddr));
313 grpc_server_start(f->server);
314}
315
316static void chttp2_tear_down_micro_fullstack(grpc_end2end_test_fixture *f) {
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800317 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700318 micro_fullstack_fixture_data *ffd = f->fixture_data;
319 grpc_channel_destroy(ffd->master_channel);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800320 if (ffd->sniffed_subchannel) {
321 GRPC_SUBCHANNEL_UNREF(&exec_ctx, ffd->sniffed_subchannel, "sniffed");
322 }
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700323 gpr_free(ffd->localaddr);
324 gpr_free(ffd);
Craig Tiller2a1bb7f2015-11-29 21:54:26 -0800325 grpc_exec_ctx_finish(&exec_ctx);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700326}
327
328/* All test configurations */
329static grpc_end2end_test_config configs[] = {
Craig Tiller1d881fb2015-12-01 07:39:04 -0800330 {"chttp2/micro_fullstack", 0, chttp2_create_fixture_micro_fullstack,
331 chttp2_init_client_micro_fullstack, chttp2_init_server_micro_fullstack,
332 chttp2_tear_down_micro_fullstack},
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700333};
334
335int main(int argc, char **argv) {
336 size_t i;
337
338 grpc_test_init(argc, argv);
339 grpc_init();
340
341 for (i = 0; i < sizeof(configs) / sizeof(*configs); i++) {
Craig Tiller0fe5ee72015-12-22 12:50:36 -0800342 grpc_end2end_tests(argc, argv, configs[i]);
David Garcia Quintas7b1bd2c2015-10-05 18:22:10 -0700343 }
344
345 grpc_shutdown();
346
347 return 0;
348}