1 /* $NetBSD: regress_util.c,v 1.5 2016/01/08 21:35:41 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 #include "../util-internal.h" 29 30 #ifdef _WIN32 31 #include <winsock2.h> 32 #include <windows.h> 33 #include <ws2tcpip.h> 34 #endif 35 36 #include "event2/event-config.h" 37 38 #include <sys/types.h> 39 40 #ifndef _WIN32 41 #include <sys/socket.h> 42 #include <netinet/in.h> 43 #include <arpa/inet.h> 44 #include <unistd.h> 45 #endif 46 #ifdef EVENT__HAVE_NETINET_IN6_H 47 #include <netinet/in6.h> 48 #endif 49 #ifdef EVENT__HAVE_SYS_WAIT_H 50 #include <sys/wait.h> 51 #endif 52 #include <signal.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 57 #include "event2/event.h" 58 #include "event2/util.h" 59 #include "../ipv6-internal.h" 60 #include "../log-internal.h" 61 #include "../strlcpy-internal.h" 62 #include "../mm-internal.h" 63 #include "../time-internal.h" 64 65 #include "regress.h" 66 67 enum entry_status { NORMAL, CANONICAL, BAD }; 68 69 /* This is a big table of results we expect from generating and parsing */ 70 static struct ipv4_entry { 71 const char *addr; 72 ev_uint32_t res; 73 enum entry_status status; 74 } ipv4_entries[] = { 75 { "1.2.3.4", 0x01020304u, CANONICAL }, 76 { "255.255.255.255", 0xffffffffu, CANONICAL }, 77 { "256.0.0.0", 0, BAD }, 78 { "ABC", 0, BAD }, 79 { "1.2.3.4.5", 0, BAD }, 80 { "176.192.208.244", 0xb0c0d0f4, CANONICAL }, 81 { NULL, 0, BAD }, 82 }; 83 84 static struct ipv6_entry { 85 const char *addr; 86 ev_uint32_t res[4]; 87 enum entry_status status; 88 } ipv6_entries[] = { 89 { "::", { 0, 0, 0, 0, }, CANONICAL }, 90 { "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL }, 91 { "::1", { 0, 0, 0, 1, }, CANONICAL }, 92 { "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL }, 93 { "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL }, 94 { "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL }, 95 { "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL }, 96 { "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL }, 97 { "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL }, 98 { "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL }, 99 { "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL }, 100 { "foobar.", { 0, 0, 0, 0 }, BAD }, 101 { "foobar", { 0, 0, 0, 0 }, BAD }, 102 { "fo:obar", { 0, 0, 0, 0 }, BAD }, 103 { "ffff", { 0, 0, 0, 0 }, BAD }, 104 { "fffff::", { 0, 0, 0, 0 }, BAD }, 105 { "fffff::", { 0, 0, 0, 0 }, BAD }, 106 { "::1.0.1.1000", { 0, 0, 0, 0 }, BAD }, 107 { "1:2:33333:4::", { 0, 0, 0, 0 }, BAD }, 108 { "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD }, 109 { "1::2::3", { 0, 0, 0, 0 }, BAD }, 110 { ":::1", { 0, 0, 0, 0 }, BAD }, 111 { NULL, { 0, 0, 0, 0, }, BAD }, 112 }; 113 114 static void 115 regress_ipv4_parse(void *ptr) 116 { 117 int i; 118 for (i = 0; ipv4_entries[i].addr; ++i) { 119 char written[128]; 120 struct ipv4_entry *ent = &ipv4_entries[i]; 121 struct in_addr in; 122 int r; 123 r = evutil_inet_pton(AF_INET, ent->addr, &in); 124 if (r == 0) { 125 if (ent->status != BAD) { 126 TT_FAIL(("%s did not parse, but it's a good address!", 127 ent->addr)); 128 } 129 continue; 130 } 131 if (ent->status == BAD) { 132 TT_FAIL(("%s parsed, but we expected an error", ent->addr)); 133 continue; 134 } 135 if (ntohl(in.s_addr) != ent->res) { 136 TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr, 137 (unsigned long)ntohl(in.s_addr), 138 (unsigned long)ent->res)); 139 continue; 140 } 141 if (ent->status == CANONICAL) { 142 const char *w = evutil_inet_ntop(AF_INET, &in, written, 143 sizeof(written)); 144 if (!w) { 145 TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); 146 continue; 147 } 148 if (strcmp(written, ent->addr)) { 149 TT_FAIL(("Tried to write out %s; got %s", 150 ent->addr, written)); 151 continue; 152 } 153 } 154 155 } 156 157 } 158 159 static void 160 regress_ipv6_parse(void *ptr) 161 { 162 #ifdef AF_INET6 163 int i, j; 164 165 for (i = 0; ipv6_entries[i].addr; ++i) { 166 char written[128]; 167 struct ipv6_entry *ent = &ipv6_entries[i]; 168 struct in6_addr in6; 169 int r; 170 r = evutil_inet_pton(AF_INET6, ent->addr, &in6); 171 if (r == 0) { 172 if (ent->status != BAD) 173 TT_FAIL(("%s did not parse, but it's a good address!", 174 ent->addr)); 175 continue; 176 } 177 if (ent->status == BAD) { 178 TT_FAIL(("%s parsed, but we expected an error", ent->addr)); 179 continue; 180 } 181 for (j = 0; j < 4; ++j) { 182 /* Can't use s6_addr32 here; some don't have it. */ 183 ev_uint32_t u = 184 ((ev_uint32_t)in6.s6_addr[j*4 ] << 24) | 185 ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) | 186 ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) | 187 ((ev_uint32_t)in6.s6_addr[j*4+3]); 188 if (u != ent->res[j]) { 189 TT_FAIL(("%s did not parse as expected.", ent->addr)); 190 continue; 191 } 192 } 193 if (ent->status == CANONICAL) { 194 const char *w = evutil_inet_ntop(AF_INET6, &in6, written, 195 sizeof(written)); 196 if (!w) { 197 TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); 198 continue; 199 } 200 if (strcmp(written, ent->addr)) { 201 TT_FAIL(("Tried to write out %s; got %s", ent->addr, written)); 202 continue; 203 } 204 } 205 206 } 207 #else 208 TT_BLATHER(("Skipping IPv6 address parsing.")); 209 #endif 210 } 211 212 static struct sa_port_ent { 213 const char *parse; 214 int safamily; 215 const char *addr; 216 int port; 217 } sa_port_ents[] = { 218 { "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 }, 219 { "[ffff::1]", AF_INET6, "ffff::1", 0 }, 220 { "[ffff::1", 0, NULL, 0 }, 221 { "[ffff::1]:65599", 0, NULL, 0 }, 222 { "[ffff::1]:0", 0, NULL, 0 }, 223 { "[ffff::1]:-1", 0, NULL, 0 }, 224 { "::1", AF_INET6, "::1", 0 }, 225 { "1:2::1", AF_INET6, "1:2::1", 0 }, 226 { "192.168.0.1:50", AF_INET, "192.168.0.1", 50 }, 227 { "1.2.3.4", AF_INET, "1.2.3.4", 0 }, 228 { NULL, 0, NULL, 0 }, 229 }; 230 231 static void 232 regress_sockaddr_port_parse(void *ptr) 233 { 234 struct sockaddr_storage ss; 235 int i, r; 236 237 for (i = 0; sa_port_ents[i].parse; ++i) { 238 struct sa_port_ent *ent = &sa_port_ents[i]; 239 int len = sizeof(ss); 240 memset(&ss, 0, sizeof(ss)); 241 r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); 242 if (r < 0) { 243 if (ent->safamily) 244 TT_FAIL(("Couldn't parse %s!", ent->parse)); 245 continue; 246 } else if (! ent->safamily) { 247 TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse)); 248 continue; 249 } 250 if (ent->safamily == AF_INET) { 251 struct sockaddr_in sin; 252 memset(&sin, 0, sizeof(sin)); 253 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 254 sin.sin_len = sizeof(sin); 255 #endif 256 sin.sin_family = AF_INET; 257 sin.sin_port = htons(ent->port); 258 r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr); 259 if (1 != r) { 260 TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr)); 261 } else if (memcmp(&sin, &ss, sizeof(sin))) { 262 TT_FAIL(("Parse for %s was not as expected.", ent->parse)); 263 } else if (len != sizeof(sin)) { 264 TT_FAIL(("Length for %s not as expected.",ent->parse)); 265 } 266 } else { 267 struct sockaddr_in6 sin6; 268 memset(&sin6, 0, sizeof(sin6)); 269 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN 270 sin6.sin6_len = sizeof(sin6); 271 #endif 272 sin6.sin6_family = AF_INET6; 273 sin6.sin6_port = htons(ent->port); 274 r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr); 275 if (1 != r) { 276 TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr)); 277 } else if (memcmp(&sin6, &ss, sizeof(sin6))) { 278 TT_FAIL(("Parse for %s was not as expected.", ent->parse)); 279 } else if (len != sizeof(sin6)) { 280 TT_FAIL(("Length for %s not as expected.",ent->parse)); 281 } 282 } 283 } 284 } 285 286 287 static void 288 regress_sockaddr_port_format(void *ptr) 289 { 290 struct sockaddr_storage ss; 291 int len; 292 const char *cp; 293 char cbuf[128]; 294 int r; 295 296 len = sizeof(ss); 297 r = evutil_parse_sockaddr_port("192.168.1.1:80", 298 (struct sockaddr*)&ss, &len); 299 tt_int_op(r,==,0); 300 cp = evutil_format_sockaddr_port_( 301 (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); 302 tt_ptr_op(cp,==,cbuf); 303 tt_str_op(cp,==,"192.168.1.1:80"); 304 305 len = sizeof(ss); 306 r = evutil_parse_sockaddr_port("[ff00::8010]:999", 307 (struct sockaddr*)&ss, &len); 308 tt_int_op(r,==,0); 309 cp = evutil_format_sockaddr_port_( 310 (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); 311 tt_ptr_op(cp,==,cbuf); 312 tt_str_op(cp,==,"[ff00::8010]:999"); 313 314 ss.ss_family=99; 315 cp = evutil_format_sockaddr_port_( 316 (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); 317 tt_ptr_op(cp,==,cbuf); 318 tt_str_op(cp,==,"<addr with socktype 99>"); 319 end: 320 ; 321 } 322 323 static struct sa_pred_ent { 324 const char *parse; 325 326 int is_loopback; 327 } sa_pred_entries[] = { 328 { "127.0.0.1", 1 }, 329 { "127.0.3.2", 1 }, 330 { "128.1.2.3", 0 }, 331 { "18.0.0.1", 0 }, 332 { "129.168.1.1", 0 }, 333 334 { "::1", 1 }, 335 { "::0", 0 }, 336 { "f::1", 0 }, 337 { "::501", 0 }, 338 { NULL, 0 }, 339 340 }; 341 342 static void 343 test_evutil_sockaddr_predicates(void *ptr) 344 { 345 struct sockaddr_storage ss; 346 int r, i; 347 348 for (i=0; sa_pred_entries[i].parse; ++i) { 349 struct sa_pred_ent *ent = &sa_pred_entries[i]; 350 int len = sizeof(ss); 351 352 r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); 353 354 if (r<0) { 355 TT_FAIL(("Couldn't parse %s!", ent->parse)); 356 continue; 357 } 358 359 /* sockaddr_is_loopback */ 360 if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) { 361 TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected", 362 ent->parse)); 363 } 364 } 365 } 366 367 static void 368 test_evutil_strtoll(void *ptr) 369 { 370 const char *s; 371 char *endptr; 372 373 tt_want(evutil_strtoll("5000000000", NULL, 10) == 374 ((ev_int64_t)5000000)*1000); 375 tt_want(evutil_strtoll("-5000000000", NULL, 10) == 376 ((ev_int64_t)5000000)*-1000); 377 s = " 99999stuff"; 378 tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999); 379 tt_want(endptr == s+6); 380 tt_want(evutil_strtoll("foo", NULL, 10) == 0); 381 } 382 383 static void 384 test_evutil_snprintf(void *ptr) 385 { 386 char buf[16]; 387 int r; 388 ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200; 389 ev_int64_t i64 = -1 * (ev_int64_t) u64; 390 size_t size = 8000; 391 ev_ssize_t ssize = -9000; 392 393 r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100); 394 tt_str_op(buf, ==, "50 100"); 395 tt_int_op(r, ==, 6); 396 397 r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890); 398 tt_str_op(buf, ==, "longish 1234567"); 399 tt_int_op(r, ==, 18); 400 401 r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64)); 402 tt_str_op(buf, ==, "200000000000"); 403 tt_int_op(r, ==, 12); 404 405 r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64)); 406 tt_str_op(buf, ==, "-200000000000"); 407 tt_int_op(r, ==, 13); 408 409 r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT, 410 EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize)); 411 tt_str_op(buf, ==, "8000 -9000"); 412 tt_int_op(r, ==, 10); 413 414 end: 415 ; 416 } 417 418 static void 419 test_evutil_casecmp(void *ptr) 420 { 421 tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0); 422 tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0); 423 tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0); 424 tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0); 425 tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0); 426 427 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0); 428 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0); 429 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0); 430 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0); 431 tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0); 432 tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0); 433 tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0); 434 tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0); 435 end: 436 ; 437 } 438 439 static void 440 test_evutil_rtrim(void *ptr) 441 { 442 #define TEST_TRIM(s, result) \ 443 do { \ 444 if (cp) mm_free(cp); \ 445 cp = mm_strdup(s); \ 446 tt_assert(cp); \ 447 evutil_rtrim_lws_(cp); \ 448 tt_str_op(cp, ==, result); \ 449 } while(0) 450 451 char *cp = NULL; 452 (void) ptr; 453 454 TEST_TRIM("", ""); 455 TEST_TRIM("a", "a"); 456 TEST_TRIM("abcdef ghi", "abcdef ghi"); 457 458 TEST_TRIM(" ", ""); 459 TEST_TRIM(" ", ""); 460 TEST_TRIM("a ", "a"); 461 TEST_TRIM("abcdef gH ", "abcdef gH"); 462 463 TEST_TRIM("\t\t", ""); 464 TEST_TRIM(" \t", ""); 465 TEST_TRIM("\t", ""); 466 TEST_TRIM("a \t", "a"); 467 TEST_TRIM("a\t ", "a"); 468 TEST_TRIM("a\t", "a"); 469 TEST_TRIM("abcdef gH \t ", "abcdef gH"); 470 471 end: 472 if (cp) 473 mm_free(cp); 474 } 475 476 static int logsev = 0; 477 static char *logmsg = NULL; 478 479 static void 480 logfn(int severity, const char *msg) 481 { 482 logsev = severity; 483 tt_want(msg); 484 if (msg) { 485 if (logmsg) 486 free(logmsg); 487 logmsg = strdup(msg); 488 } 489 } 490 491 static int fatal_want_severity = 0; 492 static const char *fatal_want_message = NULL; 493 static void 494 fatalfn(int exitcode) 495 { 496 if (logsev != fatal_want_severity || 497 !logmsg || 498 strcmp(logmsg, fatal_want_message)) 499 exit(0); 500 else 501 exit(exitcode); 502 } 503 504 #ifndef _WIN32 505 #define CAN_CHECK_ERR 506 static void 507 check_error_logging(void (*fn)(void), int wantexitcode, 508 int wantseverity, const char *wantmsg) 509 { 510 pid_t pid; 511 int status = 0, exitcode; 512 fatal_want_severity = wantseverity; 513 fatal_want_message = wantmsg; 514 if ((pid = regress_fork()) == 0) { 515 /* child process */ 516 fn(); 517 exit(0); /* should be unreachable. */ 518 } else { 519 wait(&status); 520 exitcode = WEXITSTATUS(status); 521 tt_int_op(wantexitcode, ==, exitcode); 522 } 523 end: 524 ; 525 } 526 527 static void 528 errx_fn(void) 529 { 530 event_errx(2, "Fatal error; too many kumquats (%d)", 5); 531 } 532 533 static void 534 err_fn(void) 535 { 536 errno = ENOENT; 537 event_err(5,"Couldn't open %s", "/very/bad/file"); 538 } 539 540 static void 541 sock_err_fn(void) 542 { 543 evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0); 544 #ifdef _WIN32 545 EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); 546 #else 547 errno = EAGAIN; 548 #endif 549 event_sock_err(20, fd, "Unhappy socket"); 550 } 551 #endif 552 553 static void 554 test_evutil_log(void *ptr) 555 { 556 evutil_socket_t fd = -1; 557 char buf[128]; 558 559 event_set_log_callback(logfn); 560 event_set_fatal_callback(fatalfn); 561 #define RESET() do { \ 562 logsev = 0; \ 563 if (logmsg) free(logmsg); \ 564 logmsg = NULL; \ 565 } while (0) 566 #define LOGEQ(sev,msg) do { \ 567 tt_int_op(logsev,==,sev); \ 568 tt_assert(logmsg != NULL); \ 569 tt_str_op(logmsg,==,msg); \ 570 } while (0) 571 572 #ifdef CAN_CHECK_ERR 573 /* We need to disable these tests for now. Previously, the logging 574 * module didn't enforce the requirement that a fatal callback 575 * actually exit. Now, it exits no matter what, so if we wan to 576 * reinstate these tests, we'll need to fork for each one. */ 577 check_error_logging(errx_fn, 2, EVENT_LOG_ERR, 578 "Fatal error; too many kumquats (5)"); 579 RESET(); 580 #endif 581 582 event_warnx("Far too many %s (%d)", "wombats", 99); 583 LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)"); 584 RESET(); 585 586 event_msgx("Connecting lime to coconut"); 587 LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut"); 588 RESET(); 589 590 event_debug(("A millisecond passed! We should log that!")); 591 #ifdef USE_DEBUG 592 LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!"); 593 #else 594 tt_int_op(logsev,==,0); 595 tt_ptr_op(logmsg,==,NULL); 596 #endif 597 RESET(); 598 599 /* Try with an errno. */ 600 errno = ENOENT; 601 event_warn("Couldn't open %s", "/bad/file"); 602 evutil_snprintf(buf, sizeof(buf), 603 "Couldn't open /bad/file: %s",strerror(ENOENT)); 604 LOGEQ(EVENT_LOG_WARN,buf); 605 RESET(); 606 607 #ifdef CAN_CHECK_ERR 608 evutil_snprintf(buf, sizeof(buf), 609 "Couldn't open /very/bad/file: %s",strerror(ENOENT)); 610 check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf); 611 RESET(); 612 #endif 613 614 /* Try with a socket errno. */ 615 fd = socket(AF_INET, SOCK_STREAM, 0); 616 #ifdef _WIN32 617 evutil_snprintf(buf, sizeof(buf), 618 "Unhappy socket: %s", 619 evutil_socket_error_to_string(WSAEWOULDBLOCK)); 620 EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); 621 #else 622 evutil_snprintf(buf, sizeof(buf), 623 "Unhappy socket: %s", strerror(EAGAIN)); 624 errno = EAGAIN; 625 #endif 626 event_sock_warn(fd, "Unhappy socket"); 627 LOGEQ(EVENT_LOG_WARN, buf); 628 RESET(); 629 630 #ifdef CAN_CHECK_ERR 631 check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf); 632 RESET(); 633 #endif 634 635 #undef RESET 636 #undef LOGEQ 637 end: 638 if (logmsg) 639 free(logmsg); 640 if (fd >= 0) 641 evutil_closesocket(fd); 642 } 643 644 static void 645 test_evutil_strlcpy(void *arg) 646 { 647 char buf[8]; 648 649 /* Successful case. */ 650 tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf))); 651 tt_str_op(buf, ==, "Hello"); 652 653 /* Overflow by a lot. */ 654 tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf))); 655 tt_str_op(buf, ==, "pentasy"); 656 657 /* Overflow by exactly one. */ 658 tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf))); 659 tt_str_op(buf, ==, "overlon"); 660 end: 661 ; 662 } 663 664 struct example_struct { 665 const char *a; 666 const char *b; 667 long c; 668 }; 669 670 static void 671 test_evutil_upcast(void *arg) 672 { 673 struct example_struct es1; 674 const char **cp; 675 es1.a = "World"; 676 es1.b = "Hello"; 677 es1.c = -99; 678 679 tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*)); 680 681 cp = &es1.b; 682 tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1); 683 684 end: 685 ; 686 } 687 688 static void 689 test_evutil_integers(void *arg) 690 { 691 ev_int64_t i64; 692 ev_uint64_t u64; 693 ev_int32_t i32; 694 ev_uint32_t u32; 695 ev_int16_t i16; 696 ev_uint16_t u16; 697 ev_int8_t i8; 698 ev_uint8_t u8; 699 700 void *ptr; 701 ev_intptr_t iptr; 702 ev_uintptr_t uptr; 703 704 ev_ssize_t ssize; 705 706 tt_int_op(sizeof(u64), ==, 8); 707 tt_int_op(sizeof(i64), ==, 8); 708 tt_int_op(sizeof(u32), ==, 4); 709 tt_int_op(sizeof(i32), ==, 4); 710 tt_int_op(sizeof(u16), ==, 2); 711 tt_int_op(sizeof(i16), ==, 2); 712 tt_int_op(sizeof(u8), ==, 1); 713 tt_int_op(sizeof(i8), ==, 1); 714 715 tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t)); 716 tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *)); 717 tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t)); 718 719 u64 = 1000000000; 720 u64 *= 1000000000; 721 tt_assert(u64 / 1000000000 == 1000000000); 722 i64 = -1000000000; 723 i64 *= 1000000000; 724 tt_assert(i64 / 1000000000 == -1000000000); 725 726 u64 = EV_UINT64_MAX; 727 i64 = EV_INT64_MAX; 728 tt_assert(u64 > 0); 729 tt_assert(i64 > 0); 730 u64++; 731 /* i64++; */ 732 tt_assert(u64 == 0); 733 /* tt_assert(i64 == EV_INT64_MIN); */ 734 /* tt_assert(i64 < 0); */ 735 736 u32 = EV_UINT32_MAX; 737 i32 = EV_INT32_MAX; 738 tt_assert(u32 > 0); 739 tt_assert(i32 > 0); 740 u32++; 741 /* i32++; */ 742 tt_assert(u32 == 0); 743 /* tt_assert(i32 == EV_INT32_MIN); */ 744 /* tt_assert(i32 < 0); */ 745 746 u16 = EV_UINT16_MAX; 747 i16 = EV_INT16_MAX; 748 tt_assert(u16 > 0); 749 tt_assert(i16 > 0); 750 u16++; 751 /* i16++; */ 752 tt_assert(u16 == 0); 753 /* tt_assert(i16 == EV_INT16_MIN); */ 754 /* tt_assert(i16 < 0); */ 755 756 u8 = EV_UINT8_MAX; 757 i8 = EV_INT8_MAX; 758 tt_assert(u8 > 0); 759 tt_assert(i8 > 0); 760 u8++; 761 /* i8++;*/ 762 tt_assert(u8 == 0); 763 /* tt_assert(i8 == EV_INT8_MIN); */ 764 /* tt_assert(i8 < 0); */ 765 766 /* 767 ssize = EV_SSIZE_MAX; 768 tt_assert(ssize > 0); 769 ssize++; 770 tt_assert(ssize < 0); 771 tt_assert(ssize == EV_SSIZE_MIN); 772 */ 773 774 ptr = &ssize; 775 iptr = (ev_intptr_t)ptr; 776 uptr = (ev_uintptr_t)ptr; 777 ptr = (void *)iptr; 778 tt_assert(ptr == &ssize); 779 ptr = (void *)uptr; 780 tt_assert(ptr == &ssize); 781 782 iptr = -1; 783 tt_assert(iptr < 0); 784 end: 785 ; 786 } 787 788 struct evutil_addrinfo * 789 ai_find_by_family(struct evutil_addrinfo *ai, int family) 790 { 791 while (ai) { 792 if (ai->ai_family == family) 793 return ai; 794 ai = ai->ai_next; 795 } 796 return NULL; 797 } 798 799 struct evutil_addrinfo * 800 ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol) 801 { 802 while (ai) { 803 if (ai->ai_protocol == protocol) 804 return ai; 805 ai = ai->ai_next; 806 } 807 return NULL; 808 } 809 810 811 int 812 test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port, 813 int socktype, int protocol, int line) 814 { 815 struct sockaddr_storage ss; 816 int slen = sizeof(ss); 817 int gotport; 818 char buf[128]; 819 memset(&ss, 0, sizeof(ss)); 820 if (socktype > 0) 821 tt_int_op(ai->ai_socktype, ==, socktype); 822 if (protocol > 0) 823 tt_int_op(ai->ai_protocol, ==, protocol); 824 825 if (evutil_parse_sockaddr_port( 826 sockaddr_port, (struct sockaddr*)&ss, &slen)<0) { 827 TT_FAIL(("Couldn't parse expected address %s on line %d", 828 sockaddr_port, line)); 829 return -1; 830 } 831 if (ai->ai_family != ss.ss_family) { 832 TT_FAIL(("Address family %d did not match %d on line %d", 833 ai->ai_family, ss.ss_family, line)); 834 return -1; 835 } 836 if (ai->ai_addr->sa_family == AF_INET) { 837 struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr; 838 evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); 839 gotport = ntohs(sin->sin_port); 840 if (ai->ai_addrlen != sizeof(struct sockaddr_in)) { 841 TT_FAIL(("Addr size mismatch on line %d", line)); 842 return -1; 843 } 844 } else { 845 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr; 846 evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf)); 847 gotport = ntohs(sin6->sin6_port); 848 if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) { 849 TT_FAIL(("Addr size mismatch on line %d", line)); 850 return -1; 851 } 852 } 853 if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) { 854 TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port, 855 buf, gotport, line)); 856 return -1; 857 } else { 858 TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port, 859 buf, gotport, line)); 860 } 861 return 0; 862 end: 863 TT_FAIL(("Test failed on line %d", line)); 864 return -1; 865 } 866 867 static void 868 test_evutil_rand(void *arg) 869 { 870 char buf1[32]; 871 char buf2[32]; 872 int counts[256]; 873 int i, j, k, n=0; 874 struct evutil_weakrand_state seed = { 12346789U }; 875 876 memset(buf2, 0, sizeof(buf2)); 877 memset(counts, 0, sizeof(counts)); 878 879 for (k=0;k<32;++k) { 880 /* Try a few different start and end points; try to catch 881 * the various misaligned cases of arc4random_buf */ 882 int startpoint = evutil_weakrand_(&seed) % 4; 883 int endpoint = 32 - (evutil_weakrand_(&seed) % 4); 884 885 memset(buf2, 0, sizeof(buf2)); 886 887 /* Do 6 runs over buf1, or-ing the result into buf2 each 888 * time, to make sure we're setting each byte that we mean 889 * to set. */ 890 for (i=0;i<8;++i) { 891 memset(buf1, 0, sizeof(buf1)); 892 evutil_secure_rng_get_bytes(buf1 + startpoint, 893 endpoint-startpoint); 894 n += endpoint - startpoint; 895 for (j=0; j<32; ++j) { 896 if (j >= startpoint && j < endpoint) { 897 buf2[j] |= buf1[j]; 898 ++counts[(unsigned char)buf1[j]]; 899 } else { 900 tt_assert(buf1[j] == 0); 901 tt_int_op(buf1[j], ==, 0); 902 903 } 904 } 905 } 906 907 /* This will give a false positive with P=(256**8)==(2**64) 908 * for each character. */ 909 for (j=startpoint;j<endpoint;++j) { 910 tt_int_op(buf2[j], !=, 0); 911 } 912 } 913 914 evutil_weakrand_seed_(&seed, 0); 915 for (i = 0; i < 10000; ++i) { 916 ev_int32_t r = evutil_weakrand_range_(&seed, 9999); 917 tt_int_op(0, <=, r); 918 tt_int_op(r, <, 9999); 919 } 920 921 /* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */ 922 end: 923 ; 924 } 925 926 static void 927 test_evutil_getaddrinfo(void *arg) 928 { 929 struct evutil_addrinfo *ai = NULL, *a; 930 struct evutil_addrinfo hints; 931 int r; 932 933 /* Try using it as a pton. */ 934 memset(&hints, 0, sizeof(hints)); 935 hints.ai_family = PF_UNSPEC; 936 hints.ai_socktype = SOCK_STREAM; 937 r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai); 938 tt_int_op(r, ==, 0); 939 tt_assert(ai); 940 tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */ 941 test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP); 942 evutil_freeaddrinfo(ai); 943 ai = NULL; 944 945 memset(&hints, 0, sizeof(hints)); 946 hints.ai_family = PF_UNSPEC; 947 hints.ai_protocol = IPPROTO_UDP; 948 r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai); 949 tt_int_op(r, ==, 0); 950 tt_assert(ai); 951 tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */ 952 test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP); 953 evutil_freeaddrinfo(ai); 954 ai = NULL; 955 956 /* Try out the behavior of nodename=NULL */ 957 memset(&hints, 0, sizeof(hints)); 958 hints.ai_family = PF_INET; 959 hints.ai_protocol = IPPROTO_TCP; 960 hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */ 961 r = evutil_getaddrinfo(NULL, "9999", &hints, &ai); 962 tt_int_op(r,==,0); 963 tt_assert(ai); 964 tt_ptr_op(ai->ai_next, ==, NULL); 965 test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP); 966 evutil_freeaddrinfo(ai); 967 ai = NULL; 968 hints.ai_flags = 0; /* as if for connect */ 969 r = evutil_getaddrinfo(NULL, "9998", &hints, &ai); 970 tt_assert(ai); 971 tt_int_op(r,==,0); 972 test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP); 973 tt_ptr_op(ai->ai_next, ==, NULL); 974 evutil_freeaddrinfo(ai); 975 ai = NULL; 976 977 hints.ai_flags = 0; /* as if for connect */ 978 hints.ai_family = PF_INET6; 979 r = evutil_getaddrinfo(NULL, "9997", &hints, &ai); 980 tt_assert(ai); 981 tt_int_op(r,==,0); 982 tt_ptr_op(ai->ai_next, ==, NULL); 983 test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP); 984 evutil_freeaddrinfo(ai); 985 ai = NULL; 986 987 hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */ 988 hints.ai_family = PF_INET6; 989 r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); 990 tt_assert(ai); 991 tt_int_op(r,==,0); 992 tt_ptr_op(ai->ai_next, ==, NULL); 993 test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); 994 evutil_freeaddrinfo(ai); 995 ai = NULL; 996 997 /* Now try an unspec one. We should get a v6 and a v4. */ 998 hints.ai_family = PF_UNSPEC; 999 r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); 1000 tt_assert(ai); 1001 tt_int_op(r,==,0); 1002 a = ai_find_by_family(ai, PF_INET6); 1003 tt_assert(a); 1004 test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); 1005 a = ai_find_by_family(ai, PF_INET); 1006 tt_assert(a); 1007 test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP); 1008 evutil_freeaddrinfo(ai); 1009 ai = NULL; 1010 1011 /* Try out AI_NUMERICHOST: successful case. Also try 1012 * multiprotocol. */ 1013 memset(&hints, 0, sizeof(hints)); 1014 hints.ai_family = PF_UNSPEC; 1015 hints.ai_flags = EVUTIL_AI_NUMERICHOST; 1016 r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai); 1017 tt_int_op(r, ==, 0); 1018 a = ai_find_by_protocol(ai, IPPROTO_TCP); 1019 tt_assert(a); 1020 test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP); 1021 a = ai_find_by_protocol(ai, IPPROTO_UDP); 1022 tt_assert(a); 1023 test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP); 1024 evutil_freeaddrinfo(ai); 1025 ai = NULL; 1026 1027 /* Try the failing case of AI_NUMERICHOST */ 1028 memset(&hints, 0, sizeof(hints)); 1029 hints.ai_family = PF_UNSPEC; 1030 hints.ai_flags = EVUTIL_AI_NUMERICHOST; 1031 r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); 1032 tt_int_op(r, ==, EVUTIL_EAI_NONAME); 1033 tt_ptr_op(ai, ==, NULL); 1034 1035 /* Try symbolic service names wit AI_NUMERICSERV */ 1036 memset(&hints, 0, sizeof(hints)); 1037 hints.ai_family = PF_UNSPEC; 1038 hints.ai_socktype = SOCK_STREAM; 1039 hints.ai_flags = EVUTIL_AI_NUMERICSERV; 1040 r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); 1041 tt_int_op(r,==,EVUTIL_EAI_NONAME); 1042 1043 /* Try symbolic service names */ 1044 memset(&hints, 0, sizeof(hints)); 1045 hints.ai_family = PF_UNSPEC; 1046 hints.ai_socktype = SOCK_STREAM; 1047 r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); 1048 if (r!=0) { 1049 TT_DECLARE("SKIP", ("Symbolic service names seem broken.")); 1050 } else { 1051 tt_assert(ai); 1052 test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP); 1053 evutil_freeaddrinfo(ai); 1054 ai = NULL; 1055 } 1056 1057 end: 1058 if (ai) 1059 evutil_freeaddrinfo(ai); 1060 } 1061 1062 static void 1063 test_evutil_getaddrinfo_live(void *arg) 1064 { 1065 struct evutil_addrinfo *ai = NULL; 1066 struct evutil_addrinfo hints; 1067 1068 struct sockaddr_in6 *sin6; 1069 struct sockaddr_in *sin; 1070 char buf[128]; 1071 const char *cp; 1072 int r; 1073 1074 /* Now do some actual lookups. */ 1075 memset(&hints, 0, sizeof(hints)); 1076 hints.ai_family = PF_INET; 1077 hints.ai_protocol = IPPROTO_TCP; 1078 hints.ai_socktype = SOCK_STREAM; 1079 r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); 1080 if (r != 0) { 1081 TT_DECLARE("SKIP", ("Couldn't resolve www.google.com")); 1082 } else { 1083 tt_assert(ai); 1084 tt_int_op(ai->ai_family, ==, PF_INET); 1085 tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP); 1086 tt_int_op(ai->ai_socktype, ==, SOCK_STREAM); 1087 tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in)); 1088 sin = (struct sockaddr_in*)ai->ai_addr; 1089 tt_int_op(sin->sin_family, ==, AF_INET); 1090 tt_int_op(sin->sin_port, ==, htons(80)); 1091 tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff); 1092 1093 cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); 1094 TT_BLATHER(("www.google.com resolved to %s", 1095 cp?cp:"<unwriteable>")); 1096 evutil_freeaddrinfo(ai); 1097 ai = NULL; 1098 } 1099 1100 hints.ai_family = PF_INET6; 1101 r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai); 1102 if (r != 0) { 1103 TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com")); 1104 } else { 1105 tt_assert(ai); 1106 tt_int_op(ai->ai_family, ==, PF_INET6); 1107 tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6)); 1108 sin6 = (struct sockaddr_in6*)ai->ai_addr; 1109 tt_int_op(sin6->sin6_port, ==, htons(80)); 1110 1111 cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, 1112 sizeof(buf)); 1113 TT_BLATHER(("ipv6.google.com resolved to %s", 1114 cp?cp:"<unwriteable>")); 1115 } 1116 1117 end: 1118 if (ai) 1119 evutil_freeaddrinfo(ai); 1120 } 1121 1122 #ifdef _WIN32 1123 static void 1124 test_evutil_loadsyslib(void *arg) 1125 { 1126 HMODULE h=NULL; 1127 1128 h = evutil_load_windows_system_library_(TEXT("kernel32.dll")); 1129 tt_assert(h); 1130 1131 end: 1132 if (h) 1133 CloseHandle(h); 1134 1135 } 1136 #endif 1137 1138 /** Test mm_malloc(). */ 1139 static void 1140 test_event_malloc(void *arg) 1141 { 1142 void *p = NULL; 1143 (void)arg; 1144 1145 /* mm_malloc(0) should simply return NULL. */ 1146 #ifndef EVENT__DISABLE_MM_REPLACEMENT 1147 errno = 0; 1148 p = mm_malloc(0); 1149 tt_assert(p == NULL); 1150 tt_int_op(errno, ==, 0); 1151 #endif 1152 1153 /* Trivial case. */ 1154 errno = 0; 1155 p = mm_malloc(8); 1156 tt_assert(p != NULL); 1157 tt_int_op(errno, ==, 0); 1158 mm_free(p); 1159 1160 end: 1161 errno = 0; 1162 return; 1163 } 1164 1165 static void 1166 test_event_calloc(void *arg) 1167 { 1168 void *p = NULL; 1169 (void)arg; 1170 1171 #ifndef EVENT__DISABLE_MM_REPLACEMENT 1172 /* mm_calloc() should simply return NULL 1173 * if either argument is zero. */ 1174 errno = 0; 1175 p = mm_calloc(0, 0); 1176 tt_assert(p == NULL); 1177 tt_int_op(errno, ==, 0); 1178 errno = 0; 1179 p = mm_calloc(0, 1); 1180 tt_assert(p == NULL); 1181 tt_int_op(errno, ==, 0); 1182 errno = 0; 1183 p = mm_calloc(1, 0); 1184 tt_assert(p == NULL); 1185 tt_int_op(errno, ==, 0); 1186 #endif 1187 1188 /* Trivial case. */ 1189 errno = 0; 1190 p = mm_calloc(8, 8); 1191 tt_assert(p != NULL); 1192 tt_int_op(errno, ==, 0); 1193 mm_free(p); 1194 p = NULL; 1195 1196 /* mm_calloc() should set errno = ENOMEM and return NULL 1197 * in case of potential overflow. */ 1198 errno = 0; 1199 p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8); 1200 tt_assert(p == NULL); 1201 tt_int_op(errno, ==, ENOMEM); 1202 1203 end: 1204 errno = 0; 1205 if (p) 1206 mm_free(p); 1207 1208 return; 1209 } 1210 1211 static void 1212 test_event_strdup(void *arg) 1213 { 1214 void *p = NULL; 1215 (void)arg; 1216 1217 #ifndef EVENT__DISABLE_MM_REPLACEMENT 1218 /* mm_strdup(NULL) should set errno = EINVAL and return NULL. */ 1219 errno = 0; 1220 p = mm_strdup(NULL); 1221 tt_assert(p == NULL); 1222 tt_int_op(errno, ==, EINVAL); 1223 #endif 1224 1225 /* Trivial cases. */ 1226 1227 errno = 0; 1228 p = mm_strdup(""); 1229 tt_assert(p != NULL); 1230 tt_int_op(errno, ==, 0); 1231 tt_str_op(p, ==, ""); 1232 mm_free(p); 1233 1234 errno = 0; 1235 p = mm_strdup("foo"); 1236 tt_assert(p != NULL); 1237 tt_int_op(errno, ==, 0); 1238 tt_str_op(p, ==, "foo"); 1239 mm_free(p); 1240 1241 /* XXX 1242 * mm_strdup(str) where str is a string of length EV_SIZE_MAX 1243 * should set errno = ENOMEM and return NULL. */ 1244 1245 end: 1246 errno = 0; 1247 return; 1248 } 1249 1250 static void 1251 test_evutil_usleep(void *arg) 1252 { 1253 struct timeval tv1, tv2, tv3, diff1, diff2; 1254 const struct timeval quarter_sec = {0, 250*1000}; 1255 const struct timeval tenth_sec = {0, 100*1000}; 1256 long usec1, usec2; 1257 1258 evutil_gettimeofday(&tv1, NULL); 1259 evutil_usleep_(&quarter_sec); 1260 evutil_gettimeofday(&tv2, NULL); 1261 evutil_usleep_(&tenth_sec); 1262 evutil_gettimeofday(&tv3, NULL); 1263 1264 evutil_timersub(&tv2, &tv1, &diff1); 1265 evutil_timersub(&tv3, &tv2, &diff2); 1266 usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec; 1267 usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec; 1268 1269 tt_int_op(usec1, >, 200000); 1270 tt_int_op(usec1, <, 300000); 1271 tt_int_op(usec2, >, 80000); 1272 tt_int_op(usec2, <, 120000); 1273 1274 end: 1275 ; 1276 } 1277 1278 static void 1279 test_evutil_monotonic_res(void *data_) 1280 { 1281 /* Basic santity-test for monotonic timers. What we'd really like 1282 * to do is make sure that they can't go backwards even when the 1283 * system clock goes backwards. But we haven't got a good way to 1284 * move the system clock backwards. 1285 */ 1286 struct basic_test_data *data = data_; 1287 struct evutil_monotonic_timer timer; 1288 const int precise = strstr(data->setup_data, "precise") != NULL; 1289 const int fallback = strstr(data->setup_data, "fallback") != NULL; 1290 struct timeval tv[10], delay; 1291 int total_diff = 0; 1292 1293 int flags = 0, wantres, acceptdiff, i; 1294 if (precise) 1295 flags |= EV_MONOT_PRECISE; 1296 if (fallback) 1297 flags |= EV_MONOT_FALLBACK; 1298 if (precise || fallback) { 1299 #ifdef _WIN32 1300 wantres = 10*1000; 1301 acceptdiff = 1000; 1302 #else 1303 wantres = 1000; 1304 acceptdiff = 300; 1305 #endif 1306 } else { 1307 wantres = 40*1000; 1308 acceptdiff = 20*1000; 1309 } 1310 1311 TT_BLATHER(("Precise = %d", precise)); 1312 TT_BLATHER(("Fallback = %d", fallback)); 1313 1314 /* First, make sure we match up with usleep. */ 1315 1316 delay.tv_sec = 0; 1317 delay.tv_usec = wantres; 1318 1319 tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0); 1320 1321 for (i = 0; i < 10; ++i) { 1322 evutil_gettime_monotonic_(&timer, &tv[i]); 1323 evutil_usleep_(&delay); 1324 } 1325 1326 for (i = 0; i < 9; ++i) { 1327 struct timeval diff; 1328 tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <)); 1329 evutil_timersub(&tv[i+1], &tv[i], &diff); 1330 tt_int_op(diff.tv_sec, ==, 0); 1331 total_diff += diff.tv_usec; 1332 TT_BLATHER(("Difference = %d", (int)diff.tv_usec)); 1333 } 1334 tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff); 1335 1336 end: 1337 ; 1338 } 1339 1340 static void 1341 test_evutil_monotonic_prc(void *data_) 1342 { 1343 struct basic_test_data *data = data_; 1344 struct evutil_monotonic_timer timer; 1345 const int precise = strstr(data->setup_data, "precise") != NULL; 1346 const int fallback = strstr(data->setup_data, "fallback") != NULL; 1347 struct timeval tv[10]; 1348 int total_diff = 0; 1349 int i, maxstep = 25*1000,flags=0; 1350 if (precise) 1351 maxstep = 500; 1352 if (precise) 1353 flags |= EV_MONOT_PRECISE; 1354 if (fallback) 1355 flags |= EV_MONOT_FALLBACK; 1356 tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0); 1357 1358 /* find out what precision we actually see. */ 1359 1360 evutil_gettime_monotonic_(&timer, &tv[0]); 1361 for (i = 1; i < 10; ++i) { 1362 do { 1363 evutil_gettime_monotonic_(&timer, &tv[i]); 1364 } while (evutil_timercmp(&tv[i-1], &tv[i], ==)); 1365 } 1366 1367 total_diff = 0; 1368 for (i = 0; i < 9; ++i) { 1369 struct timeval diff; 1370 tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <)); 1371 evutil_timersub(&tv[i+1], &tv[i], &diff); 1372 tt_int_op(diff.tv_sec, ==, 0); 1373 total_diff += diff.tv_usec; 1374 TT_BLATHER(("Step difference = %d", (int)diff.tv_usec)); 1375 } 1376 TT_BLATHER(("Average step difference = %d", total_diff / 9)); 1377 tt_int_op(total_diff/9, <, maxstep); 1378 1379 end: 1380 ; 1381 } 1382 1383 struct testcase_t util_testcases[] = { 1384 { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, 1385 { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, 1386 { "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL }, 1387 { "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL }, 1388 { "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL }, 1389 { "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL }, 1390 { "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL }, 1391 { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL }, 1392 { "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL }, 1393 { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL }, 1394 { "log", test_evutil_log, TT_FORK, NULL, NULL }, 1395 { "upcast", test_evutil_upcast, 0, NULL, NULL }, 1396 { "integers", test_evutil_integers, 0, NULL, NULL }, 1397 { "rand", test_evutil_rand, TT_FORK, NULL, NULL }, 1398 { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL }, 1399 { "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL }, 1400 #ifdef _WIN32 1401 { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL }, 1402 #endif 1403 { "mm_malloc", test_event_malloc, 0, NULL, NULL }, 1404 { "mm_calloc", test_event_calloc, 0, NULL, NULL }, 1405 { "mm_strdup", test_event_strdup, 0, NULL, NULL }, 1406 { "usleep", test_evutil_usleep, 0, NULL, NULL }, 1407 { "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" }, 1408 { "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" }, 1409 { "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" }, 1410 { "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" }, 1411 { "monotonic_prc_precise", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"precise" }, 1412 { "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" }, 1413 END_OF_TESTCASES, 1414 }; 1415 1416