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