|
160
|
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 #include <stdio.h>
|
|
|
25 #include <stdlib.h>
|
|
|
26
|
|
|
27 static uv_timer_t timer;
|
|
|
28 static uv_tcp_t tcp;
|
|
|
29 static uv_connect_t connect_req;
|
|
|
30 static uv_write_t write_req;
|
|
|
31 static uv_shutdown_t shutdown_req;
|
|
|
32 static uv_buf_t qbuf;
|
|
|
33 static int got_q;
|
|
|
34 static int got_eof;
|
|
|
35 static int called_connect_cb;
|
|
|
36 static int called_shutdown_cb;
|
|
|
37 static int called_tcp_close_cb;
|
|
|
38 static int called_timer_close_cb;
|
|
|
39 static int called_timer_cb;
|
|
|
40
|
|
|
41
|
|
|
42 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
|
|
|
43 buf->base = malloc(size);
|
|
|
44 buf->len = size;
|
|
|
45 }
|
|
|
46
|
|
|
47
|
|
|
48 static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) {
|
|
|
49 ASSERT_PTR_EQ((uv_tcp_t*)t, &tcp);
|
|
|
50
|
|
|
51 if (nread == 0) {
|
|
|
52 free(buf->base);
|
|
|
53 return;
|
|
|
54 }
|
|
|
55
|
|
|
56 if (!got_q) {
|
|
|
57 ASSERT_EQ(1, nread);
|
|
|
58 ASSERT(!got_eof);
|
|
|
59 ASSERT_EQ(buf->base[0], 'Q');
|
|
|
60 free(buf->base);
|
|
|
61 got_q = 1;
|
|
|
62 puts("got Q");
|
|
|
63 } else {
|
|
|
64 ASSERT_EQ(nread, UV_EOF);
|
|
|
65 if (buf->base) {
|
|
|
66 free(buf->base);
|
|
|
67 }
|
|
|
68 got_eof = 1;
|
|
|
69 puts("got EOF");
|
|
|
70 }
|
|
|
71 }
|
|
|
72
|
|
|
73
|
|
|
74 static void shutdown_cb(uv_shutdown_t *req, int status) {
|
|
|
75 ASSERT_PTR_EQ(req, &shutdown_req);
|
|
|
76
|
|
|
77 ASSERT_EQ(1, called_connect_cb);
|
|
|
78 ASSERT(!got_eof);
|
|
|
79 ASSERT_OK(called_tcp_close_cb);
|
|
|
80 ASSERT_OK(called_timer_close_cb);
|
|
|
81 ASSERT_OK(called_timer_cb);
|
|
|
82
|
|
|
83 called_shutdown_cb++;
|
|
|
84 }
|
|
|
85
|
|
|
86
|
|
|
87 static void connect_cb(uv_connect_t *req, int status) {
|
|
|
88 ASSERT_OK(status);
|
|
|
89 ASSERT_PTR_EQ(req, &connect_req);
|
|
|
90
|
|
|
91 /* Start reading from our connection so we can receive the EOF. */
|
|
|
92 ASSERT_OK(uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));
|
|
|
93
|
|
|
94 /* Check error handling. */
|
|
|
95 ASSERT_EQ(UV_EALREADY, uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb));
|
|
|
96 ASSERT_EQ(UV_EINVAL, uv_read_start(NULL, alloc_cb, read_cb));
|
|
|
97 ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, NULL, read_cb));
|
|
|
98 ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, alloc_cb, NULL));
|
|
|
99
|
|
|
100 /*
|
|
|
101 * Write the letter 'Q' to gracefully kill the echo-server. This will not
|
|
|
102 * effect our connection.
|
|
|
103 */
|
|
|
104 uv_write(&write_req, (uv_stream_t*) &tcp, &qbuf, 1, NULL);
|
|
|
105
|
|
|
106 /* Shutdown our end of the connection. */
|
|
|
107 uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb);
|
|
|
108
|
|
|
109 called_connect_cb++;
|
|
|
110 ASSERT_OK(called_shutdown_cb);
|
|
|
111 }
|
|
|
112
|
|
|
113
|
|
|
114 static void tcp_close_cb(uv_handle_t* handle) {
|
|
|
115 ASSERT_PTR_EQ(handle, (uv_handle_t*) &tcp);
|
|
|
116
|
|
|
117 ASSERT_EQ(1, called_connect_cb);
|
|
|
118 ASSERT(got_q);
|
|
|
119 ASSERT(got_eof);
|
|
|
120 ASSERT_EQ(1, called_timer_cb);
|
|
|
121
|
|
|
122 called_tcp_close_cb++;
|
|
|
123 }
|
|
|
124
|
|
|
125
|
|
|
126 static void timer_close_cb(uv_handle_t* handle) {
|
|
|
127 ASSERT_PTR_EQ(handle, (uv_handle_t*) &timer);
|
|
|
128 called_timer_close_cb++;
|
|
|
129 }
|
|
|
130
|
|
|
131
|
|
|
132 static void timer_cb(uv_timer_t* handle) {
|
|
|
133 ASSERT_PTR_EQ(handle, &timer);
|
|
|
134 uv_close((uv_handle_t*) handle, timer_close_cb);
|
|
|
135
|
|
|
136 /*
|
|
|
137 * The most important assert of the test: we have not received
|
|
|
138 * tcp_close_cb yet.
|
|
|
139 */
|
|
|
140 ASSERT_OK(called_tcp_close_cb);
|
|
|
141 uv_close((uv_handle_t*) &tcp, tcp_close_cb);
|
|
|
142
|
|
|
143 called_timer_cb++;
|
|
|
144 }
|
|
|
145
|
|
|
146
|
|
|
147 /*
|
|
|
148 * This test has a client which connects to the echo_server and immediately
|
|
|
149 * issues a shutdown. The echo-server, in response, will also shutdown their
|
|
|
150 * connection. We check, with a timer, that libuv is not automatically
|
|
|
151 * calling uv_close when the client receives the EOF from echo-server.
|
|
|
152 */
|
|
|
153 TEST_IMPL(shutdown_eof) {
|
|
|
154 struct sockaddr_in server_addr;
|
|
|
155 int r;
|
|
|
156
|
|
|
157 qbuf.base = "Q";
|
|
|
158 qbuf.len = 1;
|
|
|
159
|
|
|
160 r = uv_timer_init(uv_default_loop(), &timer);
|
|
|
161 ASSERT_OK(r);
|
|
|
162
|
|
|
163 uv_timer_start(&timer, timer_cb, 100, 0);
|
|
|
164
|
|
|
165 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
|
|
|
166 r = uv_tcp_init(uv_default_loop(), &tcp);
|
|
|
167 ASSERT(!r);
|
|
|
168
|
|
|
169 r = uv_tcp_connect(&connect_req,
|
|
|
170 &tcp,
|
|
|
171 (const struct sockaddr*) &server_addr,
|
|
|
172 connect_cb);
|
|
|
173 ASSERT(!r);
|
|
|
174
|
|
|
175 uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
|
|
176
|
|
|
177 ASSERT_EQ(1, called_connect_cb);
|
|
|
178 ASSERT_EQ(1, called_shutdown_cb);
|
|
|
179 ASSERT(got_eof);
|
|
|
180 ASSERT(got_q);
|
|
|
181 ASSERT_EQ(1, called_tcp_close_cb);
|
|
|
182 ASSERT_EQ(1, called_timer_close_cb);
|
|
|
183 ASSERT_EQ(1, called_timer_cb);
|
|
|
184
|
|
|
185 MAKE_VALGRIND_HAPPY(uv_default_loop());
|
|
|
186 return 0;
|
|
|
187 }
|
|
|
188
|