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: michael@0: #include michael@0: #include michael@0: #include "event2/event.h" michael@0: #include "event2/thread.h" michael@0: #include "event2/buffer.h" michael@0: #include "event2/buffer_compat.h" michael@0: #include "event2/bufferevent.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "regress.h" michael@0: #include "tinytest.h" michael@0: #include "tinytest_macros.h" michael@0: michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #include michael@0: #undef WIN32_LEAN_AND_MEAN michael@0: michael@0: #include "iocp-internal.h" michael@0: #include "evbuffer-internal.h" michael@0: #include "evthread-internal.h" michael@0: michael@0: /* FIXME remove these ones */ michael@0: #include michael@0: #include "event2/event_struct.h" michael@0: #include "event-internal.h" michael@0: michael@0: #define MAX_CALLS 16 michael@0: michael@0: static void *count_lock = NULL, *count_cond = NULL; michael@0: static int count = 0; michael@0: michael@0: static void michael@0: count_init(void) michael@0: { michael@0: EVTHREAD_ALLOC_LOCK(count_lock, 0); michael@0: EVTHREAD_ALLOC_COND(count_cond); michael@0: michael@0: tt_assert(count_lock); michael@0: tt_assert(count_cond); michael@0: michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: count_free(void) michael@0: { michael@0: EVTHREAD_FREE_LOCK(count_lock, 0); michael@0: EVTHREAD_FREE_COND(count_cond); michael@0: } michael@0: michael@0: static void michael@0: count_incr(void) michael@0: { michael@0: EVLOCK_LOCK(count_lock, 0); michael@0: count++; michael@0: EVTHREAD_COND_BROADCAST(count_cond); michael@0: EVLOCK_UNLOCK(count_lock, 0); michael@0: } michael@0: michael@0: static int michael@0: count_wait_for(int i, int ms) michael@0: { michael@0: struct timeval tv; michael@0: DWORD elapsed; michael@0: int rv = -1; michael@0: michael@0: EVLOCK_LOCK(count_lock, 0); michael@0: while (ms > 0 && count != i) { michael@0: tv.tv_sec = 0; michael@0: tv.tv_usec = ms * 1000; michael@0: elapsed = GetTickCount(); michael@0: EVTHREAD_COND_WAIT_TIMED(count_cond, count_lock, &tv); michael@0: elapsed = GetTickCount() - elapsed; michael@0: ms -= elapsed; michael@0: } michael@0: if (count == i) michael@0: rv = 0; michael@0: EVLOCK_UNLOCK(count_lock, 0); michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: struct dummy_overlapped { michael@0: struct event_overlapped eo; michael@0: void *lock; michael@0: int call_count; michael@0: uintptr_t keys[MAX_CALLS]; michael@0: ev_ssize_t sizes[MAX_CALLS]; michael@0: }; michael@0: michael@0: static void michael@0: dummy_cb(struct event_overlapped *o, uintptr_t key, ev_ssize_t n, int ok) michael@0: { michael@0: struct dummy_overlapped *d_o = michael@0: EVUTIL_UPCAST(o, struct dummy_overlapped, eo); michael@0: michael@0: EVLOCK_LOCK(d_o->lock, 0); michael@0: if (d_o->call_count < MAX_CALLS) { michael@0: d_o->keys[d_o->call_count] = key; michael@0: d_o->sizes[d_o->call_count] = n; michael@0: } michael@0: d_o->call_count++; michael@0: EVLOCK_UNLOCK(d_o->lock, 0); michael@0: michael@0: count_incr(); michael@0: } michael@0: michael@0: static int michael@0: pair_is_in(struct dummy_overlapped *o, uintptr_t key, ev_ssize_t n) michael@0: { michael@0: int i; michael@0: int result = 0; michael@0: EVLOCK_LOCK(o->lock, 0); michael@0: for (i=0; i < o->call_count; ++i) { michael@0: if (o->keys[i] == key && o->sizes[i] == n) { michael@0: result = 1; michael@0: break; michael@0: } michael@0: } michael@0: EVLOCK_UNLOCK(o->lock, 0); michael@0: return result; michael@0: } michael@0: michael@0: static void michael@0: test_iocp_port(void *ptr) michael@0: { michael@0: struct event_iocp_port *port = NULL; michael@0: struct dummy_overlapped o1, o2; michael@0: michael@0: memset(&o1, 0, sizeof(o1)); michael@0: memset(&o2, 0, sizeof(o2)); michael@0: michael@0: count_init(); michael@0: EVTHREAD_ALLOC_LOCK(o1.lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: EVTHREAD_ALLOC_LOCK(o2.lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: michael@0: tt_assert(o1.lock); michael@0: tt_assert(o2.lock); michael@0: michael@0: event_overlapped_init(&o1.eo, dummy_cb); michael@0: event_overlapped_init(&o2.eo, dummy_cb); michael@0: michael@0: port = event_iocp_port_launch(0); michael@0: tt_assert(port); michael@0: michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 10, 100)); michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 20, 200)); michael@0: michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 11, 101)); michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 21, 201)); michael@0: michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 12, 102)); michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 22, 202)); michael@0: michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o1.eo, 13, 103)); michael@0: tt_assert(!event_iocp_activate_overlapped(port, &o2.eo, 23, 203)); michael@0: michael@0: tt_int_op(count_wait_for(8, 2000), ==, 0); michael@0: michael@0: tt_want(!event_iocp_shutdown(port, 2000)); michael@0: michael@0: tt_int_op(o1.call_count, ==, 4); michael@0: tt_int_op(o2.call_count, ==, 4); michael@0: michael@0: tt_want(pair_is_in(&o1, 10, 100)); michael@0: tt_want(pair_is_in(&o1, 11, 101)); michael@0: tt_want(pair_is_in(&o1, 12, 102)); michael@0: tt_want(pair_is_in(&o1, 13, 103)); michael@0: michael@0: tt_want(pair_is_in(&o2, 20, 200)); michael@0: tt_want(pair_is_in(&o2, 21, 201)); michael@0: tt_want(pair_is_in(&o2, 22, 202)); michael@0: tt_want(pair_is_in(&o2, 23, 203)); michael@0: michael@0: end: michael@0: EVTHREAD_FREE_LOCK(o1.lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: EVTHREAD_FREE_LOCK(o2.lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: count_free(); michael@0: } michael@0: michael@0: static struct evbuffer *rbuf = NULL, *wbuf = NULL; michael@0: michael@0: static void michael@0: read_complete(struct event_overlapped *eo, uintptr_t key, michael@0: ev_ssize_t nbytes, int ok) michael@0: { michael@0: tt_assert(ok); michael@0: evbuffer_commit_read(rbuf, nbytes); michael@0: count_incr(); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: write_complete(struct event_overlapped *eo, uintptr_t key, michael@0: ev_ssize_t nbytes, int ok) michael@0: { michael@0: tt_assert(ok); michael@0: evbuffer_commit_write(wbuf, nbytes); michael@0: count_incr(); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: test_iocp_evbuffer(void *ptr) michael@0: { michael@0: struct event_overlapped rol, wol; michael@0: struct basic_test_data *data = ptr; michael@0: struct event_iocp_port *port = NULL; michael@0: struct evbuffer *buf=NULL; michael@0: struct evbuffer_chain *chain; michael@0: char junk[1024]; michael@0: int i; michael@0: michael@0: count_init(); michael@0: event_overlapped_init(&rol, read_complete); michael@0: event_overlapped_init(&wol, write_complete); michael@0: michael@0: for (i = 0; i < (int)sizeof(junk); ++i) michael@0: junk[i] = (char)(i); michael@0: michael@0: rbuf = evbuffer_overlapped_new(data->pair[0]); michael@0: wbuf = evbuffer_overlapped_new(data->pair[1]); michael@0: evbuffer_enable_locking(rbuf, NULL); michael@0: evbuffer_enable_locking(wbuf, NULL); michael@0: michael@0: port = event_iocp_port_launch(0); michael@0: tt_assert(port); michael@0: tt_assert(rbuf); michael@0: tt_assert(wbuf); michael@0: michael@0: tt_assert(!event_iocp_port_associate(port, data->pair[0], 100)); michael@0: tt_assert(!event_iocp_port_associate(port, data->pair[1], 100)); michael@0: michael@0: for (i=0;i<10;++i) michael@0: evbuffer_add(wbuf, junk, sizeof(junk)); michael@0: michael@0: buf = evbuffer_new(); michael@0: tt_assert(buf != NULL); michael@0: evbuffer_add(rbuf, junk, sizeof(junk)); michael@0: tt_assert(!evbuffer_launch_read(rbuf, 2048, &rol)); michael@0: evbuffer_add_buffer(buf, rbuf); michael@0: tt_int_op(evbuffer_get_length(buf), ==, sizeof(junk)); michael@0: for (chain = buf->first; chain; chain = chain->next) michael@0: tt_int_op(chain->flags & EVBUFFER_MEM_PINNED_ANY, ==, 0); michael@0: tt_assert(!evbuffer_get_length(rbuf)); michael@0: tt_assert(!evbuffer_launch_write(wbuf, 512, &wol)); michael@0: michael@0: tt_int_op(count_wait_for(2, 2000), ==, 0); michael@0: michael@0: tt_int_op(evbuffer_get_length(rbuf),==,512); michael@0: michael@0: /* FIXME Actually test some stuff here. */ michael@0: michael@0: tt_want(!event_iocp_shutdown(port, 2000)); michael@0: end: michael@0: count_free(); michael@0: evbuffer_free(rbuf); michael@0: evbuffer_free(wbuf); michael@0: if (buf) evbuffer_free(buf); michael@0: } michael@0: michael@0: static int got_readcb = 0; michael@0: michael@0: static void michael@0: async_readcb(struct bufferevent *bev, void *arg) michael@0: { michael@0: /* Disabling read should cause the loop to quit */ michael@0: bufferevent_disable(bev, EV_READ); michael@0: got_readcb++; michael@0: } michael@0: michael@0: static void michael@0: test_iocp_bufferevent_async(void *ptr) michael@0: { michael@0: struct basic_test_data *data = ptr; michael@0: struct event_iocp_port *port = NULL; michael@0: struct bufferevent *bea1=NULL, *bea2=NULL; michael@0: char buf[128]; michael@0: size_t n; michael@0: michael@0: event_base_start_iocp(data->base, 0); michael@0: port = event_base_get_iocp(data->base); michael@0: tt_assert(port); michael@0: michael@0: bea1 = bufferevent_async_new(data->base, data->pair[0], michael@0: BEV_OPT_DEFER_CALLBACKS); michael@0: bea2 = bufferevent_async_new(data->base, data->pair[1], michael@0: BEV_OPT_DEFER_CALLBACKS); michael@0: tt_assert(bea1); michael@0: tt_assert(bea2); michael@0: michael@0: bufferevent_setcb(bea2, async_readcb, NULL, NULL, NULL); michael@0: bufferevent_enable(bea1, EV_WRITE); michael@0: bufferevent_enable(bea2, EV_READ); michael@0: michael@0: bufferevent_write(bea1, "Hello world", strlen("Hello world")+1); michael@0: michael@0: event_base_dispatch(data->base); michael@0: michael@0: tt_int_op(got_readcb, ==, 1); michael@0: n = bufferevent_read(bea2, buf, sizeof(buf)-1); michael@0: buf[n]='\0'; michael@0: tt_str_op(buf, ==, "Hello world"); michael@0: michael@0: end: michael@0: bufferevent_free(bea1); michael@0: bufferevent_free(bea2); michael@0: } michael@0: michael@0: michael@0: struct testcase_t iocp_testcases[] = { michael@0: { "port", test_iocp_port, TT_FORK|TT_NEED_THREADS, &basic_setup, NULL }, michael@0: { "evbuffer", test_iocp_evbuffer, michael@0: TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_THREADS, michael@0: &basic_setup, NULL }, michael@0: { "bufferevent_async", test_iocp_bufferevent_async, michael@0: TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_THREADS|TT_NEED_BASE, michael@0: &basic_setup, NULL }, michael@0: END_OF_TESTCASES michael@0: };