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
25static uv_loop_t loop;
26static uv_tcp_t tcp_client;
27static uv_connect_t connect_req;
28static uv_write_t write_req;
29static char reset_me_cmd[] = {'Q', 'S', 'H'};
30
31static int connect_cb_called;
32static int read_cb_called;
33static int write_cb_called;
34static int close_cb_called;
35
36static void write_cb(uv_write_t* req, int status) {
37  write_cb_called++;
38  ASSERT_OK(status);
39}
40
41static 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
49static void close_cb(uv_handle_t* handle) {
50  close_cb_called++;
51}
52
53static void read_cb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) {
54  read_cb_called++;
55
56  ASSERT((nread < 0) && (nread != UV_EOF));
57  ASSERT_OK(uv_is_writable(handle));
58  ASSERT_OK(uv_is_readable(handle));
59
60  uv_close((uv_handle_t*) handle, close_cb);
61}
62
63static void connect_cb(uv_connect_t* req, int status) {
64  int r;
65  uv_buf_t reset_me;
66
67  connect_cb_called++;
68  ASSERT_OK(status);
69
70  r = uv_read_start((uv_stream_t*) &tcp_client, alloc_cb, read_cb);
71  ASSERT_OK(r);
72
73  reset_me = uv_buf_init(reset_me_cmd, sizeof(reset_me_cmd));
74
75  r = uv_write(&write_req,
76               (uv_stream_t*) &tcp_client,
77               &reset_me,
78               1,
79               write_cb);
80
81  ASSERT_OK(r);
82}
83
84TEST_IMPL(not_readable_nor_writable_on_read_error) {
85  struct sockaddr_in sa;
86  ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &sa));
87  ASSERT_OK(uv_loop_init(&loop));
88  ASSERT_OK(uv_tcp_init(&loop, &tcp_client));
89
90  ASSERT_OK(uv_tcp_connect(&connect_req,
91                           &tcp_client,
92                           (const struct sockaddr*) &sa,
93                           connect_cb));
94
95  ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT));
96
97  ASSERT_EQ(1, connect_cb_called);
98  ASSERT_EQ(1, read_cb_called);
99  ASSERT_EQ(1, write_cb_called);
100  ASSERT_EQ(1, close_cb_called);
101
102  MAKE_VALGRIND_HAPPY(&loop);
103  return 0;
104}
105