2013-12-17 09:08:59 +11:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2013-12-17 09:08:59 +11:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10: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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://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.
|
2013-12-17 09:08:59 +11:00
|
|
|
*/
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
|
|
#include <setjmp.h>
|
2018-11-07 12:23:15 +07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
2013-12-17 09:08:59 +11:00
|
|
|
#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>
|
2013-12-17 09:08:59 +11:00
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2013-12-17 09:08:59 +11:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dbiterator.h>
|
|
|
|
#include <dns/journal.h>
|
2017-09-06 09:58:29 +10:00
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/rdatalist.h>
|
2013-12-17 09:08:59 +11:00
|
|
|
|
2022-05-03 11:37:31 +02:00
|
|
|
#include <tests/dns.h>
|
2013-12-17 09:08:59 +11:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define BUFLEN 255
|
|
|
|
#define BIGBUFLEN (64 * 1024)
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TEST_ORIGIN "test"
|
2013-12-17 09:08:59 +11:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Individual unit tests
|
|
|
|
*/
|
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
/* test multiple calls to dns_db_getoriginnode */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(getoriginnode) {
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_db_t *db = NULL;
|
2013-12-17 09:08:59 +11:00
|
|
|
dns_dbnode_t *node = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2013-12-17 09:08:59 +11:00
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
UNUSED(state);
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_rdataclass_in, 0, NULL, &db);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2013-12-17 09:08:59 +11:00
|
|
|
|
|
|
|
result = dns_db_getoriginnode(db, &node);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2013-12-17 09:08:59 +11:00
|
|
|
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);
|
2013-12-17 09:08:59 +11:00
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
/* test getservestalettl and setservestalettl */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(getsetservestalettl) {
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_db_t *db = NULL;
|
2017-09-06 09:58:29 +10:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_ttl_t ttl;
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
UNUSED(state);
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_cache,
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_rdataclass_in, 0, NULL, &db);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
ttl = 6 * 3600;
|
|
|
|
result = dns_db_setservestalettl(db, ttl);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
/* check DNS_DBFIND_STALEOK works */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(dns_dbfind_staleok) {
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_db_t *db = NULL;
|
|
|
|
dns_dbnode_t *node = NULL;
|
2017-09-06 09:58:29 +10:00
|
|
|
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;
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_rdatalist_t rdatalist;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdataset_t rdataset;
|
|
|
|
int count;
|
|
|
|
int pass;
|
|
|
|
isc_result_t result;
|
|
|
|
unsigned char data[] = { 0x0a, 0x00, 0x00, 0x01 };
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
UNUSED(state);
|
|
|
|
|
2019-06-18 15:01:43 +02:00
|
|
|
result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_cache,
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_rdataclass_in, 0, NULL, &db);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
example = dns_fixedname_initname(&example_fixed);
|
|
|
|
found = dns_fixedname_initname(&found_fixed);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
result = dns_name_fromstring(example, "example", 0, NULL);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2017-09-06 09:58:29 +10:00
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_findnode(db, example, true, &node);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
dns_rdataset_disassociate(&rdataset);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
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);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* May loop for up to 2 seconds performing non stale lookups.
|
|
|
|
*/
|
|
|
|
count = 0;
|
|
|
|
do {
|
|
|
|
count++;
|
2020-05-18 13:45:10 +10:00
|
|
|
assert_in_range(count, 1, 21); /* loop sanity */
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(rdataset.attributes &
|
2020-02-12 13:59:18 +01:00
|
|
|
DNS_RDATASETATTR_STALE,
|
|
|
|
0);
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_true(rdataset.ttl > 0);
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
dns_rdataset_disassociate(&rdataset);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
usleep(100000); /* 100 ms */
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_db_find(db, example, NULL, dns_rdatatype_a,
|
|
|
|
0, 0, &node, found, &rdataset,
|
|
|
|
NULL);
|
2017-09-06 09:58:29 +10:00
|
|
|
} while (result == ISC_R_SUCCESS);
|
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
2017-09-06 09:58:29 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether we can get stale data.
|
|
|
|
*/
|
|
|
|
result = dns_db_find(db, example, NULL, dns_rdatatype_a,
|
2020-02-12 13:59:18 +01:00
|
|
|
DNS_DBFIND_STALEOK, 0, &node, found,
|
|
|
|
&rdataset, NULL);
|
2017-09-06 09:58:29 +10:00
|
|
|
switch (pass) {
|
|
|
|
case 0:
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
2017-09-06 09:58:29 +10:00
|
|
|
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 &
|
2020-02-12 13:59:18 +01:00
|
|
|
DNS_RDATASETATTR_STALE,
|
|
|
|
DNS_RDATASETATTR_STALE);
|
2017-09-06 09:58:29 +10:00
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
dns_rdataset_disassociate(&rdataset);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
usleep(100000); /* 100 ms */
|
2017-09-06 09:58:29 +10:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_db_find(
|
|
|
|
db, example, NULL, dns_rdatatype_a,
|
|
|
|
DNS_DBFIND_STALEOK, 0, &node, found,
|
|
|
|
&rdataset, NULL);
|
2017-09-06 09:58:29 +10:00
|
|
|
} while (result == ISC_R_SUCCESS);
|
2021-06-09 23:54:14 +10:00
|
|
|
/*
|
|
|
|
* 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);
|
2017-09-06 09:58:29 +10:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-11-07 12:23:15 +07:00
|
|
|
assert_int_equal(result, ISC_R_NOTFOUND);
|
2017-09-06 09:58:29 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
2018-11-07 12:23:15 +07:00
|
|
|
/* database class */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(class) {
|
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
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_db_create(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
|
|
|
|
2020-02-12 13:59:18 +01: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 */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(dbtype) {
|
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 */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_db_create(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);
|
2020-02-12 13:59:18 +01: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);
|
|
|
|
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 */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
result = dns_db_create(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);
|
2020-02-12 13:59:18 +01: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);
|
|
|
|
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 */
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_RUN_TEST_IMPL(version) {
|
2020-02-13 14:44:37 -08:00
|
|
|
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);
|
2018-03-28 14:38:09 +02:00
|
|
|
foundname = dns_fixedname_initname(&ffound);
|
2018-02-28 20:18:27 -08:00
|
|
|
dns_rdataset_init(&rdataset);
|
2020-02-12 13:59:18 +01:00
|
|
|
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);
|
2018-04-17 08:29:14 -07:00
|
|
|
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);
|
2018-03-28 14:38:09 +02:00
|
|
|
foundname = dns_fixedname_initname(&ffound);
|
2018-02-28 20:18:27 -08:00
|
|
|
dns_rdataset_init(&rdataset);
|
2020-02-12 13:59:18 +01:00
|
|
|
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
|
|
|
|
2020-02-20 14:49:36 -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
|
|
|
|
2018-04-17 08:29:14 -07: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);
|
2018-04-17 08:29:14 -07:00
|
|
|
dns_db_closeversion(db, &ver, false);
|
2018-02-28 20:18:27 -08:00
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_TEST_LIST_START
|
|
|
|
ISC_TEST_ENTRY(getoriginnode)
|
|
|
|
ISC_TEST_ENTRY(getsetservestalettl)
|
|
|
|
ISC_TEST_ENTRY(dns_dbfind_staleok)
|
|
|
|
ISC_TEST_ENTRY(class)
|
|
|
|
ISC_TEST_ENTRY(dbtype)
|
|
|
|
ISC_TEST_ENTRY(version)
|
|
|
|
ISC_TEST_LIST_END
|
2018-11-07 12:23:15 +07:00
|
|
|
|
Give the unit tests a big overhaul
The unit tests contain a lot of duplicated code and here's an attempt
to reduce code duplication.
This commit does several things:
1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake
conditionals.
2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test
implementations, test lists, and the main test routine, so we don't
have to repeat this all over again. The macros were modeled after
libuv test suite but adapted to cmocka as the test driver.
A simple example of a unit test would be:
ISC_RUN_TEST_IMPL(test1) { assert_true(true); }
ISC_TEST_LIST_START
ISC_TEST_ENTRY(test1)
ISC_TEST_LIST_END
ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?)
For more complicated examples including group setup and teardown
functions, and per-test setup and teardown functions.
3. The macros prefix the test functions and cmocka entries, so the name
of the test can now match the tested function name, and we don't have
to append `_test` because `run_test_` is automatically prepended to
the main test function, and `setup_test_` and `teardown_test_` is
prepended to setup and teardown function.
4. Update all the unit tests to use the new syntax and fix a few bits
here and there.
5. In the future, we can separate the test declarations and test
implementations which are going to greatly help with uncluttering the
bigger unit tests like doh_test and netmgr_test, because the test
implementations are not declared static (see `ISC_RUN_TEST_DECLARE`
and `ISC_RUN_TEST_IMPL` for more details.
NOTE: This heavily relies on preprocessor macros, but the result greatly
outweighs all the negatives of using the macros. There's less
duplicated code, the tests are more uniform and the implementation can
be more flexible.
2022-05-02 10:56:42 +02:00
|
|
|
ISC_TEST_MAIN
|