1290001Sglebius/*
2290001Sglebius * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3290001Sglebius *
4290001Sglebius * Redistribution and use in source and binary forms, with or without
5290001Sglebius * modification, are permitted provided that the following conditions
6290001Sglebius * are met:
7290001Sglebius * 1. Redistributions of source code must retain the above copyright
8290001Sglebius *    notice, this list of conditions and the following disclaimer.
9290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
10290001Sglebius *    notice, this list of conditions and the following disclaimer in the
11290001Sglebius *    documentation and/or other materials provided with the distribution.
12290001Sglebius * 3. The name of the author may not be used to endorse or promote products
13290001Sglebius *    derived from this software without specific prior written permission.
14290001Sglebius *
15290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25290001Sglebius */
26290001Sglebius#include "util-internal.h"
27290001Sglebius
28290001Sglebius#ifdef _WIN32
29290001Sglebius#include <winsock2.h>
30290001Sglebius#include <windows.h>
31290001Sglebius#endif
32290001Sglebius
33290001Sglebius#include <sys/types.h>
34290001Sglebius
35290001Sglebius#ifndef _WIN32
36290001Sglebius#include <sys/socket.h>
37290001Sglebius#include <netinet/in.h>
38290001Sglebius# ifdef _XOPEN_SOURCE_EXTENDED
39290001Sglebius#  include <arpa/inet.h>
40290001Sglebius# endif
41290001Sglebius#include <unistd.h>
42290001Sglebius#endif
43290001Sglebius
44290001Sglebius#include <string.h>
45290001Sglebius
46290001Sglebius#include "event2/listener.h"
47290001Sglebius#include "event2/event.h"
48290001Sglebius#include "event2/util.h"
49290001Sglebius
50290001Sglebius#include "regress.h"
51290001Sglebius#include "tinytest.h"
52290001Sglebius#include "tinytest_macros.h"
53290001Sglebius
54290001Sglebiusstatic void
55290001Sglebiusacceptcb(struct evconnlistener *listener, evutil_socket_t fd,
56290001Sglebius    struct sockaddr *addr, int socklen, void *arg)
57290001Sglebius{
58290001Sglebius	int *ptr = arg;
59290001Sglebius	--*ptr;
60290001Sglebius	TT_BLATHER(("Got one for %p", ptr));
61290001Sglebius	evutil_closesocket(fd);
62290001Sglebius
63290001Sglebius	if (! *ptr)
64290001Sglebius		evconnlistener_disable(listener);
65290001Sglebius}
66290001Sglebius
67290001Sglebiusstatic void
68290001Sglebiusregress_pick_a_port(void *arg)
69290001Sglebius{
70290001Sglebius	struct basic_test_data *data = arg;
71290001Sglebius	struct event_base *base = data->base;
72290001Sglebius	struct evconnlistener *listener1 = NULL, *listener2 = NULL;
73290001Sglebius	struct sockaddr_in sin;
74290001Sglebius	int count1 = 2, count2 = 1;
75290001Sglebius	struct sockaddr_storage ss1, ss2;
76290001Sglebius	struct sockaddr_in *sin1, *sin2;
77290001Sglebius	ev_socklen_t slen1 = sizeof(ss1), slen2 = sizeof(ss2);
78290001Sglebius	unsigned int flags =
79290001Sglebius	    LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC;
80290001Sglebius
81290001Sglebius	evutil_socket_t fd1 = -1, fd2 = -1, fd3 = -1;
82290001Sglebius
83290001Sglebius	if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
84290001Sglebius		flags |= LEV_OPT_THREADSAFE;
85290001Sglebius	}
86290001Sglebius
87290001Sglebius	memset(&sin, 0, sizeof(sin));
88290001Sglebius	sin.sin_family = AF_INET;
89290001Sglebius	sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
90290001Sglebius	sin.sin_port = 0; /* "You pick!" */
91290001Sglebius
92290001Sglebius	listener1 = evconnlistener_new_bind(base, acceptcb, &count1,
93290001Sglebius	    flags, -1, (struct sockaddr *)&sin, sizeof(sin));
94290001Sglebius	tt_assert(listener1);
95290001Sglebius	listener2 = evconnlistener_new_bind(base, acceptcb, &count2,
96290001Sglebius	    flags, -1, (struct sockaddr *)&sin, sizeof(sin));
97290001Sglebius	tt_assert(listener2);
98290001Sglebius
99290001Sglebius	tt_int_op(evconnlistener_get_fd(listener1), >=, 0);
100290001Sglebius	tt_int_op(evconnlistener_get_fd(listener2), >=, 0);
101290001Sglebius	tt_assert(getsockname(evconnlistener_get_fd(listener1),
102290001Sglebius		(struct sockaddr*)&ss1, &slen1) == 0);
103290001Sglebius	tt_assert(getsockname(evconnlistener_get_fd(listener2),
104290001Sglebius		(struct sockaddr*)&ss2, &slen2) == 0);
105290001Sglebius	tt_int_op(ss1.ss_family, ==, AF_INET);
106290001Sglebius	tt_int_op(ss2.ss_family, ==, AF_INET);
107290001Sglebius
108290001Sglebius	sin1 = (struct sockaddr_in*)&ss1;
109290001Sglebius	sin2 = (struct sockaddr_in*)&ss2;
110290001Sglebius	tt_int_op(ntohl(sin1->sin_addr.s_addr), ==, 0x7f000001);
111290001Sglebius	tt_int_op(ntohl(sin2->sin_addr.s_addr), ==, 0x7f000001);
112290001Sglebius	tt_int_op(sin1->sin_port, !=, sin2->sin_port);
113290001Sglebius
114290001Sglebius	tt_ptr_op(evconnlistener_get_base(listener1), ==, base);
115290001Sglebius	tt_ptr_op(evconnlistener_get_base(listener2), ==, base);
116290001Sglebius
117290001Sglebius	fd1 = fd2 = fd3 = -1;
118290001Sglebius	evutil_socket_connect_(&fd1, (struct sockaddr*)&ss1, slen1);
119290001Sglebius	evutil_socket_connect_(&fd2, (struct sockaddr*)&ss1, slen1);
120290001Sglebius	evutil_socket_connect_(&fd3, (struct sockaddr*)&ss2, slen2);
121290001Sglebius
122290001Sglebius#ifdef _WIN32
123290001Sglebius	Sleep(100); /* XXXX this is a stupid stopgap. */
124290001Sglebius#endif
125290001Sglebius	event_base_dispatch(base);
126290001Sglebius
127290001Sglebius	tt_int_op(count1, ==, 0);
128290001Sglebius	tt_int_op(count2, ==, 0);
129290001Sglebius
130290001Sglebiusend:
131290001Sglebius	if (fd1>=0)
132290001Sglebius		EVUTIL_CLOSESOCKET(fd1);
133290001Sglebius	if (fd2>=0)
134290001Sglebius		EVUTIL_CLOSESOCKET(fd2);
135290001Sglebius	if (fd3>=0)
136290001Sglebius		EVUTIL_CLOSESOCKET(fd3);
137290001Sglebius	if (listener1)
138290001Sglebius		evconnlistener_free(listener1);
139290001Sglebius	if (listener2)
140290001Sglebius		evconnlistener_free(listener2);
141290001Sglebius}
142290001Sglebius
143290001Sglebiusstatic void
144290001Sglebiuserrorcb(struct evconnlistener *lis, void *data_)
145290001Sglebius{
146290001Sglebius	int *data = data_;
147290001Sglebius	*data = 1000;
148290001Sglebius	evconnlistener_disable(lis);
149290001Sglebius}
150290001Sglebius
151290001Sglebiusstatic void
152290001Sglebiusregress_listener_error(void *arg)
153290001Sglebius{
154290001Sglebius	struct basic_test_data *data = arg;
155290001Sglebius	struct event_base *base = data->base;
156290001Sglebius	struct evconnlistener *listener = NULL;
157290001Sglebius	int count = 1;
158290001Sglebius	unsigned int flags = LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE;
159290001Sglebius
160290001Sglebius	if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
161290001Sglebius		flags |= LEV_OPT_THREADSAFE;
162290001Sglebius	}
163290001Sglebius
164290001Sglebius	/* send, so that pair[0] will look 'readable'*/
165290001Sglebius	tt_int_op(send(data->pair[1], "hello", 5, 0), >, 0);
166290001Sglebius
167290001Sglebius	/* Start a listener with a bogus socket. */
168290001Sglebius	listener = evconnlistener_new(base, acceptcb, &count,
169290001Sglebius	    flags, 0,
170290001Sglebius	    data->pair[0]);
171290001Sglebius	tt_assert(listener);
172290001Sglebius
173290001Sglebius	evconnlistener_set_error_cb(listener, errorcb);
174290001Sglebius
175290001Sglebius	tt_assert(listener);
176290001Sglebius
177290001Sglebius	event_base_dispatch(base);
178290001Sglebius	tt_int_op(count,==,1000); /* set by error cb */
179290001Sglebius
180290001Sglebiusend:
181290001Sglebius	if (listener)
182290001Sglebius		evconnlistener_free(listener);
183290001Sglebius}
184290001Sglebius
185290001Sglebiusstruct testcase_t listener_testcases[] = {
186290001Sglebius
187290001Sglebius	{ "randport", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
188290001Sglebius	  &basic_setup, NULL},
189290001Sglebius
190290001Sglebius	{ "randport_ts", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
191290001Sglebius	  &basic_setup, (char*)"ts"},
192290001Sglebius
193290001Sglebius	{ "error", regress_listener_error,
194290001Sglebius	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
195290001Sglebius	  &basic_setup, NULL},
196290001Sglebius
197290001Sglebius	{ "error_ts", regress_listener_error,
198290001Sglebius	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
199290001Sglebius	  &basic_setup, (char*)"ts"},
200290001Sglebius
201290001Sglebius	END_OF_TESTCASES,
202290001Sglebius};
203290001Sglebius
204290001Sglebiusstruct testcase_t listener_iocp_testcases[] = {
205290001Sglebius	{ "randport", regress_pick_a_port,
206290001Sglebius	  TT_FORK|TT_NEED_BASE|TT_ENABLE_IOCP,
207290001Sglebius	  &basic_setup, NULL},
208290001Sglebius
209290001Sglebius	{ "error", regress_listener_error,
210290001Sglebius	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR|TT_ENABLE_IOCP,
211290001Sglebius	  &basic_setup, NULL},
212290001Sglebius
213290001Sglebius	END_OF_TESTCASES,
214290001Sglebius};
215