Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
murgatroid99 | c36f6ea | 2016-10-03 09:24:09 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
| 35 | |
| 36 | // This test won't work except with posix sockets enabled |
| 37 | #ifdef GRPC_POSIX_SOCKET |
| 38 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 39 | #include "src/core/lib/iomgr/tcp_server.h" |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 40 | |
| 41 | #include <errno.h> |
| 42 | #include <netinet/in.h> |
| 43 | #include <string.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <unistd.h> |
| 46 | |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 47 | #include <grpc/grpc.h> |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 48 | #include <grpc/support/alloc.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 49 | #include <grpc/support/log.h> |
| 50 | #include <grpc/support/sync.h> |
| 51 | #include <grpc/support/time.h> |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 52 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 53 | #include "src/core/lib/iomgr/iomgr.h" |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 54 | #include "src/core/lib/iomgr/resolve_address.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 55 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Dan Born | d890a21 | 2016-01-14 15:00:21 -0800 | [diff] [blame] | 56 | #include "test/core/util/port.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | #include "test/core/util/test_config.h" |
| 58 | |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 59 | #define LOG_TEST(x) gpr_log(GPR_INFO, "%s", #x) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 60 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 61 | static gpr_mu *g_mu; |
| 62 | static grpc_pollset *g_pollset; |
Craig Tiller | 451b4df | 2015-05-12 16:47:46 -0700 | [diff] [blame] | 63 | static int g_nconnects = 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 64 | |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 65 | typedef struct on_connect_result { |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 66 | /* Owns a ref to server. */ |
| 67 | grpc_tcp_server *server; |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 68 | unsigned port_index; |
| 69 | unsigned fd_index; |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 70 | int server_fd; |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 71 | } on_connect_result; |
| 72 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 73 | typedef struct server_weak_ref { |
| 74 | grpc_tcp_server *server; |
| 75 | |
| 76 | /* arg is this server_weak_ref. */ |
| 77 | grpc_closure server_shutdown; |
| 78 | } server_weak_ref; |
| 79 | |
| 80 | static on_connect_result g_result = {NULL, 0, 0, -1}; |
| 81 | |
| 82 | static void on_connect_result_init(on_connect_result *result) { |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 83 | result->server = NULL; |
| 84 | result->port_index = 0; |
| 85 | result->fd_index = 0; |
| 86 | result->server_fd = -1; |
| 87 | } |
| 88 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 89 | static void on_connect_result_set(on_connect_result *result, |
| 90 | const grpc_tcp_server_acceptor *acceptor) { |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 91 | result->server = grpc_tcp_server_ref(acceptor->from_server); |
| 92 | result->port_index = acceptor->port_index; |
| 93 | result->fd_index = acceptor->fd_index; |
| 94 | result->server_fd = grpc_tcp_server_port_fd( |
| 95 | result->server, acceptor->port_index, acceptor->fd_index); |
| 96 | } |
| 97 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 98 | static void server_weak_ref_shutdown(grpc_exec_ctx *exec_ctx, void *arg, |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 99 | grpc_error *error) { |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 100 | server_weak_ref *weak_ref = arg; |
| 101 | weak_ref->server = NULL; |
| 102 | } |
| 103 | |
| 104 | static void server_weak_ref_init(server_weak_ref *weak_ref) { |
| 105 | weak_ref->server = NULL; |
| 106 | grpc_closure_init(&weak_ref->server_shutdown, server_weak_ref_shutdown, |
| 107 | weak_ref); |
| 108 | } |
| 109 | |
| 110 | /* Make weak_ref->server_shutdown a shutdown_starting cb on server. |
| 111 | grpc_tcp_server promises that the server object will live until |
| 112 | weak_ref->server_shutdown has returned. A strong ref on grpc_tcp_server |
| 113 | should be held until server_weak_ref_set() returns to avoid a race where the |
| 114 | server is deleted before the shutdown_starting cb is added. */ |
| 115 | static void server_weak_ref_set(server_weak_ref *weak_ref, |
| 116 | grpc_tcp_server *server) { |
| 117 | grpc_tcp_server_shutdown_starting_add(server, &weak_ref->server_shutdown); |
| 118 | weak_ref->server = server; |
| 119 | } |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 120 | |
| 121 | static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp, |
Craig Tiller | 29dc490 | 2016-05-20 08:43:48 -0700 | [diff] [blame] | 122 | grpc_pollset *pollset, |
Dan Born | 5d81d15 | 2016-01-12 20:29:29 -0800 | [diff] [blame] | 123 | grpc_tcp_server_acceptor *acceptor) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 124 | grpc_endpoint_shutdown(exec_ctx, tcp); |
| 125 | grpc_endpoint_destroy(exec_ctx, tcp); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 126 | |
Craig Tiller | 2406e31 | 2016-10-19 13:26:48 -0700 | [diff] [blame] | 127 | on_connect_result temp_result; |
| 128 | on_connect_result_set(&temp_result, acceptor); |
| 129 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 130 | gpr_mu_lock(g_mu); |
Craig Tiller | 2406e31 | 2016-10-19 13:26:48 -0700 | [diff] [blame] | 131 | g_result = temp_result; |
Craig Tiller | 451b4df | 2015-05-12 16:47:46 -0700 | [diff] [blame] | 132 | g_nconnects++; |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 133 | GPR_ASSERT( |
| 134 | GRPC_LOG_IF_ERROR("pollset_kick", grpc_pollset_kick(g_pollset, NULL))); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 135 | gpr_mu_unlock(g_mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 138 | static void test_no_op(void) { |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 139 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 140 | grpc_tcp_server *s; |
Craig Tiller | 30ff60e | 2016-09-23 14:54:27 -0700 | [diff] [blame] | 141 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 142 | grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 143 | grpc_tcp_server_unref(&exec_ctx, s); |
Craig Tiller | b8b1a46 | 2015-09-22 12:50:03 -0700 | [diff] [blame] | 144 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 147 | static void test_no_op_with_start(void) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 148 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 149 | grpc_tcp_server *s; |
Craig Tiller | 30ff60e | 2016-09-23 14:54:27 -0700 | [diff] [blame] | 150 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 151 | grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | LOG_TEST("test_no_op_with_start"); |
| 153 | grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL); |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 154 | grpc_tcp_server_unref(&exec_ctx, s); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 155 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 158 | static void test_no_op_with_port(void) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 159 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 160 | grpc_resolved_address resolved_addr; |
| 161 | struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 162 | grpc_tcp_server *s; |
Craig Tiller | 30ff60e | 2016-09-23 14:54:27 -0700 | [diff] [blame] | 163 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 164 | grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | LOG_TEST("test_no_op_with_port"); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 167 | memset(&resolved_addr, 0, sizeof(resolved_addr)); |
| 168 | resolved_addr.len = sizeof(struct sockaddr_in); |
| 169 | addr->sin_family = AF_INET; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 170 | int port; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 171 | GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == |
| 172 | GRPC_ERROR_NONE && |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 173 | port > 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 174 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 175 | grpc_tcp_server_unref(&exec_ctx, s); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 179 | static void test_no_op_with_port_and_start(void) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 180 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 181 | grpc_resolved_address resolved_addr; |
| 182 | struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 183 | grpc_tcp_server *s; |
Craig Tiller | 30ff60e | 2016-09-23 14:54:27 -0700 | [diff] [blame] | 184 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 185 | grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | LOG_TEST("test_no_op_with_port_and_start"); |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 187 | int port; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 189 | memset(&resolved_addr, 0, sizeof(resolved_addr)); |
| 190 | resolved_addr.len = sizeof(struct sockaddr_in); |
| 191 | addr->sin_family = AF_INET; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 192 | GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == |
| 193 | GRPC_ERROR_NONE && |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 194 | port > 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 195 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 196 | grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 198 | grpc_tcp_server_unref(&exec_ctx, s); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | grpc_exec_ctx_finish(&exec_ctx); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 202 | static void tcp_connect(grpc_exec_ctx *exec_ctx, const struct sockaddr *remote, |
| 203 | socklen_t remote_len, on_connect_result *result) { |
| 204 | gpr_timespec deadline = GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10); |
| 205 | int clifd = socket(remote->sa_family, SOCK_STREAM, 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 206 | int nconnects_before; |
Craig Tiller | 451b4df | 2015-05-12 16:47:46 -0700 | [diff] [blame] | 207 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 208 | gpr_mu_lock(g_mu); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 209 | nconnects_before = g_nconnects; |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 210 | on_connect_result_init(&g_result); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 211 | GPR_ASSERT(clifd >= 0); |
| 212 | gpr_log(GPR_DEBUG, "start connect"); |
| 213 | GPR_ASSERT(connect(clifd, remote, remote_len) == 0); |
| 214 | gpr_log(GPR_DEBUG, "wait"); |
| 215 | while (g_nconnects == nconnects_before && |
| 216 | gpr_time_cmp(deadline, gpr_now(deadline.clock_type)) > 0) { |
Craig Tiller | f290e30 | 2016-02-18 08:04:36 -0800 | [diff] [blame] | 217 | grpc_pollset_worker *worker = NULL; |
Craig Tiller | 1aee536 | 2016-05-07 11:26:50 -0700 | [diff] [blame] | 218 | GPR_ASSERT(GRPC_LOG_IF_ERROR( |
| 219 | "pollset_work", |
| 220 | grpc_pollset_work(exec_ctx, g_pollset, &worker, |
| 221 | gpr_now(GPR_CLOCK_MONOTONIC), deadline))); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 222 | gpr_mu_unlock(g_mu); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 223 | grpc_exec_ctx_finish(exec_ctx); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 224 | gpr_mu_lock(g_mu); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 225 | } |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 226 | gpr_log(GPR_DEBUG, "wait done"); |
| 227 | GPR_ASSERT(g_nconnects == nconnects_before + 1); |
| 228 | close(clifd); |
| 229 | *result = g_result; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 230 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 231 | gpr_mu_unlock(g_mu); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 234 | /* Tests a tcp server with multiple ports. TODO(daniel-j-born): Multiple fds for |
| 235 | the same port should be tested. */ |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 236 | static void test_connect(unsigned n) { |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 237 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 238 | grpc_resolved_address resolved_addr; |
| 239 | grpc_resolved_address resolved_addr1; |
| 240 | struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 241 | struct sockaddr_storage *addr1 = |
| 242 | (struct sockaddr_storage *)resolved_addr1.addr; |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 243 | unsigned svr_fd_count; |
| 244 | int svr_port; |
| 245 | unsigned svr1_fd_count; |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 246 | int svr1_port; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 247 | grpc_tcp_server *s; |
Craig Tiller | 30ff60e | 2016-09-23 14:54:27 -0700 | [diff] [blame] | 248 | GPR_ASSERT(GRPC_ERROR_NONE == |
| 249 | grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 250 | unsigned i; |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 251 | server_weak_ref weak_ref; |
| 252 | server_weak_ref_init(&weak_ref); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 253 | LOG_TEST("test_connect"); |
| 254 | gpr_log(GPR_INFO, "clients=%d", n); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 255 | memset(&resolved_addr, 0, sizeof(resolved_addr)); |
| 256 | memset(&resolved_addr1, 0, sizeof(resolved_addr1)); |
| 257 | resolved_addr.len = sizeof(struct sockaddr_storage); |
| 258 | resolved_addr1.len = sizeof(struct sockaddr_storage); |
| 259 | addr->ss_family = addr1->ss_family = AF_INET; |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 260 | GPR_ASSERT(GRPC_ERROR_NONE == |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 261 | grpc_tcp_server_add_port(s, &resolved_addr, &svr_port)); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 262 | GPR_ASSERT(svr_port > 0); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 263 | /* Cannot use wildcard (port==0), because add_port() will try to reuse the |
| 264 | same port as a previous add_port(). */ |
| 265 | svr1_port = grpc_pick_unused_port_or_die(); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 266 | grpc_sockaddr_set_port(&resolved_addr1, svr1_port); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 267 | GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr1, &svr_port) == |
| 268 | GRPC_ERROR_NONE && |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 269 | svr_port == svr1_port); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 270 | |
| 271 | /* Bad port_index. */ |
| 272 | GPR_ASSERT(grpc_tcp_server_port_fd_count(s, 2) == 0); |
| 273 | GPR_ASSERT(grpc_tcp_server_port_fd(s, 2, 0) < 0); |
| 274 | |
| 275 | /* Bad fd_index. */ |
| 276 | GPR_ASSERT(grpc_tcp_server_port_fd(s, 0, 100) < 0); |
| 277 | GPR_ASSERT(grpc_tcp_server_port_fd(s, 1, 100) < 0); |
| 278 | |
| 279 | /* Got at least one fd per port. */ |
| 280 | svr_fd_count = grpc_tcp_server_port_fd_count(s, 0); |
| 281 | GPR_ASSERT(svr_fd_count >= 1); |
| 282 | svr1_fd_count = grpc_tcp_server_port_fd_count(s, 1); |
| 283 | GPR_ASSERT(svr1_fd_count >= 1); |
| 284 | |
| 285 | for (i = 0; i < svr_fd_count; ++i) { |
| 286 | int fd = grpc_tcp_server_port_fd(s, 0, i); |
| 287 | GPR_ASSERT(fd >= 0); |
| 288 | if (i == 0) { |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 289 | GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr, |
| 290 | (socklen_t *)&resolved_addr.len) == 0); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 291 | GPR_ASSERT(resolved_addr.len <= sizeof(*addr)); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | for (i = 0; i < svr1_fd_count; ++i) { |
| 295 | int fd = grpc_tcp_server_port_fd(s, 1, i); |
| 296 | GPR_ASSERT(fd >= 0); |
| 297 | if (i == 0) { |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 298 | GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr1, |
| 299 | (socklen_t *)&resolved_addr1.len) == 0); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 300 | GPR_ASSERT(resolved_addr1.len <= sizeof(*addr1)); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 304 | grpc_tcp_server_start(&exec_ctx, s, &g_pollset, 1, on_connect, NULL); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 305 | |
| 306 | for (i = 0; i < n; i++) { |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 307 | on_connect_result result; |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 308 | int svr_fd; |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 309 | on_connect_result_init(&result); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 310 | tcp_connect(&exec_ctx, (struct sockaddr *)addr, |
| 311 | (socklen_t)resolved_addr.len, &result); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 312 | GPR_ASSERT(result.server_fd >= 0); |
| 313 | svr_fd = result.server_fd; |
Dan Born | a78ca38 | 2016-01-14 13:02:09 -0800 | [diff] [blame] | 314 | GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == |
| 315 | result.server_fd); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 316 | GPR_ASSERT(result.port_index == 0); |
| 317 | GPR_ASSERT(result.fd_index < svr_fd_count); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 318 | GPR_ASSERT(result.server == s); |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 319 | if (weak_ref.server == NULL) { |
| 320 | server_weak_ref_set(&weak_ref, result.server); |
| 321 | } |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 322 | grpc_tcp_server_unref(&exec_ctx, result.server); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 323 | |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 324 | on_connect_result_init(&result); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 325 | tcp_connect(&exec_ctx, (struct sockaddr *)addr1, |
| 326 | (socklen_t)resolved_addr1.len, &result); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 327 | GPR_ASSERT(result.server_fd >= 0); |
| 328 | GPR_ASSERT(result.server_fd != svr_fd); |
Dan Born | a78ca38 | 2016-01-14 13:02:09 -0800 | [diff] [blame] | 329 | GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == |
| 330 | result.server_fd); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 331 | GPR_ASSERT(result.port_index == 1); |
| 332 | GPR_ASSERT(result.fd_index < svr_fd_count); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 333 | GPR_ASSERT(result.server == s); |
| 334 | grpc_tcp_server_unref(&exec_ctx, result.server); |
Dan Born | 601acad | 2016-01-13 19:52:58 -0800 | [diff] [blame] | 335 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 337 | /* Weak ref to server valid until final unref. */ |
| 338 | GPR_ASSERT(weak_ref.server != NULL); |
| 339 | GPR_ASSERT(grpc_tcp_server_port_fd(s, 0, 0) >= 0); |
| 340 | |
Dan Born | fa6b606 | 2016-01-08 21:01:59 -0800 | [diff] [blame] | 341 | grpc_tcp_server_unref(&exec_ctx, s); |
Dan Born | 930c298 | 2016-09-08 19:12:18 -0700 | [diff] [blame] | 342 | grpc_exec_ctx_finish(&exec_ctx); |
Dan Born | 9c12bc2 | 2016-01-13 16:52:20 -0800 | [diff] [blame] | 343 | |
| 344 | /* Weak ref lost. */ |
| 345 | GPR_ASSERT(weak_ref.server == NULL); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Craig Tiller | f707d62 | 2016-05-06 14:26:12 -0700 | [diff] [blame] | 348 | static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, |
| 349 | grpc_error *error) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 350 | grpc_pollset_destroy(p); |
Craig Tiller | 294d0ec | 2015-09-21 14:59:45 -0700 | [diff] [blame] | 351 | } |
Craig Tiller | 451b4df | 2015-05-12 16:47:46 -0700 | [diff] [blame] | 352 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 353 | int main(int argc, char **argv) { |
Craig Tiller | 294d0ec | 2015-09-21 14:59:45 -0700 | [diff] [blame] | 354 | grpc_closure destroyed; |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 355 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | grpc_test_init(argc, argv); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 357 | grpc_init(); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 358 | g_pollset = gpr_malloc(grpc_pollset_size()); |
| 359 | grpc_pollset_init(g_pollset, &g_mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 360 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 361 | test_no_op(); |
| 362 | test_no_op_with_start(); |
| 363 | test_no_op_with_port(); |
| 364 | test_no_op_with_port_and_start(); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 365 | test_connect(1); |
| 366 | test_connect(10); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 367 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 368 | grpc_closure_init(&destroyed, destroy_pollset, g_pollset); |
| 369 | grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 370 | grpc_exec_ctx_finish(&exec_ctx); |
Dan Born | b13a69d | 2016-01-14 16:53:18 -0800 | [diff] [blame] | 371 | grpc_shutdown(); |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 372 | gpr_free(g_pollset); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 373 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 374 | } |
murgatroid99 | c36f6ea | 2016-10-03 09:24:09 -0700 | [diff] [blame] | 375 | |
| 376 | #else /* GRPC_POSIX_SOCKET */ |
| 377 | |
murgatroid99 | aa9c578 | 2016-10-10 12:16:13 -0700 | [diff] [blame] | 378 | int main(int argc, char **argv) { return 1; } |
murgatroid99 | c36f6ea | 2016-10-03 09:24:09 -0700 | [diff] [blame] | 379 | |
| 380 | #endif /* GRPC_POSIX_SOCKET */ |