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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial