Expand PMF_FN_* macros.
[netbsd-mini2440.git] / external / bsd / libevent / dist / test / regress.c
blob765b0c0eafc2187136bc96a34c9f41de700bf08e
1 /* $NetBSD$ */
2 /*
3 * Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #ifdef WIN32
30 #include <winsock2.h>
31 #include <windows.h>
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #include <sys/queue.h>
44 #ifndef WIN32
45 #include <sys/socket.h>
46 #include <sys/wait.h>
47 #include <signal.h>
48 #include <unistd.h>
49 #include <netdb.h>
50 #endif
51 #include <assert.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <errno.h>
59 #include "event.h"
60 #include "evutil.h"
61 #include "event-internal.h"
62 #include "log.h"
64 #include "regress.h"
65 #ifndef WIN32
66 #include "regress.gen.h"
67 #endif
69 int pair[2];
70 int test_ok;
71 static int called;
72 static char wbuf[4096];
73 static char rbuf[4096];
74 static int woff;
75 static int roff;
76 static int usepersist;
77 static struct timeval tset;
78 static struct timeval tcalled;
79 static struct event_base *global_base;
81 #define TEST1 "this is a test"
82 #define SECONDS 1
84 #ifndef SHUT_WR
85 #define SHUT_WR 1
86 #endif
88 #ifdef WIN32
89 #define write(fd,buf,len) send((fd),(buf),(len),0)
90 #define read(fd,buf,len) recv((fd),(buf),(len),0)
91 #endif
93 static void
94 simple_read_cb(int fd, short event, void *arg)
96 char buf[256];
97 int len;
99 if (arg == NULL)
100 return;
102 len = read(fd, buf, sizeof(buf));
104 if (len) {
105 if (!called) {
106 if (event_add(arg, NULL) == -1)
107 exit(1);
109 } else if (called == 1)
110 test_ok = 1;
112 called++;
115 static void
116 simple_write_cb(int fd, short event, void *arg)
118 int len;
120 if (arg == NULL)
121 return;
123 len = write(fd, TEST1, strlen(TEST1) + 1);
124 if (len == -1)
125 test_ok = 0;
126 else
127 test_ok = 1;
130 static void
131 multiple_write_cb(int fd, short event, void *arg)
133 struct event *ev = arg;
134 int len;
136 len = 128;
137 if (woff + len >= sizeof(wbuf))
138 len = sizeof(wbuf) - woff;
140 len = write(fd, wbuf + woff, len);
141 if (len == -1) {
142 fprintf(stderr, "%s: write\n", __func__);
143 if (usepersist)
144 event_del(ev);
145 return;
148 woff += len;
150 if (woff >= sizeof(wbuf)) {
151 shutdown(fd, SHUT_WR);
152 if (usepersist)
153 event_del(ev);
154 return;
157 if (!usepersist) {
158 if (event_add(ev, NULL) == -1)
159 exit(1);
163 static void
164 multiple_read_cb(int fd, short event, void *arg)
166 struct event *ev = arg;
167 int len;
169 len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
170 if (len == -1)
171 fprintf(stderr, "%s: read\n", __func__);
172 if (len <= 0) {
173 if (usepersist)
174 event_del(ev);
175 return;
178 roff += len;
179 if (!usepersist) {
180 if (event_add(ev, NULL) == -1)
181 exit(1);
185 static void
186 timeout_cb(int fd, short event, void *arg)
188 struct timeval tv;
189 int diff;
191 evutil_gettimeofday(&tcalled, NULL);
192 if (evutil_timercmp(&tcalled, &tset, >))
193 evutil_timersub(&tcalled, &tset, &tv);
194 else
195 evutil_timersub(&tset, &tcalled, &tv);
197 diff = tv.tv_sec*1000 + tv.tv_usec/1000 - SECONDS * 1000;
198 if (diff < 0)
199 diff = -diff;
201 if (diff < 100)
202 test_ok = 1;
205 #ifndef WIN32
206 static void
207 signal_cb_sa(int sig)
209 test_ok = 2;
212 static void
213 signal_cb(int fd, short event, void *arg)
215 struct event *ev = arg;
217 signal_del(ev);
218 test_ok = 1;
220 #endif
222 struct both {
223 struct event ev;
224 int nread;
227 static void
228 combined_read_cb(int fd, short event, void *arg)
230 struct both *both = arg;
231 char buf[128];
232 int len;
234 len = read(fd, buf, sizeof(buf));
235 if (len == -1)
236 fprintf(stderr, "%s: read\n", __func__);
237 if (len <= 0)
238 return;
240 both->nread += len;
241 if (event_add(&both->ev, NULL) == -1)
242 exit(1);
245 static void
246 combined_write_cb(int fd, short event, void *arg)
248 struct both *both = arg;
249 char buf[128];
250 int len;
252 len = sizeof(buf);
253 if (len > both->nread)
254 len = both->nread;
256 len = write(fd, buf, len);
257 if (len == -1)
258 fprintf(stderr, "%s: write\n", __func__);
259 if (len <= 0) {
260 shutdown(fd, SHUT_WR);
261 return;
264 both->nread -= len;
265 if (event_add(&both->ev, NULL) == -1)
266 exit(1);
269 /* Test infrastructure */
271 static int
272 setup_test(const char *name)
275 fprintf(stdout, "%s", name);
277 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
278 fprintf(stderr, "%s: socketpair\n", __func__);
279 exit(1);
282 #ifdef HAVE_FCNTL
283 if (fcntl(pair[0], F_SETFL, O_NONBLOCK) == -1)
284 fprintf(stderr, "fcntl(O_NONBLOCK)");
286 if (fcntl(pair[1], F_SETFL, O_NONBLOCK) == -1)
287 fprintf(stderr, "fcntl(O_NONBLOCK)");
288 #endif
290 test_ok = 0;
291 called = 0;
292 return (0);
295 static int
296 cleanup_test(void)
298 #ifndef WIN32
299 close(pair[0]);
300 close(pair[1]);
301 #else
302 CloseHandle((HANDLE)pair[0]);
303 CloseHandle((HANDLE)pair[1]);
304 #endif
305 if (test_ok)
306 fprintf(stdout, "OK\n");
307 else {
308 fprintf(stdout, "FAILED\n");
309 exit(1);
311 test_ok = 0;
312 return (0);
315 static void
316 test_registerfds(void)
318 int i, j;
319 int pair[2];
320 struct event read_evs[512];
321 struct event write_evs[512];
323 struct event_base *base = event_base_new();
325 fprintf(stdout, "Testing register fds: ");
327 for (i = 0; i < 512; ++i) {
328 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
329 /* run up to the limit of file descriptors */
330 break;
332 event_set(&read_evs[i], pair[0],
333 EV_READ|EV_PERSIST, simple_read_cb, NULL);
334 event_base_set(base, &read_evs[i]);
335 event_add(&read_evs[i], NULL);
336 event_set(&write_evs[i], pair[1],
337 EV_WRITE|EV_PERSIST, simple_write_cb, NULL);
338 event_base_set(base, &write_evs[i]);
339 event_add(&write_evs[i], NULL);
341 /* just loop once */
342 event_base_loop(base, EVLOOP_ONCE);
345 /* now delete everything */
346 for (j = 0; j < i; ++j) {
347 event_del(&read_evs[j]);
348 event_del(&write_evs[j]);
349 #ifndef WIN32
350 close(read_evs[j].ev_fd);
351 close(write_evs[j].ev_fd);
352 #else
353 CloseHandle((HANDLE)read_evs[j].ev_fd);
354 CloseHandle((HANDLE)write_evs[j].ev_fd);
355 #endif
357 /* just loop once */
358 event_base_loop(base, EVLOOP_ONCE);
361 event_base_free(base);
363 fprintf(stdout, "OK\n");
366 static void
367 test_simpleread(void)
369 struct event ev;
371 /* Very simple read test */
372 setup_test("Simple read: ");
374 write(pair[0], TEST1, strlen(TEST1)+1);
375 shutdown(pair[0], SHUT_WR);
377 event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
378 if (event_add(&ev, NULL) == -1)
379 exit(1);
380 event_dispatch();
382 cleanup_test();
385 static void
386 test_simplewrite(void)
388 struct event ev;
390 /* Very simple write test */
391 setup_test("Simple write: ");
393 event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
394 if (event_add(&ev, NULL) == -1)
395 exit(1);
396 event_dispatch();
398 cleanup_test();
401 static void
402 test_multiple(void)
404 struct event ev, ev2;
405 int i;
407 /* Multiple read and write test */
408 setup_test("Multiple read/write: ");
409 memset(rbuf, 0, sizeof(rbuf));
410 for (i = 0; i < sizeof(wbuf); i++)
411 wbuf[i] = i;
413 roff = woff = 0;
414 usepersist = 0;
416 event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
417 if (event_add(&ev, NULL) == -1)
418 exit(1);
419 event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
420 if (event_add(&ev2, NULL) == -1)
421 exit(1);
422 event_dispatch();
424 if (roff == woff)
425 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
427 cleanup_test();
430 static void
431 test_persistent(void)
433 struct event ev, ev2;
434 int i;
436 /* Multiple read and write test with persist */
437 setup_test("Persist read/write: ");
438 memset(rbuf, 0, sizeof(rbuf));
439 for (i = 0; i < sizeof(wbuf); i++)
440 wbuf[i] = i;
442 roff = woff = 0;
443 usepersist = 1;
445 event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
446 if (event_add(&ev, NULL) == -1)
447 exit(1);
448 event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
449 if (event_add(&ev2, NULL) == -1)
450 exit(1);
451 event_dispatch();
453 if (roff == woff)
454 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
456 cleanup_test();
459 static void
460 test_combined(void)
462 struct both r1, r2, w1, w2;
464 setup_test("Combined read/write: ");
465 memset(&r1, 0, sizeof(r1));
466 memset(&r2, 0, sizeof(r2));
467 memset(&w1, 0, sizeof(w1));
468 memset(&w2, 0, sizeof(w2));
470 w1.nread = 4096;
471 w2.nread = 8192;
473 event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
474 event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
475 event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
476 event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
477 if (event_add(&r1.ev, NULL) == -1)
478 exit(1);
479 if (event_add(&w1.ev, NULL))
480 exit(1);
481 if (event_add(&r2.ev, NULL))
482 exit(1);
483 if (event_add(&w2.ev, NULL))
484 exit(1);
486 event_dispatch();
488 if (r1.nread == 8192 && r2.nread == 4096)
489 test_ok = 1;
491 cleanup_test();
494 static void
495 test_simpletimeout(void)
497 struct timeval tv;
498 struct event ev;
500 setup_test("Simple timeout: ");
502 tv.tv_usec = 0;
503 tv.tv_sec = SECONDS;
504 evtimer_set(&ev, timeout_cb, NULL);
505 evtimer_add(&ev, &tv);
507 evutil_gettimeofday(&tset, NULL);
508 event_dispatch();
510 cleanup_test();
513 #ifndef WIN32
514 extern struct event_base *current_base;
516 static void
517 child_signal_cb(int fd, short event, void *arg)
519 struct timeval tv;
520 int *pint = arg;
522 *pint = 1;
524 tv.tv_usec = 500000;
525 tv.tv_sec = 0;
526 event_loopexit(&tv);
529 static void
530 test_fork(void)
532 int status, got_sigchld = 0;
533 struct event ev, sig_ev;
534 pid_t pid;
536 setup_test("After fork: ");
538 write(pair[0], TEST1, strlen(TEST1)+1);
540 event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
541 if (event_add(&ev, NULL) == -1)
542 exit(1);
544 signal_set(&sig_ev, SIGCHLD, child_signal_cb, &got_sigchld);
545 signal_add(&sig_ev, NULL);
547 if ((pid = fork()) == 0) {
548 /* in the child */
549 if (event_reinit(current_base) == -1) {
550 fprintf(stderr, "FAILED (reinit)\n");
551 exit(1);
554 signal_del(&sig_ev);
556 called = 0;
558 event_dispatch();
560 /* we do not send an EOF; simple_read_cb requires an EOF
561 * to set test_ok. we just verify that the callback was
562 * called. */
563 exit(test_ok != 0 || called != 2 ? -2 : 76);
566 /* wait for the child to read the data */
567 sleep(1);
569 write(pair[0], TEST1, strlen(TEST1)+1);
571 if (waitpid(pid, &status, 0) == -1) {
572 fprintf(stderr, "FAILED (fork)\n");
573 exit(1);
576 if (WEXITSTATUS(status) != 76) {
577 fprintf(stderr, "FAILED (exit): %d\n", WEXITSTATUS(status));
578 exit(1);
581 /* test that the current event loop still works */
582 write(pair[0], TEST1, strlen(TEST1)+1);
583 shutdown(pair[0], SHUT_WR);
585 event_dispatch();
587 if (!got_sigchld) {
588 fprintf(stdout, "FAILED (sigchld)\n");
589 exit(1);
592 signal_del(&sig_ev);
594 cleanup_test();
597 static void
598 test_simplesignal(void)
600 struct event ev;
601 struct itimerval itv;
603 setup_test("Simple signal: ");
604 signal_set(&ev, SIGALRM, signal_cb, &ev);
605 signal_add(&ev, NULL);
606 /* find bugs in which operations are re-ordered */
607 signal_del(&ev);
608 signal_add(&ev, NULL);
610 memset(&itv, 0, sizeof(itv));
611 itv.it_value.tv_sec = 1;
612 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
613 goto skip_simplesignal;
615 event_dispatch();
616 skip_simplesignal:
617 if (signal_del(&ev) == -1)
618 test_ok = 0;
620 cleanup_test();
623 static void
624 test_multiplesignal(void)
626 struct event ev_one, ev_two;
627 struct itimerval itv;
629 setup_test("Multiple signal: ");
631 signal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
632 signal_add(&ev_one, NULL);
634 signal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
635 signal_add(&ev_two, NULL);
637 memset(&itv, 0, sizeof(itv));
638 itv.it_value.tv_sec = 1;
639 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
640 goto skip_simplesignal;
642 event_dispatch();
644 skip_simplesignal:
645 if (signal_del(&ev_one) == -1)
646 test_ok = 0;
647 if (signal_del(&ev_two) == -1)
648 test_ok = 0;
650 cleanup_test();
653 static void
654 test_immediatesignal(void)
656 struct event ev;
658 test_ok = 0;
659 printf("Immediate signal: ");
660 signal_set(&ev, SIGUSR1, signal_cb, &ev);
661 signal_add(&ev, NULL);
662 raise(SIGUSR1);
663 event_loop(EVLOOP_NONBLOCK);
664 signal_del(&ev);
665 cleanup_test();
668 static void
669 test_signal_dealloc(void)
671 /* make sure that signal_event is event_del'ed and pipe closed */
672 struct event ev;
673 struct event_base *base = event_init();
674 printf("Signal dealloc: ");
675 signal_set(&ev, SIGUSR1, signal_cb, &ev);
676 signal_add(&ev, NULL);
677 signal_del(&ev);
678 event_base_free(base);
679 /* If we got here without asserting, we're fine. */
680 test_ok = 1;
681 cleanup_test();
684 static void
685 test_signal_pipeloss(void)
687 /* make sure that the base1 pipe is closed correctly. */
688 struct event_base *base1, *base2;
689 int pipe1;
690 test_ok = 0;
691 printf("Signal pipeloss: ");
692 base1 = event_init();
693 pipe1 = base1->sig.ev_signal_pair[0];
694 base2 = event_init();
695 event_base_free(base2);
696 event_base_free(base1);
697 if (close(pipe1) != -1 || errno!=EBADF) {
698 /* fd must be closed, so second close gives -1, EBADF */
699 printf("signal pipe not closed. ");
700 test_ok = 0;
701 } else {
702 test_ok = 1;
704 cleanup_test();
708 * make two bases to catch signals, use both of them. this only works
709 * for event mechanisms that use our signal pipe trick. kqueue handles
710 * signals internally, and all interested kqueues get all the signals.
712 static void
713 test_signal_switchbase(void)
715 struct event ev1, ev2;
716 struct event_base *base1, *base2;
717 int is_kqueue;
718 test_ok = 0;
719 printf("Signal switchbase: ");
720 base1 = event_init();
721 base2 = event_init();
722 is_kqueue = !strcmp(event_get_method(),"kqueue");
723 signal_set(&ev1, SIGUSR1, signal_cb, &ev1);
724 signal_set(&ev2, SIGUSR1, signal_cb, &ev2);
725 if (event_base_set(base1, &ev1) ||
726 event_base_set(base2, &ev2) ||
727 event_add(&ev1, NULL) ||
728 event_add(&ev2, NULL)) {
729 fprintf(stderr, "%s: cannot set base, add\n", __func__);
730 exit(1);
733 test_ok = 0;
734 /* can handle signal before loop is called */
735 raise(SIGUSR1);
736 event_base_loop(base2, EVLOOP_NONBLOCK);
737 if (is_kqueue) {
738 if (!test_ok)
739 goto done;
740 test_ok = 0;
742 event_base_loop(base1, EVLOOP_NONBLOCK);
743 if (test_ok && !is_kqueue) {
744 test_ok = 0;
746 /* set base1 to handle signals */
747 event_base_loop(base1, EVLOOP_NONBLOCK);
748 raise(SIGUSR1);
749 event_base_loop(base1, EVLOOP_NONBLOCK);
750 event_base_loop(base2, EVLOOP_NONBLOCK);
752 done:
753 event_base_free(base1);
754 event_base_free(base2);
755 cleanup_test();
759 * assert that a signal event removed from the event queue really is
760 * removed - with no possibility of it's parent handler being fired.
762 static void
763 test_signal_assert(void)
765 struct event ev;
766 struct event_base *base = event_init();
767 test_ok = 0;
768 printf("Signal handler assert: ");
769 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
770 signal_set(&ev, SIGCONT, signal_cb, &ev);
771 signal_add(&ev, NULL);
773 * if signal_del() fails to reset the handler, it's current handler
774 * will still point to evsignal_handler().
776 signal_del(&ev);
778 raise(SIGCONT);
779 /* only way to verify we were in evsignal_handler() */
780 if (base->sig.evsignal_caught)
781 test_ok = 0;
782 else
783 test_ok = 1;
785 event_base_free(base);
786 cleanup_test();
787 return;
791 * assert that we restore our previous signal handler properly.
793 static void
794 test_signal_restore(void)
796 struct event ev;
797 struct event_base *base = event_init();
798 #ifdef HAVE_SIGACTION
799 struct sigaction sa;
800 #endif
802 test_ok = 0;
803 printf("Signal handler restore: ");
804 #ifdef HAVE_SIGACTION
805 sa.sa_handler = signal_cb_sa;
806 sa.sa_flags = 0x0;
807 sigemptyset(&sa.sa_mask);
808 if (sigaction(SIGUSR1, &sa, NULL) == -1)
809 goto out;
810 #else
811 if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
812 goto out;
813 #endif
814 signal_set(&ev, SIGUSR1, signal_cb, &ev);
815 signal_add(&ev, NULL);
816 signal_del(&ev);
818 raise(SIGUSR1);
819 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
820 if (test_ok != 2)
821 test_ok = 0;
822 out:
823 event_base_free(base);
824 cleanup_test();
825 return;
828 static void
829 signal_cb_swp(int sig, short event, void *arg)
831 called++;
832 if (called < 5)
833 raise(sig);
834 else
835 event_loopexit(NULL);
837 static void
838 timeout_cb_swp(int fd, short event, void *arg)
840 if (called == -1) {
841 struct timeval tv = {5, 0};
843 called = 0;
844 evtimer_add((struct event *)arg, &tv);
845 raise(SIGUSR1);
846 return;
848 test_ok = 0;
849 event_loopexit(NULL);
852 static void
853 test_signal_while_processing(void)
855 struct event_base *base = event_init();
856 struct event ev, ev_timer;
857 struct timeval tv = {0, 0};
859 setup_test("Receiving a signal while processing other signal: ");
861 called = -1;
862 test_ok = 1;
863 signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
864 signal_add(&ev, NULL);
865 evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
866 evtimer_add(&ev_timer, &tv);
867 event_dispatch();
869 event_base_free(base);
870 cleanup_test();
871 return;
873 #endif
875 static void
876 test_free_active_base(void)
878 struct event_base *base1;
879 struct event ev1;
880 setup_test("Free active base: ");
881 base1 = event_init();
882 event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
883 event_base_set(base1, &ev1);
884 event_add(&ev1, NULL);
885 /* event_del(&ev1); */
886 event_base_free(base1);
887 test_ok = 1;
888 cleanup_test();
891 static void
892 test_event_base_new(void)
894 struct event_base *base;
895 struct event ev1;
896 setup_test("Event base new: ");
898 write(pair[0], TEST1, strlen(TEST1)+1);
899 shutdown(pair[0], SHUT_WR);
901 base = event_base_new();
902 event_set(&ev1, pair[1], EV_READ, simple_read_cb, &ev1);
903 event_base_set(base, &ev1);
904 event_add(&ev1, NULL);
906 event_base_dispatch(base);
908 event_base_free(base);
909 test_ok = 1;
910 cleanup_test();
913 static void
914 test_loopexit(void)
916 struct timeval tv, tv_start, tv_end;
917 struct event ev;
919 setup_test("Loop exit: ");
921 tv.tv_usec = 0;
922 tv.tv_sec = 60*60*24;
923 evtimer_set(&ev, timeout_cb, NULL);
924 evtimer_add(&ev, &tv);
926 tv.tv_usec = 0;
927 tv.tv_sec = 1;
928 event_loopexit(&tv);
930 evutil_gettimeofday(&tv_start, NULL);
931 event_dispatch();
932 evutil_gettimeofday(&tv_end, NULL);
933 evutil_timersub(&tv_end, &tv_start, &tv_end);
935 evtimer_del(&ev);
937 if (tv.tv_sec < 2)
938 test_ok = 1;
940 cleanup_test();
943 static void
944 test_loopexit_multiple(void)
946 struct timeval tv;
947 struct event_base *base;
949 setup_test("Loop Multiple exit: ");
951 base = event_base_new();
953 tv.tv_usec = 0;
954 tv.tv_sec = 1;
955 event_base_loopexit(base, &tv);
957 tv.tv_usec = 0;
958 tv.tv_sec = 2;
959 event_base_loopexit(base, &tv);
961 event_base_dispatch(base);
963 event_base_free(base);
965 test_ok = 1;
967 cleanup_test();
970 static void
971 break_cb(int fd, short events, void *arg)
973 test_ok = 1;
974 event_loopbreak();
977 static void
978 fail_cb(int fd, short events, void *arg)
980 test_ok = 0;
983 static void
984 test_loopbreak(void)
986 struct event ev1, ev2;
987 struct timeval tv;
989 setup_test("Loop break: ");
991 tv.tv_sec = 0;
992 tv.tv_usec = 0;
993 evtimer_set(&ev1, break_cb, NULL);
994 evtimer_add(&ev1, &tv);
995 evtimer_set(&ev2, fail_cb, NULL);
996 evtimer_add(&ev2, &tv);
998 event_dispatch();
1000 evtimer_del(&ev1);
1001 evtimer_del(&ev2);
1003 cleanup_test();
1006 static void
1007 test_evbuffer(void) {
1009 struct evbuffer *evb = evbuffer_new();
1010 setup_test("Testing Evbuffer: ");
1012 evbuffer_add_printf(evb, "%s/%d", "hello", 1);
1014 if (EVBUFFER_LENGTH(evb) == 7 &&
1015 strcmp((char*)EVBUFFER_DATA(evb), "hello/1") == 0)
1016 test_ok = 1;
1018 evbuffer_free(evb);
1020 cleanup_test();
1023 static void
1024 test_evbuffer_find(void)
1026 u_char* p;
1027 const char* test1 = "1234567890\r\n";
1028 const char* test2 = "1234567890\r";
1029 #define EVBUFFER_INITIAL_LENGTH 256
1030 char test3[EVBUFFER_INITIAL_LENGTH];
1031 unsigned int i;
1032 struct evbuffer * buf = evbuffer_new();
1034 /* make sure evbuffer_find doesn't match past the end of the buffer */
1035 fprintf(stdout, "Testing evbuffer_find 1: ");
1036 evbuffer_add(buf, (u_char*)test1, strlen(test1));
1037 evbuffer_drain(buf, strlen(test1));
1038 evbuffer_add(buf, (u_char*)test2, strlen(test2));
1039 p = evbuffer_find(buf, (u_char*)"\r\n", 2);
1040 if (p == NULL) {
1041 fprintf(stdout, "OK\n");
1042 } else {
1043 fprintf(stdout, "FAILED\n");
1044 exit(1);
1048 * drain the buffer and do another find; in r309 this would
1049 * read past the allocated buffer causing a valgrind error.
1051 fprintf(stdout, "Testing evbuffer_find 2: ");
1052 evbuffer_drain(buf, strlen(test2));
1053 for (i = 0; i < EVBUFFER_INITIAL_LENGTH; ++i)
1054 test3[i] = 'a';
1055 test3[EVBUFFER_INITIAL_LENGTH - 1] = 'x';
1056 evbuffer_add(buf, (u_char *)test3, EVBUFFER_INITIAL_LENGTH);
1057 p = evbuffer_find(buf, (u_char *)"xy", 2);
1058 if (p == NULL) {
1059 printf("OK\n");
1060 } else {
1061 fprintf(stdout, "FAILED\n");
1062 exit(1);
1065 /* simple test for match at end of allocated buffer */
1066 fprintf(stdout, "Testing evbuffer_find 3: ");
1067 p = evbuffer_find(buf, (u_char *)"ax", 2);
1068 if (p != NULL && strncmp((char*)p, "ax", 2) == 0) {
1069 printf("OK\n");
1070 } else {
1071 fprintf(stdout, "FAILED\n");
1072 exit(1);
1075 evbuffer_free(buf);
1079 * simple bufferevent test
1082 static void
1083 readcb(struct bufferevent *bev, void *arg)
1085 if (EVBUFFER_LENGTH(bev->input) == 8333) {
1086 bufferevent_disable(bev, EV_READ);
1087 test_ok++;
1091 static void
1092 writecb(struct bufferevent *bev, void *arg)
1094 if (EVBUFFER_LENGTH(bev->output) == 0)
1095 test_ok++;
1098 static void
1099 errorcb(struct bufferevent *bev, short what, void *arg)
1101 test_ok = -2;
1104 static void
1105 test_bufferevent(void)
1107 struct bufferevent *bev1, *bev2;
1108 char buffer[8333];
1109 int i;
1111 setup_test("Bufferevent: ");
1113 bev1 = bufferevent_new(pair[0], readcb, writecb, errorcb, NULL);
1114 bev2 = bufferevent_new(pair[1], readcb, writecb, errorcb, NULL);
1116 bufferevent_disable(bev1, EV_READ);
1117 bufferevent_enable(bev2, EV_READ);
1119 for (i = 0; i < sizeof(buffer); i++)
1120 buffer[i] = i;
1122 bufferevent_write(bev1, buffer, sizeof(buffer));
1124 event_dispatch();
1126 bufferevent_free(bev1);
1127 bufferevent_free(bev2);
1129 if (test_ok != 2)
1130 test_ok = 0;
1132 cleanup_test();
1136 * test watermarks and bufferevent
1139 static void
1140 wm_readcb(struct bufferevent *bev, void *arg)
1142 int len = EVBUFFER_LENGTH(bev->input);
1143 static int nread;
1145 assert(len >= 10 && len <= 20);
1147 evbuffer_drain(bev->input, len);
1149 nread += len;
1150 if (nread == 65000) {
1151 bufferevent_disable(bev, EV_READ);
1152 test_ok++;
1156 static void
1157 wm_writecb(struct bufferevent *bev, void *arg)
1159 if (EVBUFFER_LENGTH(bev->output) == 0)
1160 test_ok++;
1163 static void
1164 wm_errorcb(struct bufferevent *bev, short what, void *arg)
1166 test_ok = -2;
1169 static void
1170 test_bufferevent_watermarks(void)
1172 struct bufferevent *bev1, *bev2;
1173 char buffer[65000];
1174 int i;
1176 setup_test("Bufferevent Watermarks: ");
1178 bev1 = bufferevent_new(pair[0], NULL, wm_writecb, wm_errorcb, NULL);
1179 bev2 = bufferevent_new(pair[1], wm_readcb, NULL, wm_errorcb, NULL);
1181 bufferevent_disable(bev1, EV_READ);
1182 bufferevent_enable(bev2, EV_READ);
1184 for (i = 0; i < sizeof(buffer); i++)
1185 buffer[i] = i;
1187 bufferevent_write(bev1, buffer, sizeof(buffer));
1189 /* limit the reading on the receiving bufferevent */
1190 bufferevent_setwatermark(bev2, EV_READ, 10, 20);
1192 event_dispatch();
1194 bufferevent_free(bev1);
1195 bufferevent_free(bev2);
1197 if (test_ok != 2)
1198 test_ok = 0;
1200 cleanup_test();
1203 struct test_pri_event {
1204 struct event ev;
1205 int count;
1208 static void
1209 test_priorities_cb(int fd, short what, void *arg)
1211 struct test_pri_event *pri = arg;
1212 struct timeval tv;
1214 if (pri->count == 3) {
1215 event_loopexit(NULL);
1216 return;
1219 pri->count++;
1221 evutil_timerclear(&tv);
1222 event_add(&pri->ev, &tv);
1225 static void
1226 test_priorities(int npriorities)
1228 char buf[32];
1229 struct test_pri_event one, two;
1230 struct timeval tv;
1232 evutil_snprintf(buf, sizeof(buf), "Testing Priorities %d: ", npriorities);
1233 setup_test(buf);
1235 event_base_priority_init(global_base, npriorities);
1237 memset(&one, 0, sizeof(one));
1238 memset(&two, 0, sizeof(two));
1240 timeout_set(&one.ev, test_priorities_cb, &one);
1241 if (event_priority_set(&one.ev, 0) == -1) {
1242 fprintf(stderr, "%s: failed to set priority", __func__);
1243 exit(1);
1246 timeout_set(&two.ev, test_priorities_cb, &two);
1247 if (event_priority_set(&two.ev, npriorities - 1) == -1) {
1248 fprintf(stderr, "%s: failed to set priority", __func__);
1249 exit(1);
1252 evutil_timerclear(&tv);
1254 if (event_add(&one.ev, &tv) == -1)
1255 exit(1);
1256 if (event_add(&two.ev, &tv) == -1)
1257 exit(1);
1259 event_dispatch();
1261 event_del(&one.ev);
1262 event_del(&two.ev);
1264 if (npriorities == 1) {
1265 if (one.count == 3 && two.count == 3)
1266 test_ok = 1;
1267 } else if (npriorities == 2) {
1268 /* Two is called once because event_loopexit is priority 1 */
1269 if (one.count == 3 && two.count == 1)
1270 test_ok = 1;
1271 } else {
1272 if (one.count == 3 && two.count == 0)
1273 test_ok = 1;
1276 cleanup_test();
1279 static void
1280 test_multiple_cb(int fd, short event, void *arg)
1282 if (event & EV_READ)
1283 test_ok |= 1;
1284 else if (event & EV_WRITE)
1285 test_ok |= 2;
1288 static void
1289 test_multiple_events_for_same_fd(void)
1291 struct event e1, e2;
1293 setup_test("Multiple events for same fd: ");
1295 event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
1296 event_add(&e1, NULL);
1297 event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
1298 event_add(&e2, NULL);
1299 event_loop(EVLOOP_ONCE);
1300 event_del(&e2);
1301 write(pair[1], TEST1, strlen(TEST1)+1);
1302 event_loop(EVLOOP_ONCE);
1303 event_del(&e1);
1305 if (test_ok != 3)
1306 test_ok = 0;
1308 cleanup_test();
1311 int evtag_decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
1312 int evtag_encode_tag(struct evbuffer *evbuf, uint32_t number);
1313 int evtag_decode_tag(uint32_t *pnumber, struct evbuffer *evbuf);
1315 static void
1316 read_once_cb(int fd, short event, void *arg)
1318 char buf[256];
1319 int len;
1321 len = read(fd, buf, sizeof(buf));
1323 if (called) {
1324 test_ok = 0;
1325 } else if (len) {
1326 /* Assumes global pair[0] can be used for writing */
1327 write(pair[0], TEST1, strlen(TEST1)+1);
1328 test_ok = 1;
1331 called++;
1334 static void
1335 test_want_only_once(void)
1337 struct event ev;
1338 struct timeval tv;
1340 /* Very simple read test */
1341 setup_test("Want read only once: ");
1343 write(pair[0], TEST1, strlen(TEST1)+1);
1345 /* Setup the loop termination */
1346 evutil_timerclear(&tv);
1347 tv.tv_sec = 1;
1348 event_loopexit(&tv);
1350 event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
1351 if (event_add(&ev, NULL) == -1)
1352 exit(1);
1353 event_dispatch();
1355 cleanup_test();
1358 #define TEST_MAX_INT 6
1360 static void
1361 evtag_int_test(void)
1363 struct evbuffer *tmp = evbuffer_new();
1364 uint32_t integers[TEST_MAX_INT] = {
1365 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1367 uint32_t integer;
1368 int i;
1370 for (i = 0; i < TEST_MAX_INT; i++) {
1371 int oldlen, newlen;
1372 oldlen = EVBUFFER_LENGTH(tmp);
1373 encode_int(tmp, integers[i]);
1374 newlen = EVBUFFER_LENGTH(tmp);
1375 fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
1376 integers[i], newlen - oldlen);
1379 for (i = 0; i < TEST_MAX_INT; i++) {
1380 if (evtag_decode_int(&integer, tmp) == -1) {
1381 fprintf(stderr, "decode %d failed", i);
1382 exit(1);
1384 if (integer != integers[i]) {
1385 fprintf(stderr, "got %x, wanted %x",
1386 integer, integers[i]);
1387 exit(1);
1391 if (EVBUFFER_LENGTH(tmp) != 0) {
1392 fprintf(stderr, "trailing data");
1393 exit(1);
1395 evbuffer_free(tmp);
1397 fprintf(stdout, "\t%s: OK\n", __func__);
1400 static void
1401 evtag_fuzz(void)
1403 u_char buffer[4096];
1404 struct evbuffer *tmp = evbuffer_new();
1405 struct timeval tv;
1406 int i, j;
1408 int not_failed = 0;
1409 for (j = 0; j < 100; j++) {
1410 for (i = 0; i < sizeof(buffer); i++)
1411 buffer[i] = rand();
1412 evbuffer_drain(tmp, -1);
1413 evbuffer_add(tmp, buffer, sizeof(buffer));
1415 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
1416 not_failed++;
1419 /* The majority of decodes should fail */
1420 if (not_failed >= 10) {
1421 fprintf(stderr, "evtag_unmarshal should have failed");
1422 exit(1);
1425 /* Now insert some corruption into the tag length field */
1426 evbuffer_drain(tmp, -1);
1427 evutil_timerclear(&tv);
1428 tv.tv_sec = 1;
1429 evtag_marshal_timeval(tmp, 0, &tv);
1430 evbuffer_add(tmp, buffer, sizeof(buffer));
1432 EVBUFFER_DATA(tmp)[1] = 0xff;
1433 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
1434 fprintf(stderr, "evtag_unmarshal_timeval should have failed");
1435 exit(1);
1438 evbuffer_free(tmp);
1440 fprintf(stdout, "\t%s: OK\n", __func__);
1443 static void
1444 evtag_tag_encoding(void)
1446 struct evbuffer *tmp = evbuffer_new();
1447 uint32_t integers[TEST_MAX_INT] = {
1448 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1450 uint32_t integer;
1451 int i;
1453 for (i = 0; i < TEST_MAX_INT; i++) {
1454 int oldlen, newlen;
1455 oldlen = EVBUFFER_LENGTH(tmp);
1456 evtag_encode_tag(tmp, integers[i]);
1457 newlen = EVBUFFER_LENGTH(tmp);
1458 fprintf(stdout, "\t\tencoded 0x%08x with %d bytes\n",
1459 integers[i], newlen - oldlen);
1462 for (i = 0; i < TEST_MAX_INT; i++) {
1463 if (evtag_decode_tag(&integer, tmp) == -1) {
1464 fprintf(stderr, "decode %d failed", i);
1465 exit(1);
1467 if (integer != integers[i]) {
1468 fprintf(stderr, "got %x, wanted %x",
1469 integer, integers[i]);
1470 exit(1);
1474 if (EVBUFFER_LENGTH(tmp) != 0) {
1475 fprintf(stderr, "trailing data");
1476 exit(1);
1478 evbuffer_free(tmp);
1480 fprintf(stdout, "\t%s: OK\n", __func__);
1483 static void
1484 evtag_test(void)
1486 fprintf(stdout, "Testing Tagging:\n");
1488 evtag_init();
1489 evtag_int_test();
1490 evtag_fuzz();
1492 evtag_tag_encoding();
1494 fprintf(stdout, "OK\n");
1497 #ifndef WIN32
1498 static void
1499 rpc_test(void)
1501 struct msg *msg, *msg2;
1502 struct kill *attack;
1503 struct run *run;
1504 struct evbuffer *tmp = evbuffer_new();
1505 struct timeval tv_start, tv_end;
1506 uint32_t tag;
1507 int i;
1509 fprintf(stdout, "Testing RPC: ");
1511 msg = msg_new();
1512 EVTAG_ASSIGN(msg, from_name, "niels");
1513 EVTAG_ASSIGN(msg, to_name, "phoenix");
1515 if (EVTAG_GET(msg, attack, &attack) == -1) {
1516 fprintf(stderr, "Failed to set kill message.\n");
1517 exit(1);
1520 EVTAG_ASSIGN(attack, weapon, "feather");
1521 EVTAG_ASSIGN(attack, action, "tickle");
1523 evutil_gettimeofday(&tv_start, NULL);
1524 for (i = 0; i < 1000; ++i) {
1525 run = EVTAG_ADD(msg, run);
1526 if (run == NULL) {
1527 fprintf(stderr, "Failed to add run message.\n");
1528 exit(1);
1530 EVTAG_ASSIGN(run, how, "very fast but with some data in it");
1531 EVTAG_ASSIGN(run, fixed_bytes,
1532 (unsigned char*)"012345678901234567890123");
1535 if (msg_complete(msg) == -1) {
1536 fprintf(stderr, "Failed to make complete message.\n");
1537 exit(1);
1540 evtag_marshal_msg(tmp, 0xdeaf, msg);
1542 if (evtag_peek(tmp, &tag) == -1) {
1543 fprintf(stderr, "Failed to peak tag.\n");
1544 exit (1);
1547 if (tag != 0xdeaf) {
1548 fprintf(stderr, "Got incorrect tag: %0x.\n", tag);
1549 exit (1);
1552 msg2 = msg_new();
1553 if (evtag_unmarshal_msg(tmp, 0xdeaf, msg2) == -1) {
1554 fprintf(stderr, "Failed to unmarshal message.\n");
1555 exit(1);
1558 evutil_gettimeofday(&tv_end, NULL);
1559 evutil_timersub(&tv_end, &tv_start, &tv_end);
1560 fprintf(stderr, "(%.1f us/add) ",
1561 (float)tv_end.tv_sec/(float)i * 1000000.0 +
1562 tv_end.tv_usec / (float)i);
1564 if (!EVTAG_HAS(msg2, from_name) ||
1565 !EVTAG_HAS(msg2, to_name) ||
1566 !EVTAG_HAS(msg2, attack)) {
1567 fprintf(stderr, "Missing data structures.\n");
1568 exit(1);
1571 if (EVTAG_LEN(msg2, run) != i) {
1572 fprintf(stderr, "Wrong number of run messages.\n");
1573 exit(1);
1576 msg_free(msg);
1577 msg_free(msg2);
1579 evbuffer_free(tmp);
1581 fprintf(stdout, "OK\n");
1583 #endif
1585 static void
1586 test_evutil_strtoll(void)
1588 const char *s;
1589 char *endptr;
1590 setup_test("evutil_stroll: ");
1591 test_ok = 0;
1593 if (evutil_strtoll("5000000000", NULL, 10) != ((ev_int64_t)5000000)*1000)
1594 goto err;
1595 if (evutil_strtoll("-5000000000", NULL, 10) != ((ev_int64_t)5000000)*-1000)
1596 goto err;
1597 s = " 99999stuff";
1598 if (evutil_strtoll(s, &endptr, 10) != (ev_int64_t)99999)
1599 goto err;
1600 if (endptr != s+6)
1601 goto err;
1602 if (evutil_strtoll("foo", NULL, 10) != 0)
1603 goto err;
1605 test_ok = 1;
1606 err:
1607 cleanup_test();
1612 main (int argc, char **argv)
1614 #ifdef WIN32
1615 WORD wVersionRequested;
1616 WSADATA wsaData;
1617 int err;
1619 wVersionRequested = MAKEWORD( 2, 2 );
1621 err = WSAStartup( wVersionRequested, &wsaData );
1622 #endif
1624 #ifndef WIN32
1625 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
1626 return (1);
1627 #endif
1628 setvbuf(stdout, NULL, _IONBF, 0);
1630 /* Initalize the event library */
1631 global_base = event_init();
1633 test_registerfds();
1635 test_evutil_strtoll();
1637 /* use the global event base and need to be called first */
1638 test_priorities(1);
1639 test_priorities(2);
1640 test_priorities(3);
1642 test_evbuffer();
1643 test_evbuffer_find();
1645 test_bufferevent();
1646 test_bufferevent_watermarks();
1648 test_free_active_base();
1650 test_event_base_new();
1652 http_suite();
1654 #ifndef WIN32
1655 rpc_suite();
1656 #endif
1658 dns_suite();
1660 #ifndef WIN32
1661 test_fork();
1662 #endif
1664 test_simpleread();
1666 test_simplewrite();
1668 test_multiple();
1670 test_persistent();
1672 test_combined();
1674 test_simpletimeout();
1675 #ifndef WIN32
1676 test_simplesignal();
1677 test_multiplesignal();
1678 test_immediatesignal();
1679 #endif
1680 test_loopexit();
1681 test_loopbreak();
1683 test_loopexit_multiple();
1685 test_multiple_events_for_same_fd();
1687 test_want_only_once();
1689 evtag_test();
1691 #ifndef WIN32
1692 rpc_test();
1694 test_signal_dealloc();
1695 test_signal_pipeloss();
1696 test_signal_switchbase();
1697 test_signal_restore();
1698 test_signal_assert();
1699 test_signal_while_processing();
1700 #endif
1702 return (0);