libuv-svace-build
196 строк · 5.1 Кб
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#include <stdio.h>
25#include <stdlib.h>
26
27
28static int connect_cb_called;
29static int close_cb_called;
30
31static uv_connect_t connect_req;
32static uv_timer_t timer;
33static uv_tcp_t conn;
34
35static void connect_cb(uv_connect_t* req, int status);
36static void timer_cb(uv_timer_t* handle);
37static void close_cb(uv_handle_t* handle);
38
39
40static void connect_cb(uv_connect_t* req, int status) {
41ASSERT_PTR_EQ(req, &connect_req);
42ASSERT_EQ(status, UV_ECANCELED);
43connect_cb_called++;
44}
45
46
47static void timer_cb(uv_timer_t* handle) {
48ASSERT_PTR_EQ(handle, &timer);
49uv_close((uv_handle_t*)&conn, close_cb);
50uv_close((uv_handle_t*)&timer, close_cb);
51}
52
53
54static void close_cb(uv_handle_t* handle) {
55ASSERT(handle == (uv_handle_t*)&conn || handle == (uv_handle_t*)&timer);
56close_cb_called++;
57}
58
59
60/* Verify that connecting to an unreachable address or port doesn't hang
61* the event loop.
62*/
63TEST_IMPL(tcp_connect_timeout) {
64struct sockaddr_in addr;
65int r;
66
67ASSERT_OK(uv_ip4_addr("8.8.8.8", 9999, &addr));
68
69r = uv_timer_init(uv_default_loop(), &timer);
70ASSERT_OK(r);
71
72r = uv_timer_start(&timer, timer_cb, 50, 0);
73ASSERT_OK(r);
74
75r = uv_tcp_init(uv_default_loop(), &conn);
76ASSERT_OK(r);
77
78r = uv_tcp_connect(&connect_req,
79&conn,
80(const struct sockaddr*) &addr,
81connect_cb);
82if (r == UV_ENETUNREACH)
83RETURN_SKIP("Network unreachable.");
84ASSERT_OK(r);
85
86r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
87ASSERT_OK(r);
88
89MAKE_VALGRIND_HAPPY(uv_default_loop());
90return 0;
91}
92
93/* Make sure connect fails instantly if the target is nonexisting
94* local port.
95*/
96
97static void connect_local_cb(uv_connect_t* req, int status) {
98ASSERT_PTR_EQ(req, &connect_req);
99ASSERT_NE(status, UV_ECANCELED);
100connect_cb_called++;
101}
102
103static int is_supported_system(void) {
104int semver[3];
105int min_semver[3] = {10, 0, 16299};
106int cnt;
107uv_utsname_t uname;
108ASSERT_OK(uv_os_uname(&uname));
109if (strcmp(uname.sysname, "Windows_NT") == 0) {
110cnt = sscanf(uname.release, "%d.%d.%d", &semver[0], &semver[1], &semver[2]);
111if (cnt != 3) {
112return 0;
113}
114/* release >= 10.0.16299 */
115for (cnt = 0; cnt < 3; ++cnt) {
116if (semver[cnt] > min_semver[cnt])
117return 1;
118if (semver[cnt] < min_semver[cnt])
119return 0;
120}
121return 1;
122}
123return 1;
124}
125
126TEST_IMPL(tcp_local_connect_timeout) {
127struct sockaddr_in addr;
128int r;
129
130if (!is_supported_system()) {
131RETURN_SKIP("Unsupported system");
132}
133ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
134
135r = uv_timer_init(uv_default_loop(), &timer);
136ASSERT_OK(r);
137
138/* Give it 1s to timeout. */
139r = uv_timer_start(&timer, timer_cb, 1000, 0);
140ASSERT_OK(r);
141
142r = uv_tcp_init(uv_default_loop(), &conn);
143ASSERT_OK(r);
144
145r = uv_tcp_connect(&connect_req,
146&conn,
147(const struct sockaddr*) &addr,
148connect_local_cb);
149if (r == UV_ENETUNREACH)
150RETURN_SKIP("Network unreachable.");
151ASSERT_OK(r);
152
153r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
154ASSERT_OK(r);
155
156MAKE_VALGRIND_HAPPY(uv_default_loop());
157return 0;
158}
159
160TEST_IMPL(tcp6_local_connect_timeout) {
161struct sockaddr_in6 addr;
162int r;
163
164if (!is_supported_system()) {
165RETURN_SKIP("Unsupported system");
166}
167if (!can_ipv6()) {
168RETURN_SKIP("IPv6 not supported");
169}
170
171ASSERT_OK(uv_ip6_addr("::1", 9999, &addr));
172
173r = uv_timer_init(uv_default_loop(), &timer);
174ASSERT_OK(r);
175
176/* Give it 1s to timeout. */
177r = uv_timer_start(&timer, timer_cb, 1000, 0);
178ASSERT_OK(r);
179
180r = uv_tcp_init(uv_default_loop(), &conn);
181ASSERT_OK(r);
182
183r = uv_tcp_connect(&connect_req,
184&conn,
185(const struct sockaddr*) &addr,
186connect_local_cb);
187if (r == UV_ENETUNREACH)
188RETURN_SKIP("Network unreachable.");
189ASSERT_OK(r);
190
191r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
192ASSERT_OK(r);
193
194MAKE_VALGRIND_HAPPY(uv_default_loop());
195return 0;
196}
197