xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_dns.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert #include "../util-internal.h"
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert #ifdef _WIN32
302b15cb3dSCy Schubert #include <winsock2.h>
312b15cb3dSCy Schubert #include <windows.h>
322b15cb3dSCy Schubert #include <ws2tcpip.h>
332b15cb3dSCy Schubert #endif
342b15cb3dSCy Schubert 
352b15cb3dSCy Schubert #include "event2/event-config.h"
362b15cb3dSCy Schubert 
372b15cb3dSCy Schubert #include <sys/types.h>
382b15cb3dSCy Schubert #include <sys/stat.h>
392b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
402b15cb3dSCy Schubert #include <sys/time.h>
412b15cb3dSCy Schubert #endif
422b15cb3dSCy Schubert #include <sys/queue.h>
432b15cb3dSCy Schubert #ifndef _WIN32
442b15cb3dSCy Schubert #include <sys/socket.h>
452b15cb3dSCy Schubert #include <signal.h>
462b15cb3dSCy Schubert #include <netinet/in.h>
472b15cb3dSCy Schubert #include <arpa/inet.h>
482b15cb3dSCy Schubert #include <unistd.h>
492b15cb3dSCy Schubert #endif
502b15cb3dSCy Schubert #ifdef EVENT__HAVE_NETINET_IN6_H
512b15cb3dSCy Schubert #include <netinet/in6.h>
522b15cb3dSCy Schubert #endif
532b15cb3dSCy Schubert #ifdef HAVE_NETDB_H
542b15cb3dSCy Schubert #include <netdb.h>
552b15cb3dSCy Schubert #endif
562b15cb3dSCy Schubert #include <fcntl.h>
572b15cb3dSCy Schubert #include <stdlib.h>
582b15cb3dSCy Schubert #include <stdio.h>
592b15cb3dSCy Schubert #include <string.h>
602b15cb3dSCy Schubert #include <errno.h>
612b15cb3dSCy Schubert 
62*a466cc55SCy Schubert #ifdef EVENT__HAVE_SYS_RESOURCE_H
63*a466cc55SCy Schubert #include <sys/resource.h>
64*a466cc55SCy Schubert #endif
65*a466cc55SCy Schubert 
662b15cb3dSCy Schubert #include "event2/dns.h"
672b15cb3dSCy Schubert #include "event2/dns_compat.h"
682b15cb3dSCy Schubert #include "event2/dns_struct.h"
692b15cb3dSCy Schubert #include "event2/event.h"
702b15cb3dSCy Schubert #include "event2/event_compat.h"
712b15cb3dSCy Schubert #include "event2/event_struct.h"
722b15cb3dSCy Schubert #include "event2/util.h"
732b15cb3dSCy Schubert #include "event2/listener.h"
742b15cb3dSCy Schubert #include "event2/bufferevent.h"
75*a466cc55SCy Schubert #include <event2/thread.h>
762b15cb3dSCy Schubert #include "log-internal.h"
77*a466cc55SCy Schubert #include "evthread-internal.h"
782b15cb3dSCy Schubert #include "regress.h"
792b15cb3dSCy Schubert #include "regress_testutils.h"
80*a466cc55SCy Schubert #include "regress_thread.h"
81*a466cc55SCy Schubert 
82*a466cc55SCy Schubert #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
832b15cb3dSCy Schubert 
842b15cb3dSCy Schubert static int dns_ok = 0;
852b15cb3dSCy Schubert static int dns_got_cancel = 0;
862b15cb3dSCy Schubert static int dns_err = 0;
872b15cb3dSCy Schubert 
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert static void
dns_gethostbyname_cb(int result,char type,int count,int ttl,void * addresses,void * arg)902b15cb3dSCy Schubert dns_gethostbyname_cb(int result, char type, int count, int ttl,
912b15cb3dSCy Schubert     void *addresses, void *arg)
922b15cb3dSCy Schubert {
932b15cb3dSCy Schubert 	dns_ok = dns_err = 0;
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert 	if (result == DNS_ERR_TIMEOUT) {
962b15cb3dSCy Schubert 		printf("[Timed out] ");
972b15cb3dSCy Schubert 		dns_err = result;
982b15cb3dSCy Schubert 		goto out;
992b15cb3dSCy Schubert 	}
1002b15cb3dSCy Schubert 
1012b15cb3dSCy Schubert 	if (result != DNS_ERR_NONE) {
1022b15cb3dSCy Schubert 		printf("[Error code %d] ", result);
1032b15cb3dSCy Schubert 		goto out;
1042b15cb3dSCy Schubert 	}
1052b15cb3dSCy Schubert 
1062b15cb3dSCy Schubert 	TT_BLATHER(("type: %d, count: %d, ttl: %d: ", type, count, ttl));
1072b15cb3dSCy Schubert 
1082b15cb3dSCy Schubert 	switch (type) {
1092b15cb3dSCy Schubert 	case DNS_IPv6_AAAA: {
1102b15cb3dSCy Schubert #if defined(EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
1112b15cb3dSCy Schubert 		struct in6_addr *in6_addrs = addresses;
1122b15cb3dSCy Schubert 		char buf[INET6_ADDRSTRLEN+1];
1132b15cb3dSCy Schubert 		int i;
1142b15cb3dSCy Schubert 		/* a resolution that's not valid does not help */
1152b15cb3dSCy Schubert 		if (ttl < 0)
1162b15cb3dSCy Schubert 			goto out;
1172b15cb3dSCy Schubert 		for (i = 0; i < count; ++i) {
1182b15cb3dSCy Schubert 			const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[i], buf,sizeof(buf));
1192b15cb3dSCy Schubert 			if (b)
1202b15cb3dSCy Schubert 				TT_BLATHER(("%s ", b));
1212b15cb3dSCy Schubert 			else
1222b15cb3dSCy Schubert 				TT_BLATHER(("%s ", strerror(errno)));
1232b15cb3dSCy Schubert 		}
1242b15cb3dSCy Schubert #endif
1252b15cb3dSCy Schubert 		break;
1262b15cb3dSCy Schubert 	}
1272b15cb3dSCy Schubert 	case DNS_IPv4_A: {
1282b15cb3dSCy Schubert 		struct in_addr *in_addrs = addresses;
1292b15cb3dSCy Schubert 		int i;
1302b15cb3dSCy Schubert 		/* a resolution that's not valid does not help */
1312b15cb3dSCy Schubert 		if (ttl < 0)
1322b15cb3dSCy Schubert 			goto out;
1332b15cb3dSCy Schubert 		for (i = 0; i < count; ++i)
1342b15cb3dSCy Schubert 			TT_BLATHER(("%s ", inet_ntoa(in_addrs[i])));
1352b15cb3dSCy Schubert 		break;
1362b15cb3dSCy Schubert 	}
1372b15cb3dSCy Schubert 	case DNS_PTR:
1382b15cb3dSCy Schubert 		/* may get at most one PTR */
1392b15cb3dSCy Schubert 		if (count != 1)
1402b15cb3dSCy Schubert 			goto out;
1412b15cb3dSCy Schubert 
1422b15cb3dSCy Schubert 		TT_BLATHER(("%s ", *(char **)addresses));
1432b15cb3dSCy Schubert 		break;
1442b15cb3dSCy Schubert 	default:
1452b15cb3dSCy Schubert 		goto out;
1462b15cb3dSCy Schubert 	}
1472b15cb3dSCy Schubert 
1482b15cb3dSCy Schubert 	dns_ok = type;
1492b15cb3dSCy Schubert 
1502b15cb3dSCy Schubert out:
1512b15cb3dSCy Schubert 	if (arg == NULL)
1522b15cb3dSCy Schubert 		event_loopexit(NULL);
1532b15cb3dSCy Schubert 	else
1542b15cb3dSCy Schubert 		event_base_loopexit((struct event_base *)arg, NULL);
1552b15cb3dSCy Schubert }
1562b15cb3dSCy Schubert 
1572b15cb3dSCy Schubert static void
dns_gethostbyname(void)1582b15cb3dSCy Schubert dns_gethostbyname(void)
1592b15cb3dSCy Schubert {
1602b15cb3dSCy Schubert 	dns_ok = 0;
1612b15cb3dSCy Schubert 	evdns_resolve_ipv4("www.monkey.org", 0, dns_gethostbyname_cb, NULL);
1622b15cb3dSCy Schubert 	event_dispatch();
1632b15cb3dSCy Schubert 
1642b15cb3dSCy Schubert 	tt_int_op(dns_ok, ==, DNS_IPv4_A);
1652b15cb3dSCy Schubert 	test_ok = dns_ok;
1662b15cb3dSCy Schubert end:
1672b15cb3dSCy Schubert 	;
1682b15cb3dSCy Schubert }
1692b15cb3dSCy Schubert 
1702b15cb3dSCy Schubert static void
dns_gethostbyname6(void)1712b15cb3dSCy Schubert dns_gethostbyname6(void)
1722b15cb3dSCy Schubert {
1732b15cb3dSCy Schubert 	dns_ok = 0;
1742b15cb3dSCy Schubert 	evdns_resolve_ipv6("www.ietf.org", 0, dns_gethostbyname_cb, NULL);
1752b15cb3dSCy Schubert 	event_dispatch();
1762b15cb3dSCy Schubert 
1772b15cb3dSCy Schubert 	if (!dns_ok && dns_err == DNS_ERR_TIMEOUT) {
1782b15cb3dSCy Schubert 		tt_skip();
1792b15cb3dSCy Schubert 	}
1802b15cb3dSCy Schubert 
1812b15cb3dSCy Schubert 	tt_int_op(dns_ok, ==, DNS_IPv6_AAAA);
1822b15cb3dSCy Schubert 	test_ok = 1;
1832b15cb3dSCy Schubert end:
1842b15cb3dSCy Schubert 	;
1852b15cb3dSCy Schubert }
1862b15cb3dSCy Schubert 
1872b15cb3dSCy Schubert static void
dns_gethostbyaddr(void)1882b15cb3dSCy Schubert dns_gethostbyaddr(void)
1892b15cb3dSCy Schubert {
1902b15cb3dSCy Schubert 	struct in_addr in;
1912b15cb3dSCy Schubert 	in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
1922b15cb3dSCy Schubert 	dns_ok = 0;
1932b15cb3dSCy Schubert 	evdns_resolve_reverse(&in, 0, dns_gethostbyname_cb, NULL);
1942b15cb3dSCy Schubert 	event_dispatch();
1952b15cb3dSCy Schubert 
1962b15cb3dSCy Schubert 	tt_int_op(dns_ok, ==, DNS_PTR);
1972b15cb3dSCy Schubert 	test_ok = dns_ok;
1982b15cb3dSCy Schubert end:
1992b15cb3dSCy Schubert 	;
2002b15cb3dSCy Schubert }
2012b15cb3dSCy Schubert 
2022b15cb3dSCy Schubert static void
dns_resolve_reverse(void * ptr)2032b15cb3dSCy Schubert dns_resolve_reverse(void *ptr)
2042b15cb3dSCy Schubert {
2052b15cb3dSCy Schubert 	struct in_addr in;
2062b15cb3dSCy Schubert 	struct event_base *base = event_base_new();
207*a466cc55SCy Schubert 	struct evdns_base *dns = evdns_base_new(base, EVDNS_BASE_INITIALIZE_NAMESERVERS);
2082b15cb3dSCy Schubert 	struct evdns_request *req = NULL;
2092b15cb3dSCy Schubert 
2102b15cb3dSCy Schubert 	tt_assert(base);
2112b15cb3dSCy Schubert 	tt_assert(dns);
2122b15cb3dSCy Schubert 	in.s_addr = htonl(0x7f000001ul); /* 127.0.0.1 */
2132b15cb3dSCy Schubert 	dns_ok = 0;
2142b15cb3dSCy Schubert 
2152b15cb3dSCy Schubert 	req = evdns_base_resolve_reverse(
2162b15cb3dSCy Schubert 		dns, &in, 0, dns_gethostbyname_cb, base);
2172b15cb3dSCy Schubert 	tt_assert(req);
2182b15cb3dSCy Schubert 
2192b15cb3dSCy Schubert 	event_base_dispatch(base);
2202b15cb3dSCy Schubert 
2212b15cb3dSCy Schubert 	tt_int_op(dns_ok, ==, DNS_PTR);
2222b15cb3dSCy Schubert 
2232b15cb3dSCy Schubert end:
2242b15cb3dSCy Schubert 	if (dns)
2252b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
2262b15cb3dSCy Schubert 	if (base)
2272b15cb3dSCy Schubert 		event_base_free(base);
2282b15cb3dSCy Schubert }
2292b15cb3dSCy Schubert 
2302b15cb3dSCy Schubert static int n_server_responses = 0;
2312b15cb3dSCy Schubert 
2322b15cb3dSCy Schubert static void
dns_server_request_cb(struct evdns_server_request * req,void * data)2332b15cb3dSCy Schubert dns_server_request_cb(struct evdns_server_request *req, void *data)
2342b15cb3dSCy Schubert {
2352b15cb3dSCy Schubert 	int i, r;
2362b15cb3dSCy Schubert 	const char TEST_ARPA[] = "11.11.168.192.in-addr.arpa";
2372b15cb3dSCy Schubert 	const char TEST_IN6[] =
2382b15cb3dSCy Schubert 	    "f.e.f.e." "0.0.0.0." "0.0.0.0." "1.1.1.1."
2392b15cb3dSCy Schubert 	    "a.a.a.a." "0.0.0.0." "0.0.0.0." "0.f.f.f.ip6.arpa";
2402b15cb3dSCy Schubert 
2412b15cb3dSCy Schubert 	for (i = 0; i < req->nquestions; ++i) {
2422b15cb3dSCy Schubert 		const int qtype = req->questions[i]->type;
2432b15cb3dSCy Schubert 		const int qclass = req->questions[i]->dns_question_class;
2442b15cb3dSCy Schubert 		const char *qname = req->questions[i]->name;
2452b15cb3dSCy Schubert 
2462b15cb3dSCy Schubert 		struct in_addr ans;
2472b15cb3dSCy Schubert 		ans.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
2482b15cb3dSCy Schubert 		if (qtype == EVDNS_TYPE_A &&
2492b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
2502b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "zz.example.com")) {
2512b15cb3dSCy Schubert 			r = evdns_server_request_add_a_reply(req, qname,
2522b15cb3dSCy Schubert 			    1, &ans.s_addr, 12345);
2532b15cb3dSCy Schubert 			if (r<0)
2542b15cb3dSCy Schubert 				dns_ok = 0;
2552b15cb3dSCy Schubert 		} else if (qtype == EVDNS_TYPE_AAAA &&
2562b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
2572b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "zz.example.com")) {
2582b15cb3dSCy Schubert 			char addr6[17] = "abcdefghijklmnop";
2592b15cb3dSCy Schubert 			r = evdns_server_request_add_aaaa_reply(req,
2602b15cb3dSCy Schubert 			    qname, 1, addr6, 123);
2612b15cb3dSCy Schubert 			if (r<0)
2622b15cb3dSCy Schubert 				dns_ok = 0;
2632b15cb3dSCy Schubert 		} else if (qtype == EVDNS_TYPE_PTR &&
2642b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
2652b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, TEST_ARPA)) {
2662b15cb3dSCy Schubert 			r = evdns_server_request_add_ptr_reply(req, NULL,
2672b15cb3dSCy Schubert 			    qname, "ZZ.EXAMPLE.COM", 54321);
2682b15cb3dSCy Schubert 			if (r<0)
2692b15cb3dSCy Schubert 				dns_ok = 0;
2702b15cb3dSCy Schubert 		} else if (qtype == EVDNS_TYPE_PTR &&
2712b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
2722b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, TEST_IN6)){
2732b15cb3dSCy Schubert 			r = evdns_server_request_add_ptr_reply(req, NULL,
2742b15cb3dSCy Schubert 			    qname,
2752b15cb3dSCy Schubert 			    "ZZ-INET6.EXAMPLE.COM", 54322);
2762b15cb3dSCy Schubert 			if (r<0)
2772b15cb3dSCy Schubert 				dns_ok = 0;
2782b15cb3dSCy Schubert 		} else if (qtype == EVDNS_TYPE_A &&
2792b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
2802b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "drop.example.com")) {
2812b15cb3dSCy Schubert 			if (evdns_server_request_drop(req)<0)
2822b15cb3dSCy Schubert 				dns_ok = 0;
2832b15cb3dSCy Schubert 			return;
2842b15cb3dSCy Schubert 		} else {
2852b15cb3dSCy Schubert 			printf("Unexpected question %d %d \"%s\" ",
2862b15cb3dSCy Schubert 			    qtype, qclass, qname);
2872b15cb3dSCy Schubert 			dns_ok = 0;
2882b15cb3dSCy Schubert 		}
2892b15cb3dSCy Schubert 	}
2902b15cb3dSCy Schubert 	r = evdns_server_request_respond(req, 0);
2912b15cb3dSCy Schubert 	if (r<0) {
2922b15cb3dSCy Schubert 		printf("Couldn't send reply. ");
2932b15cb3dSCy Schubert 		dns_ok = 0;
2942b15cb3dSCy Schubert 	}
2952b15cb3dSCy Schubert }
2962b15cb3dSCy Schubert 
2972b15cb3dSCy Schubert static void
dns_server_gethostbyname_cb(int result,char type,int count,int ttl,void * addresses,void * arg)2982b15cb3dSCy Schubert dns_server_gethostbyname_cb(int result, char type, int count, int ttl,
2992b15cb3dSCy Schubert     void *addresses, void *arg)
3002b15cb3dSCy Schubert {
3012b15cb3dSCy Schubert 	if (result == DNS_ERR_CANCEL) {
3022b15cb3dSCy Schubert 		if (arg != (void*)(char*)90909) {
3032b15cb3dSCy Schubert 			printf("Unexpected cancelation");
3042b15cb3dSCy Schubert 			dns_ok = 0;
3052b15cb3dSCy Schubert 		}
3062b15cb3dSCy Schubert 		dns_got_cancel = 1;
3072b15cb3dSCy Schubert 		goto out;
3082b15cb3dSCy Schubert 	}
3092b15cb3dSCy Schubert 	if (result != DNS_ERR_NONE) {
3102b15cb3dSCy Schubert 		printf("Unexpected result %d. ", result);
3112b15cb3dSCy Schubert 		dns_ok = 0;
3122b15cb3dSCy Schubert 		goto out;
3132b15cb3dSCy Schubert 	}
3142b15cb3dSCy Schubert 	if (count != 1) {
3152b15cb3dSCy Schubert 		printf("Unexpected answer count %d. ", count);
3162b15cb3dSCy Schubert 		dns_ok = 0;
3172b15cb3dSCy Schubert 		goto out;
3182b15cb3dSCy Schubert 	}
3192b15cb3dSCy Schubert 	switch (type) {
3202b15cb3dSCy Schubert 	case DNS_IPv4_A: {
3212b15cb3dSCy Schubert 		struct in_addr *in_addrs = addresses;
3222b15cb3dSCy Schubert 		if (in_addrs[0].s_addr != htonl(0xc0a80b0bUL) || ttl != 12345) {
3232b15cb3dSCy Schubert 			printf("Bad IPv4 response \"%s\" %d. ",
3242b15cb3dSCy Schubert 					inet_ntoa(in_addrs[0]), ttl);
3252b15cb3dSCy Schubert 			dns_ok = 0;
3262b15cb3dSCy Schubert 			goto out;
3272b15cb3dSCy Schubert 		}
3282b15cb3dSCy Schubert 		break;
3292b15cb3dSCy Schubert 	}
3302b15cb3dSCy Schubert 	case DNS_IPv6_AAAA: {
3312b15cb3dSCy Schubert #if defined (EVENT__HAVE_STRUCT_IN6_ADDR) && defined(EVENT__HAVE_INET_NTOP) && defined(INET6_ADDRSTRLEN)
3322b15cb3dSCy Schubert 		struct in6_addr *in6_addrs = addresses;
3332b15cb3dSCy Schubert 		char buf[INET6_ADDRSTRLEN+1];
3342b15cb3dSCy Schubert 		if (memcmp(&in6_addrs[0].s6_addr, "abcdefghijklmnop", 16)
3352b15cb3dSCy Schubert 		    || ttl != 123) {
3362b15cb3dSCy Schubert 			const char *b = evutil_inet_ntop(AF_INET6, &in6_addrs[0],buf,sizeof(buf));
3372b15cb3dSCy Schubert 			printf("Bad IPv6 response \"%s\" %d. ", b, ttl);
3382b15cb3dSCy Schubert 			dns_ok = 0;
3392b15cb3dSCy Schubert 			goto out;
3402b15cb3dSCy Schubert 		}
3412b15cb3dSCy Schubert #endif
3422b15cb3dSCy Schubert 		break;
3432b15cb3dSCy Schubert 	}
3442b15cb3dSCy Schubert 	case DNS_PTR: {
3452b15cb3dSCy Schubert 		char **addrs = addresses;
3462b15cb3dSCy Schubert 		if (arg != (void*)6) {
3472b15cb3dSCy Schubert 			if (strcmp(addrs[0], "ZZ.EXAMPLE.COM") ||
3482b15cb3dSCy Schubert 			    ttl != 54321) {
3492b15cb3dSCy Schubert 				printf("Bad PTR response \"%s\" %d. ",
3502b15cb3dSCy Schubert 				    addrs[0], ttl);
3512b15cb3dSCy Schubert 				dns_ok = 0;
3522b15cb3dSCy Schubert 				goto out;
3532b15cb3dSCy Schubert 			}
3542b15cb3dSCy Schubert 		} else {
3552b15cb3dSCy Schubert 			if (strcmp(addrs[0], "ZZ-INET6.EXAMPLE.COM") ||
3562b15cb3dSCy Schubert 			    ttl != 54322) {
3572b15cb3dSCy Schubert 				printf("Bad ipv6 PTR response \"%s\" %d. ",
3582b15cb3dSCy Schubert 				    addrs[0], ttl);
3592b15cb3dSCy Schubert 				dns_ok = 0;
3602b15cb3dSCy Schubert 				goto out;
3612b15cb3dSCy Schubert 			}
3622b15cb3dSCy Schubert 		}
3632b15cb3dSCy Schubert 		break;
3642b15cb3dSCy Schubert 	}
3652b15cb3dSCy Schubert 	default:
3662b15cb3dSCy Schubert 		printf("Bad response type %d. ", type);
3672b15cb3dSCy Schubert 		dns_ok = 0;
3682b15cb3dSCy Schubert 	}
3692b15cb3dSCy Schubert  out:
3702b15cb3dSCy Schubert 	if (++n_server_responses == 3) {
3712b15cb3dSCy Schubert 		event_loopexit(NULL);
3722b15cb3dSCy Schubert 	}
3732b15cb3dSCy Schubert }
3742b15cb3dSCy Schubert 
3752b15cb3dSCy Schubert static void
dns_server(void)3762b15cb3dSCy Schubert dns_server(void)
3772b15cb3dSCy Schubert {
3782b15cb3dSCy Schubert 	evutil_socket_t sock=-1;
3792b15cb3dSCy Schubert 	struct sockaddr_in my_addr;
3802b15cb3dSCy Schubert 	struct sockaddr_storage ss;
3812b15cb3dSCy Schubert 	ev_socklen_t slen;
3822b15cb3dSCy Schubert 	struct evdns_server_port *port=NULL;
3832b15cb3dSCy Schubert 	struct in_addr resolve_addr;
3842b15cb3dSCy Schubert 	struct in6_addr resolve_addr6;
3852b15cb3dSCy Schubert 	struct evdns_base *base=NULL;
3862b15cb3dSCy Schubert 	struct evdns_request *req=NULL;
3872b15cb3dSCy Schubert 
3882b15cb3dSCy Schubert 	dns_ok = 1;
3892b15cb3dSCy Schubert 
3902b15cb3dSCy Schubert 	base = evdns_base_new(NULL, 0);
3912b15cb3dSCy Schubert 
3922b15cb3dSCy Schubert 	/* Now configure a nameserver port. */
3932b15cb3dSCy Schubert 	sock = socket(AF_INET, SOCK_DGRAM, 0);
3942b15cb3dSCy Schubert 	if (sock<0) {
3952b15cb3dSCy Schubert 		tt_abort_perror("socket");
3962b15cb3dSCy Schubert 	}
3972b15cb3dSCy Schubert 
3982b15cb3dSCy Schubert 	evutil_make_socket_nonblocking(sock);
3992b15cb3dSCy Schubert 
4002b15cb3dSCy Schubert 	memset(&my_addr, 0, sizeof(my_addr));
4012b15cb3dSCy Schubert 	my_addr.sin_family = AF_INET;
4022b15cb3dSCy Schubert 	my_addr.sin_port = 0; /* kernel picks */
4032b15cb3dSCy Schubert 	my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
4042b15cb3dSCy Schubert 	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
4052b15cb3dSCy Schubert 		tt_abort_perror("bind");
4062b15cb3dSCy Schubert 	}
4072b15cb3dSCy Schubert 	slen = sizeof(ss);
4082b15cb3dSCy Schubert 	if (getsockname(sock, (struct sockaddr*)&ss, &slen) < 0) {
4092b15cb3dSCy Schubert 		tt_abort_perror("getsockname");
4102b15cb3dSCy Schubert 	}
4112b15cb3dSCy Schubert 
4122b15cb3dSCy Schubert 	port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL);
4132b15cb3dSCy Schubert 
4142b15cb3dSCy Schubert 	/* Add ourself as the only nameserver, and make sure we really are
4152b15cb3dSCy Schubert 	 * the only nameserver. */
4162b15cb3dSCy Schubert 	evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0);
4172b15cb3dSCy Schubert 	tt_int_op(evdns_base_count_nameservers(base), ==, 1);
418a25439b6SCy Schubert 	{
419a25439b6SCy Schubert 		struct sockaddr_storage ss2;
420a25439b6SCy Schubert 		int slen2;
421a25439b6SCy Schubert 
422a25439b6SCy Schubert 		memset(&ss2, 0, sizeof(ss2));
423a25439b6SCy Schubert 
424a25439b6SCy Schubert 		slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, 3);
425a25439b6SCy Schubert 		tt_int_op(slen2, ==, slen);
426a25439b6SCy Schubert 		tt_int_op(ss2.ss_family, ==, 0);
427a25439b6SCy Schubert 		slen2 = evdns_base_get_nameserver_addr(base, 0, (struct sockaddr *)&ss2, sizeof(ss2));
428a25439b6SCy Schubert 		tt_int_op(slen2, ==, slen);
429a25439b6SCy Schubert 		tt_mem_op(&ss2, ==, &ss, slen);
430a25439b6SCy Schubert 
431a25439b6SCy Schubert 		slen2 = evdns_base_get_nameserver_addr(base, 1, (struct sockaddr *)&ss2, sizeof(ss2));
432a25439b6SCy Schubert 		tt_int_op(-1, ==, slen2);
433a25439b6SCy Schubert 	}
4342b15cb3dSCy Schubert 
4352b15cb3dSCy Schubert 	/* Send some queries. */
4362b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH,
4372b15cb3dSCy Schubert 					   dns_server_gethostbyname_cb, NULL);
4382b15cb3dSCy Schubert 	evdns_base_resolve_ipv6(base, "zz.example.com", DNS_QUERY_NO_SEARCH,
4392b15cb3dSCy Schubert 					   dns_server_gethostbyname_cb, NULL);
4402b15cb3dSCy Schubert 	resolve_addr.s_addr = htonl(0xc0a80b0bUL); /* 192.168.11.11 */
4412b15cb3dSCy Schubert 	evdns_base_resolve_reverse(base, &resolve_addr, 0,
4422b15cb3dSCy Schubert 	    dns_server_gethostbyname_cb, NULL);
4432b15cb3dSCy Schubert 	memcpy(resolve_addr6.s6_addr,
4442b15cb3dSCy Schubert 	    "\xff\xf0\x00\x00\x00\x00\xaa\xaa"
4452b15cb3dSCy Schubert 	    "\x11\x11\x00\x00\x00\x00\xef\xef", 16);
4462b15cb3dSCy Schubert 	evdns_base_resolve_reverse_ipv6(base, &resolve_addr6, 0,
4472b15cb3dSCy Schubert 	    dns_server_gethostbyname_cb, (void*)6);
4482b15cb3dSCy Schubert 
4492b15cb3dSCy Schubert 	req = evdns_base_resolve_ipv4(base,
4502b15cb3dSCy Schubert 	    "drop.example.com", DNS_QUERY_NO_SEARCH,
4512b15cb3dSCy Schubert 	    dns_server_gethostbyname_cb, (void*)(char*)90909);
4522b15cb3dSCy Schubert 
4532b15cb3dSCy Schubert 	evdns_cancel_request(base, req);
4542b15cb3dSCy Schubert 
4552b15cb3dSCy Schubert 	event_dispatch();
4562b15cb3dSCy Schubert 
4572b15cb3dSCy Schubert 	tt_assert(dns_got_cancel);
4582b15cb3dSCy Schubert 	test_ok = dns_ok;
4592b15cb3dSCy Schubert 
4602b15cb3dSCy Schubert end:
4612b15cb3dSCy Schubert 	if (port)
4622b15cb3dSCy Schubert 		evdns_close_server_port(port);
4632b15cb3dSCy Schubert 	if (sock >= 0)
4642b15cb3dSCy Schubert 		evutil_closesocket(sock);
4652b15cb3dSCy Schubert 	if (base)
4662b15cb3dSCy Schubert 		evdns_base_free(base, 0);
4672b15cb3dSCy Schubert }
4682b15cb3dSCy Schubert 
4692b15cb3dSCy Schubert static int n_replies_left;
4702b15cb3dSCy Schubert static struct event_base *exit_base;
471a25439b6SCy Schubert static struct evdns_server_port *exit_port;
4722b15cb3dSCy Schubert 
4732b15cb3dSCy Schubert struct generic_dns_callback_result {
4742b15cb3dSCy Schubert 	int result;
4752b15cb3dSCy Schubert 	char type;
4762b15cb3dSCy Schubert 	int count;
4772b15cb3dSCy Schubert 	int ttl;
4782b15cb3dSCy Schubert 	size_t addrs_len;
4792b15cb3dSCy Schubert 	void *addrs;
4802b15cb3dSCy Schubert 	char addrs_buf[256];
4812b15cb3dSCy Schubert };
4822b15cb3dSCy Schubert 
4832b15cb3dSCy Schubert static void
generic_dns_callback(int result,char type,int count,int ttl,void * addresses,void * arg)4842b15cb3dSCy Schubert generic_dns_callback(int result, char type, int count, int ttl, void *addresses,
4852b15cb3dSCy Schubert     void *arg)
4862b15cb3dSCy Schubert {
4872b15cb3dSCy Schubert 	size_t len;
4882b15cb3dSCy Schubert 	struct generic_dns_callback_result *res = arg;
4892b15cb3dSCy Schubert 	res->result = result;
4902b15cb3dSCy Schubert 	res->type = type;
4912b15cb3dSCy Schubert 	res->count = count;
4922b15cb3dSCy Schubert 	res->ttl = ttl;
4932b15cb3dSCy Schubert 
4942b15cb3dSCy Schubert 	if (type == DNS_IPv4_A)
4952b15cb3dSCy Schubert 		len = count * 4;
4962b15cb3dSCy Schubert 	else if (type == DNS_IPv6_AAAA)
4972b15cb3dSCy Schubert 		len = count * 16;
4982b15cb3dSCy Schubert 	else if (type == DNS_PTR)
4992b15cb3dSCy Schubert 		len = strlen(addresses)+1;
5002b15cb3dSCy Schubert 	else {
5012b15cb3dSCy Schubert 		res->addrs_len = len = 0;
5022b15cb3dSCy Schubert 		res->addrs = NULL;
5032b15cb3dSCy Schubert 	}
5042b15cb3dSCy Schubert 	if (len) {
5052b15cb3dSCy Schubert 		res->addrs_len = len;
5062b15cb3dSCy Schubert 		if (len > 256)
5072b15cb3dSCy Schubert 			len = 256;
5082b15cb3dSCy Schubert 		memcpy(res->addrs_buf, addresses, len);
5092b15cb3dSCy Schubert 		res->addrs = res->addrs_buf;
5102b15cb3dSCy Schubert 	}
5112b15cb3dSCy Schubert 
512a25439b6SCy Schubert 	--n_replies_left;
513a25439b6SCy Schubert 	if (n_replies_left == 0) {
514a25439b6SCy Schubert 		if (exit_port) {
515a25439b6SCy Schubert 			evdns_close_server_port(exit_port);
516a25439b6SCy Schubert 			exit_port = NULL;
517a25439b6SCy Schubert 		} else
5182b15cb3dSCy Schubert 			event_base_loopexit(exit_base, NULL);
5192b15cb3dSCy Schubert 	}
520a25439b6SCy Schubert }
5212b15cb3dSCy Schubert 
5222b15cb3dSCy Schubert static struct regress_dns_server_table search_table[] = {
523*a466cc55SCy Schubert 	{ "host.a.example.com", "err", "3", 0, 0 },
524*a466cc55SCy Schubert 	{ "host.b.example.com", "err", "3", 0, 0 },
525*a466cc55SCy Schubert 	{ "host.c.example.com", "A", "11.22.33.44", 0, 0 },
526*a466cc55SCy Schubert 	{ "host2.a.example.com", "err", "3", 0, 0 },
527*a466cc55SCy Schubert 	{ "host2.b.example.com", "A", "200.100.0.100", 0, 0 },
528*a466cc55SCy Schubert 	{ "host2.c.example.com", "err", "3", 0, 0 },
529*a466cc55SCy Schubert 	{ "hostn.a.example.com", "errsoa", "0", 0, 0 },
530*a466cc55SCy Schubert 	{ "hostn.b.example.com", "errsoa", "3", 0, 0 },
531*a466cc55SCy Schubert 	{ "hostn.c.example.com", "err", "0", 0, 0 },
5322b15cb3dSCy Schubert 
533*a466cc55SCy Schubert 	{ "host", "err", "3", 0, 0 },
534*a466cc55SCy Schubert 	{ "host2", "err", "3", 0, 0 },
535*a466cc55SCy Schubert 	{ "*", "err", "3", 0, 0 },
536*a466cc55SCy Schubert 	{ NULL, NULL, NULL, 0, 0 }
5372b15cb3dSCy Schubert };
5382b15cb3dSCy Schubert static void
dns_search_test_impl(void * arg,int lower)539*a466cc55SCy Schubert dns_search_test_impl(void *arg, int lower)
5402b15cb3dSCy Schubert {
541*a466cc55SCy Schubert 	struct regress_dns_server_table table[ARRAY_SIZE(search_table)];
5422b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
5432b15cb3dSCy Schubert 	struct event_base *base = data->base;
5442b15cb3dSCy Schubert 	struct evdns_base *dns = NULL;
5452b15cb3dSCy Schubert 	ev_uint16_t portnum = 0;
5462b15cb3dSCy Schubert 	char buf[64];
5472b15cb3dSCy Schubert 
5482b15cb3dSCy Schubert 	struct generic_dns_callback_result r[8];
549*a466cc55SCy Schubert 	size_t i;
5502b15cb3dSCy Schubert 
551*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(table); ++i) {
552*a466cc55SCy Schubert 		table[i] = search_table[i];
553*a466cc55SCy Schubert 		table[i].lower = lower;
554*a466cc55SCy Schubert 	}
555*a466cc55SCy Schubert 
556*a466cc55SCy Schubert 	tt_assert(regress_dnsserver(base, &portnum, table));
5572b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
5582b15cb3dSCy Schubert 
5592b15cb3dSCy Schubert 	dns = evdns_base_new(base, 0);
5602b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
5612b15cb3dSCy Schubert 
5622b15cb3dSCy Schubert 	evdns_base_search_add(dns, "a.example.com");
5632b15cb3dSCy Schubert 	evdns_base_search_add(dns, "b.example.com");
5642b15cb3dSCy Schubert 	evdns_base_search_add(dns, "c.example.com");
5652b15cb3dSCy Schubert 
566*a466cc55SCy Schubert 	n_replies_left = ARRAY_SIZE(r);
5672b15cb3dSCy Schubert 	exit_base = base;
5682b15cb3dSCy Schubert 
5692b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host", 0, generic_dns_callback, &r[0]);
5702b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host2", 0, generic_dns_callback, &r[1]);
5712b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host", DNS_NO_SEARCH, generic_dns_callback, &r[2]);
5722b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host2", DNS_NO_SEARCH, generic_dns_callback, &r[3]);
5732b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host3", 0, generic_dns_callback, &r[4]);
5742b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "hostn.a.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[5]);
5752b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "hostn.b.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[6]);
5762b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "hostn.c.example.com", DNS_NO_SEARCH, generic_dns_callback, &r[7]);
5772b15cb3dSCy Schubert 
5782b15cb3dSCy Schubert 	event_base_dispatch(base);
5792b15cb3dSCy Schubert 
5802b15cb3dSCy Schubert 	tt_int_op(r[0].type, ==, DNS_IPv4_A);
5812b15cb3dSCy Schubert 	tt_int_op(r[0].count, ==, 1);
5822b15cb3dSCy Schubert 	tt_int_op(((ev_uint32_t*)r[0].addrs)[0], ==, htonl(0x0b16212c));
5832b15cb3dSCy Schubert 	tt_int_op(r[1].type, ==, DNS_IPv4_A);
5842b15cb3dSCy Schubert 	tt_int_op(r[1].count, ==, 1);
5852b15cb3dSCy Schubert 	tt_int_op(((ev_uint32_t*)r[1].addrs)[0], ==, htonl(0xc8640064));
5862b15cb3dSCy Schubert 	tt_int_op(r[2].result, ==, DNS_ERR_NOTEXIST);
5872b15cb3dSCy Schubert 	tt_int_op(r[3].result, ==, DNS_ERR_NOTEXIST);
5882b15cb3dSCy Schubert 	tt_int_op(r[4].result, ==, DNS_ERR_NOTEXIST);
5892b15cb3dSCy Schubert 	tt_int_op(r[5].result, ==, DNS_ERR_NODATA);
5902b15cb3dSCy Schubert 	tt_int_op(r[5].ttl, ==, 42);
5912b15cb3dSCy Schubert 	tt_int_op(r[6].result, ==, DNS_ERR_NOTEXIST);
5922b15cb3dSCy Schubert 	tt_int_op(r[6].ttl, ==, 42);
5932b15cb3dSCy Schubert 	tt_int_op(r[7].result, ==, DNS_ERR_NODATA);
5942b15cb3dSCy Schubert 	tt_int_op(r[7].ttl, ==, 0);
5952b15cb3dSCy Schubert 
5962b15cb3dSCy Schubert end:
5972b15cb3dSCy Schubert 	if (dns)
5982b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
5992b15cb3dSCy Schubert 
6002b15cb3dSCy Schubert 	regress_clean_dnsserver();
6012b15cb3dSCy Schubert }
602*a466cc55SCy Schubert static void
dns_search_empty_test(void * arg)603*a466cc55SCy Schubert dns_search_empty_test(void *arg)
604*a466cc55SCy Schubert {
605*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
606*a466cc55SCy Schubert 	struct event_base *base = data->base;
607*a466cc55SCy Schubert 	struct evdns_base *dns = NULL;
608*a466cc55SCy Schubert 
609*a466cc55SCy Schubert 	dns = evdns_base_new(base, 0);
610*a466cc55SCy Schubert 
611*a466cc55SCy Schubert 	evdns_base_search_add(dns, "whatever.example.com");
612*a466cc55SCy Schubert 
613*a466cc55SCy Schubert 	n_replies_left = 1;
614*a466cc55SCy Schubert 	exit_base = base;
615*a466cc55SCy Schubert 
616*a466cc55SCy Schubert 	tt_ptr_op(evdns_base_resolve_ipv4(dns, "", 0, generic_dns_callback, NULL), ==, NULL);
617*a466cc55SCy Schubert 
618*a466cc55SCy Schubert end:
619*a466cc55SCy Schubert 	if (dns)
620*a466cc55SCy Schubert 		evdns_base_free(dns, 0);
621*a466cc55SCy Schubert }
dns_search_test(void * arg)622*a466cc55SCy Schubert static void dns_search_test(void *arg) { dns_search_test_impl(arg, 0); }
dns_search_lower_test(void * arg)623*a466cc55SCy Schubert static void dns_search_lower_test(void *arg) { dns_search_test_impl(arg, 1); }
6242b15cb3dSCy Schubert 
6252b15cb3dSCy Schubert static int request_count = 0;
6262b15cb3dSCy Schubert static struct evdns_request *current_req = NULL;
6272b15cb3dSCy Schubert 
6282b15cb3dSCy Schubert static void
search_cancel_server_cb(struct evdns_server_request * req,void * data)6292b15cb3dSCy Schubert search_cancel_server_cb(struct evdns_server_request *req, void *data)
6302b15cb3dSCy Schubert {
6312b15cb3dSCy Schubert 	const char *question;
6322b15cb3dSCy Schubert 
6332b15cb3dSCy Schubert 	if (req->nquestions != 1)
6342b15cb3dSCy Schubert 		TT_DIE(("Only handling one question at a time; got %d",
6352b15cb3dSCy Schubert 			req->nquestions));
6362b15cb3dSCy Schubert 
6372b15cb3dSCy Schubert 	question = req->questions[0]->name;
6382b15cb3dSCy Schubert 
6392b15cb3dSCy Schubert 	TT_BLATHER(("got question, %s", question));
6402b15cb3dSCy Schubert 
6412b15cb3dSCy Schubert 	tt_assert(request_count > 0);
6422b15cb3dSCy Schubert 	tt_assert(!evdns_server_request_respond(req, 3));
6432b15cb3dSCy Schubert 
6442b15cb3dSCy Schubert 	if (!--request_count)
6452b15cb3dSCy Schubert 		evdns_cancel_request(NULL, current_req);
6462b15cb3dSCy Schubert 
6472b15cb3dSCy Schubert end:
6482b15cb3dSCy Schubert 	;
6492b15cb3dSCy Schubert }
6502b15cb3dSCy Schubert 
6512b15cb3dSCy Schubert static void
dns_search_cancel_test(void * arg)6522b15cb3dSCy Schubert dns_search_cancel_test(void *arg)
6532b15cb3dSCy Schubert {
6542b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
6552b15cb3dSCy Schubert 	struct event_base *base = data->base;
6562b15cb3dSCy Schubert 	struct evdns_base *dns = NULL;
6572b15cb3dSCy Schubert 	struct evdns_server_port *port = NULL;
6582b15cb3dSCy Schubert 	ev_uint16_t portnum = 0;
6592b15cb3dSCy Schubert 	struct generic_dns_callback_result r1;
6602b15cb3dSCy Schubert 	char buf[64];
6612b15cb3dSCy Schubert 
6622b15cb3dSCy Schubert 	port = regress_get_dnsserver(base, &portnum, NULL,
6632b15cb3dSCy Schubert 	    search_cancel_server_cb, NULL);
6642b15cb3dSCy Schubert 	tt_assert(port);
6652b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
6662b15cb3dSCy Schubert 
6672b15cb3dSCy Schubert 	dns = evdns_base_new(base, 0);
6682b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
6692b15cb3dSCy Schubert 
6702b15cb3dSCy Schubert 	evdns_base_search_add(dns, "a.example.com");
6712b15cb3dSCy Schubert 	evdns_base_search_add(dns, "b.example.com");
6722b15cb3dSCy Schubert 	evdns_base_search_add(dns, "c.example.com");
6732b15cb3dSCy Schubert 	evdns_base_search_add(dns, "d.example.com");
6742b15cb3dSCy Schubert 
6752b15cb3dSCy Schubert 	exit_base = base;
6762b15cb3dSCy Schubert 	request_count = 3;
6772b15cb3dSCy Schubert 	n_replies_left = 1;
6782b15cb3dSCy Schubert 
6792b15cb3dSCy Schubert 	current_req = evdns_base_resolve_ipv4(dns, "host", 0,
6802b15cb3dSCy Schubert 					generic_dns_callback, &r1);
6812b15cb3dSCy Schubert 	event_base_dispatch(base);
6822b15cb3dSCy Schubert 
6832b15cb3dSCy Schubert 	tt_int_op(r1.result, ==, DNS_ERR_CANCEL);
6842b15cb3dSCy Schubert 
6852b15cb3dSCy Schubert end:
6862b15cb3dSCy Schubert 	if (port)
6872b15cb3dSCy Schubert 		evdns_close_server_port(port);
6882b15cb3dSCy Schubert 	if (dns)
6892b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
6902b15cb3dSCy Schubert }
6912b15cb3dSCy Schubert 
6922b15cb3dSCy Schubert static void
fail_server_cb(struct evdns_server_request * req,void * data)6932b15cb3dSCy Schubert fail_server_cb(struct evdns_server_request *req, void *data)
6942b15cb3dSCy Schubert {
6952b15cb3dSCy Schubert 	const char *question;
6962b15cb3dSCy Schubert 	int *count = data;
6972b15cb3dSCy Schubert 	struct in_addr in;
6982b15cb3dSCy Schubert 
6992b15cb3dSCy Schubert 	/* Drop the first N requests that we get. */
7002b15cb3dSCy Schubert 	if (*count > 0) {
7012b15cb3dSCy Schubert 		--*count;
7022b15cb3dSCy Schubert 		tt_want(! evdns_server_request_drop(req));
7032b15cb3dSCy Schubert 		return;
7042b15cb3dSCy Schubert 	}
7052b15cb3dSCy Schubert 
7062b15cb3dSCy Schubert 	if (req->nquestions != 1)
7072b15cb3dSCy Schubert 		TT_DIE(("Only handling one question at a time; got %d",
7082b15cb3dSCy Schubert 			req->nquestions));
7092b15cb3dSCy Schubert 
7102b15cb3dSCy Schubert 	question = req->questions[0]->name;
7112b15cb3dSCy Schubert 
7122b15cb3dSCy Schubert 	if (!evutil_ascii_strcasecmp(question, "google.com")) {
7132b15cb3dSCy Schubert 		/* Detect a probe, and get out of the loop. */
7142b15cb3dSCy Schubert 		event_base_loopexit(exit_base, NULL);
7152b15cb3dSCy Schubert 	}
7162b15cb3dSCy Schubert 
7172b15cb3dSCy Schubert 	tt_assert(evutil_inet_pton(AF_INET, "16.32.64.128", &in));
7182b15cb3dSCy Schubert 	evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
7192b15cb3dSCy Schubert 	    100);
7202b15cb3dSCy Schubert 	tt_assert(! evdns_server_request_respond(req, 0))
7212b15cb3dSCy Schubert 	return;
7222b15cb3dSCy Schubert end:
7232b15cb3dSCy Schubert 	tt_want(! evdns_server_request_drop(req));
7242b15cb3dSCy Schubert }
7252b15cb3dSCy Schubert 
7262b15cb3dSCy Schubert static void
dns_retry_test_impl(void * arg,int flags)727a25439b6SCy Schubert dns_retry_test_impl(void *arg, int flags)
7282b15cb3dSCy Schubert {
7292b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
7302b15cb3dSCy Schubert 	struct event_base *base = data->base;
7312b15cb3dSCy Schubert 	struct evdns_server_port *port = NULL;
7322b15cb3dSCy Schubert 	struct evdns_base *dns = NULL;
7332b15cb3dSCy Schubert 	int drop_count = 2;
7342b15cb3dSCy Schubert 	ev_uint16_t portnum = 0;
7352b15cb3dSCy Schubert 	char buf[64];
7362b15cb3dSCy Schubert 
7372b15cb3dSCy Schubert 	struct generic_dns_callback_result r1;
7382b15cb3dSCy Schubert 
7392b15cb3dSCy Schubert 	port = regress_get_dnsserver(base, &portnum, NULL,
7402b15cb3dSCy Schubert 	    fail_server_cb, &drop_count);
7412b15cb3dSCy Schubert 	tt_assert(port);
7422b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
7432b15cb3dSCy Schubert 
744a25439b6SCy Schubert 	dns = evdns_base_new(base, flags);
7452b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
7462b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "timeout", "0.2"));
7472b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10"));
7482b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.1"));
7492b15cb3dSCy Schubert 
7502b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host.example.com", 0,
7512b15cb3dSCy Schubert 	    generic_dns_callback, &r1);
7522b15cb3dSCy Schubert 
7532b15cb3dSCy Schubert 	n_replies_left = 1;
7542b15cb3dSCy Schubert 	exit_base = base;
7552b15cb3dSCy Schubert 
7562b15cb3dSCy Schubert 	event_base_dispatch(base);
7572b15cb3dSCy Schubert 
7582b15cb3dSCy Schubert 	tt_int_op(drop_count, ==, 0);
7592b15cb3dSCy Schubert 
7602b15cb3dSCy Schubert 	tt_int_op(r1.type, ==, DNS_IPv4_A);
7612b15cb3dSCy Schubert 	tt_int_op(r1.count, ==, 1);
7622b15cb3dSCy Schubert 	tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080));
7632b15cb3dSCy Schubert 
7642b15cb3dSCy Schubert 	/* Now try again, but this time have the server get treated as
7652b15cb3dSCy Schubert 	 * failed, so we can send it a test probe. */
7662b15cb3dSCy Schubert 	drop_count = 4;
7672b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
7682b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "attempts:", "3"));
7692b15cb3dSCy Schubert 	memset(&r1, 0, sizeof(r1));
7702b15cb3dSCy Schubert 
7712b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host.example.com", 0,
7722b15cb3dSCy Schubert 	    generic_dns_callback, &r1);
7732b15cb3dSCy Schubert 
7742b15cb3dSCy Schubert 	n_replies_left = 2;
7752b15cb3dSCy Schubert 
7762b15cb3dSCy Schubert 	/* This will run until it answers the "google.com" probe request. */
7772b15cb3dSCy Schubert 	event_base_dispatch(base);
7782b15cb3dSCy Schubert 
7792b15cb3dSCy Schubert 	/* We'll treat the server as failed here. */
7802b15cb3dSCy Schubert 	tt_int_op(r1.result, ==, DNS_ERR_TIMEOUT);
7812b15cb3dSCy Schubert 
7822b15cb3dSCy Schubert 	/* It should work this time. */
7832b15cb3dSCy Schubert 	tt_int_op(drop_count, ==, 0);
7842b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "host.example.com", 0,
7852b15cb3dSCy Schubert 	    generic_dns_callback, &r1);
7862b15cb3dSCy Schubert 
7872b15cb3dSCy Schubert 	event_base_dispatch(base);
7882b15cb3dSCy Schubert 	tt_int_op(r1.result, ==, DNS_ERR_NONE);
7892b15cb3dSCy Schubert 	tt_int_op(r1.type, ==, DNS_IPv4_A);
7902b15cb3dSCy Schubert 	tt_int_op(r1.count, ==, 1);
7912b15cb3dSCy Schubert 	tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0x10204080));
7922b15cb3dSCy Schubert 
7932b15cb3dSCy Schubert end:
7942b15cb3dSCy Schubert 	if (dns)
7952b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
7962b15cb3dSCy Schubert 	if (port)
7972b15cb3dSCy Schubert 		evdns_close_server_port(port);
7982b15cb3dSCy Schubert }
799a25439b6SCy Schubert static void
dns_retry_test(void * arg)800a25439b6SCy Schubert dns_retry_test(void *arg)
801a25439b6SCy Schubert {
802a25439b6SCy Schubert 	dns_retry_test_impl(arg, 0);
803a25439b6SCy Schubert }
804a25439b6SCy Schubert static void
dns_retry_disable_when_inactive_test(void * arg)805a25439b6SCy Schubert dns_retry_disable_when_inactive_test(void *arg)
806a25439b6SCy Schubert {
807a25439b6SCy Schubert 	dns_retry_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
808a25439b6SCy Schubert }
8092b15cb3dSCy Schubert 
8102b15cb3dSCy Schubert static struct regress_dns_server_table internal_error_table[] = {
8112b15cb3dSCy Schubert 	/* Error 4 (NOTIMPL) makes us reissue the request to another server
8122b15cb3dSCy Schubert 	   if we can.
8132b15cb3dSCy Schubert 
8142b15cb3dSCy Schubert 	   XXXX we should reissue under a much wider set of circumstances!
8152b15cb3dSCy Schubert 	 */
816*a466cc55SCy Schubert 	{ "foof.example.com", "err", "4", 0, 0 },
817*a466cc55SCy Schubert 	{ NULL, NULL, NULL, 0, 0 }
8182b15cb3dSCy Schubert };
8192b15cb3dSCy Schubert 
8202b15cb3dSCy Schubert static struct regress_dns_server_table reissue_table[] = {
821*a466cc55SCy Schubert 	{ "foof.example.com", "A", "240.15.240.15", 0, 0 },
822*a466cc55SCy Schubert 	{ NULL, NULL, NULL, 0, 0 }
8232b15cb3dSCy Schubert };
8242b15cb3dSCy Schubert 
8252b15cb3dSCy Schubert static void
dns_reissue_test_impl(void * arg,int flags)826a25439b6SCy Schubert dns_reissue_test_impl(void *arg, int flags)
8272b15cb3dSCy Schubert {
8282b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
8292b15cb3dSCy Schubert 	struct event_base *base = data->base;
8302b15cb3dSCy Schubert 	struct evdns_server_port *port1 = NULL, *port2 = NULL;
8312b15cb3dSCy Schubert 	struct evdns_base *dns = NULL;
8322b15cb3dSCy Schubert 	struct generic_dns_callback_result r1;
8332b15cb3dSCy Schubert 	ev_uint16_t portnum1 = 0, portnum2=0;
8342b15cb3dSCy Schubert 	char buf1[64], buf2[64];
8352b15cb3dSCy Schubert 
8362b15cb3dSCy Schubert 	port1 = regress_get_dnsserver(base, &portnum1, NULL,
8372b15cb3dSCy Schubert 	    regress_dns_server_cb, internal_error_table);
8382b15cb3dSCy Schubert 	tt_assert(port1);
8392b15cb3dSCy Schubert 	port2 = regress_get_dnsserver(base, &portnum2, NULL,
8402b15cb3dSCy Schubert 	    regress_dns_server_cb, reissue_table);
8412b15cb3dSCy Schubert 	tt_assert(port2);
8422b15cb3dSCy Schubert 	evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1);
8432b15cb3dSCy Schubert 	evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2);
8442b15cb3dSCy Schubert 
845a25439b6SCy Schubert 	dns = evdns_base_new(base, flags);
8462b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf1));
8472b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3"));
8482b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
8492b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "attempts:", "5"));
8502b15cb3dSCy Schubert 
8512b15cb3dSCy Schubert 	memset(&r1, 0, sizeof(r1));
8522b15cb3dSCy Schubert 	evdns_base_resolve_ipv4(dns, "foof.example.com", 0,
8532b15cb3dSCy Schubert 	    generic_dns_callback, &r1);
8542b15cb3dSCy Schubert 
8552b15cb3dSCy Schubert 	/* Add this after, so that we are sure to get a reissue. */
8562b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf2));
8572b15cb3dSCy Schubert 
8582b15cb3dSCy Schubert 	n_replies_left = 1;
8592b15cb3dSCy Schubert 	exit_base = base;
8602b15cb3dSCy Schubert 
8612b15cb3dSCy Schubert 	event_base_dispatch(base);
8622b15cb3dSCy Schubert 	tt_int_op(r1.result, ==, DNS_ERR_NONE);
8632b15cb3dSCy Schubert 	tt_int_op(r1.type, ==, DNS_IPv4_A);
8642b15cb3dSCy Schubert 	tt_int_op(r1.count, ==, 1);
8652b15cb3dSCy Schubert 	tt_int_op(((ev_uint32_t*)r1.addrs)[0], ==, htonl(0xf00ff00f));
8662b15cb3dSCy Schubert 
8672b15cb3dSCy Schubert 	/* Make sure we dropped at least once. */
8682b15cb3dSCy Schubert 	tt_int_op(internal_error_table[0].seen, >, 0);
8692b15cb3dSCy Schubert 
8702b15cb3dSCy Schubert end:
8712b15cb3dSCy Schubert 	if (dns)
8722b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
8732b15cb3dSCy Schubert 	if (port1)
8742b15cb3dSCy Schubert 		evdns_close_server_port(port1);
8752b15cb3dSCy Schubert 	if (port2)
8762b15cb3dSCy Schubert 		evdns_close_server_port(port2);
8772b15cb3dSCy Schubert }
878a25439b6SCy Schubert static void
dns_reissue_test(void * arg)879a25439b6SCy Schubert dns_reissue_test(void *arg)
880a25439b6SCy Schubert {
881a25439b6SCy Schubert 	dns_reissue_test_impl(arg, 0);
882a25439b6SCy Schubert }
883a25439b6SCy Schubert static void
dns_reissue_disable_when_inactive_test(void * arg)884a25439b6SCy Schubert dns_reissue_disable_when_inactive_test(void *arg)
885a25439b6SCy Schubert {
886a25439b6SCy Schubert 	dns_reissue_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
887a25439b6SCy Schubert }
8882b15cb3dSCy Schubert 
8892b15cb3dSCy Schubert #if 0
8902b15cb3dSCy Schubert static void
8912b15cb3dSCy Schubert dumb_bytes_fn(char *p, size_t n)
8922b15cb3dSCy Schubert {
8932b15cb3dSCy Schubert 	unsigned i;
8942b15cb3dSCy Schubert 	/* This gets us 6 bits of entropy per transaction ID, which means we
8952b15cb3dSCy Schubert 	 * will have probably have collisions and need to pick again. */
8962b15cb3dSCy Schubert 	for (i=0;i<n;++i)
8972b15cb3dSCy Schubert 		p[i] = (char)(rand() & 7);
8982b15cb3dSCy Schubert }
8992b15cb3dSCy Schubert #endif
9002b15cb3dSCy Schubert 
9012b15cb3dSCy Schubert static void
dns_inflight_test_impl(void * arg,int flags)902a25439b6SCy Schubert dns_inflight_test_impl(void *arg, int flags)
9032b15cb3dSCy Schubert {
9042b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
9052b15cb3dSCy Schubert 	struct event_base *base = data->base;
9062b15cb3dSCy Schubert 	struct evdns_base *dns = NULL;
907a25439b6SCy Schubert 	struct evdns_server_port *dns_port = NULL;
9082b15cb3dSCy Schubert 	ev_uint16_t portnum = 0;
9092b15cb3dSCy Schubert 	char buf[64];
910a25439b6SCy Schubert 	int disable_when_inactive = flags & EVDNS_BASE_DISABLE_WHEN_INACTIVE;
9112b15cb3dSCy Schubert 
9122b15cb3dSCy Schubert 	struct generic_dns_callback_result r[20];
9132b15cb3dSCy Schubert 	int i;
9142b15cb3dSCy Schubert 
915a25439b6SCy Schubert 	dns_port = regress_get_dnsserver(base, &portnum, NULL,
916a25439b6SCy Schubert 		regress_dns_server_cb, reissue_table);
917a25439b6SCy Schubert 	tt_assert(dns_port);
918a25439b6SCy Schubert 	if (disable_when_inactive) {
919a25439b6SCy Schubert 		exit_port = dns_port;
920a25439b6SCy Schubert 	}
921a25439b6SCy Schubert 
9222b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
9232b15cb3dSCy Schubert 
924a25439b6SCy Schubert 	dns = evdns_base_new(base, flags);
9252b15cb3dSCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
9262b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3"));
9272b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0"));
9282b15cb3dSCy Schubert 
9292b15cb3dSCy Schubert 	for (i=0;i<20;++i)
9302b15cb3dSCy Schubert 		evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r[i]);
9312b15cb3dSCy Schubert 
9322b15cb3dSCy Schubert 	n_replies_left = 20;
9332b15cb3dSCy Schubert 	exit_base = base;
9342b15cb3dSCy Schubert 
9352b15cb3dSCy Schubert 	event_base_dispatch(base);
9362b15cb3dSCy Schubert 
9372b15cb3dSCy Schubert 	for (i=0;i<20;++i) {
9382b15cb3dSCy Schubert 		tt_int_op(r[i].type, ==, DNS_IPv4_A);
9392b15cb3dSCy Schubert 		tt_int_op(r[i].count, ==, 1);
9402b15cb3dSCy Schubert 		tt_int_op(((ev_uint32_t*)r[i].addrs)[0], ==, htonl(0xf00ff00f));
9412b15cb3dSCy Schubert 	}
9422b15cb3dSCy Schubert 
9432b15cb3dSCy Schubert end:
9442b15cb3dSCy Schubert 	if (dns)
9452b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
946a25439b6SCy Schubert 	if (exit_port) {
947a25439b6SCy Schubert 		evdns_close_server_port(exit_port);
948a25439b6SCy Schubert 		exit_port = NULL;
949a25439b6SCy Schubert 	} else if (! disable_when_inactive) {
950a25439b6SCy Schubert 		evdns_close_server_port(dns_port);
951a25439b6SCy Schubert 	}
952a25439b6SCy Schubert }
953a25439b6SCy Schubert 
954a25439b6SCy Schubert static void
dns_inflight_test(void * arg)955a25439b6SCy Schubert dns_inflight_test(void *arg)
956a25439b6SCy Schubert {
957a25439b6SCy Schubert 	dns_inflight_test_impl(arg, 0);
958a25439b6SCy Schubert }
959a25439b6SCy Schubert 
960a25439b6SCy Schubert static void
dns_disable_when_inactive_test(void * arg)961a25439b6SCy Schubert dns_disable_when_inactive_test(void *arg)
962a25439b6SCy Schubert {
963a25439b6SCy Schubert 	dns_inflight_test_impl(arg, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
964a25439b6SCy Schubert }
965a25439b6SCy Schubert 
966a25439b6SCy Schubert static void
dns_disable_when_inactive_no_ns_test(void * arg)967a25439b6SCy Schubert dns_disable_when_inactive_no_ns_test(void *arg)
968a25439b6SCy Schubert {
969a25439b6SCy Schubert 	struct basic_test_data *data = arg;
970a25439b6SCy Schubert 	struct event_base *base = data->base, *inactive_base;
971a25439b6SCy Schubert 	struct evdns_base *dns = NULL;
972a25439b6SCy Schubert 	ev_uint16_t portnum = 0;
973a25439b6SCy Schubert 	char buf[64];
974a25439b6SCy Schubert 	struct generic_dns_callback_result r;
975a25439b6SCy Schubert 
976a25439b6SCy Schubert 	inactive_base = event_base_new();
977a25439b6SCy Schubert 	tt_assert(inactive_base);
978a25439b6SCy Schubert 
979a25439b6SCy Schubert 	/** Create dns server with inactive base, to avoid replying to clients */
980a25439b6SCy Schubert 	tt_assert(regress_dnsserver(inactive_base, &portnum, search_table));
981a25439b6SCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
982a25439b6SCy Schubert 
983a25439b6SCy Schubert 	dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
984a25439b6SCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
985a25439b6SCy Schubert 	tt_assert(! evdns_base_set_option(dns, "timeout:", "0.1"));
986a25439b6SCy Schubert 
987a25439b6SCy Schubert 	evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r);
988a25439b6SCy Schubert 	n_replies_left = 1;
989a25439b6SCy Schubert 	exit_base = base;
990a25439b6SCy Schubert 
991a25439b6SCy Schubert 	event_base_dispatch(base);
992a25439b6SCy Schubert 
993a25439b6SCy Schubert 	tt_int_op(n_replies_left, ==, 0);
994a25439b6SCy Schubert 
995a25439b6SCy Schubert 	tt_int_op(r.result, ==, DNS_ERR_TIMEOUT);
996a25439b6SCy Schubert 	tt_int_op(r.count, ==, 0);
997a25439b6SCy Schubert 	tt_ptr_op(r.addrs, ==, NULL);
998a25439b6SCy Schubert 
999a25439b6SCy Schubert end:
1000a25439b6SCy Schubert 	if (dns)
1001a25439b6SCy Schubert 		evdns_base_free(dns, 0);
10022b15cb3dSCy Schubert 	regress_clean_dnsserver();
1003a25439b6SCy Schubert 	if (inactive_base)
1004a25439b6SCy Schubert 		event_base_free(inactive_base);
10052b15cb3dSCy Schubert }
10062b15cb3dSCy Schubert 
1007*a466cc55SCy Schubert static void
dns_initialize_nameservers_test(void * arg)1008*a466cc55SCy Schubert dns_initialize_nameservers_test(void *arg)
1009*a466cc55SCy Schubert {
1010*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
1011*a466cc55SCy Schubert 	struct event_base *base = data->base;
1012*a466cc55SCy Schubert 	struct evdns_base *dns = NULL;
1013*a466cc55SCy Schubert 
1014*a466cc55SCy Schubert 	dns = evdns_base_new(base, 0);
1015*a466cc55SCy Schubert 	tt_assert(dns);
1016*a466cc55SCy Schubert 	tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, -1);
1017*a466cc55SCy Schubert 	evdns_base_free(dns, 0);
1018*a466cc55SCy Schubert 
1019*a466cc55SCy Schubert 	dns = evdns_base_new(base, EVDNS_BASE_INITIALIZE_NAMESERVERS);
1020*a466cc55SCy Schubert 	tt_assert(dns);
1021*a466cc55SCy Schubert 	tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, sizeof(struct sockaddr));
1022*a466cc55SCy Schubert 
1023*a466cc55SCy Schubert end:
1024*a466cc55SCy Schubert 	if (dns)
1025*a466cc55SCy Schubert 		evdns_base_free(dns, 0);
1026*a466cc55SCy Schubert }
1027*a466cc55SCy Schubert #ifndef _WIN32
1028*a466cc55SCy Schubert #define RESOLV_FILE "empty-resolv.conf"
1029*a466cc55SCy Schubert static void
dns_nameservers_no_default_test(void * arg)1030*a466cc55SCy Schubert dns_nameservers_no_default_test(void *arg)
1031*a466cc55SCy Schubert {
1032*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
1033*a466cc55SCy Schubert 	struct event_base *base = data->base;
1034*a466cc55SCy Schubert 	struct evdns_base *dns = NULL;
1035*a466cc55SCy Schubert 	int ok = access(RESOLV_FILE, R_OK);
1036*a466cc55SCy Schubert 
1037*a466cc55SCy Schubert 	tt_assert(ok);
1038*a466cc55SCy Schubert 
1039*a466cc55SCy Schubert 	dns = evdns_base_new(base, 0);
1040*a466cc55SCy Schubert 	tt_assert(dns);
1041*a466cc55SCy Schubert 	tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, -1);
1042*a466cc55SCy Schubert 
1043*a466cc55SCy Schubert 	/* We cannot test
1044*a466cc55SCy Schubert 	 * EVDNS_BASE_INITIALIZE_NAMESERVERS|EVDNS_BASE_NAMESERVERS_NO_DEFAULT
1045*a466cc55SCy Schubert 	 * because we cannot mock "/etc/resolv.conf" (yet). */
1046*a466cc55SCy Schubert 
1047*a466cc55SCy Schubert 	evdns_base_resolv_conf_parse(dns,
1048*a466cc55SCy Schubert 		DNS_OPTIONS_ALL|DNS_OPTION_NAMESERVERS_NO_DEFAULT, RESOLV_FILE);
1049*a466cc55SCy Schubert 	tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, -1);
1050*a466cc55SCy Schubert 
1051*a466cc55SCy Schubert 	evdns_base_resolv_conf_parse(dns, DNS_OPTIONS_ALL, RESOLV_FILE);
1052*a466cc55SCy Schubert 	tt_int_op(evdns_base_get_nameserver_addr(dns, 0, NULL, 0), ==, sizeof(struct sockaddr));
1053*a466cc55SCy Schubert 
1054*a466cc55SCy Schubert end:
1055*a466cc55SCy Schubert 	if (dns)
1056*a466cc55SCy Schubert 		evdns_base_free(dns, 0);
1057*a466cc55SCy Schubert }
1058*a466cc55SCy Schubert #endif
1059*a466cc55SCy Schubert 
10602b15cb3dSCy Schubert /* === Test for bufferevent_socket_connect_hostname */
10612b15cb3dSCy Schubert 
10622b15cb3dSCy Schubert static int total_connected_or_failed = 0;
10632b15cb3dSCy Schubert static int total_n_accepted = 0;
10642b15cb3dSCy Schubert static struct event_base *be_connect_hostname_base = NULL;
10652b15cb3dSCy Schubert 
10662b15cb3dSCy Schubert /* Implements a DNS server for the connect_hostname test and the
10672b15cb3dSCy Schubert  * getaddrinfo_async test */
10682b15cb3dSCy Schubert static void
be_getaddrinfo_server_cb(struct evdns_server_request * req,void * data)10692b15cb3dSCy Schubert be_getaddrinfo_server_cb(struct evdns_server_request *req, void *data)
10702b15cb3dSCy Schubert {
10712b15cb3dSCy Schubert 	int i;
10722b15cb3dSCy Schubert 	int *n_got_p=data;
10732b15cb3dSCy Schubert 	int added_any=0;
10742b15cb3dSCy Schubert 	++*n_got_p;
10752b15cb3dSCy Schubert 
10762b15cb3dSCy Schubert 	for (i = 0; i < req->nquestions; ++i) {
10772b15cb3dSCy Schubert 		const int qtype = req->questions[i]->type;
10782b15cb3dSCy Schubert 		const int qclass = req->questions[i]->dns_question_class;
10792b15cb3dSCy Schubert 		const char *qname = req->questions[i]->name;
10802b15cb3dSCy Schubert 		struct in_addr ans;
10812b15cb3dSCy Schubert 		struct in6_addr ans6;
10822b15cb3dSCy Schubert 		memset(&ans6, 0, sizeof(ans6));
10832b15cb3dSCy Schubert 
10842b15cb3dSCy Schubert 		TT_BLATHER(("Got question about %s, type=%d", qname, qtype));
10852b15cb3dSCy Schubert 
10862b15cb3dSCy Schubert 		if (qtype == EVDNS_TYPE_A &&
10872b15cb3dSCy Schubert 		    qclass == EVDNS_CLASS_INET &&
10882b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "nobodaddy.example.com")) {
10892b15cb3dSCy Schubert 			ans.s_addr = htonl(0x7f000001);
10902b15cb3dSCy Schubert 			evdns_server_request_add_a_reply(req, qname,
10912b15cb3dSCy Schubert 			    1, &ans.s_addr, 2000);
10922b15cb3dSCy Schubert 			added_any = 1;
10932b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
10942b15cb3dSCy Schubert 			"nosuchplace.example.com")) {
10952b15cb3dSCy Schubert 			/* ok, just say notfound. */
10962b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
10972b15cb3dSCy Schubert 			"both.example.com")) {
10982b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_A) {
10992b15cb3dSCy Schubert 				ans.s_addr = htonl(0x50502020);
11002b15cb3dSCy Schubert 				evdns_server_request_add_a_reply(req, qname,
11012b15cb3dSCy Schubert 				    1, &ans.s_addr, 2000);
11022b15cb3dSCy Schubert 				added_any = 1;
11032b15cb3dSCy Schubert 			} else if (qtype == EVDNS_TYPE_AAAA) {
11042b15cb3dSCy Schubert 				ans6.s6_addr[0] = 0x80;
11052b15cb3dSCy Schubert 				ans6.s6_addr[1] = 0xff;
11062b15cb3dSCy Schubert 				ans6.s6_addr[14] = 0xbb;
11072b15cb3dSCy Schubert 				ans6.s6_addr[15] = 0xbb;
11082b15cb3dSCy Schubert 				evdns_server_request_add_aaaa_reply(req, qname,
11092b15cb3dSCy Schubert 				    1, &ans6.s6_addr, 2000);
11102b15cb3dSCy Schubert 				added_any = 1;
11112b15cb3dSCy Schubert 			}
11122b15cb3dSCy Schubert 			evdns_server_request_add_cname_reply(req, qname,
11132b15cb3dSCy Schubert 			    "both-canonical.example.com", 1000);
11142b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11152b15cb3dSCy Schubert 			"v4only.example.com") ||
11162b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "v4assert.example.com")) {
11172b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_A) {
11182b15cb3dSCy Schubert 				ans.s_addr = htonl(0x12345678);
11192b15cb3dSCy Schubert 				evdns_server_request_add_a_reply(req, qname,
11202b15cb3dSCy Schubert 				    1, &ans.s_addr, 2000);
11212b15cb3dSCy Schubert 				added_any = 1;
11222b15cb3dSCy Schubert 			} else if (!evutil_ascii_strcasecmp(qname,
11232b15cb3dSCy Schubert 				"v4assert.example.com")) {
11242b15cb3dSCy Schubert 				TT_FAIL(("Got an AAAA request for v4assert"));
11252b15cb3dSCy Schubert 			}
11262b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11272b15cb3dSCy Schubert 			"v6only.example.com") ||
11282b15cb3dSCy Schubert 		    !evutil_ascii_strcasecmp(qname, "v6assert.example.com")) {
11292b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_AAAA) {
11302b15cb3dSCy Schubert 				ans6.s6_addr[0] = 0x0b;
11312b15cb3dSCy Schubert 				ans6.s6_addr[1] = 0x0b;
11322b15cb3dSCy Schubert 				ans6.s6_addr[14] = 0xf0;
11332b15cb3dSCy Schubert 				ans6.s6_addr[15] = 0x0d;
11342b15cb3dSCy Schubert 				evdns_server_request_add_aaaa_reply(req, qname,
11352b15cb3dSCy Schubert 				    1, &ans6.s6_addr, 2000);
11362b15cb3dSCy Schubert 				added_any = 1;
11372b15cb3dSCy Schubert 			}  else if (!evutil_ascii_strcasecmp(qname,
11382b15cb3dSCy Schubert 				"v6assert.example.com")) {
11392b15cb3dSCy Schubert 				TT_FAIL(("Got a A request for v6assert"));
11402b15cb3dSCy Schubert 			}
11412b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11422b15cb3dSCy Schubert 			"v6timeout.example.com")) {
11432b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_A) {
11442b15cb3dSCy Schubert 				ans.s_addr = htonl(0xabcdef01);
11452b15cb3dSCy Schubert 				evdns_server_request_add_a_reply(req, qname,
11462b15cb3dSCy Schubert 				    1, &ans.s_addr, 2000);
11472b15cb3dSCy Schubert 				added_any = 1;
11482b15cb3dSCy Schubert 			} else if (qtype == EVDNS_TYPE_AAAA) {
11492b15cb3dSCy Schubert 				/* Let the v6 request time out.*/
11502b15cb3dSCy Schubert 				evdns_server_request_drop(req);
11512b15cb3dSCy Schubert 				return;
11522b15cb3dSCy Schubert 			}
11532b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11542b15cb3dSCy Schubert 			"v4timeout.example.com")) {
11552b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_AAAA) {
11562b15cb3dSCy Schubert 				ans6.s6_addr[0] = 0x0a;
11572b15cb3dSCy Schubert 				ans6.s6_addr[1] = 0x0a;
11582b15cb3dSCy Schubert 				ans6.s6_addr[14] = 0xff;
11592b15cb3dSCy Schubert 				ans6.s6_addr[15] = 0x01;
11602b15cb3dSCy Schubert 				evdns_server_request_add_aaaa_reply(req, qname,
11612b15cb3dSCy Schubert 				    1, &ans6.s6_addr, 2000);
11622b15cb3dSCy Schubert 				added_any = 1;
11632b15cb3dSCy Schubert 			} else if (qtype == EVDNS_TYPE_A) {
11642b15cb3dSCy Schubert 				/* Let the v4 request time out.*/
11652b15cb3dSCy Schubert 				evdns_server_request_drop(req);
11662b15cb3dSCy Schubert 				return;
11672b15cb3dSCy Schubert 			}
11682b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11692b15cb3dSCy Schubert 			"v6timeout-nonexist.example.com")) {
11702b15cb3dSCy Schubert 			if (qtype == EVDNS_TYPE_A) {
11712b15cb3dSCy Schubert 				/* Fall through, give an nexist. */
11722b15cb3dSCy Schubert 			} else if (qtype == EVDNS_TYPE_AAAA) {
11732b15cb3dSCy Schubert 				/* Let the v6 request time out.*/
11742b15cb3dSCy Schubert 				evdns_server_request_drop(req);
11752b15cb3dSCy Schubert 				return;
11762b15cb3dSCy Schubert 			}
11772b15cb3dSCy Schubert 		} else if (!evutil_ascii_strcasecmp(qname,
11782b15cb3dSCy Schubert 			"all-timeout.example.com")) {
11792b15cb3dSCy Schubert 			/* drop all requests */
11802b15cb3dSCy Schubert 			evdns_server_request_drop(req);
11812b15cb3dSCy Schubert 			return;
11822b15cb3dSCy Schubert 		} else {
11832b15cb3dSCy Schubert 			TT_GRIPE(("Got weird request for %s",qname));
11842b15cb3dSCy Schubert 		}
11852b15cb3dSCy Schubert 	}
11862b15cb3dSCy Schubert 	if (added_any) {
11872b15cb3dSCy Schubert 		TT_BLATHER(("answering"));
11882b15cb3dSCy Schubert 		evdns_server_request_respond(req, 0);
11892b15cb3dSCy Schubert 	} else {
11902b15cb3dSCy Schubert 		TT_BLATHER(("saying nexist."));
11912b15cb3dSCy Schubert 		evdns_server_request_respond(req, 3);
11922b15cb3dSCy Schubert 	}
11932b15cb3dSCy Schubert }
11942b15cb3dSCy Schubert 
11952b15cb3dSCy Schubert /* Implements a listener for connect_hostname test. */
11962b15cb3dSCy Schubert static void
nil_accept_cb(struct evconnlistener * l,evutil_socket_t fd,struct sockaddr * s,int socklen,void * arg)11972b15cb3dSCy Schubert nil_accept_cb(struct evconnlistener *l, evutil_socket_t fd, struct sockaddr *s,
11982b15cb3dSCy Schubert     int socklen, void *arg)
11992b15cb3dSCy Schubert {
12002b15cb3dSCy Schubert 	int *p = arg;
12012b15cb3dSCy Schubert 	(*p)++;
12022b15cb3dSCy Schubert 	++total_n_accepted;
12032b15cb3dSCy Schubert 	/* don't do anything with the socket; let it close when we exit() */
12042b15cb3dSCy Schubert 	if (total_n_accepted >= 3 && total_connected_or_failed >= 5)
12052b15cb3dSCy Schubert 		event_base_loopexit(be_connect_hostname_base,
12062b15cb3dSCy Schubert 		    NULL);
12072b15cb3dSCy Schubert }
12082b15cb3dSCy Schubert 
12092b15cb3dSCy Schubert struct be_conn_hostname_result {
12102b15cb3dSCy Schubert 	int dnserr;
12112b15cb3dSCy Schubert 	int what;
12122b15cb3dSCy Schubert };
12132b15cb3dSCy Schubert 
12142b15cb3dSCy Schubert /* Bufferevent event callback for the connect_hostname test: remembers what
12152b15cb3dSCy Schubert  * event we got. */
12162b15cb3dSCy Schubert static void
be_connect_hostname_event_cb(struct bufferevent * bev,short what,void * ctx)12172b15cb3dSCy Schubert be_connect_hostname_event_cb(struct bufferevent *bev, short what, void *ctx)
12182b15cb3dSCy Schubert {
12192b15cb3dSCy Schubert 	struct be_conn_hostname_result *got = ctx;
1220*a466cc55SCy Schubert 
1221*a466cc55SCy Schubert 	if (got->what) {
1222*a466cc55SCy Schubert 		TT_FAIL(("Two events on one bufferevent. %d,%d",
1223*a466cc55SCy Schubert 			got->what, (int)what));
1224*a466cc55SCy Schubert 	}
1225*a466cc55SCy Schubert 
12262b15cb3dSCy Schubert 	TT_BLATHER(("Got a bufferevent event %d", what));
12272b15cb3dSCy Schubert 	got->what = what;
12282b15cb3dSCy Schubert 
12292b15cb3dSCy Schubert 	if ((what & BEV_EVENT_CONNECTED) || (what & BEV_EVENT_ERROR)) {
1230*a466cc55SCy Schubert 		int expected = 3;
1231*a466cc55SCy Schubert 		int r = bufferevent_socket_get_dns_error(bev);
1232*a466cc55SCy Schubert 
1233*a466cc55SCy Schubert 		if (r) {
12342b15cb3dSCy Schubert 			got->dnserr = r;
12352b15cb3dSCy Schubert 			TT_BLATHER(("DNS error %d: %s", r,
12362b15cb3dSCy Schubert 				   evutil_gai_strerror(r)));
1237*a466cc55SCy Schubert 		}
1238*a466cc55SCy Schubert 		++total_connected_or_failed;
12392b15cb3dSCy Schubert 		TT_BLATHER(("Got %d connections or errors.", total_connected_or_failed));
12402b15cb3dSCy Schubert 
1241*a466cc55SCy Schubert 		/** emfile test */
1242*a466cc55SCy Schubert 		if (errno == EMFILE) {
1243*a466cc55SCy Schubert 			expected = 0;
1244*a466cc55SCy Schubert 		}
1245*a466cc55SCy Schubert 
1246*a466cc55SCy Schubert 		if (total_n_accepted >= expected && total_connected_or_failed >= 5)
12472b15cb3dSCy Schubert 			event_base_loopexit(be_connect_hostname_base,
12482b15cb3dSCy Schubert 			    NULL);
12492b15cb3dSCy Schubert 	}
12502b15cb3dSCy Schubert }
12512b15cb3dSCy Schubert 
12522b15cb3dSCy Schubert static void
test_bufferevent_connect_hostname(void * arg)12532b15cb3dSCy Schubert test_bufferevent_connect_hostname(void *arg)
12542b15cb3dSCy Schubert {
12552b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
12562b15cb3dSCy Schubert 	struct evconnlistener *listener = NULL;
1257*a466cc55SCy Schubert 	struct bufferevent *be[5];
1258*a466cc55SCy Schubert 	struct be_conn_hostname_result be_outcome[ARRAY_SIZE(be)];
1259*a466cc55SCy Schubert 	int expect_err;
12602b15cb3dSCy Schubert 	struct evdns_base *dns=NULL;
12612b15cb3dSCy Schubert 	struct evdns_server_port *port=NULL;
12622b15cb3dSCy Schubert 	struct sockaddr_in sin;
12632b15cb3dSCy Schubert 	int listener_port=-1;
12642b15cb3dSCy Schubert 	ev_uint16_t dns_port=0;
12652b15cb3dSCy Schubert 	int n_accept=0, n_dns=0;
12662b15cb3dSCy Schubert 	char buf[128];
1267*a466cc55SCy Schubert 	int emfile = data->setup_data && !strcmp(data->setup_data, "emfile");
1268*a466cc55SCy Schubert 	unsigned i;
1269*a466cc55SCy Schubert 	int ret;
12702b15cb3dSCy Schubert 
12712b15cb3dSCy Schubert 	be_connect_hostname_base = data->base;
12722b15cb3dSCy Schubert 
12732b15cb3dSCy Schubert 	/* Bind an address and figure out what port it's on. */
12742b15cb3dSCy Schubert 	memset(&sin, 0, sizeof(sin));
12752b15cb3dSCy Schubert 	sin.sin_family = AF_INET;
12762b15cb3dSCy Schubert 	sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
12772b15cb3dSCy Schubert 	sin.sin_port = 0;
12782b15cb3dSCy Schubert 	listener = evconnlistener_new_bind(data->base, nil_accept_cb,
12792b15cb3dSCy Schubert 	    &n_accept,
12802b15cb3dSCy Schubert 	    LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC,
12812b15cb3dSCy Schubert 	    -1, (struct sockaddr *)&sin, sizeof(sin));
12822b15cb3dSCy Schubert 	tt_assert(listener);
12832b15cb3dSCy Schubert 	listener_port = regress_get_socket_port(
12842b15cb3dSCy Schubert 		evconnlistener_get_fd(listener));
12852b15cb3dSCy Schubert 
12862b15cb3dSCy Schubert 	port = regress_get_dnsserver(data->base, &dns_port, NULL,
12872b15cb3dSCy Schubert 	    be_getaddrinfo_server_cb, &n_dns);
12882b15cb3dSCy Schubert 	tt_assert(port);
12892b15cb3dSCy Schubert 	tt_int_op(dns_port, >=, 0);
12902b15cb3dSCy Schubert 
12912b15cb3dSCy Schubert 	/* Start an evdns_base that uses the server as its resolver. */
12922b15cb3dSCy Schubert 	dns = evdns_base_new(data->base, 0);
12932b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)dns_port);
12942b15cb3dSCy Schubert 	evdns_base_nameserver_ip_add(dns, buf);
12952b15cb3dSCy Schubert 
1296*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
1297*a466cc55SCy Schubert 	if (emfile) {
1298*a466cc55SCy Schubert 		int fd = socket(AF_INET, SOCK_STREAM, 0);
1299*a466cc55SCy Schubert 		struct rlimit file = { fd, fd };
1300*a466cc55SCy Schubert 
1301*a466cc55SCy Schubert 		tt_int_op(fd, >=, 0);
1302*a466cc55SCy Schubert 		tt_assert(!close(fd));
1303*a466cc55SCy Schubert 
1304*a466cc55SCy Schubert 		tt_assert(!setrlimit(RLIMIT_NOFILE, &file));
1305*a466cc55SCy Schubert 	}
1306*a466cc55SCy Schubert #endif
1307*a466cc55SCy Schubert 
13082b15cb3dSCy Schubert 	/* Now, finally, at long last, launch the bufferevents.	 One should do
13092b15cb3dSCy Schubert 	 * a failing lookup IP, one should do a successful lookup by IP,
13102b15cb3dSCy Schubert 	 * and one should do a successful lookup by hostname. */
1311*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(be); ++i) {
1312*a466cc55SCy Schubert 		memset(&be_outcome[i], 0, sizeof(be_outcome[i]));
1313*a466cc55SCy Schubert 		be[i] = bufferevent_socket_new(data->base, -1, BEV_OPT_CLOSE_ON_FREE);
1314*a466cc55SCy Schubert 		bufferevent_setcb(be[i], NULL, NULL, be_connect_hostname_event_cb,
1315*a466cc55SCy Schubert 			&be_outcome[i]);
1316*a466cc55SCy Schubert 	}
13172b15cb3dSCy Schubert 
13182b15cb3dSCy Schubert 	/* Use the blocking resolver.  This one will fail if your resolver
13192b15cb3dSCy Schubert 	 * can't resolve localhost to 127.0.0.1 */
1320*a466cc55SCy Schubert 	tt_assert(!bufferevent_socket_connect_hostname(be[3], NULL, AF_INET,
13212b15cb3dSCy Schubert 		"localhost", listener_port));
13222b15cb3dSCy Schubert 	/* Use the blocking resolver with a nonexistent hostname. */
1323*a466cc55SCy Schubert 	tt_assert(!bufferevent_socket_connect_hostname(be[4], NULL, AF_INET,
13242b15cb3dSCy Schubert 		"nonesuch.nowhere.example.com", 80));
13252b15cb3dSCy Schubert 	{
13262b15cb3dSCy Schubert 		/* The blocking resolver will use the system nameserver, which
13272b15cb3dSCy Schubert 		 * might tell us anything.  (Yes, some twits even pretend that
13282b15cb3dSCy Schubert 		 * example.com is real.) Let's see what answer to expect. */
13292b15cb3dSCy Schubert 		struct evutil_addrinfo hints, *ai = NULL;
13302b15cb3dSCy Schubert 		memset(&hints, 0, sizeof(hints));
13312b15cb3dSCy Schubert 		hints.ai_family = AF_INET;
13322b15cb3dSCy Schubert 		hints.ai_socktype = SOCK_STREAM;
13332b15cb3dSCy Schubert 		hints.ai_protocol = IPPROTO_TCP;
1334*a466cc55SCy Schubert 		expect_err = evutil_getaddrinfo(
13352b15cb3dSCy Schubert 			"nonesuch.nowhere.example.com", "80", &hints, &ai);
13362b15cb3dSCy Schubert 	}
1337*a466cc55SCy Schubert 	/* Launch an async resolve that will fail. */
1338*a466cc55SCy Schubert 	tt_assert(!bufferevent_socket_connect_hostname(be[0], dns, AF_INET,
1339*a466cc55SCy Schubert 		"nosuchplace.example.com", listener_port));
1340*a466cc55SCy Schubert 	/* Connect to the IP without resolving. */
1341*a466cc55SCy Schubert 	tt_assert(!bufferevent_socket_connect_hostname(be[1], dns, AF_INET,
1342*a466cc55SCy Schubert 		"127.0.0.1", listener_port));
1343*a466cc55SCy Schubert 	/* Launch an async resolve that will succeed. */
1344*a466cc55SCy Schubert 	tt_assert(!bufferevent_socket_connect_hostname(be[2], dns, AF_INET,
1345*a466cc55SCy Schubert 		"nobodaddy.example.com", listener_port));
13462b15cb3dSCy Schubert 
1347*a466cc55SCy Schubert 	ret = event_base_dispatch(data->base);
1348*a466cc55SCy Schubert #ifdef __sun__
1349*a466cc55SCy Schubert 	if (emfile && !strcmp(event_base_get_method(data->base), "devpoll")) {
1350*a466cc55SCy Schubert 		tt_int_op(ret, ==, -1);
1351*a466cc55SCy Schubert 		/** DP_POLL failed */
1352*a466cc55SCy Schubert 		tt_skip();
1353*a466cc55SCy Schubert 	} else
1354*a466cc55SCy Schubert #endif
1355*a466cc55SCy Schubert 	{
1356*a466cc55SCy Schubert 		tt_int_op(ret, ==, 0);
13572b15cb3dSCy Schubert 	}
13582b15cb3dSCy Schubert 
1359*a466cc55SCy Schubert 	tt_int_op(be_outcome[0].what, ==, BEV_EVENT_ERROR);
1360*a466cc55SCy Schubert 	tt_int_op(be_outcome[0].dnserr, ==, EVUTIL_EAI_NONAME);
1361*a466cc55SCy Schubert 	tt_int_op(be_outcome[1].what, ==, !emfile ? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR);
1362*a466cc55SCy Schubert 	tt_int_op(be_outcome[1].dnserr, ==, 0);
1363*a466cc55SCy Schubert 	tt_int_op(be_outcome[2].what, ==, !emfile ? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR);
1364*a466cc55SCy Schubert 	tt_int_op(be_outcome[2].dnserr, ==, 0);
1365*a466cc55SCy Schubert 	tt_int_op(be_outcome[3].what, ==, !emfile ? BEV_EVENT_CONNECTED : BEV_EVENT_ERROR);
1366*a466cc55SCy Schubert 	if (!emfile) {
1367*a466cc55SCy Schubert 		tt_int_op(be_outcome[3].dnserr, ==, 0);
1368*a466cc55SCy Schubert 	} else {
1369*a466cc55SCy Schubert 		tt_int_op(be_outcome[3].dnserr, !=, 0);
1370*a466cc55SCy Schubert 	}
1371*a466cc55SCy Schubert 	if (expect_err) {
1372*a466cc55SCy Schubert 		tt_int_op(be_outcome[4].what, ==, BEV_EVENT_ERROR);
1373*a466cc55SCy Schubert 		tt_int_op(be_outcome[4].dnserr, ==, expect_err);
1374*a466cc55SCy Schubert 	}
1375*a466cc55SCy Schubert 
1376*a466cc55SCy Schubert 	if (emfile) {
1377*a466cc55SCy Schubert 		tt_int_op(n_accept, ==, 0);
1378*a466cc55SCy Schubert 	} else {
13792b15cb3dSCy Schubert 		tt_int_op(n_accept, ==, 3);
1380*a466cc55SCy Schubert 	}
13812b15cb3dSCy Schubert 	tt_int_op(n_dns, ==, 2);
13822b15cb3dSCy Schubert 
13832b15cb3dSCy Schubert end:
13842b15cb3dSCy Schubert 	if (listener)
13852b15cb3dSCy Schubert 		evconnlistener_free(listener);
13862b15cb3dSCy Schubert 	if (port)
13872b15cb3dSCy Schubert 		evdns_close_server_port(port);
13882b15cb3dSCy Schubert 	if (dns)
13892b15cb3dSCy Schubert 		evdns_base_free(dns, 0);
1390*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(be); ++i) {
1391*a466cc55SCy Schubert 		if (be[i])
1392*a466cc55SCy Schubert 			bufferevent_free(be[i]);
1393*a466cc55SCy Schubert 	}
13942b15cb3dSCy Schubert }
13952b15cb3dSCy Schubert 
13962b15cb3dSCy Schubert 
13972b15cb3dSCy Schubert struct gai_outcome {
13982b15cb3dSCy Schubert 	int err;
13992b15cb3dSCy Schubert 	struct evutil_addrinfo *ai;
14002b15cb3dSCy Schubert };
14012b15cb3dSCy Schubert 
14022b15cb3dSCy Schubert static int n_gai_results_pending = 0;
14032b15cb3dSCy Schubert static struct event_base *exit_base_on_no_pending_results = NULL;
14042b15cb3dSCy Schubert 
14052b15cb3dSCy Schubert static void
gai_cb(int err,struct evutil_addrinfo * res,void * ptr)14062b15cb3dSCy Schubert gai_cb(int err, struct evutil_addrinfo *res, void *ptr)
14072b15cb3dSCy Schubert {
14082b15cb3dSCy Schubert 	struct gai_outcome *go = ptr;
14092b15cb3dSCy Schubert 	go->err = err;
14102b15cb3dSCy Schubert 	go->ai = res;
14112b15cb3dSCy Schubert 	if (--n_gai_results_pending <= 0 && exit_base_on_no_pending_results)
14122b15cb3dSCy Schubert 		event_base_loopexit(exit_base_on_no_pending_results, NULL);
14132b15cb3dSCy Schubert 	if (n_gai_results_pending < 900)
14142b15cb3dSCy Schubert 		TT_BLATHER(("Got an answer; expecting %d more.",
14152b15cb3dSCy Schubert 			n_gai_results_pending));
14162b15cb3dSCy Schubert }
14172b15cb3dSCy Schubert 
14182b15cb3dSCy Schubert static void
cancel_gai_cb(evutil_socket_t fd,short what,void * ptr)14192b15cb3dSCy Schubert cancel_gai_cb(evutil_socket_t fd, short what, void *ptr)
14202b15cb3dSCy Schubert {
14212b15cb3dSCy Schubert 	struct evdns_getaddrinfo_request *r = ptr;
14222b15cb3dSCy Schubert 	evdns_getaddrinfo_cancel(r);
14232b15cb3dSCy Schubert }
14242b15cb3dSCy Schubert 
14252b15cb3dSCy Schubert static void
test_getaddrinfo_async(void * arg)14262b15cb3dSCy Schubert test_getaddrinfo_async(void *arg)
14272b15cb3dSCy Schubert {
14282b15cb3dSCy Schubert 	struct basic_test_data *data = arg;
14292b15cb3dSCy Schubert 	struct evutil_addrinfo hints, *a;
14302b15cb3dSCy Schubert 	struct gai_outcome local_outcome;
14312b15cb3dSCy Schubert 	struct gai_outcome a_out[12];
1432*a466cc55SCy Schubert 	unsigned i;
14332b15cb3dSCy Schubert 	struct evdns_getaddrinfo_request *r;
14342b15cb3dSCy Schubert 	char buf[128];
14352b15cb3dSCy Schubert 	struct evdns_server_port *port = NULL;
14362b15cb3dSCy Schubert 	ev_uint16_t dns_port = 0;
14372b15cb3dSCy Schubert 	int n_dns_questions = 0;
14382b15cb3dSCy Schubert 	struct evdns_base *dns_base;
14392b15cb3dSCy Schubert 
1440*a466cc55SCy Schubert 	memset(a_out, 0, sizeof(a_out));
14412b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
14422b15cb3dSCy Schubert 
14432b15cb3dSCy Schubert 	dns_base = evdns_base_new(data->base, 0);
14442b15cb3dSCy Schubert 	tt_assert(dns_base);
14452b15cb3dSCy Schubert 
14462b15cb3dSCy Schubert 	/* for localhost */
14472b15cb3dSCy Schubert 	evdns_base_load_hosts(dns_base, NULL);
14482b15cb3dSCy Schubert 
14492b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns_base, "timeout", "0.3"));
14502b15cb3dSCy Schubert 	tt_assert(! evdns_base_set_option(dns_base, "getaddrinfo-allow-skew", "0.2"));
14512b15cb3dSCy Schubert 
14522b15cb3dSCy Schubert 	n_gai_results_pending = 10000; /* don't think about exiting yet. */
14532b15cb3dSCy Schubert 
14542b15cb3dSCy Schubert 	/* 1. Try some cases that will never hit the asynchronous resolver. */
14552b15cb3dSCy Schubert 	/* 1a. Simple case with a symbolic service name */
14562b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
14572b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
14582b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
14592b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
14602b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "1.2.3.4", "http",
14612b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
14622b15cb3dSCy Schubert 	tt_assert(! r);
14632b15cb3dSCy Schubert 	if (!local_outcome.err) {
14642b15cb3dSCy Schubert 		tt_ptr_op(local_outcome.ai,!=,NULL);
14652b15cb3dSCy Schubert 		test_ai_eq(local_outcome.ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
14662b15cb3dSCy Schubert 		evutil_freeaddrinfo(local_outcome.ai);
14672b15cb3dSCy Schubert 		local_outcome.ai = NULL;
14682b15cb3dSCy Schubert 	} else {
14692b15cb3dSCy Schubert 		TT_BLATHER(("Apparently we have no getservbyname."));
14702b15cb3dSCy Schubert 	}
14712b15cb3dSCy Schubert 
14722b15cb3dSCy Schubert 	/* 1b. EVUTIL_AI_NUMERICHOST is set */
14732b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
14742b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
14752b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
14762b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
14772b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "www.google.com", "80",
14782b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
14792b15cb3dSCy Schubert 	tt_ptr_op(r,==,NULL);
14802b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,EVUTIL_EAI_NONAME);
14812b15cb3dSCy Schubert 	tt_ptr_op(local_outcome.ai,==,NULL);
14822b15cb3dSCy Schubert 
14832b15cb3dSCy Schubert 	/* 1c. We give a numeric address (ipv6) */
14842b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
14852b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
14862b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
14872b15cb3dSCy Schubert 	hints.ai_protocol = IPPROTO_TCP;
14882b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "f::f", "8008",
14892b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
14902b15cb3dSCy Schubert 	tt_assert(!r);
14912b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
14922b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
14932b15cb3dSCy Schubert 	tt_ptr_op(local_outcome.ai->ai_next,==,NULL);
14942b15cb3dSCy Schubert 	test_ai_eq(local_outcome.ai, "[f::f]:8008", SOCK_STREAM, IPPROTO_TCP);
14952b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
14962b15cb3dSCy Schubert 	local_outcome.ai = NULL;
14972b15cb3dSCy Schubert 
14982b15cb3dSCy Schubert 	/* 1d. We give a numeric address (ipv4) */
14992b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
15002b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
15012b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
15022b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "5.6.7.8", NULL,
15032b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
15042b15cb3dSCy Schubert 	tt_assert(!r);
15052b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
15062b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
15072b15cb3dSCy Schubert 	a = ai_find_by_protocol(local_outcome.ai, IPPROTO_TCP);
15082b15cb3dSCy Schubert 	tt_assert(a);
15092b15cb3dSCy Schubert 	test_ai_eq(a, "5.6.7.8", SOCK_STREAM, IPPROTO_TCP);
15102b15cb3dSCy Schubert 	a = ai_find_by_protocol(local_outcome.ai, IPPROTO_UDP);
15112b15cb3dSCy Schubert 	tt_assert(a);
15122b15cb3dSCy Schubert 	test_ai_eq(a, "5.6.7.8", SOCK_DGRAM, IPPROTO_UDP);
15132b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
15142b15cb3dSCy Schubert 	local_outcome.ai = NULL;
15152b15cb3dSCy Schubert 
15162b15cb3dSCy Schubert 	/* 1e. nodename is NULL (bind) */
15172b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
15182b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
15192b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
15202b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_DGRAM;
15212b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_PASSIVE;
15222b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, NULL, "9090",
15232b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
15242b15cb3dSCy Schubert 	tt_assert(!r);
15252b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
15262b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
15272b15cb3dSCy Schubert 	/* we should get a v4 address of 0.0.0.0... */
15282b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET);
15292b15cb3dSCy Schubert 	tt_assert(a);
15302b15cb3dSCy Schubert 	test_ai_eq(a, "0.0.0.0:9090", SOCK_DGRAM, IPPROTO_UDP);
15312b15cb3dSCy Schubert 	/* ... and a v6 address of ::0 */
15322b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET6);
15332b15cb3dSCy Schubert 	tt_assert(a);
15342b15cb3dSCy Schubert 	test_ai_eq(a, "[::]:9090", SOCK_DGRAM, IPPROTO_UDP);
15352b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
15362b15cb3dSCy Schubert 	local_outcome.ai = NULL;
15372b15cb3dSCy Schubert 
15382b15cb3dSCy Schubert 	/* 1f. nodename is NULL (connect) */
15392b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
15402b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
15412b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
15422b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
15432b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, NULL, "2",
15442b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
15452b15cb3dSCy Schubert 	tt_assert(!r);
15462b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
15472b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
15482b15cb3dSCy Schubert 	/* we should get a v4 address of 127.0.0.1 .... */
15492b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET);
15502b15cb3dSCy Schubert 	tt_assert(a);
15512b15cb3dSCy Schubert 	test_ai_eq(a, "127.0.0.1:2", SOCK_STREAM, IPPROTO_TCP);
15522b15cb3dSCy Schubert 	/* ... and a v6 address of ::1 */
15532b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET6);
15542b15cb3dSCy Schubert 	tt_assert(a);
15552b15cb3dSCy Schubert 	test_ai_eq(a, "[::1]:2", SOCK_STREAM, IPPROTO_TCP);
15562b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
15572b15cb3dSCy Schubert 	local_outcome.ai = NULL;
15582b15cb3dSCy Schubert 
15592b15cb3dSCy Schubert 	/* 1g. We find localhost immediately. (pf_unspec) */
15602b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
15612b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
15622b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
15632b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
15642b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "LOCALHOST", "80",
15652b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
15662b15cb3dSCy Schubert 	tt_assert(!r);
15672b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
15682b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
15692b15cb3dSCy Schubert 	/* we should get a v4 address of 127.0.0.1 .... */
15702b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET);
15712b15cb3dSCy Schubert 	tt_assert(a);
15722b15cb3dSCy Schubert 	test_ai_eq(a, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP);
15732b15cb3dSCy Schubert 	/* ... and a v6 address of ::1 */
15742b15cb3dSCy Schubert 	a = ai_find_by_family(local_outcome.ai, PF_INET6);
15752b15cb3dSCy Schubert 	tt_assert(a);
15762b15cb3dSCy Schubert 	test_ai_eq(a, "[::1]:80", SOCK_STREAM, IPPROTO_TCP);
15772b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
15782b15cb3dSCy Schubert 	local_outcome.ai = NULL;
15792b15cb3dSCy Schubert 
15802b15cb3dSCy Schubert 	/* 1g. We find localhost immediately. (pf_inet6) */
15812b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
15822b15cb3dSCy Schubert 	memset(&local_outcome, 0, sizeof(local_outcome));
15832b15cb3dSCy Schubert 	hints.ai_family = PF_INET6;
15842b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
15852b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "LOCALHOST", "9999",
15862b15cb3dSCy Schubert 	    &hints, gai_cb, &local_outcome);
15872b15cb3dSCy Schubert 	tt_assert(! r);
15882b15cb3dSCy Schubert 	tt_int_op(local_outcome.err,==,0);
15892b15cb3dSCy Schubert 	tt_assert(local_outcome.ai);
15902b15cb3dSCy Schubert 	a = local_outcome.ai;
15912b15cb3dSCy Schubert 	test_ai_eq(a, "[::1]:9999", SOCK_STREAM, IPPROTO_TCP);
15922b15cb3dSCy Schubert 	tt_ptr_op(a->ai_next, ==, NULL);
15932b15cb3dSCy Schubert 	evutil_freeaddrinfo(local_outcome.ai);
15942b15cb3dSCy Schubert 	local_outcome.ai = NULL;
15952b15cb3dSCy Schubert 
15962b15cb3dSCy Schubert 	/* 2. Okay, now we can actually test the asynchronous resolver. */
15972b15cb3dSCy Schubert 	/* Start a dummy local dns server... */
15982b15cb3dSCy Schubert 	port = regress_get_dnsserver(data->base, &dns_port, NULL,
15992b15cb3dSCy Schubert 	    be_getaddrinfo_server_cb, &n_dns_questions);
16002b15cb3dSCy Schubert 	tt_assert(port);
16012b15cb3dSCy Schubert 	tt_int_op(dns_port, >=, 0);
16022b15cb3dSCy Schubert 	/* ... and tell the evdns_base about it. */
16032b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port);
16042b15cb3dSCy Schubert 	evdns_base_nameserver_ip_add(dns_base, buf);
16052b15cb3dSCy Schubert 
16062b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
16072b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
16082b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
16092b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_CANONNAME;
16102b15cb3dSCy Schubert 	/* 0: Request for both.example.com should return both addresses. */
16112b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "both.example.com", "8000",
16122b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[0]);
16132b15cb3dSCy Schubert 	tt_assert(r);
16142b15cb3dSCy Schubert 
16152b15cb3dSCy Schubert 	/* 1: Request for v4only.example.com should return one address. */
16162b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v4only.example.com", "8001",
16172b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[1]);
16182b15cb3dSCy Schubert 	tt_assert(r);
16192b15cb3dSCy Schubert 
16202b15cb3dSCy Schubert 	/* 2: Request for v6only.example.com should return one address. */
16212b15cb3dSCy Schubert 	hints.ai_flags = 0;
16222b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v6only.example.com", "8002",
16232b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[2]);
16242b15cb3dSCy Schubert 	tt_assert(r);
16252b15cb3dSCy Schubert 
16262b15cb3dSCy Schubert 	/* 3: PF_INET request for v4assert.example.com should not generate a
16272b15cb3dSCy Schubert 	 * v6 request.	The server will fail the test if it does. */
16282b15cb3dSCy Schubert 	hints.ai_family = PF_INET;
16292b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v4assert.example.com", "8003",
16302b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[3]);
16312b15cb3dSCy Schubert 	tt_assert(r);
16322b15cb3dSCy Schubert 
16332b15cb3dSCy Schubert 	/* 4: PF_INET6 request for v6assert.example.com should not generate a
16342b15cb3dSCy Schubert 	 * v4 request.	The server will fail the test if it does. */
16352b15cb3dSCy Schubert 	hints.ai_family = PF_INET6;
16362b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v6assert.example.com", "8004",
16372b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[4]);
16382b15cb3dSCy Schubert 	tt_assert(r);
16392b15cb3dSCy Schubert 
16402b15cb3dSCy Schubert 	/* 5: PF_INET request for nosuchplace.example.com should give NEXIST. */
16412b15cb3dSCy Schubert 	hints.ai_family = PF_INET;
16422b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8005",
16432b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[5]);
16442b15cb3dSCy Schubert 	tt_assert(r);
16452b15cb3dSCy Schubert 
16462b15cb3dSCy Schubert 	/* 6: PF_UNSPEC request for nosuchplace.example.com should give NEXIST.
16472b15cb3dSCy Schubert 	 */
16482b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
16492b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "nosuchplace.example.com", "8006",
16502b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[6]);
16512b15cb3dSCy Schubert 	tt_assert(r);
16522b15cb3dSCy Schubert 
16532b15cb3dSCy Schubert 	/* 7: PF_UNSPEC request for v6timeout.example.com should give an ipv4
16542b15cb3dSCy Schubert 	 * address only. */
16552b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
16562b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v6timeout.example.com", "8007",
16572b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[7]);
16582b15cb3dSCy Schubert 	tt_assert(r);
16592b15cb3dSCy Schubert 
16602b15cb3dSCy Schubert 	/* 8: PF_UNSPEC request for v6timeout-nonexist.example.com should give
16612b15cb3dSCy Schubert 	 * a NEXIST */
16622b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
16632b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v6timeout-nonexist.example.com",
16642b15cb3dSCy Schubert 	    "8008", &hints, gai_cb, &a_out[8]);
16652b15cb3dSCy Schubert 	tt_assert(r);
16662b15cb3dSCy Schubert 
16672b15cb3dSCy Schubert 	/* 9: AI_ADDRCONFIG should at least not crash.	Can't test it more
16682b15cb3dSCy Schubert 	 * without knowing what kind of internet we have. */
16692b15cb3dSCy Schubert 	hints.ai_flags |= EVUTIL_AI_ADDRCONFIG;
16702b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "both.example.com",
16712b15cb3dSCy Schubert 	    "8009", &hints, gai_cb, &a_out[9]);
16722b15cb3dSCy Schubert 	tt_assert(r);
16732b15cb3dSCy Schubert 
16742b15cb3dSCy Schubert 	/* 10: PF_UNSPEC for v4timeout.example.com should give an ipv6 address
16752b15cb3dSCy Schubert 	 * only. */
16762b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
16772b15cb3dSCy Schubert 	hints.ai_flags = 0;
16782b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "v4timeout.example.com", "8010",
16792b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[10]);
16802b15cb3dSCy Schubert 	tt_assert(r);
16812b15cb3dSCy Schubert 
16822b15cb3dSCy Schubert 	/* 11: timeout.example.com: cancel it after 100 msec. */
16832b15cb3dSCy Schubert 	r = evdns_getaddrinfo(dns_base, "all-timeout.example.com", "8011",
16842b15cb3dSCy Schubert 	    &hints, gai_cb, &a_out[11]);
16852b15cb3dSCy Schubert 	tt_assert(r);
16862b15cb3dSCy Schubert 	{
16872b15cb3dSCy Schubert 		struct timeval tv;
16882b15cb3dSCy Schubert 		tv.tv_sec = 0;
16892b15cb3dSCy Schubert 		tv.tv_usec = 100*1000; /* 100 msec */
16902b15cb3dSCy Schubert 		event_base_once(data->base, -1, EV_TIMEOUT, cancel_gai_cb,
16912b15cb3dSCy Schubert 		    r, &tv);
16922b15cb3dSCy Schubert 	}
16932b15cb3dSCy Schubert 
16942b15cb3dSCy Schubert 	/* XXXXX There are more tests we could do, including:
16952b15cb3dSCy Schubert 
16962b15cb3dSCy Schubert 	   - A test to elicit NODATA.
16972b15cb3dSCy Schubert 
16982b15cb3dSCy Schubert 	 */
16992b15cb3dSCy Schubert 
17002b15cb3dSCy Schubert 	n_gai_results_pending = 12;
17012b15cb3dSCy Schubert 	exit_base_on_no_pending_results = data->base;
17022b15cb3dSCy Schubert 
17032b15cb3dSCy Schubert 	event_base_dispatch(data->base);
17042b15cb3dSCy Schubert 
17052b15cb3dSCy Schubert 	/* 0: both.example.com */
17062b15cb3dSCy Schubert 	tt_int_op(a_out[0].err, ==, 0);
17072b15cb3dSCy Schubert 	tt_assert(a_out[0].ai);
17082b15cb3dSCy Schubert 	tt_assert(a_out[0].ai->ai_next);
17092b15cb3dSCy Schubert 	tt_assert(!a_out[0].ai->ai_next->ai_next);
17102b15cb3dSCy Schubert 	a = ai_find_by_family(a_out[0].ai, PF_INET);
17112b15cb3dSCy Schubert 	tt_assert(a);
17122b15cb3dSCy Schubert 	test_ai_eq(a, "80.80.32.32:8000", SOCK_STREAM, IPPROTO_TCP);
17132b15cb3dSCy Schubert 	a = ai_find_by_family(a_out[0].ai, PF_INET6);
17142b15cb3dSCy Schubert 	tt_assert(a);
17152b15cb3dSCy Schubert 	test_ai_eq(a, "[80ff::bbbb]:8000", SOCK_STREAM, IPPROTO_TCP);
17162b15cb3dSCy Schubert 	tt_assert(a_out[0].ai->ai_canonname);
17172b15cb3dSCy Schubert 	tt_str_op(a_out[0].ai->ai_canonname, ==, "both-canonical.example.com");
17182b15cb3dSCy Schubert 
17192b15cb3dSCy Schubert 	/* 1: v4only.example.com */
17202b15cb3dSCy Schubert 	tt_int_op(a_out[1].err, ==, 0);
17212b15cb3dSCy Schubert 	tt_assert(a_out[1].ai);
17222b15cb3dSCy Schubert 	tt_assert(! a_out[1].ai->ai_next);
17232b15cb3dSCy Schubert 	test_ai_eq(a_out[1].ai, "18.52.86.120:8001", SOCK_STREAM, IPPROTO_TCP);
17242b15cb3dSCy Schubert 	tt_assert(a_out[1].ai->ai_canonname == NULL);
17252b15cb3dSCy Schubert 
17262b15cb3dSCy Schubert 
17272b15cb3dSCy Schubert 	/* 2: v6only.example.com */
17282b15cb3dSCy Schubert 	tt_int_op(a_out[2].err, ==, 0);
17292b15cb3dSCy Schubert 	tt_assert(a_out[2].ai);
17302b15cb3dSCy Schubert 	tt_assert(! a_out[2].ai->ai_next);
17312b15cb3dSCy Schubert 	test_ai_eq(a_out[2].ai, "[b0b::f00d]:8002", SOCK_STREAM, IPPROTO_TCP);
17322b15cb3dSCy Schubert 
17332b15cb3dSCy Schubert 	/* 3: v4assert.example.com */
17342b15cb3dSCy Schubert 	tt_int_op(a_out[3].err, ==, 0);
17352b15cb3dSCy Schubert 	tt_assert(a_out[3].ai);
17362b15cb3dSCy Schubert 	tt_assert(! a_out[3].ai->ai_next);
17372b15cb3dSCy Schubert 	test_ai_eq(a_out[3].ai, "18.52.86.120:8003", SOCK_STREAM, IPPROTO_TCP);
17382b15cb3dSCy Schubert 
17392b15cb3dSCy Schubert 	/* 4: v6assert.example.com */
17402b15cb3dSCy Schubert 	tt_int_op(a_out[4].err, ==, 0);
17412b15cb3dSCy Schubert 	tt_assert(a_out[4].ai);
17422b15cb3dSCy Schubert 	tt_assert(! a_out[4].ai->ai_next);
17432b15cb3dSCy Schubert 	test_ai_eq(a_out[4].ai, "[b0b::f00d]:8004", SOCK_STREAM, IPPROTO_TCP);
17442b15cb3dSCy Schubert 
17452b15cb3dSCy Schubert 	/* 5: nosuchplace.example.com (inet) */
17462b15cb3dSCy Schubert 	tt_int_op(a_out[5].err, ==, EVUTIL_EAI_NONAME);
17472b15cb3dSCy Schubert 	tt_assert(! a_out[5].ai);
17482b15cb3dSCy Schubert 
17492b15cb3dSCy Schubert 	/* 6: nosuchplace.example.com (unspec) */
17502b15cb3dSCy Schubert 	tt_int_op(a_out[6].err, ==, EVUTIL_EAI_NONAME);
17512b15cb3dSCy Schubert 	tt_assert(! a_out[6].ai);
17522b15cb3dSCy Schubert 
17532b15cb3dSCy Schubert 	/* 7: v6timeout.example.com */
17542b15cb3dSCy Schubert 	tt_int_op(a_out[7].err, ==, 0);
17552b15cb3dSCy Schubert 	tt_assert(a_out[7].ai);
17562b15cb3dSCy Schubert 	tt_assert(! a_out[7].ai->ai_next);
17572b15cb3dSCy Schubert 	test_ai_eq(a_out[7].ai, "171.205.239.1:8007", SOCK_STREAM, IPPROTO_TCP);
17582b15cb3dSCy Schubert 
17592b15cb3dSCy Schubert 	/* 8: v6timeout-nonexist.example.com */
17602b15cb3dSCy Schubert 	tt_int_op(a_out[8].err, ==, EVUTIL_EAI_NONAME);
17612b15cb3dSCy Schubert 	tt_assert(! a_out[8].ai);
17622b15cb3dSCy Schubert 
17632b15cb3dSCy Schubert 	/* 9: both (ADDRCONFIG) */
17642b15cb3dSCy Schubert 	tt_int_op(a_out[9].err, ==, 0);
17652b15cb3dSCy Schubert 	tt_assert(a_out[9].ai);
17662b15cb3dSCy Schubert 	a = ai_find_by_family(a_out[9].ai, PF_INET);
17672b15cb3dSCy Schubert 	if (a)
17682b15cb3dSCy Schubert 		test_ai_eq(a, "80.80.32.32:8009", SOCK_STREAM, IPPROTO_TCP);
17692b15cb3dSCy Schubert 	else
17702b15cb3dSCy Schubert 		tt_assert(ai_find_by_family(a_out[9].ai, PF_INET6));
17712b15cb3dSCy Schubert 	a = ai_find_by_family(a_out[9].ai, PF_INET6);
17722b15cb3dSCy Schubert 	if (a)
17732b15cb3dSCy Schubert 		test_ai_eq(a, "[80ff::bbbb]:8009", SOCK_STREAM, IPPROTO_TCP);
17742b15cb3dSCy Schubert 	else
17752b15cb3dSCy Schubert 		tt_assert(ai_find_by_family(a_out[9].ai, PF_INET));
17762b15cb3dSCy Schubert 
17772b15cb3dSCy Schubert 	/* 10: v4timeout.example.com */
17782b15cb3dSCy Schubert 	tt_int_op(a_out[10].err, ==, 0);
17792b15cb3dSCy Schubert 	tt_assert(a_out[10].ai);
17802b15cb3dSCy Schubert 	tt_assert(! a_out[10].ai->ai_next);
17812b15cb3dSCy Schubert 	test_ai_eq(a_out[10].ai, "[a0a::ff01]:8010", SOCK_STREAM, IPPROTO_TCP);
17822b15cb3dSCy Schubert 
17832b15cb3dSCy Schubert 	/* 11: cancelled request. */
17842b15cb3dSCy Schubert 	tt_int_op(a_out[11].err, ==, EVUTIL_EAI_CANCEL);
17852b15cb3dSCy Schubert 	tt_assert(a_out[11].ai == NULL);
17862b15cb3dSCy Schubert 
17872b15cb3dSCy Schubert end:
17882b15cb3dSCy Schubert 	if (local_outcome.ai)
17892b15cb3dSCy Schubert 		evutil_freeaddrinfo(local_outcome.ai);
1790*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(a_out); ++i) {
17912b15cb3dSCy Schubert 		if (a_out[i].ai)
17922b15cb3dSCy Schubert 			evutil_freeaddrinfo(a_out[i].ai);
17932b15cb3dSCy Schubert 	}
17942b15cb3dSCy Schubert 	if (port)
17952b15cb3dSCy Schubert 		evdns_close_server_port(port);
17962b15cb3dSCy Schubert 	if (dns_base)
17972b15cb3dSCy Schubert 		evdns_base_free(dns_base, 0);
17982b15cb3dSCy Schubert }
17992b15cb3dSCy Schubert 
18002b15cb3dSCy Schubert struct gaic_request_status {
18012b15cb3dSCy Schubert 	int magic;
18022b15cb3dSCy Schubert 	struct event_base *base;
18032b15cb3dSCy Schubert 	struct evdns_base *dns_base;
18042b15cb3dSCy Schubert 	struct evdns_getaddrinfo_request *request;
18052b15cb3dSCy Schubert 	struct event cancel_event;
18062b15cb3dSCy Schubert 	int canceled;
18072b15cb3dSCy Schubert };
18082b15cb3dSCy Schubert 
18092b15cb3dSCy Schubert #define GAIC_MAGIC 0x1234abcd
18102b15cb3dSCy Schubert 
1811*a466cc55SCy Schubert static int gaic_pending = 0;
1812*a466cc55SCy Schubert static int gaic_freed = 0;
18132b15cb3dSCy Schubert 
18142b15cb3dSCy Schubert static void
gaic_cancel_request_cb(evutil_socket_t fd,short what,void * arg)18152b15cb3dSCy Schubert gaic_cancel_request_cb(evutil_socket_t fd, short what, void *arg)
18162b15cb3dSCy Schubert {
18172b15cb3dSCy Schubert 	struct gaic_request_status *status = arg;
18182b15cb3dSCy Schubert 
18192b15cb3dSCy Schubert 	tt_assert(status->magic == GAIC_MAGIC);
18202b15cb3dSCy Schubert 	status->canceled = 1;
18212b15cb3dSCy Schubert 	evdns_getaddrinfo_cancel(status->request);
18222b15cb3dSCy Schubert 	return;
18232b15cb3dSCy Schubert end:
18242b15cb3dSCy Schubert 	event_base_loopexit(status->base, NULL);
18252b15cb3dSCy Schubert }
18262b15cb3dSCy Schubert 
18272b15cb3dSCy Schubert static void
gaic_server_cb(struct evdns_server_request * req,void * arg)18282b15cb3dSCy Schubert gaic_server_cb(struct evdns_server_request *req, void *arg)
18292b15cb3dSCy Schubert {
18302b15cb3dSCy Schubert 	ev_uint32_t answer = 0x7f000001;
18312b15cb3dSCy Schubert 	tt_assert(req->nquestions);
18322b15cb3dSCy Schubert 	evdns_server_request_add_a_reply(req, req->questions[0]->name, 1,
18332b15cb3dSCy Schubert 	    &answer, 100);
18342b15cb3dSCy Schubert 	evdns_server_request_respond(req, 0);
18352b15cb3dSCy Schubert 	return;
18362b15cb3dSCy Schubert end:
18372b15cb3dSCy Schubert 	evdns_server_request_respond(req, DNS_ERR_REFUSED);
18382b15cb3dSCy Schubert }
18392b15cb3dSCy Schubert 
18402b15cb3dSCy Schubert 
18412b15cb3dSCy Schubert static void
gaic_getaddrinfo_cb(int result,struct evutil_addrinfo * res,void * arg)18422b15cb3dSCy Schubert gaic_getaddrinfo_cb(int result, struct evutil_addrinfo *res, void *arg)
18432b15cb3dSCy Schubert {
18442b15cb3dSCy Schubert 	struct gaic_request_status *status = arg;
18452b15cb3dSCy Schubert 	struct event_base *base = status->base;
18462b15cb3dSCy Schubert 	tt_assert(status->magic == GAIC_MAGIC);
18472b15cb3dSCy Schubert 
18482b15cb3dSCy Schubert 	if (result == EVUTIL_EAI_CANCEL) {
18492b15cb3dSCy Schubert 		tt_assert(status->canceled);
18502b15cb3dSCy Schubert 	}
18512b15cb3dSCy Schubert 	event_del(&status->cancel_event);
18522b15cb3dSCy Schubert 
18532b15cb3dSCy Schubert 	memset(status, 0xf0, sizeof(*status));
18542b15cb3dSCy Schubert 	free(status);
18552b15cb3dSCy Schubert 
18562b15cb3dSCy Schubert end:
1857*a466cc55SCy Schubert 	if (res)
1858*a466cc55SCy Schubert 	{
1859*a466cc55SCy Schubert 		TT_BLATHER(("evutil_freeaddrinfo(%p)", res));
1860*a466cc55SCy Schubert 		evutil_freeaddrinfo(res);
1861*a466cc55SCy Schubert 		++gaic_freed;
1862*a466cc55SCy Schubert 	}
1863*a466cc55SCy Schubert 	if (--gaic_pending <= 0)
18642b15cb3dSCy Schubert 		event_base_loopexit(base, NULL);
18652b15cb3dSCy Schubert }
18662b15cb3dSCy Schubert 
18672b15cb3dSCy Schubert static void
gaic_launch(struct event_base * base,struct evdns_base * dns_base)18682b15cb3dSCy Schubert gaic_launch(struct event_base *base, struct evdns_base *dns_base)
18692b15cb3dSCy Schubert {
18702b15cb3dSCy Schubert 	struct gaic_request_status *status = calloc(1,sizeof(*status));
18712b15cb3dSCy Schubert 	struct timeval tv = { 0, 10000 };
18722b15cb3dSCy Schubert 	status->magic = GAIC_MAGIC;
18732b15cb3dSCy Schubert 	status->base = base;
18742b15cb3dSCy Schubert 	status->dns_base = dns_base;
18752b15cb3dSCy Schubert 	event_assign(&status->cancel_event, base, -1, 0, gaic_cancel_request_cb,
18762b15cb3dSCy Schubert 	    status);
18772b15cb3dSCy Schubert 	status->request = evdns_getaddrinfo(dns_base,
18782b15cb3dSCy Schubert 	    "foobar.bazquux.example.com", "80", NULL, gaic_getaddrinfo_cb,
18792b15cb3dSCy Schubert 	    status);
18802b15cb3dSCy Schubert 	event_add(&status->cancel_event, &tv);
1881*a466cc55SCy Schubert 	++gaic_pending;
18822b15cb3dSCy Schubert }
18832b15cb3dSCy Schubert 
18842b15cb3dSCy Schubert #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
18852b15cb3dSCy Schubert /* FIXME: We should move this to regress_main.c if anything else needs it.*/
18862b15cb3dSCy Schubert 
18872b15cb3dSCy Schubert /* Trivial replacements for malloc/free/realloc to check for memory leaks.
18882b15cb3dSCy Schubert  * Not threadsafe. */
18892b15cb3dSCy Schubert static int allocated_chunks = 0;
18902b15cb3dSCy Schubert 
18912b15cb3dSCy Schubert static void *
cnt_malloc(size_t sz)18922b15cb3dSCy Schubert cnt_malloc(size_t sz)
18932b15cb3dSCy Schubert {
18942b15cb3dSCy Schubert 	allocated_chunks += 1;
18952b15cb3dSCy Schubert 	return malloc(sz);
18962b15cb3dSCy Schubert }
18972b15cb3dSCy Schubert 
18982b15cb3dSCy Schubert static void *
cnt_realloc(void * old,size_t sz)18992b15cb3dSCy Schubert cnt_realloc(void *old, size_t sz)
19002b15cb3dSCy Schubert {
19012b15cb3dSCy Schubert 	if (!old)
19022b15cb3dSCy Schubert 		allocated_chunks += 1;
19032b15cb3dSCy Schubert 	if (!sz)
19042b15cb3dSCy Schubert 		allocated_chunks -= 1;
19052b15cb3dSCy Schubert 	return realloc(old, sz);
19062b15cb3dSCy Schubert }
19072b15cb3dSCy Schubert 
19082b15cb3dSCy Schubert static void
cnt_free(void * ptr)19092b15cb3dSCy Schubert cnt_free(void *ptr)
19102b15cb3dSCy Schubert {
19112b15cb3dSCy Schubert 	allocated_chunks -= 1;
19122b15cb3dSCy Schubert 	free(ptr);
19132b15cb3dSCy Schubert }
19142b15cb3dSCy Schubert 
19152b15cb3dSCy Schubert struct testleak_env_t {
19162b15cb3dSCy Schubert 	struct event_base *base;
19172b15cb3dSCy Schubert 	struct evdns_base *dns_base;
19182b15cb3dSCy Schubert 	struct evdns_request *req;
19192b15cb3dSCy Schubert 	struct generic_dns_callback_result r;
19202b15cb3dSCy Schubert };
19212b15cb3dSCy Schubert 
19222b15cb3dSCy Schubert static void *
testleak_setup(const struct testcase_t * testcase)19232b15cb3dSCy Schubert testleak_setup(const struct testcase_t *testcase)
19242b15cb3dSCy Schubert {
19252b15cb3dSCy Schubert 	struct testleak_env_t *env;
19262b15cb3dSCy Schubert 
19272b15cb3dSCy Schubert 	allocated_chunks = 0;
1928a25439b6SCy Schubert 
1929a25439b6SCy Schubert 	/* Reset allocation counter, to start allocations from the very beginning.
1930a25439b6SCy Schubert 	 * (this will avoid false-positive negative numbers for allocated_chunks)
1931a25439b6SCy Schubert 	 */
1932a25439b6SCy Schubert 	libevent_global_shutdown();
1933a25439b6SCy Schubert 
19342b15cb3dSCy Schubert 	event_set_mem_functions(cnt_malloc, cnt_realloc, cnt_free);
1935a25439b6SCy Schubert 
19362b15cb3dSCy Schubert 	event_enable_debug_mode();
19372b15cb3dSCy Schubert 
19382b15cb3dSCy Schubert 	/* not mm_calloc: we don't want to mess with the count. */
19392b15cb3dSCy Schubert 	env = calloc(1, sizeof(struct testleak_env_t));
19402b15cb3dSCy Schubert 	env->base = event_base_new();
19412b15cb3dSCy Schubert 	env->dns_base = evdns_base_new(env->base, 0);
19422b15cb3dSCy Schubert 	env->req = evdns_base_resolve_ipv4(
19432b15cb3dSCy Schubert 		env->dns_base, "example.com", DNS_QUERY_NO_SEARCH,
19442b15cb3dSCy Schubert 		generic_dns_callback, &env->r);
19452b15cb3dSCy Schubert 	return env;
19462b15cb3dSCy Schubert }
19472b15cb3dSCy Schubert 
19482b15cb3dSCy Schubert static int
testleak_cleanup(const struct testcase_t * testcase,void * env_)19492b15cb3dSCy Schubert testleak_cleanup(const struct testcase_t *testcase, void *env_)
19502b15cb3dSCy Schubert {
19512b15cb3dSCy Schubert 	int ok = 0;
19522b15cb3dSCy Schubert 	struct testleak_env_t *env = env_;
19532b15cb3dSCy Schubert 	tt_assert(env);
19542b15cb3dSCy Schubert #ifdef EVENT__DISABLE_DEBUG_MODE
19552b15cb3dSCy Schubert 	tt_int_op(allocated_chunks, ==, 0);
19562b15cb3dSCy Schubert #else
1957a25439b6SCy Schubert 	libevent_global_shutdown();
1958a25439b6SCy Schubert 	tt_int_op(allocated_chunks, ==, 0);
19592b15cb3dSCy Schubert #endif
19602b15cb3dSCy Schubert 	ok = 1;
19612b15cb3dSCy Schubert end:
19622b15cb3dSCy Schubert 	if (env) {
19632b15cb3dSCy Schubert 		if (env->dns_base)
19642b15cb3dSCy Schubert 			evdns_base_free(env->dns_base, 0);
19652b15cb3dSCy Schubert 		if (env->base)
19662b15cb3dSCy Schubert 			event_base_free(env->base);
19672b15cb3dSCy Schubert 		free(env);
19682b15cb3dSCy Schubert 	}
19692b15cb3dSCy Schubert 	return ok;
19702b15cb3dSCy Schubert }
19712b15cb3dSCy Schubert 
19722b15cb3dSCy Schubert static struct testcase_setup_t testleak_funcs = {
19732b15cb3dSCy Schubert 	testleak_setup, testleak_cleanup
19742b15cb3dSCy Schubert };
19752b15cb3dSCy Schubert 
19762b15cb3dSCy Schubert static void
test_dbg_leak_cancel(void * env_)19772b15cb3dSCy Schubert test_dbg_leak_cancel(void *env_)
19782b15cb3dSCy Schubert {
19792b15cb3dSCy Schubert 	/* cancel, loop, free/dns, free/base */
19802b15cb3dSCy Schubert 	struct testleak_env_t *env = env_;
19812b15cb3dSCy Schubert 	int send_err_shutdown = 1;
19822b15cb3dSCy Schubert 	evdns_cancel_request(env->dns_base, env->req);
19832b15cb3dSCy Schubert 	env->req = 0;
19842b15cb3dSCy Schubert 
19852b15cb3dSCy Schubert 	/* `req` is freed in callback, that's why one loop is required. */
19862b15cb3dSCy Schubert 	event_base_loop(env->base, EVLOOP_NONBLOCK);
19872b15cb3dSCy Schubert 
19882b15cb3dSCy Schubert 	/* send_err_shutdown means nothing as soon as our request is
19892b15cb3dSCy Schubert 	 * already canceled */
19902b15cb3dSCy Schubert 	evdns_base_free(env->dns_base, send_err_shutdown);
19912b15cb3dSCy Schubert 	env->dns_base = 0;
19922b15cb3dSCy Schubert 	event_base_free(env->base);
19932b15cb3dSCy Schubert 	env->base = 0;
19942b15cb3dSCy Schubert }
19952b15cb3dSCy Schubert 
19962b15cb3dSCy Schubert static void
dbg_leak_resume(void * env_,int cancel,int send_err_shutdown)19972b15cb3dSCy Schubert dbg_leak_resume(void *env_, int cancel, int send_err_shutdown)
19982b15cb3dSCy Schubert {
19992b15cb3dSCy Schubert 	/* cancel, loop, free/dns, free/base */
20002b15cb3dSCy Schubert 	struct testleak_env_t *env = env_;
20012b15cb3dSCy Schubert 	if (cancel) {
20022b15cb3dSCy Schubert 		evdns_cancel_request(env->dns_base, env->req);
20032b15cb3dSCy Schubert 		tt_assert(!evdns_base_resume(env->dns_base));
20042b15cb3dSCy Schubert 	} else {
20052b15cb3dSCy Schubert 		/* TODO: No nameservers, request can't be processed, must be errored */
20062b15cb3dSCy Schubert 		tt_assert(!evdns_base_resume(env->dns_base));
20072b15cb3dSCy Schubert 	}
20082b15cb3dSCy Schubert 
2009*a466cc55SCy Schubert 	event_base_loop(env->base, EVLOOP_NONBLOCK);
20102b15cb3dSCy Schubert 	/**
2011*a466cc55SCy Schubert 	 * Because we don't cancel request, and want our callback to recieve
2012*a466cc55SCy Schubert 	 * DNS_ERR_SHUTDOWN, we use deferred callback, and there was:
20132b15cb3dSCy Schubert 	 * - one extra malloc(),
20142b15cb3dSCy Schubert 	 *   @see reply_schedule_callback()
20152b15cb3dSCy Schubert 	 * - and one missing free
20162b15cb3dSCy Schubert 	 *   @see request_finished() (req->handle->pending_cb = 1)
2017*a466cc55SCy Schubert 	 * than we don't need to count in testleak_cleanup(), but we can clean them
2018*a466cc55SCy Schubert 	 * if we will run loop once again, but *after* evdns base freed.
20192b15cb3dSCy Schubert 	 */
2020*a466cc55SCy Schubert 	evdns_base_free(env->dns_base, send_err_shutdown);
2021*a466cc55SCy Schubert 	env->dns_base = 0;
20222b15cb3dSCy Schubert 	event_base_loop(env->base, EVLOOP_NONBLOCK);
20232b15cb3dSCy Schubert 
20242b15cb3dSCy Schubert end:
20252b15cb3dSCy Schubert 	event_base_free(env->base);
20262b15cb3dSCy Schubert 	env->base = 0;
20272b15cb3dSCy Schubert }
20282b15cb3dSCy Schubert 
20292b15cb3dSCy Schubert #define IMPL_DBG_LEAK_RESUME(name, cancel, send_err_shutdown)      \
20302b15cb3dSCy Schubert 	static void                                                    \
20312b15cb3dSCy Schubert 	test_dbg_leak_##name##_(void *env_)                            \
20322b15cb3dSCy Schubert 	{                                                              \
20332b15cb3dSCy Schubert 		dbg_leak_resume(env_, cancel, send_err_shutdown);          \
20342b15cb3dSCy Schubert 	}
20352b15cb3dSCy Schubert IMPL_DBG_LEAK_RESUME(resume, 0, 0)
20362b15cb3dSCy Schubert IMPL_DBG_LEAK_RESUME(cancel_and_resume, 1, 0)
20372b15cb3dSCy Schubert IMPL_DBG_LEAK_RESUME(resume_send_err, 0, 1)
20382b15cb3dSCy Schubert IMPL_DBG_LEAK_RESUME(cancel_and_resume_send_err, 1, 1)
20392b15cb3dSCy Schubert 
20402b15cb3dSCy Schubert static void
test_dbg_leak_shutdown(void * env_)20412b15cb3dSCy Schubert test_dbg_leak_shutdown(void *env_)
20422b15cb3dSCy Schubert {
20432b15cb3dSCy Schubert 	/* free/dns, loop, free/base */
20442b15cb3dSCy Schubert 	struct testleak_env_t *env = env_;
20452b15cb3dSCy Schubert 	int send_err_shutdown = 1;
20462b15cb3dSCy Schubert 
20472b15cb3dSCy Schubert 	/* `req` is freed both with `send_err_shutdown` and without it,
20482b15cb3dSCy Schubert 	 * the only difference is `evdns_callback` call */
20492b15cb3dSCy Schubert 	env->req = 0;
20502b15cb3dSCy Schubert 
20512b15cb3dSCy Schubert 	evdns_base_free(env->dns_base, send_err_shutdown);
20522b15cb3dSCy Schubert 	env->dns_base = 0;
20532b15cb3dSCy Schubert 
20542b15cb3dSCy Schubert 	/* `req` is freed in callback, that's why one loop is required */
20552b15cb3dSCy Schubert 	event_base_loop(env->base, EVLOOP_NONBLOCK);
20562b15cb3dSCy Schubert 	event_base_free(env->base);
20572b15cb3dSCy Schubert 	env->base = 0;
20582b15cb3dSCy Schubert }
20592b15cb3dSCy Schubert #endif
20602b15cb3dSCy Schubert 
20612b15cb3dSCy Schubert static void
test_getaddrinfo_async_cancel_stress(void * ptr)20622b15cb3dSCy Schubert test_getaddrinfo_async_cancel_stress(void *ptr)
20632b15cb3dSCy Schubert {
20642b15cb3dSCy Schubert 	struct event_base *base;
20652b15cb3dSCy Schubert 	struct evdns_base *dns_base = NULL;
20662b15cb3dSCy Schubert 	struct evdns_server_port *server = NULL;
20672b15cb3dSCy Schubert 	evutil_socket_t fd = -1;
20682b15cb3dSCy Schubert 	struct sockaddr_in sin;
20692b15cb3dSCy Schubert 	struct sockaddr_storage ss;
20702b15cb3dSCy Schubert 	ev_socklen_t slen;
2071*a466cc55SCy Schubert 	unsigned i;
20722b15cb3dSCy Schubert 
20732b15cb3dSCy Schubert 	base = event_base_new();
20742b15cb3dSCy Schubert 	dns_base = evdns_base_new(base, 0);
20752b15cb3dSCy Schubert 
20762b15cb3dSCy Schubert 	memset(&sin, 0, sizeof(sin));
20772b15cb3dSCy Schubert 	sin.sin_family = AF_INET;
20782b15cb3dSCy Schubert 	sin.sin_port = 0;
20792b15cb3dSCy Schubert 	sin.sin_addr.s_addr = htonl(0x7f000001);
20802b15cb3dSCy Schubert 	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
20812b15cb3dSCy Schubert 		tt_abort_perror("socket");
20822b15cb3dSCy Schubert 	}
20832b15cb3dSCy Schubert 	evutil_make_socket_nonblocking(fd);
20842b15cb3dSCy Schubert 	if (bind(fd, (struct sockaddr*)&sin, sizeof(sin))<0) {
20852b15cb3dSCy Schubert 		tt_abort_perror("bind");
20862b15cb3dSCy Schubert 	}
20872b15cb3dSCy Schubert 	server = evdns_add_server_port_with_base(base, fd, 0, gaic_server_cb,
20882b15cb3dSCy Schubert 	    base);
20892b15cb3dSCy Schubert 
20902b15cb3dSCy Schubert 	memset(&ss, 0, sizeof(ss));
20912b15cb3dSCy Schubert 	slen = sizeof(ss);
20922b15cb3dSCy Schubert 	if (getsockname(fd, (struct sockaddr*)&ss, &slen)<0) {
20932b15cb3dSCy Schubert 		tt_abort_perror("getsockname");
20942b15cb3dSCy Schubert 	}
20952b15cb3dSCy Schubert 	evdns_base_nameserver_sockaddr_add(dns_base,
20962b15cb3dSCy Schubert 	    (struct sockaddr*)&ss, slen, 0);
20972b15cb3dSCy Schubert 
20982b15cb3dSCy Schubert 	for (i = 0; i < 1000; ++i) {
20992b15cb3dSCy Schubert 		gaic_launch(base, dns_base);
21002b15cb3dSCy Schubert 	}
21012b15cb3dSCy Schubert 
21022b15cb3dSCy Schubert 	event_base_dispatch(base);
21032b15cb3dSCy Schubert 
2104*a466cc55SCy Schubert 	// at least some was canceled via external event
2105*a466cc55SCy Schubert 	tt_int_op(gaic_freed, !=, 1000);
2106*a466cc55SCy Schubert 
21072b15cb3dSCy Schubert end:
21082b15cb3dSCy Schubert 	if (dns_base)
21092b15cb3dSCy Schubert 		evdns_base_free(dns_base, 1);
21102b15cb3dSCy Schubert 	if (server)
21112b15cb3dSCy Schubert 		evdns_close_server_port(server);
2112a25439b6SCy Schubert 	if (base)
2113a25439b6SCy Schubert 		event_base_free(base);
21142b15cb3dSCy Schubert 	if (fd >= 0)
21152b15cb3dSCy Schubert 		evutil_closesocket(fd);
21162b15cb3dSCy Schubert }
21172b15cb3dSCy Schubert 
2118*a466cc55SCy Schubert static void
dns_client_fail_requests_test(void * arg)2119*a466cc55SCy Schubert dns_client_fail_requests_test(void *arg)
2120*a466cc55SCy Schubert {
2121*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
2122*a466cc55SCy Schubert 	struct event_base *base = data->base;
2123*a466cc55SCy Schubert 	int limit_inflight = data->setup_data && !strcmp(data->setup_data, "limit-inflight");
2124*a466cc55SCy Schubert 	struct evdns_base *dns = NULL;
2125*a466cc55SCy Schubert 	struct evdns_server_port *dns_port = NULL;
2126*a466cc55SCy Schubert 	ev_uint16_t portnum = 0;
2127*a466cc55SCy Schubert 	char buf[64];
2128*a466cc55SCy Schubert 
2129*a466cc55SCy Schubert 	struct generic_dns_callback_result r[20];
2130*a466cc55SCy Schubert 	unsigned i;
2131*a466cc55SCy Schubert 
2132*a466cc55SCy Schubert 	dns_port = regress_get_dnsserver(base, &portnum, NULL,
2133*a466cc55SCy Schubert 		regress_dns_server_cb, reissue_table);
2134*a466cc55SCy Schubert 	tt_assert(dns_port);
2135*a466cc55SCy Schubert 
2136*a466cc55SCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
2137*a466cc55SCy Schubert 
2138*a466cc55SCy Schubert 	dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
2139*a466cc55SCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
2140*a466cc55SCy Schubert 
2141*a466cc55SCy Schubert 	if (limit_inflight)
2142*a466cc55SCy Schubert 		tt_assert(!evdns_base_set_option(dns, "max-inflight:", "11"));
2143*a466cc55SCy Schubert 
2144*a466cc55SCy Schubert 	for (i = 0; i < 20; ++i)
2145*a466cc55SCy Schubert 		evdns_base_resolve_ipv4(dns, "foof.example.com", 0, generic_dns_callback, &r[i]);
2146*a466cc55SCy Schubert 
2147*a466cc55SCy Schubert 	n_replies_left = 20;
2148*a466cc55SCy Schubert 	exit_base = base;
2149*a466cc55SCy Schubert 
2150*a466cc55SCy Schubert 	evdns_base_free(dns, 1 /** fail requests */);
2151*a466cc55SCy Schubert 	/** run defered callbacks, to trigger UAF */
2152*a466cc55SCy Schubert 	event_base_dispatch(base);
2153*a466cc55SCy Schubert 
2154*a466cc55SCy Schubert 	tt_int_op(n_replies_left, ==, 0);
2155*a466cc55SCy Schubert 	for (i = 0; i < 20; ++i)
2156*a466cc55SCy Schubert 		tt_int_op(r[i].result, ==, DNS_ERR_SHUTDOWN);
2157*a466cc55SCy Schubert 
2158*a466cc55SCy Schubert end:
2159*a466cc55SCy Schubert 	evdns_close_server_port(dns_port);
2160*a466cc55SCy Schubert }
2161*a466cc55SCy Schubert 
2162*a466cc55SCy Schubert static void
getaddrinfo_cb(int err,struct evutil_addrinfo * res,void * ptr)2163*a466cc55SCy Schubert getaddrinfo_cb(int err, struct evutil_addrinfo *res, void *ptr)
2164*a466cc55SCy Schubert {
2165*a466cc55SCy Schubert 	generic_dns_callback(err, 0, 0, 0, NULL, ptr);
2166*a466cc55SCy Schubert }
2167*a466cc55SCy Schubert static void
dns_client_fail_requests_getaddrinfo_test(void * arg)2168*a466cc55SCy Schubert dns_client_fail_requests_getaddrinfo_test(void *arg)
2169*a466cc55SCy Schubert {
2170*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
2171*a466cc55SCy Schubert 	struct event_base *base = data->base;
2172*a466cc55SCy Schubert 	struct evdns_base *dns = NULL;
2173*a466cc55SCy Schubert 	struct evdns_server_port *dns_port = NULL;
2174*a466cc55SCy Schubert 	ev_uint16_t portnum = 0;
2175*a466cc55SCy Schubert 	char buf[64];
2176*a466cc55SCy Schubert 
2177*a466cc55SCy Schubert 	struct generic_dns_callback_result r[20];
2178*a466cc55SCy Schubert 	int i;
2179*a466cc55SCy Schubert 
2180*a466cc55SCy Schubert 	dns_port = regress_get_dnsserver(base, &portnum, NULL,
2181*a466cc55SCy Schubert 		regress_dns_server_cb, reissue_table);
2182*a466cc55SCy Schubert 	tt_assert(dns_port);
2183*a466cc55SCy Schubert 
2184*a466cc55SCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
2185*a466cc55SCy Schubert 
2186*a466cc55SCy Schubert 	dns = evdns_base_new(base, EVDNS_BASE_DISABLE_WHEN_INACTIVE);
2187*a466cc55SCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
2188*a466cc55SCy Schubert 
2189*a466cc55SCy Schubert 	for (i = 0; i < 20; ++i)
2190*a466cc55SCy Schubert 		tt_assert(evdns_getaddrinfo(dns, "foof.example.com", "80", NULL, getaddrinfo_cb, &r[i]));
2191*a466cc55SCy Schubert 
2192*a466cc55SCy Schubert 	n_replies_left = 20;
2193*a466cc55SCy Schubert 	exit_base = base;
2194*a466cc55SCy Schubert 
2195*a466cc55SCy Schubert 	evdns_base_free(dns, 1 /** fail requests */);
2196*a466cc55SCy Schubert 	/** run defered callbacks, to trigger UAF */
2197*a466cc55SCy Schubert 	event_base_dispatch(base);
2198*a466cc55SCy Schubert 
2199*a466cc55SCy Schubert 	tt_int_op(n_replies_left, ==, 0);
2200*a466cc55SCy Schubert 	for (i = 0; i < 20; ++i)
2201*a466cc55SCy Schubert 		tt_int_op(r[i].result, ==, EVUTIL_EAI_FAIL);
2202*a466cc55SCy Schubert 
2203*a466cc55SCy Schubert end:
2204*a466cc55SCy Schubert 	evdns_close_server_port(dns_port);
2205*a466cc55SCy Schubert }
2206*a466cc55SCy Schubert 
2207*a466cc55SCy Schubert #ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED
2208*a466cc55SCy Schubert struct race_param
2209*a466cc55SCy Schubert {
2210*a466cc55SCy Schubert 	void *lock;
2211*a466cc55SCy Schubert 	void *reqs_cmpl_cond;
2212*a466cc55SCy Schubert 	int bw_threads;
2213*a466cc55SCy Schubert 	void *bw_threads_exited_cond;
2214*a466cc55SCy Schubert 	volatile int stopping;
2215*a466cc55SCy Schubert 	void *base;
2216*a466cc55SCy Schubert 	void *dns;
2217*a466cc55SCy Schubert 
2218*a466cc55SCy Schubert 	int locked;
2219*a466cc55SCy Schubert };
2220*a466cc55SCy Schubert static void *
race_base_run(void * arg)2221*a466cc55SCy Schubert race_base_run(void *arg)
2222*a466cc55SCy Schubert {
2223*a466cc55SCy Schubert 	struct race_param *rp = (struct race_param *)arg;
2224*a466cc55SCy Schubert 	event_base_loop(rp->base, EVLOOP_NO_EXIT_ON_EMPTY);
2225*a466cc55SCy Schubert 	THREAD_RETURN();
2226*a466cc55SCy Schubert }
2227*a466cc55SCy Schubert static void *
race_busywait_run(void * arg)2228*a466cc55SCy Schubert race_busywait_run(void *arg)
2229*a466cc55SCy Schubert {
2230*a466cc55SCy Schubert 	struct race_param *rp = (struct race_param *)arg;
2231*a466cc55SCy Schubert 	struct sockaddr_storage ss;
2232*a466cc55SCy Schubert 	while (!rp->stopping)
2233*a466cc55SCy Schubert 		evdns_base_get_nameserver_addr(rp->dns, 0, (struct sockaddr *)&ss, sizeof(ss));
2234*a466cc55SCy Schubert 	EVLOCK_LOCK(rp->lock, 0);
2235*a466cc55SCy Schubert 	if (--rp->bw_threads == 0)
2236*a466cc55SCy Schubert 		EVTHREAD_COND_SIGNAL(rp->bw_threads_exited_cond);
2237*a466cc55SCy Schubert 	EVLOCK_UNLOCK(rp->lock, 0);
2238*a466cc55SCy Schubert 	THREAD_RETURN();
2239*a466cc55SCy Schubert }
2240*a466cc55SCy Schubert static void
race_gai_cb(int result,struct evutil_addrinfo * res,void * arg)2241*a466cc55SCy Schubert race_gai_cb(int result, struct evutil_addrinfo *res, void *arg)
2242*a466cc55SCy Schubert {
2243*a466cc55SCy Schubert 	struct race_param *rp = arg;
2244*a466cc55SCy Schubert 	(void)result;
2245*a466cc55SCy Schubert 	(void)res;
2246*a466cc55SCy Schubert 
2247*a466cc55SCy Schubert 	--n_replies_left;
2248*a466cc55SCy Schubert 	if (n_replies_left == 0) {
2249*a466cc55SCy Schubert 		EVLOCK_LOCK(rp->lock, 0);
2250*a466cc55SCy Schubert 		EVTHREAD_COND_SIGNAL(rp->reqs_cmpl_cond);
2251*a466cc55SCy Schubert 		EVLOCK_UNLOCK(rp->lock, 0);
2252*a466cc55SCy Schubert 	}
2253*a466cc55SCy Schubert }
2254*a466cc55SCy Schubert static void
getaddrinfo_race_gotresolve_test(void * arg)2255*a466cc55SCy Schubert getaddrinfo_race_gotresolve_test(void *arg)
2256*a466cc55SCy Schubert {
2257*a466cc55SCy Schubert 	struct race_param rp;
2258*a466cc55SCy Schubert 	struct evdns_server_port *dns_port = NULL;
2259*a466cc55SCy Schubert 	ev_uint16_t portnum = 0;
2260*a466cc55SCy Schubert 	char buf[64];
2261*a466cc55SCy Schubert 	int i;
2262*a466cc55SCy Schubert 
2263*a466cc55SCy Schubert 	// Some stress is needed to yield inside getaddrinfo between resolve_ipv4 and resolve_ipv6
2264*a466cc55SCy Schubert 	int n_reqs = 16384;
2265*a466cc55SCy Schubert #ifdef _SC_NPROCESSORS_ONLN
2266*a466cc55SCy Schubert 	int n_threads = sysconf(_SC_NPROCESSORS_ONLN) + 1;
2267*a466cc55SCy Schubert #else
2268*a466cc55SCy Schubert 	int n_threads = 17;
2269*a466cc55SCy Schubert #endif
2270*a466cc55SCy Schubert 	THREAD_T thread[n_threads];
2271*a466cc55SCy Schubert 	struct timeval tv;
2272*a466cc55SCy Schubert 
2273*a466cc55SCy Schubert 	(void)arg;
2274*a466cc55SCy Schubert 
2275*a466cc55SCy Schubert 	evthread_use_pthreads();
2276*a466cc55SCy Schubert 
2277*a466cc55SCy Schubert 	rp.base = event_base_new();
2278*a466cc55SCy Schubert 	tt_assert(rp.base);
2279*a466cc55SCy Schubert 	if (evthread_make_base_notifiable(rp.base) < 0)
2280*a466cc55SCy Schubert 		tt_abort_msg("Couldn't make base notifiable!");
2281*a466cc55SCy Schubert 
2282*a466cc55SCy Schubert 	dns_port = regress_get_dnsserver(rp.base, &portnum, NULL,
2283*a466cc55SCy Schubert 									 regress_dns_server_cb, reissue_table);
2284*a466cc55SCy Schubert 	tt_assert(dns_port);
2285*a466cc55SCy Schubert 
2286*a466cc55SCy Schubert 	evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
2287*a466cc55SCy Schubert 
2288*a466cc55SCy Schubert 	rp.dns = evdns_base_new(rp.base, 0);
2289*a466cc55SCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(rp.dns, buf));
2290*a466cc55SCy Schubert 
2291*a466cc55SCy Schubert 	n_replies_left = n_reqs;
2292*a466cc55SCy Schubert 
2293*a466cc55SCy Schubert 	EVTHREAD_ALLOC_LOCK(rp.lock, 0);
2294*a466cc55SCy Schubert 	EVTHREAD_ALLOC_COND(rp.reqs_cmpl_cond);
2295*a466cc55SCy Schubert 	EVTHREAD_ALLOC_COND(rp.bw_threads_exited_cond);
2296*a466cc55SCy Schubert 	tt_assert(rp.lock);
2297*a466cc55SCy Schubert 	tt_assert(rp.reqs_cmpl_cond);
2298*a466cc55SCy Schubert 	tt_assert(rp.bw_threads_exited_cond);
2299*a466cc55SCy Schubert 	rp.bw_threads = 0;
2300*a466cc55SCy Schubert 	rp.stopping = 0;
2301*a466cc55SCy Schubert 
2302*a466cc55SCy Schubert 	// Run resolver thread
2303*a466cc55SCy Schubert 	THREAD_START(thread[0], race_base_run, &rp);
2304*a466cc55SCy Schubert 	// Run busy-wait threads used to force yield this thread
2305*a466cc55SCy Schubert 	for (i = 1; i < n_threads; i++) {
2306*a466cc55SCy Schubert 		rp.bw_threads++;
2307*a466cc55SCy Schubert 		THREAD_START(thread[i], race_busywait_run, &rp);
2308*a466cc55SCy Schubert 	}
2309*a466cc55SCy Schubert 
2310*a466cc55SCy Schubert 	EVLOCK_LOCK(rp.lock, 0);
2311*a466cc55SCy Schubert 	rp.locked = 1;
2312*a466cc55SCy Schubert 
2313*a466cc55SCy Schubert 	for (i = 0; i < n_reqs; ++i) {
2314*a466cc55SCy Schubert 		tt_assert(evdns_getaddrinfo(rp.dns, "foof.example.com", "80", NULL, race_gai_cb, &rp));
2315*a466cc55SCy Schubert 		// This magic along with busy-wait threads make this thread yield frequently
2316*a466cc55SCy Schubert 		if (i % 100 == 0) {
2317*a466cc55SCy Schubert 			tv.tv_sec = 0;
2318*a466cc55SCy Schubert 			tv.tv_usec = 10000;
2319*a466cc55SCy Schubert 			evutil_usleep_(&tv);
2320*a466cc55SCy Schubert 		}
2321*a466cc55SCy Schubert 	}
2322*a466cc55SCy Schubert 
2323*a466cc55SCy Schubert 	exit_base = rp.base;
2324*a466cc55SCy Schubert 
2325*a466cc55SCy Schubert 	// Wait for some time
2326*a466cc55SCy Schubert 	tv.tv_sec = 5;
2327*a466cc55SCy Schubert 	tv.tv_usec = 0;
2328*a466cc55SCy Schubert 	EVTHREAD_COND_WAIT_TIMED(rp.reqs_cmpl_cond, rp.lock, &tv);
2329*a466cc55SCy Schubert 
2330*a466cc55SCy Schubert 	// Stop busy-wait threads
2331*a466cc55SCy Schubert 	tv.tv_sec = 1;
2332*a466cc55SCy Schubert 	tv.tv_usec = 0;
2333*a466cc55SCy Schubert 	rp.stopping = 1;
2334*a466cc55SCy Schubert 	tt_assert(EVTHREAD_COND_WAIT_TIMED(rp.bw_threads_exited_cond, rp.lock, &tv) == 0);
2335*a466cc55SCy Schubert 
2336*a466cc55SCy Schubert 	EVLOCK_UNLOCK(rp.lock, 0);
2337*a466cc55SCy Schubert 	rp.locked = 0;
2338*a466cc55SCy Schubert 
2339*a466cc55SCy Schubert 	evdns_base_free(rp.dns, 1 /** fail requests */);
2340*a466cc55SCy Schubert 
2341*a466cc55SCy Schubert 	tt_int_op(n_replies_left, ==, 0);
2342*a466cc55SCy Schubert 
2343*a466cc55SCy Schubert end:
2344*a466cc55SCy Schubert 	if (rp.locked)
2345*a466cc55SCy Schubert 		EVLOCK_UNLOCK(rp.lock, 0);
2346*a466cc55SCy Schubert 	EVTHREAD_FREE_LOCK(rp.lock, 0);
2347*a466cc55SCy Schubert 	EVTHREAD_FREE_COND(rp.reqs_cmpl_cond);
2348*a466cc55SCy Schubert 	EVTHREAD_FREE_COND(rp.bw_threads_exited_cond);
2349*a466cc55SCy Schubert 	evdns_close_server_port(dns_port);
2350*a466cc55SCy Schubert 	event_base_loopbreak(rp.base);
2351*a466cc55SCy Schubert 	event_base_free(rp.base);
2352*a466cc55SCy Schubert }
2353*a466cc55SCy Schubert #endif
2354*a466cc55SCy Schubert 
2355*a466cc55SCy Schubert static void
test_set_so_rcvbuf_so_sndbuf(void * arg)2356*a466cc55SCy Schubert test_set_so_rcvbuf_so_sndbuf(void *arg)
2357*a466cc55SCy Schubert {
2358*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
2359*a466cc55SCy Schubert 	struct evdns_base *dns_base;
2360*a466cc55SCy Schubert 
2361*a466cc55SCy Schubert 	dns_base = evdns_base_new(data->base, 0);
2362*a466cc55SCy Schubert 	tt_assert(dns_base);
2363*a466cc55SCy Schubert 
2364*a466cc55SCy Schubert 	tt_assert(!evdns_base_set_option(dns_base, "so-rcvbuf", "10240"));
2365*a466cc55SCy Schubert 	tt_assert(!evdns_base_set_option(dns_base, "so-sndbuf", "10240"));
2366*a466cc55SCy Schubert 
2367*a466cc55SCy Schubert 	/* actually check SO_RCVBUF/SO_SNDBUF not fails */
2368*a466cc55SCy Schubert 	tt_assert(!evdns_base_nameserver_ip_add(dns_base, "127.0.0.1"));
2369*a466cc55SCy Schubert 
2370*a466cc55SCy Schubert end:
2371*a466cc55SCy Schubert 	if (dns_base)
2372*a466cc55SCy Schubert 		evdns_base_free(dns_base, 0);
2373*a466cc55SCy Schubert }
2374*a466cc55SCy Schubert 
2375*a466cc55SCy Schubert static void
test_set_option(void * arg)2376*a466cc55SCy Schubert test_set_option(void *arg)
2377*a466cc55SCy Schubert {
2378*a466cc55SCy Schubert #define SUCCESS 0
2379*a466cc55SCy Schubert #define FAIL -1
2380*a466cc55SCy Schubert 	struct basic_test_data *data = arg;
2381*a466cc55SCy Schubert 	struct evdns_base *dns_base;
2382*a466cc55SCy Schubert 	size_t i;
2383*a466cc55SCy Schubert 	/* Option names are allowed to have ':' at the end.
2384*a466cc55SCy Schubert 	 * So all test option names come in pairs.
2385*a466cc55SCy Schubert 	 */
2386*a466cc55SCy Schubert 	const char *int_options[] = {
2387*a466cc55SCy Schubert 		"ndots", "ndots:",
2388*a466cc55SCy Schubert 		"max-timeouts", "max-timeouts:",
2389*a466cc55SCy Schubert 		"max-inflight", "max-inflight:",
2390*a466cc55SCy Schubert 		"attempts", "attempts:",
2391*a466cc55SCy Schubert 		"randomize-case", "randomize-case:",
2392*a466cc55SCy Schubert 		"so-rcvbuf", "so-rcvbuf:",
2393*a466cc55SCy Schubert 		"so-sndbuf", "so-sndbuf:",
2394*a466cc55SCy Schubert 	};
2395*a466cc55SCy Schubert 	const char *timeval_options[] = {
2396*a466cc55SCy Schubert 		"timeout", "timeout:",
2397*a466cc55SCy Schubert 		"getaddrinfo-allow-skew", "getaddrinfo-allow-skew:",
2398*a466cc55SCy Schubert 		"initial-probe-timeout", "initial-probe-timeout:",
2399*a466cc55SCy Schubert 	};
2400*a466cc55SCy Schubert 	const char *addr_port_options[] = {
2401*a466cc55SCy Schubert 		"bind-to", "bind-to:",
2402*a466cc55SCy Schubert 	};
2403*a466cc55SCy Schubert 
2404*a466cc55SCy Schubert 	dns_base = evdns_base_new(data->base, 0);
2405*a466cc55SCy Schubert 	tt_assert(dns_base);
2406*a466cc55SCy Schubert 
2407*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(int_options); ++i) {
2408*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "0"));
2409*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "1"));
2410*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, int_options[i], "10000"));
2411*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, int_options[i], "foo"));
2412*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, int_options[i], "3.14"));
2413*a466cc55SCy Schubert 	}
2414*a466cc55SCy Schubert 
2415*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(timeval_options); ++i) {
2416*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "1"));
2417*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "0.001"));
2418*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "3.14"));
2419*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, timeval_options[i], "10000"));
2420*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, timeval_options[i], "0"));
2421*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, timeval_options[i], "foo"));
2422*a466cc55SCy Schubert 	}
2423*a466cc55SCy Schubert 
2424*a466cc55SCy Schubert 	for (i = 0; i < ARRAY_SIZE(addr_port_options); ++i) {
2425*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "8.8.8.8:80"));
2426*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "1.2.3.4"));
2427*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "::1:82"));
2428*a466cc55SCy Schubert 		tt_assert(SUCCESS == evdns_base_set_option(dns_base, addr_port_options[i], "3::4"));
2429*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, addr_port_options[i], "3.14"));
2430*a466cc55SCy Schubert 		tt_assert(FAIL == evdns_base_set_option(dns_base, addr_port_options[i], "foo"));
2431*a466cc55SCy Schubert 	}
2432*a466cc55SCy Schubert 
2433*a466cc55SCy Schubert #undef SUCCESS
2434*a466cc55SCy Schubert #undef FAIL
2435*a466cc55SCy Schubert end:
2436*a466cc55SCy Schubert 	if (dns_base)
2437*a466cc55SCy Schubert 		evdns_base_free(dns_base, 0);
2438*a466cc55SCy Schubert }
24392b15cb3dSCy Schubert 
24402b15cb3dSCy Schubert #define DNS_LEGACY(name, flags)					       \
24412b15cb3dSCy Schubert 	{ #name, run_legacy_test_fn, flags|TT_LEGACY, &legacy_setup,   \
24422b15cb3dSCy Schubert 		    dns_##name }
24432b15cb3dSCy Schubert 
24442b15cb3dSCy Schubert struct testcase_t dns_testcases[] = {
24452b15cb3dSCy Schubert 	DNS_LEGACY(server, TT_FORK|TT_NEED_BASE),
24462b15cb3dSCy Schubert 	DNS_LEGACY(gethostbyname, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
24472b15cb3dSCy Schubert 	DNS_LEGACY(gethostbyname6, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
24482b15cb3dSCy Schubert 	DNS_LEGACY(gethostbyaddr, TT_FORK|TT_NEED_BASE|TT_NEED_DNS|TT_OFF_BY_DEFAULT),
24492b15cb3dSCy Schubert 	{ "resolve_reverse", dns_resolve_reverse, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
2450*a466cc55SCy Schubert 	{ "search_empty", dns_search_empty_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
24512b15cb3dSCy Schubert 	{ "search", dns_search_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2452*a466cc55SCy Schubert 	{ "search_lower", dns_search_lower_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
24532b15cb3dSCy Schubert 	{ "search_cancel", dns_search_cancel_test,
24542b15cb3dSCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
24552b15cb3dSCy Schubert 	{ "retry", dns_retry_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2456a25439b6SCy Schubert 	{ "retry_disable_when_inactive", dns_retry_disable_when_inactive_test,
2457a25439b6SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
24582b15cb3dSCy Schubert 	{ "reissue", dns_reissue_test, TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2459a25439b6SCy Schubert 	{ "reissue_disable_when_inactive", dns_reissue_disable_when_inactive_test,
2460a25439b6SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
24612b15cb3dSCy Schubert 	{ "inflight", dns_inflight_test, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
24622b15cb3dSCy Schubert 	{ "bufferevent_connect_hostname", test_bufferevent_connect_hostname,
24632b15cb3dSCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2464*a466cc55SCy Schubert #ifdef EVENT__HAVE_SETRLIMIT
2465*a466cc55SCy Schubert 	{ "bufferevent_connect_hostname_emfile", test_bufferevent_connect_hostname,
2466*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"emfile" },
2467*a466cc55SCy Schubert #endif
2468a25439b6SCy Schubert 	{ "disable_when_inactive", dns_disable_when_inactive_test,
2469a25439b6SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2470a25439b6SCy Schubert 	{ "disable_when_inactive_no_ns", dns_disable_when_inactive_no_ns_test,
2471*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2472*a466cc55SCy Schubert 
2473*a466cc55SCy Schubert 	{ "initialize_nameservers", dns_initialize_nameservers_test,
2474a25439b6SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2475*a466cc55SCy Schubert #ifndef _WIN32
2476*a466cc55SCy Schubert 	{ "nameservers_no_default", dns_nameservers_no_default_test,
2477*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2478*a466cc55SCy Schubert #endif
24792b15cb3dSCy Schubert 
24802b15cb3dSCy Schubert 	{ "getaddrinfo_async", test_getaddrinfo_async,
24812b15cb3dSCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, (char*)"" },
24822b15cb3dSCy Schubert 	{ "getaddrinfo_cancel_stress", test_getaddrinfo_async_cancel_stress,
24832b15cb3dSCy Schubert 	  TT_FORK, NULL, NULL },
24842b15cb3dSCy Schubert 
24852b15cb3dSCy Schubert #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
24862b15cb3dSCy Schubert 	{ "leak_shutdown", test_dbg_leak_shutdown, TT_FORK, &testleak_funcs, NULL },
24872b15cb3dSCy Schubert 	{ "leak_cancel", test_dbg_leak_cancel, TT_FORK, &testleak_funcs, NULL },
24882b15cb3dSCy Schubert 
24892b15cb3dSCy Schubert 	{ "leak_resume", test_dbg_leak_resume_, TT_FORK, &testleak_funcs, NULL },
24902b15cb3dSCy Schubert 	{ "leak_cancel_and_resume", test_dbg_leak_cancel_and_resume_,
24912b15cb3dSCy Schubert 	  TT_FORK, &testleak_funcs, NULL },
24922b15cb3dSCy Schubert 	{ "leak_resume_send_err", test_dbg_leak_resume_send_err_,
24932b15cb3dSCy Schubert 	  TT_FORK, &testleak_funcs, NULL },
24942b15cb3dSCy Schubert 	{ "leak_cancel_and_resume_send_err", test_dbg_leak_cancel_and_resume_send_err_,
24952b15cb3dSCy Schubert 	  TT_FORK, &testleak_funcs, NULL },
24962b15cb3dSCy Schubert #endif
24972b15cb3dSCy Schubert 
2498*a466cc55SCy Schubert 	{ "client_fail_requests", dns_client_fail_requests_test,
2499*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2500*a466cc55SCy Schubert 	{ "client_fail_waiting_requests", dns_client_fail_requests_test,
2501*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, (char*)"limit-inflight" },
2502*a466cc55SCy Schubert 	{ "client_fail_requests_getaddrinfo",
2503*a466cc55SCy Schubert 	  dns_client_fail_requests_getaddrinfo_test,
2504*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE|TT_NO_LOGS, &basic_setup, NULL },
2505*a466cc55SCy Schubert #ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED
2506*a466cc55SCy Schubert 	{ "getaddrinfo_race_gotresolve",
2507*a466cc55SCy Schubert 	  getaddrinfo_race_gotresolve_test,
2508*a466cc55SCy Schubert 	  TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
2509*a466cc55SCy Schubert #endif
2510*a466cc55SCy Schubert 
2511*a466cc55SCy Schubert 	{ "set_SO_RCVBUF_SO_SNDBUF", test_set_so_rcvbuf_so_sndbuf,
2512*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2513*a466cc55SCy Schubert 	{ "set_options", test_set_option,
2514*a466cc55SCy Schubert 	  TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
2515*a466cc55SCy Schubert 
25162b15cb3dSCy Schubert 	END_OF_TESTCASES
25172b15cb3dSCy Schubert };
25182b15cb3dSCy Schubert 
2519