• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2015 Saúl Ibarra Corretgé <saghul@gmail.com>.
2  * All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 
23 #include "uv.h"
24 #include "task.h"
25 #include <string.h>
26 
27 #ifdef _WIN32
28 # define INVALID_FD (INVALID_HANDLE_VALUE)
29 #else
30 # define INVALID_FD (-1)
31 #endif
32 
33 
on_connect(uv_connect_t * req,int status)34 static void on_connect(uv_connect_t* req, int status) {
35   ASSERT(status == 0);
36   uv_close((uv_handle_t*) req->handle, NULL);
37 }
38 
39 
on_connection(uv_stream_t * server,int status)40 static void on_connection(uv_stream_t* server, int status) {
41   uv_tcp_t* handle;
42   int r;
43 
44   ASSERT(status == 0);
45 
46   handle = malloc(sizeof(*handle));
47   ASSERT_NOT_NULL(handle);
48 
49   r = uv_tcp_init_ex(server->loop, handle, AF_INET);
50   ASSERT(r == 0);
51 
52   r = uv_accept(server, (uv_stream_t*)handle);
53   ASSERT(r == UV_EBUSY);
54 
55   uv_close((uv_handle_t*) server, NULL);
56   uv_close((uv_handle_t*) handle, (uv_close_cb)free);
57 }
58 
59 
tcp_listener(uv_loop_t * loop,uv_tcp_t * server)60 static void tcp_listener(uv_loop_t* loop, uv_tcp_t* server) {
61   struct sockaddr_in addr;
62   int r;
63 
64   ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
65 
66   r = uv_tcp_init(loop, server);
67   ASSERT(r == 0);
68 
69   r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
70   ASSERT(r == 0);
71 
72   r = uv_listen((uv_stream_t*) server, 128, on_connection);
73   ASSERT(r == 0);
74 }
75 
76 
tcp_connector(uv_loop_t * loop,uv_tcp_t * client,uv_connect_t * req)77 static void tcp_connector(uv_loop_t* loop, uv_tcp_t* client, uv_connect_t* req) {
78   struct sockaddr_in server_addr;
79   int r;
80 
81   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
82 
83   r = uv_tcp_init(loop, client);
84   ASSERT(r == 0);
85 
86   r = uv_tcp_connect(req,
87                      client,
88                      (const struct sockaddr*) &server_addr,
89                      on_connect);
90   ASSERT(r == 0);
91 }
92 
93 
TEST_IMPL(tcp_create_early)94 TEST_IMPL(tcp_create_early) {
95   struct sockaddr_in addr;
96   struct sockaddr_in sockname;
97   uv_tcp_t client;
98   uv_os_fd_t fd;
99   int r, namelen;
100 
101   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
102 
103   r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET);
104   ASSERT(r == 0);
105 
106   r = uv_fileno((const uv_handle_t*) &client, &fd);
107   ASSERT(r == 0);
108   ASSERT(fd != INVALID_FD);
109 
110   /* Windows returns WSAEINVAL if the socket is not bound */
111 #ifndef _WIN32
112   namelen = sizeof sockname;
113   r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
114   ASSERT(r == 0);
115   ASSERT(sockname.sin_family == AF_INET);
116 #endif
117 
118   r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
119   ASSERT(r == 0);
120 
121   namelen = sizeof sockname;
122   r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
123   ASSERT(r == 0);
124   ASSERT(memcmp(&addr.sin_addr,
125                 &sockname.sin_addr,
126                 sizeof(addr.sin_addr)) == 0);
127 
128   uv_close((uv_handle_t*) &client, NULL);
129   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
130 
131   MAKE_VALGRIND_HAPPY();
132   return 0;
133 }
134 
135 
TEST_IMPL(tcp_create_early_bad_bind)136 TEST_IMPL(tcp_create_early_bad_bind) {
137   struct sockaddr_in addr;
138   uv_tcp_t client;
139   uv_os_fd_t fd;
140   int r;
141 
142   if (!can_ipv6())
143     RETURN_SKIP("IPv6 not supported");
144 
145   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
146 
147   r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET6);
148   ASSERT(r == 0);
149 
150   r = uv_fileno((const uv_handle_t*) &client, &fd);
151   ASSERT(r == 0);
152   ASSERT(fd != INVALID_FD);
153 
154   /* Windows returns WSAEINVAL if the socket is not bound */
155 #ifndef _WIN32
156   {
157     int namelen;
158     struct sockaddr_in6 sockname;
159     namelen = sizeof sockname;
160     r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
161     ASSERT(r == 0);
162     ASSERT(sockname.sin6_family == AF_INET6);
163   }
164 #endif
165 
166   r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
167 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
168   ASSERT(r == UV_EINVAL);
169 #else
170   ASSERT(r == UV_EFAULT);
171 #endif
172 
173   uv_close((uv_handle_t*) &client, NULL);
174   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
175 
176   MAKE_VALGRIND_HAPPY();
177   return 0;
178 }
179 
180 
TEST_IMPL(tcp_create_early_bad_domain)181 TEST_IMPL(tcp_create_early_bad_domain) {
182   uv_tcp_t client;
183   int r;
184 
185   r = uv_tcp_init_ex(uv_default_loop(), &client, 47);
186   ASSERT(r == UV_EINVAL);
187 
188   r = uv_tcp_init_ex(uv_default_loop(), &client, 1024);
189   ASSERT(r == UV_EINVAL);
190 
191   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
192 
193   MAKE_VALGRIND_HAPPY();
194   return 0;
195 }
196 
197 
TEST_IMPL(tcp_create_early_accept)198 TEST_IMPL(tcp_create_early_accept) {
199   uv_tcp_t client, server;
200   uv_connect_t connect_req;
201 
202   tcp_listener(uv_default_loop(), &server);
203   tcp_connector(uv_default_loop(), &client, &connect_req);
204 
205   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
206 
207   MAKE_VALGRIND_HAPPY();
208   return 0;
209 }
210