| 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 <unistd.h> |
| 21 |
|
| 22 |
#define UNIT_TESTING |
| 23 |
#include <cmocka.h> |
| 24 |
|
| 25 |
#include <dns/db.h> |
| 26 |
#include <dns/dbiterator.h> |
| 27 |
#include <dns/journal.h> |
| 28 |
#include <dns/name.h> |
| 29 |
#include <dns/rdatalist.h> |
| 30 |
|
| 31 |
#include <tests/dns.h> |
| 32 |
|
| 33 |
#define BUFLEN 255 |
| 34 |
#define BIGBUFLEN (64 * 1024) |
| 35 |
#define TEST_ORIGIN "test" |
| 36 |
|
| 37 |
/* |
| 38 |
* Individual unit tests |
| 39 |
*/ |
| 40 |
|
| 41 |
/* test multiple calls to dns_db_getoriginnode */ |
| 42 |
ISC_LOOP_TEST_IMPL(getoriginnode) { |
| 43 |
dns_db_t *db = NULL; |
| 44 |
dns_dbnode_t *node = NULL; |
| 45 |
isc_result_t result; |
| 46 |
|
| 47 |
result = dns_db_create(mctx, ZONEDB_DEFAULT, dns_rootname, |
| 48 |
dns_dbtype_zone, dns_rdataclass_in, 0, NULL, |
| 49 |
&db); |
| 50 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 51 |
|
| 52 |
result = dns_db_getoriginnode(db, &node); |
| 53 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 54 |
dns_db_detachnode(db, &node); |
| 55 |
|
| 56 |
result = dns_db_getoriginnode(db, &node); |
| 57 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 58 |
dns_db_detachnode(db, &node); |
| 59 |
|
| 60 |
dns_db_detach(&db); |
| 61 |
isc_loopmgr_shutdown(loopmgr); |
| 62 |
} |
| 63 |
|
| 64 |
/* test getservestalettl and setservestalettl */ |
| 65 |
ISC_LOOP_TEST_IMPL(getsetservestalettl) { |
| 66 |
dns_db_t *db = NULL; |
| 67 |
isc_result_t result; |
| 68 |
dns_ttl_t ttl; |
| 69 |
|
| 70 |
result = dns_db_create(mctx, CACHEDB_DEFAULT, dns_rootname, |
| 71 |
dns_dbtype_cache, dns_rdataclass_in, 0, NULL, |
| 72 |
&db); |
| 73 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 74 |
|
| 75 |
ttl = 5000; |
| 76 |
result = dns_db_getservestalettl(db, &ttl); |
| 77 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 78 |
assert_int_equal(ttl, 0); |
| 79 |
|
| 80 |
ttl = 6 * 3600; |
| 81 |
result = dns_db_setservestalettl(db, ttl); |
| 82 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 83 |
|
| 84 |
ttl = 5000; |
| 85 |
result = dns_db_getservestalettl(db, &ttl); |
| 86 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 87 |
assert_int_equal(ttl, 6 * 3600); |
| 88 |
|
| 89 |
dns_db_detach(&db); |
| 90 |
isc_loopmgr_shutdown(loopmgr); |
| 91 |
} |
| 92 |
|
| 93 |
/* check DNS_DBFIND_STALEOK works */ |
| 94 |
ISC_LOOP_TEST_IMPL(dns_dbfind_staleok) { |
| 95 |
dns_db_t *db = NULL; |
| 96 |
dns_dbnode_t *node = NULL; |
| 97 |
dns_fixedname_t example_fixed; |
| 98 |
dns_fixedname_t found_fixed; |
| 99 |
dns_name_t *example; |
| 100 |
dns_name_t *found; |
| 101 |
dns_rdatalist_t rdatalist; |
| 102 |
dns_rdataset_t rdataset; |
| 103 |
int count; |
| 104 |
int pass; |
| 105 |
isc_result_t result; |
| 106 |
unsigned char data[] = { 0x0a, 0x00, 0x00, 0x01 }; |
| 107 |
|
| 108 |
result = dns_db_create(mctx, CACHEDB_DEFAULT, dns_rootname, |
| 109 |
dns_dbtype_cache, dns_rdataclass_in, 0, NULL, |
| 110 |
&db); |
| 111 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 112 |
|
| 113 |
example = dns_fixedname_initname(&example_fixed); |
| 114 |
found = dns_fixedname_initname(&found_fixed); |
| 115 |
|
| 116 |
result = dns_name_fromstring(example, "example", dns_rootname, 0, NULL); |
| 117 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 118 |
|
| 119 |
/* |
| 120 |
* Pass 0: default; no stale processing permitted. |
| 121 |
* Pass 1: stale processing for 1 second. |
| 122 |
* Pass 2: stale turned off after being on. |
| 123 |
*/ |
| 124 |
for (pass = 0; pass < 3; pass++) { |
| 125 |
dns_rdata_t rdata = DNS_RDATA_INIT; |
| 126 |
|
| 127 |
/* 10.0.0.1 */ |
| 128 |
rdata.data = data; |
| 129 |
rdata.length = 4; |
| 130 |
rdata.rdclass = dns_rdataclass_in; |
| 131 |
rdata.type = dns_rdatatype_a; |
| 132 |
|
| 133 |
dns_rdatalist_init(&rdatalist); |
| 134 |
rdatalist.ttl = 2; |
| 135 |
rdatalist.type = dns_rdatatype_a; |
| 136 |
rdatalist.rdclass = dns_rdataclass_in; |
| 137 |
ISC_LIST_APPEND(rdatalist.rdata, &rdata, link); |
| 138 |
|
| 139 |
switch (pass) { |
| 140 |
case 0: |
| 141 |
/* default: stale processing off */ |
| 142 |
break; |
| 143 |
case 1: |
| 144 |
/* turn on stale processing */ |
| 145 |
result = dns_db_setservestalettl(db, 1); |
| 146 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 147 |
break; |
| 148 |
case 2: |
| 149 |
/* turn off stale processing */ |
| 150 |
result = dns_db_setservestalettl(db, 0); |
| 151 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
dns_rdataset_init(&rdataset); |
| 156 |
dns_rdatalist_tordataset(&rdatalist, &rdataset); |
| 157 |
|
| 158 |
result = dns_db_findnode(db, example, true, &node); |
| 159 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 160 |
|
| 161 |
result = dns_db_addrdataset(db, node, NULL, 0, &rdataset, 0, |
| 162 |
NULL); |
| 163 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 164 |
|
| 165 |
dns_db_detachnode(db, &node); |
| 166 |
dns_rdataset_disassociate(&rdataset); |
| 167 |
|
| 168 |
result = dns_db_find(db, example, NULL, dns_rdatatype_a, 0, 0, |
| 169 |
&node, found, &rdataset, NULL); |
| 170 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 171 |
|
| 172 |
/* |
| 173 |
* May loop for up to 2 seconds performing non stale lookups. |
| 174 |
*/ |
| 175 |
count = 0; |
| 176 |
do { |
| 177 |
count++; |
| 178 |
assert_in_range(count, 1, 21); /* loop sanity */ |
| 179 |
assert_int_equal(rdataset.attributes & |
| 180 |
DNS_RDATASETATTR_STALE, |
| 181 |
0); |
| 182 |
assert_true(rdataset.ttl > 0); |
| 183 |
dns_db_detachnode(db, &node); |
| 184 |
dns_rdataset_disassociate(&rdataset); |
| 185 |
|
| 186 |
usleep(100000); /* 100 ms */ |
| 187 |
|
| 188 |
result = dns_db_find(db, example, NULL, dns_rdatatype_a, |
| 189 |
0, 0, &node, found, &rdataset, |
| 190 |
NULL); |
| 191 |
} while (result == ISC_R_SUCCESS); |
| 192 |
|
| 193 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 194 |
|
| 195 |
/* |
| 196 |
* Check whether we can get stale data. |
| 197 |
*/ |
| 198 |
result = dns_db_find(db, example, NULL, dns_rdatatype_a, |
| 199 |
DNS_DBFIND_STALEOK, 0, &node, found, |
| 200 |
&rdataset, NULL); |
| 201 |
switch (pass) { |
| 202 |
case 0: |
| 203 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 204 |
break; |
| 205 |
case 1: |
| 206 |
/* |
| 207 |
* Should loop for 1 second with stale lookups then |
| 208 |
* stop. |
| 209 |
*/ |
| 210 |
count = 0; |
| 211 |
do { |
| 212 |
count++; |
| 213 |
assert_in_range(count, 0, 49); /* loop sanity */ |
| 214 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 215 |
assert_int_equal(rdataset.attributes & |
| 216 |
DNS_RDATASETATTR_STALE, |
| 217 |
DNS_RDATASETATTR_STALE); |
| 218 |
dns_db_detachnode(db, &node); |
| 219 |
dns_rdataset_disassociate(&rdataset); |
| 220 |
|
| 221 |
usleep(100000); /* 100 ms */ |
| 222 |
|
| 223 |
result = dns_db_find( |
| 224 |
db, example, NULL, dns_rdatatype_a, |
| 225 |
DNS_DBFIND_STALEOK, 0, &node, found, |
| 226 |
&rdataset, NULL); |
| 227 |
} while (result == ISC_R_SUCCESS); |
| 228 |
/* |
| 229 |
* usleep(100000) can be slightly less than 10ms so |
| 230 |
* allow the count to reach 11. |
| 231 |
*/ |
| 232 |
assert_in_range(count, 1, 11); |
| 233 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 234 |
break; |
| 235 |
case 2: |
| 236 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
dns_db_detach(&db); |
| 242 |
isc_loopmgr_shutdown(loopmgr); |
| 243 |
} |
| 244 |
|
| 245 |
/* database class */ |
| 246 |
ISC_LOOP_TEST_IMPL(class) { |
| 247 |
isc_result_t result; |
| 248 |
dns_db_t *db = NULL; |
| 249 |
|
| 250 |
result = dns_db_create(mctx, ZONEDB_DEFAULT, dns_rootname, |
| 251 |
dns_dbtype_zone, dns_rdataclass_in, 0, NULL, |
| 252 |
&db); |
| 253 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 254 |
|
| 255 |
result = dns_db_load(db, TESTS_DIR "/testdata/db/data.db", |
| 256 |
dns_masterformat_text, 0); |
| 257 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 258 |
|
| 259 |
assert_int_equal(dns_db_class(db), dns_rdataclass_in); |
| 260 |
|
| 261 |
dns_db_detach(&db); |
| 262 |
isc_loopmgr_shutdown(loopmgr); |
| 263 |
} |
| 264 |
|
| 265 |
/* database type */ |
| 266 |
ISC_LOOP_TEST_IMPL(dbtype) { |
| 267 |
isc_result_t result; |
| 268 |
dns_db_t *db = NULL; |
| 269 |
|
| 270 |
/* DB has zone semantics */ |
| 271 |
result = dns_db_create(mctx, ZONEDB_DEFAULT, dns_rootname, |
| 272 |
dns_dbtype_zone, dns_rdataclass_in, 0, NULL, |
| 273 |
&db); |
| 274 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 275 |
result = dns_db_load(db, TESTS_DIR "/testdata/db/data.db", |
| 276 |
dns_masterformat_text, 0); |
| 277 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 278 |
assert_true(dns_db_iszone(db)); |
| 279 |
assert_false(dns_db_iscache(db)); |
| 280 |
dns_db_detach(&db); |
| 281 |
|
| 282 |
/* DB has cache semantics */ |
| 283 |
result = dns_db_create(mctx, CACHEDB_DEFAULT, dns_rootname, |
| 284 |
dns_dbtype_cache, dns_rdataclass_in, 0, NULL, |
| 285 |
&db); |
| 286 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 287 |
assert_true(dns_db_iscache(db)); |
| 288 |
assert_false(dns_db_iszone(db)); |
| 289 |
dns_db_detach(&db); |
| 290 |
isc_loopmgr_shutdown(loopmgr); |
| 291 |
} |
| 292 |
|
| 293 |
/* database versions */ |
| 294 |
ISC_LOOP_TEST_IMPL(version) { |
| 295 |
isc_result_t result; |
| 296 |
dns_fixedname_t fname, ffound; |
| 297 |
dns_name_t *name, *foundname; |
| 298 |
dns_db_t *db = NULL; |
| 299 |
dns_dbversion_t *ver = NULL, *new = NULL; |
| 300 |
dns_dbnode_t *node = NULL; |
| 301 |
dns_rdataset_t rdataset; |
| 302 |
|
| 303 |
result = dns_test_loaddb(&db, dns_dbtype_zone, "test.test", |
| 304 |
TESTS_DIR "/testdata/db/data.db"); |
| 305 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 306 |
|
| 307 |
/* Open current version for reading */ |
| 308 |
dns_db_currentversion(db, &ver); |
| 309 |
dns_test_namefromstring("b.test.test.", &fname); |
| 310 |
name = dns_fixedname_name(&fname); |
| 311 |
foundname = dns_fixedname_initname(&ffound); |
| 312 |
dns_rdataset_init(&rdataset); |
| 313 |
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node, |
| 314 |
foundname, &rdataset, NULL); |
| 315 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 316 |
dns_rdataset_disassociate(&rdataset); |
| 317 |
dns_db_detachnode(db, &node); |
| 318 |
dns_db_closeversion(db, &ver, false); |
| 319 |
|
| 320 |
/* Open new version for writing */ |
| 321 |
dns_db_currentversion(db, &ver); |
| 322 |
dns_test_namefromstring("b.test.test.", &fname); |
| 323 |
name = dns_fixedname_name(&fname); |
| 324 |
foundname = dns_fixedname_initname(&ffound); |
| 325 |
dns_rdataset_init(&rdataset); |
| 326 |
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node, |
| 327 |
foundname, &rdataset, NULL); |
| 328 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 329 |
|
| 330 |
result = dns_db_newversion(db, &new); |
| 331 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 332 |
|
| 333 |
/* Delete the rdataset from the new version */ |
| 334 |
result = dns_db_deleterdataset(db, node, new, dns_rdatatype_a, 0); |
| 335 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 336 |
|
| 337 |
dns_rdataset_disassociate(&rdataset); |
| 338 |
dns_db_detachnode(db, &node); |
| 339 |
|
| 340 |
/* This should fail now */ |
| 341 |
result = dns_db_find(db, name, new, dns_rdatatype_a, 0, 0, &node, |
| 342 |
foundname, &rdataset, NULL); |
| 343 |
assert_int_equal(result, DNS_R_NXDOMAIN); |
| 344 |
|
| 345 |
dns_db_closeversion(db, &new, true); |
| 346 |
|
| 347 |
/* But this should still succeed */ |
| 348 |
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node, |
| 349 |
foundname, &rdataset, NULL); |
| 350 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 351 |
dns_rdataset_disassociate(&rdataset); |
| 352 |
dns_db_detachnode(db, &node); |
| 353 |
dns_db_closeversion(db, &ver, false); |
| 354 |
|
| 355 |
dns_db_detach(&db); |
| 356 |
isc_loopmgr_shutdown(loopmgr); |
| 357 |
} |
| 358 |
|
| 359 |
ISC_TEST_LIST_START |
| 360 |
ISC_TEST_ENTRY_CUSTOM(getoriginnode, setup_managers, teardown_managers) |
| 361 |
ISC_TEST_ENTRY_CUSTOM(getsetservestalettl, setup_managers, teardown_managers) |
| 362 |
ISC_TEST_ENTRY_CUSTOM(dns_dbfind_staleok, setup_managers, teardown_managers) |
| 363 |
ISC_TEST_ENTRY_CUSTOM(class, setup_managers, teardown_managers) |
| 364 |
ISC_TEST_ENTRY_CUSTOM(dbtype, setup_managers, teardown_managers) |
| 365 |
ISC_TEST_ENTRY_CUSTOM(version, setup_managers, teardown_managers) |
| 366 |
ISC_TEST_LIST_END |
| 367 |
|
| 368 |
ISC_TEST_MAIN |