ipc/chromium/src/third_party/libevent/test/regress_main.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/test/regress_main.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,420 @@
     1.4 +/*
     1.5 + * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
     1.6 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 3. The name of the author may not be used to endorse or promote products
    1.17 + *    derived from this software without specific prior written permission.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.20 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.21 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.22 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.23 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.24 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.25 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.26 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.27 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.28 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#ifdef WIN32
    1.32 +#include <winsock2.h>
    1.33 +#include <windows.h>
    1.34 +#include <io.h>
    1.35 +#include <fcntl.h>
    1.36 +#endif
    1.37 +
    1.38 +#if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
    1.39 +#if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
    1.40 +    __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
    1.41 +#define FORK_BREAKS_GCOV
    1.42 +#include <vproc.h>
    1.43 +#endif
    1.44 +#endif
    1.45 +
    1.46 +#include "event2/event-config.h"
    1.47 +
    1.48 +#ifdef _EVENT___func__
    1.49 +#define __func__ _EVENT___func__
    1.50 +#endif
    1.51 +
    1.52 +#if 0
    1.53 +#include <sys/types.h>
    1.54 +#include <sys/stat.h>
    1.55 +#ifdef _EVENT_HAVE_SYS_TIME_H
    1.56 +#include <sys/time.h>
    1.57 +#endif
    1.58 +#include <sys/queue.h>
    1.59 +#include <signal.h>
    1.60 +#include <errno.h>
    1.61 +#endif
    1.62 +
    1.63 +#include <sys/types.h>
    1.64 +#ifdef _EVENT_HAVE_SYS_STAT_H
    1.65 +#include <sys/stat.h>
    1.66 +#endif
    1.67 +
    1.68 +#ifndef WIN32
    1.69 +#include <sys/socket.h>
    1.70 +#include <sys/wait.h>
    1.71 +#include <signal.h>
    1.72 +#include <unistd.h>
    1.73 +#include <netdb.h>
    1.74 +#endif
    1.75 +
    1.76 +#include <stdlib.h>
    1.77 +#include <stdio.h>
    1.78 +#include <string.h>
    1.79 +#include <assert.h>
    1.80 +
    1.81 +#include "event2/util.h"
    1.82 +#include "event2/event.h"
    1.83 +#include "event2/event_compat.h"
    1.84 +#include "event2/dns.h"
    1.85 +#include "event2/dns_compat.h"
    1.86 +#include "event2/thread.h"
    1.87 +
    1.88 +#include "event2/event-config.h"
    1.89 +#include "regress.h"
    1.90 +#include "tinytest.h"
    1.91 +#include "tinytest_macros.h"
    1.92 +#include "../iocp-internal.h"
    1.93 +#include "../event-internal.h"
    1.94 +
    1.95 +long
    1.96 +timeval_msec_diff(const struct timeval *start, const struct timeval *end)
    1.97 +{
    1.98 +	long ms = end->tv_sec - start->tv_sec;
    1.99 +	ms *= 1000;
   1.100 +	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
   1.101 +	return ms;
   1.102 +
   1.103 +}
   1.104 +
   1.105 +/* ============================================================ */
   1.106 +/* Code to wrap up old legacy test cases that used setup() and cleanup().
   1.107 + *
   1.108 + * Not all of the tests designated "legacy" are ones that used setup() and
   1.109 + * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
   1.110 + * if it wants to find its event base/socketpair in global variables (ugh),
   1.111 + * OR if it wants to communicate success/failure through test_ok.
   1.112 + */
   1.113 +
   1.114 +/* This is set to true if we're inside a legacy test wrapper.  It lets the
   1.115 +   setup() and cleanup() functions in regress.c know they're not needed.
   1.116 + */
   1.117 +int in_legacy_test_wrapper = 0;
   1.118 +
   1.119 +static void dnslogcb(int w, const char *m)
   1.120 +{
   1.121 +	TT_BLATHER(("%s", m));
   1.122 +}
   1.123 +
   1.124 +/* creates a temporary file with the data in it */
   1.125 +int
   1.126 +regress_make_tmpfile(const void *data, size_t datalen)
   1.127 +{
   1.128 +#ifndef WIN32
   1.129 +	char tmpfilename[32];
   1.130 +	int fd;
   1.131 +	strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
   1.132 +#ifdef _EVENT_HAVE_UMASK
   1.133 +	umask(0077);
   1.134 +#endif
   1.135 +	fd = mkstemp(tmpfilename);
   1.136 +	if (fd == -1)
   1.137 +		return (-1);
   1.138 +	if (write(fd, data, datalen) != (int)datalen) {
   1.139 +		close(fd);
   1.140 +		return (-1);
   1.141 +	}
   1.142 +	lseek(fd, 0, SEEK_SET);
   1.143 +	/* remove it from the file system */
   1.144 +	unlink(tmpfilename);
   1.145 +	return (fd);
   1.146 +#else
   1.147 +	/* XXXX actually delete the file later */
   1.148 +	char tmpfilepath[MAX_PATH];
   1.149 +	char tmpfilename[MAX_PATH];
   1.150 +	DWORD r, written;
   1.151 +	int tries = 16;
   1.152 +	HANDLE h;
   1.153 +	r = GetTempPathA(MAX_PATH, tmpfilepath);
   1.154 +	if (r > MAX_PATH || r == 0)
   1.155 +		return (-1);
   1.156 +	for (; tries > 0; --tries) {
   1.157 +		r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
   1.158 +		if (r == 0)
   1.159 +			return (-1);
   1.160 +		h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
   1.161 +		    0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
   1.162 +		if (h != INVALID_HANDLE_VALUE)
   1.163 +			break;
   1.164 +	}
   1.165 +	if (tries == 0)
   1.166 +		return (-1);
   1.167 +	written = 0;
   1.168 +	WriteFile(h, data, (DWORD)datalen, &written, NULL);
   1.169 +	/* Closing the fd returned by this function will indeed close h. */
   1.170 +	return _open_osfhandle((intptr_t)h,_O_RDONLY);
   1.171 +#endif
   1.172 +}
   1.173 +
   1.174 +#ifndef _WIN32
   1.175 +pid_t
   1.176 +regress_fork(void)
   1.177 +{
   1.178 +	pid_t pid = fork();
   1.179 +#ifdef FORK_BREAKS_GCOV
   1.180 +	vproc_transaction_begin(0);
   1.181 +#endif
   1.182 +	return pid;
   1.183 +}
   1.184 +#endif
   1.185 +
   1.186 +static void
   1.187 +ignore_log_cb(int s, const char *msg)
   1.188 +{
   1.189 +}
   1.190 +
   1.191 +static void *
   1.192 +basic_test_setup(const struct testcase_t *testcase)
   1.193 +{
   1.194 +	struct event_base *base = NULL;
   1.195 +	evutil_socket_t spair[2] = { -1, -1 };
   1.196 +	struct basic_test_data *data = NULL;
   1.197 +
   1.198 +#ifndef WIN32
   1.199 +	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
   1.200 +		return (void*)TT_SKIP;
   1.201 +#endif
   1.202 +
   1.203 +	if (testcase->flags & TT_NEED_THREADS) {
   1.204 +		if (!(testcase->flags & TT_FORK))
   1.205 +			return NULL;
   1.206 +#if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
   1.207 +		if (evthread_use_pthreads())
   1.208 +			exit(1);
   1.209 +#elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
   1.210 +		if (evthread_use_windows_threads())
   1.211 +			exit(1);
   1.212 +#else
   1.213 +		return (void*)TT_SKIP;
   1.214 +#endif
   1.215 +	}
   1.216 +
   1.217 +	if (testcase->flags & TT_NEED_SOCKETPAIR) {
   1.218 +		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
   1.219 +			fprintf(stderr, "%s: socketpair\n", __func__);
   1.220 +			exit(1);
   1.221 +		}
   1.222 +
   1.223 +		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
   1.224 +			fprintf(stderr, "fcntl(O_NONBLOCK)");
   1.225 +			exit(1);
   1.226 +		}
   1.227 +
   1.228 +		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
   1.229 +			fprintf(stderr, "fcntl(O_NONBLOCK)");
   1.230 +			exit(1);
   1.231 +		}
   1.232 +	}
   1.233 +	if (testcase->flags & TT_NEED_BASE) {
   1.234 +		if (testcase->flags & TT_LEGACY)
   1.235 +			base = event_init();
   1.236 +		else
   1.237 +			base = event_base_new();
   1.238 +		if (!base)
   1.239 +			exit(1);
   1.240 +	}
   1.241 +	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
   1.242 +		if (event_base_start_iocp(base, 0)<0) {
   1.243 +			event_base_free(base);
   1.244 +			return (void*)TT_SKIP;
   1.245 +		}
   1.246 +	}
   1.247 +
   1.248 +	if (testcase->flags & TT_NEED_DNS) {
   1.249 +		evdns_set_log_fn(dnslogcb);
   1.250 +		if (evdns_init())
   1.251 +			return NULL; /* fast failure */ /*XXX asserts. */
   1.252 +	}
   1.253 +
   1.254 +	if (testcase->flags & TT_NO_LOGS)
   1.255 +		event_set_log_callback(ignore_log_cb);
   1.256 +
   1.257 +	data = calloc(1, sizeof(*data));
   1.258 +	if (!data)
   1.259 +		exit(1);
   1.260 +	data->base = base;
   1.261 +	data->pair[0] = spair[0];
   1.262 +	data->pair[1] = spair[1];
   1.263 +	data->setup_data = testcase->setup_data;
   1.264 +	return data;
   1.265 +}
   1.266 +
   1.267 +static int
   1.268 +basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
   1.269 +{
   1.270 +	struct basic_test_data *data = ptr;
   1.271 +
   1.272 +	if (testcase->flags & TT_NO_LOGS)
   1.273 +		event_set_log_callback(NULL);
   1.274 +
   1.275 +	if (testcase->flags & TT_NEED_SOCKETPAIR) {
   1.276 +		if (data->pair[0] != -1)
   1.277 +			evutil_closesocket(data->pair[0]);
   1.278 +		if (data->pair[1] != -1)
   1.279 +			evutil_closesocket(data->pair[1]);
   1.280 +	}
   1.281 +
   1.282 +	if (testcase->flags & TT_NEED_DNS) {
   1.283 +		evdns_shutdown(0);
   1.284 +	}
   1.285 +
   1.286 +	if (testcase->flags & TT_NEED_BASE) {
   1.287 +		if (data->base) {
   1.288 +			event_base_assert_ok(data->base);
   1.289 +			event_base_free(data->base);
   1.290 +		}
   1.291 +	}
   1.292 +
   1.293 +	free(data);
   1.294 +
   1.295 +	return 1;
   1.296 +}
   1.297 +
   1.298 +const struct testcase_setup_t basic_setup = {
   1.299 +	basic_test_setup, basic_test_cleanup
   1.300 +};
   1.301 +
   1.302 +/* The "data" for a legacy test is just a pointer to the void fn(void)
   1.303 +   function implementing the test case.  We need to set up some globals,
   1.304 +   though, since that's where legacy tests expect to find a socketpair
   1.305 +   (sometimes) and a global event_base (sometimes).
   1.306 + */
   1.307 +static void *
   1.308 +legacy_test_setup(const struct testcase_t *testcase)
   1.309 +{
   1.310 +	struct basic_test_data *data = basic_test_setup(testcase);
   1.311 +	if (data == (void*)TT_SKIP || data == NULL)
   1.312 +		return data;
   1.313 +	global_base = data->base;
   1.314 +	pair[0] = data->pair[0];
   1.315 +	pair[1] = data->pair[1];
   1.316 +	data->legacy_test_fn = testcase->setup_data;
   1.317 +	return data;
   1.318 +}
   1.319 +
   1.320 +/* This function is the implementation of every legacy test case.  It
   1.321 +   sets test_ok to 0, invokes the test function, and tells tinytest that
   1.322 +   the test failed if the test didn't set test_ok to 1.
   1.323 + */
   1.324 +void
   1.325 +run_legacy_test_fn(void *ptr)
   1.326 +{
   1.327 +	struct basic_test_data *data = ptr;
   1.328 +	test_ok = called = 0;
   1.329 +
   1.330 +	in_legacy_test_wrapper = 1;
   1.331 +	data->legacy_test_fn(); /* This part actually calls the test */
   1.332 +	in_legacy_test_wrapper = 0;
   1.333 +
   1.334 +	if (!test_ok)
   1.335 +		tt_abort_msg("Legacy unit test failed");
   1.336 +
   1.337 +end:
   1.338 +	test_ok = 0;
   1.339 +}
   1.340 +
   1.341 +/* This function doesn't have to clean up ptr (which is just a pointer
   1.342 +   to the test function), but it may need to close the socketpair or
   1.343 +   free the event_base.
   1.344 + */
   1.345 +static int
   1.346 +legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
   1.347 +{
   1.348 +	int r = basic_test_cleanup(testcase, ptr);
   1.349 +	pair[0] = pair[1] = -1;
   1.350 +	global_base = NULL;
   1.351 +	return r;
   1.352 +}
   1.353 +
   1.354 +const struct testcase_setup_t legacy_setup = {
   1.355 +	legacy_test_setup, legacy_test_cleanup
   1.356 +};
   1.357 +
   1.358 +/* ============================================================ */
   1.359 +
   1.360 +#if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT)
   1.361 +struct testcase_t thread_testcases[] = {
   1.362 +	{ "basic", NULL, TT_SKIP, NULL, NULL },
   1.363 +	END_OF_TESTCASES
   1.364 +};
   1.365 +#endif
   1.366 +
   1.367 +struct testgroup_t testgroups[] = {
   1.368 +	{ "main/", main_testcases },
   1.369 +	{ "heap/", minheap_testcases },
   1.370 +	{ "et/", edgetriggered_testcases },
   1.371 +	{ "evbuffer/", evbuffer_testcases },
   1.372 +	{ "signal/", signal_testcases },
   1.373 +	{ "util/", util_testcases },
   1.374 +	{ "bufferevent/", bufferevent_testcases },
   1.375 +	{ "http/", http_testcases },
   1.376 +	{ "dns/", dns_testcases },
   1.377 +	{ "evtag/", evtag_testcases },
   1.378 +	{ "rpc/", rpc_testcases },
   1.379 +	{ "thread/", thread_testcases },
   1.380 +	{ "listener/", listener_testcases },
   1.381 +#ifdef WIN32
   1.382 +	{ "iocp/", iocp_testcases },
   1.383 +	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
   1.384 +	{ "iocp/listener/", listener_iocp_testcases },
   1.385 +#endif
   1.386 +#ifdef _EVENT_HAVE_OPENSSL
   1.387 +	{ "ssl/", ssl_testcases },
   1.388 +#endif
   1.389 +	END_OF_GROUPS
   1.390 +};
   1.391 +
   1.392 +int
   1.393 +main(int argc, const char **argv)
   1.394 +{
   1.395 +#ifdef WIN32
   1.396 +	WORD wVersionRequested;
   1.397 +	WSADATA wsaData;
   1.398 +
   1.399 +	wVersionRequested = MAKEWORD(2, 2);
   1.400 +
   1.401 +	(void) WSAStartup(wVersionRequested, &wsaData);
   1.402 +#endif
   1.403 +
   1.404 +#ifndef WIN32
   1.405 +	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
   1.406 +		return 1;
   1.407 +#endif
   1.408 +
   1.409 +#ifdef WIN32
   1.410 +	tinytest_skip(testgroups, "http/connection_retry");
   1.411 +#endif
   1.412 +
   1.413 +#ifndef _EVENT_DISABLE_THREAD_SUPPORT
   1.414 +	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
   1.415 +		evthread_enable_lock_debuging();
   1.416 +#endif
   1.417 +
   1.418 +	if (tinytest_main(argc,argv,testgroups))
   1.419 +		return 1;
   1.420 +
   1.421 +	return 0;
   1.422 +}
   1.423 +

mercurial