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 static uv_loop_t* loop;
29 static uv_tcp_t tcp_server;
30 static uv_tcp_t tcp_client;
31 static uv_tcp_t tcp_accepted;
32 static uv_connect_t connect_req;
33 static uv_shutdown_t shutdown_req;
34 static uv_write_t write_reqs[4];
35 
36 static int client_close;
37 static int shutdown_before_close;
38 
39 static int write_cb_called;
40 static int close_cb_called;
41 static int shutdown_cb_called;
42 
43 static void connect_cb(uv_connect_t* req, int status);
44 static void write_cb(uv_write_t* req, int status);
45 static void close_cb(uv_handle_t* handle);
46 static void shutdown_cb(uv_shutdown_t* req, int status);
47 
48 static int read_size;
49 
50 
do_write(uv_tcp_t * handle)51 static void do_write(uv_tcp_t* handle) {
52   uv_buf_t buf;
53   unsigned i;
54   int r;
55 
56   buf = uv_buf_init("PING", 4);
57   for (i = 0; i < ARRAY_SIZE(write_reqs); i++) {
58     r = uv_write(&write_reqs[i], (uv_stream_t*) handle, &buf, 1, write_cb);
59     ASSERT(r == 0);
60   }
61 }
62 
63 
do_close(uv_tcp_t * handle)64 static void do_close(uv_tcp_t* handle) {
65   if (shutdown_before_close == 1) {
66     ASSERT(0 == uv_shutdown(&shutdown_req, (uv_stream_t*) handle, shutdown_cb));
67     ASSERT(UV_EINVAL == uv_tcp_close_reset(handle, close_cb));
68   } else {
69     ASSERT(0 == uv_tcp_close_reset(handle, close_cb));
70     ASSERT(UV_ENOTCONN == uv_shutdown(&shutdown_req, (uv_stream_t*) handle, shutdown_cb));
71   }
72 
73   uv_close((uv_handle_t*) &tcp_server, NULL);
74 }
75 
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)76 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
77   static char slab[1024];
78   buf->base = slab;
79   buf->len = sizeof(slab);
80 }
81 
read_cb2(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)82 static void read_cb2(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
83   ASSERT((uv_tcp_t*)stream == &tcp_client);
84   if (nread == UV_EOF)
85     uv_close((uv_handle_t*) stream, NULL);
86 }
87 
88 
connect_cb(uv_connect_t * conn_req,int status)89 static void connect_cb(uv_connect_t* conn_req, int status) {
90   ASSERT(conn_req == &connect_req);
91   uv_read_start((uv_stream_t*) &tcp_client, alloc_cb, read_cb2);
92   do_write(&tcp_client);
93   if (client_close)
94     do_close(&tcp_client);
95 }
96 
97 
write_cb(uv_write_t * req,int status)98 static void write_cb(uv_write_t* req, int status) {
99   /* write callbacks should run before the close callback */
100   ASSERT(close_cb_called == 0);
101   ASSERT(req->handle == (uv_stream_t*)&tcp_client);
102   write_cb_called++;
103 }
104 
105 
close_cb(uv_handle_t * handle)106 static void close_cb(uv_handle_t* handle) {
107   if (client_close)
108     ASSERT(handle == (uv_handle_t*) &tcp_client);
109   else
110     ASSERT(handle == (uv_handle_t*) &tcp_accepted);
111 
112   close_cb_called++;
113 }
114 
shutdown_cb(uv_shutdown_t * req,int status)115 static void shutdown_cb(uv_shutdown_t* req, int status) {
116   if (client_close)
117     ASSERT(req->handle == (uv_stream_t*) &tcp_client);
118   else
119     ASSERT(req->handle == (uv_stream_t*) &tcp_accepted);
120 
121   shutdown_cb_called++;
122 }
123 
124 
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)125 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
126   ASSERT((uv_tcp_t*)stream == &tcp_accepted);
127   if (nread < 0) {
128     uv_close((uv_handle_t*) stream, NULL);
129   } else {
130     read_size += nread;
131     if (read_size == 16 && client_close == 0)
132       do_close(&tcp_accepted);
133   }
134 }
135 
136 
connection_cb(uv_stream_t * server,int status)137 static void connection_cb(uv_stream_t* server, int status) {
138   ASSERT(status == 0);
139 
140   ASSERT(0 == uv_tcp_init(loop, &tcp_accepted));
141   ASSERT(0 == uv_accept(server, (uv_stream_t*) &tcp_accepted));
142 
143   uv_read_start((uv_stream_t*) &tcp_accepted, alloc_cb, read_cb);
144 }
145 
146 
start_server(uv_loop_t * loop,uv_tcp_t * handle)147 static void start_server(uv_loop_t* loop, uv_tcp_t* handle) {
148   struct sockaddr_in addr;
149   int r;
150 
151   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
152 
153   r = uv_tcp_init(loop, handle);
154   ASSERT(r == 0);
155 
156   r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0);
157   ASSERT(r == 0);
158 
159   r = uv_listen((uv_stream_t*)handle, 128, connection_cb);
160   ASSERT(r == 0);
161 }
162 
163 
do_connect(uv_loop_t * loop,uv_tcp_t * tcp_client)164 static void do_connect(uv_loop_t* loop, uv_tcp_t* tcp_client) {
165   struct sockaddr_in addr;
166   int r;
167 
168   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
169 
170   r = uv_tcp_init(loop, tcp_client);
171   ASSERT(r == 0);
172 
173   r = uv_tcp_connect(&connect_req,
174                      tcp_client,
175                      (const struct sockaddr*) &addr,
176                      connect_cb);
177   ASSERT(r == 0);
178 }
179 
180 
181 /* Check that pending write requests have their callbacks
182  * invoked when the handle is closed.
183  */
TEST_IMPL(tcp_close_reset_client)184 TEST_IMPL(tcp_close_reset_client) {
185   int r;
186 
187   loop = uv_default_loop();
188 
189   start_server(loop, &tcp_server);
190 
191   client_close = 1;
192   shutdown_before_close = 0;
193 
194   do_connect(loop, &tcp_client);
195 
196   ASSERT(write_cb_called == 0);
197   ASSERT(close_cb_called == 0);
198   ASSERT(shutdown_cb_called == 0);
199 
200   r = uv_run(loop, UV_RUN_DEFAULT);
201   ASSERT(r == 0);
202 
203   ASSERT(write_cb_called == 4);
204   ASSERT(close_cb_called == 1);
205   ASSERT(shutdown_cb_called == 0);
206 
207   MAKE_VALGRIND_HAPPY();
208   return 0;
209 }
210 
TEST_IMPL(tcp_close_reset_client_after_shutdown)211 TEST_IMPL(tcp_close_reset_client_after_shutdown) {
212   int r;
213 
214   loop = uv_default_loop();
215 
216   start_server(loop, &tcp_server);
217 
218   client_close = 1;
219   shutdown_before_close = 1;
220 
221   do_connect(loop, &tcp_client);
222 
223   ASSERT(write_cb_called == 0);
224   ASSERT(close_cb_called == 0);
225   ASSERT(shutdown_cb_called == 0);
226 
227   r = uv_run(loop, UV_RUN_DEFAULT);
228   ASSERT(r == 0);
229 
230   ASSERT(write_cb_called == 4);
231   ASSERT(close_cb_called == 0);
232   ASSERT(shutdown_cb_called == 1);
233 
234   MAKE_VALGRIND_HAPPY();
235   return 0;
236 }
237 
TEST_IMPL(tcp_close_reset_accepted)238 TEST_IMPL(tcp_close_reset_accepted) {
239   int r;
240 
241   loop = uv_default_loop();
242 
243   start_server(loop, &tcp_server);
244 
245   client_close = 0;
246   shutdown_before_close = 0;
247 
248   do_connect(loop, &tcp_client);
249 
250   ASSERT(write_cb_called == 0);
251   ASSERT(close_cb_called == 0);
252   ASSERT(shutdown_cb_called == 0);
253 
254   r = uv_run(loop, UV_RUN_DEFAULT);
255   ASSERT(r == 0);
256 
257   ASSERT(write_cb_called == 4);
258   ASSERT(close_cb_called == 1);
259   ASSERT(shutdown_cb_called == 0);
260 
261   MAKE_VALGRIND_HAPPY();
262   return 0;
263 }
264 
TEST_IMPL(tcp_close_reset_accepted_after_shutdown)265 TEST_IMPL(tcp_close_reset_accepted_after_shutdown) {
266   int r;
267 
268   loop = uv_default_loop();
269 
270   start_server(loop, &tcp_server);
271 
272   client_close = 0;
273   shutdown_before_close = 1;
274 
275   do_connect(loop, &tcp_client);
276 
277   ASSERT(write_cb_called == 0);
278   ASSERT(close_cb_called == 0);
279   ASSERT(shutdown_cb_called == 0);
280 
281   r = uv_run(loop, UV_RUN_DEFAULT);
282   ASSERT(r == 0);
283 
284   ASSERT(write_cb_called == 4);
285   ASSERT(close_cb_called == 0);
286   ASSERT(shutdown_cb_called == 1);
287 
288   MAKE_VALGRIND_HAPPY();
289   return 0;
290 }
291