xref: /libuv/test/test-tcp-write-fail.c (revision 011a1ac1)
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 #ifndef _WIN32
27 # include <unistd.h>
28 #endif
29 
30 
31 static int connect_cb_called = 0;
32 static int write_cb_called = 0;
33 static int close_cb_called = 0;
34 
35 static uv_connect_t connect_req;
36 static uv_write_t write_req;
37 
38 
close_socket(uv_tcp_t * sock)39 static void close_socket(uv_tcp_t* sock) {
40   uv_os_fd_t fd;
41   int r;
42 
43   r = uv_fileno((uv_handle_t*)sock, &fd);
44   ASSERT_OK(r);
45 #ifdef _WIN32
46   r = closesocket((uv_os_sock_t)fd);
47 #else
48   r = close(fd);
49 #endif
50   ASSERT_OK(r);
51 }
52 
53 
close_cb(uv_handle_t * handle)54 static void close_cb(uv_handle_t* handle) {
55   ASSERT_NOT_NULL(handle);
56   close_cb_called++;
57 }
58 
59 
write_cb(uv_write_t * req,int status)60 static void write_cb(uv_write_t* req, int status) {
61   ASSERT_NOT_NULL(req);
62 
63   ASSERT(status);
64   fprintf(stderr, "uv_write error: %s\n", uv_strerror(status));
65   write_cb_called++;
66 
67   uv_close((uv_handle_t*)(req->handle), close_cb);
68 }
69 
70 
connect_cb(uv_connect_t * req,int status)71 static void connect_cb(uv_connect_t* req, int status) {
72   uv_buf_t buf;
73   uv_stream_t* stream;
74   int r;
75 
76   ASSERT_PTR_EQ(req, &connect_req);
77   ASSERT_OK(status);
78 
79   stream = req->handle;
80   connect_cb_called++;
81 
82   /* close the socket, the hard way */
83   close_socket((uv_tcp_t*)stream);
84 
85   buf = uv_buf_init("hello\n", 6);
86   r = uv_write(&write_req, stream, &buf, 1, write_cb);
87   ASSERT_OK(r);
88 }
89 
90 
TEST_IMPL(tcp_write_fail)91 TEST_IMPL(tcp_write_fail) {
92   struct sockaddr_in addr;
93   uv_tcp_t client;
94   int r;
95 
96   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
97 
98   r = uv_tcp_init(uv_default_loop(), &client);
99   ASSERT_OK(r);
100 
101   r = uv_tcp_connect(&connect_req,
102                      &client,
103                      (const struct sockaddr*) &addr,
104                      connect_cb);
105   ASSERT_OK(r);
106 
107   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
108 
109   ASSERT_EQ(1, connect_cb_called);
110   ASSERT_EQ(1, write_cb_called);
111   ASSERT_EQ(1, close_cb_called);
112 
113   MAKE_VALGRIND_HAPPY(uv_default_loop());
114   return 0;
115 }
116