| 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/list.h> |
| 28 |
#include <isc/region.h> |
| 29 |
#include <isc/result.h> |
| 30 |
#include <isc/stdtime.h> |
| 31 |
#include <isc/types.h> |
| 32 |
#include <isc/util.h> |
| 33 |
|
| 34 |
#include <dns/db.h> |
| 35 |
#include <dns/diff.h> |
| 36 |
#include <dns/dnssec.h> |
| 37 |
#include <dns/fixedname.h> |
| 38 |
#include <dns/name.h> |
| 39 |
#include <dns/rdata.h> |
| 40 |
#include <dns/rdatastruct.h> |
| 41 |
#include <dns/rdatatype.h> |
| 42 |
#include <dns/types.h> |
| 43 |
#include <dns/zone.h> |
| 44 |
|
| 45 |
#include <dst/dst.h> |
| 46 |
|
| 47 |
#include "zone_p.h" |
| 48 |
|
| 49 |
#include <tests/dns.h> |
| 50 |
|
| 51 |
/*% |
| 52 |
* Structure characterizing a single diff tuple in the dns_diff_t structure |
| 53 |
* prepared by dns__zone_updatesigs(). |
| 54 |
*/ |
| 55 |
typedef struct { |
| 56 |
dns_diffop_t op; |
| 57 |
const char *owner; |
| 58 |
dns_ttl_t ttl; |
| 59 |
const char *type; |
| 60 |
} zonediff_t; |
| 61 |
|
| 62 |
#define ZONEDIFF_SENTINEL { 0, NULL, 0, NULL } |
| 63 |
|
| 64 |
/*% |
| 65 |
* Structure defining a dns__zone_updatesigs() test. |
| 66 |
*/ |
| 67 |
typedef struct { |
| 68 |
const char *description; /* test description */ |
| 69 |
const zonechange_t *changes; /* array of "raw" zone changes */ |
| 70 |
const zonediff_t *zonediff; /* array of "processed" zone changes |
| 71 |
* */ |
| 72 |
} updatesigs_test_params_t; |
| 73 |
|
| 74 |
static int |
| 75 |
setup_test(void **state) { |
| 76 |
isc_result_t result; |
| 77 |
|
| 78 |
UNUSED(state); |
| 79 |
|
| 80 |
result = dst_lib_init(mctx, NULL); |
| 81 |
|
| 82 |
if (result != ISC_R_SUCCESS) { |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int |
| 90 |
teardown_test(void **state) { |
| 91 |
UNUSED(state); |
| 92 |
|
| 93 |
dst_lib_destroy(); |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
/*% |
| 99 |
* Check whether the 'found' tuple matches the 'expected' tuple. 'found' is |
| 100 |
* the 'index'th tuple output by dns__zone_updatesigs() in test 'test'. |
| 101 |
*/ |
| 102 |
static void |
| 103 |
compare_tuples(const zonediff_t *expected, dns_difftuple_t *found, |
| 104 |
size_t index) { |
| 105 |
char found_covers[DNS_RDATATYPE_FORMATSIZE] = {}; |
| 106 |
char found_type[DNS_RDATATYPE_FORMATSIZE] = {}; |
| 107 |
char found_name[DNS_NAME_FORMATSIZE]; |
| 108 |
isc_consttextregion_t typeregion; |
| 109 |
dns_fixedname_t expected_fname; |
| 110 |
dns_rdatatype_t expected_type; |
| 111 |
dns_name_t *expected_name; |
| 112 |
dns_rdata_rrsig_t rrsig; |
| 113 |
isc_buffer_t typebuf; |
| 114 |
isc_result_t result; |
| 115 |
|
| 116 |
REQUIRE(expected != NULL); |
| 117 |
REQUIRE(found != NULL); |
| 118 |
REQUIRE(index > 0); |
| 119 |
|
| 120 |
/* |
| 121 |
* Check operation. |
| 122 |
*/ |
| 123 |
assert_int_equal(expected->op, found->op); |
| 124 |
|
| 125 |
/* |
| 126 |
* Check owner name. |
| 127 |
*/ |
| 128 |
expected_name = dns_fixedname_initname(&expected_fname); |
| 129 |
result = dns_name_fromstring(expected_name, expected->owner, |
| 130 |
dns_rootname, 0, mctx); |
| 131 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 132 |
dns_name_format(&found->name, found_name, sizeof(found_name)); |
| 133 |
assert_true(dns_name_equal(expected_name, &found->name)); |
| 134 |
|
| 135 |
/* |
| 136 |
* Check TTL. |
| 137 |
*/ |
| 138 |
assert_int_equal(expected->ttl, found->ttl); |
| 139 |
|
| 140 |
/* |
| 141 |
* Parse expected RR type. |
| 142 |
*/ |
| 143 |
typeregion.base = expected->type; |
| 144 |
typeregion.length = strlen(expected->type); |
| 145 |
result = dns_rdatatype_fromtext(&expected_type, |
| 146 |
(isc_textregion_t *)&typeregion); |
| 147 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 148 |
|
| 149 |
/* |
| 150 |
* Format found RR type for reporting purposes. |
| 151 |
*/ |
| 152 |
isc_buffer_init(&typebuf, found_type, sizeof(found_type)); |
| 153 |
result = dns_rdatatype_totext(found->rdata.type, &typebuf); |
| 154 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 155 |
|
| 156 |
/* |
| 157 |
* Check RR type. |
| 158 |
*/ |
| 159 |
switch (expected->op) { |
| 160 |
case DNS_DIFFOP_ADDRESIGN: |
| 161 |
case DNS_DIFFOP_DELRESIGN: |
| 162 |
/* |
| 163 |
* Found tuple must be of type RRSIG. |
| 164 |
*/ |
| 165 |
assert_int_equal(found->rdata.type, dns_rdatatype_rrsig); |
| 166 |
if (found->rdata.type != dns_rdatatype_rrsig) { |
| 167 |
break; |
| 168 |
} |
| 169 |
/* |
| 170 |
* The signature must cover an RRset of type 'expected->type'. |
| 171 |
*/ |
| 172 |
result = dns_rdata_tostruct(&found->rdata, &rrsig, NULL); |
| 173 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 174 |
isc_buffer_init(&typebuf, found_covers, sizeof(found_covers)); |
| 175 |
result = dns_rdatatype_totext(rrsig.covered, &typebuf); |
| 176 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 177 |
assert_int_equal(expected_type, rrsig.covered); |
| 178 |
break; |
| 179 |
default: |
| 180 |
/* |
| 181 |
* Found tuple must be of type 'expected->type'. |
| 182 |
*/ |
| 183 |
assert_int_equal(expected_type, found->rdata.type); |
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/*% |
| 189 |
* Perform a single dns__zone_updatesigs() test defined in 'test'. All other |
| 190 |
* arguments are expected to remain constant between subsequent invocations of |
| 191 |
* this function. |
| 192 |
*/ |
| 193 |
static void |
| 194 |
updatesigs_test(const updatesigs_test_params_t *test, dns_zone_t *zone, |
| 195 |
dns_db_t *db, dst_key_t *zone_keys[], unsigned int nkeys, |
| 196 |
isc_stdtime_t now) { |
| 197 |
size_t tuples_expected, tuples_found, index; |
| 198 |
dns_dbversion_t *version = NULL; |
| 199 |
dns_diff_t raw_diff, zone_diff; |
| 200 |
const zonediff_t *expected; |
| 201 |
dns_difftuple_t *found; |
| 202 |
isc_result_t result; |
| 203 |
|
| 204 |
dns__zonediff_t zonediff = { |
| 205 |
.diff = &zone_diff, |
| 206 |
.offline = false, |
| 207 |
}; |
| 208 |
|
| 209 |
REQUIRE(test != NULL); |
| 210 |
REQUIRE(test->description != NULL); |
| 211 |
REQUIRE(test->changes != NULL); |
| 212 |
REQUIRE(zone != NULL); |
| 213 |
REQUIRE(db != NULL); |
| 214 |
REQUIRE(zone_keys != NULL); |
| 215 |
|
| 216 |
/* |
| 217 |
* Create a new version of the zone's database. |
| 218 |
*/ |
| 219 |
result = dns_db_newversion(db, &version); |
| 220 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 221 |
|
| 222 |
/* |
| 223 |
* Create a diff representing the supplied changes. |
| 224 |
*/ |
| 225 |
result = dns_test_difffromchanges(&raw_diff, test->changes, false); |
| 226 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 227 |
|
| 228 |
/* |
| 229 |
* Apply the "raw" diff to the new version of the zone's database as |
| 230 |
* this is what dns__zone_updatesigs() expects to happen before it is |
| 231 |
* called. |
| 232 |
*/ |
| 233 |
dns_diff_apply(&raw_diff, db, version); |
| 234 |
|
| 235 |
/* |
| 236 |
* Initialize the structure dns__zone_updatesigs() will modify. |
| 237 |
*/ |
| 238 |
dns_diff_init(mctx, &zone_diff); |
| 239 |
|
| 240 |
/* |
| 241 |
* Check whether dns__zone_updatesigs() behaves as expected. |
| 242 |
*/ |
| 243 |
result = dns__zone_updatesigs(&raw_diff, db, version, zone_keys, nkeys, |
| 244 |
zone, now - 3600, now + 3600, 0, now, |
| 245 |
&zonediff); |
| 246 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 247 |
assert_true(ISC_LIST_EMPTY(raw_diff.tuples)); |
| 248 |
assert_false(ISC_LIST_EMPTY(zone_diff.tuples)); |
| 249 |
|
| 250 |
/* |
| 251 |
* Ensure that the number of tuples in the zone diff is as expected. |
| 252 |
*/ |
| 253 |
|
| 254 |
tuples_expected = 0; |
| 255 |
for (expected = test->zonediff; expected->owner != NULL; expected++) { |
| 256 |
tuples_expected++; |
| 257 |
} |
| 258 |
|
| 259 |
tuples_found = 0; |
| 260 |
for (found = ISC_LIST_HEAD(zone_diff.tuples); found != NULL; |
| 261 |
found = ISC_LIST_NEXT(found, link)) |
| 262 |
{ |
| 263 |
tuples_found++; |
| 264 |
} |
| 265 |
|
| 266 |
assert_int_equal(tuples_expected, tuples_found); |
| 267 |
|
| 268 |
/* |
| 269 |
* Ensure that every tuple in the zone diff matches expectations. |
| 270 |
*/ |
| 271 |
expected = test->zonediff; |
| 272 |
index = 1; |
| 273 |
for (found = ISC_LIST_HEAD(zone_diff.tuples); found != NULL; |
| 274 |
found = ISC_LIST_NEXT(found, link)) |
| 275 |
{ |
| 276 |
compare_tuples(expected, found, index); |
| 277 |
expected++; |
| 278 |
index++; |
| 279 |
} |
| 280 |
|
| 281 |
/* |
| 282 |
* Apply changes to zone database contents and clean up. |
| 283 |
*/ |
| 284 |
dns_db_closeversion(db, &version, true); |
| 285 |
dns_diff_clear(&zone_diff); |
| 286 |
dns_diff_clear(&raw_diff); |
| 287 |
} |
| 288 |
|
| 289 |
/* dns__zone_updatesigs() tests */ |
| 290 |
ISC_RUN_TEST_IMPL(updatesigs_next) { |
| 291 |
dst_key_t *zone_keys[DNS_MAXZONEKEYS]; |
| 292 |
dns_zone_t *zone = NULL; |
| 293 |
dns_db_t *db = NULL; |
| 294 |
isc_result_t result; |
| 295 |
unsigned int nkeys; |
| 296 |
isc_stdtime_t now = isc_stdtime_now(); |
| 297 |
size_t i; |
| 298 |
|
| 299 |
UNUSED(state); |
| 300 |
|
| 301 |
/* |
| 302 |
* Prepare a zone along with its signing keys. |
| 303 |
*/ |
| 304 |
|
| 305 |
result = dns_test_makezone("example", &zone, NULL, false); |
| 306 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 307 |
|
| 308 |
result = dns_test_loaddb(&db, dns_dbtype_zone, "example", |
| 309 |
"testdata/master/master18.data"); |
| 310 |
assert_int_equal(result, DNS_R_SEENINCLUDE); |
| 311 |
|
| 312 |
result = dns_zone_setkeydirectory(zone, TESTS_DIR "/testkeys"); |
| 313 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 314 |
|
| 315 |
result = dns_zone_findkeys(zone, db, NULL, now, mctx, DNS_MAXZONEKEYS, |
| 316 |
zone_keys, &nkeys); |
| 317 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 318 |
assert_int_equal(nkeys, 2); |
| 319 |
|
| 320 |
/* |
| 321 |
* Define the tests to be run. Note that changes to zone database |
| 322 |
* contents introduced by each test are preserved between tests. |
| 323 |
*/ |
| 324 |
|
| 325 |
const zonechange_t changes_add[] = { |
| 326 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT", "foo" }, |
| 327 |
{ DNS_DIFFOP_ADD, "bar.example", 600, "TXT", "bar" }, |
| 328 |
ZONECHANGE_SENTINEL, |
| 329 |
}; |
| 330 |
const zonediff_t zonediff_add[] = { |
| 331 |
{ DNS_DIFFOP_ADDRESIGN, "foo.example", 300, "TXT" }, |
| 332 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT" }, |
| 333 |
{ DNS_DIFFOP_ADDRESIGN, "bar.example", 600, "TXT" }, |
| 334 |
{ DNS_DIFFOP_ADD, "bar.example", 600, "TXT" }, |
| 335 |
ZONEDIFF_SENTINEL, |
| 336 |
}; |
| 337 |
const updatesigs_test_params_t test_add = { |
| 338 |
.description = "add new RRsets", |
| 339 |
.changes = changes_add, |
| 340 |
.zonediff = zonediff_add, |
| 341 |
}; |
| 342 |
|
| 343 |
const zonechange_t changes_append[] = { |
| 344 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT", "foo1" }, |
| 345 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT", "foo2" }, |
| 346 |
ZONECHANGE_SENTINEL, |
| 347 |
}; |
| 348 |
const zonediff_t zonediff_append[] = { |
| 349 |
{ DNS_DIFFOP_DELRESIGN, "foo.example", 300, "TXT" }, |
| 350 |
{ DNS_DIFFOP_ADDRESIGN, "foo.example", 300, "TXT" }, |
| 351 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT" }, |
| 352 |
{ DNS_DIFFOP_ADD, "foo.example", 300, "TXT" }, |
| 353 |
ZONEDIFF_SENTINEL, |
| 354 |
}; |
| 355 |
const updatesigs_test_params_t test_append = { |
| 356 |
.description = "append multiple RRs to an existing RRset", |
| 357 |
.changes = changes_append, |
| 358 |
.zonediff = zonediff_append, |
| 359 |
}; |
| 360 |
|
| 361 |
const zonechange_t changes_replace[] = { |
| 362 |
{ DNS_DIFFOP_DEL, "bar.example", 600, "TXT", "bar" }, |
| 363 |
{ DNS_DIFFOP_ADD, "bar.example", 600, "TXT", "rab" }, |
| 364 |
ZONECHANGE_SENTINEL, |
| 365 |
}; |
| 366 |
const zonediff_t zonediff_replace[] = { |
| 367 |
{ DNS_DIFFOP_DELRESIGN, "bar.example", 600, "TXT" }, |
| 368 |
{ DNS_DIFFOP_ADDRESIGN, "bar.example", 600, "TXT" }, |
| 369 |
{ DNS_DIFFOP_DEL, "bar.example", 600, "TXT" }, |
| 370 |
{ DNS_DIFFOP_ADD, "bar.example", 600, "TXT" }, |
| 371 |
ZONEDIFF_SENTINEL, |
| 372 |
}; |
| 373 |
const updatesigs_test_params_t test_replace = { |
| 374 |
.description = "replace an existing RRset", |
| 375 |
.changes = changes_replace, |
| 376 |
.zonediff = zonediff_replace, |
| 377 |
}; |
| 378 |
|
| 379 |
const zonechange_t changes_delete[] = { |
| 380 |
{ DNS_DIFFOP_DEL, "bar.example", 600, "TXT", "rab" }, |
| 381 |
ZONECHANGE_SENTINEL, |
| 382 |
}; |
| 383 |
const zonediff_t zonediff_delete[] = { |
| 384 |
{ DNS_DIFFOP_DELRESIGN, "bar.example", 600, "TXT" }, |
| 385 |
{ DNS_DIFFOP_DEL, "bar.example", 600, "TXT" }, |
| 386 |
ZONEDIFF_SENTINEL, |
| 387 |
}; |
| 388 |
const updatesigs_test_params_t test_delete = { |
| 389 |
.description = "delete an existing RRset", |
| 390 |
.changes = changes_delete, |
| 391 |
.zonediff = zonediff_delete, |
| 392 |
}; |
| 393 |
|
| 394 |
const zonechange_t changes_mixed[] = { |
| 395 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "TXT", "baz1" }, |
| 396 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "A", "127.0.0.1" }, |
| 397 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "TXT", "baz2" }, |
| 398 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "AAAA", "::1" }, |
| 399 |
ZONECHANGE_SENTINEL, |
| 400 |
}; |
| 401 |
const zonediff_t zonediff_mixed[] = { |
| 402 |
{ DNS_DIFFOP_ADDRESIGN, "baz.example", 900, "TXT" }, |
| 403 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "TXT" }, |
| 404 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "TXT" }, |
| 405 |
{ DNS_DIFFOP_ADDRESIGN, "baz.example", 900, "A" }, |
| 406 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "A" }, |
| 407 |
{ DNS_DIFFOP_ADDRESIGN, "baz.example", 900, "AAAA" }, |
| 408 |
{ DNS_DIFFOP_ADD, "baz.example", 900, "AAAA" }, |
| 409 |
ZONEDIFF_SENTINEL, |
| 410 |
}; |
| 411 |
const updatesigs_test_params_t test_mixed = { |
| 412 |
.description = "add different RRsets with common owner name", |
| 413 |
.changes = changes_mixed, |
| 414 |
.zonediff = zonediff_mixed, |
| 415 |
}; |
| 416 |
|
| 417 |
const updatesigs_test_params_t *tests[] = { |
| 418 |
&test_add, &test_append, &test_replace, |
| 419 |
&test_delete, &test_mixed, |
| 420 |
}; |
| 421 |
|
| 422 |
/* |
| 423 |
* Run tests. |
| 424 |
*/ |
| 425 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 426 |
updatesigs_test(tests[i], zone, db, zone_keys, nkeys, now); |
| 427 |
} |
| 428 |
|
| 429 |
/* |
| 430 |
* Clean up. |
| 431 |
*/ |
| 432 |
for (i = 0; i < nkeys; i++) { |
| 433 |
dst_key_free(&zone_keys[i]); |
| 434 |
} |
| 435 |
dns_db_detach(&db); |
| 436 |
dns_zone_detach(&zone); |
| 437 |
} |
| 438 |
|
| 439 |
ISC_TEST_LIST_START |
| 440 |
ISC_TEST_ENTRY_CUSTOM(updatesigs_next, setup_test, teardown_test) |
| 441 |
ISC_TEST_LIST_END |
| 442 |
|
| 443 |
ISC_TEST_MAIN |