t_resolver.c 6.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/*
 * Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
 *
 * Permission to use, copy, modify, and/or 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 ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC 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.
 */

/* $Id: t_resolver.c,v 1.2 2011/02/03 05:41:53 marka Exp $ */

#include <config.h>

#include <ctype.h>
#include <stdlib.h>

#include <isc/mem.h>
#include <isc/util.h>
#include <isc/string.h>
#include <isc/timer.h>
#include <isc/task.h>

#include <dns/dispatch.h>
#include <dns/resolver.h>
#include <dns/result.h>
#include <dns/view.h>

#include <tests/t_api.h>

char *progname;

#define CHECK(x) RUNTIME_CHECK(ISC_R_SUCCESS == (x))


isc_mem_t *mctx = NULL;
isc_timermgr_t *timer_manager = NULL;
isc_socketmgr_t *socket_manager = NULL;
isc_taskmgr_t *task_manager = NULL;
dns_dispatchmgr_t *dispatch_manager = NULL;
dns_view_t *view = NULL;
dns_dispatch_t *dispatch_v4 = NULL;

static void
setup_create_dispatch_v4(void)
{
	isc_sockaddr_t local_address;
	isc_sockaddr_any(&local_address);
	
	CHECK(dns_dispatch_getudp(dispatch_manager, socket_manager, task_manager,
				  &local_address, 4096, 100, 100, 100, 500,
				  0, 0, /* unsigned int attributes, unsigned int mask, */
				  &dispatch_v4));
}
static void
setup(void) {
	/* 1 */ CHECK(isc_mem_create(0, 0, &mctx));
	/* 2 */ CHECK(isc_timermgr_create(mctx, &timer_manager));
	/* 3 */ CHECK(isc_taskmgr_create(mctx, 1, 0, &task_manager));
	/* 4 */ CHECK(isc_socketmgr_create(mctx, &socket_manager));
	/* 5 */ CHECK(dns_dispatchmgr_create(mctx, NULL, &dispatch_manager));
	/* 6 */ CHECK(dns_view_create(mctx, dns_rdataclass_in, "testview", &view));
	/* 7 */ setup_create_dispatch_v4();
}

static void
teardown(void) {
	/* 7 */ dns_dispatch_detach(&dispatch_v4);
	/* 6 */ dns_view_detach(&view);
	/* 5 */ dns_dispatchmgr_destroy(&dispatch_manager);
	/* 4 */ isc_socketmgr_destroy(&socket_manager);
	/* 3 */ isc_taskmgr_destroy(&task_manager);
	/* 2 */ isc_timermgr_destroy(&timer_manager);
	/* 1 */ isc_mem_destroy(&mctx);
}

static isc_result_t
make_resolver(dns_resolver_t **resolverp) {
	isc_result_t result;
	
	result = dns_resolver_create(view,
			    task_manager, 1,
			    socket_manager,
			    timer_manager,
			    0, /* unsigned int options, */
			    dispatch_manager,
			    dispatch_v4,
			    NULL, /* dns_dispatch_t *dispatchv6, */
			    resolverp);

	return (result);
}

static void
destroy_resolver(dns_resolver_t **resolverp) {
	dns_resolver_shutdown(*resolverp);
	dns_resolver_detach(resolverp);
}

static void
test_dns_resolver_create() {
	dns_resolver_t *resolver = NULL;

	t_assert("test_dns_resolver_create", 1, T_REQUIRED, "%s",
		 "a resolver can be created successfully");
	setup();
	CHECK(make_resolver(&resolver));

	destroy_resolver(&resolver);
	teardown();

	t_result(T_PASS);
}

static void
test_dns_resolver_gettimeout(void) {
	dns_resolver_t *resolver = NULL;
	int test_result;
	unsigned int timeout;

	t_assert("test_dns_resolver_gettimeout", 1, T_REQUIRED, "%s",
		 "The default timeout is returned from _gettimeout()");
	setup();
	CHECK(make_resolver(&resolver));
	
	timeout = dns_resolver_gettimeout(resolver);
	t_info("The default timeout is %d second%s\n", timeout, (timeout == 1 ? "" : "s"));
	test_result = (timeout > 0) ? T_PASS : T_FAIL;

	destroy_resolver(&resolver);
	teardown();

	t_result(test_result);
}

static void
test_dns_resolver_settimeout(void) {
	dns_resolver_t *resolver = NULL;
	int test_result;
	unsigned int default_timeout, timeout;

	t_assert("test_dns_resolver_settimeout", 1, T_REQUIRED, "%s",
		 "_settimeout() can change the timeout to a non-default");
	setup();
	CHECK(make_resolver(&resolver));
	
	default_timeout = dns_resolver_gettimeout(resolver);
	t_info("The default timeout is %d second%s\n", default_timeout,
	       (default_timeout == 1 ? "" : "s"));

	dns_resolver_settimeout(resolver, default_timeout - 1);
	timeout = dns_resolver_gettimeout(resolver);
	t_info("The new timeout is %d second%s\n", timeout,
	       (timeout == 1 ? "" : "s"));
	test_result = (timeout == default_timeout - 1) ? T_PASS : T_FAIL;

	destroy_resolver(&resolver);
	teardown();
	
	t_result(test_result);
}

static void
test_dns_resolver_settimeout_to_default(void) {
	dns_resolver_t *resolver = NULL;
	int test_result;
	unsigned int default_timeout, timeout;

	t_assert("test_dns_resolver_settimeout_to_default", 1, T_REQUIRED, "%s",
		 "_settimeout() can change the timeout back to a default value"
		 " by specifying 0 as the timeout.");
	setup();
	CHECK(make_resolver(&resolver));
	
	default_timeout = dns_resolver_gettimeout(resolver);
	t_info("The default timeout is %d second%s\n", default_timeout,
	       (default_timeout == 1 ? "" : "s"));

	dns_resolver_settimeout(resolver, default_timeout - 1);
	timeout = dns_resolver_gettimeout(resolver);
	t_info("The new timeout is %d second%s\n", timeout,
	       (timeout == 1 ? "" : "s"));

	dns_resolver_settimeout(resolver, 0);
	timeout = dns_resolver_gettimeout(resolver);
	test_result = (timeout == default_timeout) ? T_PASS : T_FAIL;

	destroy_resolver(&resolver);
	teardown();
	
	t_result(test_result);
}

static void
test_dns_resolver_settimeout_over_maximum(void) {
	dns_resolver_t *resolver = NULL;
	int test_result;
	unsigned int timeout;

	t_assert("test_dns_resolver_settimeout_over_maximum", 1, T_REQUIRED, "%s",
		 "_settimeout() cannot set the value larger than the maximum.");
	setup();
	CHECK(make_resolver(&resolver));
	
	dns_resolver_settimeout(resolver, 4000000);
	timeout = dns_resolver_gettimeout(resolver);
	t_info("The new timeout is %d second%s\n", timeout,
	       (timeout == 1 ? "" : "s"));

	test_result = (timeout < 4000000 && timeout > 0) ? T_PASS : T_FAIL;

	destroy_resolver(&resolver);
	teardown();
	
	t_result(test_result);
}


testspec_t T_testlist[] = {
	{ test_dns_resolver_create,	"dns_resolver_create"		},
	{ test_dns_resolver_settimeout,	"dns_resolver_settimeout"	},
	{ test_dns_resolver_gettimeout,	"dns_resolver_gettimeout"	},
	{ test_dns_resolver_settimeout_to_default, "test_dns_resolver_settimeout_to_default" },
	{ test_dns_resolver_settimeout_over_maximum, "test_dns_resolver_settimeout_over_maximum" },
	{ NULL,	NULL }
};