Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*	$NetBSD: regress_testutils.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $	*/
      2      1.1  christos /*
      3      1.1  christos  * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
      4      1.1  christos  *
      5      1.1  christos  * Redistribution and use in source and binary forms, with or without
      6      1.1  christos  * modification, are permitted provided that the following conditions
      7      1.1  christos  * are met:
      8      1.1  christos  * 1. Redistributions of source code must retain the above copyright
      9      1.1  christos  *    notice, this list of conditions and the following disclaimer.
     10      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  christos  *    documentation and/or other materials provided with the distribution.
     13      1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     14      1.1  christos  *    derived from this software without specific prior written permission.
     15      1.1  christos  *
     16      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.1  christos  */
     27  1.1.1.2  christos #include "../util-internal.h"
     28      1.1  christos 
     29  1.1.1.2  christos #ifdef _WIN32
     30      1.1  christos #include <winsock2.h>
     31      1.1  christos #include <windows.h>
     32      1.1  christos #include <ws2tcpip.h>
     33      1.1  christos #endif
     34      1.1  christos 
     35      1.1  christos #include "event2/event-config.h"
     36      1.1  christos #include <sys/cdefs.h>
     37      1.1  christos __RCSID("$NetBSD: regress_testutils.c,v 1.1.1.3 2021/04/07 02:43:15 christos Exp $");
     38      1.1  christos 
     39      1.1  christos #include <sys/types.h>
     40      1.1  christos #include <sys/stat.h>
     41  1.1.1.2  christos #ifdef EVENT__HAVE_SYS_TIME_H
     42      1.1  christos #include <sys/time.h>
     43      1.1  christos #endif
     44      1.1  christos #include <sys/queue.h>
     45  1.1.1.2  christos #ifndef _WIN32
     46      1.1  christos #include <sys/socket.h>
     47      1.1  christos #include <signal.h>
     48      1.1  christos #include <netinet/in.h>
     49      1.1  christos #include <arpa/inet.h>
     50      1.1  christos #include <unistd.h>
     51      1.1  christos #endif
     52  1.1.1.2  christos #ifdef EVENT__HAVE_NETINET_IN6_H
     53      1.1  christos #include <netinet/in6.h>
     54      1.1  christos #endif
     55      1.1  christos #ifdef HAVE_NETDB_H
     56      1.1  christos #include <netdb.h>
     57      1.1  christos #endif
     58      1.1  christos #include <fcntl.h>
     59      1.1  christos #include <stdlib.h>
     60      1.1  christos #include <stdio.h>
     61      1.1  christos #include <string.h>
     62      1.1  christos #include <errno.h>
     63      1.1  christos 
     64      1.1  christos #include "event2/dns.h"
     65      1.1  christos #include "event2/dns_struct.h"
     66      1.1  christos #include "event2/event.h"
     67      1.1  christos #include "event2/event_compat.h"
     68      1.1  christos #include "event2/util.h"
     69      1.1  christos #include "event2/listener.h"
     70      1.1  christos #include "event2/bufferevent.h"
     71      1.1  christos #include "log-internal.h"
     72      1.1  christos #include "regress.h"
     73      1.1  christos #include "regress_testutils.h"
     74      1.1  christos 
     75      1.1  christos /* globals */
     76      1.1  christos static struct evdns_server_port *dns_port;
     77      1.1  christos evutil_socket_t dns_sock = -1;
     78      1.1  christos 
     79      1.1  christos /* Helper: return the port that a socket is bound on, in host order. */
     80      1.1  christos int
     81      1.1  christos regress_get_socket_port(evutil_socket_t fd)
     82      1.1  christos {
     83      1.1  christos 	struct sockaddr_storage ss;
     84      1.1  christos 	ev_socklen_t socklen = sizeof(ss);
     85      1.1  christos 	if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
     86      1.1  christos 		return -1;
     87      1.1  christos 	if (ss.ss_family == AF_INET)
     88      1.1  christos 		return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
     89      1.1  christos 	else if (ss.ss_family == AF_INET6)
     90      1.1  christos 		return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
     91      1.1  christos 	else
     92      1.1  christos 		return -1;
     93      1.1  christos }
     94      1.1  christos 
     95      1.1  christos struct evdns_server_port *
     96      1.1  christos regress_get_dnsserver(struct event_base *base,
     97      1.1  christos     ev_uint16_t *portnum,
     98      1.1  christos     evutil_socket_t *psock,
     99      1.1  christos     evdns_request_callback_fn_type cb,
    100      1.1  christos     void *arg)
    101      1.1  christos {
    102      1.1  christos 	struct evdns_server_port *port = NULL;
    103      1.1  christos 	evutil_socket_t sock;
    104      1.1  christos 	struct sockaddr_in my_addr;
    105      1.1  christos 
    106      1.1  christos 	sock = socket(AF_INET, SOCK_DGRAM, 0);
    107      1.1  christos 	if (sock < 0) {
    108      1.1  christos 		tt_abort_perror("socket");
    109      1.1  christos 	}
    110      1.1  christos 
    111      1.1  christos 	evutil_make_socket_nonblocking(sock);
    112      1.1  christos 
    113      1.1  christos 	memset(&my_addr, 0, sizeof(my_addr));
    114      1.1  christos 	my_addr.sin_family = AF_INET;
    115      1.1  christos 	my_addr.sin_port = htons(*portnum);
    116  1.1.1.3  christos 	my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    117      1.1  christos 	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
    118      1.1  christos 		evutil_closesocket(sock);
    119      1.1  christos 		tt_abort_perror("bind");
    120      1.1  christos 	}
    121      1.1  christos 	port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
    122      1.1  christos 	if (!*portnum)
    123      1.1  christos 		*portnum = regress_get_socket_port(sock);
    124      1.1  christos 	if (psock)
    125      1.1  christos 		*psock = sock;
    126      1.1  christos 
    127      1.1  christos 	return port;
    128      1.1  christos end:
    129      1.1  christos 	return NULL;
    130      1.1  christos }
    131      1.1  christos 
    132      1.1  christos void
    133      1.1  christos regress_clean_dnsserver(void)
    134      1.1  christos {
    135  1.1.1.2  christos 	if (dns_port) {
    136      1.1  christos 		evdns_close_server_port(dns_port);
    137  1.1.1.2  christos 		dns_port = NULL;
    138  1.1.1.2  christos 	}
    139  1.1.1.2  christos 	if (dns_sock >= 0) {
    140      1.1  christos 		evutil_closesocket(dns_sock);
    141  1.1.1.2  christos 		dns_sock = -1;
    142  1.1.1.2  christos 	}
    143      1.1  christos }
    144      1.1  christos 
    145  1.1.1.2  christos static void strtolower(char *s)
    146  1.1.1.2  christos {
    147  1.1.1.2  christos 	while (*s) {
    148  1.1.1.2  christos 		*s = EVUTIL_TOLOWER_(*s);
    149  1.1.1.2  christos 		++s;
    150  1.1.1.2  christos 	}
    151  1.1.1.2  christos }
    152      1.1  christos void
    153      1.1  christos regress_dns_server_cb(struct evdns_server_request *req, void *data)
    154      1.1  christos {
    155      1.1  christos 	struct regress_dns_server_table *tab = data;
    156  1.1.1.2  christos 	char *question;
    157      1.1  christos 
    158      1.1  christos 	if (req->nquestions != 1)
    159      1.1  christos 		TT_DIE(("Only handling one question at a time; got %d",
    160      1.1  christos 			req->nquestions));
    161      1.1  christos 
    162      1.1  christos 	question = req->questions[0]->name;
    163      1.1  christos 
    164      1.1  christos 	while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
    165      1.1  christos 	    strcmp("*", tab->q))
    166      1.1  christos 		++tab;
    167      1.1  christos 	if (tab->q == NULL)
    168      1.1  christos 		TT_DIE(("Unexpected question: '%s'", question));
    169      1.1  christos 
    170      1.1  christos 	++tab->seen;
    171      1.1  christos 
    172  1.1.1.2  christos 	if (tab->lower)
    173  1.1.1.2  christos 		strtolower(question);
    174  1.1.1.2  christos 
    175      1.1  christos 	if (!strcmp(tab->anstype, "err")) {
    176      1.1  christos 		int err = atoi(tab->ans);
    177      1.1  christos 		tt_assert(! evdns_server_request_respond(req, err));
    178      1.1  christos 		return;
    179      1.1  christos 	} else if (!strcmp(tab->anstype, "errsoa")) {
    180      1.1  christos 		int err = atoi(tab->ans);
    181      1.1  christos 		char soa_record[] =
    182      1.1  christos 			"\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
    183      1.1  christos 			"\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
    184      1.1  christos 			"\x77\xde\x5e\xba" /* serial */
    185      1.1  christos 			"\x00\x00\x1c\x20" /* refreshtime = 2h */
    186      1.1  christos 			"\x00\x00\x0e\x10" /* retry = 1h */
    187      1.1  christos 			"\x00\x12\x75\x00" /* expiration = 14d */
    188      1.1  christos 			"\x00\x00\x0e\x10" /* min.ttl = 1h */
    189      1.1  christos 			;
    190      1.1  christos 		evdns_server_request_add_reply(
    191      1.1  christos 			req, EVDNS_AUTHORITY_SECTION,
    192      1.1  christos 			"example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
    193      1.1  christos 			42, sizeof(soa_record) - 1, 0, soa_record);
    194      1.1  christos 		tt_assert(! evdns_server_request_respond(req, err));
    195      1.1  christos 		return;
    196      1.1  christos 	} else if (!strcmp(tab->anstype, "A")) {
    197      1.1  christos 		struct in_addr in;
    198      1.1  christos 		if (!evutil_inet_pton(AF_INET, tab->ans, &in)) {
    199      1.1  christos 			TT_DIE(("Bad A value %s in table", tab->ans));
    200      1.1  christos 		}
    201      1.1  christos 		evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
    202      1.1  christos 		    100);
    203      1.1  christos 	} else if (!strcmp(tab->anstype, "AAAA")) {
    204      1.1  christos 		struct in6_addr in6;
    205      1.1  christos 		if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
    206      1.1  christos 			TT_DIE(("Bad AAAA value %s in table", tab->ans));
    207      1.1  christos 		}
    208      1.1  christos 		evdns_server_request_add_aaaa_reply(req,
    209      1.1  christos 		    question, 1, &in6.s6_addr, 100);
    210      1.1  christos 	} else {
    211      1.1  christos 		TT_DIE(("Weird table entry with type '%s'", tab->anstype));
    212      1.1  christos 	}
    213      1.1  christos 	tt_assert(! evdns_server_request_respond(req, 0))
    214      1.1  christos 	return;
    215      1.1  christos end:
    216      1.1  christos 	tt_want(! evdns_server_request_drop(req));
    217      1.1  christos }
    218      1.1  christos 
    219      1.1  christos int
    220      1.1  christos regress_dnsserver(struct event_base *base, ev_uint16_t *port,
    221      1.1  christos     struct regress_dns_server_table *search_table)
    222      1.1  christos {
    223      1.1  christos 	dns_port = regress_get_dnsserver(base, port, &dns_sock,
    224      1.1  christos 	    regress_dns_server_cb, search_table);
    225      1.1  christos 	return dns_port != NULL;
    226      1.1  christos }
    227      1.1  christos 
    228      1.1  christos int
    229      1.1  christos regress_get_listener_addr(struct evconnlistener *lev,
    230      1.1  christos     struct sockaddr *sa, ev_socklen_t *socklen)
    231      1.1  christos {
    232      1.1  christos 	evutil_socket_t s = evconnlistener_get_fd(lev);
    233      1.1  christos 	if (s <= 0)
    234      1.1  christos 		return -1;
    235      1.1  christos 	return getsockname(s, sa, socklen);
    236      1.1  christos }
    237