2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/tests/isc/udp_test.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

238 lines
6.6 KiB
C
Raw Normal View History

/*
* 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 <inttypes.h>
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
/*
* As a workaround, include an OpenSSL header file before including cmocka.h,
* because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a
* redefined malloc in cmocka.h.
*/
#include <openssl/err.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/async.h>
#include <isc/job.h>
#include <isc/lib.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>
/* 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);
}
Fix the streaming read callback shutdown logic When shutting down TCP sockets, the read callback calling logic was flawed, it would call either one less callback or one extra. Fix the logic in the way: 1. When isc_nm_read() has been called but isc_nm_read_stop() hasn't on the handle, the read callback will be called with ISC_R_CANCELED to cancel active reading from the socket/handle. 2. When isc_nm_read() has been called and isc_nm_read_stop() has been called on the on the handle, the read callback will be called with ISC_R_SHUTTINGDOWN to signal that the dormant (not-reading) socket is being shut down. 3. The .reading and .recv_read flags are little bit tricky. The .reading flag indicates if the outer layer is reading the data (that would be uv_tcp_t for TCP and isc_nmsocket_t (TCP) for TLSStream), the .recv_read flag indicates whether somebody is interested in the data read from the socket. Usually, you would expect that the .reading should be false when .recv_read is false, but it gets even more tricky with TLSStream as the TLS protocol might need to read from the socket even when sending data. Fix the usage of the .recv_read and .reading flags in the TLSStream to their true meaning - which mostly consist of using .recv_read everywhere and then wrapping isc_nm_read() and isc_nm_read_stop() with the .reading flag. 4. The TLS failed read helper has been modified to resemble the TCP code as much as possible, clearing and re-setting the .recv_read flag in the TCP timeout code has been fixed and .recv_read is now cleared when isc_nm_read_stop() has been called on the streaming socket. 5. The use of Network Manager in the named_controlconf, isccc_ccmsg, and isc_httpd units have been greatly simplified due to the improved design. 6. More unit tests for TCP and TLS testing the shutdown conditions have been added. Co-authored-by: Ondřej Surý <ondrej@isc.org> Co-authored-by: Artem Boldariev <artem@isc.org>
2023-04-13 17:27:50 +02:00
static void
udp_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);
}
/* 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,
udp_connect_nomemory_cb, NULL, UDP_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,
udp_connect_nomemory_cb, NULL, UDP_T_CONNECT);
isc_loopmgr_shutdown(loopmgr);
RESET_RETURN;
}
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,
udp_connect_nomemory_cb, NULL, UDP_T_CONNECT);
isc_loopmgr_shutdown(loopmgr);
RESET_RETURN;
}
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, UDP_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, UDP_T_CONNECT);
isc_loopmgr_shutdown(loopmgr);
RESET_RETURN;
}
ISC_LOOP_TEST_IMPL(udp_noop) { udp_noop(arg); }
ISC_LOOP_TEST_IMPL(udp_noresponse) { udp_noresponse(arg); }
ISC_LOOP_TEST_IMPL(udp_timeout_recovery) { udp_timeout_recovery(arg); }
ISC_LOOP_TEST_IMPL(udp_shutdown_connect) { udp_shutdown_connect(arg); }
ISC_LOOP_TEST_IMPL(udp_shutdown_read) { udp_shutdown_read(arg); }
ISC_LOOP_TEST_IMPL(udp_cancel_read) { udp_cancel_read(arg); }
ISC_LOOP_TEST_IMPL(udp_recv_one) { udp_recv_one(arg); }
ISC_LOOP_TEST_IMPL(udp_recv_two) { udp_recv_two(arg); }
ISC_LOOP_TEST_IMPL(udp_recv_send) { udp_recv_send(arg); }
ISC_LOOP_TEST_IMPL(udp_double_read) { udp_double_read(arg); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_open, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_bind, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_recv_start, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_open, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_bind, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_connect, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_recv_buffer_size, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_send_buffer_size, setup_udp_test,
teardown_udp_test)
ISC_TEST_ENTRY_CUSTOM(udp_noop, udp_noop_setup, udp_noop_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_noresponse, udp_noresponse_setup,
udp_noresponse_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_timeout_recovery, udp_timeout_recovery_setup,
udp_timeout_recovery_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_shutdown_read, udp_shutdown_read_setup,
udp_shutdown_read_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_cancel_read, udp_cancel_read_setup,
udp_cancel_read_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_shutdown_connect, udp_shutdown_connect_setup,
udp_shutdown_connect_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_double_read, udp_double_read_setup,
udp_double_read_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_recv_one, udp_recv_one_setup, udp_recv_one_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_recv_two, udp_recv_two_setup, udp_recv_two_teardown)
ISC_TEST_ENTRY_CUSTOM(udp_recv_send, udp_recv_send_setup,
udp_recv_send_teardown)
ISC_TEST_LIST_END
ISC_TEST_MAIN