test-tcp-close-reset.c
Go to the documentation of this file.
1 /* Copyright libuv project 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 <errno.h>
26 #include <string.h> /* memset */
27 
28 static uv_loop_t* loop;
35 
36 static int client_close;
38 
39 static int write_cb_called;
40 static int close_cb_called;
41 static int shutdown_cb_called;
42 
43 static void connect_cb(uv_connect_t* req, int status);
44 static void write_cb(uv_write_t* req, int status);
45 static void close_cb(uv_handle_t* handle);
46 static void shutdown_cb(uv_shutdown_t* req, int status);
47 
48 static int read_size;
49 
50 
51 static void do_write(uv_tcp_t* handle) {
52  uv_buf_t buf;
53  unsigned i;
54  int r;
55 
56  buf = uv_buf_init("PING", 4);
57  for (i = 0; i < ARRAY_SIZE(write_reqs); i++) {
59  ASSERT(r == 0);
60  }
61 }
62 
63 
64 static void do_close(uv_tcp_t* handle) {
65  if (shutdown_before_close == 1) {
67  ASSERT(UV_EINVAL == uv_tcp_close_reset(handle, close_cb));
68  } else {
71  }
72 
73  uv_close((uv_handle_t*) &tcp_server, NULL);
74 }
75 
76 static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
77  static char slab[1024];
78  buf->base = slab;
79  buf->len = sizeof(slab);
80 }
81 
82 static void read_cb2(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
84  if (nread == UV_EOF)
85  uv_close((uv_handle_t*) stream, NULL);
86 }
87 
88 
89 static void connect_cb(uv_connect_t* conn_req, int status) {
93  if (client_close)
95 }
96 
97 
98 static void write_cb(uv_write_t* req, int status) {
99  /* write callbacks should run before the close callback */
100  ASSERT(close_cb_called == 0);
102  write_cb_called++;
103 }
104 
105 
106 static void close_cb(uv_handle_t* handle) {
107  if (client_close)
109  else
111 
112  close_cb_called++;
113 }
114 
115 static void shutdown_cb(uv_shutdown_t* req, int status) {
116  if (client_close)
118  else
120 
122 }
123 
124 
125 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
127  if (nread < 0) {
128  uv_close((uv_handle_t*) stream, NULL);
129  } else {
130  read_size += nread;
131  if (read_size == 16 && client_close == 0)
133  }
134 }
135 
136 
138  ASSERT(status == 0);
139 
142 
144 }
145 
146 
148  struct sockaddr_in addr;
149  int r;
150 
151  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
152 
153  r = uv_tcp_init(loop, handle);
154  ASSERT(r == 0);
155 
156  r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0);
157  ASSERT(r == 0);
158 
160  ASSERT(r == 0);
161 }
162 
163 
165  struct sockaddr_in addr;
166  int r;
167 
168  ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
169 
171  ASSERT(r == 0);
172 
174  tcp_client,
175  (const struct sockaddr*) &addr,
176  connect_cb);
177  ASSERT(r == 0);
178 }
179 
180 
181 /* Check that pending write requests have their callbacks
182  * invoked when the handle is closed.
183  */
184 TEST_IMPL(tcp_close_reset_client) {
185  int r;
186 
187  loop = uv_default_loop();
188 
190 
191  client_close = 1;
193 
195 
196  ASSERT(write_cb_called == 0);
197  ASSERT(close_cb_called == 0);
199 
201  ASSERT(r == 0);
202 
203  ASSERT(write_cb_called == 4);
204  ASSERT(close_cb_called == 1);
206 
208  return 0;
209 }
210 
211 TEST_IMPL(tcp_close_reset_client_after_shutdown) {
212  int r;
213 
214  loop = uv_default_loop();
215 
217 
218  client_close = 1;
220 
222 
223  ASSERT(write_cb_called == 0);
224  ASSERT(close_cb_called == 0);
226 
228  ASSERT(r == 0);
229 
230  ASSERT(write_cb_called == 4);
231  ASSERT(close_cb_called == 0);
233 
235  return 0;
236 }
237 
238 TEST_IMPL(tcp_close_reset_accepted) {
239  int r;
240 
241  loop = uv_default_loop();
242 
244 
245  client_close = 0;
247 
249 
250  ASSERT(write_cb_called == 0);
251  ASSERT(close_cb_called == 0);
253 
255  ASSERT(r == 0);
256 
257  ASSERT(write_cb_called == 4);
258  ASSERT(close_cb_called == 1);
260 
262  return 0;
263 }
264 
265 TEST_IMPL(tcp_close_reset_accepted_after_shutdown) {
266  int r;
267 
268  loop = uv_default_loop();
269 
271 
272  client_close = 0;
274 
276 
277  ASSERT(write_cb_called == 0);
278  ASSERT(close_cb_called == 0);
280 
282  ASSERT(r == 0);
283 
284  ASSERT(write_cb_called == 4);
285  ASSERT(close_cb_called == 0);
287 
289  return 0;
290 }
write_cb_called
static int write_cb_called
Definition: test-tcp-close-reset.c:39
slab
static char slab[1]
Definition: test-watcher-cross-stop.c:37
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
tcp_server
static uv_tcp_t tcp_server
Definition: test-tcp-close-reset.c:29
uv_connect_s
Definition: uv.h:580
read_cb
static void read_cb(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: test-tcp-close-reset.c:125
uv_shutdown_s
Definition: uv.h:417
string.h
read_cb2
static void read_cb2(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
Definition: test-tcp-close-reset.c:82
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
uv_listen
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: unix/stream.c:656
uv_connect_s::handle
uv_stream_t * handle
Definition: uv.h:583
ASSERT
#define ASSERT(expr)
Definition: task.h:102
status
absl::Status status
Definition: rls.cc:251
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
shutdown_cb_called
static int shutdown_cb_called
Definition: test-tcp-close-reset.c:41
start_server
static void start_server(uv_loop_t *loop, uv_tcp_t *handle)
Definition: test-tcp-close-reset.c:147
uv_tcp_bind
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
TEST_PORT
#define TEST_PORT
Definition: task.h:53
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
uv_tcp_connect
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:315
uv_ip4_addr
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
shutdown_cb
static void shutdown_cb(uv_shutdown_t *req, int status)
Definition: test-tcp-close-reset.c:115
TEST_IMPL
TEST_IMPL(tcp_close_reset_client)
Definition: test-tcp-close-reset.c:184
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
conn_req
static uv_connect_t conn_req
Definition: test-pipe-connect-prepare.c:41
uv_write
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: unix/stream.c:1492
req
static uv_connect_t req
Definition: test-connection-fail.c:30
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_tcp_close_reset
UV_EXTERN int uv_tcp_close_reset(uv_tcp_t *handle, uv_close_cb close_cb)
Definition: unix/tcp.c:311
uv_shutdown
UV_PRIVATE_REQ_TYPES UV_EXTERN int uv_shutdown(uv_shutdown_t *req, uv_stream_t *handle, uv_shutdown_cb cb)
Definition: unix/stream.c:1259
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
do_connect
static void do_connect(uv_loop_t *loop, uv_tcp_t *tcp_client)
Definition: test-tcp-close-reset.c:164
uv_tcp_init
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: unix/tcp.c:143
uv_accept
UV_EXTERN int uv_accept(uv_stream_t *server, uv_stream_t *client)
Definition: unix/stream.c:591
loop
static uv_loop_t * loop
Definition: test-tcp-close-reset.c:28
shutdown_req
static uv_shutdown_t shutdown_req
Definition: test-tcp-close-reset.c:33
tcp_accepted
static uv_tcp_t tcp_accepted
Definition: test-tcp-close-reset.c:31
close_cb_called
static int close_cb_called
Definition: test-tcp-close-reset.c:40
uv_tcp_s
Definition: uv.h:544
shutdown_before_close
static int shutdown_before_close
Definition: test-tcp-close-reset.c:37
uv.h
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-tcp-close-reset.c:106
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_buf_t
Definition: unix.h:121
connection_cb
static void connection_cb(uv_stream_t *server, int status)
Definition: test-tcp-close-reset.c:137
do_close
static void do_close(uv_tcp_t *handle)
Definition: test-tcp-close-reset.c:64
server
Definition: examples/python/async_streaming/server.py:1
connect_req
static uv_connect_t connect_req
Definition: test-tcp-close-reset.c:32
connect_cb
static void connect_cb(uv_connect_t *req, int status)
Definition: test-tcp-close-reset.c:89
fix_build_deps.r
r
Definition: fix_build_deps.py:491
alloc_cb
static void alloc_cb(uv_handle_t *handle, size_t size, uv_buf_t *buf)
Definition: test-tcp-close-reset.c:76
uv_buf_init
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
client_close
static int client_close
Definition: test-tcp-close-reset.c:36
uv_write_s
Definition: uv.h:522
write_cb
static void write_cb(uv_write_t *req, int status)
Definition: test-tcp-close-reset.c:98
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
do_write
static void do_write(uv_tcp_t *handle)
Definition: test-tcp-close-reset.c:51
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
read_size
static int read_size
Definition: test-tcp-close-reset.c:48
tcp_client
static uv_tcp_t tcp_client
Definition: test-tcp-close-reset.c:30
addr
struct sockaddr_in addr
Definition: libuv/docs/code/tcp-echo-server/main.c:10
errno.h
write_reqs
static uv_write_t write_reqs[4]
Definition: test-tcp-close-reset.c:34
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:30