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