node/deps/uv/test/test-tcp-bind-error.c

199 lines
4.8 KiB
C
Raw Normal View History

2011-05-13 10:16:40 +08:00
/* 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"
2011-05-13 10:16:40 +08:00
#include "task.h"
#include <stdio.h>
#include <stdlib.h>
static int close_cb_called = 0;
2011-06-09 21:14:58 +08:00
static void close_cb(uv_handle_t* handle) {
2011-05-13 10:16:40 +08:00
ASSERT(handle != NULL);
close_cb_called++;
}
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_error_addrinuse) {
2011-05-13 10:16:40 +08:00
struct sockaddr_in addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
uv_tcp_t server1, server2;
2011-05-13 10:16:40 +08:00
int r;
r = uv_tcp_init(uv_default_loop(), &server1);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server1, addr);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
r = uv_tcp_init(uv_default_loop(), &server2);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server2, addr);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-27 09:54:00 +08:00
r = uv_listen((uv_stream_t*)&server1, 128, NULL);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-27 09:54:00 +08:00
r = uv_listen((uv_stream_t*)&server2, 128, NULL);
2011-05-13 10:16:40 +08:00
ASSERT(r == -1);
ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRINUSE);
2011-05-13 10:16:40 +08:00
2011-06-09 21:14:58 +08:00
uv_close((uv_handle_t*)&server1, close_cb);
uv_close((uv_handle_t*)&server2, close_cb);
2011-05-13 10:16:40 +08:00
uv_run(uv_default_loop());
2011-05-13 10:16:40 +08:00
ASSERT(close_cb_called == 2);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-05-13 10:16:40 +08:00
return 0;
}
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_error_addrnotavail_1) {
2011-05-13 10:16:40 +08:00
struct sockaddr_in addr = uv_ip4_addr("127.255.255.255", TEST_PORT);
uv_tcp_t server;
2011-05-13 10:16:40 +08:00
int r;
r = uv_tcp_init(uv_default_loop(), &server);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, addr);
2011-05-13 10:16:40 +08:00
/* It seems that Linux is broken here - bind succeeds. */
if (r == -1) {
ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRNOTAVAIL);
2011-05-13 10:16:40 +08:00
}
2011-06-09 21:14:58 +08:00
uv_close((uv_handle_t*)&server, close_cb);
2011-05-13 10:16:40 +08:00
uv_run(uv_default_loop());
2011-05-13 10:16:40 +08:00
ASSERT(close_cb_called == 1);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-05-13 10:16:40 +08:00
return 0;
}
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_error_addrnotavail_2) {
2011-05-13 10:16:40 +08:00
struct sockaddr_in addr = uv_ip4_addr("4.4.4.4", TEST_PORT);
uv_tcp_t server;
2011-05-13 10:16:40 +08:00
int r;
r = uv_tcp_init(uv_default_loop(), &server);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, addr);
2011-05-13 10:16:40 +08:00
ASSERT(r == -1);
ASSERT(uv_last_error(uv_default_loop()).code == UV_EADDRNOTAVAIL);
2011-05-13 10:16:40 +08:00
2011-06-09 21:14:58 +08:00
uv_close((uv_handle_t*)&server, close_cb);
2011-05-13 10:16:40 +08:00
uv_run(uv_default_loop());
2011-05-13 10:16:40 +08:00
ASSERT(close_cb_called == 1);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-05-13 10:16:40 +08:00
return 0;
}
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_error_fault) {
2011-05-13 10:16:40 +08:00
char garbage[] = "blah blah blah blah blah blah blah blah blah blah blah blah";
struct sockaddr_in* garbage_addr;
uv_tcp_t server;
2011-05-13 10:16:40 +08:00
int r;
garbage_addr = (struct sockaddr_in*) &garbage;
r = uv_tcp_init(uv_default_loop(), &server);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, *garbage_addr);
2011-05-13 10:16:40 +08:00
ASSERT(r == -1);
ASSERT(uv_last_error(uv_default_loop()).code == UV_EFAULT);
2011-05-13 10:16:40 +08:00
2011-06-09 21:14:58 +08:00
uv_close((uv_handle_t*)&server, close_cb);
2011-05-13 10:16:40 +08:00
uv_run(uv_default_loop());
2011-05-13 10:16:40 +08:00
ASSERT(close_cb_called == 1);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-05-13 10:16:40 +08:00
return 0;
}
/* Notes: On Linux uv_bind(server, NULL) will segfault the program. */
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_error_inval) {
2011-05-13 10:16:40 +08:00
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;
2011-05-13 10:16:40 +08:00
int r;
r = uv_tcp_init(uv_default_loop(), &server);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, addr1);
2011-05-13 10:16:40 +08:00
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, addr2);
2011-05-13 10:16:40 +08:00
ASSERT(r == -1);
ASSERT(uv_last_error(uv_default_loop()).code == UV_EINVAL);
2011-05-13 10:16:40 +08:00
2011-06-09 21:14:58 +08:00
uv_close((uv_handle_t*)&server, close_cb);
2011-05-13 10:16:40 +08:00
uv_run(uv_default_loop());
2011-05-13 10:16:40 +08:00
ASSERT(close_cb_called == 1);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-05-13 10:16:40 +08:00
return 0;
}
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_bind_localhost_ok) {
struct sockaddr_in addr = uv_ip4_addr("127.0.0.1", TEST_PORT);
uv_tcp_t server;
int r;
r = uv_tcp_init(uv_default_loop(), &server);
ASSERT(r == 0);
2011-07-02 00:30:44 +08:00
r = uv_tcp_bind(&server, addr);
ASSERT(r == 0);
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
return 0;
}
2011-07-19 17:47:15 +08:00
2011-07-21 06:19:31 +08:00
TEST_IMPL(tcp_listen_without_bind) {
2011-07-19 17:47:15 +08:00
int r;
uv_tcp_t server;
r = uv_tcp_init(uv_default_loop(), &server);
2011-07-19 17:47:15 +08:00
ASSERT(r == 0);
2011-07-27 09:54:00 +08:00
r = uv_listen((uv_stream_t*)&server, 128, NULL);
2011-07-19 17:47:15 +08:00
ASSERT(r == 0);
2011-07-21 00:06:16 +08:00
2012-10-18 00:33:40 +08:00
MAKE_VALGRIND_HAPPY();
2011-07-21 00:06:16 +08:00
return 0;
2011-07-19 17:47:15 +08:00
}