1 /* Copyright libuv project and 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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "uv.h"
27 #include "task.h"
28
29 static uv_tcp_t server;
30 static uv_tcp_t client;
31 static uv_tcp_t incoming;
32 static int connect_cb_called;
33 static int close_cb_called;
34 static int connection_cb_called;
35 static uv_write_t write_req;
36
37 static char hello[] = "HELLO!";
38
39
close_cb(uv_handle_t * handle)40 static void close_cb(uv_handle_t* handle) {
41 close_cb_called++;
42 }
43
write_cb(uv_write_t * req,int status)44 static void write_cb(uv_write_t* req, int status) {
45 ASSERT(status == 0);
46 }
47
conn_alloc_cb(uv_handle_t * handle,size_t size,uv_buf_t * buf)48 static void conn_alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
49 /* Do nothing, read_cb should be called with UV_ENOBUFS. */
50 }
51
conn_read_cb(uv_stream_t * stream,ssize_t nread,const uv_buf_t * buf)52 static void conn_read_cb(uv_stream_t* stream,
53 ssize_t nread,
54 const uv_buf_t* buf) {
55 ASSERT(nread == UV_ENOBUFS);
56 ASSERT_NULL(buf->base);
57 ASSERT(buf->len == 0);
58
59 uv_close((uv_handle_t*) &incoming, close_cb);
60 uv_close((uv_handle_t*) &client, close_cb);
61 uv_close((uv_handle_t*) &server, close_cb);
62 }
63
connect_cb(uv_connect_t * req,int status)64 static void connect_cb(uv_connect_t* req, int status) {
65 int r;
66 uv_buf_t buf;
67
68 ASSERT(status == 0);
69 connect_cb_called++;
70
71 buf = uv_buf_init(hello, sizeof(hello));
72 r = uv_write(&write_req, req->handle, &buf, 1, write_cb);
73 ASSERT(r == 0);
74 }
75
76
connection_cb(uv_stream_t * tcp,int status)77 static void connection_cb(uv_stream_t* tcp, int status) {
78 ASSERT(status == 0);
79
80 ASSERT(0 == uv_tcp_init(tcp->loop, &incoming));
81 ASSERT(0 == uv_accept(tcp, (uv_stream_t*) &incoming));
82 ASSERT(0 == uv_read_start((uv_stream_t*) &incoming,
83 conn_alloc_cb,
84 conn_read_cb));
85
86 connection_cb_called++;
87 }
88
89
start_server(void)90 static void start_server(void) {
91 struct sockaddr_in addr;
92
93 ASSERT(0 == uv_ip4_addr("0.0.0.0", TEST_PORT, &addr));
94
95 ASSERT(0 == uv_tcp_init(uv_default_loop(), &server));
96 ASSERT(0 == uv_tcp_bind(&server, (struct sockaddr*) &addr, 0));
97 ASSERT(0 == uv_listen((uv_stream_t*) &server, 128, connection_cb));
98 }
99
100
TEST_IMPL(tcp_alloc_cb_fail)101 TEST_IMPL(tcp_alloc_cb_fail) {
102 uv_connect_t connect_req;
103 struct sockaddr_in addr;
104
105 start_server();
106
107 ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
108
109 ASSERT(0 == uv_tcp_init(uv_default_loop(), &client));
110 ASSERT(0 == uv_tcp_connect(&connect_req,
111 &client,
112 (struct sockaddr*) &addr,
113 connect_cb));
114
115 ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
116
117 ASSERT(connect_cb_called == 1);
118 ASSERT(connection_cb_called == 1);
119 ASSERT(close_cb_called == 3);
120
121 MAKE_VALGRIND_HAPPY();
122 return 0;
123 }
124