1 /* $NetBSD: regress_testutils.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $ */
3 * Copyright (c) 2010-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "event2/event-config.h"
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: regress_testutils.c,v 1.1.1.1 2013/04/11 16:43:32 christos Exp $");
38 #include <sys/types.h>
40 #ifdef _EVENT_HAVE_SYS_TIME_H
43 #include <sys/queue.h>
45 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
51 #ifdef _EVENT_HAVE_NETINET_IN6_H
52 #include <netinet/in6.h>
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"
72 #include "regress_testutils.h"
74 #include "../util-internal.h"
77 static struct evdns_server_port
*dns_port
;
78 evutil_socket_t dns_sock
= -1;
80 /* Helper: return the port that a socket is bound on, in host order. */
82 regress_get_socket_port(evutil_socket_t fd
)
84 struct sockaddr_storage ss
;
85 ev_socklen_t socklen
= sizeof(ss
);
86 if (getsockname(fd
, (struct sockaddr
*)&ss
, &socklen
) != 0)
88 if (ss
.ss_family
== AF_INET
)
89 return ntohs( ((struct sockaddr_in
*)&ss
)->sin_port
);
90 else if (ss
.ss_family
== AF_INET6
)
91 return ntohs( ((struct sockaddr_in6
*)&ss
)->sin6_port
);
96 struct evdns_server_port
*
97 regress_get_dnsserver(struct event_base
*base
,
99 evutil_socket_t
*psock
,
100 evdns_request_callback_fn_type cb
,
103 struct evdns_server_port
*port
= NULL
;
104 evutil_socket_t sock
;
105 struct sockaddr_in my_addr
;
107 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
109 tt_abort_perror("socket");
112 evutil_make_socket_nonblocking(sock
);
114 memset(&my_addr
, 0, sizeof(my_addr
));
115 my_addr
.sin_family
= AF_INET
;
116 my_addr
.sin_port
= htons(*portnum
);
117 my_addr
.sin_addr
.s_addr
= htonl(0x7f000001UL
);
118 if (bind(sock
, (struct sockaddr
*)&my_addr
, sizeof(my_addr
)) < 0) {
119 evutil_closesocket(sock
);
120 tt_abort_perror("bind");
122 port
= evdns_add_server_port_with_base(base
, sock
, 0, cb
, arg
);
124 *portnum
= regress_get_socket_port(sock
);
134 regress_clean_dnsserver(void)
137 evdns_close_server_port(dns_port
);
139 evutil_closesocket(dns_sock
);
143 regress_dns_server_cb(struct evdns_server_request
*req
, void *data
)
145 struct regress_dns_server_table
*tab
= data
;
146 const char *question
;
148 if (req
->nquestions
!= 1)
149 TT_DIE(("Only handling one question at a time; got %d",
152 question
= req
->questions
[0]->name
;
154 while (tab
->q
&& evutil_ascii_strcasecmp(question
, tab
->q
) &&
158 TT_DIE(("Unexpected question: '%s'", question
));
162 if (!strcmp(tab
->anstype
, "err")) {
163 int err
= atoi(tab
->ans
);
164 tt_assert(! evdns_server_request_respond(req
, err
));
166 } else if (!strcmp(tab
->anstype
, "errsoa")) {
167 int err
= atoi(tab
->ans
);
169 "\x04" "dns1" "\x05" "icann" "\x03" "org" "\0"
170 "\x0a" "hostmaster" "\x05" "icann" "\x03" "org" "\0"
171 "\x77\xde\x5e\xba" /* serial */
172 "\x00\x00\x1c\x20" /* refreshtime = 2h */
173 "\x00\x00\x0e\x10" /* retry = 1h */
174 "\x00\x12\x75\x00" /* expiration = 14d */
175 "\x00\x00\x0e\x10" /* min.ttl = 1h */
177 evdns_server_request_add_reply(
178 req
, EVDNS_AUTHORITY_SECTION
,
179 "example.com", EVDNS_TYPE_SOA
, EVDNS_CLASS_INET
,
180 42, sizeof(soa_record
) - 1, 0, soa_record
);
181 tt_assert(! evdns_server_request_respond(req
, err
));
183 } else if (!strcmp(tab
->anstype
, "A")) {
185 if (!evutil_inet_pton(AF_INET
, tab
->ans
, &in
)) {
186 TT_DIE(("Bad A value %s in table", tab
->ans
));
188 evdns_server_request_add_a_reply(req
, question
, 1, &in
.s_addr
,
190 } else if (!strcmp(tab
->anstype
, "AAAA")) {
192 if (!evutil_inet_pton(AF_INET6
, tab
->ans
, &in6
)) {
193 TT_DIE(("Bad AAAA value %s in table", tab
->ans
));
195 evdns_server_request_add_aaaa_reply(req
,
196 question
, 1, &in6
.s6_addr
, 100);
198 TT_DIE(("Weird table entry with type '%s'", tab
->anstype
));
200 tt_assert(! evdns_server_request_respond(req
, 0))
203 tt_want(! evdns_server_request_drop(req
));
207 regress_dnsserver(struct event_base
*base
, ev_uint16_t
*port
,
208 struct regress_dns_server_table
*search_table
)
210 dns_port
= regress_get_dnsserver(base
, port
, &dns_sock
,
211 regress_dns_server_cb
, search_table
);
212 return dns_port
!= NULL
;
216 regress_get_listener_addr(struct evconnlistener
*lev
,
217 struct sockaddr
*sa
, ev_socklen_t
*socklen
)
219 evutil_socket_t s
= evconnlistener_get_fd(lev
);
222 return getsockname(s
, sa
, socklen
);