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 25static void write_cb(uv_write_t* req, int status); 26static void shutdown_cb(uv_shutdown_t* req, int status); 27 28static uv_tcp_t conn; 29static uv_timer_t timer; 30static uv_connect_t connect_req; 31static uv_write_t write_req; 32static uv_shutdown_t shutdown_req; 33 34static int connect_cb_called; 35static int write_cb_called; 36static int shutdown_cb_called; 37 38static int conn_close_cb_called; 39static int timer_close_cb_called; 40 41 42static 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 52static 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 61static 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_OK(r); 70 71 r = uv_shutdown(&shutdown_req, (uv_stream_t*)&conn, shutdown_cb); 72 ASSERT_OK(r); 73} 74 75 76static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { 77} 78 79 80static void connect_cb(uv_connect_t* req, int status) { 81 int r; 82 83 ASSERT_OK(status); 84 connect_cb_called++; 85 86 r = uv_read_start((uv_stream_t*)&conn, alloc_cb, read_cb); 87 ASSERT_OK(r); 88} 89 90 91static void write_cb(uv_write_t* req, int status) { 92 ASSERT_OK(status); 93 write_cb_called++; 94} 95 96 97static void shutdown_cb(uv_shutdown_t* req, int status) { 98 ASSERT_OK(status); 99 shutdown_cb_called++; 100 uv_close((uv_handle_t*)&conn, close_cb); 101} 102 103 104TEST_IMPL(tcp_shutdown_after_write) { 105 struct sockaddr_in addr; 106 uv_loop_t* loop; 107 int r; 108 109 ASSERT_OK(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_OK(r); 114 115 r = uv_timer_start(&timer, timer_cb, 125, 0); 116 ASSERT_OK(r); 117 118 r = uv_tcp_init(loop, &conn); 119 ASSERT_OK(r); 120 121 r = uv_tcp_connect(&connect_req, 122 &conn, 123 (const struct sockaddr*) &addr, 124 connect_cb); 125 ASSERT_OK(r); 126 127 r = uv_run(loop, UV_RUN_DEFAULT); 128 ASSERT_OK(r); 129 130 ASSERT_EQ(1, connect_cb_called); 131 ASSERT_EQ(1, write_cb_called); 132 ASSERT_EQ(1, shutdown_cb_called); 133 ASSERT_EQ(1, conn_close_cb_called); 134 ASSERT_EQ(1, timer_close_cb_called); 135 136 MAKE_VALGRIND_HAPPY(loop); 137 return 0; 138} 139