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