xref: /libuv/test/test-udp-send-unreachable.c (revision 011a1ac1)
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 #define CHECK_HANDLE(handle) \
30   ASSERT_NE((uv_udp_t*)(handle) == &client || (uv_udp_t*)(handle) == &client2, 0)
31 
32 static uv_udp_t client;
33 static uv_udp_t client2;
34 static uv_timer_t timer;
35 
36 static int send_cb_called;
37 static int recv_cb_called;
38 static int close_cb_called;
39 static int alloc_cb_called;
40 static int timer_cb_called;
41 static int can_recverr;
42 
43 
alloc_cb(uv_handle_t * handle,size_t suggested_size,uv_buf_t * buf)44 static void alloc_cb(uv_handle_t* handle,
45                      size_t suggested_size,
46                      uv_buf_t* buf) {
47   static char slab[65536];
48   CHECK_HANDLE(handle);
49   ASSERT_LE(suggested_size, sizeof(slab));
50   buf->base = slab;
51   buf->len = sizeof(slab);
52   alloc_cb_called++;
53 }
54 
55 
close_cb(uv_handle_t * handle)56 static void close_cb(uv_handle_t* handle) {
57   ASSERT_EQ(1, uv_is_closing(handle));
58   close_cb_called++;
59 }
60 
61 
send_cb(uv_udp_send_t * req,int status)62 static void send_cb(uv_udp_send_t* req, int status) {
63   ASSERT_NOT_NULL(req);
64   ASSERT_OK(status);
65   ASSERT_OK(status);
66   CHECK_HANDLE(req->handle);
67   send_cb_called++;
68 }
69 
send_cb_recverr(uv_udp_send_t * req,int status)70 static void send_cb_recverr(uv_udp_send_t* req, int status) {
71   ASSERT_PTR_NE(req, NULL);
72   ASSERT(status == 0 || status == UV_ECONNREFUSED);
73   CHECK_HANDLE(req->handle);
74   send_cb_called++;
75 }
76 
recv_cb(uv_udp_t * handle,ssize_t nread,const uv_buf_t * rcvbuf,const struct sockaddr * addr,unsigned flags)77 static void recv_cb(uv_udp_t* handle,
78                        ssize_t nread,
79                        const uv_buf_t* rcvbuf,
80                        const struct sockaddr* addr,
81                        unsigned flags) {
82   CHECK_HANDLE(handle);
83   recv_cb_called++;
84 
85   if (nread < 0) {
86     ASSERT(0 && "unexpected error");
87   } else if (nread == 0) {
88     /* Returning unused buffer */
89     ASSERT_NULL(addr);
90   } else {
91     ASSERT_NOT_NULL(addr);
92   }
93 }
94 
95 
timer_cb(uv_timer_t * h)96 static void timer_cb(uv_timer_t* h) {
97   ASSERT_PTR_EQ(h, &timer);
98   timer_cb_called++;
99   uv_close((uv_handle_t*) &client, close_cb);
100   if (can_recverr)
101     uv_close((uv_handle_t*) &client2, close_cb);
102   uv_close((uv_handle_t*) h, close_cb);
103 }
104 
105 
TEST_IMPL(udp_send_unreachable)106 TEST_IMPL(udp_send_unreachable) {
107   struct sockaddr_in addr;
108   struct sockaddr_in addr2;
109   struct sockaddr_in addr3;
110   uv_udp_send_t req1, req2, req3, req4;
111   uv_buf_t buf;
112   int r;
113 
114 #ifdef __linux__
115   can_recverr = 1;
116 #endif
117 
118   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
119   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT_2, &addr2));
120   ASSERT_OK(uv_ip4_addr("127.0.0.1", TEST_PORT_3, &addr3));
121 
122   r = uv_timer_init( uv_default_loop(), &timer );
123   ASSERT_OK(r);
124 
125   r = uv_timer_start( &timer, timer_cb, 1000, 0 );
126   ASSERT_OK(r);
127 
128   r = uv_udp_init(uv_default_loop(), &client);
129   ASSERT_OK(r);
130 
131   r = uv_udp_bind(&client, (const struct sockaddr*) &addr2, 0);
132   ASSERT_OK(r);
133 
134   r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
135   ASSERT_OK(r);
136 
137   /* client sends "PING", then "PANG" */
138   buf = uv_buf_init("PING", 4);
139 
140   r = uv_udp_send(&req1,
141                   &client,
142                   &buf,
143                   1,
144                   (const struct sockaddr*) &addr,
145                   send_cb);
146   ASSERT_OK(r);
147 
148   buf = uv_buf_init("PANG", 4);
149 
150   r = uv_udp_send(&req2,
151                   &client,
152                   &buf,
153                   1,
154                   (const struct sockaddr*) &addr,
155                   send_cb);
156   ASSERT_OK(r);
157 
158   if (can_recverr) {
159     r = uv_udp_init(uv_default_loop(), &client2);
160     ASSERT_OK(r);
161 
162     r = uv_udp_bind(&client2,
163                     (const struct sockaddr*) &addr3,
164                     UV_UDP_LINUX_RECVERR);
165     ASSERT_OK(r);
166 
167     r = uv_udp_recv_start(&client2, alloc_cb, recv_cb);
168     ASSERT_OK(r);
169 
170     /* client sends "PING", then "PANG" */
171     buf = uv_buf_init("PING", 4);
172 
173     r = uv_udp_send(&req3,
174                     &client2,
175                     &buf,
176                     1,
177                     (const struct sockaddr*) &addr,
178                     send_cb_recverr);
179     ASSERT_OK(r);
180 
181     buf = uv_buf_init("PANG", 4);
182 
183     r = uv_udp_send(&req4,
184                     &client2,
185                     &buf,
186                     1,
187                     (const struct sockaddr*) &addr,
188                     send_cb_recverr);
189     ASSERT_OK(r);
190   }
191 
192   uv_run(uv_default_loop(), UV_RUN_DEFAULT);
193 
194   ASSERT_EQ(send_cb_called, (long)(can_recverr ? 4 : 2));
195   ASSERT_EQ(recv_cb_called, alloc_cb_called);
196   ASSERT_EQ(1, timer_cb_called);
197   ASSERT_EQ(close_cb_called, (long)(can_recverr ? 3 : 2));
198 
199   MAKE_VALGRIND_HAPPY(uv_default_loop());
200   return 0;
201 }
202