libuv-svace-build
110 строк · 3.2 Кб
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
25static uv_tcp_t tcp;
26static uv_connect_t connect_req;
27static uv_buf_t qbuf;
28static int called_alloc_cb;
29static int called_connect_cb;
30static int called_close_cb;
31
32
33static void close_cb(uv_handle_t* handle) {
34ASSERT_PTR_EQ(handle, (uv_handle_t*) &tcp);
35called_close_cb++;
36}
37
38
39static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
40buf->base = malloc(size);
41buf->len = size;
42called_alloc_cb++;
43}
44
45
46static void read_cb(uv_stream_t* t, ssize_t nread, const uv_buf_t* buf) {
47ASSERT_PTR_EQ((uv_tcp_t*) t, &tcp);
48ASSERT_EQ(nread, UV_ECONNRESET);
49
50int fd;
51ASSERT_OK(uv_fileno((uv_handle_t*) t, &fd));
52uv_handle_type type = uv_guess_handle(fd);
53ASSERT_EQ(type, UV_TCP);
54
55uv_close((uv_handle_t *) t, close_cb);
56free(buf->base);
57}
58
59
60static void connect_cb(uv_connect_t *req, int status) {
61ASSERT_OK(status);
62ASSERT_PTR_EQ(req, &connect_req);
63
64/* Start reading from the connection so we receive the RST in uv__read. */
65ASSERT_OK(uv_read_start((uv_stream_t*) &tcp, alloc_cb, read_cb));
66
67/* Write 'QSH' to receive RST from the echo server. */
68ASSERT_EQ(qbuf.len, uv_try_write((uv_stream_t*) &tcp, &qbuf, 1));
69
70called_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*/
78TEST_IMPL(tcp_rst) {
79#if defined(__OpenBSD__)
80RETURN_SKIP("Test does not currently work in OpenBSD");
81#endif
82#ifndef _WIN32
83struct sockaddr_in server_addr;
84int r;
85
86qbuf.base = "QSH";
87qbuf.len = 3;
88
89ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &server_addr));
90r = uv_tcp_init(uv_default_loop(), &tcp);
91ASSERT_OK(r);
92
93r = uv_tcp_connect(&connect_req,
94&tcp,
95(const struct sockaddr*) &server_addr,
96connect_cb);
97ASSERT_OK(r);
98
99uv_run(uv_default_loop(), UV_RUN_DEFAULT);
100
101ASSERT_EQ(1, called_alloc_cb);
102ASSERT_EQ(1, called_connect_cb);
103ASSERT_EQ(1, called_close_cb);
104
105MAKE_VALGRIND_HAPPY(uv_default_loop());
106return 0;
107#else
108RETURN_SKIP("Unix only test");
109#endif
110}
111