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