From 1292abfde97472be1625898f74b4d6f33ceafc8c Mon Sep 17 00:00:00 2001 From: Bob Halley Date: Mon, 28 Jun 1999 23:35:03 +0000 Subject: [PATCH] add res_test --- bin/tests/.cvsignore | 1 + bin/tests/Makefile.in | 6 +- bin/tests/res_test.c | 215 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 bin/tests/res_test.c diff --git a/bin/tests/.cvsignore b/bin/tests/.cvsignore index c19ffcf1a6..5d87102b9f 100644 --- a/bin/tests/.cvsignore +++ b/bin/tests/.cvsignore @@ -16,3 +16,4 @@ db_test compress_test t_journal mempool_test +res_test diff --git a/bin/tests/Makefile.in b/bin/tests/Makefile.in index b83ba70739..5ec41dc770 100644 --- a/bin/tests/Makefile.in +++ b/bin/tests/Makefile.in @@ -35,7 +35,8 @@ ISCDEPLIBS = ../../lib/isc/libisc.a LIBS = @LIBS@ SUBDIRS = master names rbt -TARGETS = lex_test \ +TARGETS = res_test \ + lex_test \ name_test \ sock_test \ sym_test \ @@ -53,6 +54,9 @@ TARGETS = lex_test \ @BIND9_MAKE_RULES@ +res_test: res_test.o ${ISCDEPLIBS} ${DNSDEPLIBS} + ${CC} ${CFLAGS} -o $@ res_test.o ${DNSLIBS} ${ISCLIBS} ${LIBS} + lex_test: lex_test.o ${ISCDEPLIBS} ${CC} ${CFLAGS} -o $@ lex_test.o ${ISCLIBS} ${LIBS} diff --git a/bin/tests/res_test.c b/bin/tests/res_test.c new file mode 100644 index 0000000000..392d8d2a54 --- /dev/null +++ b/bin/tests/res_test.c @@ -0,0 +1,215 @@ +/* + * Copyright (C) 1999 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + */ + +/* + * Principal Author: Bob Halley + */ + +#include + +#include +#include +#include +#include /* XXX Naughty. */ + +#include +#include +#include +#include +#include +#include +#include + +#include "../../isc/util.h" /* XXX Naughty. */ + +#include +#include +#include +#include +#include +#include + +isc_mutex_t lock; + +ISC_LIST(dns_fetch_t) fetches; + +static void +cancel(dns_fetch_t *fetch, isc_task_t *task) { + isc_boolean_t need_shutdown = ISC_FALSE; + + LOCK(&lock); + + printf("fetch %p canceled\n", fetch); + ISC_LIST_UNLINK(fetches, fetch, link); + dns_resolver_destroyfetch(&fetch, task); + if (ISC_LIST_EMPTY(fetches)) + need_shutdown = ISC_TRUE; + + UNLOCK(&lock); + + if (need_shutdown) + isc_app_shutdown(); +} + +static void +done(isc_task_t *task, isc_event_t *event) { + dns_fetchdoneevent_t *devent = (dns_fetchdoneevent_t *)event; + isc_boolean_t need_shutdown = ISC_FALSE; + dns_fetch_t *fetch, *next_fetch; + + REQUIRE(devent->type == DNS_EVENT_FETCHDONE); + + (void)task; + + LOCK(&lock); + + for (fetch = ISC_LIST_HEAD(fetches); + fetch != NULL; + fetch = next_fetch) { + next_fetch = ISC_LIST_NEXT(fetch, link); + if (fetch->private == event->sender && + fetch->tag == event->tag) + break; + } + INSIST(fetch != NULL); + printf("fetch %p done: %s\n", fetch, + dns_result_totext(devent->result)); + ISC_LIST_UNLINK(fetches, fetch, link); + if (ISC_LIST_EMPTY(fetches)) + need_shutdown = ISC_TRUE; + + UNLOCK(&lock); + + isc_event_free(&event); + dns_resolver_destroyfetch(&fetch, NULL); + + if (need_shutdown) + isc_app_shutdown(); +} + +static dns_fetch_t * +launch(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type, + isc_boolean_t shared_ok, isc_task_t *task) +{ + dns_fetch_t *fetch; + unsigned int options = 0; + + if (!shared_ok) + options |= DNS_FETCHOPT_UNSHARED; + + LOCK(&lock); + + fetch = NULL; + RUNTIME_CHECK(dns_resolver_createfetch(res, name, type, NULL, NULL, + options, task, done, + NULL, + &fetch) == + DNS_R_SUCCESS); + ISC_LIST_APPEND(fetches, fetch, link); + + UNLOCK(&lock); + + return (fetch); +} + +int +main(int argc, char *argv[]) { + isc_mem_t *mctx; + isc_boolean_t verbose = ISC_FALSE; + unsigned int workers = 2; + isc_taskmgr_t *taskmgr; + isc_task_t *task1, *task2; + isc_timermgr_t *timermgr; + dns_resolver_t *res; + int ch; + dns_fetch_t *fetch; + + RUNTIME_CHECK(isc_app_start() == ISC_R_SUCCESS); + + RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS); + + mctx = NULL; + RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); + + while ((ch = getopt(argc, argv, "vw:")) != -1) { + switch (ch) { + case 'v': + verbose = ISC_TRUE; + break; + case 'w': + workers = (unsigned int)atoi(optarg); + break; + } + } + + if (verbose) + printf("%u workers\n", workers); + + taskmgr = NULL; + RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &taskmgr) == + ISC_R_SUCCESS); + task1 = NULL; + RUNTIME_CHECK(isc_task_create(taskmgr, mctx, 0, &task1) == + ISC_R_SUCCESS); + task2 = NULL; + RUNTIME_CHECK(isc_task_create(taskmgr, mctx, 0, &task2) == + ISC_R_SUCCESS); + timermgr = NULL; + RUNTIME_CHECK(isc_timermgr_create(mctx, &timermgr) == ISC_R_SUCCESS); + + argc -= optind; + argv += optind; + + if (argc != 0) + printf("ignoring trailing arguments\n"); + + res = NULL; + RUNTIME_CHECK(dns_resolver_create(mctx, taskmgr, 10, timermgr, + dns_rdataclass_in, NULL, &res) == + DNS_R_SUCCESS); + + ISC_LIST_INIT(fetches); + fetch = launch(res, dns_rootname, dns_rdatatype_a, ISC_TRUE, task1); + fetch = launch(res, dns_rootname, dns_rdatatype_a, ISC_TRUE, task2); + fetch = launch(res, dns_rootname, dns_rdatatype_a, ISC_TRUE, task2); + fetch = launch(res, dns_rootname, dns_rdatatype_mx, ISC_TRUE, task1); + fetch = launch(res, dns_rootname, dns_rdatatype_a, ISC_FALSE, task2); + fetch = launch(res, dns_rootname, dns_rdatatype_ns, ISC_TRUE, task1); + cancel(fetch, task1); + + (void)isc_app_run(); + + dns_resolver_detach(&res); + + isc_task_shutdown(task1); + isc_task_detach(&task1); + isc_task_shutdown(task2); + isc_task_detach(&task2); + isc_taskmgr_destroy(&taskmgr); + + isc_timermgr_destroy(&timermgr); + + if (verbose) + isc_mem_stats(mctx, stdout); + isc_mem_destroy(&mctx); + + isc_mutex_destroy(&lock); + + isc_app_finish(); + + return (0); +}