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