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