1 1.1 christos /* Copyright (c) 2015 Sal Ibarra Corretg <saghul (at) gmail.com>. 2 1.1 christos * All rights reserved. 3 1.1 christos * 4 1.1 christos * Permission is hereby granted, free of charge, to any person obtaining a copy 5 1.1 christos * of this software and associated documentation files (the "Software"), to 6 1.1 christos * deal in the Software without restriction, including without limitation the 7 1.1 christos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 8 1.1 christos * sell copies of the Software, and to permit persons to whom the Software is 9 1.1 christos * furnished to do so, subject to the following conditions: 10 1.1 christos * 11 1.1 christos * The above copyright notice and this permission notice shall be included in 12 1.1 christos * all copies or substantial portions of the Software. 13 1.1 christos * 14 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 1.1 christos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 1.1 christos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 1.1 christos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 1.1 christos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 1.1 christos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20 1.1 christos * IN THE SOFTWARE. 21 1.1 christos */ 22 1.1 christos 23 1.1 christos #include "uv.h" 24 1.1 christos #include "task.h" 25 1.1 christos #include <string.h> 26 1.1 christos 27 1.1 christos #ifdef _WIN32 28 1.1 christos # define INVALID_FD (INVALID_HANDLE_VALUE) 29 1.1 christos #else 30 1.1 christos # define INVALID_FD (-1) 31 1.1 christos #endif 32 1.1 christos 33 1.1 christos 34 1.1 christos TEST_IMPL(udp_create_early) { 35 1.1 christos struct sockaddr_in addr; 36 1.1 christos struct sockaddr_in sockname; 37 1.1 christos uv_udp_t client; 38 1.1 christos uv_os_fd_t fd; 39 1.1 christos int r, namelen; 40 1.1 christos 41 1.1.1.2 christos ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); 42 1.1 christos 43 1.1 christos r = uv_udp_init_ex(uv_default_loop(), &client, AF_INET); 44 1.1.1.2 christos ASSERT_OK(r); 45 1.1 christos 46 1.1 christos r = uv_fileno((const uv_handle_t*) &client, &fd); 47 1.1.1.2 christos ASSERT_OK(r); 48 1.1 christos 49 1.1 christos /* Windows returns WSAEINVAL if the socket is not bound */ 50 1.1 christos #ifndef _WIN32 51 1.1.1.2 christos ASSERT_NE(fd, INVALID_FD); 52 1.1 christos namelen = sizeof sockname; 53 1.1 christos r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen); 54 1.1.1.2 christos ASSERT_OK(r); 55 1.1.1.2 christos ASSERT_EQ(sockname.sin_family, AF_INET); 56 1.1.1.2 christos #else 57 1.1.1.2 christos ASSERT_PTR_NE(fd, INVALID_FD); 58 1.1 christos #endif 59 1.1 christos 60 1.1 christos r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0); 61 1.1.1.2 christos ASSERT_OK(r); 62 1.1 christos 63 1.1 christos namelen = sizeof sockname; 64 1.1 christos r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen); 65 1.1.1.2 christos ASSERT_OK(r); 66 1.1.1.2 christos ASSERT_OK(memcmp(&addr.sin_addr, 67 1.1.1.2 christos &sockname.sin_addr, 68 1.1.1.2 christos sizeof(addr.sin_addr))); 69 1.1 christos 70 1.1 christos uv_close((uv_handle_t*) &client, NULL); 71 1.1 christos uv_run(uv_default_loop(), UV_RUN_DEFAULT); 72 1.1 christos 73 1.1.1.2 christos MAKE_VALGRIND_HAPPY(uv_default_loop()); 74 1.1 christos return 0; 75 1.1 christos } 76 1.1 christos 77 1.1 christos 78 1.1 christos TEST_IMPL(udp_create_early_bad_bind) { 79 1.1 christos struct sockaddr_in addr; 80 1.1 christos uv_udp_t client; 81 1.1 christos uv_os_fd_t fd; 82 1.1 christos int r; 83 1.1 christos 84 1.1 christos if (!can_ipv6()) 85 1.1 christos RETURN_SKIP("IPv6 not supported"); 86 1.1 christos 87 1.1.1.2 christos ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr)); 88 1.1 christos 89 1.1 christos r = uv_udp_init_ex(uv_default_loop(), &client, AF_INET6); 90 1.1.1.2 christos ASSERT_OK(r); 91 1.1 christos 92 1.1 christos r = uv_fileno((const uv_handle_t*) &client, &fd); 93 1.1.1.2 christos ASSERT_OK(r); 94 1.1 christos 95 1.1 christos /* Windows returns WSAEINVAL if the socket is not bound */ 96 1.1.1.2 christos #ifndef _WIN32 97 1.1.1.2 christos ASSERT_NE(fd, INVALID_FD); 98 1.1.1.2 christos { 99 1.1 christos int namelen; 100 1.1 christos struct sockaddr_in6 sockname; 101 1.1 christos namelen = sizeof sockname; 102 1.1 christos r = uv_udp_getsockname(&client, (struct sockaddr*) &sockname, &namelen); 103 1.1.1.2 christos ASSERT_OK(r); 104 1.1.1.2 christos ASSERT_EQ(sockname.sin6_family, AF_INET6); 105 1.1 christos } 106 1.1.1.2 christos #else 107 1.1.1.2 christos ASSERT_PTR_NE(fd, INVALID_FD); 108 1.1 christos #endif 109 1.1 christos 110 1.1 christos r = uv_udp_bind(&client, (const struct sockaddr*) &addr, 0); 111 1.1 christos #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__) 112 1.1.1.2 christos ASSERT_EQ(r, UV_EINVAL); 113 1.1 christos #else 114 1.1.1.2 christos ASSERT_EQ(r, UV_EFAULT); 115 1.1 christos #endif 116 1.1 christos 117 1.1 christos uv_close((uv_handle_t*) &client, NULL); 118 1.1 christos uv_run(uv_default_loop(), UV_RUN_DEFAULT); 119 1.1 christos 120 1.1.1.2 christos MAKE_VALGRIND_HAPPY(uv_default_loop()); 121 1.1 christos return 0; 122 1.1 christos } 123 1.1 christos 124 1.1 christos 125 1.1 christos TEST_IMPL(udp_create_early_bad_domain) { 126 1.1 christos uv_udp_t client; 127 1.1 christos int r; 128 1.1 christos 129 1.1 christos r = uv_udp_init_ex(uv_default_loop(), &client, 47); 130 1.1.1.2 christos ASSERT_EQ(r, UV_EINVAL); 131 1.1 christos 132 1.1 christos r = uv_udp_init_ex(uv_default_loop(), &client, 1024); 133 1.1.1.2 christos ASSERT_EQ(r, UV_EINVAL); 134 1.1 christos 135 1.1 christos uv_run(uv_default_loop(), UV_RUN_DEFAULT); 136 1.1 christos 137 1.1.1.2 christos MAKE_VALGRIND_HAPPY(uv_default_loop()); 138 1.1 christos return 0; 139 1.1 christos } 140