1 /* Copyright Joyent, Inc. and other Node 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 <stdio.h>
26 #include <string.h>
27 
28 static struct sockaddr_in addr;
29 static uv_tcp_t tcp_server;
30 static uv_tcp_t tcp_outgoing[2];
31 static uv_tcp_t tcp_incoming[ARRAY_SIZE(tcp_outgoing)];
32 static uv_connect_t connect_reqs[ARRAY_SIZE(tcp_outgoing)];
33 static uv_tcp_t tcp_check;
34 static uv_connect_t tcp_check_req;
35 static uv_write_t write_reqs[ARRAY_SIZE(tcp_outgoing)];
36 static unsigned int got_connections;
37 static unsigned int close_cb_called;
38 static unsigned int write_cb_called;
39 static unsigned int read_cb_called;
40 
close_cb(uv_handle_t * handle)41 static void close_cb(uv_handle_t* handle) {
42   close_cb_called++;
43 }
44 
write_cb(uv_write_t * req,int status)45 static void write_cb(uv_write_t* req, int status) {
46   ASSERT(status == 0);
47   write_cb_called++;
48 }
49 
connect_cb(uv_connect_t * req,int status)50 static void connect_cb(uv_connect_t* req, int status) {
51   unsigned int i;
52   uv_buf_t buf;
53   uv_stream_t* outgoing;
54 
55   if (req == &tcp_check_req) {
56     ASSERT(status != 0);
57 
58     /* Close check and incoming[0], time to finish test */
59     uv_close((uv_handle_t*) &tcp_incoming[0], close_cb);
60     uv_close((uv_handle_t*) &tcp_check, close_cb);
61     return;
62   }
63 
64   ASSERT(status == 0);
65   ASSERT(connect_reqs <= req);
66   ASSERT(req <= connect_reqs + ARRAY_SIZE(connect_reqs));
67   i = req - connect_reqs;
68 
69   buf = uv_buf_init("x", 1);
70   outgoing = (uv_stream_t*) &tcp_outgoing[i];
71   ASSERT(0 == uv_write(&write_reqs[i], outgoing, &buf, 1, write_cb));
72 }
73 
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)74 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
75   static char slab[1];
76   buf->base = slab;
77   buf->len = sizeof(slab);
78 }
79 
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)80 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
81   uv_loop_t* loop;
82   unsigned int i;
83 
84   /* Only first stream should receive read events */
85   ASSERT(stream == (uv_stream_t*) &tcp_incoming[0]);
86   ASSERT(0 == uv_read_stop(stream));
87   ASSERT(1 == nread);
88 
89   loop = stream->loop;
90   read_cb_called++;
91 
92   /* Close all active incomings, except current one */
93   for (i = 1; i < got_connections; i++)
94     uv_close((uv_handle_t*) &tcp_incoming[i], close_cb);
95 
96   /* Create new fd that should be one of the closed incomings */
97   ASSERT(0 == uv_tcp_init(loop, &tcp_check));
98   ASSERT(0 == uv_tcp_connect(&tcp_check_req,
99                              &tcp_check,
100                              (const struct sockaddr*) &addr,
101                              connect_cb));
102   ASSERT(0 == uv_read_start((uv_stream_t*) &tcp_check, alloc_cb, read_cb));
103 
104   /* Close server, so no one will connect to it */
105   uv_close((uv_handle_t*) &tcp_server, close_cb);
106 }
107 
connection_cb(uv_stream_t * server,int status)108 static void connection_cb(uv_stream_t* server, int status) {
109   unsigned int i;
110   uv_tcp_t* incoming;
111 
112   ASSERT(server == (uv_stream_t*) &tcp_server);
113 
114   /* Ignore tcp_check connection */
115   if (got_connections == ARRAY_SIZE(tcp_incoming))
116     return;
117 
118   /* Accept everyone */
119   incoming = &tcp_incoming[got_connections++];
120   ASSERT(0 == uv_tcp_init(server->loop, incoming));
121   ASSERT(0 == uv_accept(server, (uv_stream_t*) incoming));
122 
123   if (got_connections != ARRAY_SIZE(tcp_incoming))
124     return;
125 
126   /* Once all clients are accepted - start reading */
127   for (i = 0; i < ARRAY_SIZE(tcp_incoming); i++) {
128     incoming = &tcp_incoming[i];
129     ASSERT(0 == uv_read_start((uv_stream_t*) incoming, alloc_cb, read_cb));
130   }
131 }
132 
TEST_IMPL(tcp_close_accept)133 TEST_IMPL(tcp_close_accept) {
134   unsigned int i;
135   uv_loop_t* loop;
136   uv_tcp_t* client;
137 
138   /*
139    * A little explanation of what goes on below:
140    *
141    * We'll create server and connect to it using two clients, each writing one
142    * byte once connected.
143    *
144    * When all clients will be accepted by server - we'll start reading from them
145    * and, on first client's first byte, will close second client and server.
146    * After that, we'll immediately initiate new connection to server using
147    * tcp_check handle (thus, reusing fd from second client).
148    *
149    * In this situation uv__io_poll()'s event list should still contain read
150    * event for second client, and, if not cleaned up properly, `tcp_check` will
151    * receive stale event of second incoming and invoke `connect_cb` with zero
152    * status.
153    */
154 
155   loop = uv_default_loop();
156   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
157 
158   ASSERT(0 == uv_tcp_init(loop, &tcp_server));
159   ASSERT(0 == uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0));
160   ASSERT(0 == uv_listen((uv_stream_t*) &tcp_server,
161                         ARRAY_SIZE(tcp_outgoing),
162                         connection_cb));
163 
164   for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
165     client = tcp_outgoing + i;
166 
167     ASSERT(0 == uv_tcp_init(loop, client));
168     ASSERT(0 == uv_tcp_connect(&connect_reqs[i],
169                                client,
170                                (const struct sockaddr*) &addr,
171                                connect_cb));
172   }
173 
174   uv_run(loop, UV_RUN_DEFAULT);
175 
176   ASSERT(ARRAY_SIZE(tcp_outgoing) == got_connections);
177   ASSERT((ARRAY_SIZE(tcp_outgoing) + 2) == close_cb_called);
178   ASSERT(ARRAY_SIZE(tcp_outgoing) == write_cb_called);
179   ASSERT(1 == read_cb_called);
180 
181   MAKE_VALGRIND_HAPPY();
182   return 0;
183 }
184