isctest.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011-2014  Internet Systems Consortium, Inc. ("ISC")
00003  *
00004  * Permission to use, copy, modify, and/or distribute this software for any
00005  * purpose with or without fee is hereby granted, provided that the above
00006  * copyright notice and this permission notice appear in all copies.
00007  *
00008  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00009  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00010  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00011  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00012  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00013  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00014  * PERFORMANCE OF THIS SOFTWARE.
00015  */
00016 
00017 /* $Id$ */
00018 
00019 /*! \file */
00020 
00021 #include <config.h>
00022 
00023 #include <time.h>
00024 
00025 #include <isc/app.h>
00026 #include <isc/buffer.h>
00027 #include <isc/entropy.h>
00028 #include <isc/hash.h>
00029 #include <isc/mem.h>
00030 #include <isc/os.h>
00031 #include <isc/socket.h>
00032 #include <isc/string.h>
00033 #include <isc/task.h>
00034 #include <isc/timer.h>
00035 #include <isc/util.h>
00036 
00037 #include "isctest.h"
00038 
00039 isc_mem_t *mctx = NULL;
00040 isc_entropy_t *ectx = NULL;
00041 isc_log_t *lctx = NULL;
00042 isc_taskmgr_t *taskmgr = NULL;
00043 isc_timermgr_t *timermgr = NULL;
00044 isc_socketmgr_t *socketmgr = NULL;
00045 isc_task_t *maintask = NULL;
00046 int ncpus;
00047 
00048 static isc_boolean_t hash_active = ISC_FALSE;
00049 
00050 /*
00051  * Logging categories: this needs to match the list in bin/named/log.c.
00052  */
00053 static isc_logcategory_t categories[] = {
00054                 { "",                0 },
00055                 { "client",          0 },
00056                 { "network",         0 },
00057                 { "update",          0 },
00058                 { "queries",         0 },
00059                 { "unmatched",       0 },
00060                 { "update-security", 0 },
00061                 { "query-errors",    0 },
00062                 { NULL,              0 }
00063 };
00064 
00065 static void
00066 cleanup_managers(void) {
00067         if (maintask != NULL)
00068                 isc_task_destroy(&maintask);
00069         if (socketmgr != NULL)
00070                 isc_socketmgr_destroy(&socketmgr);
00071         if (taskmgr != NULL)
00072                 isc_taskmgr_destroy(&taskmgr);
00073         if (timermgr != NULL)
00074                 isc_timermgr_destroy(&timermgr);
00075 }
00076 
00077 static isc_result_t
00078 create_managers(void) {
00079         isc_result_t result;
00080 #ifdef ISC_PLATFORM_USETHREADS
00081         ncpus = isc_os_ncpus();
00082 #else
00083         ncpus = 1;
00084 #endif
00085 
00086         CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr));
00087         CHECK(isc_task_create(taskmgr, 0, &maintask));
00088         isc_taskmgr_setexcltask(taskmgr, maintask);
00089 
00090         CHECK(isc_timermgr_create(mctx, &timermgr));
00091         CHECK(isc_socketmgr_create(mctx, &socketmgr));
00092         return (ISC_R_SUCCESS);
00093 
00094  cleanup:
00095         cleanup_managers();
00096         return (result);
00097 }
00098 
00099 isc_result_t
00100 isc_test_begin(FILE *logfile, isc_boolean_t start_managers) {
00101         isc_result_t result;
00102 
00103         isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
00104         CHECK(isc_mem_create(0, 0, &mctx));
00105         CHECK(isc_entropy_create(mctx, &ectx));
00106 
00107         CHECK(isc_hash_create(mctx, ectx, 255));
00108         hash_active = ISC_TRUE;
00109 
00110         if (logfile != NULL) {
00111                 isc_logdestination_t destination;
00112                 isc_logconfig_t *logconfig = NULL;
00113 
00114                 CHECK(isc_log_create(mctx, &lctx, &logconfig));
00115                 isc_log_registercategories(lctx, categories);
00116                 isc_log_setcontext(lctx);
00117 
00118                 destination.file.stream = logfile;
00119                 destination.file.name = NULL;
00120                 destination.file.versions = ISC_LOG_ROLLNEVER;
00121                 destination.file.maximum_size = 0;
00122                 CHECK(isc_log_createchannel(logconfig, "stderr",
00123                                             ISC_LOG_TOFILEDESC,
00124                                             ISC_LOG_DYNAMIC,
00125                                             &destination, 0));
00126                 CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL));
00127         }
00128 
00129 #ifdef ISC_PLATFORM_USETHREADS
00130         ncpus = isc_os_ncpus();
00131 #else
00132         ncpus = 1;
00133 #endif
00134 
00135         if (start_managers)
00136                 CHECK(create_managers());
00137 
00138         return (ISC_R_SUCCESS);
00139 
00140   cleanup:
00141         isc_test_end();
00142         return (result);
00143 }
00144 
00145 void
00146 isc_test_end(void) {
00147         if (maintask != NULL)
00148                 isc_task_detach(&maintask);
00149         if (taskmgr != NULL)
00150                 isc_taskmgr_destroy(&taskmgr);
00151         if (lctx != NULL)
00152                 isc_log_destroy(&lctx);
00153         if (hash_active) {
00154                 isc_hash_destroy();
00155                 hash_active = ISC_FALSE;
00156         }
00157         if (ectx != NULL)
00158                 isc_entropy_detach(&ectx);
00159 
00160         cleanup_managers();
00161 
00162         if (mctx != NULL)
00163                 isc_mem_destroy(&mctx);
00164 }
00165 
00166 /*
00167  * Sleep for 'usec' microseconds.
00168  */
00169 void
00170 isc_test_nap(isc_uint32_t usec) {
00171 #ifdef HAVE_NANOSLEEP
00172         struct timespec ts;
00173 
00174         ts.tv_sec = usec / 1000000;
00175         ts.tv_nsec = (usec % 1000000) * 1000;
00176         nanosleep(&ts, NULL);
00177 #elif HAVE_USLEEP
00178         usleep(usec);
00179 #else
00180         /*
00181          * No fractional-second sleep function is available, so we
00182          * round up to the nearest second and sleep instead
00183          */
00184         sleep((usec / 1000000) + 1);
00185 #endif
00186 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha