1/* Copyright libuv project and 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 25static uv_tcp_t client; 26static uv_tcp_t connection; 27static uv_connect_t connect_req; 28static uv_timer_t timer; 29 30static int read_cb_called; 31static int on_close_called; 32 33static void on_connection(uv_stream_t* server, int status); 34 35static void on_client_connect(uv_connect_t* req, int status); 36static void on_client_alloc(uv_handle_t* handle, 37 size_t suggested_size, 38 uv_buf_t* buf); 39static void on_client_read(uv_stream_t* stream, 40 ssize_t nread, 41 const uv_buf_t* buf); 42static void on_client_timeout(uv_timer_t* handle); 43 44static void on_close(uv_handle_t* handle); 45 46 47static void on_client_connect(uv_connect_t* conn_req, int status) { 48 int r; 49 50 r = uv_read_start((uv_stream_t*) &client, on_client_alloc, on_client_read); 51 ASSERT_EQ(r, 0); 52 53 r = uv_timer_start(&timer, on_client_timeout, 1000, 0); 54 ASSERT_EQ(r, 0); 55} 56 57 58static void on_client_alloc(uv_handle_t* handle, 59 size_t suggested_size, 60 uv_buf_t* buf) { 61 static char slab[8]; 62 buf->base = slab; 63 buf->len = sizeof(slab); 64} 65 66 67static void on_client_read(uv_stream_t* stream, ssize_t nread, 68 const uv_buf_t* buf) { 69 ASSERT_LT(nread, 0); 70 read_cb_called++; 71} 72 73 74static void on_client_timeout(uv_timer_t* handle) { 75 ASSERT_EQ(handle, &timer); 76 ASSERT_EQ(read_cb_called, 0); 77 uv_read_stop((uv_stream_t*) &client); 78 uv_close((uv_handle_t*) &client, on_close); 79 uv_close((uv_handle_t*) &timer, on_close); 80} 81 82 83static void on_connection_alloc(uv_handle_t* handle, 84 size_t suggested_size, 85 uv_buf_t* buf) { 86 static char slab[8]; 87 buf->base = slab; 88 buf->len = sizeof(slab); 89} 90 91 92static void on_connection_read(uv_stream_t* stream, 93 ssize_t nread, 94 const uv_buf_t* buf) { 95 ASSERT_EQ(nread, UV_EOF); 96 read_cb_called++; 97 uv_close((uv_handle_t*) stream, on_close); 98} 99 100 101static void on_connection(uv_stream_t* server, int status) { 102 int r; 103 104 ASSERT_EQ(status, 0); 105 ASSERT_EQ(uv_accept(server, (uv_stream_t*) &connection), 0); 106 107 r = uv_read_start((uv_stream_t*) &connection, 108 on_connection_alloc, 109 on_connection_read); 110 ASSERT_EQ(r, 0); 111} 112 113 114static void on_close(uv_handle_t* handle) { 115 ASSERT(handle == (uv_handle_t*) &client || 116 handle == (uv_handle_t*) &connection || 117 handle == (uv_handle_t*) &timer); 118 on_close_called++; 119} 120 121 122static void start_server(uv_loop_t* loop, uv_tcp_t* handle) { 123 struct sockaddr_in addr; 124 int r; 125 126 ASSERT_EQ(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr), 0); 127 128 r = uv_tcp_init(loop, handle); 129 ASSERT_EQ(r, 0); 130 131 r = uv_tcp_bind(handle, (const struct sockaddr*) &addr, 0); 132 ASSERT_EQ(r, 0); 133 134 r = uv_listen((uv_stream_t*) handle, 128, on_connection); 135 ASSERT_EQ(r, 0); 136 137 uv_unref((uv_handle_t*) handle); 138} 139 140 141/* Check that pending write requests have their callbacks 142 * invoked when the handle is closed. 143 */ 144TEST_IMPL(tcp_close_after_read_timeout) { 145 struct sockaddr_in addr; 146 uv_tcp_t tcp_server; 147 uv_loop_t* loop; 148 int r; 149 150 ASSERT_EQ(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr), 0); 151 152 loop = uv_default_loop(); 153 154 /* We can't use the echo server, it doesn't handle ECONNRESET. */ 155 start_server(loop, &tcp_server); 156 157 r = uv_tcp_init(loop, &client); 158 ASSERT_EQ(r, 0); 159 160 r = uv_tcp_connect(&connect_req, 161 &client, 162 (const struct sockaddr*) &addr, 163 on_client_connect); 164 ASSERT_EQ(r, 0); 165 166 r = uv_tcp_init(loop, &connection); 167 ASSERT_EQ(r, 0); 168 169 r = uv_timer_init(loop, &timer); 170 ASSERT_EQ(r, 0); 171 172 ASSERT_EQ(read_cb_called, 0); 173 ASSERT_EQ(on_close_called, 0); 174 175 r = uv_run(loop, UV_RUN_DEFAULT); 176 ASSERT_EQ(r, 0); 177 178 ASSERT_EQ(read_cb_called, 1); 179 ASSERT_EQ(on_close_called, 3); 180 181 MAKE_VALGRIND_HAPPY(); 182 return 0; 183} 184