xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_main.c (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert /*
2*2b15cb3dSCy Schubert  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3*2b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4*2b15cb3dSCy Schubert  *
5*2b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
6*2b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
7*2b15cb3dSCy Schubert  * are met:
8*2b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
9*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
10*2b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
11*2b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
12*2b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
13*2b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
14*2b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
15*2b15cb3dSCy Schubert  *
16*2b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*2b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*2b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*2b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*2b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*2b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*2b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*2b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*2b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*2b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*2b15cb3dSCy Schubert  */
27*2b15cb3dSCy Schubert #include "util-internal.h"
28*2b15cb3dSCy Schubert 
29*2b15cb3dSCy Schubert #ifdef _WIN32
30*2b15cb3dSCy Schubert #include <winsock2.h>
31*2b15cb3dSCy Schubert #include <windows.h>
32*2b15cb3dSCy Schubert #include <io.h>
33*2b15cb3dSCy Schubert #include <fcntl.h>
34*2b15cb3dSCy Schubert #endif
35*2b15cb3dSCy Schubert 
36*2b15cb3dSCy Schubert #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
37*2b15cb3dSCy Schubert #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
38*2b15cb3dSCy Schubert     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
39*2b15cb3dSCy Schubert #define FORK_BREAKS_GCOV
40*2b15cb3dSCy Schubert #include <vproc.h>
41*2b15cb3dSCy Schubert #endif
42*2b15cb3dSCy Schubert #endif
43*2b15cb3dSCy Schubert 
44*2b15cb3dSCy Schubert #include "event2/event-config.h"
45*2b15cb3dSCy Schubert 
46*2b15cb3dSCy Schubert #ifdef EVENT____func__
47*2b15cb3dSCy Schubert #define __func__ EVENT____func__
48*2b15cb3dSCy Schubert #endif
49*2b15cb3dSCy Schubert 
50*2b15cb3dSCy Schubert #if 0
51*2b15cb3dSCy Schubert #include <sys/types.h>
52*2b15cb3dSCy Schubert #include <sys/stat.h>
53*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
54*2b15cb3dSCy Schubert #include <sys/time.h>
55*2b15cb3dSCy Schubert #endif
56*2b15cb3dSCy Schubert #include <sys/queue.h>
57*2b15cb3dSCy Schubert #include <signal.h>
58*2b15cb3dSCy Schubert #include <errno.h>
59*2b15cb3dSCy Schubert #endif
60*2b15cb3dSCy Schubert 
61*2b15cb3dSCy Schubert #include <sys/types.h>
62*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_STAT_H
63*2b15cb3dSCy Schubert #include <sys/stat.h>
64*2b15cb3dSCy Schubert #endif
65*2b15cb3dSCy Schubert 
66*2b15cb3dSCy Schubert #ifndef _WIN32
67*2b15cb3dSCy Schubert #include <sys/socket.h>
68*2b15cb3dSCy Schubert #include <sys/wait.h>
69*2b15cb3dSCy Schubert #include <signal.h>
70*2b15cb3dSCy Schubert #include <unistd.h>
71*2b15cb3dSCy Schubert #include <netdb.h>
72*2b15cb3dSCy Schubert #endif
73*2b15cb3dSCy Schubert 
74*2b15cb3dSCy Schubert #include <stdlib.h>
75*2b15cb3dSCy Schubert #include <stdio.h>
76*2b15cb3dSCy Schubert #include <string.h>
77*2b15cb3dSCy Schubert #include <assert.h>
78*2b15cb3dSCy Schubert 
79*2b15cb3dSCy Schubert #include "event2/util.h"
80*2b15cb3dSCy Schubert #include "event2/event.h"
81*2b15cb3dSCy Schubert #include "event2/event_compat.h"
82*2b15cb3dSCy Schubert #include "event2/dns.h"
83*2b15cb3dSCy Schubert #include "event2/dns_compat.h"
84*2b15cb3dSCy Schubert #include "event2/thread.h"
85*2b15cb3dSCy Schubert 
86*2b15cb3dSCy Schubert #include "event2/event-config.h"
87*2b15cb3dSCy Schubert #include "regress.h"
88*2b15cb3dSCy Schubert #include "tinytest.h"
89*2b15cb3dSCy Schubert #include "tinytest_macros.h"
90*2b15cb3dSCy Schubert #include "../iocp-internal.h"
91*2b15cb3dSCy Schubert #include "../event-internal.h"
92*2b15cb3dSCy Schubert 
93*2b15cb3dSCy Schubert long
94*2b15cb3dSCy Schubert timeval_msec_diff(const struct timeval *start, const struct timeval *end)
95*2b15cb3dSCy Schubert {
96*2b15cb3dSCy Schubert 	long ms = end->tv_sec - start->tv_sec;
97*2b15cb3dSCy Schubert 	ms *= 1000;
98*2b15cb3dSCy Schubert 	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
99*2b15cb3dSCy Schubert 	return ms;
100*2b15cb3dSCy Schubert }
101*2b15cb3dSCy Schubert 
102*2b15cb3dSCy Schubert /* ============================================================ */
103*2b15cb3dSCy Schubert /* Code to wrap up old legacy test cases that used setup() and cleanup().
104*2b15cb3dSCy Schubert  *
105*2b15cb3dSCy Schubert  * Not all of the tests designated "legacy" are ones that used setup() and
106*2b15cb3dSCy Schubert  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
107*2b15cb3dSCy Schubert  * if it wants to find its event base/socketpair in global variables (ugh),
108*2b15cb3dSCy Schubert  * OR if it wants to communicate success/failure through test_ok.
109*2b15cb3dSCy Schubert  */
110*2b15cb3dSCy Schubert 
111*2b15cb3dSCy Schubert /* This is set to true if we're inside a legacy test wrapper.  It lets the
112*2b15cb3dSCy Schubert    setup() and cleanup() functions in regress.c know they're not needed.
113*2b15cb3dSCy Schubert  */
114*2b15cb3dSCy Schubert int in_legacy_test_wrapper = 0;
115*2b15cb3dSCy Schubert 
116*2b15cb3dSCy Schubert static void dnslogcb(int w, const char *m)
117*2b15cb3dSCy Schubert {
118*2b15cb3dSCy Schubert 	TT_BLATHER(("%s", m));
119*2b15cb3dSCy Schubert }
120*2b15cb3dSCy Schubert 
121*2b15cb3dSCy Schubert /* creates a temporary file with the data in it.  If *filename_out gets set,
122*2b15cb3dSCy Schubert  * the caller should try to unlink it. */
123*2b15cb3dSCy Schubert int
124*2b15cb3dSCy Schubert regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
125*2b15cb3dSCy Schubert {
126*2b15cb3dSCy Schubert #ifndef _WIN32
127*2b15cb3dSCy Schubert 	char tmpfilename[32];
128*2b15cb3dSCy Schubert 	int fd;
129*2b15cb3dSCy Schubert 	*filename_out = NULL;
130*2b15cb3dSCy Schubert 	strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
131*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_UMASK
132*2b15cb3dSCy Schubert 	umask(0077);
133*2b15cb3dSCy Schubert #endif
134*2b15cb3dSCy Schubert 	fd = mkstemp(tmpfilename);
135*2b15cb3dSCy Schubert 	if (fd == -1)
136*2b15cb3dSCy Schubert 		return (-1);
137*2b15cb3dSCy Schubert 	if (write(fd, data, datalen) != (int)datalen) {
138*2b15cb3dSCy Schubert 		close(fd);
139*2b15cb3dSCy Schubert 		return (-1);
140*2b15cb3dSCy Schubert 	}
141*2b15cb3dSCy Schubert 	lseek(fd, 0, SEEK_SET);
142*2b15cb3dSCy Schubert 	/* remove it from the file system */
143*2b15cb3dSCy Schubert 	unlink(tmpfilename);
144*2b15cb3dSCy Schubert 	return (fd);
145*2b15cb3dSCy Schubert #else
146*2b15cb3dSCy Schubert 	/* XXXX actually delete the file later */
147*2b15cb3dSCy Schubert 	char tmpfilepath[MAX_PATH];
148*2b15cb3dSCy Schubert 	char tmpfilename[MAX_PATH];
149*2b15cb3dSCy Schubert 	DWORD r, written;
150*2b15cb3dSCy Schubert 	int tries = 16;
151*2b15cb3dSCy Schubert 	HANDLE h;
152*2b15cb3dSCy Schubert 	r = GetTempPathA(MAX_PATH, tmpfilepath);
153*2b15cb3dSCy Schubert 	if (r > MAX_PATH || r == 0)
154*2b15cb3dSCy Schubert 		return (-1);
155*2b15cb3dSCy Schubert 	for (; tries > 0; --tries) {
156*2b15cb3dSCy Schubert 		r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
157*2b15cb3dSCy Schubert 		if (r == 0)
158*2b15cb3dSCy Schubert 			return (-1);
159*2b15cb3dSCy Schubert 		h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
160*2b15cb3dSCy Schubert 		    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
161*2b15cb3dSCy Schubert 		if (h != INVALID_HANDLE_VALUE)
162*2b15cb3dSCy Schubert 			break;
163*2b15cb3dSCy Schubert 	}
164*2b15cb3dSCy Schubert 	if (tries == 0)
165*2b15cb3dSCy Schubert 		return (-1);
166*2b15cb3dSCy Schubert 	written = 0;
167*2b15cb3dSCy Schubert 	*filename_out = strdup(tmpfilename);
168*2b15cb3dSCy Schubert 	WriteFile(h, data, (DWORD)datalen, &written, NULL);
169*2b15cb3dSCy Schubert 	/* Closing the fd returned by this function will indeed close h. */
170*2b15cb3dSCy Schubert 	return _open_osfhandle((intptr_t)h,_O_RDONLY);
171*2b15cb3dSCy Schubert #endif
172*2b15cb3dSCy Schubert }
173*2b15cb3dSCy Schubert 
174*2b15cb3dSCy Schubert #ifndef _WIN32
175*2b15cb3dSCy Schubert pid_t
176*2b15cb3dSCy Schubert regress_fork(void)
177*2b15cb3dSCy Schubert {
178*2b15cb3dSCy Schubert 	pid_t pid = fork();
179*2b15cb3dSCy Schubert #ifdef FORK_BREAKS_GCOV
180*2b15cb3dSCy Schubert 	vproc_transaction_begin(0);
181*2b15cb3dSCy Schubert #endif
182*2b15cb3dSCy Schubert 	return pid;
183*2b15cb3dSCy Schubert }
184*2b15cb3dSCy Schubert #endif
185*2b15cb3dSCy Schubert 
186*2b15cb3dSCy Schubert static void
187*2b15cb3dSCy Schubert ignore_log_cb(int s, const char *msg)
188*2b15cb3dSCy Schubert {
189*2b15cb3dSCy Schubert }
190*2b15cb3dSCy Schubert 
191*2b15cb3dSCy Schubert static void *
192*2b15cb3dSCy Schubert basic_test_setup(const struct testcase_t *testcase)
193*2b15cb3dSCy Schubert {
194*2b15cb3dSCy Schubert 	struct event_base *base = NULL;
195*2b15cb3dSCy Schubert 	evutil_socket_t spair[2] = { -1, -1 };
196*2b15cb3dSCy Schubert 	struct basic_test_data *data = NULL;
197*2b15cb3dSCy Schubert 
198*2b15cb3dSCy Schubert #ifndef _WIN32
199*2b15cb3dSCy Schubert 	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
200*2b15cb3dSCy Schubert 		return (void*)TT_SKIP;
201*2b15cb3dSCy Schubert #endif
202*2b15cb3dSCy Schubert 
203*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_THREADS) {
204*2b15cb3dSCy Schubert 		if (!(testcase->flags & TT_FORK))
205*2b15cb3dSCy Schubert 			return NULL;
206*2b15cb3dSCy Schubert #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
207*2b15cb3dSCy Schubert 		if (evthread_use_pthreads())
208*2b15cb3dSCy Schubert 			exit(1);
209*2b15cb3dSCy Schubert #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
210*2b15cb3dSCy Schubert 		if (evthread_use_windows_threads())
211*2b15cb3dSCy Schubert 			exit(1);
212*2b15cb3dSCy Schubert #else
213*2b15cb3dSCy Schubert 		return (void*)TT_SKIP;
214*2b15cb3dSCy Schubert #endif
215*2b15cb3dSCy Schubert 	}
216*2b15cb3dSCy Schubert 
217*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
218*2b15cb3dSCy Schubert 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
219*2b15cb3dSCy Schubert 			fprintf(stderr, "%s: socketpair\n", __func__);
220*2b15cb3dSCy Schubert 			exit(1);
221*2b15cb3dSCy Schubert 		}
222*2b15cb3dSCy Schubert 
223*2b15cb3dSCy Schubert 		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
224*2b15cb3dSCy Schubert 			fprintf(stderr, "fcntl(O_NONBLOCK)");
225*2b15cb3dSCy Schubert 			exit(1);
226*2b15cb3dSCy Schubert 		}
227*2b15cb3dSCy Schubert 
228*2b15cb3dSCy Schubert 		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
229*2b15cb3dSCy Schubert 			fprintf(stderr, "fcntl(O_NONBLOCK)");
230*2b15cb3dSCy Schubert 			exit(1);
231*2b15cb3dSCy Schubert 		}
232*2b15cb3dSCy Schubert 	}
233*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_BASE) {
234*2b15cb3dSCy Schubert 		if (testcase->flags & TT_LEGACY)
235*2b15cb3dSCy Schubert 			base = event_init();
236*2b15cb3dSCy Schubert 		else
237*2b15cb3dSCy Schubert 			base = event_base_new();
238*2b15cb3dSCy Schubert 		if (!base)
239*2b15cb3dSCy Schubert 			exit(1);
240*2b15cb3dSCy Schubert 	}
241*2b15cb3dSCy Schubert 	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
242*2b15cb3dSCy Schubert 		if (event_base_start_iocp_(base, 0)<0) {
243*2b15cb3dSCy Schubert 			event_base_free(base);
244*2b15cb3dSCy Schubert 			return (void*)TT_SKIP;
245*2b15cb3dSCy Schubert 		}
246*2b15cb3dSCy Schubert 	}
247*2b15cb3dSCy Schubert 
248*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_DNS) {
249*2b15cb3dSCy Schubert 		evdns_set_log_fn(dnslogcb);
250*2b15cb3dSCy Schubert 		if (evdns_init())
251*2b15cb3dSCy Schubert 			return NULL; /* fast failure */ /*XXX asserts. */
252*2b15cb3dSCy Schubert 	}
253*2b15cb3dSCy Schubert 
254*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NO_LOGS)
255*2b15cb3dSCy Schubert 		event_set_log_callback(ignore_log_cb);
256*2b15cb3dSCy Schubert 
257*2b15cb3dSCy Schubert 	data = calloc(1, sizeof(*data));
258*2b15cb3dSCy Schubert 	if (!data)
259*2b15cb3dSCy Schubert 		exit(1);
260*2b15cb3dSCy Schubert 	data->base = base;
261*2b15cb3dSCy Schubert 	data->pair[0] = spair[0];
262*2b15cb3dSCy Schubert 	data->pair[1] = spair[1];
263*2b15cb3dSCy Schubert 	data->setup_data = testcase->setup_data;
264*2b15cb3dSCy Schubert 	return data;
265*2b15cb3dSCy Schubert }
266*2b15cb3dSCy Schubert 
267*2b15cb3dSCy Schubert static int
268*2b15cb3dSCy Schubert basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
269*2b15cb3dSCy Schubert {
270*2b15cb3dSCy Schubert 	struct basic_test_data *data = ptr;
271*2b15cb3dSCy Schubert 
272*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NO_LOGS)
273*2b15cb3dSCy Schubert 		event_set_log_callback(NULL);
274*2b15cb3dSCy Schubert 
275*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
276*2b15cb3dSCy Schubert 		if (data->pair[0] != -1)
277*2b15cb3dSCy Schubert 			evutil_closesocket(data->pair[0]);
278*2b15cb3dSCy Schubert 		if (data->pair[1] != -1)
279*2b15cb3dSCy Schubert 			evutil_closesocket(data->pair[1]);
280*2b15cb3dSCy Schubert 	}
281*2b15cb3dSCy Schubert 
282*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_DNS) {
283*2b15cb3dSCy Schubert 		evdns_shutdown(0);
284*2b15cb3dSCy Schubert 	}
285*2b15cb3dSCy Schubert 
286*2b15cb3dSCy Schubert 	if (testcase->flags & TT_NEED_BASE) {
287*2b15cb3dSCy Schubert 		if (data->base) {
288*2b15cb3dSCy Schubert 			event_base_assert_ok_(data->base);
289*2b15cb3dSCy Schubert 			event_base_free(data->base);
290*2b15cb3dSCy Schubert 		}
291*2b15cb3dSCy Schubert 	}
292*2b15cb3dSCy Schubert 
293*2b15cb3dSCy Schubert 	if (testcase->flags & TT_FORK)
294*2b15cb3dSCy Schubert 		libevent_global_shutdown();
295*2b15cb3dSCy Schubert 
296*2b15cb3dSCy Schubert 	free(data);
297*2b15cb3dSCy Schubert 
298*2b15cb3dSCy Schubert 	return 1;
299*2b15cb3dSCy Schubert }
300*2b15cb3dSCy Schubert 
301*2b15cb3dSCy Schubert const struct testcase_setup_t basic_setup = {
302*2b15cb3dSCy Schubert 	basic_test_setup, basic_test_cleanup
303*2b15cb3dSCy Schubert };
304*2b15cb3dSCy Schubert 
305*2b15cb3dSCy Schubert /* The "data" for a legacy test is just a pointer to the void fn(void)
306*2b15cb3dSCy Schubert    function implementing the test case.  We need to set up some globals,
307*2b15cb3dSCy Schubert    though, since that's where legacy tests expect to find a socketpair
308*2b15cb3dSCy Schubert    (sometimes) and a global event_base (sometimes).
309*2b15cb3dSCy Schubert  */
310*2b15cb3dSCy Schubert static void *
311*2b15cb3dSCy Schubert legacy_test_setup(const struct testcase_t *testcase)
312*2b15cb3dSCy Schubert {
313*2b15cb3dSCy Schubert 	struct basic_test_data *data = basic_test_setup(testcase);
314*2b15cb3dSCy Schubert 	if (data == (void*)TT_SKIP || data == NULL)
315*2b15cb3dSCy Schubert 		return data;
316*2b15cb3dSCy Schubert 	global_base = data->base;
317*2b15cb3dSCy Schubert 	pair[0] = data->pair[0];
318*2b15cb3dSCy Schubert 	pair[1] = data->pair[1];
319*2b15cb3dSCy Schubert 	data->legacy_test_fn = testcase->setup_data;
320*2b15cb3dSCy Schubert 	return data;
321*2b15cb3dSCy Schubert }
322*2b15cb3dSCy Schubert 
323*2b15cb3dSCy Schubert /* This function is the implementation of every legacy test case.  It
324*2b15cb3dSCy Schubert    sets test_ok to 0, invokes the test function, and tells tinytest that
325*2b15cb3dSCy Schubert    the test failed if the test didn't set test_ok to 1.
326*2b15cb3dSCy Schubert  */
327*2b15cb3dSCy Schubert void
328*2b15cb3dSCy Schubert run_legacy_test_fn(void *ptr)
329*2b15cb3dSCy Schubert {
330*2b15cb3dSCy Schubert 	struct basic_test_data *data = ptr;
331*2b15cb3dSCy Schubert 	test_ok = called = 0;
332*2b15cb3dSCy Schubert 
333*2b15cb3dSCy Schubert 	in_legacy_test_wrapper = 1;
334*2b15cb3dSCy Schubert 	data->legacy_test_fn(); /* This part actually calls the test */
335*2b15cb3dSCy Schubert 	in_legacy_test_wrapper = 0;
336*2b15cb3dSCy Schubert 
337*2b15cb3dSCy Schubert 	if (!test_ok)
338*2b15cb3dSCy Schubert 		tt_abort_msg("Legacy unit test failed");
339*2b15cb3dSCy Schubert 
340*2b15cb3dSCy Schubert end:
341*2b15cb3dSCy Schubert 	test_ok = 0;
342*2b15cb3dSCy Schubert }
343*2b15cb3dSCy Schubert 
344*2b15cb3dSCy Schubert /* This function doesn't have to clean up ptr (which is just a pointer
345*2b15cb3dSCy Schubert    to the test function), but it may need to close the socketpair or
346*2b15cb3dSCy Schubert    free the event_base.
347*2b15cb3dSCy Schubert  */
348*2b15cb3dSCy Schubert static int
349*2b15cb3dSCy Schubert legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
350*2b15cb3dSCy Schubert {
351*2b15cb3dSCy Schubert 	int r = basic_test_cleanup(testcase, ptr);
352*2b15cb3dSCy Schubert 	pair[0] = pair[1] = -1;
353*2b15cb3dSCy Schubert 	global_base = NULL;
354*2b15cb3dSCy Schubert 	return r;
355*2b15cb3dSCy Schubert }
356*2b15cb3dSCy Schubert 
357*2b15cb3dSCy Schubert const struct testcase_setup_t legacy_setup = {
358*2b15cb3dSCy Schubert 	legacy_test_setup, legacy_test_cleanup
359*2b15cb3dSCy Schubert };
360*2b15cb3dSCy Schubert 
361*2b15cb3dSCy Schubert /* ============================================================ */
362*2b15cb3dSCy Schubert 
363*2b15cb3dSCy Schubert #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
364*2b15cb3dSCy Schubert struct testcase_t thread_testcases[] = {
365*2b15cb3dSCy Schubert 	{ "basic", NULL, TT_SKIP, NULL, NULL },
366*2b15cb3dSCy Schubert 	END_OF_TESTCASES
367*2b15cb3dSCy Schubert };
368*2b15cb3dSCy Schubert #endif
369*2b15cb3dSCy Schubert 
370*2b15cb3dSCy Schubert struct testgroup_t testgroups[] = {
371*2b15cb3dSCy Schubert 	{ "main/", main_testcases },
372*2b15cb3dSCy Schubert 	{ "heap/", minheap_testcases },
373*2b15cb3dSCy Schubert 	{ "et/", edgetriggered_testcases },
374*2b15cb3dSCy Schubert 	{ "finalize/", finalize_testcases },
375*2b15cb3dSCy Schubert 	{ "evbuffer/", evbuffer_testcases },
376*2b15cb3dSCy Schubert 	{ "signal/", signal_testcases },
377*2b15cb3dSCy Schubert 	{ "util/", util_testcases },
378*2b15cb3dSCy Schubert 	{ "bufferevent/", bufferevent_testcases },
379*2b15cb3dSCy Schubert 	{ "http/", http_testcases },
380*2b15cb3dSCy Schubert 	{ "dns/", dns_testcases },
381*2b15cb3dSCy Schubert 	{ "evtag/", evtag_testcases },
382*2b15cb3dSCy Schubert 	{ "rpc/", rpc_testcases },
383*2b15cb3dSCy Schubert 	{ "thread/", thread_testcases },
384*2b15cb3dSCy Schubert 	{ "listener/", listener_testcases },
385*2b15cb3dSCy Schubert #ifdef _WIN32
386*2b15cb3dSCy Schubert 	{ "iocp/", iocp_testcases },
387*2b15cb3dSCy Schubert 	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
388*2b15cb3dSCy Schubert 	{ "iocp/listener/", listener_iocp_testcases },
389*2b15cb3dSCy Schubert #endif
390*2b15cb3dSCy Schubert #ifdef EVENT__HAVE_OPENSSL
391*2b15cb3dSCy Schubert 	{ "ssl/", ssl_testcases },
392*2b15cb3dSCy Schubert #endif
393*2b15cb3dSCy Schubert 	END_OF_GROUPS
394*2b15cb3dSCy Schubert };
395*2b15cb3dSCy Schubert 
396*2b15cb3dSCy Schubert const char *alltests[] = { "+..", NULL };
397*2b15cb3dSCy Schubert const char *livenettests[] = {
398*2b15cb3dSCy Schubert 	"+util/getaddrinfo_live",
399*2b15cb3dSCy Schubert 	"+dns/gethostby..",
400*2b15cb3dSCy Schubert 	"+dns/resolve_reverse",
401*2b15cb3dSCy Schubert 	NULL
402*2b15cb3dSCy Schubert };
403*2b15cb3dSCy Schubert const char *finetimetests[] = {
404*2b15cb3dSCy Schubert 	"+util/monotonic_res_precise",
405*2b15cb3dSCy Schubert 	"+util/monotonic_res_fallback",
406*2b15cb3dSCy Schubert 	"+thread/deferred_cb_skew",
407*2b15cb3dSCy Schubert 	"+http/connection_retry",
408*2b15cb3dSCy Schubert 	NULL
409*2b15cb3dSCy Schubert };
410*2b15cb3dSCy Schubert struct testlist_alias_t testaliases[] = {
411*2b15cb3dSCy Schubert 	{ "all", alltests },
412*2b15cb3dSCy Schubert 	{ "live_net", livenettests },
413*2b15cb3dSCy Schubert 	{ "fine_timing", finetimetests },
414*2b15cb3dSCy Schubert 	END_OF_ALIASES
415*2b15cb3dSCy Schubert };
416*2b15cb3dSCy Schubert 
417*2b15cb3dSCy Schubert int libevent_tests_running_in_debug_mode = 0;
418*2b15cb3dSCy Schubert 
419*2b15cb3dSCy Schubert int
420*2b15cb3dSCy Schubert main(int argc, const char **argv)
421*2b15cb3dSCy Schubert {
422*2b15cb3dSCy Schubert #ifdef _WIN32
423*2b15cb3dSCy Schubert 	WORD wVersionRequested;
424*2b15cb3dSCy Schubert 	WSADATA wsaData;
425*2b15cb3dSCy Schubert 
426*2b15cb3dSCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
427*2b15cb3dSCy Schubert 
428*2b15cb3dSCy Schubert 	(void) WSAStartup(wVersionRequested, &wsaData);
429*2b15cb3dSCy Schubert #endif
430*2b15cb3dSCy Schubert 
431*2b15cb3dSCy Schubert #ifndef _WIN32
432*2b15cb3dSCy Schubert 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
433*2b15cb3dSCy Schubert 		return 1;
434*2b15cb3dSCy Schubert #endif
435*2b15cb3dSCy Schubert 
436*2b15cb3dSCy Schubert #ifdef _WIN32
437*2b15cb3dSCy Schubert 	tinytest_skip(testgroups, "http/connection_retry");
438*2b15cb3dSCy Schubert #endif
439*2b15cb3dSCy Schubert 
440*2b15cb3dSCy Schubert #ifndef EVENT__DISABLE_THREAD_SUPPORT
441*2b15cb3dSCy Schubert 	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
442*2b15cb3dSCy Schubert 		evthread_enable_lock_debugging();
443*2b15cb3dSCy Schubert #endif
444*2b15cb3dSCy Schubert 
445*2b15cb3dSCy Schubert 	if (getenv("EVENT_DEBUG_MODE")) {
446*2b15cb3dSCy Schubert 		event_enable_debug_mode();
447*2b15cb3dSCy Schubert 		libevent_tests_running_in_debug_mode = 1;
448*2b15cb3dSCy Schubert 	}
449*2b15cb3dSCy Schubert 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
450*2b15cb3dSCy Schubert 		event_enable_debug_logging(EVENT_DBG_ALL);
451*2b15cb3dSCy Schubert 	}
452*2b15cb3dSCy Schubert 
453*2b15cb3dSCy Schubert 	tinytest_set_aliases(testaliases);
454*2b15cb3dSCy Schubert 
455*2b15cb3dSCy Schubert 	if (tinytest_main(argc,argv,testgroups))
456*2b15cb3dSCy Schubert 		return 1;
457*2b15cb3dSCy Schubert 
458*2b15cb3dSCy Schubert 	libevent_global_shutdown();
459*2b15cb3dSCy Schubert 
460*2b15cb3dSCy Schubert 	return 0;
461*2b15cb3dSCy Schubert }
462*2b15cb3dSCy Schubert 
463