2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/lib/dns/tests/db_test.c

427 lines
11 KiB
C
Raw Normal View History

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* 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 https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
2018-11-07 12:23:15 +07:00
#if HAVE_CMOCKA
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
2018-11-07 12:23:15 +07:00
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.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 <unistd.h>
2018-11-07 12:23:15 +07:00
#define UNIT_TESTING
#include <cmocka.h>
#include <dns/db.h>
#include <dns/dbiterator.h>
#include <dns/journal.h>
#include <dns/name.h>
#include <dns/rdatalist.h>
#include "dnstest.h"
2018-11-07 12:23:15 +07:00
static int
2020-02-13 14:44:37 -08:00
_setup(void **state) {
2018-11-07 12:23:15 +07:00
isc_result_t result;
UNUSED(state);
result = dns_test_begin(NULL, false);
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
}
static int
2020-02-13 14:44:37 -08:00
_teardown(void **state) {
2018-11-07 12:23:15 +07:00
UNUSED(state);
dns_test_end();
return (0);
}
2020-02-13 14:44:37 -08:00
#define BUFLEN 255
#define BIGBUFLEN (64 * 1024)
#define TEST_ORIGIN "test"
/*
* Individual unit tests
*/
2018-11-07 12:23:15 +07:00
/* test multiple calls to dns_db_getoriginnode */
static void
2020-02-13 14:44:37 -08:00
getoriginnode_test(void **state) {
dns_db_t *db = NULL;
dns_dbnode_t *node = NULL;
2020-02-13 14:44:37 -08:00
isc_mem_t *mctx = NULL;
isc_result_t result;
2018-11-07 12:23:15 +07:00
UNUSED(state);
isc_mem_create(&mctx);
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_db_getoriginnode(db, &node);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
dns_db_detachnode(db, &node);
result = dns_db_getoriginnode(db, &node);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
dns_db_detachnode(db, &node);
dns_db_detach(&db);
isc_mem_detach(&mctx);
}
2018-11-07 12:23:15 +07:00
/* test getservestalettl and setservestalettl */
static void
2020-02-13 14:44:37 -08:00
getsetservestalettl_test(void **state) {
dns_db_t *db = NULL;
isc_mem_t *mctx = NULL;
isc_result_t result;
2020-02-13 14:44:37 -08:00
dns_ttl_t ttl;
2018-11-07 12:23:15 +07:00
UNUSED(state);
isc_mem_create(&mctx);
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_cache,
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
ttl = 5000;
result = dns_db_getservestalettl(db, &ttl);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(ttl, 0);
ttl = 6 * 3600;
result = dns_db_setservestalettl(db, ttl);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
ttl = 5000;
result = dns_db_getservestalettl(db, &ttl);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(ttl, 6 * 3600);
dns_db_detach(&db);
isc_mem_detach(&mctx);
}
2018-11-07 12:23:15 +07:00
/* check DNS_DBFIND_STALEOK works */
static void
2020-02-13 14:44:37 -08:00
dns_dbfind_staleok_test(void **state) {
dns_db_t *db = NULL;
dns_dbnode_t *node = NULL;
dns_fixedname_t example_fixed;
dns_fixedname_t found_fixed;
2020-02-13 14:44:37 -08:00
dns_name_t *example;
dns_name_t *found;
dns_rdatalist_t rdatalist;
2020-02-13 14:44:37 -08:00
dns_rdataset_t rdataset;
int count;
int pass;
isc_mem_t *mctx = NULL;
isc_result_t result;
unsigned char data[] = { 0x0a, 0x00, 0x00, 0x01 };
2018-11-07 12:23:15 +07:00
UNUSED(state);
isc_mem_create(&mctx);
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_cache,
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
example = dns_fixedname_initname(&example_fixed);
found = dns_fixedname_initname(&found_fixed);
result = dns_name_fromstring(example, "example", 0, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
/*
* Pass 0: default; no stale processing permitted.
* Pass 1: stale processing for 1 second.
* Pass 2: stale turned off after being on.
*/
for (pass = 0; pass < 3; pass++) {
dns_rdata_t rdata = DNS_RDATA_INIT;
/* 10.0.0.1 */
rdata.data = data;
rdata.length = 4;
rdata.rdclass = dns_rdataclass_in;
rdata.type = dns_rdatatype_a;
dns_rdatalist_init(&rdatalist);
rdatalist.ttl = 2;
rdatalist.type = dns_rdatatype_a;
rdatalist.rdclass = dns_rdataclass_in;
ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
switch (pass) {
case 0:
/* default: stale processing off */
break;
case 1:
/* turn on stale processing */
result = dns_db_setservestalettl(db, 1);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
break;
case 2:
/* turn off stale processing */
result = dns_db_setservestalettl(db, 0);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
break;
}
dns_rdataset_init(&rdataset);
result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_db_findnode(db, example, true, &node);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_db_addrdataset(db, node, NULL, 0, &rdataset, 0,
NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
dns_db_detachnode(db, &node);
dns_rdataset_disassociate(&rdataset);
result = dns_db_find(db, example, NULL, dns_rdatatype_a, 0, 0,
&node, found, &rdataset, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
/*
* May loop for up to 2 seconds performing non stale lookups.
*/
count = 0;
do {
count++;
assert_in_range(count, 1, 21); /* loop sanity */
2018-11-07 12:23:15 +07:00
assert_int_equal(rdataset.attributes &
DNS_RDATASETATTR_STALE,
0);
2018-11-07 12:23:15 +07:00
assert_true(rdataset.ttl > 0);
dns_db_detachnode(db, &node);
dns_rdataset_disassociate(&rdataset);
usleep(100000); /* 100 ms */
result = dns_db_find(db, example, NULL, dns_rdatatype_a,
0, 0, &node, found, &rdataset,
NULL);
} while (result == ISC_R_SUCCESS);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_NOTFOUND);
/*
* Check whether we can get stale data.
*/
result = dns_db_find(db, example, NULL, dns_rdatatype_a,
DNS_DBFIND_STALEOK, 0, &node, found,
&rdataset, NULL);
switch (pass) {
case 0:
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_NOTFOUND);
break;
case 1:
/*
* Should loop for 1 second with stale lookups then
* stop.
*/
count = 0;
do {
count++;
2018-11-07 12:23:15 +07:00
assert_in_range(count, 0, 49); /* loop sanity */
assert_int_equal(result, ISC_R_SUCCESS);
assert_int_equal(rdataset.attributes &
DNS_RDATASETATTR_STALE,
DNS_RDATASETATTR_STALE);
dns_db_detachnode(db, &node);
dns_rdataset_disassociate(&rdataset);
usleep(100000); /* 100 ms */
result = dns_db_find(
db, example, NULL, dns_rdatatype_a,
DNS_DBFIND_STALEOK, 0, &node, found,
&rdataset, NULL);
} while (result == ISC_R_SUCCESS);
/*
* usleep(100000) can be slightly less than 10ms so
* allow the count to reach 11.
*/
assert_in_range(count, 1, 11);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_NOTFOUND);
break;
case 2:
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_NOTFOUND);
break;
}
}
dns_db_detach(&db);
isc_mem_detach(&mctx);
}
2018-11-07 12:23:15 +07:00
/* database class */
static void
2020-02-13 14:44:37 -08:00
class_test(void **state) {
2018-02-28 20:18:27 -08:00
isc_result_t result;
2020-02-13 14:44:37 -08:00
dns_db_t *db = NULL;
2018-02-28 20:18:27 -08:00
2018-11-07 12:23:15 +07:00
UNUSED(state);
2018-02-28 20:18:27 -08:00
result = dns_db_create(dt_mctx, "rbt", dns_rootname, dns_dbtype_zone,
2018-02-28 20:18:27 -08:00
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text,
0);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
2018-11-07 12:23:15 +07:00
assert_int_equal(dns_db_class(db), dns_rdataclass_in);
2018-02-28 20:18:27 -08:00
dns_db_detach(&db);
}
2018-11-07 12:23:15 +07:00
/* database type */
static void
2020-02-13 14:44:37 -08:00
dbtype_test(void **state) {
2018-02-28 20:18:27 -08:00
isc_result_t result;
2020-02-13 14:44:37 -08:00
dns_db_t *db = NULL;
2018-02-28 20:18:27 -08:00
2018-11-07 12:23:15 +07:00
UNUSED(state);
2018-02-28 20:18:27 -08:00
/* DB has zone semantics */
result = dns_db_create(dt_mctx, "rbt", dns_rootname, dns_dbtype_zone,
2018-02-28 20:18:27 -08:00
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text,
0);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(dns_db_iszone(db));
assert_false(dns_db_iscache(db));
2018-02-28 20:18:27 -08:00
dns_db_detach(&db);
/* DB has cache semantics */
result = dns_db_create(dt_mctx, "rbt", dns_rootname, dns_dbtype_cache,
2018-02-28 20:18:27 -08:00
dns_rdataclass_in, 0, NULL, &db);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dns_db_load(db, "testdata/db/data.db", dns_masterformat_text,
0);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
assert_true(dns_db_iscache(db));
assert_false(dns_db_iszone(db));
2018-02-28 20:18:27 -08:00
dns_db_detach(&db);
}
2018-11-07 12:23:15 +07:00
/* database versions */
static void
2020-02-13 14:44:37 -08:00
version_test(void **state) {
isc_result_t result;
dns_fixedname_t fname, ffound;
dns_name_t *name, *foundname;
dns_db_t *db = NULL;
2018-02-28 20:18:27 -08:00
dns_dbversion_t *ver = NULL, *new = NULL;
2020-02-13 14:44:37 -08:00
dns_dbnode_t *node = NULL;
dns_rdataset_t rdataset;
2018-02-28 20:18:27 -08:00
2018-11-07 12:23:15 +07:00
UNUSED(state);
2018-02-28 20:18:27 -08:00
result = dns_test_loaddb(&db, dns_dbtype_zone, "test.test",
"testdata/db/data.db");
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
/* Open current version for reading */
dns_db_currentversion(db, &ver);
dns_test_namefromstring("b.test.test", &fname);
name = dns_fixedname_name(&fname);
foundname = dns_fixedname_initname(&ffound);
2018-02-28 20:18:27 -08:00
dns_rdataset_init(&rdataset);
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node,
2018-02-28 20:18:27 -08:00
foundname, &rdataset, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
dns_rdataset_disassociate(&rdataset);
dns_db_detachnode(db, &node);
dns_db_closeversion(db, &ver, false);
2018-02-28 20:18:27 -08:00
/* Open new version for writing */
dns_db_currentversion(db, &ver);
dns_test_namefromstring("b.test.test", &fname);
name = dns_fixedname_name(&fname);
foundname = dns_fixedname_initname(&ffound);
2018-02-28 20:18:27 -08:00
dns_rdataset_init(&rdataset);
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node,
2018-02-28 20:18:27 -08:00
foundname, &rdataset, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
result = dns_db_newversion(db, &new);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
/* Delete the rdataset from the new version */
2018-02-28 20:18:27 -08:00
result = dns_db_deleterdataset(db, node, new, dns_rdatatype_a, 0);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
dns_rdataset_disassociate(&rdataset);
dns_db_detachnode(db, &node);
/* This should fail now */
result = dns_db_find(db, name, new, dns_rdatatype_a, 0, 0, &node,
foundname, &rdataset, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, DNS_R_NXDOMAIN);
2018-02-28 20:18:27 -08:00
dns_db_closeversion(db, &new, true);
2018-02-28 20:18:27 -08:00
/* But this should still succeed */
result = dns_db_find(db, name, ver, dns_rdatatype_a, 0, 0, &node,
foundname, &rdataset, NULL);
2018-11-07 12:23:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-02-28 20:18:27 -08:00
dns_rdataset_disassociate(&rdataset);
dns_db_detachnode(db, &node);
dns_db_closeversion(db, &ver, false);
2018-02-28 20:18:27 -08:00
dns_db_detach(&db);
}
2018-11-07 12:23:15 +07:00
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-11-07 12:23:15 +07:00
const struct CMUnitTest tests[] = {
cmocka_unit_test(getoriginnode_test),
cmocka_unit_test(getsetservestalettl_test),
cmocka_unit_test(dns_dbfind_staleok_test),
cmocka_unit_test_setup_teardown(class_test, _setup, _teardown),
cmocka_unit_test_setup_teardown(dbtype_test, _setup, _teardown),
cmocka_unit_test_setup_teardown(version_test, _setup,
_teardown),
2018-11-07 12:23:15 +07:00
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
2018-11-07 12:23:15 +07:00
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-11-07 12:23:15 +07:00
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
2018-11-07 12:23:15 +07:00
}
#endif /* if HAVE_CMOCKA */