1 /* Copyright the libuv project 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 static uv_loop_t loop;
26 static uv_tcp_t tcp_client;
27 static uv_connect_t connect_req;
28 static uv_write_t write_req;
29 static char close_me_cmd[] = {'Q', 'S'};
30
31 static int connect_cb_called;
32 static int read_cb_called;
33 static int write_cb_called;
34 static int close_cb_called;
35
write_cb(uv_write_t * req,int status)36 static void write_cb(uv_write_t* req, int status) {
37 write_cb_called++;
38 ASSERT_EQ(status, 0);
39 }
40
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)41 static void alloc_cb(uv_handle_t* handle,
42 size_t suggested_size,
43 uv_buf_t* buf) {
44 static char slab[64];
45 buf->base = slab;
46 buf->len = sizeof(slab);
47 }
48
close_cb(uv_handle_t * handle)49 static void close_cb(uv_handle_t* handle) {
50 close_cb_called++;
51 }
52
read_cb(uv_stream_t * handle,ssize_t nread,const uv_buf_t * buf)53 static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
54 int r;
55
56 ASSERT_EQ(nread, UV_EOF);
57 ASSERT_EQ(uv_is_readable(handle), 1);
58 ASSERT_EQ(uv_is_writable(handle), 1);
59
60 if (++read_cb_called == 3) {
61 uv_close((uv_handle_t*) handle, close_cb);
62 ASSERT_EQ(uv_is_readable(handle), 0);
63 ASSERT_EQ(uv_is_writable(handle), 0);
64 } else {
65 r = uv_read_start((uv_stream_t*) &tcp_client, alloc_cb, read_cb);
66 ASSERT_EQ(r, 0);
67 }
68 }
69
connect_cb(uv_connect_t * req,int status)70 static void connect_cb(uv_connect_t* req, int status) {
71 int r;
72 uv_buf_t close_me;
73
74 connect_cb_called++;
75 ASSERT_EQ(status, 0);
76
77 read_cb((uv_stream_t*) &tcp_client, UV_EOF, NULL);
78
79 close_me = uv_buf_init(close_me_cmd, sizeof(close_me_cmd));
80
81 r = uv_write(&write_req,
82 (uv_stream_t*) &tcp_client,
83 &close_me,
84 1,
85 write_cb);
86
87 ASSERT_EQ(r, 0);
88 }
89
TEST_IMPL(readable_on_eof)90 TEST_IMPL(readable_on_eof) {
91 struct sockaddr_in sa;
92 ASSERT_EQ(uv_ip4_addr("127.0.0.1", TEST_PORT, &sa), 0);
93 ASSERT_EQ(uv_loop_init(&loop), 0);
94 ASSERT_EQ(uv_tcp_init(&loop, &tcp_client), 0);
95
96 ASSERT_EQ(uv_tcp_connect(&connect_req,
97 &tcp_client,
98 (const struct sockaddr*) &sa,
99 connect_cb),
100 0);
101
102 ASSERT_EQ(uv_run(&loop, UV_RUN_DEFAULT), 0);
103
104 ASSERT_EQ(connect_cb_called, 1);
105 ASSERT_EQ(read_cb_called, 3);
106 ASSERT_EQ(write_cb_called, 1);
107 ASSERT_EQ(close_cb_called, 1);
108
109 MAKE_VALGRIND_HAPPY();
110 return 0;
111 }
112