| 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 <sched.h> /* IWYU pragma: keep */ |
| 15 |
#include <setjmp.h> |
| 16 |
#include <signal.h> |
| 17 |
#include <stdarg.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <unistd.h> |
| 20 |
|
| 21 |
/* |
| 22 |
* As a workaround, include an OpenSSL header file before including cmocka.h, |
| 23 |
* because OpenSSL 3.1.0 uses __attribute__(malloc), conflicting with a |
| 24 |
* redefined malloc in cmocka.h. |
| 25 |
*/ |
| 26 |
#include <openssl/err.h> |
| 27 |
|
| 28 |
#define UNIT_TESTING |
| 29 |
#include <cmocka.h> |
| 30 |
|
| 31 |
#include <isc/loop.h> |
| 32 |
#include <isc/nonce.h> |
| 33 |
#include <isc/os.h> |
| 34 |
#include <isc/quota.h> |
| 35 |
#include <isc/refcount.h> |
| 36 |
#include <isc/sockaddr.h> |
| 37 |
#include <isc/thread.h> |
| 38 |
#include <isc/util.h> |
| 39 |
#include <isc/uv.h> |
| 40 |
|
| 41 |
#include "uv_wrap.h" |
| 42 |
#define KEEP_BEFORE |
| 43 |
|
| 44 |
#include "netmgr/tcp.c" |
| 45 |
#include "netmgr_common.h" |
| 46 |
|
| 47 |
#include <tests/isc.h> |
| 48 |
|
| 49 |
static isc_nm_proxyheader_info_t custom_info; |
| 50 |
|
| 51 |
char complete_proxy_data[] = { 0x0d, 0x0a, 0x0d, 0x0a, 0x00, 0x0d, 0x0a, |
| 52 |
0x51, 0x55, 0x49, 0x54, 0x0a, 0x21, 0x11, |
| 53 |
0x00, 0x0c, 0x01, 0x02, 0x03, 0x04, 0x04, |
| 54 |
0x03, 0x02, 0x01, 0x14, 0xe9, 0x14, 0xe9 }; |
| 55 |
|
| 56 |
/* TCP */ |
| 57 |
ISC_LOOP_TEST_IMPL(proxystream_noop) { |
| 58 |
stream_noop(arg); |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
ISC_LOOP_TEST_IMPL(proxystream_noresponse) { |
| 63 |
stream_noresponse(arg); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
ISC_LOOP_TEST_IMPL(proxystream_shutdownconnect) { |
| 68 |
stream_shutdownconnect(arg); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
ISC_LOOP_TEST_IMPL(proxystream_shutdownread) { |
| 73 |
stream_shutdownread(arg); |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
ISC_LOOP_TEST_IMPL(proxystream_timeout_recovery) { |
| 78 |
stream_timeout_recovery(arg); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
ISC_LOOP_TEST_IMPL(proxystream_recv_one) { |
| 83 |
stream_recv_one(arg); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
proxystream_recv_one_prerendered(void **arg ISC_ATTR_UNUSED) { |
| 89 |
isc_region_t header = { 0 }; |
| 90 |
header.base = (unsigned char *)complete_proxy_data; |
| 91 |
header.length = sizeof(complete_proxy_data); |
| 92 |
|
| 93 |
isc_nm_proxyheader_info_init_complete(&custom_info, &header); |
| 94 |
|
| 95 |
set_proxyheader_info(&custom_info); |
| 96 |
|
| 97 |
stream_recv_one(arg); |
| 98 |
} |
| 99 |
|
| 100 |
ISC_LOOP_TEST_IMPL(proxystream_recv_one_prerendered) { |
| 101 |
proxystream_recv_one_prerendered(arg); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
ISC_LOOP_TEST_IMPL(proxystream_recv_two) { |
| 106 |
stream_recv_two(arg); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
ISC_LOOP_TEST_IMPL(proxystream_recv_send) { |
| 111 |
stream_recv_send(arg); |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
ISC_LOOP_TEST_IMPL(proxystream_recv_send_sendback) { |
| 116 |
allow_send_back = true; |
| 117 |
stream_recv_send(arg); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
/* TCP Quota */ |
| 122 |
|
| 123 |
ISC_LOOP_TEST_IMPL(proxystream_recv_one_quota) { |
| 124 |
atomic_store(&check_listener_quota, true); |
| 125 |
stream_recv_one(arg); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
ISC_LOOP_TEST_IMPL(proxystream_recv_two_quota) { |
| 130 |
atomic_store(&check_listener_quota, true); |
| 131 |
stream_recv_two(arg); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
ISC_LOOP_TEST_IMPL(proxystream_recv_send_quota) { |
| 136 |
atomic_store(&check_listener_quota, true); |
| 137 |
stream_recv_send(arg); |
| 138 |
} |
| 139 |
|
| 140 |
ISC_LOOP_TEST_IMPL(proxystream_recv_send_quota_sendback) { |
| 141 |
atomic_store(&check_listener_quota, true); |
| 142 |
allow_send_back = true; |
| 143 |
stream_recv_send(arg); |
| 144 |
} |
| 145 |
|
| 146 |
/* PROXY over TLS (as used by, e.g., dnsdist) */ |
| 147 |
|
| 148 |
/* TCP */ |
| 149 |
ISC_LOOP_TEST_IMPL(proxystreamtls_noop) { |
| 150 |
stream_noop(arg); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
ISC_LOOP_TEST_IMPL(proxystreamtls_noresponse) { |
| 155 |
stream_noresponse(arg); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
ISC_LOOP_TEST_IMPL(proxystreamtls_shutdownconnect) { |
| 160 |
stream_shutdownconnect(arg); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
ISC_LOOP_TEST_IMPL(proxystreamtls_shutdownread) { |
| 165 |
stream_shutdownread(arg); |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
ISC_LOOP_TEST_IMPL(proxystreamtls_timeout_recovery) { |
| 170 |
stream_timeout_recovery(arg); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_one) { |
| 175 |
stream_recv_one(arg); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_one_prerendered) { |
| 180 |
proxystream_recv_one_prerendered(arg); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_two) { |
| 185 |
stream_recv_two(arg); |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_send) { |
| 190 |
stream_recv_send(arg); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_send_sendback) { |
| 195 |
allow_send_back = true; |
| 196 |
stream_recv_send(arg); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
/* TCP Quota */ |
| 201 |
|
| 202 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_one_quota) { |
| 203 |
atomic_store(&check_listener_quota, true); |
| 204 |
stream_recv_one(arg); |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_two_quota) { |
| 209 |
atomic_store(&check_listener_quota, true); |
| 210 |
stream_recv_two(arg); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_send_quota) { |
| 215 |
atomic_store(&check_listener_quota, true); |
| 216 |
stream_recv_send(arg); |
| 217 |
} |
| 218 |
|
| 219 |
ISC_LOOP_TEST_IMPL(proxystreamtls_recv_send_quota_sendback) { |
| 220 |
atomic_store(&check_listener_quota, true); |
| 221 |
allow_send_back = true; |
| 222 |
stream_recv_send(arg); |
| 223 |
} |
| 224 |
|
| 225 |
ISC_TEST_LIST_START |
| 226 |
|
| 227 |
/* Stream */ |
| 228 |
ISC_TEST_ENTRY_CUSTOM(proxystream_noop, proxystream_noop_setup, |
| 229 |
proxystream_noop_teardown) |
| 230 |
ISC_TEST_ENTRY_CUSTOM(proxystream_noresponse, proxystream_noresponse_setup, |
| 231 |
proxystream_noresponse_teardown) |
| 232 |
ISC_TEST_ENTRY_CUSTOM(proxystream_shutdownconnect, |
| 233 |
proxystream_shutdownconnect_setup, |
| 234 |
proxystream_shutdownconnect_teardown) |
| 235 |
ISC_TEST_ENTRY_CUSTOM(proxystream_shutdownread, proxystream_shutdownread_setup, |
| 236 |
proxystream_shutdownread_teardown) |
| 237 |
ISC_TEST_ENTRY_CUSTOM(proxystream_timeout_recovery, |
| 238 |
proxystream_timeout_recovery_setup, |
| 239 |
proxystream_timeout_recovery_teardown) |
| 240 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_one, proxystream_recv_one_setup, |
| 241 |
proxystream_recv_one_teardown) |
| 242 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_one_prerendered, |
| 243 |
proxystream_recv_one_setup, proxystream_recv_one_teardown) |
| 244 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_two, proxystream_recv_two_setup, |
| 245 |
proxystream_recv_two_teardown) |
| 246 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_send, proxystream_recv_send_setup, |
| 247 |
proxystream_recv_send_teardown) |
| 248 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_send_sendback, |
| 249 |
proxystream_recv_send_setup, |
| 250 |
proxystream_recv_send_teardown) |
| 251 |
|
| 252 |
/* Stream Quota */ |
| 253 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_one_quota, proxystream_recv_one_setup, |
| 254 |
proxystream_recv_one_teardown) |
| 255 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_two_quota, proxystream_recv_two_setup, |
| 256 |
proxystream_recv_two_teardown) |
| 257 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_send_quota, proxystream_recv_send_setup, |
| 258 |
proxystream_recv_send_teardown) |
| 259 |
ISC_TEST_ENTRY_CUSTOM(proxystream_recv_send_quota_sendback, |
| 260 |
proxystream_recv_send_setup, |
| 261 |
proxystream_recv_send_teardown) |
| 262 |
|
| 263 |
/* PROXY over TLS */ |
| 264 |
|
| 265 |
/* Stream */ |
| 266 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_noop, proxystreamtls_noop_setup, |
| 267 |
proxystreamtls_noop_teardown) |
| 268 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_noresponse, |
| 269 |
proxystreamtls_noresponse_setup, |
| 270 |
proxystreamtls_noresponse_teardown) |
| 271 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_shutdownconnect, |
| 272 |
proxystreamtls_shutdownconnect_setup, |
| 273 |
proxystreamtls_shutdownconnect_teardown) |
| 274 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_shutdownread, |
| 275 |
proxystreamtls_shutdownread_setup, |
| 276 |
proxystreamtls_shutdownread_teardown) |
| 277 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_timeout_recovery, |
| 278 |
proxystreamtls_timeout_recovery_setup, |
| 279 |
proxystreamtls_timeout_recovery_teardown) |
| 280 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_one, proxystreamtls_recv_one_setup, |
| 281 |
proxystreamtls_recv_one_teardown) |
| 282 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_one_prerendered, |
| 283 |
proxystreamtls_recv_one_setup, |
| 284 |
proxystreamtls_recv_one_teardown) |
| 285 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_two, proxystreamtls_recv_two_setup, |
| 286 |
proxystreamtls_recv_two_teardown) |
| 287 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_send, proxystreamtls_recv_send_setup, |
| 288 |
proxystreamtls_recv_send_teardown) |
| 289 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_send_sendback, |
| 290 |
proxystreamtls_recv_send_setup, |
| 291 |
proxystreamtls_recv_send_teardown) |
| 292 |
|
| 293 |
/* Stream Quota */ |
| 294 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_one_quota, |
| 295 |
proxystreamtls_recv_one_setup, |
| 296 |
proxystreamtls_recv_one_teardown) |
| 297 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_two_quota, |
| 298 |
proxystreamtls_recv_two_setup, |
| 299 |
proxystreamtls_recv_two_teardown) |
| 300 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_send_quota, |
| 301 |
proxystreamtls_recv_send_setup, |
| 302 |
proxystreamtls_recv_send_teardown) |
| 303 |
ISC_TEST_ENTRY_CUSTOM(proxystreamtls_recv_send_quota_sendback, |
| 304 |
proxystreamtls_recv_send_setup, |
| 305 |
proxystreamtls_recv_send_teardown) |
| 306 |
|
| 307 |
ISC_TEST_LIST_END |
| 308 |
|
| 309 |
static int |
| 310 |
proxystream_setup(void **state ISC_ATTR_UNUSED) { |
| 311 |
stream_port = PROXYSTREAM_TEST_PORT; |
| 312 |
stream_use_TLS = false; |
| 313 |
stream = true; |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
ISC_TEST_MAIN_CUSTOM(proxystream_setup, NULL) |