Mercurial
comparison third_party/libuv/test/test-connection-fail.c @ 160:948de3f54cea
[ThirdParty] Added libuv
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 14 Jan 2026 19:39:52 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 159:05cf9467a1c3 | 160:948de3f54cea |
|---|---|
| 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 | |
| 29 static uv_tcp_t tcp; | |
| 30 static uv_connect_t req; | |
| 31 static int connect_cb_calls; | |
| 32 static int close_cb_calls; | |
| 33 | |
| 34 static uv_timer_t timer; | |
| 35 static int timer_close_cb_calls; | |
| 36 static int timer_cb_calls; | |
| 37 | |
| 38 | |
| 39 static void on_close(uv_handle_t* handle) { | |
| 40 close_cb_calls++; | |
| 41 } | |
| 42 | |
| 43 | |
| 44 static void timer_close_cb(uv_handle_t* handle) { | |
| 45 timer_close_cb_calls++; | |
| 46 } | |
| 47 | |
| 48 | |
| 49 static 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_OK(close_cb_calls); | |
| 58 ASSERT_EQ(1, connect_cb_calls); | |
| 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 | |
| 68 static void on_connect_with_close(uv_connect_t *req, int status) { | |
| 69 ASSERT_PTR_EQ((uv_stream_t*) &tcp, req->handle); | |
| 70 ASSERT_EQ(status, UV_ECONNREFUSED); | |
| 71 connect_cb_calls++; | |
| 72 | |
| 73 ASSERT_OK(close_cb_calls); | |
| 74 uv_close((uv_handle_t*)req->handle, on_close); | |
| 75 } | |
| 76 | |
| 77 | |
| 78 static void on_connect_without_close(uv_connect_t *req, int status) { | |
| 79 ASSERT_EQ(status, UV_ECONNREFUSED); | |
| 80 connect_cb_calls++; | |
| 81 | |
| 82 uv_timer_start(&timer, timer_cb, 100, 0); | |
| 83 | |
| 84 ASSERT_OK(close_cb_calls); | |
| 85 } | |
| 86 | |
| 87 | |
| 88 static void connection_fail(uv_connect_cb connect_cb) { | |
| 89 struct sockaddr_in client_addr, server_addr; | |
| 90 int r; | |
| 91 | |
| 92 ASSERT_OK(uv_ip4_addr("0.0.0.0", 0, &client_addr)); | |
| 93 | |
| 94 /* There should be no servers listening on this port. */ | |
| 95 ASSERT_OK(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_OK(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_EQ(1, connect_cb_calls); | |
| 114 ASSERT_EQ(1, close_cb_calls); | |
| 115 } | |
| 116 | |
| 117 | |
| 118 /* | |
| 119 * This test attempts to connect to a port where no server is running. We | |
| 120 * expect an error. | |
| 121 */ | |
| 122 TEST_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_OK(timer_close_cb_calls); | |
| 131 ASSERT_OK(timer_cb_calls); | |
| 132 | |
| 133 MAKE_VALGRIND_HAPPY(uv_default_loop()); | |
| 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 */ | |
| 143 TEST_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_OK(r); | |
| 153 | |
| 154 connection_fail(on_connect_without_close); | |
| 155 | |
| 156 ASSERT_EQ(1, timer_close_cb_calls); | |
| 157 ASSERT_EQ(1, timer_cb_calls); | |
| 158 | |
| 159 MAKE_VALGRIND_HAPPY(uv_default_loop()); | |
| 160 return 0; | |
| 161 } |