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
29static void connection_cb(uv_stream_t* server, int status);
30static void connect_cb(uv_connect_t* req, int status);
31static void write_cb(uv_write_t* req, int status);
32static void read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
33static void alloc_cb(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf);
34
35static uv_tcp_t tcp_server;
36static uv_tcp_t tcp_client;
37static uv_tcp_t tcp_peer; /* client socket as accept()-ed by server */
38static uv_connect_t connect_req;
39static uv_write_t write_req;
40
41static int write_cb_called;
42static int read_cb_called;
43
44static void connection_cb(uv_stream_t* server, int status) {
45 int r;
46 uv_buf_t buf;
47
48 ASSERT(server == (uv_stream_t*)&tcp_server);
49 ASSERT(status == 0);
50
51 r = uv_tcp_init(server->loop, &tcp_peer);
52 ASSERT(r == 0);
53
54 r = uv_accept(server, (uv_stream_t*)&tcp_peer);
55 ASSERT(r == 0);
56
57 r = uv_read_start((uv_stream_t*)&tcp_peer, alloc_cb, read_cb);
58 ASSERT(r == 0);
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(r == 0);
65}
66
67
68static 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
77static 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
90static void connect_cb(uv_connect_t* req, int status) {
91 ASSERT(req == &connect_req);
92 ASSERT(status == 0);
93
94 /* Close the client. */
95 uv_close((uv_handle_t*)&tcp_client, NULL);
96}
97
98
99static void write_cb(uv_write_t* req, int status) {
100 ASSERT(status == 0);
101 write_cb_called++;
102}
103
104
105TEST_IMPL(tcp_write_to_half_open_connection) {
106 struct sockaddr_in addr;
107 uv_loop_t* loop;
108 int r;
109
110 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
111
112 loop = uv_default_loop();
113 ASSERT(loop != NULL);
114
115 r = uv_tcp_init(loop, &tcp_server);
116 ASSERT(r == 0);
117
118 r = uv_tcp_bind(&tcp_server, (const struct sockaddr*) &addr, 0);
119 ASSERT(r == 0);
120
121 r = uv_listen((uv_stream_t*)&tcp_server, 1, connection_cb);
122 ASSERT(r == 0);
123
124 r = uv_tcp_init(loop, &tcp_client);
125 ASSERT(r == 0);
126
127 r = uv_tcp_connect(&connect_req,
128 &tcp_client,
129 (const struct sockaddr*) &addr,
130 connect_cb);
131 ASSERT(r == 0);
132
133 r = uv_run(loop, UV_RUN_DEFAULT);
134 ASSERT(r == 0);
135
136 ASSERT(write_cb_called > 0);
137 ASSERT(read_cb_called > 0);
138
139 MAKE_VALGRIND_HAPPY();
140 return 0;
141}
142