xref: /netbsd-src/external/bsd/libevent/dist/test/regress_et.c (revision 7e68cdd7306a8b6c32d6a32c16ba01e5a2ddc083)
1*7e68cdd7Schristos /*	$NetBSD: regress_et.c,v 1.4 2021/04/07 03:36:48 christos Exp $	*/
2*7e68cdd7Schristos 
36ecf6635Schristos /*
46ecf6635Schristos  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
286ecf6635Schristos #include "../util-internal.h"
296ecf6635Schristos #include "event2/event-config.h"
306ecf6635Schristos #include <sys/cdefs.h>
31*7e68cdd7Schristos __RCSID("$NetBSD: regress_et.c,v 1.4 2021/04/07 03:36:48 christos Exp $");
326ecf6635Schristos 
330d738af4Schristos #ifdef _WIN32
346ecf6635Schristos #include <winsock2.h>
356ecf6635Schristos #endif
366ecf6635Schristos #include <sys/types.h>
376ecf6635Schristos #include <sys/stat.h>
380d738af4Schristos #ifdef EVENT__HAVE_SYS_SOCKET_H
396ecf6635Schristos #include <sys/socket.h>
406ecf6635Schristos #endif
416ecf6635Schristos #include <fcntl.h>
426ecf6635Schristos #include <stdlib.h>
436ecf6635Schristos #include <stdio.h>
446ecf6635Schristos #include <string.h>
450d738af4Schristos #ifndef _WIN32
466ecf6635Schristos #include <sys/time.h>
476ecf6635Schristos #include <unistd.h>
486ecf6635Schristos #endif
496ecf6635Schristos #include <errno.h>
506ecf6635Schristos 
516ecf6635Schristos #include "event2/event.h"
526ecf6635Schristos #include "event2/util.h"
536ecf6635Schristos 
546ecf6635Schristos #include "regress.h"
556ecf6635Schristos 
566ecf6635Schristos static int was_et = 0;
576ecf6635Schristos 
base_supports_et(struct event_base * base)58*7e68cdd7Schristos static int base_supports_et(struct event_base *base)
59*7e68cdd7Schristos {
60*7e68cdd7Schristos 	return
61*7e68cdd7Schristos 		(!strcmp(event_base_get_method(base), "epoll") ||
62*7e68cdd7Schristos 		!strcmp(event_base_get_method(base), "epoll (with changelist)") ||
63*7e68cdd7Schristos 		!strcmp(event_base_get_method(base), "kqueue"));
64*7e68cdd7Schristos }
65*7e68cdd7Schristos 
666ecf6635Schristos static void
read_cb(evutil_socket_t fd,short event,void * arg)676ecf6635Schristos read_cb(evutil_socket_t fd, short event, void *arg)
686ecf6635Schristos {
696ecf6635Schristos 	char buf;
706ecf6635Schristos 	int len;
716ecf6635Schristos 
726ecf6635Schristos 	len = recv(fd, &buf, sizeof(buf), 0);
736ecf6635Schristos 
746ecf6635Schristos 	called++;
756ecf6635Schristos 	if (event & EV_ET)
766ecf6635Schristos 		was_et = 1;
776ecf6635Schristos 
786ecf6635Schristos 	if (!len)
796ecf6635Schristos 		event_del(arg);
806ecf6635Schristos }
816ecf6635Schristos 
826ecf6635Schristos static void
test_edgetriggered(void * data_)83*7e68cdd7Schristos test_edgetriggered(void *data_)
846ecf6635Schristos {
85*7e68cdd7Schristos 	struct basic_test_data *data = data_;
86*7e68cdd7Schristos 	struct event_base *base = data->base;
87*7e68cdd7Schristos 	evutil_socket_t *xpair = data->pair;
886ecf6635Schristos 	struct event *ev = NULL;
896ecf6635Schristos 	const char *test = "test string";
906ecf6635Schristos 	int supports_et;
916ecf6635Schristos 
926ecf6635Schristos 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
936ecf6635Schristos 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
946ecf6635Schristos 	 * socket, even though there is no state change, so we don't actually
956ecf6635Schristos 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
966ecf6635Schristos 	 * problem.
976ecf6635Schristos 	 */
986ecf6635Schristos 
996ecf6635Schristos 	called = was_et = 0;
1006ecf6635Schristos 
101883110aaSchristos 	tt_int_op(send(xpair[0], test, (int)strlen(test)+1, 0), >, 0);
102*7e68cdd7Schristos 	tt_int_op(shutdown(xpair[0], EVUTIL_SHUT_WR), ==, 0);
1036ecf6635Schristos 
104*7e68cdd7Schristos 	supports_et = base_supports_et(base);
1056ecf6635Schristos 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
1066ecf6635Schristos 				"support edge-triggering", event_base_get_method(base),
1076ecf6635Schristos 				supports_et?"":"not "));
1086ecf6635Schristos 
109*7e68cdd7Schristos 	/* Initialize one event */
110883110aaSchristos 	ev = event_new(base, xpair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
111*7e68cdd7Schristos 	tt_assert(ev != NULL);
112*7e68cdd7Schristos 	tt_int_op(event_add(ev, NULL), ==, 0);
1136ecf6635Schristos 
1146ecf6635Schristos 	/* We're going to call the dispatch function twice.  The first invocation
115883110aaSchristos 	 * will read a single byte from xpair[1] in either case.  If we're edge
1166ecf6635Schristos 	 * triggered, we'll only see the event once (since we only see transitions
1176ecf6635Schristos 	 * from no data to data), so the second invocation of event_base_loop will
1186ecf6635Schristos 	 * do nothing.  If we're level triggered, the second invocation of
1196ecf6635Schristos 	 * event_base_loop will also activate the event (because there's still
1206ecf6635Schristos 	 * data to read). */
121*7e68cdd7Schristos 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
122*7e68cdd7Schristos 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
1236ecf6635Schristos 
1246ecf6635Schristos 	if (supports_et) {
1256ecf6635Schristos 		tt_int_op(called, ==, 1);
1266ecf6635Schristos 		tt_assert(was_et);
1276ecf6635Schristos 	} else {
1286ecf6635Schristos 		tt_int_op(called, ==, 2);
1296ecf6635Schristos 		tt_assert(!was_et);
1306ecf6635Schristos 	}
1316ecf6635Schristos 
1326ecf6635Schristos end:
1336ecf6635Schristos 	if (ev) {
1346ecf6635Schristos 		event_del(ev);
1356ecf6635Schristos 		event_free(ev);
1366ecf6635Schristos 	}
1376ecf6635Schristos }
1386ecf6635Schristos 
1396ecf6635Schristos static void
test_edgetriggered_mix_error(void * data_)1406ecf6635Schristos test_edgetriggered_mix_error(void *data_)
1416ecf6635Schristos {
1426ecf6635Schristos 	struct basic_test_data *data = data_;
1436ecf6635Schristos 	struct event_base *base = NULL;
1446ecf6635Schristos 	struct event *ev_et=NULL, *ev_lt=NULL;
1456ecf6635Schristos 
1460d738af4Schristos #ifdef EVENT__DISABLE_DEBUG_MODE
1476ecf6635Schristos 	if (1)
1486ecf6635Schristos 		tt_skip();
1496ecf6635Schristos #endif
1506ecf6635Schristos 
1510d738af4Schristos 	if (!libevent_tests_running_in_debug_mode)
1526ecf6635Schristos 		event_enable_debug_mode();
1536ecf6635Schristos 
1546ecf6635Schristos 	base = event_base_new();
1556ecf6635Schristos 
1566ecf6635Schristos 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
1576ecf6635Schristos 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
1586ecf6635Schristos 	tt_assert(ev_et);
1596ecf6635Schristos 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
1606ecf6635Schristos 	tt_assert(ev_lt);
1616ecf6635Schristos 
1626ecf6635Schristos 	/* Add edge-triggered, then level-triggered.  Get an error. */
1636ecf6635Schristos 	tt_int_op(0, ==, event_add(ev_et, NULL));
1646ecf6635Schristos 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
1656ecf6635Schristos 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
1666ecf6635Schristos 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
1676ecf6635Schristos 
1686ecf6635Schristos 	tt_int_op(0, ==, event_del(ev_et));
1696ecf6635Schristos 	/* Add level-triggered, then edge-triggered.  Get an error. */
1706ecf6635Schristos 	tt_int_op(0, ==, event_add(ev_lt, NULL));
1716ecf6635Schristos 	tt_int_op(-1, ==, event_add(ev_et, NULL));
1726ecf6635Schristos 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
1736ecf6635Schristos 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
1746ecf6635Schristos 
1756ecf6635Schristos end:
1766ecf6635Schristos 	if (ev_et)
1776ecf6635Schristos 		event_free(ev_et);
1786ecf6635Schristos 	if (ev_lt)
1796ecf6635Schristos 		event_free(ev_lt);
1806ecf6635Schristos 	if (base)
1816ecf6635Schristos 		event_base_free(base);
1826ecf6635Schristos }
1836ecf6635Schristos 
184*7e68cdd7Schristos static int read_notification_count;
185*7e68cdd7Schristos static int last_read_notification_was_et;
186*7e68cdd7Schristos static void
read_notification_cb(evutil_socket_t fd,short event,void * arg)187*7e68cdd7Schristos read_notification_cb(evutil_socket_t fd, short event, void *arg)
188*7e68cdd7Schristos {
189*7e68cdd7Schristos 	read_notification_count++;
190*7e68cdd7Schristos 	last_read_notification_was_et = (event & EV_ET);
191*7e68cdd7Schristos }
192*7e68cdd7Schristos 
193*7e68cdd7Schristos static int write_notification_count;
194*7e68cdd7Schristos static int last_write_notification_was_et;
195*7e68cdd7Schristos static void
write_notification_cb(evutil_socket_t fd,short event,void * arg)196*7e68cdd7Schristos write_notification_cb(evutil_socket_t fd, short event, void *arg)
197*7e68cdd7Schristos {
198*7e68cdd7Schristos 	write_notification_count++;
199*7e68cdd7Schristos 	last_write_notification_was_et = (event & EV_ET);
200*7e68cdd7Schristos }
201*7e68cdd7Schristos 
202*7e68cdd7Schristos /* After two or more events have been registered for the same
203*7e68cdd7Schristos  * file descriptor using EV_ET, if one of the events is
204*7e68cdd7Schristos  * deleted, then the epoll_ctl() call issued by libevent drops
205*7e68cdd7Schristos  * the EPOLLET flag resulting in level triggered
206*7e68cdd7Schristos  * notifications.
207*7e68cdd7Schristos  */
208*7e68cdd7Schristos static void
test_edge_triggered_multiple_events(void * data_)209*7e68cdd7Schristos test_edge_triggered_multiple_events(void *data_)
210*7e68cdd7Schristos {
211*7e68cdd7Schristos 	struct basic_test_data *data = data_;
212*7e68cdd7Schristos 	struct event *read_ev = NULL;
213*7e68cdd7Schristos 	struct event *write_ev = NULL;
214*7e68cdd7Schristos 	const char c = 'A';
215*7e68cdd7Schristos 	struct event_base *base = data->base;
216*7e68cdd7Schristos 	evutil_socket_t *xpair = data->pair;
217*7e68cdd7Schristos 
218*7e68cdd7Schristos 	if (!base_supports_et(base)) {
219*7e68cdd7Schristos 		tt_skip();
220*7e68cdd7Schristos 		return;
221*7e68cdd7Schristos 	}
222*7e68cdd7Schristos 
223*7e68cdd7Schristos 	read_notification_count = 0;
224*7e68cdd7Schristos 	last_read_notification_was_et = 0;
225*7e68cdd7Schristos 	write_notification_count = 0;
226*7e68cdd7Schristos 	last_write_notification_was_et = 0;
227*7e68cdd7Schristos 
228*7e68cdd7Schristos 	/* Make xpair[1] readable */
229*7e68cdd7Schristos 	tt_int_op(send(xpair[0], &c, 1, 0), >, 0);
230*7e68cdd7Schristos 
231*7e68cdd7Schristos 	read_ev = event_new(base, xpair[1], EV_READ|EV_ET|EV_PERSIST,
232*7e68cdd7Schristos 		read_notification_cb, NULL);
233*7e68cdd7Schristos 	write_ev = event_new(base, xpair[1], EV_WRITE|EV_ET|EV_PERSIST,
234*7e68cdd7Schristos 		write_notification_cb, NULL);
235*7e68cdd7Schristos 
236*7e68cdd7Schristos 	event_add(read_ev, NULL);
237*7e68cdd7Schristos 	event_add(write_ev, NULL);
238*7e68cdd7Schristos 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
239*7e68cdd7Schristos 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
240*7e68cdd7Schristos 
241*7e68cdd7Schristos 	tt_assert(last_read_notification_was_et);
242*7e68cdd7Schristos 	tt_int_op(read_notification_count, ==, 1);
243*7e68cdd7Schristos 	tt_assert(last_write_notification_was_et);
244*7e68cdd7Schristos 	tt_int_op(write_notification_count, ==, 1);
245*7e68cdd7Schristos 
246*7e68cdd7Schristos 	event_del(read_ev);
247*7e68cdd7Schristos 
248*7e68cdd7Schristos 	/* trigger acitivity second time for the backend that can have multiple
249*7e68cdd7Schristos 	 * events for one fd (like kqueue) */
250*7e68cdd7Schristos 	close(xpair[0]);
251*7e68cdd7Schristos 	xpair[0] = -1;
252*7e68cdd7Schristos 
253*7e68cdd7Schristos 	/* Verify that we are still edge-triggered for write notifications */
254*7e68cdd7Schristos 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
255*7e68cdd7Schristos 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
256*7e68cdd7Schristos 	tt_assert(last_write_notification_was_et);
257*7e68cdd7Schristos 	tt_int_op(write_notification_count, ==, 2);
258*7e68cdd7Schristos 
259*7e68cdd7Schristos end:
260*7e68cdd7Schristos 	if (read_ev)
261*7e68cdd7Schristos 		event_free(read_ev);
262*7e68cdd7Schristos 	if (write_ev)
263*7e68cdd7Schristos 		event_free(write_ev);
264*7e68cdd7Schristos }
265*7e68cdd7Schristos 
2666ecf6635Schristos struct testcase_t edgetriggered_testcases[] = {
267*7e68cdd7Schristos 	{ "et", test_edgetriggered,
268*7e68cdd7Schristos 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
2696ecf6635Schristos 	{ "et_mix_error", test_edgetriggered_mix_error,
2706ecf6635Schristos 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
271*7e68cdd7Schristos 	{ "et_multiple_events", test_edge_triggered_multiple_events,
272*7e68cdd7Schristos 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
2736ecf6635Schristos 	END_OF_TESTCASES
2746ecf6635Schristos };
275