1/* $NetBSD: listenlist_test.c,v 1.2 2024/02/21 22:52: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/print.h> 31#include <isc/random.h> 32#include <isc/util.h> 33 34#include <dns/acl.h> 35 36#include <ns/listenlist.h> 37 38#include <tests/ns.h> 39 40static int 41_setup(void **state) { 42 isc__nm_force_tid(0); 43 44 setup_managers(state); 45 46 return (0); 47} 48 49static int 50_teardown(void **state) { 51 isc__nm_force_tid(-1); 52 53 teardown_managers(state); 54 55 return (0); 56} 57 58/* test that ns_listenlist_default() works */ 59ISC_RUN_TEST_IMPL(ns_listenlist_default) { 60 isc_result_t result; 61 in_port_t port = 5300 + isc_random8(); 62 ns_listenlist_t *list = NULL; 63 ns_listenelt_t *elt; 64 int count; 65 66 UNUSED(state); 67 68 result = ns_listenlist_default(mctx, port, false, AF_INET, &list); 69 assert_int_equal(result, ISC_R_SUCCESS); 70 assert_non_null(list); 71 72 assert_false(ISC_LIST_EMPTY(list->elts)); 73 74 count = 0; 75 elt = ISC_LIST_HEAD(list->elts); 76 while (elt != NULL) { 77 ns_listenelt_t *next = ISC_LIST_NEXT(elt, link); 78 dns_acl_t *acl = NULL; 79 80 dns_acl_attach(elt->acl, &acl); 81 ISC_LIST_UNLINK(list->elts, elt, link); 82 ns_listenelt_destroy(elt); 83 elt = next; 84 85 assert_true(dns_acl_isnone(acl)); 86 dns_acl_detach(&acl); 87 count++; 88 } 89 90 assert_true(ISC_LIST_EMPTY(list->elts)); 91 assert_int_equal(count, 1); 92 93 ns_listenlist_detach(&list); 94 95 result = ns_listenlist_default(mctx, port, true, AF_INET, &list); 96 assert_int_equal(result, ISC_R_SUCCESS); 97 98 assert_false(ISC_LIST_EMPTY(list->elts)); 99 100 /* This time just use ns_listenlist_detach() to destroy elements */ 101 count = 0; 102 elt = ISC_LIST_HEAD(list->elts); 103 while (elt != NULL) { 104 ns_listenelt_t *next = ISC_LIST_NEXT(elt, link); 105 assert_true(dns_acl_isany(elt->acl)); 106 elt = next; 107 count++; 108 } 109 110 assert_int_equal(count, 1); 111 112 ns_listenlist_detach(&list); 113} 114 115ISC_TEST_LIST_START 116 117ISC_TEST_ENTRY_CUSTOM(ns_listenlist_default, _setup, _teardown) 118 119ISC_TEST_LIST_END 120 121ISC_TEST_MAIN 122