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