mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-01 15:05:23 +00:00
Split netmgr_test into separate per-transport unit tests
The netmgr_test unit test has been subdivided into tcp_test, tcpdns_test, tls_test, tlsdns_test, and udp_test components. These have been updated to use the new loopmgr.
This commit is contained in:
@@ -39,8 +39,13 @@ check_PROGRAMS = \
|
||||
stats_test \
|
||||
symtab_test \
|
||||
task_test \
|
||||
tcp_test \
|
||||
tcpdns_test \
|
||||
time_test \
|
||||
timer_test \
|
||||
tls_test \
|
||||
tlsdns_test \
|
||||
udp_test \
|
||||
work_test
|
||||
|
||||
if HAVE_LIBNGHTTP2
|
||||
@@ -88,6 +93,76 @@ task_test_CPPFLAGS = \
|
||||
task_test_LDADD = \
|
||||
$(LDADD)
|
||||
|
||||
tcp_test_CPPFLAGS = \
|
||||
$(AM_CPPFLAGS) \
|
||||
$(OPENSSL_CFLAGS)
|
||||
|
||||
tcp_test_LDADD = \
|
||||
$(LDADD) \
|
||||
$(OPENSSL_LIBS)
|
||||
|
||||
tcp_test_SOURCES = \
|
||||
tcp_test.c \
|
||||
netmgr_common.h \
|
||||
netmgr_common.c \
|
||||
uv_wrap.h
|
||||
|
||||
tcpdns_test_CPPFLAGS = \
|
||||
$(AM_CPPFLAGS) \
|
||||
$(OPENSSL_CFLAGS)
|
||||
|
||||
tcpdns_test_LDADD = \
|
||||
$(LDADD) \
|
||||
$(OPENSSL_LIBS)
|
||||
|
||||
tcpdns_test_SOURCES = \
|
||||
tcpdns_test.c \
|
||||
netmgr_common.h \
|
||||
netmgr_common.c \
|
||||
uv_wrap.h
|
||||
|
||||
tls_test_CPPFLAGS = \
|
||||
$(AM_CPPFLAGS) \
|
||||
$(OPENSSL_CFLAGS)
|
||||
|
||||
tls_test_LDADD = \
|
||||
$(LDADD) \
|
||||
$(OPENSSL_LIBS)
|
||||
|
||||
tls_test_SOURCES = \
|
||||
tls_test.c \
|
||||
netmgr_common.h \
|
||||
netmgr_common.c \
|
||||
uv_wrap.h
|
||||
|
||||
tlsdns_test_CPPFLAGS = \
|
||||
$(AM_CPPFLAGS) \
|
||||
$(OPENSSL_CFLAGS)
|
||||
|
||||
tlsdns_test_LDADD = \
|
||||
$(LDADD) \
|
||||
$(OPENSSL_LIBS)
|
||||
|
||||
tlsdns_test_SOURCES = \
|
||||
tlsdns_test.c \
|
||||
netmgr_common.h \
|
||||
netmgr_common.c \
|
||||
uv_wrap.h
|
||||
|
||||
udp_test_CPPFLAGS = \
|
||||
$(AM_CPPFLAGS) \
|
||||
$(OPENSSL_CFLAGS)
|
||||
|
||||
udp_test_LDADD = \
|
||||
$(LDADD) \
|
||||
$(OPENSSL_LIBS)
|
||||
|
||||
udp_test_SOURCES = \
|
||||
udp_test.c \
|
||||
netmgr_common.h \
|
||||
netmgr_common.c \
|
||||
uv_wrap.h
|
||||
|
||||
if HAVE_LIBXML2
|
||||
task_test_CPPFLAGS += $(LIBXML2_CFLAGS)
|
||||
task_test_LDADD += $(LIBXML2_LIBS)
|
||||
|
@@ -82,8 +82,6 @@ static int expected_ctimeouts;
|
||||
#define have_expected_ctimeouts(v) \
|
||||
((v) >= expected_ctimeouts && expected_ctimeouts >= 0)
|
||||
|
||||
static atomic_bool test_was_error = false;
|
||||
|
||||
static bool noanswer = false;
|
||||
|
||||
static atomic_bool POST = true;
|
||||
@@ -161,8 +159,6 @@ error:
|
||||
isc_mem_putanddetach(&data.mctx, data.region.base, data.region.length);
|
||||
if (result == ISC_R_TOOMANYOPENFILES) {
|
||||
atomic_store(&slowdown, true);
|
||||
} else {
|
||||
atomic_store(&test_was_error, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,8 +299,6 @@ setup_test(void **state) {
|
||||
expected_ssends = -1;
|
||||
expected_ctimeouts = -1;
|
||||
|
||||
atomic_store(&test_was_error, false);
|
||||
|
||||
atomic_store(&POST, false);
|
||||
atomic_store(&use_TLS, false);
|
||||
|
||||
@@ -405,8 +399,6 @@ doh_receive_reply_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
} else {
|
||||
/* We failed to connect; try again */
|
||||
atomic_store(&test_was_error, true);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
}
|
||||
@@ -432,7 +424,6 @@ doh_receive_request_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
assert_non_null(handle);
|
||||
|
||||
if (eresult != ISC_R_SUCCESS) {
|
||||
atomic_store(&test_was_error, true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -487,6 +478,15 @@ ISC_LOOP_TEST_IMPL(mock_doh_uv_tcp_bind) {
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
listen_sock_close(void *arg) {
|
||||
isc_nmsocket_t *listen_sock = arg;
|
||||
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
doh_noop(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_t *listen_nm = nm[0];
|
||||
@@ -503,10 +503,7 @@ doh_noop(void *arg __attribute__((__unused__))) {
|
||||
&tcp_listen_addr, 0, NULL, NULL, endpoints,
|
||||
0, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
isc_loop_teardown(mainloop, listen_sock_close, listen_sock);
|
||||
|
||||
sockaddr_to_url(&tcp_listen_addr, false, req_url, sizeof(req_url),
|
||||
ISC_NM_HTTP_DEFAULT_PATH);
|
||||
@@ -549,6 +546,7 @@ doh_noresponse(void *arg __attribute__((__unused__))) {
|
||||
&tcp_listen_addr, 0, NULL, NULL, endpoints,
|
||||
0, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, listen_sock_close, listen_sock);
|
||||
|
||||
sockaddr_to_url(&tcp_listen_addr, false, req_url, sizeof(req_url),
|
||||
ISC_NM_HTTP_DEFAULT_PATH);
|
||||
@@ -557,9 +555,6 @@ doh_noresponse(void *arg __attribute__((__unused__))) {
|
||||
.length = send_msg.len },
|
||||
noop_read_cb, NULL, atomic_load(&use_TLS), 30000);
|
||||
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
@@ -613,7 +608,7 @@ timeout_request_cb(isc_nmhandle_t *handle, isc_result_t result, void *arg) {
|
||||
REQUIRE(VALID_NMHANDLE(handle));
|
||||
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto error;
|
||||
return;
|
||||
}
|
||||
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
@@ -624,19 +619,6 @@ timeout_request_cb(isc_nmhandle_t *handle, isc_result_t result, void *arg) {
|
||||
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, timeout_retry_cb, NULL);
|
||||
return;
|
||||
|
||||
error:
|
||||
atomic_store(&test_was_error, true);
|
||||
}
|
||||
|
||||
static void
|
||||
listen_sock_close(void *arg) {
|
||||
isc_nmsocket_t *listen_sock = arg;
|
||||
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -729,9 +711,6 @@ doh_receive_send_reply_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
if (sends <= 0) {
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
} else {
|
||||
atomic_store(&test_was_error, true);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
isc_nmhandle_detach(&thandle);
|
||||
}
|
||||
@@ -870,29 +849,26 @@ doh_connect_send_two_requests_cb(isc_nmhandle_t *handle, isc_result_t result,
|
||||
void *arg) {
|
||||
REQUIRE(VALID_NMHANDLE(handle));
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
result = isc__nm_http_request(
|
||||
handle,
|
||||
&(isc_region_t){ .base = (uint8_t *)send_msg.base,
|
||||
.length = send_msg.len },
|
||||
doh_receive_reply_cb, arg);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
result = isc__nm_http_request(
|
||||
handle,
|
||||
&(isc_region_t){ .base = (uint8_t *)send_msg.base,
|
||||
.length = send_msg.len },
|
||||
doh_receive_reply_cb, arg);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto error;
|
||||
}
|
||||
return;
|
||||
error:
|
||||
atomic_store(&test_was_error, true);
|
||||
}
|
||||
|
||||
result = isc__nm_http_request(
|
||||
handle,
|
||||
&(isc_region_t){ .base = (uint8_t *)send_msg.base,
|
||||
.length = send_msg.len },
|
||||
doh_receive_reply_cb, arg);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return;
|
||||
}
|
||||
|
||||
result = isc__nm_http_request(
|
||||
handle,
|
||||
&(isc_region_t){ .base = (uint8_t *)send_msg.base,
|
||||
.length = send_msg.len },
|
||||
doh_receive_reply_cb, arg);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -1103,7 +1079,6 @@ doh_bad_connect_uri_teardown(void **state) {
|
||||
X(ssends);
|
||||
|
||||
/* As we used an ill-formed URI, there ought to be an error. */
|
||||
assert_true(atomic_load(&test_was_error));
|
||||
assert_int_equal(atomic_load(&csends), 0);
|
||||
assert_int_equal(atomic_load(&creads), 0);
|
||||
assert_int_equal(atomic_load(&sreads), 0);
|
||||
@@ -1799,7 +1774,12 @@ ISC_RUN_TEST_IMPL(doh_connect_makeuri) {
|
||||
}
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
/* Mock tests are unreliable on OpenBSD */
|
||||
#if !defined(__OpenBSD__)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_doh_uv_tcp_bind, setup_test, teardown_test)
|
||||
#endif /* !defined(__OpenBSD__) */
|
||||
|
||||
ISC_TEST_ENTRY(doh_parse_GET_query_string)
|
||||
ISC_TEST_ENTRY(doh_base64url_to_base64)
|
||||
ISC_TEST_ENTRY(doh_base64_to_base64url)
|
||||
|
971
tests/isc/netmgr_common.c
Normal file
971
tests/isc/netmgr_common.c
Normal file
@@ -0,0 +1,971 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/async.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/thread.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/uv.h>
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr_common.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
isc_nm_t *listen_nm = NULL;
|
||||
isc_nm_t *connect_nm = NULL;
|
||||
|
||||
isc_sockaddr_t tcp_listen_addr;
|
||||
isc_sockaddr_t tcp_connect_addr;
|
||||
isc_tlsctx_t *tcp_listen_tlsctx = NULL;
|
||||
isc_tlsctx_t *tcp_connect_tlsctx = NULL;
|
||||
isc_tlsctx_client_session_cache_t *tcp_tlsctx_client_sess_cache = NULL;
|
||||
|
||||
uint64_t send_magic = 0;
|
||||
|
||||
isc_region_t send_msg = { .base = (unsigned char *)&send_magic,
|
||||
.length = sizeof(send_magic) };
|
||||
|
||||
atomic_bool do_send = false;
|
||||
|
||||
atomic_int_fast64_t nsends = 0;
|
||||
int_fast64_t esends = 0; /* expected sends */
|
||||
|
||||
atomic_int_fast64_t ssends = 0;
|
||||
atomic_int_fast64_t sreads = 0;
|
||||
atomic_int_fast64_t saccepts = 0;
|
||||
|
||||
atomic_int_fast64_t cconnects = 0;
|
||||
atomic_int_fast64_t csends = 0;
|
||||
atomic_int_fast64_t creads = 0;
|
||||
atomic_int_fast64_t ctimeouts = 0;
|
||||
|
||||
int expected_ssends;
|
||||
int expected_sreads;
|
||||
int expected_csends;
|
||||
int expected_cconnects;
|
||||
int expected_creads;
|
||||
int expected_saccepts;
|
||||
int expected_ctimeouts;
|
||||
|
||||
bool ssends_shutdown;
|
||||
bool sreads_shutdown;
|
||||
bool saccepts_shutdown;
|
||||
bool csends_shutdown;
|
||||
bool cconnects_shutdown;
|
||||
bool creads_shutdown;
|
||||
bool ctimeouts_shutdown;
|
||||
|
||||
isc_refcount_t active_cconnects = 0;
|
||||
isc_refcount_t active_csends = 0;
|
||||
isc_refcount_t active_creads = 0;
|
||||
isc_refcount_t active_ssends = 0;
|
||||
isc_refcount_t active_sreads = 0;
|
||||
|
||||
isc_nmsocket_t *listen_sock = NULL;
|
||||
|
||||
isc_quota_t listener_quota;
|
||||
atomic_bool check_listener_quota = false;
|
||||
|
||||
bool allow_send_back = false;
|
||||
bool noanswer = false;
|
||||
bool stream_use_TLS = false;
|
||||
|
||||
isc_nm_recv_cb_t connect_readcb = NULL;
|
||||
|
||||
int
|
||||
setup_ephemeral_port(isc_sockaddr_t *addr, sa_family_t family) {
|
||||
socklen_t addrlen = sizeof(*addr);
|
||||
uv_os_sock_t fd = -1;
|
||||
int r;
|
||||
|
||||
isc_sockaddr_fromin6(addr, &in6addr_loopback, 0);
|
||||
|
||||
fd = socket(AF_INET6, family, 0);
|
||||
if (fd < 0) {
|
||||
perror("setup_ephemeral_port: socket()");
|
||||
return (-1);
|
||||
}
|
||||
|
||||
r = bind(fd, (const struct sockaddr *)&addr->type.sa,
|
||||
sizeof(addr->type.sin6));
|
||||
if (r != 0) {
|
||||
perror("setup_ephemeral_port: bind()");
|
||||
isc__nm_closesocket(fd);
|
||||
return (r);
|
||||
}
|
||||
|
||||
r = getsockname(fd, (struct sockaddr *)&addr->type.sa, &addrlen);
|
||||
if (r != 0) {
|
||||
perror("setup_ephemeral_port: getsockname()");
|
||||
isc__nm_closesocket(fd);
|
||||
return (r);
|
||||
}
|
||||
|
||||
#if IPV6_RECVERR
|
||||
#define setsockopt_on(socket, level, name) \
|
||||
setsockopt(socket, level, name, &(int){ 1 }, sizeof(int))
|
||||
|
||||
r = setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVERR);
|
||||
if (r != 0) {
|
||||
perror("setup_ephemeral_port");
|
||||
isc__nm_closesocket(fd);
|
||||
return (r);
|
||||
}
|
||||
#endif
|
||||
|
||||
return (fd);
|
||||
}
|
||||
|
||||
int
|
||||
setup_netmgr_test(void **state) {
|
||||
char *env_workers = getenv("ISC_TASK_WORKERS");
|
||||
uv_os_sock_t tcp_listen_sock = -1;
|
||||
size_t nworkers;
|
||||
|
||||
tcp_connect_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
isc_sockaddr_fromin6(&tcp_connect_addr, &in6addr_loopback, 0);
|
||||
|
||||
tcp_listen_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
tcp_listen_sock = setup_ephemeral_port(&tcp_listen_addr, SOCK_STREAM);
|
||||
if (tcp_listen_sock < 0) {
|
||||
return (-1);
|
||||
}
|
||||
isc__nm_closesocket(tcp_listen_sock);
|
||||
tcp_listen_sock = -1;
|
||||
|
||||
if (env_workers != NULL) {
|
||||
workers = atoi(env_workers);
|
||||
} else {
|
||||
workers = isc_os_ncpus();
|
||||
}
|
||||
INSIST(workers > 0);
|
||||
nworkers = ISC_MAX(ISC_MIN(workers, 32), 1);
|
||||
|
||||
esends = NSENDS * nworkers;
|
||||
|
||||
atomic_store(&nsends, esends);
|
||||
|
||||
atomic_store(&saccepts, 0);
|
||||
atomic_store(&sreads, 0);
|
||||
atomic_store(&ssends, 0);
|
||||
|
||||
atomic_store(&cconnects, 0);
|
||||
atomic_store(&csends, 0);
|
||||
atomic_store(&creads, 0);
|
||||
atomic_store(&ctimeouts, 0);
|
||||
allow_send_back = false;
|
||||
|
||||
expected_cconnects = -1;
|
||||
expected_csends = -1;
|
||||
expected_creads = -1;
|
||||
expected_sreads = -1;
|
||||
expected_ssends = -1;
|
||||
expected_saccepts = -1;
|
||||
expected_ctimeouts = -1;
|
||||
|
||||
ssends_shutdown = true;
|
||||
sreads_shutdown = true;
|
||||
saccepts_shutdown = true;
|
||||
csends_shutdown = true;
|
||||
cconnects_shutdown = true;
|
||||
creads_shutdown = true;
|
||||
ctimeouts_shutdown = true;
|
||||
|
||||
do_send = false;
|
||||
|
||||
isc_refcount_init(&active_cconnects, 0);
|
||||
isc_refcount_init(&active_csends, 0);
|
||||
isc_refcount_init(&active_creads, 0);
|
||||
isc_refcount_init(&active_ssends, 0);
|
||||
isc_refcount_init(&active_sreads, 0);
|
||||
|
||||
isc_nonce_buf(&send_magic, sizeof(send_magic));
|
||||
|
||||
setup_loopmgr(state);
|
||||
isc_netmgr_create(mctx, loopmgr, &listen_nm);
|
||||
assert_non_null(listen_nm);
|
||||
isc_nm_settimeouts(listen_nm, T_INIT, T_IDLE, T_KEEPALIVE,
|
||||
T_ADVERTISED);
|
||||
|
||||
isc_netmgr_create(mctx, loopmgr, &connect_nm);
|
||||
assert_non_null(connect_nm);
|
||||
isc_nm_settimeouts(connect_nm, T_INIT, T_IDLE, T_KEEPALIVE,
|
||||
T_ADVERTISED);
|
||||
|
||||
isc_quota_init(&listener_quota, 0);
|
||||
atomic_store(&check_listener_quota, false);
|
||||
|
||||
connect_readcb = connect_read_cb;
|
||||
noanswer = false;
|
||||
|
||||
if (isc_tlsctx_createserver(NULL, NULL, &tcp_listen_tlsctx) !=
|
||||
ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
if (isc_tlsctx_createclient(&tcp_connect_tlsctx) != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
isc_tlsctx_enable_dot_client_alpn(tcp_connect_tlsctx);
|
||||
|
||||
tcp_tlsctx_client_sess_cache = isc_tlsctx_client_session_cache_new(
|
||||
mctx, tcp_connect_tlsctx,
|
||||
ISC_TLSCTX_CLIENT_SESSION_CACHE_DEFAULT_SIZE);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
teardown_netmgr_test(void **state __attribute__((__unused__))) {
|
||||
UNUSED(state);
|
||||
|
||||
isc_tlsctx_client_session_cache_detach(&tcp_tlsctx_client_sess_cache);
|
||||
|
||||
isc_tlsctx_free(&tcp_connect_tlsctx);
|
||||
isc_tlsctx_free(&tcp_listen_tlsctx);
|
||||
|
||||
isc_netmgr_destroy(&connect_nm);
|
||||
assert_null(connect_nm);
|
||||
|
||||
isc_netmgr_destroy(&listen_nm);
|
||||
assert_null(listen_nm);
|
||||
|
||||
teardown_loopmgr(state);
|
||||
|
||||
isc_refcount_destroy(&active_cconnects);
|
||||
isc_refcount_destroy(&active_csends);
|
||||
isc_refcount_destroy(&active_creads);
|
||||
isc_refcount_destroy(&active_ssends);
|
||||
isc_refcount_destroy(&active_sreads);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void
|
||||
stop_listening(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
/* Callbacks */
|
||||
|
||||
void
|
||||
noop_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
|
||||
void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(eresult);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
}
|
||||
|
||||
unsigned int
|
||||
noop_accept_cb(isc_nmhandle_t *handle, unsigned int result, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
if (result == ISC_R_SUCCESS) {
|
||||
(void)atomic_fetch_add(&saccepts, 1);
|
||||
}
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void
|
||||
connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
void
|
||||
connect_send(isc_nmhandle_t *handle);
|
||||
|
||||
void
|
||||
connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *sendhandle = handle;
|
||||
|
||||
assert_non_null(sendhandle);
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
if (eresult != ISC_R_SUCCESS) {
|
||||
/* Send failed, we need to stop reading too */
|
||||
isc_nm_cancelread(handle);
|
||||
goto unref;
|
||||
}
|
||||
|
||||
if (have_expected_csends(atomic_fetch_add(&csends, 1) + 1)) {
|
||||
do_csends_shutdown(loopmgr);
|
||||
}
|
||||
unref:
|
||||
isc_refcount_decrement(&active_csends);
|
||||
isc_nmhandle_detach(&sendhandle);
|
||||
}
|
||||
|
||||
void
|
||||
connect_send(isc_nmhandle_t *handle) {
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
isc_nm_send(sendhandle, &send_msg, connect_send_cb, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
connect_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
uint64_t magic = 0;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
F();
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_SUCCESS:
|
||||
assert_true(region->length >= sizeof(magic));
|
||||
|
||||
memmove(&magic, region->base, sizeof(magic));
|
||||
|
||||
assert_true(magic == send_magic);
|
||||
|
||||
if (have_expected_creads(atomic_fetch_add(&creads, 1) + 1)) {
|
||||
do_creads_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
if (magic == send_magic && allow_send_back) {
|
||||
connect_send(handle);
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
case ISC_R_TIMEDOUT:
|
||||
case ISC_R_EOF:
|
||||
case ISC_R_SHUTTINGDOWN:
|
||||
case ISC_R_CANCELED:
|
||||
case ISC_R_CONNECTIONRESET:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
|
||||
isc_result_totext(eresult), cbarg);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
}
|
||||
|
||||
void
|
||||
connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
if (eresult != ISC_R_SUCCESS || connect_readcb == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* We are finished, initiate the shutdown */
|
||||
if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) + 1)) {
|
||||
do_cconnects_shutdown(loopmgr);
|
||||
} else if (do_send) {
|
||||
isc_job_run(loopmgr, stream_recv_send_connect,
|
||||
get_stream_connect_function());
|
||||
}
|
||||
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, connect_readcb, NULL);
|
||||
|
||||
connect_send(handle);
|
||||
}
|
||||
|
||||
void
|
||||
listen_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *sendhandle = handle;
|
||||
|
||||
UNUSED(cbarg);
|
||||
UNUSED(eresult);
|
||||
|
||||
assert_non_null(sendhandle);
|
||||
|
||||
F();
|
||||
|
||||
isc_refcount_decrement(&active_ssends);
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_SUCCESS:
|
||||
if (have_expected_ssends(atomic_fetch_add(&ssends, 1) + 1)) {
|
||||
do_ssends_shutdown(loopmgr);
|
||||
}
|
||||
break;
|
||||
case ISC_R_CANCELED:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
|
||||
isc_result_totext(eresult), cbarg);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_nmhandle_detach(&sendhandle);
|
||||
}
|
||||
|
||||
void
|
||||
listen_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
uint64_t magic = 0;
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
F();
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_EOF:
|
||||
case ISC_R_SHUTTINGDOWN:
|
||||
case ISC_R_CANCELED:
|
||||
break;
|
||||
case ISC_R_SUCCESS:
|
||||
memmove(&magic, region->base, sizeof(magic));
|
||||
assert_true(magic == send_magic);
|
||||
|
||||
if (have_expected_sreads(atomic_fetch_add(&sreads, 1) + 1)) {
|
||||
do_sreads_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
assert_true(region->length >= sizeof(magic));
|
||||
|
||||
memmove(&magic, region->base, sizeof(magic));
|
||||
assert_true(magic == send_magic);
|
||||
|
||||
if (magic == send_magic) {
|
||||
if (!noanswer) {
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_refcount_increment0(&active_ssends);
|
||||
isc_nmhandle_setwritetimeout(sendhandle,
|
||||
T_IDLE);
|
||||
isc_nm_send(sendhandle, &send_msg,
|
||||
listen_send_cb, cbarg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
|
||||
isc_result_totext(eresult), cbarg);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_refcount_decrement(&active_sreads);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
listen_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
if (eresult != ISC_R_SUCCESS) {
|
||||
return (eresult);
|
||||
}
|
||||
|
||||
if (have_expected_saccepts(atomic_fetch_add(&saccepts, 1) + 1)) {
|
||||
do_saccepts_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
return (eresult);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
stream_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
if (eresult != ISC_R_SUCCESS) {
|
||||
return (eresult);
|
||||
}
|
||||
|
||||
if (have_expected_saccepts(atomic_fetch_add(&saccepts, 1) + 1)) {
|
||||
do_saccepts_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
isc_refcount_increment0(&active_sreads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, listen_read_cb, readhandle);
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
void
|
||||
stream_recv_send_connect(void *arg) {
|
||||
connect_func connect = (connect_func)arg;
|
||||
isc_sockaddr_t connect_addr;
|
||||
|
||||
connect_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
isc_sockaddr_fromin6(&connect_addr, &in6addr_loopback, 0);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
connect(connect_nm);
|
||||
}
|
||||
|
||||
/* Common stream protocols code */
|
||||
|
||||
void
|
||||
timeout_retry_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
F();
|
||||
|
||||
if (eresult == ISC_R_TIMEDOUT &&
|
||||
atomic_fetch_add(&ctimeouts, 1) + 1 < expected_ctimeouts)
|
||||
{
|
||||
isc_nmhandle_settimeout(handle, T_SOFT);
|
||||
connect_send(handle);
|
||||
return;
|
||||
}
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
isc_quota_t *
|
||||
tcp_listener_init_quota(size_t nthreads) {
|
||||
isc_quota_t *quotap = NULL;
|
||||
if (atomic_load(&check_listener_quota)) {
|
||||
unsigned max_quota = ISC_MAX(nthreads / 2, 1);
|
||||
isc_quota_max(&listener_quota, max_quota);
|
||||
quotap = &listener_quota;
|
||||
}
|
||||
return (quotap);
|
||||
}
|
||||
|
||||
static void
|
||||
tcp_connect(isc_nm_t *nm) {
|
||||
isc_nm_tcpconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
#if HAVE_LIBNGHTTP2
|
||||
static void
|
||||
tls_connect(isc_nm_t *nm) {
|
||||
isc_nm_tlsconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, tcp_connect_tlsctx,
|
||||
tcp_tlsctx_client_sess_cache, T_CONNECT);
|
||||
}
|
||||
#endif
|
||||
|
||||
stream_connect_function
|
||||
get_stream_connect_function(void) {
|
||||
#if HAVE_LIBNGHTTP2
|
||||
if (stream_use_TLS) {
|
||||
return (tls_connect);
|
||||
}
|
||||
#endif
|
||||
return (tcp_connect);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
stream_listen(isc_nm_accept_cb_t accept_cb, void *accept_cbarg, int backlog,
|
||||
isc_quota_t *quota, isc_nmsocket_t **sockp) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
#if HAVE_LIBNGHTTP2
|
||||
if (stream_use_TLS) {
|
||||
result = isc_nm_listentls(listen_nm, ISC_NM_LISTEN_ALL,
|
||||
&tcp_listen_addr, accept_cb,
|
||||
accept_cbarg, backlog, quota,
|
||||
tcp_listen_tlsctx, sockp);
|
||||
return (result);
|
||||
}
|
||||
#endif
|
||||
result = isc_nm_listentcp(listen_nm, ISC_NM_LISTEN_ALL,
|
||||
&tcp_listen_addr, accept_cb, accept_cbarg,
|
||||
backlog, quota, sockp);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
void
|
||||
stream_connect(isc_nm_cb_t cb, void *cbarg, unsigned int timeout) {
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
#if HAVE_LIBNGHTTP2
|
||||
if (stream_use_TLS) {
|
||||
isc_nm_tlsconnect(connect_nm, &tcp_connect_addr,
|
||||
&tcp_listen_addr, cb, cbarg,
|
||||
tcp_connect_tlsctx,
|
||||
tcp_tlsctx_client_sess_cache, timeout);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
isc_nm_tcpconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr, cb,
|
||||
cbarg, timeout);
|
||||
}
|
||||
|
||||
void
|
||||
connect_success_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) + 1)) {
|
||||
do_cconnects_shutdown(loopmgr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
stream_noop_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
expected_cconnects = 1;
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_noop(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
result = stream_listen(noop_accept_cb, NULL, 128, NULL, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
connect_readcb = NULL;
|
||||
stream_connect(connect_success_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
int
|
||||
stream_noop_teardown(void **state __attribute__((__unused__))) {
|
||||
atomic_assert_int_eq(cconnects, 1);
|
||||
atomic_assert_int_eq(csends, 0);
|
||||
atomic_assert_int_eq(creads, 0);
|
||||
atomic_assert_int_eq(sreads, 0);
|
||||
atomic_assert_int_eq(ssends, 0);
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
||||
|
||||
static void
|
||||
noresponse_readcb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
if (eresult == ISC_R_EOF) {
|
||||
eresult = ISC_R_CONNECTIONRESET;
|
||||
}
|
||||
|
||||
assert_int_equal(eresult, ISC_R_CONNECTIONRESET);
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
noresponse_sendcb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
UNUSED(cbarg);
|
||||
UNUSED(eresult);
|
||||
|
||||
assert_non_null(handle);
|
||||
atomic_fetch_add(&csends, 1);
|
||||
isc_nmhandle_detach(&handle);
|
||||
isc_refcount_decrement(&active_csends);
|
||||
}
|
||||
|
||||
static void
|
||||
noresponse_connectcb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
UNUSED(handle);
|
||||
|
||||
F();
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, noresponse_readcb, NULL);
|
||||
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
|
||||
isc_nm_send(handle, (isc_region_t *)&send_msg, noresponse_sendcb,
|
||||
cbarg);
|
||||
}
|
||||
|
||||
int
|
||||
stream_noresponse_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
expected_cconnects = 1;
|
||||
expected_saccepts = 1;
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_noresponse(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
result = stream_listen(noop_accept_cb, NULL, 128, NULL, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
stream_connect(noresponse_connectcb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
int
|
||||
stream_noresponse_teardown(void **state __attribute__((__unused__))) {
|
||||
X(cconnects);
|
||||
X(csends);
|
||||
X(creads);
|
||||
X(sreads);
|
||||
X(ssends);
|
||||
|
||||
atomic_assert_int_eq(cconnects, 1);
|
||||
atomic_assert_int_eq(creads, 0);
|
||||
atomic_assert_int_eq(sreads, 0);
|
||||
atomic_assert_int_eq(ssends, 0);
|
||||
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
||||
|
||||
int
|
||||
stream_timeout_recovery_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
|
||||
expected_ctimeouts = 4;
|
||||
ctimeouts_shutdown = false;
|
||||
|
||||
expected_sreads = 5;
|
||||
sreads_shutdown = true;
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_timeout_recovery(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
/*
|
||||
* Accept connections but don't send responses, forcing client
|
||||
* reads to time out.
|
||||
*/
|
||||
noanswer = true;
|
||||
result = stream_listen(stream_accept_cb, NULL, 128, NULL, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
/*
|
||||
* Shorten all the client timeouts to 0.05 seconds.
|
||||
*/
|
||||
isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT);
|
||||
connect_readcb = timeout_retry_cb;
|
||||
stream_connect(connect_connect_cb, NULL, T_SOFT);
|
||||
}
|
||||
|
||||
int
|
||||
stream_timeout_recovery_teardown(void **state __attribute__((__unused__))) {
|
||||
atomic_assert_int_eq(ctimeouts, expected_ctimeouts);
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_one_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
|
||||
expected_cconnects = 1;
|
||||
cconnects_shutdown = false;
|
||||
|
||||
expected_csends = 1;
|
||||
csends_shutdown = false;
|
||||
|
||||
expected_saccepts = 1;
|
||||
saccepts_shutdown = false;
|
||||
|
||||
expected_sreads = 1;
|
||||
sreads_shutdown = false;
|
||||
|
||||
expected_ssends = 1;
|
||||
ssends_shutdown = false;
|
||||
|
||||
expected_creads = 1;
|
||||
creads_shutdown = true;
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_recv_one(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
isc_quota_t *quotap = tcp_listener_init_quota(1);
|
||||
|
||||
atomic_store(&nsends, 1);
|
||||
|
||||
result = stream_listen(stream_accept_cb, NULL, 128, quotap,
|
||||
&listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
stream_connect(connect_connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_one_teardown(void **state __attribute__((__unused__))) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
atomic_assert_int_eq(saccepts, expected_saccepts);
|
||||
atomic_assert_int_eq(sreads, expected_sreads);
|
||||
atomic_assert_int_eq(ssends, expected_ssends);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_two_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
|
||||
expected_cconnects = 2;
|
||||
cconnects_shutdown = false;
|
||||
|
||||
expected_csends = 2;
|
||||
csends_shutdown = false;
|
||||
|
||||
expected_saccepts = 2;
|
||||
saccepts_shutdown = false;
|
||||
|
||||
expected_sreads = 2;
|
||||
sreads_shutdown = false;
|
||||
|
||||
expected_ssends = 2;
|
||||
ssends_shutdown = false;
|
||||
|
||||
expected_creads = 2;
|
||||
creads_shutdown = true;
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_recv_two(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
isc_quota_t *quotap = tcp_listener_init_quota(1);
|
||||
|
||||
atomic_store(&nsends, 2);
|
||||
|
||||
result = stream_listen(stream_accept_cb, NULL, 128, quotap,
|
||||
&listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
stream_connect(connect_connect_cb, NULL, T_CONNECT);
|
||||
|
||||
stream_connect(connect_connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_two_teardown(void **state __attribute__((__unused__))) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
atomic_assert_int_eq(sreads, expected_saccepts);
|
||||
atomic_assert_int_eq(sreads, expected_sreads);
|
||||
atomic_assert_int_eq(ssends, expected_ssends);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_send_setup(void **state __attribute__((__unused__))) {
|
||||
int r = setup_netmgr_test(state);
|
||||
expected_cconnects = workers;
|
||||
cconnects_shutdown = false;
|
||||
nsends = expected_creads = workers;
|
||||
do_send = true;
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
void
|
||||
stream_recv_send(void **state __attribute__((__unused__))) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
isc_quota_t *quotap = tcp_listener_init_quota(workers);
|
||||
|
||||
result = stream_listen(stream_accept_cb, NULL, 128, quotap,
|
||||
&listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
|
||||
for (size_t i = 0; i < workers; i++) {
|
||||
isc_async_run(isc_loop_get(loopmgr, i),
|
||||
stream_recv_send_connect,
|
||||
get_stream_connect_function());
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
stream_recv_send_teardown(void **state __attribute__((__unused__))) {
|
||||
X(cconnects);
|
||||
X(csends);
|
||||
X(creads);
|
||||
X(sreads);
|
||||
X(ssends);
|
||||
|
||||
CHECK_RANGE_FULL(csends);
|
||||
CHECK_RANGE_FULL(creads);
|
||||
CHECK_RANGE_FULL(sreads);
|
||||
CHECK_RANGE_FULL(ssends);
|
||||
|
||||
return (teardown_netmgr_test(state));
|
||||
}
|
317
tests/isc/netmgr_common.h
Normal file
317
tests/isc/netmgr_common.h
Normal file
@@ -0,0 +1,317 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <isc/atomic.h>
|
||||
#include <isc/netmgr.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/thread.h>
|
||||
|
||||
#include "netmgr/netmgr-int.h"
|
||||
|
||||
typedef void (*stream_connect_function)(isc_nm_t *nm);
|
||||
typedef void (*connect_func)(isc_nm_t *);
|
||||
|
||||
extern isc_nm_t *listen_nm;
|
||||
extern isc_nm_t *connect_nm;
|
||||
|
||||
extern isc_sockaddr_t tcp_listen_addr;
|
||||
extern isc_sockaddr_t tcp_connect_addr;
|
||||
extern isc_tlsctx_t *tcp_listen_tlsctx;
|
||||
extern isc_tlsctx_t *tcp_connect_tlsctx;
|
||||
extern isc_tlsctx_client_session_cache_t *tcp_tlsctx_client_sess_cache;
|
||||
|
||||
extern uint64_t send_magic;
|
||||
extern uint64_t stop_magic;
|
||||
|
||||
extern isc_region_t send_msg;
|
||||
extern isc_region_t stop_msg;
|
||||
|
||||
extern atomic_bool do_send;
|
||||
|
||||
extern atomic_int_fast64_t nsends;
|
||||
extern int_fast64_t esends; /* expected sends */
|
||||
|
||||
extern atomic_int_fast64_t ssends;
|
||||
extern atomic_int_fast64_t sreads;
|
||||
extern atomic_int_fast64_t saccepts;
|
||||
|
||||
extern atomic_int_fast64_t cconnects;
|
||||
extern atomic_int_fast64_t csends;
|
||||
extern atomic_int_fast64_t creads;
|
||||
extern atomic_int_fast64_t ctimeouts;
|
||||
|
||||
extern int expected_ssends;
|
||||
extern int expected_sreads;
|
||||
extern int expected_csends;
|
||||
extern int expected_cconnects;
|
||||
extern int expected_creads;
|
||||
extern int expected_ctimeouts;
|
||||
|
||||
extern bool ssends_shutdown;
|
||||
extern bool sreads_shutdown;
|
||||
extern bool csends_shutdown;
|
||||
extern bool cconnects_shutdown;
|
||||
extern bool creads_shutdown;
|
||||
extern bool ctimeouts_shutdown;
|
||||
|
||||
#define have_expected_ssends(v) ((v) >= expected_ssends && expected_ssends >= 0)
|
||||
#define have_expected_sreads(v) ((v) >= expected_sreads && expected_sreads >= 0)
|
||||
#define have_expected_saccepts(v) \
|
||||
((v) >= expected_saccepts && expected_saccepts >= 0)
|
||||
#define have_expected_csends(v) ((v) >= expected_csends && expected_csends >= 0)
|
||||
#define have_expected_cconnects(v) \
|
||||
((v) >= expected_cconnects && expected_cconnects >= 0)
|
||||
#define have_expected_creads(v) ((v) >= expected_creads && expected_creads >= 0)
|
||||
#define have_expected_ctimeouts(v) \
|
||||
((v) >= expected_ctimeouts && expected_ctimeouts >= 0)
|
||||
|
||||
#define do_ssends_shutdown(lm) \
|
||||
if (ssends_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_sreads_shutdown(lm) \
|
||||
if (sreads_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_saccepts_shutdown(lm) \
|
||||
if (saccepts_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_csends_shutdown(lm) \
|
||||
if (csends_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_cconnects_shutdown(lm) \
|
||||
if (cconnects_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_creads_shutdown(lm) \
|
||||
if (creads_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
#define do_ctimeouts_shutdown(lm) \
|
||||
if (ctimeouts_shutdown) { \
|
||||
isc_loopmgr_shutdown(lm); \
|
||||
}
|
||||
|
||||
extern isc_refcount_t active_cconnects;
|
||||
extern isc_refcount_t active_csends;
|
||||
extern isc_refcount_t active_creads;
|
||||
extern isc_refcount_t active_ssends;
|
||||
extern isc_refcount_t active_sreads;
|
||||
|
||||
extern isc_nmsocket_t *listen_sock;
|
||||
|
||||
extern isc_quota_t listener_quota;
|
||||
extern atomic_bool check_listener_quota;
|
||||
|
||||
extern bool allow_send_back;
|
||||
extern bool noanswer;
|
||||
extern bool stream_use_TLS;
|
||||
|
||||
extern isc_nm_recv_cb_t connect_readcb;
|
||||
|
||||
#define NSENDS 100
|
||||
|
||||
/* Timeout for soft-timeout tests (0.05 seconds) */
|
||||
#define T_SOFT 50
|
||||
|
||||
/* Timeouts in miliseconds */
|
||||
#define T_INIT 120 * 1000
|
||||
#define T_IDLE 120 * 1000
|
||||
#define T_KEEPALIVE 120 * 1000
|
||||
#define T_ADVERTISED 120 * 1000
|
||||
#define T_CONNECT 30 * 1000
|
||||
|
||||
/* Wait for 1 second (1000 milliseconds) */
|
||||
#define WAIT_REPEATS 1000
|
||||
#define T_WAIT 1 /* 1 millisecond */
|
||||
|
||||
#define WAIT_FOR(v, op, val) \
|
||||
{ \
|
||||
X(v); \
|
||||
int_fast64_t __r = WAIT_REPEATS; \
|
||||
int_fast64_t __o = 0; \
|
||||
do { \
|
||||
int_fast64_t __l = atomic_load(&v); \
|
||||
if (__l op val) { \
|
||||
break; \
|
||||
}; \
|
||||
if (__o == __l) { \
|
||||
__r--; \
|
||||
} else { \
|
||||
__r = WAIT_REPEATS; \
|
||||
} \
|
||||
__o = __l; \
|
||||
uv_sleep(T_WAIT); \
|
||||
} while (__r > 0); \
|
||||
X(v); \
|
||||
P(__r); \
|
||||
assert_true(atomic_load(&v) op val); \
|
||||
}
|
||||
|
||||
#define WAIT_FOR_EQ(v, val) WAIT_FOR(v, ==, val)
|
||||
#define WAIT_FOR_NE(v, val) WAIT_FOR(v, !=, val)
|
||||
#define WAIT_FOR_LE(v, val) WAIT_FOR(v, <=, val)
|
||||
#define WAIT_FOR_LT(v, val) WAIT_FOR(v, <, val)
|
||||
#define WAIT_FOR_GE(v, val) WAIT_FOR(v, >=, val)
|
||||
#define WAIT_FOR_GT(v, val) WAIT_FOR(v, >, val)
|
||||
|
||||
#define DONE() atomic_store(&do_send, false);
|
||||
|
||||
#define CHECK_RANGE_FULL(v) \
|
||||
{ \
|
||||
int __v = atomic_load(&v); \
|
||||
assert_true(__v > 1); \
|
||||
}
|
||||
|
||||
#define CHECK_RANGE_HALF(v) \
|
||||
{ \
|
||||
int __v = atomic_load(&v); \
|
||||
assert_true(__v > 1); \
|
||||
}
|
||||
|
||||
/* Enable this to print values while running tests */
|
||||
#undef PRINT_DEBUG
|
||||
#ifdef PRINT_DEBUG
|
||||
#define X(v) \
|
||||
fprintf(stderr, "%s:%s:%d:%s = %" PRId64 "\n", __func__, __FILE__, \
|
||||
__LINE__, #v, atomic_load(&v))
|
||||
#define P(v) fprintf(stderr, #v " = %" PRId64 "\n", v)
|
||||
#define F() \
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle, \
|
||||
isc_result_totext(eresult), cbarg)
|
||||
|
||||
#define isc_loopmgr_shutdown(loopmgr) \
|
||||
{ \
|
||||
fprintf(stderr, "%s:%s:%d:isc_loopmgr_shutdown(%p)\n", \
|
||||
__func__, __FILE__, __LINE__, loopmgr); \
|
||||
isc_loopmgr_shutdown(loopmgr); \
|
||||
}
|
||||
#else
|
||||
#define X(v)
|
||||
#define P(v)
|
||||
#define F()
|
||||
#endif
|
||||
|
||||
#define atomic_assert_int_eq(val, exp) assert_int_equal(atomic_load(&val), exp)
|
||||
#define atomic_assert_int_ne(val, exp) \
|
||||
assert_int_not_equal(atomic_load(&val), exp)
|
||||
#define atomic_assert_int_le(val, exp) assert_true(atomic_load(&val) <= exp)
|
||||
#define atomic_assert_int_lt(val, exp) assert_true(atomic_load(&val) > exp)
|
||||
#define atomic_assert_int_ge(val, exp) assert_true(atomic_load(&val) >= exp)
|
||||
#define atomic_assert_int_gt(val, exp) assert_true(atomic_load(&val) > exp)
|
||||
|
||||
int
|
||||
setup_ephemeral_port(isc_sockaddr_t *addr, sa_family_t family);
|
||||
|
||||
int
|
||||
setup_netmgr_test(void **state);
|
||||
int
|
||||
teardown_netmgr_test(void **state __attribute__((unused)));
|
||||
|
||||
void
|
||||
noop_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
|
||||
void *cbarg);
|
||||
|
||||
unsigned int
|
||||
noop_accept_cb(isc_nmhandle_t *handle, unsigned int result, void *cbarg);
|
||||
|
||||
void
|
||||
connect_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
void
|
||||
connect_send(isc_nmhandle_t *handle);
|
||||
|
||||
void
|
||||
connect_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg);
|
||||
|
||||
void
|
||||
connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
void
|
||||
connect_success_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
void
|
||||
listen_send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
void
|
||||
listen_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg);
|
||||
|
||||
isc_result_t
|
||||
listen_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
isc_result_t
|
||||
stream_accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
|
||||
void
|
||||
timeout_retry_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg);
|
||||
|
||||
isc_quota_t *
|
||||
tcp_listener_init_quota(size_t nthreads);
|
||||
|
||||
stream_connect_function
|
||||
get_stream_connect_function(void);
|
||||
|
||||
isc_result_t
|
||||
stream_listen(isc_nm_accept_cb_t accept_cb, void *accept_cbarg, int backlog,
|
||||
isc_quota_t *quota, isc_nmsocket_t **sockp);
|
||||
|
||||
void
|
||||
stream_connect(isc_nm_cb_t cb, void *cbarg, unsigned int timeout);
|
||||
|
||||
int
|
||||
stream_noop_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_noop(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_noop_teardown(void **state __attribute__((unused)));
|
||||
|
||||
int
|
||||
stream_noresponse_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_noresponse(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_noresponse_teardown(void **state __attribute__((unused)));
|
||||
|
||||
int
|
||||
stream_timeout_recovery_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_timeout_recovery(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_timeout_recovery_teardown(void **state __attribute__((unused)));
|
||||
|
||||
int
|
||||
stream_recv_one_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_recv_one(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_recv_one_teardown(void **state __attribute__((unused)));
|
||||
|
||||
int
|
||||
stream_recv_two_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_recv_two(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_recv_two_teardown(void **state __attribute__((unused)));
|
||||
|
||||
int
|
||||
stream_recv_send_setup(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_recv_send(void **state __attribute__((unused)));
|
||||
int
|
||||
stream_recv_send_teardown(void **state __attribute__((unused)));
|
||||
void
|
||||
stream_recv_send_connect(void *arg);
|
File diff suppressed because it is too large
Load Diff
133
tests/isc/tcp_test.c
Normal file
133
tests/isc/tcp_test.c
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/loop.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/thread.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/uv.h>
|
||||
|
||||
#include "uv_wrap.h"
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr/tcp.c"
|
||||
#include "netmgr_common.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
/* TCP */
|
||||
ISC_LOOP_TEST_IMPL(tcp_noop) {
|
||||
stream_noop(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_noresponse) {
|
||||
stream_noresponse(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_timeout_recovery) {
|
||||
stream_timeout_recovery(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_one) {
|
||||
stream_recv_one(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_two) {
|
||||
stream_recv_two(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_send) {
|
||||
stream_recv_send(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_send_sendback) {
|
||||
allow_send_back = true;
|
||||
stream_recv_send(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
/* TCP Quota */
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_one_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_one(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_two_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_two(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_send_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_send(arg);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcp_recv_send_quota_sendback) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
allow_send_back = true;
|
||||
stream_recv_send(arg);
|
||||
}
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
/* TCP */
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_noop, stream_noop_setup, stream_noop_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_noresponse, stream_noresponse_setup,
|
||||
stream_noresponse_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_timeout_recovery, stream_timeout_recovery_setup,
|
||||
stream_timeout_recovery_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_one, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_two, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_send, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_send_sendback, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
|
||||
/* TCP Quota */
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_one_quota, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_two_quota, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_send_quota, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcp_recv_send_quota_sendback, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
|
||||
ISC_TEST_LIST_END
|
||||
|
||||
ISC_TEST_MAIN
|
145
tests/isc/tcpdns_test.c
Normal file
145
tests/isc/tcpdns_test.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/async.h>
|
||||
#include <isc/loop.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/uv.h>
|
||||
|
||||
#include "uv_wrap.h"
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr_common.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
/* TCPDNS */
|
||||
|
||||
static void
|
||||
stop_listening(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
start_listening(uint32_t nworkers, isc_nm_accept_cb_t accept_cb,
|
||||
isc_nm_recv_cb_t recv_cb) {
|
||||
isc_result_t result = isc_nm_listentcpdns(
|
||||
listen_nm, nworkers, &tcp_listen_addr, recv_cb, NULL, accept_cb,
|
||||
NULL, 128, NULL, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
tcpdns_connect(isc_nm_t *nm) {
|
||||
isc_nm_tcpdnsconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_noop) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, noop_accept_cb, noop_recv_cb);
|
||||
|
||||
connect_readcb = NULL;
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_tcpdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_success_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_noresponse) {
|
||||
start_listening(ISC_NM_LISTEN_ALL, noop_accept_cb, noop_recv_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_tcpdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_timeout_recovery) {
|
||||
/*
|
||||
* Accept connections but don't send responses, forcing client
|
||||
* reads to time out.
|
||||
*/
|
||||
noanswer = true;
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
/*
|
||||
* Shorten all the TCP client timeouts to 0.05 seconds, connect,
|
||||
* then sleep for at least a second for each 'tick'.
|
||||
* timeout_retry_cb() will give up after five timeouts.
|
||||
*/
|
||||
connect_readcb = timeout_retry_cb;
|
||||
isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT);
|
||||
|
||||
isc_async_run(isc_loop_current(loopmgr), stream_recv_send_connect,
|
||||
tcpdns_connect);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_recv_one) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
isc_async_run(isc_loop_current(loopmgr), stream_recv_send_connect,
|
||||
tcpdns_connect);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_recv_two) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
isc_async_run(isc_loop_current(loopmgr), stream_recv_send_connect,
|
||||
tcpdns_connect);
|
||||
|
||||
isc_async_run(isc_loop_current(loopmgr), stream_recv_send_connect,
|
||||
tcpdns_connect);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tcpdns_recv_send) {
|
||||
start_listening(ISC_NM_LISTEN_ALL, listen_accept_cb, listen_read_cb);
|
||||
|
||||
for (size_t i = 0; i < workers; i++) {
|
||||
isc_async_run(isc_loop_get(loopmgr, i),
|
||||
stream_recv_send_connect, tcpdns_connect);
|
||||
}
|
||||
}
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_noop, stream_noop_setup, stream_noop_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_noresponse, stream_noresponse_setup,
|
||||
stream_noresponse_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_timeout_recovery, stream_timeout_recovery_setup,
|
||||
stream_timeout_recovery_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_one, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_two, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_send, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
|
||||
ISC_TEST_LIST_END
|
||||
ISC_TEST_MAIN
|
139
tests/isc/tls_test.c
Normal file
139
tests/isc/tls_test.c
Normal file
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/loop.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/thread.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/uv.h>
|
||||
|
||||
#include "uv_wrap.h"
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr_common.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
#if HAVE_LIBNGHTTP2
|
||||
ISC_LOOP_TEST_IMPL(tls_noop) {
|
||||
stream_noop(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_noresponse) {
|
||||
stream_noresponse(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_timeout_recovery) {
|
||||
stream_timeout_recovery(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_one) {
|
||||
stream_recv_one(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_two) {
|
||||
stream_recv_two(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_send) {
|
||||
stream_recv_send(arg);
|
||||
return;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_send_sendback) {
|
||||
allow_send_back = true;
|
||||
stream_recv_send(arg);
|
||||
}
|
||||
|
||||
/* TLS quota */
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_one_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_one(arg);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_two_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_two(arg);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_send_quota) {
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_send(arg);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tls_recv_send_quota_sendback) {
|
||||
allow_send_back = true;
|
||||
atomic_store(&check_listener_quota, true);
|
||||
stream_recv_send(arg);
|
||||
}
|
||||
#endif
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
#if HAVE_LIBNGHTTP2
|
||||
/* TLS */
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_noop, stream_noop_setup, stream_noop_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_noresponse, stream_noresponse_setup,
|
||||
stream_noresponse_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_timeout_recovery, stream_timeout_recovery_setup,
|
||||
stream_timeout_recovery_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_one, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_two, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_send, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_send_sendback, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
|
||||
/* TLS quota */
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_one_quota, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_two_quota, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_send_quota, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tls_recv_send_quota_sendback, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
#endif
|
||||
|
||||
ISC_TEST_LIST_END
|
||||
|
||||
static int
|
||||
tls_setup(void **state __attribute__((__unused__))) {
|
||||
stream_use_TLS = true;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEST_MAIN_CUSTOM(tls_setup, NULL)
|
150
tests/isc/tlsdns_test.c
Normal file
150
tests/isc/tlsdns_test.c
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/async.h>
|
||||
#include <isc/loop.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/thread.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/uv.h>
|
||||
|
||||
#include "uv_wrap.h"
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr_common.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
static void
|
||||
stop_listening(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
start_listening(uint32_t nworkers, isc_nm_accept_cb_t accept_cb,
|
||||
isc_nm_recv_cb_t recv_cb) {
|
||||
isc_result_t result = isc_nm_listentlsdns(
|
||||
listen_nm, nworkers, &tcp_listen_addr, recv_cb, NULL, accept_cb,
|
||||
NULL, 128, NULL, tcp_listen_tlsctx, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
tlsdns_connect(isc_nm_t *nm) {
|
||||
isc_nm_tlsdnsconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_CONNECT,
|
||||
tcp_connect_tlsctx, tcp_tlsctx_client_sess_cache);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_noop) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, noop_accept_cb, noop_recv_cb);
|
||||
|
||||
connect_readcb = NULL;
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_tlsdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_success_cb, NULL, T_CONNECT,
|
||||
tcp_connect_tlsctx, tcp_tlsctx_client_sess_cache);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_noresponse) {
|
||||
start_listening(ISC_NM_LISTEN_ALL, noop_accept_cb, noop_recv_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_tlsdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_CONNECT,
|
||||
tcp_connect_tlsctx, tcp_tlsctx_client_sess_cache);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_timeout_recovery) {
|
||||
/*
|
||||
* Accept connections but don't send responses, forcing client
|
||||
* reads to time out.
|
||||
*/
|
||||
noanswer = true;
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
/*
|
||||
* Shorten all the TCP client timeouts to 0.05 seconds, connect,
|
||||
* then sleep for at least a second for each 'tick'.
|
||||
* timeout_retry_cb() will give up after five timeouts.
|
||||
*/
|
||||
connect_readcb = timeout_retry_cb;
|
||||
isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT);
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_tlsdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
|
||||
connect_connect_cb, NULL, T_SOFT,
|
||||
tcp_connect_tlsctx, tcp_tlsctx_client_sess_cache);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_recv_one) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
tlsdns_connect(connect_nm);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_recv_two) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
tlsdns_connect(connect_nm);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
tlsdns_connect(connect_nm);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(tlsdns_recv_send) {
|
||||
start_listening(ISC_NM_LISTEN_ALL, listen_accept_cb, listen_read_cb);
|
||||
|
||||
for (size_t i = 0; i < workers; i++) {
|
||||
isc_async_run(isc_loop_get(loopmgr, i),
|
||||
stream_recv_send_connect, tlsdns_connect);
|
||||
}
|
||||
}
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_noop, stream_noop_setup, stream_noop_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_noresponse, stream_noresponse_setup,
|
||||
stream_noresponse_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_timeout_recovery, stream_timeout_recovery_setup,
|
||||
stream_timeout_recovery_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_one, stream_recv_one_setup,
|
||||
stream_recv_one_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_two, stream_recv_two_setup,
|
||||
stream_recv_two_teardown)
|
||||
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_send, stream_recv_send_setup,
|
||||
stream_recv_send_teardown)
|
||||
|
||||
/* FIXME: Re-add the noalpn tests */
|
||||
|
||||
ISC_TEST_LIST_END
|
||||
ISC_TEST_MAIN
|
965
tests/isc/udp_test.c
Normal file
965
tests/isc/udp_test.c
Normal file
@@ -0,0 +1,965 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* See the COPYRIGHT file distributed with this work for additional
|
||||
* information regarding copyright ownership.
|
||||
*/
|
||||
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/async.h>
|
||||
#include <isc/job.h>
|
||||
#include <isc/nonce.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/quota.h>
|
||||
#include <isc/refcount.h>
|
||||
#include <isc/sockaddr.h>
|
||||
#include <isc/thread.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include "uv_wrap.h"
|
||||
#define KEEP_BEFORE
|
||||
|
||||
#include "netmgr/socket.c"
|
||||
#include "netmgr/udp.c"
|
||||
#include "netmgr_common.h"
|
||||
#include "uv.c"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
static isc_sockaddr_t udp_listen_addr;
|
||||
static isc_sockaddr_t udp_connect_addr;
|
||||
|
||||
/* Timeout for soft-timeout tests (0.05 seconds) */
|
||||
#define T_SOFT 50
|
||||
|
||||
/* Timeouts in miliseconds */
|
||||
#define T_INIT 120 * 1000
|
||||
#define T_IDLE 120 * 1000
|
||||
#define T_KEEPALIVE 120 * 1000
|
||||
#define T_ADVERTISED 120 * 1000
|
||||
#define T_CONNECT 30 * 1000
|
||||
|
||||
static int
|
||||
setup_test(void **state) {
|
||||
uv_os_sock_t udp_listen_sock = -1;
|
||||
|
||||
setup_loopmgr(state);
|
||||
setup_netmgr(state);
|
||||
|
||||
udp_connect_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
isc_sockaddr_fromin6(&udp_connect_addr, &in6addr_loopback, 0);
|
||||
|
||||
udp_listen_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
udp_listen_sock = setup_ephemeral_port(&udp_listen_addr, SOCK_DGRAM);
|
||||
if (udp_listen_sock < 0) {
|
||||
return (-1);
|
||||
}
|
||||
isc__nm_closesocket(udp_listen_sock);
|
||||
|
||||
atomic_store(&sreads, 0);
|
||||
atomic_store(&ssends, 0);
|
||||
|
||||
atomic_store(&cconnects, 0);
|
||||
atomic_store(&csends, 0);
|
||||
atomic_store(&creads, 0);
|
||||
atomic_store(&ctimeouts, 0);
|
||||
|
||||
isc_refcount_init(&active_cconnects, 0);
|
||||
isc_refcount_init(&active_csends, 0);
|
||||
isc_refcount_init(&active_creads, 0);
|
||||
isc_refcount_init(&active_ssends, 0);
|
||||
isc_refcount_init(&active_sreads, 0);
|
||||
|
||||
expected_cconnects = -1;
|
||||
expected_csends = -1;
|
||||
expected_creads = -1;
|
||||
expected_sreads = -1;
|
||||
expected_ssends = -1;
|
||||
expected_ctimeouts = -1;
|
||||
|
||||
ssends_shutdown = true;
|
||||
sreads_shutdown = true;
|
||||
csends_shutdown = true;
|
||||
cconnects_shutdown = true;
|
||||
creads_shutdown = true;
|
||||
|
||||
isc_nonce_buf(&send_magic, sizeof(send_magic));
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int
|
||||
teardown_test(void **state) {
|
||||
UNUSED(state);
|
||||
|
||||
isc_refcount_destroy(&active_cconnects);
|
||||
isc_refcount_destroy(&active_csends);
|
||||
isc_refcount_destroy(&active_creads);
|
||||
isc_refcount_destroy(&active_ssends);
|
||||
isc_refcount_destroy(&active_sreads);
|
||||
|
||||
teardown_netmgr(state);
|
||||
teardown_loopmgr(state);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* Callbacks */
|
||||
|
||||
static void
|
||||
mock_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
|
||||
void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(eresult);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
}
|
||||
|
||||
static void
|
||||
connect_nomemory_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
assert_int_equal(eresult, ISC_R_NOMEMORY);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
stop_listening(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_stoplistening(listen_sock);
|
||||
isc_nmsocket_close(&listen_sock);
|
||||
assert_null(listen_sock);
|
||||
}
|
||||
|
||||
static void
|
||||
start_listening(uint32_t nworkers, isc_nm_recv_cb_t cb) {
|
||||
isc_result_t result = isc_nm_listenudp(
|
||||
netmgr, nworkers, &udp_listen_addr, cb, NULL, &listen_sock);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
isc_loop_teardown(mainloop, stop_listening, listen_sock);
|
||||
}
|
||||
|
||||
/* UDP */
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_open) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
WILL_RETURN(uv_udp_open, UV_ENOMEM);
|
||||
|
||||
result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
|
||||
mock_recv_cb, NULL, &listen_sock);
|
||||
assert_int_not_equal(result, ISC_R_SUCCESS);
|
||||
assert_null(listen_sock);
|
||||
|
||||
RESET_RETURN;
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_bind) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
WILL_RETURN(uv_udp_bind, UV_EADDRINUSE);
|
||||
|
||||
result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
|
||||
mock_recv_cb, NULL, &listen_sock);
|
||||
assert_int_not_equal(result, ISC_R_SUCCESS);
|
||||
assert_null(listen_sock);
|
||||
|
||||
RESET_RETURN;
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_recv_start) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
|
||||
WILL_RETURN(uv_udp_recv_start, UV_EADDRINUSE);
|
||||
|
||||
result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
|
||||
mock_recv_cb, NULL, &listen_sock);
|
||||
assert_int_not_equal(result, ISC_R_SUCCESS);
|
||||
assert_null(listen_sock);
|
||||
|
||||
RESET_RETURN;
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_open) {
|
||||
WILL_RETURN(uv_udp_open, UV_ENOMEM);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_nomemory_cb, NULL, T_CONNECT);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
|
||||
RESET_RETURN;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_bind) {
|
||||
WILL_RETURN(uv_udp_bind, UV_ENOMEM);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_nomemory_cb, NULL, T_CONNECT);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
|
||||
RESET_RETURN;
|
||||
}
|
||||
|
||||
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
|
||||
ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_connect) {
|
||||
WILL_RETURN(uv_udp_connect, UV_ENOMEM);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_nomemory_cb, NULL, T_CONNECT);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
|
||||
RESET_RETURN;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_recv_buffer_size) {
|
||||
WILL_RETURN(uv_recv_buffer_size, UV_ENOMEM);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_success_cb, NULL, T_CONNECT);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
|
||||
RESET_RETURN;
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_send_buffer_size) {
|
||||
WILL_RETURN(uv_send_buffer_size, UV_ENOMEM);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_success_cb, NULL, T_CONNECT);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
|
||||
RESET_RETURN;
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_noop) {
|
||||
setup_test(state);
|
||||
expected_cconnects = 1;
|
||||
cconnects_shutdown = true;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_noop) {
|
||||
atomic_assert_int_eq(cconnects, 1);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_noop) {
|
||||
/* isc_result_t result = ISC_R_SUCCESS; */
|
||||
|
||||
/* result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL,
|
||||
* &udp_listen_addr, */
|
||||
/* mock_recv_cb, NULL, &listen_sock); */
|
||||
/* assert_int_equal(result, ISC_R_SUCCESS); */
|
||||
|
||||
/* isc_nm_stoplistening(listen_sock); */
|
||||
/* isc_nmsocket_close(&listen_sock); */
|
||||
/* assert_null(listen_sock); */
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
connect_success_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_noresponse_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(eresult);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_noresponse_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_TIMEDOUT);
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
|
||||
atomic_fetch_add(&creads, 1);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_noresponse_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(handle);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
atomic_fetch_add(&csends, 1);
|
||||
isc_nmhandle_detach(&handle);
|
||||
isc_refcount_decrement(&active_csends);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_noresponse_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
/* Read */
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, udp_noresponse_read_cb, cbarg);
|
||||
|
||||
/* Send */
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
|
||||
udp_noresponse_send_cb, cbarg);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_noresponse) {
|
||||
setup_test(state);
|
||||
expected_csends = 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_noresponse) {
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_noresponse) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, udp_noresponse_recv_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp_noresponse_connect_cb, listen_sock, T_SOFT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_timeout_recovery_ssend_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_ssends);
|
||||
assert_non_null(handle);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
atomic_fetch_add(&ssends, 1);
|
||||
isc_nmhandle_detach(&handle);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_timeout_recovery_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
uint64_t magic = 0;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
int _creads = atomic_fetch_add(&creads, 1) + 1;
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
assert_true(region->length == sizeof(magic));
|
||||
|
||||
memmove(&magic, region->base, sizeof(magic));
|
||||
assert_true(magic == send_magic);
|
||||
assert_true(_creads < 6);
|
||||
|
||||
if (_creads == 5) {
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_refcount_increment0(&active_ssends);
|
||||
isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
|
||||
udp_timeout_recovery_ssend_cb, cbarg);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
udp_timeout_recovery_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
F();
|
||||
|
||||
if (eresult == ISC_R_TIMEDOUT &&
|
||||
atomic_fetch_add(&ctimeouts, 1) + 1 < expected_ctimeouts)
|
||||
{
|
||||
isc_nmhandle_settimeout(handle, T_SOFT);
|
||||
return;
|
||||
}
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
atomic_fetch_add(&creads, 1);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_timeout_recovery_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(handle);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
atomic_fetch_add(&csends, 1);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
isc_refcount_decrement(&active_csends);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_timeout_recovery_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
/* Read */
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, udp_timeout_recovery_read_cb, cbarg);
|
||||
|
||||
/* Send */
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
|
||||
udp_timeout_recovery_send_cb, cbarg);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_timeout_recovery) {
|
||||
setup_test(state);
|
||||
expected_cconnects = 1;
|
||||
expected_csends = 1;
|
||||
expected_creads = 1;
|
||||
expected_ctimeouts = 4;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_timeout_recovery) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
atomic_assert_int_eq(csends, expected_creads);
|
||||
atomic_assert_int_eq(ctimeouts, expected_ctimeouts);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_timeout_recovery) {
|
||||
/*
|
||||
* Listen using the noop callback so that client reads will time out.
|
||||
*/
|
||||
start_listening(ISC_NM_LISTEN_ONE, udp_timeout_recovery_recv_cb);
|
||||
|
||||
/*
|
||||
* Connect with client timeout set to 0.05 seconds, then sleep for at
|
||||
* least a second for each 'tick'. timeout_retry_cb() will give up
|
||||
* after five timeouts.
|
||||
*/
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp_timeout_recovery_connect_cb, listen_sock, T_SOFT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_shutdown_connect_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SHUTTINGDOWN);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_connect_udpconnect(void *arg __attribute__((__unused__))) {
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp_shutdown_connect_connect_cb, NULL, T_SOFT);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_shutdown_connect) {
|
||||
setup_test(state);
|
||||
expected_cconnects = 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_shutdown_connect) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_shutdown_connect) {
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_job_run(loopmgr, udp_connect_udpconnect, netmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_shutdown_read_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SHUTTINGDOWN);
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
|
||||
atomic_fetch_add(&creads, 1);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_shutdown_read_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, udp_shutdown_read_read_cb, cbarg);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_shutdown_read) {
|
||||
setup_test(state);
|
||||
expected_cconnects = 1;
|
||||
expected_creads = 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_shutdown_read) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_shutdown_read) {
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp_shutdown_read_connect_cb, NULL, T_SOFT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_cancel_read_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
uint64_t magic = 0;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
assert_non_null(handle);
|
||||
|
||||
F();
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
assert_true(region->length == sizeof(magic));
|
||||
|
||||
memmove(&magic, region->base, sizeof(magic));
|
||||
assert_true(magic == send_magic);
|
||||
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_refcount_increment0(&active_ssends);
|
||||
isc_nmhandle_setwritetimeout(sendhandle, T_IDLE);
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg, listen_send_cb,
|
||||
cbarg);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_cancel_read_read_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
isc_region_t *region, void *cbarg) {
|
||||
UNUSED(handle);
|
||||
UNUSED(region);
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_TIMEDOUT:
|
||||
case ISC_R_EOF:
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
|
||||
isc_refcount_decrement(&active_creads);
|
||||
|
||||
atomic_fetch_add(&creads, 1);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_cancel_read_send_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
assert_non_null(handle);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
atomic_fetch_add(&csends, 1);
|
||||
|
||||
isc_nmhandle_detach(&handle);
|
||||
isc_refcount_decrement(&active_csends);
|
||||
}
|
||||
|
||||
static void
|
||||
udp_cancel_read_connect_cb(isc_nmhandle_t *handle, isc_result_t eresult,
|
||||
void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
UNUSED(handle);
|
||||
UNUSED(cbarg);
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, udp_cancel_read_read_cb, cbarg);
|
||||
|
||||
/* Send */
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg,
|
||||
udp_cancel_read_send_cb, readhandle);
|
||||
|
||||
isc_nm_cancelread(readhandle);
|
||||
|
||||
atomic_fetch_add(&cconnects, 1);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_cancel_read) {
|
||||
setup_test(state);
|
||||
expected_cconnects = 1;
|
||||
expected_creads = 1;
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_cancel_read) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_cancel_read) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, udp_cancel_read_recv_cb);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp_cancel_read_connect_cb, NULL, T_SOFT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp__send_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *sendhandle = handle;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
assert_non_null(sendhandle);
|
||||
|
||||
F();
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_SUCCESS:
|
||||
if (have_expected_csends(atomic_fetch_add(&csends, 1) + 1)) {
|
||||
if (csends_shutdown) {
|
||||
isc_nm_cancelread(handle);
|
||||
isc_loopmgr_shutdown(loopmgr);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ISC_R_CANCELED:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
|
||||
isc_result_totext(eresult), cbarg);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_nmhandle_detach(&sendhandle);
|
||||
isc_refcount_decrement(&active_csends);
|
||||
}
|
||||
|
||||
static void
|
||||
udp__connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg);
|
||||
static void
|
||||
udp__connect(void *arg __attribute__((__unused__))) {
|
||||
isc_sockaddr_t connect_addr;
|
||||
|
||||
connect_addr = (isc_sockaddr_t){ .length = 0 };
|
||||
isc_sockaddr_fromin6(&connect_addr, &in6addr_loopback, 0);
|
||||
|
||||
isc_refcount_increment0(&active_cconnects);
|
||||
|
||||
isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
|
||||
udp__connect_cb, NULL, T_CONNECT);
|
||||
}
|
||||
|
||||
static void
|
||||
udp__connect_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *cbarg) {
|
||||
isc_nmhandle_t *readhandle = NULL;
|
||||
isc_nmhandle_t *sendhandle = NULL;
|
||||
|
||||
UNUSED(cbarg);
|
||||
|
||||
F();
|
||||
|
||||
isc_refcount_decrement(&active_cconnects);
|
||||
|
||||
switch (eresult) {
|
||||
case ISC_R_SUCCESS:
|
||||
if (have_expected_cconnects(atomic_fetch_add(&cconnects, 1) +
|
||||
1)) {
|
||||
do_cconnects_shutdown(loopmgr);
|
||||
} else if (do_send) {
|
||||
isc_job_run(loopmgr, udp__connect, cbarg);
|
||||
}
|
||||
|
||||
isc_refcount_increment0(&active_creads);
|
||||
isc_nmhandle_attach(handle, &readhandle);
|
||||
isc_nm_read(handle, connect_read_cb, cbarg);
|
||||
|
||||
isc_refcount_increment0(&active_csends);
|
||||
isc_nmhandle_attach(handle, &sendhandle);
|
||||
isc_nmhandle_setwritetimeout(handle, T_IDLE);
|
||||
|
||||
isc_nm_send(sendhandle, (isc_region_t *)&send_msg, udp__send_cb,
|
||||
cbarg);
|
||||
|
||||
break;
|
||||
case ISC_R_ADDRINUSE:
|
||||
/* Try again */
|
||||
udp__connect(NULL);
|
||||
break;
|
||||
case ISC_R_SHUTTINGDOWN:
|
||||
case ISC_R_CANCELED:
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "%s(%p, %s, %p)\n", __func__, handle,
|
||||
isc_result_totext(eresult), cbarg);
|
||||
assert_int_equal(eresult, ISC_R_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_recv_one) {
|
||||
setup_test(state);
|
||||
|
||||
expected_cconnects = 1;
|
||||
cconnects_shutdown = false;
|
||||
|
||||
expected_csends = 1;
|
||||
csends_shutdown = false;
|
||||
|
||||
expected_sreads = 1;
|
||||
sreads_shutdown = false;
|
||||
|
||||
expected_ssends = 1;
|
||||
ssends_shutdown = false;
|
||||
|
||||
expected_creads = 1;
|
||||
creads_shutdown = true;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_recv_one) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
atomic_assert_int_eq(sreads, expected_sreads);
|
||||
atomic_assert_int_eq(ssends, expected_ssends);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
|
||||
teardown_test(state);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_recv_one) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_read_cb);
|
||||
|
||||
udp__connect(NULL);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_recv_two) {
|
||||
setup_test(state);
|
||||
|
||||
expected_cconnects = 2;
|
||||
cconnects_shutdown = false;
|
||||
|
||||
expected_csends = 2;
|
||||
csends_shutdown = false;
|
||||
|
||||
expected_sreads = 2;
|
||||
sreads_shutdown = false;
|
||||
|
||||
expected_ssends = 2;
|
||||
ssends_shutdown = false;
|
||||
|
||||
expected_creads = 2;
|
||||
creads_shutdown = true;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_recv_two) {
|
||||
atomic_assert_int_eq(cconnects, expected_cconnects);
|
||||
atomic_assert_int_eq(csends, expected_csends);
|
||||
atomic_assert_int_eq(sreads, expected_sreads);
|
||||
atomic_assert_int_eq(ssends, expected_ssends);
|
||||
atomic_assert_int_eq(creads, expected_creads);
|
||||
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_recv_two) {
|
||||
start_listening(ISC_NM_LISTEN_ONE, listen_read_cb);
|
||||
|
||||
udp__connect(NULL);
|
||||
udp__connect(NULL);
|
||||
}
|
||||
|
||||
ISC_SETUP_TEST_IMPL(udp_recv_send) {
|
||||
setup_test(state);
|
||||
|
||||
/* Allow some leeway (+1) as datagram service is unreliable */
|
||||
expected_cconnects = (workers + 1) * NSENDS;
|
||||
cconnects_shutdown = false;
|
||||
|
||||
expected_creads = workers * NSENDS;
|
||||
do_send = true;
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_TEARDOWN_TEST_IMPL(udp_recv_send) {
|
||||
atomic_assert_int_ge(cconnects, expected_creads);
|
||||
atomic_assert_int_ge(csends, expected_creads);
|
||||
atomic_assert_int_ge(sreads, expected_creads);
|
||||
atomic_assert_int_ge(ssends, expected_creads);
|
||||
atomic_assert_int_ge(creads, expected_creads);
|
||||
|
||||
teardown_test(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
ISC_LOOP_TEST_IMPL(udp_recv_send) {
|
||||
start_listening(ISC_NM_LISTEN_ALL, listen_read_cb);
|
||||
|
||||
for (size_t i = 0; i < workers; i++) {
|
||||
isc_async_run(isc_loop_get(loopmgr, i), udp__connect, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
ISC_TEST_LIST_START
|
||||
|
||||
/* Mock tests are unreliable on OpenBSD */
|
||||
#if !defined(__OpenBSD__)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_open, setup_test, teardown_test)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_bind, setup_test, teardown_test)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_recv_start, setup_test,
|
||||
teardown_test)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_open, setup_test, teardown_test)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_bind, setup_test, teardown_test)
|
||||
#if UV_VERSION_HEX >= UV_VERSION(1, 27, 0)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_connect, setup_test, teardown_test)
|
||||
#endif
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_recv_buffer_size, setup_test,
|
||||
teardown_test)
|
||||
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_send_buffer_size, setup_test,
|
||||
teardown_test)
|
||||
#endif /* !defined(__OpenBSD__) */
|
||||
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_noop)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_noresponse)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_shutdown_connect)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_shutdown_read)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_cancel_read)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_timeout_recovery)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_recv_one)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_recv_two)
|
||||
ISC_TEST_ENTRY_SETUP_TEARDOWN(udp_recv_send)
|
||||
|
||||
ISC_TEST_LIST_END
|
||||
|
||||
ISC_TEST_MAIN
|
Reference in New Issue
Block a user