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 void connection_cb(uv_stream_t* server, int status);
30 static void connect_cb(uv_connect_t* req, int status);
31 static void write_cb(uv_write_t* req, int status);
32 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
33 static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
34
35 static uv_tcp_t tcp_server;
36 static uv_tcp_t tcp_client;
37 static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
38 static uv_connect_t connect_req;
39 static uv_write_t write_req;
40
41 static int write_cb_called;
42 static int read_cb_called;
43
connection_cb(uv_stream_t * server,int status)44 static void connection_cb(uv_stream_t* server, int status) {
45 int r;
46 uv_buf_t buf;
47
48 ASSERT_PTR_EQ(server, (uv_stream_t*)&tcp_server);
49 ASSERT_OK(status);
50
51 r = uv_tcp_init(server->loop, &tcp_peer);
52 ASSERT_OK(r);
53
54 r = uv_accept(server, (uv_stream_t*)&tcp_peer);
55 ASSERT_OK(r);
56
57 r = uv_read_start((uv_stream_t*)&tcp_peer, alloc_cb, read_cb);
58 ASSERT_OK(r);
59
60 buf.base = "hello\n";
61 buf.len = 6;
62
63 r = uv_write(&write_req, (uv_stream_t*)&tcp_peer, &buf, 1, write_cb);
64 ASSERT_OK(r);
65 }
66
67
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)68 static void alloc_cb(uv_handle_t* handle,
69 size_t suggested_size,
70 uv_buf_t* buf) {
71 static char slab[1024];
72 buf->base = slab;
73 buf->len = sizeof(slab);
74 }
75
76
read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)77 static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf) {
78 if (nread < 0) {
79 fprintf(stderr, "read_cb error: %s\n", uv_err_name(nread));
80 ASSERT(nread == UV_ECONNRESET || nread == UV_EOF);
81
82 uv_close((uv_handle_t*)&tcp_server, NULL);
83 uv_close((uv_handle_t*)&tcp_peer, NULL);
84 }
85
86 read_cb_called++;
87 }
88
89
connect_cb(uv_connect_t * req,int status)90 static void connect_cb(uv_connect_t* req, int status) {
91 ASSERT_PTR_EQ(req, &connect_req);
92 ASSERT_OK(status);
93
94 /* Close the client. */
95 uv_close((uv_handle_t*)&tcp_client, NULL);
96 }
97
98
write_cb(uv_write_t * req,int status)99 static void write_cb(uv_write_t* req, int status) {
100 ASSERT_OK(status);
101 write_cb_called++;
102 }
103
104
TEST_IMPL(tcp_write_to_half_open_connection)105 TEST_IMPL(tcp_write_to_half_open_connection) {
106 struct sockaddr_in addr;
107 uv_loop_t* loop;
108 int r;
109
110 ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
111
112 loop = uv_default_loop();
113 ASSERT_NOT_NULL(loop);
114
115 r = uv_tcp_init(loop, &tcp_server);
116 ASSERT_OK(r);
117
118 r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
119 ASSERT_OK(r);
120
121 r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
122 ASSERT_OK(r);
123
124 r = uv_tcp_init(loop, &tcp_client);
125 ASSERT_OK(r);
126
127 r = uv_tcp_connect(&connect_req,
128 &tcp_client,
129 (const struct sockaddr*) &addr,
130 connect_cb);
131 ASSERT_OK(r);
132
133 r = uv_run(loop, UV_RUN_DEFAULT);
134 ASSERT_OK(r);
135
136 ASSERT_GT(write_cb_called, 0);
137 ASSERT_GT(read_cb_called, 0);
138
139 MAKE_VALGRIND_HAPPY(loop);
140 return 0;
141 }
142