diff options
author | Craig Tiller <ctiller@google.com> | 2016-10-28 07:31:20 -0700 |
---|---|---|
committer | Craig Tiller <ctiller@google.com> | 2016-10-28 07:31:20 -0700 |
commit | fa014a86e3598abf02134189a88c1b8f80c64191 (patch) | |
tree | e0395bf34392dbfa232e6a9b39ae49571b62262d /test/core/iomgr/tcp_server_posix_test.c | |
parent | 6d4340dda88f6f8771dde54e21620dc32dc4e65a (diff) | |
parent | 6628e36a89b3ec709477b7ff494ec5e237ac8231 (diff) |
Merge github.com:grpc/grpc into pid_ctl
Diffstat (limited to 'test/core/iomgr/tcp_server_posix_test.c')
-rw-r--r-- | test/core/iomgr/tcp_server_posix_test.c | 92 |
1 files changed, 60 insertions, 32 deletions
diff --git a/test/core/iomgr/tcp_server_posix_test.c b/test/core/iomgr/tcp_server_posix_test.c index 5791f562e1..1b8a39c1be 100644 --- a/test/core/iomgr/tcp_server_posix_test.c +++ b/test/core/iomgr/tcp_server_posix_test.c @@ -31,6 +31,11 @@ * */ +#include "src/core/lib/iomgr/port.h" + +// This test won't work except with posix sockets enabled +#ifdef GRPC_POSIX_SOCKET + #include "src/core/lib/iomgr/tcp_server.h" #include <errno.h> @@ -46,6 +51,7 @@ #include <grpc/support/time.h> #include "src/core/lib/iomgr/iomgr.h" +#include "src/core/lib/iomgr/resolve_address.h" #include "src/core/lib/iomgr/sockaddr_utils.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" @@ -132,7 +138,8 @@ static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp, static void test_no_op(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_tcp_server *s; - GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); + GPR_ASSERT(GRPC_ERROR_NONE == + grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); grpc_tcp_server_unref(&exec_ctx, s); grpc_exec_ctx_finish(&exec_ctx); } @@ -140,7 +147,8 @@ static void test_no_op(void) { static void test_no_op_with_start(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; grpc_tcp_server *s; - GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); + GPR_ASSERT(GRPC_ERROR_NONE == + grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); LOG_TEST("test_no_op_with_start"); grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL); grpc_tcp_server_unref(&exec_ctx, s); @@ -149,16 +157,19 @@ static void test_no_op_with_start(void) { static void test_no_op_with_port(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_tcp_server *s; - GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); + GPR_ASSERT(GRPC_ERROR_NONE == + grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); LOG_TEST("test_no_op_with_port"); - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; int port; - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - &port) == GRPC_ERROR_NONE && + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == + GRPC_ERROR_NONE && port > 0); grpc_tcp_server_unref(&exec_ctx, s); @@ -167,16 +178,19 @@ static void test_no_op_with_port(void) { static void test_no_op_with_port_and_start(void) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_in addr; + grpc_resolved_address resolved_addr; + struct sockaddr_in *addr = (struct sockaddr_in *)resolved_addr.addr; grpc_tcp_server *s; - GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); + GPR_ASSERT(GRPC_ERROR_NONE == + grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); LOG_TEST("test_no_op_with_port_and_start"); int port; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, sizeof(addr), - &port) == GRPC_ERROR_NONE && + memset(&resolved_addr, 0, sizeof(resolved_addr)); + resolved_addr.len = sizeof(struct sockaddr_in); + addr->sin_family = AF_INET; + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr, &port) == + GRPC_ERROR_NONE && port > 0); grpc_tcp_server_start(&exec_ctx, s, NULL, 0, on_connect, NULL); @@ -221,33 +235,37 @@ static void tcp_connect(grpc_exec_ctx *exec_ctx, const struct sockaddr *remote, the same port should be tested. */ static void test_connect(unsigned n) { grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; - struct sockaddr_storage addr; - struct sockaddr_storage addr1; - socklen_t addr_len = sizeof(addr); + grpc_resolved_address resolved_addr; + grpc_resolved_address resolved_addr1; + struct sockaddr_storage *addr = (struct sockaddr_storage *)resolved_addr.addr; + struct sockaddr_storage *addr1 = + (struct sockaddr_storage *)resolved_addr1.addr; unsigned svr_fd_count; int svr_port; unsigned svr1_fd_count; int svr1_port; grpc_tcp_server *s; - GPR_ASSERT(GRPC_ERROR_NONE == grpc_tcp_server_create(NULL, NULL, &s)); + GPR_ASSERT(GRPC_ERROR_NONE == + grpc_tcp_server_create(&exec_ctx, NULL, NULL, &s)); unsigned i; server_weak_ref weak_ref; server_weak_ref_init(&weak_ref); LOG_TEST("test_connect"); gpr_log(GPR_INFO, "clients=%d", n); - memset(&addr, 0, sizeof(addr)); - memset(&addr1, 0, sizeof(addr1)); - addr.ss_family = addr1.ss_family = AF_INET; + memset(&resolved_addr, 0, sizeof(resolved_addr)); + memset(&resolved_addr1, 0, sizeof(resolved_addr1)); + resolved_addr.len = sizeof(struct sockaddr_storage); + resolved_addr1.len = sizeof(struct sockaddr_storage); + addr->ss_family = addr1->ss_family = AF_INET; GPR_ASSERT(GRPC_ERROR_NONE == - grpc_tcp_server_add_port(s, (struct sockaddr *)&addr, addr_len, - &svr_port)); + grpc_tcp_server_add_port(s, &resolved_addr, &svr_port)); GPR_ASSERT(svr_port > 0); /* Cannot use wildcard (port==0), because add_port() will try to reuse the same port as a previous add_port(). */ svr1_port = grpc_pick_unused_port_or_die(); - grpc_sockaddr_set_port((struct sockaddr *)&addr1, svr1_port); - GPR_ASSERT(grpc_tcp_server_add_port(s, (struct sockaddr *)&addr1, addr_len, - &svr_port) == GRPC_ERROR_NONE && + grpc_sockaddr_set_port(&resolved_addr1, svr1_port); + GPR_ASSERT(grpc_tcp_server_add_port(s, &resolved_addr1, &svr_port) == + GRPC_ERROR_NONE && svr_port == svr1_port); /* Bad port_index. */ @@ -268,16 +286,18 @@ static void test_connect(unsigned n) { int fd = grpc_tcp_server_port_fd(s, 0, i); GPR_ASSERT(fd >= 0); if (i == 0) { - GPR_ASSERT(getsockname(fd, (struct sockaddr *)&addr, &addr_len) == 0); - GPR_ASSERT(addr_len <= sizeof(addr)); + GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr, + (socklen_t *)&resolved_addr.len) == 0); + GPR_ASSERT(resolved_addr.len <= sizeof(*addr)); } } for (i = 0; i < svr1_fd_count; ++i) { int fd = grpc_tcp_server_port_fd(s, 1, i); GPR_ASSERT(fd >= 0); if (i == 0) { - GPR_ASSERT(getsockname(fd, (struct sockaddr *)&addr1, &addr_len) == 0); - GPR_ASSERT(addr_len <= sizeof(addr1)); + GPR_ASSERT(getsockname(fd, (struct sockaddr *)addr1, + (socklen_t *)&resolved_addr1.len) == 0); + GPR_ASSERT(resolved_addr1.len <= sizeof(*addr1)); } } @@ -287,7 +307,8 @@ static void test_connect(unsigned n) { on_connect_result result; int svr_fd; on_connect_result_init(&result); - tcp_connect(&exec_ctx, (struct sockaddr *)&addr, addr_len, &result); + tcp_connect(&exec_ctx, (struct sockaddr *)addr, + (socklen_t)resolved_addr.len, &result); GPR_ASSERT(result.server_fd >= 0); svr_fd = result.server_fd; GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == @@ -301,7 +322,8 @@ static void test_connect(unsigned n) { grpc_tcp_server_unref(&exec_ctx, result.server); on_connect_result_init(&result); - tcp_connect(&exec_ctx, (struct sockaddr *)&addr1, addr_len, &result); + tcp_connect(&exec_ctx, (struct sockaddr *)addr1, + (socklen_t)resolved_addr1.len, &result); GPR_ASSERT(result.server_fd >= 0); GPR_ASSERT(result.server_fd != svr_fd); GPR_ASSERT(grpc_tcp_server_port_fd(s, result.port_index, result.fd_index) == @@ -350,3 +372,9 @@ int main(int argc, char **argv) { gpr_free(g_pollset); return 0; } + +#else /* GRPC_POSIX_SOCKET */ + +int main(int argc, char **argv) { return 1; } + +#endif /* GRPC_POSIX_SOCKET */ |