2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "../util-internal.h"
35 #include "event2/event-config.h"
37 #include <sys/types.h>
39 #ifdef EVENT__HAVE_SYS_TIME_H
42 #include <sys/queue.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #ifdef EVENT__HAVE_NETINET_IN6_H
51 #include <netinet/in6.h>
62 #include "event2/dns.h"
63 #include "event2/dns_compat.h"
64 #include "event2/dns_struct.h"
65 #include "event2/event.h"
66 #include "event2/event_compat.h"
67 #include "event2/event_struct.h"
68 #include "event2/util.h"
69 #include "event2/listener.h"
70 #include "event2/bufferevent.h"
71 #include "log-internal.h"
73 #include "regress_testutils.h"
75 static int dns_ok = 0;
76 static int dns_got_cancel = 0;
77 static int dns_err = 0;
81 dns_gethostbyname_cb(int result, char type, int count, int ttl,
82 void *addresses, void *arg)
86 if (result == DNS_ERR_TIMEOUT) {
87 printf("[Timed out] ");
92 if (result != DNS_ERR_NONE) {
93 printf("[Error code %d] ", result);
97 TT_BLATHER(("type: %d, count: %d, ttl: %d: ", type, count, ttl));
100 case DNS_IPv6_AAAA: {
101 #if defined(EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
102 struct in6_addr *in6_addrs = addresses;
103 char buf[INET6_ADDRSTRLEN+1];
105 /* a resolution that's not valid does not help */
108 for (i = 0; i < count; ++i) {
109 const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf));
111 TT_BLATHER(("%s ", b));
113 TT_BLATHER(("%s ", strerror(errno)));
119 struct in_addr *in_addrs = addresses;
121 /* a resolution that's not valid does not help */
124 for (i = 0; i < count; ++i)
125 TT_BLATHER(("%s ", inet_ntoa(in_addrs[i])));
129 /* may get at most one PTR */
133 TT_BLATHER(("%s ", *(char **)addresses));
143 event_loopexit(NULL);
145 event_base_loopexit((struct event_base *)arg, NULL);
149 dns_gethostbyname(void)
152 evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
155 tt_int_op(dns_ok, ==, DNS_IPv4_A);
162 dns_gethostbyname6(void)
165 evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL);
168 if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) {
172 tt_int_op(dns_ok, ==, DNS_IPv6_AAAA);
179 dns_gethostbyaddr(void)
182 in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
184 evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL);
187 tt_int_op(dns_ok, ==, DNS_PTR);
194 dns_resolve_reverse(void *ptr)
197 struct event_base *base = event_base_new();
198 struct evdns_base *dns = evdns_base_new(base, 1/* init name servers */);
199 struct evdns_request *req = NULL;
203 in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
206 req = evdns_base_resolve_reverse(
207 dns, &in, 0, dns_gethostbyname_cb, base);
210 event_base_dispatch(base);
212 tt_int_op(dns_ok, ==, DNS_PTR);
216 evdns_base_free(dns, 0);
218 event_base_free(base);
221 static int n_server_responses = 0;
224 dns_server_request_cb(struct evdns_server_request *req, void *data)
227 const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa";
228 const char TEST_IN6[] =
229 "f.e.f.e." "0.0.0.0." "0.0.0.0." "1.1.1.1."
230 "a.a.a.a." "0.0.0.0." "0.0.0.0." "0.f.f.f.ip6.arpa";
232 for (i = 0; i < req->nquestions; ++i) {
233 const int qtype = req->questions[i]->type;
234 const int qclass = req->questions[i]->dns_question_class;
235 const char *qname = req->questions[i]->name;
238 ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
239 if (qtype == EVDNS_TYPE_A &&
240 qclass == EVDNS_CLASS_INET &&
241 !evutil_ascii_strcasecmp(qname, "zz.example.com")) {
242 r = evdns_server_request_add_a_reply(req, qname,
243 1, &ans.s_addr, 12345);
246 } else if (qtype == EVDNS_TYPE_AAAA &&
247 qclass == EVDNS_CLASS_INET &&
248 !evutil_ascii_strcasecmp(qname, "zz.example.com")) {
249 char addr6[17] = "abcdefghijklmnop";
250 r = evdns_server_request_add_aaaa_reply(req,
251 qname, 1, addr6, 123);
254 } else if (qtype == EVDNS_TYPE_PTR &&
255 qclass == EVDNS_CLASS_INET &&
256 !evutil_ascii_strcasecmp(qname, TEST_ARPA)) {
257 r = evdns_server_request_add_ptr_reply(req, NULL,
258 qname, "ZZ.EXAMPLE.COM", 54321);
261 } else if (qtype == EVDNS_TYPE_PTR &&
262 qclass == EVDNS_CLASS_INET &&
263 !evutil_ascii_strcasecmp(qname, TEST_IN6)){
264 r = evdns_server_request_add_ptr_reply(req, NULL,
266 "ZZ-INET6.EXAMPLE.COM", 54322);
269 } else if (qtype == EVDNS_TYPE_A &&
270 qclass == EVDNS_CLASS_INET &&
271 !evutil_ascii_strcasecmp(qname, "drop.example.com")) {
272 if (evdns_server_request_drop(req)<0)
276 printf("Unexpected question %d %d \"%s\" ",
277 qtype, qclass, qname);
281 r = evdns_server_request_respond(req, 0);
283 printf("Couldn't send reply. ");
289 dns_server_gethostbyname_cb(int result, char type, int count, int ttl,
290 void *addresses, void *arg)
292 if (result == DNS_ERR_CANCEL) {
293 if (arg != (void*)(char*)90909) {
294 printf("Unexpected cancelation");
300 if (result != DNS_ERR_NONE) {
301 printf("Unexpected result %d. ", result);
306 printf("Unexpected answer count %d. ", count);
312 struct in_addr *in_addrs = addresses;
313 if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) {
314 printf("Bad IPv4 response \"%s\" %d. ",
315 inet_ntoa(in_addrs[0]), ttl);
321 case DNS_IPv6_AAAA: {
322 #if defined (EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
323 struct in6_addr *in6_addrs = addresses;
324 char buf[INET6_ADDRSTRLEN+1];
325 if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16)
327 const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf));
328 printf("Bad IPv6 response \"%s\" %d. ", b, ttl);
336 char **addrs = addresses;
337 if (arg != (void*)6) {
338 if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") ||
340 printf("Bad PTR response \"%s\" %d. ",
346 if (strcmp(addrs[0], "ZZ-INET6.EXAMPLE.COM") ||
348 printf("Bad ipv6 PTR response \"%s\" %d. ",
357 printf("Bad response type %d. ", type);
361 if (++n_server_responses == 3) {
362 event_loopexit(NULL);
369 evutil_socket_t sock=-1;
370 struct sockaddr_in my_addr;
371 struct sockaddr_storage ss;
373 struct evdns_server_port *port=NULL;
374 struct in_addr resolve_addr;
375 struct in6_addr resolve_addr6;
376 struct evdns_base *base=NULL;
377 struct evdns_request *req=NULL;
381 base = evdns_base_new(NULL, 0);
383 /* Now configure a nameserver port. */
384 sock = socket(AF_INET, SOCK_DGRAM, 0);
386 tt_abort_perror("socket");
389 evutil_make_socket_nonblocking(sock);
391 memset(&my_addr, 0, sizeof(my_addr));
392 my_addr.sin_family = AF_INET;
393 my_addr.sin_port = 0; /* kernel picks */
394 my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
395 if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
396 tt_abort_perror("bind");
399 if (getsockname(sock, (struct sockaddr*)&ss, &slen) < 0) {
400 tt_abort_perror("getsockname");
403 port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL);
405 /* Add ourself as the only nameserver, and make sure we really are
406 * the only nameserver. */
407 evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0);
408 tt_int_op(evdns_base_count_nameservers(base), ==, 1);
410 struct sockaddr_storage ss2;
413 memset(&ss2, 0, sizeof(ss2));
415 slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, 3);
416 tt_int_op(slen2, ==, slen);
417 tt_int_op(ss2.ss_family, ==, 0);
418 slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, sizeof(ss2));
419 tt_int_op(slen2, ==, slen);
420 tt_mem_op(&ss2, ==, &ss, slen);
422 slen2 = evdns_base_get_nameserver_addr(base, 1, (struct sockaddr *)&ss2, sizeof(ss2));
423 tt_int_op(-1, ==, slen2);
426 /* Send some queries. */
427 evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH,
428 dns_server_gethostbyname_cb, NULL);
429 evdns_base_resolve_ipv6(base, "zz.example.com", DNS_QUERY_NO_SEARCH,
430 dns_server_gethostbyname_cb, NULL);
431 resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
432 evdns_base_resolve_reverse(base, &resolve_addr, 0,
433 dns_server_gethostbyname_cb, NULL);
434 memcpy(resolve_addr6.s6_addr,
435 "\xff\xf0\x00\x00\x00\x00\xaa\xaa"
436 "\x11\x11\x00\x00\x00\x00\xef\xef", 16);
437 evdns_base_resolve_reverse_ipv6(base, &resolve_addr6, 0,
438 dns_server_gethostbyname_cb, (void*)6);
440 req = evdns_base_resolve_ipv4(base,
441 "drop.example.com", DNS_QUERY_NO_SEARCH,
442 dns_server_gethostbyname_cb, (void*)(char*)90909);
444 evdns_cancel_request(base, req);
448 tt_assert(dns_got_cancel);
453 evdns_close_server_port(port);
455 evutil_closesocket(sock);
457 evdns_base_free(base, 0);
460 static int n_replies_left;
461 static struct event_base *exit_base;
462 static struct evdns_server_port *exit_port;
464 struct generic_dns_callback_result {
475 generic_dns_callback(int result, char type, int count, int ttl, void *addresses,
479 struct generic_dns_callback_result *res = arg;
480 res->result = result;
485 if (type == DNS_IPv4_A)
487 else if (type == DNS_IPv6_AAAA)
489 else if (type == DNS_PTR)
490 len = strlen(addresses)+1;
492 res->addrs_len = len = 0;
496 res->addrs_len = len;
499 memcpy(res->addrs_buf, addresses, len);
500 res->addrs = res->addrs_buf;
504 if (n_replies_left == 0) {
506 evdns_close_server_port(exit_port);
509 event_base_loopexit(exit_base, NULL);
513 static struct regress_dns_server_table search_table[] = {
514 { "host.a.example.com", "err", "3", 0 },
515 { "host.b.example.com", "err", "3", 0 },
516 { "host.c.example.com", "A", "11.22.33.44", 0 },
517 { "host2.a.example.com", "err", "3", 0 },
518 { "host2.b.example.com", "A", "200.100.0.100", 0 },
519 { "host2.c.example.com", "err", "3", 0 },
520 { "hostn.a.example.com", "errsoa", "0", 0 },
521 { "hostn.b.example.com", "errsoa", "3", 0 },
522 { "hostn.c.example.com", "err", "0", 0 },
524 { "host", "err", "3", 0 },
525 { "host2", "err", "3", 0 },
526 { "*", "err", "3", 0 },
527 { NULL, NULL, NULL, 0 }
531 dns_search_test(void *arg)
533 struct basic_test_data *data = arg;
534 struct event_base *base = data->base;
535 struct evdns_base *dns = NULL;
536 ev_uint16_t portnum = 0;
539 struct generic_dns_callback_result r[8];
541 tt_assert(regress_dnsserver(base, &portnum, search_table));
542 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
544 dns = evdns_base_new(base, 0);
545 tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
547 evdns_base_search_add(dns, "a.example.com");
548 evdns_base_search_add(dns, "b.example.com");
549 evdns_base_search_add(dns, "c.example.com");
551 n_replies_left = sizeof(r)/sizeof(r[0]);
554 evdns_base_resolve_ipv4(dns, "host", 0, generic_dns_callback, &r[0]);
555 evdns_base_resolve_ipv4(dns, "host2", 0, generic_dns_callback, &r[1]);
556 evdns_base_resolve_ipv4(dns, "host", DNS_NO_SEARCH, generic_dns_callback, &r[2]);
557 evdns_base_resolve_ipv4(dns, "host2", DNS_NO_SEARCH, generic_dns_callback, &r[3]);
558 evdns_base_resolve_ipv4(dns, "host3", 0, generic_dns_callback, &r[4]);
559 evdns_base_resolve_ipv4(dns, "hostn.a.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[5]);
560 evdns_base_resolve_ipv4(dns, "hostn.b.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[6]);
561 evdns_base_resolve_ipv4(dns, "hostn.c.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[7]);
563 event_base_dispatch(base);
565 tt_int_op(r[0].type, ==, DNS_IPv4_A);
566 tt_int_op(r[0].count, ==, 1);
567 tt_int_op(((ev_uint32_t*)r[0].addrs)[0], ==, htonl(0x0b16212c));
568 tt_int_op(r[1].type, ==, DNS_IPv4_A);
569 tt_int_op(r[1].count, ==, 1);
570 tt_int_op(((ev_uint32_t*)r[1].addrs)[0], ==, htonl(0xc8640064));
571 tt_int_op(r[2].result, ==, DNS_ERR_NOTEXIST);
572 tt_int_op(r[3].result, ==, DNS_ERR_NOTEXIST);
573 tt_int_op(r[4].result, ==, DNS_ERR_NOTEXIST);
574 tt_int_op(r[5].result, ==, DNS_ERR_NODATA);
575 tt_int_op(r[5].ttl, ==, 42);
576 tt_int_op(r[6].result, ==, DNS_ERR_NOTEXIST);
577 tt_int_op(r[6].ttl, ==, 42);
578 tt_int_op(r[7].result, ==, DNS_ERR_NODATA);
579 tt_int_op(r[7].ttl, ==, 0);
583 evdns_base_free(dns, 0);
585 regress_clean_dnsserver();
588 static int request_count = 0;
589 static struct evdns_request *current_req = NULL;
592 search_cancel_server_cb(struct evdns_server_request *req, void *data)
594 const char *question;
596 if (req->nquestions != 1)
597 TT_DIE(("Only handling one question at a time; got %d",
600 question = req->questions[0]->name;
602 TT_BLATHER(("got question, %s", question));
604 tt_assert(request_count > 0);
605 tt_assert(!evdns_server_request_respond(req, 3));
607 if (!--request_count)
608 evdns_cancel_request(NULL, current_req);
615 dns_search_cancel_test(void *arg)
617 struct basic_test_data *data = arg;
618 struct event_base *base = data->base;
619 struct evdns_base *dns = NULL;
620 struct evdns_server_port *port = NULL;
621 ev_uint16_t portnum = 0;
622 struct generic_dns_callback_result r1;
625 port = regress_get_dnsserver(base, &portnum, NULL,
626 search_cancel_server_cb, NULL);
628 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
630 dns = evdns_base_new(base, 0);
631 tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
633 evdns_base_search_add(dns, "a.example.com");
634 evdns_base_search_add(dns, "b.example.com");
635 evdns_base_search_add(dns, "c.example.com");
636 evdns_base_search_add(dns, "d.example.com");
642 current_req = evdns_base_resolve_ipv4(dns, "host", 0,
643 generic_dns_callback, &r1);
644 event_base_dispatch(base);
646 tt_int_op(r1.result, ==, DNS_ERR_CANCEL);
650 evdns_close_server_port(port);
652 evdns_base_free(dns, 0);
656 fail_server_cb(struct evdns_server_request *req, void *data)
658 const char *question;
662 /* Drop the first N requests that we get. */
665 tt_want(! evdns_server_request_drop(req));
669 if (req->nquestions != 1)
670 TT_DIE(("Only handling one question at a time; got %d",
673 question = req->questions[0]->name;
675 if (!evutil_ascii_strcasecmp(question, "google.com")) {
676 /* Detect a probe, and get out of the loop. */
677 event_base_loopexit(exit_base, NULL);
680 tt_assert(evutil_inet_pton(AF_INET, "16.32.64.128", &in));
681 evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
683 tt_assert(! evdns_server_request_respond(req, 0))
686 tt_want(! evdns_server_request_drop(req));
690 dns_retry_test_impl(void *arg, int flags)
692 struct basic_test_data *data = arg;
693 struct event_base *base = data->base;
694 struct evdns_server_port *port = NULL;
695 struct evdns_base *dns = NULL;
697 ev_uint16_t portnum = 0;
700 struct generic_dns_callback_result r1;
702 port = regress_get_dnsserver(base, &portnum, NULL,
703 fail_server_cb, &drop_count);
705 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
707 dns = evdns_base_new(base, flags);
708 tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
709 tt_assert(! evdns_base_set_option(dns, "timeout", "0.2"));
710 tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10"));
711 tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.1"));
713 evdns_base_resolve_ipv4(dns, "host.example.com", 0,
714 generic_dns_callback, &r1);
719 event_base_dispatch(base);
721 tt_int_op(drop_count, ==, 0);
723 tt_int_op(r1.type, ==, DNS_IPv4_A);
724 tt_int_op(r1.count, ==, 1);
725 tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080));
727 /* Now try again, but this time have the server get treated as
728 * failed, so we can send it a test probe. */
730 tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
731 tt_assert(! evdns_base_set_option(dns, "attempts:", "3"));
732 memset(&r1, 0, sizeof(r1));
734 evdns_base_resolve_ipv4(dns, "host.example.com", 0,
735 generic_dns_callback, &r1);
739 /* This will run until it answers the "google.com" probe request. */
740 event_base_dispatch(base);
742 /* We'll treat the server as failed here. */
743 tt_int_op(r1.result, ==, DNS_ERR_TIMEOUT);
745 /* It should work this time. */
746 tt_int_op(drop_count, ==, 0);
747 evdns_base_resolve_ipv4(dns, "host.example.com", 0,
748 generic_dns_callback, &r1);
750 event_base_dispatch(base);
751 tt_int_op(r1.result, ==, DNS_ERR_NONE);
752 tt_int_op(r1.type, ==, DNS_IPv4_A);
753 tt_int_op(r1.count, ==, 1);
754 tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080));
758 evdns_base_free(dns, 0);
760 evdns_close_server_port(port);
763 dns_retry_test(void *arg)
765 dns_retry_test_impl(arg, 0);
768 dns_retry_disable_when_inactive_test(void *arg)
770 dns_retry_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
773 static struct regress_dns_server_table internal_error_table[] = {
774 /* Error 4 (NOTIMPL) makes us reissue the request to another server
777 XXXX we should reissue under a much wider set of circumstances!
779 { "foof.example.com", "err", "4", 0 },
780 { NULL, NULL, NULL, 0 }
783 static struct regress_dns_server_table reissue_table[] = {
784 { "foof.example.com", "A", "240.15.240.15", 0 },
785 { NULL, NULL, NULL, 0 }
789 dns_reissue_test_impl(void *arg, int flags)
791 struct basic_test_data *data = arg;
792 struct event_base *base = data->base;
793 struct evdns_server_port *port1 = NULL, *port2 = NULL;
794 struct evdns_base *dns = NULL;
795 struct generic_dns_callback_result r1;
796 ev_uint16_t portnum1 = 0, portnum2=0;
797 char buf1[64], buf2[64];
799 port1 = regress_get_dnsserver(base, &portnum1, NULL,
800 regress_dns_server_cb, internal_error_table);
802 port2 = regress_get_dnsserver(base, &portnum2, NULL,
803 regress_dns_server_cb, reissue_table);
805 evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1);
806 evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2);
808 dns = evdns_base_new(base, flags);
809 tt_assert(!evdns_base_nameserver_ip_add(dns, buf1));
810 tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3"));
811 tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
812 tt_assert(! evdns_base_set_option(dns, "attempts:", "5"));
814 memset(&r1, 0, sizeof(r1));
815 evdns_base_resolve_ipv4(dns, "foof.example.com", 0,
816 generic_dns_callback, &r1);
818 /* Add this after, so that we are sure to get a reissue. */
819 tt_assert(!evdns_base_nameserver_ip_add(dns, buf2));
824 event_base_dispatch(base);
825 tt_int_op(r1.result, ==, DNS_ERR_NONE);
826 tt_int_op(r1.type, ==, DNS_IPv4_A);
827 tt_int_op(r1.count, ==, 1);
828 tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0xf00ff00f));
830 /* Make sure we dropped at least once. */
831 tt_int_op(internal_error_table[0].seen, >, 0);
835 evdns_base_free(dns, 0);
837 evdns_close_server_port(port1);
839 evdns_close_server_port(port2);
842 dns_reissue_test(void *arg)
844 dns_reissue_test_impl(arg, 0);
847 dns_reissue_disable_when_inactive_test(void *arg)
849 dns_reissue_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
854 dumb_bytes_fn(char *p, size_t n)
857 /* This gets us 6 bits of entropy per transaction ID, which means we
858 * will have probably have collisions and need to pick again. */
860 p[i] = (char)(rand() & 7);
865 dns_inflight_test_impl(void *arg, int flags)
867 struct basic_test_data *data = arg;
868 struct event_base *base = data->base;
869 struct evdns_base *dns = NULL;
870 struct evdns_server_port *dns_port = NULL;
871 ev_uint16_t portnum = 0;
873 int disable_when_inactive = flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE;
875 struct generic_dns_callback_result r[20];
878 dns_port = regress_get_dnsserver(base, &portnum, NULL,
879 regress_dns_server_cb, reissue_table);
881 if (disable_when_inactive) {
882 exit_port = dns_port;
885 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
887 dns = evdns_base_new(base, flags);
888 tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
889 tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3"));
890 tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0"));
893 evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r[i]);
898 event_base_dispatch(base);
901 tt_int_op(r[i].type, ==, DNS_IPv4_A);
902 tt_int_op(r[i].count, ==, 1);
903 tt_int_op(((ev_uint32_t*)r[i].addrs)[0], ==, htonl(0xf00ff00f));
908 evdns_base_free(dns, 0);
910 evdns_close_server_port(exit_port);
912 } else if (! disable_when_inactive) {
913 evdns_close_server_port(dns_port);
918 dns_inflight_test(void *arg)
920 dns_inflight_test_impl(arg, 0);
924 dns_disable_when_inactive_test(void *arg)
926 dns_inflight_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
930 dns_disable_when_inactive_no_ns_test(void *arg)
932 struct basic_test_data *data = arg;
933 struct event_base *base = data->base, *inactive_base;
934 struct evdns_base *dns = NULL;
935 ev_uint16_t portnum = 0;
937 struct generic_dns_callback_result r;
939 inactive_base = event_base_new();
940 tt_assert(inactive_base);
942 /** Create dns server with inactive base, to avoid replying to clients */
943 tt_assert(regress_dnsserver(inactive_base, &portnum, search_table));
944 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
946 dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
947 tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
948 tt_assert(! evdns_base_set_option(dns, "timeout:", "0.1"));
950 evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r);
954 event_base_dispatch(base);
956 tt_int_op(n_replies_left, ==, 0);
958 tt_int_op(r.result, ==, DNS_ERR_TIMEOUT);
959 tt_int_op(r.count, ==, 0);
960 tt_ptr_op(r.addrs, ==, NULL);
964 evdns_base_free(dns, 0);
965 regress_clean_dnsserver();
967 event_base_free(inactive_base);
970 /* === Test for bufferevent_socket_connect_hostname */
972 static int total_connected_or_failed = 0;
973 static int total_n_accepted = 0;
974 static struct event_base *be_connect_hostname_base = NULL;
976 /* Implements a DNS server for the connect_hostname test and the
977 * getaddrinfo_async test */
979 be_getaddrinfo_server_cb(struct evdns_server_request *req, void *data)
986 for (i=0;i<req->nquestions;++i) {
987 const int qtype = req->questions[i]->type;
988 const int qclass = req->questions[i]->dns_question_class;
989 const char *qname = req->questions[i]->name;
991 struct in6_addr ans6;
992 memset(&ans6, 0, sizeof(ans6));
994 TT_BLATHER(("Got question about %s, type=%d", qname, qtype));
996 if (qtype == EVDNS_TYPE_A &&
997 qclass == EVDNS_CLASS_INET &&
998 !evutil_ascii_strcasecmp(qname, "nobodaddy.example.com")) {
999 ans.s_addr = htonl(0x7f000001);
1000 evdns_server_request_add_a_reply(req, qname,
1001 1, &ans.s_addr, 2000);
1003 } else if (!evutil_ascii_strcasecmp(qname,
1004 "nosuchplace.example.com")) {
1005 /* ok, just say notfound. */
1006 } else if (!evutil_ascii_strcasecmp(qname,
1007 "both.example.com")) {
1008 if (qtype == EVDNS_TYPE_A) {
1009 ans.s_addr = htonl(0x50502020);
1010 evdns_server_request_add_a_reply(req, qname,
1011 1, &ans.s_addr, 2000);
1013 } else if (qtype == EVDNS_TYPE_AAAA) {
1014 ans6.s6_addr[0] = 0x80;
1015 ans6.s6_addr[1] = 0xff;
1016 ans6.s6_addr[14] = 0xbb;
1017 ans6.s6_addr[15] = 0xbb;
1018 evdns_server_request_add_aaaa_reply(req, qname,
1019 1, &ans6.s6_addr, 2000);
1022 evdns_server_request_add_cname_reply(req, qname,
1023 "both-canonical.example.com", 1000);
1024 } else if (!evutil_ascii_strcasecmp(qname,
1025 "v4only.example.com") ||
1026 !evutil_ascii_strcasecmp(qname, "v4assert.example.com")) {
1027 if (qtype == EVDNS_TYPE_A) {
1028 ans.s_addr = htonl(0x12345678);
1029 evdns_server_request_add_a_reply(req, qname,
1030 1, &ans.s_addr, 2000);
1032 } else if (!evutil_ascii_strcasecmp(qname,
1033 "v4assert.example.com")) {
1034 TT_FAIL(("Got an AAAA request for v4assert"));
1036 } else if (!evutil_ascii_strcasecmp(qname,
1037 "v6only.example.com") ||
1038 !evutil_ascii_strcasecmp(qname, "v6assert.example.com")) {
1039 if (qtype == EVDNS_TYPE_AAAA) {
1040 ans6.s6_addr[0] = 0x0b;
1041 ans6.s6_addr[1] = 0x0b;
1042 ans6.s6_addr[14] = 0xf0;
1043 ans6.s6_addr[15] = 0x0d;
1044 evdns_server_request_add_aaaa_reply(req, qname,
1045 1, &ans6.s6_addr, 2000);
1047 } else if (!evutil_ascii_strcasecmp(qname,
1048 "v6assert.example.com")) {
1049 TT_FAIL(("Got a A request for v6assert"));
1051 } else if (!evutil_ascii_strcasecmp(qname,
1052 "v6timeout.example.com")) {
1053 if (qtype == EVDNS_TYPE_A) {
1054 ans.s_addr = htonl(0xabcdef01);
1055 evdns_server_request_add_a_reply(req, qname,
1056 1, &ans.s_addr, 2000);
1058 } else if (qtype == EVDNS_TYPE_AAAA) {
1059 /* Let the v6 request time out.*/
1060 evdns_server_request_drop(req);
1063 } else if (!evutil_ascii_strcasecmp(qname,
1064 "v4timeout.example.com")) {
1065 if (qtype == EVDNS_TYPE_AAAA) {
1066 ans6.s6_addr[0] = 0x0a;
1067 ans6.s6_addr[1] = 0x0a;
1068 ans6.s6_addr[14] = 0xff;
1069 ans6.s6_addr[15] = 0x01;
1070 evdns_server_request_add_aaaa_reply(req, qname,
1071 1, &ans6.s6_addr, 2000);
1073 } else if (qtype == EVDNS_TYPE_A) {
1074 /* Let the v4 request time out.*/
1075 evdns_server_request_drop(req);
1078 } else if (!evutil_ascii_strcasecmp(qname,
1079 "v6timeout-nonexist.example.com")) {
1080 if (qtype == EVDNS_TYPE_A) {
1081 /* Fall through, give an nexist. */
1082 } else if (qtype == EVDNS_TYPE_AAAA) {
1083 /* Let the v6 request time out.*/
1084 evdns_server_request_drop(req);
1087 } else if (!evutil_ascii_strcasecmp(qname,
1088 "all-timeout.example.com")) {
1089 /* drop all requests */
1090 evdns_server_request_drop(req);
1093 TT_GRIPE(("Got weird request for %s",qname));
1097 TT_BLATHER(("answering"));
1098 evdns_server_request_respond(req, 0);
1100 TT_BLATHER(("saying nexist."));
1101 evdns_server_request_respond(req, 3);
1105 /* Implements a listener for connect_hostname test. */
1107 nil_accept_cb(struct evconnlistener *l, evutil_socket_t fd, struct sockaddr *s,
1108 int socklen, void *arg)
1113 /* don't do anything with the socket; let it close when we exit() */
1114 if (total_n_accepted >= 3 && total_connected_or_failed >= 5)
1115 event_base_loopexit(be_connect_hostname_base,
1119 struct be_conn_hostname_result {
1124 /* Bufferevent event callback for the connect_hostname test: remembers what
1127 be_connect_hostname_event_cb(struct bufferevent *bev, short what, void *ctx)
1129 struct be_conn_hostname_result *got = ctx;
1131 TT_BLATHER(("Got a bufferevent event %d", what));
1134 if ((what & BEV_EVENT_CONNECTED) || (what & BEV_EVENT_ERROR)) {
1136 if ((r = bufferevent_socket_get_dns_error(bev))) {
1138 TT_BLATHER(("DNS error %d: %s", r,
1139 evutil_gai_strerror(r)));
1140 } ++total_connected_or_failed;
1141 TT_BLATHER(("Got %d connections or errors.", total_connected_or_failed));
1143 if (total_n_accepted >= 3 && total_connected_or_failed >= 5)
1144 event_base_loopexit(be_connect_hostname_base,
1148 TT_FAIL(("Two events on one bufferevent. %d,%d",
1149 got->what, (int)what));
1154 test_bufferevent_connect_hostname(void *arg)
1156 struct basic_test_data *data = arg;
1157 struct evconnlistener *listener = NULL;
1158 struct bufferevent *be1=NULL, *be2=NULL, *be3=NULL, *be4=NULL, *be5=NULL;
1159 struct be_conn_hostname_result be1_outcome={0,0}, be2_outcome={0,0},
1160 be3_outcome={0,0}, be4_outcome={0,0}, be5_outcome={0,0};
1162 struct evdns_base *dns=NULL;
1163 struct evdns_server_port *port=NULL;
1164 struct sockaddr_in sin;
1165 int listener_port=-1;
1166 ev_uint16_t dns_port=0;
1167 int n_accept=0, n_dns=0;
1170 be_connect_hostname_base = data->base;
1172 /* Bind an address and figure out what port it's on. */
1173 memset(&sin, 0, sizeof(sin));
1174 sin.sin_family = AF_INET;
1175 sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
1177 listener = evconnlistener_new_bind(data->base, nil_accept_cb,
1179 LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC,
1180 -1, (struct sockaddr *)&sin, sizeof(sin));
1181 tt_assert(listener);
1182 listener_port = regress_get_socket_port(
1183 evconnlistener_get_fd(listener));
1185 port = regress_get_dnsserver(data->base, &dns_port, NULL,
1186 be_getaddrinfo_server_cb, &n_dns);
1188 tt_int_op(dns_port, >=, 0);
1190 /* Start an evdns_base that uses the server as its resolver. */
1191 dns = evdns_base_new(data->base, 0);
1192 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)dns_port);
1193 evdns_base_nameserver_ip_add(dns, buf);
1195 /* Now, finally, at long last, launch the bufferevents. One should do
1196 * a failing lookup IP, one should do a successful lookup by IP,
1197 * and one should do a successful lookup by hostname. */
1198 be1 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1199 be2 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1200 be3 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1201 be4 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1202 be5 = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1204 bufferevent_setcb(be1, NULL, NULL, be_connect_hostname_event_cb,
1206 bufferevent_setcb(be2, NULL, NULL, be_connect_hostname_event_cb,
1208 bufferevent_setcb(be3, NULL, NULL, be_connect_hostname_event_cb,
1210 bufferevent_setcb(be4, NULL, NULL, be_connect_hostname_event_cb,
1212 bufferevent_setcb(be5, NULL, NULL, be_connect_hostname_event_cb,
1215 /* Launch an async resolve that will fail. */
1216 tt_assert(!bufferevent_socket_connect_hostname(be1, dns, AF_INET,
1217 "nosuchplace.example.com", listener_port));
1218 /* Connect to the IP without resolving. */
1219 tt_assert(!bufferevent_socket_connect_hostname(be2, dns, AF_INET,
1220 "127.0.0.1", listener_port));
1221 /* Launch an async resolve that will succeed. */
1222 tt_assert(!bufferevent_socket_connect_hostname(be3, dns, AF_INET,
1223 "nobodaddy.example.com", listener_port));
1224 /* Use the blocking resolver. This one will fail if your resolver
1225 * can't resolve localhost to 127.0.0.1 */
1226 tt_assert(!bufferevent_socket_connect_hostname(be4, NULL, AF_INET,
1227 "localhost", listener_port));
1228 /* Use the blocking resolver with a nonexistent hostname. */
1229 tt_assert(!bufferevent_socket_connect_hostname(be5, NULL, AF_INET,
1230 "nonesuch.nowhere.example.com", 80));
1232 /* The blocking resolver will use the system nameserver, which
1233 * might tell us anything. (Yes, some twits even pretend that
1234 * example.com is real.) Let's see what answer to expect. */
1235 struct evutil_addrinfo hints, *ai = NULL;
1236 memset(&hints, 0, sizeof(hints));
1237 hints.ai_family = AF_INET;
1238 hints.ai_socktype = SOCK_STREAM;
1239 hints.ai_protocol = IPPROTO_TCP;
1240 expect_err5 = evutil_getaddrinfo(
1241 "nonesuch.nowhere.example.com", "80", &hints, &ai);
1244 event_base_dispatch(data->base);
1246 tt_int_op(be1_outcome.what, ==, BEV_EVENT_ERROR);
1247 tt_int_op(be1_outcome.dnserr, ==, EVUTIL_EAI_NONAME);
1248 tt_int_op(be2_outcome.what, ==, BEV_EVENT_CONNECTED);
1249 tt_int_op(be2_outcome.dnserr, ==, 0);
1250 tt_int_op(be3_outcome.what, ==, BEV_EVENT_CONNECTED);
1251 tt_int_op(be3_outcome.dnserr, ==, 0);
1252 tt_int_op(be4_outcome.what, ==, BEV_EVENT_CONNECTED);
1253 tt_int_op(be4_outcome.dnserr, ==, 0);
1255 tt_int_op(be5_outcome.what, ==, BEV_EVENT_ERROR);
1256 tt_int_op(be5_outcome.dnserr, ==, expect_err5);
1259 tt_int_op(n_accept, ==, 3);
1260 tt_int_op(n_dns, ==, 2);
1264 evconnlistener_free(listener);
1266 evdns_close_server_port(port);
1268 evdns_base_free(dns, 0);
1270 bufferevent_free(be1);
1272 bufferevent_free(be2);
1274 bufferevent_free(be3);
1276 bufferevent_free(be4);
1278 bufferevent_free(be5);
1282 struct gai_outcome {
1284 struct evutil_addrinfo *ai;
1287 static int n_gai_results_pending = 0;
1288 static struct event_base *exit_base_on_no_pending_results = NULL;
1291 gai_cb(int err, struct evutil_addrinfo *res, void *ptr)
1293 struct gai_outcome *go = ptr;
1296 if (--n_gai_results_pending <= 0 && exit_base_on_no_pending_results)
1297 event_base_loopexit(exit_base_on_no_pending_results, NULL);
1298 if (n_gai_results_pending < 900)
1299 TT_BLATHER(("Got an answer; expecting %d more.",
1300 n_gai_results_pending));
1304 cancel_gai_cb(evutil_socket_t fd, short what, void *ptr)
1306 struct evdns_getaddrinfo_request *r = ptr;
1307 evdns_getaddrinfo_cancel(r);
1311 test_getaddrinfo_async(void *arg)
1313 struct basic_test_data *data = arg;
1314 struct evutil_addrinfo hints, *a;
1315 struct gai_outcome local_outcome;
1316 struct gai_outcome a_out[12];
1318 struct evdns_getaddrinfo_request *r;
1320 struct evdns_server_port *port = NULL;
1321 ev_uint16_t dns_port = 0;
1322 int n_dns_questions = 0;
1323 struct evdns_base *dns_base;
1325 memset(&a_out, 0, sizeof(a_out));
1326 memset(&local_outcome, 0, sizeof(local_outcome));
1328 dns_base = evdns_base_new(data->base, 0);
1329 tt_assert(dns_base);
1332 evdns_base_load_hosts(dns_base, NULL);
1334 tt_assert(! evdns_base_set_option(dns_base, "timeout", "0.3"));
1335 tt_assert(! evdns_base_set_option(dns_base, "getaddrinfo-allow-skew", "0.2"));
1337 n_gai_results_pending = 10000; /* don't think about exiting yet. */
1339 /* 1. Try some cases that will never hit the asynchronous resolver. */
1340 /* 1a. Simple case with a symbolic service name */
1341 memset(&hints, 0, sizeof(hints));
1342 hints.ai_family = PF_UNSPEC;
1343 hints.ai_socktype = SOCK_STREAM;
1344 memset(&local_outcome, 0, sizeof(local_outcome));
1345 r = evdns_getaddrinfo(dns_base, "1.2.3.4", "http",
1346 &hints, gai_cb, &local_outcome);
1348 if (!local_outcome.err) {
1349 tt_ptr_op(local_outcome.ai,!=,NULL);
1350 test_ai_eq(local_outcome.ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
1351 evutil_freeaddrinfo(local_outcome.ai);
1352 local_outcome.ai = NULL;
1354 TT_BLATHER(("Apparently we have no getservbyname."));
1357 /* 1b. EVUTIL_AI_NUMERICHOST is set */
1358 memset(&hints, 0, sizeof(hints));
1359 hints.ai_family = PF_UNSPEC;
1360 hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1361 memset(&local_outcome, 0, sizeof(local_outcome));
1362 r = evdns_getaddrinfo(dns_base, "www.google.com", "80",
1363 &hints, gai_cb, &local_outcome);
1364 tt_ptr_op(r,==,NULL);
1365 tt_int_op(local_outcome.err,==,EVUTIL_EAI_NONAME);
1366 tt_ptr_op(local_outcome.ai,==,NULL);
1368 /* 1c. We give a numeric address (ipv6) */
1369 memset(&hints, 0, sizeof(hints));
1370 memset(&local_outcome, 0, sizeof(local_outcome));
1371 hints.ai_family = PF_UNSPEC;
1372 hints.ai_protocol = IPPROTO_TCP;
1373 r = evdns_getaddrinfo(dns_base, "f::f", "8008",
1374 &hints, gai_cb, &local_outcome);
1376 tt_int_op(local_outcome.err,==,0);
1377 tt_assert(local_outcome.ai);
1378 tt_ptr_op(local_outcome.ai->ai_next,==,NULL);
1379 test_ai_eq(local_outcome.ai, "[f::f]:8008", SOCK_STREAM, IPPROTO_TCP);
1380 evutil_freeaddrinfo(local_outcome.ai);
1381 local_outcome.ai = NULL;
1383 /* 1d. We give a numeric address (ipv4) */
1384 memset(&hints, 0, sizeof(hints));
1385 memset(&local_outcome, 0, sizeof(local_outcome));
1386 hints.ai_family = PF_UNSPEC;
1387 r = evdns_getaddrinfo(dns_base, "5.6.7.8", NULL,
1388 &hints, gai_cb, &local_outcome);
1390 tt_int_op(local_outcome.err,==,0);
1391 tt_assert(local_outcome.ai);
1392 a = ai_find_by_protocol(local_outcome.ai, IPPROTO_TCP);
1394 test_ai_eq(a, "5.6.7.8", SOCK_STREAM, IPPROTO_TCP);
1395 a = ai_find_by_protocol(local_outcome.ai, IPPROTO_UDP);
1397 test_ai_eq(a, "5.6.7.8", SOCK_DGRAM, IPPROTO_UDP);
1398 evutil_freeaddrinfo(local_outcome.ai);
1399 local_outcome.ai = NULL;
1401 /* 1e. nodename is NULL (bind) */
1402 memset(&hints, 0, sizeof(hints));
1403 memset(&local_outcome, 0, sizeof(local_outcome));
1404 hints.ai_family = PF_UNSPEC;
1405 hints.ai_socktype = SOCK_DGRAM;
1406 hints.ai_flags = EVUTIL_AI_PASSIVE;
1407 r = evdns_getaddrinfo(dns_base, NULL, "9090",
1408 &hints, gai_cb, &local_outcome);
1410 tt_int_op(local_outcome.err,==,0);
1411 tt_assert(local_outcome.ai);
1412 /* we should get a v4 address of 0.0.0.0... */
1413 a = ai_find_by_family(local_outcome.ai, PF_INET);
1415 test_ai_eq(a, "0.0.0.0:9090", SOCK_DGRAM, IPPROTO_UDP);
1416 /* ... and a v6 address of ::0 */
1417 a = ai_find_by_family(local_outcome.ai, PF_INET6);
1419 test_ai_eq(a, "[::]:9090", SOCK_DGRAM, IPPROTO_UDP);
1420 evutil_freeaddrinfo(local_outcome.ai);
1421 local_outcome.ai = NULL;
1423 /* 1f. nodename is NULL (connect) */
1424 memset(&hints, 0, sizeof(hints));
1425 memset(&local_outcome, 0, sizeof(local_outcome));
1426 hints.ai_family = PF_UNSPEC;
1427 hints.ai_socktype = SOCK_STREAM;
1428 r = evdns_getaddrinfo(dns_base, NULL, "2",
1429 &hints, gai_cb, &local_outcome);
1431 tt_int_op(local_outcome.err,==,0);
1432 tt_assert(local_outcome.ai);
1433 /* we should get a v4 address of 127.0.0.1 .... */
1434 a = ai_find_by_family(local_outcome.ai, PF_INET);
1436 test_ai_eq(a, "127.0.0.1:2", SOCK_STREAM, IPPROTO_TCP);
1437 /* ... and a v6 address of ::1 */
1438 a = ai_find_by_family(local_outcome.ai, PF_INET6);
1440 test_ai_eq(a, "[::1]:2", SOCK_STREAM, IPPROTO_TCP);
1441 evutil_freeaddrinfo(local_outcome.ai);
1442 local_outcome.ai = NULL;
1444 /* 1g. We find localhost immediately. (pf_unspec) */
1445 memset(&hints, 0, sizeof(hints));
1446 memset(&local_outcome, 0, sizeof(local_outcome));
1447 hints.ai_family = PF_UNSPEC;
1448 hints.ai_socktype = SOCK_STREAM;
1449 r = evdns_getaddrinfo(dns_base, "LOCALHOST", "80",
1450 &hints, gai_cb, &local_outcome);
1452 tt_int_op(local_outcome.err,==,0);
1453 tt_assert(local_outcome.ai);
1454 /* we should get a v4 address of 127.0.0.1 .... */
1455 a = ai_find_by_family(local_outcome.ai, PF_INET);
1457 test_ai_eq(a, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP);
1458 /* ... and a v6 address of ::1 */
1459 a = ai_find_by_family(local_outcome.ai, PF_INET6);
1461 test_ai_eq(a, "[::1]:80", SOCK_STREAM, IPPROTO_TCP);
1462 evutil_freeaddrinfo(local_outcome.ai);
1463 local_outcome.ai = NULL;
1465 /* 1g. We find localhost immediately. (pf_inet6) */
1466 memset(&hints, 0, sizeof(hints));
1467 memset(&local_outcome, 0, sizeof(local_outcome));
1468 hints.ai_family = PF_INET6;
1469 hints.ai_socktype = SOCK_STREAM;
1470 r = evdns_getaddrinfo(dns_base, "LOCALHOST", "9999",
1471 &hints, gai_cb, &local_outcome);
1473 tt_int_op(local_outcome.err,==,0);
1474 tt_assert(local_outcome.ai);
1475 a = local_outcome.ai;
1476 test_ai_eq(a, "[::1]:9999", SOCK_STREAM, IPPROTO_TCP);
1477 tt_ptr_op(a->ai_next, ==, NULL);
1478 evutil_freeaddrinfo(local_outcome.ai);
1479 local_outcome.ai = NULL;
1481 /* 2. Okay, now we can actually test the asynchronous resolver. */
1482 /* Start a dummy local dns server... */
1483 port = regress_get_dnsserver(data->base, &dns_port, NULL,
1484 be_getaddrinfo_server_cb, &n_dns_questions);
1486 tt_int_op(dns_port, >=, 0);
1487 /* ... and tell the evdns_base about it. */
1488 evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port);
1489 evdns_base_nameserver_ip_add(dns_base, buf);
1491 memset(&hints, 0, sizeof(hints));
1492 hints.ai_family = PF_UNSPEC;
1493 hints.ai_socktype = SOCK_STREAM;
1494 hints.ai_flags = EVUTIL_AI_CANONNAME;
1495 /* 0: Request for both.example.com should return both addresses. */
1496 r = evdns_getaddrinfo(dns_base, "both.example.com", "8000",
1497 &hints, gai_cb, &a_out[0]);
1500 /* 1: Request for v4only.example.com should return one address. */
1501 r = evdns_getaddrinfo(dns_base, "v4only.example.com", "8001",
1502 &hints, gai_cb, &a_out[1]);
1505 /* 2: Request for v6only.example.com should return one address. */
1507 r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002",
1508 &hints, gai_cb, &a_out[2]);
1511 /* 3: PF_INET request for v4assert.example.com should not generate a
1512 * v6 request. The server will fail the test if it does. */
1513 hints.ai_family = PF_INET;
1514 r = evdns_getaddrinfo(dns_base, "v4assert.example.com", "8003",
1515 &hints, gai_cb, &a_out[3]);
1518 /* 4: PF_INET6 request for v6assert.example.com should not generate a
1519 * v4 request. The server will fail the test if it does. */
1520 hints.ai_family = PF_INET6;
1521 r = evdns_getaddrinfo(dns_base, "v6assert.example.com", "8004",
1522 &hints, gai_cb, &a_out[4]);
1525 /* 5: PF_INET request for nosuchplace.example.com should give NEXIST. */
1526 hints.ai_family = PF_INET;
1527 r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8005",
1528 &hints, gai_cb, &a_out[5]);
1531 /* 6: PF_UNSPEC request for nosuchplace.example.com should give NEXIST.
1533 hints.ai_family = PF_UNSPEC;
1534 r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8006",
1535 &hints, gai_cb, &a_out[6]);
1538 /* 7: PF_UNSPEC request for v6timeout.example.com should give an ipv4
1540 hints.ai_family = PF_UNSPEC;
1541 r = evdns_getaddrinfo(dns_base, "v6timeout.example.com", "8007",
1542 &hints, gai_cb, &a_out[7]);
1545 /* 8: PF_UNSPEC request for v6timeout-nonexist.example.com should give
1547 hints.ai_family = PF_UNSPEC;
1548 r = evdns_getaddrinfo(dns_base, "v6timeout-nonexist.example.com",
1549 "8008", &hints, gai_cb, &a_out[8]);
1552 /* 9: AI_ADDRCONFIG should at least not crash. Can't test it more
1553 * without knowing what kind of internet we have. */
1554 hints.ai_flags |= EVUTIL_AI_ADDRCONFIG;
1555 r = evdns_getaddrinfo(dns_base, "both.example.com",
1556 "8009", &hints, gai_cb, &a_out[9]);
1559 /* 10: PF_UNSPEC for v4timeout.example.com should give an ipv6 address
1561 hints.ai_family = PF_UNSPEC;
1563 r = evdns_getaddrinfo(dns_base, "v4timeout.example.com", "8010",
1564 &hints, gai_cb, &a_out[10]);
1567 /* 11: timeout.example.com: cancel it after 100 msec. */
1568 r = evdns_getaddrinfo(dns_base, "all-timeout.example.com", "8011",
1569 &hints, gai_cb, &a_out[11]);
1574 tv.tv_usec = 100*1000; /* 100 msec */
1575 event_base_once(data->base, -1, EV_TIMEOUT, cancel_gai_cb,
1579 /* XXXXX There are more tests we could do, including:
1581 - A test to elicit NODATA.
1585 n_gai_results_pending = 12;
1586 exit_base_on_no_pending_results = data->base;
1588 event_base_dispatch(data->base);
1590 /* 0: both.example.com */
1591 tt_int_op(a_out[0].err, ==, 0);
1592 tt_assert(a_out[0].ai);
1593 tt_assert(a_out[0].ai->ai_next);
1594 tt_assert(!a_out[0].ai->ai_next->ai_next);
1595 a = ai_find_by_family(a_out[0].ai, PF_INET);
1597 test_ai_eq(a, "80.80.32.32:8000", SOCK_STREAM, IPPROTO_TCP);
1598 a = ai_find_by_family(a_out[0].ai, PF_INET6);
1600 test_ai_eq(a, "[80ff::bbbb]:8000", SOCK_STREAM, IPPROTO_TCP);
1601 tt_assert(a_out[0].ai->ai_canonname);
1602 tt_str_op(a_out[0].ai->ai_canonname, ==, "both-canonical.example.com");
1604 /* 1: v4only.example.com */
1605 tt_int_op(a_out[1].err, ==, 0);
1606 tt_assert(a_out[1].ai);
1607 tt_assert(! a_out[1].ai->ai_next);
1608 test_ai_eq(a_out[1].ai, "18.52.86.120:8001", SOCK_STREAM, IPPROTO_TCP);
1609 tt_assert(a_out[1].ai->ai_canonname == NULL);
1612 /* 2: v6only.example.com */
1613 tt_int_op(a_out[2].err, ==, 0);
1614 tt_assert(a_out[2].ai);
1615 tt_assert(! a_out[2].ai->ai_next);
1616 test_ai_eq(a_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP);
1618 /* 3: v4assert.example.com */
1619 tt_int_op(a_out[3].err, ==, 0);
1620 tt_assert(a_out[3].ai);
1621 tt_assert(! a_out[3].ai->ai_next);
1622 test_ai_eq(a_out[3].ai, "18.52.86.120:8003", SOCK_STREAM, IPPROTO_TCP);
1624 /* 4: v6assert.example.com */
1625 tt_int_op(a_out[4].err, ==, 0);
1626 tt_assert(a_out[4].ai);
1627 tt_assert(! a_out[4].ai->ai_next);
1628 test_ai_eq(a_out[4].ai, "[b0b::f00d]:8004", SOCK_STREAM, IPPROTO_TCP);
1630 /* 5: nosuchplace.example.com (inet) */
1631 tt_int_op(a_out[5].err, ==, EVUTIL_EAI_NONAME);
1632 tt_assert(! a_out[5].ai);
1634 /* 6: nosuchplace.example.com (unspec) */
1635 tt_int_op(a_out[6].err, ==, EVUTIL_EAI_NONAME);
1636 tt_assert(! a_out[6].ai);
1638 /* 7: v6timeout.example.com */
1639 tt_int_op(a_out[7].err, ==, 0);
1640 tt_assert(a_out[7].ai);
1641 tt_assert(! a_out[7].ai->ai_next);
1642 test_ai_eq(a_out[7].ai, "171.205.239.1:8007", SOCK_STREAM, IPPROTO_TCP);
1644 /* 8: v6timeout-nonexist.example.com */
1645 tt_int_op(a_out[8].err, ==, EVUTIL_EAI_NONAME);
1646 tt_assert(! a_out[8].ai);
1648 /* 9: both (ADDRCONFIG) */
1649 tt_int_op(a_out[9].err, ==, 0);
1650 tt_assert(a_out[9].ai);
1651 a = ai_find_by_family(a_out[9].ai, PF_INET);
1653 test_ai_eq(a, "80.80.32.32:8009", SOCK_STREAM, IPPROTO_TCP);
1655 tt_assert(ai_find_by_family(a_out[9].ai, PF_INET6));
1656 a = ai_find_by_family(a_out[9].ai, PF_INET6);
1658 test_ai_eq(a, "[80ff::bbbb]:8009", SOCK_STREAM, IPPROTO_TCP);
1660 tt_assert(ai_find_by_family(a_out[9].ai, PF_INET));
1662 /* 10: v4timeout.example.com */
1663 tt_int_op(a_out[10].err, ==, 0);
1664 tt_assert(a_out[10].ai);
1665 tt_assert(! a_out[10].ai->ai_next);
1666 test_ai_eq(a_out[10].ai, "[a0a::ff01]:8010", SOCK_STREAM, IPPROTO_TCP);
1668 /* 11: cancelled request. */
1669 tt_int_op(a_out[11].err, ==, EVUTIL_EAI_CANCEL);
1670 tt_assert(a_out[11].ai == NULL);
1673 if (local_outcome.ai)
1674 evutil_freeaddrinfo(local_outcome.ai);
1675 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
1676 for (i=0;i<(int)ARRAY_SIZE(a_out);++i) {
1678 evutil_freeaddrinfo(a_out[i].ai);
1681 evdns_close_server_port(port);
1683 evdns_base_free(dns_base, 0);
1686 struct gaic_request_status {
1688 struct event_base *base;
1689 struct evdns_base *dns_base;
1690 struct evdns_getaddrinfo_request *request;
1691 struct event cancel_event;
1695 #define GAIC_MAGIC 0x1234abcd
1697 static int pending = 0;
1700 gaic_cancel_request_cb(evutil_socket_t fd, short what, void *arg)
1702 struct gaic_request_status *status = arg;
1704 tt_assert(status->magic == GAIC_MAGIC);
1705 status->canceled = 1;
1706 evdns_getaddrinfo_cancel(status->request);
1709 event_base_loopexit(status->base, NULL);
1713 gaic_server_cb(struct evdns_server_request *req, void *arg)
1715 ev_uint32_t answer = 0x7f000001;
1716 tt_assert(req->nquestions);
1717 evdns_server_request_add_a_reply(req, req->questions[0]->name, 1,
1719 evdns_server_request_respond(req, 0);
1722 evdns_server_request_respond(req, DNS_ERR_REFUSED);
1727 gaic_getaddrinfo_cb(int result, struct evutil_addrinfo *res, void *arg)
1729 struct gaic_request_status *status = arg;
1730 struct event_base *base = status->base;
1731 tt_assert(status->magic == GAIC_MAGIC);
1733 if (result == EVUTIL_EAI_CANCEL) {
1734 tt_assert(status->canceled);
1736 event_del(&status->cancel_event);
1738 memset(status, 0xf0, sizeof(*status));
1743 event_base_loopexit(base, NULL);
1747 gaic_launch(struct event_base *base, struct evdns_base *dns_base)
1749 struct gaic_request_status *status = calloc(1, sizeof(*status));
1751 struct timeval tv = { 0, 10000 };
1752 status->magic = GAIC_MAGIC;
1753 status->base = base;
1754 status->dns_base = dns_base;
1755 event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb,
1757 status->request = evdns_getaddrinfo(dns_base,
1758 "foobar.bazquux.example.com", "80", NULL, gaic_getaddrinfo_cb,
1760 event_add(&status->cancel_event, &tv);
1764 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
1765 /* FIXME: We should move this to regress_main.c if anything else needs it.*/
1767 /* Trivial replacements for malloc/free/realloc to check for memory leaks.
1768 * Not threadsafe. */
1769 static int allocated_chunks = 0;
1772 cnt_malloc(size_t sz)
1774 allocated_chunks += 1;
1779 cnt_realloc(void *old, size_t sz)
1782 allocated_chunks += 1;
1784 allocated_chunks -= 1;
1785 return realloc(old, sz);
1791 allocated_chunks -= 1;
1795 struct testleak_env_t {
1796 struct event_base *base;
1797 struct evdns_base *dns_base;
1798 struct evdns_request *req;
1799 struct generic_dns_callback_result r;
1803 testleak_setup(const struct testcase_t *testcase)
1805 struct testleak_env_t *env;
1807 allocated_chunks = 0;
1809 /* Reset allocation counter, to start allocations from the very beginning.
1810 * (this will avoid false-positive negative numbers for allocated_chunks)
1812 libevent_global_shutdown();
1814 event_set_mem_functions(cnt_malloc, cnt_realloc, cnt_free);
1816 event_enable_debug_mode();
1818 /* not mm_calloc: we don't want to mess with the count. */
1819 env = calloc(1, sizeof(struct testleak_env_t));
1820 env->base = event_base_new();
1821 env->dns_base = evdns_base_new(env->base, 0);
1822 env->req = evdns_base_resolve_ipv4(
1823 env->dns_base, "example.com", DNS_QUERY_NO_SEARCH,
1824 generic_dns_callback, &env->r);
1829 testleak_cleanup(const struct testcase_t *testcase, void *env_)
1832 struct testleak_env_t *env = env_;
1834 #ifdef EVENT__DISABLE_DEBUG_MODE
1835 tt_int_op(allocated_chunks, ==, 0);
1837 libevent_global_shutdown();
1838 tt_int_op(allocated_chunks, ==, 0);
1844 evdns_base_free(env->dns_base, 0);
1846 event_base_free(env->base);
1852 static struct testcase_setup_t testleak_funcs = {
1853 testleak_setup, testleak_cleanup
1857 test_dbg_leak_cancel(void *env_)
1859 /* cancel, loop, free/dns, free/base */
1860 struct testleak_env_t *env = env_;
1861 int send_err_shutdown = 1;
1862 evdns_cancel_request(env->dns_base, env->req);
1865 /* `req` is freed in callback, that's why one loop is required. */
1866 event_base_loop(env->base, EVLOOP_NONBLOCK);
1868 /* send_err_shutdown means nothing as soon as our request is
1869 * already canceled */
1870 evdns_base_free(env->dns_base, send_err_shutdown);
1872 event_base_free(env->base);
1877 dbg_leak_resume(void *env_, int cancel, int send_err_shutdown)
1879 /* cancel, loop, free/dns, free/base */
1880 struct testleak_env_t *env = env_;
1882 evdns_cancel_request(env->dns_base, env->req);
1883 tt_assert(!evdns_base_resume(env->dns_base));
1885 /* TODO: No nameservers, request can't be processed, must be errored */
1886 tt_assert(!evdns_base_resume(env->dns_base));
1890 * Because we don't cancel request,
1891 * and want our callback to recieve DNS_ERR_SHUTDOWN,
1892 * we use deferred callback, and there was
1893 * - one extra malloc(),
1894 * @see reply_schedule_callback()
1895 * - and one missing free
1896 * @see request_finished() (req->handle->pending_cb = 1)
1897 * than we don't need to count in testleak_cleanup()
1899 * So just decrement allocated_chunks to 2,
1900 * like we already take care about it.
1902 if (!cancel && send_err_shutdown) {
1903 allocated_chunks -= 2;
1906 event_base_loop(env->base, EVLOOP_NONBLOCK);
1909 evdns_base_free(env->dns_base, send_err_shutdown);
1912 event_base_free(env->base);
1916 #define IMPL_DBG_LEAK_RESUME(name, cancel, send_err_shutdown) \
1918 test_dbg_leak_##name##_(void *env_) \
1920 dbg_leak_resume(env_, cancel, send_err_shutdown); \
1922 IMPL_DBG_LEAK_RESUME(resume, 0, 0)
1923 IMPL_DBG_LEAK_RESUME(cancel_and_resume, 1, 0)
1924 IMPL_DBG_LEAK_RESUME(resume_send_err, 0, 1)
1925 IMPL_DBG_LEAK_RESUME(cancel_and_resume_send_err, 1, 1)
1928 test_dbg_leak_shutdown(void *env_)
1930 /* free/dns, loop, free/base */
1931 struct testleak_env_t *env = env_;
1932 int send_err_shutdown = 1;
1934 /* `req` is freed both with `send_err_shutdown` and without it,
1935 * the only difference is `evdns_callback` call */
1938 evdns_base_free(env->dns_base, send_err_shutdown);
1941 /* `req` is freed in callback, that's why one loop is required */
1942 event_base_loop(env->base, EVLOOP_NONBLOCK);
1943 event_base_free(env->base);
1949 test_getaddrinfo_async_cancel_stress(void *ptr)
1951 struct event_base *base;
1952 struct evdns_base *dns_base = NULL;
1953 struct evdns_server_port *server = NULL;
1954 evutil_socket_t fd = -1;
1955 struct sockaddr_in sin;
1956 struct sockaddr_storage ss;
1960 base = event_base_new();
1961 dns_base = evdns_base_new(base, 0);
1963 memset(&sin, 0, sizeof(sin));
1964 sin.sin_family = AF_INET;
1966 sin.sin_addr.s_addr = htonl(0x7f000001);
1967 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
1968 tt_abort_perror("socket");
1970 evutil_make_socket_nonblocking(fd);
1971 if (bind(fd, (struct sockaddr*)&sin, sizeof(sin))<0) {
1972 tt_abort_perror("bind");
1974 server = evdns_add_server_port_with_base(base, fd, 0, gaic_server_cb,
1977 memset(&ss, 0, sizeof(ss));
1979 if (getsockname(fd, (struct sockaddr*)&ss, &slen)<0) {
1980 tt_abort_perror("getsockname");
1982 evdns_base_nameserver_sockaddr_add(dns_base,
1983 (struct sockaddr*)&ss, slen, 0);
1985 for (i = 0; i < 1000; ++i) {
1986 gaic_launch(base, dns_base);
1989 event_base_dispatch(base);
1993 evdns_base_free(dns_base, 1);
1995 evdns_close_server_port(server);
1997 event_base_free(base);
1999 evutil_closesocket(fd);
2003 #define DNS_LEGACY(name, flags) \
2004 { #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup, \
2007 struct testcase_t dns_testcases[] = {
2008 DNS_LEGACY(server, TT_FORK|TT_NEED_BASE),
2009 DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
2010 DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
2011 DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
2012 { "resolve_reverse", dns_resolve_reverse, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
2013 { "search", dns_search_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2014 { "search_cancel", dns_search_cancel_test,
2015 TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2016 { "retry", dns_retry_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2017 { "retry_disable_when_inactive", dns_retry_disable_when_inactive_test,
2018 TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2019 { "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2020 { "reissue_disable_when_inactive", dns_reissue_disable_when_inactive_test,
2021 TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2022 { "inflight", dns_inflight_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2023 { "bufferevent_connect_hostname", test_bufferevent_connect_hostname,
2024 TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2025 { "disable_when_inactive", dns_disable_when_inactive_test,
2026 TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2027 { "disable_when_inactive_no_ns", dns_disable_when_inactive_no_ns_test,
2028 TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2030 { "getaddrinfo_async", test_getaddrinfo_async,
2031 TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"" },
2032 { "getaddrinfo_cancel_stress", test_getaddrinfo_async_cancel_stress,
2033 TT_FORK, NULL, NULL },
2035 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
2036 { "leak_shutdown", test_dbg_leak_shutdown, TT_FORK, &testleak_funcs, NULL },
2037 { "leak_cancel", test_dbg_leak_cancel, TT_FORK, &testleak_funcs, NULL },
2039 { "leak_resume", test_dbg_leak_resume_, TT_FORK, &testleak_funcs, NULL },
2040 { "leak_cancel_and_resume", test_dbg_leak_cancel_and_resume_,
2041 TT_FORK, &testleak_funcs, NULL },
2042 { "leak_resume_send_err", test_dbg_leak_resume_send_err_,
2043 TT_FORK, &testleak_funcs, NULL },
2044 { "leak_cancel_and_resume_send_err", test_dbg_leak_cancel_and_resume_send_err_,
2045 TT_FORK, &testleak_funcs, NULL },