| 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/loop.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 |
#include <isc/uv.h> |
| 42 |
|
| 43 |
#include "uv_wrap.h" |
| 44 |
#define KEEP_BEFORE |
| 45 |
|
| 46 |
#include "netmgr_common.h" |
| 47 |
|
| 48 |
#include <tests/isc.h> |
| 49 |
|
| 50 |
static void |
| 51 |
start_listening(uint32_t nworkers, isc_nm_accept_cb_t accept_cb, |
| 52 |
isc_nm_recv_cb_t recv_cb) { |
| 53 |
isc_result_t result = isc_nm_listenstreamdns( |
| 54 |
listen_nm, nworkers, &tcp_listen_addr, recv_cb, NULL, accept_cb, |
| 55 |
NULL, 128, NULL, tcp_listen_tlsctx, get_proxy_type(), |
| 56 |
&listen_sock); |
| 57 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 58 |
|
| 59 |
isc_loop_teardown(mainloop, stop_listening, listen_sock); |
| 60 |
} |
| 61 |
|
| 62 |
static void |
| 63 |
tlsdns_connect(isc_nm_t *nm) { |
| 64 |
isc_nm_streamdnsconnect( |
| 65 |
nm, &tcp_connect_addr, &tcp_listen_addr, connect_connect_cb, |
| 66 |
tlsdns_connect, T_CONNECT, tcp_connect_tlsctx, |
| 67 |
tcp_tlsctx_client_sess_cache, get_proxy_type(), NULL); |
| 68 |
} |
| 69 |
|
| 70 |
ISC_LOOP_TEST_IMPL(tlsdns_noop) { |
| 71 |
start_listening(ISC_NM_LISTEN_ONE, noop_accept_cb, noop_recv_cb); |
| 72 |
|
| 73 |
connect_readcb = NULL; |
| 74 |
isc_refcount_increment0(&active_cconnects); |
| 75 |
isc_nm_streamdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr, |
| 76 |
connect_success_cb, tlsdns_connect, T_CONNECT, |
| 77 |
tcp_connect_tlsctx, |
| 78 |
tcp_tlsctx_client_sess_cache, get_proxy_type(), |
| 79 |
NULL); |
| 80 |
} |
| 81 |
|
| 82 |
ISC_LOOP_TEST_IMPL(tlsdns_noresponse) { |
| 83 |
start_listening(ISC_NM_LISTEN_ALL, noop_accept_cb, noop_recv_cb); |
| 84 |
|
| 85 |
isc_refcount_increment0(&active_cconnects); |
| 86 |
isc_nm_streamdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr, |
| 87 |
connect_connect_cb, tlsdns_connect, T_CONNECT, |
| 88 |
tcp_connect_tlsctx, |
| 89 |
tcp_tlsctx_client_sess_cache, get_proxy_type(), |
| 90 |
NULL); |
| 91 |
} |
| 92 |
|
| 93 |
ISC_LOOP_TEST_IMPL(tlsdns_timeout_recovery) { |
| 94 |
/* |
| 95 |
* Accept connections but don't send responses, forcing client |
| 96 |
* reads to time out. |
| 97 |
*/ |
| 98 |
noanswer = true; |
| 99 |
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb); |
| 100 |
|
| 101 |
/* |
| 102 |
* Shorten all the TCP client timeouts to 0.05 seconds, connect, |
| 103 |
* then sleep for at least a second for each 'tick'. |
| 104 |
* timeout_retry_cb() will give up after five timeouts. |
| 105 |
*/ |
| 106 |
connect_readcb = timeout_retry_cb; |
| 107 |
isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT); |
| 108 |
isc_refcount_increment0(&active_cconnects); |
| 109 |
isc_nm_streamdnsconnect( |
| 110 |
connect_nm, &tcp_connect_addr, &tcp_listen_addr, |
| 111 |
connect_connect_cb, tlsdns_connect, T_SOFT, tcp_connect_tlsctx, |
| 112 |
tcp_tlsctx_client_sess_cache, get_proxy_type(), NULL); |
| 113 |
} |
| 114 |
|
| 115 |
ISC_LOOP_TEST_IMPL(tlsdns_recv_one) { |
| 116 |
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb); |
| 117 |
|
| 118 |
isc_refcount_increment0(&active_cconnects); |
| 119 |
tlsdns_connect(connect_nm); |
| 120 |
} |
| 121 |
|
| 122 |
ISC_LOOP_TEST_IMPL(tlsdns_recv_two) { |
| 123 |
start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb); |
| 124 |
|
| 125 |
isc_refcount_increment0(&active_cconnects); |
| 126 |
tlsdns_connect(connect_nm); |
| 127 |
|
| 128 |
isc_refcount_increment0(&active_cconnects); |
| 129 |
tlsdns_connect(connect_nm); |
| 130 |
} |
| 131 |
|
| 132 |
ISC_LOOP_TEST_IMPL(tlsdns_recv_send) { |
| 133 |
start_listening(ISC_NM_LISTEN_ALL, listen_accept_cb, listen_read_cb); |
| 134 |
|
| 135 |
for (size_t i = 0; i < workers; i++) { |
| 136 |
isc_async_run(isc_loop_get(loopmgr, i), |
| 137 |
stream_recv_send_connect, tlsdns_connect); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* PROXY tests */ |
| 142 |
|
| 143 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_noop) { loop_test_tlsdns_noop(arg); } |
| 144 |
|
| 145 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_noresponse) { |
| 146 |
loop_test_tlsdns_noresponse(arg); |
| 147 |
} |
| 148 |
|
| 149 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_timeout_recovery) { |
| 150 |
loop_test_tlsdns_timeout_recovery(arg); |
| 151 |
} |
| 152 |
|
| 153 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_recv_one) { loop_test_tlsdns_recv_one(arg); } |
| 154 |
|
| 155 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_recv_two) { loop_test_tlsdns_recv_two(arg); } |
| 156 |
|
| 157 |
ISC_LOOP_TEST_IMPL(proxy_tlsdns_recv_send) { loop_test_tlsdns_recv_send(arg); } |
| 158 |
|
| 159 |
/* PROXY over TLS tests */ |
| 160 |
|
| 161 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_noop) { loop_test_tlsdns_noop(arg); } |
| 162 |
|
| 163 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_noresponse) { |
| 164 |
loop_test_tlsdns_noresponse(arg); |
| 165 |
} |
| 166 |
|
| 167 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_timeout_recovery) { |
| 168 |
loop_test_tlsdns_timeout_recovery(arg); |
| 169 |
} |
| 170 |
|
| 171 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_recv_one) { loop_test_tlsdns_recv_one(arg); } |
| 172 |
|
| 173 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_recv_two) { loop_test_tlsdns_recv_two(arg); } |
| 174 |
|
| 175 |
ISC_LOOP_TEST_IMPL(proxytls_tlsdns_recv_send) { |
| 176 |
loop_test_tlsdns_recv_send(arg); |
| 177 |
} |
| 178 |
|
| 179 |
ISC_TEST_LIST_START |
| 180 |
|
| 181 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_noop, stream_noop_setup, stream_noop_teardown) |
| 182 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_noresponse, stream_noresponse_setup, |
| 183 |
stream_noresponse_teardown) |
| 184 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_timeout_recovery, stream_timeout_recovery_setup, |
| 185 |
stream_timeout_recovery_teardown) |
| 186 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_one, stream_recv_one_setup, |
| 187 |
stream_recv_one_teardown) |
| 188 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_two, stream_recv_two_setup, |
| 189 |
stream_recv_two_teardown) |
| 190 |
ISC_TEST_ENTRY_CUSTOM(tlsdns_recv_send, stream_recv_send_setup, |
| 191 |
stream_recv_send_teardown) |
| 192 |
|
| 193 |
/* FIXME: Re-add the noalpn tests */ |
| 194 |
|
| 195 |
/* PROXY */ |
| 196 |
|
| 197 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_noop, proxystream_noop_setup, |
| 198 |
proxystream_noop_teardown) |
| 199 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_noresponse, proxystream_noresponse_setup, |
| 200 |
proxystream_noresponse_teardown) |
| 201 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_timeout_recovery, |
| 202 |
proxystream_timeout_recovery_setup, |
| 203 |
proxystream_timeout_recovery_teardown) |
| 204 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_recv_one, proxystream_recv_one_setup, |
| 205 |
proxystream_recv_one_teardown) |
| 206 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_recv_two, proxystream_recv_two_setup, |
| 207 |
proxystream_recv_two_teardown) |
| 208 |
ISC_TEST_ENTRY_CUSTOM(proxy_tlsdns_recv_send, proxystream_recv_send_setup, |
| 209 |
proxystream_recv_send_teardown) |
| 210 |
|
| 211 |
/* PROXY over TLS */ |
| 212 |
|
| 213 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_noop, proxystreamtls_noop_setup, |
| 214 |
proxystreamtls_noop_teardown) |
| 215 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_noresponse, |
| 216 |
proxystreamtls_noresponse_setup, |
| 217 |
proxystreamtls_noresponse_teardown) |
| 218 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_timeout_recovery, |
| 219 |
proxystreamtls_timeout_recovery_setup, |
| 220 |
proxystreamtls_timeout_recovery_teardown) |
| 221 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_recv_one, proxystreamtls_recv_one_setup, |
| 222 |
proxystreamtls_recv_one_teardown) |
| 223 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_recv_two, proxystreamtls_recv_two_setup, |
| 224 |
proxystreamtls_recv_two_teardown) |
| 225 |
ISC_TEST_ENTRY_CUSTOM(proxytls_tlsdns_recv_send, proxystreamtls_recv_send_setup, |
| 226 |
proxystreamtls_recv_send_teardown) |
| 227 |
|
| 228 |
ISC_TEST_LIST_END |
| 229 |
|
| 230 |
static int |
| 231 |
tlsdns_setup(void **state ISC_ATTR_UNUSED) { |
| 232 |
stream_port = TLSDNS_TEST_PORT; |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
ISC_TEST_MAIN_CUSTOM(tlsdns_setup, NULL) |