Stop the unit tests from running twice
Move the libtest code into a 'libtest' subdirectory and make it one of the SUBDIRS in the tests Makefile. having it at the top level required having "." as one of the subdirs, and that caused the unit tests to be executed twice.
This commit is contained in:
28
tests/libtest/Makefile.am
Normal file
28
tests/libtest/Makefile.am
Normal file
@@ -0,0 +1,28 @@
|
||||
include $(top_srcdir)/Makefile.top
|
||||
|
||||
AM_CPPFLAGS += \
|
||||
$(LIBISC_CFLAGS) \
|
||||
$(LIBDNS_CFLAGS) \
|
||||
$(LIBNS_CFLAGS) \
|
||||
$(LIBUV_CFLAGS) \
|
||||
-I$(top_srcdir)/lib/isc
|
||||
|
||||
LDADD += \
|
||||
$(LIBISC_LIBS) \
|
||||
$(LIBDNS_LIBS) \
|
||||
$(LIBNS_LIBS)
|
||||
|
||||
check_LTLIBRARIES = libtest.la
|
||||
|
||||
noinst_libtest_ladir = ..
|
||||
noinst_libtest_la_HEADERS = \
|
||||
../include/tests/dns.h \
|
||||
../include/tests/isc.h \
|
||||
../include/tests/ns.h
|
||||
libtest_la_SOURCES = \
|
||||
$(noinst_libtest_la_HEADERS) \
|
||||
dns.c \
|
||||
isc.c \
|
||||
ns.c
|
||||
|
||||
include $(top_srcdir)/Makefile.tests
|
||||
496
tests/libtest/dns.c
Normal file
496
tests/libtest/dns.c
Normal file
@@ -0,0 +1,496 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*! \file */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <sched.h> /* IWYU pragma: keep */
|
||||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define UNIT_TESTING
|
||||
#include <cmocka.h>
|
||||
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/file.h>
|
||||
#include <isc/hash.h>
|
||||
#include <isc/hex.h>
|
||||
#include <isc/lex.h>
|
||||
#include <isc/managers.h>
|
||||
#include <isc/mem.h>
|
||||
#include <isc/netmgr.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/print.h>
|
||||
#include <isc/random.h>
|
||||
#include <isc/result.h>
|
||||
#include <isc/stdio.h>
|
||||
#include <isc/string.h>
|
||||
#include <isc/task.h>
|
||||
#include <isc/timer.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include <dns/callbacks.h>
|
||||
#include <dns/db.h>
|
||||
#include <dns/fixedname.h>
|
||||
#include <dns/log.h>
|
||||
#include <dns/name.h>
|
||||
#include <dns/view.h>
|
||||
#include <dns/zone.h>
|
||||
|
||||
#include <tests/dns.h>
|
||||
|
||||
dns_zonemgr_t *zonemgr = NULL;
|
||||
|
||||
/*
|
||||
* Create a view.
|
||||
*/
|
||||
isc_result_t
|
||||
dns_test_makeview(const char *name, bool with_cache, dns_view_t **viewp) {
|
||||
isc_result_t result;
|
||||
dns_view_t *view = NULL;
|
||||
dns_cache_t *cache = NULL;
|
||||
|
||||
result = dns_view_create(mctx, dns_rdataclass_in, name, &view);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
if (with_cache) {
|
||||
result = dns_cache_create(mctx, mctx, taskmgr, timermgr,
|
||||
dns_rdataclass_in, "", "rbt", 0, NULL,
|
||||
&cache);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
dns_view_detach(&view);
|
||||
return (result);
|
||||
}
|
||||
|
||||
dns_view_setcache(view, cache, false);
|
||||
/*
|
||||
* Reference count for "cache" is now at 2, so decrement it in
|
||||
* order for the cache to be automatically freed when "view"
|
||||
* gets freed.
|
||||
*/
|
||||
dns_cache_detach(&cache);
|
||||
}
|
||||
|
||||
*viewp = view;
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
|
||||
bool createview) {
|
||||
dns_fixedname_t fixed_origin;
|
||||
dns_zone_t *zone = NULL;
|
||||
isc_result_t result;
|
||||
dns_name_t *origin;
|
||||
|
||||
REQUIRE(view == NULL || !createview);
|
||||
|
||||
/*
|
||||
* Create the zone structure.
|
||||
*/
|
||||
result = dns_zone_create(&zone, mctx, 0);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set zone type and origin.
|
||||
*/
|
||||
dns_zone_settype(zone, dns_zone_primary);
|
||||
origin = dns_fixedname_initname(&fixed_origin);
|
||||
result = dns_name_fromstring(origin, name, 0, NULL);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_zone;
|
||||
}
|
||||
result = dns_zone_setorigin(zone, origin);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_zone;
|
||||
}
|
||||
|
||||
/*
|
||||
* If requested, create a view.
|
||||
*/
|
||||
if (createview) {
|
||||
result = dns_test_makeview("view", false, &view);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_zone;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If a view was passed as an argument or created above, attach the
|
||||
* created zone to it. Otherwise, set the zone's class to IN.
|
||||
*/
|
||||
if (view != NULL) {
|
||||
dns_zone_setview(zone, view);
|
||||
dns_zone_setclass(zone, view->rdclass);
|
||||
dns_view_addzone(view, zone);
|
||||
} else {
|
||||
dns_zone_setclass(zone, dns_rdataclass_in);
|
||||
}
|
||||
|
||||
*zonep = zone;
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
detach_zone:
|
||||
dns_zone_detach(&zone);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_setupzonemgr(void) {
|
||||
isc_result_t result;
|
||||
REQUIRE(zonemgr == NULL);
|
||||
|
||||
result = dns_zonemgr_create(mctx, taskmgr, timermgr, netmgr, &zonemgr);
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_managezone(dns_zone_t *zone) {
|
||||
isc_result_t result;
|
||||
REQUIRE(zonemgr != NULL);
|
||||
|
||||
result = dns_zonemgr_managezone(zonemgr, zone);
|
||||
return (result);
|
||||
}
|
||||
|
||||
void
|
||||
dns_test_releasezone(dns_zone_t *zone) {
|
||||
REQUIRE(zonemgr != NULL);
|
||||
dns_zonemgr_releasezone(zonemgr, zone);
|
||||
}
|
||||
|
||||
void
|
||||
dns_test_closezonemgr(void) {
|
||||
REQUIRE(zonemgr != NULL);
|
||||
|
||||
dns_zonemgr_shutdown(zonemgr);
|
||||
dns_zonemgr_detach(&zonemgr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sleep for 'usec' microseconds.
|
||||
*/
|
||||
void
|
||||
dns_test_nap(uint32_t usec) {
|
||||
struct timespec ts;
|
||||
|
||||
ts.tv_sec = usec / 1000000;
|
||||
ts.tv_nsec = (usec % 1000000) * 1000;
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin,
|
||||
const char *testfile) {
|
||||
isc_result_t result;
|
||||
dns_fixedname_t fixed;
|
||||
dns_name_t *name;
|
||||
|
||||
name = dns_fixedname_initname(&fixed);
|
||||
|
||||
result = dns_name_fromstring(name, origin, 0, NULL);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
result = dns_db_create(mctx, "rbt", name, dbtype, dns_rdataclass_in, 0,
|
||||
NULL, db);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
result = dns_db_load(*db, testfile, dns_masterformat_text, 0);
|
||||
return (result);
|
||||
}
|
||||
|
||||
static int
|
||||
fromhex(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return (c - '0');
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
return (c - 'a' + 10);
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
return (c - 'A' + 10);
|
||||
}
|
||||
|
||||
printf("bad input format: %02x\n", c);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
/*
|
||||
* Format contents of given memory region as a hex string, using the buffer
|
||||
* of length 'buflen' pointed to by 'buf'. 'buflen' must be at least three
|
||||
* times 'len'. Always returns 'buf'.
|
||||
*/
|
||||
char *
|
||||
dns_test_tohex(const unsigned char *data, size_t len, char *buf,
|
||||
size_t buflen) {
|
||||
isc_constregion_t source = { .base = data, .length = len };
|
||||
isc_buffer_t target;
|
||||
isc_result_t result;
|
||||
|
||||
memset(buf, 0, buflen);
|
||||
isc_buffer_init(&target, buf, buflen);
|
||||
result = isc_hex_totext((isc_region_t *)&source, 1, " ", &target);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
return (buf);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz,
|
||||
size_t *sizep) {
|
||||
isc_result_t result;
|
||||
unsigned char *bp;
|
||||
char *rp, *wp;
|
||||
char s[BUFSIZ];
|
||||
size_t len, i;
|
||||
FILE *f = NULL;
|
||||
int n;
|
||||
|
||||
result = isc_stdio_open(file, "r", &f);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
bp = buf;
|
||||
while (fgets(s, sizeof(s), f) != NULL) {
|
||||
rp = s;
|
||||
wp = s;
|
||||
len = 0;
|
||||
while (*rp != '\0') {
|
||||
if (*rp == '#') {
|
||||
break;
|
||||
}
|
||||
if (*rp != ' ' && *rp != '\t' && *rp != '\r' &&
|
||||
*rp != '\n') {
|
||||
*wp++ = *rp;
|
||||
len++;
|
||||
}
|
||||
rp++;
|
||||
}
|
||||
if (len == 0U) {
|
||||
continue;
|
||||
}
|
||||
if (len % 2 != 0U) {
|
||||
result = ISC_R_UNEXPECTEDEND;
|
||||
break;
|
||||
}
|
||||
if (len > bufsiz * 2) {
|
||||
result = ISC_R_NOSPACE;
|
||||
break;
|
||||
}
|
||||
rp = s;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
n = fromhex(*rp++);
|
||||
n *= 16;
|
||||
n += fromhex(*rp++);
|
||||
*bp++ = n;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == ISC_R_SUCCESS) {
|
||||
*sizep = bp - buf;
|
||||
}
|
||||
|
||||
isc_stdio_close(f);
|
||||
return (result);
|
||||
}
|
||||
|
||||
static void
|
||||
nullmsg(dns_rdatacallbacks_t *cb, const char *fmt, ...) {
|
||||
UNUSED(cb);
|
||||
UNUSED(fmt);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_rdatafromstring(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
|
||||
dns_rdatatype_t rdtype, unsigned char *dst,
|
||||
size_t dstlen, const char *src, bool warnings) {
|
||||
dns_rdatacallbacks_t callbacks;
|
||||
isc_buffer_t source, target;
|
||||
isc_lex_t *lex = NULL;
|
||||
isc_lexspecials_t specials = { 0 };
|
||||
isc_result_t result;
|
||||
size_t length;
|
||||
|
||||
REQUIRE(rdata != NULL);
|
||||
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
|
||||
REQUIRE(dst != NULL);
|
||||
REQUIRE(src != NULL);
|
||||
|
||||
/*
|
||||
* Set up source to hold the input string.
|
||||
*/
|
||||
length = strlen(src);
|
||||
isc_buffer_constinit(&source, src, length);
|
||||
isc_buffer_add(&source, length);
|
||||
|
||||
/*
|
||||
* Create a lexer as one is required by dns_rdata_fromtext().
|
||||
*/
|
||||
result = isc_lex_create(mctx, 64, &lex);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set characters which will be treated as valid multi-line RDATA
|
||||
* delimiters while reading the source string. These should match
|
||||
* specials from lib/dns/master.c.
|
||||
*/
|
||||
specials[0] = 1;
|
||||
specials['('] = 1;
|
||||
specials[')'] = 1;
|
||||
specials['"'] = 1;
|
||||
isc_lex_setspecials(lex, specials);
|
||||
|
||||
/*
|
||||
* Expect DNS masterfile comments.
|
||||
*/
|
||||
isc_lex_setcomments(lex, ISC_LEXCOMMENT_DNSMASTERFILE);
|
||||
|
||||
/*
|
||||
* Point lexer at source.
|
||||
*/
|
||||
result = isc_lex_openbuffer(lex, &source);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto destroy_lexer;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set up target for storing uncompressed wire form of provided RDATA.
|
||||
*/
|
||||
isc_buffer_init(&target, dst, dstlen);
|
||||
|
||||
/*
|
||||
* Set up callbacks so warnings and errors are not printed.
|
||||
*/
|
||||
if (!warnings) {
|
||||
dns_rdatacallbacks_init(&callbacks);
|
||||
callbacks.warn = callbacks.error = nullmsg;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse input string, determining result.
|
||||
*/
|
||||
result = dns_rdata_fromtext(rdata, rdclass, rdtype, lex, dns_rootname,
|
||||
0, mctx, &target, &callbacks);
|
||||
|
||||
destroy_lexer:
|
||||
isc_lex_destroy(&lex);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
void
|
||||
dns_test_namefromstring(const char *namestr, dns_fixedname_t *fname) {
|
||||
size_t length;
|
||||
isc_buffer_t *b = NULL;
|
||||
isc_result_t result;
|
||||
dns_name_t *name;
|
||||
|
||||
length = strlen(namestr);
|
||||
|
||||
name = dns_fixedname_initname(fname);
|
||||
|
||||
isc_buffer_allocate(mctx, &b, length);
|
||||
|
||||
isc_buffer_putmem(b, (const unsigned char *)namestr, length);
|
||||
result = dns_name_fromtext(name, b, dns_rootname, 0, NULL);
|
||||
assert_int_equal(result, ISC_R_SUCCESS);
|
||||
|
||||
isc_buffer_free(&b);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_test_difffromchanges(dns_diff_t *diff, const zonechange_t *changes,
|
||||
bool warnings) {
|
||||
isc_result_t result = ISC_R_SUCCESS;
|
||||
unsigned char rdata_buf[1024];
|
||||
dns_difftuple_t *tuple = NULL;
|
||||
isc_consttextregion_t region;
|
||||
dns_rdatatype_t rdatatype;
|
||||
dns_fixedname_t fixedname;
|
||||
dns_rdata_t rdata;
|
||||
dns_name_t *name;
|
||||
size_t i;
|
||||
|
||||
REQUIRE(diff != NULL);
|
||||
REQUIRE(changes != NULL);
|
||||
|
||||
dns_diff_init(mctx, diff);
|
||||
|
||||
for (i = 0; changes[i].owner != NULL; i++) {
|
||||
/*
|
||||
* Parse owner name.
|
||||
*/
|
||||
name = dns_fixedname_initname(&fixedname);
|
||||
result = dns_name_fromstring(name, changes[i].owner, 0, mctx);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse RDATA type.
|
||||
*/
|
||||
region.base = changes[i].type;
|
||||
region.length = strlen(changes[i].type);
|
||||
result = dns_rdatatype_fromtext(&rdatatype,
|
||||
(isc_textregion_t *)®ion);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse RDATA.
|
||||
*/
|
||||
dns_rdata_init(&rdata);
|
||||
result = dns_test_rdatafromstring(
|
||||
&rdata, dns_rdataclass_in, rdatatype, rdata_buf,
|
||||
sizeof(rdata_buf), changes[i].rdata, warnings);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a diff tuple for the parsed change and append it to
|
||||
* the diff.
|
||||
*/
|
||||
result = dns_difftuple_create(mctx, changes[i].op, name,
|
||||
changes[i].ttl, &rdata, &tuple);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
break;
|
||||
}
|
||||
dns_diff_append(diff, &tuple);
|
||||
}
|
||||
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
dns_diff_clear(diff);
|
||||
}
|
||||
|
||||
return (result);
|
||||
}
|
||||
87
tests/libtest/isc.c
Normal file
87
tests/libtest/isc.c
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*! \file */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <signal.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/hash.h>
|
||||
#include <isc/managers.h>
|
||||
#include <isc/mem.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/string.h>
|
||||
#include <isc/task.h>
|
||||
#include <isc/timer.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include "netmgr_p.h"
|
||||
#include "task_p.h"
|
||||
#include "timer_p.h"
|
||||
|
||||
#include <tests/isc.h>
|
||||
|
||||
isc_mem_t *mctx = NULL;
|
||||
isc_taskmgr_t *taskmgr = NULL;
|
||||
isc_timermgr_t *timermgr = NULL;
|
||||
isc_nm_t *netmgr = NULL;
|
||||
unsigned int workers = 0;
|
||||
isc_task_t *maintask = NULL;
|
||||
|
||||
int
|
||||
setup_managers(void **state) {
|
||||
isc_result_t result;
|
||||
|
||||
UNUSED(state);
|
||||
|
||||
REQUIRE(mctx != NULL);
|
||||
|
||||
if (workers == 0) {
|
||||
char *env_workers = getenv("ISC_TASK_WORKERS");
|
||||
if (env_workers != NULL) {
|
||||
workers = atoi(env_workers);
|
||||
} else {
|
||||
workers = isc_os_ncpus();
|
||||
}
|
||||
INSIST(workers > 0);
|
||||
}
|
||||
|
||||
result = isc_managers_create(mctx, workers, 0, &netmgr, &taskmgr,
|
||||
&timermgr);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
result = isc_task_create(taskmgr, 0, &maintask, 0);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
isc_taskmgr_setexcltask(taskmgr, maintask);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
teardown_managers(void **state) {
|
||||
UNUSED(state);
|
||||
|
||||
isc_task_detach(&maintask);
|
||||
isc_managers_destroy(&netmgr, &taskmgr, &timermgr);
|
||||
|
||||
return (0);
|
||||
}
|
||||
661
tests/libtest/ns.c
Normal file
661
tests/libtest/ns.c
Normal file
@@ -0,0 +1,661 @@
|
||||
/*
|
||||
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
||||
*
|
||||
* SPDX-License-Identifier: MPL-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*! \file */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <isc/buffer.h>
|
||||
#include <isc/file.h>
|
||||
#include <isc/hash.h>
|
||||
#include <isc/managers.h>
|
||||
#include <isc/mem.h>
|
||||
#include <isc/netmgr.h>
|
||||
#include <isc/os.h>
|
||||
#include <isc/print.h>
|
||||
#include <isc/random.h>
|
||||
#include <isc/resource.h>
|
||||
#include <isc/result.h>
|
||||
#include <isc/stdio.h>
|
||||
#include <isc/string.h>
|
||||
#include <isc/task.h>
|
||||
#include <isc/timer.h>
|
||||
#include <isc/util.h>
|
||||
|
||||
#include <dns/cache.h>
|
||||
#include <dns/db.h>
|
||||
#include <dns/dispatch.h>
|
||||
#include <dns/fixedname.h>
|
||||
#include <dns/log.h>
|
||||
#include <dns/name.h>
|
||||
#include <dns/view.h>
|
||||
#include <dns/zone.h>
|
||||
|
||||
#include <ns/client.h>
|
||||
#include <ns/hooks.h>
|
||||
#include <ns/interfacemgr.h>
|
||||
#include <ns/server.h>
|
||||
|
||||
#include <tests/ns.h>
|
||||
|
||||
dns_dispatchmgr_t *dispatchmgr = NULL;
|
||||
ns_clientmgr_t *clientmgr = NULL;
|
||||
ns_interfacemgr_t *interfacemgr = NULL;
|
||||
ns_server_t *sctx = NULL;
|
||||
bool debug_mem_record = true;
|
||||
|
||||
static isc_result_t
|
||||
matchview(isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr,
|
||||
dns_message_t *message, dns_aclenv_t *env, isc_result_t *sigresultp,
|
||||
dns_view_t **viewp) {
|
||||
UNUSED(srcaddr);
|
||||
UNUSED(destaddr);
|
||||
UNUSED(message);
|
||||
UNUSED(env);
|
||||
UNUSED(sigresultp);
|
||||
UNUSED(viewp);
|
||||
|
||||
return (ISC_R_NOTIMPLEMENTED);
|
||||
}
|
||||
|
||||
int
|
||||
setup_server(void **state) {
|
||||
isc_result_t result;
|
||||
ns_listenlist_t *listenon = NULL;
|
||||
in_port_t port = 5300 + isc_random8();
|
||||
|
||||
setup_managers(state);
|
||||
|
||||
ns_server_create(mctx, matchview, &sctx);
|
||||
|
||||
result = dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
result = ns_interfacemgr_create(mctx, sctx, taskmgr, timermgr, netmgr,
|
||||
dispatchmgr, maintask, NULL, false,
|
||||
&interfacemgr);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
result = ns_listenlist_default(mctx, port, -1, true, AF_INET,
|
||||
&listenon);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (-1);
|
||||
}
|
||||
|
||||
ns_interfacemgr_setlistenon4(interfacemgr, listenon);
|
||||
ns_listenlist_detach(&listenon);
|
||||
|
||||
clientmgr = ns_interfacemgr_getclientmgr(interfacemgr);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
teardown_server(void **state) {
|
||||
if (interfacemgr != NULL) {
|
||||
ns_interfacemgr_shutdown(interfacemgr);
|
||||
ns_interfacemgr_detach(&interfacemgr);
|
||||
}
|
||||
|
||||
if (dispatchmgr != NULL) {
|
||||
dns_dispatchmgr_detach(&dispatchmgr);
|
||||
}
|
||||
|
||||
if (sctx != NULL) {
|
||||
ns_server_detach(&sctx);
|
||||
}
|
||||
|
||||
teardown_managers(state);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static dns_zone_t *served_zone = NULL;
|
||||
|
||||
/*
|
||||
* We don't want to use netmgr-based client accounting, we need to emulate it.
|
||||
*/
|
||||
atomic_uint_fast32_t client_refs[32];
|
||||
atomic_uintptr_t client_addrs[32];
|
||||
|
||||
void
|
||||
isc__nmhandle_attach(isc_nmhandle_t *source, isc_nmhandle_t **targetp FLARG) {
|
||||
ns_client_t *client = (ns_client_t *)source;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (atomic_load(&client_addrs[i]) == (uintptr_t)client) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
INSIST(i < 32);
|
||||
INSIST(atomic_load(&client_refs[i]) > 0);
|
||||
|
||||
atomic_fetch_add(&client_refs[i], 1);
|
||||
|
||||
*targetp = source;
|
||||
return;
|
||||
}
|
||||
|
||||
void
|
||||
isc__nmhandle_detach(isc_nmhandle_t **handlep FLARG) {
|
||||
isc_nmhandle_t *handle = *handlep;
|
||||
ns_client_t *client = (ns_client_t *)handle;
|
||||
int i;
|
||||
|
||||
*handlep = NULL;
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (atomic_load(&client_addrs[i]) == (uintptr_t)client) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
INSIST(i < 32);
|
||||
|
||||
if (atomic_fetch_sub(&client_refs[i], 1) == 1) {
|
||||
dns_view_detach(&client->view);
|
||||
client->state = 4;
|
||||
ns__client_reset_cb(client);
|
||||
ns__client_put_cb(client);
|
||||
atomic_store(&client_addrs[i], (uintptr_t)NULL);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
ns_test_serve_zone(const char *zonename, const char *filename,
|
||||
dns_view_t *view) {
|
||||
isc_result_t result;
|
||||
dns_db_t *db = NULL;
|
||||
|
||||
/*
|
||||
* Prepare zone structure for further processing.
|
||||
*/
|
||||
result = dns_test_makezone(zonename, &served_zone, view, false);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Start zone manager.
|
||||
*/
|
||||
result = dns_test_setupzonemgr();
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto free_zone;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the zone to the zone manager.
|
||||
*/
|
||||
result = dns_test_managezone(served_zone);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto close_zonemgr;
|
||||
}
|
||||
|
||||
view->nocookieudp = 512;
|
||||
|
||||
/*
|
||||
* Set path to the master file for the zone and then load it.
|
||||
*/
|
||||
dns_zone_setfile(served_zone, filename, dns_masterformat_text,
|
||||
&dns_master_style_default);
|
||||
result = dns_zone_load(served_zone, false);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto release_zone;
|
||||
}
|
||||
|
||||
/*
|
||||
* The zone should now be loaded; test it.
|
||||
*/
|
||||
result = dns_zone_getdb(served_zone, &db);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto release_zone;
|
||||
}
|
||||
if (db != NULL) {
|
||||
dns_db_detach(&db);
|
||||
}
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
release_zone:
|
||||
dns_test_releasezone(served_zone);
|
||||
close_zonemgr:
|
||||
dns_test_closezonemgr();
|
||||
free_zone:
|
||||
dns_zone_detach(&served_zone);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
void
|
||||
ns_test_cleanup_zone(void) {
|
||||
dns_test_releasezone(served_zone);
|
||||
dns_test_closezonemgr();
|
||||
|
||||
dns_zone_detach(&served_zone);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
ns_test_getclient(ns_interface_t *ifp0, bool tcp, ns_client_t **clientp) {
|
||||
isc_result_t result;
|
||||
ns_client_t *client = isc_mem_get(clientmgr->mctx, sizeof(*client));
|
||||
int i;
|
||||
|
||||
UNUSED(ifp0);
|
||||
UNUSED(tcp);
|
||||
|
||||
result = ns__client_setup(client, clientmgr, true);
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
if (atomic_load(&client_addrs[i]) == (uintptr_t)NULL ||
|
||||
atomic_load(&client_addrs[i]) == (uintptr_t)client)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
REQUIRE(i < 32);
|
||||
|
||||
atomic_store(&client_refs[i], 2);
|
||||
atomic_store(&client_addrs[i], (uintptr_t)client);
|
||||
client->handle = (isc_nmhandle_t *)client; /* Hack */
|
||||
*clientp = client;
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*%
|
||||
* Synthesize a DNS message based on supplied QNAME, QTYPE and flags, then
|
||||
* parse it and store the results in client->message.
|
||||
*/
|
||||
static isc_result_t
|
||||
attach_query_msg_to_client(ns_client_t *client, const char *qnamestr,
|
||||
dns_rdatatype_t qtype, unsigned int qflags) {
|
||||
dns_rdataset_t *qrdataset = NULL;
|
||||
dns_message_t *message = NULL;
|
||||
unsigned char query[65536];
|
||||
dns_name_t *qname = NULL;
|
||||
isc_buffer_t querybuf;
|
||||
dns_compress_t cctx;
|
||||
isc_result_t result;
|
||||
|
||||
REQUIRE(client != NULL);
|
||||
REQUIRE(qnamestr != NULL);
|
||||
|
||||
/*
|
||||
* Create a new DNS message holding a query.
|
||||
*/
|
||||
dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &message);
|
||||
|
||||
/*
|
||||
* Set query ID to a random value.
|
||||
*/
|
||||
message->id = isc_random16();
|
||||
|
||||
/*
|
||||
* Set query flags as requested by the caller.
|
||||
*/
|
||||
message->flags = qflags;
|
||||
|
||||
/*
|
||||
* Allocate structures required to construct the query.
|
||||
*/
|
||||
dns_message_gettemprdataset(message, &qrdataset);
|
||||
dns_message_gettempname(message, &qname);
|
||||
|
||||
/*
|
||||
* Convert "qnamestr" to a DNS name, create a question rdataset of
|
||||
* class IN and type "qtype", link the two and add the result to the
|
||||
* QUESTION section of the query.
|
||||
*/
|
||||
result = dns_name_fromstring(qname, qnamestr, 0, mctx);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto put_name;
|
||||
}
|
||||
dns_rdataset_makequestion(qrdataset, dns_rdataclass_in, qtype);
|
||||
ISC_LIST_APPEND(qname->list, qrdataset, link);
|
||||
dns_message_addname(message, qname, DNS_SECTION_QUESTION);
|
||||
|
||||
/*
|
||||
* Render the query.
|
||||
*/
|
||||
dns_compress_init(&cctx, -1, mctx);
|
||||
isc_buffer_init(&querybuf, query, sizeof(query));
|
||||
result = dns_message_renderbegin(message, &cctx, &querybuf);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto destroy_message;
|
||||
}
|
||||
result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto destroy_message;
|
||||
}
|
||||
result = dns_message_renderend(message);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto destroy_message;
|
||||
}
|
||||
dns_compress_invalidate(&cctx);
|
||||
|
||||
/*
|
||||
* Destroy the created message as it was rendered into "querybuf" and
|
||||
* the latter is all we are going to need from now on.
|
||||
*/
|
||||
dns_message_detach(&message);
|
||||
|
||||
/*
|
||||
* Parse the rendered query, storing results in client->message.
|
||||
*/
|
||||
isc_buffer_first(&querybuf);
|
||||
return (dns_message_parse(client->message, &querybuf, 0));
|
||||
|
||||
put_name:
|
||||
dns_message_puttempname(message, &qname);
|
||||
dns_message_puttemprdataset(message, &qrdataset);
|
||||
destroy_message:
|
||||
dns_message_detach(&message);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*%
|
||||
* A hook action which stores the query context pointed to by "arg" at
|
||||
* "data". Causes execution to be interrupted at hook insertion
|
||||
* point.
|
||||
*/
|
||||
static ns_hookresult_t
|
||||
extract_qctx(void *arg, void *data, isc_result_t *resultp) {
|
||||
query_ctx_t **qctxp;
|
||||
query_ctx_t *qctx;
|
||||
|
||||
REQUIRE(arg != NULL);
|
||||
REQUIRE(data != NULL);
|
||||
REQUIRE(resultp != NULL);
|
||||
|
||||
/*
|
||||
* qctx is a stack variable in lib/ns/query.c. Its contents need to be
|
||||
* duplicated or otherwise they will become invalidated once the stack
|
||||
* gets unwound.
|
||||
*/
|
||||
qctx = isc_mem_get(mctx, sizeof(*qctx));
|
||||
if (qctx != NULL) {
|
||||
memmove(qctx, (query_ctx_t *)arg, sizeof(*qctx));
|
||||
}
|
||||
|
||||
qctxp = (query_ctx_t **)data;
|
||||
/*
|
||||
* If memory allocation failed, the supplied pointer will simply be set
|
||||
* to NULL. We rely on the user of this hook to react properly.
|
||||
*/
|
||||
*qctxp = qctx;
|
||||
*resultp = ISC_R_UNSET;
|
||||
|
||||
return (NS_HOOK_RETURN);
|
||||
}
|
||||
|
||||
/*%
|
||||
* Initialize a query context for "client" and store it in "qctxp".
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* \li "client->message" to hold a parsed DNS query.
|
||||
*/
|
||||
static isc_result_t
|
||||
create_qctx_for_client(ns_client_t *client, query_ctx_t **qctxp) {
|
||||
ns_hooktable_t *saved_hook_table = NULL, *query_hooks = NULL;
|
||||
const ns_hook_t hook = {
|
||||
.action = extract_qctx,
|
||||
.action_data = qctxp,
|
||||
};
|
||||
|
||||
REQUIRE(client != NULL);
|
||||
REQUIRE(qctxp != NULL);
|
||||
REQUIRE(*qctxp == NULL);
|
||||
|
||||
/*
|
||||
* Call ns_query_start() to initialize a query context for given
|
||||
* client, but first hook into query_setup() so that we can just
|
||||
* extract an initialized query context, without kicking off any
|
||||
* further processing. Make sure we do not overwrite any previously
|
||||
* set hooks.
|
||||
*/
|
||||
|
||||
ns_hooktable_create(mctx, &query_hooks);
|
||||
ns_hook_add(query_hooks, mctx, NS_QUERY_SETUP, &hook);
|
||||
|
||||
saved_hook_table = ns__hook_table;
|
||||
ns__hook_table = query_hooks;
|
||||
|
||||
ns_query_start(client, client->handle);
|
||||
|
||||
ns__hook_table = saved_hook_table;
|
||||
ns_hooktable_free(mctx, (void **)&query_hooks);
|
||||
|
||||
isc_nmhandle_detach(&client->reqhandle);
|
||||
|
||||
if (*qctxp == NULL) {
|
||||
return (ISC_R_NOMEMORY);
|
||||
} else {
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
ns_test_qctx_create(const ns_test_qctx_create_params_t *params,
|
||||
query_ctx_t **qctxp) {
|
||||
ns_client_t *client = NULL;
|
||||
isc_result_t result;
|
||||
isc_nmhandle_t *handle = NULL;
|
||||
|
||||
REQUIRE(params != NULL);
|
||||
REQUIRE(params->qname != NULL);
|
||||
REQUIRE(qctxp != NULL);
|
||||
REQUIRE(*qctxp == NULL);
|
||||
|
||||
/*
|
||||
* Allocate and initialize a client structure.
|
||||
*/
|
||||
result = ns_test_getclient(NULL, false, &client);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
TIME_NOW(&client->tnow);
|
||||
|
||||
/*
|
||||
* Every client needs to belong to a view.
|
||||
*/
|
||||
result = dns_test_makeview("view", params->with_cache, &client->view);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_client;
|
||||
}
|
||||
|
||||
/*
|
||||
* Synthesize a DNS query using given QNAME, QTYPE and flags, storing
|
||||
* it in client->message.
|
||||
*/
|
||||
result = attach_query_msg_to_client(client, params->qname,
|
||||
params->qtype, params->qflags);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_view;
|
||||
}
|
||||
|
||||
/*
|
||||
* Allow recursion for the client. As NS_CLIENTATTR_RA normally gets
|
||||
* set in ns__client_request(), i.e. earlier than the unit tests hook
|
||||
* into the call chain, just set it manually.
|
||||
*/
|
||||
client->attributes |= NS_CLIENTATTR_RA;
|
||||
|
||||
/*
|
||||
* Create a query context for a client sending the previously
|
||||
* synthesized query.
|
||||
*/
|
||||
result = create_qctx_for_client(client, qctxp);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
goto detach_query;
|
||||
}
|
||||
|
||||
/*
|
||||
* The reference count for "client" is now at 2, so we need to
|
||||
* decrement it in order for it to drop to zero when "qctx" gets
|
||||
* destroyed.
|
||||
*/
|
||||
handle = client->handle;
|
||||
isc_nmhandle_detach(&handle);
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
detach_query:
|
||||
dns_message_detach(&client->message);
|
||||
detach_view:
|
||||
dns_view_detach(&client->view);
|
||||
detach_client:
|
||||
isc_nmhandle_detach(&client->handle);
|
||||
|
||||
return (result);
|
||||
}
|
||||
|
||||
void
|
||||
ns_test_qctx_destroy(query_ctx_t **qctxp) {
|
||||
query_ctx_t *qctx;
|
||||
|
||||
REQUIRE(qctxp != NULL);
|
||||
REQUIRE(*qctxp != NULL);
|
||||
|
||||
qctx = *qctxp;
|
||||
*qctxp = NULL;
|
||||
|
||||
if (qctx->zone != NULL) {
|
||||
dns_zone_detach(&qctx->zone);
|
||||
}
|
||||
if (qctx->db != NULL) {
|
||||
dns_db_detach(&qctx->db);
|
||||
}
|
||||
if (qctx->client != NULL) {
|
||||
isc_nmhandle_detach(&qctx->client->handle);
|
||||
}
|
||||
|
||||
isc_mem_put(mctx, qctx, sizeof(*qctx));
|
||||
}
|
||||
|
||||
ns_hookresult_t
|
||||
ns_test_hook_catch_call(void *arg, void *data, isc_result_t *resultp) {
|
||||
UNUSED(arg);
|
||||
UNUSED(data);
|
||||
|
||||
*resultp = ISC_R_UNSET;
|
||||
|
||||
return (NS_HOOK_RETURN);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
ns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin,
|
||||
const char *testfile) {
|
||||
isc_result_t result;
|
||||
dns_fixedname_t fixed;
|
||||
dns_name_t *name;
|
||||
|
||||
name = dns_fixedname_initname(&fixed);
|
||||
|
||||
result = dns_name_fromstring(name, origin, 0, NULL);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
result = dns_db_create(mctx, "rbt", name, dbtype, dns_rdataclass_in, 0,
|
||||
NULL, db);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
result = dns_db_load(*db, testfile, dns_masterformat_text, 0);
|
||||
return (result);
|
||||
}
|
||||
|
||||
static int
|
||||
fromhex(char c) {
|
||||
if (c >= '0' && c <= '9') {
|
||||
return (c - '0');
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
return (c - 'a' + 10);
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
return (c - 'A' + 10);
|
||||
}
|
||||
|
||||
printf("bad input format: %02x\n", c);
|
||||
exit(3);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
ns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz,
|
||||
size_t *sizep) {
|
||||
isc_result_t result;
|
||||
unsigned char *bp;
|
||||
char *rp, *wp;
|
||||
char s[BUFSIZ];
|
||||
size_t len, i;
|
||||
FILE *f = NULL;
|
||||
int n;
|
||||
|
||||
result = isc_stdio_open(file, "r", &f);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
bp = buf;
|
||||
while (fgets(s, sizeof(s), f) != NULL) {
|
||||
rp = s;
|
||||
wp = s;
|
||||
len = 0;
|
||||
while (*rp != '\0') {
|
||||
if (*rp == '#') {
|
||||
break;
|
||||
}
|
||||
if (*rp != ' ' && *rp != '\t' && *rp != '\r' &&
|
||||
*rp != '\n') {
|
||||
*wp++ = *rp;
|
||||
len++;
|
||||
}
|
||||
rp++;
|
||||
}
|
||||
if (len == 0U) {
|
||||
continue;
|
||||
}
|
||||
if (len % 2 != 0U) {
|
||||
CHECK(ISC_R_UNEXPECTEDEND);
|
||||
}
|
||||
if (len > bufsiz * 2) {
|
||||
CHECK(ISC_R_NOSPACE);
|
||||
}
|
||||
rp = s;
|
||||
for (i = 0; i < len; i += 2) {
|
||||
n = fromhex(*rp++);
|
||||
n *= 16;
|
||||
n += fromhex(*rp++);
|
||||
*bp++ = n;
|
||||
}
|
||||
}
|
||||
|
||||
*sizep = bp - buf;
|
||||
|
||||
result = ISC_R_SUCCESS;
|
||||
|
||||
cleanup:
|
||||
isc_stdio_close(f);
|
||||
return (result);
|
||||
}
|
||||
Reference in New Issue
Block a user