isctest.c revision f07b2fccaf6be13d9440d323e9e79ee84fe345e2
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt/*
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt *
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * Permission to use, copy, modify, and/or distribute this software for any
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * purpose with or without fee is hereby granted, provided that the above
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * copyright notice and this permission notice appear in all copies.
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt *
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt * PERFORMANCE OF THIS SOFTWARE.
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt/* $Id: isctest.c,v 1.3 2011/07/28 04:04:37 each Exp $ */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt/*! \file */
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt#include <config.h>
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan 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;
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
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntcleanup_managers() {
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
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Huntcreate_managers() {
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));
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(isc_timermgr_create(mctx, &timermgr));
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt CHECK(isc_socketmgr_create(mctx, &socketmgr));
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt return (ISC_R_SUCCESS);
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan Hunt
f07b2fccaf6be13d9440d323e9e79ee84fe345e2Evan 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
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Huntisc_test_end() {
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (taskmgr != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_taskmgr_destroy(&taskmgr);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (lctx != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_log_destroy(&lctx);
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
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt if (mctx != NULL)
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt isc_mem_destroy(&mctx);
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt}
c46ce2d79b4398a3db7c25b50c7ca3b292f0afa8Evan Hunt