| 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 <stdbool.h> |
| 19 |
#include <stddef.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/quota.h> |
| 27 |
#include <isc/util.h> |
| 28 |
|
| 29 |
#include <dns/badcache.h> |
| 30 |
#include <dns/view.h> |
| 31 |
#include <dns/zone.h> |
| 32 |
|
| 33 |
#include <ns/client.h> |
| 34 |
#include <ns/hooks.h> |
| 35 |
#include <ns/query.h> |
| 36 |
#include <ns/server.h> |
| 37 |
#include <ns/stats.h> |
| 38 |
|
| 39 |
#include <tests/ns.h> |
| 40 |
|
| 41 |
/* can be used for client->sendcb to avoid disruption on sending a response */ |
| 42 |
static void |
| 43 |
send_noop(isc_buffer_t *buffer) { |
| 44 |
UNUSED(buffer); |
| 45 |
} |
| 46 |
|
| 47 |
/***** |
| 48 |
***** ns__query_sfcache() tests |
| 49 |
*****/ |
| 50 |
|
| 51 |
/*% |
| 52 |
* Structure containing parameters for ns__query_sfcache_test(). |
| 53 |
*/ |
| 54 |
typedef struct { |
| 55 |
const ns_test_id_t id; /* libns test identifier */ |
| 56 |
unsigned int qflags; /* query flags */ |
| 57 |
bool cache_entry_present; /* whether a SERVFAIL |
| 58 |
* cache entry |
| 59 |
* matching the query |
| 60 |
* should be |
| 61 |
* present */ |
| 62 |
uint32_t cache_entry_flags; /* NS_FAILCACHE_* flags to |
| 63 |
* set for |
| 64 |
* the SERVFAIL cache entry |
| 65 |
* */ |
| 66 |
bool servfail_expected; /* whether a cached |
| 67 |
* SERVFAIL is |
| 68 |
* expected to be returned |
| 69 |
* */ |
| 70 |
} ns__query_sfcache_test_params_t; |
| 71 |
|
| 72 |
/*% |
| 73 |
* Perform a single ns__query_sfcache() check using given parameters. |
| 74 |
*/ |
| 75 |
static void |
| 76 |
run_sfcache_test(const ns__query_sfcache_test_params_t *test) { |
| 77 |
ns_hooktable_t *query_hooks = NULL; |
| 78 |
query_ctx_t *qctx = NULL; |
| 79 |
isc_result_t result; |
| 80 |
const ns_hook_t hook = { |
| 81 |
.action = ns_test_hook_catch_call, |
| 82 |
}; |
| 83 |
|
| 84 |
REQUIRE(test != NULL); |
| 85 |
REQUIRE(test->id.description != NULL); |
| 86 |
REQUIRE(test->cache_entry_present || test->cache_entry_flags == 0); |
| 87 |
|
| 88 |
/* |
| 89 |
* Interrupt execution if ns_query_done() is called. |
| 90 |
*/ |
| 91 |
|
| 92 |
ns_hooktable_create(mctx, &query_hooks); |
| 93 |
ns_hook_add(query_hooks, mctx, NS_QUERY_DONE_BEGIN, &hook); |
| 94 |
ns__hook_table = query_hooks; |
| 95 |
|
| 96 |
/* |
| 97 |
* Construct a query context for a ./NS query with given flags. |
| 98 |
*/ |
| 99 |
{ |
| 100 |
const ns_test_qctx_create_params_t qctx_params = { |
| 101 |
.qname = ".", |
| 102 |
.qtype = dns_rdatatype_ns, |
| 103 |
.qflags = test->qflags, |
| 104 |
.with_cache = true, |
| 105 |
}; |
| 106 |
|
| 107 |
result = ns_test_qctx_create(&qctx_params, &qctx); |
| 108 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
* If this test wants a SERVFAIL cache entry matching the query to |
| 113 |
* exist, create it. |
| 114 |
*/ |
| 115 |
if (test->cache_entry_present) { |
| 116 |
isc_interval_t hour; |
| 117 |
isc_time_t expire; |
| 118 |
|
| 119 |
isc_interval_set(&hour, 3600, 0); |
| 120 |
result = isc_time_nowplusinterval(&expire, &hour); |
| 121 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 122 |
|
| 123 |
dns_badcache_add(qctx->client->view->failcache, dns_rootname, |
| 124 |
dns_rdatatype_ns, test->cache_entry_flags, |
| 125 |
isc_time_seconds(&expire)); |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* Check whether ns__query_sfcache() behaves as expected. |
| 130 |
*/ |
| 131 |
ns__query_sfcache(qctx); |
| 132 |
|
| 133 |
if (test->servfail_expected) { |
| 134 |
if (qctx->result != DNS_R_SERVFAIL) { |
| 135 |
fail_msg("# test \"%s\" on line %d: " |
| 136 |
"expected SERVFAIL, got %s", |
| 137 |
test->id.description, test->id.lineno, |
| 138 |
isc_result_totext(qctx->result)); |
| 139 |
} |
| 140 |
} else { |
| 141 |
if (qctx->result != ISC_R_SUCCESS) { |
| 142 |
fail_msg("# test \"%s\" on line %d: " |
| 143 |
"expected success, got %s", |
| 144 |
test->id.description, test->id.lineno, |
| 145 |
isc_result_totext(qctx->result)); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* Clean up. |
| 151 |
*/ |
| 152 |
ns_test_qctx_destroy(&qctx); |
| 153 |
ns_hooktable_free(mctx, (void **)&query_hooks); |
| 154 |
} |
| 155 |
|
| 156 |
/* test ns__query_sfcache() */ |
| 157 |
ISC_LOOP_TEST_IMPL(ns__query_sfcache) { |
| 158 |
const ns__query_sfcache_test_params_t tests[] = { |
| 159 |
/* |
| 160 |
* Sanity check for an empty SERVFAIL cache. |
| 161 |
*/ |
| 162 |
{ |
| 163 |
NS_TEST_ID("query: RD=1, CD=0; cache: empty"), |
| 164 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 165 |
.cache_entry_present = false, |
| 166 |
.servfail_expected = false, |
| 167 |
}, |
| 168 |
/* |
| 169 |
* Query: RD=1, CD=0. Cache entry: CD=0. Should SERVFAIL. |
| 170 |
*/ |
| 171 |
{ |
| 172 |
NS_TEST_ID("query: RD=1, CD=0; cache: CD=0"), |
| 173 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 174 |
.cache_entry_present = true, |
| 175 |
.cache_entry_flags = 0, |
| 176 |
.servfail_expected = true, |
| 177 |
}, |
| 178 |
/* |
| 179 |
* Query: RD=1, CD=1. Cache entry: CD=0. Should not SERVFAIL: |
| 180 |
* failed validation should not influence CD=1 queries. |
| 181 |
*/ |
| 182 |
{ |
| 183 |
NS_TEST_ID("query: RD=1, CD=1; cache: CD=0"), |
| 184 |
.qflags = DNS_MESSAGEFLAG_RD | DNS_MESSAGEFLAG_CD, |
| 185 |
.cache_entry_present = true, |
| 186 |
.cache_entry_flags = 0, |
| 187 |
.servfail_expected = false, |
| 188 |
}, |
| 189 |
/* |
| 190 |
* Query: RD=1, CD=1. Cache entry: CD=1. Should SERVFAIL: |
| 191 |
* SERVFAIL responses elicited by CD=1 queries can be |
| 192 |
* "replayed" for other CD=1 queries during the lifetime of the |
| 193 |
* SERVFAIL cache entry. |
| 194 |
*/ |
| 195 |
{ |
| 196 |
NS_TEST_ID("query: RD=1, CD=1; cache: CD=1"), |
| 197 |
.qflags = DNS_MESSAGEFLAG_RD | DNS_MESSAGEFLAG_CD, |
| 198 |
.cache_entry_present = true, |
| 199 |
.cache_entry_flags = NS_FAILCACHE_CD, |
| 200 |
.servfail_expected = true, |
| 201 |
}, |
| 202 |
/* |
| 203 |
* Query: RD=1, CD=0. Cache entry: CD=1. Should SERVFAIL: if |
| 204 |
* a CD=1 query elicited a SERVFAIL, a CD=0 query for the same |
| 205 |
* QNAME and QTYPE will SERVFAIL as well. |
| 206 |
*/ |
| 207 |
{ |
| 208 |
NS_TEST_ID("query: RD=1, CD=0; cache: CD=1"), |
| 209 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 210 |
.cache_entry_present = true, |
| 211 |
.cache_entry_flags = NS_FAILCACHE_CD, |
| 212 |
.servfail_expected = true, |
| 213 |
}, |
| 214 |
/* |
| 215 |
* Query: RD=0, CD=0. Cache entry: CD=0. Should not SERVFAIL |
| 216 |
* despite a matching entry being present as the SERVFAIL cache |
| 217 |
* should not be consulted for non-recursive queries. |
| 218 |
*/ |
| 219 |
{ |
| 220 |
NS_TEST_ID("query: RD=0, CD=0; cache: CD=0"), |
| 221 |
.qflags = 0, |
| 222 |
.cache_entry_present = true, |
| 223 |
.cache_entry_flags = 0, |
| 224 |
.servfail_expected = false, |
| 225 |
}, |
| 226 |
}; |
| 227 |
|
| 228 |
for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 229 |
run_sfcache_test(&tests[i]); |
| 230 |
} |
| 231 |
|
| 232 |
isc_loop_teardown(mainloop, shutdown_interfacemgr, NULL); |
| 233 |
isc_loopmgr_shutdown(loopmgr); |
| 234 |
} |
| 235 |
|
| 236 |
/***** |
| 237 |
***** ns__query_start() tests |
| 238 |
*****/ |
| 239 |
|
| 240 |
/*% |
| 241 |
* Structure containing parameters for ns__query_start_test(). |
| 242 |
*/ |
| 243 |
typedef struct { |
| 244 |
const ns_test_id_t id; /* libns test identifier */ |
| 245 |
const char *qname; /* QNAME */ |
| 246 |
dns_rdatatype_t qtype; /* QTYPE */ |
| 247 |
unsigned int qflags; /* query flags */ |
| 248 |
bool disable_name_checks; /* if set to true, owner |
| 249 |
* name checks will |
| 250 |
* be disabled for the |
| 251 |
* view created |
| 252 |
*/ |
| 253 |
bool recursive_service; /* if set to true, the view |
| 254 |
* created will have a cache |
| 255 |
* database attached */ |
| 256 |
const char *auth_zone_origin; /* origin name of the zone |
| 257 |
* the created view will be |
| 258 |
* authoritative for */ |
| 259 |
const char *auth_zone_path; /* path to load the |
| 260 |
* authoritative |
| 261 |
* zone from */ |
| 262 |
enum { /* expected result: */ |
| 263 |
NS__QUERY_START_R_INVALID, |
| 264 |
NS__QUERY_START_R_REFUSE, /* query should be REFUSED */ |
| 265 |
NS__QUERY_START_R_CACHE, /* query should be answered from |
| 266 |
* cache */ |
| 267 |
NS__QUERY_START_R_AUTH, /* query should be answered using |
| 268 |
* authoritative data */ |
| 269 |
} expected_result; |
| 270 |
} ns__query_start_test_params_t; |
| 271 |
|
| 272 |
/*% |
| 273 |
* Perform a single ns__query_start() check using given parameters. |
| 274 |
*/ |
| 275 |
static void |
| 276 |
run_start_test(const ns__query_start_test_params_t *test) { |
| 277 |
ns_hooktable_t *query_hooks = NULL; |
| 278 |
query_ctx_t *qctx = NULL; |
| 279 |
isc_result_t result; |
| 280 |
const ns_hook_t hook = { |
| 281 |
.action = ns_test_hook_catch_call, |
| 282 |
}; |
| 283 |
|
| 284 |
REQUIRE(test != NULL); |
| 285 |
REQUIRE(test->id.description != NULL); |
| 286 |
REQUIRE((test->auth_zone_origin == NULL && |
| 287 |
test->auth_zone_path == NULL) || |
| 288 |
(test->auth_zone_origin != NULL && |
| 289 |
test->auth_zone_path != NULL)); |
| 290 |
|
| 291 |
/* |
| 292 |
* Interrupt execution if query_lookup() or ns_query_done() is called. |
| 293 |
*/ |
| 294 |
ns_hooktable_create(mctx, &query_hooks); |
| 295 |
ns_hook_add(query_hooks, mctx, NS_QUERY_LOOKUP_BEGIN, &hook); |
| 296 |
ns_hook_add(query_hooks, mctx, NS_QUERY_DONE_BEGIN, &hook); |
| 297 |
ns__hook_table = query_hooks; |
| 298 |
|
| 299 |
/* |
| 300 |
* Construct a query context using the supplied parameters. |
| 301 |
*/ |
| 302 |
{ |
| 303 |
const ns_test_qctx_create_params_t qctx_params = { |
| 304 |
.qname = test->qname, |
| 305 |
.qtype = test->qtype, |
| 306 |
.qflags = test->qflags, |
| 307 |
.with_cache = test->recursive_service, |
| 308 |
}; |
| 309 |
result = ns_test_qctx_create(&qctx_params, &qctx); |
| 310 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 311 |
} |
| 312 |
|
| 313 |
/* |
| 314 |
* Enable view->checknames by default, disable if requested. |
| 315 |
*/ |
| 316 |
qctx->client->view->checknames = !test->disable_name_checks; |
| 317 |
|
| 318 |
/* |
| 319 |
* Load zone from file and attach it to the client's view, if |
| 320 |
* requested. |
| 321 |
*/ |
| 322 |
if (test->auth_zone_path != NULL) { |
| 323 |
result = ns_test_serve_zone(test->auth_zone_origin, |
| 324 |
test->auth_zone_path, |
| 325 |
qctx->client->view); |
| 326 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* Check whether ns__query_start() behaves as expected. |
| 331 |
*/ |
| 332 |
ns__query_start(qctx); |
| 333 |
|
| 334 |
switch (test->expected_result) { |
| 335 |
case NS__QUERY_START_R_REFUSE: |
| 336 |
if (qctx->result != DNS_R_REFUSED) { |
| 337 |
fail_msg("# test \"%s\" on line %d: " |
| 338 |
"expected REFUSED, got %s", |
| 339 |
test->id.description, test->id.lineno, |
| 340 |
isc_result_totext(qctx->result)); |
| 341 |
} |
| 342 |
if (qctx->zone != NULL) { |
| 343 |
fail_msg("# test \"%s\" on line %d: " |
| 344 |
"no zone was expected to be attached to " |
| 345 |
"query context, but some was", |
| 346 |
test->id.description, test->id.lineno); |
| 347 |
} |
| 348 |
if (qctx->db != NULL) { |
| 349 |
fail_msg("# test \"%s\" on line %d: " |
| 350 |
"no database was expected to be attached to " |
| 351 |
"query context, but some was", |
| 352 |
test->id.description, test->id.lineno); |
| 353 |
} |
| 354 |
break; |
| 355 |
case NS__QUERY_START_R_CACHE: |
| 356 |
if (qctx->result != ISC_R_SUCCESS) { |
| 357 |
fail_msg("# test \"%s\" on line %d: " |
| 358 |
"expected success, got %s", |
| 359 |
test->id.description, test->id.lineno, |
| 360 |
isc_result_totext(qctx->result)); |
| 361 |
} |
| 362 |
if (qctx->zone != NULL) { |
| 363 |
fail_msg("# test \"%s\" on line %d: " |
| 364 |
"no zone was expected to be attached to " |
| 365 |
"query context, but some was", |
| 366 |
test->id.description, test->id.lineno); |
| 367 |
} |
| 368 |
if (qctx->db == NULL || qctx->db != qctx->client->view->cachedb) |
| 369 |
{ |
| 370 |
fail_msg("# test \"%s\" on line %d: " |
| 371 |
"cache database was expected to be " |
| 372 |
"attached to query context, but it was not", |
| 373 |
test->id.description, test->id.lineno); |
| 374 |
} |
| 375 |
break; |
| 376 |
case NS__QUERY_START_R_AUTH: |
| 377 |
if (qctx->result != ISC_R_SUCCESS) { |
| 378 |
fail_msg("# test \"%s\" on line %d: " |
| 379 |
"expected success, got %s", |
| 380 |
test->id.description, test->id.lineno, |
| 381 |
isc_result_totext(qctx->result)); |
| 382 |
} |
| 383 |
if (qctx->zone == NULL) { |
| 384 |
fail_msg("# test \"%s\" on line %d: " |
| 385 |
"a zone was expected to be attached to query " |
| 386 |
"context, but it was not", |
| 387 |
test->id.description, test->id.lineno); |
| 388 |
} |
| 389 |
if (qctx->db == qctx->client->view->cachedb) { |
| 390 |
fail_msg("# test \"%s\" on line %d: " |
| 391 |
"cache database was not expected to be " |
| 392 |
"attached to query context, but it is", |
| 393 |
test->id.description, test->id.lineno); |
| 394 |
} |
| 395 |
break; |
| 396 |
case NS__QUERY_START_R_INVALID: |
| 397 |
fail_msg("# test \"%s\" on line %d has no expected result set", |
| 398 |
test->id.description, test->id.lineno); |
| 399 |
break; |
| 400 |
default: |
| 401 |
UNREACHABLE(); |
| 402 |
} |
| 403 |
|
| 404 |
/* |
| 405 |
* Clean up. |
| 406 |
*/ |
| 407 |
if (test->auth_zone_path != NULL) { |
| 408 |
ns_test_cleanup_zone(); |
| 409 |
} |
| 410 |
ns_test_qctx_destroy(&qctx); |
| 411 |
ns_hooktable_free(mctx, (void **)&query_hooks); |
| 412 |
} |
| 413 |
|
| 414 |
/* test ns__query_start() */ |
| 415 |
ISC_LOOP_TEST_IMPL(ns__query_start) { |
| 416 |
size_t i; |
| 417 |
|
| 418 |
const ns__query_start_test_params_t tests[] = { |
| 419 |
/* |
| 420 |
* Recursive foo/A query to a server without recursive service |
| 421 |
* and no zones configured. Query should be REFUSED. |
| 422 |
*/ |
| 423 |
{ |
| 424 |
NS_TEST_ID("foo/A, no cache, no auth"), |
| 425 |
.qname = "foo", |
| 426 |
.qtype = dns_rdatatype_a, |
| 427 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 428 |
.recursive_service = false, |
| 429 |
.expected_result = NS__QUERY_START_R_REFUSE, |
| 430 |
}, |
| 431 |
/* |
| 432 |
* Recursive foo/A query to a server with recursive service and |
| 433 |
* no zones configured. Query should be answered from cache. |
| 434 |
*/ |
| 435 |
{ |
| 436 |
NS_TEST_ID("foo/A, cache, no auth"), |
| 437 |
.qname = "foo", |
| 438 |
.qtype = dns_rdatatype_a, |
| 439 |
.recursive_service = true, |
| 440 |
.expected_result = NS__QUERY_START_R_CACHE, |
| 441 |
}, |
| 442 |
/* |
| 443 |
* Recursive foo/A query to a server with recursive service and |
| 444 |
* zone "foo" configured. Query should be answered from |
| 445 |
* authoritative data. |
| 446 |
*/ |
| 447 |
{ |
| 448 |
NS_TEST_ID("foo/A, RD=1, cache, auth for foo"), |
| 449 |
.qname = "foo", |
| 450 |
.qtype = dns_rdatatype_a, |
| 451 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 452 |
.recursive_service = true, |
| 453 |
.auth_zone_origin = "foo", |
| 454 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 455 |
.expected_result = NS__QUERY_START_R_AUTH, |
| 456 |
}, |
| 457 |
/* |
| 458 |
* Recursive bar/A query to a server without recursive service |
| 459 |
* and zone "foo" configured. Query should be REFUSED. |
| 460 |
*/ |
| 461 |
{ |
| 462 |
NS_TEST_ID("bar/A, RD=1, no cache, auth for foo"), |
| 463 |
.qname = "bar", |
| 464 |
.qtype = dns_rdatatype_a, |
| 465 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 466 |
.recursive_service = false, |
| 467 |
.auth_zone_origin = "foo", |
| 468 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 469 |
.expected_result = NS__QUERY_START_R_REFUSE, |
| 470 |
}, |
| 471 |
/* |
| 472 |
* Recursive bar/A query to a server with recursive service and |
| 473 |
* zone "foo" configured. Query should be answered from |
| 474 |
* cache. |
| 475 |
*/ |
| 476 |
{ |
| 477 |
NS_TEST_ID("bar/A, RD=1, cache, auth for foo"), |
| 478 |
.qname = "bar", |
| 479 |
.qtype = dns_rdatatype_a, |
| 480 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 481 |
.recursive_service = true, |
| 482 |
.auth_zone_origin = "foo", |
| 483 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 484 |
.expected_result = NS__QUERY_START_R_CACHE, |
| 485 |
}, |
| 486 |
/* |
| 487 |
* Recursive bar.foo/DS query to a server with recursive |
| 488 |
* service and zone "foo" configured. Query should be answered |
| 489 |
* from authoritative data. |
| 490 |
*/ |
| 491 |
{ |
| 492 |
NS_TEST_ID("bar.foo/DS, RD=1, cache, auth for foo"), |
| 493 |
.qname = "bar.foo", |
| 494 |
.qtype = dns_rdatatype_ds, |
| 495 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 496 |
.recursive_service = true, |
| 497 |
.auth_zone_origin = "foo", |
| 498 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 499 |
.expected_result = NS__QUERY_START_R_AUTH, |
| 500 |
}, |
| 501 |
/* |
| 502 |
* Non-recursive bar.foo/DS query to a server with recursive |
| 503 |
* service and zone "foo" configured. Query should be answered |
| 504 |
* from authoritative data. |
| 505 |
*/ |
| 506 |
{ |
| 507 |
NS_TEST_ID("bar.foo/DS, RD=0, cache, auth for foo"), |
| 508 |
.qname = "bar.foo", |
| 509 |
.qtype = dns_rdatatype_ds, |
| 510 |
.qflags = 0, |
| 511 |
.recursive_service = true, |
| 512 |
.auth_zone_origin = "foo", |
| 513 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 514 |
.expected_result = NS__QUERY_START_R_AUTH, |
| 515 |
}, |
| 516 |
/* |
| 517 |
* Recursive foo/DS query to a server with recursive service |
| 518 |
* and zone "foo" configured. Query should be answered from |
| 519 |
* cache. |
| 520 |
*/ |
| 521 |
{ |
| 522 |
NS_TEST_ID("foo/DS, RD=1, cache, auth for foo"), |
| 523 |
.qname = "foo", |
| 524 |
.qtype = dns_rdatatype_ds, |
| 525 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 526 |
.recursive_service = true, |
| 527 |
.auth_zone_origin = "foo", |
| 528 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 529 |
.expected_result = NS__QUERY_START_R_CACHE, |
| 530 |
}, |
| 531 |
/* |
| 532 |
* Non-recursive foo/DS query to a server with recursive |
| 533 |
* service and zone "foo" configured. Query should be answered |
| 534 |
* from authoritative data. |
| 535 |
*/ |
| 536 |
{ |
| 537 |
NS_TEST_ID("foo/DS, RD=0, cache, auth for foo"), |
| 538 |
.qname = "foo", |
| 539 |
.qtype = dns_rdatatype_ds, |
| 540 |
.qflags = 0, |
| 541 |
.recursive_service = true, |
| 542 |
.auth_zone_origin = "foo", |
| 543 |
.auth_zone_path = TESTS_DIR "/testdata/query/foo.db", |
| 544 |
.expected_result = NS__QUERY_START_R_AUTH, |
| 545 |
}, |
| 546 |
/* |
| 547 |
* Recursive _foo/A query to a server with recursive service, |
| 548 |
* no zones configured and owner name checks disabled. Query |
| 549 |
* should be answered from cache. |
| 550 |
*/ |
| 551 |
{ |
| 552 |
NS_TEST_ID("_foo/A, cache, no auth, name checks off"), |
| 553 |
.qname = "_foo", |
| 554 |
.qtype = dns_rdatatype_a, |
| 555 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 556 |
.disable_name_checks = true, |
| 557 |
.recursive_service = true, |
| 558 |
.expected_result = NS__QUERY_START_R_CACHE, |
| 559 |
}, |
| 560 |
/* |
| 561 |
* Recursive _foo/A query to a server with recursive service, |
| 562 |
* no zones configured and owner name checks enabled. Query |
| 563 |
* should be REFUSED. |
| 564 |
*/ |
| 565 |
{ |
| 566 |
NS_TEST_ID("_foo/A, cache, no auth, name checks on"), |
| 567 |
.qname = "_foo", |
| 568 |
.qtype = dns_rdatatype_a, |
| 569 |
.qflags = DNS_MESSAGEFLAG_RD, |
| 570 |
.disable_name_checks = false, |
| 571 |
.recursive_service = true, |
| 572 |
.expected_result = NS__QUERY_START_R_REFUSE, |
| 573 |
}, |
| 574 |
}; |
| 575 |
|
| 576 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 577 |
run_start_test(&tests[i]); |
| 578 |
} |
| 579 |
|
| 580 |
isc_loop_teardown(mainloop, shutdown_interfacemgr, NULL); |
| 581 |
isc_loopmgr_shutdown(loopmgr); |
| 582 |
} |
| 583 |
|
| 584 |
/***** |
| 585 |
***** tests for ns_query_hookasync(). |
| 586 |
*****/ |
| 587 |
|
| 588 |
/*% |
| 589 |
* Structure containing parameters for ns__query_hookasync_test(). |
| 590 |
*/ |
| 591 |
typedef struct { |
| 592 |
const ns_test_id_t id; /* libns test identifier */ |
| 593 |
ns_hookpoint_t hookpoint; /* hook point specified for resume */ |
| 594 |
ns_hookpoint_t hookpoint2; /* expected hook point used after resume */ |
| 595 |
ns_hook_action_t action; /* action for the hook point */ |
| 596 |
isc_result_t start_result; /* result of 'runasync' */ |
| 597 |
bool quota_ok; /* true if recursion quota should be okay */ |
| 598 |
bool do_cancel; /* true if query should be canceled |
| 599 |
* in test */ |
| 600 |
} ns__query_hookasync_test_params_t; |
| 601 |
|
| 602 |
/* Data structure passed from tests to hooks */ |
| 603 |
typedef struct hookasync_data { |
| 604 |
bool async; /* true if in a hook-triggered |
| 605 |
* asynchronous process */ |
| 606 |
bool canceled; /* true if the query has been canceled */ |
| 607 |
isc_result_t start_result; /* result of 'runasync' */ |
| 608 |
ns_hook_resume_t *rev; /* resume state sent on completion */ |
| 609 |
query_ctx_t qctx; /* shallow copy of qctx passed to hook */ |
| 610 |
ns_hookpoint_t hookpoint; /* specifies where to resume */ |
| 611 |
ns_hookpoint_t lasthookpoint; /* remember the last hook point called */ |
| 612 |
} hookasync_data_t; |
| 613 |
|
| 614 |
/* |
| 615 |
* 'destroy' callback of hook recursion ctx. |
| 616 |
* The dynamically allocated context will be freed here, thereby proving |
| 617 |
* this is actually called; otherwise tests would fail due to memory leak. |
| 618 |
*/ |
| 619 |
static void |
| 620 |
destroy_hookactx(ns_hookasync_t **ctxp) { |
| 621 |
ns_hookasync_t *ctx = *ctxp; |
| 622 |
|
| 623 |
*ctxp = NULL; |
| 624 |
isc_mem_putanddetach(&ctx->mctx, ctx, sizeof(*ctx)); |
| 625 |
} |
| 626 |
|
| 627 |
/* 'cancel' callback of hook recursion ctx. */ |
| 628 |
static void |
| 629 |
cancel_hookactx(ns_hookasync_t *ctx) { |
| 630 |
/* Mark the hook data so the test can confirm this is called. */ |
| 631 |
((hookasync_data_t *)ctx->private)->canceled = true; |
| 632 |
} |
| 633 |
|
| 634 |
/* 'runasync' callback passed to ns_query_hookasync */ |
| 635 |
static isc_result_t |
| 636 |
test_hookasync(query_ctx_t *qctx, isc_mem_t *memctx, void *arg, |
| 637 |
isc_loop_t *loop, isc_job_cb cb, void *evarg, |
| 638 |
ns_hookasync_t **ctxp) { |
| 639 |
hookasync_data_t *asdata = arg; |
| 640 |
ns_hookasync_t *ctx = NULL; |
| 641 |
ns_hook_resume_t *rev = NULL; |
| 642 |
|
| 643 |
if (asdata->start_result != ISC_R_SUCCESS) { |
| 644 |
return asdata->start_result; |
| 645 |
} |
| 646 |
|
| 647 |
ctx = isc_mem_get(memctx, sizeof(*ctx)); |
| 648 |
rev = isc_mem_get(memctx, sizeof(*rev)); |
| 649 |
*rev = (ns_hook_resume_t){ |
| 650 |
.hookpoint = asdata->hookpoint, |
| 651 |
.origresult = DNS_R_NXDOMAIN, |
| 652 |
.saved_qctx = qctx, |
| 653 |
.ctx = ctx, |
| 654 |
.loop = loop, |
| 655 |
.cb = cb, |
| 656 |
.arg = evarg, |
| 657 |
}; |
| 658 |
|
| 659 |
asdata->rev = rev; |
| 660 |
|
| 661 |
*ctx = (ns_hookasync_t){ |
| 662 |
.destroy = destroy_hookactx, |
| 663 |
.cancel = cancel_hookactx, |
| 664 |
.private = asdata, |
| 665 |
}; |
| 666 |
isc_mem_attach(memctx, &ctx->mctx); |
| 667 |
|
| 668 |
*ctxp = ctx; |
| 669 |
return ISC_R_SUCCESS; |
| 670 |
} |
| 671 |
|
| 672 |
/* |
| 673 |
* Main logic for hook actions. |
| 674 |
* 'hookpoint' should identify the point that calls the hook. It will be |
| 675 |
* remembered in the hook data, so that the test can confirm which hook point |
| 676 |
* was last used. |
| 677 |
*/ |
| 678 |
static ns_hookresult_t |
| 679 |
hook_async_common(void *arg, void *data, isc_result_t *resultp, |
| 680 |
ns_hookpoint_t hookpoint) { |
| 681 |
query_ctx_t *qctx = arg; |
| 682 |
hookasync_data_t *asdata = data; |
| 683 |
isc_result_t result; |
| 684 |
|
| 685 |
asdata->qctx = *qctx; /* remember passed ctx for inspection */ |
| 686 |
asdata->lasthookpoint = hookpoint; /* ditto */ |
| 687 |
|
| 688 |
if (!asdata->async) { |
| 689 |
/* Initial call to the hook; start recursion */ |
| 690 |
result = ns_query_hookasync(qctx, test_hookasync, asdata); |
| 691 |
if (result == ISC_R_SUCCESS) { |
| 692 |
asdata->async = true; |
| 693 |
} |
| 694 |
} else { |
| 695 |
/* |
| 696 |
* Resume from the completion of async event. The fetch handle |
| 697 |
* should have been detached so that we can start another async |
| 698 |
* event or DNS recursive resolution. |
| 699 |
*/ |
| 700 |
INSIST(HANDLE_RECTYPE_HOOK(qctx->client) == NULL); |
| 701 |
asdata->async = false; |
| 702 |
switch (hookpoint) { |
| 703 |
case NS_QUERY_GOT_ANSWER_BEGIN: |
| 704 |
case NS_QUERY_NODATA_BEGIN: |
| 705 |
case NS_QUERY_NXDOMAIN_BEGIN: |
| 706 |
case NS_QUERY_NCACHE_BEGIN: |
| 707 |
INSIST(*resultp == DNS_R_NXDOMAIN); |
| 708 |
break; |
| 709 |
default:; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
*resultp = ISC_R_UNSET; |
| 714 |
return NS_HOOK_RETURN; |
| 715 |
} |
| 716 |
|
| 717 |
static ns_hookresult_t |
| 718 |
hook_async_query_setup(void *arg, void *data, isc_result_t *resultp) { |
| 719 |
return hook_async_common(arg, data, resultp, NS_QUERY_SETUP); |
| 720 |
} |
| 721 |
|
| 722 |
static ns_hookresult_t |
| 723 |
hook_async_query_start_begin(void *arg, void *data, isc_result_t *resultp) { |
| 724 |
return hook_async_common(arg, data, resultp, NS_QUERY_START_BEGIN); |
| 725 |
} |
| 726 |
|
| 727 |
static ns_hookresult_t |
| 728 |
hook_async_query_lookup_begin(void *arg, void *data, isc_result_t *resultp) { |
| 729 |
return hook_async_common(arg, data, resultp, NS_QUERY_LOOKUP_BEGIN); |
| 730 |
} |
| 731 |
|
| 732 |
static ns_hookresult_t |
| 733 |
hook_async_query_resume_begin(void *arg, void *data, isc_result_t *resultp) { |
| 734 |
return hook_async_common(arg, data, resultp, NS_QUERY_RESUME_BEGIN); |
| 735 |
} |
| 736 |
|
| 737 |
static ns_hookresult_t |
| 738 |
hook_async_query_got_answer_begin(void *arg, void *data, |
| 739 |
isc_result_t *resultp) { |
| 740 |
return hook_async_common(arg, data, resultp, NS_QUERY_GOT_ANSWER_BEGIN); |
| 741 |
} |
| 742 |
|
| 743 |
static ns_hookresult_t |
| 744 |
hook_async_query_respond_any_begin(void *arg, void *data, |
| 745 |
isc_result_t *resultp) { |
| 746 |
return hook_async_common(arg, data, resultp, |
| 747 |
NS_QUERY_RESPOND_ANY_BEGIN); |
| 748 |
} |
| 749 |
|
| 750 |
static ns_hookresult_t |
| 751 |
hook_async_query_addanswer_begin(void *arg, void *data, isc_result_t *resultp) { |
| 752 |
return hook_async_common(arg, data, resultp, NS_QUERY_ADDANSWER_BEGIN); |
| 753 |
} |
| 754 |
|
| 755 |
static ns_hookresult_t |
| 756 |
hook_async_query_notfound_begin(void *arg, void *data, isc_result_t *resultp) { |
| 757 |
return hook_async_common(arg, data, resultp, NS_QUERY_NOTFOUND_BEGIN); |
| 758 |
} |
| 759 |
|
| 760 |
static ns_hookresult_t |
| 761 |
hook_async_query_prep_delegation_begin(void *arg, void *data, |
| 762 |
isc_result_t *resultp) { |
| 763 |
return hook_async_common(arg, data, resultp, |
| 764 |
NS_QUERY_PREP_DELEGATION_BEGIN); |
| 765 |
} |
| 766 |
|
| 767 |
static ns_hookresult_t |
| 768 |
hook_async_query_zone_delegation_begin(void *arg, void *data, |
| 769 |
isc_result_t *resultp) { |
| 770 |
return hook_async_common(arg, data, resultp, |
| 771 |
NS_QUERY_ZONE_DELEGATION_BEGIN); |
| 772 |
} |
| 773 |
|
| 774 |
static ns_hookresult_t |
| 775 |
hook_async_query_delegation_begin(void *arg, void *data, |
| 776 |
isc_result_t *resultp) { |
| 777 |
return hook_async_common(arg, data, resultp, NS_QUERY_DELEGATION_BEGIN); |
| 778 |
} |
| 779 |
|
| 780 |
static ns_hookresult_t |
| 781 |
hook_async_query_delegation_recurse_begin(void *arg, void *data, |
| 782 |
isc_result_t *resultp) { |
| 783 |
return hook_async_common(arg, data, resultp, |
| 784 |
NS_QUERY_DELEGATION_RECURSE_BEGIN); |
| 785 |
} |
| 786 |
|
| 787 |
static ns_hookresult_t |
| 788 |
hook_async_query_nodata_begin(void *arg, void *data, isc_result_t *resultp) { |
| 789 |
return hook_async_common(arg, data, resultp, NS_QUERY_NODATA_BEGIN); |
| 790 |
} |
| 791 |
|
| 792 |
static ns_hookresult_t |
| 793 |
hook_async_query_nxdomain_begin(void *arg, void *data, isc_result_t *resultp) { |
| 794 |
return hook_async_common(arg, data, resultp, NS_QUERY_NXDOMAIN_BEGIN); |
| 795 |
} |
| 796 |
|
| 797 |
static ns_hookresult_t |
| 798 |
hook_async_query_ncache_begin(void *arg, void *data, isc_result_t *resultp) { |
| 799 |
return hook_async_common(arg, data, resultp, NS_QUERY_NCACHE_BEGIN); |
| 800 |
} |
| 801 |
|
| 802 |
static ns_hookresult_t |
| 803 |
hook_async_query_cname_begin(void *arg, void *data, isc_result_t *resultp) { |
| 804 |
return hook_async_common(arg, data, resultp, NS_QUERY_CNAME_BEGIN); |
| 805 |
} |
| 806 |
|
| 807 |
static ns_hookresult_t |
| 808 |
hook_async_query_dname_begin(void *arg, void *data, isc_result_t *resultp) { |
| 809 |
return hook_async_common(arg, data, resultp, NS_QUERY_DNAME_BEGIN); |
| 810 |
} |
| 811 |
|
| 812 |
static ns_hookresult_t |
| 813 |
hook_async_query_respond_begin(void *arg, void *data, isc_result_t *resultp) { |
| 814 |
return hook_async_common(arg, data, resultp, NS_QUERY_RESPOND_BEGIN); |
| 815 |
} |
| 816 |
|
| 817 |
static ns_hookresult_t |
| 818 |
hook_async_query_response_begin(void *arg, void *data, isc_result_t *resultp) { |
| 819 |
return hook_async_common(arg, data, resultp, |
| 820 |
NS_QUERY_PREP_RESPONSE_BEGIN); |
| 821 |
} |
| 822 |
|
| 823 |
static ns_hookresult_t |
| 824 |
hook_async_query_done_begin(void *arg, void *data, isc_result_t *resultp) { |
| 825 |
return hook_async_common(arg, data, resultp, NS_QUERY_DONE_BEGIN); |
| 826 |
} |
| 827 |
|
| 828 |
/* |
| 829 |
* hook on destroying actx. Can't be used for async event, but we use this |
| 830 |
* to remember the qctx at that point. |
| 831 |
*/ |
| 832 |
static ns_hookresult_t |
| 833 |
ns_test_qctx_destroy_hook(void *arg, void *data, isc_result_t *resultp) { |
| 834 |
query_ctx_t *qctx = arg; |
| 835 |
hookasync_data_t *asdata = data; |
| 836 |
|
| 837 |
asdata->qctx = *qctx; /* remember passed ctx for inspection */ |
| 838 |
*resultp = ISC_R_UNSET; |
| 839 |
return NS_HOOK_CONTINUE; |
| 840 |
} |
| 841 |
|
| 842 |
static void |
| 843 |
run_hookasync_test(const ns__query_hookasync_test_params_t *test) { |
| 844 |
query_ctx_t *qctx = NULL; |
| 845 |
isc_result_t result; |
| 846 |
hookasync_data_t asdata = { |
| 847 |
.async = false, |
| 848 |
.canceled = false, |
| 849 |
.start_result = test->start_result, |
| 850 |
.hookpoint = test->hookpoint, |
| 851 |
}; |
| 852 |
const ns_hook_t testhook = { |
| 853 |
.action = test->action, |
| 854 |
.action_data = &asdata, |
| 855 |
}; |
| 856 |
const ns_hook_t destroyhook = { |
| 857 |
.action = ns_test_qctx_destroy_hook, |
| 858 |
.action_data = &asdata, |
| 859 |
}; |
| 860 |
isc_statscounter_t srvfail_cnt; |
| 861 |
bool expect_servfail = false; |
| 862 |
|
| 863 |
/* |
| 864 |
* Prepare hooks. We always begin with ns__query_start for simplicity. |
| 865 |
* Its action will specify various different resume points (unusual |
| 866 |
* in practice, but that's fine for the testing purpose). |
| 867 |
*/ |
| 868 |
ns__hook_table = NULL; |
| 869 |
ns_hooktable_create(mctx, &ns__hook_table); |
| 870 |
ns_hook_add(ns__hook_table, mctx, NS_QUERY_START_BEGIN, &testhook); |
| 871 |
if (test->hookpoint2 != NS_QUERY_START_BEGIN) { |
| 872 |
/* |
| 873 |
* unless testing START_BEGIN itself, specify the hook for the |
| 874 |
* expected resume point, too. |
| 875 |
*/ |
| 876 |
ns_hook_add(ns__hook_table, mctx, test->hookpoint2, &testhook); |
| 877 |
} |
| 878 |
ns_hook_add(ns__hook_table, mctx, NS_QUERY_QCTX_DESTROYED, |
| 879 |
&destroyhook); |
| 880 |
|
| 881 |
{ |
| 882 |
const ns_test_qctx_create_params_t qctx_params = { |
| 883 |
.qname = "test.example.com", |
| 884 |
.qtype = dns_rdatatype_aaaa, |
| 885 |
}; |
| 886 |
result = ns_test_qctx_create(&qctx_params, &qctx); |
| 887 |
INSIST(result == ISC_R_SUCCESS); |
| 888 |
qctx->client->sendcb = send_noop; |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
* Set recursion quota to the lowest possible value, then make it full |
| 893 |
* if we want to exercise a quota failure case. |
| 894 |
*/ |
| 895 |
isc_quota_max(&sctx->recursionquota, 1); |
| 896 |
if (!test->quota_ok) { |
| 897 |
result = isc_quota_acquire(&sctx->recursionquota); |
| 898 |
INSIST(result == ISC_R_SUCCESS); |
| 899 |
} |
| 900 |
|
| 901 |
/* Remember SERVFAIL counter */ |
| 902 |
srvfail_cnt = ns_stats_get_counter(qctx->client->manager->sctx->nsstats, |
| 903 |
ns_statscounter_servfail); |
| 904 |
|
| 905 |
/* |
| 906 |
* If the query has been canceled, or async event didn't succeed, |
| 907 |
* SERVFAIL will have to be sent. In this case we need to have |
| 908 |
* 'reqhandle' attach to the client's handle as it's detached in |
| 909 |
* query_error. |
| 910 |
*/ |
| 911 |
if (test->start_result != ISC_R_SUCCESS || !test->quota_ok || |
| 912 |
test->do_cancel) |
| 913 |
{ |
| 914 |
expect_servfail = true; |
| 915 |
isc_nmhandle_attach(qctx->client->handle, |
| 916 |
&qctx->client->reqhandle); |
| 917 |
} |
| 918 |
|
| 919 |
/* |
| 920 |
* Emulate query handling from query_start. |
| 921 |
* Specified hook should be called. |
| 922 |
*/ |
| 923 |
qctx->client->state = NS_CLIENTSTATE_WORKING; |
| 924 |
result = ns__query_start(qctx); |
| 925 |
INSIST(result == ISC_R_UNSET); |
| 926 |
|
| 927 |
/* |
| 928 |
* hook-triggered async event should be happening unless it hits |
| 929 |
* recursion quota limit or 'runasync' callback fails. |
| 930 |
*/ |
| 931 |
INSIST(asdata.async == |
| 932 |
(test->quota_ok && test->start_result == ISC_R_SUCCESS)); |
| 933 |
|
| 934 |
/* |
| 935 |
* Emulate cancel if so specified. |
| 936 |
* The cancel callback should be called. |
| 937 |
*/ |
| 938 |
if (test->do_cancel) { |
| 939 |
ns_query_cancel(qctx->client); |
| 940 |
} |
| 941 |
INSIST(asdata.canceled == test->do_cancel); |
| 942 |
|
| 943 |
/* If async event has started, manually invoke the 'done' event. */ |
| 944 |
if (asdata.async) { |
| 945 |
qctx->client->now = 0; /* set to sentinel before resume */ |
| 946 |
asdata.rev->cb(asdata.rev); |
| 947 |
|
| 948 |
/* Confirm necessary cleanup has been performed. */ |
| 949 |
INSIST(qctx->client->query.hookactx == NULL); |
| 950 |
INSIST(qctx->client->state == NS_CLIENTSTATE_WORKING); |
| 951 |
INSIST(ns_stats_get_counter( |
| 952 |
qctx->client->manager->sctx->nsstats, |
| 953 |
ns_statscounter_recursclients) == 0); |
| 954 |
INSIST(!ISC_LINK_LINKED(qctx->client, rlink)); |
| 955 |
if (!test->do_cancel) { |
| 956 |
/* |
| 957 |
* In the normal case the client's timestamp is updated |
| 958 |
* and the query handling has been resumed from the |
| 959 |
* expected point. |
| 960 |
*/ |
| 961 |
INSIST(qctx->client->now != 0); |
| 962 |
INSIST(asdata.lasthookpoint == test->hookpoint2); |
| 963 |
} |
| 964 |
} else { |
| 965 |
INSIST(qctx->client->query.hookactx == NULL); |
| 966 |
} |
| 967 |
|
| 968 |
/* |
| 969 |
* Confirm SERVFAIL has been sent if it was expected. |
| 970 |
* Also, the last-generated qctx should have detach_client being true. |
| 971 |
*/ |
| 972 |
if (expect_servfail) { |
| 973 |
INSIST(ns_stats_get_counter( |
| 974 |
qctx->client->manager->sctx->nsstats, |
| 975 |
ns_statscounter_servfail) == srvfail_cnt + 1); |
| 976 |
if (test->do_cancel) { |
| 977 |
/* qctx was created on resume and copied in hook */ |
| 978 |
INSIST(asdata.qctx.detach_client); |
| 979 |
} else { |
| 980 |
INSIST(qctx->detach_client); |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
/* |
| 985 |
* Cleanup. Note that we've kept 'qctx' until now; otherwise |
| 986 |
* qctx->client may have been invalidated while we still need it. |
| 987 |
*/ |
| 988 |
ns_test_qctx_destroy(&qctx); |
| 989 |
ns_hooktable_free(mctx, (void **)&ns__hook_table); |
| 990 |
if (!test->quota_ok) { |
| 991 |
isc_quota_release(&sctx->recursionquota); |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
ISC_LOOP_TEST_IMPL(ns__query_hookasync) { |
| 996 |
size_t i; |
| 997 |
|
| 998 |
const ns__query_hookasync_test_params_t tests[] = { |
| 999 |
{ |
| 1000 |
NS_TEST_ID("normal case"), |
| 1001 |
NS_QUERY_START_BEGIN, |
| 1002 |
NS_QUERY_START_BEGIN, |
| 1003 |
hook_async_query_start_begin, |
| 1004 |
ISC_R_SUCCESS, |
| 1005 |
true, |
| 1006 |
false, |
| 1007 |
}, |
| 1008 |
{ |
| 1009 |
NS_TEST_ID("quota fail"), |
| 1010 |
NS_QUERY_START_BEGIN, |
| 1011 |
NS_QUERY_START_BEGIN, |
| 1012 |
hook_async_query_start_begin, |
| 1013 |
ISC_R_SUCCESS, |
| 1014 |
false, |
| 1015 |
false, |
| 1016 |
}, |
| 1017 |
{ |
| 1018 |
NS_TEST_ID("start fail"), |
| 1019 |
NS_QUERY_START_BEGIN, |
| 1020 |
NS_QUERY_START_BEGIN, |
| 1021 |
hook_async_query_start_begin, |
| 1022 |
ISC_R_FAILURE, |
| 1023 |
true, |
| 1024 |
false, |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
NS_TEST_ID("query cancel"), |
| 1028 |
NS_QUERY_START_BEGIN, |
| 1029 |
NS_QUERY_START_BEGIN, |
| 1030 |
hook_async_query_start_begin, |
| 1031 |
ISC_R_SUCCESS, |
| 1032 |
true, |
| 1033 |
true, |
| 1034 |
}, |
| 1035 |
/* |
| 1036 |
* The rest of the test case just confirms supported hookpoints |
| 1037 |
* with the same test logic. |
| 1038 |
*/ |
| 1039 |
{ |
| 1040 |
NS_TEST_ID("async from setup"), |
| 1041 |
NS_QUERY_SETUP, |
| 1042 |
NS_QUERY_SETUP, |
| 1043 |
hook_async_query_setup, |
| 1044 |
ISC_R_SUCCESS, |
| 1045 |
true, |
| 1046 |
false, |
| 1047 |
}, |
| 1048 |
{ |
| 1049 |
NS_TEST_ID("async from lookup"), |
| 1050 |
NS_QUERY_LOOKUP_BEGIN, |
| 1051 |
NS_QUERY_LOOKUP_BEGIN, |
| 1052 |
hook_async_query_lookup_begin, |
| 1053 |
ISC_R_SUCCESS, |
| 1054 |
true, |
| 1055 |
false, |
| 1056 |
}, |
| 1057 |
{ |
| 1058 |
NS_TEST_ID("async from resume"), |
| 1059 |
NS_QUERY_RESUME_BEGIN, |
| 1060 |
NS_QUERY_RESUME_BEGIN, |
| 1061 |
hook_async_query_resume_begin, |
| 1062 |
ISC_R_SUCCESS, |
| 1063 |
true, |
| 1064 |
false, |
| 1065 |
}, |
| 1066 |
{ |
| 1067 |
NS_TEST_ID("async from resume restored"), |
| 1068 |
NS_QUERY_RESUME_RESTORED, |
| 1069 |
NS_QUERY_RESUME_BEGIN, |
| 1070 |
hook_async_query_resume_begin, |
| 1071 |
ISC_R_SUCCESS, |
| 1072 |
true, |
| 1073 |
false, |
| 1074 |
}, |
| 1075 |
{ |
| 1076 |
NS_TEST_ID("async from gotanswer"), |
| 1077 |
NS_QUERY_GOT_ANSWER_BEGIN, |
| 1078 |
NS_QUERY_GOT_ANSWER_BEGIN, |
| 1079 |
hook_async_query_got_answer_begin, |
| 1080 |
ISC_R_SUCCESS, |
| 1081 |
true, |
| 1082 |
false, |
| 1083 |
}, |
| 1084 |
{ |
| 1085 |
NS_TEST_ID("async from respond any"), |
| 1086 |
NS_QUERY_RESPOND_ANY_BEGIN, |
| 1087 |
NS_QUERY_RESPOND_ANY_BEGIN, |
| 1088 |
hook_async_query_respond_any_begin, |
| 1089 |
ISC_R_SUCCESS, |
| 1090 |
true, |
| 1091 |
false, |
| 1092 |
}, |
| 1093 |
{ |
| 1094 |
NS_TEST_ID("async from add answer"), |
| 1095 |
NS_QUERY_ADDANSWER_BEGIN, |
| 1096 |
NS_QUERY_ADDANSWER_BEGIN, |
| 1097 |
hook_async_query_addanswer_begin, |
| 1098 |
ISC_R_SUCCESS, |
| 1099 |
true, |
| 1100 |
false, |
| 1101 |
}, |
| 1102 |
{ |
| 1103 |
NS_TEST_ID("async from notfound"), |
| 1104 |
NS_QUERY_NOTFOUND_BEGIN, |
| 1105 |
NS_QUERY_NOTFOUND_BEGIN, |
| 1106 |
hook_async_query_notfound_begin, |
| 1107 |
ISC_R_SUCCESS, |
| 1108 |
true, |
| 1109 |
false, |
| 1110 |
}, |
| 1111 |
{ |
| 1112 |
NS_TEST_ID("async from prep delegation"), |
| 1113 |
NS_QUERY_PREP_DELEGATION_BEGIN, |
| 1114 |
NS_QUERY_PREP_DELEGATION_BEGIN, |
| 1115 |
hook_async_query_prep_delegation_begin, |
| 1116 |
ISC_R_SUCCESS, |
| 1117 |
true, |
| 1118 |
false, |
| 1119 |
}, |
| 1120 |
{ |
| 1121 |
NS_TEST_ID("async from zone delegation"), |
| 1122 |
NS_QUERY_ZONE_DELEGATION_BEGIN, |
| 1123 |
NS_QUERY_ZONE_DELEGATION_BEGIN, |
| 1124 |
hook_async_query_zone_delegation_begin, |
| 1125 |
ISC_R_SUCCESS, |
| 1126 |
true, |
| 1127 |
false, |
| 1128 |
}, |
| 1129 |
{ |
| 1130 |
NS_TEST_ID("async from delegation"), |
| 1131 |
NS_QUERY_DELEGATION_BEGIN, |
| 1132 |
NS_QUERY_DELEGATION_BEGIN, |
| 1133 |
hook_async_query_delegation_begin, |
| 1134 |
ISC_R_SUCCESS, |
| 1135 |
true, |
| 1136 |
false, |
| 1137 |
}, |
| 1138 |
{ |
| 1139 |
NS_TEST_ID("async from async delegation"), |
| 1140 |
NS_QUERY_DELEGATION_RECURSE_BEGIN, |
| 1141 |
NS_QUERY_DELEGATION_RECURSE_BEGIN, |
| 1142 |
hook_async_query_delegation_recurse_begin, |
| 1143 |
ISC_R_SUCCESS, |
| 1144 |
true, |
| 1145 |
false, |
| 1146 |
}, |
| 1147 |
{ |
| 1148 |
NS_TEST_ID("async from nodata"), |
| 1149 |
NS_QUERY_NODATA_BEGIN, |
| 1150 |
NS_QUERY_NODATA_BEGIN, |
| 1151 |
hook_async_query_nodata_begin, |
| 1152 |
ISC_R_SUCCESS, |
| 1153 |
true, |
| 1154 |
false, |
| 1155 |
}, |
| 1156 |
{ |
| 1157 |
NS_TEST_ID("async from nxdomain"), |
| 1158 |
NS_QUERY_NXDOMAIN_BEGIN, |
| 1159 |
NS_QUERY_NXDOMAIN_BEGIN, |
| 1160 |
hook_async_query_nxdomain_begin, |
| 1161 |
ISC_R_SUCCESS, |
| 1162 |
true, |
| 1163 |
false, |
| 1164 |
}, |
| 1165 |
{ |
| 1166 |
NS_TEST_ID("async from ncache"), |
| 1167 |
NS_QUERY_NCACHE_BEGIN, |
| 1168 |
NS_QUERY_NCACHE_BEGIN, |
| 1169 |
hook_async_query_ncache_begin, |
| 1170 |
ISC_R_SUCCESS, |
| 1171 |
true, |
| 1172 |
false, |
| 1173 |
}, |
| 1174 |
{ |
| 1175 |
NS_TEST_ID("async from CNAME"), |
| 1176 |
NS_QUERY_CNAME_BEGIN, |
| 1177 |
NS_QUERY_CNAME_BEGIN, |
| 1178 |
hook_async_query_cname_begin, |
| 1179 |
ISC_R_SUCCESS, |
| 1180 |
true, |
| 1181 |
false, |
| 1182 |
}, |
| 1183 |
{ |
| 1184 |
NS_TEST_ID("async from DNAME"), |
| 1185 |
NS_QUERY_DNAME_BEGIN, |
| 1186 |
NS_QUERY_DNAME_BEGIN, |
| 1187 |
hook_async_query_dname_begin, |
| 1188 |
ISC_R_SUCCESS, |
| 1189 |
true, |
| 1190 |
false, |
| 1191 |
}, |
| 1192 |
{ |
| 1193 |
NS_TEST_ID("async from prep response"), |
| 1194 |
NS_QUERY_PREP_RESPONSE_BEGIN, |
| 1195 |
NS_QUERY_PREP_RESPONSE_BEGIN, |
| 1196 |
hook_async_query_response_begin, |
| 1197 |
ISC_R_SUCCESS, |
| 1198 |
true, |
| 1199 |
false, |
| 1200 |
}, |
| 1201 |
{ |
| 1202 |
NS_TEST_ID("async from respond"), |
| 1203 |
NS_QUERY_RESPOND_BEGIN, |
| 1204 |
NS_QUERY_RESPOND_BEGIN, |
| 1205 |
hook_async_query_respond_begin, |
| 1206 |
ISC_R_SUCCESS, |
| 1207 |
true, |
| 1208 |
false, |
| 1209 |
}, |
| 1210 |
{ |
| 1211 |
NS_TEST_ID("async from done begin"), |
| 1212 |
NS_QUERY_DONE_BEGIN, |
| 1213 |
NS_QUERY_DONE_BEGIN, |
| 1214 |
hook_async_query_done_begin, |
| 1215 |
ISC_R_SUCCESS, |
| 1216 |
true, |
| 1217 |
false, |
| 1218 |
}, |
| 1219 |
{ |
| 1220 |
NS_TEST_ID("async from done send"), |
| 1221 |
NS_QUERY_DONE_SEND, |
| 1222 |
NS_QUERY_DONE_BEGIN, |
| 1223 |
hook_async_query_done_begin, |
| 1224 |
ISC_R_SUCCESS, |
| 1225 |
true, |
| 1226 |
false, |
| 1227 |
}, |
| 1228 |
}; |
| 1229 |
|
| 1230 |
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 1231 |
run_hookasync_test(&tests[i]); |
| 1232 |
} |
| 1233 |
|
| 1234 |
isc_loop_teardown(mainloop, shutdown_interfacemgr, NULL); |
| 1235 |
isc_loopmgr_shutdown(loopmgr); |
| 1236 |
} |
| 1237 |
|
| 1238 |
/***** |
| 1239 |
***** tests for higher level ("e2e") behavior of ns_query_hookasync(). |
| 1240 |
***** It exercises overall behavior for some selected cases, while |
| 1241 |
***** ns__query_hookasync_test exercises implementation details for a |
| 1242 |
***** simple scenario and for all supported hook points. |
| 1243 |
*****/ |
| 1244 |
|
| 1245 |
/*% |
| 1246 |
* Structure containing parameters for ns__query_hookasync_e2e_test(). |
| 1247 |
*/ |
| 1248 |
typedef struct { |
| 1249 |
const ns_test_id_t id; /* libns test identifier */ |
| 1250 |
const char *qname; /* QNAME */ |
| 1251 |
ns_hookpoint_t hookpoint; /* hook point specified for resume */ |
| 1252 |
isc_result_t start_result; /* result of 'runasync' */ |
| 1253 |
bool do_cancel; /* true if query should be canceled |
| 1254 |
* in test */ |
| 1255 |
dns_rcode_t expected_rcode; |
| 1256 |
} ns__query_hookasync_e2e_test_params_t; |
| 1257 |
|
| 1258 |
/* data structure passed from tests to hooks */ |
| 1259 |
typedef struct hookasync_e2e_data { |
| 1260 |
bool async; /* true if in a hook-triggered |
| 1261 |
* asynchronous process */ |
| 1262 |
ns_hook_resume_t *rev; /* resume state sent on completion */ |
| 1263 |
ns_hookpoint_t hookpoint; /* specifies where to resume */ |
| 1264 |
isc_result_t start_result; /* result of 'runasync' */ |
| 1265 |
dns_rcode_t expected_rcode; |
| 1266 |
bool done; /* if SEND_DONE hook is called */ |
| 1267 |
} hookasync_e2e_data_t; |
| 1268 |
|
| 1269 |
/* Cancel callback. Just need to be defined, it doesn't have to do anything. */ |
| 1270 |
static void |
| 1271 |
cancel_e2ehookactx(ns_hookasync_t *ctx) { |
| 1272 |
UNUSED(ctx); |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* 'runasync' callback passed to ns_query_hookasync */ |
| 1276 |
static isc_result_t |
| 1277 |
test_hookasync_e2e(query_ctx_t *qctx, isc_mem_t *memctx, void *arg, |
| 1278 |
isc_loop_t *loop, isc_job_cb cb, void *evarg, |
| 1279 |
ns_hookasync_t **ctxp) { |
| 1280 |
ns_hookasync_t *ctx = NULL; |
| 1281 |
ns_hook_resume_t *rev = NULL; |
| 1282 |
hookasync_e2e_data_t *asdata = arg; |
| 1283 |
|
| 1284 |
if (asdata->start_result != ISC_R_SUCCESS) { |
| 1285 |
return asdata->start_result; |
| 1286 |
} |
| 1287 |
|
| 1288 |
ctx = isc_mem_get(memctx, sizeof(*ctx)); |
| 1289 |
rev = isc_mem_get(memctx, sizeof(*rev)); |
| 1290 |
*rev = (ns_hook_resume_t){ |
| 1291 |
.hookpoint = asdata->hookpoint, |
| 1292 |
.saved_qctx = qctx, |
| 1293 |
.ctx = ctx, |
| 1294 |
.loop = loop, |
| 1295 |
.cb = cb, |
| 1296 |
.arg = evarg, |
| 1297 |
}; |
| 1298 |
|
| 1299 |
asdata->rev = rev; |
| 1300 |
|
| 1301 |
*ctx = (ns_hookasync_t){ |
| 1302 |
.destroy = destroy_hookactx, |
| 1303 |
.cancel = cancel_e2ehookactx, |
| 1304 |
.private = asdata, |
| 1305 |
}; |
| 1306 |
isc_mem_attach(memctx, &ctx->mctx); |
| 1307 |
|
| 1308 |
*ctxp = ctx; |
| 1309 |
return ISC_R_SUCCESS; |
| 1310 |
} |
| 1311 |
|
| 1312 |
static ns_hookresult_t |
| 1313 |
hook_async_e2e(void *arg, void *data, isc_result_t *resultp) { |
| 1314 |
query_ctx_t *qctx = arg; |
| 1315 |
hookasync_e2e_data_t *asdata = data; |
| 1316 |
isc_result_t result; |
| 1317 |
|
| 1318 |
if (!asdata->async) { |
| 1319 |
/* Initial call to the hook; start async event */ |
| 1320 |
result = ns_query_hookasync(qctx, test_hookasync_e2e, asdata); |
| 1321 |
if (result != ISC_R_SUCCESS) { |
| 1322 |
*resultp = result; |
| 1323 |
return NS_HOOK_RETURN; |
| 1324 |
} |
| 1325 |
|
| 1326 |
asdata->async = true; |
| 1327 |
asdata->rev->origresult = *resultp; /* save it for resume */ |
| 1328 |
*resultp = ISC_R_UNSET; |
| 1329 |
return NS_HOOK_RETURN; |
| 1330 |
} else { |
| 1331 |
/* Resume from the completion of async event */ |
| 1332 |
asdata->async = false; |
| 1333 |
/* Don't touch 'resultp' */ |
| 1334 |
return NS_HOOK_CONTINUE; |
| 1335 |
} |
| 1336 |
} |
| 1337 |
|
| 1338 |
/* |
| 1339 |
* Check whether the final response has expected the RCODE according to |
| 1340 |
* the test scenario. |
| 1341 |
*/ |
| 1342 |
static ns_hookresult_t |
| 1343 |
hook_donesend(void *arg, void *data, isc_result_t *resultp) { |
| 1344 |
query_ctx_t *qctx = arg; |
| 1345 |
hookasync_e2e_data_t *asdata = data; |
| 1346 |
|
| 1347 |
INSIST(qctx->client->message->rcode == asdata->expected_rcode); |
| 1348 |
asdata->done = true; /* Let the test know this hook is called */ |
| 1349 |
*resultp = ISC_R_UNSET; |
| 1350 |
return NS_HOOK_CONTINUE; |
| 1351 |
} |
| 1352 |
|
| 1353 |
static void |
| 1354 |
run_hookasync_e2e_test(const ns__query_hookasync_e2e_test_params_t *test) { |
| 1355 |
query_ctx_t *qctx = NULL; |
| 1356 |
isc_result_t result; |
| 1357 |
hookasync_e2e_data_t asdata = { |
| 1358 |
.async = false, |
| 1359 |
.hookpoint = test->hookpoint, |
| 1360 |
.start_result = test->start_result, |
| 1361 |
.expected_rcode = test->expected_rcode, |
| 1362 |
.done = false, |
| 1363 |
}; |
| 1364 |
const ns_hook_t donesend_hook = { |
| 1365 |
.action = hook_donesend, |
| 1366 |
.action_data = &asdata, |
| 1367 |
}; |
| 1368 |
const ns_hook_t hook = { |
| 1369 |
.action = hook_async_e2e, |
| 1370 |
.action_data = &asdata, |
| 1371 |
}; |
| 1372 |
const ns_test_qctx_create_params_t qctx_params = { |
| 1373 |
.qname = test->qname, |
| 1374 |
.qtype = dns_rdatatype_a, |
| 1375 |
.with_cache = true, |
| 1376 |
}; |
| 1377 |
|
| 1378 |
ns__hook_table = NULL; |
| 1379 |
ns_hooktable_create(mctx, &ns__hook_table); |
| 1380 |
ns_hook_add(ns__hook_table, mctx, test->hookpoint, &hook); |
| 1381 |
ns_hook_add(ns__hook_table, mctx, NS_QUERY_DONE_SEND, &donesend_hook); |
| 1382 |
|
| 1383 |
result = ns_test_qctx_create(&qctx_params, &qctx); |
| 1384 |
INSIST(result == ISC_R_SUCCESS); |
| 1385 |
|
| 1386 |
isc_sockaddr_any(&qctx->client->peeraddr); /* for sortlist */ |
| 1387 |
qctx->client->sendcb = send_noop; |
| 1388 |
|
| 1389 |
/* Load a zone. it should have ns.foo/A */ |
| 1390 |
result = ns_test_serve_zone("foo", TESTS_DIR "/testdata/query/foo.db", |
| 1391 |
qctx->client->view); |
| 1392 |
INSIST(result == ISC_R_SUCCESS); |
| 1393 |
|
| 1394 |
/* |
| 1395 |
* We expect to have a response sent all cases, so we need to |
| 1396 |
* setup reqhandle (which will be detached on the send). |
| 1397 |
*/ |
| 1398 |
isc_nmhandle_attach(qctx->client->handle, &qctx->client->reqhandle); |
| 1399 |
|
| 1400 |
/* Handle the query. hook-based async event will be triggered. */ |
| 1401 |
qctx->client->state = NS_CLIENTSTATE_WORKING; |
| 1402 |
ns__query_start(qctx); |
| 1403 |
|
| 1404 |
/* If specified cancel the query at this point. */ |
| 1405 |
if (test->do_cancel) { |
| 1406 |
ns_query_cancel(qctx->client); |
| 1407 |
} |
| 1408 |
|
| 1409 |
if (test->start_result == ISC_R_SUCCESS) { |
| 1410 |
/* |
| 1411 |
* If async event has started, manually invoke the done event. |
| 1412 |
*/ |
| 1413 |
INSIST(asdata.async); |
| 1414 |
asdata.rev->cb(asdata.rev); |
| 1415 |
|
| 1416 |
/* |
| 1417 |
* Usually 'async' is reset to false on the 2nd call to |
| 1418 |
* the hook. But the hook isn't called if the query is |
| 1419 |
* canceled. |
| 1420 |
*/ |
| 1421 |
INSIST(asdata.done == !test->do_cancel); |
| 1422 |
INSIST(asdata.async == test->do_cancel); |
| 1423 |
} else { |
| 1424 |
INSIST(!asdata.async); |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* Cleanup */ |
| 1428 |
ns_test_qctx_destroy(&qctx); |
| 1429 |
ns_test_cleanup_zone(); |
| 1430 |
ns_hooktable_free(mctx, (void **)&ns__hook_table); |
| 1431 |
} |
| 1432 |
|
| 1433 |
ISC_LOOP_TEST_IMPL(ns__query_hookasync_e2e) { |
| 1434 |
const ns__query_hookasync_e2e_test_params_t tests[] = { |
| 1435 |
{ |
| 1436 |
NS_TEST_ID("positive answer"), |
| 1437 |
"ns.foo", |
| 1438 |
NS_QUERY_GOT_ANSWER_BEGIN, |
| 1439 |
ISC_R_SUCCESS, |
| 1440 |
false, |
| 1441 |
dns_rcode_noerror, |
| 1442 |
}, |
| 1443 |
{ |
| 1444 |
NS_TEST_ID("NXDOMAIN"), |
| 1445 |
"notexist.foo", |
| 1446 |
NS_QUERY_NXDOMAIN_BEGIN, |
| 1447 |
ISC_R_SUCCESS, |
| 1448 |
false, |
| 1449 |
dns_rcode_nxdomain, |
| 1450 |
}, |
| 1451 |
{ |
| 1452 |
NS_TEST_ID("async fail"), |
| 1453 |
"ns.foo", |
| 1454 |
NS_QUERY_DONE_BEGIN, |
| 1455 |
ISC_R_FAILURE, |
| 1456 |
false, |
| 1457 |
-1, |
| 1458 |
}, |
| 1459 |
{ |
| 1460 |
NS_TEST_ID("cancel query"), |
| 1461 |
"ns.foo", |
| 1462 |
NS_QUERY_DONE_BEGIN, |
| 1463 |
ISC_R_SUCCESS, |
| 1464 |
true, |
| 1465 |
-1, |
| 1466 |
}, |
| 1467 |
}; |
| 1468 |
|
| 1469 |
for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { |
| 1470 |
run_hookasync_e2e_test(&tests[i]); |
| 1471 |
} |
| 1472 |
|
| 1473 |
isc_loop_teardown(mainloop, shutdown_interfacemgr, NULL); |
| 1474 |
isc_loopmgr_shutdown(loopmgr); |
| 1475 |
} |
| 1476 |
|
| 1477 |
ISC_TEST_LIST_START |
| 1478 |
ISC_TEST_ENTRY_CUSTOM(ns__query_sfcache, setup_server, teardown_server) |
| 1479 |
ISC_TEST_ENTRY_CUSTOM(ns__query_start, setup_server, teardown_server) |
| 1480 |
ISC_TEST_ENTRY_CUSTOM(ns__query_hookasync, setup_server, teardown_server) |
| 1481 |
ISC_TEST_ENTRY_CUSTOM(ns__query_hookasync_e2e, setup_server, teardown_server) |
| 1482 |
ISC_TEST_LIST_END |
| 1483 |
|
| 1484 |
ISC_TEST_MAIN |