Home | History | Annotate | Line # | Download | only in test
regress_testutils.c revision 1.1
      1  1.1  christos /*	$NetBSD: regress_testutils.c,v 1.1 2013/04/11 16:43:32 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  christos 
     28  1.1  christos #ifdef WIN32
     29  1.1  christos #include <winsock2.h>
     30  1.1  christos #include <windows.h>
     31  1.1  christos #include <ws2tcpip.h>
     32  1.1  christos #endif
     33  1.1  christos 
     34  1.1  christos #include "event2/event-config.h"
     35  1.1  christos #include <sys/cdefs.h>
     36  1.1  christos __RCSID("$NetBSD: regress_testutils.c,v 1.1 2013/04/11 16:43:32 christos Exp $");
     37  1.1  christos 
     38  1.1  christos #include <sys/types.h>
     39  1.1  christos #include <sys/stat.h>
     40  1.1  christos #ifdef _EVENT_HAVE_SYS_TIME_H
     41  1.1  christos #include <sys/time.h>
     42  1.1  christos #endif
     43  1.1  christos #include <sys/queue.h>
     44  1.1  christos #ifndef WIN32
     45  1.1  christos #include <sys/socket.h>
     46  1.1  christos #include <signal.h>
     47  1.1  christos #include <netinet/in.h>
     48  1.1  christos #include <arpa/inet.h>
     49  1.1  christos #include <unistd.h>
     50  1.1  christos #endif
     51  1.1  christos #ifdef _EVENT_HAVE_NETINET_IN6_H
     52  1.1  christos #include <netinet/in6.h>
     53  1.1  christos #endif
     54  1.1  christos #ifdef HAVE_NETDB_H
     55  1.1  christos #include <netdb.h>
     56  1.1  christos #endif
     57  1.1  christos #include <fcntl.h>
     58  1.1  christos #include <stdlib.h>
     59  1.1  christos #include <stdio.h>
     60  1.1  christos #include <string.h>
     61  1.1  christos #include <errno.h>
     62  1.1  christos 
     63  1.1  christos #include "event2/dns.h"
     64  1.1  christos #include "event2/dns_struct.h"
     65  1.1  christos #include "event2/event.h"
     66  1.1  christos #include "event2/event_compat.h"
     67  1.1  christos #include "event2/util.h"
     68  1.1  christos #include "event2/listener.h"
     69  1.1  christos #include "event2/bufferevent.h"
     70  1.1  christos #include "log-internal.h"
     71  1.1  christos #include "regress.h"
     72  1.1  christos #include "regress_testutils.h"
     73  1.1  christos 
     74  1.1  christos #include "../util-internal.h"
     75  1.1  christos 
     76  1.1  christos /* globals */
     77  1.1  christos static struct evdns_server_port *dns_port;
     78  1.1  christos evutil_socket_t dns_sock = -1;
     79  1.1  christos 
     80  1.1  christos /* Helper: return the port that a socket is bound on, in host order. */
     81  1.1  christos int
     82  1.1  christos regress_get_socket_port(evutil_socket_t fd)
     83  1.1  christos {
     84  1.1  christos 	struct sockaddr_storage ss;
     85  1.1  christos 	ev_socklen_t socklen = sizeof(ss);
     86  1.1  christos 	if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
     87  1.1  christos 		return -1;
     88  1.1  christos 	if (ss.ss_family == AF_INET)
     89  1.1  christos 		return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
     90  1.1  christos 	else if (ss.ss_family == AF_INET6)
     91  1.1  christos 		return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
     92  1.1  christos 	else
     93  1.1  christos 		return -1;
     94  1.1  christos }
     95  1.1  christos 
     96  1.1  christos struct evdns_server_port *
     97  1.1  christos regress_get_dnsserver(struct event_base *base,
     98  1.1  christos     ev_uint16_t *portnum,
     99  1.1  christos     evutil_socket_t *psock,
    100  1.1  christos     evdns_request_callback_fn_type cb,
    101  1.1  christos     void *arg)
    102  1.1  christos {
    103  1.1  christos 	struct evdns_server_port *port = NULL;
    104  1.1  christos 	evutil_socket_t sock;
    105  1.1  christos 	struct sockaddr_in my_addr;
    106  1.1  christos 
    107  1.1  christos 	sock = socket(AF_INET, SOCK_DGRAM, 0);
    108  1.1  christos 	if (sock < 0) {
    109  1.1  christos 		tt_abort_perror("socket");
    110  1.1  christos 	}
    111  1.1  christos 
    112  1.1  christos 	evutil_make_socket_nonblocking(sock);
    113  1.1  christos 
    114  1.1  christos 	memset(&my_addr, 0, sizeof(my_addr));
    115  1.1  christos 	my_addr.sin_family = AF_INET;
    116  1.1  christos 	my_addr.sin_port = htons(*portnum);
    117  1.1  christos 	my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
    118  1.1  christos 	if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
    119  1.1  christos 		evutil_closesocket(sock);
    120  1.1  christos 		tt_abort_perror("bind");
    121  1.1  christos 	}
    122  1.1  christos 	port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
    123  1.1  christos 	if (!*portnum)
    124  1.1  christos 		*portnum = regress_get_socket_port(sock);
    125  1.1  christos 	if (psock)
    126  1.1  christos 		*psock = sock;
    127  1.1  christos 
    128  1.1  christos 	return port;
    129  1.1  christos end:
    130  1.1  christos 	return NULL;
    131  1.1  christos }
    132  1.1  christos 
    133  1.1  christos void
    134  1.1  christos regress_clean_dnsserver(void)
    135  1.1  christos {
    136  1.1  christos 	if (dns_port)
    137  1.1  christos 		evdns_close_server_port(dns_port);
    138  1.1  christos 	if (dns_sock >= 0)
    139  1.1  christos 		evutil_closesocket(dns_sock);
    140  1.1  christos }
    141  1.1  christos 
    142  1.1  christos void
    143  1.1  christos regress_dns_server_cb(struct evdns_server_request *req, void *data)
    144  1.1  christos {
    145  1.1  christos 	struct regress_dns_server_table *tab = data;
    146  1.1  christos 	const char *question;
    147  1.1  christos 
    148  1.1  christos 	if (req->nquestions != 1)
    149  1.1  christos 		TT_DIE(("Only handling one question at a time; got %d",
    150  1.1  christos 			req->nquestions));
    151  1.1  christos 
    152  1.1  christos 	question = req->questions[0]->name;
    153  1.1  christos 
    154  1.1  christos 	while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
    155  1.1  christos 	    strcmp("*", tab->q))
    156  1.1  christos 		++tab;
    157  1.1  christos 	if (tab->q == NULL)
    158  1.1  christos 		TT_DIE(("Unexpected question: '%s'", question));
    159  1.1  christos 
    160  1.1  christos 	++tab->seen;
    161  1.1  christos 
    162  1.1  christos 	if (!strcmp(tab->anstype, "err")) {
    163  1.1  christos 		int err = atoi(tab->ans);
    164  1.1  christos 		tt_assert(! evdns_server_request_respond(req, err));
    165  1.1  christos 		return;
    166  1.1  christos 	} else if (!strcmp(tab->anstype, "errsoa")) {
    167  1.1  christos 		int err = atoi(tab->ans);
    168  1.1  christos 		char soa_record[] =
    169  1.1  christos 			"\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
    170  1.1  christos 			"\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
    171  1.1  christos 			"\x77\xde\x5e\xba" /* serial */
    172  1.1  christos 			"\x00\x00\x1c\x20" /* refreshtime = 2h */
    173  1.1  christos 			"\x00\x00\x0e\x10" /* retry = 1h */
    174  1.1  christos 			"\x00\x12\x75\x00" /* expiration = 14d */
    175  1.1  christos 			"\x00\x00\x0e\x10" /* min.ttl = 1h */
    176  1.1  christos 			;
    177  1.1  christos 		evdns_server_request_add_reply(
    178  1.1  christos 			req, EVDNS_AUTHORITY_SECTION,
    179  1.1  christos 			"example.com", EVDNS_TYPE_SOA, EVDNS_CLASS_INET,
    180  1.1  christos 			42, sizeof(soa_record) - 1, 0, soa_record);
    181  1.1  christos 		tt_assert(! evdns_server_request_respond(req, err));
    182  1.1  christos 		return;
    183  1.1  christos 	} else if (!strcmp(tab->anstype, "A")) {
    184  1.1  christos 		struct in_addr in;
    185  1.1  christos 		if (!evutil_inet_pton(AF_INET, tab->ans, &in)) {
    186  1.1  christos 			TT_DIE(("Bad A value %s in table", tab->ans));
    187  1.1  christos 		}
    188  1.1  christos 		evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
    189  1.1  christos 		    100);
    190  1.1  christos 	} else if (!strcmp(tab->anstype, "AAAA")) {
    191  1.1  christos 		struct in6_addr in6;
    192  1.1  christos 		if (!evutil_inet_pton(AF_INET6, tab->ans, &in6)) {
    193  1.1  christos 			TT_DIE(("Bad AAAA value %s in table", tab->ans));
    194  1.1  christos 		}
    195  1.1  christos 		evdns_server_request_add_aaaa_reply(req,
    196  1.1  christos 		    question, 1, &in6.s6_addr, 100);
    197  1.1  christos 	} else {
    198  1.1  christos 		TT_DIE(("Weird table entry with type '%s'", tab->anstype));
    199  1.1  christos 	}
    200  1.1  christos 	tt_assert(! evdns_server_request_respond(req, 0))
    201  1.1  christos 	return;
    202  1.1  christos end:
    203  1.1  christos 	tt_want(! evdns_server_request_drop(req));
    204  1.1  christos }
    205  1.1  christos 
    206  1.1  christos int
    207  1.1  christos regress_dnsserver(struct event_base *base, ev_uint16_t *port,
    208  1.1  christos     struct regress_dns_server_table *search_table)
    209  1.1  christos {
    210  1.1  christos 	dns_port = regress_get_dnsserver(base, port, &dns_sock,
    211  1.1  christos 	    regress_dns_server_cb, search_table);
    212  1.1  christos 	return dns_port != NULL;
    213  1.1  christos }
    214  1.1  christos 
    215  1.1  christos int
    216  1.1  christos regress_get_listener_addr(struct evconnlistener *lev,
    217  1.1  christos     struct sockaddr *sa, ev_socklen_t *socklen)
    218  1.1  christos {
    219  1.1  christos 	evutil_socket_t s = evconnlistener_get_fd(lev);
    220  1.1  christos 	if (s <= 0)
    221  1.1  christos 		return -1;
    222  1.1  christos 	return getsockname(s, sa, socklen);
    223  1.1  christos }
    224