struct evconnlistener *lev=NULL;
struct bufferevent *bev1=NULL, *bev2=NULL;
struct sockaddr_in localhost;
- struct sockaddr *sa = (struct sockaddr*)&localhost;
+ struct sockaddr_storage ss;
+ struct sockaddr *sa;
+ ev_socklen_t slen;
+
int be_flags=BEV_OPT_CLOSE_ON_FREE;
if (strstr((char*)data->setup_data, "defer")) {
memset(&localhost, 0, sizeof(localhost));
- localhost.sin_port = htons(27015);
+ localhost.sin_port = 0; /* pick-a-port */
localhost.sin_addr.s_addr = htonl(0x7f000001L);
localhost.sin_family = AF_INET;
-
+ sa = (struct sockaddr *)&localhost;
lev = evconnlistener_new_bind(data->base, listen_cb, data->base,
LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE,
16, sa, sizeof(localhost));
tt_assert(lev);
+
+ sa = (struct sockaddr *)&ss;
+ slen = sizeof(ss);
+ if (evconnlistener_get_address(lev, sa, &slen) < 0) {
+ tt_abort_perror("getsockname");
+ }
+
tt_assert(!evconnlistener_enable(lev));
bev1 = bufferevent_socket_new(data->base, -1, be_flags);
bev2 = bufferevent_socket_new(data->base, -1, be_flags);
test_ok = 0;
memset(&localhost, 0, sizeof(localhost));
- localhost.sin_port = 0;
+ localhost.sin_port = 0; /* have the kernel pick a port */
localhost.sin_addr.s_addr = htonl(0x7f000001L);
localhost.sin_family = AF_INET;
{
evutil_socket_t sock=-1;
struct sockaddr_in my_addr;
+ struct sockaddr_storage ss;
+ ev_socklen_t slen;
struct evdns_server_port *port=NULL;
struct in_addr resolve_addr;
struct in6_addr resolve_addr6;
base = evdns_base_new(NULL, 0);
- /* Add ourself as the only nameserver, and make sure we really are
- * the only nameserver. */
- evdns_base_nameserver_ip_add(base, "127.0.0.1:35353");
-
- tt_int_op(evdns_base_count_nameservers(base), ==, 1);
/* Now configure a nameserver port. */
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock<0) {
memset(&my_addr, 0, sizeof(my_addr));
my_addr.sin_family = AF_INET;
- my_addr.sin_port = htons(35353);
+ my_addr.sin_port = 0; /* kernel picks */
my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
tt_abort_perror("bind");
}
+ slen = sizeof(ss);
+ if (getsockname(sock, (struct sockaddr*)&ss, &slen) < 0) {
+ tt_abort_perror("getsockname");
+ }
+
port = evdns_add_server_port(sock, 0, dns_server_request_cb, NULL);
+ /* Add ourself as the only nameserver, and make sure we really are
+ * the only nameserver. */
+ evdns_base_nameserver_sockaddr_add(base, (struct sockaddr*)&ss, slen, 0);
+ tt_int_op(evdns_base_count_nameservers(base), ==, 1);
+
/* Send some queries. */
evdns_base_resolve_ipv4(base, "zz.example.com", DNS_QUERY_NO_SEARCH,
dns_server_gethostbyname_cb, NULL);
struct basic_test_data *data = arg;
struct event_base *base = data->base;
struct evdns_base *dns = NULL;
- ev_uint16_t portnum = 53900;/*XXXX let the code pick a port*/
+ ev_uint16_t portnum = 0;
+ char buf[64];
struct generic_dns_callback_result r1, r2, r3, r4, r5;
tt_assert(regress_dnsserver(base, &portnum, search_table));
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
dns = evdns_base_new(base, 0);
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53900"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
evdns_base_search_add(dns, "a.example.com");
evdns_base_search_add(dns, "b.example.com");
struct event_base *base = data->base;
struct evdns_base *dns = NULL;
struct evdns_server_port *port = NULL;
- ev_uint16_t portnum = 53900;/*XXXX let the code pick a port*/
+ ev_uint16_t portnum = 0;
struct generic_dns_callback_result r1;
+ char buf[64];
port = regress_get_dnsserver(base, &portnum, NULL,
search_cancel_server_cb, NULL);
tt_assert(port);
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
dns = evdns_base_new(base, 0);
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53900"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
evdns_base_search_add(dns, "a.example.com");
evdns_base_search_add(dns, "b.example.com");
exit_base = base;
request_count = 3;
n_replies_left = 1;
-
+
current_req = evdns_base_resolve_ipv4(dns, "host", 0,
generic_dns_callback, &r1);
event_base_dispatch(base);
struct evdns_server_port *port = NULL;
struct evdns_base *dns = NULL;
int drop_count = 2;
- ev_uint16_t portnum = 53900;/*XXXX let the code pick a port*/
+ ev_uint16_t portnum = 0;
+ char buf[64];
struct generic_dns_callback_result r1;
port = regress_get_dnsserver(base, &portnum, NULL,
fail_server_cb, &drop_count);
tt_assert(port);
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
dns = evdns_base_new(base, 0);
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53900"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
tt_assert(! evdns_base_set_option(dns, "timeout", "0.3"));
tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "10"));
tt_assert(! evdns_base_set_option(dns, "initial-probe-timeout", "0.5"));
struct evdns_server_port *port1 = NULL, *port2 = NULL;
struct evdns_base *dns = NULL;
struct generic_dns_callback_result r1;
- ev_uint16_t portnum1 = 53900, portnum2=53901;
+ ev_uint16_t portnum1 = 0, portnum2=0;
+ char buf1[64], buf2[64];
port1 = regress_get_dnsserver(base, &portnum1, NULL,
regress_dns_server_cb, internal_error_table);
port2 = regress_get_dnsserver(base, &portnum2, NULL,
regress_dns_server_cb, reissue_table);
tt_assert(port2);
+ evutil_snprintf(buf1, sizeof(buf1), "127.0.0.1:%d", (int)portnum1);
+ evutil_snprintf(buf2, sizeof(buf2), "127.0.0.1:%d", (int)portnum2);
dns = evdns_base_new(base, 0);
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53900"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf1));
tt_assert(! evdns_base_set_option(dns, "timeout:", "0.3"));
tt_assert(! evdns_base_set_option(dns, "max-timeouts:", "2"));
tt_assert(! evdns_base_set_option(dns, "attempts:", "5"));
generic_dns_callback, &r1);
/* Add this after, so that we are sure to get a reissue. */
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53901"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf2));
n_replies_left = 1;
exit_base = base;
struct basic_test_data *data = arg;
struct event_base *base = data->base;
struct evdns_base *dns = NULL;
- ev_uint16_t portnum = 53900;/*XXXX let the code pick a port*/
+ ev_uint16_t portnum = 0;
+ char buf[64];
struct generic_dns_callback_result r[20];
int i;
tt_assert(regress_dnsserver(base, &portnum, reissue_table));
-
-#if 0
- /* Make sure that having another (very bad!) RNG doesn't mess us
- * up. */
- evdns_set_random_bytes_fn(dumb_bytes_fn);
-#endif
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)portnum);
dns = evdns_base_new(base, 0);
- tt_assert(!evdns_base_nameserver_ip_add(dns, "127.0.0.1:53900"));
+ tt_assert(!evdns_base_nameserver_ip_add(dns, buf));
tt_assert(! evdns_base_set_option(dns, "max-inflight:", "3"));
tt_assert(! evdns_base_set_option(dns, "randomize-case:", "0"));
/* Start an evdns_base that uses the server as its resolver. */
dns = evdns_base_new(data->base, 0);
- evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", dns_port);
+ evutil_snprintf(buf, sizeof(buf), "127.0.0.1:%d", (int)dns_port);
evdns_base_nameserver_ip_add(dns, buf);
/* Now, finally, at long last, launch the bufferevents. One should do
static void http_large_delay_cb(struct evhttp_request *req, void *arg);
static void http_badreq_cb(struct evhttp_request *req, void *arg);
static void http_dispatcher_cb(struct evhttp_request *req, void *arg);
+static int
+http_bind(struct evhttp *myhttp, ev_uint16_t *pport)
+{
+ int port;
+ struct evhttp_bound_socket *sock;
+
+ sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", *pport);
+ if (sock == NULL)
+ event_errx(1, "Could not start web server");
+
+ port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
+ if (port < 0)
+ return -1;
+ *pport = (ev_uint16_t) port;
+
+ return 0;
+}
+
static struct evhttp *
-http_setup(short *pport, struct event_base *base)
+http_setup(ev_uint16_t *pport, struct event_base *base)
{
- int i;
struct evhttp *myhttp;
- short port = -1;
/* Try a few different ports */
myhttp = evhttp_new(base);
- for (i = 0; i < 50; ++i) {
- if (evhttp_bind_socket(myhttp, "127.0.0.1", 8080 + i) != -1) {
- port = 8080 + i;
- break;
- }
- }
- if (port == -1)
- event_errx(1, "Could not start web server");
+ if (http_bind(myhttp, pport) < 0)
+ return NULL;
/* Register a callback for certain types of requests */
evhttp_set_cb(myhttp, "/test", http_basic_cb, NULL);
evhttp_set_cb(myhttp, "/largedelay", http_large_delay_cb, NULL);
evhttp_set_cb(myhttp, "/badrequest", http_badreq_cb, NULL);
evhttp_set_cb(myhttp, "/", http_dispatcher_cb, NULL);
-
- *pport = port;
return (myhttp);
}
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0, port2 = 0;
test_ok = 0;
http = http_setup(&port, NULL);
/* bind to a second socket */
- if (evhttp_bind_socket(http, "127.0.0.1", port + 1) == -1) {
+ if (http_bind(http, &port2) == -1) {
fprintf(stdout, "FAILED (bind)\n");
exit(1);
}
bufferevent_free(bev);
evutil_closesocket(fd);
- fd = http_connect("127.0.0.1", port + 1);
+ fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
bev = bufferevent_new(fd, http_readcb, http_writecb,
struct bufferevent *bev = NULL;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port, port2;
test_ok = 0;
http = http_setup(&port, NULL);
/* bind to a second socket */
- if (evhttp_bind_socket(http, "127.0.0.1", port + 1) == -1)
+ if (http_bind(http, &port2) == -1)
TT_DIE(("Bind socket failed"));
/* NULL request test */
/* Second answer (BAD REQUEST) on connection close */
/* connect to the second port */
- fd = http_connect("127.0.0.1", port + 1);
+ fd = http_connect("127.0.0.1", port2);
/* Stupid thing to send a request */
bev = bufferevent_new(fd, http_badreq_readcb, http_writecb,
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port;
test_ok = 0;
static void
_http_connection_test(int persistent)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
static void
http_connection_async_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct evdns_base *dns_base = NULL;
static void
http_cancel_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct timeval tv;
static void
http_virtual_host_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct evhttp *second = NULL, *third = NULL;
static void
http_dispatcher_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
static void
http_post_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
static void
http_put_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0;
test_ok = 0;
static void
_http_close_detection(int with_delay)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0;
test_ok = 0;
base = event_init();
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0;
struct timeval tv_start, tv_end;
test_ok = 0;
struct bufferevent *bev;
evutil_socket_t fd;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0;
struct timeval tv_start, tv_end;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
static void
http_stream_out_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct evhttp_connection *evcon;
struct evbuffer *reply = evbuffer_new();
struct evhttp_request *req = NULL;
- short port = -1;
+ ev_uint16_t port = 0;
http = http_setup(&port, NULL);
{
struct evhttp_connection *evcon;
struct evhttp_request *req = NULL;
- short port = -1;
+ ev_uint16_t port = 0;
http = http_setup(&port, NULL);
static void
http_make_web_server(evutil_socket_t fd, short what, void *arg)
{
- short port = -1;
+ ev_uint16_t port = *(ev_uint16_t*)arg;
http = http_setup(&port, NULL);
}
static void
http_connection_retry_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
struct timeval tv, tv_start, tv_end;
test_ok = 0;
- /* auto detect the port */
+ /* auto detect a port */
http = http_setup(&port, NULL);
evhttp_free(http);
+ http = NULL;
evcon = evhttp_connection_new("127.0.0.1", port);
tt_assert(evcon);
*/
evutil_timerclear(&tv);
tv.tv_sec = 1;
- event_once(-1, EV_TIMEOUT, http_make_web_server, NULL, &tv);
+ event_once(-1, EV_TIMEOUT, http_make_web_server, &port, &tv);
evutil_gettimeofday(&tv_start, NULL);
event_dispatch();
struct bufferevent *bev= NULL;
evutil_socket_t fd = -1;
const char *http_start_request;
- short port = -1;
+ ev_uint16_t port = 0;
test_ok = 0;
static void
http_negative_content_length_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
static void
http_data_length_constraints_test(void)
{
- short port = -1;
+ ev_uint16_t port = 0;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req = NULL;
char long_str[8192];
struct bufferevent *bev = NULL;
struct timeval tv;
const char *http_request;
- short port = -1;
+ ev_uint16_t port = 0;
evutil_socket_t fd = -1;
test_ok = 0;
#include "regress.gen.h"
#include "regress.h"
+#include "regress_testutils.h"
static struct evhttp *
-http_setup(short *pport)
+http_setup(ev_uint16_t *pport)
{
- int i;
struct evhttp *myhttp;
- short port = -1;
+ ev_uint16_t port;
+ struct evhttp_bound_socket *sock;
+
+ myhttp = evhttp_new(NULL);
+ if (!myhttp)
+ event_errx(1, "Could not start web server");
/* Try a few different ports */
- for (i = 0; i < 50; ++i) {
- myhttp = evhttp_start("127.0.0.1", 8080 + i);
- if (myhttp != NULL) {
- port = 8080 + i;
- break;
- }
- }
+ sock = evhttp_bind_socket_with_handle(myhttp, "127.0.0.1", 0);
+ if (!sock)
+ event_errx(1, "Couldn't open web port");
- if (port == -1)
- event_errx(1, "Could not start web server");
+ port = regress_get_socket_port(evhttp_bound_socket_get_fd(sock));
*pport = port;
return (myhttp);
}
static void
-rpc_setup(struct evhttp **phttp, short *pport, struct evrpc_base **pbase)
+rpc_setup(struct evhttp **phttp, ev_uint16_t *pport, struct evrpc_base **pbase)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
static void
rpc_basic_test(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evhttp_connection *evcon = NULL;
static void
rpc_basic_message(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evhttp_connection *evcon = NULL;
}
static struct evrpc_pool *
-rpc_pool_with_connection(short port)
+rpc_pool_with_connection(ev_uint16_t port)
{
struct evhttp_connection *evcon;
struct evrpc_pool *pool;
static void
rpc_basic_client(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evrpc_pool *pool = NULL;
static void
rpc_basic_queued_client(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evrpc_pool *pool = NULL;
static void
rpc_basic_client_with_pause(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evrpc_pool *pool = NULL;
static void
rpc_client_timeout(void)
{
- short port;
+ ev_uint16_t port;
struct evhttp *http = NULL;
struct evrpc_base *base = NULL;
struct evrpc_pool *pool = NULL;
#ifndef _TESTUTILS_H
#define _TESTUTILS_H
+#include <event2/dns.h>
+
struct regress_dns_server_table {
const char *q;
const char *anstype;