Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (c) 2003, 2004 Niels Provos <provos (at) citi.umich.edu>
      3  * All rights reserved.
      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 #ifdef HAVE_CONFIG_H
     34 #include "config.h"
     35 #endif
     36 
     37 #include <sys/types.h>
     38 #include <sys/stat.h>
     39 #ifdef HAVE_SYS_TIME_H
     40 #include <sys/time.h>
     41 #endif
     42 #include <sys/queue.h>
     43 #ifndef WIN32
     44 #include <sys/socket.h>
     45 #include <sys/wait.h>
     46 #include <signal.h>
     47 #include <unistd.h>
     48 #include <netdb.h>
     49 #endif
     50 #include <assert.h>
     51 #include <fcntl.h>
     52 #include <signal.h>
     53 #include <stdlib.h>
     54 #include <stdio.h>
     55 #include <string.h>
     56 #include <errno.h>
     57 
     58 #include "event.h"
     59 #include "evutil.h"
     60 #include "event-internal.h"
     61 #include "log.h"
     62 
     63 #include "regress.h"
     64 #ifndef WIN32
     65 #include "regress.gen.h"
     66 #endif
     67 
     68 int pair[2];
     69 int test_ok;
     70 static int called;
     71 static char wbuf[4096];
     72 static char rbuf[4096];
     73 static int woff;
     74 static int roff;
     75 static int usepersist;
     76 static struct timeval tset;
     77 static struct timeval tcalled;
     78 static struct event_base *global_base;
     79 
     80 #define TEST1	"this is a test"
     81 #define SECONDS	1
     82 
     83 #ifndef SHUT_WR
     84 #define SHUT_WR 1
     85 #endif
     86 
     87 #ifdef WIN32
     88 #define write(fd,buf,len) send((fd),(buf),(len),0)
     89 #define read(fd,buf,len) recv((fd),(buf),(len),0)
     90 #endif
     91 
     92 static void
     93 simple_read_cb(int fd, short event, void *arg)
     94 {
     95 	char buf[256];
     96 	int len;
     97 
     98 	if (arg == NULL)
     99 		return;
    100 
    101 	len = read(fd, buf, sizeof(buf));
    102 
    103 	if (len) {
    104 		if (!called) {
    105 			if (event_add(arg, NULL) == -1)
    106 				exit(1);
    107 		}
    108 	} else if (called == 1)
    109 		test_ok = 1;
    110 
    111 	called++;
    112 }
    113 
    114 static void
    115 simple_write_cb(int fd, short event, void *arg)
    116 {
    117 	int len;
    118 
    119 	if (arg == NULL)
    120 		return;
    121 
    122 	len = write(fd, TEST1, strlen(TEST1) + 1);
    123 	if (len == -1)
    124 		test_ok = 0;
    125 	else
    126 		test_ok = 1;
    127 }
    128 
    129 static void
    130 multiple_write_cb(int fd, short event, void *arg)
    131 {
    132 	struct event *ev = arg;
    133 	int len;
    134 
    135 	len = 128;
    136 	if (woff + len >= sizeof(wbuf))
    137 		len = sizeof(wbuf) - woff;
    138 
    139 	len = write(fd, wbuf + woff, len);
    140 	if (len == -1) {
    141 		fprintf(stderr, "%s: write\n", __func__);
    142 		if (usepersist)
    143 			event_del(ev);
    144 		return;
    145 	}
    146 
    147 	woff += len;
    148 
    149 	if (woff >= sizeof(wbuf)) {
    150 		shutdown(fd, SHUT_WR);
    151 		if (usepersist)
    152 			event_del(ev);
    153 		return;
    154 	}
    155 
    156 	if (!usepersist) {
    157 		if (event_add(ev, NULL) == -1)
    158 			exit(1);
    159 	}
    160 }
    161 
    162 static void
    163 multiple_read_cb(int fd, short event, void *arg)
    164 {
    165 	struct event *ev = arg;
    166 	int len;
    167 
    168 	len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
    169 	if (len == -1)
    170 		fprintf(stderr, "%s: read\n", __func__);
    171 	if (len <= 0) {
    172 		if (usepersist)
    173 			event_del(ev);
    174 		return;
    175 	}
    176 
    177 	roff += len;
    178 	if (!usepersist) {
    179 		if (event_add(ev, NULL) == -1)
    180 			exit(1);
    181 	}
    182 }
    183 
    184 static void
    185 timeout_cb(int fd, short event, void *arg)
    186 {
    187 	struct timeval tv;
    188 	int diff;
    189 
    190 	evutil_gettimeofday(&tcalled, NULL);
    191 	if (evutil_timercmp(&tcalled, &tset, >))
    192 		evutil_timersub(&tcalled, &tset, &tv);
    193 	else
    194 		evutil_timersub(&tset, &tcalled, &tv);
    195 
    196 	diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000;
    197 	if (diff < 0)
    198 		diff = -diff;
    199 
    200 	if (diff < 100)
    201 		test_ok = 1;
    202 }
    203 
    204 #ifndef WIN32
    205 static void
    206 signal_cb_sa(int sig)
    207 {
    208 	test_ok = 2;
    209 }
    210 
    211 static void
    212 signal_cb(int fd, short event, void *arg)
    213 {
    214 	struct event *ev = arg;
    215 
    216 	signal_del(ev);
    217 	test_ok = 1;
    218 }
    219 #endif
    220 
    221 struct both {
    222 	struct event ev;
    223 	int nread;
    224 };
    225 
    226 static void
    227 combined_read_cb(int fd, short event, void *arg)
    228 {
    229 	struct both *both = arg;
    230 	char buf[128];
    231 	int len;
    232 
    233 	len = read(fd, buf, sizeof(buf));
    234 	if (len == -1)
    235 		fprintf(stderr, "%s: read\n", __func__);
    236 	if (len <= 0)
    237 		return;
    238 
    239 	both->nread += len;
    240 	if (event_add(&both->ev, NULL) == -1)
    241 		exit(1);
    242 }
    243 
    244 static void
    245 combined_write_cb(int fd, short event, void *arg)
    246 {
    247 	struct both *both = arg;
    248 	char buf[128];
    249 	int len;
    250 
    251 	len = sizeof(buf);
    252 	if (len > both->nread)
    253 		len = both->nread;
    254 
    255 	len = write(fd, buf, len);
    256 	if (len == -1)
    257 		fprintf(stderr, "%s: write\n", __func__);
    258 	if (len <= 0) {
    259 		shutdown(fd, SHUT_WR);
    260 		return;
    261 	}
    262 
    263 	both->nread -= len;
    264 	if (event_add(&both->ev, NULL) == -1)
    265 		exit(1);
    266 }
    267 
    268 /* Test infrastructure */
    269 
    270 static int
    271 setup_test(const char *name)
    272 {
    273 
    274 	fprintf(stdout, "%s", name);
    275 
    276 	if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
    277 		fprintf(stderr, "%s: socketpair\n", __func__);
    278 		exit(1);
    279 	}
    280 
    281 #ifdef HAVE_FCNTL
    282         if (fcntl(pair[0], F_SETFL, O_NONBLOCK) == -1)
    283 		fprintf(stderr, "fcntl(O_NONBLOCK)");
    284 
    285         if (fcntl(pair[1], F_SETFL, O_NONBLOCK) == -1)
    286 		fprintf(stderr, "fcntl(O_NONBLOCK)");
    287 #endif
    288 
    289 	test_ok = 0;
    290 	called = 0;
    291 	return (0);
    292 }
    293 
    294 static int
    295 cleanup_test(void)
    296 {
    297 #ifndef WIN32
    298 	close(pair[0]);
    299 	close(pair[1]);
    300 #else
    301 	CloseHandle((HANDLE)pair[0]);
    302 	CloseHandle((HANDLE)pair[1]);
    303 #endif
    304 	if (test_ok)
    305 		fprintf(stdout, "OK\n");
    306 	else {
    307 		fprintf(stdout, "FAILED\n");
    308 		exit(1);
    309 	}
    310         test_ok = 0;
    311 	return (0);
    312 }
    313 
    314 static void
    315 test_registerfds(void)
    316 {
    317 	int i, j;
    318 	int pair[2];
    319 	struct event read_evs[512];
    320 	struct event write_evs[512];
    321 
    322 	struct event_base *base = event_base_new();
    323 
    324 	fprintf(stdout, "Testing register fds: ");
    325 
    326 	for (i = 0; i < 512; ++i) {
    327 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
    328 			/* run up to the limit of file descriptors */
    329 			break;
    330 		}
    331 		event_set(&read_evs[i], pair[0],
    332 		    EV_READ|EV_PERSIST, simple_read_cb, NULL);
    333 		event_base_set(base, &read_evs[i]);
    334 		event_add(&read_evs[i], NULL);
    335 		event_set(&write_evs[i], pair[1],
    336 		    EV_WRITE|EV_PERSIST, simple_write_cb, NULL);
    337 		event_base_set(base, &write_evs[i]);
    338 		event_add(&write_evs[i], NULL);
    339 
    340 		/* just loop once */
    341 		event_base_loop(base, EVLOOP_ONCE);
    342 	}
    343 
    344 	/* now delete everything */
    345 	for (j = 0; j < i; ++j) {
    346 		event_del(&read_evs[j]);
    347 		event_del(&write_evs[j]);
    348 #ifndef WIN32
    349 		close(read_evs[j].ev_fd);
    350 		close(write_evs[j].ev_fd);
    351 #else
    352 		CloseHandle((HANDLE)read_evs[j].ev_fd);
    353 		CloseHandle((HANDLE)write_evs[j].ev_fd);
    354 #endif
    355 
    356 		/* just loop once */
    357 		event_base_loop(base, EVLOOP_ONCE);
    358 	}
    359 
    360 	event_base_free(base);
    361 
    362 	fprintf(stdout, "OK\n");
    363 }
    364 
    365 static void
    366 test_simpleread(void)
    367 {
    368 	struct event ev;
    369 
    370 	/* Very simple read test */
    371 	setup_test("Simple read: ");
    372 
    373 	write(pair[0], TEST1, strlen(TEST1)+1);
    374 	shutdown(pair[0], SHUT_WR);
    375 
    376 	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
    377 	if (event_add(&ev, NULL) == -1)
    378 		exit(1);
    379 	event_dispatch();
    380 
    381 	cleanup_test();
    382 }
    383 
    384 static void
    385 test_simplewrite(void)
    386 {
    387 	struct event ev;
    388 
    389 	/* Very simple write test */
    390 	setup_test("Simple write: ");
    391 
    392 	event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
    393 	if (event_add(&ev, NULL) == -1)
    394 		exit(1);
    395 	event_dispatch();
    396 
    397 	cleanup_test();
    398 }
    399 
    400 static void
    401 test_multiple(void)
    402 {
    403 	struct event ev, ev2;
    404 	int i;
    405 
    406 	/* Multiple read and write test */
    407 	setup_test("Multiple read/write: ");
    408 	memset(rbuf, 0, sizeof(rbuf));
    409 	for (i = 0; i < sizeof(wbuf); i++)
    410 		wbuf[i] = i;
    411 
    412 	roff = woff = 0;
    413 	usepersist = 0;
    414 
    415 	event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
    416 	if (event_add(&ev, NULL) == -1)
    417 		exit(1);
    418 	event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
    419 	if (event_add(&ev2, NULL) == -1)
    420 		exit(1);
    421 	event_dispatch();
    422 
    423 	if (roff == woff)
    424 		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
    425 
    426 	cleanup_test();
    427 }
    428 
    429 static void
    430 test_persistent(void)
    431 {
    432 	struct event ev, ev2;
    433 	int i;
    434 
    435 	/* Multiple read and write test with persist */
    436 	setup_test("Persist read/write: ");
    437 	memset(rbuf, 0, sizeof(rbuf));
    438 	for (i = 0; i < sizeof(wbuf); i++)
    439 		wbuf[i] = i;
    440 
    441 	roff = woff = 0;
    442 	usepersist = 1;
    443 
    444 	event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
    445 	if (event_add(&ev, NULL) == -1)
    446 		exit(1);
    447 	event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
    448 	if (event_add(&ev2, NULL) == -1)
    449 		exit(1);
    450 	event_dispatch();
    451 
    452 	if (roff == woff)
    453 		test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
    454 
    455 	cleanup_test();
    456 }
    457 
    458 static void
    459 test_combined(void)
    460 {
    461 	struct both r1, r2, w1, w2;
    462 
    463 	setup_test("Combined read/write: ");
    464 	memset(&r1, 0, sizeof(r1));
    465 	memset(&r2, 0, sizeof(r2));
    466 	memset(&w1, 0, sizeof(w1));
    467 	memset(&w2, 0, sizeof(w2));
    468 
    469 	w1.nread = 4096;
    470 	w2.nread = 8192;
    471 
    472 	event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
    473 	event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
    474 	event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
    475 	event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
    476 	if (event_add(&r1.ev, NULL) == -1)
    477 		exit(1);
    478 	if (event_add(&w1.ev, NULL))
    479 		exit(1);
    480 	if (event_add(&r2.ev, NULL))
    481 		exit(1);
    482 	if (event_add(&w2.ev, NULL))
    483 		exit(1);
    484 
    485 	event_dispatch();
    486 
    487 	if (r1.nread == 8192 && r2.nread == 4096)
    488 		test_ok = 1;
    489 
    490 	cleanup_test();
    491 }
    492 
    493 static void
    494 test_simpletimeout(void)
    495 {
    496 	struct timeval tv;
    497 	struct event ev;
    498 
    499 	setup_test("Simple timeout: ");
    500 
    501 	tv.tv_usec = 0;
    502 	tv.tv_sec = SECONDS;
    503 	evtimer_set(&ev, timeout_cb, NULL);
    504 	evtimer_add(&ev, &tv);
    505 
    506 	evutil_gettimeofday(&tset, NULL);
    507 	event_dispatch();
    508 
    509 	cleanup_test();
    510 }
    511 
    512 #ifndef WIN32
    513 extern struct event_base *current_base;
    514 
    515 static void
    516 child_signal_cb(int fd, short event, void *arg)
    517 {
    518 	struct timeval tv;
    519 	int *pint = arg;
    520 
    521 	*pint = 1;
    522 
    523 	tv.tv_usec = 500000;
    524 	tv.tv_sec = 0;
    525 	event_loopexit(&tv);
    526 }
    527 
    528 static void
    529 test_fork(void)
    530 {
    531 	int status, got_sigchld = 0;
    532 	struct event ev, sig_ev;
    533 	pid_t pid;
    534 
    535 	setup_test("After fork: ");
    536 
    537 	write(pair[0], TEST1, strlen(TEST1)+1);
    538 
    539 	event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
    540 	if (event_add(&ev, NULL) == -1)
    541 		exit(1);
    542 
    543 	signal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld);
    544 	signal_add(&sig_ev, NULL);
    545 
    546 	if ((pid = fork()) == 0) {
    547 		/* in the child */
    548 		if (event_reinit(current_base) == -1) {
    549 			fprintf(stderr, "FAILED (reinit)\n");
    550 			exit(1);
    551 		}
    552 
    553 		signal_del(&sig_ev);
    554 
    555 		called = 0;
    556 
    557 		event_dispatch();
    558 
    559 		/* we do not send an EOF; simple_read_cb requires an EOF
    560 		 * to set test_ok.  we just verify that the callback was
    561 		 * called. */
    562 		exit(test_ok != 0 || called != 2 ? -2 : 76);
    563 	}
    564 
    565 	/* wait for the child to read the data */
    566 	sleep(1);
    567 
    568 	write(pair[0], TEST1, strlen(TEST1)+1);
    569 
    570 	if (waitpid(pid, &status, 0) == -1) {
    571 		fprintf(stderr, "FAILED (fork)\n");
    572 		exit(1);
    573 	}
    574 
    575 	if (WEXITSTATUS(status) != 76) {
    576 		fprintf(stderr, "FAILED (exit): %d\n", WEXITSTATUS(status));
    577 		exit(1);
    578 	}
    579 
    580 	/* test that the current event loop still works */
    581 	write(pair[0], TEST1, strlen(TEST1)+1);
    582 	shutdown(pair[0], SHUT_WR);
    583 
    584 	event_dispatch();
    585 
    586 	if (!got_sigchld) {
    587 		fprintf(stdout, "FAILED (sigchld)\n");
    588 		exit(1);
    589 	}
    590 
    591 	signal_del(&sig_ev);
    592 
    593 	cleanup_test();
    594 }
    595 
    596 static void
    597 test_simplesignal(void)
    598 {
    599 	struct event ev;
    600 	struct itimerval itv;
    601 
    602 	setup_test("Simple signal: ");
    603 	signal_set(&ev, SIGALRM, signal_cb, &ev);
    604 	signal_add(&ev, NULL);
    605 	/* find bugs in which operations are re-ordered */
    606 	signal_del(&ev);
    607 	signal_add(&ev, NULL);
    608 
    609 	memset(&itv, 0, sizeof(itv));
    610 	itv.it_value.tv_sec = 1;
    611 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
    612 		goto skip_simplesignal;
    613 
    614 	event_dispatch();
    615  skip_simplesignal:
    616 	if (signal_del(&ev) == -1)
    617 		test_ok = 0;
    618 
    619 	cleanup_test();
    620 }
    621 
    622 static void
    623 test_multiplesignal(void)
    624 {
    625 	struct event ev_one, ev_two;
    626 	struct itimerval itv;
    627 
    628 	setup_test("Multiple signal: ");
    629 
    630 	signal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
    631 	signal_add(&ev_one, NULL);
    632 
    633 	signal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
    634 	signal_add(&ev_two, NULL);
    635 
    636 	memset(&itv, 0, sizeof(itv));
    637 	itv.it_value.tv_sec = 1;
    638 	if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
    639 		goto skip_simplesignal;
    640 
    641 	event_dispatch();
    642 
    643  skip_simplesignal:
    644 	if (signal_del(&ev_one) == -1)
    645 		test_ok = 0;
    646 	if (signal_del(&ev_two) == -1)
    647 		test_ok = 0;
    648 
    649 	cleanup_test();
    650 }
    651 
    652 static void
    653 test_immediatesignal(void)
    654 {
    655 	struct event ev;
    656 
    657 	test_ok = 0;
    658 	printf("Immediate signal: ");
    659 	signal_set(&ev, SIGUSR1, signal_cb, &ev);
    660 	signal_add(&ev, NULL);
    661 	raise(SIGUSR1);
    662 	event_loop(EVLOOP_NONBLOCK);
    663 	signal_del(&ev);
    664 	cleanup_test();
    665 }
    666 
    667 static void
    668 test_signal_dealloc(void)
    669 {
    670 	/* make sure that signal_event is event_del'ed and pipe closed */
    671 	struct event ev;
    672 	struct event_base *base = event_init();
    673 	printf("Signal dealloc: ");
    674 	signal_set(&ev, SIGUSR1, signal_cb, &ev);
    675 	signal_add(&ev, NULL);
    676 	signal_del(&ev);
    677 	event_base_free(base);
    678         /* If we got here without asserting, we're fine. */
    679         test_ok = 1;
    680 	cleanup_test();
    681 }
    682 
    683 static void
    684 test_signal_pipeloss(void)
    685 {
    686 	/* make sure that the base1 pipe is closed correctly. */
    687 	struct event_base *base1, *base2;
    688 	int pipe1;
    689 	test_ok = 0;
    690 	printf("Signal pipeloss: ");
    691 	base1 = event_init();
    692 	pipe1 = base1->sig.ev_signal_pair[0];
    693 	base2 = event_init();
    694 	event_base_free(base2);
    695 	event_base_free(base1);
    696 	if (close(pipe1) != -1 || errno!=EBADF) {
    697 		/* fd must be closed, so second close gives -1, EBADF */
    698 		printf("signal pipe not closed. ");
    699 		test_ok = 0;
    700 	} else {
    701 		test_ok = 1;
    702 	}
    703 	cleanup_test();
    704 }
    705 
    706 /*
    707  * make two bases to catch signals, use both of them.  this only works
    708  * for event mechanisms that use our signal pipe trick.  kqueue handles
    709  * signals internally, and all interested kqueues get all the signals.
    710  */
    711 static void
    712 test_signal_switchbase(void)
    713 {
    714 	struct event ev1, ev2;
    715 	struct event_base *base1, *base2;
    716         int is_kqueue;
    717 	test_ok = 0;
    718 	printf("Signal switchbase: ");
    719 	base1 = event_init();
    720 	base2 = event_init();
    721         is_kqueue = !strcmp(event_get_method(),"kqueue");
    722 	signal_set(&ev1, SIGUSR1, signal_cb, &ev1);
    723 	signal_set(&ev2, SIGUSR1, signal_cb, &ev2);
    724 	if (event_base_set(base1, &ev1) ||
    725 	    event_base_set(base2, &ev2) ||
    726 	    event_add(&ev1, NULL) ||
    727 	    event_add(&ev2, NULL)) {
    728 		fprintf(stderr, "%s: cannot set base, add\n", __func__);
    729 		exit(1);
    730 	}
    731 
    732 	test_ok = 0;
    733 	/* can handle signal before loop is called */
    734 	raise(SIGUSR1);
    735 	event_base_loop(base2, EVLOOP_NONBLOCK);
    736         if (is_kqueue) {
    737                 if (!test_ok)
    738                         goto done;
    739                 test_ok = 0;
    740         }
    741 	event_base_loop(base1, EVLOOP_NONBLOCK);
    742 	if (test_ok && !is_kqueue) {
    743 		test_ok = 0;
    744 
    745 		/* set base1 to handle signals */
    746 		event_base_loop(base1, EVLOOP_NONBLOCK);
    747 		raise(SIGUSR1);
    748 		event_base_loop(base1, EVLOOP_NONBLOCK);
    749 		event_base_loop(base2, EVLOOP_NONBLOCK);
    750 	}
    751  done:
    752 	event_base_free(base1);
    753 	event_base_free(base2);
    754 	cleanup_test();
    755 }
    756 
    757 /*
    758  * assert that a signal event removed from the event queue really is
    759  * removed - with no possibility of it's parent handler being fired.
    760  */
    761 static void
    762 test_signal_assert(void)
    763 {
    764 	struct event ev;
    765 	struct event_base *base = event_init();
    766 	test_ok = 0;
    767 	printf("Signal handler assert: ");
    768 	/* use SIGCONT so we don't kill ourselves when we signal to nowhere */
    769 	signal_set(&ev, SIGCONT, signal_cb, &ev);
    770 	signal_add(&ev, NULL);
    771 	/*
    772 	 * if signal_del() fails to reset the handler, it's current handler
    773 	 * will still point to evsignal_handler().
    774 	 */
    775 	signal_del(&ev);
    776 
    777 	raise(SIGCONT);
    778 	/* only way to verify we were in evsignal_handler() */
    779 	if (base->sig.evsignal_caught)
    780 		test_ok = 0;
    781 	else
    782 		test_ok = 1;
    783 
    784 	event_base_free(base);
    785 	cleanup_test();
    786 	return;
    787 }
    788 
    789 /*
    790  * assert that we restore our previous signal handler properly.
    791  */
    792 static void
    793 test_signal_restore(void)
    794 {
    795 	struct event ev;
    796 	struct event_base *base = event_init();
    797 #ifdef HAVE_SIGACTION
    798 	struct sigaction sa;
    799 #endif
    800 
    801 	test_ok = 0;
    802 	printf("Signal handler restore: ");
    803 #ifdef HAVE_SIGACTION
    804 	sa.sa_handler = signal_cb_sa;
    805 	sa.sa_flags = 0x0;
    806 	sigemptyset(&sa.sa_mask);
    807 	if (sigaction(SIGUSR1, &sa, NULL) == -1)
    808 		goto out;
    809 #else
    810 	if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
    811 		goto out;
    812 #endif
    813 	signal_set(&ev, SIGUSR1, signal_cb, &ev);
    814 	signal_add(&ev, NULL);
    815 	signal_del(&ev);
    816 
    817 	raise(SIGUSR1);
    818 	/* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
    819 	if (test_ok != 2)
    820 		test_ok = 0;
    821 out:
    822 	event_base_free(base);
    823 	cleanup_test();
    824 	return;
    825 }
    826 
    827 static void
    828 signal_cb_swp(int sig, short event, void *arg)
    829 {
    830 	called++;
    831 	if (called < 5)
    832 		raise(sig);
    833 	else
    834 		event_loopexit(NULL);
    835 }
    836 static void
    837 timeout_cb_swp(int fd, short event, void *arg)
    838 {
    839 	if (called == -1) {
    840 		struct timeval tv = {5, 0};
    841 
    842 		called = 0;
    843 		evtimer_add((struct event *)arg, &tv);
    844 		raise(SIGUSR1);
    845 		return;
    846 	}
    847 	test_ok = 0;
    848 	event_loopexit(NULL);
    849 }
    850 
    851 static void
    852 test_signal_while_processing(void)
    853 {
    854 	struct event_base *base = event_init();
    855 	struct event ev, ev_timer;
    856 	struct timeval tv = {0, 0};
    857 
    858 	setup_test("Receiving a signal while processing other signal: ");
    859 
    860 	called = -1;
    861 	test_ok = 1;
    862 	signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
    863 	signal_add(&ev, NULL);
    864 	evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
    865 	evtimer_add(&ev_timer, &tv);
    866 	event_dispatch();
    867 
    868 	event_base_free(base);
    869 	cleanup_test();
    870 	return;
    871 }
    872 #endif
    873 
    874 static void
    875 test_free_active_base(void)
    876 {
    877 	struct event_base *base1;
    878 	struct event ev1;
    879 	setup_test("Free active base: ");
    880 	base1 = event_init();
    881 	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
    882 	event_base_set(base1, &ev1);
    883 	event_add(&ev1, NULL);
    884 	/* event_del(&ev1); */
    885 	event_base_free(base1);
    886 	test_ok = 1;
    887 	cleanup_test();
    888 }
    889 
    890 static void
    891 test_event_base_new(void)
    892 {
    893 	struct event_base *base;
    894 	struct event ev1;
    895 	setup_test("Event base new: ");
    896 
    897 	write(pair[0], TEST1, strlen(TEST1)+1);
    898 	shutdown(pair[0], SHUT_WR);
    899 
    900 	base = event_base_new();
    901 	event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
    902 	event_base_set(base, &ev1);
    903 	event_add(&ev1, NULL);
    904 
    905 	event_base_dispatch(base);
    906 
    907 	event_base_free(base);
    908 	test_ok = 1;
    909 	cleanup_test();
    910 }
    911 
    912 static void
    913 test_loopexit(void)
    914 {
    915 	struct timeval tv, tv_start, tv_end;
    916 	struct event ev;
    917 
    918 	setup_test("Loop exit: ");
    919 
    920 	tv.tv_usec = 0;
    921 	tv.tv_sec = 60*60*24;
    922 	evtimer_set(&ev, timeout_cb, NULL);
    923 	evtimer_add(&ev, &tv);
    924 
    925 	tv.tv_usec = 0;
    926 	tv.tv_sec = 1;
    927 	event_loopexit(&tv);
    928 
    929 	evutil_gettimeofday(&tv_start, NULL);
    930 	event_dispatch();
    931 	evutil_gettimeofday(&tv_end, NULL);
    932 	evutil_timersub(&tv_end, &tv_start, &tv_end);
    933 
    934 	evtimer_del(&ev);
    935 
    936 	if (tv.tv_sec < 2)
    937 		test_ok = 1;
    938 
    939 	cleanup_test();
    940 }
    941 
    942 static void
    943 test_loopexit_multiple(void)
    944 {
    945 	struct timeval tv;
    946 	struct event_base *base;
    947 
    948 	setup_test("Loop Multiple exit: ");
    949 
    950 	base = event_base_new();
    951 
    952 	tv.tv_usec = 0;
    953 	tv.tv_sec = 1;
    954 	event_base_loopexit(base, &tv);
    955 
    956 	tv.tv_usec = 0;
    957 	tv.tv_sec = 2;
    958 	event_base_loopexit(base, &tv);
    959 
    960 	event_base_dispatch(base);
    961 
    962 	event_base_free(base);
    963 
    964 	test_ok = 1;
    965 
    966 	cleanup_test();
    967 }
    968 
    969 static void
    970 break_cb(int fd, short events, void *arg)
    971 {
    972 	test_ok = 1;
    973 	event_loopbreak();
    974 }
    975 
    976 static void
    977 fail_cb(int fd, short events, void *arg)
    978 {
    979 	test_ok = 0;
    980 }
    981 
    982 static void
    983 test_loopbreak(void)
    984 {
    985 	struct event ev1, ev2;
    986 	struct timeval tv;
    987 
    988 	setup_test("Loop break: ");
    989 
    990 	tv.tv_sec = 0;
    991 	tv.tv_usec = 0;
    992 	evtimer_set(&ev1, break_cb, NULL);
    993 	evtimer_add(&ev1, &tv);
    994 	evtimer_set(&ev2, fail_cb, NULL);
    995 	evtimer_add(&ev2, &tv);
    996 
    997 	event_dispatch();
    998 
    999 	evtimer_del(&ev1);
   1000 	evtimer_del(&ev2);
   1001 
   1002 	cleanup_test();
   1003 }
   1004 
   1005 static void
   1006 test_evbuffer(void) {
   1007 
   1008 	struct evbuffer *evb = evbuffer_new();
   1009 	setup_test("Testing Evbuffer: ");
   1010 
   1011 	evbuffer_add_printf(evb, "%s/%d", "hello", 1);
   1012 
   1013 	if (EVBUFFER_LENGTH(evb) == 7 &&
   1014 	    strcmp((char*)EVBUFFER_DATA(evb), "hello/1") == 0)
   1015 	    test_ok = 1;
   1016 
   1017 	evbuffer_free(evb);
   1018 
   1019 	cleanup_test();
   1020 }
   1021 
   1022 static void
   1023 test_evbuffer_find(void)
   1024 {
   1025 	u_char* p;
   1026 	const char* test1 = "1234567890\r\n";
   1027 	const char* test2 = "1234567890\r";
   1028 #define EVBUFFER_INITIAL_LENGTH 256
   1029 	char test3[EVBUFFER_INITIAL_LENGTH];
   1030 	unsigned int i;
   1031 	struct evbuffer * buf = evbuffer_new();
   1032 
   1033 	/* make sure evbuffer_find doesn't match past the end of the buffer */
   1034 	fprintf(stdout, "Testing evbuffer_find 1: ");
   1035 	evbuffer_add(buf, (u_char*)test1, strlen(test1));
   1036 	evbuffer_drain(buf, strlen(test1));
   1037 	evbuffer_add(buf, (u_char*)test2, strlen(test2));
   1038 	p = evbuffer_find(buf, (u_char*)"\r\n", 2);
   1039 	if (p == NULL) {
   1040 		fprintf(stdout, "OK\n");
   1041 	} else {
   1042 		fprintf(stdout, "FAILED\n");
   1043 		exit(1);
   1044 	}
   1045 
   1046 	/*
   1047 	 * drain the buffer and do another find; in r309 this would
   1048 	 * read past the allocated buffer causing a valgrind error.
   1049 	 */
   1050 	fprintf(stdout, "Testing evbuffer_find 2: ");
   1051 	evbuffer_drain(buf, strlen(test2));
   1052 	for (i = 0; i < EVBUFFER_INITIAL_LENGTH; ++i)
   1053 		test3[i] = 'a';
   1054 	test3[EVBUFFER_INITIAL_LENGTH - 1] = 'x';
   1055 	evbuffer_add(buf, (u_char *)test3, EVBUFFER_INITIAL_LENGTH);
   1056 	p = evbuffer_find(buf, (u_char *)"xy", 2);
   1057 	if (p == NULL) {
   1058 		printf("OK\n");
   1059 	} else {
   1060 		fprintf(stdout, "FAILED\n");
   1061 		exit(1);
   1062 	}
   1063 
   1064 	/* simple test for match at end of allocated buffer */
   1065 	fprintf(stdout, "Testing evbuffer_find 3: ");
   1066 	p = evbuffer_find(buf, (u_char *)"ax", 2);
   1067 	if (p != NULL && strncmp((char*)p, "ax", 2) == 0) {
   1068 		printf("OK\n");
   1069 	} else {
   1070 		fprintf(stdout, "FAILED\n");
   1071 		exit(1);
   1072 	}
   1073 
   1074 	evbuffer_free(buf);
   1075 }
   1076 
   1077 /*
   1078  * simple bufferevent test
   1079  */
   1080 
   1081 static void
   1082 readcb(struct bufferevent *bev, void *arg)
   1083 {
   1084 	if (EVBUFFER_LENGTH(bev->input) == 8333) {
   1085 		bufferevent_disable(bev, EV_READ);
   1086 		test_ok++;
   1087 	}
   1088 }
   1089 
   1090 static void
   1091 writecb(struct bufferevent *bev, void *arg)
   1092 {
   1093 	if (EVBUFFER_LENGTH(bev->output) == 0)
   1094 		test_ok++;
   1095 }
   1096 
   1097 static void
   1098 errorcb(struct bufferevent *bev, short what, void *arg)
   1099 {
   1100 	test_ok = -2;
   1101 }
   1102 
   1103 static void
   1104 test_bufferevent(void)
   1105 {
   1106 	struct bufferevent *bev1, *bev2;
   1107 	char buffer[8333];
   1108 	int i;
   1109 
   1110 	setup_test("Bufferevent: ");
   1111 
   1112 	bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL);
   1113 	bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL);
   1114 
   1115 	bufferevent_disable(bev1, EV_READ);
   1116 	bufferevent_enable(bev2, EV_READ);
   1117 
   1118 	for (i = 0; i < sizeof(buffer); i++)
   1119 		buffer[i] = i;
   1120 
   1121 	bufferevent_write(bev1, buffer, sizeof(buffer));
   1122 
   1123 	event_dispatch();
   1124 
   1125 	bufferevent_free(bev1);
   1126 	bufferevent_free(bev2);
   1127 
   1128 	if (test_ok != 2)
   1129 		test_ok = 0;
   1130 
   1131 	cleanup_test();
   1132 }
   1133 
   1134 /*
   1135  * test watermarks and bufferevent
   1136  */
   1137 
   1138 static void
   1139 wm_readcb(struct bufferevent *bev, void *arg)
   1140 {
   1141 	int len = EVBUFFER_LENGTH(bev->input);
   1142 	static int nread;
   1143 
   1144 	assert(len >= 10 && len <= 20);
   1145 
   1146 	evbuffer_drain(bev->input, len);
   1147 
   1148 	nread += len;
   1149 	if (nread == 65000) {
   1150 		bufferevent_disable(bev, EV_READ);
   1151 		test_ok++;
   1152 	}
   1153 }
   1154 
   1155 static void
   1156 wm_writecb(struct bufferevent *bev, void *arg)
   1157 {
   1158 	if (EVBUFFER_LENGTH(bev->output) == 0)
   1159 		test_ok++;
   1160 }
   1161 
   1162 static void
   1163 wm_errorcb(struct bufferevent *bev, short what, void *arg)
   1164 {
   1165 	test_ok = -2;
   1166 }
   1167 
   1168 static void
   1169 test_bufferevent_watermarks(void)
   1170 {
   1171 	struct bufferevent *bev1, *bev2;
   1172 	char buffer[65000];
   1173 	int i;
   1174 
   1175 	setup_test("Bufferevent Watermarks: ");
   1176 
   1177 	bev1 = bufferevent_new(pair[0], NULL, wm_writecb, wm_errorcb, NULL);
   1178 	bev2 = bufferevent_new(pair[1], wm_readcb, NULL, wm_errorcb, NULL);
   1179 
   1180 	bufferevent_disable(bev1, EV_READ);
   1181 	bufferevent_enable(bev2, EV_READ);
   1182 
   1183 	for (i = 0; i < sizeof(buffer); i++)
   1184 		buffer[i] = i;
   1185 
   1186 	bufferevent_write(bev1, buffer, sizeof(buffer));
   1187 
   1188 	/* limit the reading on the receiving bufferevent */
   1189 	bufferevent_setwatermark(bev2, EV_READ, 10, 20);
   1190 
   1191 	event_dispatch();
   1192 
   1193 	bufferevent_free(bev1);
   1194 	bufferevent_free(bev2);
   1195 
   1196 	if (test_ok != 2)
   1197 		test_ok = 0;
   1198 
   1199 	cleanup_test();
   1200 }
   1201 
   1202 struct test_pri_event {
   1203 	struct event ev;
   1204 	int count;
   1205 };
   1206 
   1207 static void
   1208 test_priorities_cb(int fd, short what, void *arg)
   1209 {
   1210 	struct test_pri_event *pri = arg;
   1211 	struct timeval tv;
   1212 
   1213 	if (pri->count == 3) {
   1214 		event_loopexit(NULL);
   1215 		return;
   1216 	}
   1217 
   1218 	pri->count++;
   1219 
   1220 	evutil_timerclear(&tv);
   1221 	event_add(&pri->ev, &tv);
   1222 }
   1223 
   1224 static void
   1225 test_priorities(int npriorities)
   1226 {
   1227 	char buf[32];
   1228 	struct test_pri_event one, two;
   1229 	struct timeval tv;
   1230 
   1231 	evutil_snprintf(buf, sizeof(buf), "Testing Priorities %d: ", npriorities);
   1232 	setup_test(buf);
   1233 
   1234 	event_base_priority_init(global_base, npriorities);
   1235 
   1236 	memset(&one, 0, sizeof(one));
   1237 	memset(&two, 0, sizeof(two));
   1238 
   1239 	timeout_set(&one.ev, test_priorities_cb, &one);
   1240 	if (event_priority_set(&one.ev, 0) == -1) {
   1241 		fprintf(stderr, "%s: failed to set priority", __func__);
   1242 		exit(1);
   1243 	}
   1244 
   1245 	timeout_set(&two.ev, test_priorities_cb, &two);
   1246 	if (event_priority_set(&two.ev, npriorities - 1) == -1) {
   1247 		fprintf(stderr, "%s: failed to set priority", __func__);
   1248 		exit(1);
   1249 	}
   1250 
   1251 	evutil_timerclear(&tv);
   1252 
   1253 	if (event_add(&one.ev, &tv) == -1)
   1254 		exit(1);
   1255 	if (event_add(&two.ev, &tv) == -1)
   1256 		exit(1);
   1257 
   1258 	event_dispatch();
   1259 
   1260 	event_del(&one.ev);
   1261 	event_del(&two.ev);
   1262 
   1263 	if (npriorities == 1) {
   1264 		if (one.count == 3 && two.count == 3)
   1265 			test_ok = 1;
   1266 	} else if (npriorities == 2) {
   1267 		/* Two is called once because event_loopexit is priority 1 */
   1268 		if (one.count == 3 && two.count == 1)
   1269 			test_ok = 1;
   1270 	} else {
   1271 		if (one.count == 3 && two.count == 0)
   1272 			test_ok = 1;
   1273 	}
   1274 
   1275 	cleanup_test();
   1276 }
   1277 
   1278 static void
   1279 test_multiple_cb(int fd, short event, void *arg)
   1280 {
   1281 	if (event & EV_READ)
   1282 		test_ok |= 1;
   1283 	else if (event & EV_WRITE)
   1284 		test_ok |= 2;
   1285 }
   1286 
   1287 static void
   1288 test_multiple_events_for_same_fd(void)
   1289 {
   1290    struct event e1, e2;
   1291 
   1292    setup_test("Multiple events for same fd: ");
   1293 
   1294    event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
   1295    event_add(&e1, NULL);
   1296    event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
   1297    event_add(&e2, NULL);
   1298    event_loop(EVLOOP_ONCE);
   1299    event_del(&e2);
   1300    write(pair[1], TEST1, strlen(TEST1)+1);
   1301    event_loop(EVLOOP_ONCE);
   1302    event_del(&e1);
   1303 
   1304    if (test_ok != 3)
   1305 	   test_ok = 0;
   1306 
   1307    cleanup_test();
   1308 }
   1309 
   1310 int evtag_decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
   1311 int evtag_encode_tag(struct evbuffer *evbuf, uint32_t number);
   1312 int evtag_decode_tag(uint32_t *pnumber, struct evbuffer *evbuf);
   1313 
   1314 static void
   1315 read_once_cb(int fd, short event, void *arg)
   1316 {
   1317 	char buf[256];
   1318 	int len;
   1319 
   1320 	len = read(fd, buf, sizeof(buf));
   1321 
   1322 	if (called) {
   1323 		test_ok = 0;
   1324 	} else if (len) {
   1325 		/* Assumes global pair[0] can be used for writing */
   1326 		write(pair[0], TEST1, strlen(TEST1)+1);
   1327 		test_ok = 1;
   1328 	}
   1329 
   1330 	called++;
   1331 }
   1332 
   1333 static void
   1334 test_want_only_once(void)
   1335 {
   1336 	struct event ev;
   1337 	struct timeval tv;
   1338 
   1339 	/* Very simple read test */
   1340 	setup_test("Want read only once: ");
   1341 
   1342 	write(pair[0], TEST1, strlen(TEST1)+1);
   1343 
   1344 	/* Setup the loop termination */
   1345 	evutil_timerclear(&tv);
   1346 	tv.tv_sec = 1;
   1347 	event_loopexit(&tv);
   1348 
   1349 	event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
   1350 	if (event_add(&ev, NULL) == -1)
   1351 		exit(1);
   1352 	event_dispatch();
   1353 
   1354 	cleanup_test();
   1355 }
   1356 
   1357 #define TEST_MAX_INT	6
   1358 
   1359 static void
   1360 evtag_int_test(void)
   1361 {
   1362 	struct evbuffer *tmp = evbuffer_new();
   1363 	uint32_t integers[TEST_MAX_INT] = {
   1364 		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
   1365 	};
   1366 	uint32_t integer;
   1367 	int i;
   1368 
   1369 	for (i = 0; i < TEST_MAX_INT; i++) {
   1370 		int oldlen, newlen;
   1371 		oldlen = EVBUFFER_LENGTH(tmp);
   1372 		encode_int(tmp, integers[i]);
   1373 		newlen = EVBUFFER_LENGTH(tmp);
   1374 		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
   1375 		    integers[i], newlen - oldlen);
   1376 	}
   1377 
   1378 	for (i = 0; i < TEST_MAX_INT; i++) {
   1379 		if (evtag_decode_int(&integer, tmp) == -1) {
   1380 			fprintf(stderr, "decode %d failed", i);
   1381 			exit(1);
   1382 		}
   1383 		if (integer != integers[i]) {
   1384 			fprintf(stderr, "got %x, wanted %x",
   1385 			    integer, integers[i]);
   1386 			exit(1);
   1387 		}
   1388 	}
   1389 
   1390 	if (EVBUFFER_LENGTH(tmp) != 0) {
   1391 		fprintf(stderr, "trailing data");
   1392 		exit(1);
   1393 	}
   1394 	evbuffer_free(tmp);
   1395 
   1396 	fprintf(stdout, "\t%s: OK\n", __func__);
   1397 }
   1398 
   1399 static void
   1400 evtag_fuzz(void)
   1401 {
   1402 	u_char buffer[4096];
   1403 	struct evbuffer *tmp = evbuffer_new();
   1404 	struct timeval tv;
   1405 	int i, j;
   1406 
   1407 	int not_failed = 0;
   1408 	for (j = 0; j < 100; j++) {
   1409 		for (i = 0; i < sizeof(buffer); i++)
   1410 			buffer[i] = rand();
   1411 		evbuffer_drain(tmp, -1);
   1412 		evbuffer_add(tmp, buffer, sizeof(buffer));
   1413 
   1414 		if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
   1415 			not_failed++;
   1416 	}
   1417 
   1418 	/* The majority of decodes should fail */
   1419 	if (not_failed >= 10) {
   1420 		fprintf(stderr, "evtag_unmarshal should have failed");
   1421 		exit(1);
   1422 	}
   1423 
   1424 	/* Now insert some corruption into the tag length field */
   1425 	evbuffer_drain(tmp, -1);
   1426 	evutil_timerclear(&tv);
   1427 	tv.tv_sec = 1;
   1428 	evtag_marshal_timeval(tmp, 0, &tv);
   1429 	evbuffer_add(tmp, buffer, sizeof(buffer));
   1430 
   1431 	EVBUFFER_DATA(tmp)[1] = 0xff;
   1432 	if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
   1433 		fprintf(stderr, "evtag_unmarshal_timeval should have failed");
   1434 		exit(1);
   1435 	}
   1436 
   1437 	evbuffer_free(tmp);
   1438 
   1439 	fprintf(stdout, "\t%s: OK\n", __func__);
   1440 }
   1441 
   1442 static void
   1443 evtag_tag_encoding(void)
   1444 {
   1445 	struct evbuffer *tmp = evbuffer_new();
   1446 	uint32_t integers[TEST_MAX_INT] = {
   1447 		0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
   1448 	};
   1449 	uint32_t integer;
   1450 	int i;
   1451 
   1452 	for (i = 0; i < TEST_MAX_INT; i++) {
   1453 		int oldlen, newlen;
   1454 		oldlen = EVBUFFER_LENGTH(tmp);
   1455 		evtag_encode_tag(tmp, integers[i]);
   1456 		newlen = EVBUFFER_LENGTH(tmp);
   1457 		fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
   1458 		    integers[i], newlen - oldlen);
   1459 	}
   1460 
   1461 	for (i = 0; i < TEST_MAX_INT; i++) {
   1462 		if (evtag_decode_tag(&integer, tmp) == -1) {
   1463 			fprintf(stderr, "decode %d failed", i);
   1464 			exit(1);
   1465 		}
   1466 		if (integer != integers[i]) {
   1467 			fprintf(stderr, "got %x, wanted %x",
   1468 			    integer, integers[i]);
   1469 			exit(1);
   1470 		}
   1471 	}
   1472 
   1473 	if (EVBUFFER_LENGTH(tmp) != 0) {
   1474 		fprintf(stderr, "trailing data");
   1475 		exit(1);
   1476 	}
   1477 	evbuffer_free(tmp);
   1478 
   1479 	fprintf(stdout, "\t%s: OK\n", __func__);
   1480 }
   1481 
   1482 static void
   1483 evtag_test(void)
   1484 {
   1485 	fprintf(stdout, "Testing Tagging:\n");
   1486 
   1487 	evtag_init();
   1488 	evtag_int_test();
   1489 	evtag_fuzz();
   1490 
   1491 	evtag_tag_encoding();
   1492 
   1493 	fprintf(stdout, "OK\n");
   1494 }
   1495 
   1496 #ifndef WIN32
   1497 static void
   1498 rpc_test(void)
   1499 {
   1500 	struct msg *msg, *msg2;
   1501 	struct kill *attack;
   1502 	struct run *run;
   1503 	struct evbuffer *tmp = evbuffer_new();
   1504 	struct timeval tv_start, tv_end;
   1505 	uint32_t tag;
   1506 	int i;
   1507 
   1508 	fprintf(stdout, "Testing RPC: ");
   1509 
   1510 	msg = msg_new();
   1511 	EVTAG_ASSIGN(msg, from_name, "niels");
   1512 	EVTAG_ASSIGN(msg, to_name, "phoenix");
   1513 
   1514 	if (EVTAG_GET(msg, attack, &attack) == -1) {
   1515 		fprintf(stderr, "Failed to set kill message.\n");
   1516 		exit(1);
   1517 	}
   1518 
   1519 	EVTAG_ASSIGN(attack, weapon, "feather");
   1520 	EVTAG_ASSIGN(attack, action, "tickle");
   1521 
   1522 	evutil_gettimeofday(&tv_start, NULL);
   1523 	for (i = 0; i < 1000; ++i) {
   1524 		run = EVTAG_ADD(msg, run);
   1525 		if (run == NULL) {
   1526 			fprintf(stderr, "Failed to add run message.\n");
   1527 			exit(1);
   1528 		}
   1529 		EVTAG_ASSIGN(run, how, "very fast but with some data in it");
   1530 		EVTAG_ASSIGN(run, fixed_bytes,
   1531 		    (unsigned char*)"012345678901234567890123");
   1532 	}
   1533 
   1534 	if (msg_complete(msg) == -1) {
   1535 		fprintf(stderr, "Failed to make complete message.\n");
   1536 		exit(1);
   1537 	}
   1538 
   1539 	evtag_marshal_msg(tmp, 0xdeaf, msg);
   1540 
   1541 	if (evtag_peek(tmp, &tag) == -1) {
   1542 		fprintf(stderr, "Failed to peak tag.\n");
   1543 		exit (1);
   1544 	}
   1545 
   1546 	if (tag != 0xdeaf) {
   1547 		fprintf(stderr, "Got incorrect tag: %0x.\n", tag);
   1548 		exit (1);
   1549 	}
   1550 
   1551 	msg2 = msg_new();
   1552 	if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1) {
   1553 		fprintf(stderr, "Failed to unmarshal message.\n");
   1554 		exit(1);
   1555 	}
   1556 
   1557 	evutil_gettimeofday(&tv_end, NULL);
   1558 	evutil_timersub(&tv_end, &tv_start, &tv_end);
   1559 	fprintf(stderr, "(%.1f us/add) ",
   1560 	    (float)tv_end.tv_sec/(float)i * 1000000.0 +
   1561 	    tv_end.tv_usec / (float)i);
   1562 
   1563 	if (!EVTAG_HAS(msg2, from_name) ||
   1564 	    !EVTAG_HAS(msg2, to_name) ||
   1565 	    !EVTAG_HAS(msg2, attack)) {
   1566 		fprintf(stderr, "Missing data structures.\n");
   1567 		exit(1);
   1568 	}
   1569 
   1570 	if (EVTAG_LEN(msg2, run) != i) {
   1571 		fprintf(stderr, "Wrong number of run messages.\n");
   1572 		exit(1);
   1573 	}
   1574 
   1575 	msg_free(msg);
   1576 	msg_free(msg2);
   1577 
   1578 	evbuffer_free(tmp);
   1579 
   1580 	fprintf(stdout, "OK\n");
   1581 }
   1582 #endif
   1583 
   1584 static void
   1585 test_evutil_strtoll(void)
   1586 {
   1587         const char *s;
   1588         char *endptr;
   1589         setup_test("evutil_stroll: ");
   1590         test_ok = 0;
   1591 
   1592         if (evutil_strtoll("5000000000", NULL, 10) != ((ev_int64_t)5000000)*1000)
   1593                 goto err;
   1594         if (evutil_strtoll("-5000000000", NULL, 10) != ((ev_int64_t)5000000)*-1000)
   1595                 goto err;
   1596         s = " 99999stuff";
   1597         if (evutil_strtoll(s, &endptr, 10) != (ev_int64_t)99999)
   1598                 goto err;
   1599         if (endptr != s+6)
   1600                 goto err;
   1601         if (evutil_strtoll("foo", NULL, 10) != 0)
   1602                 goto err;
   1603 
   1604         test_ok = 1;
   1605  err:
   1606         cleanup_test();
   1607 }
   1608 
   1609 
   1610 int
   1611 main (int argc, char **argv)
   1612 {
   1613 #ifdef WIN32
   1614 	WORD wVersionRequested;
   1615 	WSADATA wsaData;
   1616 	int	err;
   1617 
   1618 	wVersionRequested = MAKEWORD( 2, 2 );
   1619 
   1620 	err = WSAStartup( wVersionRequested, &wsaData );
   1621 #endif
   1622 
   1623 #ifndef WIN32
   1624 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
   1625 		return (1);
   1626 #endif
   1627 	setvbuf(stdout, NULL, _IONBF, 0);
   1628 
   1629 	/* Initalize the event library */
   1630 	global_base = event_init();
   1631 
   1632 	test_registerfds();
   1633 
   1634         test_evutil_strtoll();
   1635 
   1636 	/* use the global event base and need to be called first */
   1637 	test_priorities(1);
   1638 	test_priorities(2);
   1639 	test_priorities(3);
   1640 
   1641 	test_evbuffer();
   1642 	test_evbuffer_find();
   1643 
   1644 	test_bufferevent();
   1645 	test_bufferevent_watermarks();
   1646 
   1647 	test_free_active_base();
   1648 
   1649 	test_event_base_new();
   1650 
   1651 	http_suite();
   1652 
   1653 #ifndef WIN32
   1654 	rpc_suite();
   1655 #endif
   1656 
   1657 	dns_suite();
   1658 
   1659 #ifndef WIN32
   1660 	test_fork();
   1661 #endif
   1662 
   1663 	test_simpleread();
   1664 
   1665 	test_simplewrite();
   1666 
   1667 	test_multiple();
   1668 
   1669 	test_persistent();
   1670 
   1671 	test_combined();
   1672 
   1673 	test_simpletimeout();
   1674 #ifndef WIN32
   1675 	test_simplesignal();
   1676 	test_multiplesignal();
   1677 	test_immediatesignal();
   1678 #endif
   1679 	test_loopexit();
   1680 	test_loopbreak();
   1681 
   1682 	test_loopexit_multiple();
   1683 
   1684 	test_multiple_events_for_same_fd();
   1685 
   1686 	test_want_only_once();
   1687 
   1688 	evtag_test();
   1689 
   1690 #ifndef WIN32
   1691 	rpc_test();
   1692 
   1693 	test_signal_dealloc();
   1694 	test_signal_pipeloss();
   1695 	test_signal_switchbase();
   1696 	test_signal_restore();
   1697 	test_signal_assert();
   1698 	test_signal_while_processing();
   1699 #endif
   1700 
   1701 	return (0);
   1702 }
   1703 
   1704