Debian Codegraph @

tests/isc/tcpdns_test.c

raw
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/util.h>
40 #include <isc/uv.h>
41
42 #include "uv_wrap.h"
43 #define KEEP_BEFORE
44
45 #include "netmgr_common.h"
46
47 #include <tests/isc.h>
48
49 /* TCPDNS */
50
51 static void
52 start_listening(uint32_t nworkers, isc_nm_accept_cb_t accept_cb,
53 isc_nm_recv_cb_t recv_cb) {
54 isc_result_t result = isc_nm_listenstreamdns(
55 listen_nm, nworkers, &tcp_listen_addr, recv_cb, NULL, accept_cb,
56 NULL, 128, NULL, NULL, get_proxy_type(), &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 tcpdns_connect(isc_nm_t *nm) {
64 isc_nm_streamdnsconnect(nm, &tcp_connect_addr, &tcp_listen_addr,
65 connect_connect_cb, tcpdns_connect, T_CONNECT,
66 NULL, NULL, get_proxy_type(), NULL);
67 }
68
69 ISC_LOOP_TEST_IMPL(tcpdns_noop) {
70 start_listening(ISC_NM_LISTEN_ONE, noop_accept_cb, noop_recv_cb);
71
72 connect_readcb = NULL;
73 isc_refcount_increment0(&active_cconnects);
74 isc_nm_streamdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
75 connect_success_cb, tcpdns_connect, T_CONNECT,
76 NULL, NULL, get_proxy_type(), NULL);
77 }
78
79 ISC_LOOP_TEST_IMPL(tcpdns_noresponse) {
80 start_listening(ISC_NM_LISTEN_ALL, noop_accept_cb, noop_recv_cb);
81
82 isc_refcount_increment0(&active_cconnects);
83 isc_nm_streamdnsconnect(connect_nm, &tcp_connect_addr, &tcp_listen_addr,
84 connect_connect_cb, tcpdns_connect, T_CONNECT,
85 NULL, NULL, get_proxy_type(), NULL);
86 }
87
88 ISC_LOOP_TEST_IMPL(tcpdns_timeout_recovery) {
89 /*
90 * Accept connections but don't send responses, forcing client
91 * reads to time out.
92 */
93 noanswer = true;
94 start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
95
96 /*
97 * Shorten all the TCP client timeouts to 0.05 seconds, connect,
98 * then sleep for at least a second for each 'tick'.
99 * timeout_retry_cb() will give up after five timeouts.
100 */
101 connect_readcb = timeout_retry_cb;
102 isc_nm_settimeouts(connect_nm, T_SOFT, T_SOFT, T_SOFT, T_SOFT);
103
104 isc_async_current(stream_recv_send_connect, tcpdns_connect);
105 }
106
107 ISC_LOOP_TEST_IMPL(tcpdns_recv_one) {
108 start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
109
110 isc_async_current(stream_recv_send_connect, tcpdns_connect);
111 }
112
113 ISC_LOOP_TEST_IMPL(tcpdns_recv_two) {
114 start_listening(ISC_NM_LISTEN_ONE, listen_accept_cb, listen_read_cb);
115
116 isc_async_current(stream_recv_send_connect, tcpdns_connect);
117
118 isc_async_current(stream_recv_send_connect, tcpdns_connect);
119 }
120
121 ISC_LOOP_TEST_IMPL(tcpdns_recv_send) {
122 start_listening(ISC_NM_LISTEN_ALL, listen_accept_cb, listen_read_cb);
123
124 for (size_t i = 0; i < workers; i++) {
125 isc_async_run(isc_loop_get(loopmgr, i),
126 stream_recv_send_connect, tcpdns_connect);
127 }
128 }
129
130 /* PROXY tests */
131
132 ISC_LOOP_TEST_IMPL(proxy_tcpdns_noop) { loop_test_tcpdns_noop(arg); }
133
134 ISC_LOOP_TEST_IMPL(proxy_tcpdns_noresponse) {
135 loop_test_tcpdns_noresponse(arg);
136 }
137
138 ISC_LOOP_TEST_IMPL(proxy_tcpdns_timeout_recovery) {
139 loop_test_tcpdns_timeout_recovery(arg);
140 }
141
142 ISC_LOOP_TEST_IMPL(proxy_tcpdns_recv_one) { loop_test_tcpdns_recv_one(arg); }
143
144 ISC_LOOP_TEST_IMPL(proxy_tcpdns_recv_two) { loop_test_tcpdns_recv_two(arg); }
145
146 ISC_LOOP_TEST_IMPL(proxy_tcpdns_recv_send) { loop_test_tcpdns_recv_send(arg); }
147
148 ISC_TEST_LIST_START
149
150 ISC_TEST_ENTRY_CUSTOM(tcpdns_noop, stream_noop_setup, stream_noop_teardown)
151 ISC_TEST_ENTRY_CUSTOM(tcpdns_noresponse, stream_noresponse_setup,
152 stream_noresponse_teardown)
153 ISC_TEST_ENTRY_CUSTOM(tcpdns_timeout_recovery, stream_timeout_recovery_setup,
154 stream_timeout_recovery_teardown)
155 ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_one, stream_recv_one_setup,
156 stream_recv_one_teardown)
157 ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_two, stream_recv_two_setup,
158 stream_recv_two_teardown)
159 ISC_TEST_ENTRY_CUSTOM(tcpdns_recv_send, stream_recv_send_setup,
160 stream_recv_send_teardown)
161 /* PROXY */
162
163 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_noop, proxystream_noop_setup,
164 proxystream_noop_teardown)
165 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_noresponse, proxystream_noresponse_setup,
166 proxystream_noresponse_teardown)
167 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_timeout_recovery,
168 proxystream_timeout_recovery_setup,
169 proxystream_timeout_recovery_teardown)
170 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_recv_one, proxystream_recv_one_setup,
171 proxystream_recv_one_teardown)
172 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_recv_two, proxystream_recv_two_setup,
173 proxystream_recv_two_teardown)
174 ISC_TEST_ENTRY_CUSTOM(proxy_tcpdns_recv_send, proxystream_recv_send_setup,
175 proxystream_recv_send_teardown)
176
177 ISC_TEST_LIST_END
178
179 static int
180 tcpdns_setup(void **state ISC_ATTR_UNUSED) {
181 stream_port = TCPDNS_TEST_PORT;
182
183 return 0;
184 }
185
186 ISC_TEST_MAIN_CUSTOM(tcpdns_setup, NULL)