mirror of
https://github.com/libuv/libuv
synced 2025-03-28 21:13:16 +00:00
test: fix test-udp-send-unreachable
To properly handle sending UDP packet to unreachable address. PR-URL: https://github.com/libuv/libuv/pull/2872 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
This commit is contained in:
parent
c382d39a85
commit
285a5ea819
@ -52,6 +52,7 @@
|
||||
|
||||
#define TEST_PORT 9123
|
||||
#define TEST_PORT_2 9124
|
||||
#define TEST_PORT_3 9125
|
||||
|
||||
#ifdef _WIN32
|
||||
# define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
|
||||
|
@ -27,9 +27,10 @@
|
||||
#include <string.h>
|
||||
|
||||
#define CHECK_HANDLE(handle) \
|
||||
ASSERT((uv_udp_t*)(handle) == &client)
|
||||
ASSERT((uv_udp_t*)(handle) == &client || (uv_udp_t*)(handle) == &client2)
|
||||
|
||||
static uv_udp_t client;
|
||||
static uv_udp_t client2;
|
||||
static uv_timer_t timer;
|
||||
|
||||
static int send_cb_called;
|
||||
@ -37,6 +38,7 @@ static int recv_cb_called;
|
||||
static int close_cb_called;
|
||||
static int alloc_cb_called;
|
||||
static int timer_cb_called;
|
||||
static int can_recverr;
|
||||
|
||||
|
||||
static void alloc_cb(uv_handle_t* handle,
|
||||
@ -44,7 +46,7 @@ static void alloc_cb(uv_handle_t* handle,
|
||||
uv_buf_t* buf) {
|
||||
static char slab[65536];
|
||||
CHECK_HANDLE(handle);
|
||||
ASSERT(suggested_size <= sizeof(slab));
|
||||
ASSERT_LE(suggested_size, sizeof(slab));
|
||||
buf->base = slab;
|
||||
buf->len = sizeof(slab);
|
||||
alloc_cb_called++;
|
||||
@ -52,7 +54,7 @@ static void alloc_cb(uv_handle_t* handle,
|
||||
|
||||
|
||||
static void close_cb(uv_handle_t* handle) {
|
||||
ASSERT(1 == uv_is_closing(handle));
|
||||
ASSERT_EQ(1, uv_is_closing(handle));
|
||||
close_cb_called++;
|
||||
}
|
||||
|
||||
@ -60,10 +62,17 @@ static void close_cb(uv_handle_t* handle) {
|
||||
static void send_cb(uv_udp_send_t* req, int status) {
|
||||
ASSERT_NOT_NULL(req);
|
||||
ASSERT(status == 0);
|
||||
ASSERT_EQ(status, 0);
|
||||
CHECK_HANDLE(req->handle);
|
||||
send_cb_called++;
|
||||
}
|
||||
|
||||
static void send_cb_recverr(uv_udp_send_t* req, int status) {
|
||||
ASSERT_PTR_NE(req, NULL);
|
||||
ASSERT(status == 0 || status == UV_ECONNREFUSED);
|
||||
CHECK_HANDLE(req->handle);
|
||||
send_cb_called++;
|
||||
}
|
||||
|
||||
static void recv_cb(uv_udp_t* handle,
|
||||
ssize_t nread,
|
||||
@ -85,9 +94,11 @@ static void recv_cb(uv_udp_t* handle,
|
||||
|
||||
|
||||
static void timer_cb(uv_timer_t* h) {
|
||||
ASSERT(h == &timer);
|
||||
ASSERT_PTR_EQ(h, &timer);
|
||||
timer_cb_called++;
|
||||
uv_close((uv_handle_t*) &client, close_cb);
|
||||
if (can_recverr)
|
||||
uv_close((uv_handle_t*) &client2, close_cb);
|
||||
uv_close((uv_handle_t*) h, close_cb);
|
||||
}
|
||||
|
||||
@ -95,27 +106,33 @@ static void timer_cb(uv_timer_t* h) {
|
||||
TEST_IMPL(udp_send_unreachable) {
|
||||
struct sockaddr_in addr;
|
||||
struct sockaddr_in addr2;
|
||||
uv_udp_send_t req1, req2;
|
||||
struct sockaddr_in addr3;
|
||||
uv_udp_send_t req1, req2, req3, req4;
|
||||
uv_buf_t buf;
|
||||
int r;
|
||||
|
||||
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
||||
ASSERT(0 == uv_ip4_addr("127.0.0.1", TEST_PORT_2, &addr2));
|
||||
#ifdef __linux__
|
||||
can_recverr = 1;
|
||||
#endif
|
||||
|
||||
ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT, &addr));
|
||||
ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT_2, &addr2));
|
||||
ASSERT_EQ(0, uv_ip4_addr("127.0.0.1", TEST_PORT_3, &addr3));
|
||||
|
||||
r = uv_timer_init( uv_default_loop(), &timer );
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_timer_start( &timer, timer_cb, 1000, 0 );
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_udp_init(uv_default_loop(), &client);
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_udp_bind(&client, (const struct sockaddr*) &addr2, 0);
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_udp_recv_start(&client, alloc_cb, recv_cb);
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
/* client sends "PING", then "PANG" */
|
||||
buf = uv_buf_init("PING", 4);
|
||||
@ -126,7 +143,7 @@ TEST_IMPL(udp_send_unreachable) {
|
||||
1,
|
||||
(const struct sockaddr*) &addr,
|
||||
send_cb);
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
buf = uv_buf_init("PANG", 4);
|
||||
|
||||
@ -136,14 +153,48 @@ TEST_IMPL(udp_send_unreachable) {
|
||||
1,
|
||||
(const struct sockaddr*) &addr,
|
||||
send_cb);
|
||||
ASSERT(r == 0);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
if (can_recverr) {
|
||||
r = uv_udp_init(uv_default_loop(), &client2);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_udp_bind(&client2,
|
||||
(const struct sockaddr*) &addr3,
|
||||
UV_UDP_LINUX_RECVERR);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
r = uv_udp_recv_start(&client2, alloc_cb, recv_cb);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
/* client sends "PING", then "PANG" */
|
||||
buf = uv_buf_init("PING", 4);
|
||||
|
||||
r = uv_udp_send(&req3,
|
||||
&client2,
|
||||
&buf,
|
||||
1,
|
||||
(const struct sockaddr*) &addr,
|
||||
send_cb_recverr);
|
||||
ASSERT_EQ(r, 0);
|
||||
|
||||
buf = uv_buf_init("PANG", 4);
|
||||
|
||||
r = uv_udp_send(&req4,
|
||||
&client2,
|
||||
&buf,
|
||||
1,
|
||||
(const struct sockaddr*) &addr,
|
||||
send_cb_recverr);
|
||||
ASSERT_EQ(r, 0);
|
||||
}
|
||||
|
||||
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
|
||||
|
||||
ASSERT(send_cb_called == 2);
|
||||
ASSERT(recv_cb_called == alloc_cb_called);
|
||||
ASSERT(timer_cb_called == 1);
|
||||
ASSERT(close_cb_called == 2);
|
||||
ASSERT_EQ(send_cb_called, (long)(can_recverr ? 4 : 2));
|
||||
ASSERT_EQ(recv_cb_called, alloc_cb_called);
|
||||
ASSERT_EQ(timer_cb_called, 1);
|
||||
ASSERT_EQ(close_cb_called, (long)(can_recverr ? 3 : 2));
|
||||
|
||||
MAKE_VALGRIND_HAPPY();
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user