10e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
20e552da7Schristos *
30e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
40e552da7Schristos * of this software and associated documentation files (the "Software"), to
50e552da7Schristos * deal in the Software without restriction, including without limitation the
60e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
70e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
80e552da7Schristos * furnished to do so, subject to the following conditions:
90e552da7Schristos *
100e552da7Schristos * The above copyright notice and this permission notice shall be included in
110e552da7Schristos * all copies or substantial portions of the Software.
120e552da7Schristos *
130e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
140e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
160e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
170e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
180e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
190e552da7Schristos * IN THE SOFTWARE.
200e552da7Schristos */
210e552da7Schristos
220e552da7Schristos #include "uv.h"
230e552da7Schristos #include "task.h"
240e552da7Schristos #include <stdio.h>
250e552da7Schristos #include <stdlib.h>
260e552da7Schristos
270e552da7Schristos
280e552da7Schristos static int close_cb_called = 0;
290e552da7Schristos
300e552da7Schristos
close_cb(uv_handle_t * handle)310e552da7Schristos static void close_cb(uv_handle_t* handle) {
32*5f2f4271Schristos ASSERT_NOT_NULL(handle);
330e552da7Schristos close_cb_called++;
340e552da7Schristos }
350e552da7Schristos
360e552da7Schristos
TEST_IMPL(tcp_bind6_error_addrinuse)370e552da7Schristos TEST_IMPL(tcp_bind6_error_addrinuse) {
380e552da7Schristos struct sockaddr_in6 addr;
390e552da7Schristos uv_tcp_t server1, server2;
400e552da7Schristos int r;
410e552da7Schristos
420e552da7Schristos if (!can_ipv6())
430e552da7Schristos RETURN_SKIP("IPv6 not supported");
440e552da7Schristos
450e552da7Schristos ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr));
460e552da7Schristos
470e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server1);
480e552da7Schristos ASSERT(r == 0);
490e552da7Schristos r = uv_tcp_bind(&server1, (const struct sockaddr*) &addr, 0);
500e552da7Schristos ASSERT(r == 0);
510e552da7Schristos
520e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server2);
530e552da7Schristos ASSERT(r == 0);
540e552da7Schristos r = uv_tcp_bind(&server2, (const struct sockaddr*) &addr, 0);
550e552da7Schristos ASSERT(r == 0);
560e552da7Schristos
570e552da7Schristos r = uv_listen((uv_stream_t*)&server1, 128, NULL);
580e552da7Schristos ASSERT(r == 0);
590e552da7Schristos r = uv_listen((uv_stream_t*)&server2, 128, NULL);
600e552da7Schristos ASSERT(r == UV_EADDRINUSE);
610e552da7Schristos
620e552da7Schristos uv_close((uv_handle_t*)&server1, close_cb);
630e552da7Schristos uv_close((uv_handle_t*)&server2, close_cb);
640e552da7Schristos
650e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
660e552da7Schristos
670e552da7Schristos ASSERT(close_cb_called == 2);
680e552da7Schristos
690e552da7Schristos MAKE_VALGRIND_HAPPY();
700e552da7Schristos return 0;
710e552da7Schristos }
720e552da7Schristos
730e552da7Schristos
TEST_IMPL(tcp_bind6_error_addrnotavail)740e552da7Schristos TEST_IMPL(tcp_bind6_error_addrnotavail) {
750e552da7Schristos struct sockaddr_in6 addr;
760e552da7Schristos uv_tcp_t server;
770e552da7Schristos int r;
780e552da7Schristos
790e552da7Schristos if (!can_ipv6())
800e552da7Schristos RETURN_SKIP("IPv6 not supported");
810e552da7Schristos
820e552da7Schristos ASSERT(0 == uv_ip6_addr("4:4:4:4:4:4:4:4", TEST_PORT, &addr));
830e552da7Schristos
840e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server);
850e552da7Schristos ASSERT(r == 0);
860e552da7Schristos r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
870e552da7Schristos ASSERT(r == UV_EADDRNOTAVAIL);
880e552da7Schristos
890e552da7Schristos uv_close((uv_handle_t*)&server, close_cb);
900e552da7Schristos
910e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
920e552da7Schristos
930e552da7Schristos ASSERT(close_cb_called == 1);
940e552da7Schristos
950e552da7Schristos MAKE_VALGRIND_HAPPY();
960e552da7Schristos return 0;
970e552da7Schristos }
980e552da7Schristos
990e552da7Schristos
TEST_IMPL(tcp_bind6_error_fault)1000e552da7Schristos TEST_IMPL(tcp_bind6_error_fault) {
1010e552da7Schristos char garbage[] =
1020e552da7Schristos "blah blah blah blah blah blah blah blah blah blah blah blah";
1030e552da7Schristos struct sockaddr_in6* garbage_addr;
1040e552da7Schristos uv_tcp_t server;
1050e552da7Schristos int r;
1060e552da7Schristos
1070e552da7Schristos if (!can_ipv6())
1080e552da7Schristos RETURN_SKIP("IPv6 not supported");
1090e552da7Schristos
1100e552da7Schristos garbage_addr = (struct sockaddr_in6*) &garbage;
1110e552da7Schristos
1120e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server);
1130e552da7Schristos ASSERT(r == 0);
1140e552da7Schristos r = uv_tcp_bind(&server, (const struct sockaddr*) garbage_addr, 0);
1150e552da7Schristos ASSERT(r == UV_EINVAL);
1160e552da7Schristos
1170e552da7Schristos uv_close((uv_handle_t*)&server, close_cb);
1180e552da7Schristos
1190e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1200e552da7Schristos
1210e552da7Schristos ASSERT(close_cb_called == 1);
1220e552da7Schristos
1230e552da7Schristos MAKE_VALGRIND_HAPPY();
1240e552da7Schristos return 0;
1250e552da7Schristos }
1260e552da7Schristos
1270e552da7Schristos /* Notes: On Linux uv_bind6(server, NULL) will segfault the program. */
1280e552da7Schristos
TEST_IMPL(tcp_bind6_error_inval)1290e552da7Schristos TEST_IMPL(tcp_bind6_error_inval) {
1300e552da7Schristos struct sockaddr_in6 addr1;
1310e552da7Schristos struct sockaddr_in6 addr2;
1320e552da7Schristos uv_tcp_t server;
1330e552da7Schristos int r;
1340e552da7Schristos
1350e552da7Schristos if (!can_ipv6())
1360e552da7Schristos RETURN_SKIP("IPv6 not supported");
1370e552da7Schristos
1380e552da7Schristos ASSERT(0 == uv_ip6_addr("::", TEST_PORT, &addr1));
1390e552da7Schristos ASSERT(0 == uv_ip6_addr("::", TEST_PORT_2, &addr2));
1400e552da7Schristos
1410e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server);
1420e552da7Schristos ASSERT(r == 0);
1430e552da7Schristos r = uv_tcp_bind(&server, (const struct sockaddr*) &addr1, 0);
1440e552da7Schristos ASSERT(r == 0);
1450e552da7Schristos r = uv_tcp_bind(&server, (const struct sockaddr*) &addr2, 0);
1460e552da7Schristos ASSERT(r == UV_EINVAL);
1470e552da7Schristos
1480e552da7Schristos uv_close((uv_handle_t*)&server, close_cb);
1490e552da7Schristos
1500e552da7Schristos uv_run(uv_default_loop(), UV_RUN_DEFAULT);
1510e552da7Schristos
1520e552da7Schristos ASSERT(close_cb_called == 1);
1530e552da7Schristos
1540e552da7Schristos MAKE_VALGRIND_HAPPY();
1550e552da7Schristos return 0;
1560e552da7Schristos }
1570e552da7Schristos
1580e552da7Schristos
TEST_IMPL(tcp_bind6_localhost_ok)1590e552da7Schristos TEST_IMPL(tcp_bind6_localhost_ok) {
1600e552da7Schristos struct sockaddr_in6 addr;
1610e552da7Schristos uv_tcp_t server;
1620e552da7Schristos int r;
1630e552da7Schristos
1640e552da7Schristos if (!can_ipv6())
1650e552da7Schristos RETURN_SKIP("IPv6 not supported");
1660e552da7Schristos
1670e552da7Schristos ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr));
1680e552da7Schristos
1690e552da7Schristos r = uv_tcp_init(uv_default_loop(), &server);
1700e552da7Schristos ASSERT(r == 0);
1710e552da7Schristos r = uv_tcp_bind(&server, (const struct sockaddr*) &addr, 0);
1720e552da7Schristos ASSERT(r == 0);
1730e552da7Schristos
1740e552da7Schristos MAKE_VALGRIND_HAPPY();
1750e552da7Schristos return 0;
1760e552da7Schristos }
177