michael@0: /* michael@0: * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #include "../util-internal.h" michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_SOCKET_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: #include "event2/event.h" michael@0: #include "event2/util.h" michael@0: michael@0: #include "regress.h" michael@0: michael@0: static int was_et = 0; michael@0: michael@0: static void michael@0: read_cb(evutil_socket_t fd, short event, void *arg) michael@0: { michael@0: char buf; michael@0: int len; michael@0: michael@0: len = recv(fd, &buf, sizeof(buf), 0); michael@0: michael@0: called++; michael@0: if (event & EV_ET) michael@0: was_et = 1; michael@0: michael@0: if (!len) michael@0: event_del(arg); michael@0: } michael@0: michael@0: #ifndef SHUT_WR michael@0: #define SHUT_WR 1 michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: #define LOCAL_SOCKETPAIR_AF AF_INET michael@0: #else michael@0: #define LOCAL_SOCKETPAIR_AF AF_UNIX michael@0: #endif michael@0: michael@0: static void michael@0: test_edgetriggered(void *et) michael@0: { michael@0: struct event *ev = NULL; michael@0: struct event_base *base = NULL; michael@0: const char *test = "test string"; michael@0: evutil_socket_t pair[2] = {-1,-1}; michael@0: int supports_et; michael@0: michael@0: /* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick), michael@0: * doing a "recv" on an AF_UNIX socket resets the readability of the michael@0: * socket, even though there is no state change, so we don't actually michael@0: * get edge-triggered behavior. Yuck! Linux 3.1.9 didn't have this michael@0: * problem. michael@0: */ michael@0: #ifdef __linux__ michael@0: if (evutil_ersatz_socketpair(AF_INET, SOCK_STREAM, 0, pair) == -1) { michael@0: tt_abort_perror("socketpair"); michael@0: } michael@0: #else michael@0: if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) { michael@0: tt_abort_perror("socketpair"); michael@0: } michael@0: #endif michael@0: michael@0: called = was_et = 0; michael@0: michael@0: tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0); michael@0: shutdown(pair[0], SHUT_WR); michael@0: michael@0: /* Initalize the event library */ michael@0: base = event_base_new(); michael@0: michael@0: if (!strcmp(event_base_get_method(base), "epoll") || michael@0: !strcmp(event_base_get_method(base), "epoll (with changelist)") || michael@0: !strcmp(event_base_get_method(base), "kqueue")) michael@0: supports_et = 1; michael@0: else michael@0: supports_et = 0; michael@0: michael@0: TT_BLATHER(("Checking for edge-triggered events with %s, which should %s" michael@0: "support edge-triggering", event_base_get_method(base), michael@0: supports_et?"":"not ")); michael@0: michael@0: /* Initalize one event */ michael@0: ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev); michael@0: michael@0: event_add(ev, NULL); michael@0: michael@0: /* We're going to call the dispatch function twice. The first invocation michael@0: * will read a single byte from pair[1] in either case. If we're edge michael@0: * triggered, we'll only see the event once (since we only see transitions michael@0: * from no data to data), so the second invocation of event_base_loop will michael@0: * do nothing. If we're level triggered, the second invocation of michael@0: * event_base_loop will also activate the event (because there's still michael@0: * data to read). */ michael@0: event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); michael@0: event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); michael@0: michael@0: if (supports_et) { michael@0: tt_int_op(called, ==, 1); michael@0: tt_assert(was_et); michael@0: } else { michael@0: tt_int_op(called, ==, 2); michael@0: tt_assert(!was_et); michael@0: } michael@0: michael@0: end: michael@0: if (ev) { michael@0: event_del(ev); michael@0: event_free(ev); michael@0: } michael@0: if (base) michael@0: event_base_free(base); michael@0: evutil_closesocket(pair[0]); michael@0: evutil_closesocket(pair[1]); michael@0: } michael@0: michael@0: static void michael@0: test_edgetriggered_mix_error(void *data_) michael@0: { michael@0: struct basic_test_data *data = data_; michael@0: struct event_base *base = NULL; michael@0: struct event *ev_et=NULL, *ev_lt=NULL; michael@0: michael@0: #ifdef _EVENT_DISABLE_DEBUG_MODE michael@0: if (1) michael@0: tt_skip(); michael@0: #endif michael@0: michael@0: event_enable_debug_mode(); michael@0: michael@0: base = event_base_new(); michael@0: michael@0: /* try mixing edge-triggered and level-triggered to make sure it fails*/ michael@0: ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et); michael@0: tt_assert(ev_et); michael@0: ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt); michael@0: tt_assert(ev_lt); michael@0: michael@0: /* Add edge-triggered, then level-triggered. Get an error. */ michael@0: tt_int_op(0, ==, event_add(ev_et, NULL)); michael@0: tt_int_op(-1, ==, event_add(ev_lt, NULL)); michael@0: tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL)); michael@0: tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL)); michael@0: michael@0: tt_int_op(0, ==, event_del(ev_et)); michael@0: /* Add level-triggered, then edge-triggered. Get an error. */ michael@0: tt_int_op(0, ==, event_add(ev_lt, NULL)); michael@0: tt_int_op(-1, ==, event_add(ev_et, NULL)); michael@0: tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL)); michael@0: tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL)); michael@0: michael@0: end: michael@0: if (ev_et) michael@0: event_free(ev_et); michael@0: if (ev_lt) michael@0: event_free(ev_lt); michael@0: if (base) michael@0: event_base_free(base); michael@0: } michael@0: michael@0: struct testcase_t edgetriggered_testcases[] = { michael@0: { "et", test_edgetriggered, TT_FORK, NULL, NULL }, michael@0: { "et_mix_error", test_edgetriggered_mix_error, michael@0: TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL }, michael@0: END_OF_TESTCASES michael@0: };