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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 
29 static int connect_cb_called;
30 static int write_cb_called;
31 static int close_cb_called;
32 
33 
close_cb(uv_handle_t * handle)34 static void close_cb(uv_handle_t* handle) {
35   close_cb_called++;
36 }
37 
38 
connect_cb(uv_connect_t * req,int status)39 static void connect_cb(uv_connect_t* req, int status) {
40   ASSERT_LT(status, 0);
41   connect_cb_called++;
42   uv_close((uv_handle_t*)req->handle, close_cb);
43 }
44 
45 
write_cb(uv_write_t * req,int status)46 static void write_cb(uv_write_t* req, int status) {
47   ASSERT_LT(status, 0);
48   write_cb_called++;
49 }
50 
51 
52 /*
53  * Try to connect to an address on which nothing listens, get ECONNREFUSED
54  * (uv errno 12) and get connect_cb() called once with status != 0.
55  * Related issue: https://github.com/joyent/libuv/issues/443
56  */
TEST_IMPL(tcp_connect_error_after_write)57 TEST_IMPL(tcp_connect_error_after_write) {
58 #ifdef _WIN32
59   RETURN_SKIP("This test is disabled on Windows for now. "
60               "See https://github.com/joyent/libuv/issues/444\n");
61 #else
62 
63   uv_connect_t connect_req;
64   struct sockaddr_in addr;
65   uv_write_t write_req;
66   uv_tcp_t conn;
67   uv_buf_t buf;
68   int r;
69 
70   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
71   buf = uv_buf_init("TEST", 4);
72 
73   r = uv_tcp_init(uv_default_loop(), &conn);
74   ASSERT_OK(r);
75 
76   r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
77   ASSERT_EQ(r, UV_EBADF);
78 
79   r = uv_tcp_connect(&connect_req,
80                      &conn,
81                      (const struct sockaddr*) &addr,
82                      connect_cb);
83   ASSERT_OK(r);
84 
85   r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
86   ASSERT_OK(r);
87 
88   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
89   ASSERT_OK(r);
90 
91   ASSERT_EQ(1, connect_cb_called);
92   ASSERT_EQ(1, write_cb_called);
93   ASSERT_EQ(1, close_cb_called);
94 
95   MAKE_VALGRIND_HAPPY(uv_default_loop());
96   return 0;
97 #endif
98 }
99