xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_et.c (revision 2dd295436a0082eb4f8d294f4aa73c223413d0f2)
1 /*	$NetBSD: regress_et.c,v 1.5 2020/05/25 20:47:34 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 void
57 read_cb(evutil_socket_t fd, short event, void *arg)
58 {
59 	char buf;
60 	int len;
61 
62 	len = recv(fd, &buf, sizeof(buf), 0);
63 
64 	called++;
65 	if (event & EV_ET)
66 		was_et = 1;
67 
68 	if (!len)
69 		event_del(arg);
70 }
71 
72 #ifndef SHUT_WR
73 #define SHUT_WR 1
74 #endif
75 
76 #ifdef _WIN32
77 #define LOCAL_SOCKETPAIR_AF AF_INET
78 #else
79 #define LOCAL_SOCKETPAIR_AF AF_UNIX
80 #endif
81 
82 static void
83 test_edgetriggered(void *et)
84 {
85 	struct event *ev = NULL;
86 	struct event_base *base = NULL;
87 	const char *test = "test string";
88 	evutil_socket_t pair[2] = {-1,-1};
89 	int supports_et;
90 
91 	/* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick),
92 	 * doing a "recv" on an AF_UNIX socket resets the readability of the
93 	 * socket, even though there is no state change, so we don't actually
94 	 * get edge-triggered behavior.  Yuck!  Linux 3.1.9 didn't have this
95 	 * problem.
96 	 */
97 #ifdef __linux__
98 	if (evutil_ersatz_socketpair_(AF_INET, SOCK_STREAM, 0, pair) == -1) {
99 		tt_abort_perror("socketpair");
100 	}
101 #else
102 	if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) {
103 		tt_abort_perror("socketpair");
104 	}
105 #endif
106 
107 	called = was_et = 0;
108 
109 	tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0);
110 	shutdown(pair[0], SHUT_WR);
111 
112 	/* Initalize the event library */
113 	base = event_base_new();
114 
115 	if (!strcmp(event_base_get_method(base), "epoll") ||
116 	    !strcmp(event_base_get_method(base), "epoll (with changelist)") ||
117 	    !strcmp(event_base_get_method(base), "kqueue"))
118 		supports_et = 1;
119 	else
120 		supports_et = 0;
121 
122 	TT_BLATHER(("Checking for edge-triggered events with %s, which should %s"
123 				"support edge-triggering", event_base_get_method(base),
124 				supports_et?"":"not "));
125 
126 	/* Initalize one event */
127 	ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev);
128 
129 	event_add(ev, NULL);
130 
131 	/* We're going to call the dispatch function twice.  The first invocation
132 	 * will read a single byte from pair[1] in either case.  If we're edge
133 	 * triggered, we'll only see the event once (since we only see transitions
134 	 * from no data to data), so the second invocation of event_base_loop will
135 	 * do nothing.  If we're level triggered, the second invocation of
136 	 * event_base_loop will also activate the event (because there's still
137 	 * data to read). */
138 	event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE);
139 	event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE);
140 
141 	if (supports_et) {
142 		tt_int_op(called, ==, 1);
143 		tt_assert(was_et);
144 	} else {
145 		tt_int_op(called, ==, 2);
146 		tt_assert(!was_et);
147 	}
148 
149  end:
150 	if (ev) {
151 		event_del(ev);
152 		event_free(ev);
153 	}
154 	if (base)
155 		event_base_free(base);
156 	evutil_closesocket(pair[0]);
157 	evutil_closesocket(pair[1]);
158 }
159 
160 static void
161 test_edgetriggered_mix_error(void *data_)
162 {
163 	struct basic_test_data *data = data_;
164 	struct event_base *base = NULL;
165 	struct event *ev_et=NULL, *ev_lt=NULL;
166 
167 #ifdef EVENT__DISABLE_DEBUG_MODE
168 	if (1)
169 		tt_skip();
170 #endif
171 
172 	if (!libevent_tests_running_in_debug_mode)
173 		event_enable_debug_mode();
174 
175 	base = event_base_new();
176 
177 	/* try mixing edge-triggered and level-triggered to make sure it fails*/
178 	ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et);
179 	tt_assert(ev_et);
180 	ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt);
181 	tt_assert(ev_lt);
182 
183 	/* Add edge-triggered, then level-triggered.  Get an error. */
184 	tt_int_op(0, ==, event_add(ev_et, NULL));
185 	tt_int_op(-1, ==, event_add(ev_lt, NULL));
186 	tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL));
187 	tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL));
188 
189 	tt_int_op(0, ==, event_del(ev_et));
190 	/* Add level-triggered, then edge-triggered.  Get an error. */
191 	tt_int_op(0, ==, event_add(ev_lt, NULL));
192 	tt_int_op(-1, ==, event_add(ev_et, NULL));
193 	tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL));
194 	tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL));
195 
196 end:
197 	if (ev_et)
198 		event_free(ev_et);
199 	if (ev_lt)
200 		event_free(ev_lt);
201 	if (base)
202 		event_base_free(base);
203 }
204 
205 struct testcase_t edgetriggered_testcases[] = {
206 	{ "et", test_edgetriggered, TT_FORK, NULL, NULL },
207 	{ "et_mix_error", test_edgetriggered_mix_error,
208 	  TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL },
209 	END_OF_TESTCASES
210 };
211