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 tcp; 26static uv_connect_t connect_req; 27static uv_buf_t qbuf; 28static int called_alloc_cb; 29static int called_connect_cb; 30static int called_close_cb; 31 32 33static void close_cb(uv_handle_t* handle) { 34 ASSERT(handle == (uv_handle_t*) &tcp); 35 called_close_cb++; 36} 37 38 39static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) { 40 buf->base = malloc(size); 41 buf->len = size; 42 called_alloc_cb++; 43} 44 45 46static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) { 47 ASSERT_PTR_EQ((uv_tcp_t*) t, &tcp); 48 ASSERT_EQ(nread, UV_ECONNRESET); 49 50 int fd; 51 ASSERT_EQ(0, uv_fileno((uv_handle_t*) t, &fd)); 52 uv_handle_type type = uv_guess_handle(fd); 53 ASSERT_EQ(type, UV_TCP); 54 55 uv_close((uv_handle_t *) t, close_cb); 56 free(buf->base); 57} 58 59 60static void connect_cb(uv_connect_t *req, int status) { 61 ASSERT_EQ(status, 0); 62 ASSERT_PTR_EQ(req, &connect_req); 63 64 /* Start reading from the connection so we receive the RST in uv__read. */ 65 ASSERT_EQ(0, uv_read_start((uv_stream_t*) &tcp, alloc_cb, read_cb)); 66 67 /* Write 'QSH' to receive RST from the echo server. */ 68 ASSERT_EQ(qbuf.len, uv_try_write((uv_stream_t*) &tcp, &qbuf, 1)); 69 70 called_connect_cb++; 71} 72 73 74/* 75 * This test has a client which connects to the echo_server and receives TCP 76 * RST. Test checks that uv_guess_handle still works on a reset TCP handle. 77 */ 78TEST_IMPL(tcp_rst) { 79#ifndef _WIN32 80 struct sockaddr_in server_addr; 81 int r; 82 83 qbuf.base = "QSH"; 84 qbuf.len = 3; 85 86 ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr)); 87 r = uv_tcp_init(uv_default_loop(), &tcp); 88 ASSERT_EQ(r, 0); 89 90 r = uv_tcp_connect(&connect_req, 91 &tcp, 92 (const struct sockaddr*) &server_addr, 93 connect_cb); 94 ASSERT_EQ(r, 0); 95 96 uv_run(uv_default_loop(), UV_RUN_DEFAULT); 97 98 ASSERT_EQ(called_alloc_cb, 1); 99 ASSERT_EQ(called_connect_cb, 1); 100 ASSERT_EQ(called_close_cb, 1); 101 102 MAKE_VALGRIND_HAPPY(); 103 return 0; 104#else 105 RETURN_SKIP("Unix only test"); 106#endif 107} 108