]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_listener.c
Add libevent
[netatalk.git] / libevent / test / regress_listener.c
1 /*
2  * Copyright (c) 2009-2010 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
27 #ifdef WIN32
28 #include <winsock2.h>
29 #include <windows.h>
30 #endif
31
32 #include <sys/types.h>
33
34 #ifndef WIN32
35 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <unistd.h>
38 #endif
39
40 #include <string.h>
41
42 #include "event2/listener.h"
43 #include "event2/event.h"
44 #include "event2/util.h"
45
46 #include "regress.h"
47 #include "tinytest.h"
48 #include "tinytest_macros.h"
49 #include "util-internal.h"
50
51 static void
52 acceptcb(struct evconnlistener *listener, evutil_socket_t fd,
53     struct sockaddr *addr, int socklen, void *arg)
54 {
55         int *ptr = arg;
56         --*ptr;
57         TT_BLATHER(("Got one for %p", ptr));
58         evutil_closesocket(fd);
59
60         if (! *ptr)
61                 evconnlistener_disable(listener);
62 }
63
64 static void
65 regress_pick_a_port(void *arg)
66 {
67         struct basic_test_data *data = arg;
68         struct event_base *base = data->base;
69         struct evconnlistener *listener1 = NULL, *listener2 = NULL;
70         struct sockaddr_in sin;
71         int count1 = 2, count2 = 1;
72         struct sockaddr_storage ss1, ss2;
73         struct sockaddr_in *sin1, *sin2;
74         ev_socklen_t slen1 = sizeof(ss1), slen2 = sizeof(ss2);
75         unsigned int flags =
76             LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_EXEC;
77
78         evutil_socket_t fd1 = -1, fd2 = -1, fd3 = -1;
79
80         if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
81                 flags |= LEV_OPT_THREADSAFE;
82         }
83
84         memset(&sin, 0, sizeof(sin));
85         sin.sin_family = AF_INET;
86         sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */
87         sin.sin_port = 0; /* "You pick!" */
88
89         listener1 = evconnlistener_new_bind(base, acceptcb, &count1,
90             flags, -1, (struct sockaddr *)&sin, sizeof(sin));
91         tt_assert(listener1);
92         listener2 = evconnlistener_new_bind(base, acceptcb, &count2,
93             flags, -1, (struct sockaddr *)&sin, sizeof(sin));
94         tt_assert(listener2);
95
96         tt_int_op(evconnlistener_get_fd(listener1), >=, 0);
97         tt_int_op(evconnlistener_get_fd(listener2), >=, 0);
98         tt_assert(getsockname(evconnlistener_get_fd(listener1),
99                 (struct sockaddr*)&ss1, &slen1) == 0);
100         tt_assert(getsockname(evconnlistener_get_fd(listener2),
101                 (struct sockaddr*)&ss2, &slen2) == 0);
102         tt_int_op(ss1.ss_family, ==, AF_INET);
103         tt_int_op(ss2.ss_family, ==, AF_INET);
104
105         sin1 = (struct sockaddr_in*)&ss1;
106         sin2 = (struct sockaddr_in*)&ss2;
107         tt_int_op(ntohl(sin1->sin_addr.s_addr), ==, 0x7f000001);
108         tt_int_op(ntohl(sin2->sin_addr.s_addr), ==, 0x7f000001);
109         tt_int_op(sin1->sin_port, !=, sin2->sin_port);
110
111         tt_ptr_op(evconnlistener_get_base(listener1), ==, base);
112         tt_ptr_op(evconnlistener_get_base(listener2), ==, base);
113
114         fd1 = fd2 = fd3 = -1;
115         evutil_socket_connect(&fd1, (struct sockaddr*)&ss1, slen1);
116         evutil_socket_connect(&fd2, (struct sockaddr*)&ss1, slen1);
117         evutil_socket_connect(&fd3, (struct sockaddr*)&ss2, slen2);
118
119 #ifdef WIN32
120         Sleep(100); /* XXXX this is a stupid stopgap. */
121 #endif
122         event_base_dispatch(base);
123
124         tt_int_op(count1, ==, 0);
125         tt_int_op(count2, ==, 0);
126
127 end:
128         if (fd1>=0)
129                 EVUTIL_CLOSESOCKET(fd1);
130         if (fd2>=0)
131                 EVUTIL_CLOSESOCKET(fd2);
132         if (fd3>=0)
133                 EVUTIL_CLOSESOCKET(fd3);
134         if (listener1)
135                 evconnlistener_free(listener1);
136         if (listener2)
137                 evconnlistener_free(listener2);
138 }
139
140 static void
141 errorcb(struct evconnlistener *lis, void *data_)
142 {
143         int *data = data_;
144         *data = 1000;
145         evconnlistener_disable(lis);
146 }
147
148 static void
149 regress_listener_error(void *arg)
150 {
151         struct basic_test_data *data = arg;
152         struct event_base *base = data->base;
153         struct evconnlistener *listener = NULL;
154         int count = 1;
155         unsigned int flags = LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE;
156
157         if (data->setup_data && strstr((char*)data->setup_data, "ts")) {
158                 flags |= LEV_OPT_THREADSAFE;
159         }
160
161         /* send, so that pair[0] will look 'readable'*/
162         send(data->pair[1], "hello", 5, 0);
163
164         /* Start a listener with a bogus socket. */
165         listener = evconnlistener_new(base, acceptcb, &count,
166             flags, 0,
167             data->pair[0]);
168         tt_assert(listener);
169
170         evconnlistener_set_error_cb(listener, errorcb);
171
172         tt_assert(listener);
173
174         event_base_dispatch(base);
175         tt_int_op(count,==,1000); /* set by error cb */
176
177 end:
178         if (listener)
179                 evconnlistener_free(listener);
180 }
181
182 struct testcase_t listener_testcases[] = {
183
184         { "randport", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
185           &basic_setup, NULL},
186
187         { "randport_ts", regress_pick_a_port, TT_FORK|TT_NEED_BASE,
188           &basic_setup, (char*)"ts"},
189
190         { "error", regress_listener_error,
191           TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
192           &basic_setup, NULL},
193
194         { "error_ts", regress_listener_error,
195           TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR,
196           &basic_setup, (char*)"ts"},
197
198         END_OF_TESTCASES,
199 };
200
201 struct testcase_t listener_iocp_testcases[] = {
202         { "randport", regress_pick_a_port,
203           TT_FORK|TT_NEED_BASE|TT_ENABLE_IOCP,
204           &basic_setup, NULL},
205
206         { "error", regress_listener_error,
207           TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR|TT_ENABLE_IOCP,
208           &basic_setup, NULL},
209
210         END_OF_TESTCASES,
211 };