From afc998759f332db4aaadd2775e17e39b7315df60 Mon Sep 17 00:00:00 2001 From: Igor Zinkovsky Date: Thu, 21 Jul 2011 02:00:12 +0200 Subject: [PATCH] Add uv_pipe_bind tests --- msvs/libuv-test.vcxproj | 5 +- test/task.h | 2 + test/test-list.h | 57 ++++--- test/test-pipe-bind-error.c | 149 ++++++++++++++++++ ...est-bind-error.c => test-tcp-bind-error.c} | 14 +- ...t-bind6-error.c => test-tcp-bind6-error.c} | 10 +- 6 files changed, 199 insertions(+), 38 deletions(-) create mode 100644 test/test-pipe-bind-error.c rename test/{test-bind-error.c => test-tcp-bind-error.c} (94%) rename test/{test-bind6-error.c => test-tcp-bind6-error.c} (95%) diff --git a/msvs/libuv-test.vcxproj b/msvs/libuv-test.vcxproj index 3d648b6e..8408735b 100644 --- a/msvs/libuv-test.vcxproj +++ b/msvs/libuv-test.vcxproj @@ -144,7 +144,6 @@ - @@ -159,8 +158,10 @@ - + + + diff --git a/test/task.h b/test/task.h index 42ca8f22..76c69033 100644 --- a/test/task.h +++ b/test/task.h @@ -32,8 +32,10 @@ #ifdef _WIN32 # define TEST_PIPENAME "\\\\.\\pipe\\uv-test" +# define TEST_PIPENAME_2 "\\\\.\\pipe\\uv-test2" #else # define TEST_PIPENAME "/tmp/uv-test-sock" +# define TEST_PIPENAME_2 "/tmp/uv-test-sock2" #endif typedef enum { diff --git a/test/test-list.h b/test/test-list.h index 2a7e5676..d1edcd30 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -24,18 +24,22 @@ TEST_DECLARE (tcp_ping_pong_v6) TEST_DECLARE (pipe_ping_pong) TEST_DECLARE (delayed_accept) TEST_DECLARE (tcp_writealot) -TEST_DECLARE (bind_error_addrinuse) -TEST_DECLARE (bind_error_addrnotavail_1) -TEST_DECLARE (bind_error_addrnotavail_2) -TEST_DECLARE (bind_error_fault) -TEST_DECLARE (bind_error_inval) -TEST_DECLARE (bind_localhost_ok) -TEST_DECLARE (listen_without_bind) -TEST_DECLARE (bind6_error_addrinuse) -TEST_DECLARE (bind6_error_addrnotavail) -TEST_DECLARE (bind6_error_fault) -TEST_DECLARE (bind6_error_inval) -TEST_DECLARE (bind6_localhost_ok) +TEST_DECLARE (tcp_bind_error_addrinuse) +TEST_DECLARE (tcp_bind_error_addrnotavail_1) +TEST_DECLARE (tcp_bind_error_addrnotavail_2) +TEST_DECLARE (tcp_bind_error_fault) +TEST_DECLARE (tcp_bind_error_inval) +TEST_DECLARE (tcp_bind_localhost_ok) +TEST_DECLARE (tcp_listen_without_bind) +TEST_DECLARE (tcp_bind6_error_addrinuse) +TEST_DECLARE (tcp_bind6_error_addrnotavail) +TEST_DECLARE (tcp_bind6_error_fault) +TEST_DECLARE (tcp_bind6_error_inval) +TEST_DECLARE (tcp_bind6_localhost_ok) +TEST_DECLARE (pipe_bind_error_addrinuse) +TEST_DECLARE (pipe_bind_error_addrnotavail) +TEST_DECLARE (pipe_bind_error_inval) +TEST_DECLARE (pipe_listen_without_bind) TEST_DECLARE (connection_fail) TEST_DECLARE (connection_fail_doesnt_auto_close) TEST_DECLARE (shutdown_eof) @@ -77,19 +81,24 @@ TASK_LIST_START TEST_ENTRY (tcp_writealot) TEST_HELPER (tcp_writealot, tcp4_echo_server) - TEST_ENTRY (bind_error_addrinuse) - TEST_ENTRY (bind_error_addrnotavail_1) - TEST_ENTRY (bind_error_addrnotavail_2) - TEST_ENTRY (bind_error_fault) - TEST_ENTRY (bind_error_inval) - TEST_ENTRY (bind_localhost_ok) - TEST_ENTRY (listen_without_bind) + TEST_ENTRY (tcp_bind_error_addrinuse) + TEST_ENTRY (tcp_bind_error_addrnotavail_1) + TEST_ENTRY (tcp_bind_error_addrnotavail_2) + TEST_ENTRY (tcp_bind_error_fault) + TEST_ENTRY (tcp_bind_error_inval) + TEST_ENTRY (tcp_bind_localhost_ok) + TEST_ENTRY (tcp_listen_without_bind) - TEST_ENTRY (bind6_error_addrinuse) - TEST_ENTRY (bind6_error_addrnotavail) - TEST_ENTRY (bind6_error_fault) - TEST_ENTRY (bind6_error_inval) - TEST_ENTRY (bind6_localhost_ok) + TEST_ENTRY (tcp_bind6_error_addrinuse) + TEST_ENTRY (tcp_bind6_error_addrnotavail) + TEST_ENTRY (tcp_bind6_error_fault) + TEST_ENTRY (tcp_bind6_error_inval) + TEST_ENTRY (tcp_bind6_localhost_ok) + + TEST_ENTRY (pipe_bind_error_addrinuse) + TEST_ENTRY (pipe_bind_error_addrnotavail) + TEST_ENTRY (pipe_bind_error_inval) + TEST_ENTRY (pipe_listen_without_bind) TEST_ENTRY (connection_fail) TEST_ENTRY (connection_fail_doesnt_auto_close) diff --git a/test/test-pipe-bind-error.c b/test/test-pipe-bind-error.c new file mode 100644 index 00000000..f38e39e7 --- /dev/null +++ b/test/test-pipe-bind-error.c @@ -0,0 +1,149 @@ +/* Copyright Joyent, Inc. and other Node contributors. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include "uv.h" +#include "task.h" +#include +#include + + +#ifdef _WIN32 +# define BAD_PIPENAME "bad-pipe" +#else +/* TODO: define a bad pipe name for unix (see pipe_bind_error_addrnotavail) */ +# define BAD_PIPENAME "" +#endif + + +static int close_cb_called = 0; + + +static void close_cb(uv_handle_t* handle) { + ASSERT(handle != NULL); + close_cb_called++; +} + + +TEST_IMPL(pipe_bind_error_addrinuse) { + uv_pipe_t server1, server2; + int r; + + uv_init(); + + r = uv_pipe_init(&server1); + ASSERT(r == 0); + r = uv_pipe_bind(&server1, TEST_PIPENAME); + ASSERT(r == 0); + + r = uv_pipe_init(&server2); + ASSERT(r == 0); + r = uv_pipe_bind(&server2, TEST_PIPENAME); + ASSERT(r == -1); + + ASSERT(uv_last_error().code == UV_EADDRINUSE); + + r = uv_pipe_listen(&server1, NULL); + ASSERT(r == 0); + r = uv_pipe_listen(&server2, NULL); + ASSERT(r == -1); + + ASSERT(uv_last_error().code == UV_EADDRINUSE); + + uv_close((uv_handle_t*)&server1, close_cb); + uv_close((uv_handle_t*)&server2, close_cb); + + uv_run(); + + ASSERT(close_cb_called == 2); + + return 0; +} + + +TEST_IMPL(pipe_bind_error_addrnotavail) { + uv_pipe_t server; + int r; + + uv_init(); + + r = uv_pipe_init(&server); + ASSERT(r == 0); + r = uv_pipe_bind(&server, BAD_PIPENAME); + + ASSERT(r == -1); + ASSERT(uv_last_error().code == UV_EADDRNOTAVAIL); + + uv_close((uv_handle_t*)&server, close_cb); + + uv_run(); + + ASSERT(close_cb_called == 1); + + return 0; +} + + +TEST_IMPL(pipe_bind_error_inval) { + uv_pipe_t server; + int r; + + uv_init(); + + r = uv_pipe_init(&server); + ASSERT(r == 0); + r = uv_pipe_bind(&server, TEST_PIPENAME); + ASSERT(r == 0); + r = uv_pipe_bind(&server, TEST_PIPENAME_2); + ASSERT(r == -1); + + ASSERT(uv_last_error().code == UV_EINVAL); + + uv_close((uv_handle_t*)&server, close_cb); + + uv_run(); + + ASSERT(close_cb_called == 1); + + return 0; +} + + +TEST_IMPL(pipe_listen_without_bind) { + uv_pipe_t server; + int r; + + uv_init(); + + r = uv_pipe_init(&server); + ASSERT(r == 0); + r = uv_pipe_listen(&server, NULL); + ASSERT(r == -1); + + ASSERT(uv_last_error().code == UV_ENOTCONN); + + uv_close((uv_handle_t*)&server, close_cb); + + uv_run(); + + ASSERT(close_cb_called == 1); + + return 0; +} diff --git a/test/test-bind-error.c b/test/test-tcp-bind-error.c similarity index 94% rename from test/test-bind-error.c rename to test/test-tcp-bind-error.c index 834f4ffa..9034438d 100644 --- a/test/test-bind-error.c +++ b/test/test-tcp-bind-error.c @@ -34,7 +34,7 @@ static void close_cb(uv_handle_t* handle) { } -TEST_IMPL(bind_error_addrinuse) { +TEST_IMPL(tcp_bind_error_addrinuse) { struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT); uv_tcp_t server1, server2; int r; @@ -69,7 +69,7 @@ TEST_IMPL(bind_error_addrinuse) { } -TEST_IMPL(bind_error_addrnotavail_1) { +TEST_IMPL(tcp_bind_error_addrnotavail_1) { struct sockaddr_in addr = uv_ip4_addr("127.255.255.255", TEST_PORT); uv_tcp_t server; int r; @@ -95,7 +95,7 @@ TEST_IMPL(bind_error_addrnotavail_1) { } -TEST_IMPL(bind_error_addrnotavail_2) { +TEST_IMPL(tcp_bind_error_addrnotavail_2) { struct sockaddr_in addr = uv_ip4_addr("4.4.4.4", TEST_PORT); uv_tcp_t server; int r; @@ -118,7 +118,7 @@ TEST_IMPL(bind_error_addrnotavail_2) { } -TEST_IMPL(bind_error_fault) { +TEST_IMPL(tcp_bind_error_fault) { char garbage[] = "blah blah blah blah blah blah blah blah blah blah blah blah"; struct sockaddr_in* garbage_addr; uv_tcp_t server; @@ -146,7 +146,7 @@ TEST_IMPL(bind_error_fault) { /* Notes: On Linux uv_bind(server, NULL) will segfault the program. */ -TEST_IMPL(bind_error_inval) { +TEST_IMPL(tcp_bind_error_inval) { struct sockaddr_in addr1 = uv_ip4_addr("0.0.0.0", TEST_PORT); struct sockaddr_in addr2 = uv_ip4_addr("0.0.0.0", TEST_PORT_2); uv_tcp_t server; @@ -173,7 +173,7 @@ TEST_IMPL(bind_error_inval) { } -TEST_IMPL(bind_localhost_ok) { +TEST_IMPL(tcp_bind_localhost_ok) { struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT); uv_tcp_t server; @@ -190,7 +190,7 @@ TEST_IMPL(bind_localhost_ok) { } -TEST_IMPL(listen_without_bind) { +TEST_IMPL(tcp_listen_without_bind) { int r; uv_tcp_t server; diff --git a/test/test-bind6-error.c b/test/test-tcp-bind6-error.c similarity index 95% rename from test/test-bind6-error.c rename to test/test-tcp-bind6-error.c index 6072dcdc..d2127858 100644 --- a/test/test-bind6-error.c +++ b/test/test-tcp-bind6-error.c @@ -34,7 +34,7 @@ static void close_cb(uv_handle_t* handle) { } -TEST_IMPL(bind6_error_addrinuse) { +TEST_IMPL(tcp_bind6_error_addrinuse) { struct sockaddr_in6 addr = uv_ip6_addr("::", TEST_PORT); uv_tcp_t server1, server2; int r; @@ -69,7 +69,7 @@ TEST_IMPL(bind6_error_addrinuse) { } -TEST_IMPL(bind6_error_addrnotavail) { +TEST_IMPL(tcp_bind6_error_addrnotavail) { struct sockaddr_in6 addr = uv_ip6_addr("4:4:4:4:4:4:4:4", TEST_PORT); uv_tcp_t server; int r; @@ -92,7 +92,7 @@ TEST_IMPL(bind6_error_addrnotavail) { } -TEST_IMPL(bind6_error_fault) { +TEST_IMPL(tcp_bind6_error_fault) { char garbage[] = "blah blah blah blah blah blah blah blah blah blah blah blah"; struct sockaddr_in6* garbage_addr; uv_tcp_t server; @@ -120,7 +120,7 @@ TEST_IMPL(bind6_error_fault) { /* Notes: On Linux uv_bind6(server, NULL) will segfault the program. */ -TEST_IMPL(bind6_error_inval) { +TEST_IMPL(tcp_bind6_error_inval) { struct sockaddr_in6 addr1 = uv_ip6_addr("::", TEST_PORT); struct sockaddr_in6 addr2 = uv_ip6_addr("::", TEST_PORT_2); uv_tcp_t server; @@ -147,7 +147,7 @@ TEST_IMPL(bind6_error_inval) { } -TEST_IMPL(bind6_localhost_ok) { +TEST_IMPL(tcp_bind6_localhost_ok) { struct sockaddr_in6 addr = uv_ip6_addr("::1", TEST_PORT); uv_tcp_t server;