|
160
|
1 /* Copyright (c) 2015 Saúl Ibarra Corretgé <[email protected]>.
|
|
|
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
|
|
|
34 static void on_connect(uv_connect_t* req, int status) {
|
|
|
35 ASSERT_OK(status);
|
|
|
36 uv_close((uv_handle_t*) req->handle, NULL);
|
|
|
37 }
|
|
|
38
|
|
|
39
|
|
|
40 static void on_connection(uv_stream_t* server, int status) {
|
|
|
41 uv_tcp_t* handle;
|
|
|
42 int r;
|
|
|
43
|
|
|
44 ASSERT_OK(status);
|
|
|
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_OK(r);
|
|
|
51
|
|
|
52 r = uv_accept(server, (uv_stream_t*)handle);
|
|
|
53 ASSERT_EQ(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
|
|
|
60 static void tcp_listener(uv_loop_t* loop, uv_tcp_t* server) {
|
|
|
61 struct sockaddr_in addr;
|
|
|
62 int r;
|
|
|
63
|
|
|
64 ASSERT_OK(uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
|
|
|
65
|
|
|
66 r = uv_tcp_init(loop, server);
|
|
|
67 ASSERT_OK(r);
|
|
|
68
|
|
|
69 r = uv_tcp_bind(server, (const struct sockaddr*) &addr, 0);
|
|
|
70 ASSERT_OK(r);
|
|
|
71
|
|
|
72 r = uv_listen((uv_stream_t*) server, 128, on_connection);
|
|
|
73 ASSERT_OK(r);
|
|
|
74 }
|
|
|
75
|
|
|
76
|
|
|
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_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
|
|
|
82
|
|
|
83 r = uv_tcp_init(loop, client);
|
|
|
84 ASSERT_OK(r);
|
|
|
85
|
|
|
86 r = uv_tcp_connect(req,
|
|
|
87 client,
|
|
|
88 (const struct sockaddr*) &server_addr,
|
|
|
89 on_connect);
|
|
|
90 ASSERT_OK(r);
|
|
|
91 }
|
|
|
92
|
|
|
93
|
|
|
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_OK(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_OK(r);
|
|
|
105
|
|
|
106 r = uv_fileno((const uv_handle_t*) &client, &fd);
|
|
|
107 ASSERT_OK(r);
|
|
|
108
|
|
|
109 /* Windows returns WSAEINVAL if the socket is not bound */
|
|
|
110 #ifndef _WIN32
|
|
|
111 ASSERT_NE(fd, INVALID_FD);
|
|
|
112 namelen = sizeof sockname;
|
|
|
113 r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
|
|
|
114 ASSERT_OK(r);
|
|
|
115 ASSERT_EQ(sockname.sin_family, AF_INET);
|
|
|
116 #else
|
|
|
117 ASSERT_PTR_NE(fd, INVALID_FD);
|
|
|
118 #endif
|
|
|
119
|
|
|
120 r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
|
|
|
121 ASSERT_OK(r);
|
|
|
122
|
|
|
123 namelen = sizeof sockname;
|
|
|
124 r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
|
|
|
125 ASSERT_OK(r);
|
|
|
126 ASSERT_OK(memcmp(&addr.sin_addr,
|
|
|
127 &sockname.sin_addr,
|
|
|
128 sizeof(addr.sin_addr)));
|
|
|
129
|
|
|
130 uv_close((uv_handle_t*) &client, NULL);
|
|
|
131 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
132
|
|
|
133 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
134 return 0;
|
|
|
135 }
|
|
|
136
|
|
|
137
|
|
|
138 TEST_IMPL(tcp_create_early_bad_bind) {
|
|
|
139 struct sockaddr_in addr;
|
|
|
140 uv_tcp_t client;
|
|
|
141 uv_os_fd_t fd;
|
|
|
142 int r;
|
|
|
143
|
|
|
144 if (!can_ipv6())
|
|
|
145 RETURN_SKIP("IPv6 not supported");
|
|
|
146
|
|
|
147 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
|
|
148
|
|
|
149 r = uv_tcp_init_ex(uv_default_loop(), &client, AF_INET6);
|
|
|
150 ASSERT_OK(r);
|
|
|
151
|
|
|
152 r = uv_fileno((const uv_handle_t*) &client, &fd);
|
|
|
153 ASSERT_OK(r);
|
|
|
154
|
|
|
155 /* Windows returns WSAEINVAL if the socket is not bound */
|
|
|
156 #ifndef _WIN32
|
|
|
157 ASSERT_NE(fd, INVALID_FD);
|
|
|
158 {
|
|
|
159 int namelen;
|
|
|
160 struct sockaddr_in6 sockname;
|
|
|
161 namelen = sizeof sockname;
|
|
|
162 r = uv_tcp_getsockname(&client, (struct sockaddr*) &sockname, &namelen);
|
|
|
163 ASSERT_OK(r);
|
|
|
164 ASSERT_EQ(sockname.sin6_family, AF_INET6);
|
|
|
165 }
|
|
|
166 #else
|
|
|
167 ASSERT_PTR_NE(fd, INVALID_FD);
|
|
|
168 #endif
|
|
|
169
|
|
|
170 r = uv_tcp_bind(&client, (const struct sockaddr*) &addr, 0);
|
|
|
171 #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MSYS__)
|
|
|
172 ASSERT_EQ(r, UV_EINVAL);
|
|
|
173 #else
|
|
|
174 ASSERT_EQ(r, UV_EFAULT);
|
|
|
175 #endif
|
|
|
176
|
|
|
177 uv_close((uv_handle_t*) &client, NULL);
|
|
|
178 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
179
|
|
|
180 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
181 return 0;
|
|
|
182 }
|
|
|
183
|
|
|
184
|
|
|
185 TEST_IMPL(tcp_create_early_bad_domain) {
|
|
|
186 uv_tcp_t client;
|
|
|
187 int r;
|
|
|
188
|
|
|
189 r = uv_tcp_init_ex(uv_default_loop(), &client, 47);
|
|
|
190 ASSERT_EQ(r, UV_EINVAL);
|
|
|
191
|
|
|
192 r = uv_tcp_init_ex(uv_default_loop(), &client, 1024);
|
|
|
193 ASSERT_EQ(r, UV_EINVAL);
|
|
|
194
|
|
|
195 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
196
|
|
|
197 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
198 return 0;
|
|
|
199 }
|
|
|
200
|
|
|
201
|
|
|
202 TEST_IMPL(tcp_create_early_accept) {
|
|
|
203 uv_tcp_t client, server;
|
|
|
204 uv_connect_t connect_req;
|
|
|
205
|
|
|
206 tcp_listener(uv_default_loop(), &server);
|
|
|
207 tcp_connector(uv_default_loop(), &client, &connect_req);
|
|
|
208
|
|
|
209 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
210
|
|
|
211 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
212 return 0;
|
|
|
213 }
|