blob: 72f7237be6e48465ca495b9babfa45caff5200d7 [file] [log] [blame]
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001/*
2 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26#include "util-internal.h"
27
28#ifdef _WIN32
29#include <winsock2.h>
30#include <windows.h>
31#endif
32
33#include <sys/types.h>
34
35#ifndef _WIN32
36#include <sys/socket.h>
37#include <netinet/in.h>
38# ifdef _XOPEN_SOURCE_EXTENDED
39# include <arpa/inet.h>
40# endif
41#include <unistd.h>
42#endif
43#ifdef EVENT__HAVE_SYS_RESOURCE_H
44#include <sys/resource.h>
45#endif
46
47#include <string.h>
48
49#include "event2/listener.h"
50#include "event2/event.h"
51#include "event2/util.h"
52
53#include "regress.h"
54#include "tinytest.h"
55#include "tinytest_macros.h"
56
57static void
58acceptcb(struct evconnlistener *listener, evutil_socket_t fd,
59 struct sockaddr *addr, int socklen, void *arg)
60{
61 int *ptr = arg;
62 --*ptr;
63 TT_BLATHER(("Got one for %p", ptr));
64 evutil_closesocket(fd);
65
66 if (! *ptr)
67 evconnlistener_disable(listener);
68}
69
70static void
71regress_pick_a_port(void *arg)
72{
73 struct basic_test_data *data = arg;
74 struct event_base *base = data->base;
75 struct evconnlistener *listener1 = NULL, *listener2 = NULL;
76 struct sockaddr_in sin;
77 int count1 = 2, count2 = 1;
78 struct sockaddr_storage ss1, ss2;
79 struct sockaddr_in *sin1, *sin2;
80 ev_socklen_t slen1 = sizeof(ss1), slen2 = sizeof(ss2);
81 unsigned int flags =
82 LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC;
83
84 evutil_socket_t fd1 = -1, fd2 = -1, fd3 = -1;
85
86 if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
87 flags |= LEV_OPT_THREADSAFE;
88 }
89
90 memset(&sin, 0, sizeof(sin));
91 sin.sin_family = AF_INET;
92 sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
93 sin.sin_port = 0; /* "You pick!" */
94
95 listener1 = evconnlistener_new_bind(base, acceptcb, &count1,
96 flags, -1, (struct sockaddr *)&sin, sizeof(sin));
97 tt_assert(listener1);
98 listener2 = evconnlistener_new_bind(base, acceptcb, &count2,
99 flags, -1, (struct sockaddr *)&sin, sizeof(sin));
100 tt_assert(listener2);
101
102 tt_int_op(evconnlistener_get_fd(listener1), >=, 0);
103 tt_int_op(evconnlistener_get_fd(listener2), >=, 0);
104 tt_assert(getsockname(evconnlistener_get_fd(listener1),
105 (struct sockaddr*)&ss1, &slen1) == 0);
106 tt_assert(getsockname(evconnlistener_get_fd(listener2),
107 (struct sockaddr*)&ss2, &slen2) == 0);
108 tt_int_op(ss1.ss_family, ==, AF_INET);
109 tt_int_op(ss2.ss_family, ==, AF_INET);
110
111 sin1 = (struct sockaddr_in*)&ss1;
112 sin2 = (struct sockaddr_in*)&ss2;
113 tt_int_op(ntohl(sin1->sin_addr.s_addr), ==, 0x7f000001);
114 tt_int_op(ntohl(sin2->sin_addr.s_addr), ==, 0x7f000001);
115 tt_int_op(sin1->sin_port, !=, sin2->sin_port);
116
117 tt_ptr_op(evconnlistener_get_base(listener1), ==, base);
118 tt_ptr_op(evconnlistener_get_base(listener2), ==, base);
119
120 fd1 = fd2 = fd3 = -1;
121 evutil_socket_connect_(&fd1, (struct sockaddr*)&ss1, slen1);
122 evutil_socket_connect_(&fd2, (struct sockaddr*)&ss1, slen1);
123 evutil_socket_connect_(&fd3, (struct sockaddr*)&ss2, slen2);
124
125#ifdef _WIN32
126 Sleep(100); /* XXXX this is a stupid stopgap. */
127#endif
128 event_base_dispatch(base);
129
130 tt_int_op(count1, ==, 0);
131 tt_int_op(count2, ==, 0);
132
133end:
134 if (fd1>=0)
135 EVUTIL_CLOSESOCKET(fd1);
136 if (fd2>=0)
137 EVUTIL_CLOSESOCKET(fd2);
138 if (fd3>=0)
139 EVUTIL_CLOSESOCKET(fd3);
140 if (listener1)
141 evconnlistener_free(listener1);
142 if (listener2)
143 evconnlistener_free(listener2);
144}
145
146static void
147errorcb(struct evconnlistener *lis, void *data_)
148{
149 int *data = data_;
150 *data = 1000;
151 evconnlistener_disable(lis);
152}
153
154static void
155regress_listener_error(void *arg)
156{
157 struct basic_test_data *data = arg;
158 struct event_base *base = data->base;
159 struct evconnlistener *listener = NULL;
160 int count = 1;
161 unsigned int flags = LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE;
162
163 if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
164 flags |= LEV_OPT_THREADSAFE;
165 }
166
167 /* send, so that pair[0] will look 'readable'*/
168 tt_int_op(send(data->pair[1], "hello", 5, 0), >, 0);
169
170 /* Start a listener with a bogus socket. */
171 listener = evconnlistener_new(base, acceptcb, &count,
172 flags, 0,
173 data->pair[0]);
174 tt_assert(listener);
175
176 evconnlistener_set_error_cb(listener, errorcb);
177
178 tt_assert(listener);
179
180 event_base_dispatch(base);
181 tt_int_op(count,==,1000); /* set by error cb */
182
183end:
184 if (listener)
185 evconnlistener_free(listener);
186}
187
188#ifdef EVENT__HAVE_SETRLIMIT
189static void
190regress_listener_error_unlock(void *arg)
191{
192 struct basic_test_data *data = arg;
193 struct event_base *base = data->base;
194 struct evconnlistener *listener = NULL;
195 unsigned int flags =
196 LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_THREADSAFE;
197
198 tt_int_op(send(data->pair[1], "hello", 5, 0), >, 0);
199
200 /* Start a listener with a bogus socket. */
201 listener = evconnlistener_new(base, acceptcb, NULL, flags, 0, data->pair[0]);
202 tt_assert(listener);
203
204 /** accept() must errored out with EMFILE */
205 {
206 struct rlimit rl;
207 rl.rlim_cur = rl.rlim_max = data->pair[1];
208 if (setrlimit(RLIMIT_NOFILE, &rl) == -1) {
209 TT_DIE(("Can't change RLIMIT_NOFILE"));
210 }
211 }
212
213 event_base_loop(base, EVLOOP_ONCE);
214
215 /** with lock debugging, can fail on lock->count assertion */
216
217end:
218 if (listener)
219 evconnlistener_free(listener);
220}
221#endif
222
223struct testcase_t listener_testcases[] = {
224
225 { "randport", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
226 &basic_setup, NULL},
227
228 { "randport_ts", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
229 &basic_setup, (char*)"ts"},
230
231#ifdef EVENT__HAVE_SETRLIMIT
232 { "error_unlock", regress_listener_error_unlock,
233 TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
234 &basic_setup, NULL},
235#endif
236
237 { "error", regress_listener_error,
238 TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
239 &basic_setup, NULL},
240
241 { "error_ts", regress_listener_error,
242 TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
243 &basic_setup, (char*)"ts"},
244
245 END_OF_TESTCASES,
246};
247
248struct testcase_t listener_iocp_testcases[] = {
249 { "randport", regress_pick_a_port,
250 TT_FORK|TT_NEED_BASE|TT_ENABLE_IOCP,
251 &basic_setup, NULL},
252
253 { "error", regress_listener_error,
254 TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR|TT_ENABLE_IOCP,
255 &basic_setup, NULL},
256
257 END_OF_TESTCASES,
258};