2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "util-internal.h"
36 #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
37 #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
38 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
39 #define FORK_BREAKS_GCOV
44 #include "event2/event-config.h"
47 #include <sys/types.h>
49 #ifdef EVENT__HAVE_SYS_TIME_H
52 #include <sys/queue.h>
57 #include <sys/types.h>
58 #ifdef EVENT__HAVE_SYS_STAT_H
63 #include <sys/socket.h>
75 #include "event2/util.h"
76 #include "event2/event.h"
77 #include "event2/event_compat.h"
78 #include "event2/dns.h"
79 #include "event2/dns_compat.h"
80 #include "event2/thread.h"
82 #include "event2/event-config.h"
85 #include "tinytest_macros.h"
86 #include "../iocp-internal.h"
87 #include "../event-internal.h"
89 struct evutil_weakrand_state test_weakrand_state;
92 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
94 long ms = end->tv_sec - start->tv_sec;
96 ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
100 /* ============================================================ */
101 /* Code to wrap up old legacy test cases that used setup() and cleanup().
103 * Not all of the tests designated "legacy" are ones that used setup() and
104 * cleanup(), of course. A test is legacy it it uses setup()/cleanup(), OR
105 * if it wants to find its event base/socketpair in global variables (ugh),
106 * OR if it wants to communicate success/failure through test_ok.
109 /* This is set to true if we're inside a legacy test wrapper. It lets the
110 setup() and cleanup() functions in regress.c know they're not needed.
112 int in_legacy_test_wrapper = 0;
114 static void dnslogcb(int w, const char *m)
116 TT_BLATHER(("%s", m));
119 /* creates a temporary file with the data in it. If *filename_out gets set,
120 * the caller should try to unlink it. */
122 regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
125 char tmpfilename[32];
127 *filename_out = NULL;
128 strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
129 #ifdef EVENT__HAVE_UMASK
132 fd = mkstemp(tmpfilename);
135 if (write(fd, data, datalen) != (int)datalen) {
139 lseek(fd, 0, SEEK_SET);
140 /* remove it from the file system */
144 /* XXXX actually delete the file later */
145 char tmpfilepath[MAX_PATH];
146 char tmpfilename[MAX_PATH];
150 r = GetTempPathA(MAX_PATH, tmpfilepath);
151 if (r > MAX_PATH || r == 0)
153 for (; tries > 0; --tries) {
154 r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
157 h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
158 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
159 if (h != INVALID_HANDLE_VALUE)
165 *filename_out = strdup(tmpfilename);
166 WriteFile(h, data, (DWORD)datalen, &written, NULL);
167 /* Closing the fd returned by this function will indeed close h. */
168 return _open_osfhandle((intptr_t)h,_O_RDONLY);
177 #ifdef FORK_BREAKS_GCOV
178 vproc_transaction_begin(0);
185 ignore_log_cb(int s, const char *msg)
190 basic_test_setup(const struct testcase_t *testcase)
192 struct event_base *base = NULL;
193 evutil_socket_t spair[2] = { -1, -1 };
194 struct basic_test_data *data = NULL;
197 if (testcase->flags & TT_ENABLE_IOCP_FLAG)
198 return (void*)TT_SKIP;
201 if (testcase->flags & TT_NEED_THREADS) {
202 if (!(testcase->flags & TT_FORK))
204 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
205 if (evthread_use_pthreads())
207 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
208 if (evthread_use_windows_threads())
211 return (void*)TT_SKIP;
215 if (testcase->flags & TT_NEED_SOCKETPAIR) {
216 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
217 fprintf(stderr, "%s: socketpair\n", __func__);
221 if (evutil_make_socket_nonblocking(spair[0]) == -1) {
222 fprintf(stderr, "fcntl(O_NONBLOCK)");
226 if (evutil_make_socket_nonblocking(spair[1]) == -1) {
227 fprintf(stderr, "fcntl(O_NONBLOCK)");
231 if (testcase->flags & TT_NEED_BASE) {
232 if (testcase->flags & TT_LEGACY)
235 base = event_base_new();
239 if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
240 if (event_base_start_iocp_(base, 0)<0) {
241 event_base_free(base);
242 return (void*)TT_SKIP;
246 if (testcase->flags & TT_NEED_DNS) {
247 evdns_set_log_fn(dnslogcb);
249 return NULL; /* fast failure */ /*XXX asserts. */
252 if (testcase->flags & TT_NO_LOGS)
253 event_set_log_callback(ignore_log_cb);
255 data = calloc(1, sizeof(*data));
259 data->pair[0] = spair[0];
260 data->pair[1] = spair[1];
261 data->setup_data = testcase->setup_data;
266 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
268 struct basic_test_data *data = ptr;
270 if (testcase->flags & TT_NO_LOGS)
271 event_set_log_callback(NULL);
273 if (testcase->flags & TT_NEED_SOCKETPAIR) {
274 if (data->pair[0] != -1)
275 evutil_closesocket(data->pair[0]);
276 if (data->pair[1] != -1)
277 evutil_closesocket(data->pair[1]);
280 if (testcase->flags & TT_NEED_DNS) {
284 if (testcase->flags & TT_NEED_BASE) {
286 event_base_assert_ok_(data->base);
287 event_base_free(data->base);
291 if (testcase->flags & TT_FORK)
292 libevent_global_shutdown();
299 const struct testcase_setup_t basic_setup = {
300 basic_test_setup, basic_test_cleanup
303 /* The "data" for a legacy test is just a pointer to the void fn(void)
304 function implementing the test case. We need to set up some globals,
305 though, since that's where legacy tests expect to find a socketpair
306 (sometimes) and a global event_base (sometimes).
309 legacy_test_setup(const struct testcase_t *testcase)
311 struct basic_test_data *data = basic_test_setup(testcase);
312 if (data == (void*)TT_SKIP || data == NULL)
314 global_base = data->base;
315 pair[0] = data->pair[0];
316 pair[1] = data->pair[1];
317 data->legacy_test_fn = testcase->setup_data;
321 /* This function is the implementation of every legacy test case. It
322 sets test_ok to 0, invokes the test function, and tells tinytest that
323 the test failed if the test didn't set test_ok to 1.
326 run_legacy_test_fn(void *ptr)
328 struct basic_test_data *data = ptr;
329 test_ok = called = 0;
331 in_legacy_test_wrapper = 1;
332 data->legacy_test_fn(); /* This part actually calls the test */
333 in_legacy_test_wrapper = 0;
336 tt_abort_msg("Legacy unit test failed");
342 /* This function doesn't have to clean up ptr (which is just a pointer
343 to the test function), but it may need to close the socketpair or
347 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
349 int r = basic_test_cleanup(testcase, ptr);
350 pair[0] = pair[1] = -1;
355 const struct testcase_setup_t legacy_setup = {
356 legacy_test_setup, legacy_test_cleanup
359 /* ============================================================ */
361 #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
362 struct testcase_t thread_testcases[] = {
363 { "basic", NULL, TT_SKIP, NULL, NULL },
368 struct testgroup_t testgroups[] = {
369 { "main/", main_testcases },
370 { "heap/", minheap_testcases },
371 { "et/", edgetriggered_testcases },
372 { "finalize/", finalize_testcases },
373 { "evbuffer/", evbuffer_testcases },
374 { "signal/", signal_testcases },
375 { "util/", util_testcases },
376 { "bufferevent/", bufferevent_testcases },
377 { "http/", http_testcases },
378 { "dns/", dns_testcases },
379 { "evtag/", evtag_testcases },
380 { "rpc/", rpc_testcases },
381 { "thread/", thread_testcases },
382 { "listener/", listener_testcases },
384 { "iocp/", iocp_testcases },
385 { "iocp/bufferevent/", bufferevent_iocp_testcases },
386 { "iocp/listener/", listener_iocp_testcases },
388 #ifdef EVENT__HAVE_OPENSSL
389 { "ssl/", ssl_testcases },
394 const char *alltests[] = { "+..", NULL };
395 const char *livenettests[] = {
396 "+util/getaddrinfo_live",
398 "+dns/resolve_reverse",
401 const char *finetimetests[] = {
402 "+util/monotonic_res_precise",
403 "+util/monotonic_res_fallback",
404 "+thread/deferred_cb_skew",
405 "+http/connection_retry",
406 "+http/https_connection_retry",
409 struct testlist_alias_t testaliases[] = {
411 { "live_net", livenettests },
412 { "fine_timing", finetimetests },
416 int libevent_tests_running_in_debug_mode = 0;
419 main(int argc, const char **argv)
422 WORD wVersionRequested;
425 wVersionRequested = MAKEWORD(2, 2);
427 (void) WSAStartup(wVersionRequested, &wsaData);
431 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
436 tinytest_skip(testgroups, "http/connection_retry");
437 tinytest_skip(testgroups, "http/https_connection_retry");
440 #ifndef EVENT__DISABLE_THREAD_SUPPORT
441 if (!getenv("EVENT_NO_DEBUG_LOCKS"))
442 evthread_enable_lock_debugging();
445 if (getenv("EVENT_DEBUG_MODE")) {
446 event_enable_debug_mode();
447 libevent_tests_running_in_debug_mode = 1;
449 if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
450 event_enable_debug_logging(EVENT_DBG_ALL);
453 tinytest_set_aliases(testaliases);
455 evutil_weakrand_seed_(&test_weakrand_state, 0);
457 if (tinytest_main(argc,argv,testgroups))
460 libevent_global_shutdown();