michael@0: /* michael@0: * Copyright (c) 2003-2007 Niels Provos michael@0: * Copyright (c) 2007-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: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) michael@0: #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \ michael@0: __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070) michael@0: #define FORK_BREAKS_GCOV michael@0: #include michael@0: #endif michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifdef _EVENT___func__ michael@0: #define __func__ _EVENT___func__ michael@0: #endif michael@0: michael@0: #if 0 michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_TIME_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_STAT_H michael@0: #include michael@0: #endif michael@0: michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/util.h" michael@0: #include "event2/event.h" michael@0: #include "event2/event_compat.h" michael@0: #include "event2/dns.h" michael@0: #include "event2/dns_compat.h" michael@0: #include "event2/thread.h" michael@0: michael@0: #include "event2/event-config.h" michael@0: #include "regress.h" michael@0: #include "tinytest.h" michael@0: #include "tinytest_macros.h" michael@0: #include "../iocp-internal.h" michael@0: #include "../event-internal.h" michael@0: michael@0: long michael@0: timeval_msec_diff(const struct timeval *start, const struct timeval *end) michael@0: { michael@0: long ms = end->tv_sec - start->tv_sec; michael@0: ms *= 1000; michael@0: ms += ((end->tv_usec - start->tv_usec)+500) / 1000; michael@0: return ms; michael@0: michael@0: } michael@0: michael@0: /* ============================================================ */ michael@0: /* Code to wrap up old legacy test cases that used setup() and cleanup(). michael@0: * michael@0: * Not all of the tests designated "legacy" are ones that used setup() and michael@0: * cleanup(), of course. A test is legacy it it uses setup()/cleanup(), OR michael@0: * if it wants to find its event base/socketpair in global variables (ugh), michael@0: * OR if it wants to communicate success/failure through test_ok. michael@0: */ michael@0: michael@0: /* This is set to true if we're inside a legacy test wrapper. It lets the michael@0: setup() and cleanup() functions in regress.c know they're not needed. michael@0: */ michael@0: int in_legacy_test_wrapper = 0; michael@0: michael@0: static void dnslogcb(int w, const char *m) michael@0: { michael@0: TT_BLATHER(("%s", m)); michael@0: } michael@0: michael@0: /* creates a temporary file with the data in it */ michael@0: int michael@0: regress_make_tmpfile(const void *data, size_t datalen) michael@0: { michael@0: #ifndef WIN32 michael@0: char tmpfilename[32]; michael@0: int fd; michael@0: strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX"); michael@0: #ifdef _EVENT_HAVE_UMASK michael@0: umask(0077); michael@0: #endif michael@0: fd = mkstemp(tmpfilename); michael@0: if (fd == -1) michael@0: return (-1); michael@0: if (write(fd, data, datalen) != (int)datalen) { michael@0: close(fd); michael@0: return (-1); michael@0: } michael@0: lseek(fd, 0, SEEK_SET); michael@0: /* remove it from the file system */ michael@0: unlink(tmpfilename); michael@0: return (fd); michael@0: #else michael@0: /* XXXX actually delete the file later */ michael@0: char tmpfilepath[MAX_PATH]; michael@0: char tmpfilename[MAX_PATH]; michael@0: DWORD r, written; michael@0: int tries = 16; michael@0: HANDLE h; michael@0: r = GetTempPathA(MAX_PATH, tmpfilepath); michael@0: if (r > MAX_PATH || r == 0) michael@0: return (-1); michael@0: for (; tries > 0; --tries) { michael@0: r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename); michael@0: if (r == 0) michael@0: return (-1); michael@0: h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE, michael@0: 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); michael@0: if (h != INVALID_HANDLE_VALUE) michael@0: break; michael@0: } michael@0: if (tries == 0) michael@0: return (-1); michael@0: written = 0; michael@0: WriteFile(h, data, (DWORD)datalen, &written, NULL); michael@0: /* Closing the fd returned by this function will indeed close h. */ michael@0: return _open_osfhandle((intptr_t)h,_O_RDONLY); michael@0: #endif michael@0: } michael@0: michael@0: #ifndef _WIN32 michael@0: pid_t michael@0: regress_fork(void) michael@0: { michael@0: pid_t pid = fork(); michael@0: #ifdef FORK_BREAKS_GCOV michael@0: vproc_transaction_begin(0); michael@0: #endif michael@0: return pid; michael@0: } michael@0: #endif michael@0: michael@0: static void michael@0: ignore_log_cb(int s, const char *msg) michael@0: { michael@0: } michael@0: michael@0: static void * michael@0: basic_test_setup(const struct testcase_t *testcase) michael@0: { michael@0: struct event_base *base = NULL; michael@0: evutil_socket_t spair[2] = { -1, -1 }; michael@0: struct basic_test_data *data = NULL; michael@0: michael@0: #ifndef WIN32 michael@0: if (testcase->flags & TT_ENABLE_IOCP_FLAG) michael@0: return (void*)TT_SKIP; michael@0: #endif michael@0: michael@0: if (testcase->flags & TT_NEED_THREADS) { michael@0: if (!(testcase->flags & TT_FORK)) michael@0: return NULL; michael@0: #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED) michael@0: if (evthread_use_pthreads()) michael@0: exit(1); michael@0: #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED) michael@0: if (evthread_use_windows_threads()) michael@0: exit(1); michael@0: #else michael@0: return (void*)TT_SKIP; michael@0: #endif michael@0: } michael@0: michael@0: if (testcase->flags & TT_NEED_SOCKETPAIR) { michael@0: if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) { michael@0: fprintf(stderr, "%s: socketpair\n", __func__); michael@0: exit(1); michael@0: } michael@0: michael@0: if (evutil_make_socket_nonblocking(spair[0]) == -1) { michael@0: fprintf(stderr, "fcntl(O_NONBLOCK)"); michael@0: exit(1); michael@0: } michael@0: michael@0: if (evutil_make_socket_nonblocking(spair[1]) == -1) { michael@0: fprintf(stderr, "fcntl(O_NONBLOCK)"); michael@0: exit(1); michael@0: } michael@0: } michael@0: if (testcase->flags & TT_NEED_BASE) { michael@0: if (testcase->flags & TT_LEGACY) michael@0: base = event_init(); michael@0: else michael@0: base = event_base_new(); michael@0: if (!base) michael@0: exit(1); michael@0: } michael@0: if (testcase->flags & TT_ENABLE_IOCP_FLAG) { michael@0: if (event_base_start_iocp(base, 0)<0) { michael@0: event_base_free(base); michael@0: return (void*)TT_SKIP; michael@0: } michael@0: } michael@0: michael@0: if (testcase->flags & TT_NEED_DNS) { michael@0: evdns_set_log_fn(dnslogcb); michael@0: if (evdns_init()) michael@0: return NULL; /* fast failure */ /*XXX asserts. */ michael@0: } michael@0: michael@0: if (testcase->flags & TT_NO_LOGS) michael@0: event_set_log_callback(ignore_log_cb); michael@0: michael@0: data = calloc(1, sizeof(*data)); michael@0: if (!data) michael@0: exit(1); michael@0: data->base = base; michael@0: data->pair[0] = spair[0]; michael@0: data->pair[1] = spair[1]; michael@0: data->setup_data = testcase->setup_data; michael@0: return data; michael@0: } michael@0: michael@0: static int michael@0: basic_test_cleanup(const struct testcase_t *testcase, void *ptr) michael@0: { michael@0: struct basic_test_data *data = ptr; michael@0: michael@0: if (testcase->flags & TT_NO_LOGS) michael@0: event_set_log_callback(NULL); michael@0: michael@0: if (testcase->flags & TT_NEED_SOCKETPAIR) { michael@0: if (data->pair[0] != -1) michael@0: evutil_closesocket(data->pair[0]); michael@0: if (data->pair[1] != -1) michael@0: evutil_closesocket(data->pair[1]); michael@0: } michael@0: michael@0: if (testcase->flags & TT_NEED_DNS) { michael@0: evdns_shutdown(0); michael@0: } michael@0: michael@0: if (testcase->flags & TT_NEED_BASE) { michael@0: if (data->base) { michael@0: event_base_assert_ok(data->base); michael@0: event_base_free(data->base); michael@0: } michael@0: } michael@0: michael@0: free(data); michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: const struct testcase_setup_t basic_setup = { michael@0: basic_test_setup, basic_test_cleanup michael@0: }; michael@0: michael@0: /* The "data" for a legacy test is just a pointer to the void fn(void) michael@0: function implementing the test case. We need to set up some globals, michael@0: though, since that's where legacy tests expect to find a socketpair michael@0: (sometimes) and a global event_base (sometimes). michael@0: */ michael@0: static void * michael@0: legacy_test_setup(const struct testcase_t *testcase) michael@0: { michael@0: struct basic_test_data *data = basic_test_setup(testcase); michael@0: if (data == (void*)TT_SKIP || data == NULL) michael@0: return data; michael@0: global_base = data->base; michael@0: pair[0] = data->pair[0]; michael@0: pair[1] = data->pair[1]; michael@0: data->legacy_test_fn = testcase->setup_data; michael@0: return data; michael@0: } michael@0: michael@0: /* This function is the implementation of every legacy test case. It michael@0: sets test_ok to 0, invokes the test function, and tells tinytest that michael@0: the test failed if the test didn't set test_ok to 1. michael@0: */ michael@0: void michael@0: run_legacy_test_fn(void *ptr) michael@0: { michael@0: struct basic_test_data *data = ptr; michael@0: test_ok = called = 0; michael@0: michael@0: in_legacy_test_wrapper = 1; michael@0: data->legacy_test_fn(); /* This part actually calls the test */ michael@0: in_legacy_test_wrapper = 0; michael@0: michael@0: if (!test_ok) michael@0: tt_abort_msg("Legacy unit test failed"); michael@0: michael@0: end: michael@0: test_ok = 0; michael@0: } michael@0: michael@0: /* This function doesn't have to clean up ptr (which is just a pointer michael@0: to the test function), but it may need to close the socketpair or michael@0: free the event_base. michael@0: */ michael@0: static int michael@0: legacy_test_cleanup(const struct testcase_t *testcase, void *ptr) michael@0: { michael@0: int r = basic_test_cleanup(testcase, ptr); michael@0: pair[0] = pair[1] = -1; michael@0: global_base = NULL; michael@0: return r; michael@0: } michael@0: michael@0: const struct testcase_setup_t legacy_setup = { michael@0: legacy_test_setup, legacy_test_cleanup michael@0: }; michael@0: michael@0: /* ============================================================ */ michael@0: michael@0: #if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT) michael@0: struct testcase_t thread_testcases[] = { michael@0: { "basic", NULL, TT_SKIP, NULL, NULL }, michael@0: END_OF_TESTCASES michael@0: }; michael@0: #endif michael@0: michael@0: struct testgroup_t testgroups[] = { michael@0: { "main/", main_testcases }, michael@0: { "heap/", minheap_testcases }, michael@0: { "et/", edgetriggered_testcases }, michael@0: { "evbuffer/", evbuffer_testcases }, michael@0: { "signal/", signal_testcases }, michael@0: { "util/", util_testcases }, michael@0: { "bufferevent/", bufferevent_testcases }, michael@0: { "http/", http_testcases }, michael@0: { "dns/", dns_testcases }, michael@0: { "evtag/", evtag_testcases }, michael@0: { "rpc/", rpc_testcases }, michael@0: { "thread/", thread_testcases }, michael@0: { "listener/", listener_testcases }, michael@0: #ifdef WIN32 michael@0: { "iocp/", iocp_testcases }, michael@0: { "iocp/bufferevent/", bufferevent_iocp_testcases }, michael@0: { "iocp/listener/", listener_iocp_testcases }, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_OPENSSL michael@0: { "ssl/", ssl_testcases }, michael@0: #endif michael@0: END_OF_GROUPS michael@0: }; michael@0: michael@0: int michael@0: main(int argc, const char **argv) michael@0: { michael@0: #ifdef WIN32 michael@0: WORD wVersionRequested; michael@0: WSADATA wsaData; michael@0: michael@0: wVersionRequested = MAKEWORD(2, 2); michael@0: michael@0: (void) WSAStartup(wVersionRequested, &wsaData); michael@0: #endif michael@0: michael@0: #ifndef WIN32 michael@0: if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) michael@0: return 1; michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: tinytest_skip(testgroups, "http/connection_retry"); michael@0: #endif michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: if (!getenv("EVENT_NO_DEBUG_LOCKS")) michael@0: evthread_enable_lock_debuging(); michael@0: #endif michael@0: michael@0: if (tinytest_main(argc,argv,testgroups)) michael@0: return 1; michael@0: michael@0: return 0; michael@0: } michael@0: