1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
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;
41 static void close_cb(uv_handle_t* handle) {
45 static void write_cb(uv_write_t* req, int status) {
50 static void connect_cb(uv_connect_t* req, int status) {
53 uv_stream_t* outgoing;
55 if (req == &tcp_check_req) {
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);
65 ASSERT(connect_reqs <= req);
66 ASSERT(req <= connect_reqs + ARRAY_SIZE(connect_reqs));
67 i = req - connect_reqs;
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));
74 static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
77 return uv_buf_init(buf, sizeof(buf));
80 static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t b) {
84 /* Only first stream should receive read events */
85 ASSERT(stream == (uv_stream_t*) &tcp_incoming[0]);
86 ASSERT(0 == uv_read_stop(stream));
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);
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, &tcp_check, addr, connect_cb));
99 ASSERT(0 == uv_read_start((uv_stream_t*) &tcp_check, alloc_cb, read_cb));
101 /* Close server, so no one will connect to it */
102 uv_close((uv_handle_t*) &tcp_server, close_cb);
105 static void connection_cb(uv_stream_t* server, int status) {
109 ASSERT(server == (uv_stream_t*) &tcp_server);
111 /* Ignore tcp_check connection */
112 if (got_connections == ARRAY_SIZE(tcp_incoming))
115 /* Accept everyone */
116 incoming = &tcp_incoming[got_connections++];
117 ASSERT(0 == uv_tcp_init(server->loop, incoming));
118 ASSERT(0 == uv_accept(server, (uv_stream_t*) incoming));
120 if (got_connections != ARRAY_SIZE(tcp_incoming))
123 /* Once all clients are accepted - start reading */
124 for (i = 0; i < ARRAY_SIZE(tcp_incoming); i++) {
125 incoming = &tcp_incoming[i];
126 ASSERT(0 == uv_read_start((uv_stream_t*) incoming, alloc_cb, read_cb));
130 TEST_IMPL(tcp_close_accept) {
136 * A little explanation of what goes on below:
138 * We'll create server and connect to it using two clients, each writing one
139 * byte once connected.
141 * When all clients will be accepted by server - we'll start reading from them
142 * and, on first client's first byte, will close second client and server.
143 * After that, we'll immediately initiate new connection to server using
144 * tcp_check handle (thus, reusing fd from second client).
146 * In this situation uv__io_poll()'s event list should still contain read
147 * event for second client, and, if not cleaned up properly, `tcp_check` will
148 * receive stale event of second incoming and invoke `connect_cb` with zero
152 loop = uv_default_loop();
153 addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
155 ASSERT(0 == uv_tcp_init(loop, &tcp_server));
156 ASSERT(0 == uv_tcp_bind(&tcp_server, addr));
157 ASSERT(0 == uv_listen((uv_stream_t*) &tcp_server,
158 ARRAY_SIZE(tcp_outgoing),
161 for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
162 client = tcp_outgoing + i;
164 ASSERT(0 == uv_tcp_init(loop, client));
165 ASSERT(0 == uv_tcp_connect(&connect_reqs[i], client, addr, connect_cb));
168 uv_run(loop, UV_RUN_DEFAULT);
170 ASSERT(ARRAY_SIZE(tcp_outgoing) == got_connections);
171 ASSERT((ARRAY_SIZE(tcp_outgoing) + 2) == close_cb_called);
172 ASSERT(ARRAY_SIZE(tcp_outgoing) == write_cb_called);
173 ASSERT(1 == read_cb_called);
175 MAKE_VALGRIND_HAPPY();