From 10c4b3c70d8816f7e5f395ad214bc815c8f4dfb3 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 11 Dec 2015 14:38:18 -0800 Subject: [PATCH] Add a test for binding a server to the same port twice; fix crashes --- src/core/iomgr/tcp_server_posix.c | 8 ++++++-- test/core/surface/server_test.c | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index b758702da8e..835675c390d 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -532,8 +532,12 @@ void grpc_tcp_server_start(grpc_exec_ctx *exec_ctx, grpc_tcp_server *s, } int grpc_tcp_listener_get_port(grpc_tcp_listener *listener) { - grpc_tcp_listener *sp = listener; - return sp->port; + if (listener != NULL) { + grpc_tcp_listener *sp = listener; + return sp->port; + } else { + return 0; + } } void grpc_tcp_listener_ref(grpc_tcp_listener *listener) { diff --git a/test/core/surface/server_test.c b/test/core/surface/server_test.c index f180bcdaa13..133d023952c 100644 --- a/test/core/surface/server_test.c +++ b/test/core/surface/server_test.c @@ -33,6 +33,8 @@ #include #include +#include +#include "test/core/util/port.h" #include "test/core/util/test_config.h" void test_register_method_fail(void) { @@ -58,11 +60,31 @@ void test_request_call_on_no_server_cq(void) { grpc_completion_queue_destroy(cc); } +void test_bind_server_twice(void) { + char *addr; + grpc_server *server1 = grpc_server_create(NULL, NULL); + grpc_server *server2 = grpc_server_create(NULL, NULL); + grpc_completion_queue *cq = grpc_completion_queue_create(NULL); + int port = grpc_pick_unused_port_or_die(); + gpr_asprintf(&addr, "[::]:%d", port); + grpc_server_register_completion_queue(server1, cq, NULL); + grpc_server_register_completion_queue(server2, cq, NULL); + GPR_ASSERT(port == grpc_server_add_insecure_http2_port(server1, addr)); + GPR_ASSERT(0 == grpc_server_add_insecure_http2_port(server2, addr)); + grpc_server_shutdown_and_notify(server1, cq, NULL); + grpc_server_shutdown_and_notify(server2, cq, NULL); + grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL); + grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_MONOTONIC), NULL); + grpc_server_destroy(server1); + grpc_server_destroy(server2); +} + int main(int argc, char **argv) { grpc_test_init(argc, argv); grpc_init(); test_register_method_fail(); test_request_call_on_no_server_cq(); + test_bind_server_twice(); grpc_shutdown(); return 0; }