1 /* $NetBSD: t_resolver.c,v 1.11 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: t_resolver.c,v 1.3 2011/02/03 12:18:11 tbox Exp */
28 #include <isc/string.h>
29 #include <isc/timer.h>
32 #include <dns/dispatch.h>
33 #include <dns/resolver.h>
34 #include <dns/result.h>
37 #include <tests/t_api.h>
41 #define CHECK(x) RUNTIME_CHECK(ISC_R_SUCCESS == (x))
44 isc_mem_t
*mctx
= NULL
;
45 isc_timermgr_t
*timer_manager
= NULL
;
46 isc_socketmgr_t
*socket_manager
= NULL
;
47 isc_taskmgr_t
*task_manager
= NULL
;
48 dns_dispatchmgr_t
*dispatch_manager
= NULL
;
49 dns_view_t
*view
= NULL
;
50 dns_dispatch_t
*dispatch_v4
= NULL
;
53 setup_create_dispatch_v4(void)
55 isc_sockaddr_t local_address
;
56 isc_sockaddr_any(&local_address
);
58 CHECK(dns_dispatch_getudp(dispatch_manager
, socket_manager
,
59 task_manager
, &local_address
,
60 4096, 100, 100, 100, 500, 0, 0,
65 /* 1 */ CHECK(isc_mem_create(0, 0, &mctx
));
66 /* 2 */ CHECK(isc_timermgr_create(mctx
, &timer_manager
));
67 /* 3 */ CHECK(isc_taskmgr_create(mctx
, 1, 0, &task_manager
));
68 /* 4 */ CHECK(isc_socketmgr_create(mctx
, &socket_manager
));
69 /* 5 */ CHECK(dns_dispatchmgr_create(mctx
, NULL
, &dispatch_manager
));
70 /* 6 */ CHECK(dns_view_create(mctx
, dns_rdataclass_in
, "testview", &view
));
71 /* 7 */ setup_create_dispatch_v4();
76 /* 7 */ dns_dispatch_detach(&dispatch_v4
);
77 /* 6 */ dns_view_detach(&view
);
78 /* 5 */ dns_dispatchmgr_destroy(&dispatch_manager
);
79 /* 4 */ isc_socketmgr_destroy(&socket_manager
);
80 /* 3 */ isc_taskmgr_destroy(&task_manager
);
81 /* 2 */ isc_timermgr_destroy(&timer_manager
);
82 /* 1 */ isc_mem_destroy(&mctx
);
86 make_resolver(dns_resolver_t
**resolverp
) {
89 result
= dns_resolver_create(view
,
93 0, /* unsigned int options, */
96 NULL
, /* dns_dispatch_t *dispatchv6, */
103 destroy_resolver(dns_resolver_t
**resolverp
) {
104 dns_resolver_shutdown(*resolverp
);
105 dns_resolver_detach(resolverp
);
109 test_dns_resolver_create(void) {
110 dns_resolver_t
*resolver
= NULL
;
112 t_assert("test_dns_resolver_create", 1, T_REQUIRED
, "%s",
113 "a resolver can be created successfully");
115 CHECK(make_resolver(&resolver
));
117 destroy_resolver(&resolver
);
124 test_dns_resolver_gettimeout(void) {
125 dns_resolver_t
*resolver
= NULL
;
127 unsigned int timeout
;
129 t_assert("test_dns_resolver_gettimeout", 1, T_REQUIRED
, "%s",
130 "The default timeout is returned from _gettimeout()");
132 CHECK(make_resolver(&resolver
));
134 timeout
= dns_resolver_gettimeout(resolver
);
135 t_info("The default timeout is %d second%s\n", timeout
, (timeout
== 1 ? "" : "s"));
136 test_result
= (timeout
> 0) ? T_PASS
: T_FAIL
;
138 destroy_resolver(&resolver
);
141 t_result(test_result
);
145 test_dns_resolver_settimeout(void) {
146 dns_resolver_t
*resolver
= NULL
;
148 unsigned int default_timeout
, timeout
;
150 t_assert("test_dns_resolver_settimeout", 1, T_REQUIRED
, "%s",
151 "_settimeout() can change the timeout to a non-default");
153 CHECK(make_resolver(&resolver
));
155 default_timeout
= dns_resolver_gettimeout(resolver
);
156 t_info("The default timeout is %d second%s\n", default_timeout
,
157 (default_timeout
== 1 ? "" : "s"));
159 dns_resolver_settimeout(resolver
, default_timeout
+ 1);
160 timeout
= dns_resolver_gettimeout(resolver
);
161 t_info("The new timeout is %d second%s\n", timeout
,
162 (timeout
== 1 ? "" : "s"));
163 test_result
= (timeout
== default_timeout
+ 1) ? T_PASS
: T_FAIL
;
165 destroy_resolver(&resolver
);
168 t_result(test_result
);
172 test_dns_resolver_settimeout_to_default(void) {
173 dns_resolver_t
*resolver
= NULL
;
175 unsigned int default_timeout
, timeout
;
177 t_assert("test_dns_resolver_settimeout_to_default", 1, T_REQUIRED
, "%s",
178 "_settimeout() can change the timeout back to a default value"
179 " by specifying 0 as the timeout.");
181 CHECK(make_resolver(&resolver
));
183 default_timeout
= dns_resolver_gettimeout(resolver
);
184 t_info("The default timeout is %d second%s\n", default_timeout
,
185 (default_timeout
== 1 ? "" : "s"));
187 dns_resolver_settimeout(resolver
, default_timeout
- 1);
188 timeout
= dns_resolver_gettimeout(resolver
);
189 t_info("The new timeout is %d second%s\n", timeout
,
190 (timeout
== 1 ? "" : "s"));
192 dns_resolver_settimeout(resolver
, 0);
193 timeout
= dns_resolver_gettimeout(resolver
);
194 test_result
= (timeout
== default_timeout
) ? T_PASS
: T_FAIL
;
196 destroy_resolver(&resolver
);
199 t_result(test_result
);
203 test_dns_resolver_settimeout_over_maximum(void) {
204 dns_resolver_t
*resolver
= NULL
;
206 unsigned int timeout
;
208 t_assert("test_dns_resolver_settimeout_over_maximum", 1, T_REQUIRED
, "%s",
209 "_settimeout() cannot set the value larger than the maximum.");
211 CHECK(make_resolver(&resolver
));
213 dns_resolver_settimeout(resolver
, 4000000);
214 timeout
= dns_resolver_gettimeout(resolver
);
215 t_info("The new timeout is %d second%s\n", timeout
,
216 (timeout
== 1 ? "" : "s"));
218 test_result
= (timeout
< 4000000 && timeout
> 0) ? T_PASS
: T_FAIL
;
220 destroy_resolver(&resolver
);
223 t_result(test_result
);
227 testspec_t T_testlist
[] = {
228 { (PFV
) test_dns_resolver_create
, "dns_resolver_create" },
229 { (PFV
) test_dns_resolver_settimeout
, "dns_resolver_settimeout" },
230 { (PFV
) test_dns_resolver_gettimeout
, "dns_resolver_gettimeout" },
231 { (PFV
) test_dns_resolver_settimeout_to_default
, "test_dns_resolver_settimeout_to_default" },
232 { (PFV
) test_dns_resolver_settimeout_over_maximum
, "test_dns_resolver_settimeout_over_maximum" },
238 main(int argc
, char **argv
) {
239 t_settests(T_testlist
);
240 return (t_main(argc
, argv
));