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 | |
michael@0 | 27 | #include <stdlib.h> |
michael@0 | 28 | #include <string.h> |
michael@0 | 29 | #include "event2/event.h" |
michael@0 | 30 | #include "event2/thread.h" |
michael@0 | 31 | #include "event2/buffer.h" |
michael@0 | 32 | #include "event2/buffer_compat.h" |
michael@0 | 33 | #include "event2/bufferevent.h" |
michael@0 | 34 | |
michael@0 | 35 | #include <winsock2.h> |
michael@0 | 36 | #include <ws2tcpip.h> |
michael@0 | 37 | |
michael@0 | 38 | #include "regress.h" |
michael@0 | 39 | #include "tinytest.h" |
michael@0 | 40 | #include "tinytest_macros.h" |
michael@0 | 41 | |
michael@0 | 42 | #define WIN32_LEAN_AND_MEAN |
michael@0 | 43 | #include <windows.h> |
michael@0 | 44 | #include <winsock2.h> |
michael@0 | 45 | #undef WIN32_LEAN_AND_MEAN |
michael@0 | 46 | |
michael@0 | 47 | #include "iocp-internal.h" |
michael@0 | 48 | #include "evbuffer-internal.h" |
michael@0 | 49 | #include "evthread-internal.h" |
michael@0 | 50 | |
michael@0 | 51 | /* FIXME remove these ones */ |
michael@0 | 52 | #include <sys/queue.h> |
michael@0 | 53 | #include "event2/event_struct.h" |
michael@0 | 54 | #include "event-internal.h" |
michael@0 | 55 | |
michael@0 | 56 | #define MAX_CALLS 16 |
michael@0 | 57 | |
michael@0 | 58 | static void *count_lock = NULL, *count_cond = NULL; |
michael@0 | 59 | static int count = 0; |
michael@0 | 60 | |
michael@0 | 61 | static void |
michael@0 | 62 | count_init(void) |
michael@0 | 63 | { |
michael@0 | 64 | EVTHREAD_ALLOC_LOCK(count_lock, 0); |
michael@0 | 65 | EVTHREAD_ALLOC_COND(count_cond); |
michael@0 | 66 | |
michael@0 | 67 | tt_assert(count_lock); |
michael@0 | 68 | tt_assert(count_cond); |
michael@0 | 69 | |
michael@0 | 70 | end: |
michael@0 | 71 | ; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | static void |
michael@0 | 75 | count_free(void) |
michael@0 | 76 | { |
michael@0 | 77 | EVTHREAD_FREE_LOCK(count_lock, 0); |
michael@0 | 78 | EVTHREAD_FREE_COND(count_cond); |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | static void |
michael@0 | 82 | count_incr(void) |
michael@0 | 83 | { |
michael@0 | 84 | EVLOCK_LOCK(count_lock, 0); |
michael@0 | 85 | count++; |
michael@0 | 86 | EVTHREAD_COND_BROADCAST(count_cond); |
michael@0 | 87 | EVLOCK_UNLOCK(count_lock, 0); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | static int |
michael@0 | 91 | count_wait_for(int i, int ms) |
michael@0 | 92 | { |
michael@0 | 93 | struct timeval tv; |
michael@0 | 94 | DWORD elapsed; |
michael@0 | 95 | int rv = -1; |
michael@0 | 96 | |
michael@0 | 97 | EVLOCK_LOCK(count_lock, 0); |
michael@0 | 98 | while (ms > 0 && count != i) { |
michael@0 | 99 | tv.tv_sec = 0; |
michael@0 | 100 | tv.tv_usec = ms * 1000; |
michael@0 | 101 | elapsed = GetTickCount(); |
michael@0 | 102 | EVTHREAD_COND_WAIT_TIMED(count_cond, count_lock, &tv); |
michael@0 | 103 | elapsed = GetTickCount() - elapsed; |
michael@0 | 104 | ms -= elapsed; |
michael@0 | 105 | } |
michael@0 | 106 | if (count == i) |
michael@0 | 107 | rv = 0; |
michael@0 | 108 | EVLOCK_UNLOCK(count_lock, 0); |
michael@0 | 109 | |
michael@0 | 110 | return rv; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | struct dummy_overlapped { |
michael@0 | 114 | struct event_overlapped eo; |
michael@0 | 115 | void *lock; |
michael@0 | 116 | int call_count; |
michael@0 | 117 | uintptr_t keys[MAX_CALLS]; |
michael@0 | 118 | ev_ssize_t sizes[MAX_CALLS]; |
michael@0 | 119 | }; |
michael@0 | 120 | |
michael@0 | 121 | static void |
michael@0 | 122 | dummy_cb(struct event_overlapped *o, uintptr_t key, ev_ssize_t n, int ok) |
michael@0 | 123 | { |
michael@0 | 124 | struct dummy_overlapped *d_o = |
michael@0 | 125 | EVUTIL_UPCAST(o, struct dummy_overlapped, eo); |
michael@0 | 126 | |
michael@0 | 127 | EVLOCK_LOCK(d_o->lock, 0); |
michael@0 | 128 | if (d_o->call_count < MAX_CALLS) { |
michael@0 | 129 | d_o->keys[d_o->call_count] = key; |
michael@0 | 130 | d_o->sizes[d_o->call_count] = n; |
michael@0 | 131 | } |
michael@0 | 132 | d_o->call_count++; |
michael@0 | 133 | EVLOCK_UNLOCK(d_o->lock, 0); |
michael@0 | 134 | |
michael@0 | 135 | count_incr(); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | static int |
michael@0 | 139 | pair_is_in(struct dummy_overlapped *o, uintptr_t key, ev_ssize_t n) |
michael@0 | 140 | { |
michael@0 | 141 | int i; |
michael@0 | 142 | int result = 0; |
michael@0 | 143 | EVLOCK_LOCK(o->lock, 0); |
michael@0 | 144 | for (i=0; i < o->call_count; ++i) { |
michael@0 | 145 | if (o->keys[i] == key && o->sizes[i] == n) { |
michael@0 | 146 | result = 1; |
michael@0 | 147 | break; |
michael@0 | 148 | } |
michael@0 | 149 | } |
michael@0 | 150 | EVLOCK_UNLOCK(o->lock, 0); |
michael@0 | 151 | return result; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | static void |
michael@0 | 155 | test_iocp_port(void *ptr) |
michael@0 | 156 | { |
michael@0 | 157 | struct event_iocp_port *port = NULL; |
michael@0 | 158 | struct dummy_overlapped o1, o2; |
michael@0 | 159 | |
michael@0 | 160 | memset(&o1, 0, sizeof(o1)); |
michael@0 | 161 | memset(&o2, 0, sizeof(o2)); |
michael@0 | 162 | |
michael@0 | 163 | count_init(); |
michael@0 | 164 | EVTHREAD_ALLOC_LOCK(o1.lock, EVTHREAD_LOCKTYPE_RECURSIVE); |
michael@0 | 165 | EVTHREAD_ALLOC_LOCK(o2.lock, EVTHREAD_LOCKTYPE_RECURSIVE); |
michael@0 | 166 | |
michael@0 | 167 | tt_assert(o1.lock); |
michael@0 | 168 | tt_assert(o2.lock); |
michael@0 | 169 | |
michael@0 | 170 | event_overlapped_init(&o1.eo, dummy_cb); |
michael@0 | 171 | event_overlapped_init(&o2.eo, dummy_cb); |
michael@0 | 172 | |
michael@0 | 173 | port = event_iocp_port_launch(0); |
michael@0 | 174 | tt_assert(port); |
michael@0 | 175 | |
michael@0 | 176 | tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 10, 100)); |
michael@0 | 177 | tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 20, 200)); |
michael@0 | 178 | |
michael@0 | 179 | tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 11, 101)); |
michael@0 | 180 | tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 21, 201)); |
michael@0 | 181 | |
michael@0 | 182 | tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 12, 102)); |
michael@0 | 183 | tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 22, 202)); |
michael@0 | 184 | |
michael@0 | 185 | tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 13, 103)); |
michael@0 | 186 | tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 23, 203)); |
michael@0 | 187 | |
michael@0 | 188 | tt_int_op(count_wait_for(8, 2000), ==, 0); |
michael@0 | 189 | |
michael@0 | 190 | tt_want(!event_iocp_shutdown(port, 2000)); |
michael@0 | 191 | |
michael@0 | 192 | tt_int_op(o1.call_count, ==, 4); |
michael@0 | 193 | tt_int_op(o2.call_count, ==, 4); |
michael@0 | 194 | |
michael@0 | 195 | tt_want(pair_is_in(&o1, 10, 100)); |
michael@0 | 196 | tt_want(pair_is_in(&o1, 11, 101)); |
michael@0 | 197 | tt_want(pair_is_in(&o1, 12, 102)); |
michael@0 | 198 | tt_want(pair_is_in(&o1, 13, 103)); |
michael@0 | 199 | |
michael@0 | 200 | tt_want(pair_is_in(&o2, 20, 200)); |
michael@0 | 201 | tt_want(pair_is_in(&o2, 21, 201)); |
michael@0 | 202 | tt_want(pair_is_in(&o2, 22, 202)); |
michael@0 | 203 | tt_want(pair_is_in(&o2, 23, 203)); |
michael@0 | 204 | |
michael@0 | 205 | end: |
michael@0 | 206 | EVTHREAD_FREE_LOCK(o1.lock, EVTHREAD_LOCKTYPE_RECURSIVE); |
michael@0 | 207 | EVTHREAD_FREE_LOCK(o2.lock, EVTHREAD_LOCKTYPE_RECURSIVE); |
michael@0 | 208 | count_free(); |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | static struct evbuffer *rbuf = NULL, *wbuf = NULL; |
michael@0 | 212 | |
michael@0 | 213 | static void |
michael@0 | 214 | read_complete(struct event_overlapped *eo, uintptr_t key, |
michael@0 | 215 | ev_ssize_t nbytes, int ok) |
michael@0 | 216 | { |
michael@0 | 217 | tt_assert(ok); |
michael@0 | 218 | evbuffer_commit_read(rbuf, nbytes); |
michael@0 | 219 | count_incr(); |
michael@0 | 220 | end: |
michael@0 | 221 | ; |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | static void |
michael@0 | 225 | write_complete(struct event_overlapped *eo, uintptr_t key, |
michael@0 | 226 | ev_ssize_t nbytes, int ok) |
michael@0 | 227 | { |
michael@0 | 228 | tt_assert(ok); |
michael@0 | 229 | evbuffer_commit_write(wbuf, nbytes); |
michael@0 | 230 | count_incr(); |
michael@0 | 231 | end: |
michael@0 | 232 | ; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | static void |
michael@0 | 236 | test_iocp_evbuffer(void *ptr) |
michael@0 | 237 | { |
michael@0 | 238 | struct event_overlapped rol, wol; |
michael@0 | 239 | struct basic_test_data *data = ptr; |
michael@0 | 240 | struct event_iocp_port *port = NULL; |
michael@0 | 241 | struct evbuffer *buf=NULL; |
michael@0 | 242 | struct evbuffer_chain *chain; |
michael@0 | 243 | char junk[1024]; |
michael@0 | 244 | int i; |
michael@0 | 245 | |
michael@0 | 246 | count_init(); |
michael@0 | 247 | event_overlapped_init(&rol, read_complete); |
michael@0 | 248 | event_overlapped_init(&wol, write_complete); |
michael@0 | 249 | |
michael@0 | 250 | for (i = 0; i < (int)sizeof(junk); ++i) |
michael@0 | 251 | junk[i] = (char)(i); |
michael@0 | 252 | |
michael@0 | 253 | rbuf = evbuffer_overlapped_new(data->pair[0]); |
michael@0 | 254 | wbuf = evbuffer_overlapped_new(data->pair[1]); |
michael@0 | 255 | evbuffer_enable_locking(rbuf, NULL); |
michael@0 | 256 | evbuffer_enable_locking(wbuf, NULL); |
michael@0 | 257 | |
michael@0 | 258 | port = event_iocp_port_launch(0); |
michael@0 | 259 | tt_assert(port); |
michael@0 | 260 | tt_assert(rbuf); |
michael@0 | 261 | tt_assert(wbuf); |
michael@0 | 262 | |
michael@0 | 263 | tt_assert(!event_iocp_port_associate(port, data->pair[0], 100)); |
michael@0 | 264 | tt_assert(!event_iocp_port_associate(port, data->pair[1], 100)); |
michael@0 | 265 | |
michael@0 | 266 | for (i=0;i<10;++i) |
michael@0 | 267 | evbuffer_add(wbuf, junk, sizeof(junk)); |
michael@0 | 268 | |
michael@0 | 269 | buf = evbuffer_new(); |
michael@0 | 270 | tt_assert(buf != NULL); |
michael@0 | 271 | evbuffer_add(rbuf, junk, sizeof(junk)); |
michael@0 | 272 | tt_assert(!evbuffer_launch_read(rbuf, 2048, &rol)); |
michael@0 | 273 | evbuffer_add_buffer(buf, rbuf); |
michael@0 | 274 | tt_int_op(evbuffer_get_length(buf), ==, sizeof(junk)); |
michael@0 | 275 | for (chain = buf->first; chain; chain = chain->next) |
michael@0 | 276 | tt_int_op(chain->flags & EVBUFFER_MEM_PINNED_ANY, ==, 0); |
michael@0 | 277 | tt_assert(!evbuffer_get_length(rbuf)); |
michael@0 | 278 | tt_assert(!evbuffer_launch_write(wbuf, 512, &wol)); |
michael@0 | 279 | |
michael@0 | 280 | tt_int_op(count_wait_for(2, 2000), ==, 0); |
michael@0 | 281 | |
michael@0 | 282 | tt_int_op(evbuffer_get_length(rbuf),==,512); |
michael@0 | 283 | |
michael@0 | 284 | /* FIXME Actually test some stuff here. */ |
michael@0 | 285 | |
michael@0 | 286 | tt_want(!event_iocp_shutdown(port, 2000)); |
michael@0 | 287 | end: |
michael@0 | 288 | count_free(); |
michael@0 | 289 | evbuffer_free(rbuf); |
michael@0 | 290 | evbuffer_free(wbuf); |
michael@0 | 291 | if (buf) evbuffer_free(buf); |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | static int got_readcb = 0; |
michael@0 | 295 | |
michael@0 | 296 | static void |
michael@0 | 297 | async_readcb(struct bufferevent *bev, void *arg) |
michael@0 | 298 | { |
michael@0 | 299 | /* Disabling read should cause the loop to quit */ |
michael@0 | 300 | bufferevent_disable(bev, EV_READ); |
michael@0 | 301 | got_readcb++; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | static void |
michael@0 | 305 | test_iocp_bufferevent_async(void *ptr) |
michael@0 | 306 | { |
michael@0 | 307 | struct basic_test_data *data = ptr; |
michael@0 | 308 | struct event_iocp_port *port = NULL; |
michael@0 | 309 | struct bufferevent *bea1=NULL, *bea2=NULL; |
michael@0 | 310 | char buf[128]; |
michael@0 | 311 | size_t n; |
michael@0 | 312 | |
michael@0 | 313 | event_base_start_iocp(data->base, 0); |
michael@0 | 314 | port = event_base_get_iocp(data->base); |
michael@0 | 315 | tt_assert(port); |
michael@0 | 316 | |
michael@0 | 317 | bea1 = bufferevent_async_new(data->base, data->pair[0], |
michael@0 | 318 | BEV_OPT_DEFER_CALLBACKS); |
michael@0 | 319 | bea2 = bufferevent_async_new(data->base, data->pair[1], |
michael@0 | 320 | BEV_OPT_DEFER_CALLBACKS); |
michael@0 | 321 | tt_assert(bea1); |
michael@0 | 322 | tt_assert(bea2); |
michael@0 | 323 | |
michael@0 | 324 | bufferevent_setcb(bea2, async_readcb, NULL, NULL, NULL); |
michael@0 | 325 | bufferevent_enable(bea1, EV_WRITE); |
michael@0 | 326 | bufferevent_enable(bea2, EV_READ); |
michael@0 | 327 | |
michael@0 | 328 | bufferevent_write(bea1, "Hello world", strlen("Hello world")+1); |
michael@0 | 329 | |
michael@0 | 330 | event_base_dispatch(data->base); |
michael@0 | 331 | |
michael@0 | 332 | tt_int_op(got_readcb, ==, 1); |
michael@0 | 333 | n = bufferevent_read(bea2, buf, sizeof(buf)-1); |
michael@0 | 334 | buf[n]='\0'; |
michael@0 | 335 | tt_str_op(buf, ==, "Hello world"); |
michael@0 | 336 | |
michael@0 | 337 | end: |
michael@0 | 338 | bufferevent_free(bea1); |
michael@0 | 339 | bufferevent_free(bea2); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | |
michael@0 | 343 | struct testcase_t iocp_testcases[] = { |
michael@0 | 344 | { "port", test_iocp_port, TT_FORK|TT_NEED_THREADS, &basic_setup, NULL }, |
michael@0 | 345 | { "evbuffer", test_iocp_evbuffer, |
michael@0 | 346 | TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_THREADS, |
michael@0 | 347 | &basic_setup, NULL }, |
michael@0 | 348 | { "bufferevent_async", test_iocp_bufferevent_async, |
michael@0 | 349 | TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_THREADS|TT_NEED_BASE, |
michael@0 | 350 | &basic_setup, NULL }, |
michael@0 | 351 | END_OF_TESTCASES |
michael@0 | 352 | }; |