xref: /netbsd-src/external/mpl/bind/dist/tests/ns/listenlist_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: listenlist_test.c,v 1.3 2025/01/26 16:25:51 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 <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #define UNIT_TESTING
27 #include <cmocka.h>
28 
29 #include <isc/list.h>
30 #include <isc/random.h>
31 #include <isc/util.h>
32 
33 #include <dns/acl.h>
34 
35 #include <ns/listenlist.h>
36 
37 #include <tests/ns.h>
38 
39 /* test that ns_listenlist_default() works */
40 ISC_RUN_TEST_IMPL(ns_listenlist_default) {
41 	isc_result_t result;
42 	in_port_t port = 5300 + isc_random8();
43 	ns_listenlist_t *list = NULL;
44 	ns_listenelt_t *elt;
45 	int count;
46 
47 	UNUSED(state);
48 
49 	result = ns_listenlist_default(mctx, port, false, AF_INET, &list);
50 	assert_int_equal(result, ISC_R_SUCCESS);
51 	assert_non_null(list);
52 
53 	assert_false(ISC_LIST_EMPTY(list->elts));
54 
55 	count = 0;
56 	elt = ISC_LIST_HEAD(list->elts);
57 	while (elt != NULL) {
58 		ns_listenelt_t *next = ISC_LIST_NEXT(elt, link);
59 		dns_acl_t *acl = NULL;
60 
61 		dns_acl_attach(elt->acl, &acl);
62 		ISC_LIST_UNLINK(list->elts, elt, link);
63 		ns_listenelt_destroy(elt);
64 		elt = next;
65 
66 		assert_true(dns_acl_isnone(acl));
67 		dns_acl_detach(&acl);
68 		count++;
69 	}
70 
71 	assert_true(ISC_LIST_EMPTY(list->elts));
72 	assert_int_equal(count, 1);
73 
74 	ns_listenlist_detach(&list);
75 
76 	result = ns_listenlist_default(mctx, port, true, AF_INET, &list);
77 	assert_int_equal(result, ISC_R_SUCCESS);
78 
79 	assert_false(ISC_LIST_EMPTY(list->elts));
80 
81 	/* This time just use ns_listenlist_detach() to destroy elements */
82 	count = 0;
83 	elt = ISC_LIST_HEAD(list->elts);
84 	while (elt != NULL) {
85 		ns_listenelt_t *next = ISC_LIST_NEXT(elt, link);
86 		assert_true(dns_acl_isany(elt->acl));
87 		elt = next;
88 		count++;
89 	}
90 
91 	assert_int_equal(count, 1);
92 
93 	ns_listenlist_detach(&list);
94 }
95 
96 ISC_TEST_LIST_START
97 ISC_TEST_ENTRY(ns_listenlist_default)
98 ISC_TEST_LIST_END
99 
100 ISC_TEST_MAIN
101