1*0e552da7Schristos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2*0e552da7Schristos *
3*0e552da7Schristos * Permission is hereby granted, free of charge, to any person obtaining a copy
4*0e552da7Schristos * of this software and associated documentation files (the "Software"), to
5*0e552da7Schristos * deal in the Software without restriction, including without limitation the
6*0e552da7Schristos * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7*0e552da7Schristos * sell copies of the Software, and to permit persons to whom the Software is
8*0e552da7Schristos * furnished to do so, subject to the following conditions:
9*0e552da7Schristos *
10*0e552da7Schristos * The above copyright notice and this permission notice shall be included in
11*0e552da7Schristos * all copies or substantial portions of the Software.
12*0e552da7Schristos *
13*0e552da7Schristos * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14*0e552da7Schristos * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15*0e552da7Schristos * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16*0e552da7Schristos * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17*0e552da7Schristos * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18*0e552da7Schristos * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19*0e552da7Schristos * IN THE SOFTWARE.
20*0e552da7Schristos */
21*0e552da7Schristos
22*0e552da7Schristos /* this test is Unix only */
23*0e552da7Schristos #ifndef _WIN32
24*0e552da7Schristos
25*0e552da7Schristos #include "uv.h"
26*0e552da7Schristos #include "task.h"
27*0e552da7Schristos
28*0e552da7Schristos #include <stdio.h>
29*0e552da7Schristos #include <string.h>
30*0e552da7Schristos
31*0e552da7Schristos static struct sockaddr_in addr;
32*0e552da7Schristos static uv_tcp_t tcp_server;
33*0e552da7Schristos static uv_tcp_t tcp_outgoing[2];
34*0e552da7Schristos static uv_tcp_t tcp_incoming[ARRAY_SIZE(tcp_outgoing)];
35*0e552da7Schristos static uv_connect_t connect_reqs[ARRAY_SIZE(tcp_outgoing)];
36*0e552da7Schristos static uv_tcp_t tcp_check;
37*0e552da7Schristos static uv_connect_t tcp_check_req;
38*0e552da7Schristos static uv_write_t write_reqs[ARRAY_SIZE(tcp_outgoing)];
39*0e552da7Schristos static unsigned int got_connections;
40*0e552da7Schristos static unsigned int close_cb_called;
41*0e552da7Schristos static unsigned int write_cb_called;
42*0e552da7Schristos static unsigned int read_cb_called;
43*0e552da7Schristos static unsigned int pending_incoming;
44*0e552da7Schristos
close_cb(uv_handle_t * handle)45*0e552da7Schristos static void close_cb(uv_handle_t* handle) {
46*0e552da7Schristos close_cb_called++;
47*0e552da7Schristos }
48*0e552da7Schristos
write_cb(uv_write_t * req,int status)49*0e552da7Schristos static void write_cb(uv_write_t* req, int status) {
50*0e552da7Schristos ASSERT(status == 0);
51*0e552da7Schristos write_cb_called++;
52*0e552da7Schristos }
53*0e552da7Schristos
connect_cb(uv_connect_t * req,int status)54*0e552da7Schristos static void connect_cb(uv_connect_t* req, int status) {
55*0e552da7Schristos unsigned int i;
56*0e552da7Schristos uv_buf_t buf;
57*0e552da7Schristos uv_stream_t* outgoing;
58*0e552da7Schristos
59*0e552da7Schristos if (req == &tcp_check_req) {
60*0e552da7Schristos ASSERT(status != 0);
61*0e552da7Schristos
62*0e552da7Schristos /*
63*0e552da7Schristos * Time to finish the test: close both the check and pending incoming
64*0e552da7Schristos * connections
65*0e552da7Schristos */
66*0e552da7Schristos uv_close((uv_handle_t*) &tcp_incoming[pending_incoming], close_cb);
67*0e552da7Schristos uv_close((uv_handle_t*) &tcp_check, close_cb);
68*0e552da7Schristos return;
69*0e552da7Schristos }
70*0e552da7Schristos
71*0e552da7Schristos ASSERT(status == 0);
72*0e552da7Schristos ASSERT(connect_reqs <= req);
73*0e552da7Schristos ASSERT(req <= connect_reqs + ARRAY_SIZE(connect_reqs));
74*0e552da7Schristos i = req - connect_reqs;
75*0e552da7Schristos
76*0e552da7Schristos buf = uv_buf_init("x", 1);
77*0e552da7Schristos outgoing = (uv_stream_t*) &tcp_outgoing[i];
78*0e552da7Schristos ASSERT(0 == uv_write(&write_reqs[i], outgoing, &buf, 1, write_cb));
79*0e552da7Schristos }
80*0e552da7Schristos
alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)81*0e552da7Schristos static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
82*0e552da7Schristos static char slab[1];
83*0e552da7Schristos buf->base = slab;
84*0e552da7Schristos buf->len = sizeof(slab);
85*0e552da7Schristos }
86*0e552da7Schristos
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)87*0e552da7Schristos static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
88*0e552da7Schristos uv_loop_t* loop;
89*0e552da7Schristos unsigned int i;
90*0e552da7Schristos
91*0e552da7Schristos pending_incoming = (uv_tcp_t*) stream - &tcp_incoming[0];
92*0e552da7Schristos ASSERT(pending_incoming < got_connections);
93*0e552da7Schristos ASSERT(0 == uv_read_stop(stream));
94*0e552da7Schristos ASSERT(1 == nread);
95*0e552da7Schristos
96*0e552da7Schristos loop = stream->loop;
97*0e552da7Schristos read_cb_called++;
98*0e552da7Schristos
99*0e552da7Schristos /* Close all active incomings, except current one */
100*0e552da7Schristos for (i = 0; i < got_connections; i++) {
101*0e552da7Schristos if (i != pending_incoming)
102*0e552da7Schristos uv_close((uv_handle_t*) &tcp_incoming[i], close_cb);
103*0e552da7Schristos }
104*0e552da7Schristos
105*0e552da7Schristos /* Close server, so no one will connect to it */
106*0e552da7Schristos uv_close((uv_handle_t*) &tcp_server, close_cb);
107*0e552da7Schristos
108*0e552da7Schristos /* Create new fd that should be one of the closed incomings */
109*0e552da7Schristos ASSERT(0 == uv_tcp_init(loop, &tcp_check));
110*0e552da7Schristos ASSERT(0 == uv_tcp_connect(&tcp_check_req,
111*0e552da7Schristos &tcp_check,
112*0e552da7Schristos (const struct sockaddr*) &addr,
113*0e552da7Schristos connect_cb));
114*0e552da7Schristos ASSERT(0 == uv_read_start((uv_stream_t*) &tcp_check, alloc_cb, read_cb));
115*0e552da7Schristos }
116*0e552da7Schristos
connection_cb(uv_stream_t * server,int status)117*0e552da7Schristos static void connection_cb(uv_stream_t* server, int status) {
118*0e552da7Schristos unsigned int i;
119*0e552da7Schristos uv_tcp_t* incoming;
120*0e552da7Schristos
121*0e552da7Schristos ASSERT(server == (uv_stream_t*) &tcp_server);
122*0e552da7Schristos
123*0e552da7Schristos /* Ignore tcp_check connection */
124*0e552da7Schristos if (got_connections == ARRAY_SIZE(tcp_incoming))
125*0e552da7Schristos return;
126*0e552da7Schristos
127*0e552da7Schristos /* Accept everyone */
128*0e552da7Schristos incoming = &tcp_incoming[got_connections++];
129*0e552da7Schristos ASSERT(0 == uv_tcp_init(server->loop, incoming));
130*0e552da7Schristos ASSERT(0 == uv_accept(server, (uv_stream_t*) incoming));
131*0e552da7Schristos
132*0e552da7Schristos if (got_connections != ARRAY_SIZE(tcp_incoming))
133*0e552da7Schristos return;
134*0e552da7Schristos
135*0e552da7Schristos /* Once all clients are accepted - start reading */
136*0e552da7Schristos for (i = 0; i < ARRAY_SIZE(tcp_incoming); i++) {
137*0e552da7Schristos incoming = &tcp_incoming[i];
138*0e552da7Schristos ASSERT(0 == uv_read_start((uv_stream_t*) incoming, alloc_cb, read_cb));
139*0e552da7Schristos }
140*0e552da7Schristos }
141*0e552da7Schristos
TEST_IMPL(tcp_close_accept)142*0e552da7Schristos TEST_IMPL(tcp_close_accept) {
143*0e552da7Schristos unsigned int i;
144*0e552da7Schristos uv_loop_t* loop;
145*0e552da7Schristos uv_tcp_t* client;
146*0e552da7Schristos
147*0e552da7Schristos /*
148*0e552da7Schristos * A little explanation of what goes on below:
149*0e552da7Schristos *
150*0e552da7Schristos * We'll create server and connect to it using two clients, each writing one
151*0e552da7Schristos * byte once connected.
152*0e552da7Schristos *
153*0e552da7Schristos * When all clients will be accepted by server - we'll start reading from them
154*0e552da7Schristos * and, on first client's first byte, will close second client and server.
155*0e552da7Schristos * After that, we'll immediately initiate new connection to server using
156*0e552da7Schristos * tcp_check handle (thus, reusing fd from second client).
157*0e552da7Schristos *
158*0e552da7Schristos * In this situation uv__io_poll()'s event list should still contain read
159*0e552da7Schristos * event for second client, and, if not cleaned up properly, `tcp_check` will
160*0e552da7Schristos * receive stale event of second incoming and invoke `connect_cb` with zero
161*0e552da7Schristos * status.
162*0e552da7Schristos */
163*0e552da7Schristos
164*0e552da7Schristos loop = uv_default_loop();
165*0e552da7Schristos ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
166*0e552da7Schristos
167*0e552da7Schristos ASSERT(0 == uv_tcp_init(loop, &tcp_server));
168*0e552da7Schristos ASSERT(0 == uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0));
169*0e552da7Schristos ASSERT(0 == uv_listen((uv_stream_t*) &tcp_server,
170*0e552da7Schristos ARRAY_SIZE(tcp_outgoing),
171*0e552da7Schristos connection_cb));
172*0e552da7Schristos
173*0e552da7Schristos for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
174*0e552da7Schristos client = tcp_outgoing + i;
175*0e552da7Schristos
176*0e552da7Schristos ASSERT(0 == uv_tcp_init(loop, client));
177*0e552da7Schristos ASSERT(0 == uv_tcp_connect(&connect_reqs[i],
178*0e552da7Schristos client,
179*0e552da7Schristos (const struct sockaddr*) &addr,
180*0e552da7Schristos connect_cb));
181*0e552da7Schristos }
182*0e552da7Schristos
183*0e552da7Schristos uv_run(loop, UV_RUN_DEFAULT);
184*0e552da7Schristos
185*0e552da7Schristos ASSERT(ARRAY_SIZE(tcp_outgoing) == got_connections);
186*0e552da7Schristos ASSERT((ARRAY_SIZE(tcp_outgoing) + 2) == close_cb_called);
187*0e552da7Schristos ASSERT(ARRAY_SIZE(tcp_outgoing) == write_cb_called);
188*0e552da7Schristos ASSERT(1 == read_cb_called);
189*0e552da7Schristos
190*0e552da7Schristos MAKE_VALGRIND_HAPPY();
191*0e552da7Schristos return 0;
192*0e552da7Schristos }
193*0e552da7Schristos
194*0e552da7Schristos #else
195*0e552da7Schristos
196*0e552da7Schristos typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
197*0e552da7Schristos
198*0e552da7Schristos #endif /* !_WIN32 */
199