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