Remove building with NOCRYPTO option
[minix.git] / external / bsd / bind / dist / bin / tests / lwres_test.c
blobee8b6878d77a285de14ba618126752136d5f46d8
1 /* $NetBSD: lwres_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
3 /*
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 */
22 #include <config.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <isc/mem.h>
29 #include <isc/netaddr.h>
30 #include <isc/util.h>
32 #include <lwres/lwres.h>
34 #define USE_ISC_MEM
36 static inline void
37 CHECK(int val, const char *msg) {
38 if (val != 0) {
39 fprintf(stderr, "%s returned %d\n", msg, val);
40 exit(1);
44 static void
45 hexdump(const char *msg, void *base, size_t len) {
46 unsigned char *p;
47 unsigned int cnt;
49 p = base;
50 cnt = 0;
52 printf("*** %s (%lu bytes @ %p)\n", msg, (unsigned long)len, base);
54 while (cnt < len) {
55 if (cnt % 16 == 0)
56 printf("%p: ", p);
57 else if (cnt % 8 == 0)
58 printf(" |");
59 printf(" %02x", *p++);
60 cnt++;
62 if (cnt % 16 == 0)
63 printf("\n");
66 if (cnt % 16 != 0)
67 printf("\n");
70 static const char *TESTSTRING = "This is a test. This is only a test. !!!";
71 static lwres_context_t *ctx;
73 static void
74 test_noop(void) {
75 int ret;
76 lwres_lwpacket_t pkt, pkt2;
77 lwres_nooprequest_t nooprequest, *nooprequest2;
78 lwres_noopresponse_t noopresponse, *noopresponse2;
79 lwres_buffer_t b;
81 pkt.pktflags = 0;
82 pkt.serial = 0x11223344;
83 pkt.recvlength = 0x55667788;
84 pkt.result = 0;
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));
103 nooprequest2 = NULL;
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);
114 b.base = NULL;
115 b.length = 0;
117 pkt.pktflags = 0;
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);
150 b.base = NULL;
151 b.length = 0;
154 static void
155 test_gabn(const char *target) {
156 lwres_gabnresponse_t *res;
157 lwres_addr_t *addr;
158 int ret;
159 unsigned int i;
160 char outbuf[64];
162 res = NULL;
163 ret = lwres_getaddrsbyname(ctx, target,
164 LWRES_ADDRTYPE_V4 | LWRES_ADDRTYPE_V6,
165 &res);
166 printf("gabn %s ret == %d\n", target, ret);
167 if (ret != 0) {
168 printf("FAILURE!\n");
169 if (res != NULL)
170 lwres_gabnresponse_free(ctx, &res);
171 return;
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));
187 else
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);
198 static void
199 test_gnba(const char *target, lwres_uint32_t af) {
200 lwres_gnbaresponse_t *res;
201 int ret;
202 unsigned int i;
203 unsigned char addrbuf[16];
204 unsigned int len;
206 if (af == LWRES_ADDRTYPE_V4) {
207 len = 4;
208 ret = inet_pton(AF_INET, target, addrbuf);
209 assert(ret == 1);
210 } else {
211 len = 16;
212 ret = inet_pton(AF_INET6, target, addrbuf);
213 assert(ret == 1);
216 res = NULL;
217 ret = lwres_getnamebyaddr(ctx, af, len, addrbuf, &res);
218 printf("gnba %s ret == %d\n", target, ret);
219 assert(ret == 0);
220 assert(res != NULL);
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);
231 #ifdef USE_ISC_MEM
233 * Wrappers around our memory management stuff, for the lwres functions.
235 static void *
236 mem_alloc(void *arg, size_t size) {
237 return (isc_mem_get(arg, size));
240 static void
241 mem_free(void *arg, void *mem, size_t size) {
242 isc_mem_put(arg, mem, size);
244 #endif
247 main(int argc, char *argv[]) {
248 int ret;
249 #ifdef USE_ISC_MEM
250 isc_mem_t *mem;
251 isc_result_t result;
252 #endif
254 (void)argc;
255 (void)argv;
257 #ifdef USE_ISC_MEM
258 mem = NULL;
259 result = isc_mem_create(0, 0, &mem);
260 INSIST(result == ISC_R_SUCCESS);
261 #endif
263 ctx = NULL;
264 #ifdef USE_ISC_MEM
265 ret = lwres_context_create(&ctx, mem, mem_alloc, mem_free, 0);
266 #else
267 ret = lwres_context_create(&ctx, NULL, NULL, NULL, 0);
268 #endif
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);
277 test_noop();
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);
300 #ifdef USE_ISC_MEM
301 isc_mem_stats(mem, stdout);
302 isc_mem_destroy(&mem);
303 #endif
305 return (0);