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