2017-09-08 13:39:09 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2017-09-08 13:39:09 -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-09-08 13:39:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <isc/app.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/file.h>
|
|
|
|
#include <isc/hash.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/os.h>
|
|
|
|
#include <isc/print.h>
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <isc/random.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/socket.h>
|
|
|
|
#include <isc/stdio.h>
|
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/timer.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
#include <dns/cache.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dispatch.h>
|
|
|
|
#include <dns/fixedname.h>
|
|
|
|
#include <dns/log.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/view.h>
|
|
|
|
#include <dns/zone.h>
|
|
|
|
|
|
|
|
#include <ns/client.h>
|
2018-08-03 14:16:41 -07:00
|
|
|
#include <ns/hooks.h>
|
2017-09-08 13:39:09 -07:00
|
|
|
#include <ns/interfacemgr.h>
|
|
|
|
#include <ns/server.h>
|
|
|
|
|
|
|
|
#include "nstest.h"
|
|
|
|
|
|
|
|
isc_mem_t *mctx = NULL;
|
|
|
|
isc_log_t *lctx = NULL;
|
|
|
|
isc_taskmgr_t *taskmgr = NULL;
|
|
|
|
isc_task_t *maintask = NULL;
|
|
|
|
isc_timermgr_t *timermgr = NULL;
|
|
|
|
isc_socketmgr_t *socketmgr = NULL;
|
|
|
|
dns_zonemgr_t *zonemgr = NULL;
|
|
|
|
dns_dispatchmgr_t *dispatchmgr = NULL;
|
|
|
|
ns_clientmgr_t *clientmgr = NULL;
|
|
|
|
ns_interfacemgr_t *interfacemgr = NULL;
|
|
|
|
ns_server_t *sctx = NULL;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool app_running = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
int ncpus;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool debug_mem_record = true;
|
|
|
|
bool run_managers = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool dst_active = false;
|
2018-10-25 17:02:46 -07:00
|
|
|
static bool test_running = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
static dns_zone_t *served_zone = NULL;
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
/*
|
|
|
|
* Logging categories: this needs to match the list in lib/ns/log.c.
|
|
|
|
*/
|
|
|
|
static isc_logcategory_t categories[] = {
|
|
|
|
{ "", 0 },
|
|
|
|
{ "client", 0 },
|
|
|
|
{ "network", 0 },
|
|
|
|
{ "update", 0 },
|
|
|
|
{ "queries", 0 },
|
|
|
|
{ "unmatched", 0 },
|
|
|
|
{ "update-security", 0 },
|
|
|
|
{ "query-errors", 0 },
|
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
matchview(isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr,
|
2018-04-26 20:57:41 -07:00
|
|
|
dns_message_t *message, dns_aclenv_t *env,
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t *sigresultp, dns_view_t **viewp)
|
|
|
|
{
|
|
|
|
UNUSED(srcaddr);
|
|
|
|
UNUSED(destaddr);
|
|
|
|
UNUSED(message);
|
2018-02-05 20:24:14 +01:00
|
|
|
UNUSED(env);
|
2017-09-08 13:39:09 -07:00
|
|
|
UNUSED(sigresultp);
|
|
|
|
UNUSED(viewp);
|
|
|
|
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These need to be shut down from a running task.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
bool shutdown_done = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
static void
|
|
|
|
shutdown_managers(isc_task_t *task, isc_event_t *event) {
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
if (clientmgr != NULL) {
|
|
|
|
ns_clientmgr_destroy(&clientmgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interfacemgr != NULL) {
|
|
|
|
ns_interfacemgr_shutdown(interfacemgr);
|
|
|
|
ns_interfacemgr_detach(&interfacemgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dispatchmgr != NULL) {
|
|
|
|
dns_dispatchmgr_destroy(&dispatchmgr);
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
shutdown_done = true;
|
|
|
|
run_managers = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_managers(void) {
|
2018-04-17 08:29:14 -07:00
|
|
|
shutdown_done = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
if (maintask != NULL) {
|
|
|
|
isc_task_shutdown(maintask);
|
|
|
|
isc_task_destroy(&maintask);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (run_managers && !shutdown_done) {
|
|
|
|
/*
|
|
|
|
* There's no straightforward way to determine
|
|
|
|
* whether all the clients have shut down, so
|
|
|
|
* we'll just sleep for a bit and hope.
|
|
|
|
*/
|
|
|
|
ns_test_nap(500000);
|
|
|
|
}
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
if (sctx != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
ns_server_detach(&sctx);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (socketmgr != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_socketmgr_destroy(&socketmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (taskmgr != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_taskmgr_destroy(&taskmgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (timermgr != NULL) {
|
2017-09-12 12:56:00 -07:00
|
|
|
isc_timermgr_destroy(&timermgr);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (app_running) {
|
|
|
|
isc_app_finish();
|
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
scan_interfaces(isc_task_t *task, isc_event_t *event) {
|
|
|
|
UNUSED(task);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
ns_interfacemgr_scan(interfacemgr, true);
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_event_free(&event);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
create_managers(void) {
|
|
|
|
isc_result_t result;
|
|
|
|
ns_listenlist_t *listenon = NULL;
|
|
|
|
isc_event_t *event = NULL;
|
|
|
|
ncpus = isc_os_ncpus();
|
|
|
|
|
|
|
|
CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr));
|
|
|
|
CHECK(isc_task_create(taskmgr, 0, &maintask));
|
|
|
|
isc_taskmgr_setexcltask(taskmgr, maintask);
|
|
|
|
CHECK(isc_task_onshutdown(maintask, shutdown_managers, NULL));
|
|
|
|
|
|
|
|
CHECK(isc_timermgr_create(mctx, &timermgr));
|
|
|
|
|
|
|
|
CHECK(isc_socketmgr_create(mctx, &socketmgr));
|
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
CHECK(ns_server_create(mctx, matchview, &sctx));
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-04-22 14:56:28 +02:00
|
|
|
CHECK(dns_dispatchmgr_create(mctx, &dispatchmgr));
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
CHECK(ns_interfacemgr_create(mctx, sctx, taskmgr, timermgr,
|
|
|
|
socketmgr, dispatchmgr, maintask,
|
|
|
|
ncpus, NULL, &interfacemgr));
|
|
|
|
|
|
|
|
CHECK(ns_clientmgr_create(mctx, sctx, taskmgr, timermgr,
|
|
|
|
&clientmgr));
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
CHECK(ns_listenlist_default(mctx, 5300, -1, true, &listenon));
|
2017-09-08 13:39:09 -07:00
|
|
|
ns_interfacemgr_setlistenon4(interfacemgr, listenon);
|
|
|
|
ns_listenlist_detach(&listenon);
|
|
|
|
|
|
|
|
event = isc_event_allocate(mctx, maintask, ISC_TASKEVENT_TEST,
|
|
|
|
scan_interfaces, NULL,
|
|
|
|
sizeof (isc_event_t));
|
|
|
|
isc_task_send(maintask, &event);
|
2018-10-25 17:02:46 -07:00
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
/*
|
|
|
|
* There's no straightforward way to determine
|
|
|
|
* whether the interfaces have been scanned,
|
|
|
|
* we'll just sleep for a bit and hope.
|
|
|
|
*/
|
|
|
|
ns_test_nap(500000);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
run_managers = true;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
cleanup_managers();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
ns_test_begin(FILE *logfile, bool start_managers) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
INSIST(!test_running);
|
|
|
|
test_running = true;
|
|
|
|
|
|
|
|
if (start_managers) {
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(isc_app_start());
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
if (debug_mem_record) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
INSIST(mctx == NULL);
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(isc_mem_create(0, 0, &mctx));
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
if (!dst_active) {
|
|
|
|
CHECK(dst_lib_init(mctx, NULL));
|
|
|
|
dst_active = true;
|
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
if (logfile != NULL) {
|
|
|
|
isc_logdestination_t destination;
|
|
|
|
isc_logconfig_t *logconfig = NULL;
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
INSIST(lctx == NULL);
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(isc_log_create(mctx, &lctx, &logconfig));
|
2018-10-25 17:02:46 -07:00
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_registercategories(lctx, categories);
|
|
|
|
isc_log_setcontext(lctx);
|
|
|
|
dns_log_init(lctx);
|
|
|
|
dns_log_setcontext(lctx);
|
|
|
|
|
|
|
|
destination.file.stream = logfile;
|
|
|
|
destination.file.name = NULL;
|
|
|
|
destination.file.versions = ISC_LOG_ROLLNEVER;
|
|
|
|
destination.file.maximum_size = 0;
|
|
|
|
CHECK(isc_log_createchannel(logconfig, "stderr",
|
|
|
|
ISC_LOG_TOFILEDESC,
|
|
|
|
ISC_LOG_DYNAMIC,
|
|
|
|
&destination, 0));
|
|
|
|
CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_result_register();
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
if (start_managers) {
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(create_managers());
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* atf-run changes us to a /tmp directory, so tests
|
|
|
|
* that access test data files must first chdir to the proper
|
|
|
|
* location.
|
|
|
|
*/
|
2018-10-25 17:02:46 -07:00
|
|
|
if (chdir(TESTS) == -1) {
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(ISC_R_FAILURE);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
ns_test_end();
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_test_end(void) {
|
2018-08-24 11:21:27 +02:00
|
|
|
cleanup_managers();
|
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
dst_lib_destroy();
|
|
|
|
dst_active = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
if (lctx != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_log_destroy(&lctx);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
2017-09-08 13:39:09 -07:00
|
|
|
|
2018-10-25 17:02:46 -07:00
|
|
|
if (mctx != NULL) {
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_mem_destroy(&mctx);
|
2018-10-25 17:02:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test_running = false;
|
2017-09-08 13:39:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
ns_test_makeview(const char *name, bool with_cache,
|
2017-10-11 15:02:50 -07:00
|
|
|
dns_view_t **viewp)
|
|
|
|
{
|
|
|
|
dns_cache_t *cache = NULL;
|
2017-09-08 13:39:09 -07:00
|
|
|
dns_view_t *view = NULL;
|
2017-10-11 15:02:50 -07:00
|
|
|
isc_result_t result;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
CHECK(dns_view_create(mctx, dns_rdataclass_in, name, &view));
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
if (with_cache) {
|
2018-04-03 13:10:15 +02:00
|
|
|
CHECK(dns_cache_create(mctx, mctx, taskmgr, timermgr,
|
|
|
|
dns_rdataclass_in, "", "rbt", 0, NULL,
|
2017-10-11 15:02:50 -07:00
|
|
|
&cache));
|
2018-04-17 08:29:14 -07:00
|
|
|
dns_view_setcache(view, cache, false);
|
2017-10-11 15:02:50 -07:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
*viewp = view;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (view != NULL)
|
|
|
|
dns_view_detach(&view);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a zone with origin 'name', return a pointer to the zone object in
|
|
|
|
* 'zonep'. If 'view' is set, add the zone to that view; otherwise, create
|
|
|
|
* a new view for the purpose.
|
|
|
|
*
|
|
|
|
* If the created view is going to be needed by the caller subsequently,
|
|
|
|
* then 'keepview' should be set to true; this will prevent the view
|
|
|
|
* from being detached. In this case, the caller is responsible for
|
|
|
|
* detaching the view.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
ns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
|
2018-04-17 08:29:14 -07:00
|
|
|
bool keepview)
|
2017-09-08 13:39:09 -07:00
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
dns_zone_t *zone = NULL;
|
|
|
|
isc_buffer_t buffer;
|
|
|
|
dns_fixedname_t fixorigin;
|
|
|
|
dns_name_t *origin;
|
|
|
|
|
|
|
|
if (view == NULL)
|
|
|
|
CHECK(dns_view_create(mctx, dns_rdataclass_in, "view", &view));
|
|
|
|
else if (!keepview)
|
2018-04-17 08:29:14 -07:00
|
|
|
keepview = true;
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
zone = *zonep;
|
|
|
|
if (zone == NULL)
|
|
|
|
CHECK(dns_zone_create(&zone, mctx));
|
|
|
|
|
|
|
|
isc_buffer_constinit(&buffer, name, strlen(name));
|
|
|
|
isc_buffer_add(&buffer, strlen(name));
|
2018-03-28 14:38:09 +02:00
|
|
|
origin = dns_fixedname_initname(&fixorigin);
|
2017-09-08 13:39:09 -07:00
|
|
|
CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
|
|
|
|
CHECK(dns_zone_setorigin(zone, origin));
|
|
|
|
dns_zone_setview(zone, view);
|
|
|
|
dns_zone_settype(zone, dns_zone_master);
|
|
|
|
dns_zone_setclass(zone, view->rdclass);
|
|
|
|
dns_view_addzone(view, zone);
|
|
|
|
|
|
|
|
if (!keepview)
|
|
|
|
dns_view_detach(&view);
|
|
|
|
|
|
|
|
*zonep = zone;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (zone != NULL)
|
|
|
|
dns_zone_detach(&zone);
|
|
|
|
if (view != NULL)
|
|
|
|
dns_view_detach(&view);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_test_setupzonemgr(void) {
|
|
|
|
isc_result_t result;
|
|
|
|
REQUIRE(zonemgr == NULL);
|
|
|
|
|
|
|
|
result = dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
|
|
|
|
&zonemgr);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
ns_test_managezone(dns_zone_t *zone) {
|
|
|
|
isc_result_t result;
|
|
|
|
REQUIRE(zonemgr != NULL);
|
|
|
|
|
|
|
|
result = dns_zonemgr_setsize(zonemgr, 1);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
result = dns_zonemgr_managezone(zonemgr, zone);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_test_releasezone(dns_zone_t *zone) {
|
|
|
|
REQUIRE(zonemgr != NULL);
|
|
|
|
dns_zonemgr_releasezone(zonemgr, zone);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_test_closezonemgr(void) {
|
|
|
|
REQUIRE(zonemgr != NULL);
|
|
|
|
|
|
|
|
dns_zonemgr_shutdown(zonemgr);
|
|
|
|
dns_zonemgr_detach(&zonemgr);
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
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.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
result = ns_test_makezone(zonename, &served_zone, view, true);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start zone manager.
|
|
|
|
*/
|
|
|
|
result = ns_test_setupzonemgr();
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto free_zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add the zone to the zone manager.
|
|
|
|
*/
|
|
|
|
result = ns_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.
|
|
|
|
*/
|
2018-04-03 16:34:41 +02:00
|
|
|
dns_zone_setfile(served_zone, filename, dns_masterformat_text,
|
|
|
|
&dns_master_style_default);
|
2018-07-10 18:43:55 +02:00
|
|
|
result = dns_zone_load(served_zone, false);
|
2017-10-11 15:02:50 -07:00
|
|
|
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:
|
|
|
|
ns_test_releasezone(served_zone);
|
|
|
|
close_zonemgr:
|
|
|
|
ns_test_closezonemgr();
|
|
|
|
free_zone:
|
|
|
|
dns_zone_detach(&served_zone);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_test_cleanup_zone(void) {
|
|
|
|
ns_test_releasezone(served_zone);
|
|
|
|
ns_test_closezonemgr();
|
|
|
|
|
|
|
|
dns_zone_detach(&served_zone);
|
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
ns_test_getclient(ns_interface_t *ifp0, bool tcp,
|
2017-09-08 13:39:09 -07:00
|
|
|
ns_client_t **clientp)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
|
|
|
ns_interface_t *ifp = ifp0;
|
|
|
|
|
|
|
|
if (ifp == NULL) {
|
|
|
|
ifp = ns__interfacemgr_getif(interfacemgr);
|
|
|
|
}
|
|
|
|
if (ifp == NULL) {
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = ns__clientmgr_getclient(clientmgr, ifp, tcp, clientp);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
/*%
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &message);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set query ID to a random value.
|
|
|
|
*/
|
2018-05-28 15:22:23 +02:00
|
|
|
message->id = isc_random16();
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set query flags as requested by the caller.
|
|
|
|
*/
|
|
|
|
message->flags = qflags;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate structures required to construct the query.
|
|
|
|
*/
|
|
|
|
result = dns_message_gettemprdataset(message, &qrdataset);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto destroy_message;
|
|
|
|
}
|
|
|
|
result = dns_message_gettempname(message, &qname);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto put_rdataset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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_destroy(&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);
|
|
|
|
put_rdataset:
|
|
|
|
dns_message_puttemprdataset(message, &qrdataset);
|
|
|
|
destroy_message:
|
|
|
|
dns_message_destroy(&message);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
2018-08-13 21:08:08 -07:00
|
|
|
* A hook action which stores the query context pointed to by "arg" at
|
|
|
|
* "data". Causes execution to be interrupted at hook insertion
|
2017-10-11 15:02:50 -07:00
|
|
|
* point.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
static bool
|
2018-08-13 21:08:08 -07:00
|
|
|
extract_qctx(void *arg, void *data, isc_result_t *resultp) {
|
2017-10-11 15:02:50 -07:00
|
|
|
query_ctx_t **qctxp;
|
|
|
|
query_ctx_t *qctx;
|
|
|
|
|
2018-08-13 21:08:08 -07:00
|
|
|
REQUIRE(arg != NULL);
|
|
|
|
REQUIRE(data != NULL);
|
2017-10-11 15:02:50 -07:00
|
|
|
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) {
|
2018-08-13 21:08:08 -07:00
|
|
|
memmove(qctx, (query_ctx_t *)arg, sizeof(*qctx));
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2018-08-13 21:08:08 -07:00
|
|
|
qctxp = (query_ctx_t **)data;
|
2017-10-11 15:02:50 -07:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* 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) {
|
2018-08-03 14:16:41 -07:00
|
|
|
ns_hooktable_t *saved_hook_table, query_hooks;
|
|
|
|
ns_hook_t hook = {
|
2018-08-13 21:08:08 -07:00
|
|
|
.action = extract_qctx,
|
|
|
|
.action_data = qctxp,
|
2017-10-11 15:02:50 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2018-08-03 14:16:41 -07:00
|
|
|
|
|
|
|
ns_hooktable_init(&query_hooks);
|
2018-08-13 21:08:08 -07:00
|
|
|
ns_hook_add(&query_hooks, NS_QUERY_SETUP, &hook);
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2018-08-12 11:19:36 -07:00
|
|
|
saved_hook_table = ns__hook_table;
|
|
|
|
ns__hook_table = &query_hooks;
|
2018-08-03 14:16:41 -07:00
|
|
|
|
2017-10-11 15:02:50 -07:00
|
|
|
ns_query_start(client);
|
2018-08-12 11:19:36 -07:00
|
|
|
|
|
|
|
ns__hook_table = saved_hook_table;
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
REQUIRE(params != NULL);
|
|
|
|
REQUIRE(params->qname != NULL);
|
|
|
|
REQUIRE(qctxp != NULL);
|
|
|
|
REQUIRE(*qctxp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate and initialize a client structure.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
result = ns_test_getclient(NULL, false, &client);
|
2017-10-11 15:02:50 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
TIME_NOW(&client->tnow);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Every client needs to belong to a view.
|
|
|
|
*/
|
|
|
|
result = ns_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_client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 destroy_query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reference count for "client" is now at 2, so decrement it in order
|
|
|
|
* for it to drop to zero when "qctx" gets destroyed.
|
|
|
|
*/
|
|
|
|
ns_client_detach(&client);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
destroy_query:
|
|
|
|
dns_message_destroy(&client->message);
|
|
|
|
detach_client:
|
|
|
|
ns_client_detach(&client);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ns_test_qctx_destroy(query_ctx_t **qctxp) {
|
|
|
|
query_ctx_t *qctx;
|
|
|
|
|
|
|
|
REQUIRE(qctxp != NULL);
|
|
|
|
REQUIRE(*qctxp != NULL);
|
|
|
|
|
|
|
|
qctx = *qctxp;
|
|
|
|
|
|
|
|
ns_client_detach(&qctx->client);
|
|
|
|
|
|
|
|
if (qctx->zone != NULL) {
|
|
|
|
dns_zone_detach(&qctx->zone);
|
|
|
|
}
|
|
|
|
if (qctx->db != NULL) {
|
|
|
|
dns_db_detach(&qctx->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_put(mctx, qctx, sizeof(*qctx));
|
|
|
|
*qctxp = NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2018-08-13 21:08:08 -07:00
|
|
|
ns_test_hook_catch_call(void *arg, void *data, isc_result_t *resultp)
|
2017-10-11 15:02:50 -07:00
|
|
|
{
|
2018-08-13 21:08:08 -07:00
|
|
|
UNUSED(arg);
|
|
|
|
UNUSED(data);
|
2017-10-11 15:02:50 -07:00
|
|
|
|
|
|
|
*resultp = ISC_R_UNSET;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
return (true);
|
2017-10-11 15:02:50 -07:00
|
|
|
}
|
|
|
|
|
2017-09-08 13:39:09 -07:00
|
|
|
/*
|
|
|
|
* Sleep for 'usec' microseconds.
|
|
|
|
*/
|
|
|
|
void
|
2018-03-28 14:19:37 +02:00
|
|
|
ns_test_nap(uint32_t usec) {
|
2017-09-08 13:39:09 -07:00
|
|
|
#ifdef HAVE_NANOSLEEP
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
ts.tv_sec = usec / 1000000;
|
|
|
|
ts.tv_nsec = (usec % 1000000) * 1000;
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
#elif HAVE_USLEEP
|
|
|
|
usleep(usec);
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* No fractional-second sleep function is available, so we
|
|
|
|
* round up to the nearest second and sleep instead
|
|
|
|
*/
|
|
|
|
sleep((usec / 1000000) + 1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
name = dns_fixedname_initname(&fixed);
|
2017-09-08 13:39:09 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-04-03 13:22:09 +02:00
|
|
|
result = dns_db_load(*db, testfile, dns_masterformat_text, 0);
|
2017-09-08 13:39:09 -07:00
|
|
|
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);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|