blob: 8abb81c803a03032179720783f63f90bdf270d3d [file] [log] [blame]
nnoble0c475f02014-12-05 15:37:39 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
nnoble0c475f02014-12-05 15:37:39 -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 Tiller68bad432015-04-29 14:33:26 -070034#include <string.h>
David Garcia Quintas2bfd2752015-08-20 11:42:29 -070035
nnoble0c475f02014-12-05 15:37:39 -080036#include <grpc/grpc.h>
37#include <grpc/support/alloc.h>
38#include <grpc/support/host_port.h>
39#include <grpc/support/log.h>
Craig Tiller698d00c2015-07-20 12:32:58 -070040#include <grpc/support/string_util.h>
David Garcia Quintas2bfd2752015-08-20 11:42:29 -070041
Craig Tiller9533d042016-03-25 17:11:06 -070042#include "src/core/lib/iomgr/resolve_address.h"
43#include "src/core/lib/iomgr/socket_utils_posix.h"
44#include "src/core/lib/support/string.h"
David Garcia Quintas2bfd2752015-08-20 11:42:29 -070045
nnoble0c475f02014-12-05 15:37:39 -080046#include "test/core/end2end/cq_verifier.h"
47#include "test/core/util/port.h"
48#include "test/core/util/test_config.h"
49
50/* This test exercises IPv4, IPv6, and dualstack sockets in various ways. */
51
Craig Tiller7536af02015-12-22 13:49:30 -080052static void *tag(intptr_t i) { return (void *)i; }
Craig Tillera82950e2015-09-22 12:33:20 -070053
54static gpr_timespec ms_from_now(int ms) {
55 return GRPC_TIMEOUT_MILLIS_TO_DEADLINE(ms);
nnoble0c475f02014-12-05 15:37:39 -080056}
57
Craig Tillera82950e2015-09-22 12:33:20 -070058static void drain_cq(grpc_completion_queue *cq) {
Craig Tiller64be9f72015-05-04 14:53:51 -070059 grpc_event ev;
Craig Tillera82950e2015-09-22 12:33:20 -070060 do {
61 ev = grpc_completion_queue_next(cq, ms_from_now(5000), NULL);
62 } while (ev.type != GRPC_QUEUE_SHUTDOWN);
nnoble0c475f02014-12-05 15:37:39 -080063}
64
Craig Tillera82950e2015-09-22 12:33:20 -070065static void do_nothing(void *ignored) {}
Craig Tiller45724b32015-09-22 10:42:19 -070066
Craig Tillera82950e2015-09-22 12:33:20 -070067void test_connect(const char *server_host, const char *client_host, int port,
68 int expect_ok) {
nnoble0c475f02014-12-05 15:37:39 -080069 char *client_hostport;
70 char *server_hostport;
71 grpc_channel *client;
72 grpc_server *server;
Craig Tillerbed32602015-05-11 15:26:02 -070073 grpc_completion_queue *cq;
nnoble0c475f02014-12-05 15:37:39 -080074 grpc_call *c;
75 grpc_call *s;
Craig Tillerbed32602015-05-11 15:26:02 -070076 cq_verifier *cqv;
nnoble0c475f02014-12-05 15:37:39 -080077 gpr_timespec deadline;
ctiller570d1f42015-01-12 16:29:52 -080078 int got_port;
Craig Tiller68bad432015-04-29 14:33:26 -070079 grpc_op ops[6];
80 grpc_op *op;
81 grpc_metadata_array initial_metadata_recv;
82 grpc_metadata_array trailing_metadata_recv;
83 grpc_metadata_array request_metadata_recv;
84 grpc_status_code status;
Nicolas "Pixel" Noble9a123df2015-07-29 23:45:08 +020085 grpc_call_error error;
Craig Tiller68bad432015-04-29 14:33:26 -070086 char *details = NULL;
87 size_t details_capacity = 0;
88 int was_cancelled = 2;
89 grpc_call_details call_details;
Craig Tiller1b22b9d2015-07-20 13:42:22 -070090 char *peer;
David Klempner6fb122d2016-05-13 15:24:17 -070091 int picked_port = 0;
nnoble0c475f02014-12-05 15:37:39 -080092
Craig Tillera82950e2015-09-22 12:33:20 -070093 if (port == 0) {
94 port = grpc_pick_unused_port_or_die();
David Klempner6fb122d2016-05-13 15:24:17 -070095 picked_port = 1;
Craig Tillera82950e2015-09-22 12:33:20 -070096 }
David Klempner57c00612015-02-25 13:37:12 -080097
Craig Tillera82950e2015-09-22 12:33:20 -070098 gpr_join_host_port(&server_hostport, server_host, port);
nnoble0c475f02014-12-05 15:37:39 -080099
Craig Tillera82950e2015-09-22 12:33:20 -0700100 grpc_metadata_array_init(&initial_metadata_recv);
101 grpc_metadata_array_init(&trailing_metadata_recv);
102 grpc_metadata_array_init(&request_metadata_recv);
103 grpc_call_details_init(&call_details);
Craig Tiller68bad432015-04-29 14:33:26 -0700104
nnoble0c475f02014-12-05 15:37:39 -0800105 /* Create server. */
Craig Tillera82950e2015-09-22 12:33:20 -0700106 cq = grpc_completion_queue_create(NULL);
107 server = grpc_server_create(NULL, NULL);
108 grpc_server_register_completion_queue(server, cq, NULL);
109 GPR_ASSERT((got_port = grpc_server_add_insecure_http2_port(
110 server, server_hostport)) > 0);
111 if (port == 0) {
112 port = got_port;
113 } else {
114 GPR_ASSERT(port == got_port);
115 }
116 grpc_server_start(server);
117 cqv = cq_verifier_create(cq);
nnoble0c475f02014-12-05 15:37:39 -0800118
119 /* Create client. */
Craig Tillera82950e2015-09-22 12:33:20 -0700120 if (client_host[0] == 'i') {
121 /* for ipv4:/ipv6: addresses, concatenate the port to each of the parts */
122 size_t i;
123 gpr_slice uri_slice;
124 gpr_slice_buffer uri_parts;
125 char **hosts_with_port;
David Garcia Quintas2bfd2752015-08-20 11:42:29 -0700126
Craig Tillera82950e2015-09-22 12:33:20 -0700127 uri_slice =
128 gpr_slice_new((char *)client_host, strlen(client_host), do_nothing);
129 gpr_slice_buffer_init(&uri_parts);
130 gpr_slice_split(uri_slice, ",", &uri_parts);
131 hosts_with_port = gpr_malloc(sizeof(char *) * uri_parts.count);
132 for (i = 0; i < uri_parts.count; i++) {
133 char *uri_part_str = gpr_dump_slice(uri_parts.slices[i], GPR_DUMP_ASCII);
134 gpr_asprintf(&hosts_with_port[i], "%s:%d", uri_part_str, port);
135 gpr_free(uri_part_str);
David Garcia Quintas2bfd2752015-08-20 11:42:29 -0700136 }
Craig Tillera82950e2015-09-22 12:33:20 -0700137 client_hostport = gpr_strjoin_sep((const char **)hosts_with_port,
138 uri_parts.count, ",", NULL);
139 for (i = 0; i < uri_parts.count; i++) {
140 gpr_free(hosts_with_port[i]);
David Garcia Quintas2bfd2752015-08-20 11:42:29 -0700141 }
Craig Tillera82950e2015-09-22 12:33:20 -0700142 gpr_free(hosts_with_port);
143 gpr_slice_buffer_destroy(&uri_parts);
144 gpr_slice_unref(uri_slice);
145 } else {
146 gpr_join_host_port(&client_hostport, client_host, port);
147 }
148 client = grpc_insecure_channel_create(client_hostport, NULL, NULL);
nnoble0c475f02014-12-05 15:37:39 -0800149
Craig Tillera82950e2015-09-22 12:33:20 -0700150 gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)",
151 server_hostport, client_hostport, expect_ok ? "success" : "failure");
ctiller570d1f42015-01-12 16:29:52 -0800152
Craig Tillera82950e2015-09-22 12:33:20 -0700153 gpr_free(client_hostport);
154 gpr_free(server_hostport);
ctiller570d1f42015-01-12 16:29:52 -0800155
Craig Tillera82950e2015-09-22 12:33:20 -0700156 if (expect_ok) {
157 /* Normal deadline, shouldn't be reached. */
158 deadline = ms_from_now(60000);
159 } else {
160 /* Give up faster when failure is expected.
161 BUG: Setting this to 1000 reveals a memory leak (b/18608927). */
162 deadline = ms_from_now(1500);
163 }
nnoble0c475f02014-12-05 15:37:39 -0800164
165 /* Send a trivial request. */
Craig Tillera82950e2015-09-22 12:33:20 -0700166 c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, cq,
167 "/foo", "foo.test.google.fr", deadline, NULL);
168 GPR_ASSERT(c);
nnoble0c475f02014-12-05 15:37:39 -0800169
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700170 memset(ops, 0, sizeof(ops));
Craig Tiller68bad432015-04-29 14:33:26 -0700171 op = ops;
172 op->op = GRPC_OP_SEND_INITIAL_METADATA;
173 op->data.send_initial_metadata.count = 0;
Craig Tiller5b1e6592016-03-11 15:05:30 -0800174 op->flags = expect_ok ? GRPC_INITIAL_METADATA_IGNORE_CONNECTIVITY : 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200175 op->reserved = NULL;
Craig Tiller68bad432015-04-29 14:33:26 -0700176 op++;
177 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700178 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200179 op->reserved = NULL;
Craig Tiller68bad432015-04-29 14:33:26 -0700180 op++;
181 op->op = GRPC_OP_RECV_INITIAL_METADATA;
182 op->data.recv_initial_metadata = &initial_metadata_recv;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700183 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200184 op->reserved = NULL;
Craig Tiller68bad432015-04-29 14:33:26 -0700185 op++;
186 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
187 op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
188 op->data.recv_status_on_client.status = &status;
189 op->data.recv_status_on_client.status_details = &details;
190 op->data.recv_status_on_client.status_details_capacity = &details_capacity;
David Garcia Quintas1d5aca52015-06-14 14:42:04 -0700191 op->flags = 0;
Nicolas "Pixel" Noble49605162015-08-01 00:12:01 +0200192 op->reserved = NULL;
Craig Tiller68bad432015-04-29 14:33:26 -0700193 op++;
Craig Tillera82950e2015-09-22 12:33:20 -0700194 error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL);
195 GPR_ASSERT(GRPC_CALL_OK == error);
Craig Tiller68bad432015-04-29 14:33:26 -0700196
Craig Tillera82950e2015-09-22 12:33:20 -0700197 if (expect_ok) {
198 /* Check for a successful request. */
199 error = grpc_server_request_call(server, &s, &call_details,
200 &request_metadata_recv, cq, cq, tag(101));
201 GPR_ASSERT(GRPC_CALL_OK == error);
Mark D. Roth7187ab92016-08-24 13:49:22 -0700202 CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700203 cq_verify(cqv);
nnoble0c475f02014-12-05 15:37:39 -0800204
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700205 memset(ops, 0, sizeof(ops));
Craig Tillera82950e2015-09-22 12:33:20 -0700206 op = ops;
207 op->op = GRPC_OP_SEND_INITIAL_METADATA;
208 op->data.send_initial_metadata.count = 0;
209 op->flags = 0;
210 op++;
211 op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
212 op->data.send_status_from_server.trailing_metadata_count = 0;
213 op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
214 op->data.send_status_from_server.status_details = "xyz";
215 op->flags = 0;
216 op++;
217 op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
218 op->data.recv_close_on_server.cancelled = &was_cancelled;
219 op->flags = 0;
220 op++;
221 error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), NULL);
222 GPR_ASSERT(GRPC_CALL_OK == error);
nnoble0c475f02014-12-05 15:37:39 -0800223
Mark D. Roth7187ab92016-08-24 13:49:22 -0700224 CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
225 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700226 cq_verify(cqv);
Craig Tiller68bad432015-04-29 14:33:26 -0700227
Craig Tillera82950e2015-09-22 12:33:20 -0700228 peer = grpc_call_get_peer(c);
229 gpr_log(GPR_DEBUG, "got peer: '%s'", peer);
230 gpr_free(peer);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700231
Craig Tillera82950e2015-09-22 12:33:20 -0700232 GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
233 GPR_ASSERT(0 == strcmp(details, "xyz"));
234 GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
235 GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
236 GPR_ASSERT(was_cancelled == 1);
Craig Tiller68bad432015-04-29 14:33:26 -0700237
Craig Tillera82950e2015-09-22 12:33:20 -0700238 grpc_call_destroy(s);
239 } else {
240 /* Check for a failed connection. */
Mark D. Roth7187ab92016-08-24 13:49:22 -0700241 CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700242 cq_verify(cqv);
nnoble0c475f02014-12-05 15:37:39 -0800243
Craig Tiller8c0d96f2016-03-11 14:27:52 -0800244 GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE);
Craig Tillera82950e2015-09-22 12:33:20 -0700245 }
nnoble0c475f02014-12-05 15:37:39 -0800246
Craig Tillera82950e2015-09-22 12:33:20 -0700247 grpc_call_destroy(c);
Craig Tiller68bad432015-04-29 14:33:26 -0700248
Craig Tillera82950e2015-09-22 12:33:20 -0700249 cq_verifier_destroy(cqv);
nnoble0c475f02014-12-05 15:37:39 -0800250
251 /* Destroy client. */
Craig Tillera82950e2015-09-22 12:33:20 -0700252 grpc_channel_destroy(client);
nnoble0c475f02014-12-05 15:37:39 -0800253
254 /* Destroy server. */
Craig Tillera82950e2015-09-22 12:33:20 -0700255 grpc_server_shutdown_and_notify(server, cq, tag(1000));
Craig Tillerf40df232016-03-25 13:38:14 -0700256 GPR_ASSERT(grpc_completion_queue_pluck(
257 cq, tag(1000), GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5), NULL)
258 .type == GRPC_OP_COMPLETE);
Craig Tillera82950e2015-09-22 12:33:20 -0700259 grpc_server_destroy(server);
260 grpc_completion_queue_shutdown(cq);
261 drain_cq(cq);
262 grpc_completion_queue_destroy(cq);
Craig Tiller99d5c102015-05-20 16:19:09 -0700263
Craig Tillera82950e2015-09-22 12:33:20 -0700264 grpc_metadata_array_destroy(&initial_metadata_recv);
265 grpc_metadata_array_destroy(&trailing_metadata_recv);
266 grpc_metadata_array_destroy(&request_metadata_recv);
Craig Tiller0dc5e6c2015-07-10 10:07:53 -0700267
Craig Tillera82950e2015-09-22 12:33:20 -0700268 grpc_call_details_destroy(&call_details);
269 gpr_free(details);
David Klempner6fb122d2016-05-13 15:24:17 -0700270 if (picked_port) {
271 grpc_recycle_unused_port(port);
272 }
nnoble0c475f02014-12-05 15:37:39 -0800273}
274
Craig Tillera82950e2015-09-22 12:33:20 -0700275int external_dns_works(const char *host) {
yang-gc7940ba2016-07-06 16:48:19 -0700276 grpc_resolved_addresses *res = NULL;
Craig Tillerf707d622016-05-06 14:26:12 -0700277 grpc_error *error = grpc_blocking_resolve_address(host, "80", &res);
278 GRPC_ERROR_UNREF(error);
Craig Tillera82950e2015-09-22 12:33:20 -0700279 if (res != NULL) {
280 grpc_resolved_addresses_destroy(res);
281 return 1;
282 }
Paul Marks6a70b012015-09-08 18:11:15 -0700283 return 0;
284}
285
Craig Tillera82950e2015-09-22 12:33:20 -0700286int main(int argc, char **argv) {
pmarks465554e2014-12-10 18:09:25 -0800287 int do_ipv6 = 1;
nnoble0c475f02014-12-05 15:37:39 -0800288
Craig Tillera82950e2015-09-22 12:33:20 -0700289 grpc_test_init(argc, argv);
290 grpc_init();
nnoble0c475f02014-12-05 15:37:39 -0800291
Craig Tillera82950e2015-09-22 12:33:20 -0700292 if (!grpc_ipv6_loopback_available()) {
293 gpr_log(GPR_INFO, "Can't bind to ::1. Skipping IPv6 tests.");
294 do_ipv6 = 0;
295 }
pmarks465554e2014-12-10 18:09:25 -0800296
Craig Tilleree76f4f2015-04-08 16:10:29 -0700297 /* For coverage, test with and without dualstack sockets. */
Craig Tillera82950e2015-09-22 12:33:20 -0700298 for (grpc_forbid_dualstack_sockets_for_testing = 0;
299 grpc_forbid_dualstack_sockets_for_testing <= 1;
300 grpc_forbid_dualstack_sockets_for_testing++) {
301 /* :: and 0.0.0.0 are handled identically. */
302 test_connect("::", "127.0.0.1", 0, 1);
303 test_connect("::", "::ffff:127.0.0.1", 0, 1);
304 test_connect("::", "ipv4:127.0.0.1", 0, 1);
305 test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1);
306 test_connect("::", "localhost", 0, 1);
307 test_connect("0.0.0.0", "127.0.0.1", 0, 1);
308 test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1);
309 test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1);
310 test_connect("0.0.0.0", "ipv4:127.0.0.1,127.0.0.2,127.0.0.3", 0, 1);
311 test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1],[::ffff:127.0.0.2]", 0, 1);
312 test_connect("0.0.0.0", "localhost", 0, 1);
313 if (do_ipv6) {
314 test_connect("::", "::1", 0, 1);
315 test_connect("0.0.0.0", "::1", 0, 1);
316 test_connect("::", "ipv6:[::1]", 0, 1);
317 test_connect("0.0.0.0", "ipv6:[::1]", 0, 1);
David Klempner57c00612015-02-25 13:37:12 -0800318 }
nnoble0c475f02014-12-05 15:37:39 -0800319
Craig Tillera82950e2015-09-22 12:33:20 -0700320 /* These only work when the families agree. */
321 test_connect("127.0.0.1", "127.0.0.1", 0, 1);
322 test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1);
323 if (do_ipv6) {
324 test_connect("::1", "::1", 0, 1);
325 test_connect("::1", "127.0.0.1", 0, 0);
326 test_connect("127.0.0.1", "::1", 0, 0);
327 test_connect("::1", "ipv6:[::1]", 0, 1);
328 test_connect("::1", "ipv4:127.0.0.1", 0, 0);
329 test_connect("127.0.0.1", "ipv6:[::1]", 0, 0);
330 }
331
332 if (!external_dns_works("loopback46.unittest.grpc.io")) {
333 gpr_log(GPR_INFO, "Skipping tests that depend on *.unittest.grpc.io.");
334 } else {
335 test_connect("loopback46.unittest.grpc.io", "loopback4.unittest.grpc.io",
336 0, 1);
337 test_connect("loopback4.unittest.grpc.io", "loopback46.unittest.grpc.io",
338 0, 1);
339 if (do_ipv6) {
340 test_connect("loopback46.unittest.grpc.io",
341 "loopback6.unittest.grpc.io", 0, 1);
342 test_connect("loopback6.unittest.grpc.io",
343 "loopback46.unittest.grpc.io", 0, 1);
344 test_connect("loopback4.unittest.grpc.io", "loopback6.unittest.grpc.io",
345 0, 0);
346 test_connect("loopback6.unittest.grpc.io", "loopback4.unittest.grpc.io",
347 0, 0);
348 }
349 }
350 }
351
352 grpc_shutdown();
nnoble0c475f02014-12-05 15:37:39 -0800353
354 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800355}