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(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 | |
68 | static 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 | |
78 | static 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 | |
88 | static 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 | */ |
122 | TEST_IMPL(connection_fail) { |
123 | connection_fail(on_connect_with_close); |
124 | |
125 | ASSERT(timer_close_cb_calls == 0); |
126 | ASSERT(timer_cb_calls == 0); |
127 | |
128 | MAKE_VALGRIND_HAPPY(); |
129 | return 0; |
130 | } |
131 | |
132 | |
133 | /* |
134 | * This test is the same as the first except it check that the close |
135 | * callback of the tcp handle hasn't been made after the failed connection |
136 | * attempt. |
137 | */ |
138 | TEST_IMPL(connection_fail_doesnt_auto_close) { |
139 | int r; |
140 | |
141 | r = uv_timer_init(uv_default_loop(), &timer); |
142 | ASSERT(r == 0); |
143 | |
144 | connection_fail(on_connect_without_close); |
145 | |
146 | ASSERT(timer_close_cb_calls == 1); |
147 | ASSERT(timer_cb_calls == 1); |
148 | |
149 | MAKE_VALGRIND_HAPPY(); |
150 | return 0; |
151 | } |
152 | |