Mercurial
comparison third_party/libuv/test/test-tcp-rst.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 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 | |
| 25 static uv_tcp_t tcp; | |
| 26 static uv_connect_t connect_req; | |
| 27 static uv_buf_t qbuf; | |
| 28 static int called_alloc_cb; | |
| 29 static int called_connect_cb; | |
| 30 static int called_close_cb; | |
| 31 | |
| 32 | |
| 33 static void close_cb(uv_handle_t* handle) { | |
| 34 ASSERT_PTR_EQ(handle, (uv_handle_t*) &tcp); | |
| 35 called_close_cb++; | |
| 36 } | |
| 37 | |
| 38 | |
| 39 static 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 | |
| 46 static 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_OK(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 | |
| 60 static void connect_cb(uv_connect_t *req, int status) { | |
| 61 ASSERT_OK(status); | |
| 62 ASSERT_PTR_EQ(req, &connect_req); | |
| 63 | |
| 64 /* Start reading from the connection so we receive the RST in uv__read. */ | |
| 65 ASSERT_OK(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 */ | |
| 78 TEST_IMPL(tcp_rst) { | |
| 79 #if defined(__OpenBSD__) | |
| 80 RETURN_SKIP("Test does not currently work in OpenBSD"); | |
| 81 #endif | |
| 82 #ifndef _WIN32 | |
| 83 struct sockaddr_in server_addr; | |
| 84 int r; | |
| 85 | |
| 86 qbuf.base = "QSH"; | |
| 87 qbuf.len = 3; | |
| 88 | |
| 89 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr)); | |
| 90 r = uv_tcp_init(uv_default_loop(), &tcp); | |
| 91 ASSERT_OK(r); | |
| 92 | |
| 93 r = uv_tcp_connect(&connect_req, | |
| 94 &tcp, | |
| 95 (const struct sockaddr*) &server_addr, | |
| 96 connect_cb); | |
| 97 ASSERT_OK(r); | |
| 98 | |
| 99 uv_run(uv_default_loop(), UV_RUN_DEFAULT); | |
| 100 | |
| 101 ASSERT_EQ(1, called_alloc_cb); | |
| 102 ASSERT_EQ(1, called_connect_cb); | |
| 103 ASSERT_EQ(1, called_close_cb); | |
| 104 | |
| 105 MAKE_VALGRIND_HAPPY(uv_default_loop()); | |
| 106 return 0; | |
| 107 #else | |
| 108 RETURN_SKIP("Unix only test"); | |
| 109 #endif | |
| 110 } |