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#include <stdlib.h> 26#include <stdio.h> 27 28 29static uv_tcp_t tcp; 30static uv_connect_t req; 31static int connect_cb_calls; 32static int close_cb_calls; 33 34static uv_timer_t timer; 35static int timer_close_cb_calls; 36static int timer_cb_calls; 37 38 39static void on_close(uv_handle_t* handle) { 40 close_cb_calls++; 41} 42 43 44static void timer_close_cb(uv_handle_t* handle) { 45 timer_close_cb_calls++; 46} 47 48 49static void timer_cb(uv_timer_t* handle) { 50 timer_cb_calls++; 51 52 /* 53 * These are the important asserts. The connection callback has been made, 54 * but libuv hasn't automatically closed the socket. The user must 55 * uv_close the handle manually. 56 */ 57 ASSERT(close_cb_calls == 0); 58 ASSERT(connect_cb_calls == 1); 59 60 /* Close the tcp handle. */ 61 uv_close((uv_handle_t*)&tcp, on_close); 62 63 /* Close the timer. */ 64 uv_close((uv_handle_t*)handle, timer_close_cb); 65} 66 67 68static void on_connect_with_close(uv_connect_t *req, int status) { 69 ASSERT((uv_stream_t*) &tcp == req->handle); 70 ASSERT(status == UV_ECONNREFUSED); 71 connect_cb_calls++; 72 73 ASSERT(close_cb_calls == 0); 74 uv_close((uv_handle_t*)req->handle, on_close); 75} 76 77 78static void on_connect_without_close(uv_connect_t *req, int status) { 79 ASSERT(status == UV_ECONNREFUSED); 80 connect_cb_calls++; 81 82 uv_timer_start(&timer, timer_cb, 100, 0); 83 84 ASSERT(close_cb_calls == 0); 85} 86 87 88static void connection_fail(uv_connect_cb connect_cb) { 89 struct sockaddr_in client_addr, server_addr; 90 int r; 91 92 ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &client_addr)); 93 94 /* There should be no servers listening on this port. */ 95 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr)); 96 97 /* Try to connect to the server and do NUM_PINGS ping-pongs. */ 98 r = uv_tcp_init(uv_default_loop(), &tcp); 99 ASSERT(!r); 100 101 /* We are never doing multiple reads/connects at a time anyway. so these 102 * handles can be pre-initialized. */ 103 ASSERT(0 == uv_tcp_bind(&tcp, (const struct sockaddr*) &client_addr, 0)); 104 105 r = uv_tcp_connect(&req, 106 &tcp, 107 (const struct sockaddr*) &server_addr, 108 connect_cb); 109 ASSERT(!r); 110 111 uv_run(uv_default_loop(), UV_RUN_DEFAULT); 112 113 ASSERT(connect_cb_calls == 1); 114 ASSERT(close_cb_calls == 1); 115} 116 117 118/* 119 * This test attempts to connect to a port where no server is running. We 120 * expect an error. 121 */ 122TEST_IMPL(connection_fail) { 123/* TODO(gengjiawen): Fix test on QEMU. */ 124#if defined(__QEMU__) 125 RETURN_SKIP("Test does not currently work in QEMU"); 126#endif 127 128 connection_fail(on_connect_with_close); 129 130 ASSERT(timer_close_cb_calls == 0); 131 ASSERT(timer_cb_calls == 0); 132 133 MAKE_VALGRIND_HAPPY(); 134 return 0; 135} 136 137 138/* 139 * This test is the same as the first except it check that the close 140 * callback of the tcp handle hasn't been made after the failed connection 141 * attempt. 142 */ 143TEST_IMPL(connection_fail_doesnt_auto_close) { 144/* TODO(gengjiawen): Fix test on QEMU. */ 145#if defined(__QEMU__) 146 RETURN_SKIP("Test does not currently work in QEMU"); 147#endif 148 149 int r; 150 151 r = uv_timer_init(uv_default_loop(), &timer); 152 ASSERT(r == 0); 153 154 connection_fail(on_connect_without_close); 155 156 ASSERT(timer_close_cb_calls == 1); 157 ASSERT(timer_cb_calls == 1); 158 159 MAKE_VALGRIND_HAPPY(); 160 return 0; 161} 162