xref: /netbsd-src/external/bsd/libevent/dist/test/regress_et.c (revision 7e68cdd7306a8b6c32d6a32c16ba01e5a2ddc083)
1 /*	$NetBSD: regress_et.c,v 1.4 2021/04/07 03:36:48 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "../util-internal.h"
29 #include "event2/event-config.h"
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: regress_et.c,v 1.4 2021/04/07 03:36:48 christos Exp $");
32 
33 #ifdef _WIN32
34 #include <winsock2.h>
35 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #ifdef EVENT__HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #ifndef _WIN32
46 #include <sys/time.h>
47 #include <unistd.h>
48 #endif
49 #include <errno.h>
50 
51 #include "event2/event.h"
52 #include "event2/util.h"
53 
54 #include "regress.h"
55 
56 static int was_et = 0;
57 
base_supports_et(struct event_base * base)58 static int base_supports_et(struct event_base *base)
59 {
60 	return
61 		(!strcmp(event_base_get_method(base), "epoll") ||
62 		!strcmp(event_base_get_method(base), "epoll (with changelist)") ||
63 		!strcmp(event_base_get_method(base), "kqueue"));
64 }
65 
66 static void
read_cb(evutil_socket_t fd,short event,void * arg)67 read_cb(evutil_socket_t fd, short event, void *arg)
68 {
69 	char buf;
70 	int len;
71 
72 	len = recv(fd, &buf, sizeof(buf), 0);
73 
74 	called++;
75 	if (event & EV_ET)
76 		was_et = 1;
77 
78 	if (!len)
79 		event_del(arg);
80 }
81 
82 static void
test_edgetriggered(void * data_)83 test_edgetriggered(void *data_)
84 {
85 	struct basic_test_data *data = data_;
86 	struct event_base *base = data->base;
87 	evutil_socket_t *xpair = data->pair;
88 	struct event *ev = NULL;
89 	const char *test = "test string";
90 	int supports_et;
91 
92 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
93 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
94 	 * socket, even though there is no state change, so we don't actually
95 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
96 	 * problem.
97 	 */
98 
99 	called = was_et = 0;
100 
101 	tt_int_op(send(xpair[0], test, (int)strlen(test)+1, 0), >, 0);
102 	tt_int_op(shutdown(xpair[0], EVUTIL_SHUT_WR), ==, 0);
103 
104 	supports_et = base_supports_et(base);
105 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
106 				"support edge-triggering", event_base_get_method(base),
107 				supports_et?"":"not "));
108 
109 	/* Initialize one event */
110 	ev = event_new(base, xpair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
111 	tt_assert(ev != NULL);
112 	tt_int_op(event_add(ev, NULL), ==, 0);
113 
114 	/* We're going to call the dispatch function twice.  The first invocation
115 	 * will read a single byte from xpair[1] in either case.  If we're edge
116 	 * triggered, we'll only see the event once (since we only see transitions
117 	 * from no data to data), so the second invocation of event_base_loop will
118 	 * do nothing.  If we're level triggered, the second invocation of
119 	 * event_base_loop will also activate the event (because there's still
120 	 * data to read). */
121 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
122 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
123 
124 	if (supports_et) {
125 		tt_int_op(called, ==, 1);
126 		tt_assert(was_et);
127 	} else {
128 		tt_int_op(called, ==, 2);
129 		tt_assert(!was_et);
130 	}
131 
132 end:
133 	if (ev) {
134 		event_del(ev);
135 		event_free(ev);
136 	}
137 }
138 
139 static void
test_edgetriggered_mix_error(void * data_)140 test_edgetriggered_mix_error(void *data_)
141 {
142 	struct basic_test_data *data = data_;
143 	struct event_base *base = NULL;
144 	struct event *ev_et=NULL, *ev_lt=NULL;
145 
146 #ifdef EVENT__DISABLE_DEBUG_MODE
147 	if (1)
148 		tt_skip();
149 #endif
150 
151 	if (!libevent_tests_running_in_debug_mode)
152 		event_enable_debug_mode();
153 
154 	base = event_base_new();
155 
156 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
157 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
158 	tt_assert(ev_et);
159 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
160 	tt_assert(ev_lt);
161 
162 	/* Add edge-triggered, then level-triggered.  Get an error. */
163 	tt_int_op(0, ==, event_add(ev_et, NULL));
164 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
165 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
166 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
167 
168 	tt_int_op(0, ==, event_del(ev_et));
169 	/* Add level-triggered, then edge-triggered.  Get an error. */
170 	tt_int_op(0, ==, event_add(ev_lt, NULL));
171 	tt_int_op(-1, ==, event_add(ev_et, NULL));
172 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
173 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
174 
175 end:
176 	if (ev_et)
177 		event_free(ev_et);
178 	if (ev_lt)
179 		event_free(ev_lt);
180 	if (base)
181 		event_base_free(base);
182 }
183 
184 static int read_notification_count;
185 static int last_read_notification_was_et;
186 static void
read_notification_cb(evutil_socket_t fd,short event,void * arg)187 read_notification_cb(evutil_socket_t fd, short event, void *arg)
188 {
189 	read_notification_count++;
190 	last_read_notification_was_et = (event & EV_ET);
191 }
192 
193 static int write_notification_count;
194 static int last_write_notification_was_et;
195 static void
write_notification_cb(evutil_socket_t fd,short event,void * arg)196 write_notification_cb(evutil_socket_t fd, short event, void *arg)
197 {
198 	write_notification_count++;
199 	last_write_notification_was_et = (event & EV_ET);
200 }
201 
202 /* After two or more events have been registered for the same
203  * file descriptor using EV_ET, if one of the events is
204  * deleted, then the epoll_ctl() call issued by libevent drops
205  * the EPOLLET flag resulting in level triggered
206  * notifications.
207  */
208 static void
test_edge_triggered_multiple_events(void * data_)209 test_edge_triggered_multiple_events(void *data_)
210 {
211 	struct basic_test_data *data = data_;
212 	struct event *read_ev = NULL;
213 	struct event *write_ev = NULL;
214 	const char c = 'A';
215 	struct event_base *base = data->base;
216 	evutil_socket_t *xpair = data->pair;
217 
218 	if (!base_supports_et(base)) {
219 		tt_skip();
220 		return;
221 	}
222 
223 	read_notification_count = 0;
224 	last_read_notification_was_et = 0;
225 	write_notification_count = 0;
226 	last_write_notification_was_et = 0;
227 
228 	/* Make xpair[1] readable */
229 	tt_int_op(send(xpair[0], &c, 1, 0), >, 0);
230 
231 	read_ev = event_new(base, xpair[1], EV_READ|EV_ET|EV_PERSIST,
232 		read_notification_cb, NULL);
233 	write_ev = event_new(base, xpair[1], EV_WRITE|EV_ET|EV_PERSIST,
234 		write_notification_cb, NULL);
235 
236 	event_add(read_ev, NULL);
237 	event_add(write_ev, NULL);
238 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
239 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
240 
241 	tt_assert(last_read_notification_was_et);
242 	tt_int_op(read_notification_count, ==, 1);
243 	tt_assert(last_write_notification_was_et);
244 	tt_int_op(write_notification_count, ==, 1);
245 
246 	event_del(read_ev);
247 
248 	/* trigger acitivity second time for the backend that can have multiple
249 	 * events for one fd (like kqueue) */
250 	close(xpair[0]);
251 	xpair[0] = -1;
252 
253 	/* Verify that we are still edge-triggered for write notifications */
254 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
255 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
256 	tt_assert(last_write_notification_was_et);
257 	tt_int_op(write_notification_count, ==, 2);
258 
259 end:
260 	if (read_ev)
261 		event_free(read_ev);
262 	if (write_ev)
263 		event_free(write_ev);
264 }
265 
266 struct testcase_t edgetriggered_testcases[] = {
267 	{ "et", test_edgetriggered,
268 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
269 	{ "et_mix_error", test_edgetriggered_mix_error,
270 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
271 	{ "et_multiple_events", test_edge_triggered_multiple_events,
272 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
273 	END_OF_TESTCASES
274 };
275