xref: /netbsd-src/external/bsd/libevent/dist/test/regress_testutils.c (revision 657871a79c9a2060a6255a242fa1a1ef76b56ec6)
1*657871a7Schristos /*	$NetBSD: regress_testutils.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
26ecf6635Schristos /*
36ecf6635Schristos  * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
46ecf6635Schristos  *
56ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
66ecf6635Schristos  * modification, are permitted provided that the following conditions
76ecf6635Schristos  * are met:
86ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
96ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
106ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
116ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
126ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
136ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
146ecf6635Schristos  *    derived from this software without specific prior written permission.
156ecf6635Schristos  *
166ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
256ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
266ecf6635Schristos  */
27805a1ce9Schristos #include "../util-internal.h"
286ecf6635Schristos 
29805a1ce9Schristos #ifdef _WIN32
306ecf6635Schristos #include <winsock2.h>
316ecf6635Schristos #include <windows.h>
326ecf6635Schristos #include <ws2tcpip.h>
336ecf6635Schristos #endif
346ecf6635Schristos 
356ecf6635Schristos #include "event2/event-config.h"
366ecf6635Schristos #include <sys/cdefs.h>
37*657871a7Schristos __RCSID("$NetBSD: regress_testutils.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $");
386ecf6635Schristos 
396ecf6635Schristos #include <sys/types.h>
406ecf6635Schristos #include <sys/stat.h>
41805a1ce9Schristos #ifdef EVENT__HAVE_SYS_TIME_H
426ecf6635Schristos #include <sys/time.h>
436ecf6635Schristos #endif
446ecf6635Schristos #include <sys/queue.h>
45805a1ce9Schristos #ifndef _WIN32
466ecf6635Schristos #include <sys/socket.h>
476ecf6635Schristos #include <signal.h>
486ecf6635Schristos #include <netinet/in.h>
496ecf6635Schristos #include <arpa/inet.h>
506ecf6635Schristos #include <unistd.h>
516ecf6635Schristos #endif
52805a1ce9Schristos #ifdef EVENT__HAVE_NETINET_IN6_H
536ecf6635Schristos #include <netinet/in6.h>
546ecf6635Schristos #endif
556ecf6635Schristos #ifdef HAVE_NETDB_H
566ecf6635Schristos #include <netdb.h>
576ecf6635Schristos #endif
586ecf6635Schristos #include <fcntl.h>
596ecf6635Schristos #include <stdlib.h>
606ecf6635Schristos #include <stdio.h>
616ecf6635Schristos #include <string.h>
626ecf6635Schristos #include <errno.h>
636ecf6635Schristos 
646ecf6635Schristos #include "event2/dns.h"
656ecf6635Schristos #include "event2/dns_struct.h"
666ecf6635Schristos #include "event2/event.h"
676ecf6635Schristos #include "event2/event_compat.h"
686ecf6635Schristos #include "event2/util.h"
696ecf6635Schristos #include "event2/listener.h"
706ecf6635Schristos #include "event2/bufferevent.h"
716ecf6635Schristos #include "log-internal.h"
726ecf6635Schristos #include "regress.h"
736ecf6635Schristos #include "regress_testutils.h"
746ecf6635Schristos 
756ecf6635Schristos /* globals */
766ecf6635Schristos static struct evdns_server_port *dns_port;
776ecf6635Schristos evutil_socket_t dns_sock = -1;
786ecf6635Schristos 
796ecf6635Schristos /* Helper: return the port that a socket is bound on, in host order. */
806ecf6635Schristos int
regress_get_socket_port(evutil_socket_t fd)816ecf6635Schristos regress_get_socket_port(evutil_socket_t fd)
826ecf6635Schristos {
836ecf6635Schristos 	struct sockaddr_storage ss;
846ecf6635Schristos 	ev_socklen_t socklen = sizeof(ss);
856ecf6635Schristos 	if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
866ecf6635Schristos 		return -1;
876ecf6635Schristos 	if (ss.ss_family == AF_INET)
886ecf6635Schristos 		return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
896ecf6635Schristos 	else if (ss.ss_family == AF_INET6)
906ecf6635Schristos 		return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
916ecf6635Schristos 	else
926ecf6635Schristos 		return -1;
936ecf6635Schristos }
946ecf6635Schristos 
956ecf6635Schristos struct evdns_server_port *
regress_get_dnsserver(struct event_base * base,ev_uint16_t * portnum,evutil_socket_t * psock,evdns_request_callback_fn_type cb,void * arg)966ecf6635Schristos regress_get_dnsserver(struct event_base *base,
976ecf6635Schristos     ev_uint16_t *portnum,
986ecf6635Schristos     evutil_socket_t *psock,
996ecf6635Schristos     evdns_request_callback_fn_type cb,
1006ecf6635Schristos     void *arg)
1016ecf6635Schristos {
1026ecf6635Schristos 	struct evdns_server_port *port = NULL;
1036ecf6635Schristos 	evutil_socket_t sock;
1046ecf6635Schristos 	struct sockaddr_in my_addr;
1056ecf6635Schristos 
1066ecf6635Schristos 	sock = socket(AF_INET, SOCK_DGRAM, 0);
1076ecf6635Schristos 	if (sock < 0) {
1086ecf6635Schristos 		tt_abort_perror("socket");
1096ecf6635Schristos 	}
1106ecf6635Schristos 
1116ecf6635Schristos 	evutil_make_socket_nonblocking(sock);
1126ecf6635Schristos 
1136ecf6635Schristos 	memset(&my_addr, 0, sizeof(my_addr));
1146ecf6635Schristos 	my_addr.sin_family = AF_INET;
1156ecf6635Schristos 	my_addr.sin_port = htons(*portnum);
116*657871a7Schristos 	my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
1176ecf6635Schristos 	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
1186ecf6635Schristos 		evutil_closesocket(sock);
1196ecf6635Schristos 		tt_abort_perror("bind");
1206ecf6635Schristos 	}
1216ecf6635Schristos 	port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
1226ecf6635Schristos 	if (!*portnum)
1236ecf6635Schristos 		*portnum = regress_get_socket_port(sock);
1246ecf6635Schristos 	if (psock)
1256ecf6635Schristos 		*psock = sock;
1266ecf6635Schristos 
1276ecf6635Schristos 	return port;
1286ecf6635Schristos end:
1296ecf6635Schristos 	return NULL;
1306ecf6635Schristos }
1316ecf6635Schristos 
1326ecf6635Schristos void
regress_clean_dnsserver(void)1336ecf6635Schristos regress_clean_dnsserver(void)
1346ecf6635Schristos {
135805a1ce9Schristos 	if (dns_port) {
1366ecf6635Schristos 		evdns_close_server_port(dns_port);
137805a1ce9Schristos 		dns_port = NULL;
138805a1ce9Schristos 	}
139805a1ce9Schristos 	if (dns_sock >= 0) {
1406ecf6635Schristos 		evutil_closesocket(dns_sock);
141805a1ce9Schristos 		dns_sock = -1;
142805a1ce9Schristos 	}
1436ecf6635Schristos }
1446ecf6635Schristos 
strtolower(char * s)145805a1ce9Schristos static void strtolower(char *s)
146805a1ce9Schristos {
147805a1ce9Schristos 	while (*s) {
148805a1ce9Schristos 		*s = EVUTIL_TOLOWER_(*s);
149805a1ce9Schristos 		++s;
150805a1ce9Schristos 	}
151805a1ce9Schristos }
1526ecf6635Schristos void
regress_dns_server_cb(struct evdns_server_request * req,void * data)1536ecf6635Schristos regress_dns_server_cb(struct evdns_server_request *req, void *data)
1546ecf6635Schristos {
1556ecf6635Schristos 	struct regress_dns_server_table *tab = data;
156805a1ce9Schristos 	char *question;
1576ecf6635Schristos 
1586ecf6635Schristos 	if (req->nquestions != 1)
1596ecf6635Schristos 		TT_DIE(("Only handling one question at a time; got %d",
1606ecf6635Schristos 			req->nquestions));
1616ecf6635Schristos 
1626ecf6635Schristos 	question = req->questions[0]->name;
1636ecf6635Schristos 
1646ecf6635Schristos 	while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
1656ecf6635Schristos 	    strcmp("*", tab->q))
1666ecf6635Schristos 		++tab;
1676ecf6635Schristos 	if (tab->q == NULL)
1686ecf6635Schristos 		TT_DIE(("Unexpected question: '%s'", question));
1696ecf6635Schristos 
1706ecf6635Schristos 	++tab->seen;
1716ecf6635Schristos 
172805a1ce9Schristos 	if (tab->lower)
173805a1ce9Schristos 		strtolower(question);
174805a1ce9Schristos 
1756ecf6635Schristos 	if (!strcmp(tab->anstype, "err")) {
1766ecf6635Schristos 		int err = atoi(tab->ans);
1776ecf6635Schristos 		tt_assert(! evdns_server_request_respond(req, err));
1786ecf6635Schristos 		return;
1796ecf6635Schristos 	} else if (!strcmp(tab->anstype, "errsoa")) {
1806ecf6635Schristos 		int err = atoi(tab->ans);
1816ecf6635Schristos 		char soa_record[] =
1826ecf6635Schristos 			"\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
1836ecf6635Schristos 			"\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
1846ecf6635Schristos 			"\x77\xde\x5e\xba" /* serial */
1856ecf6635Schristos 			"\x00\x00\x1c\x20" /* refreshtime = 2h */
1866ecf6635Schristos 			"\x00\x00\x0e\x10" /* retry = 1h */
1876ecf6635Schristos 			"\x00\x12\x75\x00" /* expiration = 14d */
1886ecf6635Schristos 			"\x00\x00\x0e\x10" /* min.ttl = 1h */
1896ecf6635Schristos 			;
1906ecf6635Schristos 		evdns_server_request_add_reply(
1916ecf6635Schristos 			req, EVDNS_AUTHORITY_SECTION,
1926ecf6635Schristos 			"example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
1936ecf6635Schristos 			42, sizeof(soa_record) - 1, 0, soa_record);
1946ecf6635Schristos 		tt_assert(! evdns_server_request_respond(req, err));
1956ecf6635Schristos 		return;
1966ecf6635Schristos 	} else if (!strcmp(tab->anstype, "A")) {
1976ecf6635Schristos 		struct in_addr in;
1986ecf6635Schristos 		if (!evutil_inet_pton(AF_INET, tab->ans, &in)) {
1996ecf6635Schristos 			TT_DIE(("Bad A value %s in table", tab->ans));
2006ecf6635Schristos 		}
2016ecf6635Schristos 		evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
2026ecf6635Schristos 		    100);
2036ecf6635Schristos 	} else if (!strcmp(tab->anstype, "AAAA")) {
2046ecf6635Schristos 		struct in6_addr in6;
2056ecf6635Schristos 		if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
2066ecf6635Schristos 			TT_DIE(("Bad AAAA value %s in table", tab->ans));
2076ecf6635Schristos 		}
2086ecf6635Schristos 		evdns_server_request_add_aaaa_reply(req,
2096ecf6635Schristos 		    question, 1, &in6.s6_addr, 100);
2106ecf6635Schristos 	} else {
2116ecf6635Schristos 		TT_DIE(("Weird table entry with type '%s'", tab->anstype));
2126ecf6635Schristos 	}
2136ecf6635Schristos 	tt_assert(! evdns_server_request_respond(req, 0))
2146ecf6635Schristos 	return;
2156ecf6635Schristos end:
2166ecf6635Schristos 	tt_want(! evdns_server_request_drop(req));
2176ecf6635Schristos }
2186ecf6635Schristos 
2196ecf6635Schristos int
regress_dnsserver(struct event_base * base,ev_uint16_t * port,struct regress_dns_server_table * search_table)2206ecf6635Schristos regress_dnsserver(struct event_base *base, ev_uint16_t *port,
2216ecf6635Schristos     struct regress_dns_server_table *search_table)
2226ecf6635Schristos {
2236ecf6635Schristos 	dns_port = regress_get_dnsserver(base, port, &dns_sock,
2246ecf6635Schristos 	    regress_dns_server_cb, search_table);
2256ecf6635Schristos 	return dns_port != NULL;
2266ecf6635Schristos }
2276ecf6635Schristos 
2286ecf6635Schristos int
regress_get_listener_addr(struct evconnlistener * lev,struct sockaddr * sa,ev_socklen_t * socklen)2296ecf6635Schristos regress_get_listener_addr(struct evconnlistener *lev,
2306ecf6635Schristos     struct sockaddr *sa, ev_socklen_t *socklen)
2316ecf6635Schristos {
2326ecf6635Schristos 	evutil_socket_t s = evconnlistener_get_fd(lev);
2336ecf6635Schristos 	if (s <= 0)
2346ecf6635Schristos 		return -1;
2356ecf6635Schristos 	return getsockname(s, sa, socklen);
2366ecf6635Schristos }
237