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