Debian Codegraph @

tests/ns/listenlist_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 <stddef.h>
19 #include <stdio.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/list.h>
28 #include <isc/random.h>
29 #include <isc/util.h>
30
31 #include <dns/acl.h>
32
33 #include <ns/listenlist.h>
34
35 #include <tests/ns.h>
36
37 /* test that ns_listenlist_default() works */
38 ISC_RUN_TEST_IMPL(ns_listenlist_default) {
39 isc_result_t result;
40 in_port_t port = 5300 + isc_random8();
41 ns_listenlist_t *list = NULL;
42 ns_listenelt_t *elt;
43 int count;
44
45 UNUSED(state);
46
47 result = ns_listenlist_default(mctx, port, false, AF_INET, &list);
48 assert_int_equal(result, ISC_R_SUCCESS);
49 assert_non_null(list);
50
51 assert_false(ISC_LIST_EMPTY(list->elts));
52
53 count = 0;
54 elt = ISC_LIST_HEAD(list->elts);
55 while (elt != NULL) {
56 ns_listenelt_t *next = ISC_LIST_NEXT(elt, link);
57 dns_acl_t *acl = NULL;
58
59 dns_acl_attach(elt->acl, &acl);
60 ISC_LIST_UNLINK(list->elts, elt, link);
61 ns_listenelt_destroy(elt);
62 elt = next;
63
64 assert_true(dns_acl_isnone(acl));
65 dns_acl_detach(&acl);
66 count++;
67 }
68
69 assert_true(ISC_LIST_EMPTY(list->elts));
70 assert_int_equal(count, 1);
71
72 ns_listenlist_detach(&list);
73
74 result = ns_listenlist_default(mctx, port, true, AF_INET, &list);
75 assert_int_equal(result, ISC_R_SUCCESS);
76
77 assert_false(ISC_LIST_EMPTY(list->elts));
78
79 /* This time just use ns_listenlist_detach() to destroy elements */
80 count = 0;
81 elt = ISC_LIST_HEAD(list->elts);
82 while (elt != NULL) {
83 ns_listenelt_t *next = ISC_LIST_NEXT(elt, link);
84 assert_true(dns_acl_isany(elt->acl));
85 elt = next;
86 count++;
87 }
88
89 assert_int_equal(count, 1);
90
91 ns_listenlist_detach(&list);
92 }
93
94 ISC_TEST_LIST_START
95 ISC_TEST_ENTRY(ns_listenlist_default)
96 ISC_TEST_LIST_END
97
98 ISC_TEST_MAIN