uv: Upgrade to v0.10.19
[platform/upstream/nodejs.git] / deps / uv / test / test-tcp-close-accept.c
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
41 static void close_cb(uv_handle_t* handle) {
42   close_cb_called++;
43 }
44
45 static void write_cb(uv_write_t* req, int status) {
46   ASSERT(status == 0);
47   write_cb_called++;
48 }
49
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
74 static uv_buf_t alloc_cb(uv_handle_t* handle, size_t suggested_size) {
75   static char buf[1];
76
77   return uv_buf_init(buf, sizeof(buf));
78 }
79
80 static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t b) {
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, &tcp_check, addr, connect_cb));
99   ASSERT(0 == uv_read_start((uv_stream_t*) &tcp_check, alloc_cb, read_cb));
100
101   /* Close server, so no one will connect to it */
102   uv_close((uv_handle_t*) &tcp_server, close_cb);
103 }
104
105 static void connection_cb(uv_stream_t* server, int status) {
106   unsigned int i;
107   uv_tcp_t* incoming;
108
109   ASSERT(server == (uv_stream_t*) &tcp_server);
110
111   /* Ignore tcp_check connection */
112   if (got_connections == ARRAY_SIZE(tcp_incoming))
113     return;
114
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));
119
120   if (got_connections != ARRAY_SIZE(tcp_incoming))
121     return;
122
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));
127   }
128 }
129
130 TEST_IMPL(tcp_close_accept) {
131   unsigned int i;
132   uv_loop_t* loop;
133   uv_tcp_t* client;
134
135   /*
136    * A little explanation of what goes on below:
137    *
138    * We'll create server and connect to it using two clients, each writing one
139    * byte once connected.
140    *
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).
145    *
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
149    * status.
150    */
151
152   loop = uv_default_loop();
153   addr = uv_ip4_addr("0.0.0.0", TEST_PORT);
154
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),
159                         connection_cb));
160
161   for (i = 0; i < ARRAY_SIZE(tcp_outgoing); i++) {
162     client = tcp_outgoing + i;
163
164     ASSERT(0 == uv_tcp_init(loop, client));
165     ASSERT(0 == uv_tcp_connect(&connect_reqs[i], client, addr, connect_cb));
166   }
167
168   uv_run(loop, UV_RUN_DEFAULT);
169
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);
174
175   MAKE_VALGRIND_HAPPY();
176   return 0;
177 }