1 /* Copyright libuv project contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22 #include "uv.h"
23 #include "task.h"
24
25 #include <errno.h>
26 #include <string.h> /* memset */
27
28 #ifdef _WIN32
29 # define INVALID_FD (INVALID_HANDLE_VALUE)
30 #else
31 # define INVALID_FD (-1)
32 #endif
33
34 static uv_loop_t* loop;
35 static uv_tcp_t tcp_server;
36 static uv_tcp_t tcp_client;
37 static uv_tcp_t tcp_accepted;
38 static uv_connect_t connect_req;
39 static uv_shutdown_t shutdown_req;
40 static uv_write_t write_reqs[4];
41
42 static int client_close;
43 static int shutdown_before_close;
44
45 static int write_cb_called;
46 static int close_cb_called;
47 static int shutdown_cb_called;
48
49 static void connect_cb(uv_connect_t* req, int status);
50 static void write_cb(uv_write_t* req, int status);
51 static void close_cb(uv_handle_t* handle);
52 static void shutdown_cb(uv_shutdown_t* req, int status);
53
54 static int read_size;
55
56
do_write(uv_tcp_t * handle)57 static void do_write(uv_tcp_t* handle) {
58 uv_buf_t buf;
59 unsigned i;
60 int r;
61
62 buf = uv_buf_init("PING", 4);
63 for (i = 0; i < ARRAY_SIZE(write_reqs); i++) {
64 r = uv_write(&write_reqs[i], (uv_stream_t*) handle, &buf, 1, write_cb);
65 ASSERT(r == 0);
66 }
67 }
68
69
do_close(uv_tcp_t * handle)70 static void do_close(uv_tcp_t* handle) {
71 uv_os_fd_t fd;
72 int r;
73
74 if (shutdown_before_close == 1) {
75 ASSERT(0 == uv_shutdown(&shutdown_req, (uv_stream_t*) handle, shutdown_cb));
76 ASSERT(UV_EINVAL == uv_tcp_close_reset(handle, close_cb));
77 } else if (shutdown_before_close == 2) {
78 r = uv_fileno((const uv_handle_t*) handle, &fd);
79 ASSERT_EQ(r, 0);
80 ASSERT_NE(fd, INVALID_FD);
81 #ifdef _WIN32
82 ASSERT_EQ(0, shutdown(fd, SD_BOTH));
83 #else
84 ASSERT_EQ(0, shutdown(fd, SHUT_RDWR));
85 #endif
86 ASSERT_EQ(0, uv_tcp_close_reset(handle, close_cb));
87 } else {
88 ASSERT(0 == uv_tcp_close_reset(handle, close_cb));
89 ASSERT(UV_ENOTCONN == uv_shutdown(&shutdown_req, (uv_stream_t*) handle, shutdown_cb));
90 }
91
92 uv_close((uv_handle_t*) &tcp_server, NULL);
93 }
94
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)95 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
96 static char slab[1024];
97 buf->base = slab;
98 buf->len = sizeof(slab);
99 }
100
read_cb2(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)101 static void read_cb2(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
102 ASSERT((uv_tcp_t*)stream == &tcp_client);
103 if (nread == UV_EOF)
104 uv_close((uv_handle_t*) stream, NULL);
105 }
106
107
connect_cb(uv_connect_t * conn_req,int status)108 static void connect_cb(uv_connect_t* conn_req, int status) {
109 ASSERT(conn_req == &connect_req);
110 uv_read_start((uv_stream_t*) &tcp_client, alloc_cb, read_cb2);
111 do_write(&tcp_client);
112 if (client_close)
113 do_close(&tcp_client);
114 }
115
116
write_cb(uv_write_t * req,int status)117 static void write_cb(uv_write_t* req, int status) {
118 /* write callbacks should run before the close callback */
119 ASSERT(close_cb_called == 0);
120 ASSERT(req->handle == (uv_stream_t*)&tcp_client);
121 write_cb_called++;
122 }
123
124
close_cb(uv_handle_t * handle)125 static void close_cb(uv_handle_t* handle) {
126 if (client_close)
127 ASSERT(handle == (uv_handle_t*) &tcp_client);
128 else
129 ASSERT(handle == (uv_handle_t*) &tcp_accepted);
130
131 close_cb_called++;
132 }
133
shutdown_cb(uv_shutdown_t * req,int status)134 static void shutdown_cb(uv_shutdown_t* req, int status) {
135 if (client_close)
136 ASSERT(req->handle == (uv_stream_t*) &tcp_client);
137 else
138 ASSERT(req->handle == (uv_stream_t*) &tcp_accepted);
139
140 shutdown_cb_called++;
141 }
142
143
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)144 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
145 ASSERT((uv_tcp_t*)stream == &tcp_accepted);
146 if (nread < 0) {
147 uv_close((uv_handle_t*) stream, NULL);
148 } else {
149 read_size += nread;
150 if (read_size == 16 && client_close == 0)
151 do_close(&tcp_accepted);
152 }
153 }
154
155
connection_cb(uv_stream_t * server,int status)156 static void connection_cb(uv_stream_t* server, int status) {
157 ASSERT(status == 0);
158
159 ASSERT(0 == uv_tcp_init(loop, &tcp_accepted));
160 ASSERT(0 == uv_accept(server, (uv_stream_t*) &tcp_accepted));
161
162 uv_read_start((uv_stream_t*) &tcp_accepted, alloc_cb, read_cb);
163 }
164
165
start_server(uv_loop_t * loop,uv_tcp_t * handle)166 static void start_server(uv_loop_t* loop, uv_tcp_t* handle) {
167 struct sockaddr_in addr;
168 int r;
169
170 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
171
172 r = uv_tcp_init(loop, handle);
173 ASSERT(r == 0);
174
175 r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0);
176 ASSERT(r == 0);
177
178 r = uv_listen((uv_stream_t*)handle, 128, connection_cb);
179 ASSERT(r == 0);
180 }
181
182
do_connect(uv_loop_t * loop,uv_tcp_t * tcp_client)183 static void do_connect(uv_loop_t* loop, uv_tcp_t* tcp_client) {
184 struct sockaddr_in addr;
185 int r;
186
187 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
188
189 r = uv_tcp_init(loop, tcp_client);
190 ASSERT(r == 0);
191
192 r = uv_tcp_connect(&connect_req,
193 tcp_client,
194 (const struct sockaddr*) &addr,
195 connect_cb);
196 ASSERT(r == 0);
197 }
198
199
200 /* Check that pending write requests have their callbacks
201 * invoked when the handle is closed.
202 */
TEST_IMPL(tcp_close_reset_client)203 TEST_IMPL(tcp_close_reset_client) {
204 int r;
205
206 loop = uv_default_loop();
207
208 start_server(loop, &tcp_server);
209
210 client_close = 1;
211 shutdown_before_close = 0;
212
213 do_connect(loop, &tcp_client);
214
215 ASSERT(write_cb_called == 0);
216 ASSERT(close_cb_called == 0);
217 ASSERT(shutdown_cb_called == 0);
218
219 r = uv_run(loop, UV_RUN_DEFAULT);
220 ASSERT(r == 0);
221
222 ASSERT(write_cb_called == 4);
223 ASSERT(close_cb_called == 1);
224 ASSERT(shutdown_cb_called == 0);
225
226 MAKE_VALGRIND_HAPPY();
227 return 0;
228 }
229
TEST_IMPL(tcp_close_reset_client_after_shutdown)230 TEST_IMPL(tcp_close_reset_client_after_shutdown) {
231 int r;
232
233 loop = uv_default_loop();
234
235 start_server(loop, &tcp_server);
236
237 client_close = 1;
238 shutdown_before_close = 1;
239
240 do_connect(loop, &tcp_client);
241
242 ASSERT(write_cb_called == 0);
243 ASSERT(close_cb_called == 0);
244 ASSERT(shutdown_cb_called == 0);
245
246 r = uv_run(loop, UV_RUN_DEFAULT);
247 ASSERT(r == 0);
248
249 ASSERT(write_cb_called == 4);
250 ASSERT(close_cb_called == 0);
251 ASSERT(shutdown_cb_called == 1);
252
253 MAKE_VALGRIND_HAPPY();
254 return 0;
255 }
256
TEST_IMPL(tcp_close_reset_accepted)257 TEST_IMPL(tcp_close_reset_accepted) {
258 int r;
259
260 loop = uv_default_loop();
261
262 start_server(loop, &tcp_server);
263
264 client_close = 0;
265 shutdown_before_close = 0;
266
267 do_connect(loop, &tcp_client);
268
269 ASSERT(write_cb_called == 0);
270 ASSERT(close_cb_called == 0);
271 ASSERT(shutdown_cb_called == 0);
272
273 r = uv_run(loop, UV_RUN_DEFAULT);
274 ASSERT(r == 0);
275
276 ASSERT(write_cb_called == 4);
277 ASSERT(close_cb_called == 1);
278 ASSERT(shutdown_cb_called == 0);
279
280 MAKE_VALGRIND_HAPPY();
281 return 0;
282 }
283
TEST_IMPL(tcp_close_reset_accepted_after_shutdown)284 TEST_IMPL(tcp_close_reset_accepted_after_shutdown) {
285 int r;
286
287 loop = uv_default_loop();
288
289 start_server(loop, &tcp_server);
290
291 client_close = 0;
292 shutdown_before_close = 1;
293
294 do_connect(loop, &tcp_client);
295
296 ASSERT(write_cb_called == 0);
297 ASSERT(close_cb_called == 0);
298 ASSERT(shutdown_cb_called == 0);
299
300 r = uv_run(loop, UV_RUN_DEFAULT);
301 ASSERT(r == 0);
302
303 ASSERT(write_cb_called == 4);
304 ASSERT(close_cb_called == 0);
305 ASSERT(shutdown_cb_called == 1);
306
307 MAKE_VALGRIND_HAPPY();
308 return 0;
309 }
310
TEST_IMPL(tcp_close_reset_accepted_after_socket_shutdown)311 TEST_IMPL(tcp_close_reset_accepted_after_socket_shutdown) {
312 int r;
313
314 loop = uv_default_loop();
315
316 start_server(loop, &tcp_server);
317
318 client_close = 0;
319 shutdown_before_close = 2;
320
321 do_connect(loop, &tcp_client);
322
323 ASSERT_EQ(write_cb_called, 0);
324 ASSERT_EQ(close_cb_called, 0);
325 ASSERT_EQ(shutdown_cb_called, 0);
326
327 r = uv_run(loop, UV_RUN_DEFAULT);
328 ASSERT_EQ(r, 0);
329
330 ASSERT_EQ(write_cb_called, 4);
331 ASSERT_EQ(close_cb_called, 1);
332 ASSERT_EQ(shutdown_cb_called, 0);
333
334 MAKE_VALGRIND_HAPPY();
335 return 0;
336 }
337