xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_et.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: regress_et.c,v 1.6 2024/08/18 20:47:23 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 
31 #ifdef _WIN32
32 #include <winsock2.h>
33 #endif
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #ifdef EVENT__HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42 #include <string.h>
43 #ifndef _WIN32
44 #include <sys/time.h>
45 #include <unistd.h>
46 #endif
47 #include <errno.h>
48 
49 #include "event2/event.h"
50 #include "event2/util.h"
51 
52 #include "regress.h"
53 
54 static int was_et = 0;
55 
56 static int base_supports_et(struct event_base *base)
57 {
58 	return
59 		(!strcmp(event_base_get_method(base), "epoll") ||
60 		!strcmp(event_base_get_method(base), "epoll (with changelist)") ||
61 		!strcmp(event_base_get_method(base), "kqueue"));
62 }
63 
64 static void
65 read_cb(evutil_socket_t fd, short event, void *arg)
66 {
67 	char buf;
68 	int len;
69 
70 	len = recv(fd, &buf, sizeof(buf), 0);
71 
72 	called++;
73 	if (event & EV_ET)
74 		was_et = 1;
75 
76 	if (!len)
77 		event_del(arg);
78 }
79 
80 static void
81 test_edgetriggered(void *data_)
82 {
83 	struct basic_test_data *data = data_;
84 	struct event_base *base = data->base;
85 	evutil_socket_t *pair = data->pair;
86 	struct event *ev = NULL;
87 	const char *test = "test string";
88 	int supports_et;
89 
90 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
91 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
92 	 * socket, even though there is no state change, so we don't actually
93 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
94 	 * problem.
95 	 */
96 
97 	called = was_et = 0;
98 
99 	tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0);
100 	tt_int_op(shutdown(pair[0], EVUTIL_SHUT_WR), ==, 0);
101 
102 	supports_et = base_supports_et(base);
103 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
104 				"support edge-triggering", event_base_get_method(base),
105 				supports_et?"":"not "));
106 
107 	/* Initialize one event */
108 	ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
109 	tt_assert(ev != NULL);
110 	tt_int_op(event_add(ev, NULL), ==, 0);
111 
112 	/* We're going to call the dispatch function twice.  The first invocation
113 	 * will read a single byte from pair[1] in either case.  If we're edge
114 	 * triggered, we'll only see the event once (since we only see transitions
115 	 * from no data to data), so the second invocation of event_base_loop will
116 	 * do nothing.  If we're level triggered, the second invocation of
117 	 * event_base_loop will also activate the event (because there's still
118 	 * data to read). */
119 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
120 	tt_int_op(event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE), ==, 0);
121 
122 	if (supports_et) {
123 		tt_int_op(called, ==, 1);
124 		tt_assert(was_et);
125 	} else {
126 		tt_int_op(called, ==, 2);
127 		tt_assert(!was_et);
128 	}
129 
130 end:
131 	if (ev) {
132 		event_del(ev);
133 		event_free(ev);
134 	}
135 }
136 
137 static void
138 test_edgetriggered_mix_error(void *data_)
139 {
140 	struct basic_test_data *data = data_;
141 	struct event_base *base = NULL;
142 	struct event *ev_et=NULL, *ev_lt=NULL;
143 
144 #ifdef EVENT__DISABLE_DEBUG_MODE
145 	if (1)
146 		tt_skip();
147 #endif
148 
149 	if (!libevent_tests_running_in_debug_mode)
150 		event_enable_debug_mode();
151 
152 	base = event_base_new();
153 
154 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
155 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
156 	tt_assert(ev_et);
157 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
158 	tt_assert(ev_lt);
159 
160 	/* Add edge-triggered, then level-triggered.  Get an error. */
161 	tt_int_op(0, ==, event_add(ev_et, NULL));
162 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
163 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
164 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
165 
166 	tt_int_op(0, ==, event_del(ev_et));
167 	/* Add level-triggered, then edge-triggered.  Get an error. */
168 	tt_int_op(0, ==, event_add(ev_lt, NULL));
169 	tt_int_op(-1, ==, event_add(ev_et, NULL));
170 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
171 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
172 
173 end:
174 	if (ev_et)
175 		event_free(ev_et);
176 	if (ev_lt)
177 		event_free(ev_lt);
178 	if (base)
179 		event_base_free(base);
180 }
181 
182 static int read_notification_count;
183 static int last_read_notification_was_et;
184 static void
185 read_notification_cb(evutil_socket_t fd, short event, void *arg)
186 {
187 	read_notification_count++;
188 	last_read_notification_was_et = (event & EV_ET);
189 }
190 
191 static int write_notification_count;
192 static int last_write_notification_was_et;
193 static void
194 write_notification_cb(evutil_socket_t fd, short event, void *arg)
195 {
196 	write_notification_count++;
197 	last_write_notification_was_et = (event & EV_ET);
198 }
199 
200 /* After two or more events have been registered for the same
201  * file descriptor using EV_ET, if one of the events is
202  * deleted, then the epoll_ctl() call issued by libevent drops
203  * the EPOLLET flag resulting in level triggered
204  * notifications.
205  */
206 static void
207 test_edge_triggered_multiple_events(void *data_)
208 {
209 	struct basic_test_data *data = data_;
210 	struct event *read_ev = NULL;
211 	struct event *write_ev = NULL;
212 	const char c = 'A';
213 	struct event_base *base = data->base;
214 	evutil_socket_t *pair = data->pair;
215 
216 	if (!base_supports_et(base)) {
217 		tt_skip();
218 		return;
219 	}
220 
221 	read_notification_count = 0;
222 	last_read_notification_was_et = 0;
223 	write_notification_count = 0;
224 	last_write_notification_was_et = 0;
225 
226 	/* Make pair[1] readable */
227 	tt_int_op(send(pair[0], &c, 1, 0), >, 0);
228 
229 	read_ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST,
230 		read_notification_cb, NULL);
231 	write_ev = event_new(base, pair[1], EV_WRITE|EV_ET|EV_PERSIST,
232 		write_notification_cb, NULL);
233 
234 	event_add(read_ev, NULL);
235 	event_add(write_ev, NULL);
236 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
237 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
238 
239 	tt_assert(last_read_notification_was_et);
240 	tt_int_op(read_notification_count, ==, 1);
241 	tt_assert(last_write_notification_was_et);
242 	tt_int_op(write_notification_count, ==, 1);
243 
244 	event_del(read_ev);
245 
246 	/* trigger acitivity second time for the backend that can have multiple
247 	 * events for one fd (like kqueue) */
248 	close(pair[0]);
249 	pair[0] = -1;
250 
251 	/* Verify that we are still edge-triggered for write notifications */
252 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
253 	event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
254 	tt_assert(last_write_notification_was_et);
255 	tt_int_op(write_notification_count, ==, 2);
256 
257 end:
258 	if (read_ev)
259 		event_free(read_ev);
260 	if (write_ev)
261 		event_free(write_ev);
262 }
263 
264 struct testcase_t edgetriggered_testcases[] = {
265 	{ "et", test_edgetriggered,
266 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
267 	{ "et_mix_error", test_edgetriggered_mix_error,
268 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
269 	{ "et_multiple_events", test_edge_triggered_multiple_events,
270 	  TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR, &basic_setup, NULL },
271 	END_OF_TESTCASES
272 };
273