From 4ed6496b9ce416ac4bf1257b1eae06e0d4eb479c Mon Sep 17 00:00:00 2001 From: Yorkie Date: Fri, 13 Dec 2013 01:01:38 +0800 Subject: [PATCH] unix, windows: detect errors in uv_ip4/6_addr --- Makefile.am | 1 + src/uv-common.c | 9 ++------- test/test-ip4-addr.c | 46 ++++++++++++++++++++++++++++++++++++++++++++ test/test-list.h | 2 ++ uv.gyp | 1 + 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 test/test-ip4-addr.c diff --git a/Makefile.am b/Makefile.am index 4602f82a..4e41a6ef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -139,6 +139,7 @@ test_run_tests_SOURCES = test/blackhole-server.c \ test/test-getsockname.c \ test/test-hrtime.c \ test/test-idle.c \ + test/test-ip4-addr.c \ test/test-ip6-addr.c \ test/test-ipc-send-recv.c \ test/test-ipc.c \ diff --git a/src/uv-common.c b/src/uv-common.c index 197789b5..c4cf3c7f 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -102,9 +102,7 @@ int uv_ip4_addr(const char* ip, int port, struct sockaddr_in* addr) { memset(addr, 0, sizeof(*addr)); addr->sin_family = AF_INET; addr->sin_port = htons(port); - /* TODO(bnoordhuis) Don't use inet_addr(), no good way to detect errors. */ - addr->sin_addr.s_addr = inet_addr(ip); - return 0; + return uv_inet_pton(AF_INET, ip, &(addr->sin_addr.s_addr)); } @@ -140,10 +138,7 @@ int uv_ip6_addr(const char* ip, int port, struct sockaddr_in6* addr) { } #endif - /* TODO(bnoordhuis) Return an error when the address is bad. */ - uv_inet_pton(AF_INET6, ip, &addr->sin6_addr); - - return 0; + return uv_inet_pton(AF_INET6, ip, &addr->sin6_addr); } diff --git a/test/test-ip4-addr.c b/test/test-ip4-addr.c new file mode 100644 index 00000000..fc61f508 --- /dev/null +++ b/test/test-ip4-addr.c @@ -0,0 +1,46 @@ +/* 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 + + +TEST_IMPL(ip4_addr) { + + struct sockaddr_in addr; + + ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); + ASSERT(0 == uv_ip4_addr("255.255.255.255", TEST_PORT, &addr)); + ASSERT(UV_EINVAL == uv_ip4_addr("255.255.255*000", TEST_PORT, &addr)); + ASSERT(UV_EINVAL == uv_ip4_addr("255.255.255.256", TEST_PORT, &addr)); + ASSERT(UV_EINVAL == uv_ip4_addr("2555.0.0.0", TEST_PORT, &addr)); + ASSERT(UV_EINVAL == uv_ip4_addr("255", TEST_PORT, &addr)); + + // for broken address family + ASSERT(UV_EAFNOSUPPORT == uv_inet_pton(42, "127.0.0.1", + &addr.sin_addr.s_addr)); + + MAKE_VALGRIND_HAPPY(); + return 0; +} diff --git a/test/test-list.h b/test/test-list.h index 614ddb33..4be44bbc 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -217,6 +217,7 @@ TEST_DECLARE (dlerror) TEST_DECLARE (poll_duplex) TEST_DECLARE (poll_unidirectional) TEST_DECLARE (poll_close) +TEST_DECLARE (ip4_addr) TEST_DECLARE (ip6_addr_link_local) #ifdef _WIN32 TEST_DECLARE (spawn_detect_pipe_name_collisions_on_windows) @@ -517,6 +518,7 @@ TASK_LIST_START TEST_ENTRY (thread_rwlock) TEST_ENTRY (thread_create) TEST_ENTRY (dlerror) + TEST_ENTRY (ip4_addr) TEST_ENTRY (ip6_addr_link_local) #if 0 /* These are for testing the test runner. */ diff --git a/uv.gyp b/uv.gyp index b9e27305..712e59f1 100644 --- a/uv.gyp +++ b/uv.gyp @@ -382,6 +382,7 @@ 'test/test-udp-multicast-join.c', 'test/test-dlerror.c', 'test/test-udp-multicast-ttl.c', + 'test/test-ip4-addr.c', 'test/test-ip6-addr.c', ], 'conditions': [