xref: /netbsd-src/external/bsd/libevent/dist/test/regress_main.c (revision 7e68cdd7306a8b6c32d6a32c16ba01e5a2ddc083)
1*7e68cdd7Schristos /*	$NetBSD: regress_main.c,v 1.4 2021/04/07 03:36:48 christos Exp $	*/
2*7e68cdd7Schristos 
36ecf6635Schristos /*
46ecf6635Schristos  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
56ecf6635Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
66ecf6635Schristos  *
76ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
86ecf6635Schristos  * modification, are permitted provided that the following conditions
96ecf6635Schristos  * are met:
106ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
116ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
126ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
136ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
146ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
156ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
166ecf6635Schristos  *    derived from this software without specific prior written permission.
176ecf6635Schristos  *
186ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
196ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
206ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
216ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
226ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
236ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
256ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
266ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
276ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286ecf6635Schristos  */
290d738af4Schristos #include "util-internal.h"
306ecf6635Schristos 
310d738af4Schristos #ifdef _WIN32
326ecf6635Schristos #include <winsock2.h>
336ecf6635Schristos #include <windows.h>
346ecf6635Schristos #include <io.h>
356ecf6635Schristos #include <fcntl.h>
366ecf6635Schristos #endif
376ecf6635Schristos 
38*7e68cdd7Schristos /* move_pthread_to_realtime_scheduling_class() */
39*7e68cdd7Schristos #ifdef EVENT__HAVE_MACH_MACH_H
40*7e68cdd7Schristos #include <mach/mach.h>
41*7e68cdd7Schristos #endif
42*7e68cdd7Schristos #ifdef EVENT__HAVE_MACH_MACH_TIME_H
43*7e68cdd7Schristos #include <mach/mach_time.h>
44*7e68cdd7Schristos #endif
45*7e68cdd7Schristos 
466ecf6635Schristos #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
476ecf6635Schristos #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
486ecf6635Schristos     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
496ecf6635Schristos #define FORK_BREAKS_GCOV
506ecf6635Schristos #include <vproc.h>
516ecf6635Schristos #endif
526ecf6635Schristos #endif
536ecf6635Schristos 
546ecf6635Schristos #include "event2/event-config.h"
556ecf6635Schristos #include <sys/cdefs.h>
56*7e68cdd7Schristos __RCSID("$NetBSD: regress_main.c,v 1.4 2021/04/07 03:36:48 christos Exp $");
576ecf6635Schristos 
586ecf6635Schristos #if 0
596ecf6635Schristos #include <sys/types.h>
606ecf6635Schristos #include <sys/stat.h>
610d738af4Schristos #ifdef EVENT__HAVE_SYS_TIME_H
626ecf6635Schristos #include <sys/time.h>
636ecf6635Schristos #endif
646ecf6635Schristos #include <sys/queue.h>
656ecf6635Schristos #include <signal.h>
666ecf6635Schristos #include <errno.h>
676ecf6635Schristos #endif
686ecf6635Schristos 
696ecf6635Schristos #include <sys/types.h>
700d738af4Schristos #ifdef EVENT__HAVE_SYS_STAT_H
716ecf6635Schristos #include <sys/stat.h>
726ecf6635Schristos #endif
736ecf6635Schristos 
740d738af4Schristos #ifndef _WIN32
756ecf6635Schristos #include <sys/socket.h>
766ecf6635Schristos #include <sys/wait.h>
776ecf6635Schristos #include <signal.h>
786ecf6635Schristos #include <unistd.h>
796ecf6635Schristos #include <netdb.h>
806ecf6635Schristos #endif
816ecf6635Schristos 
826ecf6635Schristos #include <stdlib.h>
836ecf6635Schristos #include <stdio.h>
846ecf6635Schristos #include <string.h>
856ecf6635Schristos #include <assert.h>
866ecf6635Schristos 
876ecf6635Schristos #include "event2/util.h"
886ecf6635Schristos #include "event2/event.h"
896ecf6635Schristos #include "event2/event_compat.h"
906ecf6635Schristos #include "event2/dns.h"
916ecf6635Schristos #include "event2/dns_compat.h"
926ecf6635Schristos #include "event2/thread.h"
936ecf6635Schristos 
946ecf6635Schristos #include "event2/event-config.h"
956ecf6635Schristos #include <sys/cdefs.h>
966ecf6635Schristos #include "regress.h"
97*7e68cdd7Schristos #include "regress_thread.h"
986ecf6635Schristos #include "tinytest.h"
996ecf6635Schristos #include "tinytest_macros.h"
1006ecf6635Schristos #include "../iocp-internal.h"
1016ecf6635Schristos #include "../event-internal.h"
102*7e68cdd7Schristos #include "../evthread-internal.h"
1036ecf6635Schristos 
1040d738af4Schristos struct evutil_weakrand_state test_weakrand_state;
1050d738af4Schristos 
1066ecf6635Schristos long
timeval_msec_diff(const struct timeval * start,const struct timeval * end)1076ecf6635Schristos timeval_msec_diff(const struct timeval *start, const struct timeval *end)
1086ecf6635Schristos {
1096ecf6635Schristos 	long ms = end->tv_sec - start->tv_sec;
1106ecf6635Schristos 	ms *= 1000;
1116ecf6635Schristos 	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
1126ecf6635Schristos 	return ms;
1136ecf6635Schristos }
1146ecf6635Schristos 
1156ecf6635Schristos /* ============================================================ */
1166ecf6635Schristos /* Code to wrap up old legacy test cases that used setup() and cleanup().
1176ecf6635Schristos  *
1186ecf6635Schristos  * Not all of the tests designated "legacy" are ones that used setup() and
1196ecf6635Schristos  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
1206ecf6635Schristos  * if it wants to find its event base/socketpair in global variables (ugh),
1216ecf6635Schristos  * OR if it wants to communicate success/failure through test_ok.
1226ecf6635Schristos  */
1236ecf6635Schristos 
1246ecf6635Schristos /* This is set to true if we're inside a legacy test wrapper.  It lets the
1256ecf6635Schristos    setup() and cleanup() functions in regress.c know they're not needed.
1266ecf6635Schristos  */
1276ecf6635Schristos int in_legacy_test_wrapper = 0;
1286ecf6635Schristos 
dnslogcb(int w,const char * m)1296ecf6635Schristos static void dnslogcb(int w, const char *m)
1306ecf6635Schristos {
1316ecf6635Schristos 	TT_BLATHER(("%s", m));
1326ecf6635Schristos }
1336ecf6635Schristos 
1340d738af4Schristos /* creates a temporary file with the data in it.  If *filename_out gets set,
1350d738af4Schristos  * the caller should try to unlink it. */
1366ecf6635Schristos int
regress_make_tmpfile(const void * data,size_t datalen,char ** filename_out)1370d738af4Schristos regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
1386ecf6635Schristos {
1390d738af4Schristos #ifndef _WIN32
1406ecf6635Schristos 	char tmpfilename[32];
1416ecf6635Schristos 	int fd;
1420d738af4Schristos 	*filename_out = NULL;
1436ecf6635Schristos 	strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
1440d738af4Schristos #ifdef EVENT__HAVE_UMASK
1456ecf6635Schristos 	umask(0077);
1466ecf6635Schristos #endif
1476ecf6635Schristos 	fd = mkstemp(tmpfilename);
1486ecf6635Schristos 	if (fd == -1)
1496ecf6635Schristos 		return (-1);
1506ecf6635Schristos 	if (write(fd, data, datalen) != (int)datalen) {
1516ecf6635Schristos 		close(fd);
1526ecf6635Schristos 		return (-1);
1536ecf6635Schristos 	}
1546ecf6635Schristos 	lseek(fd, 0, SEEK_SET);
1556ecf6635Schristos 	/* remove it from the file system */
1566ecf6635Schristos 	unlink(tmpfilename);
1576ecf6635Schristos 	return (fd);
1586ecf6635Schristos #else
1596ecf6635Schristos 	/* XXXX actually delete the file later */
1606ecf6635Schristos 	char tmpfilepath[MAX_PATH];
1616ecf6635Schristos 	char tmpfilename[MAX_PATH];
1626ecf6635Schristos 	DWORD r, written;
1636ecf6635Schristos 	int tries = 16;
1646ecf6635Schristos 	HANDLE h;
1656ecf6635Schristos 	r = GetTempPathA(MAX_PATH, tmpfilepath);
1666ecf6635Schristos 	if (r > MAX_PATH || r == 0)
1676ecf6635Schristos 		return (-1);
1686ecf6635Schristos 	for (; tries > 0; --tries) {
1696ecf6635Schristos 		r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
1706ecf6635Schristos 		if (r == 0)
1716ecf6635Schristos 			return (-1);
1726ecf6635Schristos 		h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
1736ecf6635Schristos 		    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1746ecf6635Schristos 		if (h != INVALID_HANDLE_VALUE)
1756ecf6635Schristos 			break;
1766ecf6635Schristos 	}
1776ecf6635Schristos 	if (tries == 0)
1786ecf6635Schristos 		return (-1);
1796ecf6635Schristos 	written = 0;
1800d738af4Schristos 	*filename_out = strdup(tmpfilename);
1816ecf6635Schristos 	WriteFile(h, data, (DWORD)datalen, &written, NULL);
1826ecf6635Schristos 	/* Closing the fd returned by this function will indeed close h. */
1836ecf6635Schristos 	return _open_osfhandle((intptr_t)h,_O_RDONLY);
1846ecf6635Schristos #endif
1856ecf6635Schristos }
1866ecf6635Schristos 
1876ecf6635Schristos #ifndef _WIN32
1886ecf6635Schristos pid_t
regress_fork(void)1896ecf6635Schristos regress_fork(void)
1906ecf6635Schristos {
1916ecf6635Schristos 	pid_t pid = fork();
1926ecf6635Schristos #ifdef FORK_BREAKS_GCOV
1936ecf6635Schristos 	vproc_transaction_begin(0);
1946ecf6635Schristos #endif
1956ecf6635Schristos 	return pid;
1966ecf6635Schristos }
1976ecf6635Schristos #endif
1986ecf6635Schristos 
1996ecf6635Schristos static void
ignore_log_cb(int s,const char * msg)2006ecf6635Schristos ignore_log_cb(int s, const char *msg)
2016ecf6635Schristos {
2026ecf6635Schristos }
2036ecf6635Schristos 
204*7e68cdd7Schristos /**
205*7e68cdd7Schristos  * Put into the real time scheduling class for better timers latency.
206*7e68cdd7Schristos  * https://developer.apple.com/library/archive/technotes/tn2169/_index.html#//apple_ref/doc/uid/DTS40013172-CH1-TNTAG6000
207*7e68cdd7Schristos  */
208*7e68cdd7Schristos #if defined(__APPLE__)
move_pthread_to_realtime_scheduling_class(pthread_t pthread)209*7e68cdd7Schristos static void move_pthread_to_realtime_scheduling_class(pthread_t pthread)
210*7e68cdd7Schristos {
211*7e68cdd7Schristos 	mach_timebase_info_data_t info;
212*7e68cdd7Schristos 	mach_timebase_info(&info);
213*7e68cdd7Schristos 
214*7e68cdd7Schristos 	const uint64_t NANOS_PER_MSEC = 1000000ULL;
215*7e68cdd7Schristos 	double clock2abs =
216*7e68cdd7Schristos 		((double)info.denom / (double)info.numer) * NANOS_PER_MSEC;
217*7e68cdd7Schristos 
218*7e68cdd7Schristos 	thread_time_constraint_policy_data_t policy;
219*7e68cdd7Schristos 	policy.period      = 0;
220*7e68cdd7Schristos 	policy.computation = (uint32_t)(5 * clock2abs); // 5 ms of work
221*7e68cdd7Schristos 	policy.constraint  = (uint32_t)(10 * clock2abs);
222*7e68cdd7Schristos 	policy.preemptible = FALSE;
223*7e68cdd7Schristos 
224*7e68cdd7Schristos 	int kr = thread_policy_set(pthread_mach_thread_np(pthread),
225*7e68cdd7Schristos 		THREAD_TIME_CONSTRAINT_POLICY,
226*7e68cdd7Schristos 		(thread_policy_t)&policy,
227*7e68cdd7Schristos 		THREAD_TIME_CONSTRAINT_POLICY_COUNT);
228*7e68cdd7Schristos 	if (kr != KERN_SUCCESS) {
229*7e68cdd7Schristos 		mach_error("thread_policy_set:", kr);
230*7e68cdd7Schristos 		exit(1);
231*7e68cdd7Schristos 	}
232*7e68cdd7Schristos }
233*7e68cdd7Schristos 
thread_setup(THREAD_T pthread)234*7e68cdd7Schristos void thread_setup(THREAD_T pthread)
235*7e68cdd7Schristos {
236*7e68cdd7Schristos 	move_pthread_to_realtime_scheduling_class(pthread);
237*7e68cdd7Schristos }
238*7e68cdd7Schristos #else /** \__APPLE__ */
thread_setup(THREAD_T pthread)239*7e68cdd7Schristos void thread_setup(THREAD_T pthread) {}
240*7e68cdd7Schristos #endif /** \!__APPLE__ */
241*7e68cdd7Schristos 
242*7e68cdd7Schristos 
243*7e68cdd7Schristos void *
basic_test_setup(const struct testcase_t * testcase)2446ecf6635Schristos basic_test_setup(const struct testcase_t *testcase)
2456ecf6635Schristos {
2466ecf6635Schristos 	struct event_base *base = NULL;
2476ecf6635Schristos 	evutil_socket_t spair[2] = { -1, -1 };
2486ecf6635Schristos 	struct basic_test_data *data = NULL;
2496ecf6635Schristos 
250*7e68cdd7Schristos 	thread_setup(THREAD_SELF());
251*7e68cdd7Schristos 
2520d738af4Schristos #ifndef _WIN32
2536ecf6635Schristos 	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
2546ecf6635Schristos 		return (void*)TT_SKIP;
2556ecf6635Schristos #endif
2566ecf6635Schristos 
257*7e68cdd7Schristos 	if (testcase->flags & TT_ENABLE_DEBUG_MODE &&
258*7e68cdd7Schristos 		!libevent_tests_running_in_debug_mode) {
259*7e68cdd7Schristos 		event_enable_debug_mode();
260*7e68cdd7Schristos 		libevent_tests_running_in_debug_mode = 1;
261*7e68cdd7Schristos 	}
262*7e68cdd7Schristos 
2636ecf6635Schristos 	if (testcase->flags & TT_NEED_THREADS) {
2646ecf6635Schristos 		if (!(testcase->flags & TT_FORK))
2656ecf6635Schristos 			return NULL;
2666ecf6635Schristos #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
2676ecf6635Schristos 		if (evthread_use_pthreads())
2686ecf6635Schristos 			exit(1);
2696ecf6635Schristos #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
2706ecf6635Schristos 		if (evthread_use_windows_threads())
2716ecf6635Schristos 			exit(1);
2726ecf6635Schristos #else
2736ecf6635Schristos 		return (void*)TT_SKIP;
2746ecf6635Schristos #endif
2756ecf6635Schristos 	}
2766ecf6635Schristos 
2776ecf6635Schristos 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
2786ecf6635Schristos 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
2796ecf6635Schristos 			fprintf(stderr, "%s: socketpair\n", __func__);
2806ecf6635Schristos 			exit(1);
2816ecf6635Schristos 		}
2826ecf6635Schristos 
2836ecf6635Schristos 		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
2846ecf6635Schristos 			fprintf(stderr, "fcntl(O_NONBLOCK)");
2856ecf6635Schristos 			exit(1);
2866ecf6635Schristos 		}
2876ecf6635Schristos 
2886ecf6635Schristos 		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
2896ecf6635Schristos 			fprintf(stderr, "fcntl(O_NONBLOCK)");
2906ecf6635Schristos 			exit(1);
2916ecf6635Schristos 		}
2926ecf6635Schristos 	}
2936ecf6635Schristos 	if (testcase->flags & TT_NEED_BASE) {
2946ecf6635Schristos 		if (testcase->flags & TT_LEGACY)
2956ecf6635Schristos 			base = event_init();
2966ecf6635Schristos 		else
2976ecf6635Schristos 			base = event_base_new();
2986ecf6635Schristos 		if (!base)
2996ecf6635Schristos 			exit(1);
3006ecf6635Schristos 	}
3016ecf6635Schristos 	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
3020d738af4Schristos 		if (event_base_start_iocp_(base, 0)<0) {
3036ecf6635Schristos 			event_base_free(base);
3046ecf6635Schristos 			return (void*)TT_SKIP;
3056ecf6635Schristos 		}
3066ecf6635Schristos 	}
3076ecf6635Schristos 
3086ecf6635Schristos 	if (testcase->flags & TT_NEED_DNS) {
3096ecf6635Schristos 		evdns_set_log_fn(dnslogcb);
3106ecf6635Schristos 		if (evdns_init())
3116ecf6635Schristos 			return NULL; /* fast failure */ /*XXX asserts. */
3126ecf6635Schristos 	}
3136ecf6635Schristos 
3146ecf6635Schristos 	if (testcase->flags & TT_NO_LOGS)
3156ecf6635Schristos 		event_set_log_callback(ignore_log_cb);
3166ecf6635Schristos 
3176ecf6635Schristos 	data = calloc(1, sizeof(*data));
3186ecf6635Schristos 	if (!data)
3196ecf6635Schristos 		exit(1);
3206ecf6635Schristos 	data->base = base;
3216ecf6635Schristos 	data->pair[0] = spair[0];
3226ecf6635Schristos 	data->pair[1] = spair[1];
3236ecf6635Schristos 	data->setup_data = testcase->setup_data;
3246ecf6635Schristos 	return data;
3256ecf6635Schristos }
3266ecf6635Schristos 
327*7e68cdd7Schristos int
basic_test_cleanup(const struct testcase_t * testcase,void * ptr)3286ecf6635Schristos basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
3296ecf6635Schristos {
3306ecf6635Schristos 	struct basic_test_data *data = ptr;
3316ecf6635Schristos 
3326ecf6635Schristos 	if (testcase->flags & TT_NO_LOGS)
3336ecf6635Schristos 		event_set_log_callback(NULL);
3346ecf6635Schristos 
3356ecf6635Schristos 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
3366ecf6635Schristos 		if (data->pair[0] != -1)
3376ecf6635Schristos 			evutil_closesocket(data->pair[0]);
3386ecf6635Schristos 		if (data->pair[1] != -1)
3396ecf6635Schristos 			evutil_closesocket(data->pair[1]);
3406ecf6635Schristos 	}
3416ecf6635Schristos 
3426ecf6635Schristos 	if (testcase->flags & TT_NEED_DNS) {
3436ecf6635Schristos 		evdns_shutdown(0);
3446ecf6635Schristos 	}
3456ecf6635Schristos 
3466ecf6635Schristos 	if (testcase->flags & TT_NEED_BASE) {
3476ecf6635Schristos 		if (data->base) {
3480d738af4Schristos 			event_base_assert_ok_(data->base);
3496ecf6635Schristos 			event_base_free(data->base);
3506ecf6635Schristos 		}
3516ecf6635Schristos 	}
3526ecf6635Schristos 
3530d738af4Schristos 	if (testcase->flags & TT_FORK)
3540d738af4Schristos 		libevent_global_shutdown();
3550d738af4Schristos 
3566ecf6635Schristos 	free(data);
3576ecf6635Schristos 
3586ecf6635Schristos 	return 1;
3596ecf6635Schristos }
3606ecf6635Schristos 
3616ecf6635Schristos const struct testcase_setup_t basic_setup = {
3626ecf6635Schristos 	basic_test_setup, basic_test_cleanup
3636ecf6635Schristos };
3646ecf6635Schristos 
3656ecf6635Schristos /* The "data" for a legacy test is just a pointer to the void fn(void)
3666ecf6635Schristos    function implementing the test case.  We need to set up some globals,
3676ecf6635Schristos    though, since that's where legacy tests expect to find a socketpair
3686ecf6635Schristos    (sometimes) and a global event_base (sometimes).
3696ecf6635Schristos  */
3706ecf6635Schristos static void *
legacy_test_setup(const struct testcase_t * testcase)3716ecf6635Schristos legacy_test_setup(const struct testcase_t *testcase)
3726ecf6635Schristos {
3736ecf6635Schristos 	struct basic_test_data *data = basic_test_setup(testcase);
3746ecf6635Schristos 	if (data == (void*)TT_SKIP || data == NULL)
3756ecf6635Schristos 		return data;
3766ecf6635Schristos 	global_base = data->base;
3776ecf6635Schristos 	pair[0] = data->pair[0];
3786ecf6635Schristos 	pair[1] = data->pair[1];
3796ecf6635Schristos 	data->legacy_test_fn = testcase->setup_data;
3806ecf6635Schristos 	return data;
3816ecf6635Schristos }
3826ecf6635Schristos 
3836ecf6635Schristos /* This function is the implementation of every legacy test case.  It
3846ecf6635Schristos    sets test_ok to 0, invokes the test function, and tells tinytest that
3856ecf6635Schristos    the test failed if the test didn't set test_ok to 1.
3866ecf6635Schristos  */
3876ecf6635Schristos void
run_legacy_test_fn(void * ptr)3886ecf6635Schristos run_legacy_test_fn(void *ptr)
3896ecf6635Schristos {
3906ecf6635Schristos 	struct basic_test_data *data = ptr;
3916ecf6635Schristos 	test_ok = called = 0;
3926ecf6635Schristos 
3936ecf6635Schristos 	in_legacy_test_wrapper = 1;
3946ecf6635Schristos 	data->legacy_test_fn(); /* This part actually calls the test */
3956ecf6635Schristos 	in_legacy_test_wrapper = 0;
3966ecf6635Schristos 
3976ecf6635Schristos 	if (!test_ok)
3986ecf6635Schristos 		tt_abort_msg("Legacy unit test failed");
3996ecf6635Schristos 
4006ecf6635Schristos end:
4016ecf6635Schristos 	test_ok = 0;
4026ecf6635Schristos }
4036ecf6635Schristos 
4046ecf6635Schristos /* This function doesn't have to clean up ptr (which is just a pointer
4056ecf6635Schristos    to the test function), but it may need to close the socketpair or
4066ecf6635Schristos    free the event_base.
4076ecf6635Schristos  */
4086ecf6635Schristos static int
legacy_test_cleanup(const struct testcase_t * testcase,void * ptr)4096ecf6635Schristos legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
4106ecf6635Schristos {
4116ecf6635Schristos 	int r = basic_test_cleanup(testcase, ptr);
4126ecf6635Schristos 	pair[0] = pair[1] = -1;
4136ecf6635Schristos 	global_base = NULL;
4146ecf6635Schristos 	return r;
4156ecf6635Schristos }
4166ecf6635Schristos 
4176ecf6635Schristos const struct testcase_setup_t legacy_setup = {
4186ecf6635Schristos 	legacy_test_setup, legacy_test_cleanup
4196ecf6635Schristos };
4206ecf6635Schristos 
4216ecf6635Schristos /* ============================================================ */
4226ecf6635Schristos 
4230d738af4Schristos #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
4246ecf6635Schristos struct testcase_t thread_testcases[] = {
4256ecf6635Schristos 	{ "basic", NULL, TT_SKIP, NULL, NULL },
4266ecf6635Schristos 	END_OF_TESTCASES
4276ecf6635Schristos };
4286ecf6635Schristos #endif
4296ecf6635Schristos 
4306ecf6635Schristos struct testgroup_t testgroups[] = {
4316ecf6635Schristos 	{ "main/", main_testcases },
4326ecf6635Schristos 	{ "heap/", minheap_testcases },
4336ecf6635Schristos 	{ "et/", edgetriggered_testcases },
4340d738af4Schristos 	{ "finalize/", finalize_testcases },
4356ecf6635Schristos 	{ "evbuffer/", evbuffer_testcases },
4366ecf6635Schristos 	{ "signal/", signal_testcases },
4376ecf6635Schristos 	{ "util/", util_testcases },
4386ecf6635Schristos 	{ "bufferevent/", bufferevent_testcases },
4396ecf6635Schristos 	{ "http/", http_testcases },
4406ecf6635Schristos 	{ "dns/", dns_testcases },
4416ecf6635Schristos 	{ "evtag/", evtag_testcases },
4426ecf6635Schristos 	{ "rpc/", rpc_testcases },
4436ecf6635Schristos 	{ "thread/", thread_testcases },
4446ecf6635Schristos 	{ "listener/", listener_testcases },
4450d738af4Schristos #ifdef _WIN32
4466ecf6635Schristos 	{ "iocp/", iocp_testcases },
4476ecf6635Schristos 	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
4486ecf6635Schristos 	{ "iocp/listener/", listener_iocp_testcases },
449*7e68cdd7Schristos 	{ "iocp/http/", http_iocp_testcases },
4506ecf6635Schristos #endif
4510d738af4Schristos #ifdef EVENT__HAVE_OPENSSL
4526ecf6635Schristos 	{ "ssl/", ssl_testcases },
4536ecf6635Schristos #endif
4546ecf6635Schristos 	END_OF_GROUPS
4556ecf6635Schristos };
4566ecf6635Schristos 
4570d738af4Schristos const char *alltests[] = { "+..", NULL };
4580d738af4Schristos const char *livenettests[] = {
4590d738af4Schristos 	"+util/getaddrinfo_live",
4600d738af4Schristos 	"+dns/gethostby..",
4610d738af4Schristos 	"+dns/resolve_reverse",
4620d738af4Schristos 	NULL
4630d738af4Schristos };
4640d738af4Schristos const char *finetimetests[] = {
4650d738af4Schristos 	"+util/monotonic_res_precise",
4660d738af4Schristos 	"+util/monotonic_res_fallback",
4670d738af4Schristos 	"+thread/deferred_cb_skew",
4680d738af4Schristos 	"+http/connection_retry",
4690d738af4Schristos 	"+http/https_connection_retry",
4700d738af4Schristos 	NULL
4710d738af4Schristos };
4720d738af4Schristos struct testlist_alias_t testaliases[] = {
4730d738af4Schristos 	{ "all", alltests },
4740d738af4Schristos 	{ "live_net", livenettests },
4750d738af4Schristos 	{ "fine_timing", finetimetests },
4760d738af4Schristos 	END_OF_ALIASES
4770d738af4Schristos };
4780d738af4Schristos 
4790d738af4Schristos int libevent_tests_running_in_debug_mode = 0;
4800d738af4Schristos 
4816ecf6635Schristos int
main(int argc,const char ** argv)4826ecf6635Schristos main(int argc, const char **argv)
4836ecf6635Schristos {
4840d738af4Schristos #ifdef _WIN32
4856ecf6635Schristos 	WORD wVersionRequested;
4866ecf6635Schristos 	WSADATA wsaData;
4876ecf6635Schristos 
4886ecf6635Schristos 	wVersionRequested = MAKEWORD(2, 2);
4896ecf6635Schristos 
4906ecf6635Schristos 	(void) WSAStartup(wVersionRequested, &wsaData);
4916ecf6635Schristos #endif
4926ecf6635Schristos 
4930d738af4Schristos #ifndef _WIN32
4946ecf6635Schristos 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
4956ecf6635Schristos 		return 1;
4966ecf6635Schristos #endif
4976ecf6635Schristos 
4980d738af4Schristos #ifdef _WIN32
4996ecf6635Schristos 	tinytest_skip(testgroups, "http/connection_retry");
5000d738af4Schristos 	tinytest_skip(testgroups, "http/https_connection_retry");
501*7e68cdd7Schristos 	tinytest_skip(testgroups, "http/read_on_write_error");
5026ecf6635Schristos #endif
5036ecf6635Schristos 
5040d738af4Schristos #ifndef EVENT__DISABLE_THREAD_SUPPORT
5056ecf6635Schristos 	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
5060d738af4Schristos 		evthread_enable_lock_debugging();
5076ecf6635Schristos #endif
5086ecf6635Schristos 
5090d738af4Schristos 	if (getenv("EVENT_DEBUG_MODE")) {
5100d738af4Schristos 		event_enable_debug_mode();
5110d738af4Schristos 		libevent_tests_running_in_debug_mode = 1;
5120d738af4Schristos 	}
5130d738af4Schristos 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
5140d738af4Schristos 		event_enable_debug_logging(EVENT_DBG_ALL);
5150d738af4Schristos 	}
5160d738af4Schristos 
5170d738af4Schristos 	tinytest_set_aliases(testaliases);
5180d738af4Schristos 
5190d738af4Schristos 	evutil_weakrand_seed_(&test_weakrand_state, 0);
5200d738af4Schristos 
521*7e68cdd7Schristos 	if (getenv("EVENT_NO_FILE_BUFFERING")) {
522*7e68cdd7Schristos 		setbuf(stdout, NULL);
523*7e68cdd7Schristos 		setbuf(stderr, NULL);
524*7e68cdd7Schristos 	}
525*7e68cdd7Schristos 
5266ecf6635Schristos 	if (tinytest_main(argc,argv,testgroups))
5276ecf6635Schristos 		return 1;
5286ecf6635Schristos 
5290d738af4Schristos 	libevent_global_shutdown();
5300d738af4Schristos 
5316ecf6635Schristos 	return 0;
5326ecf6635Schristos }
5336ecf6635Schristos 
534