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#include <stdio.h> 25#include <stdlib.h> 26 27static uv_tcp_t tcp; 28static uv_connect_t connect_req; 29static uv_shutdown_t shutdown_req; 30static uv_buf_t qbuf; 31static int got_q; 32static int got_eof; 33static int called_connect_cb; 34static int called_shutdown_cb; 35static int called_tcp_close_cb; 36 37 38static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) { 39 buf->base = malloc(size); 40 buf->len = size; 41} 42 43 44static void shutdown_cb(uv_shutdown_t *req, int status) { 45 ASSERT_PTR_EQ(req, &shutdown_req); 46 47 ASSERT_EQ(called_connect_cb, 1); 48 ASSERT_EQ(called_tcp_close_cb, 0); 49} 50 51 52static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) { 53 ASSERT_PTR_EQ((uv_tcp_t*)t, &tcp); 54 55 if (nread == 0) { 56 free(buf->base); 57 return; 58 } 59 60 if (!got_q) { 61 ASSERT_EQ(nread, 4); 62 ASSERT_EQ(got_eof, 0); 63 ASSERT_MEM_EQ(buf->base, "QQSS", 4); 64 free(buf->base); 65 got_q = 1; 66 puts("got QQSS"); 67 /* Shutdown our end of the connection simultaneously */ 68 uv_shutdown(&shutdown_req, (uv_stream_t*) &tcp, shutdown_cb); 69 called_shutdown_cb++; 70 } else { 71 ASSERT_EQ(nread, UV_EOF); 72 if (buf->base) { 73 free(buf->base); 74 } 75 got_eof = 1; 76 puts("got EOF"); 77 } 78} 79 80 81static void connect_cb(uv_connect_t *req, int status) { 82 ASSERT_EQ(status, 0); 83 ASSERT_PTR_EQ(req, &connect_req); 84 85 /* Start reading from our connection so we can receive the EOF. */ 86 ASSERT_EQ(0, uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb)); 87 88 /* Check error handling. */ 89 ASSERT_EQ(UV_EALREADY, uv_read_start((uv_stream_t*)&tcp, alloc_cb, read_cb)); 90 ASSERT_EQ(UV_EINVAL, uv_read_start(NULL, alloc_cb, read_cb)); 91 ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, NULL, read_cb)); 92 ASSERT_EQ(UV_EINVAL, uv_read_start((uv_stream_t*)&tcp, alloc_cb, NULL)); 93 94 /* 95 * Write the letter 'Q' and 'QSS` to gracefully kill the echo-server. This 96 * will not effect our connection. 97 */ 98 ASSERT_EQ(qbuf.len, uv_try_write((uv_stream_t*) &tcp, &qbuf, 1)); 99 100 called_connect_cb++; 101 ASSERT_EQ(called_shutdown_cb, 0); 102} 103 104 105/* 106 * This test has a client which connects to the echo_server and immediately 107 * issues a shutdown. We check that this does not cause libuv to hang. 108 */ 109TEST_IMPL(shutdown_simultaneous) { 110 struct sockaddr_in server_addr; 111 int r; 112 113 qbuf.base = "QQSS"; 114 qbuf.len = 4; 115 116 ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr)); 117 r = uv_tcp_init(uv_default_loop(), &tcp); 118 ASSERT_EQ(r, 0); 119 120 r = uv_tcp_connect(&connect_req, 121 &tcp, 122 (const struct sockaddr*) &server_addr, 123 connect_cb); 124 ASSERT_EQ(r, 0); 125 126 uv_run(uv_default_loop(), UV_RUN_DEFAULT); 127 128 ASSERT_EQ(called_connect_cb, 1); 129 ASSERT_EQ(called_shutdown_cb, 1); 130 ASSERT_EQ(got_eof, 1); 131 ASSERT_EQ(got_q, 1); 132 133 MAKE_VALGRIND_HAPPY(); 134 return 0; 135} 136