• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(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(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   uv_connect_t connect_req;
59   struct sockaddr_in addr;
60   uv_write_t write_req;
61   uv_tcp_t conn;
62   uv_buf_t buf;
63   int r;
64 
65 #ifdef _WIN32
66   fprintf(stderr, "This test is disabled on Windows for now.\n");
67   fprintf(stderr, "See https://github.com/joyent/libuv/issues/444\n");
68   return 0; /* windows slackers... */
69 #endif
70 
71   ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
72   buf = uv_buf_init("TEST", 4);
73 
74   r = uv_tcp_init(uv_default_loop(), &conn);
75   ASSERT(r == 0);
76 
77   r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
78   ASSERT(r == UV_EBADF);
79 
80   r = uv_tcp_connect(&connect_req,
81                      &conn,
82                      (const struct sockaddr*) &addr,
83                      connect_cb);
84   ASSERT(r == 0);
85 
86   r = uv_write(&write_req, (uv_stream_t*)&conn, &buf, 1, write_cb);
87   ASSERT(r == 0);
88 
89   r = uv_run(uv_default_loop(), UV_RUN_DEFAULT);
90   ASSERT(r == 0);
91 
92   ASSERT(connect_cb_called == 1);
93   ASSERT(write_cb_called == 1);
94   ASSERT(close_cb_called == 1);
95 
96   MAKE_VALGRIND_HAPPY();
97   return 0;
98 }
99