| 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 <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/buffer.h> |
| 27 |
#include <isc/timer.h> |
| 28 |
#include <isc/util.h> |
| 29 |
|
| 30 |
#include <dns/name.h> |
| 31 |
#include <dns/view.h> |
| 32 |
#include <dns/zone.h> |
| 33 |
|
| 34 |
#include <tests/dns.h> |
| 35 |
|
| 36 |
static int |
| 37 |
setup_test(void **state) { |
| 38 |
setup_loopmgr(state); |
| 39 |
setup_netmgr(state); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
static int |
| 45 |
teardown_test(void **state) { |
| 46 |
teardown_netmgr(state); |
| 47 |
teardown_loopmgr(state); |
| 48 |
|
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
/* create zone manager */ |
| 53 |
ISC_LOOP_TEST_IMPL(zonemgr_create) { |
| 54 |
dns_zonemgr_t *myzonemgr = NULL; |
| 55 |
|
| 56 |
UNUSED(arg); |
| 57 |
|
| 58 |
dns_zonemgr_create(mctx, netmgr, &myzonemgr); |
| 59 |
|
| 60 |
dns_zonemgr_shutdown(myzonemgr); |
| 61 |
dns_zonemgr_detach(&myzonemgr); |
| 62 |
assert_null(myzonemgr); |
| 63 |
|
| 64 |
isc_loopmgr_shutdown(loopmgr); |
| 65 |
} |
| 66 |
|
| 67 |
/* manage and release a zone */ |
| 68 |
ISC_LOOP_TEST_IMPL(zonemgr_managezone) { |
| 69 |
dns_zonemgr_t *myzonemgr = NULL; |
| 70 |
dns_zone_t *zone = NULL; |
| 71 |
isc_result_t result; |
| 72 |
|
| 73 |
UNUSED(arg); |
| 74 |
|
| 75 |
dns_zonemgr_create(mctx, netmgr, &myzonemgr); |
| 76 |
|
| 77 |
result = dns_test_makezone("foo", &zone, NULL, false); |
| 78 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 79 |
|
| 80 |
assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 0); |
| 81 |
|
| 82 |
/* Now it should succeed */ |
| 83 |
result = dns_zonemgr_managezone(myzonemgr, zone); |
| 84 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 85 |
|
| 86 |
assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 1); |
| 87 |
|
| 88 |
dns_zonemgr_releasezone(myzonemgr, zone); |
| 89 |
dns_zone_detach(&zone); |
| 90 |
|
| 91 |
assert_int_equal(dns_zonemgr_getcount(myzonemgr, DNS_ZONESTATE_ANY), 0); |
| 92 |
|
| 93 |
dns_zonemgr_shutdown(myzonemgr); |
| 94 |
dns_zonemgr_detach(&myzonemgr); |
| 95 |
assert_null(myzonemgr); |
| 96 |
|
| 97 |
isc_loopmgr_shutdown(loopmgr); |
| 98 |
} |
| 99 |
|
| 100 |
/* create and release a zone */ |
| 101 |
ISC_LOOP_TEST_IMPL(zonemgr_createzone) { |
| 102 |
dns_zonemgr_t *myzonemgr = NULL; |
| 103 |
dns_zone_t *zone = NULL; |
| 104 |
isc_result_t result; |
| 105 |
|
| 106 |
UNUSED(arg); |
| 107 |
|
| 108 |
dns_zonemgr_create(mctx, netmgr, &myzonemgr); |
| 109 |
|
| 110 |
result = dns_zonemgr_createzone(myzonemgr, &zone); |
| 111 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 112 |
assert_non_null(zone); |
| 113 |
|
| 114 |
result = dns_zonemgr_managezone(myzonemgr, zone); |
| 115 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 116 |
|
| 117 |
dns_zone_detach(&zone); |
| 118 |
|
| 119 |
dns_zonemgr_shutdown(myzonemgr); |
| 120 |
dns_zonemgr_detach(&myzonemgr); |
| 121 |
assert_null(myzonemgr); |
| 122 |
|
| 123 |
isc_loopmgr_shutdown(loopmgr); |
| 124 |
} |
| 125 |
|
| 126 |
/* manage and release a zone */ |
| 127 |
ISC_LOOP_TEST_IMPL(zonemgr_unreachable) { |
| 128 |
dns_zonemgr_t *myzonemgr = NULL; |
| 129 |
dns_zone_t *zone = NULL; |
| 130 |
isc_sockaddr_t addr1, addr2; |
| 131 |
struct in_addr in; |
| 132 |
isc_result_t result; |
| 133 |
isc_time_t now; |
| 134 |
|
| 135 |
UNUSED(arg); |
| 136 |
|
| 137 |
now = isc_time_now(); |
| 138 |
|
| 139 |
dns_zonemgr_create(mctx, netmgr, &myzonemgr); |
| 140 |
|
| 141 |
result = dns_test_makezone("foo", &zone, NULL, false); |
| 142 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 143 |
|
| 144 |
result = dns_zonemgr_managezone(myzonemgr, zone); |
| 145 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 146 |
|
| 147 |
in.s_addr = inet_addr("10.53.0.1"); |
| 148 |
isc_sockaddr_fromin(&addr1, &in, 2112); |
| 149 |
in.s_addr = inet_addr("10.53.0.2"); |
| 150 |
isc_sockaddr_fromin(&addr2, &in, 5150); |
| 151 |
assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 152 |
/* |
| 153 |
* We require multiple unreachableadd calls to mark a server as |
| 154 |
* unreachable. |
| 155 |
*/ |
| 156 |
dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now); |
| 157 |
assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 158 |
dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now); |
| 159 |
assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 160 |
|
| 161 |
in.s_addr = inet_addr("10.53.0.3"); |
| 162 |
isc_sockaddr_fromin(&addr2, &in, 5150); |
| 163 |
assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 164 |
/* |
| 165 |
* We require multiple unreachableadd calls to mark a server as |
| 166 |
* unreachable. |
| 167 |
*/ |
| 168 |
dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now); |
| 169 |
dns_zonemgr_unreachableadd(myzonemgr, &addr1, &addr2, &now); |
| 170 |
assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 171 |
|
| 172 |
dns_zonemgr_unreachabledel(myzonemgr, &addr1, &addr2); |
| 173 |
assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 174 |
|
| 175 |
in.s_addr = inet_addr("10.53.0.2"); |
| 176 |
isc_sockaddr_fromin(&addr2, &in, 5150); |
| 177 |
assert_true(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 178 |
dns_zonemgr_unreachabledel(myzonemgr, &addr1, &addr2); |
| 179 |
assert_false(dns_zonemgr_unreachable(myzonemgr, &addr1, &addr2, &now)); |
| 180 |
|
| 181 |
dns_zonemgr_releasezone(myzonemgr, zone); |
| 182 |
dns_zone_detach(&zone); |
| 183 |
dns_zonemgr_shutdown(myzonemgr); |
| 184 |
dns_zonemgr_detach(&myzonemgr); |
| 185 |
assert_null(myzonemgr); |
| 186 |
|
| 187 |
isc_loopmgr_shutdown(loopmgr); |
| 188 |
} |
| 189 |
|
| 190 |
ISC_TEST_LIST_START |
| 191 |
ISC_TEST_ENTRY_CUSTOM(zonemgr_create, setup_test, teardown_test) |
| 192 |
ISC_TEST_ENTRY_CUSTOM(zonemgr_managezone, setup_test, teardown_test) |
| 193 |
ISC_TEST_ENTRY_CUSTOM(zonemgr_createzone, setup_test, teardown_test) |
| 194 |
ISC_TEST_ENTRY_CUSTOM(zonemgr_unreachable, setup_test, teardown_test) |
| 195 |
ISC_TEST_LIST_END |
| 196 |
|
| 197 |
ISC_TEST_MAIN |