| 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 |
/*! \file */ |
| 15 |
|
| 16 |
#include <inttypes.h> |
| 17 |
#include <sched.h> /* IWYU pragma: keep */ |
| 18 |
#include <setjmp.h> |
| 19 |
#include <stdarg.h> |
| 20 |
#include <stdbool.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <time.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include <isc/buffer.h> |
| 28 |
#include <isc/file.h> |
| 29 |
#include <isc/hash.h> |
| 30 |
#include <isc/hex.h> |
| 31 |
#include <isc/lex.h> |
| 32 |
#include <isc/managers.h> |
| 33 |
#include <isc/mem.h> |
| 34 |
#include <isc/netmgr.h> |
| 35 |
#include <isc/os.h> |
| 36 |
#include <isc/random.h> |
| 37 |
#include <isc/result.h> |
| 38 |
#include <isc/stdio.h> |
| 39 |
#include <isc/string.h> |
| 40 |
#include <isc/timer.h> |
| 41 |
#include <isc/util.h> |
| 42 |
|
| 43 |
#include <dns/callbacks.h> |
| 44 |
#include <dns/db.h> |
| 45 |
#include <dns/dispatch.h> |
| 46 |
#include <dns/fixedname.h> |
| 47 |
#include <dns/log.h> |
| 48 |
#include <dns/name.h> |
| 49 |
#include <dns/view.h> |
| 50 |
#include <dns/zone.h> |
| 51 |
|
| 52 |
#include <tests/dns.h> |
| 53 |
|
| 54 |
dns_zonemgr_t *zonemgr = NULL; |
| 55 |
|
| 56 |
/* |
| 57 |
* Create a view. |
| 58 |
*/ |
| 59 |
isc_result_t |
| 60 |
dns_test_makeview(const char *name, bool with_dispatchmgr, bool with_cache, |
| 61 |
dns_view_t **viewp) { |
| 62 |
isc_result_t result; |
| 63 |
dns_view_t *view = NULL; |
| 64 |
dns_cache_t *cache = NULL; |
| 65 |
dns_dispatchmgr_t *dispatchmgr = NULL; |
| 66 |
|
| 67 |
if (with_dispatchmgr) { |
| 68 |
result = dns_dispatchmgr_create(mctx, loopmgr, netmgr, |
| 69 |
&dispatchmgr); |
| 70 |
if (result != ISC_R_SUCCESS) { |
| 71 |
return result; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
result = dns_view_create(mctx, loopmgr, dispatchmgr, dns_rdataclass_in, |
| 76 |
name, &view); |
| 77 |
|
| 78 |
if (dispatchmgr != NULL) { |
| 79 |
dns_dispatchmgr_detach(&dispatchmgr); |
| 80 |
} |
| 81 |
|
| 82 |
if (result != ISC_R_SUCCESS) { |
| 83 |
return result; |
| 84 |
} |
| 85 |
|
| 86 |
if (with_cache) { |
| 87 |
result = dns_cache_create(loopmgr, dns_rdataclass_in, "", mctx, |
| 88 |
&cache); |
| 89 |
if (result != ISC_R_SUCCESS) { |
| 90 |
dns_view_detach(&view); |
| 91 |
return result; |
| 92 |
} |
| 93 |
|
| 94 |
dns_view_setcache(view, cache, false); |
| 95 |
/* |
| 96 |
* Reference count for "cache" is now at 2, so decrement it in |
| 97 |
* order for the cache to be automatically freed when "view" |
| 98 |
* gets freed. |
| 99 |
*/ |
| 100 |
dns_cache_detach(&cache); |
| 101 |
} |
| 102 |
|
| 103 |
*viewp = view; |
| 104 |
|
| 105 |
return ISC_R_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
isc_result_t |
| 109 |
dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view, |
| 110 |
bool createview) { |
| 111 |
dns_fixedname_t fixed_origin; |
| 112 |
dns_zone_t *zone = NULL; |
| 113 |
isc_result_t result; |
| 114 |
dns_name_t *origin; |
| 115 |
|
| 116 |
REQUIRE(view == NULL || !createview); |
| 117 |
|
| 118 |
/* |
| 119 |
* Create the zone structure. |
| 120 |
*/ |
| 121 |
dns_zone_create(&zone, mctx, 0); |
| 122 |
|
| 123 |
/* |
| 124 |
* Set zone type and origin. |
| 125 |
*/ |
| 126 |
dns_zone_settype(zone, dns_zone_primary); |
| 127 |
origin = dns_fixedname_initname(&fixed_origin); |
| 128 |
result = dns_name_fromstring(origin, name, dns_rootname, 0, NULL); |
| 129 |
if (result != ISC_R_SUCCESS) { |
| 130 |
goto detach_zone; |
| 131 |
} |
| 132 |
result = dns_zone_setorigin(zone, origin); |
| 133 |
if (result != ISC_R_SUCCESS) { |
| 134 |
goto detach_zone; |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* If requested, create a view. |
| 139 |
*/ |
| 140 |
if (createview) { |
| 141 |
result = dns_test_makeview("view", false, false, &view); |
| 142 |
if (result != ISC_R_SUCCESS) { |
| 143 |
goto detach_zone; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/* |
| 148 |
* If a view was passed as an argument or created above, attach the |
| 149 |
* created zone to it. Otherwise, set the zone's class to IN. |
| 150 |
*/ |
| 151 |
if (view != NULL) { |
| 152 |
dns_zone_setview(zone, view); |
| 153 |
dns_zone_setclass(zone, view->rdclass); |
| 154 |
dns_view_addzone(view, zone); |
| 155 |
} else { |
| 156 |
dns_zone_setclass(zone, dns_rdataclass_in); |
| 157 |
} |
| 158 |
|
| 159 |
*zonep = zone; |
| 160 |
|
| 161 |
return ISC_R_SUCCESS; |
| 162 |
|
| 163 |
detach_zone: |
| 164 |
dns_zone_detach(&zone); |
| 165 |
|
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
void |
| 170 |
dns_test_setupzonemgr(void) { |
| 171 |
REQUIRE(zonemgr == NULL); |
| 172 |
|
| 173 |
dns_zonemgr_create(mctx, netmgr, &zonemgr); |
| 174 |
} |
| 175 |
|
| 176 |
isc_result_t |
| 177 |
dns_test_managezone(dns_zone_t *zone) { |
| 178 |
isc_result_t result; |
| 179 |
REQUIRE(zonemgr != NULL); |
| 180 |
|
| 181 |
result = dns_zonemgr_managezone(zonemgr, zone); |
| 182 |
return result; |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
dns_test_releasezone(dns_zone_t *zone) { |
| 187 |
REQUIRE(zonemgr != NULL); |
| 188 |
dns_zonemgr_releasezone(zonemgr, zone); |
| 189 |
} |
| 190 |
|
| 191 |
void |
| 192 |
dns_test_closezonemgr(void) { |
| 193 |
REQUIRE(zonemgr != NULL); |
| 194 |
|
| 195 |
dns_zonemgr_shutdown(zonemgr); |
| 196 |
dns_zonemgr_detach(&zonemgr); |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* Sleep for 'usec' microseconds. |
| 201 |
*/ |
| 202 |
void |
| 203 |
dns_test_nap(uint32_t usec) { |
| 204 |
struct timespec ts; |
| 205 |
|
| 206 |
ts.tv_sec = usec / (long)US_PER_SEC; |
| 207 |
ts.tv_nsec = (usec % (long)US_PER_SEC) * (long)NS_PER_US; |
| 208 |
nanosleep(&ts, NULL); |
| 209 |
} |
| 210 |
|
| 211 |
isc_result_t |
| 212 |
dns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin, |
| 213 |
const char *testfile) { |
| 214 |
isc_result_t result; |
| 215 |
dns_fixedname_t fixed; |
| 216 |
dns_name_t *name = NULL; |
| 217 |
const char *dbimp = (dbtype == dns_dbtype_zone) ? ZONEDB_DEFAULT |
| 218 |
: CACHEDB_DEFAULT; |
| 219 |
|
| 220 |
name = dns_fixedname_initname(&fixed); |
| 221 |
|
| 222 |
result = dns_name_fromstring(name, origin, dns_rootname, 0, NULL); |
| 223 |
if (result != ISC_R_SUCCESS) { |
| 224 |
return result; |
| 225 |
} |
| 226 |
|
| 227 |
result = dns_db_create(mctx, dbimp, name, dbtype, dns_rdataclass_in, 0, |
| 228 |
NULL, db); |
| 229 |
if (result != ISC_R_SUCCESS) { |
| 230 |
return result; |
| 231 |
} |
| 232 |
|
| 233 |
result = dns_db_load(*db, testfile, dns_masterformat_text, 0); |
| 234 |
return result; |
| 235 |
} |
| 236 |
|
| 237 |
static int |
| 238 |
fromhex(char c) { |
| 239 |
if (c >= '0' && c <= '9') { |
| 240 |
return c - '0'; |
| 241 |
} else if (c >= 'a' && c <= 'f') { |
| 242 |
return c - 'a' + 10; |
| 243 |
} else if (c >= 'A' && c <= 'F') { |
| 244 |
return c - 'A' + 10; |
| 245 |
} |
| 246 |
|
| 247 |
printf("bad input format: %02x\n", c); |
| 248 |
exit(3); |
| 249 |
} |
| 250 |
|
| 251 |
/* |
| 252 |
* Format contents of given memory region as a hex string, using the buffer |
| 253 |
* of length 'buflen' pointed to by 'buf'. 'buflen' must be at least three |
| 254 |
* times 'len'. Always returns 'buf'. |
| 255 |
*/ |
| 256 |
char * |
| 257 |
dns_test_tohex(const unsigned char *data, size_t len, char *buf, |
| 258 |
size_t buflen) { |
| 259 |
isc_constregion_t source = { .base = data, .length = len }; |
| 260 |
isc_buffer_t target; |
| 261 |
isc_result_t result; |
| 262 |
|
| 263 |
memset(buf, 0, buflen); |
| 264 |
isc_buffer_init(&target, buf, buflen); |
| 265 |
result = isc_hex_totext((isc_region_t *)&source, 1, " ", &target); |
| 266 |
INSIST(result == ISC_R_SUCCESS); |
| 267 |
|
| 268 |
return buf; |
| 269 |
} |
| 270 |
|
| 271 |
isc_result_t |
| 272 |
dns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz, |
| 273 |
size_t *sizep) { |
| 274 |
isc_result_t result; |
| 275 |
unsigned char *bp; |
| 276 |
char *rp, *wp; |
| 277 |
char s[BUFSIZ]; |
| 278 |
size_t len, i; |
| 279 |
FILE *f = NULL; |
| 280 |
int n; |
| 281 |
|
| 282 |
result = isc_stdio_open(file, "r", &f); |
| 283 |
if (result != ISC_R_SUCCESS) { |
| 284 |
return result; |
| 285 |
} |
| 286 |
|
| 287 |
bp = buf; |
| 288 |
while (fgets(s, sizeof(s), f) != NULL) { |
| 289 |
rp = s; |
| 290 |
wp = s; |
| 291 |
len = 0; |
| 292 |
while (*rp != '\0') { |
| 293 |
if (*rp == '#') { |
| 294 |
break; |
| 295 |
} |
| 296 |
if (*rp != ' ' && *rp != '\t' && *rp != '\r' && |
| 297 |
*rp != '\n') |
| 298 |
{ |
| 299 |
*wp++ = *rp; |
| 300 |
len++; |
| 301 |
} |
| 302 |
rp++; |
| 303 |
} |
| 304 |
if (len == 0U) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
if (len % 2 != 0U) { |
| 308 |
result = ISC_R_UNEXPECTEDEND; |
| 309 |
break; |
| 310 |
} |
| 311 |
if (len > bufsiz * 2) { |
| 312 |
result = ISC_R_NOSPACE; |
| 313 |
break; |
| 314 |
} |
| 315 |
rp = s; |
| 316 |
for (i = 0; i < len; i += 2) { |
| 317 |
n = fromhex(*rp++); |
| 318 |
n *= 16; |
| 319 |
n += fromhex(*rp++); |
| 320 |
*bp++ = n; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
if (result == ISC_R_SUCCESS) { |
| 325 |
*sizep = bp - buf; |
| 326 |
} |
| 327 |
|
| 328 |
isc_stdio_close(f); |
| 329 |
return result; |
| 330 |
} |
| 331 |
|
| 332 |
static void |
| 333 |
nullmsg(dns_rdatacallbacks_t *cb, const char *fmt, ...) { |
| 334 |
UNUSED(cb); |
| 335 |
UNUSED(fmt); |
| 336 |
} |
| 337 |
|
| 338 |
isc_result_t |
| 339 |
dns_test_rdatafromstring(dns_rdata_t *rdata, dns_rdataclass_t rdclass, |
| 340 |
dns_rdatatype_t rdtype, unsigned char *dst, |
| 341 |
size_t dstlen, const char *src, bool warnings) { |
| 342 |
dns_rdatacallbacks_t callbacks; |
| 343 |
isc_buffer_t source, target; |
| 344 |
isc_lex_t *lex = NULL; |
| 345 |
isc_lexspecials_t specials = { 0 }; |
| 346 |
isc_result_t result; |
| 347 |
size_t length; |
| 348 |
|
| 349 |
REQUIRE(rdata != NULL); |
| 350 |
REQUIRE(DNS_RDATA_INITIALIZED(rdata)); |
| 351 |
REQUIRE(dst != NULL); |
| 352 |
REQUIRE(src != NULL); |
| 353 |
|
| 354 |
/* |
| 355 |
* Set up source to hold the input string. |
| 356 |
*/ |
| 357 |
length = strlen(src); |
| 358 |
isc_buffer_constinit(&source, src, length); |
| 359 |
isc_buffer_add(&source, length); |
| 360 |
|
| 361 |
/* |
| 362 |
* Create a lexer as one is required by dns_rdata_fromtext(). |
| 363 |
*/ |
| 364 |
isc_lex_create(mctx, 64, &lex); |
| 365 |
|
| 366 |
/* |
| 367 |
* Set characters which will be treated as valid multi-line RDATA |
| 368 |
* delimiters while reading the source string. These should match |
| 369 |
* specials from lib/dns/master.c. |
| 370 |
*/ |
| 371 |
specials[0] = 1; |
| 372 |
specials['('] = 1; |
| 373 |
specials[')'] = 1; |
| 374 |
specials['"'] = 1; |
| 375 |
isc_lex_setspecials(lex, specials); |
| 376 |
|
| 377 |
/* |
| 378 |
* Expect DNS masterfile comments. |
| 379 |
*/ |
| 380 |
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE); |
| 381 |
|
| 382 |
/* |
| 383 |
* Point lexer at source. |
| 384 |
*/ |
| 385 |
result = isc_lex_openbuffer(lex, &source); |
| 386 |
if (result != ISC_R_SUCCESS) { |
| 387 |
goto destroy_lexer; |
| 388 |
} |
| 389 |
|
| 390 |
/* |
| 391 |
* Set up target for storing uncompressed wire form of provided RDATA. |
| 392 |
*/ |
| 393 |
isc_buffer_init(&target, dst, dstlen); |
| 394 |
|
| 395 |
/* |
| 396 |
* Set up callbacks so warnings and errors are not printed. |
| 397 |
*/ |
| 398 |
if (!warnings) { |
| 399 |
dns_rdatacallbacks_init(&callbacks); |
| 400 |
callbacks.warn = callbacks.error = nullmsg; |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* Parse input string, determining result. |
| 405 |
*/ |
| 406 |
result = dns_rdata_fromtext(rdata, rdclass, rdtype, lex, dns_rootname, |
| 407 |
0, mctx, &target, &callbacks); |
| 408 |
|
| 409 |
destroy_lexer: |
| 410 |
isc_lex_destroy(&lex); |
| 411 |
|
| 412 |
return result; |
| 413 |
} |
| 414 |
|
| 415 |
void |
| 416 |
dns_test_namefromstring(const char *namestr, dns_fixedname_t *fname) { |
| 417 |
size_t length; |
| 418 |
isc_buffer_t *b = NULL; |
| 419 |
isc_result_t result; |
| 420 |
dns_name_t *name; |
| 421 |
|
| 422 |
length = strlen(namestr); |
| 423 |
|
| 424 |
name = dns_fixedname_initname(fname); |
| 425 |
|
| 426 |
isc_buffer_allocate(mctx, &b, length); |
| 427 |
|
| 428 |
isc_buffer_putmem(b, (const unsigned char *)namestr, length); |
| 429 |
result = dns_name_fromtext(name, b, NULL, 0, NULL); |
| 430 |
INSIST(result == ISC_R_SUCCESS); |
| 431 |
|
| 432 |
isc_buffer_free(&b); |
| 433 |
} |
| 434 |
|
| 435 |
isc_result_t |
| 436 |
dns_test_difffromchanges(dns_diff_t *diff, const zonechange_t *changes, |
| 437 |
bool warnings) { |
| 438 |
isc_result_t result = ISC_R_SUCCESS; |
| 439 |
unsigned char rdata_buf[1024]; |
| 440 |
dns_difftuple_t *tuple = NULL; |
| 441 |
isc_consttextregion_t region; |
| 442 |
dns_rdatatype_t rdatatype; |
| 443 |
dns_fixedname_t fixedname; |
| 444 |
dns_rdata_t rdata; |
| 445 |
dns_name_t *name; |
| 446 |
size_t i; |
| 447 |
|
| 448 |
REQUIRE(diff != NULL); |
| 449 |
REQUIRE(changes != NULL); |
| 450 |
|
| 451 |
dns_diff_init(mctx, diff); |
| 452 |
|
| 453 |
for (i = 0; changes[i].owner != NULL; i++) { |
| 454 |
/* |
| 455 |
* Parse owner name. |
| 456 |
*/ |
| 457 |
name = dns_fixedname_initname(&fixedname); |
| 458 |
result = dns_name_fromstring(name, changes[i].owner, |
| 459 |
dns_rootname, 0, mctx); |
| 460 |
if (result != ISC_R_SUCCESS) { |
| 461 |
break; |
| 462 |
} |
| 463 |
|
| 464 |
/* |
| 465 |
* Parse RDATA type. |
| 466 |
*/ |
| 467 |
region.base = changes[i].type; |
| 468 |
region.length = strlen(changes[i].type); |
| 469 |
result = dns_rdatatype_fromtext(&rdatatype, |
| 470 |
(isc_textregion_t *)®ion); |
| 471 |
if (result != ISC_R_SUCCESS) { |
| 472 |
break; |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
* Parse RDATA. |
| 477 |
*/ |
| 478 |
dns_rdata_init(&rdata); |
| 479 |
result = dns_test_rdatafromstring( |
| 480 |
&rdata, dns_rdataclass_in, rdatatype, rdata_buf, |
| 481 |
sizeof(rdata_buf), changes[i].rdata, warnings); |
| 482 |
if (result != ISC_R_SUCCESS) { |
| 483 |
break; |
| 484 |
} |
| 485 |
|
| 486 |
/* |
| 487 |
* Create a diff tuple for the parsed change and append it to |
| 488 |
* the diff. |
| 489 |
*/ |
| 490 |
result = dns_difftuple_create(mctx, changes[i].op, name, |
| 491 |
changes[i].ttl, &rdata, &tuple); |
| 492 |
if (result != ISC_R_SUCCESS) { |
| 493 |
break; |
| 494 |
} |
| 495 |
dns_diff_append(diff, &tuple); |
| 496 |
} |
| 497 |
|
| 498 |
if (result != ISC_R_SUCCESS) { |
| 499 |
dns_diff_clear(diff); |
| 500 |
} |
| 501 |
|
| 502 |
return result; |
| 503 |
} |