Home | History | Annotate | Line # | Download | only in isc
      1 /*	$NetBSD: udp_test.c,v 1.2 2025/01/26 16:25:50 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #include <inttypes.h>
     17 #include <sched.h> /* IWYU pragma: keep */
     18 #include <setjmp.h>
     19 #include <signal.h>
     20 #include <stdarg.h>
     21 #include <stdlib.h>
     22 #include <unistd.h>
     23 
     24 /*
     25  * As a workaround, include an OpenSSL header file before including cmocka.h,
     26  * because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a
     27  * redefined malloc in cmocka.h.
     28  */
     29 #include <openssl/err.h>
     30 
     31 #define UNIT_TESTING
     32 #include <cmocka.h>
     33 
     34 #include <isc/async.h>
     35 #include <isc/job.h>
     36 #include <isc/nonce.h>
     37 #include <isc/os.h>
     38 #include <isc/quota.h>
     39 #include <isc/refcount.h>
     40 #include <isc/sockaddr.h>
     41 #include <isc/thread.h>
     42 #include <isc/util.h>
     43 
     44 #include "uv_wrap.h"
     45 #define KEEP_BEFORE
     46 
     47 #include "netmgr/socket.c"
     48 #include "netmgr/udp.c"
     49 #include "netmgr_common.h"
     50 #include "uv.c"
     51 
     52 #include <tests/isc.h>
     53 
     54 /* Callbacks */
     55 
     56 static void
     57 mock_recv_cb(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
     58 	     void *cbarg) {
     59 	UNUSED(handle);
     60 	UNUSED(eresult);
     61 	UNUSED(region);
     62 	UNUSED(cbarg);
     63 }
     64 
     65 static void
     66 udp_connect_nomemory_cb(isc_nmhandle_t *handle, isc_result_t eresult,
     67 			void *cbarg) {
     68 	UNUSED(handle);
     69 	UNUSED(cbarg);
     70 
     71 	isc_refcount_decrement(&active_cconnects);
     72 	assert_int_equal(eresult, ISC_R_NOMEMORY);
     73 
     74 	isc_loopmgr_shutdown(loopmgr);
     75 }
     76 
     77 /* UDP */
     78 
     79 ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_open) {
     80 	isc_result_t result = ISC_R_SUCCESS;
     81 
     82 	WILL_RETURN(uv_udp_open, UV_ENOMEM);
     83 
     84 	result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
     85 				  mock_recv_cb, NULL, &listen_sock);
     86 	assert_int_not_equal(result, ISC_R_SUCCESS);
     87 	assert_null(listen_sock);
     88 
     89 	RESET_RETURN;
     90 
     91 	isc_loopmgr_shutdown(loopmgr);
     92 }
     93 
     94 ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_bind) {
     95 	isc_result_t result = ISC_R_SUCCESS;
     96 
     97 	WILL_RETURN(uv_udp_bind, UV_EADDRINUSE);
     98 
     99 	result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
    100 				  mock_recv_cb, NULL, &listen_sock);
    101 	assert_int_not_equal(result, ISC_R_SUCCESS);
    102 	assert_null(listen_sock);
    103 
    104 	RESET_RETURN;
    105 
    106 	isc_loopmgr_shutdown(loopmgr);
    107 }
    108 
    109 ISC_LOOP_TEST_IMPL(mock_listenudp_uv_udp_recv_start) {
    110 	isc_result_t result = ISC_R_SUCCESS;
    111 
    112 	WILL_RETURN(uv_udp_recv_start, UV_EADDRINUSE);
    113 
    114 	result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ALL, &udp_listen_addr,
    115 				  mock_recv_cb, NULL, &listen_sock);
    116 	assert_int_not_equal(result, ISC_R_SUCCESS);
    117 	assert_null(listen_sock);
    118 
    119 	RESET_RETURN;
    120 
    121 	isc_loopmgr_shutdown(loopmgr);
    122 }
    123 
    124 ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_open) {
    125 	WILL_RETURN(uv_udp_open, UV_ENOMEM);
    126 
    127 	isc_refcount_increment0(&active_cconnects);
    128 	isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
    129 			  udp_connect_nomemory_cb, NULL, UDP_T_CONNECT);
    130 	isc_loopmgr_shutdown(loopmgr);
    131 
    132 	RESET_RETURN;
    133 }
    134 
    135 ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_bind) {
    136 	WILL_RETURN(uv_udp_bind, UV_ENOMEM);
    137 
    138 	isc_refcount_increment0(&active_cconnects);
    139 	isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
    140 			  udp_connect_nomemory_cb, NULL, UDP_T_CONNECT);
    141 	isc_loopmgr_shutdown(loopmgr);
    142 
    143 	RESET_RETURN;
    144 }
    145 
    146 ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_udp_connect) {
    147 	WILL_RETURN(uv_udp_connect, UV_ENOMEM);
    148 
    149 	isc_refcount_increment0(&active_cconnects);
    150 	isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
    151 			  udp_connect_nomemory_cb, NULL, UDP_T_CONNECT);
    152 	isc_loopmgr_shutdown(loopmgr);
    153 
    154 	RESET_RETURN;
    155 }
    156 
    157 ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_recv_buffer_size) {
    158 	WILL_RETURN(uv_recv_buffer_size, UV_ENOMEM);
    159 
    160 	isc_refcount_increment0(&active_cconnects);
    161 	isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
    162 			  connect_success_cb, NULL, UDP_T_CONNECT);
    163 	isc_loopmgr_shutdown(loopmgr);
    164 
    165 	RESET_RETURN;
    166 }
    167 
    168 ISC_LOOP_TEST_IMPL(mock_udpconnect_uv_send_buffer_size) {
    169 	WILL_RETURN(uv_send_buffer_size, UV_ENOMEM);
    170 
    171 	isc_refcount_increment0(&active_cconnects);
    172 	isc_nm_udpconnect(netmgr, &udp_connect_addr, &udp_listen_addr,
    173 			  connect_success_cb, NULL, UDP_T_CONNECT);
    174 	isc_loopmgr_shutdown(loopmgr);
    175 
    176 	RESET_RETURN;
    177 }
    178 
    179 ISC_LOOP_TEST_IMPL(udp_noop) { udp_noop(arg); }
    180 
    181 ISC_LOOP_TEST_IMPL(udp_noresponse) { udp_noresponse(arg); }
    182 
    183 ISC_LOOP_TEST_IMPL(udp_timeout_recovery) { udp_timeout_recovery(arg); }
    184 
    185 ISC_LOOP_TEST_IMPL(udp_shutdown_connect) { udp_shutdown_connect(arg); }
    186 
    187 ISC_LOOP_TEST_IMPL(udp_shutdown_read) { udp_shutdown_read(arg); }
    188 
    189 ISC_LOOP_TEST_IMPL(udp_cancel_read) { udp_cancel_read(arg); }
    190 
    191 ISC_LOOP_TEST_IMPL(udp_recv_one) { udp_recv_one(arg); }
    192 
    193 ISC_LOOP_TEST_IMPL(udp_recv_two) { udp_recv_two(arg); }
    194 
    195 ISC_LOOP_TEST_IMPL(udp_recv_send) { udp_recv_send(arg); }
    196 
    197 ISC_LOOP_TEST_IMPL(udp_double_read) { udp_double_read(arg); }
    198 
    199 ISC_TEST_LIST_START
    200 
    201 ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_open, setup_udp_test,
    202 		      teardown_udp_test)
    203 ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_bind, setup_udp_test,
    204 		      teardown_udp_test)
    205 ISC_TEST_ENTRY_CUSTOM(mock_listenudp_uv_udp_recv_start, setup_udp_test,
    206 		      teardown_udp_test)
    207 ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_open, setup_udp_test,
    208 		      teardown_udp_test)
    209 ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_bind, setup_udp_test,
    210 		      teardown_udp_test)
    211 ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_udp_connect, setup_udp_test,
    212 		      teardown_udp_test)
    213 ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_recv_buffer_size, setup_udp_test,
    214 		      teardown_udp_test)
    215 ISC_TEST_ENTRY_CUSTOM(mock_udpconnect_uv_send_buffer_size, setup_udp_test,
    216 		      teardown_udp_test)
    217 
    218 ISC_TEST_ENTRY_CUSTOM(udp_noop, udp_noop_setup, udp_noop_teardown)
    219 ISC_TEST_ENTRY_CUSTOM(udp_noresponse, udp_noresponse_setup,
    220 		      udp_noresponse_teardown)
    221 ISC_TEST_ENTRY_CUSTOM(udp_timeout_recovery, udp_timeout_recovery_setup,
    222 		      udp_timeout_recovery_teardown)
    223 ISC_TEST_ENTRY_CUSTOM(udp_shutdown_read, udp_shutdown_read_setup,
    224 		      udp_shutdown_read_teardown)
    225 ISC_TEST_ENTRY_CUSTOM(udp_cancel_read, udp_cancel_read_setup,
    226 		      udp_cancel_read_teardown)
    227 ISC_TEST_ENTRY_CUSTOM(udp_shutdown_connect, udp_shutdown_connect_setup,
    228 		      udp_shutdown_connect_teardown)
    229 ISC_TEST_ENTRY_CUSTOM(udp_double_read, udp_double_read_setup,
    230 		      udp_double_read_teardown)
    231 ISC_TEST_ENTRY_CUSTOM(udp_recv_one, udp_recv_one_setup, udp_recv_one_teardown)
    232 ISC_TEST_ENTRY_CUSTOM(udp_recv_two, udp_recv_two_setup, udp_recv_two_teardown)
    233 ISC_TEST_ENTRY_CUSTOM(udp_recv_send, udp_recv_send_setup,
    234 		      udp_recv_send_teardown)
    235 
    236 ISC_TEST_LIST_END
    237 
    238 ISC_TEST_MAIN
    239