xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_main.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
32b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
42b15cb3dSCy Schubert  *
52b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
62b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
72b15cb3dSCy Schubert  * are met:
82b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
92b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
102b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
112b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
122b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
132b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
142b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
152b15cb3dSCy Schubert  *
162b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262b15cb3dSCy Schubert  */
272b15cb3dSCy Schubert #include "util-internal.h"
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert #ifdef _WIN32
302b15cb3dSCy Schubert #include <winsock2.h>
312b15cb3dSCy Schubert #include <windows.h>
322b15cb3dSCy Schubert #include <io.h>
332b15cb3dSCy Schubert #include <fcntl.h>
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert 
36*a466cc55SCy Schubert /* move_pthread_to_realtime_scheduling_class() */
37*a466cc55SCy Schubert #ifdef EVENT__HAVE_MACH_MACH_H
38*a466cc55SCy Schubert #include <mach/mach.h>
39*a466cc55SCy Schubert #endif
40*a466cc55SCy Schubert #ifdef EVENT__HAVE_MACH_MACH_TIME_H
41*a466cc55SCy Schubert #include <mach/mach_time.h>
42*a466cc55SCy Schubert #endif
43*a466cc55SCy Schubert 
442b15cb3dSCy Schubert #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
452b15cb3dSCy Schubert #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
462b15cb3dSCy Schubert     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
472b15cb3dSCy Schubert #define FORK_BREAKS_GCOV
482b15cb3dSCy Schubert #include <vproc.h>
492b15cb3dSCy Schubert #endif
502b15cb3dSCy Schubert #endif
512b15cb3dSCy Schubert 
522b15cb3dSCy Schubert #include "event2/event-config.h"
532b15cb3dSCy Schubert 
542b15cb3dSCy Schubert #if 0
552b15cb3dSCy Schubert #include <sys/types.h>
562b15cb3dSCy Schubert #include <sys/stat.h>
572b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
582b15cb3dSCy Schubert #include <sys/time.h>
592b15cb3dSCy Schubert #endif
602b15cb3dSCy Schubert #include <sys/queue.h>
612b15cb3dSCy Schubert #include <signal.h>
622b15cb3dSCy Schubert #include <errno.h>
632b15cb3dSCy Schubert #endif
642b15cb3dSCy Schubert 
652b15cb3dSCy Schubert #include <sys/types.h>
662b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_STAT_H
672b15cb3dSCy Schubert #include <sys/stat.h>
682b15cb3dSCy Schubert #endif
692b15cb3dSCy Schubert 
702b15cb3dSCy Schubert #ifndef _WIN32
712b15cb3dSCy Schubert #include <sys/socket.h>
722b15cb3dSCy Schubert #include <sys/wait.h>
732b15cb3dSCy Schubert #include <signal.h>
742b15cb3dSCy Schubert #include <unistd.h>
752b15cb3dSCy Schubert #include <netdb.h>
762b15cb3dSCy Schubert #endif
772b15cb3dSCy Schubert 
782b15cb3dSCy Schubert #include <stdlib.h>
792b15cb3dSCy Schubert #include <stdio.h>
802b15cb3dSCy Schubert #include <string.h>
812b15cb3dSCy Schubert #include <assert.h>
822b15cb3dSCy Schubert 
832b15cb3dSCy Schubert #include "event2/util.h"
842b15cb3dSCy Schubert #include "event2/event.h"
852b15cb3dSCy Schubert #include "event2/event_compat.h"
862b15cb3dSCy Schubert #include "event2/dns.h"
872b15cb3dSCy Schubert #include "event2/dns_compat.h"
882b15cb3dSCy Schubert #include "event2/thread.h"
892b15cb3dSCy Schubert 
902b15cb3dSCy Schubert #include "event2/event-config.h"
912b15cb3dSCy Schubert #include "regress.h"
92*a466cc55SCy Schubert #include "regress_thread.h"
932b15cb3dSCy Schubert #include "tinytest.h"
942b15cb3dSCy Schubert #include "tinytest_macros.h"
952b15cb3dSCy Schubert #include "../iocp-internal.h"
962b15cb3dSCy Schubert #include "../event-internal.h"
97*a466cc55SCy Schubert #include "../evthread-internal.h"
982b15cb3dSCy Schubert 
99a25439b6SCy Schubert struct evutil_weakrand_state test_weakrand_state;
100a25439b6SCy Schubert 
1012b15cb3dSCy Schubert long
timeval_msec_diff(const struct timeval * start,const struct timeval * end)1022b15cb3dSCy Schubert timeval_msec_diff(const struct timeval *start, const struct timeval *end)
1032b15cb3dSCy Schubert {
1042b15cb3dSCy Schubert 	long ms = end->tv_sec - start->tv_sec;
1052b15cb3dSCy Schubert 	ms *= 1000;
1062b15cb3dSCy Schubert 	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
1072b15cb3dSCy Schubert 	return ms;
1082b15cb3dSCy Schubert }
1092b15cb3dSCy Schubert 
1102b15cb3dSCy Schubert /* ============================================================ */
1112b15cb3dSCy Schubert /* Code to wrap up old legacy test cases that used setup() and cleanup().
1122b15cb3dSCy Schubert  *
1132b15cb3dSCy Schubert  * Not all of the tests designated "legacy" are ones that used setup() and
1142b15cb3dSCy Schubert  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
1152b15cb3dSCy Schubert  * if it wants to find its event base/socketpair in global variables (ugh),
1162b15cb3dSCy Schubert  * OR if it wants to communicate success/failure through test_ok.
1172b15cb3dSCy Schubert  */
1182b15cb3dSCy Schubert 
1192b15cb3dSCy Schubert /* This is set to true if we're inside a legacy test wrapper.  It lets the
1202b15cb3dSCy Schubert    setup() and cleanup() functions in regress.c know they're not needed.
1212b15cb3dSCy Schubert  */
1222b15cb3dSCy Schubert int in_legacy_test_wrapper = 0;
1232b15cb3dSCy Schubert 
dnslogcb(int w,const char * m)1242b15cb3dSCy Schubert static void dnslogcb(int w, const char *m)
1252b15cb3dSCy Schubert {
1262b15cb3dSCy Schubert 	TT_BLATHER(("%s", m));
1272b15cb3dSCy Schubert }
1282b15cb3dSCy Schubert 
1292b15cb3dSCy Schubert /* creates a temporary file with the data in it.  If *filename_out gets set,
1302b15cb3dSCy Schubert  * the caller should try to unlink it. */
1312b15cb3dSCy Schubert int
regress_make_tmpfile(const void * data,size_t datalen,char ** filename_out)1322b15cb3dSCy Schubert regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
1332b15cb3dSCy Schubert {
1342b15cb3dSCy Schubert #ifndef _WIN32
1352b15cb3dSCy Schubert 	char tmpfilename[32];
1362b15cb3dSCy Schubert 	int fd;
1372b15cb3dSCy Schubert 	*filename_out = NULL;
1382b15cb3dSCy Schubert 	strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
1392b15cb3dSCy Schubert #ifdef EVENT__HAVE_UMASK
1402b15cb3dSCy Schubert 	umask(0077);
1412b15cb3dSCy Schubert #endif
1422b15cb3dSCy Schubert 	fd = mkstemp(tmpfilename);
1432b15cb3dSCy Schubert 	if (fd == -1)
1442b15cb3dSCy Schubert 		return (-1);
1452b15cb3dSCy Schubert 	if (write(fd, data, datalen) != (int)datalen) {
1462b15cb3dSCy Schubert 		close(fd);
1472b15cb3dSCy Schubert 		return (-1);
1482b15cb3dSCy Schubert 	}
1492b15cb3dSCy Schubert 	lseek(fd, 0, SEEK_SET);
1502b15cb3dSCy Schubert 	/* remove it from the file system */
1512b15cb3dSCy Schubert 	unlink(tmpfilename);
1522b15cb3dSCy Schubert 	return (fd);
1532b15cb3dSCy Schubert #else
1542b15cb3dSCy Schubert 	/* XXXX actually delete the file later */
1552b15cb3dSCy Schubert 	char tmpfilepath[MAX_PATH];
1562b15cb3dSCy Schubert 	char tmpfilename[MAX_PATH];
1572b15cb3dSCy Schubert 	DWORD r, written;
1582b15cb3dSCy Schubert 	int tries = 16;
1592b15cb3dSCy Schubert 	HANDLE h;
1602b15cb3dSCy Schubert 	r = GetTempPathA(MAX_PATH, tmpfilepath);
1612b15cb3dSCy Schubert 	if (r > MAX_PATH || r == 0)
1622b15cb3dSCy Schubert 		return (-1);
1632b15cb3dSCy Schubert 	for (; tries > 0; --tries) {
1642b15cb3dSCy Schubert 		r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
1652b15cb3dSCy Schubert 		if (r == 0)
1662b15cb3dSCy Schubert 			return (-1);
1672b15cb3dSCy Schubert 		h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
1682b15cb3dSCy Schubert 		    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1692b15cb3dSCy Schubert 		if (h != INVALID_HANDLE_VALUE)
1702b15cb3dSCy Schubert 			break;
1712b15cb3dSCy Schubert 	}
1722b15cb3dSCy Schubert 	if (tries == 0)
1732b15cb3dSCy Schubert 		return (-1);
1742b15cb3dSCy Schubert 	written = 0;
1752b15cb3dSCy Schubert 	*filename_out = strdup(tmpfilename);
1762b15cb3dSCy Schubert 	WriteFile(h, data, (DWORD)datalen, &written, NULL);
1772b15cb3dSCy Schubert 	/* Closing the fd returned by this function will indeed close h. */
1782b15cb3dSCy Schubert 	return _open_osfhandle((intptr_t)h,_O_RDONLY);
1792b15cb3dSCy Schubert #endif
1802b15cb3dSCy Schubert }
1812b15cb3dSCy Schubert 
1822b15cb3dSCy Schubert #ifndef _WIN32
1832b15cb3dSCy Schubert pid_t
regress_fork(void)1842b15cb3dSCy Schubert regress_fork(void)
1852b15cb3dSCy Schubert {
1862b15cb3dSCy Schubert 	pid_t pid = fork();
1872b15cb3dSCy Schubert #ifdef FORK_BREAKS_GCOV
1882b15cb3dSCy Schubert 	vproc_transaction_begin(0);
1892b15cb3dSCy Schubert #endif
1902b15cb3dSCy Schubert 	return pid;
1912b15cb3dSCy Schubert }
1922b15cb3dSCy Schubert #endif
1932b15cb3dSCy Schubert 
1942b15cb3dSCy Schubert static void
ignore_log_cb(int s,const char * msg)1952b15cb3dSCy Schubert ignore_log_cb(int s, const char *msg)
1962b15cb3dSCy Schubert {
1972b15cb3dSCy Schubert }
1982b15cb3dSCy Schubert 
199*a466cc55SCy Schubert /**
200*a466cc55SCy Schubert  * Put into the real time scheduling class for better timers latency.
201*a466cc55SCy Schubert  * https://developer.apple.com/library/archive/technotes/tn2169/_index.html#//apple_ref/doc/uid/DTS40013172-CH1-TNTAG6000
202*a466cc55SCy Schubert  */
203*a466cc55SCy Schubert #if defined(__APPLE__)
move_pthread_to_realtime_scheduling_class(pthread_t pthread)204*a466cc55SCy Schubert static void move_pthread_to_realtime_scheduling_class(pthread_t pthread)
205*a466cc55SCy Schubert {
206*a466cc55SCy Schubert 	mach_timebase_info_data_t info;
207*a466cc55SCy Schubert 	mach_timebase_info(&info);
208*a466cc55SCy Schubert 
209*a466cc55SCy Schubert 	const uint64_t NANOS_PER_MSEC = 1000000ULL;
210*a466cc55SCy Schubert 	double clock2abs =
211*a466cc55SCy Schubert 		((double)info.denom / (double)info.numer) * NANOS_PER_MSEC;
212*a466cc55SCy Schubert 
213*a466cc55SCy Schubert 	thread_time_constraint_policy_data_t policy;
214*a466cc55SCy Schubert 	policy.period      = 0;
215*a466cc55SCy Schubert 	policy.computation = (uint32_t)(5 * clock2abs); // 5 ms of work
216*a466cc55SCy Schubert 	policy.constraint  = (uint32_t)(10 * clock2abs);
217*a466cc55SCy Schubert 	policy.preemptible = FALSE;
218*a466cc55SCy Schubert 
219*a466cc55SCy Schubert 	int kr = thread_policy_set(pthread_mach_thread_np(pthread),
220*a466cc55SCy Schubert 		THREAD_TIME_CONSTRAINT_POLICY,
221*a466cc55SCy Schubert 		(thread_policy_t)&policy,
222*a466cc55SCy Schubert 		THREAD_TIME_CONSTRAINT_POLICY_COUNT);
223*a466cc55SCy Schubert 	if (kr != KERN_SUCCESS) {
224*a466cc55SCy Schubert 		mach_error("thread_policy_set:", kr);
225*a466cc55SCy Schubert 		exit(1);
226*a466cc55SCy Schubert 	}
227*a466cc55SCy Schubert }
228*a466cc55SCy Schubert 
thread_setup(THREAD_T pthread)229*a466cc55SCy Schubert void thread_setup(THREAD_T pthread)
230*a466cc55SCy Schubert {
231*a466cc55SCy Schubert 	move_pthread_to_realtime_scheduling_class(pthread);
232*a466cc55SCy Schubert }
233*a466cc55SCy Schubert #else /** \__APPLE__ */
thread_setup(THREAD_T pthread)234*a466cc55SCy Schubert void thread_setup(THREAD_T pthread) {}
235*a466cc55SCy Schubert #endif /** \!__APPLE__ */
236*a466cc55SCy Schubert 
237*a466cc55SCy Schubert 
238*a466cc55SCy Schubert void *
basic_test_setup(const struct testcase_t * testcase)2392b15cb3dSCy Schubert basic_test_setup(const struct testcase_t *testcase)
2402b15cb3dSCy Schubert {
2412b15cb3dSCy Schubert 	struct event_base *base = NULL;
2422b15cb3dSCy Schubert 	evutil_socket_t spair[2] = { -1, -1 };
2432b15cb3dSCy Schubert 	struct basic_test_data *data = NULL;
2442b15cb3dSCy Schubert 
245*a466cc55SCy Schubert 	thread_setup(THREAD_SELF());
246*a466cc55SCy Schubert 
2472b15cb3dSCy Schubert #ifndef _WIN32
2482b15cb3dSCy Schubert 	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
2492b15cb3dSCy Schubert 		return (void*)TT_SKIP;
2502b15cb3dSCy Schubert #endif
2512b15cb3dSCy Schubert 
252*a466cc55SCy Schubert 	if (testcase->flags & TT_ENABLE_DEBUG_MODE &&
253*a466cc55SCy Schubert 		!libevent_tests_running_in_debug_mode) {
254*a466cc55SCy Schubert 		event_enable_debug_mode();
255*a466cc55SCy Schubert 		libevent_tests_running_in_debug_mode = 1;
256*a466cc55SCy Schubert 	}
257*a466cc55SCy Schubert 
2582b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_THREADS) {
2592b15cb3dSCy Schubert 		if (!(testcase->flags & TT_FORK))
2602b15cb3dSCy Schubert 			return NULL;
2612b15cb3dSCy Schubert #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
2622b15cb3dSCy Schubert 		if (evthread_use_pthreads())
2632b15cb3dSCy Schubert 			exit(1);
2642b15cb3dSCy Schubert #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
2652b15cb3dSCy Schubert 		if (evthread_use_windows_threads())
2662b15cb3dSCy Schubert 			exit(1);
2672b15cb3dSCy Schubert #else
2682b15cb3dSCy Schubert 		return (void*)TT_SKIP;
2692b15cb3dSCy Schubert #endif
2702b15cb3dSCy Schubert 	}
2712b15cb3dSCy Schubert 
2722b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
2732b15cb3dSCy Schubert 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
2742b15cb3dSCy Schubert 			fprintf(stderr, "%s: socketpair\n", __func__);
2752b15cb3dSCy Schubert 			exit(1);
2762b15cb3dSCy Schubert 		}
2772b15cb3dSCy Schubert 
2782b15cb3dSCy Schubert 		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
2792b15cb3dSCy Schubert 			fprintf(stderr, "fcntl(O_NONBLOCK)");
2802b15cb3dSCy Schubert 			exit(1);
2812b15cb3dSCy Schubert 		}
2822b15cb3dSCy Schubert 
2832b15cb3dSCy Schubert 		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
2842b15cb3dSCy Schubert 			fprintf(stderr, "fcntl(O_NONBLOCK)");
2852b15cb3dSCy Schubert 			exit(1);
2862b15cb3dSCy Schubert 		}
2872b15cb3dSCy Schubert 	}
2882b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_BASE) {
2892b15cb3dSCy Schubert 		if (testcase->flags & TT_LEGACY)
2902b15cb3dSCy Schubert 			base = event_init();
2912b15cb3dSCy Schubert 		else
2922b15cb3dSCy Schubert 			base = event_base_new();
2932b15cb3dSCy Schubert 		if (!base)
2942b15cb3dSCy Schubert 			exit(1);
2952b15cb3dSCy Schubert 	}
2962b15cb3dSCy Schubert 	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
2972b15cb3dSCy Schubert 		if (event_base_start_iocp_(base, 0)<0) {
2982b15cb3dSCy Schubert 			event_base_free(base);
2992b15cb3dSCy Schubert 			return (void*)TT_SKIP;
3002b15cb3dSCy Schubert 		}
3012b15cb3dSCy Schubert 	}
3022b15cb3dSCy Schubert 
3032b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_DNS) {
3042b15cb3dSCy Schubert 		evdns_set_log_fn(dnslogcb);
3052b15cb3dSCy Schubert 		if (evdns_init())
3062b15cb3dSCy Schubert 			return NULL; /* fast failure */ /*XXX asserts. */
3072b15cb3dSCy Schubert 	}
3082b15cb3dSCy Schubert 
3092b15cb3dSCy Schubert 	if (testcase->flags & TT_NO_LOGS)
3102b15cb3dSCy Schubert 		event_set_log_callback(ignore_log_cb);
3112b15cb3dSCy Schubert 
3122b15cb3dSCy Schubert 	data = calloc(1, sizeof(*data));
3132b15cb3dSCy Schubert 	if (!data)
3142b15cb3dSCy Schubert 		exit(1);
3152b15cb3dSCy Schubert 	data->base = base;
3162b15cb3dSCy Schubert 	data->pair[0] = spair[0];
3172b15cb3dSCy Schubert 	data->pair[1] = spair[1];
3182b15cb3dSCy Schubert 	data->setup_data = testcase->setup_data;
3192b15cb3dSCy Schubert 	return data;
3202b15cb3dSCy Schubert }
3212b15cb3dSCy Schubert 
322*a466cc55SCy Schubert int
basic_test_cleanup(const struct testcase_t * testcase,void * ptr)3232b15cb3dSCy Schubert basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
3242b15cb3dSCy Schubert {
3252b15cb3dSCy Schubert 	struct basic_test_data *data = ptr;
3262b15cb3dSCy Schubert 
3272b15cb3dSCy Schubert 	if (testcase->flags & TT_NO_LOGS)
3282b15cb3dSCy Schubert 		event_set_log_callback(NULL);
3292b15cb3dSCy Schubert 
3302b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
3312b15cb3dSCy Schubert 		if (data->pair[0] != -1)
3322b15cb3dSCy Schubert 			evutil_closesocket(data->pair[0]);
3332b15cb3dSCy Schubert 		if (data->pair[1] != -1)
3342b15cb3dSCy Schubert 			evutil_closesocket(data->pair[1]);
3352b15cb3dSCy Schubert 	}
3362b15cb3dSCy Schubert 
3372b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_DNS) {
3382b15cb3dSCy Schubert 		evdns_shutdown(0);
3392b15cb3dSCy Schubert 	}
3402b15cb3dSCy Schubert 
3412b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_BASE) {
3422b15cb3dSCy Schubert 		if (data->base) {
3432b15cb3dSCy Schubert 			event_base_assert_ok_(data->base);
3442b15cb3dSCy Schubert 			event_base_free(data->base);
3452b15cb3dSCy Schubert 		}
3462b15cb3dSCy Schubert 	}
3472b15cb3dSCy Schubert 
3482b15cb3dSCy Schubert 	if (testcase->flags & TT_FORK)
3492b15cb3dSCy Schubert 		libevent_global_shutdown();
3502b15cb3dSCy Schubert 
3512b15cb3dSCy Schubert 	free(data);
3522b15cb3dSCy Schubert 
3532b15cb3dSCy Schubert 	return 1;
3542b15cb3dSCy Schubert }
3552b15cb3dSCy Schubert 
3562b15cb3dSCy Schubert const struct testcase_setup_t basic_setup = {
3572b15cb3dSCy Schubert 	basic_test_setup, basic_test_cleanup
3582b15cb3dSCy Schubert };
3592b15cb3dSCy Schubert 
3602b15cb3dSCy Schubert /* The "data" for a legacy test is just a pointer to the void fn(void)
3612b15cb3dSCy Schubert    function implementing the test case.  We need to set up some globals,
3622b15cb3dSCy Schubert    though, since that's where legacy tests expect to find a socketpair
3632b15cb3dSCy Schubert    (sometimes) and a global event_base (sometimes).
3642b15cb3dSCy Schubert  */
3652b15cb3dSCy Schubert static void *
legacy_test_setup(const struct testcase_t * testcase)3662b15cb3dSCy Schubert legacy_test_setup(const struct testcase_t *testcase)
3672b15cb3dSCy Schubert {
3682b15cb3dSCy Schubert 	struct basic_test_data *data = basic_test_setup(testcase);
3692b15cb3dSCy Schubert 	if (data == (void*)TT_SKIP || data == NULL)
3702b15cb3dSCy Schubert 		return data;
3712b15cb3dSCy Schubert 	global_base = data->base;
3722b15cb3dSCy Schubert 	pair[0] = data->pair[0];
3732b15cb3dSCy Schubert 	pair[1] = data->pair[1];
3742b15cb3dSCy Schubert 	data->legacy_test_fn = testcase->setup_data;
3752b15cb3dSCy Schubert 	return data;
3762b15cb3dSCy Schubert }
3772b15cb3dSCy Schubert 
3782b15cb3dSCy Schubert /* This function is the implementation of every legacy test case.  It
3792b15cb3dSCy Schubert    sets test_ok to 0, invokes the test function, and tells tinytest that
3802b15cb3dSCy Schubert    the test failed if the test didn't set test_ok to 1.
3812b15cb3dSCy Schubert  */
3822b15cb3dSCy Schubert void
run_legacy_test_fn(void * ptr)3832b15cb3dSCy Schubert run_legacy_test_fn(void *ptr)
3842b15cb3dSCy Schubert {
3852b15cb3dSCy Schubert 	struct basic_test_data *data = ptr;
3862b15cb3dSCy Schubert 	test_ok = called = 0;
3872b15cb3dSCy Schubert 
3882b15cb3dSCy Schubert 	in_legacy_test_wrapper = 1;
3892b15cb3dSCy Schubert 	data->legacy_test_fn(); /* This part actually calls the test */
3902b15cb3dSCy Schubert 	in_legacy_test_wrapper = 0;
3912b15cb3dSCy Schubert 
3922b15cb3dSCy Schubert 	if (!test_ok)
3932b15cb3dSCy Schubert 		tt_abort_msg("Legacy unit test failed");
3942b15cb3dSCy Schubert 
3952b15cb3dSCy Schubert end:
3962b15cb3dSCy Schubert 	test_ok = 0;
3972b15cb3dSCy Schubert }
3982b15cb3dSCy Schubert 
3992b15cb3dSCy Schubert /* This function doesn't have to clean up ptr (which is just a pointer
4002b15cb3dSCy Schubert    to the test function), but it may need to close the socketpair or
4012b15cb3dSCy Schubert    free the event_base.
4022b15cb3dSCy Schubert  */
4032b15cb3dSCy Schubert static int
legacy_test_cleanup(const struct testcase_t * testcase,void * ptr)4042b15cb3dSCy Schubert legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
4052b15cb3dSCy Schubert {
4062b15cb3dSCy Schubert 	int r = basic_test_cleanup(testcase, ptr);
4072b15cb3dSCy Schubert 	pair[0] = pair[1] = -1;
4082b15cb3dSCy Schubert 	global_base = NULL;
4092b15cb3dSCy Schubert 	return r;
4102b15cb3dSCy Schubert }
4112b15cb3dSCy Schubert 
4122b15cb3dSCy Schubert const struct testcase_setup_t legacy_setup = {
4132b15cb3dSCy Schubert 	legacy_test_setup, legacy_test_cleanup
4142b15cb3dSCy Schubert };
4152b15cb3dSCy Schubert 
4162b15cb3dSCy Schubert /* ============================================================ */
4172b15cb3dSCy Schubert 
4182b15cb3dSCy Schubert #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
4192b15cb3dSCy Schubert struct testcase_t thread_testcases[] = {
4202b15cb3dSCy Schubert 	{ "basic", NULL, TT_SKIP, NULL, NULL },
4212b15cb3dSCy Schubert 	END_OF_TESTCASES
4222b15cb3dSCy Schubert };
4232b15cb3dSCy Schubert #endif
4242b15cb3dSCy Schubert 
4252b15cb3dSCy Schubert struct testgroup_t testgroups[] = {
4262b15cb3dSCy Schubert 	{ "main/", main_testcases },
4272b15cb3dSCy Schubert 	{ "heap/", minheap_testcases },
4282b15cb3dSCy Schubert 	{ "et/", edgetriggered_testcases },
4292b15cb3dSCy Schubert 	{ "finalize/", finalize_testcases },
4302b15cb3dSCy Schubert 	{ "evbuffer/", evbuffer_testcases },
4312b15cb3dSCy Schubert 	{ "signal/", signal_testcases },
4322b15cb3dSCy Schubert 	{ "util/", util_testcases },
4332b15cb3dSCy Schubert 	{ "bufferevent/", bufferevent_testcases },
4342b15cb3dSCy Schubert 	{ "http/", http_testcases },
4352b15cb3dSCy Schubert 	{ "dns/", dns_testcases },
4362b15cb3dSCy Schubert 	{ "evtag/", evtag_testcases },
4372b15cb3dSCy Schubert 	{ "rpc/", rpc_testcases },
4382b15cb3dSCy Schubert 	{ "thread/", thread_testcases },
4392b15cb3dSCy Schubert 	{ "listener/", listener_testcases },
4402b15cb3dSCy Schubert #ifdef _WIN32
4412b15cb3dSCy Schubert 	{ "iocp/", iocp_testcases },
4422b15cb3dSCy Schubert 	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
4432b15cb3dSCy Schubert 	{ "iocp/listener/", listener_iocp_testcases },
444*a466cc55SCy Schubert 	{ "iocp/http/", http_iocp_testcases },
4452b15cb3dSCy Schubert #endif
4462b15cb3dSCy Schubert #ifdef EVENT__HAVE_OPENSSL
4472b15cb3dSCy Schubert 	{ "ssl/", ssl_testcases },
4482b15cb3dSCy Schubert #endif
4492b15cb3dSCy Schubert 	END_OF_GROUPS
4502b15cb3dSCy Schubert };
4512b15cb3dSCy Schubert 
4522b15cb3dSCy Schubert const char *alltests[] = { "+..", NULL };
4532b15cb3dSCy Schubert const char *livenettests[] = {
4542b15cb3dSCy Schubert 	"+util/getaddrinfo_live",
4552b15cb3dSCy Schubert 	"+dns/gethostby..",
4562b15cb3dSCy Schubert 	"+dns/resolve_reverse",
4572b15cb3dSCy Schubert 	NULL
4582b15cb3dSCy Schubert };
4592b15cb3dSCy Schubert const char *finetimetests[] = {
4602b15cb3dSCy Schubert 	"+util/monotonic_res_precise",
4612b15cb3dSCy Schubert 	"+util/monotonic_res_fallback",
4622b15cb3dSCy Schubert 	"+thread/deferred_cb_skew",
4632b15cb3dSCy Schubert 	"+http/connection_retry",
464*a466cc55SCy Schubert 	"+http/https_connection_retry",
4652b15cb3dSCy Schubert 	NULL
4662b15cb3dSCy Schubert };
4672b15cb3dSCy Schubert struct testlist_alias_t testaliases[] = {
4682b15cb3dSCy Schubert 	{ "all", alltests },
4692b15cb3dSCy Schubert 	{ "live_net", livenettests },
4702b15cb3dSCy Schubert 	{ "fine_timing", finetimetests },
4712b15cb3dSCy Schubert 	END_OF_ALIASES
4722b15cb3dSCy Schubert };
4732b15cb3dSCy Schubert 
4742b15cb3dSCy Schubert int libevent_tests_running_in_debug_mode = 0;
4752b15cb3dSCy Schubert 
4762b15cb3dSCy Schubert int
main(int argc,const char ** argv)4772b15cb3dSCy Schubert main(int argc, const char **argv)
4782b15cb3dSCy Schubert {
4792b15cb3dSCy Schubert #ifdef _WIN32
4802b15cb3dSCy Schubert 	WORD wVersionRequested;
4812b15cb3dSCy Schubert 	WSADATA wsaData;
4822b15cb3dSCy Schubert 
4832b15cb3dSCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
4842b15cb3dSCy Schubert 
4852b15cb3dSCy Schubert 	(void) WSAStartup(wVersionRequested, &wsaData);
4862b15cb3dSCy Schubert #endif
4872b15cb3dSCy Schubert 
4882b15cb3dSCy Schubert #ifndef _WIN32
4892b15cb3dSCy Schubert 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4902b15cb3dSCy Schubert 		return 1;
4912b15cb3dSCy Schubert #endif
4922b15cb3dSCy Schubert 
4932b15cb3dSCy Schubert #ifdef _WIN32
4942b15cb3dSCy Schubert 	tinytest_skip(testgroups, "http/connection_retry");
495*a466cc55SCy Schubert 	tinytest_skip(testgroups, "http/https_connection_retry");
496*a466cc55SCy Schubert 	tinytest_skip(testgroups, "http/read_on_write_error");
4972b15cb3dSCy Schubert #endif
4982b15cb3dSCy Schubert 
4992b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
5002b15cb3dSCy Schubert 	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
5012b15cb3dSCy Schubert 		evthread_enable_lock_debugging();
5022b15cb3dSCy Schubert #endif
5032b15cb3dSCy Schubert 
5042b15cb3dSCy Schubert 	if (getenv("EVENT_DEBUG_MODE")) {
5052b15cb3dSCy Schubert 		event_enable_debug_mode();
5062b15cb3dSCy Schubert 		libevent_tests_running_in_debug_mode = 1;
5072b15cb3dSCy Schubert 	}
5082b15cb3dSCy Schubert 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
5092b15cb3dSCy Schubert 		event_enable_debug_logging(EVENT_DBG_ALL);
5102b15cb3dSCy Schubert 	}
5112b15cb3dSCy Schubert 
5122b15cb3dSCy Schubert 	tinytest_set_aliases(testaliases);
5132b15cb3dSCy Schubert 
514a25439b6SCy Schubert 	evutil_weakrand_seed_(&test_weakrand_state, 0);
515a25439b6SCy Schubert 
516*a466cc55SCy Schubert 	if (getenv("EVENT_NO_FILE_BUFFERING")) {
517*a466cc55SCy Schubert 		setbuf(stdout, NULL);
518*a466cc55SCy Schubert 		setbuf(stderr, NULL);
519*a466cc55SCy Schubert 	}
520*a466cc55SCy Schubert 
5212b15cb3dSCy Schubert 	if (tinytest_main(argc,argv,testgroups))
5222b15cb3dSCy Schubert 		return 1;
5232b15cb3dSCy Schubert 
5242b15cb3dSCy Schubert 	libevent_global_shutdown();
5252b15cb3dSCy Schubert 
5262b15cb3dSCy Schubert 	return 0;
5272b15cb3dSCy Schubert }
5282b15cb3dSCy Schubert 
529