Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson |
michael@0 | 3 | * |
michael@0 | 4 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | * modification, are permitted provided that the following conditions |
michael@0 | 6 | * are met: |
michael@0 | 7 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 8 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 9 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 10 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 11 | * documentation and/or other materials provided with the distribution. |
michael@0 | 12 | * 3. The name of the author may not be used to endorse or promote products |
michael@0 | 13 | * derived from this software without specific prior written permission. |
michael@0 | 14 | * |
michael@0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
michael@0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
michael@0 | 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
michael@0 | 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
michael@0 | 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
michael@0 | 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
michael@0 | 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | #include "../util-internal.h" |
michael@0 | 27 | #include "event2/event-config.h" |
michael@0 | 28 | |
michael@0 | 29 | #ifdef WIN32 |
michael@0 | 30 | #include <winsock2.h> |
michael@0 | 31 | #endif |
michael@0 | 32 | #include <sys/types.h> |
michael@0 | 33 | #include <sys/stat.h> |
michael@0 | 34 | #ifdef _EVENT_HAVE_SYS_SOCKET_H |
michael@0 | 35 | #include <sys/socket.h> |
michael@0 | 36 | #endif |
michael@0 | 37 | #include <fcntl.h> |
michael@0 | 38 | #include <stdlib.h> |
michael@0 | 39 | #include <stdio.h> |
michael@0 | 40 | #include <string.h> |
michael@0 | 41 | #ifndef WIN32 |
michael@0 | 42 | #include <sys/time.h> |
michael@0 | 43 | #include <unistd.h> |
michael@0 | 44 | #endif |
michael@0 | 45 | #include <errno.h> |
michael@0 | 46 | |
michael@0 | 47 | #include "event2/event.h" |
michael@0 | 48 | #include "event2/util.h" |
michael@0 | 49 | |
michael@0 | 50 | #include "regress.h" |
michael@0 | 51 | |
michael@0 | 52 | static int was_et = 0; |
michael@0 | 53 | |
michael@0 | 54 | static void |
michael@0 | 55 | read_cb(evutil_socket_t fd, short event, void *arg) |
michael@0 | 56 | { |
michael@0 | 57 | char buf; |
michael@0 | 58 | int len; |
michael@0 | 59 | |
michael@0 | 60 | len = recv(fd, &buf, sizeof(buf), 0); |
michael@0 | 61 | |
michael@0 | 62 | called++; |
michael@0 | 63 | if (event & EV_ET) |
michael@0 | 64 | was_et = 1; |
michael@0 | 65 | |
michael@0 | 66 | if (!len) |
michael@0 | 67 | event_del(arg); |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | #ifndef SHUT_WR |
michael@0 | 71 | #define SHUT_WR 1 |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | #ifdef WIN32 |
michael@0 | 75 | #define LOCAL_SOCKETPAIR_AF AF_INET |
michael@0 | 76 | #else |
michael@0 | 77 | #define LOCAL_SOCKETPAIR_AF AF_UNIX |
michael@0 | 78 | #endif |
michael@0 | 79 | |
michael@0 | 80 | static void |
michael@0 | 81 | test_edgetriggered(void *et) |
michael@0 | 82 | { |
michael@0 | 83 | struct event *ev = NULL; |
michael@0 | 84 | struct event_base *base = NULL; |
michael@0 | 85 | const char *test = "test string"; |
michael@0 | 86 | evutil_socket_t pair[2] = {-1,-1}; |
michael@0 | 87 | int supports_et; |
michael@0 | 88 | |
michael@0 | 89 | /* On Linux 3.2.1 (at least, as patched by Fedora and tested by Nick), |
michael@0 | 90 | * doing a "recv" on an AF_UNIX socket resets the readability of the |
michael@0 | 91 | * socket, even though there is no state change, so we don't actually |
michael@0 | 92 | * get edge-triggered behavior. Yuck! Linux 3.1.9 didn't have this |
michael@0 | 93 | * problem. |
michael@0 | 94 | */ |
michael@0 | 95 | #ifdef __linux__ |
michael@0 | 96 | if (evutil_ersatz_socketpair(AF_INET, SOCK_STREAM, 0, pair) == -1) { |
michael@0 | 97 | tt_abort_perror("socketpair"); |
michael@0 | 98 | } |
michael@0 | 99 | #else |
michael@0 | 100 | if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair) == -1) { |
michael@0 | 101 | tt_abort_perror("socketpair"); |
michael@0 | 102 | } |
michael@0 | 103 | #endif |
michael@0 | 104 | |
michael@0 | 105 | called = was_et = 0; |
michael@0 | 106 | |
michael@0 | 107 | tt_int_op(send(pair[0], test, (int)strlen(test)+1, 0), >, 0); |
michael@0 | 108 | shutdown(pair[0], SHUT_WR); |
michael@0 | 109 | |
michael@0 | 110 | /* Initalize the event library */ |
michael@0 | 111 | base = event_base_new(); |
michael@0 | 112 | |
michael@0 | 113 | if (!strcmp(event_base_get_method(base), "epoll") || |
michael@0 | 114 | !strcmp(event_base_get_method(base), "epoll (with changelist)") || |
michael@0 | 115 | !strcmp(event_base_get_method(base), "kqueue")) |
michael@0 | 116 | supports_et = 1; |
michael@0 | 117 | else |
michael@0 | 118 | supports_et = 0; |
michael@0 | 119 | |
michael@0 | 120 | TT_BLATHER(("Checking for edge-triggered events with %s, which should %s" |
michael@0 | 121 | "support edge-triggering", event_base_get_method(base), |
michael@0 | 122 | supports_et?"":"not ")); |
michael@0 | 123 | |
michael@0 | 124 | /* Initalize one event */ |
michael@0 | 125 | ev = event_new(base, pair[1], EV_READ|EV_ET|EV_PERSIST, read_cb, &ev); |
michael@0 | 126 | |
michael@0 | 127 | event_add(ev, NULL); |
michael@0 | 128 | |
michael@0 | 129 | /* We're going to call the dispatch function twice. The first invocation |
michael@0 | 130 | * will read a single byte from pair[1] in either case. If we're edge |
michael@0 | 131 | * triggered, we'll only see the event once (since we only see transitions |
michael@0 | 132 | * from no data to data), so the second invocation of event_base_loop will |
michael@0 | 133 | * do nothing. If we're level triggered, the second invocation of |
michael@0 | 134 | * event_base_loop will also activate the event (because there's still |
michael@0 | 135 | * data to read). */ |
michael@0 | 136 | event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); |
michael@0 | 137 | event_base_loop(base,EVLOOP_NONBLOCK|EVLOOP_ONCE); |
michael@0 | 138 | |
michael@0 | 139 | if (supports_et) { |
michael@0 | 140 | tt_int_op(called, ==, 1); |
michael@0 | 141 | tt_assert(was_et); |
michael@0 | 142 | } else { |
michael@0 | 143 | tt_int_op(called, ==, 2); |
michael@0 | 144 | tt_assert(!was_et); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | end: |
michael@0 | 148 | if (ev) { |
michael@0 | 149 | event_del(ev); |
michael@0 | 150 | event_free(ev); |
michael@0 | 151 | } |
michael@0 | 152 | if (base) |
michael@0 | 153 | event_base_free(base); |
michael@0 | 154 | evutil_closesocket(pair[0]); |
michael@0 | 155 | evutil_closesocket(pair[1]); |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | static void |
michael@0 | 159 | test_edgetriggered_mix_error(void *data_) |
michael@0 | 160 | { |
michael@0 | 161 | struct basic_test_data *data = data_; |
michael@0 | 162 | struct event_base *base = NULL; |
michael@0 | 163 | struct event *ev_et=NULL, *ev_lt=NULL; |
michael@0 | 164 | |
michael@0 | 165 | #ifdef _EVENT_DISABLE_DEBUG_MODE |
michael@0 | 166 | if (1) |
michael@0 | 167 | tt_skip(); |
michael@0 | 168 | #endif |
michael@0 | 169 | |
michael@0 | 170 | event_enable_debug_mode(); |
michael@0 | 171 | |
michael@0 | 172 | base = event_base_new(); |
michael@0 | 173 | |
michael@0 | 174 | /* try mixing edge-triggered and level-triggered to make sure it fails*/ |
michael@0 | 175 | ev_et = event_new(base, data->pair[0], EV_READ|EV_ET, read_cb, ev_et); |
michael@0 | 176 | tt_assert(ev_et); |
michael@0 | 177 | ev_lt = event_new(base, data->pair[0], EV_READ, read_cb, ev_lt); |
michael@0 | 178 | tt_assert(ev_lt); |
michael@0 | 179 | |
michael@0 | 180 | /* Add edge-triggered, then level-triggered. Get an error. */ |
michael@0 | 181 | tt_int_op(0, ==, event_add(ev_et, NULL)); |
michael@0 | 182 | tt_int_op(-1, ==, event_add(ev_lt, NULL)); |
michael@0 | 183 | tt_int_op(EV_READ, ==, event_pending(ev_et, EV_READ, NULL)); |
michael@0 | 184 | tt_int_op(0, ==, event_pending(ev_lt, EV_READ, NULL)); |
michael@0 | 185 | |
michael@0 | 186 | tt_int_op(0, ==, event_del(ev_et)); |
michael@0 | 187 | /* Add level-triggered, then edge-triggered. Get an error. */ |
michael@0 | 188 | tt_int_op(0, ==, event_add(ev_lt, NULL)); |
michael@0 | 189 | tt_int_op(-1, ==, event_add(ev_et, NULL)); |
michael@0 | 190 | tt_int_op(EV_READ, ==, event_pending(ev_lt, EV_READ, NULL)); |
michael@0 | 191 | tt_int_op(0, ==, event_pending(ev_et, EV_READ, NULL)); |
michael@0 | 192 | |
michael@0 | 193 | end: |
michael@0 | 194 | if (ev_et) |
michael@0 | 195 | event_free(ev_et); |
michael@0 | 196 | if (ev_lt) |
michael@0 | 197 | event_free(ev_lt); |
michael@0 | 198 | if (base) |
michael@0 | 199 | event_base_free(base); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | struct testcase_t edgetriggered_testcases[] = { |
michael@0 | 203 | { "et", test_edgetriggered, TT_FORK, NULL, NULL }, |
michael@0 | 204 | { "et_mix_error", test_edgetriggered_mix_error, |
michael@0 | 205 | TT_FORK|TT_NEED_SOCKETPAIR|TT_NO_LOGS, &basic_setup, NULL }, |
michael@0 | 206 | END_OF_TESTCASES |
michael@0 | 207 | }; |