Debian Codegraph @

tests/dns/dispatch_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 <stdarg.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/buffer.h>
28 #include <isc/managers.h>
29 #include <isc/refcount.h>
30 #include <isc/tls.h>
31 #include <isc/util.h>
32 #include <isc/uv.h>
33
34 #include <dns/dispatch.h>
35 #include <dns/name.h>
36 #include <dns/view.h>
37
38 #include <tests/dns.h>
39
40 /* Timeouts in miliseconds */
41 #define T_SERVER_INIT (120 * 1000)
42 #define T_SERVER_IDLE (120 * 1000)
43 #define T_SERVER_KEEPALIVE (120 * 1000)
44 #define T_SERVER_ADVERTISED (120 * 1000)
45
46 #define T_CLIENT_INIT (120 * 1000)
47 #define T_CLIENT_IDLE (120 * 1000)
48 #define T_CLIENT_KEEPALIVE (120 * 1000)
49 #define T_CLIENT_ADVERTISED (120 * 1000)
50
51 #define T_CLIENT_CONNECT (30 * 1000)
52
53 /* For checks which are expected to timeout */
54 #define T_CLIENT_CONNECT_SHORT (10 * 1000)
55
56 /* dns_dispatchset_t *dset = NULL; */
57 static isc_sockaddr_t udp_server_addr;
58 static isc_sockaddr_t udp_connect_addr;
59 static isc_sockaddr_t tcp_server_addr;
60 static isc_sockaddr_t tcp_connect_addr;
61 static isc_sockaddr_t tls_server_addr;
62 static isc_sockaddr_t tls_connect_addr;
63
64 static isc_tlsctx_cache_t *tls_tlsctx_client_cache = NULL;
65 static isc_tlsctx_t *tls_listen_tlsctx = NULL;
66 static dns_name_t tls_name;
67 static const char *tls_name_str = "ephemeral";
68 static dns_transport_t *tls_transport = NULL;
69 static dns_transport_list_t *transport_list = NULL;
70
71 static isc_nmsocket_t *sock = NULL;
72
73 static isc_nm_t *connect_nm = NULL;
74
75 const struct in6_addr in6addr_blackhole = { { { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 1 } } };
77
78 struct {
79 uint8_t rbuf[12];
80 isc_region_t region;
81 uint8_t message[12];
82 } testdata;
83
84 typedef struct {
85 dns_dispatchmgr_t *dispatchmgr;
86 dns_dispatch_t *dispatch;
87 dns_dispentry_t *dispentry;
88 uint16_t id;
89 } test_dispatch_t;
90
91 static void
92 test_dispatch_done(test_dispatch_t *test) {
93 dns_dispatch_done(&test->dispentry);
94 dns_dispatch_detach(&test->dispatch);
95 dns_dispatchmgr_detach(&test->dispatchmgr);
96 isc_mem_put(mctx, test, sizeof(*test));
97 }
98
99 static void
100 test_dispatch_shutdown(test_dispatch_t *test) {
101 test_dispatch_done(test);
102 isc_loopmgr_shutdown(loopmgr);
103 }
104
105 static int
106 setup_ephemeral_port(isc_sockaddr_t *addr, sa_family_t family) {
107 socklen_t addrlen = sizeof(*addr);
108 uv_os_sock_t fd;
109 int r;
110
111 isc_sockaddr_fromin6(addr, &in6addr_loopback, 0);
112
113 fd = socket(AF_INET6, family, 0);
114 if (fd < 0) {
115 perror("setup_ephemeral_port: socket()");
116 return -1;
117 }
118
119 r = bind(fd, (const struct sockaddr *)&addr->type.sa,
120 sizeof(addr->type.sin6));
121 if (r != 0) {
122 perror("setup_ephemeral_port: bind()");
123 close(fd);
124 return r;
125 }
126
127 r = getsockname(fd, (struct sockaddr *)&addr->type.sa, &addrlen);
128 if (r != 0) {
129 perror("setup_ephemeral_port: getsockname()");
130 close(fd);
131 return r;
132 }
133
134 #if IPV6_RECVERR
135 #define setsockopt_on(socket, level, name) \
136 setsockopt(socket, level, name, &(int){ 1 }, sizeof(int))
137
138 r = setsockopt_on(fd, IPPROTO_IPV6, IPV6_RECVERR);
139 if (r != 0) {
140 perror("setup_ephemeral_port");
141 close(fd);
142 return r;
143 }
144 #endif
145
146 return fd;
147 }
148
149 static int
150 setup_test(void **state) {
151 isc_buffer_t namesrc, namebuf;
152 char namedata[DNS_NAME_FORMATSIZE + 1];
153
154 uv_os_sock_t socket = -1;
155
156 /* Create just 1 loop for this test */
157 isc_loopmgr_create(mctx, 1, &loopmgr);
158 mainloop = isc_loop_main(loopmgr);
159
160 setup_netmgr(state);
161
162 isc_netmgr_create(mctx, loopmgr, &connect_nm);
163
164 udp_connect_addr = (isc_sockaddr_t){ .length = 0 };
165 isc_sockaddr_fromin6(&udp_connect_addr, &in6addr_loopback, 0);
166
167 tcp_connect_addr = (isc_sockaddr_t){ .length = 0 };
168 isc_sockaddr_fromin6(&tcp_connect_addr, &in6addr_loopback, 0);
169
170 tls_connect_addr = (isc_sockaddr_t){ .length = 0 };
171 isc_sockaddr_fromin6(&tls_connect_addr, &in6addr_loopback, 0);
172
173 udp_server_addr = (isc_sockaddr_t){ .length = 0 };
174 socket = setup_ephemeral_port(&udp_server_addr, SOCK_DGRAM);
175 if (socket < 0) {
176 return -1;
177 }
178 close(socket);
179
180 tcp_server_addr = (isc_sockaddr_t){ .length = 0 };
181 socket = setup_ephemeral_port(&tcp_server_addr, SOCK_STREAM);
182 if (socket < 0) {
183 return -1;
184 }
185 close(socket);
186
187 tls_server_addr = (isc_sockaddr_t){ .length = 0 };
188 socket = setup_ephemeral_port(&tls_server_addr, SOCK_STREAM);
189 if (socket < 0) {
190 return -1;
191 }
192 close(socket);
193
194 isc_nm_settimeouts(netmgr, T_SERVER_INIT, T_SERVER_IDLE,
195 T_SERVER_KEEPALIVE, T_SERVER_ADVERTISED);
196
197 /*
198 * Use shorter client-side timeouts, to ensure that clients
199 * time out before the server.
200 */
201 isc_nm_settimeouts(connect_nm, T_CLIENT_INIT, T_CLIENT_IDLE,
202 T_CLIENT_KEEPALIVE, T_CLIENT_ADVERTISED);
203
204 memset(testdata.rbuf, 0, sizeof(testdata.rbuf));
205 testdata.region.base = testdata.rbuf;
206 testdata.region.length = sizeof(testdata.rbuf);
207 memset(testdata.message, 0, sizeof(testdata.message));
208
209 isc_tlsctx_cache_create(mctx, &tls_tlsctx_client_cache);
210
211 if (isc_tlsctx_createserver(NULL, NULL, &tls_listen_tlsctx) !=
212 ISC_R_SUCCESS)
213 {
214 return -1;
215 }
216
217 dns_name_init(&tls_name, NULL);
218 isc_buffer_constinit(&namesrc, tls_name_str, strlen(tls_name_str));
219 isc_buffer_add(&namesrc, strlen(tls_name_str));
220 isc_buffer_init(&namebuf, namedata, sizeof(namedata));
221 if (dns_name_fromtext(&tls_name, &namesrc, dns_rootname,
222 DNS_NAME_DOWNCASE, &namebuf) != ISC_R_SUCCESS)
223 {
224 return -1;
225 }
226 transport_list = dns_transport_list_new(mctx);
227 tls_transport = dns_transport_new(&tls_name, DNS_TRANSPORT_TLS,
228 transport_list);
229 dns_transport_set_tlsname(tls_transport, tls_name_str);
230
231 return 0;
232 }
233
234 static int
235 teardown_test(void **state) {
236 dns_transport_list_detach(&transport_list);
237 isc_tlsctx_cache_detach(&tls_tlsctx_client_cache);
238 isc_tlsctx_free(&tls_listen_tlsctx);
239
240 isc_netmgr_destroy(&connect_nm);
241
242 teardown_netmgr(state);
243 teardown_loopmgr(state);
244
245 return 0;
246 }
247
248 static isc_result_t
249 make_dispatchset(dns_dispatchmgr_t *dispatchmgr, unsigned int ndisps,
250 dns_dispatchset_t **dsetp) {
251 isc_result_t result;
252 isc_sockaddr_t any;
253 dns_dispatch_t *disp = NULL;
254
255 isc_sockaddr_any(&any);
256 result = dns_dispatch_createudp(dispatchmgr, &any, &disp);
257 if (result != ISC_R_SUCCESS) {
258 return result;
259 }
260
261 result = dns_dispatchset_create(mctx, disp, dsetp, ndisps);
262 dns_dispatch_detach(&disp);
263
264 return result;
265 }
266
267 /* create dispatch set */
268 ISC_LOOP_TEST_IMPL(dispatchset_create) {
269 dns_dispatchset_t *dset = NULL;
270 isc_result_t result;
271 dns_dispatchmgr_t *dispatchmgr = NULL;
272
273 UNUSED(arg);
274
275 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
276 &dispatchmgr);
277 assert_int_equal(result, ISC_R_SUCCESS);
278
279 result = make_dispatchset(dispatchmgr, 1, &dset);
280 assert_int_equal(result, ISC_R_SUCCESS);
281 dns_dispatchset_destroy(&dset);
282
283 result = make_dispatchset(dispatchmgr, 10, &dset);
284 assert_int_equal(result, ISC_R_SUCCESS);
285 dns_dispatchset_destroy(&dset);
286
287 dns_dispatchmgr_detach(&dispatchmgr);
288
289 isc_loopmgr_shutdown(loopmgr);
290 }
291
292 /* test dispatch set per-loop dispatch */
293 ISC_LOOP_TEST_IMPL(dispatchset_get) {
294 isc_result_t result;
295 dns_dispatchmgr_t *dispatchmgr = NULL;
296 dns_dispatchset_t *dset = NULL;
297 dns_dispatch_t *d1, *d2, *d3, *d4, *d5;
298 uint32_t tid_saved;
299
300 UNUSED(arg);
301
302 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
303 &dispatchmgr);
304 assert_int_equal(result, ISC_R_SUCCESS);
305
306 result = make_dispatchset(dispatchmgr, 1, &dset);
307 assert_int_equal(result, ISC_R_SUCCESS);
308
309 d1 = dns_dispatchset_get(dset);
310 d2 = dns_dispatchset_get(dset);
311 d3 = dns_dispatchset_get(dset);
312 d4 = dns_dispatchset_get(dset);
313 d5 = dns_dispatchset_get(dset);
314
315 assert_ptr_equal(d1, d2);
316 assert_ptr_equal(d2, d3);
317 assert_ptr_equal(d3, d4);
318 assert_ptr_equal(d4, d5);
319
320 dns_dispatchset_destroy(&dset);
321
322 result = make_dispatchset(dispatchmgr, 4, &dset);
323 assert_int_equal(result, ISC_R_SUCCESS);
324
325 /*
326 * Temporarily modify and then restore the current thread's
327 * ID value, in order to confirm that different threads get
328 * different dispatch sets but the same thread gets the same
329 * one.
330 */
331 tid_saved = isc__tid_local;
332 d1 = dns_dispatchset_get(dset);
333 isc__tid_local++;
334 d2 = dns_dispatchset_get(dset);
335 isc__tid_local++;
336 d3 = dns_dispatchset_get(dset);
337 isc__tid_local++;
338 d4 = dns_dispatchset_get(dset);
339 isc__tid_local = tid_saved;
340 d5 = dns_dispatchset_get(dset);
341
342 assert_ptr_equal(d1, d5);
343 assert_ptr_not_equal(d1, d2);
344 assert_ptr_not_equal(d2, d3);
345 assert_ptr_not_equal(d3, d4);
346 assert_ptr_not_equal(d4, d5);
347
348 dns_dispatchset_destroy(&dset);
349 dns_dispatchmgr_detach(&dispatchmgr);
350 isc_loopmgr_shutdown(loopmgr);
351 }
352
353 static atomic_bool first = true;
354
355 static void
356 server_senddone(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
357 UNUSED(handle);
358 UNUSED(eresult);
359 UNUSED(arg);
360
361 return;
362 }
363
364 static void
365 nameserver(isc_nmhandle_t *handle, isc_result_t eresult, isc_region_t *region,
366 void *arg ISC_ATTR_UNUSED) {
367 isc_region_t response1, response2;
368 static unsigned char buf1[16];
369 static unsigned char buf2[16];
370
371 if (eresult != ISC_R_SUCCESS) {
372 return;
373 }
374
375 memmove(buf1, region->base, 12);
376 memset(buf1 + 12, 0, 4);
377 buf1[2] |= 0x80; /* qr=1 */
378
379 memmove(buf2, region->base, 12);
380 memset(buf2 + 12, 1, 4);
381 buf2[2] |= 0x80; /* qr=1 */
382
383 /*
384 * send message to be discarded.
385 */
386 response1.base = buf1;
387 response1.length = sizeof(buf1);
388 isc_nm_send(handle, &response1, server_senddone, NULL);
389
390 /*
391 * send nextitem message.
392 */
393 response2.base = buf2;
394 response2.length = sizeof(buf2);
395 isc_nm_send(handle, &response2, server_senddone, NULL);
396 }
397
398 static isc_result_t
399 accept_cb(isc_nmhandle_t *handle, isc_result_t eresult, void *arg) {
400 UNUSED(handle);
401 UNUSED(arg);
402
403 return eresult;
404 }
405
406 static void
407 noop_nameserver(isc_nmhandle_t *handle, isc_result_t eresult,
408 isc_region_t *region, void *arg) {
409 UNUSED(handle);
410 UNUSED(eresult);
411 UNUSED(region);
412 UNUSED(arg);
413 }
414
415 static void
416 response_getnext(isc_result_t result, isc_region_t *region ISC_ATTR_UNUSED,
417 void *arg) {
418 test_dispatch_t *test = arg;
419
420 if (atomic_compare_exchange_strong(&first, &(bool){ true }, false)) {
421 result = dns_dispatch_getnext(test->dispentry);
422 assert_int_equal(result, ISC_R_SUCCESS);
423 } else {
424 test_dispatch_shutdown(test);
425 }
426 }
427
428 static void
429 response_noop(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
430 void *arg) {
431 test_dispatch_t *test = arg;
432
433 if (eresult == ISC_R_SUCCESS) {
434 test_dispatch_done(test);
435 }
436 }
437
438 static void
439 response_shutdown(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
440 void *arg) {
441 test_dispatch_t *test = arg;
442
443 assert_int_equal(eresult, ISC_R_SUCCESS);
444
445 test_dispatch_shutdown(test);
446 }
447
448 static void
449 response_timeout(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
450 void *arg) {
451 test_dispatch_t *test = arg;
452
453 assert_int_equal(eresult, ISC_R_TIMEDOUT);
454
455 test_dispatch_shutdown(test);
456 }
457
458 static void
459 client_senddone(isc_result_t eresult, isc_region_t *region, void *arg) {
460 UNUSED(eresult);
461 UNUSED(region);
462 UNUSED(arg);
463 }
464
465 static void
466 connected(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
467 void *arg) {
468 test_dispatch_t *test = arg;
469
470 switch (eresult) {
471 case ISC_R_CONNECTIONRESET:
472 /* Don't send any data if the connection failed */
473 test_dispatch_shutdown(test);
474 return;
475 default:
476 assert_int_equal(eresult, ISC_R_SUCCESS);
477 }
478
479 dns_dispatch_send(test->dispentry, &testdata.region);
480 }
481
482 static void
483 connected_shutdown(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
484 void *arg) {
485 test_dispatch_t *test = arg;
486
487 switch (eresult) {
488 case ISC_R_CONNECTIONRESET:
489 /* Skip */
490 break;
491 default:
492 assert_int_equal(eresult, ISC_R_SUCCESS);
493 }
494
495 test_dispatch_shutdown(test);
496 }
497
498 static void
499 connected_gettcp(isc_result_t eresult ISC_ATTR_UNUSED,
500 isc_region_t *region ISC_ATTR_UNUSED, void *arg) {
501 test_dispatch_t *test1 = arg;
502
503 /* Client 2 */
504 isc_result_t result;
505 test_dispatch_t *test2 = isc_mem_get(mctx, sizeof(*test2));
506 *test2 = (test_dispatch_t){
507 .dispatchmgr = dns_dispatchmgr_ref(test1->dispatchmgr),
508 };
509
510 result = dns_dispatch_gettcp(test2->dispatchmgr, &tcp_server_addr,
511 &tcp_connect_addr, NULL, &test2->dispatch);
512 assert_int_equal(result, ISC_R_SUCCESS);
513
514 assert_ptr_equal(test1->dispatch, test2->dispatch);
515
516 result = dns_dispatch_add(test2->dispatch, isc_loop_main(loopmgr), 0,
517 T_CLIENT_CONNECT, &tcp_server_addr, NULL,
518 NULL, connected_shutdown, client_senddone,
519 response_noop, test2, &test2->id,
520 &test2->dispentry);
521 assert_int_equal(result, ISC_R_SUCCESS);
522
523 dns_dispatch_connect(test2->dispentry);
524
525 test_dispatch_done(test1);
526 }
527
528 static void
529 connected_newtcp(isc_result_t eresult ISC_ATTR_UNUSED,
530 isc_region_t *region ISC_ATTR_UNUSED, void *arg) {
531 test_dispatch_t *test3 = arg;
532
533 /* Client - unshared */
534 isc_result_t result;
535 test_dispatch_t *test4 = isc_mem_get(mctx, sizeof(*test4));
536 *test4 = (test_dispatch_t){
537 .dispatchmgr = dns_dispatchmgr_ref(test3->dispatchmgr),
538 };
539 result = dns_dispatch_gettcp(test4->dispatchmgr, &tcp_server_addr,
540 &tcp_connect_addr, NULL, &test4->dispatch);
541 assert_int_equal(result, ISC_R_NOTFOUND);
542
543 result = dns_dispatch_createtcp(
544 test4->dispatchmgr, &tcp_connect_addr, &tcp_server_addr, NULL,
545 DNS_DISPATCHOPT_UNSHARED, &test4->dispatch);
546 assert_int_equal(result, ISC_R_SUCCESS);
547
548 assert_ptr_not_equal(test3->dispatch, test4->dispatch);
549
550 result = dns_dispatch_add(test4->dispatch, isc_loop_main(loopmgr), 0,
551 T_CLIENT_CONNECT, &tcp_server_addr, NULL,
552 NULL, connected_shutdown, client_senddone,
553 response_noop, test4, &test4->id,
554 &test4->dispentry);
555 assert_int_equal(result, ISC_R_SUCCESS);
556
557 dns_dispatch_connect(test4->dispentry);
558
559 test_dispatch_done(test3);
560 }
561
562 static void
563 timeout_connected(isc_result_t eresult, isc_region_t *region ISC_ATTR_UNUSED,
564 void *arg) {
565 test_dispatch_t *test = arg;
566
567 switch (eresult) {
568 case ISC_R_ADDRNOTAVAIL:
569 case ISC_R_CONNREFUSED:
570 /* Skip */
571 break;
572 default:
573 assert_int_equal(eresult, ISC_R_TIMEDOUT);
574 }
575
576 test_dispatch_shutdown(test);
577 }
578
579 ISC_LOOP_TEST_IMPL(dispatch_timeout_tcp_connect) {
580 isc_result_t result;
581 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
582 *test = (test_dispatch_t){ 0 };
583
584 /* Client */
585 tcp_connect_addr = (isc_sockaddr_t){ .length = 0 };
586 isc_sockaddr_fromin6(&tcp_connect_addr, &in6addr_blackhole, 0);
587
588 testdata.region.base = testdata.message;
589 testdata.region.length = sizeof(testdata.message);
590
591 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
592 &test->dispatchmgr);
593 assert_int_equal(result, ISC_R_SUCCESS);
594
595 result = dns_dispatch_createtcp(test->dispatchmgr, &tcp_connect_addr,
596 &tcp_server_addr, NULL, 0,
597 &test->dispatch);
598 assert_int_equal(result, ISC_R_SUCCESS);
599
600 result = dns_dispatch_add(test->dispatch, isc_loop_main(loopmgr), 0,
601 T_CLIENT_CONNECT_SHORT, &tcp_server_addr,
602 NULL, NULL, timeout_connected,
603 client_senddone, response_timeout, test,
604 &test->id, &test->dispentry);
605 assert_int_equal(result, ISC_R_SUCCESS);
606
607 testdata.message[0] = (test->id >> 8) & 0xff;
608 testdata.message[1] = test->id & 0xff;
609
610 dns_dispatch_connect(test->dispentry);
611 }
612
613 static void
614 stop_listening(void *arg) {
615 UNUSED(arg);
616
617 isc_nm_stoplistening(sock);
618 isc_nmsocket_close(&sock);
619 assert_null(sock);
620 }
621
622 ISC_LOOP_TEST_IMPL(dispatch_timeout_tcp_response) {
623 isc_result_t result;
624 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
625 *test = (test_dispatch_t){ 0 };
626
627 /* Server */
628 result = isc_nm_listenstreamdns(
629 netmgr, ISC_NM_LISTEN_ONE, &tcp_server_addr, noop_nameserver,
630 NULL, accept_cb, NULL, 0, NULL, NULL, ISC_NM_PROXY_NONE, &sock);
631 assert_int_equal(result, ISC_R_SUCCESS);
632
633 /* ensure we stop listening after the test is done */
634 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
635
636 /* Client */
637 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
638 &test->dispatchmgr);
639 assert_int_equal(result, ISC_R_SUCCESS);
640
641 result = dns_dispatch_createtcp(test->dispatchmgr, &tcp_connect_addr,
642 &tcp_server_addr, NULL, 0,
643 &test->dispatch);
644 assert_int_equal(result, ISC_R_SUCCESS);
645
646 result = dns_dispatch_add(test->dispatch, isc_loop_main(loopmgr), 0,
647 T_CLIENT_CONNECT_SHORT, &tcp_server_addr,
648 NULL, NULL, connected, client_senddone,
649 response_timeout, test, &test->id,
650 &test->dispentry);
651 assert_int_equal(result, ISC_R_SUCCESS);
652
653 dns_dispatch_connect(test->dispentry);
654 }
655
656 ISC_LOOP_TEST_IMPL(dispatch_tcp_response) {
657 isc_result_t result;
658 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
659 *test = (test_dispatch_t){ 0 };
660
661 /* Server */
662 result = isc_nm_listenstreamdns(
663 netmgr, ISC_NM_LISTEN_ONE, &tcp_server_addr, nameserver, NULL,
664 accept_cb, NULL, 0, NULL, NULL, ISC_NM_PROXY_NONE, &sock);
665 assert_int_equal(result, ISC_R_SUCCESS);
666
667 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
668
669 /* Client */
670 testdata.region.base = testdata.message;
671 testdata.region.length = sizeof(testdata.message);
672
673 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
674 &test->dispatchmgr);
675 assert_int_equal(result, ISC_R_SUCCESS);
676
677 result = dns_dispatch_createtcp(test->dispatchmgr, &tcp_connect_addr,
678 &tcp_server_addr, NULL, 0,
679 &test->dispatch);
680 assert_int_equal(result, ISC_R_SUCCESS);
681
682 result = dns_dispatch_add(
683 test->dispatch, isc_loop_main(loopmgr), 0, T_CLIENT_CONNECT,
684 &tcp_server_addr, NULL, NULL, connected, client_senddone,
685 response_shutdown, test, &test->id, &test->dispentry);
686 assert_int_equal(result, ISC_R_SUCCESS);
687
688 testdata.message[0] = (test->id >> 8) & 0xff;
689 testdata.message[1] = test->id & 0xff;
690
691 dns_dispatch_connect(test->dispentry);
692 }
693
694 ISC_LOOP_TEST_IMPL(dispatch_tls_response) {
695 isc_result_t result;
696 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
697 *test = (test_dispatch_t){ 0 };
698
699 /* Server */
700 result = isc_nm_listenstreamdns(
701 netmgr, ISC_NM_LISTEN_ONE, &tls_server_addr, nameserver, NULL,
702 accept_cb, NULL, 0, NULL, tls_listen_tlsctx, ISC_NM_PROXY_NONE,
703 &sock);
704 assert_int_equal(result, ISC_R_SUCCESS);
705
706 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
707
708 /* Client */
709 testdata.region.base = testdata.message;
710 testdata.region.length = sizeof(testdata.message);
711
712 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
713 &test->dispatchmgr);
714 assert_int_equal(result, ISC_R_SUCCESS);
715
716 result = dns_dispatch_createtcp(test->dispatchmgr, &tls_connect_addr,
717 &tls_server_addr, tls_transport, 0,
718 &test->dispatch);
719 assert_int_equal(result, ISC_R_SUCCESS);
720
721 result = dns_dispatch_add(test->dispatch, isc_loop_main(loopmgr), 0,
722 T_CLIENT_CONNECT, &tls_server_addr,
723 tls_transport, tls_tlsctx_client_cache,
724 connected, client_senddone, response_shutdown,
725 test, &test->id, &test->dispentry);
726 assert_int_equal(result, ISC_R_SUCCESS);
727
728 testdata.message[0] = (test->id >> 8) & 0xff;
729 testdata.message[1] = test->id & 0xff;
730
731 dns_dispatch_connect(test->dispentry);
732 }
733
734 ISC_LOOP_TEST_IMPL(dispatch_timeout_udp_response) {
735 isc_result_t result;
736 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
737 *test = (test_dispatch_t){ 0 };
738
739 /* Server */
740 result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ONE, &udp_server_addr,
741 noop_nameserver, NULL, &sock);
742 assert_int_equal(result, ISC_R_SUCCESS);
743
744 /* ensure we stop listening after the test is done */
745 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
746
747 /* Client */
748 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
749 &test->dispatchmgr);
750 assert_int_equal(result, ISC_R_SUCCESS);
751
752 result = dns_dispatch_createudp(test->dispatchmgr, &udp_connect_addr,
753 &test->dispatch);
754 assert_int_equal(result, ISC_R_SUCCESS);
755
756 result = dns_dispatch_add(test->dispatch, isc_loop_main(loopmgr), 0,
757 T_CLIENT_CONNECT_SHORT, &udp_server_addr,
758 NULL, NULL, connected, client_senddone,
759 response_timeout, test, &test->id,
760 &test->dispentry);
761 assert_int_equal(result, ISC_R_SUCCESS);
762
763 dns_dispatch_connect(test->dispentry);
764 }
765
766 /* test dispatch getnext */
767 ISC_LOOP_TEST_IMPL(dispatch_getnext) {
768 isc_result_t result;
769 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
770 *test = (test_dispatch_t){ 0 };
771
772 /* Server */
773 result = isc_nm_listenudp(netmgr, ISC_NM_LISTEN_ONE, &udp_server_addr,
774 nameserver, NULL, &sock);
775 assert_int_equal(result, ISC_R_SUCCESS);
776
777 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
778
779 /* Client */
780 testdata.region.base = testdata.message;
781 testdata.region.length = sizeof(testdata.message);
782
783 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
784 &test->dispatchmgr);
785 assert_int_equal(result, ISC_R_SUCCESS);
786
787 result = dns_dispatch_createudp(test->dispatchmgr, &udp_connect_addr,
788 &test->dispatch);
789 assert_int_equal(result, ISC_R_SUCCESS);
790
791 result = dns_dispatch_add(
792 test->dispatch, isc_loop_main(loopmgr), 0, T_CLIENT_CONNECT,
793 &udp_server_addr, NULL, NULL, connected, client_senddone,
794 response_getnext, test, &test->id, &test->dispentry);
795 assert_int_equal(result, ISC_R_SUCCESS);
796
797 testdata.message[0] = (test->id >> 8) & 0xff;
798 testdata.message[1] = test->id & 0xff;
799
800 dns_dispatch_connect(test->dispentry);
801 }
802
803 ISC_LOOP_TEST_IMPL(dispatch_gettcp) {
804 isc_result_t result;
805 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
806 *test = (test_dispatch_t){ 0 };
807
808 /* Server */
809 result = isc_nm_listenstreamdns(
810 netmgr, ISC_NM_LISTEN_ONE, &tcp_server_addr, nameserver, NULL,
811 accept_cb, NULL, 0, NULL, NULL, ISC_NM_PROXY_NONE, &sock);
812 assert_int_equal(result, ISC_R_SUCCESS);
813
814 /* ensure we stop listening after the test is done */
815 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
816
817 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
818 &test->dispatchmgr);
819 assert_int_equal(result, ISC_R_SUCCESS);
820
821 /* Client */
822 result = dns_dispatch_createtcp(test->dispatchmgr, &tcp_connect_addr,
823 &tcp_server_addr, NULL, 0,
824 &test->dispatch);
825 assert_int_equal(result, ISC_R_SUCCESS);
826
827 result = dns_dispatch_add(
828 test->dispatch, isc_loop_main(loopmgr), 0, T_CLIENT_CONNECT,
829 &tcp_server_addr, NULL, NULL, connected_gettcp, client_senddone,
830 response_noop, test, &test->id, &test->dispentry);
831 assert_int_equal(result, ISC_R_SUCCESS);
832
833 dns_dispatch_connect(test->dispentry);
834 }
835
836 ISC_LOOP_TEST_IMPL(dispatch_newtcp) {
837 isc_result_t result;
838 test_dispatch_t *test = isc_mem_get(mctx, sizeof(*test));
839 *test = (test_dispatch_t){ 0 };
840
841 /* Server */
842 result = isc_nm_listenstreamdns(
843 netmgr, ISC_NM_LISTEN_ONE, &tcp_server_addr, nameserver, NULL,
844 accept_cb, NULL, 0, NULL, NULL, ISC_NM_PROXY_NONE, &sock);
845 assert_int_equal(result, ISC_R_SUCCESS);
846
847 /* ensure we stop listening after the test is done */
848 isc_loop_teardown(isc_loop_main(loopmgr), stop_listening, sock);
849
850 /* Client - unshared */
851 result = dns_dispatchmgr_create(mctx, loopmgr, connect_nm,
852 &test->dispatchmgr);
853 assert_int_equal(result, ISC_R_SUCCESS);
854
855 result = dns_dispatch_createtcp(
856 test->dispatchmgr, &tcp_connect_addr, &tcp_server_addr, NULL,
857 DNS_DISPATCHOPT_UNSHARED, &test->dispatch);
858 assert_int_equal(result, ISC_R_SUCCESS);
859
860 result = dns_dispatch_add(
861 test->dispatch, isc_loop_main(loopmgr), 0, T_CLIENT_CONNECT,
862 &tcp_server_addr, NULL, NULL, connected_newtcp, client_senddone,
863 response_noop, test, &test->id, &test->dispentry);
864 assert_int_equal(result, ISC_R_SUCCESS);
865
866 dns_dispatch_connect(test->dispentry);
867 }
868
869 ISC_TEST_LIST_START
870 ISC_TEST_ENTRY_CUSTOM(dispatch_gettcp, setup_test, teardown_test)
871 ISC_TEST_ENTRY_CUSTOM(dispatch_newtcp, setup_test, teardown_test)
872 ISC_TEST_ENTRY_CUSTOM(dispatch_timeout_udp_response, setup_test, teardown_test)
873 ISC_TEST_ENTRY_CUSTOM(dispatchset_create, setup_test, teardown_test)
874 ISC_TEST_ENTRY_CUSTOM(dispatchset_get, setup_test, teardown_test)
875 ISC_TEST_ENTRY_CUSTOM(dispatch_timeout_tcp_response, setup_test, teardown_test)
876 ISC_TEST_ENTRY_CUSTOM(dispatch_timeout_tcp_connect, setup_test, teardown_test)
877 ISC_TEST_ENTRY_CUSTOM(dispatch_tcp_response, setup_test, teardown_test)
878 ISC_TEST_ENTRY_CUSTOM(dispatch_tls_response, setup_test, teardown_test)
879 ISC_TEST_ENTRY_CUSTOM(dispatch_getnext, setup_test, teardown_test)
880 ISC_TEST_LIST_END
881
882 ISC_TEST_MAIN