1275970Scy/*
2275970Scy * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. The name of the author may not be used to endorse or promote products
14275970Scy *    derived from this software without specific prior written permission.
15275970Scy *
16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26275970Scy */
27275970Scy#include "util-internal.h"
28275970Scy
29275970Scy#ifdef _WIN32
30275970Scy#include <winsock2.h>
31275970Scy#include <windows.h>
32275970Scy#include <io.h>
33275970Scy#include <fcntl.h>
34275970Scy#endif
35275970Scy
36275970Scy#if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
37275970Scy#if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
38275970Scy    __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
39275970Scy#define FORK_BREAKS_GCOV
40275970Scy#include <vproc.h>
41275970Scy#endif
42275970Scy#endif
43275970Scy
44275970Scy#include "event2/event-config.h"
45275970Scy
46275970Scy#ifdef EVENT____func__
47275970Scy#define __func__ EVENT____func__
48275970Scy#endif
49275970Scy
50275970Scy#if 0
51275970Scy#include <sys/types.h>
52275970Scy#include <sys/stat.h>
53275970Scy#ifdef EVENT__HAVE_SYS_TIME_H
54275970Scy#include <sys/time.h>
55275970Scy#endif
56275970Scy#include <sys/queue.h>
57275970Scy#include <signal.h>
58275970Scy#include <errno.h>
59275970Scy#endif
60275970Scy
61275970Scy#include <sys/types.h>
62275970Scy#ifdef EVENT__HAVE_SYS_STAT_H
63275970Scy#include <sys/stat.h>
64275970Scy#endif
65275970Scy
66275970Scy#ifndef _WIN32
67275970Scy#include <sys/socket.h>
68275970Scy#include <sys/wait.h>
69275970Scy#include <signal.h>
70275970Scy#include <unistd.h>
71275970Scy#include <netdb.h>
72275970Scy#endif
73275970Scy
74275970Scy#include <stdlib.h>
75275970Scy#include <stdio.h>
76275970Scy#include <string.h>
77275970Scy#include <assert.h>
78275970Scy
79275970Scy#include "event2/util.h"
80275970Scy#include "event2/event.h"
81275970Scy#include "event2/event_compat.h"
82275970Scy#include "event2/dns.h"
83275970Scy#include "event2/dns_compat.h"
84275970Scy#include "event2/thread.h"
85275970Scy
86275970Scy#include "event2/event-config.h"
87275970Scy#include "regress.h"
88275970Scy#include "tinytest.h"
89275970Scy#include "tinytest_macros.h"
90275970Scy#include "../iocp-internal.h"
91275970Scy#include "../event-internal.h"
92275970Scy
93282408Scystruct evutil_weakrand_state test_weakrand_state;
94282408Scy
95275970Scylong
96275970Scytimeval_msec_diff(const struct timeval *start, const struct timeval *end)
97275970Scy{
98275970Scy	long ms = end->tv_sec - start->tv_sec;
99275970Scy	ms *= 1000;
100275970Scy	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
101275970Scy	return ms;
102275970Scy}
103275970Scy
104275970Scy/* ============================================================ */
105275970Scy/* Code to wrap up old legacy test cases that used setup() and cleanup().
106275970Scy *
107275970Scy * Not all of the tests designated "legacy" are ones that used setup() and
108275970Scy * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
109275970Scy * if it wants to find its event base/socketpair in global variables (ugh),
110275970Scy * OR if it wants to communicate success/failure through test_ok.
111275970Scy */
112275970Scy
113275970Scy/* This is set to true if we're inside a legacy test wrapper.  It lets the
114275970Scy   setup() and cleanup() functions in regress.c know they're not needed.
115275970Scy */
116275970Scyint in_legacy_test_wrapper = 0;
117275970Scy
118275970Scystatic void dnslogcb(int w, const char *m)
119275970Scy{
120275970Scy	TT_BLATHER(("%s", m));
121275970Scy}
122275970Scy
123275970Scy/* creates a temporary file with the data in it.  If *filename_out gets set,
124275970Scy * the caller should try to unlink it. */
125275970Scyint
126275970Scyregress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
127275970Scy{
128275970Scy#ifndef _WIN32
129275970Scy	char tmpfilename[32];
130275970Scy	int fd;
131275970Scy	*filename_out = NULL;
132275970Scy	strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
133275970Scy#ifdef EVENT__HAVE_UMASK
134275970Scy	umask(0077);
135275970Scy#endif
136275970Scy	fd = mkstemp(tmpfilename);
137275970Scy	if (fd == -1)
138275970Scy		return (-1);
139275970Scy	if (write(fd, data, datalen) != (int)datalen) {
140275970Scy		close(fd);
141275970Scy		return (-1);
142275970Scy	}
143275970Scy	lseek(fd, 0, SEEK_SET);
144275970Scy	/* remove it from the file system */
145275970Scy	unlink(tmpfilename);
146275970Scy	return (fd);
147275970Scy#else
148275970Scy	/* XXXX actually delete the file later */
149275970Scy	char tmpfilepath[MAX_PATH];
150275970Scy	char tmpfilename[MAX_PATH];
151275970Scy	DWORD r, written;
152275970Scy	int tries = 16;
153275970Scy	HANDLE h;
154275970Scy	r = GetTempPathA(MAX_PATH, tmpfilepath);
155275970Scy	if (r > MAX_PATH || r == 0)
156275970Scy		return (-1);
157275970Scy	for (; tries > 0; --tries) {
158275970Scy		r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
159275970Scy		if (r == 0)
160275970Scy			return (-1);
161275970Scy		h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
162275970Scy		    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
163275970Scy		if (h != INVALID_HANDLE_VALUE)
164275970Scy			break;
165275970Scy	}
166275970Scy	if (tries == 0)
167275970Scy		return (-1);
168275970Scy	written = 0;
169275970Scy	*filename_out = strdup(tmpfilename);
170275970Scy	WriteFile(h, data, (DWORD)datalen, &written, NULL);
171275970Scy	/* Closing the fd returned by this function will indeed close h. */
172275970Scy	return _open_osfhandle((intptr_t)h,_O_RDONLY);
173275970Scy#endif
174275970Scy}
175275970Scy
176275970Scy#ifndef _WIN32
177275970Scypid_t
178275970Scyregress_fork(void)
179275970Scy{
180275970Scy	pid_t pid = fork();
181275970Scy#ifdef FORK_BREAKS_GCOV
182275970Scy	vproc_transaction_begin(0);
183275970Scy#endif
184275970Scy	return pid;
185275970Scy}
186275970Scy#endif
187275970Scy
188275970Scystatic void
189275970Scyignore_log_cb(int s, const char *msg)
190275970Scy{
191275970Scy}
192275970Scy
193275970Scystatic void *
194275970Scybasic_test_setup(const struct testcase_t *testcase)
195275970Scy{
196275970Scy	struct event_base *base = NULL;
197275970Scy	evutil_socket_t spair[2] = { -1, -1 };
198275970Scy	struct basic_test_data *data = NULL;
199275970Scy
200275970Scy#ifndef _WIN32
201275970Scy	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
202275970Scy		return (void*)TT_SKIP;
203275970Scy#endif
204275970Scy
205275970Scy	if (testcase->flags & TT_NEED_THREADS) {
206275970Scy		if (!(testcase->flags & TT_FORK))
207275970Scy			return NULL;
208275970Scy#if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
209275970Scy		if (evthread_use_pthreads())
210275970Scy			exit(1);
211275970Scy#elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
212275970Scy		if (evthread_use_windows_threads())
213275970Scy			exit(1);
214275970Scy#else
215275970Scy		return (void*)TT_SKIP;
216275970Scy#endif
217275970Scy	}
218275970Scy
219275970Scy	if (testcase->flags & TT_NEED_SOCKETPAIR) {
220275970Scy		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
221275970Scy			fprintf(stderr, "%s: socketpair\n", __func__);
222275970Scy			exit(1);
223275970Scy		}
224275970Scy
225275970Scy		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
226275970Scy			fprintf(stderr, "fcntl(O_NONBLOCK)");
227275970Scy			exit(1);
228275970Scy		}
229275970Scy
230275970Scy		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
231275970Scy			fprintf(stderr, "fcntl(O_NONBLOCK)");
232275970Scy			exit(1);
233275970Scy		}
234275970Scy	}
235275970Scy	if (testcase->flags & TT_NEED_BASE) {
236275970Scy		if (testcase->flags & TT_LEGACY)
237275970Scy			base = event_init();
238275970Scy		else
239275970Scy			base = event_base_new();
240275970Scy		if (!base)
241275970Scy			exit(1);
242275970Scy	}
243275970Scy	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
244275970Scy		if (event_base_start_iocp_(base, 0)<0) {
245275970Scy			event_base_free(base);
246275970Scy			return (void*)TT_SKIP;
247275970Scy		}
248275970Scy	}
249275970Scy
250275970Scy	if (testcase->flags & TT_NEED_DNS) {
251275970Scy		evdns_set_log_fn(dnslogcb);
252275970Scy		if (evdns_init())
253275970Scy			return NULL; /* fast failure */ /*XXX asserts. */
254275970Scy	}
255275970Scy
256275970Scy	if (testcase->flags & TT_NO_LOGS)
257275970Scy		event_set_log_callback(ignore_log_cb);
258275970Scy
259275970Scy	data = calloc(1, sizeof(*data));
260275970Scy	if (!data)
261275970Scy		exit(1);
262275970Scy	data->base = base;
263275970Scy	data->pair[0] = spair[0];
264275970Scy	data->pair[1] = spair[1];
265275970Scy	data->setup_data = testcase->setup_data;
266275970Scy	return data;
267275970Scy}
268275970Scy
269275970Scystatic int
270275970Scybasic_test_cleanup(const struct testcase_t *testcase, void *ptr)
271275970Scy{
272275970Scy	struct basic_test_data *data = ptr;
273275970Scy
274275970Scy	if (testcase->flags & TT_NO_LOGS)
275275970Scy		event_set_log_callback(NULL);
276275970Scy
277275970Scy	if (testcase->flags & TT_NEED_SOCKETPAIR) {
278275970Scy		if (data->pair[0] != -1)
279275970Scy			evutil_closesocket(data->pair[0]);
280275970Scy		if (data->pair[1] != -1)
281275970Scy			evutil_closesocket(data->pair[1]);
282275970Scy	}
283275970Scy
284275970Scy	if (testcase->flags & TT_NEED_DNS) {
285275970Scy		evdns_shutdown(0);
286275970Scy	}
287275970Scy
288275970Scy	if (testcase->flags & TT_NEED_BASE) {
289275970Scy		if (data->base) {
290275970Scy			event_base_assert_ok_(data->base);
291275970Scy			event_base_free(data->base);
292275970Scy		}
293275970Scy	}
294275970Scy
295275970Scy	if (testcase->flags & TT_FORK)
296275970Scy		libevent_global_shutdown();
297275970Scy
298275970Scy	free(data);
299275970Scy
300275970Scy	return 1;
301275970Scy}
302275970Scy
303275970Scyconst struct testcase_setup_t basic_setup = {
304275970Scy	basic_test_setup, basic_test_cleanup
305275970Scy};
306275970Scy
307275970Scy/* The "data" for a legacy test is just a pointer to the void fn(void)
308275970Scy   function implementing the test case.  We need to set up some globals,
309275970Scy   though, since that's where legacy tests expect to find a socketpair
310275970Scy   (sometimes) and a global event_base (sometimes).
311275970Scy */
312275970Scystatic void *
313275970Scylegacy_test_setup(const struct testcase_t *testcase)
314275970Scy{
315275970Scy	struct basic_test_data *data = basic_test_setup(testcase);
316275970Scy	if (data == (void*)TT_SKIP || data == NULL)
317275970Scy		return data;
318275970Scy	global_base = data->base;
319275970Scy	pair[0] = data->pair[0];
320275970Scy	pair[1] = data->pair[1];
321275970Scy	data->legacy_test_fn = testcase->setup_data;
322275970Scy	return data;
323275970Scy}
324275970Scy
325275970Scy/* This function is the implementation of every legacy test case.  It
326275970Scy   sets test_ok to 0, invokes the test function, and tells tinytest that
327275970Scy   the test failed if the test didn't set test_ok to 1.
328275970Scy */
329275970Scyvoid
330275970Scyrun_legacy_test_fn(void *ptr)
331275970Scy{
332275970Scy	struct basic_test_data *data = ptr;
333275970Scy	test_ok = called = 0;
334275970Scy
335275970Scy	in_legacy_test_wrapper = 1;
336275970Scy	data->legacy_test_fn(); /* This part actually calls the test */
337275970Scy	in_legacy_test_wrapper = 0;
338275970Scy
339275970Scy	if (!test_ok)
340275970Scy		tt_abort_msg("Legacy unit test failed");
341275970Scy
342275970Scyend:
343275970Scy	test_ok = 0;
344275970Scy}
345275970Scy
346275970Scy/* This function doesn't have to clean up ptr (which is just a pointer
347275970Scy   to the test function), but it may need to close the socketpair or
348275970Scy   free the event_base.
349275970Scy */
350275970Scystatic int
351275970Scylegacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
352275970Scy{
353275970Scy	int r = basic_test_cleanup(testcase, ptr);
354275970Scy	pair[0] = pair[1] = -1;
355275970Scy	global_base = NULL;
356275970Scy	return r;
357275970Scy}
358275970Scy
359275970Scyconst struct testcase_setup_t legacy_setup = {
360275970Scy	legacy_test_setup, legacy_test_cleanup
361275970Scy};
362275970Scy
363275970Scy/* ============================================================ */
364275970Scy
365275970Scy#if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
366275970Scystruct testcase_t thread_testcases[] = {
367275970Scy	{ "basic", NULL, TT_SKIP, NULL, NULL },
368275970Scy	END_OF_TESTCASES
369275970Scy};
370275970Scy#endif
371275970Scy
372275970Scystruct testgroup_t testgroups[] = {
373275970Scy	{ "main/", main_testcases },
374275970Scy	{ "heap/", minheap_testcases },
375275970Scy	{ "et/", edgetriggered_testcases },
376275970Scy	{ "finalize/", finalize_testcases },
377275970Scy	{ "evbuffer/", evbuffer_testcases },
378275970Scy	{ "signal/", signal_testcases },
379275970Scy	{ "util/", util_testcases },
380275970Scy	{ "bufferevent/", bufferevent_testcases },
381275970Scy	{ "http/", http_testcases },
382275970Scy	{ "dns/", dns_testcases },
383275970Scy	{ "evtag/", evtag_testcases },
384275970Scy	{ "rpc/", rpc_testcases },
385275970Scy	{ "thread/", thread_testcases },
386275970Scy	{ "listener/", listener_testcases },
387275970Scy#ifdef _WIN32
388275970Scy	{ "iocp/", iocp_testcases },
389275970Scy	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
390275970Scy	{ "iocp/listener/", listener_iocp_testcases },
391275970Scy#endif
392275970Scy#ifdef EVENT__HAVE_OPENSSL
393275970Scy	{ "ssl/", ssl_testcases },
394275970Scy#endif
395275970Scy	END_OF_GROUPS
396275970Scy};
397275970Scy
398275970Scyconst char *alltests[] = { "+..", NULL };
399275970Scyconst char *livenettests[] = {
400275970Scy	"+util/getaddrinfo_live",
401275970Scy	"+dns/gethostby..",
402275970Scy	"+dns/resolve_reverse",
403275970Scy	NULL
404275970Scy};
405275970Scyconst char *finetimetests[] = {
406275970Scy	"+util/monotonic_res_precise",
407275970Scy	"+util/monotonic_res_fallback",
408275970Scy	"+thread/deferred_cb_skew",
409275970Scy	"+http/connection_retry",
410275970Scy	NULL
411275970Scy};
412275970Scystruct testlist_alias_t testaliases[] = {
413275970Scy	{ "all", alltests },
414275970Scy	{ "live_net", livenettests },
415275970Scy	{ "fine_timing", finetimetests },
416275970Scy	END_OF_ALIASES
417275970Scy};
418275970Scy
419275970Scyint libevent_tests_running_in_debug_mode = 0;
420275970Scy
421275970Scyint
422275970Scymain(int argc, const char **argv)
423275970Scy{
424275970Scy#ifdef _WIN32
425275970Scy	WORD wVersionRequested;
426275970Scy	WSADATA wsaData;
427275970Scy
428275970Scy	wVersionRequested = MAKEWORD(2, 2);
429275970Scy
430275970Scy	(void) WSAStartup(wVersionRequested, &wsaData);
431275970Scy#endif
432275970Scy
433275970Scy#ifndef _WIN32
434275970Scy	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
435275970Scy		return 1;
436275970Scy#endif
437275970Scy
438275970Scy#ifdef _WIN32
439275970Scy	tinytest_skip(testgroups, "http/connection_retry");
440275970Scy#endif
441275970Scy
442275970Scy#ifndef EVENT__DISABLE_THREAD_SUPPORT
443275970Scy	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
444275970Scy		evthread_enable_lock_debugging();
445275970Scy#endif
446275970Scy
447275970Scy	if (getenv("EVENT_DEBUG_MODE")) {
448275970Scy		event_enable_debug_mode();
449275970Scy		libevent_tests_running_in_debug_mode = 1;
450275970Scy	}
451275970Scy	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
452275970Scy		event_enable_debug_logging(EVENT_DBG_ALL);
453275970Scy	}
454275970Scy
455275970Scy	tinytest_set_aliases(testaliases);
456275970Scy
457282408Scy	evutil_weakrand_seed_(&test_weakrand_state, 0);
458282408Scy
459275970Scy	if (tinytest_main(argc,argv,testgroups))
460275970Scy		return 1;
461275970Scy
462275970Scy	libevent_global_shutdown();
463275970Scy
464275970Scy	return 0;
465275970Scy}
466275970Scy
467