| 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/util.h> |
| 27 |
|
| 28 |
#include <dns/db.h> |
| 29 |
#include <dns/dbiterator.h> |
| 30 |
#include <dns/name.h> |
| 31 |
|
| 32 |
#include <tests/dns.h> |
| 33 |
|
| 34 |
#define BUFLEN 255 |
| 35 |
#define BIGBUFLEN (64 * 1024) |
| 36 |
#define TEST_ORIGIN "test" |
| 37 |
|
| 38 |
static isc_result_t |
| 39 |
make_name(const char *src, dns_name_t *name) { |
| 40 |
isc_buffer_t b; |
| 41 |
isc_buffer_constinit(&b, src, strlen(src)); |
| 42 |
isc_buffer_add(&b, strlen(src)); |
| 43 |
return dns_name_fromtext(name, &b, dns_rootname, 0, NULL); |
| 44 |
} |
| 45 |
|
| 46 |
/* create: make sure we can create a dbiterator */ |
| 47 |
static void |
| 48 |
test_create(const char *filename) { |
| 49 |
isc_result_t result; |
| 50 |
dns_db_t *db = NULL; |
| 51 |
dns_dbiterator_t *iter = NULL; |
| 52 |
|
| 53 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 54 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 55 |
|
| 56 |
result = dns_db_createiterator(db, 0, &iter); |
| 57 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 58 |
|
| 59 |
dns_dbiterator_destroy(&iter); |
| 60 |
dns_db_detach(&db); |
| 61 |
} |
| 62 |
|
| 63 |
ISC_RUN_TEST_IMPL(create) { |
| 64 |
UNUSED(state); |
| 65 |
|
| 66 |
test_create(TESTS_DIR "/testdata/dbiterator/zone1.data"); |
| 67 |
} |
| 68 |
|
| 69 |
ISC_RUN_TEST_IMPL(create_nsec3) { |
| 70 |
UNUSED(state); |
| 71 |
|
| 72 |
test_create(TESTS_DIR "/testdata/dbiterator/zone2.data"); |
| 73 |
} |
| 74 |
|
| 75 |
/* walk: walk a database */ |
| 76 |
static void |
| 77 |
test_walk(const char *filename, int flags, int nodes) { |
| 78 |
isc_result_t result; |
| 79 |
dns_db_t *db = NULL; |
| 80 |
dns_dbiterator_t *iter = NULL; |
| 81 |
dns_dbnode_t *node = NULL; |
| 82 |
dns_name_t *name; |
| 83 |
dns_fixedname_t f; |
| 84 |
int i = 0; |
| 85 |
|
| 86 |
name = dns_fixedname_initname(&f); |
| 87 |
|
| 88 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 89 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 90 |
|
| 91 |
result = dns_db_createiterator(db, flags, &iter); |
| 92 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 93 |
|
| 94 |
for (result = dns_dbiterator_first(iter); result == ISC_R_SUCCESS; |
| 95 |
result = dns_dbiterator_next(iter)) |
| 96 |
{ |
| 97 |
result = dns_dbiterator_current(iter, &node, name); |
| 98 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 99 |
dns_db_detachnode(db, &node); |
| 100 |
i++; |
| 101 |
} |
| 102 |
|
| 103 |
assert_int_equal(i, nodes); |
| 104 |
|
| 105 |
dns_dbiterator_destroy(&iter); |
| 106 |
dns_db_detach(&db); |
| 107 |
} |
| 108 |
|
| 109 |
ISC_RUN_TEST_IMPL(walk) { |
| 110 |
UNUSED(state); |
| 111 |
|
| 112 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone1.data", 0, 12); |
| 113 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone1.data", DNS_DB_NONSEC3, |
| 114 |
12); |
| 115 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone1.data", DNS_DB_NSEC3ONLY, |
| 116 |
0); |
| 117 |
} |
| 118 |
|
| 119 |
ISC_RUN_TEST_IMPL(walk_nsec3) { |
| 120 |
UNUSED(state); |
| 121 |
|
| 122 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone2.data", 0, 32); |
| 123 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone2.data", DNS_DB_NONSEC3, |
| 124 |
12); |
| 125 |
test_walk(TESTS_DIR "/testdata/dbiterator/zone2.data", DNS_DB_NSEC3ONLY, |
| 126 |
20); |
| 127 |
} |
| 128 |
|
| 129 |
/* reverse: walk database backwards */ |
| 130 |
static void |
| 131 |
test_reverse(const char *filename, int flags, int nodes) { |
| 132 |
isc_result_t result; |
| 133 |
dns_db_t *db = NULL; |
| 134 |
dns_dbiterator_t *iter = NULL; |
| 135 |
dns_dbnode_t *node = NULL; |
| 136 |
dns_name_t *name; |
| 137 |
dns_fixedname_t f; |
| 138 |
int i = 0; |
| 139 |
|
| 140 |
name = dns_fixedname_initname(&f); |
| 141 |
|
| 142 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 143 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 144 |
|
| 145 |
result = dns_db_createiterator(db, flags, &iter); |
| 146 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 147 |
|
| 148 |
for (result = dns_dbiterator_last(iter); result == ISC_R_SUCCESS; |
| 149 |
result = dns_dbiterator_prev(iter)) |
| 150 |
{ |
| 151 |
result = dns_dbiterator_current(iter, &node, name); |
| 152 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 153 |
dns_db_detachnode(db, &node); |
| 154 |
i++; |
| 155 |
} |
| 156 |
|
| 157 |
assert_int_equal(i, nodes); |
| 158 |
|
| 159 |
dns_dbiterator_destroy(&iter); |
| 160 |
dns_db_detach(&db); |
| 161 |
} |
| 162 |
|
| 163 |
ISC_RUN_TEST_IMPL(reverse) { |
| 164 |
UNUSED(state); |
| 165 |
|
| 166 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone1.data", 0, 12); |
| 167 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone1.data", |
| 168 |
DNS_DB_NONSEC3, 12); |
| 169 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone1.data", |
| 170 |
DNS_DB_NSEC3ONLY, 0); |
| 171 |
} |
| 172 |
|
| 173 |
ISC_RUN_TEST_IMPL(reverse_nsec3) { |
| 174 |
UNUSED(state); |
| 175 |
|
| 176 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone2.data", 0, 32); |
| 177 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone2.data", |
| 178 |
DNS_DB_NONSEC3, 12); |
| 179 |
test_reverse(TESTS_DIR "/testdata/dbiterator/zone2.data", |
| 180 |
DNS_DB_NSEC3ONLY, 20); |
| 181 |
} |
| 182 |
|
| 183 |
/* seek: walk database starting at a particular node */ |
| 184 |
static void |
| 185 |
test_seek_node(const char *filename, int flags, int nodes) { |
| 186 |
isc_result_t result; |
| 187 |
dns_db_t *db = NULL; |
| 188 |
dns_dbiterator_t *iter = NULL; |
| 189 |
dns_dbnode_t *node = NULL; |
| 190 |
dns_name_t *name, *seekname; |
| 191 |
dns_fixedname_t f1, f2; |
| 192 |
int i = 0; |
| 193 |
|
| 194 |
name = dns_fixedname_initname(&f1); |
| 195 |
seekname = dns_fixedname_initname(&f2); |
| 196 |
|
| 197 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 198 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 199 |
|
| 200 |
result = dns_db_createiterator(db, flags, &iter); |
| 201 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 202 |
|
| 203 |
result = make_name("c." TEST_ORIGIN, seekname); |
| 204 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 205 |
|
| 206 |
result = dns_dbiterator_seek(iter, seekname); |
| 207 |
if (flags == DNS_DB_NSEC3ONLY) { |
| 208 |
/* "c" isn't in the NSEC3 tree but the origin node is */ |
| 209 |
assert_int_equal(result, DNS_R_PARTIALMATCH); |
| 210 |
} else { |
| 211 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 212 |
} |
| 213 |
|
| 214 |
while (result == ISC_R_SUCCESS) { |
| 215 |
result = dns_dbiterator_current(iter, &node, name); |
| 216 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 217 |
dns_db_detachnode(db, &node); |
| 218 |
result = dns_dbiterator_next(iter); |
| 219 |
i++; |
| 220 |
} |
| 221 |
|
| 222 |
assert_int_equal(i, nodes); |
| 223 |
|
| 224 |
/* now reset the iterator and walk backwards */ |
| 225 |
i = 0; |
| 226 |
result = dns_dbiterator_seek(iter, seekname); |
| 227 |
if (flags == DNS_DB_NSEC3ONLY) { |
| 228 |
/* "c" isn't in the NSEC3 tree but the origin node is */ |
| 229 |
assert_int_equal(result, DNS_R_PARTIALMATCH); |
| 230 |
nodes = 0; |
| 231 |
} else { |
| 232 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 233 |
nodes = 4; |
| 234 |
} |
| 235 |
|
| 236 |
while (result == ISC_R_SUCCESS) { |
| 237 |
result = dns_dbiterator_current(iter, &node, name); |
| 238 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 239 |
dns_db_detachnode(db, &node); |
| 240 |
result = dns_dbiterator_prev(iter); |
| 241 |
i++; |
| 242 |
} |
| 243 |
|
| 244 |
assert_int_equal(i, nodes); |
| 245 |
|
| 246 |
dns_dbiterator_destroy(&iter); |
| 247 |
dns_db_detach(&db); |
| 248 |
} |
| 249 |
|
| 250 |
ISC_RUN_TEST_IMPL(seek_node) { |
| 251 |
UNUSED(state); |
| 252 |
|
| 253 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone1.data", 0, 9); |
| 254 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone1.data", |
| 255 |
DNS_DB_NONSEC3, 9); |
| 256 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone1.data", |
| 257 |
DNS_DB_NSEC3ONLY, 0); |
| 258 |
} |
| 259 |
|
| 260 |
ISC_RUN_TEST_IMPL(seek_node_nsec3) { |
| 261 |
UNUSED(state); |
| 262 |
|
| 263 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone2.data", 0, 29); |
| 264 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone2.data", |
| 265 |
DNS_DB_NONSEC3, 9); |
| 266 |
test_seek_node(TESTS_DIR "/testdata/dbiterator/zone2.data", |
| 267 |
DNS_DB_NSEC3ONLY, 0); |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* seek_emty: walk database starting at an empty nonterminal node |
| 272 |
* (should fail) |
| 273 |
*/ |
| 274 |
static void |
| 275 |
test_seek_empty(const char *filename) { |
| 276 |
isc_result_t result; |
| 277 |
dns_db_t *db = NULL; |
| 278 |
dns_dbiterator_t *iter = NULL; |
| 279 |
dns_name_t *seekname; |
| 280 |
dns_fixedname_t f1; |
| 281 |
|
| 282 |
seekname = dns_fixedname_initname(&f1); |
| 283 |
|
| 284 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 285 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 286 |
|
| 287 |
result = dns_db_createiterator(db, 0, &iter); |
| 288 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 289 |
|
| 290 |
result = make_name("d." TEST_ORIGIN, seekname); |
| 291 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 292 |
|
| 293 |
result = dns_dbiterator_seek(iter, seekname); |
| 294 |
assert_int_equal(result, DNS_R_PARTIALMATCH); |
| 295 |
|
| 296 |
dns_dbiterator_destroy(&iter); |
| 297 |
dns_db_detach(&db); |
| 298 |
} |
| 299 |
|
| 300 |
ISC_RUN_TEST_IMPL(seek_empty) { |
| 301 |
UNUSED(state); |
| 302 |
|
| 303 |
test_seek_empty(TESTS_DIR "/testdata/dbiterator/zone1.data"); |
| 304 |
} |
| 305 |
|
| 306 |
ISC_RUN_TEST_IMPL(seek_empty_nsec3) { |
| 307 |
UNUSED(state); |
| 308 |
|
| 309 |
test_seek_empty(TESTS_DIR "/testdata/dbiterator/zone2.data"); |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* seek_nx: walk database starting at a nonexistent node |
| 314 |
*/ |
| 315 |
static void |
| 316 |
test_seek_nx(const char *filename) { |
| 317 |
isc_result_t result; |
| 318 |
dns_db_t *db = NULL; |
| 319 |
dns_dbiterator_t *iter = NULL; |
| 320 |
dns_name_t *seekname; |
| 321 |
dns_fixedname_t f1; |
| 322 |
|
| 323 |
seekname = dns_fixedname_initname(&f1); |
| 324 |
|
| 325 |
result = dns_test_loaddb(&db, dns_dbtype_zone, TEST_ORIGIN, filename); |
| 326 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 327 |
|
| 328 |
result = dns_db_createiterator(db, 0, &iter); |
| 329 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 330 |
|
| 331 |
result = make_name("nonexistent." TEST_ORIGIN, seekname); |
| 332 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 333 |
|
| 334 |
result = dns_dbiterator_seek(iter, seekname); |
| 335 |
assert_int_equal(result, DNS_R_PARTIALMATCH); |
| 336 |
|
| 337 |
result = make_name("nonexistent.", seekname); |
| 338 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 339 |
|
| 340 |
result = dns_dbiterator_seek(iter, seekname); |
| 341 |
assert_int_equal(result, ISC_R_NOTFOUND); |
| 342 |
|
| 343 |
dns_dbiterator_destroy(&iter); |
| 344 |
dns_db_detach(&db); |
| 345 |
} |
| 346 |
|
| 347 |
ISC_RUN_TEST_IMPL(seek_nx) { |
| 348 |
UNUSED(state); |
| 349 |
|
| 350 |
test_seek_nx(TESTS_DIR "/testdata/dbiterator/zone1.data"); |
| 351 |
} |
| 352 |
|
| 353 |
ISC_RUN_TEST_IMPL(seek_nx_nsec3) { |
| 354 |
UNUSED(state); |
| 355 |
|
| 356 |
test_seek_nx(TESTS_DIR "/testdata/dbiterator/zone2.data"); |
| 357 |
} |
| 358 |
|
| 359 |
/* |
| 360 |
* XXX: |
| 361 |
* dns_dbiterator API calls that are not yet part of this unit test: |
| 362 |
* |
| 363 |
* dns_dbiterator_pause |
| 364 |
* dns_dbiterator_origin |
| 365 |
* dns_dbiterator_setcleanmode |
| 366 |
*/ |
| 367 |
ISC_TEST_LIST_START |
| 368 |
ISC_TEST_ENTRY(create) |
| 369 |
ISC_TEST_ENTRY(create_nsec3) |
| 370 |
ISC_TEST_ENTRY(walk) |
| 371 |
ISC_TEST_ENTRY(walk_nsec3) |
| 372 |
ISC_TEST_ENTRY(reverse) |
| 373 |
ISC_TEST_ENTRY(reverse_nsec3) |
| 374 |
ISC_TEST_ENTRY(seek_node) |
| 375 |
ISC_TEST_ENTRY(seek_node_nsec3) |
| 376 |
ISC_TEST_ENTRY(seek_empty) |
| 377 |
ISC_TEST_ENTRY(seek_empty_nsec3) |
| 378 |
ISC_TEST_ENTRY(seek_nx) |
| 379 |
ISC_TEST_ENTRY(seek_nx_nsec3) |
| 380 |
ISC_TEST_LIST_END |
| 381 |
|
| 382 |
ISC_TEST_MAIN |