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 static void write_cb(uv_write_t* req, int status);
26 static void shutdown_cb(uv_shutdown_t* req, int status);
27
28 static uv_tcp_t conn;
29 static uv_timer_t timer;
30 static uv_connect_t connect_req;
31 static uv_write_t write_req;
32 static uv_shutdown_t shutdown_req;
33
34 static int connect_cb_called;
35 static int write_cb_called;
36 static int shutdown_cb_called;
37
38 static int conn_close_cb_called;
39 static int timer_close_cb_called;
40
41
close_cb(uv_handle_t * handle)42 static void close_cb(uv_handle_t* handle) {
43 if (handle == (uv_handle_t*)&conn)
44 conn_close_cb_called++;
45 else if (handle == (uv_handle_t*)&timer)
46 timer_close_cb_called++;
47 else
48 ASSERT(0 && "bad handle in close_cb");
49 }
50
51
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)52 static void alloc_cb(uv_handle_t* handle,
53 size_t suggested_size,
54 uv_buf_t* buf) {
55 static char slab[64];
56 buf->base = slab;
57 buf->len = sizeof(slab);
58 }
59
60
timer_cb(uv_timer_t * handle)61 static void timer_cb(uv_timer_t* handle) {
62 uv_buf_t buf;
63 int r;
64
65 uv_close((uv_handle_t*)handle, close_cb);
66
67 buf = uv_buf_init("TEST", 4);
68 r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
69 ASSERT(r == 0);
70
71 r = uv_shutdown(&shutdown_req, (uv_stream_t*)&conn, shutdown_cb);
72 ASSERT(r == 0);
73 }
74
75
read_cb(uv_stream_t * handle,ssize_t nread,const uv_buf_t * buf)76 static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
77 }
78
79
connect_cb(uv_connect_t * req,int status)80 static void connect_cb(uv_connect_t* req, int status) {
81 int r;
82
83 ASSERT(status == 0);
84 connect_cb_called++;
85
86 r = uv_read_start((uv_stream_t*)&conn, alloc_cb, read_cb);
87 ASSERT(r == 0);
88 }
89
90
write_cb(uv_write_t * req,int status)91 static void write_cb(uv_write_t* req, int status) {
92 ASSERT(status == 0);
93 write_cb_called++;
94 }
95
96
shutdown_cb(uv_shutdown_t * req,int status)97 static void shutdown_cb(uv_shutdown_t* req, int status) {
98 ASSERT(status == 0);
99 shutdown_cb_called++;
100 uv_close((uv_handle_t*)&conn, close_cb);
101 }
102
103
TEST_IMPL(tcp_shutdown_after_write)104 TEST_IMPL(tcp_shutdown_after_write) {
105 struct sockaddr_in addr;
106 uv_loop_t* loop;
107 int r;
108
109 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
110 loop = uv_default_loop();
111
112 r = uv_timer_init(loop, &timer);
113 ASSERT(r == 0);
114
115 r = uv_timer_start(&timer, timer_cb, 125, 0);
116 ASSERT(r == 0);
117
118 r = uv_tcp_init(loop, &conn);
119 ASSERT(r == 0);
120
121 r = uv_tcp_connect(&connect_req,
122 &conn,
123 (const struct sockaddr*) &addr,
124 connect_cb);
125 ASSERT(r == 0);
126
127 r = uv_run(loop, UV_RUN_DEFAULT);
128 ASSERT(r == 0);
129
130 ASSERT(connect_cb_called == 1);
131 ASSERT(write_cb_called == 1);
132 ASSERT(shutdown_cb_called == 1);
133 ASSERT(conn_close_cb_called == 1);
134 ASSERT(timer_close_cb_called == 1);
135
136 MAKE_VALGRIND_HAPPY();
137 return 0;
138 }
139