1 /* $NetBSD: regress_dns.c,v 1.4 2013/04/11 20:14:44 christos Exp $ */
3 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "event2/event-config.h"
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: regress_dns.c,v 1.4 2013/04/11 20:14:44 christos Exp $");
39 #include <sys/types.h>
41 #ifdef _EVENT_HAVE_SYS_TIME_H
44 #include <sys/queue.h>
46 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
52 #ifdef _EVENT_HAVE_NETINET_IN6_H
53 #include <netinet/in6.h>
64 #include "event2/dns.h"
65 #include "event2/dns_compat.h"
66 #include "event2/dns_struct.h"
67 #include "event2/event.h"
68 #include "event2/event_compat.h"
69 #include "event2/event_struct.h"
70 #include "event2/util.h"
71 #include "event2/listener.h"
72 #include "event2/bufferevent.h"
73 #include "log-internal.h"
75 #include "regress_testutils.h"
77 #include "../util-internal.h"
79 static int dns_ok
= 0;
80 static int dns_got_cancel
= 0;
81 static int dns_err
= 0;
85 dns_gethostbyname_cb(int result
, char type
, int count
, int ttl
,
86 void *addresses
, void *arg
)
90 if (result
== DNS_ERR_TIMEOUT
) {
91 printf("[Timed out] ");
96 if (result
!= DNS_ERR_NONE
) {
97 printf("[Error code %d] ", result
);
101 TT_BLATHER(("type: %d, count: %d, ttl: %d: ", type
, count
, ttl
));
104 case DNS_IPv6_AAAA
: {
105 #if defined(_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
106 struct in6_addr
*in6_addrs
= addresses
;
107 char buf
[INET6_ADDRSTRLEN
+1];
109 /* a resolution that's not valid does not help */
112 for (i
= 0; i
< count
; ++i
) {
113 const char *b
= evutil_inet_ntop(AF_INET6
, &in6_addrs
[i
], buf
,sizeof(buf
));
115 TT_BLATHER(("%s ", b
));
117 TT_BLATHER(("%s ", strerror(errno
)));
123 struct in_addr
*in_addrs
= addresses
;
125 /* a resolution that's not valid does not help */
128 for (i
= 0; i
< count
; ++i
)
129 TT_BLATHER(("%s ", inet_ntoa(in_addrs
[i
])));
133 /* may get at most one PTR */
137 TT_BLATHER(("%s ", *(char **)addresses
));
147 event_loopexit(NULL
);
149 event_base_loopexit((struct event_base
*)arg
, NULL
);
153 dns_gethostbyname(void)
156 evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb
, NULL
);
159 tt_int_op(dns_ok
, ==, DNS_IPv4_A
);
166 dns_gethostbyname6(void)
169 evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb
, NULL
);
172 if (!dns_ok
&& dns_err
== DNS_ERR_TIMEOUT
) {
176 tt_int_op(dns_ok
, ==, DNS_IPv6_AAAA
);
183 dns_gethostbyaddr(void)
186 in
.s_addr
= htonl(0x7f000001ul
); /* 127.0.0.1 */
188 evdns_resolve_reverse(&in
, 0, dns_gethostbyname_cb
, NULL
);
191 tt_int_op(dns_ok
, ==, DNS_PTR
);
198 dns_resolve_reverse(void *ptr
)
201 struct event_base
*base
= event_base_new();
202 struct evdns_base
*dns
= evdns_base_new(base
, 1/* init name servers */);
203 struct evdns_request
*req
= NULL
;
207 in
.s_addr
= htonl(0x7f000001ul
); /* 127.0.0.1 */
210 req
= evdns_base_resolve_reverse(
211 dns
, &in
, 0, dns_gethostbyname_cb
, base
);
214 event_base_dispatch(base
);
216 tt_int_op(dns_ok
, ==, DNS_PTR
);
220 evdns_base_free(dns
, 0);
222 event_base_free(base
);
225 static int n_server_responses
= 0;
228 dns_server_request_cb(struct evdns_server_request
*req
, void *data
)
231 const char TEST_ARPA
[] = "11.11.168.192.in-addr.arpa";
232 const char TEST_IN6
[] =
233 "f.e.f.e." "0.0.0.0." "0.0.0.0." "1.1.1.1."
234 "a.a.a.a." "0.0.0.0." "0.0.0.0." "0.f.f.f.ip6.arpa";
236 for (i
= 0; i
< req
->nquestions
; ++i
) {
237 const int qtype
= req
->questions
[i
]->type
;
238 const int qclass
= req
->questions
[i
]->dns_question_class
;
239 const char *qname
= req
->questions
[i
]->name
;
242 ans
.s_addr
= htonl(0xc0a80b0bUL
); /* 192.168.11.11 */
243 if (qtype
== EVDNS_TYPE_A
&&
244 qclass
== EVDNS_CLASS_INET
&&
245 !evutil_ascii_strcasecmp(qname
, "zz.example.com")) {
246 r
= evdns_server_request_add_a_reply(req
, qname
,
247 1, &ans
.s_addr
, 12345);
250 } else if (qtype
== EVDNS_TYPE_AAAA
&&
251 qclass
== EVDNS_CLASS_INET
&&
252 !evutil_ascii_strcasecmp(qname
, "zz.example.com")) {
253 char addr6
[17] = "abcdefghijklmnop";
254 r
= evdns_server_request_add_aaaa_reply(req
,
255 qname
, 1, addr6
, 123);
258 } else if (qtype
== EVDNS_TYPE_PTR
&&
259 qclass
== EVDNS_CLASS_INET
&&
260 !evutil_ascii_strcasecmp(qname
, TEST_ARPA
)) {
261 r
= evdns_server_request_add_ptr_reply(req
, NULL
,
262 qname
, "ZZ.EXAMPLE.COM", 54321);
265 } else if (qtype
== EVDNS_TYPE_PTR
&&
266 qclass
== EVDNS_CLASS_INET
&&
267 !evutil_ascii_strcasecmp(qname
, TEST_IN6
)){
268 r
= evdns_server_request_add_ptr_reply(req
, NULL
,
270 "ZZ-INET6.EXAMPLE.COM", 54322);
273 } else if (qtype
== EVDNS_TYPE_A
&&
274 qclass
== EVDNS_CLASS_INET
&&
275 !evutil_ascii_strcasecmp(qname
, "drop.example.com")) {
276 if (evdns_server_request_drop(req
)<0)
280 printf("Unexpected question %d %d \"%s\" ",
281 qtype
, qclass
, qname
);
285 r
= evdns_server_request_respond(req
, 0);
287 printf("Couldn't send reply. ");
293 dns_server_gethostbyname_cb(int result
, char type
, int count
, int ttl
,
294 void *addresses
, void *arg
)
296 if (result
== DNS_ERR_CANCEL
) {
297 if (arg
!= (void*)(char*)90909) {
298 printf("Unexpected cancelation");
304 if (result
!= DNS_ERR_NONE
) {
305 printf("Unexpected result %d. ", result
);
310 printf("Unexpected answer count %d. ", count
);
316 struct in_addr
*in_addrs
= addresses
;
317 if (in_addrs
[0].s_addr
!= htonl(0xc0a80b0bUL
) || ttl
!= 12345) {
318 printf("Bad IPv4 response \"%s\" %d. ",
319 inet_ntoa(in_addrs
[0]), ttl
);
325 case DNS_IPv6_AAAA
: {
326 #if defined (_EVENT_HAVE_STRUCT_IN6_ADDR) && defined(_EVENT_HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
327 struct in6_addr
*in6_addrs
= addresses
;
328 char buf
[INET6_ADDRSTRLEN
+1];
329 if (memcmp(&in6_addrs
[0].s6_addr
, "abcdefghijklmnop", 16)
331 const char *b
= evutil_inet_ntop(AF_INET6
, &in6_addrs
[0],buf
,sizeof(buf
));
332 printf("Bad IPv6 response \"%s\" %d. ", b
, ttl
);
340 char **addrs
= addresses
;
341 if (arg
!= (void*)6) {
342 if (strcmp(addrs
[0], "ZZ.EXAMPLE.COM") ||
344 printf("Bad PTR response \"%s\" %d. ",
350 if (strcmp(addrs
[0], "ZZ-INET6.EXAMPLE.COM") ||
352 printf("Bad ipv6 PTR response \"%s\" %d. ",
361 printf("Bad response type %d. ", type
);
365 if (++n_server_responses
== 3) {
366 event_loopexit(NULL
);
373 evutil_socket_t sock
=-1;
374 struct sockaddr_in my_addr
;
375 struct sockaddr_storage ss
;
377 struct evdns_server_port
*port
=NULL
;
378 struct in_addr resolve_addr
;
379 struct in6_addr resolve_addr6
;
380 struct evdns_base
*base
=NULL
;
381 struct evdns_request
*req
=NULL
;
385 base
= evdns_base_new(NULL
, 0);
387 /* Now configure a nameserver port. */
388 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
390 tt_abort_perror("socket");
393 evutil_make_socket_nonblocking(sock
);
395 memset(&my_addr
, 0, sizeof(my_addr
));
396 my_addr
.sin_family
= AF_INET
;
397 my_addr
.sin_port
= 0; /* kernel picks */
398 my_addr
.sin_addr
.s_addr
= htonl(0x7f000001UL
);
399 if (bind(sock
, (struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0) {
400 tt_abort_perror("bind");
403 if (getsockname(sock
, (struct sockaddr
*)&ss
, &slen
) < 0) {
404 tt_abort_perror("getsockname");
407 port
= evdns_add_server_port(sock
, 0, dns_server_request_cb
, NULL
);
409 /* Add ourself as the only nameserver, and make sure we really are
410 * the only nameserver. */
411 evdns_base_nameserver_sockaddr_add(base
, (struct sockaddr
*)&ss
, slen
, 0);
412 tt_int_op(evdns_base_count_nameservers(base
), ==, 1);
414 /* Send some queries. */
415 evdns_base_resolve_ipv4(base
, "zz.example.com", DNS_QUERY_NO_SEARCH
,
416 dns_server_gethostbyname_cb
, NULL
);
417 evdns_base_resolve_ipv6(base
, "zz.example.com", DNS_QUERY_NO_SEARCH
,
418 dns_server_gethostbyname_cb
, NULL
);
419 resolve_addr
.s_addr
= htonl(0xc0a80b0bUL
); /* 192.168.11.11 */
420 evdns_base_resolve_reverse(base
, &resolve_addr
, 0,
421 dns_server_gethostbyname_cb
, NULL
);
422 memcpy(resolve_addr6
.s6_addr
,
423 "\xff\xf0\x00\x00\x00\x00\xaa\xaa"
424 "\x11\x11\x00\x00\x00\x00\xef\xef", 16);
425 evdns_base_resolve_reverse_ipv6(base
, &resolve_addr6
, 0,
426 dns_server_gethostbyname_cb
, (void*)6);
428 req
= evdns_base_resolve_ipv4(base
,
429 "drop.example.com", DNS_QUERY_NO_SEARCH
,
430 dns_server_gethostbyname_cb
, (void*)(char*)90909);
432 evdns_cancel_request(base
, req
);
436 tt_assert(dns_got_cancel
);
441 evdns_close_server_port(port
);
443 evutil_closesocket(sock
);
445 evdns_base_free(base
, 0);
448 static int n_replies_left
;
449 static struct event_base
*exit_base
;
451 struct generic_dns_callback_result
{
462 generic_dns_callback(int result
, char type
, int count
, int ttl
, void *addresses
,
466 struct generic_dns_callback_result
*res
= arg
;
467 res
->result
= result
;
472 if (type
== DNS_IPv4_A
)
474 else if (type
== DNS_IPv6_AAAA
)
476 else if (type
== DNS_PTR
)
477 len
= strlen(addresses
)+1;
479 res
->addrs_len
= len
= 0;
483 res
->addrs_len
= len
;
486 memcpy(res
->addrs_buf
, addresses
, len
);
487 res
->addrs
= res
->addrs_buf
;
490 if (--n_replies_left
== 0)
491 event_base_loopexit(exit_base
, NULL
);
494 static struct regress_dns_server_table search_table
[] = {
495 { "host.a.example.com", "err", "3", 0 },
496 { "host.b.example.com", "err", "3", 0 },
497 { "host.c.example.com", "A", "11.22.33.44", 0 },
498 { "host2.a.example.com", "err", "3", 0 },
499 { "host2.b.example.com", "A", "200.100.0.100", 0 },
500 { "host2.c.example.com", "err", "3", 0 },
501 { "hostn.a.example.com", "errsoa", "0", 0 },
502 { "hostn.b.example.com", "errsoa", "3", 0 },
503 { "hostn.c.example.com", "err", "0", 0 },
505 { "host", "err", "3", 0 },
506 { "host2", "err", "3", 0 },
507 { "*", "err", "3", 0 },
508 { NULL
, NULL
, NULL
, 0 }
512 dns_search_test(void *arg
)
514 struct basic_test_data
*data
= arg
;
515 struct event_base
*base
= data
->base
;
516 struct evdns_base
*dns
= NULL
;
517 ev_uint16_t portnum
= 0;
520 struct generic_dns_callback_result r
[8];
522 tt_assert(regress_dnsserver(base
, &portnum
, search_table
));
523 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", (int)portnum
);
525 dns
= evdns_base_new(base
, 0);
526 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf
));
528 evdns_base_search_add(dns
, "a.example.com");
529 evdns_base_search_add(dns
, "b.example.com");
530 evdns_base_search_add(dns
, "c.example.com");
532 n_replies_left
= sizeof(r
)/sizeof(r
[0]);
535 evdns_base_resolve_ipv4(dns
, "host", 0, generic_dns_callback
, &r
[0]);
536 evdns_base_resolve_ipv4(dns
, "host2", 0, generic_dns_callback
, &r
[1]);
537 evdns_base_resolve_ipv4(dns
, "host", DNS_NO_SEARCH
, generic_dns_callback
, &r
[2]);
538 evdns_base_resolve_ipv4(dns
, "host2", DNS_NO_SEARCH
, generic_dns_callback
, &r
[3]);
539 evdns_base_resolve_ipv4(dns
, "host3", 0, generic_dns_callback
, &r
[4]);
540 evdns_base_resolve_ipv4(dns
, "hostn.a.example.com", DNS_NO_SEARCH
, generic_dns_callback
, &r
[5]);
541 evdns_base_resolve_ipv4(dns
, "hostn.b.example.com", DNS_NO_SEARCH
, generic_dns_callback
, &r
[6]);
542 evdns_base_resolve_ipv4(dns
, "hostn.c.example.com", DNS_NO_SEARCH
, generic_dns_callback
, &r
[7]);
544 event_base_dispatch(base
);
546 tt_int_op(r
[0].type
, ==, DNS_IPv4_A
);
547 tt_int_op(r
[0].count
, ==, 1);
548 tt_int_op(((ev_uint32_t
*)r
[0].addrs
)[0], ==, htonl(0x0b16212c));
549 tt_int_op(r
[1].type
, ==, DNS_IPv4_A
);
550 tt_int_op(r
[1].count
, ==, 1);
551 tt_int_op(((ev_uint32_t
*)r
[1].addrs
)[0], ==, htonl(0xc8640064));
552 tt_int_op(r
[2].result
, ==, DNS_ERR_NOTEXIST
);
553 tt_int_op(r
[3].result
, ==, DNS_ERR_NOTEXIST
);
554 tt_int_op(r
[4].result
, ==, DNS_ERR_NOTEXIST
);
555 tt_int_op(r
[5].result
, ==, DNS_ERR_NODATA
);
556 tt_int_op(r
[5].ttl
, ==, 42);
557 tt_int_op(r
[6].result
, ==, DNS_ERR_NOTEXIST
);
558 tt_int_op(r
[6].ttl
, ==, 42);
559 tt_int_op(r
[7].result
, ==, DNS_ERR_NODATA
);
560 tt_int_op(r
[7].ttl
, ==, 0);
564 evdns_base_free(dns
, 0);
566 regress_clean_dnsserver();
569 static int request_count
= 0;
570 static struct evdns_request
*current_req
= NULL
;
573 search_cancel_server_cb(struct evdns_server_request
*req
, void *data
)
575 const char *question
;
577 if (req
->nquestions
!= 1)
578 TT_DIE(("Only handling one question at a time; got %d",
581 question
= req
->questions
[0]->name
;
583 TT_BLATHER(("got question, %s", question
));
585 tt_assert(request_count
> 0);
586 tt_assert(!evdns_server_request_respond(req
, 3));
588 if (!--request_count
)
589 evdns_cancel_request(NULL
, current_req
);
596 dns_search_cancel_test(void *arg
)
598 struct basic_test_data
*data
= arg
;
599 struct event_base
*base
= data
->base
;
600 struct evdns_base
*dns
= NULL
;
601 struct evdns_server_port
*port
= NULL
;
602 ev_uint16_t portnum
= 0;
603 struct generic_dns_callback_result r1
;
606 port
= regress_get_dnsserver(base
, &portnum
, NULL
,
607 search_cancel_server_cb
, NULL
);
609 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", (int)portnum
);
611 dns
= evdns_base_new(base
, 0);
612 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf
));
614 evdns_base_search_add(dns
, "a.example.com");
615 evdns_base_search_add(dns
, "b.example.com");
616 evdns_base_search_add(dns
, "c.example.com");
617 evdns_base_search_add(dns
, "d.example.com");
623 current_req
= evdns_base_resolve_ipv4(dns
, "host", 0,
624 generic_dns_callback
, &r1
);
625 event_base_dispatch(base
);
627 tt_int_op(r1
.result
, ==, DNS_ERR_CANCEL
);
631 evdns_close_server_port(port
);
633 evdns_base_free(dns
, 0);
637 fail_server_cb(struct evdns_server_request
*req
, void *data
)
639 const char *question
;
643 /* Drop the first N requests that we get. */
646 tt_want(! evdns_server_request_drop(req
));
650 if (req
->nquestions
!= 1)
651 TT_DIE(("Only handling one question at a time; got %d",
654 question
= req
->questions
[0]->name
;
656 if (!evutil_ascii_strcasecmp(question
, "google.com")) {
657 /* Detect a probe, and get out of the loop. */
658 event_base_loopexit(exit_base
, NULL
);
661 evutil_inet_pton(AF_INET
, "16.32.64.128", &in
);
662 evdns_server_request_add_a_reply(req
, question
, 1, &in
.s_addr
,
664 tt_assert(! evdns_server_request_respond(req
, 0))
667 tt_want(! evdns_server_request_drop(req
));
671 dns_retry_test(void *arg
)
673 struct basic_test_data
*data
= arg
;
674 struct event_base
*base
= data
->base
;
675 struct evdns_server_port
*port
= NULL
;
676 struct evdns_base
*dns
= NULL
;
678 ev_uint16_t portnum
= 0;
681 struct generic_dns_callback_result r1
;
683 port
= regress_get_dnsserver(base
, &portnum
, NULL
,
684 fail_server_cb
, &drop_count
);
686 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", (int)portnum
);
688 dns
= evdns_base_new(base
, 0);
689 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf
));
690 tt_assert(! evdns_base_set_option(dns
, "timeout", "0.3"));
691 tt_assert(! evdns_base_set_option(dns
, "max-timeouts:", "10"));
692 tt_assert(! evdns_base_set_option(dns
, "initial-probe-timeout", "0.5"));
694 evdns_base_resolve_ipv4(dns
, "host.example.com", 0,
695 generic_dns_callback
, &r1
);
700 event_base_dispatch(base
);
702 tt_int_op(drop_count
, ==, 0);
704 tt_int_op(r1
.type
, ==, DNS_IPv4_A
);
705 tt_int_op(r1
.count
, ==, 1);
706 tt_int_op(((ev_uint32_t
*)r1
.addrs
)[0], ==, htonl(0x10204080));
708 /* Now try again, but this time have the server get treated as
709 * failed, so we can send it a test probe. */
711 tt_assert(! evdns_base_set_option(dns
, "max-timeouts:", "3"));
712 tt_assert(! evdns_base_set_option(dns
, "attempts:", "4"));
713 memset(&r1
, 0, sizeof(r1
));
715 evdns_base_resolve_ipv4(dns
, "host.example.com", 0,
716 generic_dns_callback
, &r1
);
720 /* This will run until it answers the "google.com" probe request. */
721 event_base_dispatch(base
);
723 /* We'll treat the server as failed here. */
724 tt_int_op(r1
.result
, ==, DNS_ERR_TIMEOUT
);
726 /* It should work this time. */
727 tt_int_op(drop_count
, ==, 0);
728 evdns_base_resolve_ipv4(dns
, "host.example.com", 0,
729 generic_dns_callback
, &r1
);
731 event_base_dispatch(base
);
732 tt_int_op(r1
.result
, ==, DNS_ERR_NONE
);
733 tt_int_op(r1
.type
, ==, DNS_IPv4_A
);
734 tt_int_op(r1
.count
, ==, 1);
735 tt_int_op(((ev_uint32_t
*)r1
.addrs
)[0], ==, htonl(0x10204080));
739 evdns_base_free(dns
, 0);
741 evdns_close_server_port(port
);
744 static struct regress_dns_server_table internal_error_table
[] = {
745 /* Error 4 (NOTIMPL) makes us reissue the request to another server
748 XXXX we should reissue under a much wider set of circumstances!
750 { "foof.example.com", "err", "4", 0 },
751 { NULL
, NULL
, NULL
, 0 }
754 static struct regress_dns_server_table reissue_table
[] = {
755 { "foof.example.com", "A", "240.15.240.15", 0 },
756 { NULL
, NULL
, NULL
, 0 }
760 dns_reissue_test(void *arg
)
762 struct basic_test_data
*data
= arg
;
763 struct event_base
*base
= data
->base
;
764 struct evdns_server_port
*port1
= NULL
, *port2
= NULL
;
765 struct evdns_base
*dns
= NULL
;
766 struct generic_dns_callback_result r1
;
767 ev_uint16_t portnum1
= 0, portnum2
=0;
768 char buf1
[64], buf2
[64];
770 port1
= regress_get_dnsserver(base
, &portnum1
, NULL
,
771 regress_dns_server_cb
, internal_error_table
);
773 port2
= regress_get_dnsserver(base
, &portnum2
, NULL
,
774 regress_dns_server_cb
, reissue_table
);
776 evutil_snprintf(buf1
, sizeof(buf1
), "127.0.0.1:%d", (int)portnum1
);
777 evutil_snprintf(buf2
, sizeof(buf2
), "127.0.0.1:%d", (int)portnum2
);
779 dns
= evdns_base_new(base
, 0);
780 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf1
));
781 tt_assert(! evdns_base_set_option(dns
, "timeout:", "0.3"));
782 tt_assert(! evdns_base_set_option(dns
, "max-timeouts:", "2"));
783 tt_assert(! evdns_base_set_option(dns
, "attempts:", "5"));
785 memset(&r1
, 0, sizeof(r1
));
786 evdns_base_resolve_ipv4(dns
, "foof.example.com", 0,
787 generic_dns_callback
, &r1
);
789 /* Add this after, so that we are sure to get a reissue. */
790 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf2
));
795 event_base_dispatch(base
);
796 tt_int_op(r1
.result
, ==, DNS_ERR_NONE
);
797 tt_int_op(r1
.type
, ==, DNS_IPv4_A
);
798 tt_int_op(r1
.count
, ==, 1);
799 tt_int_op(((ev_uint32_t
*)r1
.addrs
)[0], ==, htonl(0xf00ff00f));
801 /* Make sure we dropped at least once. */
802 tt_int_op(internal_error_table
[0].seen
, >, 0);
806 evdns_base_free(dns
, 0);
808 evdns_close_server_port(port1
);
810 evdns_close_server_port(port2
);
815 dumb_bytes_fn(char *p
, size_t n
)
818 /* This gets us 6 bits of entropy per transaction ID, which means we
819 * will have probably have collisions and need to pick again. */
821 p
[i
] = (char)(rand() & 7);
826 dns_inflight_test(void *arg
)
828 struct basic_test_data
*data
= arg
;
829 struct event_base
*base
= data
->base
;
830 struct evdns_base
*dns
= NULL
;
831 ev_uint16_t portnum
= 0;
834 struct generic_dns_callback_result r
[20];
837 tt_assert(regress_dnsserver(base
, &portnum
, reissue_table
));
838 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", (int)portnum
);
840 dns
= evdns_base_new(base
, 0);
841 tt_assert(!evdns_base_nameserver_ip_add(dns
, buf
));
842 tt_assert(! evdns_base_set_option(dns
, "max-inflight:", "3"));
843 tt_assert(! evdns_base_set_option(dns
, "randomize-case:", "0"));
846 evdns_base_resolve_ipv4(dns
, "foof.example.com", 0, generic_dns_callback
, &r
[i
]);
851 event_base_dispatch(base
);
854 tt_int_op(r
[i
].type
, ==, DNS_IPv4_A
);
855 tt_int_op(r
[i
].count
, ==, 1);
856 tt_int_op(((ev_uint32_t
*)r
[i
].addrs
)[0], ==, htonl(0xf00ff00f));
861 evdns_base_free(dns
, 0);
862 regress_clean_dnsserver();
865 /* === Test for bufferevent_socket_connect_hostname */
867 static int total_connected_or_failed
= 0;
868 static int total_n_accepted
= 0;
869 static struct event_base
*be_connect_hostname_base
= NULL
;
871 /* Implements a DNS server for the connect_hostname test and the
872 * getaddrinfo_async test */
874 be_getaddrinfo_server_cb(struct evdns_server_request
*req
, void *data
)
881 for (i
=0;i
<req
->nquestions
;++i
) {
882 const int qtype
= req
->questions
[i
]->type
;
883 const int qclass
= req
->questions
[i
]->dns_question_class
;
884 const char *qname
= req
->questions
[i
]->name
;
886 struct in6_addr ans6
;
887 memset(&ans6
, 0, sizeof(ans6
));
889 if (qtype
== EVDNS_TYPE_A
&&
890 qclass
== EVDNS_CLASS_INET
&&
891 !evutil_ascii_strcasecmp(qname
, "nobodaddy.example.com")) {
892 ans
.s_addr
= htonl(0x7f000001);
893 evdns_server_request_add_a_reply(req
, qname
,
894 1, &ans
.s_addr
, 2000);
896 } else if (!evutil_ascii_strcasecmp(qname
,
897 "nosuchplace.example.com")) {
898 /* ok, just say notfound. */
899 } else if (!evutil_ascii_strcasecmp(qname
,
900 "both.example.com")) {
901 if (qtype
== EVDNS_TYPE_A
) {
902 ans
.s_addr
= htonl(0x50502020);
903 evdns_server_request_add_a_reply(req
, qname
,
904 1, &ans
.s_addr
, 2000);
906 } else if (qtype
== EVDNS_TYPE_AAAA
) {
907 ans6
.s6_addr
[0] = 0x80;
908 ans6
.s6_addr
[1] = 0xff;
909 ans6
.s6_addr
[14] = 0xbb;
910 ans6
.s6_addr
[15] = 0xbb;
911 evdns_server_request_add_aaaa_reply(req
, qname
,
912 1, &ans6
.s6_addr
, 2000);
915 evdns_server_request_add_cname_reply(req
, qname
,
916 "both-canonical.example.com", 1000);
917 } else if (!evutil_ascii_strcasecmp(qname
,
918 "v4only.example.com") ||
919 !evutil_ascii_strcasecmp(qname
, "v4assert.example.com")) {
920 if (qtype
== EVDNS_TYPE_A
) {
921 ans
.s_addr
= htonl(0x12345678);
922 evdns_server_request_add_a_reply(req
, qname
,
923 1, &ans
.s_addr
, 2000);
925 } else if (!evutil_ascii_strcasecmp(qname
,
926 "v4assert.example.com")) {
927 TT_FAIL(("Got an AAAA request for v4assert"));
929 } else if (!evutil_ascii_strcasecmp(qname
,
930 "v6only.example.com") ||
931 !evutil_ascii_strcasecmp(qname
, "v6assert.example.com")) {
932 if (qtype
== EVDNS_TYPE_AAAA
) {
933 ans6
.s6_addr
[0] = 0x0b;
934 ans6
.s6_addr
[1] = 0x0b;
935 ans6
.s6_addr
[14] = 0xf0;
936 ans6
.s6_addr
[15] = 0x0d;
937 evdns_server_request_add_aaaa_reply(req
, qname
,
938 1, &ans6
.s6_addr
, 2000);
940 } else if (!evutil_ascii_strcasecmp(qname
,
941 "v6assert.example.com")) {
942 TT_FAIL(("Got a A request for v6assert"));
944 } else if (!evutil_ascii_strcasecmp(qname
,
945 "v6timeout.example.com")) {
946 if (qtype
== EVDNS_TYPE_A
) {
947 ans
.s_addr
= htonl(0xabcdef01);
948 evdns_server_request_add_a_reply(req
, qname
,
949 1, &ans
.s_addr
, 2000);
951 } else if (qtype
== EVDNS_TYPE_AAAA
) {
952 /* Let the v6 request time out.*/
953 evdns_server_request_drop(req
);
956 } else if (!evutil_ascii_strcasecmp(qname
,
957 "v4timeout.example.com")) {
958 if (qtype
== EVDNS_TYPE_AAAA
) {
959 ans6
.s6_addr
[0] = 0x0a;
960 ans6
.s6_addr
[1] = 0x0a;
961 ans6
.s6_addr
[14] = 0xff;
962 ans6
.s6_addr
[15] = 0x01;
963 evdns_server_request_add_aaaa_reply(req
, qname
,
964 1, &ans6
.s6_addr
, 2000);
966 } else if (qtype
== EVDNS_TYPE_A
) {
967 /* Let the v4 request time out.*/
968 evdns_server_request_drop(req
);
971 } else if (!evutil_ascii_strcasecmp(qname
,
972 "v6timeout-nonexist.example.com")) {
973 if (qtype
== EVDNS_TYPE_A
) {
974 /* Fall through, give an nexist. */
975 } else if (qtype
== EVDNS_TYPE_AAAA
) {
976 /* Let the v6 request time out.*/
977 evdns_server_request_drop(req
);
980 } else if (!evutil_ascii_strcasecmp(qname
,
981 "all-timeout.example.com")) {
982 /* drop all requests */
983 evdns_server_request_drop(req
);
986 TT_GRIPE(("Got weird request for %s",qname
));
990 evdns_server_request_respond(req
, 0);
992 evdns_server_request_respond(req
, 3);
995 /* Implements a listener for connect_hostname test. */
997 nil_accept_cb(struct evconnlistener
*l
, evutil_socket_t fd
, struct sockaddr
*s
,
998 int socklen
, void *arg
)
1003 /* don't do anything with the socket; let it close when we exit() */
1004 if (total_n_accepted
>= 3 && total_connected_or_failed
>= 5)
1005 event_base_loopexit(be_connect_hostname_base
,
1009 struct be_conn_hostname_result
{
1014 /* Bufferevent event callback for the connect_hostname test: remembers what
1017 be_connect_hostname_event_cb(struct bufferevent
*bev
, short what
, void *ctx
)
1019 struct be_conn_hostname_result
*got
= ctx
;
1021 TT_BLATHER(("Got a bufferevent event %d", what
));
1024 if ((what
& BEV_EVENT_CONNECTED
) || (what
& BEV_EVENT_ERROR
)) {
1026 if ((r
= bufferevent_socket_get_dns_error(bev
))) {
1028 TT_BLATHER(("DNS error %d: %s", r
,
1029 evutil_gai_strerror(r
)));
1030 } ++total_connected_or_failed
;
1031 TT_BLATHER(("Got %d connections or errors.", total_connected_or_failed
));
1033 if (total_n_accepted
>= 3 && total_connected_or_failed
>= 5)
1034 event_base_loopexit(be_connect_hostname_base
,
1038 TT_FAIL(("Two events on one bufferevent. %d,%d",
1039 got
->what
, (int)what
));
1044 test_bufferevent_connect_hostname(void *arg
)
1046 struct basic_test_data
*data
= arg
;
1047 struct evconnlistener
*listener
= NULL
;
1048 struct bufferevent
*be1
=NULL
, *be2
=NULL
, *be3
=NULL
, *be4
=NULL
, *be5
=NULL
;
1049 struct be_conn_hostname_result be1_outcome
={0,0}, be2_outcome
={0,0},
1050 be3_outcome
={0,0}, be4_outcome
={0,0}, be5_outcome
={0,0};
1052 struct evdns_base
*dns
=NULL
;
1053 struct evdns_server_port
*port
=NULL
;
1054 struct sockaddr_in sin
;
1055 int listener_port
=-1;
1056 ev_uint16_t dns_port
=0;
1057 int n_accept
=0, n_dns
=0;
1060 be_connect_hostname_base
= data
->base
;
1062 /* Bind an address and figure out what port it's on. */
1063 memset(&sin
, 0, sizeof(sin
));
1064 sin
.sin_family
= AF_INET
;
1065 sin
.sin_addr
.s_addr
= htonl(0x7f000001); /* 127.0.0.1 */
1067 listener
= evconnlistener_new_bind(data
->base
, nil_accept_cb
,
1069 LEV_OPT_REUSEABLE
|LEV_OPT_CLOSE_ON_EXEC
,
1070 -1, (struct sockaddr
*)&sin
, sizeof(sin
));
1071 tt_assert(listener
);
1072 listener_port
= regress_get_socket_port(
1073 evconnlistener_get_fd(listener
));
1075 port
= regress_get_dnsserver(data
->base
, &dns_port
, NULL
,
1076 be_getaddrinfo_server_cb
, &n_dns
);
1078 tt_int_op(dns_port
, >=, 0);
1080 /* Start an evdns_base that uses the server as its resolver. */
1081 dns
= evdns_base_new(data
->base
, 0);
1082 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", (int)dns_port
);
1083 evdns_base_nameserver_ip_add(dns
, buf
);
1085 /* Now, finally, at long last, launch the bufferevents. One should do
1086 * a failing lookup IP, one should do a successful lookup by IP,
1087 * and one should do a successful lookup by hostname. */
1088 be1
= bufferevent_socket_new(data
->base
, -1, BEV_OPT_CLOSE_ON_FREE
);
1089 be2
= bufferevent_socket_new(data
->base
, -1, BEV_OPT_CLOSE_ON_FREE
);
1090 be3
= bufferevent_socket_new(data
->base
, -1, BEV_OPT_CLOSE_ON_FREE
);
1091 be4
= bufferevent_socket_new(data
->base
, -1, BEV_OPT_CLOSE_ON_FREE
);
1092 be5
= bufferevent_socket_new(data
->base
, -1, BEV_OPT_CLOSE_ON_FREE
);
1094 bufferevent_setcb(be1
, NULL
, NULL
, be_connect_hostname_event_cb
,
1096 bufferevent_setcb(be2
, NULL
, NULL
, be_connect_hostname_event_cb
,
1098 bufferevent_setcb(be3
, NULL
, NULL
, be_connect_hostname_event_cb
,
1100 bufferevent_setcb(be4
, NULL
, NULL
, be_connect_hostname_event_cb
,
1102 bufferevent_setcb(be5
, NULL
, NULL
, be_connect_hostname_event_cb
,
1105 /* Launch an async resolve that will fail. */
1106 tt_assert(!bufferevent_socket_connect_hostname(be1
, dns
, AF_INET
,
1107 "nosuchplace.example.com", listener_port
));
1108 /* Connect to the IP without resolving. */
1109 tt_assert(!bufferevent_socket_connect_hostname(be2
, dns
, AF_INET
,
1110 "127.0.0.1", listener_port
));
1111 /* Launch an async resolve that will succeed. */
1112 tt_assert(!bufferevent_socket_connect_hostname(be3
, dns
, AF_INET
,
1113 "nobodaddy.example.com", listener_port
));
1114 /* Use the blocking resolver. This one will fail if your resolver
1115 * can't resolve localhost to 127.0.0.1 */
1116 tt_assert(!bufferevent_socket_connect_hostname(be4
, NULL
, AF_INET
,
1117 "localhost", listener_port
));
1118 /* Use the blocking resolver with a nonexistent hostname. */
1119 tt_assert(!bufferevent_socket_connect_hostname(be5
, NULL
, AF_INET
,
1120 "nonesuch.nowhere.example.com", 80));
1122 /* The blocking resolver will use the system nameserver, which
1123 * might tell us anything. (Yes, some twits even pretend that
1124 * example.com is real.) Let's see what answer to expect. */
1125 struct evutil_addrinfo hints
, *ai
= NULL
;
1126 memset(&hints
, 0, sizeof(hints
));
1127 hints
.ai_family
= AF_INET
;
1128 hints
.ai_socktype
= SOCK_STREAM
;
1129 hints
.ai_protocol
= IPPROTO_TCP
;
1130 expect_err5
= evutil_getaddrinfo(
1131 "nonesuch.nowhere.example.com", "80", &hints
, &ai
);
1134 event_base_dispatch(data
->base
);
1136 tt_int_op(be1_outcome
.what
, ==, BEV_EVENT_ERROR
);
1137 tt_int_op(be1_outcome
.dnserr
, ==, EVUTIL_EAI_NONAME
);
1138 tt_int_op(be2_outcome
.what
, ==, BEV_EVENT_CONNECTED
);
1139 tt_int_op(be2_outcome
.dnserr
, ==, 0);
1140 tt_int_op(be3_outcome
.what
, ==, BEV_EVENT_CONNECTED
);
1141 tt_int_op(be3_outcome
.dnserr
, ==, 0);
1142 tt_int_op(be4_outcome
.what
, ==, BEV_EVENT_CONNECTED
);
1143 tt_int_op(be4_outcome
.dnserr
, ==, 0);
1145 tt_int_op(be5_outcome
.what
, ==, BEV_EVENT_ERROR
);
1146 tt_int_op(be5_outcome
.dnserr
, ==, expect_err5
);
1149 tt_int_op(n_accept
, ==, 3);
1150 tt_int_op(n_dns
, ==, 2);
1154 evconnlistener_free(listener
);
1156 evdns_close_server_port(port
);
1158 evdns_base_free(dns
, 0);
1160 bufferevent_free(be1
);
1162 bufferevent_free(be2
);
1164 bufferevent_free(be3
);
1166 bufferevent_free(be4
);
1168 bufferevent_free(be5
);
1172 struct gai_outcome
{
1174 struct evutil_addrinfo
*ai
;
1177 static int n_gai_results_pending
= 0;
1178 static struct event_base
*exit_base_on_no_pending_results
= NULL
;
1181 gai_cb(int err
, struct evutil_addrinfo
*res
, void *ptr
)
1183 struct gai_outcome
*go
= ptr
;
1186 if (--n_gai_results_pending
<= 0 && exit_base_on_no_pending_results
)
1187 event_base_loopexit(exit_base_on_no_pending_results
, NULL
);
1188 if (n_gai_results_pending
< 900)
1189 TT_BLATHER(("Got an answer; expecting %d more.",
1190 n_gai_results_pending
));
1194 cancel_gai_cb(evutil_socket_t fd
, short what
, void *ptr
)
1196 struct evdns_getaddrinfo_request
*r
= ptr
;
1197 evdns_getaddrinfo_cancel(r
);
1201 test_getaddrinfo_async(void *arg
)
1203 struct basic_test_data
*data
= arg
;
1204 struct evutil_addrinfo hints
, *a
;
1205 struct gai_outcome local_outcome
;
1206 struct gai_outcome a_out
[12];
1208 struct evdns_getaddrinfo_request
*r
;
1210 struct evdns_server_port
*port
= NULL
;
1211 ev_uint16_t dns_port
= 0;
1212 int n_dns_questions
= 0;
1214 struct evdns_base
*dns_base
= evdns_base_new(data
->base
, 0);
1215 tt_assert(dns_base
);
1218 evdns_base_load_hosts(dns_base
, NULL
);
1220 memset(a_out
, 0, sizeof(a_out
));
1221 memset(&local_outcome
, 0, sizeof(local_outcome
));
1223 n_gai_results_pending
= 10000; /* don't think about exiting yet. */
1225 /* 1. Try some cases that will never hit the asynchronous resolver. */
1226 /* 1a. Simple case with a symbolic service name */
1227 memset(&hints
, 0, sizeof(hints
));
1228 hints
.ai_family
= PF_UNSPEC
;
1229 hints
.ai_socktype
= SOCK_STREAM
;
1230 memset(&local_outcome
, 0, sizeof(local_outcome
));
1231 r
= evdns_getaddrinfo(dns_base
, "1.2.3.4", "http",
1232 &hints
, gai_cb
, &local_outcome
);
1234 if (!local_outcome
.err
) {
1235 tt_ptr_op(local_outcome
.ai
,!=,NULL
);
1236 test_ai_eq(local_outcome
.ai
, "1.2.3.4:80", SOCK_STREAM
, IPPROTO_TCP
);
1237 evutil_freeaddrinfo(local_outcome
.ai
);
1238 local_outcome
.ai
= NULL
;
1240 TT_BLATHER(("Apparently we have no getservbyname."));
1243 /* 1b. EVUTIL_AI_NUMERICHOST is set */
1244 memset(&hints
, 0, sizeof(hints
));
1245 hints
.ai_family
= PF_UNSPEC
;
1246 hints
.ai_flags
= EVUTIL_AI_NUMERICHOST
;
1247 memset(&local_outcome
, 0, sizeof(local_outcome
));
1248 r
= evdns_getaddrinfo(dns_base
, "www.google.com", "80",
1249 &hints
, gai_cb
, &local_outcome
);
1250 tt_ptr_op(r
,==,NULL
);
1251 tt_int_op(local_outcome
.err
,==,EVUTIL_EAI_NONAME
);
1252 tt_ptr_op(local_outcome
.ai
,==,NULL
);
1254 /* 1c. We give a numeric address (ipv6) */
1255 memset(&hints
, 0, sizeof(hints
));
1256 memset(&local_outcome
, 0, sizeof(local_outcome
));
1257 hints
.ai_family
= PF_UNSPEC
;
1258 hints
.ai_protocol
= IPPROTO_TCP
;
1259 r
= evdns_getaddrinfo(dns_base
, "f::f", "8008",
1260 &hints
, gai_cb
, &local_outcome
);
1262 tt_int_op(local_outcome
.err
,==,0);
1263 tt_assert(local_outcome
.ai
);
1264 tt_ptr_op(local_outcome
.ai
->ai_next
,==,NULL
);
1265 test_ai_eq(local_outcome
.ai
, "[f::f]:8008", SOCK_STREAM
, IPPROTO_TCP
);
1266 evutil_freeaddrinfo(local_outcome
.ai
);
1267 local_outcome
.ai
= NULL
;
1269 /* 1d. We give a numeric address (ipv4) */
1270 memset(&hints
, 0, sizeof(hints
));
1271 memset(&local_outcome
, 0, sizeof(local_outcome
));
1272 hints
.ai_family
= PF_UNSPEC
;
1273 r
= evdns_getaddrinfo(dns_base
, "5.6.7.8", NULL
,
1274 &hints
, gai_cb
, &local_outcome
);
1276 tt_int_op(local_outcome
.err
,==,0);
1277 tt_assert(local_outcome
.ai
);
1278 a
= ai_find_by_protocol(local_outcome
.ai
, IPPROTO_TCP
);
1280 test_ai_eq(a
, "5.6.7.8", SOCK_STREAM
, IPPROTO_TCP
);
1281 a
= ai_find_by_protocol(local_outcome
.ai
, IPPROTO_UDP
);
1283 test_ai_eq(a
, "5.6.7.8", SOCK_DGRAM
, IPPROTO_UDP
);
1284 evutil_freeaddrinfo(local_outcome
.ai
);
1285 local_outcome
.ai
= NULL
;
1287 /* 1e. nodename is NULL (bind) */
1288 memset(&hints
, 0, sizeof(hints
));
1289 memset(&local_outcome
, 0, sizeof(local_outcome
));
1290 hints
.ai_family
= PF_UNSPEC
;
1291 hints
.ai_socktype
= SOCK_DGRAM
;
1292 hints
.ai_flags
= EVUTIL_AI_PASSIVE
;
1293 r
= evdns_getaddrinfo(dns_base
, NULL
, "9090",
1294 &hints
, gai_cb
, &local_outcome
);
1296 tt_int_op(local_outcome
.err
,==,0);
1297 tt_assert(local_outcome
.ai
);
1298 /* we should get a v4 address of 0.0.0.0... */
1299 a
= ai_find_by_family(local_outcome
.ai
, PF_INET
);
1301 test_ai_eq(a
, "0.0.0.0:9090", SOCK_DGRAM
, IPPROTO_UDP
);
1302 /* ... and a v6 address of ::0 */
1303 a
= ai_find_by_family(local_outcome
.ai
, PF_INET6
);
1305 test_ai_eq(a
, "[::]:9090", SOCK_DGRAM
, IPPROTO_UDP
);
1306 evutil_freeaddrinfo(local_outcome
.ai
);
1307 local_outcome
.ai
= NULL
;
1309 /* 1f. nodename is NULL (connect) */
1310 memset(&hints
, 0, sizeof(hints
));
1311 memset(&local_outcome
, 0, sizeof(local_outcome
));
1312 hints
.ai_family
= PF_UNSPEC
;
1313 hints
.ai_socktype
= SOCK_STREAM
;
1314 r
= evdns_getaddrinfo(dns_base
, NULL
, "2",
1315 &hints
, gai_cb
, &local_outcome
);
1317 tt_int_op(local_outcome
.err
,==,0);
1318 tt_assert(local_outcome
.ai
);
1319 /* we should get a v4 address of 127.0.0.1 .... */
1320 a
= ai_find_by_family(local_outcome
.ai
, PF_INET
);
1322 test_ai_eq(a
, "127.0.0.1:2", SOCK_STREAM
, IPPROTO_TCP
);
1323 /* ... and a v6 address of ::1 */
1324 a
= ai_find_by_family(local_outcome
.ai
, PF_INET6
);
1326 test_ai_eq(a
, "[::1]:2", SOCK_STREAM
, IPPROTO_TCP
);
1327 evutil_freeaddrinfo(local_outcome
.ai
);
1328 local_outcome
.ai
= NULL
;
1330 /* 1g. We find localhost immediately. (pf_unspec) */
1331 memset(&hints
, 0, sizeof(hints
));
1332 memset(&local_outcome
, 0, sizeof(local_outcome
));
1333 hints
.ai_family
= PF_UNSPEC
;
1334 hints
.ai_socktype
= SOCK_STREAM
;
1335 r
= evdns_getaddrinfo(dns_base
, "LOCALHOST", "80",
1336 &hints
, gai_cb
, &local_outcome
);
1338 tt_int_op(local_outcome
.err
,==,0);
1339 tt_assert(local_outcome
.ai
);
1340 /* we should get a v4 address of 127.0.0.1 .... */
1341 a
= ai_find_by_family(local_outcome
.ai
, PF_INET
);
1343 test_ai_eq(a
, "127.0.0.1:80", SOCK_STREAM
, IPPROTO_TCP
);
1344 /* ... and a v6 address of ::1 */
1345 a
= ai_find_by_family(local_outcome
.ai
, PF_INET6
);
1347 test_ai_eq(a
, "[::1]:80", SOCK_STREAM
, IPPROTO_TCP
);
1348 evutil_freeaddrinfo(local_outcome
.ai
);
1349 local_outcome
.ai
= NULL
;
1351 /* 1g. We find localhost immediately. (pf_inet6) */
1352 memset(&hints
, 0, sizeof(hints
));
1353 memset(&local_outcome
, 0, sizeof(local_outcome
));
1354 hints
.ai_family
= PF_INET6
;
1355 hints
.ai_socktype
= SOCK_STREAM
;
1356 r
= evdns_getaddrinfo(dns_base
, "LOCALHOST", "9999",
1357 &hints
, gai_cb
, &local_outcome
);
1359 tt_int_op(local_outcome
.err
,==,0);
1360 tt_assert(local_outcome
.ai
);
1361 a
= local_outcome
.ai
;
1362 test_ai_eq(a
, "[::1]:9999", SOCK_STREAM
, IPPROTO_TCP
);
1363 tt_ptr_op(a
->ai_next
, ==, NULL
);
1364 evutil_freeaddrinfo(local_outcome
.ai
);
1365 local_outcome
.ai
= NULL
;
1367 /* 2. Okay, now we can actually test the asynchronous resolver. */
1368 /* Start a dummy local dns server... */
1369 port
= regress_get_dnsserver(data
->base
, &dns_port
, NULL
,
1370 be_getaddrinfo_server_cb
, &n_dns_questions
);
1372 tt_int_op(dns_port
, >=, 0);
1373 /* ... and tell the evdns_base about it. */
1374 evutil_snprintf(buf
, sizeof(buf
), "127.0.0.1:%d", dns_port
);
1375 evdns_base_nameserver_ip_add(dns_base
, buf
);
1377 memset(&hints
, 0, sizeof(hints
));
1378 hints
.ai_family
= PF_UNSPEC
;
1379 hints
.ai_socktype
= SOCK_STREAM
;
1380 hints
.ai_flags
= EVUTIL_AI_CANONNAME
;
1381 /* 0: Request for both.example.com should return both addresses. */
1382 r
= evdns_getaddrinfo(dns_base
, "both.example.com", "8000",
1383 &hints
, gai_cb
, &a_out
[0]);
1386 /* 1: Request for v4only.example.com should return one address. */
1387 r
= evdns_getaddrinfo(dns_base
, "v4only.example.com", "8001",
1388 &hints
, gai_cb
, &a_out
[1]);
1391 /* 2: Request for v6only.example.com should return one address. */
1393 r
= evdns_getaddrinfo(dns_base
, "v6only.example.com", "8002",
1394 &hints
, gai_cb
, &a_out
[2]);
1397 /* 3: PF_INET request for v4assert.example.com should not generate a
1398 * v6 request. The server will fail the test if it does. */
1399 hints
.ai_family
= PF_INET
;
1400 r
= evdns_getaddrinfo(dns_base
, "v4assert.example.com", "8003",
1401 &hints
, gai_cb
, &a_out
[3]);
1404 /* 4: PF_INET6 request for v6assert.example.com should not generate a
1405 * v4 request. The server will fail the test if it does. */
1406 hints
.ai_family
= PF_INET6
;
1407 r
= evdns_getaddrinfo(dns_base
, "v6assert.example.com", "8004",
1408 &hints
, gai_cb
, &a_out
[4]);
1411 /* 5: PF_INET request for nosuchplace.example.com should give NEXIST. */
1412 hints
.ai_family
= PF_INET
;
1413 r
= evdns_getaddrinfo(dns_base
, "nosuchplace.example.com", "8005",
1414 &hints
, gai_cb
, &a_out
[5]);
1417 /* 6: PF_UNSPEC request for nosuchplace.example.com should give NEXIST.
1419 hints
.ai_family
= PF_UNSPEC
;
1420 r
= evdns_getaddrinfo(dns_base
, "nosuchplace.example.com", "8006",
1421 &hints
, gai_cb
, &a_out
[6]);
1424 /* 7: PF_UNSPEC request for v6timeout.example.com should give an ipv4
1426 hints
.ai_family
= PF_UNSPEC
;
1427 r
= evdns_getaddrinfo(dns_base
, "v6timeout.example.com", "8007",
1428 &hints
, gai_cb
, &a_out
[7]);
1431 /* 8: PF_UNSPEC request for v6timeout-nonexist.example.com should give
1433 hints
.ai_family
= PF_UNSPEC
;
1434 r
= evdns_getaddrinfo(dns_base
, "v6timeout-nonexist.example.com",
1435 "8008", &hints
, gai_cb
, &a_out
[8]);
1438 /* 9: AI_ADDRCONFIG should at least not crash. Can't test it more
1439 * without knowing what kind of internet we have. */
1440 hints
.ai_flags
|= EVUTIL_AI_ADDRCONFIG
;
1441 r
= evdns_getaddrinfo(dns_base
, "both.example.com",
1442 "8009", &hints
, gai_cb
, &a_out
[9]);
1445 /* 10: PF_UNSPEC for v4timeout.example.com should give an ipv6 address
1447 hints
.ai_family
= PF_UNSPEC
;
1449 r
= evdns_getaddrinfo(dns_base
, "v4timeout.example.com", "8010",
1450 &hints
, gai_cb
, &a_out
[10]);
1453 /* 11: timeout.example.com: cancel it after 100 msec. */
1454 r
= evdns_getaddrinfo(dns_base
, "all-timeout.example.com", "8011",
1455 &hints
, gai_cb
, &a_out
[11]);
1460 tv
.tv_usec
= 100*1000; /* 100 msec */
1461 event_base_once(data
->base
, -1, EV_TIMEOUT
, cancel_gai_cb
,
1465 /* XXXXX There are more tests we could do, including:
1467 - A test to elicit NODATA.
1471 n_gai_results_pending
= 12;
1472 exit_base_on_no_pending_results
= data
->base
;
1474 event_base_dispatch(data
->base
);
1476 /* 0: both.example.com */
1477 tt_int_op(a_out
[0].err
, ==, 0);
1478 tt_assert(a_out
[0].ai
);
1479 tt_assert(a_out
[0].ai
->ai_next
);
1480 tt_assert(!a_out
[0].ai
->ai_next
->ai_next
);
1481 a
= ai_find_by_family(a_out
[0].ai
, PF_INET
);
1483 test_ai_eq(a
, "80.80.32.32:8000", SOCK_STREAM
, IPPROTO_TCP
);
1484 a
= ai_find_by_family(a_out
[0].ai
, PF_INET6
);
1486 test_ai_eq(a
, "[80ff::bbbb]:8000", SOCK_STREAM
, IPPROTO_TCP
);
1487 tt_assert(a_out
[0].ai
->ai_canonname
);
1488 tt_str_op(a_out
[0].ai
->ai_canonname
, ==, "both-canonical.example.com");
1490 /* 1: v4only.example.com */
1491 tt_int_op(a_out
[1].err
, ==, 0);
1492 tt_assert(a_out
[1].ai
);
1493 tt_assert(! a_out
[1].ai
->ai_next
);
1494 test_ai_eq(a_out
[1].ai
, "18.52.86.120:8001", SOCK_STREAM
, IPPROTO_TCP
);
1495 tt_assert(a_out
[1].ai
->ai_canonname
== NULL
);
1498 /* 2: v6only.example.com */
1499 tt_int_op(a_out
[2].err
, ==, 0);
1500 tt_assert(a_out
[2].ai
);
1501 tt_assert(! a_out
[2].ai
->ai_next
);
1502 test_ai_eq(a_out
[2].ai
, "[b0b::f00d]:8002", SOCK_STREAM
, IPPROTO_TCP
);
1504 /* 3: v4assert.example.com */
1505 tt_int_op(a_out
[3].err
, ==, 0);
1506 tt_assert(a_out
[3].ai
);
1507 tt_assert(! a_out
[3].ai
->ai_next
);
1508 test_ai_eq(a_out
[3].ai
, "18.52.86.120:8003", SOCK_STREAM
, IPPROTO_TCP
);
1510 /* 4: v6assert.example.com */
1511 tt_int_op(a_out
[4].err
, ==, 0);
1512 tt_assert(a_out
[4].ai
);
1513 tt_assert(! a_out
[4].ai
->ai_next
);
1514 test_ai_eq(a_out
[4].ai
, "[b0b::f00d]:8004", SOCK_STREAM
, IPPROTO_TCP
);
1516 /* 5: nosuchplace.example.com (inet) */
1517 tt_int_op(a_out
[5].err
, ==, EVUTIL_EAI_NONAME
);
1518 tt_assert(! a_out
[5].ai
);
1520 /* 6: nosuchplace.example.com (unspec) */
1521 tt_int_op(a_out
[6].err
, ==, EVUTIL_EAI_NONAME
);
1522 tt_assert(! a_out
[6].ai
);
1524 /* 7: v6timeout.example.com */
1525 tt_int_op(a_out
[7].err
, ==, 0);
1526 tt_assert(a_out
[7].ai
);
1527 tt_assert(! a_out
[7].ai
->ai_next
);
1528 test_ai_eq(a_out
[7].ai
, "171.205.239.1:8007", SOCK_STREAM
, IPPROTO_TCP
);
1530 /* 8: v6timeout-nonexist.example.com */
1531 tt_int_op(a_out
[8].err
, ==, EVUTIL_EAI_NONAME
);
1532 tt_assert(! a_out
[8].ai
);
1534 /* 9: both (ADDRCONFIG) */
1535 tt_int_op(a_out
[9].err
, ==, 0);
1536 tt_assert(a_out
[9].ai
);
1537 a
= ai_find_by_family(a_out
[9].ai
, PF_INET
);
1539 test_ai_eq(a
, "80.80.32.32:8009", SOCK_STREAM
, IPPROTO_TCP
);
1541 tt_assert(ai_find_by_family(a_out
[9].ai
, PF_INET6
));
1542 a
= ai_find_by_family(a_out
[9].ai
, PF_INET6
);
1544 test_ai_eq(a
, "[80ff::bbbb]:8009", SOCK_STREAM
, IPPROTO_TCP
);
1546 tt_assert(ai_find_by_family(a_out
[9].ai
, PF_INET
));
1548 /* 10: v4timeout.example.com */
1549 tt_int_op(a_out
[10].err
, ==, 0);
1550 tt_assert(a_out
[10].ai
);
1551 tt_assert(! a_out
[10].ai
->ai_next
);
1552 test_ai_eq(a_out
[10].ai
, "[a0a::ff01]:8010", SOCK_STREAM
, IPPROTO_TCP
);
1554 /* 11: cancelled request. */
1555 tt_int_op(a_out
[11].err
, ==, EVUTIL_EAI_CANCEL
);
1556 tt_assert(a_out
[11].ai
== NULL
);
1559 if (local_outcome
.ai
)
1560 evutil_freeaddrinfo(local_outcome
.ai
);
1561 for (i
=0;i
<10;++i
) {
1563 evutil_freeaddrinfo(a_out
[i
].ai
);
1566 evdns_close_server_port(port
);
1568 evdns_base_free(dns_base
, 0);
1571 struct gaic_request_status
{
1573 struct event_base
*base
;
1574 struct evdns_base
*dns_base
;
1575 struct evdns_getaddrinfo_request
*request
;
1576 struct event cancel_event
;
1580 #define GAIC_MAGIC 0x1234abcd
1582 static int pending
= 0;
1585 gaic_cancel_request_cb(evutil_socket_t fd
, short what
, void *arg
)
1587 struct gaic_request_status
*status
= arg
;
1589 tt_assert(status
->magic
== GAIC_MAGIC
);
1590 status
->canceled
= 1;
1591 evdns_getaddrinfo_cancel(status
->request
);
1594 event_base_loopexit(status
->base
, NULL
);
1598 gaic_server_cb(struct evdns_server_request
*req
, void *arg
)
1600 ev_uint32_t answer
= 0x7f000001;
1601 tt_assert(req
->nquestions
);
1602 evdns_server_request_add_a_reply(req
, req
->questions
[0]->name
, 1,
1604 evdns_server_request_respond(req
, 0);
1607 evdns_server_request_respond(req
, DNS_ERR_REFUSED
);
1612 gaic_getaddrinfo_cb(int result
, struct evutil_addrinfo
*res
, void *arg
)
1614 struct gaic_request_status
*status
= arg
;
1615 struct event_base
*base
= status
->base
;
1616 tt_assert(status
->magic
== GAIC_MAGIC
);
1618 if (result
== EVUTIL_EAI_CANCEL
) {
1619 tt_assert(status
->canceled
);
1621 event_del(&status
->cancel_event
);
1623 memset(status
, 0xf0, sizeof(*status
));
1628 event_base_loopexit(base
, NULL
);
1632 gaic_launch(struct event_base
*base
, struct evdns_base
*dns_base
)
1634 struct gaic_request_status
*status
= calloc(1,sizeof(*status
));
1635 struct timeval tv
= { 0, 10000 };
1636 status
->magic
= GAIC_MAGIC
;
1637 status
->base
= base
;
1638 status
->dns_base
= dns_base
;
1639 event_assign(&status
->cancel_event
, base
, -1, 0, gaic_cancel_request_cb
,
1641 status
->request
= evdns_getaddrinfo(dns_base
,
1642 "foobar.bazquux.example.com", "80", NULL
, gaic_getaddrinfo_cb
,
1644 event_add(&status
->cancel_event
, &tv
);
1648 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1649 /* FIXME: We should move this to regress_main.c if anything else needs it.*/
1651 /* Trivial replacements for malloc/free/realloc to check for memory leaks.
1652 * Not threadsafe. */
1653 static int allocated_chunks
= 0;
1656 cnt_malloc(size_t sz
)
1658 allocated_chunks
+= 1;
1663 cnt_realloc(void *old
, size_t sz
)
1666 allocated_chunks
+= 1;
1668 allocated_chunks
-= 1;
1669 return realloc(old
, sz
);
1675 allocated_chunks
-= 1;
1679 struct testleak_env_t
{
1680 struct event_base
*base
;
1681 struct evdns_base
*dns_base
;
1682 struct evdns_request
*req
;
1683 struct generic_dns_callback_result r
;
1687 testleak_setup(const struct testcase_t
*testcase
)
1689 struct testleak_env_t
*env
;
1691 allocated_chunks
= 0;
1692 event_set_mem_functions(cnt_malloc
, cnt_realloc
, cnt_free
);
1693 event_enable_debug_mode();
1695 /* not mm_calloc: we don't want to mess with the count. */
1696 env
= calloc(1, sizeof(struct testleak_env_t
));
1697 env
->base
= event_base_new();
1698 env
->dns_base
= evdns_base_new(env
->base
, 0);
1699 env
->req
= evdns_base_resolve_ipv4(
1700 env
->dns_base
, "example.com", DNS_QUERY_NO_SEARCH
,
1701 generic_dns_callback
, &env
->r
);
1706 testleak_cleanup(const struct testcase_t
*testcase
, void *env_
)
1709 struct testleak_env_t
*env
= env_
;
1711 #ifdef _EVENT_DISABLE_DEBUG_MODE
1712 tt_int_op(allocated_chunks
, ==, 0);
1714 /* FIXME: that's `1' because of event_debug_map_HT_GROW */
1715 tt_int_op(allocated_chunks
, ==, 1);
1721 evdns_base_free(env
->dns_base
, 0);
1723 event_base_free(env
->base
);
1729 static struct testcase_setup_t testleak_funcs
= {
1730 testleak_setup
, testleak_cleanup
1734 test_dbg_leak_cancel(void *env_
)
1736 /* cancel, loop, free/dns, free/base */
1737 struct testleak_env_t
*env
= env_
;
1738 int send_err_shutdown
= 1;
1739 evdns_cancel_request(env
->dns_base
, env
->req
);
1742 /* `req` is freed in callback, that's why one loop is required. */
1743 event_base_loop(env
->base
, EVLOOP_NONBLOCK
);
1745 /* send_err_shutdown means nothing as soon as our request is
1746 * already canceled */
1747 evdns_base_free(env
->dns_base
, send_err_shutdown
);
1749 event_base_free(env
->base
);
1754 test_dbg_leak_shutdown(void *env_
)
1756 /* free/dns, loop, free/base */
1757 struct testleak_env_t
*env
= env_
;
1758 int send_err_shutdown
= 1;
1760 /* `req` is freed both with `send_err_shutdown` and without it,
1761 * the only difference is `evdns_callback` call */
1764 evdns_base_free(env
->dns_base
, send_err_shutdown
);
1767 /* `req` is freed in callback, that's why one loop is required */
1768 event_base_loop(env
->base
, EVLOOP_NONBLOCK
);
1769 event_base_free(env
->base
);
1775 test_getaddrinfo_async_cancel_stress(void *ptr
)
1777 struct event_base
*base
;
1778 struct evdns_base
*dns_base
= NULL
;
1779 struct evdns_server_port
*server
= NULL
;
1780 evutil_socket_t fd
= -1;
1781 struct sockaddr_in sin
;
1782 struct sockaddr_storage ss
;
1786 base
= event_base_new();
1787 dns_base
= evdns_base_new(base
, 0);
1789 memset(&sin
, 0, sizeof(sin
));
1790 sin
.sin_family
= AF_INET
;
1792 sin
.sin_addr
.s_addr
= htonl(0x7f000001);
1793 if ((fd
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0) {
1794 tt_abort_perror("socket");
1796 evutil_make_socket_nonblocking(fd
);
1797 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
))<0) {
1798 tt_abort_perror("bind");
1800 server
= evdns_add_server_port_with_base(base
, fd
, 0, gaic_server_cb
,
1803 memset(&ss
, 0, sizeof(ss
));
1805 if (getsockname(fd
, (struct sockaddr
*)&ss
, &slen
)<0) {
1806 tt_abort_perror("getsockname");
1808 evdns_base_nameserver_sockaddr_add(dns_base
,
1809 (struct sockaddr
*)&ss
, slen
, 0);
1811 for (i
= 0; i
< 1000; ++i
) {
1812 gaic_launch(base
, dns_base
);
1815 event_base_dispatch(base
);
1819 evdns_base_free(dns_base
, 1);
1821 evdns_close_server_port(server
);
1823 evutil_closesocket(fd
);
1827 #define DNS_LEGACY(name, flags) \
1828 { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \
1831 struct testcase_t dns_testcases
[] = {
1832 DNS_LEGACY(server
, TT_FORK
|TT_NEED_BASE
),
1833 DNS_LEGACY(gethostbyname
, TT_FORK
|TT_NEED_BASE
|TT_NEED_DNS
),
1834 DNS_LEGACY(gethostbyname6
, TT_FORK
|TT_NEED_BASE
|TT_NEED_DNS
),
1835 DNS_LEGACY(gethostbyaddr
, TT_FORK
|TT_NEED_BASE
|TT_NEED_DNS
),
1836 { "resolve_reverse", dns_resolve_reverse
, TT_FORK
, NULL
, NULL
},
1837 { "search", dns_search_test
, TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1838 { "search_cancel", dns_search_cancel_test
,
1839 TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1840 { "retry", dns_retry_test
, TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1841 { "reissue", dns_reissue_test
, TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1842 { "inflight", dns_inflight_test
, TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1843 { "bufferevent_connect_hostname", test_bufferevent_connect_hostname
,
1844 TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
1846 { "getaddrinfo_async", test_getaddrinfo_async
,
1847 TT_FORK
|TT_NEED_BASE
, &basic_setup
, __UNCONST("") },
1848 { "getaddrinfo_cancel_stress", test_getaddrinfo_async_cancel_stress
,
1849 TT_FORK
, NULL
, NULL
},
1851 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1852 { "leak_shutdown", test_dbg_leak_shutdown
, TT_FORK
, &testleak_funcs
, NULL
},
1853 { "leak_cancel", test_dbg_leak_cancel
, TT_FORK
, &testleak_funcs
, NULL
},