2017-10-11 15:02:50 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-10-11 15:02:50 -07:00
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2017-10-11 15:02:50 -07:00
|
|
|
*/
|
|
|
|
|
2019-11-13 09:29:04 +01:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#if HAVE_CMOCKA && !__SANITIZE_ADDRESS__
|
2018-10-24 10:02:49 -07:00
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
2018-10-24 10:02:49 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
#include <dns/badcache.h>
|
|
|
|
#include <dns/view.h>
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <ns/client.h>
|
2018-08-03 14:16:41 -07:00
|
|
|
#include <ns/hooks.h>
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <ns/query.h>
|
|
|
|
|
|
|
|
#include "nstest.h"
|
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-24 10:02:49 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = ns_test_begin(NULL, true);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-24 10:02:49 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
ns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
/*****
|
2020-02-13 21:48:23 +01:00
|
|
|
***** ns__query_sfcache() tests
|
|
|
|
*****/
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*%
|
|
|
|
* Structure containing parameters for ns__query_sfcache_test().
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-13 14:44:37 -08:00
|
|
|
const ns_test_id_t id; /* libns test identifier */
|
|
|
|
unsigned int qflags; /* query flags */
|
|
|
|
bool cache_entry_present; /* whether a SERVFAIL
|
|
|
|
* cache entry
|
|
|
|
* matching the query
|
|
|
|
* should be
|
|
|
|
* present */
|
|
|
|
uint32_t cache_entry_flags; /* NS_FAILCACHE_* flags to
|
|
|
|
* set for
|
|
|
|
* the SERVFAIL cache entry
|
|
|
|
* */
|
|
|
|
bool servfail_expected; /* whether a cached
|
|
|
|
* SERVFAIL is
|
|
|
|
* expected to be returned
|
|
|
|
* */
|
2017-10-11 15:02:50 -07:00
|
|
|
} ns__query_sfcache_test_params_t;
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Perform a single ns__query_sfcache() check using given parameters.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
run_sfcache_test(const ns__query_sfcache_test_params_t *test) {
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_t *query_hooks = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
query_ctx_t *qctx = NULL;
|
|
|
|
isc_result_t result;
|
2018-09-19 23:38:23 -07:00
|
|
|
const ns_hook_t hook = {
|
2018-08-13 21:08:08 -07:00
|
|
|
.action = ns_test_hook_catch_call,
|
2018-08-12 11:19:36 -07:00
|
|
|
};
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
REQUIRE(test != NULL);
|
|
|
|
REQUIRE(test->id.description != NULL);
|
2018-04-17 08:29:14 -07:00
|
|
|
REQUIRE(test->cache_entry_present == true ||
|
2017-10-11 15:02:50 -07:00
|
|
|
test->cache_entry_flags == 0);
|
|
|
|
|
|
|
|
/*
|
2018-08-10 15:54:14 -07:00
|
|
|
* Interrupt execution if ns_query_done() is called.
|
2017-10-11 15:02:50 -07:00
|
|
|
*/
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_create(mctx, &query_hooks);
|
|
|
|
ns_hook_add(query_hooks, mctx, NS_QUERY_DONE_BEGIN, &hook);
|
|
|
|
ns__hook_table = query_hooks;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a query context for a ./NS query with given flags.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
const ns_test_qctx_create_params_t qctx_params = {
|
|
|
|
.qname = ".",
|
|
|
|
.qtype = dns_rdatatype_ns,
|
|
|
|
.qflags = test->qflags,
|
2018-04-17 08:29:14 -07:00
|
|
|
.with_cache = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
result = ns_test_qctx_create(&qctx_params, &qctx);
|
2018-10-24 10:02:49 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this test wants a SERVFAIL cache entry matching the query to
|
|
|
|
* exist, create it.
|
|
|
|
*/
|
|
|
|
if (test->cache_entry_present) {
|
|
|
|
isc_interval_t hour;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_time_t expire;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
isc_interval_set(&hour, 3600, 0);
|
|
|
|
result = isc_time_nowplusinterval(&expire, &hour);
|
2018-10-24 10:02:49 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
dns_badcache_add(qctx->client->view->failcache, dns_rootname,
|
2018-04-17 08:29:14 -07:00
|
|
|
dns_rdatatype_ns, true,
|
2017-10-11 15:02:50 -07:00
|
|
|
test->cache_entry_flags, &expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether ns__query_sfcache() behaves as expected.
|
|
|
|
*/
|
|
|
|
ns__query_sfcache(qctx);
|
|
|
|
|
|
|
|
if (test->servfail_expected) {
|
2018-10-24 10:02:49 -07:00
|
|
|
if (qctx->result != DNS_R_SERVFAIL) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"expected SERVFAIL, got %s",
|
|
|
|
test->id.description, test->id.lineno,
|
|
|
|
isc_result_totext(qctx->result));
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
} else {
|
2018-10-24 10:02:49 -07:00
|
|
|
if (qctx->result != ISC_R_SUCCESS) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"expected success, got %s",
|
|
|
|
test->id.description, test->id.lineno,
|
|
|
|
isc_result_totext(qctx->result));
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
ns_test_qctx_destroy(&qctx);
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_free(mctx, (void **)&query_hooks);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
/* test ns__query_sfcache() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
ns__query_sfcache_test(void **state) {
|
2017-10-11 15:02:50 -07:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const ns__query_sfcache_test_params_t tests[] = {
|
|
|
|
/*
|
|
|
|
* Sanity check for an empty SERVFAIL cache.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=1, CD=0; cache: empty"),
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = false,
|
|
|
|
.servfail_expected = false,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Query: RD=1, CD=0. Cache entry: CD=0. Should SERVFAIL.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=1, CD=0; cache: CD=0"),
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.cache_entry_flags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.servfail_expected = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Query: RD=1, CD=1. Cache entry: CD=0. Should not SERVFAIL:
|
|
|
|
* failed validation should not influence CD=1 queries.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=1, CD=1; cache: CD=0"),
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD | DNS_MESSAGEFLAG_CD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.cache_entry_flags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.servfail_expected = false,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Query: RD=1, CD=1. Cache entry: CD=1. Should SERVFAIL:
|
|
|
|
* SERVFAIL responses elicited by CD=1 queries can be
|
|
|
|
* "replayed" for other CD=1 queries during the lifetime of the
|
|
|
|
* SERVFAIL cache entry.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=1, CD=1; cache: CD=1"),
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD | DNS_MESSAGEFLAG_CD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.cache_entry_flags = NS_FAILCACHE_CD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.servfail_expected = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Query: RD=1, CD=0. Cache entry: CD=1. Should SERVFAIL: if
|
|
|
|
* a CD=1 query elicited a SERVFAIL, a CD=0 query for the same
|
|
|
|
* QNAME and QTYPE will SERVFAIL as well.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=1, CD=0; cache: CD=1"),
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.cache_entry_flags = NS_FAILCACHE_CD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.servfail_expected = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Query: RD=0, CD=0. Cache entry: CD=0. Should not SERVFAIL
|
|
|
|
* despite a matching entry being present as the SERVFAIL cache
|
|
|
|
* should not be consulted for non-recursive queries.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("query: RD=0, CD=0; cache: CD=0"),
|
|
|
|
.qflags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.cache_entry_present = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.cache_entry_flags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.servfail_expected = false,
|
2017-10-11 15:02:50 -07:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
UNUSED(state);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
2018-10-24 10:02:49 -07:00
|
|
|
run_sfcache_test(&tests[i]);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****
|
2020-02-13 21:48:23 +01:00
|
|
|
***** ns__query_start() tests
|
|
|
|
*****/
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*%
|
|
|
|
* Structure containing parameters for ns__query_start_test().
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-13 14:44:37 -08:00
|
|
|
const ns_test_id_t id; /* libns test identifier */
|
|
|
|
const char *qname; /* QNAME */
|
|
|
|
dns_rdatatype_t qtype; /* QTYPE */
|
|
|
|
unsigned int qflags; /* query flags */
|
|
|
|
bool disable_name_checks; /* if set to true, owner
|
|
|
|
* name
|
|
|
|
* checks will
|
|
|
|
* be disabled for the
|
|
|
|
* view created
|
|
|
|
* */
|
|
|
|
bool recursive_service; /* if set to true, the view
|
|
|
|
* created will
|
|
|
|
* have a cache
|
|
|
|
* database
|
|
|
|
* attached */
|
|
|
|
const char *auth_zone_origin; /* origin name of the zone
|
|
|
|
* the
|
|
|
|
* created view will be
|
|
|
|
* authoritative for */
|
|
|
|
const char *auth_zone_path; /* path to load the
|
|
|
|
* authoritative
|
|
|
|
* zone from */
|
|
|
|
enum { /* expected result: */
|
2020-02-12 13:59:18 +01:00
|
|
|
NS__QUERY_START_R_INVALID,
|
|
|
|
NS__QUERY_START_R_REFUSE, /* query should be REFUSED */
|
|
|
|
NS__QUERY_START_R_CACHE, /* query should be answered from
|
2020-02-13 21:48:23 +01:00
|
|
|
* cache */
|
2020-02-12 13:59:18 +01:00
|
|
|
NS__QUERY_START_R_AUTH, /* query should be answered using
|
2020-02-13 21:48:23 +01:00
|
|
|
* authoritative data */
|
2017-10-11 15:02:50 -07:00
|
|
|
} expected_result;
|
|
|
|
} ns__query_start_test_params_t;
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Perform a single ns__query_start() check using given parameters.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
run_start_test(const ns__query_start_test_params_t *test) {
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_t *query_hooks = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
query_ctx_t *qctx = NULL;
|
|
|
|
isc_result_t result;
|
2018-09-19 23:38:23 -07:00
|
|
|
const ns_hook_t hook = {
|
2018-08-13 21:08:08 -07:00
|
|
|
.action = ns_test_hook_catch_call,
|
2018-08-12 11:19:36 -07:00
|
|
|
};
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
REQUIRE(test != NULL);
|
|
|
|
REQUIRE(test->id.description != NULL);
|
|
|
|
REQUIRE((test->auth_zone_origin == NULL &&
|
|
|
|
test->auth_zone_path == NULL) ||
|
|
|
|
(test->auth_zone_origin != NULL &&
|
|
|
|
test->auth_zone_path != NULL));
|
|
|
|
|
|
|
|
/*
|
2018-08-10 15:54:14 -07:00
|
|
|
* Interrupt execution if query_lookup() or ns_query_done() is called.
|
2017-10-11 15:02:50 -07:00
|
|
|
*/
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_create(mctx, &query_hooks);
|
|
|
|
ns_hook_add(query_hooks, mctx, NS_QUERY_LOOKUP_BEGIN, &hook);
|
|
|
|
ns_hook_add(query_hooks, mctx, NS_QUERY_DONE_BEGIN, &hook);
|
|
|
|
ns__hook_table = query_hooks;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct a query context using the supplied parameters.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
const ns_test_qctx_create_params_t qctx_params = {
|
|
|
|
.qname = test->qname,
|
|
|
|
.qtype = test->qtype,
|
|
|
|
.qflags = test->qflags,
|
|
|
|
.with_cache = test->recursive_service,
|
|
|
|
};
|
|
|
|
result = ns_test_qctx_create(&qctx_params, &qctx);
|
2018-10-24 10:02:49 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enable view->checknames by default, disable if requested.
|
|
|
|
*/
|
|
|
|
qctx->client->view->checknames = !test->disable_name_checks;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load zone from file and attach it to the client's view, if
|
|
|
|
* requested.
|
|
|
|
*/
|
|
|
|
if (test->auth_zone_path != NULL) {
|
|
|
|
result = ns_test_serve_zone(test->auth_zone_origin,
|
|
|
|
test->auth_zone_path,
|
|
|
|
qctx->client->view);
|
2018-10-24 10:02:49 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether ns__query_start() behaves as expected.
|
|
|
|
*/
|
|
|
|
ns__query_start(qctx);
|
|
|
|
|
|
|
|
switch (test->expected_result) {
|
|
|
|
case NS__QUERY_START_R_REFUSE:
|
2018-10-24 10:02:49 -07:00
|
|
|
if (qctx->result != DNS_R_REFUSED) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"expected REFUSED, got %s",
|
|
|
|
test->id.description, test->id.lineno,
|
|
|
|
isc_result_totext(qctx->result));
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
|
|
|
if (qctx->zone != NULL) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"no zone was expected to be attached to "
|
|
|
|
"query context, but some was",
|
|
|
|
test->id.description, test->id.lineno);
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
|
|
|
if (qctx->db != NULL) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"no database was expected to be attached to "
|
|
|
|
"query context, but some was",
|
|
|
|
test->id.description, test->id.lineno);
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
break;
|
|
|
|
case NS__QUERY_START_R_CACHE:
|
2018-10-24 10:02:49 -07:00
|
|
|
if (qctx->result != ISC_R_SUCCESS) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"expected success, got %s",
|
|
|
|
test->id.description, test->id.lineno,
|
|
|
|
isc_result_totext(qctx->result));
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
|
|
|
if (qctx->zone != NULL) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"no zone was expected to be attached to "
|
|
|
|
"query context, but some was",
|
|
|
|
test->id.description, test->id.lineno);
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
2020-02-13 14:44:37 -08:00
|
|
|
if (qctx->db == NULL || qctx->db != qctx->client->view->cachedb)
|
|
|
|
{
|
2018-10-24 10:02:49 -07:00
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
|
|
"cache database was expected to be "
|
|
|
|
"attached to query context, but it was not",
|
|
|
|
test->id.description, test->id.lineno);
|
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
break;
|
|
|
|
case NS__QUERY_START_R_AUTH:
|
2018-10-24 10:02:49 -07:00
|
|
|
if (qctx->result != ISC_R_SUCCESS) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
2017-10-11 15:02:50 -07:00
|
|
|
"expected success, got %s",
|
|
|
|
test->id.description, test->id.lineno,
|
|
|
|
isc_result_totext(qctx->result));
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
|
|
|
if (qctx->zone == NULL) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
|
|
"a zone was expected to be attached to query "
|
|
|
|
"context, but it was not",
|
|
|
|
test->id.description, test->id.lineno);
|
|
|
|
}
|
|
|
|
if (qctx->db == qctx->client->view->cachedb) {
|
|
|
|
fail_msg("# test \"%s\" on line %d: "
|
|
|
|
"cache database was not expected to be "
|
|
|
|
"attached to query context, but it is",
|
|
|
|
test->id.description, test->id.lineno);
|
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
break;
|
|
|
|
case NS__QUERY_START_R_INVALID:
|
2018-10-24 10:02:49 -07:00
|
|
|
fail_msg("# test \"%s\" on line %d has no expected result set",
|
|
|
|
test->id.description, test->id.lineno);
|
2017-10-11 15:02:50 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
INSIST(0);
|
2018-11-07 15:00:07 +07:00
|
|
|
ISC_UNREACHABLE();
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
if (test->auth_zone_path != NULL) {
|
|
|
|
ns_test_cleanup_zone();
|
|
|
|
}
|
|
|
|
ns_test_qctx_destroy(&qctx);
|
2018-09-19 23:38:23 -07:00
|
|
|
ns_hooktable_free(mctx, (void **)&query_hooks);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
/* test ns__query_start() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
ns__query_start_test(void **state) {
|
2017-10-11 15:02:50 -07:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const ns__query_start_test_params_t tests[] = {
|
|
|
|
/*
|
|
|
|
* Recursive foo/A query to a server without recursive service
|
|
|
|
* and no zones configured. Query should be REFUSED.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("foo/A, no cache, no auth"),
|
|
|
|
.qname = "foo",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = false,
|
2017-10-11 15:02:50 -07:00
|
|
|
.expected_result = NS__QUERY_START_R_REFUSE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive foo/A query to a server with recursive service and
|
|
|
|
* no zones configured. Query should be answered from cache.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("foo/A, cache, no auth"),
|
|
|
|
.qname = "foo",
|
|
|
|
.qtype = dns_rdatatype_a,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.expected_result = NS__QUERY_START_R_CACHE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive foo/A query to a server with recursive service and
|
|
|
|
* zone "foo" configured. Query should be answered from
|
|
|
|
* authoritative data.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("foo/A, RD=1, cache, auth for foo"),
|
|
|
|
.qname = "foo",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_AUTH,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive bar/A query to a server without recursive service
|
|
|
|
* and zone "foo" configured. Query should be REFUSED.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("bar/A, RD=1, no cache, auth for foo"),
|
|
|
|
.qname = "bar",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = false,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_REFUSE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive bar/A query to a server with recursive service and
|
|
|
|
* zone "foo" configured. Query should be answered from
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("bar/A, RD=1, cache, auth for foo"),
|
|
|
|
.qname = "bar",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_CACHE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive bar.foo/DS query to a server with recursive
|
|
|
|
* service and zone "foo" configured. Query should be answered
|
|
|
|
* from authoritative data.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("bar.foo/DS, RD=1, cache, auth for foo"),
|
|
|
|
.qname = "bar.foo",
|
|
|
|
.qtype = dns_rdatatype_ds,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_AUTH,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Non-recursive bar.foo/DS query to a server with recursive
|
|
|
|
* service and zone "foo" configured. Query should be answered
|
|
|
|
* from authoritative data.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("bar.foo/DS, RD=0, cache, auth for foo"),
|
|
|
|
.qname = "bar.foo",
|
|
|
|
.qtype = dns_rdatatype_ds,
|
|
|
|
.qflags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_AUTH,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive foo/DS query to a server with recursive service
|
|
|
|
* and zone "foo" configured. Query should be answered from
|
|
|
|
* cache.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("foo/DS, RD=1, cache, auth for foo"),
|
|
|
|
.qname = "foo",
|
|
|
|
.qtype = dns_rdatatype_ds,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_CACHE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Non-recursive foo/DS query to a server with recursive
|
|
|
|
* service and zone "foo" configured. Query should be answered
|
|
|
|
* from authoritative data.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("foo/DS, RD=0, cache, auth for foo"),
|
|
|
|
.qname = "foo",
|
|
|
|
.qtype = dns_rdatatype_ds,
|
|
|
|
.qflags = 0,
|
2018-04-17 08:29:14 -07:00
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.auth_zone_origin = "foo",
|
|
|
|
.auth_zone_path = "testdata/query/foo.db",
|
|
|
|
.expected_result = NS__QUERY_START_R_AUTH,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive _foo/A query to a server with recursive service,
|
|
|
|
* no zones configured and owner name checks disabled. Query
|
|
|
|
* should be answered from cache.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("_foo/A, cache, no auth, name checks off"),
|
|
|
|
.qname = "_foo",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.disable_name_checks = true,
|
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.expected_result = NS__QUERY_START_R_CACHE,
|
|
|
|
},
|
|
|
|
/*
|
|
|
|
* Recursive _foo/A query to a server with recursive service,
|
|
|
|
* no zones configured and owner name checks enabled. Query
|
|
|
|
* should be REFUSED.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
NS_TEST_ID("_foo/A, cache, no auth, name checks on"),
|
|
|
|
.qname = "_foo",
|
|
|
|
.qtype = dns_rdatatype_a,
|
|
|
|
.qflags = DNS_MESSAGEFLAG_RD,
|
2018-04-17 08:29:14 -07:00
|
|
|
.disable_name_checks = false,
|
|
|
|
.recursive_service = true,
|
2017-10-11 15:02:50 -07:00
|
|
|
.expected_result = NS__QUERY_START_R_REFUSE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
UNUSED(state);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
2018-10-24 10:02:49 -07:00
|
|
|
run_start_test(&tests[i]);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
2018-10-24 10:02:49 -07:00
|
|
|
}
|
2017-10-11 15:02:50 -07:00
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 10:02:49 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(ns__query_sfcache_test, _setup,
|
|
|
|
_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(ns__query_start_test, _setup,
|
|
|
|
_teardown),
|
2018-10-24 10:02:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
2019-11-13 09:29:04 +01:00
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA && !__SANITIZE_ADDRESS__ */
|
2017-10-11 15:02:50 -07:00
|
|
|
|
2018-10-24 10:02:49 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2019-11-13 09:29:04 +01:00
|
|
|
#if __SANITIZE_ADDRESS__
|
|
|
|
/*
|
|
|
|
* We disable this test when the address sanitizer is in
|
|
|
|
* the use, as libuv will trigger errors.
|
|
|
|
*/
|
|
|
|
printf("1..0 # Skip ASAN is in use\n");
|
2020-02-12 13:59:18 +01:00
|
|
|
#else /* ADDRESS_SANIZITER */
|
2019-11-05 15:34:35 -08:00
|
|
|
printf("1..0 # Skip cmocka not available\n");
|
2019-11-13 09:29:04 +01:00
|
|
|
#endif /* __SANITIZE_ADDRESS__ */
|
2018-10-24 10:02:49 -07:00
|
|
|
return (0);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
2018-10-24 10:02:49 -07:00
|
|
|
|
2019-11-13 09:29:04 +01:00
|
|
|
#endif /* HAVE_CMOCKA && !__SANITIZE_ADDRESS__ */
|