1 /* $NetBSD: lwres_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lwres_test.c,v 1.31 2007/06/19 23:46:59 tbox Exp */
29 #include <isc/netaddr.h>
32 #include <lwres/lwres.h>
37 CHECK(int val
, const char *msg
) {
39 fprintf(stderr
, "%s returned %d\n", msg
, val
);
45 hexdump(const char *msg
, void *base
, size_t len
) {
52 printf("*** %s (%lu bytes @ %p)\n", msg
, (unsigned long)len
, base
);
57 else if (cnt
% 8 == 0)
59 printf(" %02x", *p
++);
70 static const char *TESTSTRING
= "This is a test. This is only a test. !!!";
71 static lwres_context_t
*ctx
;
76 lwres_lwpacket_t pkt
, pkt2
;
77 lwres_nooprequest_t nooprequest
, *nooprequest2
;
78 lwres_noopresponse_t noopresponse
, *noopresponse2
;
82 pkt
.serial
= 0x11223344;
83 pkt
.recvlength
= 0x55667788;
86 nooprequest
.datalength
= strlen(TESTSTRING
);
87 /* XXXDCL maybe "nooprequest.data" should be const. */
88 DE_CONST(TESTSTRING
, nooprequest
.data
);
89 ret
= lwres_nooprequest_render(ctx
, &nooprequest
, &pkt
, &b
);
90 CHECK(ret
, "lwres_nooprequest_render");
92 hexdump("rendered noop request", b
.base
, b
.used
);
95 * Now, parse it into a new structure.
97 lwres_buffer_first(&b
);
98 ret
= lwres_lwpacket_parseheader(&b
, &pkt2
);
99 CHECK(ret
, "lwres_lwpacket_parseheader");
101 hexdump("parsed pkt2", &pkt2
, sizeof(pkt2
));
104 ret
= lwres_nooprequest_parse(ctx
, &b
, &pkt2
, &nooprequest2
);
105 CHECK(ret
, "lwres_nooprequest_parse");
107 assert(nooprequest
.datalength
== nooprequest2
->datalength
);
108 assert(memcmp(nooprequest
.data
, nooprequest2
->data
,
109 nooprequest
.datalength
) == 0);
111 lwres_nooprequest_free(ctx
, &nooprequest2
);
113 lwres_context_freemem(ctx
, b
.base
, b
.length
);
118 pkt
.serial
= 0x11223344;
119 pkt
.recvlength
= 0x55667788;
120 pkt
.result
= 0xdeadbeef;
122 noopresponse
.datalength
= strlen(TESTSTRING
);
123 /* XXXDCL maybe "noopresponse.data" should be const. */
124 DE_CONST(TESTSTRING
, noopresponse
.data
);
125 ret
= lwres_noopresponse_render(ctx
, &noopresponse
, &pkt
, &b
);
126 CHECK(ret
, "lwres_noopresponse_render");
128 hexdump("rendered noop response", b
.base
, b
.used
);
131 * Now, parse it into a new structure.
133 lwres_buffer_first(&b
);
134 ret
= lwres_lwpacket_parseheader(&b
, &pkt2
);
135 CHECK(ret
, "lwres_lwpacket_parseheader");
137 hexdump("parsed pkt2", &pkt2
, sizeof(pkt2
));
139 noopresponse2
= NULL
;
140 ret
= lwres_noopresponse_parse(ctx
, &b
, &pkt2
, &noopresponse2
);
141 CHECK(ret
, "lwres_noopresponse_parse");
143 assert(noopresponse
.datalength
== noopresponse2
->datalength
);
144 assert(memcmp(noopresponse
.data
, noopresponse2
->data
,
145 noopresponse
.datalength
) == 0);
147 lwres_noopresponse_free(ctx
, &noopresponse2
);
149 lwres_context_freemem(ctx
, b
.base
, b
.length
);
155 test_gabn(const char *target
) {
156 lwres_gabnresponse_t
*res
;
163 ret
= lwres_getaddrsbyname(ctx
, target
,
164 LWRES_ADDRTYPE_V4
| LWRES_ADDRTYPE_V6
,
166 printf("gabn %s ret == %d\n", target
, ret
);
168 printf("FAILURE!\n");
170 lwres_gabnresponse_free(ctx
, &res
);
174 printf("Returned real name: (%u, %s)\n",
175 res
->realnamelen
, res
->realname
);
176 printf("%u aliases:\n", res
->naliases
);
177 for (i
= 0; i
< res
->naliases
; i
++)
178 printf("\t(%u, %s)\n", res
->aliaslen
[i
], res
->aliases
[i
]);
179 printf("%u addresses:\n", res
->naddrs
);
180 addr
= LWRES_LIST_HEAD(res
->addrs
);
181 for (i
= 0; i
< res
->naddrs
; i
++) {
182 INSIST(addr
!= NULL
);
184 if (addr
->family
== LWRES_ADDRTYPE_V4
)
185 (void)inet_ntop(AF_INET
, addr
->address
,
186 outbuf
, sizeof(outbuf
));
188 (void)inet_ntop(AF_INET6
, addr
->address
,
189 outbuf
, sizeof(outbuf
));
190 printf("\tAddr len %u family %08x %s\n",
191 addr
->length
, addr
->family
, outbuf
);
192 addr
= LWRES_LIST_NEXT(addr
, link
);
195 lwres_gabnresponse_free(ctx
, &res
);
199 test_gnba(const char *target
, lwres_uint32_t af
) {
200 lwres_gnbaresponse_t
*res
;
203 unsigned char addrbuf
[16];
206 if (af
== LWRES_ADDRTYPE_V4
) {
208 ret
= inet_pton(AF_INET
, target
, addrbuf
);
212 ret
= inet_pton(AF_INET6
, target
, addrbuf
);
217 ret
= lwres_getnamebyaddr(ctx
, af
, len
, addrbuf
, &res
);
218 printf("gnba %s ret == %d\n", target
, ret
);
222 printf("Returned real name: (%u, %s)\n",
223 res
->realnamelen
, res
->realname
);
224 printf("%u aliases:\n", res
->naliases
);
225 for (i
= 0; i
< res
->naliases
; i
++)
226 printf("\t(%u, %s)\n", res
->aliaslen
[i
], res
->aliases
[i
]);
228 lwres_gnbaresponse_free(ctx
, &res
);
233 * Wrappers around our memory management stuff, for the lwres functions.
236 mem_alloc(void *arg
, size_t size
) {
237 return (isc_mem_get(arg
, size
));
241 mem_free(void *arg
, void *mem
, size_t size
) {
242 isc_mem_put(arg
, mem
, size
);
247 main(int argc
, char *argv
[]) {
259 result
= isc_mem_create(0, 0, &mem
);
260 INSIST(result
== ISC_R_SUCCESS
);
265 ret
= lwres_context_create(&ctx
, mem
, mem_alloc
, mem_free
, 0);
267 ret
= lwres_context_create(&ctx
, NULL
, NULL
, NULL
, 0);
270 CHECK(ret
, "lwres_context_create");
272 ret
= lwres_conf_parse(ctx
, "/etc/resolv.conf");
273 CHECK(ret
, "lwres_conf_parse");
275 lwres_conf_print(ctx
, stdout
);
280 * The following comments about tests all assume your search path is
281 * nominum.com isc.org flame.org
282 * and ndots is the default of 1.
284 test_gabn("alias-05.test"); /* exact, then search. */
285 test_gabn("f.root-servers.net.");
286 test_gabn("poofball.flame.org.");
287 test_gabn("foo.ip6.int.");
288 test_gabn("notthereatall.flame.org"); /* exact, then search (!found)*/
289 test_gabn("shell"); /* search (found in nominum.com), then exact */
290 test_gabn("kechara"); /* search (found in flame.org), then exact */
291 test_gabn("lkasdjlaksjdlkasjdlkasjdlkasjd"); /* search, exact(!found)*/
293 test_gnba("198.133.199.1", LWRES_ADDRTYPE_V4
);
294 test_gnba("204.152.184.79", LWRES_ADDRTYPE_V4
);
295 test_gnba("3ffe:8050:201:1860:42::1", LWRES_ADDRTYPE_V6
);
297 lwres_conf_clear(ctx
);
298 lwres_context_destroy(&ctx
);
301 isc_mem_stats(mem
, stdout
);
302 isc_mem_destroy(&mem
);