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 28#ifdef WIN32 29#include <winsock2.h> 30#include <windows.h> 31#endif 32 33#include "event2/event-config.h" 34 35#include <sys/types.h> 36#include <sys/stat.h> 37#ifdef _EVENT_HAVE_SYS_TIME_H 38#include <sys/time.h> 39#endif 40#include <sys/queue.h> 41#ifndef WIN32 42#include <sys/socket.h> 43#include <sys/wait.h> 44#include <signal.h> 45#include <unistd.h> 46#include <netdb.h> 47#endif 48#include <fcntl.h> 49#include <signal.h> 50#include <stdlib.h> 51#include <stdio.h> 52#include <string.h> 53#include <errno.h> 54#include <assert.h> 55#include <ctype.h> 56 57#include "event2/event.h" 58#include "event2/event_struct.h" 59#include "event2/event_compat.h" 60#include "event2/tag.h" 61#include "event2/buffer.h" 62#include "event2/buffer_compat.h" 63#include "event2/util.h" 64#include "event-internal.h" 65#include "evthread-internal.h" 66#include "util-internal.h" 67#include "log-internal.h" 68 69#include "regress.h" 70 71#ifndef WIN32 72#include "regress.gen.h" 73#endif 74 75evutil_socket_t pair[2]; 76int test_ok; 77int called; 78struct event_base *global_base; 79 80static char wbuf[4096]; 81static char rbuf[4096]; 82static int woff; 83static int roff; 84static int usepersist; 85static struct timeval tset; 86static struct timeval tcalled; 87 88 89#define TEST1 "this is a test" 90#define SECONDS 1 91 92#ifndef SHUT_WR 93#define SHUT_WR 1 94#endif 95 96#ifdef WIN32 97#define write(fd,buf,len) send((fd),(buf),(int)(len),0) 98#define read(fd,buf,len) recv((fd),(buf),(int)(len),0) 99#endif 100 101struct basic_cb_args 102{ 103 struct event_base *eb; 104 struct event *ev; 105 unsigned int callcount; 106}; 107 108static void 109simple_read_cb(evutil_socket_t fd, short event, void *arg) 110{ 111 char buf[256]; 112 int len; 113 114 len = read(fd, buf, sizeof(buf)); 115 116 if (len) { 117 if (!called) { 118 if (event_add(arg, NULL) == -1) 119 exit(1); 120 } 121 } else if (called == 1) 122 test_ok = 1; 123 124 called++; 125} 126 127static void 128basic_read_cb(evutil_socket_t fd, short event, void *data) 129{ 130 char buf[256]; 131 int len; 132 struct basic_cb_args *arg = data; 133 134 len = read(fd, buf, sizeof(buf)); 135 136 if (len < 0) { 137 tt_fail_perror("read (callback)"); 138 } else { 139 switch (arg->callcount++) { 140 case 0: /* first call: expect to read data; cycle */ 141 if (len > 0) 142 return; 143 144 tt_fail_msg("EOF before data read"); 145 break; 146 147 case 1: /* second call: expect EOF; stop */ 148 if (len > 0) 149 tt_fail_msg("not all data read on first cycle"); 150 break; 151 152 default: /* third call: should not happen */ 153 tt_fail_msg("too many cycles"); 154 } 155 } 156 157 event_del(arg->ev); 158 event_base_loopexit(arg->eb, NULL); 159} 160 161static void 162dummy_read_cb(evutil_socket_t fd, short event, void *arg) 163{ 164} 165 166static void 167simple_write_cb(evutil_socket_t fd, short event, void *arg) 168{ 169 int len; 170 171 len = write(fd, TEST1, strlen(TEST1) + 1); 172 if (len == -1) 173 test_ok = 0; 174 else 175 test_ok = 1; 176} 177 178static void 179multiple_write_cb(evutil_socket_t fd, short event, void *arg) 180{ 181 struct event *ev = arg; 182 int len; 183 184 len = 128; 185 if (woff + len >= (int)sizeof(wbuf)) 186 len = sizeof(wbuf) - woff; 187 188 len = write(fd, wbuf + woff, len); 189 if (len == -1) { 190 fprintf(stderr, "%s: write\n", __func__); 191 if (usepersist) 192 event_del(ev); 193 return; 194 } 195 196 woff += len; 197 198 if (woff >= (int)sizeof(wbuf)) { 199 shutdown(fd, SHUT_WR); 200 if (usepersist) 201 event_del(ev); 202 return; 203 } 204 205 if (!usepersist) { 206 if (event_add(ev, NULL) == -1) 207 exit(1); 208 } 209} 210 211static void 212multiple_read_cb(evutil_socket_t fd, short event, void *arg) 213{ 214 struct event *ev = arg; 215 int len; 216 217 len = read(fd, rbuf + roff, sizeof(rbuf) - roff); 218 if (len == -1) 219 fprintf(stderr, "%s: read\n", __func__); 220 if (len <= 0) { 221 if (usepersist) 222 event_del(ev); 223 return; 224 } 225 226 roff += len; 227 if (!usepersist) { 228 if (event_add(ev, NULL) == -1) 229 exit(1); 230 } 231} 232 233static void 234timeout_cb(evutil_socket_t fd, short event, void *arg) 235{ 236 struct timeval tv; 237 int diff; 238 239 evutil_gettimeofday(&tcalled, NULL); 240 if (evutil_timercmp(&tcalled, &tset, >)) 241 evutil_timersub(&tcalled, &tset, &tv); 242 else 243 evutil_timersub(&tset, &tcalled, &tv); 244 245 diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000; 246 if (diff < 0) 247 diff = -diff; 248 249 if (diff < 100) 250 test_ok = 1; 251} 252 253struct both { 254 struct event ev; 255 int nread; 256}; 257 258static void 259combined_read_cb(evutil_socket_t fd, short event, void *arg) 260{ 261 struct both *both = arg; 262 char buf[128]; 263 int len; 264 265 len = read(fd, buf, sizeof(buf)); 266 if (len == -1) 267 fprintf(stderr, "%s: read\n", __func__); 268 if (len <= 0) 269 return; 270 271 both->nread += len; 272 if (event_add(&both->ev, NULL) == -1) 273 exit(1); 274} 275 276static void 277combined_write_cb(evutil_socket_t fd, short event, void *arg) 278{ 279 struct both *both = arg; 280 char buf[128]; 281 int len; 282 283 len = sizeof(buf); 284 if (len > both->nread) 285 len = both->nread; 286 287 memset(buf, 'q', len); 288 289 len = write(fd, buf, len); 290 if (len == -1) 291 fprintf(stderr, "%s: write\n", __func__); 292 if (len <= 0) { 293 shutdown(fd, SHUT_WR); 294 return; 295 } 296 297 both->nread -= len; 298 if (event_add(&both->ev, NULL) == -1) 299 exit(1); 300} 301 302/* These macros used to replicate the work of the legacy test wrapper code */ 303#define setup_test(x) do { \ 304 if (!in_legacy_test_wrapper) { \ 305 TT_FAIL(("Legacy test %s not wrapped properly", x)); \ 306 return; \ 307 } \ 308 } while (0) 309#define cleanup_test() setup_test("cleanup") 310 311static void 312test_simpleread(void) 313{ 314 struct event ev; 315 316 /* Very simple read test */ 317 setup_test("Simple read: "); 318 319 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 320 tt_fail_perror("write"); 321 } 322 323 shutdown(pair[0], SHUT_WR); 324 325 event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev); 326 if (event_add(&ev, NULL) == -1) 327 exit(1); 328 event_dispatch(); 329 330 cleanup_test(); 331} 332 333static void 334test_simplewrite(void) 335{ 336 struct event ev; 337 338 /* Very simple write test */ 339 setup_test("Simple write: "); 340 341 event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev); 342 if (event_add(&ev, NULL) == -1) 343 exit(1); 344 event_dispatch(); 345 346 cleanup_test(); 347} 348 349static void 350simpleread_multiple_cb(evutil_socket_t fd, short event, void *arg) 351{ 352 if (++called == 2) 353 test_ok = 1; 354} 355 356static void 357test_simpleread_multiple(void) 358{ 359 struct event one, two; 360 361 /* Very simple read test */ 362 setup_test("Simple read to multiple evens: "); 363 364 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 365 tt_fail_perror("write"); 366 } 367 368 shutdown(pair[0], SHUT_WR); 369 370 event_set(&one, pair[1], EV_READ, simpleread_multiple_cb, NULL); 371 if (event_add(&one, NULL) == -1) 372 exit(1); 373 event_set(&two, pair[1], EV_READ, simpleread_multiple_cb, NULL); 374 if (event_add(&two, NULL) == -1) 375 exit(1); 376 event_dispatch(); 377 378 cleanup_test(); 379} 380 381static int have_closed = 0; 382static int premature_event = 0; 383static void 384simpleclose_close_fd_cb(evutil_socket_t s, short what, void *ptr) 385{ 386 evutil_socket_t **fds = ptr; 387 TT_BLATHER(("Closing")); 388 evutil_closesocket(*fds[0]); 389 evutil_closesocket(*fds[1]); 390 *fds[0] = -1; 391 *fds[1] = -1; 392 have_closed = 1; 393} 394 395static void 396record_event_cb(evutil_socket_t s, short what, void *ptr) 397{ 398 short *whatp = ptr; 399 if (!have_closed) 400 premature_event = 1; 401 *whatp = what; 402 TT_BLATHER(("Recorded %d on socket %d", (int)what, (int)s)); 403} 404 405static void 406test_simpleclose(void *ptr) 407{ 408 /* Test that a close of FD is detected as a read and as a write. */ 409 struct event_base *base = event_base_new(); 410 evutil_socket_t pair1[2]={-1,-1}, pair2[2] = {-1, -1}; 411 evutil_socket_t *to_close[2]; 412 struct event *rev=NULL, *wev=NULL, *closeev=NULL; 413 struct timeval tv; 414 short got_read_on_close = 0, got_write_on_close = 0; 415 char buf[1024]; 416 memset(buf, 99, sizeof(buf)); 417#ifdef WIN32 418#define LOCAL_SOCKETPAIR_AF AF_INET 419#else 420#define LOCAL_SOCKETPAIR_AF AF_UNIX 421#endif 422 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair1)<0) 423 TT_DIE(("socketpair: %s", strerror(errno))); 424 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair2)<0) 425 TT_DIE(("socketpair: %s", strerror(errno))); 426 if (evutil_make_socket_nonblocking(pair1[1]) < 0) 427 TT_DIE(("make_socket_nonblocking")); 428 if (evutil_make_socket_nonblocking(pair2[1]) < 0) 429 TT_DIE(("make_socket_nonblocking")); 430 431 /** Stuff pair2[1] full of data, until write fails */ 432 while (1) { 433 int r = write(pair2[1], buf, sizeof(buf)); 434 if (r<0) { 435 int err = evutil_socket_geterror(pair2[1]); 436 if (! EVUTIL_ERR_RW_RETRIABLE(err)) 437 TT_DIE(("write failed strangely: %s", 438 evutil_socket_error_to_string(err))); 439 break; 440 } 441 } 442 to_close[0] = &pair1[0]; 443 to_close[1] = &pair2[0]; 444 445 closeev = event_new(base, -1, EV_TIMEOUT, simpleclose_close_fd_cb, 446 to_close); 447 rev = event_new(base, pair1[1], EV_READ, record_event_cb, 448 &got_read_on_close); 449 TT_BLATHER(("Waiting for read on %d", (int)pair1[1])); 450 wev = event_new(base, pair2[1], EV_WRITE, record_event_cb, 451 &got_write_on_close); 452 TT_BLATHER(("Waiting for write on %d", (int)pair2[1])); 453 tv.tv_sec = 0; 454 tv.tv_usec = 100*1000; /* Close pair1[0] after a little while, and make 455 * sure we get a read event. */ 456 event_add(closeev, &tv); 457 event_add(rev, NULL); 458 event_add(wev, NULL); 459 /* Don't let the test go on too long. */ 460 tv.tv_sec = 0; 461 tv.tv_usec = 200*1000; 462 event_base_loopexit(base, &tv); 463 event_base_loop(base, 0); 464 465 tt_int_op(got_read_on_close, ==, EV_READ); 466 tt_int_op(got_write_on_close, ==, EV_WRITE); 467 tt_int_op(premature_event, ==, 0); 468 469end: 470 if (pair1[0] >= 0) 471 evutil_closesocket(pair1[0]); 472 if (pair1[1] >= 0) 473 evutil_closesocket(pair1[1]); 474 if (pair2[0] >= 0) 475 evutil_closesocket(pair2[0]); 476 if (pair2[1] >= 0) 477 evutil_closesocket(pair2[1]); 478 if (rev) 479 event_free(rev); 480 if (wev) 481 event_free(wev); 482 if (closeev) 483 event_free(closeev); 484 if (base) 485 event_base_free(base); 486} 487 488 489static void 490test_multiple(void) 491{ 492 struct event ev, ev2; 493 int i; 494 495 /* Multiple read and write test */ 496 setup_test("Multiple read/write: "); 497 memset(rbuf, 0, sizeof(rbuf)); 498 for (i = 0; i < (int)sizeof(wbuf); i++) 499 wbuf[i] = i; 500 501 roff = woff = 0; 502 usepersist = 0; 503 504 event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev); 505 if (event_add(&ev, NULL) == -1) 506 exit(1); 507 event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2); 508 if (event_add(&ev2, NULL) == -1) 509 exit(1); 510 event_dispatch(); 511 512 if (roff == woff) 513 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0; 514 515 cleanup_test(); 516} 517 518static void 519test_persistent(void) 520{ 521 struct event ev, ev2; 522 int i; 523 524 /* Multiple read and write test with persist */ 525 setup_test("Persist read/write: "); 526 memset(rbuf, 0, sizeof(rbuf)); 527 for (i = 0; i < (int)sizeof(wbuf); i++) 528 wbuf[i] = i; 529 530 roff = woff = 0; 531 usepersist = 1; 532 533 event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev); 534 if (event_add(&ev, NULL) == -1) 535 exit(1); 536 event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2); 537 if (event_add(&ev2, NULL) == -1) 538 exit(1); 539 event_dispatch(); 540 541 if (roff == woff) 542 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0; 543 544 cleanup_test(); 545} 546 547static void 548test_combined(void) 549{ 550 struct both r1, r2, w1, w2; 551 552 setup_test("Combined read/write: "); 553 memset(&r1, 0, sizeof(r1)); 554 memset(&r2, 0, sizeof(r2)); 555 memset(&w1, 0, sizeof(w1)); 556 memset(&w2, 0, sizeof(w2)); 557 558 w1.nread = 4096; 559 w2.nread = 8192; 560 561 event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1); 562 event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1); 563 event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2); 564 event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2); 565 tt_assert(event_add(&r1.ev, NULL) != -1); 566 tt_assert(!event_add(&w1.ev, NULL)); 567 tt_assert(!event_add(&r2.ev, NULL)); 568 tt_assert(!event_add(&w2.ev, NULL)); 569 event_dispatch(); 570 571 if (r1.nread == 8192 && r2.nread == 4096) 572 test_ok = 1; 573 574end: 575 cleanup_test(); 576} 577 578static void 579test_simpletimeout(void) 580{ 581 struct timeval tv; 582 struct event ev; 583 584 setup_test("Simple timeout: "); 585 586 tv.tv_usec = 0; 587 tv.tv_sec = SECONDS; 588 evtimer_set(&ev, timeout_cb, NULL); 589 evtimer_add(&ev, &tv); 590 591 evutil_gettimeofday(&tset, NULL); 592 event_dispatch(); 593 594 cleanup_test(); 595} 596 597static void 598periodic_timeout_cb(evutil_socket_t fd, short event, void *arg) 599{ 600 int *count = arg; 601 602 (*count)++; 603 if (*count == 6) { 604 /* call loopexit only once - on slow machines(?), it is 605 * apparently possible for this to get called twice. */ 606 test_ok = 1; 607 event_base_loopexit(global_base, NULL); 608 } 609} 610 611static void 612test_persistent_timeout(void) 613{ 614 struct timeval tv; 615 struct event ev; 616 int count = 0; 617 618 evutil_timerclear(&tv); 619 tv.tv_usec = 10000; 620 621 event_assign(&ev, global_base, -1, EV_TIMEOUT|EV_PERSIST, 622 periodic_timeout_cb, &count); 623 event_add(&ev, &tv); 624 625 event_dispatch(); 626 627 event_del(&ev); 628} 629 630static void 631test_persistent_timeout_jump(void *ptr) 632{ 633 struct basic_test_data *data = ptr; 634 struct event ev; 635 int count = 0; 636 struct timeval msec100 = { 0, 100 * 1000 }; 637 struct timeval msec50 = { 0, 50 * 1000 }; 638 639 event_assign(&ev, data->base, -1, EV_PERSIST, periodic_timeout_cb, &count); 640 event_add(&ev, &msec100); 641 /* Wait for a bit */ 642#ifdef _WIN32 643 Sleep(1000); 644#else 645 sleep(1); 646#endif 647 event_base_loopexit(data->base, &msec50); 648 event_base_dispatch(data->base); 649 tt_int_op(count, ==, 1); 650 651end: 652 event_del(&ev); 653} 654 655struct persist_active_timeout_called { 656 int n; 657 short events[16]; 658 struct timeval tvs[16]; 659}; 660 661static void 662activate_cb(evutil_socket_t fd, short event, void *arg) 663{ 664 struct event *ev = arg; 665 event_active(ev, EV_READ, 1); 666} 667 668static void 669persist_active_timeout_cb(evutil_socket_t fd, short event, void *arg) 670{ 671 struct persist_active_timeout_called *c = arg; 672 if (c->n < 15) { 673 c->events[c->n] = event; 674 evutil_gettimeofday(&c->tvs[c->n], NULL); 675 ++c->n; 676 } 677} 678 679static void 680test_persistent_active_timeout(void *ptr) 681{ 682 struct timeval tv, tv2, tv_exit, start; 683 struct event ev; 684 struct persist_active_timeout_called res; 685 686 struct basic_test_data *data = ptr; 687 struct event_base *base = data->base; 688 689 memset(&res, 0, sizeof(res)); 690 691 tv.tv_sec = 0; 692 tv.tv_usec = 200 * 1000; 693 event_assign(&ev, base, -1, EV_TIMEOUT|EV_PERSIST, 694 persist_active_timeout_cb, &res); 695 event_add(&ev, &tv); 696 697 tv2.tv_sec = 0; 698 tv2.tv_usec = 100 * 1000; 699 event_base_once(base, -1, EV_TIMEOUT, activate_cb, &ev, &tv2); 700 701 tv_exit.tv_sec = 0; 702 tv_exit.tv_usec = 600 * 1000; 703 event_base_loopexit(base, &tv_exit); 704 705 event_base_assert_ok(base); 706 evutil_gettimeofday(&start, NULL); 707 708 event_base_dispatch(base); 709 event_base_assert_ok(base); 710 711 tt_int_op(res.n, ==, 3); 712 tt_int_op(res.events[0], ==, EV_READ); 713 tt_int_op(res.events[1], ==, EV_TIMEOUT); 714 tt_int_op(res.events[2], ==, EV_TIMEOUT); 715 test_timeval_diff_eq(&start, &res.tvs[0], 100); 716 test_timeval_diff_eq(&start, &res.tvs[1], 300); 717 test_timeval_diff_eq(&start, &res.tvs[2], 500); 718end: 719 event_del(&ev); 720} 721 722struct common_timeout_info { 723 struct event ev; 724 struct timeval called_at; 725 int which; 726 int count; 727}; 728 729static void 730common_timeout_cb(evutil_socket_t fd, short event, void *arg) 731{ 732 struct common_timeout_info *ti = arg; 733 ++ti->count; 734 evutil_gettimeofday(&ti->called_at, NULL); 735 if (ti->count >= 6) 736 event_del(&ti->ev); 737} 738 739static void 740test_common_timeout(void *ptr) 741{ 742 struct basic_test_data *data = ptr; 743 744 struct event_base *base = data->base; 745 int i; 746 struct common_timeout_info info[100]; 747 748 struct timeval now; 749 struct timeval tmp_100_ms = { 0, 100*1000 }; 750 struct timeval tmp_200_ms = { 0, 200*1000 }; 751 752 const struct timeval *ms_100, *ms_200; 753 754 ms_100 = event_base_init_common_timeout(base, &tmp_100_ms); 755 ms_200 = event_base_init_common_timeout(base, &tmp_200_ms); 756 tt_assert(ms_100); 757 tt_assert(ms_200); 758 tt_ptr_op(event_base_init_common_timeout(base, &tmp_200_ms), 759 ==, ms_200); 760 tt_int_op(ms_100->tv_sec, ==, 0); 761 tt_int_op(ms_200->tv_sec, ==, 0); 762 tt_int_op(ms_100->tv_usec, ==, 100000|0x50000000); 763 tt_int_op(ms_200->tv_usec, ==, 200000|0x50100000); 764 765 memset(info, 0, sizeof(info)); 766 767 for (i=0; i<100; ++i) { 768 info[i].which = i; 769 event_assign(&info[i].ev, base, -1, EV_TIMEOUT|EV_PERSIST, 770 common_timeout_cb, &info[i]); 771 if (i % 2) { 772 event_add(&info[i].ev, ms_100); 773 } else { 774 event_add(&info[i].ev, ms_200); 775 } 776 } 777 778 event_base_assert_ok(base); 779 event_base_dispatch(base); 780 781 evutil_gettimeofday(&now, NULL); 782 event_base_assert_ok(base); 783 784 for (i=0; i<10; ++i) { 785 struct timeval tmp; 786 int ms_diff; 787 tt_int_op(info[i].count, ==, 6); 788 evutil_timersub(&now, &info[i].called_at, &tmp); 789 ms_diff = tmp.tv_usec/1000 + tmp.tv_sec*1000; 790 if (i % 2) { 791 tt_int_op(ms_diff, >, 500); 792 tt_int_op(ms_diff, <, 700); 793 } else { 794 tt_int_op(ms_diff, >, -100); 795 tt_int_op(ms_diff, <, 100); 796 } 797 } 798 799 /* Make sure we can free the base with some events in. */ 800 for (i=0; i<100; ++i) { 801 if (i % 2) { 802 event_add(&info[i].ev, ms_100); 803 } else { 804 event_add(&info[i].ev, ms_200); 805 } 806 } 807 808end: 809 event_base_free(data->base); /* need to do this here before info is 810 * out-of-scope */ 811 data->base = NULL; 812} 813 814#ifndef WIN32 815static void signal_cb(evutil_socket_t fd, short event, void *arg); 816 817#define current_base event_global_current_base_ 818extern struct event_base *current_base; 819 820static void 821child_signal_cb(evutil_socket_t fd, short event, void *arg) 822{ 823 struct timeval tv; 824 int *pint = arg; 825 826 *pint = 1; 827 828 tv.tv_usec = 500000; 829 tv.tv_sec = 0; 830 event_loopexit(&tv); 831} 832 833static void 834test_fork(void) 835{ 836 int status, got_sigchld = 0; 837 struct event ev, sig_ev; 838 pid_t pid; 839 840 setup_test("After fork: "); 841 842 tt_assert(current_base); 843 evthread_make_base_notifiable(current_base); 844 845 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 846 tt_fail_perror("write"); 847 } 848 849 event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev); 850 if (event_add(&ev, NULL) == -1) 851 exit(1); 852 853 evsignal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld); 854 evsignal_add(&sig_ev, NULL); 855 856 event_base_assert_ok(current_base); 857 TT_BLATHER(("Before fork")); 858 if ((pid = regress_fork()) == 0) { 859 /* in the child */ 860 TT_BLATHER(("In child, before reinit")); 861 event_base_assert_ok(current_base); 862 if (event_reinit(current_base) == -1) { 863 fprintf(stdout, "FAILED (reinit)\n"); 864 exit(1); 865 } 866 TT_BLATHER(("After reinit")); 867 event_base_assert_ok(current_base); 868 TT_BLATHER(("After assert-ok")); 869 870 evsignal_del(&sig_ev); 871 872 called = 0; 873 874 event_dispatch(); 875 876 event_base_free(current_base); 877 878 /* we do not send an EOF; simple_read_cb requires an EOF 879 * to set test_ok. we just verify that the callback was 880 * called. */ 881 exit(test_ok != 0 || called != 2 ? -2 : 76); 882 } 883 884 /* wait for the child to read the data */ 885 sleep(1); 886 887 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 888 tt_fail_perror("write"); 889 } 890 891 TT_BLATHER(("Before waitpid")); 892 if (waitpid(pid, &status, 0) == -1) { 893 fprintf(stdout, "FAILED (fork)\n"); 894 exit(1); 895 } 896 TT_BLATHER(("After waitpid")); 897 898 if (WEXITSTATUS(status) != 76) { 899 fprintf(stdout, "FAILED (exit): %d\n", WEXITSTATUS(status)); 900 exit(1); 901 } 902 903 /* test that the current event loop still works */ 904 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 905 fprintf(stderr, "%s: write\n", __func__); 906 } 907 908 shutdown(pair[0], SHUT_WR); 909 910 event_dispatch(); 911 912 if (!got_sigchld) { 913 fprintf(stdout, "FAILED (sigchld)\n"); 914 exit(1); 915 } 916 917 evsignal_del(&sig_ev); 918 919 end: 920 cleanup_test(); 921} 922 923static void 924signal_cb_sa(int sig) 925{ 926 test_ok = 2; 927} 928 929static void 930signal_cb(evutil_socket_t fd, short event, void *arg) 931{ 932 struct event *ev = arg; 933 934 evsignal_del(ev); 935 test_ok = 1; 936} 937 938static void 939test_simplesignal(void) 940{ 941 struct event ev; 942 struct itimerval itv; 943 944 setup_test("Simple signal: "); 945 evsignal_set(&ev, SIGALRM, signal_cb, &ev); 946 evsignal_add(&ev, NULL); 947 /* find bugs in which operations are re-ordered */ 948 evsignal_del(&ev); 949 evsignal_add(&ev, NULL); 950 951 memset(&itv, 0, sizeof(itv)); 952 itv.it_value.tv_sec = 1; 953 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) 954 goto skip_simplesignal; 955 956 event_dispatch(); 957 skip_simplesignal: 958 if (evsignal_del(&ev) == -1) 959 test_ok = 0; 960 961 cleanup_test(); 962} 963 964static void 965test_multiplesignal(void) 966{ 967 struct event ev_one, ev_two; 968 struct itimerval itv; 969 970 setup_test("Multiple signal: "); 971 972 evsignal_set(&ev_one, SIGALRM, signal_cb, &ev_one); 973 evsignal_add(&ev_one, NULL); 974 975 evsignal_set(&ev_two, SIGALRM, signal_cb, &ev_two); 976 evsignal_add(&ev_two, NULL); 977 978 memset(&itv, 0, sizeof(itv)); 979 itv.it_value.tv_sec = 1; 980 if (setitimer(ITIMER_REAL, &itv, NULL) == -1) 981 goto skip_simplesignal; 982 983 event_dispatch(); 984 985 skip_simplesignal: 986 if (evsignal_del(&ev_one) == -1) 987 test_ok = 0; 988 if (evsignal_del(&ev_two) == -1) 989 test_ok = 0; 990 991 cleanup_test(); 992} 993 994static void 995test_immediatesignal(void) 996{ 997 struct event ev; 998 999 test_ok = 0; 1000 evsignal_set(&ev, SIGUSR1, signal_cb, &ev); 1001 evsignal_add(&ev, NULL); 1002 raise(SIGUSR1); 1003 event_loop(EVLOOP_NONBLOCK); 1004 evsignal_del(&ev); 1005 cleanup_test(); 1006} 1007 1008static void 1009test_signal_dealloc(void) 1010{ 1011 /* make sure that evsignal_event is event_del'ed and pipe closed */ 1012 struct event ev; 1013 struct event_base *base = event_init(); 1014 evsignal_set(&ev, SIGUSR1, signal_cb, &ev); 1015 evsignal_add(&ev, NULL); 1016 evsignal_del(&ev); 1017 event_base_free(base); 1018 /* If we got here without asserting, we're fine. */ 1019 test_ok = 1; 1020 cleanup_test(); 1021} 1022 1023static void 1024test_signal_pipeloss(void) 1025{ 1026 /* make sure that the base1 pipe is closed correctly. */ 1027 struct event_base *base1, *base2; 1028 int pipe1; 1029 test_ok = 0; 1030 base1 = event_init(); 1031 pipe1 = base1->sig.ev_signal_pair[0]; 1032 base2 = event_init(); 1033 event_base_free(base2); 1034 event_base_free(base1); 1035 if (close(pipe1) != -1 || errno!=EBADF) { 1036 /* fd must be closed, so second close gives -1, EBADF */ 1037 printf("signal pipe not closed. "); 1038 test_ok = 0; 1039 } else { 1040 test_ok = 1; 1041 } 1042 cleanup_test(); 1043} 1044 1045/* 1046 * make two bases to catch signals, use both of them. this only works 1047 * for event mechanisms that use our signal pipe trick. kqueue handles 1048 * signals internally, and all interested kqueues get all the signals. 1049 */ 1050static void 1051test_signal_switchbase(void) 1052{ 1053 struct event ev1, ev2; 1054 struct event_base *base1, *base2; 1055 int is_kqueue; 1056 test_ok = 0; 1057 base1 = event_init(); 1058 base2 = event_init(); 1059 is_kqueue = !strcmp(event_get_method(),"kqueue"); 1060 evsignal_set(&ev1, SIGUSR1, signal_cb, &ev1); 1061 evsignal_set(&ev2, SIGUSR1, signal_cb, &ev2); 1062 if (event_base_set(base1, &ev1) || 1063 event_base_set(base2, &ev2) || 1064 event_add(&ev1, NULL) || 1065 event_add(&ev2, NULL)) { 1066 fprintf(stderr, "%s: cannot set base, add\n", __func__); 1067 exit(1); 1068 } 1069 1070 tt_ptr_op(event_get_base(&ev1), ==, base1); 1071 tt_ptr_op(event_get_base(&ev2), ==, base2); 1072 1073 test_ok = 0; 1074 /* can handle signal before loop is called */ 1075 raise(SIGUSR1); 1076 event_base_loop(base2, EVLOOP_NONBLOCK); 1077 if (is_kqueue) { 1078 if (!test_ok) 1079 goto end; 1080 test_ok = 0; 1081 } 1082 event_base_loop(base1, EVLOOP_NONBLOCK); 1083 if (test_ok && !is_kqueue) { 1084 test_ok = 0; 1085 1086 /* set base1 to handle signals */ 1087 event_base_loop(base1, EVLOOP_NONBLOCK); 1088 raise(SIGUSR1); 1089 event_base_loop(base1, EVLOOP_NONBLOCK); 1090 event_base_loop(base2, EVLOOP_NONBLOCK); 1091 } 1092end: 1093 event_base_free(base1); 1094 event_base_free(base2); 1095 cleanup_test(); 1096} 1097 1098/* 1099 * assert that a signal event removed from the event queue really is 1100 * removed - with no possibility of it's parent handler being fired. 1101 */ 1102static void 1103test_signal_assert(void) 1104{ 1105 struct event ev; 1106 struct event_base *base = event_init(); 1107 test_ok = 0; 1108 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */ 1109 evsignal_set(&ev, SIGCONT, signal_cb, &ev); 1110 evsignal_add(&ev, NULL); 1111 /* 1112 * if evsignal_del() fails to reset the handler, it's current handler 1113 * will still point to evsig_handler(). 1114 */ 1115 evsignal_del(&ev); 1116 1117 raise(SIGCONT); 1118#if 0 1119 /* only way to verify we were in evsig_handler() */ 1120 /* XXXX Now there's no longer a good way. */ 1121 if (base->sig.evsig_caught) 1122 test_ok = 0; 1123 else 1124 test_ok = 1; 1125#else 1126 test_ok = 1; 1127#endif 1128 1129 event_base_free(base); 1130 cleanup_test(); 1131 return; 1132} 1133 1134/* 1135 * assert that we restore our previous signal handler properly. 1136 */ 1137static void 1138test_signal_restore(void) 1139{ 1140 struct event ev; 1141 struct event_base *base = event_init(); 1142#ifdef _EVENT_HAVE_SIGACTION 1143 struct sigaction sa; 1144#endif 1145 1146 test_ok = 0; 1147#ifdef _EVENT_HAVE_SIGACTION 1148 sa.sa_handler = signal_cb_sa; 1149 sa.sa_flags = 0x0; 1150 sigemptyset(&sa.sa_mask); 1151 if (sigaction(SIGUSR1, &sa, NULL) == -1) 1152 goto out; 1153#else 1154 if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR) 1155 goto out; 1156#endif 1157 evsignal_set(&ev, SIGUSR1, signal_cb, &ev); 1158 evsignal_add(&ev, NULL); 1159 evsignal_del(&ev); 1160 1161 raise(SIGUSR1); 1162 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */ 1163 if (test_ok != 2) 1164 test_ok = 0; 1165out: 1166 event_base_free(base); 1167 cleanup_test(); 1168 return; 1169} 1170 1171static void 1172signal_cb_swp(int sig, short event, void *arg) 1173{ 1174 called++; 1175 if (called < 5) 1176 raise(sig); 1177 else 1178 event_loopexit(NULL); 1179} 1180static void 1181timeout_cb_swp(evutil_socket_t fd, short event, void *arg) 1182{ 1183 if (called == -1) { 1184 struct timeval tv = {5, 0}; 1185 1186 called = 0; 1187 evtimer_add((struct event *)arg, &tv); 1188 raise(SIGUSR1); 1189 return; 1190 } 1191 test_ok = 0; 1192 event_loopexit(NULL); 1193} 1194 1195static void 1196test_signal_while_processing(void) 1197{ 1198 struct event_base *base = event_init(); 1199 struct event ev, ev_timer; 1200 struct timeval tv = {0, 0}; 1201 1202 setup_test("Receiving a signal while processing other signal: "); 1203 1204 called = -1; 1205 test_ok = 1; 1206 signal_set(&ev, SIGUSR1, signal_cb_swp, NULL); 1207 signal_add(&ev, NULL); 1208 evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer); 1209 evtimer_add(&ev_timer, &tv); 1210 event_dispatch(); 1211 1212 event_base_free(base); 1213 cleanup_test(); 1214 return; 1215} 1216#endif 1217 1218static void 1219test_free_active_base(void *ptr) 1220{ 1221 struct basic_test_data *data = ptr; 1222 struct event_base *base1; 1223 struct event ev1; 1224 1225 base1 = event_init(); 1226 if (base1) { 1227 event_assign(&ev1, base1, data->pair[1], EV_READ, 1228 dummy_read_cb, NULL); 1229 event_add(&ev1, NULL); 1230 event_base_free(base1); /* should not crash */ 1231 } else { 1232 tt_fail_msg("failed to create event_base for test"); 1233 } 1234 1235 base1 = event_init(); 1236 tt_assert(base1); 1237 event_assign(&ev1, base1, 0, 0, dummy_read_cb, NULL); 1238 event_active(&ev1, EV_READ, 1); 1239 event_base_free(base1); 1240end: 1241 ; 1242} 1243 1244static void 1245test_manipulate_active_events(void *ptr) 1246{ 1247 struct basic_test_data *data = ptr; 1248 struct event_base *base = data->base; 1249 struct event ev1; 1250 1251 event_assign(&ev1, base, -1, EV_TIMEOUT, dummy_read_cb, NULL); 1252 1253 /* Make sure an active event is pending. */ 1254 event_active(&ev1, EV_READ, 1); 1255 tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL), 1256 ==, EV_READ); 1257 1258 /* Make sure that activating an event twice works. */ 1259 event_active(&ev1, EV_WRITE, 1); 1260 tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL), 1261 ==, EV_READ|EV_WRITE); 1262 1263end: 1264 event_del(&ev1); 1265} 1266 1267static void 1268test_bad_assign(void *ptr) 1269{ 1270 struct event ev; 1271 int r; 1272 /* READ|SIGNAL is not allowed */ 1273 r = event_assign(&ev, NULL, -1, EV_SIGNAL|EV_READ, dummy_read_cb, NULL); 1274 tt_int_op(r,==,-1); 1275 1276end: 1277 ; 1278} 1279 1280static int reentrant_cb_run = 0; 1281 1282static void 1283bad_reentrant_run_loop_cb(evutil_socket_t fd, short what, void *ptr) 1284{ 1285 struct event_base *base = ptr; 1286 int r; 1287 reentrant_cb_run = 1; 1288 /* This reentrant call to event_base_loop should be detected and 1289 * should fail */ 1290 r = event_base_loop(base, 0); 1291 tt_int_op(r, ==, -1); 1292end: 1293 ; 1294} 1295 1296static void 1297test_bad_reentrant(void *ptr) 1298{ 1299 struct basic_test_data *data = ptr; 1300 struct event_base *base = data->base; 1301 struct event ev; 1302 int r; 1303 event_assign(&ev, base, -1, 1304 0, bad_reentrant_run_loop_cb, base); 1305 1306 event_active(&ev, EV_WRITE, 1); 1307 r = event_base_loop(base, 0); 1308 tt_int_op(r, ==, 1); 1309 tt_int_op(reentrant_cb_run, ==, 1); 1310end: 1311 ; 1312} 1313 1314static void 1315test_event_base_new(void *ptr) 1316{ 1317 struct basic_test_data *data = ptr; 1318 struct event_base *base = 0; 1319 struct event ev1; 1320 struct basic_cb_args args; 1321 1322 int towrite = (int)strlen(TEST1)+1; 1323 int len = write(data->pair[0], TEST1, towrite); 1324 1325 if (len < 0) 1326 tt_abort_perror("initial write"); 1327 else if (len != towrite) 1328 tt_abort_printf(("initial write fell short (%d of %d bytes)", 1329 len, towrite)); 1330 1331 if (shutdown(data->pair[0], SHUT_WR)) 1332 tt_abort_perror("initial write shutdown"); 1333 1334 base = event_base_new(); 1335 if (!base) 1336 tt_abort_msg("failed to create event base"); 1337 1338 args.eb = base; 1339 args.ev = &ev1; 1340 args.callcount = 0; 1341 event_assign(&ev1, base, data->pair[1], 1342 EV_READ|EV_PERSIST, basic_read_cb, &args); 1343 1344 if (event_add(&ev1, NULL)) 1345 tt_abort_perror("initial event_add"); 1346 1347 if (event_base_loop(base, 0)) 1348 tt_abort_msg("unsuccessful exit from event loop"); 1349 1350end: 1351 if (base) 1352 event_base_free(base); 1353} 1354 1355static void 1356test_loopexit(void) 1357{ 1358 struct timeval tv, tv_start, tv_end; 1359 struct event ev; 1360 1361 setup_test("Loop exit: "); 1362 1363 tv.tv_usec = 0; 1364 tv.tv_sec = 60*60*24; 1365 evtimer_set(&ev, timeout_cb, NULL); 1366 evtimer_add(&ev, &tv); 1367 1368 tv.tv_usec = 0; 1369 tv.tv_sec = 1; 1370 event_loopexit(&tv); 1371 1372 evutil_gettimeofday(&tv_start, NULL); 1373 event_dispatch(); 1374 evutil_gettimeofday(&tv_end, NULL); 1375 evutil_timersub(&tv_end, &tv_start, &tv_end); 1376 1377 evtimer_del(&ev); 1378 1379 tt_assert(event_base_got_exit(global_base)); 1380 tt_assert(!event_base_got_break(global_base)); 1381 1382 if (tv.tv_sec < 2) 1383 test_ok = 1; 1384 1385end: 1386 cleanup_test(); 1387} 1388 1389static void 1390test_loopexit_multiple(void) 1391{ 1392 struct timeval tv; 1393 struct event_base *base; 1394 1395 setup_test("Loop Multiple exit: "); 1396 1397 base = event_base_new(); 1398 1399 tv.tv_usec = 0; 1400 tv.tv_sec = 1; 1401 event_base_loopexit(base, &tv); 1402 1403 tv.tv_usec = 0; 1404 tv.tv_sec = 2; 1405 event_base_loopexit(base, &tv); 1406 1407 event_base_dispatch(base); 1408 1409 tt_assert(event_base_got_exit(base)); 1410 tt_assert(!event_base_got_break(base)); 1411 1412 event_base_free(base); 1413 1414 test_ok = 1; 1415 1416end: 1417 cleanup_test(); 1418} 1419 1420static void 1421break_cb(evutil_socket_t fd, short events, void *arg) 1422{ 1423 test_ok = 1; 1424 event_loopbreak(); 1425} 1426 1427static void 1428fail_cb(evutil_socket_t fd, short events, void *arg) 1429{ 1430 test_ok = 0; 1431} 1432 1433static void 1434test_loopbreak(void) 1435{ 1436 struct event ev1, ev2; 1437 struct timeval tv; 1438 1439 setup_test("Loop break: "); 1440 1441 tv.tv_sec = 0; 1442 tv.tv_usec = 0; 1443 evtimer_set(&ev1, break_cb, NULL); 1444 evtimer_add(&ev1, &tv); 1445 evtimer_set(&ev2, fail_cb, NULL); 1446 evtimer_add(&ev2, &tv); 1447 1448 event_dispatch(); 1449 1450 tt_assert(!event_base_got_exit(global_base)); 1451 tt_assert(event_base_got_break(global_base)); 1452 1453 evtimer_del(&ev1); 1454 evtimer_del(&ev2); 1455 1456end: 1457 cleanup_test(); 1458} 1459 1460static struct event *readd_test_event_last_added = NULL; 1461static void 1462re_add_read_cb(evutil_socket_t fd, short event, void *arg) 1463{ 1464 char buf[256]; 1465 struct event *ev_other = arg; 1466 readd_test_event_last_added = ev_other; 1467 1468 if (read(fd, buf, sizeof(buf)) < 0) { 1469 tt_fail_perror("read"); 1470 } 1471 1472 event_add(ev_other, NULL); 1473 ++test_ok; 1474} 1475 1476static void 1477test_nonpersist_readd(void) 1478{ 1479 struct event ev1, ev2; 1480 1481 setup_test("Re-add nonpersistent events: "); 1482 event_set(&ev1, pair[0], EV_READ, re_add_read_cb, &ev2); 1483 event_set(&ev2, pair[1], EV_READ, re_add_read_cb, &ev1); 1484 1485 if (write(pair[0], "Hello", 5) < 0) { 1486 tt_fail_perror("write(pair[0])"); 1487 } 1488 1489 if (write(pair[1], "Hello", 5) < 0) { 1490 tt_fail_perror("write(pair[1])\n"); 1491 } 1492 1493 if (event_add(&ev1, NULL) == -1 || 1494 event_add(&ev2, NULL) == -1) { 1495 test_ok = 0; 1496 } 1497 if (test_ok != 0) 1498 exit(1); 1499 event_loop(EVLOOP_ONCE); 1500 if (test_ok != 2) 1501 exit(1); 1502 /* At this point, we executed both callbacks. Whichever one got 1503 * called first added the second, but the second then immediately got 1504 * deleted before its callback was called. At this point, though, it 1505 * re-added the first. 1506 */ 1507 if (!readd_test_event_last_added) { 1508 test_ok = 0; 1509 } else if (readd_test_event_last_added == &ev1) { 1510 if (!event_pending(&ev1, EV_READ, NULL) || 1511 event_pending(&ev2, EV_READ, NULL)) 1512 test_ok = 0; 1513 } else { 1514 if (event_pending(&ev1, EV_READ, NULL) || 1515 !event_pending(&ev2, EV_READ, NULL)) 1516 test_ok = 0; 1517 } 1518 1519 event_del(&ev1); 1520 event_del(&ev2); 1521 1522 cleanup_test(); 1523} 1524 1525struct test_pri_event { 1526 struct event ev; 1527 int count; 1528}; 1529 1530static void 1531test_priorities_cb(evutil_socket_t fd, short what, void *arg) 1532{ 1533 struct test_pri_event *pri = arg; 1534 struct timeval tv; 1535 1536 if (pri->count == 3) { 1537 event_loopexit(NULL); 1538 return; 1539 } 1540 1541 pri->count++; 1542 1543 evutil_timerclear(&tv); 1544 event_add(&pri->ev, &tv); 1545} 1546 1547static void 1548test_priorities_impl(int npriorities) 1549{ 1550 struct test_pri_event one, two; 1551 struct timeval tv; 1552 1553 TT_BLATHER(("Testing Priorities %d: ", npriorities)); 1554 1555 event_base_priority_init(global_base, npriorities); 1556 1557 memset(&one, 0, sizeof(one)); 1558 memset(&two, 0, sizeof(two)); 1559 1560 timeout_set(&one.ev, test_priorities_cb, &one); 1561 if (event_priority_set(&one.ev, 0) == -1) { 1562 fprintf(stderr, "%s: failed to set priority", __func__); 1563 exit(1); 1564 } 1565 1566 timeout_set(&two.ev, test_priorities_cb, &two); 1567 if (event_priority_set(&two.ev, npriorities - 1) == -1) { 1568 fprintf(stderr, "%s: failed to set priority", __func__); 1569 exit(1); 1570 } 1571 1572 evutil_timerclear(&tv); 1573 1574 if (event_add(&one.ev, &tv) == -1) 1575 exit(1); 1576 if (event_add(&two.ev, &tv) == -1) 1577 exit(1); 1578 1579 event_dispatch(); 1580 1581 event_del(&one.ev); 1582 event_del(&two.ev); 1583 1584 if (npriorities == 1) { 1585 if (one.count == 3 && two.count == 3) 1586 test_ok = 1; 1587 } else if (npriorities == 2) { 1588 /* Two is called once because event_loopexit is priority 1 */ 1589 if (one.count == 3 && two.count == 1) 1590 test_ok = 1; 1591 } else { 1592 if (one.count == 3 && two.count == 0) 1593 test_ok = 1; 1594 } 1595} 1596 1597static void 1598test_priorities(void) 1599{ 1600 test_priorities_impl(1); 1601 if (test_ok) 1602 test_priorities_impl(2); 1603 if (test_ok) 1604 test_priorities_impl(3); 1605} 1606 1607/* priority-active-inversion: activate a higher-priority event, and make sure 1608 * it keeps us from running a lower-priority event first. */ 1609static int n_pai_calls = 0; 1610static struct event pai_events[3]; 1611 1612static void 1613prio_active_inversion_cb(evutil_socket_t fd, short what, void *arg) 1614{ 1615 int *call_order = arg; 1616 *call_order = n_pai_calls++; 1617 if (n_pai_calls == 1) { 1618 /* This should activate later, even though it shares a 1619 priority with us. */ 1620 event_active(&pai_events[1], EV_READ, 1); 1621 /* This should activate next, since its priority is higher, 1622 even though we activated it second. */ 1623 event_active(&pai_events[2], EV_TIMEOUT, 1); 1624 } 1625} 1626 1627static void 1628test_priority_active_inversion(void *data_) 1629{ 1630 struct basic_test_data *data = data_; 1631 struct event_base *base = data->base; 1632 int call_order[3]; 1633 int i; 1634 tt_int_op(event_base_priority_init(base, 8), ==, 0); 1635 1636 n_pai_calls = 0; 1637 memset(call_order, 0, sizeof(call_order)); 1638 1639 for (i=0;i<3;++i) { 1640 event_assign(&pai_events[i], data->base, -1, 0, 1641 prio_active_inversion_cb, &call_order[i]); 1642 } 1643 1644 event_priority_set(&pai_events[0], 4); 1645 event_priority_set(&pai_events[1], 4); 1646 event_priority_set(&pai_events[2], 0); 1647 1648 event_active(&pai_events[0], EV_WRITE, 1); 1649 1650 event_base_dispatch(base); 1651 tt_int_op(n_pai_calls, ==, 3); 1652 tt_int_op(call_order[0], ==, 0); 1653 tt_int_op(call_order[1], ==, 2); 1654 tt_int_op(call_order[2], ==, 1); 1655end: 1656 ; 1657} 1658 1659 1660static void 1661test_multiple_cb(evutil_socket_t fd, short event, void *arg) 1662{ 1663 if (event & EV_READ) 1664 test_ok |= 1; 1665 else if (event & EV_WRITE) 1666 test_ok |= 2; 1667} 1668 1669static void 1670test_multiple_events_for_same_fd(void) 1671{ 1672 struct event e1, e2; 1673 1674 setup_test("Multiple events for same fd: "); 1675 1676 event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL); 1677 event_add(&e1, NULL); 1678 event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL); 1679 event_add(&e2, NULL); 1680 event_loop(EVLOOP_ONCE); 1681 event_del(&e2); 1682 1683 if (write(pair[1], TEST1, strlen(TEST1)+1) < 0) { 1684 tt_fail_perror("write"); 1685 } 1686 1687 event_loop(EVLOOP_ONCE); 1688 event_del(&e1); 1689 1690 if (test_ok != 3) 1691 test_ok = 0; 1692 1693 cleanup_test(); 1694} 1695 1696int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf); 1697int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf); 1698int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t number); 1699int evtag_decode_tag(ev_uint32_t *pnumber, struct evbuffer *evbuf); 1700 1701static void 1702read_once_cb(evutil_socket_t fd, short event, void *arg) 1703{ 1704 char buf[256]; 1705 int len; 1706 1707 len = read(fd, buf, sizeof(buf)); 1708 1709 if (called) { 1710 test_ok = 0; 1711 } else if (len) { 1712 /* Assumes global pair[0] can be used for writing */ 1713 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 1714 tt_fail_perror("write"); 1715 test_ok = 0; 1716 } else { 1717 test_ok = 1; 1718 } 1719 } 1720 1721 called++; 1722} 1723 1724static void 1725test_want_only_once(void) 1726{ 1727 struct event ev; 1728 struct timeval tv; 1729 1730 /* Very simple read test */ 1731 setup_test("Want read only once: "); 1732 1733 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) { 1734 tt_fail_perror("write"); 1735 } 1736 1737 /* Setup the loop termination */ 1738 evutil_timerclear(&tv); 1739 tv.tv_sec = 1; 1740 event_loopexit(&tv); 1741 1742 event_set(&ev, pair[1], EV_READ, read_once_cb, &ev); 1743 if (event_add(&ev, NULL) == -1) 1744 exit(1); 1745 event_dispatch(); 1746 1747 cleanup_test(); 1748} 1749 1750#define TEST_MAX_INT 6 1751 1752static void 1753evtag_int_test(void *ptr) 1754{ 1755 struct evbuffer *tmp = evbuffer_new(); 1756 ev_uint32_t integers[TEST_MAX_INT] = { 1757 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000 1758 }; 1759 ev_uint32_t integer; 1760 ev_uint64_t big_int; 1761 int i; 1762 1763 evtag_init(); 1764 1765 for (i = 0; i < TEST_MAX_INT; i++) { 1766 int oldlen, newlen; 1767 oldlen = (int)EVBUFFER_LENGTH(tmp); 1768 evtag_encode_int(tmp, integers[i]); 1769 newlen = (int)EVBUFFER_LENGTH(tmp); 1770 TT_BLATHER(("encoded 0x%08x with %d bytes", 1771 (unsigned)integers[i], newlen - oldlen)); 1772 big_int = integers[i]; 1773 big_int *= 1000000000; /* 1 billion */ 1774 evtag_encode_int64(tmp, big_int); 1775 } 1776 1777 for (i = 0; i < TEST_MAX_INT; i++) { 1778 tt_int_op(evtag_decode_int(&integer, tmp), !=, -1); 1779 tt_uint_op(integer, ==, integers[i]); 1780 tt_int_op(evtag_decode_int64(&big_int, tmp), !=, -1); 1781 tt_assert((big_int / 1000000000) == integers[i]); 1782 } 1783 1784 tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0); 1785end: 1786 evbuffer_free(tmp); 1787} 1788 1789static void 1790evtag_fuzz(void *ptr) 1791{ 1792 u_char buffer[4096]; 1793 struct evbuffer *tmp = evbuffer_new(); 1794 struct timeval tv; 1795 int i, j; 1796 1797 int not_failed = 0; 1798 1799 evtag_init(); 1800 1801 for (j = 0; j < 100; j++) { 1802 for (i = 0; i < (int)sizeof(buffer); i++) 1803 buffer[i] = rand(); 1804 evbuffer_drain(tmp, -1); 1805 evbuffer_add(tmp, buffer, sizeof(buffer)); 1806 1807 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) 1808 not_failed++; 1809 } 1810 1811 /* The majority of decodes should fail */ 1812 tt_int_op(not_failed, <, 10); 1813 1814 /* Now insert some corruption into the tag length field */ 1815 evbuffer_drain(tmp, -1); 1816 evutil_timerclear(&tv); 1817 tv.tv_sec = 1; 1818 evtag_marshal_timeval(tmp, 0, &tv); 1819 evbuffer_add(tmp, buffer, sizeof(buffer)); 1820 1821 ((char *)EVBUFFER_DATA(tmp))[1] = '\xff'; 1822 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) { 1823 tt_abort_msg("evtag_unmarshal_timeval should have failed"); 1824 } 1825 1826end: 1827 evbuffer_free(tmp); 1828} 1829 1830static void 1831evtag_tag_encoding(void *ptr) 1832{ 1833 struct evbuffer *tmp = evbuffer_new(); 1834 ev_uint32_t integers[TEST_MAX_INT] = { 1835 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000 1836 }; 1837 ev_uint32_t integer; 1838 int i; 1839 1840 evtag_init(); 1841 1842 for (i = 0; i < TEST_MAX_INT; i++) { 1843 int oldlen, newlen; 1844 oldlen = (int)EVBUFFER_LENGTH(tmp); 1845 evtag_encode_tag(tmp, integers[i]); 1846 newlen = (int)EVBUFFER_LENGTH(tmp); 1847 TT_BLATHER(("encoded 0x%08x with %d bytes", 1848 (unsigned)integers[i], newlen - oldlen)); 1849 } 1850 1851 for (i = 0; i < TEST_MAX_INT; i++) { 1852 tt_int_op(evtag_decode_tag(&integer, tmp), !=, -1); 1853 tt_uint_op(integer, ==, integers[i]); 1854 } 1855 1856 tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0); 1857 1858end: 1859 evbuffer_free(tmp); 1860} 1861 1862static void 1863evtag_test_peek(void *ptr) 1864{ 1865 struct evbuffer *tmp = evbuffer_new(); 1866 ev_uint32_t u32; 1867 1868 evtag_marshal_int(tmp, 30, 0); 1869 evtag_marshal_string(tmp, 40, "Hello world"); 1870 1871 tt_int_op(evtag_peek(tmp, &u32), ==, 1); 1872 tt_int_op(u32, ==, 30); 1873 tt_int_op(evtag_peek_length(tmp, &u32), ==, 0); 1874 tt_int_op(u32, ==, 1+1+1); 1875 tt_int_op(evtag_consume(tmp), ==, 0); 1876 1877 tt_int_op(evtag_peek(tmp, &u32), ==, 1); 1878 tt_int_op(u32, ==, 40); 1879 tt_int_op(evtag_peek_length(tmp, &u32), ==, 0); 1880 tt_int_op(u32, ==, 1+1+11); 1881 tt_int_op(evtag_payload_length(tmp, &u32), ==, 0); 1882 tt_int_op(u32, ==, 11); 1883 1884end: 1885 evbuffer_free(tmp); 1886} 1887 1888 1889static void 1890test_methods(void *ptr) 1891{ 1892 const char **methods = event_get_supported_methods(); 1893 struct event_config *cfg = NULL; 1894 struct event_base *base = NULL; 1895 const char *backend; 1896 int n_methods = 0; 1897 1898 tt_assert(methods); 1899 1900 backend = methods[0]; 1901 while (*methods != NULL) { 1902 TT_BLATHER(("Support method: %s", *methods)); 1903 ++methods; 1904 ++n_methods; 1905 } 1906 1907 cfg = event_config_new(); 1908 assert(cfg != NULL); 1909 1910 tt_int_op(event_config_avoid_method(cfg, backend), ==, 0); 1911 event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV); 1912 1913 base = event_base_new_with_config(cfg); 1914 if (n_methods > 1) { 1915 tt_assert(base); 1916 tt_str_op(backend, !=, event_base_get_method(base)); 1917 } else { 1918 tt_assert(base == NULL); 1919 } 1920 1921end: 1922 if (base) 1923 event_base_free(base); 1924 if (cfg) 1925 event_config_free(cfg); 1926} 1927 1928static void 1929test_version(void *arg) 1930{ 1931 const char *vstr; 1932 ev_uint32_t vint; 1933 int major, minor, patch, n; 1934 1935 vstr = event_get_version(); 1936 vint = event_get_version_number(); 1937 1938 tt_assert(vstr); 1939 tt_assert(vint); 1940 1941 tt_str_op(vstr, ==, LIBEVENT_VERSION); 1942 tt_int_op(vint, ==, LIBEVENT_VERSION_NUMBER); 1943 1944 n = sscanf(vstr, "%d.%d.%d", &major, &minor, &patch); 1945 tt_assert(3 == n); 1946 tt_int_op((vint&0xffffff00), ==, ((major<<24)|(minor<<16)|(patch<<8))); 1947end: 1948 ; 1949} 1950 1951static void 1952test_base_features(void *arg) 1953{ 1954 struct event_base *base = NULL; 1955 struct event_config *cfg = NULL; 1956 1957 cfg = event_config_new(); 1958 1959 tt_assert(0 == event_config_require_features(cfg, EV_FEATURE_ET)); 1960 1961 base = event_base_new_with_config(cfg); 1962 if (base) { 1963 tt_int_op(EV_FEATURE_ET, ==, 1964 event_base_get_features(base) & EV_FEATURE_ET); 1965 } else { 1966 base = event_base_new(); 1967 tt_int_op(0, ==, event_base_get_features(base) & EV_FEATURE_ET); 1968 } 1969 1970end: 1971 if (base) 1972 event_base_free(base); 1973 if (cfg) 1974 event_config_free(cfg); 1975} 1976 1977#ifdef _EVENT_HAVE_SETENV 1978#define SETENV_OK 1979#elif !defined(_EVENT_HAVE_SETENV) && defined(_EVENT_HAVE_PUTENV) 1980static void setenv(const char *k, const char *v, int _o) 1981{ 1982 char b[256]; 1983 evutil_snprintf(b, sizeof(b), "%s=%s",k,v); 1984 putenv(b); 1985} 1986#define SETENV_OK 1987#endif 1988 1989#ifdef _EVENT_HAVE_UNSETENV 1990#define UNSETENV_OK 1991#elif !defined(_EVENT_HAVE_UNSETENV) && defined(_EVENT_HAVE_PUTENV) 1992static void unsetenv(const char *k) 1993{ 1994 char b[256]; 1995 evutil_snprintf(b, sizeof(b), "%s=",k); 1996 putenv(b); 1997} 1998#define UNSETENV_OK 1999#endif 2000 2001#if defined(SETENV_OK) && defined(UNSETENV_OK) 2002static void 2003methodname_to_envvar(const char *mname, char *buf, size_t buflen) 2004{ 2005 char *cp; 2006 evutil_snprintf(buf, buflen, "EVENT_NO%s", mname); 2007 for (cp = buf; *cp; ++cp) { 2008 *cp = EVUTIL_TOUPPER(*cp); 2009 } 2010} 2011#endif 2012 2013static void 2014test_base_environ(void *arg) 2015{ 2016 struct event_base *base = NULL; 2017 struct event_config *cfg = NULL; 2018 2019#if defined(SETENV_OK) && defined(UNSETENV_OK) 2020 const char **basenames; 2021 int i, n_methods=0; 2022 char varbuf[128]; 2023 const char *defaultname, *ignoreenvname; 2024 2025 /* See if unsetenv works before we rely on it. */ 2026 setenv("EVENT_NOWAFFLES", "1", 1); 2027 unsetenv("EVENT_NOWAFFLES"); 2028 if (getenv("EVENT_NOWAFFLES") != NULL) { 2029#ifndef _EVENT_HAVE_UNSETENV 2030 TT_DECLARE("NOTE", ("Can't fake unsetenv; skipping test")); 2031#else 2032 TT_DECLARE("NOTE", ("unsetenv doesn't work; skipping test")); 2033#endif 2034 tt_skip(); 2035 } 2036 2037 basenames = event_get_supported_methods(); 2038 for (i = 0; basenames[i]; ++i) { 2039 methodname_to_envvar(basenames[i], varbuf, sizeof(varbuf)); 2040 unsetenv(varbuf); 2041 ++n_methods; 2042 } 2043 2044 base = event_base_new(); 2045 tt_assert(base); 2046 2047 defaultname = event_base_get_method(base); 2048 TT_BLATHER(("default is <%s>", defaultname)); 2049 event_base_free(base); 2050 base = NULL; 2051 2052 /* Can we disable the method with EVENT_NOfoo ? */ 2053 if (!strcmp(defaultname, "epoll (with changelist)")) { 2054 setenv("EVENT_NOEPOLL", "1", 1); 2055 ignoreenvname = "epoll"; 2056 } else { 2057 methodname_to_envvar(defaultname, varbuf, sizeof(varbuf)); 2058 setenv(varbuf, "1", 1); 2059 ignoreenvname = defaultname; 2060 } 2061 2062 /* Use an empty cfg rather than NULL so a failure doesn't exit() */ 2063 cfg = event_config_new(); 2064 base = event_base_new_with_config(cfg); 2065 event_config_free(cfg); 2066 cfg = NULL; 2067 if (n_methods == 1) { 2068 tt_assert(!base); 2069 } else { 2070 tt_assert(base); 2071 tt_str_op(defaultname, !=, event_base_get_method(base)); 2072 event_base_free(base); 2073 base = NULL; 2074 } 2075 2076 /* Can we disable looking at the environment with IGNORE_ENV ? */ 2077 cfg = event_config_new(); 2078 event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV); 2079 base = event_base_new_with_config(cfg); 2080 tt_assert(base); 2081 tt_str_op(ignoreenvname, ==, event_base_get_method(base)); 2082#else 2083 tt_skip(); 2084#endif 2085 2086end: 2087 if (base) 2088 event_base_free(base); 2089 if (cfg) 2090 event_config_free(cfg); 2091} 2092 2093static void 2094read_called_once_cb(evutil_socket_t fd, short event, void *arg) 2095{ 2096 tt_int_op(event, ==, EV_READ); 2097 called += 1; 2098end: 2099 ; 2100} 2101 2102static void 2103timeout_called_once_cb(evutil_socket_t fd, short event, void *arg) 2104{ 2105 tt_int_op(event, ==, EV_TIMEOUT); 2106 called += 100; 2107end: 2108 ; 2109} 2110 2111static void 2112test_event_once(void *ptr) 2113{ 2114 struct basic_test_data *data = ptr; 2115 struct timeval tv; 2116 int r; 2117 2118 tv.tv_sec = 0; 2119 tv.tv_usec = 50*1000; 2120 called = 0; 2121 r = event_base_once(data->base, data->pair[0], EV_READ, 2122 read_called_once_cb, NULL, NULL); 2123 tt_int_op(r, ==, 0); 2124 r = event_base_once(data->base, -1, EV_TIMEOUT, 2125 timeout_called_once_cb, NULL, &tv); 2126 tt_int_op(r, ==, 0); 2127 r = event_base_once(data->base, -1, 0, NULL, NULL, NULL); 2128 tt_int_op(r, <, 0); 2129 2130 if (write(data->pair[1], TEST1, strlen(TEST1)+1) < 0) { 2131 tt_fail_perror("write"); 2132 } 2133 2134 shutdown(data->pair[1], SHUT_WR); 2135 2136 event_base_dispatch(data->base); 2137 2138 tt_int_op(called, ==, 101); 2139end: 2140 ; 2141} 2142 2143static void 2144test_event_pending(void *ptr) 2145{ 2146 struct basic_test_data *data = ptr; 2147 struct event *r=NULL, *w=NULL, *t=NULL; 2148 struct timeval tv, now, tv2, diff; 2149 2150 tv.tv_sec = 0; 2151 tv.tv_usec = 500 * 1000; 2152 r = event_new(data->base, data->pair[0], EV_READ, simple_read_cb, 2153 NULL); 2154 w = event_new(data->base, data->pair[1], EV_WRITE, simple_write_cb, 2155 NULL); 2156 t = evtimer_new(data->base, timeout_cb, NULL); 2157 2158 tt_assert(r); 2159 tt_assert(w); 2160 tt_assert(t); 2161 2162 evutil_gettimeofday(&now, NULL); 2163 event_add(r, NULL); 2164 event_add(t, &tv); 2165 2166 tt_assert( event_pending(r, EV_READ, NULL)); 2167 tt_assert(!event_pending(w, EV_WRITE, NULL)); 2168 tt_assert(!event_pending(r, EV_WRITE, NULL)); 2169 tt_assert( event_pending(r, EV_READ|EV_WRITE, NULL)); 2170 tt_assert(!event_pending(r, EV_TIMEOUT, NULL)); 2171 tt_assert( event_pending(t, EV_TIMEOUT, NULL)); 2172 tt_assert( event_pending(t, EV_TIMEOUT, &tv2)); 2173 2174 tt_assert(evutil_timercmp(&tv2, &now, >)); 2175 evutil_timeradd(&now, &tv, &tv); 2176 evutil_timersub(&tv2, &tv, &diff); 2177 tt_int_op(diff.tv_sec, ==, 0); 2178 tt_int_op(labs(diff.tv_usec), <, 1000); 2179 2180end: 2181 if (r) { 2182 event_del(r); 2183 event_free(r); 2184 } 2185 if (w) { 2186 event_del(w); 2187 event_free(w); 2188 } 2189 if (t) { 2190 event_del(t); 2191 event_free(t); 2192 } 2193} 2194 2195#ifndef WIN32 2196/* You can't do this test on windows, since dup2 doesn't work on sockets */ 2197 2198static void 2199dfd_cb(evutil_socket_t fd, short e, void *data) 2200{ 2201 *(int*)data = (int)e; 2202} 2203 2204/* Regression test for our workaround for a fun epoll/linux related bug 2205 * where fd2 = dup(fd1); add(fd2); close(fd2); dup2(fd1,fd2); add(fd2) 2206 * will get you an EEXIST */ 2207static void 2208test_dup_fd(void *arg) 2209{ 2210 struct basic_test_data *data = arg; 2211 struct event_base *base = data->base; 2212 struct event *ev1=NULL, *ev2=NULL; 2213 int fd, dfd=-1; 2214 int ev1_got, ev2_got; 2215 2216 tt_int_op(write(data->pair[0], "Hello world", 2217 strlen("Hello world")), >, 0); 2218 fd = data->pair[1]; 2219 2220 dfd = dup(fd); 2221 tt_int_op(dfd, >=, 0); 2222 2223 ev1 = event_new(base, fd, EV_READ|EV_PERSIST, dfd_cb, &ev1_got); 2224 ev2 = event_new(base, dfd, EV_READ|EV_PERSIST, dfd_cb, &ev2_got); 2225 ev1_got = ev2_got = 0; 2226 event_add(ev1, NULL); 2227 event_add(ev2, NULL); 2228 event_base_loop(base, EVLOOP_ONCE); 2229 tt_int_op(ev1_got, ==, EV_READ); 2230 tt_int_op(ev2_got, ==, EV_READ); 2231 2232 /* Now close and delete dfd then dispatch. We need to do the 2233 * dispatch here so that when we add it later, we think there 2234 * was an intermediate delete. */ 2235 close(dfd); 2236 event_del(ev2); 2237 ev1_got = ev2_got = 0; 2238 event_base_loop(base, EVLOOP_ONCE); 2239 tt_want_int_op(ev1_got, ==, EV_READ); 2240 tt_int_op(ev2_got, ==, 0); 2241 2242 /* Re-duplicate the fd. We need to get the same duplicated 2243 * value that we closed to provoke the epoll quirk. Also, we 2244 * need to change the events to write, or else the old lingering 2245 * read event will make the test pass whether the change was 2246 * successful or not. */ 2247 tt_int_op(dup2(fd, dfd), ==, dfd); 2248 event_free(ev2); 2249 ev2 = event_new(base, dfd, EV_WRITE|EV_PERSIST, dfd_cb, &ev2_got); 2250 event_add(ev2, NULL); 2251 ev1_got = ev2_got = 0; 2252 event_base_loop(base, EVLOOP_ONCE); 2253 tt_want_int_op(ev1_got, ==, EV_READ); 2254 tt_int_op(ev2_got, ==, EV_WRITE); 2255 2256end: 2257 if (ev1) 2258 event_free(ev1); 2259 if (ev2) 2260 event_free(ev2); 2261 if (dfd >= 0) 2262 close(dfd); 2263} 2264#endif 2265 2266#ifdef _EVENT_DISABLE_MM_REPLACEMENT 2267static void 2268test_mm_functions(void *arg) 2269{ 2270 _tinytest_set_test_skipped(); 2271} 2272#else 2273static int 2274check_dummy_mem_ok(void *_mem) 2275{ 2276 char *mem = _mem; 2277 mem -= 16; 2278 return !memcmp(mem, "{[<guardedram>]}", 16); 2279} 2280 2281static void * 2282dummy_malloc(size_t len) 2283{ 2284 char *mem = malloc(len+16); 2285 memcpy(mem, "{[<guardedram>]}", 16); 2286 return mem+16; 2287} 2288 2289static void * 2290dummy_realloc(void *_mem, size_t len) 2291{ 2292 char *mem = _mem; 2293 if (!mem) 2294 return dummy_malloc(len); 2295 tt_want(check_dummy_mem_ok(_mem)); 2296 mem -= 16; 2297 mem = realloc(mem, len+16); 2298 return mem+16; 2299} 2300 2301static void 2302dummy_free(void *_mem) 2303{ 2304 char *mem = _mem; 2305 tt_want(check_dummy_mem_ok(_mem)); 2306 mem -= 16; 2307 free(mem); 2308} 2309 2310static void 2311test_mm_functions(void *arg) 2312{ 2313 struct event_base *b = NULL; 2314 struct event_config *cfg = NULL; 2315 event_set_mem_functions(dummy_malloc, dummy_realloc, dummy_free); 2316 cfg = event_config_new(); 2317 event_config_avoid_method(cfg, "Nonesuch"); 2318 b = event_base_new_with_config(cfg); 2319 tt_assert(b); 2320 tt_assert(check_dummy_mem_ok(b)); 2321end: 2322 if (cfg) 2323 event_config_free(cfg); 2324 if (b) 2325 event_base_free(b); 2326} 2327#endif 2328 2329static void 2330many_event_cb(evutil_socket_t fd, short event, void *arg) 2331{ 2332 int *calledp = arg; 2333 *calledp += 1; 2334} 2335 2336static void 2337test_many_events(void *arg) 2338{ 2339 /* Try 70 events that should all be ready at once. This will 2340 * exercise the "resize" code on most of the backends, and will make 2341 * sure that we can get past the 64-handle limit of some windows 2342 * functions. */ 2343#define MANY 70 2344 2345 struct basic_test_data *data = arg; 2346 struct event_base *base = data->base; 2347 int one_at_a_time = data->setup_data != NULL; 2348 evutil_socket_t sock[MANY]; 2349 struct event *ev[MANY]; 2350 int called[MANY]; 2351 int i; 2352 int loopflags = EVLOOP_NONBLOCK, evflags=0; 2353 const int is_evport = !strcmp(event_base_get_method(base),"evport"); 2354 if (one_at_a_time) { 2355 loopflags |= EVLOOP_ONCE; 2356 evflags = EV_PERSIST; 2357 } 2358 2359 memset(sock, 0xff, sizeof(sock)); 2360 memset(ev, 0, sizeof(ev)); 2361 memset(called, 0, sizeof(called)); 2362 if (is_evport && one_at_a_time) { 2363 TT_DECLARE("NOTE", ("evport can't pass this in 2.0; skipping\n")); 2364 tt_skip(); 2365 } 2366 2367 for (i = 0; i < MANY; ++i) { 2368 /* We need an event that will hit the backend, and that will 2369 * be ready immediately. "Send a datagram" is an easy 2370 * instance of that. */ 2371 sock[i] = socket(AF_INET, SOCK_DGRAM, 0); 2372 tt_assert(sock[i] >= 0); 2373 called[i] = 0; 2374 ev[i] = event_new(base, sock[i], EV_WRITE|evflags, 2375 many_event_cb, &called[i]); 2376 event_add(ev[i], NULL); 2377 if (one_at_a_time) 2378 event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE); 2379 } 2380 2381 event_base_loop(base, loopflags); 2382 2383 for (i = 0; i < MANY; ++i) { 2384 if (one_at_a_time) 2385 tt_int_op(called[i], ==, MANY - i + 1); 2386 else 2387 tt_int_op(called[i], ==, 1); 2388 } 2389 2390end: 2391 for (i = 0; i < MANY; ++i) { 2392 if (ev[i]) 2393 event_free(ev[i]); 2394 if (sock[i] >= 0) 2395 evutil_closesocket(sock[i]); 2396 } 2397#undef MANY 2398} 2399 2400static void 2401test_struct_event_size(void *arg) 2402{ 2403 tt_int_op(event_get_struct_event_size(), <=, sizeof(struct event)); 2404end: 2405 ; 2406} 2407 2408struct testcase_t main_testcases[] = { 2409 /* Some converted-over tests */ 2410 { "methods", test_methods, TT_FORK, NULL, NULL }, 2411 { "version", test_version, 0, NULL, NULL }, 2412 BASIC(base_features, TT_FORK|TT_NO_LOGS), 2413 { "base_environ", test_base_environ, TT_FORK, NULL, NULL }, 2414 2415 BASIC(event_base_new, TT_FORK|TT_NEED_SOCKETPAIR), 2416 BASIC(free_active_base, TT_FORK|TT_NEED_SOCKETPAIR), 2417 2418 BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE), 2419 2420 BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS), 2421 BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS), 2422 2423 LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE), 2424 { "persistent_timeout_jump", test_persistent_timeout_jump, TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2425 { "persistent_active_timeout", test_persistent_active_timeout, 2426 TT_FORK|TT_NEED_BASE, &basic_setup, NULL }, 2427 LEGACY(priorities, TT_FORK|TT_NEED_BASE), 2428 BASIC(priority_active_inversion, TT_FORK|TT_NEED_BASE), 2429 { "common_timeout", test_common_timeout, TT_FORK|TT_NEED_BASE, 2430 &basic_setup, NULL }, 2431 2432 /* These legacy tests may not all need all of these flags. */ 2433 LEGACY(simpleread, TT_ISOLATED), 2434 LEGACY(simpleread_multiple, TT_ISOLATED), 2435 LEGACY(simplewrite, TT_ISOLATED), 2436 { "simpleclose", test_simpleclose, TT_FORK, &basic_setup, 2437 NULL }, 2438 LEGACY(multiple, TT_ISOLATED), 2439 LEGACY(persistent, TT_ISOLATED), 2440 LEGACY(combined, TT_ISOLATED), 2441 LEGACY(simpletimeout, TT_ISOLATED), 2442 LEGACY(loopbreak, TT_ISOLATED), 2443 LEGACY(loopexit, TT_ISOLATED), 2444 LEGACY(loopexit_multiple, TT_ISOLATED), 2445 LEGACY(nonpersist_readd, TT_ISOLATED), 2446 LEGACY(multiple_events_for_same_fd, TT_ISOLATED), 2447 LEGACY(want_only_once, TT_ISOLATED), 2448 { "event_once", test_event_once, TT_ISOLATED, &basic_setup, NULL }, 2449 { "event_pending", test_event_pending, TT_ISOLATED, &basic_setup, 2450 NULL }, 2451#ifndef WIN32 2452 { "dup_fd", test_dup_fd, TT_ISOLATED, &basic_setup, NULL }, 2453#endif 2454 { "mm_functions", test_mm_functions, TT_FORK, NULL, NULL }, 2455 { "many_events", test_many_events, TT_ISOLATED, &basic_setup, NULL }, 2456 { "many_events_slow_add", test_many_events, TT_ISOLATED, &basic_setup, (void*)1 }, 2457 2458 { "struct_event_size", test_struct_event_size, 0, NULL, NULL }, 2459 2460#ifndef WIN32 2461 LEGACY(fork, TT_ISOLATED), 2462#endif 2463 END_OF_TESTCASES 2464}; 2465 2466struct testcase_t evtag_testcases[] = { 2467 { "int", evtag_int_test, TT_FORK, NULL, NULL }, 2468 { "fuzz", evtag_fuzz, TT_FORK, NULL, NULL }, 2469 { "encoding", evtag_tag_encoding, TT_FORK, NULL, NULL }, 2470 { "peek", evtag_test_peek, 0, NULL, NULL }, 2471 2472 END_OF_TESTCASES 2473}; 2474 2475struct testcase_t signal_testcases[] = { 2476#ifndef WIN32 2477 LEGACY(simplesignal, TT_ISOLATED), 2478 LEGACY(multiplesignal, TT_ISOLATED), 2479 LEGACY(immediatesignal, TT_ISOLATED), 2480 LEGACY(signal_dealloc, TT_ISOLATED), 2481 LEGACY(signal_pipeloss, TT_ISOLATED), 2482 LEGACY(signal_switchbase, TT_ISOLATED|TT_NO_LOGS), 2483 LEGACY(signal_restore, TT_ISOLATED), 2484 LEGACY(signal_assert, TT_ISOLATED), 2485 LEGACY(signal_while_processing, TT_ISOLATED), 2486#endif 2487 END_OF_TESTCASES 2488}; 2489 2490