c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt/*
c15e906adf68f86f03d6dcb07b1b6c14cd4d5d14Tinderbox User * Copyright (C) 2011-2014, 2016, 2017 Internet Systems Consortium, Inc. ("ISC")
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id$ */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt/*! \file */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <config.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt#include <time.h>
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/app.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/buffer.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/entropy.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/hash.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/mem.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/os.h>
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#include <isc/socket.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/string.h>
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#include <isc/task.h>
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#include <isc/timer.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <isc/util.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include "isctest.h"
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_mem_t *mctx = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_entropy_t *ectx = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_log_t *lctx = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_taskmgr_t *taskmgr = NULL;
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntisc_timermgr_t *timermgr = NULL;
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntisc_socketmgr_t *socketmgr = NULL;
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrewsisc_task_t *maintask = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntint ncpus;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntstatic isc_boolean_t hash_active = ISC_FALSE;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt/*
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * Logging categories: this needs to match the list in bin/named/log.c.
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntstatic isc_logcategory_t categories[] = {
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "client", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "network", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "update", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "queries", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "unmatched", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "update-security", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { "query-errors", 0 },
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt { NULL, 0 }
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt};
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntstatic void
b454c0319685041db3f3e8fd7671e1b364fd20c5Evan Huntcleanup_managers(void) {
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews if (maintask != NULL)
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews isc_task_destroy(&maintask);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt if (socketmgr != NULL)
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt isc_socketmgr_destroy(&socketmgr);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt if (taskmgr != NULL)
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt isc_taskmgr_destroy(&taskmgr);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt if (timermgr != NULL)
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt isc_timermgr_destroy(&timermgr);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt}
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntstatic isc_result_t
b454c0319685041db3f3e8fd7671e1b364fd20c5Evan Huntcreate_managers(void) {
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt isc_result_t result;
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#ifdef ISC_PLATFORM_USETHREADS
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt ncpus = isc_os_ncpus();
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#else
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt ncpus = 1;
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt#endif
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr));
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews CHECK(isc_task_create(taskmgr, 0, &maintask));
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews isc_taskmgr_setexcltask(taskmgr, maintask);
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(isc_timermgr_create(mctx, &timermgr));
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(isc_socketmgr_create(mctx, &socketmgr));
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt return (ISC_R_SUCCESS);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
67adc03ef81fb610f8df093b17f55275ee816754Evan Hunt cleanup:
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt cleanup_managers();
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt return (result);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt}
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_result_t
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntisc_test_begin(FILE *logfile, isc_boolean_t start_managers) {
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_result_t result;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_mem_create(0, 0, &mctx));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_entropy_create(mctx, &ectx));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_hash_create(mctx, ectx, 255));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt hash_active = ISC_TRUE;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (logfile != NULL) {
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_logdestination_t destination;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_logconfig_t *logconfig = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_log_create(mctx, &lctx, &logconfig));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_log_registercategories(lctx, categories);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_log_setcontext(lctx);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt destination.file.stream = logfile;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt destination.file.name = NULL;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt destination.file.versions = ISC_LOG_ROLLNEVER;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt destination.file.maximum_size = 0;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_log_createchannel(logconfig, "stderr",
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt ISC_LOG_TOFILEDESC,
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt ISC_LOG_DYNAMIC,
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt &destination, 0));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt }
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#ifdef ISC_PLATFORM_USETHREADS
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt ncpus = isc_os_ncpus();
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#else
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt ncpus = 1;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#endif
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt if (start_managers)
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(create_managers());
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt return (ISC_R_SUCCESS);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt cleanup:
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_test_end();
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt return (result);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt}
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntvoid
b454c0319685041db3f3e8fd7671e1b364fd20c5Evan Huntisc_test_end(void) {
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews if (maintask != NULL)
c965b1869024ab38518fade703cc1dae2d71a59eMark Andrews isc_task_detach(&maintask);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (taskmgr != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_taskmgr_destroy(&taskmgr);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (hash_active) {
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_hash_destroy();
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt hash_active = ISC_FALSE;
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt }
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (ectx != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_entropy_detach(&ectx);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt cleanup_managers();
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
adedbbbcfb4066d53570164a0c71395f6c0f6768Evan Hunt if (lctx != NULL)
adedbbbcfb4066d53570164a0c71395f6c0f6768Evan Hunt isc_log_destroy(&lctx);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (mctx != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_mem_destroy(&mctx);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt}
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt/*
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt * Sleep for 'usec' microseconds.
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt */
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Huntvoid
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Huntisc_test_nap(isc_uint32_t usec) {
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt#ifdef HAVE_NANOSLEEP
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt struct timespec ts;
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt ts.tv_sec = usec / 1000000;
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt ts.tv_nsec = (usec % 1000000) * 1000;
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt nanosleep(&ts, NULL);
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt#elif HAVE_USLEEP
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt usleep(usec);
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt#else
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt /*
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt * No fractional-second sleep function is available, so we
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt * round up to the nearest second and sleep instead
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt */
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt sleep((usec / 1000000) + 1);
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt#endif
8a2ab2b9203120c3e2a883a5ee8c0b5d60c1808cEvan Hunt}