1 /* $NetBSD: regress.c,v 1.8 2015/01/29 07:26:02 spz Exp $ */
3 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
34 #include "event2/event-config.h"
35 #include <sys/cdefs.h>
36 __RCSID("$NetBSD: regress.c,v 1.8 2015/01/29 07:26:02 spz Exp $");
38 #include <sys/types.h>
40 #ifdef _EVENT_HAVE_SYS_TIME_H
43 #include <sys/queue.h>
45 #include <sys/socket.h>
60 #include "event2/event.h"
61 #include "event2/event_struct.h"
62 #include "event2/event_compat.h"
63 #include "event2/tag.h"
64 #include "event2/buffer.h"
65 #include "event2/buffer_compat.h"
66 #include "event2/util.h"
67 #include "event-internal.h"
68 #include "evthread-internal.h"
69 #include "util-internal.h"
70 #include "log-internal.h"
75 #include "regress.gen.h"
78 evutil_socket_t pair
[2];
81 struct event_base
*global_base
;
83 static char wbuf
[4096];
84 static char rbuf
[4096];
87 static int usepersist
;
88 static struct timeval tset
;
89 static struct timeval tcalled
;
92 #define TEST1 "this is a test"
100 #define write(fd,buf,len) send((fd),(buf),(int)(len),0)
101 #define read(fd,buf,len) recv((fd),(buf),(int)(len),0)
106 struct event_base
*eb
;
108 unsigned int callcount
;
112 simple_read_cb(evutil_socket_t fd
, short event
, void *arg
)
117 len
= read(fd
, buf
, sizeof(buf
));
121 if (event_add(arg
, NULL
) == -1)
124 } else if (called
== 1)
131 basic_read_cb(evutil_socket_t fd
, short event
, void *data
)
135 struct basic_cb_args
*arg
= data
;
137 len
= read(fd
, buf
, sizeof(buf
));
140 tt_fail_perror("read (callback)");
142 switch (arg
->callcount
++) {
143 case 0: /* first call: expect to read data; cycle */
147 tt_fail_msg("EOF before data read");
150 case 1: /* second call: expect EOF; stop */
152 tt_fail_msg("not all data read on first cycle");
155 default: /* third call: should not happen */
156 tt_fail_msg("too many cycles");
161 event_base_loopexit(arg
->eb
, NULL
);
165 dummy_read_cb(evutil_socket_t fd
, short event
, void *arg
)
170 simple_write_cb(evutil_socket_t fd
, short event
, void *arg
)
174 len
= write(fd
, TEST1
, strlen(TEST1
) + 1);
182 multiple_write_cb(evutil_socket_t fd
, short event
, void *arg
)
184 struct event
*ev
= arg
;
188 if (woff
+ len
>= (int)sizeof(wbuf
))
189 len
= sizeof(wbuf
) - woff
;
191 len
= write(fd
, wbuf
+ woff
, len
);
193 fprintf(stderr
, "%s: write\n", __func__
);
201 if (woff
>= (int)sizeof(wbuf
)) {
202 shutdown(fd
, SHUT_WR
);
209 if (event_add(ev
, NULL
) == -1)
215 multiple_read_cb(evutil_socket_t fd
, short event
, void *arg
)
217 struct event
*ev
= arg
;
220 len
= read(fd
, rbuf
+ roff
, sizeof(rbuf
) - roff
);
222 fprintf(stderr
, "%s: read\n", __func__
);
231 if (event_add(ev
, NULL
) == -1)
237 timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
242 evutil_gettimeofday(&tcalled
, NULL
);
243 if (evutil_timercmp(&tcalled
, &tset
, >))
244 evutil_timersub(&tcalled
, &tset
, &tv
);
246 evutil_timersub(&tset
, &tcalled
, &tv
);
248 diff
= tv
.tv_sec
*1000 + tv
.tv_usec
/1000 - SECONDS
* 1000;
262 combined_read_cb(evutil_socket_t fd
, short event
, void *arg
)
264 struct both
*both
= arg
;
268 len
= read(fd
, buf
, sizeof(buf
));
270 fprintf(stderr
, "%s: read\n", __func__
);
275 if (event_add(&both
->ev
, NULL
) == -1)
280 combined_write_cb(evutil_socket_t fd
, short event
, void *arg
)
282 struct both
*both
= arg
;
287 if (len
> both
->nread
)
290 memset(buf
, 'q', len
);
292 len
= write(fd
, buf
, len
);
294 fprintf(stderr
, "%s: write\n", __func__
);
296 shutdown(fd
, SHUT_WR
);
301 if (event_add(&both
->ev
, NULL
) == -1)
305 /* These macros used to replicate the work of the legacy test wrapper code */
306 #define setup_test(x) do { \
307 if (!in_legacy_test_wrapper) { \
308 TT_FAIL(("Legacy test %s not wrapped properly", x)); \
311 } while (/*CONSTCOND*/0)
312 #define cleanup_test() setup_test("cleanup")
315 test_simpleread(void)
319 /* Very simple read test */
320 setup_test("Simple read: ");
322 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
323 tt_fail_perror("write");
326 shutdown(pair
[0], SHUT_WR
);
328 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
329 if (event_add(&ev
, NULL
) == -1)
337 test_simplewrite(void)
341 /* Very simple write test */
342 setup_test("Simple write: ");
344 event_set(&ev
, pair
[0], EV_WRITE
, simple_write_cb
, &ev
);
345 if (event_add(&ev
, NULL
) == -1)
353 simpleread_multiple_cb(evutil_socket_t fd
, short event
, void *arg
)
360 test_simpleread_multiple(void)
362 struct event one
, two
;
364 /* Very simple read test */
365 setup_test("Simple read to multiple evens: ");
367 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
368 tt_fail_perror("write");
371 shutdown(pair
[0], SHUT_WR
);
373 event_set(&one
, pair
[1], EV_READ
, simpleread_multiple_cb
, NULL
);
374 if (event_add(&one
, NULL
) == -1)
376 event_set(&two
, pair
[1], EV_READ
, simpleread_multiple_cb
, NULL
);
377 if (event_add(&two
, NULL
) == -1)
384 static int have_closed
= 0;
385 static int premature_event
= 0;
387 simpleclose_close_fd_cb(evutil_socket_t s
, short what
, void *ptr
)
389 evutil_socket_t
**fds
= ptr
;
390 TT_BLATHER(("Closing"));
391 evutil_closesocket(*fds
[0]);
392 evutil_closesocket(*fds
[1]);
399 record_event_cb(evutil_socket_t s
, short what
, void *ptr
)
405 TT_BLATHER(("Recorded %d on socket %d", (int)what
, (int)s
));
409 test_simpleclose(void *ptr
)
411 /* Test that a close of FD is detected as a read and as a write. */
412 struct event_base
*base
= event_base_new();
413 evutil_socket_t pair1
[2]={-1,-1}, pair2
[2] = {-1, -1};
414 evutil_socket_t
*to_close
[2];
415 struct event
*rev
=NULL
, *wev
=NULL
, *closeev
=NULL
;
417 short got_read_on_close
= 0, got_write_on_close
= 0;
419 memset(buf
, 99, sizeof(buf
));
421 #define LOCAL_SOCKETPAIR_AF AF_INET
423 #define LOCAL_SOCKETPAIR_AF AF_UNIX
425 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF
, SOCK_STREAM
, 0, pair1
)<0)
426 TT_DIE(("socketpair: %s", strerror(errno
)));
427 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF
, SOCK_STREAM
, 0, pair2
)<0)
428 TT_DIE(("socketpair: %s", strerror(errno
)));
429 if (evutil_make_socket_nonblocking(pair1
[1]) < 0)
430 TT_DIE(("make_socket_nonblocking"));
431 if (evutil_make_socket_nonblocking(pair2
[1]) < 0)
432 TT_DIE(("make_socket_nonblocking"));
434 /** Stuff pair2[1] full of data, until write fails */
436 int r
= write(pair2
[1], buf
, sizeof(buf
));
438 int err
= evutil_socket_geterror(pair2
[1]);
439 if (! EVUTIL_ERR_RW_RETRIABLE(err
))
440 TT_DIE(("write failed strangely: %s",
441 evutil_socket_error_to_string(err
)));
445 to_close
[0] = &pair1
[0];
446 to_close
[1] = &pair2
[0];
448 closeev
= event_new(base
, -1, EV_TIMEOUT
, simpleclose_close_fd_cb
,
450 rev
= event_new(base
, pair1
[1], EV_READ
, record_event_cb
,
452 TT_BLATHER(("Waiting for read on %d", (int)pair1
[1]));
453 wev
= event_new(base
, pair2
[1], EV_WRITE
, record_event_cb
,
454 &got_write_on_close
);
455 TT_BLATHER(("Waiting for write on %d", (int)pair2
[1]));
457 tv
.tv_usec
= 100*1000; /* Close pair1[0] after a little while, and make
458 * sure we get a read event. */
459 event_add(closeev
, &tv
);
460 event_add(rev
, NULL
);
461 event_add(wev
, NULL
);
462 /* Don't let the test go on too long. */
464 tv
.tv_usec
= 200*1000;
465 event_base_loopexit(base
, &tv
);
466 event_base_loop(base
, 0);
468 tt_int_op(got_read_on_close
, ==, EV_READ
);
469 tt_int_op(got_write_on_close
, ==, EV_WRITE
);
470 tt_int_op(premature_event
, ==, 0);
474 evutil_closesocket(pair1
[0]);
476 evutil_closesocket(pair1
[1]);
478 evutil_closesocket(pair2
[0]);
480 evutil_closesocket(pair2
[1]);
488 event_base_free(base
);
495 struct event ev
, ev2
;
498 /* Multiple read and write test */
499 setup_test("Multiple read/write: ");
500 memset(rbuf
, 0, sizeof(rbuf
));
501 for (i
= 0; i
< (int)sizeof(wbuf
); i
++)
507 event_set(&ev
, pair
[0], EV_WRITE
, multiple_write_cb
, &ev
);
508 if (event_add(&ev
, NULL
) == -1)
510 event_set(&ev2
, pair
[1], EV_READ
, multiple_read_cb
, &ev2
);
511 if (event_add(&ev2
, NULL
) == -1)
516 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
522 test_persistent(void)
524 struct event ev
, ev2
;
527 /* Multiple read and write test with persist */
528 setup_test("Persist read/write: ");
529 memset(rbuf
, 0, sizeof(rbuf
));
530 for (i
= 0; i
< (int)sizeof(wbuf
); i
++)
536 event_set(&ev
, pair
[0], EV_WRITE
|EV_PERSIST
, multiple_write_cb
, &ev
);
537 if (event_add(&ev
, NULL
) == -1)
539 event_set(&ev2
, pair
[1], EV_READ
|EV_PERSIST
, multiple_read_cb
, &ev2
);
540 if (event_add(&ev2
, NULL
) == -1)
545 test_ok
= memcmp(rbuf
, wbuf
, sizeof(wbuf
)) == 0;
553 struct both r1
, r2
, w1
, w2
;
555 setup_test("Combined read/write: ");
556 memset(&r1
, 0, sizeof(r1
));
557 memset(&r2
, 0, sizeof(r2
));
558 memset(&w1
, 0, sizeof(w1
));
559 memset(&w2
, 0, sizeof(w2
));
564 event_set(&r1
.ev
, pair
[0], EV_READ
, combined_read_cb
, &r1
);
565 event_set(&w1
.ev
, pair
[0], EV_WRITE
, combined_write_cb
, &w1
);
566 event_set(&r2
.ev
, pair
[1], EV_READ
, combined_read_cb
, &r2
);
567 event_set(&w2
.ev
, pair
[1], EV_WRITE
, combined_write_cb
, &w2
);
568 tt_assert(event_add(&r1
.ev
, NULL
) != -1);
569 tt_assert(!event_add(&w1
.ev
, NULL
));
570 tt_assert(!event_add(&r2
.ev
, NULL
));
571 tt_assert(!event_add(&w2
.ev
, NULL
));
574 if (r1
.nread
== 8192 && r2
.nread
== 4096)
582 test_simpletimeout(void)
587 setup_test("Simple timeout: ");
591 evtimer_set(&ev
, timeout_cb
, NULL
);
592 evtimer_add(&ev
, &tv
);
594 evutil_gettimeofday(&tset
, NULL
);
601 periodic_timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
607 /* call loopexit only once - on slow machines(?), it is
608 * apparently possible for this to get called twice. */
610 event_base_loopexit(global_base
, NULL
);
615 test_persistent_timeout(void)
621 evutil_timerclear(&tv
);
624 event_assign(&ev
, global_base
, -1, EV_TIMEOUT
|EV_PERSIST
,
625 periodic_timeout_cb
, &count
);
634 test_persistent_timeout_jump(void *ptr
)
636 struct basic_test_data
*data
= ptr
;
639 struct timeval msec100
= { 0, 100 * 1000 };
640 struct timeval msec50
= { 0, 50 * 1000 };
642 event_assign(&ev
, data
->base
, -1, EV_PERSIST
, periodic_timeout_cb
, &count
);
643 event_add(&ev
, &msec100
);
650 event_base_loopexit(data
->base
, &msec50
);
651 event_base_dispatch(data
->base
);
652 tt_int_op(count
, ==, 1);
658 struct persist_active_timeout_called
{
661 struct timeval tvs
[16];
665 activate_cb(evutil_socket_t fd
, short event
, void *arg
)
667 struct event
*ev
= arg
;
668 event_active(ev
, EV_READ
, 1);
672 persist_active_timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
674 struct persist_active_timeout_called
*c
= arg
;
676 c
->events
[c
->n
] = event
;
677 evutil_gettimeofday(&c
->tvs
[c
->n
], NULL
);
683 test_persistent_active_timeout(void *ptr
)
685 struct timeval tv
, tv2
, tv_exit
, start
;
687 struct persist_active_timeout_called res
;
689 struct basic_test_data
*data
= ptr
;
690 struct event_base
*base
= data
->base
;
692 memset(&res
, 0, sizeof(res
));
695 tv
.tv_usec
= 200 * 1000;
696 event_assign(&ev
, base
, -1, EV_TIMEOUT
|EV_PERSIST
,
697 persist_active_timeout_cb
, &res
);
701 tv2
.tv_usec
= 100 * 1000;
702 event_base_once(base
, -1, EV_TIMEOUT
, activate_cb
, &ev
, &tv2
);
705 tv_exit
.tv_usec
= 600 * 1000;
706 event_base_loopexit(base
, &tv_exit
);
708 event_base_assert_ok(base
);
709 evutil_gettimeofday(&start
, NULL
);
711 event_base_dispatch(base
);
712 event_base_assert_ok(base
);
714 tt_int_op(res
.n
, ==, 3);
715 tt_int_op(res
.events
[0], ==, EV_READ
);
716 tt_int_op(res
.events
[1], ==, EV_TIMEOUT
);
717 tt_int_op(res
.events
[2], ==, EV_TIMEOUT
);
718 test_timeval_diff_eq(&start
, &res
.tvs
[0], 100);
719 test_timeval_diff_eq(&start
, &res
.tvs
[1], 300);
720 test_timeval_diff_eq(&start
, &res
.tvs
[2], 500);
725 struct common_timeout_info
{
727 struct timeval called_at
;
733 common_timeout_cb(evutil_socket_t fd
, short event
, void *arg
)
735 struct common_timeout_info
*ti
= arg
;
737 evutil_gettimeofday(&ti
->called_at
, NULL
);
743 test_common_timeout(void *ptr
)
745 struct basic_test_data
*data
= ptr
;
747 struct event_base
*base
= data
->base
;
749 struct common_timeout_info info
[100];
752 struct timeval tmp_100_ms
= { 0, 100*1000 };
753 struct timeval tmp_200_ms
= { 0, 200*1000 };
755 const struct timeval
*ms_100
, *ms_200
;
757 ms_100
= event_base_init_common_timeout(base
, &tmp_100_ms
);
758 ms_200
= event_base_init_common_timeout(base
, &tmp_200_ms
);
761 tt_ptr_op(event_base_init_common_timeout(base
, &tmp_200_ms
),
763 tt_int_op(ms_100
->tv_sec
, ==, 0);
764 tt_int_op(ms_200
->tv_sec
, ==, 0);
765 tt_int_op(ms_100
->tv_usec
, ==, 100000|0x50000000);
766 tt_int_op(ms_200
->tv_usec
, ==, 200000|0x50100000);
768 memset(info
, 0, sizeof(info
));
770 for (i
=0; i
<100; ++i
) {
772 event_assign(&info
[i
].ev
, base
, -1, EV_TIMEOUT
|EV_PERSIST
,
773 common_timeout_cb
, &info
[i
]);
775 event_add(&info
[i
].ev
, ms_100
);
777 event_add(&info
[i
].ev
, ms_200
);
781 event_base_assert_ok(base
);
782 event_base_dispatch(base
);
784 evutil_gettimeofday(&now
, NULL
);
785 event_base_assert_ok(base
);
787 for (i
=0; i
<10; ++i
) {
790 tt_int_op(info
[i
].count
, ==, 6);
791 evutil_timersub(&now
, &info
[i
].called_at
, &tmp
);
792 ms_diff
= tmp
.tv_usec
/1000 + tmp
.tv_sec
*1000;
794 tt_int_op(ms_diff
, >, 500);
795 tt_int_op(ms_diff
, <, 700);
797 tt_int_op(ms_diff
, >, -100);
798 tt_int_op(ms_diff
, <, 100);
802 /* Make sure we can free the base with some events in. */
803 for (i
=0; i
<100; ++i
) {
805 event_add(&info
[i
].ev
, ms_100
);
807 event_add(&info
[i
].ev
, ms_200
);
812 event_base_free(data
->base
); /* need to do this here before info is
818 static void signal_cb(evutil_socket_t fd
, short event
, void *arg
);
820 #define current_base event_global_current_base_
821 extern struct event_base
*current_base
;
824 child_signal_cb(evutil_socket_t fd
, short event
, void *arg
)
839 int status
, got_sigchld
= 0;
840 struct event ev
, sig_ev
;
843 setup_test("After fork: ");
845 tt_assert(current_base
);
846 evthread_make_base_notifiable(current_base
);
848 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
849 tt_fail_perror("write");
852 event_set(&ev
, pair
[1], EV_READ
, simple_read_cb
, &ev
);
853 if (event_add(&ev
, NULL
) == -1)
856 evsignal_set(&sig_ev
, SIGCHLD
, child_signal_cb
, &got_sigchld
);
857 evsignal_add(&sig_ev
, NULL
);
859 event_base_assert_ok(current_base
);
860 TT_BLATHER(("Before fork"));
861 if ((pid
= regress_fork()) == 0) {
863 TT_BLATHER(("In child, before reinit"));
864 event_base_assert_ok(current_base
);
865 if (event_reinit(current_base
) == -1) {
866 fprintf(stdout
, "FAILED (reinit)\n");
869 TT_BLATHER(("After reinit"));
870 event_base_assert_ok(current_base
);
871 TT_BLATHER(("After assert-ok"));
873 evsignal_del(&sig_ev
);
879 event_base_free(current_base
);
881 /* we do not send an EOF; simple_read_cb requires an EOF
882 * to set test_ok. we just verify that the callback was
884 exit(test_ok
!= 0 || called
!= 2 ? -2 : 76);
887 /* wait for the child to read the data */
890 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
891 tt_fail_perror("write");
894 TT_BLATHER(("Before waitpid"));
895 if (waitpid(pid
, &status
, 0) == -1) {
896 fprintf(stdout
, "FAILED (fork)\n");
899 TT_BLATHER(("After waitpid"));
901 if (WEXITSTATUS(status
) != 76) {
902 fprintf(stdout
, "FAILED (exit): %d\n", WEXITSTATUS(status
));
906 /* test that the current event loop still works */
907 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
908 fprintf(stderr
, "%s: write\n", __func__
);
911 shutdown(pair
[0], SHUT_WR
);
916 fprintf(stdout
, "FAILED (sigchld)\n");
920 evsignal_del(&sig_ev
);
927 signal_cb_sa(int sig
)
933 signal_cb(evutil_socket_t fd
, short event
, void *arg
)
935 struct event
*ev
= arg
;
942 test_simplesignal(void)
945 struct itimerval itv
;
947 setup_test("Simple signal: ");
948 evsignal_set(&ev
, SIGALRM
, signal_cb
, &ev
);
949 evsignal_add(&ev
, NULL
);
950 /* find bugs in which operations are re-ordered */
952 evsignal_add(&ev
, NULL
);
954 memset(&itv
, 0, sizeof(itv
));
955 itv
.it_value
.tv_sec
= 1;
956 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
957 goto skip_simplesignal
;
961 if (evsignal_del(&ev
) == -1)
968 test_multiplesignal(void)
970 struct event ev_one
, ev_two
;
971 struct itimerval itv
;
973 setup_test("Multiple signal: ");
975 evsignal_set(&ev_one
, SIGALRM
, signal_cb
, &ev_one
);
976 evsignal_add(&ev_one
, NULL
);
978 evsignal_set(&ev_two
, SIGALRM
, signal_cb
, &ev_two
);
979 evsignal_add(&ev_two
, NULL
);
981 memset(&itv
, 0, sizeof(itv
));
982 itv
.it_value
.tv_sec
= 1;
983 if (setitimer(ITIMER_REAL
, &itv
, NULL
) == -1)
984 goto skip_simplesignal
;
989 if (evsignal_del(&ev_one
) == -1)
991 if (evsignal_del(&ev_two
) == -1)
998 test_immediatesignal(void)
1003 evsignal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
1004 evsignal_add(&ev
, NULL
);
1006 event_loop(EVLOOP_NONBLOCK
);
1012 test_signal_dealloc(void)
1014 /* make sure that evsignal_event is event_del'ed and pipe closed */
1016 struct event_base
*base
= event_init();
1017 evsignal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
1018 evsignal_add(&ev
, NULL
);
1020 event_base_free(base
);
1021 /* If we got here without asserting, we're fine. */
1027 test_signal_pipeloss(void)
1029 /* make sure that the base1 pipe is closed correctly. */
1030 struct event_base
*base1
, *base2
;
1033 base1
= event_init();
1034 pipe1
= base1
->sig
.ev_signal_pair
[0];
1035 base2
= event_init();
1036 event_base_free(base2
);
1037 event_base_free(base1
);
1038 if (close(pipe1
) != -1 || errno
!=EBADF
) {
1039 /* fd must be closed, so second close gives -1, EBADF */
1040 printf("signal pipe not closed. ");
1049 * make two bases to catch signals, use both of them. this only works
1050 * for event mechanisms that use our signal pipe trick. kqueue handles
1051 * signals internally, and all interested kqueues get all the signals.
1054 test_signal_switchbase(void)
1056 struct event ev1
, ev2
;
1057 struct event_base
*base1
, *base2
;
1060 base1
= event_init();
1061 base2
= event_init();
1062 is_kqueue
= !strcmp(event_get_method(),"kqueue");
1063 evsignal_set(&ev1
, SIGUSR1
, signal_cb
, &ev1
);
1064 evsignal_set(&ev2
, SIGUSR1
, signal_cb
, &ev2
);
1065 if (event_base_set(base1
, &ev1
) ||
1066 event_base_set(base2
, &ev2
) ||
1067 event_add(&ev1
, NULL
) ||
1068 event_add(&ev2
, NULL
)) {
1069 fprintf(stderr
, "%s: cannot set base, add\n", __func__
);
1073 tt_ptr_op(event_get_base(&ev1
), ==, base1
);
1074 tt_ptr_op(event_get_base(&ev2
), ==, base2
);
1077 /* can handle signal before loop is called */
1079 event_base_loop(base2
, EVLOOP_NONBLOCK
);
1085 event_base_loop(base1
, EVLOOP_NONBLOCK
);
1086 if (test_ok
&& !is_kqueue
) {
1089 /* set base1 to handle signals */
1090 event_base_loop(base1
, EVLOOP_NONBLOCK
);
1092 event_base_loop(base1
, EVLOOP_NONBLOCK
);
1093 event_base_loop(base2
, EVLOOP_NONBLOCK
);
1096 event_base_free(base1
);
1097 event_base_free(base2
);
1102 * assert that a signal event removed from the event queue really is
1103 * removed - with no possibility of it's parent handler being fired.
1106 test_signal_assert(void)
1109 struct event_base
*base
= event_init();
1111 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
1112 evsignal_set(&ev
, SIGCONT
, signal_cb
, &ev
);
1113 evsignal_add(&ev
, NULL
);
1115 * if evsignal_del() fails to reset the handler, it's current handler
1116 * will still point to evsig_handler().
1122 /* only way to verify we were in evsig_handler() */
1123 /* XXXX Now there's no longer a good way. */
1124 if (base
->sig
.evsig_caught
)
1132 event_base_free(base
);
1138 * assert that we restore our previous signal handler properly.
1141 test_signal_restore(void)
1144 struct event_base
*base
= event_init();
1145 #ifdef _EVENT_HAVE_SIGACTION
1146 struct sigaction sa
;
1150 #ifdef _EVENT_HAVE_SIGACTION
1151 sa
.sa_handler
= signal_cb_sa
;
1153 sigemptyset(&sa
.sa_mask
);
1154 if (sigaction(SIGUSR1
, &sa
, NULL
) == -1)
1157 if (signal(SIGUSR1
, signal_cb_sa
) == SIG_ERR
)
1160 evsignal_set(&ev
, SIGUSR1
, signal_cb
, &ev
);
1161 evsignal_add(&ev
, NULL
);
1165 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
1169 event_base_free(base
);
1175 signal_cb_swp(int sig
, short event
, void *arg
)
1181 event_loopexit(NULL
);
1184 timeout_cb_swp(evutil_socket_t fd
, short event
, void *arg
)
1187 struct timeval tv
= {5, 0};
1190 evtimer_add((struct event
*)arg
, &tv
);
1195 event_loopexit(NULL
);
1199 test_signal_while_processing(void)
1201 struct event_base
*base
= event_init();
1202 struct event ev
, ev_timer
;
1203 struct timeval tv
= {0, 0};
1205 setup_test("Receiving a signal while processing other signal: ");
1209 signal_set(&ev
, SIGUSR1
, signal_cb_swp
, NULL
);
1210 signal_add(&ev
, NULL
);
1211 evtimer_set(&ev_timer
, timeout_cb_swp
, &ev_timer
);
1212 evtimer_add(&ev_timer
, &tv
);
1215 event_base_free(base
);
1222 test_free_active_base(void *ptr
)
1224 struct basic_test_data
*data
= ptr
;
1225 struct event_base
*base1
;
1228 base1
= event_init();
1230 event_assign(&ev1
, base1
, data
->pair
[1], EV_READ
,
1231 dummy_read_cb
, NULL
);
1232 event_add(&ev1
, NULL
);
1233 event_base_free(base1
); /* should not crash */
1235 tt_fail_msg("failed to create event_base for test");
1238 base1
= event_init();
1240 event_assign(&ev1
, base1
, 0, 0, dummy_read_cb
, NULL
);
1241 event_active(&ev1
, EV_READ
, 1);
1242 event_base_free(base1
);
1248 test_manipulate_active_events(void *ptr
)
1250 struct basic_test_data
*data
= ptr
;
1251 struct event_base
*base
= data
->base
;
1254 event_assign(&ev1
, base
, -1, EV_TIMEOUT
, dummy_read_cb
, NULL
);
1256 /* Make sure an active event is pending. */
1257 event_active(&ev1
, EV_READ
, 1);
1258 tt_int_op(event_pending(&ev1
, EV_READ
|EV_TIMEOUT
|EV_WRITE
, NULL
),
1261 /* Make sure that activating an event twice works. */
1262 event_active(&ev1
, EV_WRITE
, 1);
1263 tt_int_op(event_pending(&ev1
, EV_READ
|EV_TIMEOUT
|EV_WRITE
, NULL
),
1264 ==, EV_READ
|EV_WRITE
);
1271 test_bad_assign(void *ptr
)
1275 /* READ|SIGNAL is not allowed */
1276 r
= event_assign(&ev
, NULL
, -1, EV_SIGNAL
|EV_READ
, dummy_read_cb
, NULL
);
1283 static int reentrant_cb_run
= 0;
1286 bad_reentrant_run_loop_cb(evutil_socket_t fd
, short what
, void *ptr
)
1288 struct event_base
*base
= ptr
;
1290 reentrant_cb_run
= 1;
1291 /* This reentrant call to event_base_loop should be detected and
1293 r
= event_base_loop(base
, 0);
1294 tt_int_op(r
, ==, -1);
1300 test_bad_reentrant(void *ptr
)
1302 struct basic_test_data
*data
= ptr
;
1303 struct event_base
*base
= data
->base
;
1306 event_assign(&ev
, base
, -1,
1307 0, bad_reentrant_run_loop_cb
, base
);
1309 event_active(&ev
, EV_WRITE
, 1);
1310 r
= event_base_loop(base
, 0);
1311 tt_int_op(r
, ==, 1);
1312 tt_int_op(reentrant_cb_run
, ==, 1);
1318 test_event_base_new(void *ptr
)
1320 struct basic_test_data
*data
= ptr
;
1321 struct event_base
*base
= 0;
1323 struct basic_cb_args args
;
1325 int towrite
= (int)strlen(TEST1
)+1;
1326 int len
= write(data
->pair
[0], TEST1
, towrite
);
1329 tt_abort_perror("initial write");
1330 else if (len
!= towrite
)
1331 tt_abort_printf(("initial write fell short (%d of %d bytes)",
1334 if (shutdown(data
->pair
[0], SHUT_WR
))
1335 tt_abort_perror("initial write shutdown");
1337 base
= event_base_new();
1339 tt_abort_msg("failed to create event base");
1344 event_assign(&ev1
, base
, data
->pair
[1],
1345 EV_READ
|EV_PERSIST
, basic_read_cb
, &args
);
1347 if (event_add(&ev1
, NULL
))
1348 tt_abort_perror("initial event_add");
1350 if (event_base_loop(base
, 0))
1351 tt_abort_msg("unsuccessful exit from event loop");
1355 event_base_free(base
);
1361 struct timeval tv
, tv_start
, tv_end
;
1364 setup_test("Loop exit: ");
1367 tv
.tv_sec
= 60*60*24;
1368 evtimer_set(&ev
, timeout_cb
, NULL
);
1369 evtimer_add(&ev
, &tv
);
1373 event_loopexit(&tv
);
1375 evutil_gettimeofday(&tv_start
, NULL
);
1377 evutil_gettimeofday(&tv_end
, NULL
);
1378 evutil_timersub(&tv_end
, &tv_start
, &tv_end
);
1382 tt_assert(event_base_got_exit(global_base
));
1383 tt_assert(!event_base_got_break(global_base
));
1393 test_loopexit_multiple(void)
1396 struct event_base
*base
;
1398 setup_test("Loop Multiple exit: ");
1400 base
= event_base_new();
1404 event_base_loopexit(base
, &tv
);
1408 event_base_loopexit(base
, &tv
);
1410 event_base_dispatch(base
);
1412 tt_assert(event_base_got_exit(base
));
1413 tt_assert(!event_base_got_break(base
));
1415 event_base_free(base
);
1424 break_cb(evutil_socket_t fd
, short events
, void *arg
)
1431 fail_cb(evutil_socket_t fd
, short events
, void *arg
)
1437 test_loopbreak(void)
1439 struct event ev1
, ev2
;
1442 setup_test("Loop break: ");
1446 evtimer_set(&ev1
, break_cb
, NULL
);
1447 evtimer_add(&ev1
, &tv
);
1448 evtimer_set(&ev2
, fail_cb
, NULL
);
1449 evtimer_add(&ev2
, &tv
);
1453 tt_assert(!event_base_got_exit(global_base
));
1454 tt_assert(event_base_got_break(global_base
));
1463 static struct event
*readd_test_event_last_added
= NULL
;
1465 re_add_read_cb(evutil_socket_t fd
, short event
, void *arg
)
1468 struct event
*ev_other
= arg
;
1469 readd_test_event_last_added
= ev_other
;
1471 if (read(fd
, buf
, sizeof(buf
)) < 0) {
1472 tt_fail_perror("read");
1475 event_add(ev_other
, NULL
);
1480 test_nonpersist_readd(void)
1482 struct event ev1
, ev2
;
1484 setup_test("Re-add nonpersistent events: ");
1485 event_set(&ev1
, pair
[0], EV_READ
, re_add_read_cb
, &ev2
);
1486 event_set(&ev2
, pair
[1], EV_READ
, re_add_read_cb
, &ev1
);
1488 if (write(pair
[0], "Hello", 5) < 0) {
1489 tt_fail_perror("write(pair[0])");
1492 if (write(pair
[1], "Hello", 5) < 0) {
1493 tt_fail_perror("write(pair[1])\n");
1496 if (event_add(&ev1
, NULL
) == -1 ||
1497 event_add(&ev2
, NULL
) == -1) {
1502 event_loop(EVLOOP_ONCE
);
1505 /* At this point, we executed both callbacks. Whichever one got
1506 * called first added the second, but the second then immediately got
1507 * deleted before its callback was called. At this point, though, it
1508 * re-added the first.
1510 if (!readd_test_event_last_added
) {
1512 } else if (readd_test_event_last_added
== &ev1
) {
1513 if (!event_pending(&ev1
, EV_READ
, NULL
) ||
1514 event_pending(&ev2
, EV_READ
, NULL
))
1517 if (event_pending(&ev1
, EV_READ
, NULL
) ||
1518 !event_pending(&ev2
, EV_READ
, NULL
))
1528 struct test_pri_event
{
1534 test_priorities_cb(evutil_socket_t fd
, short what
, void *arg
)
1536 struct test_pri_event
*pri
= arg
;
1539 if (pri
->count
== 3) {
1540 event_loopexit(NULL
);
1546 evutil_timerclear(&tv
);
1547 event_add(&pri
->ev
, &tv
);
1551 test_priorities_impl(int npriorities
)
1553 struct test_pri_event one
, two
;
1556 TT_BLATHER(("Testing Priorities %d: ", npriorities
));
1558 event_base_priority_init(global_base
, npriorities
);
1560 memset(&one
, 0, sizeof(one
));
1561 memset(&two
, 0, sizeof(two
));
1563 timeout_set(&one
.ev
, test_priorities_cb
, &one
);
1564 if (event_priority_set(&one
.ev
, 0) == -1) {
1565 fprintf(stderr
, "%s: failed to set priority", __func__
);
1569 timeout_set(&two
.ev
, test_priorities_cb
, &two
);
1570 if (event_priority_set(&two
.ev
, npriorities
- 1) == -1) {
1571 fprintf(stderr
, "%s: failed to set priority", __func__
);
1575 evutil_timerclear(&tv
);
1577 if (event_add(&one
.ev
, &tv
) == -1)
1579 if (event_add(&two
.ev
, &tv
) == -1)
1587 if (npriorities
== 1) {
1588 if (one
.count
== 3 && two
.count
== 3)
1590 } else if (npriorities
== 2) {
1591 /* Two is called once because event_loopexit is priority 1 */
1592 if (one
.count
== 3 && two
.count
== 1)
1595 if (one
.count
== 3 && two
.count
== 0)
1601 test_priorities(void)
1603 test_priorities_impl(1);
1605 test_priorities_impl(2);
1607 test_priorities_impl(3);
1610 /* priority-active-inversion: activate a higher-priority event, and make sure
1611 * it keeps us from running a lower-priority event first. */
1612 static int n_pai_calls
= 0;
1613 static struct event pai_events
[3];
1616 prio_active_inversion_cb(evutil_socket_t fd
, short what
, void *arg
)
1618 int *call_order
= arg
;
1619 *call_order
= n_pai_calls
++;
1620 if (n_pai_calls
== 1) {
1621 /* This should activate later, even though it shares a
1622 priority with us. */
1623 event_active(&pai_events
[1], EV_READ
, 1);
1624 /* This should activate next, since its priority is higher,
1625 even though we activated it second. */
1626 event_active(&pai_events
[2], EV_TIMEOUT
, 1);
1631 test_priority_active_inversion(void *data_
)
1633 struct basic_test_data
*data
= data_
;
1634 struct event_base
*base
= data
->base
;
1637 tt_int_op(event_base_priority_init(base
, 8), ==, 0);
1640 memset(call_order
, 0, sizeof(call_order
));
1643 event_assign(&pai_events
[i
], data
->base
, -1, 0,
1644 prio_active_inversion_cb
, &call_order
[i
]);
1647 event_priority_set(&pai_events
[0], 4);
1648 event_priority_set(&pai_events
[1], 4);
1649 event_priority_set(&pai_events
[2], 0);
1651 event_active(&pai_events
[0], EV_WRITE
, 1);
1653 event_base_dispatch(base
);
1654 tt_int_op(n_pai_calls
, ==, 3);
1655 tt_int_op(call_order
[0], ==, 0);
1656 tt_int_op(call_order
[1], ==, 2);
1657 tt_int_op(call_order
[2], ==, 1);
1664 test_multiple_cb(evutil_socket_t fd
, short event
, void *arg
)
1666 if (event
& EV_READ
)
1668 else if (event
& EV_WRITE
)
1673 test_multiple_events_for_same_fd(void)
1675 struct event e1
, e2
;
1677 setup_test("Multiple events for same fd: ");
1679 event_set(&e1
, pair
[0], EV_READ
, test_multiple_cb
, NULL
);
1680 event_add(&e1
, NULL
);
1681 event_set(&e2
, pair
[0], EV_WRITE
, test_multiple_cb
, NULL
);
1682 event_add(&e2
, NULL
);
1683 event_loop(EVLOOP_ONCE
);
1686 if (write(pair
[1], TEST1
, strlen(TEST1
)+1) < 0) {
1687 tt_fail_perror("write");
1690 event_loop(EVLOOP_ONCE
);
1699 int evtag_decode_int(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
);
1700 int evtag_decode_int64(ev_uint64_t
*pnumber
, struct evbuffer
*evbuf
);
1701 int evtag_encode_tag(struct evbuffer
*evbuf
, ev_uint32_t number
);
1702 int evtag_decode_tag(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
);
1705 read_once_cb(evutil_socket_t fd
, short event
, void *arg
)
1710 len
= read(fd
, buf
, sizeof(buf
));
1715 /* Assumes global pair[0] can be used for writing */
1716 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
1717 tt_fail_perror("write");
1728 test_want_only_once(void)
1733 /* Very simple read test */
1734 setup_test("Want read only once: ");
1736 if (write(pair
[0], TEST1
, strlen(TEST1
)+1) < 0) {
1737 tt_fail_perror("write");
1740 /* Setup the loop termination */
1741 evutil_timerclear(&tv
);
1743 event_loopexit(&tv
);
1745 event_set(&ev
, pair
[1], EV_READ
, read_once_cb
, &ev
);
1746 if (event_add(&ev
, NULL
) == -1)
1753 #define TEST_MAX_INT 6
1756 evtag_int_test(void *ptr
)
1758 struct evbuffer
*tmp
= evbuffer_new();
1759 ev_uint32_t integers
[TEST_MAX_INT
] = {
1760 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1762 ev_uint32_t integer
;
1763 ev_uint64_t big_int
;
1768 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1770 oldlen
= (int)EVBUFFER_LENGTH(tmp
);
1771 evtag_encode_int(tmp
, integers
[i
]);
1772 newlen
= (int)EVBUFFER_LENGTH(tmp
);
1773 TT_BLATHER(("encoded 0x%08x with %d bytes",
1774 (unsigned)integers
[i
], newlen
- oldlen
));
1775 big_int
= integers
[i
];
1776 big_int
*= 1000000000; /* 1 billion */
1777 evtag_encode_int64(tmp
, big_int
);
1780 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1781 tt_int_op(evtag_decode_int(&integer
, tmp
), !=, -1);
1782 tt_uint_op(integer
, ==, integers
[i
]);
1783 tt_int_op(evtag_decode_int64(&big_int
, tmp
), !=, -1);
1784 tt_assert((big_int
/ 1000000000) == integers
[i
]);
1787 tt_uint_op(EVBUFFER_LENGTH(tmp
), ==, 0);
1793 evtag_fuzz(void *ptr
)
1795 u_char buffer
[4096];
1796 struct evbuffer
*tmp
= evbuffer_new();
1804 for (j
= 0; j
< 100; j
++) {
1805 for (i
= 0; i
< (int)sizeof(buffer
); i
++)
1807 evbuffer_drain(tmp
, -1);
1808 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1810 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1)
1814 /* The majority of decodes should fail */
1815 tt_int_op(not_failed
, <, 10);
1817 /* Now insert some corruption into the tag length field */
1818 evbuffer_drain(tmp
, -1);
1819 evutil_timerclear(&tv
);
1821 evtag_marshal_timeval(tmp
, 0, &tv
);
1822 evbuffer_add(tmp
, buffer
, sizeof(buffer
));
1824 ((char *)EVBUFFER_DATA(tmp
))[1] = '\xff';
1825 if (evtag_unmarshal_timeval(tmp
, 0, &tv
) != -1) {
1826 tt_abort_msg("evtag_unmarshal_timeval should have failed");
1834 evtag_tag_encoding(void *ptr
)
1836 struct evbuffer
*tmp
= evbuffer_new();
1837 ev_uint32_t integers
[TEST_MAX_INT
] = {
1838 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
1840 ev_uint32_t integer
;
1845 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1847 oldlen
= (int)EVBUFFER_LENGTH(tmp
);
1848 evtag_encode_tag(tmp
, integers
[i
]);
1849 newlen
= (int)EVBUFFER_LENGTH(tmp
);
1850 TT_BLATHER(("encoded 0x%08x with %d bytes",
1851 (unsigned)integers
[i
], newlen
- oldlen
));
1854 for (i
= 0; i
< TEST_MAX_INT
; i
++) {
1855 tt_int_op(evtag_decode_tag(&integer
, tmp
), !=, -1);
1856 tt_uint_op(integer
, ==, integers
[i
]);
1859 tt_uint_op(EVBUFFER_LENGTH(tmp
), ==, 0);
1866 evtag_test_peek(void *ptr
)
1868 struct evbuffer
*tmp
= evbuffer_new();
1871 evtag_marshal_int(tmp
, 30, 0);
1872 evtag_marshal_string(tmp
, 40, "Hello world");
1874 tt_int_op(evtag_peek(tmp
, &u32
), ==, 1);
1875 tt_int_op(u32
, ==, 30);
1876 tt_int_op(evtag_peek_length(tmp
, &u32
), ==, 0);
1877 tt_int_op(u32
, ==, 1+1+1);
1878 tt_int_op(evtag_consume(tmp
), ==, 0);
1880 tt_int_op(evtag_peek(tmp
, &u32
), ==, 1);
1881 tt_int_op(u32
, ==, 40);
1882 tt_int_op(evtag_peek_length(tmp
, &u32
), ==, 0);
1883 tt_int_op(u32
, ==, 1+1+11);
1884 tt_int_op(evtag_payload_length(tmp
, &u32
), ==, 0);
1885 tt_int_op(u32
, ==, 11);
1893 test_methods(void *ptr
)
1895 const char **methods
= event_get_supported_methods();
1896 struct event_config
*cfg
= NULL
;
1897 struct event_base
*base
= NULL
;
1898 const char *backend
;
1903 backend
= methods
[0];
1904 while (*methods
!= NULL
) {
1905 TT_BLATHER(("Support method: %s", *methods
));
1910 cfg
= event_config_new();
1911 assert(cfg
!= NULL
);
1913 tt_int_op(event_config_avoid_method(cfg
, backend
), ==, 0);
1914 event_config_set_flag(cfg
, EVENT_BASE_FLAG_IGNORE_ENV
);
1916 base
= event_base_new_with_config(cfg
);
1917 if (n_methods
> 1) {
1919 tt_str_op(backend
, !=, event_base_get_method(base
));
1921 tt_assert(base
== NULL
);
1926 event_base_free(base
);
1928 event_config_free(cfg
);
1932 test_version(void *arg
)
1936 int major
, minor
, patch
, n
;
1938 vstr
= event_get_version();
1939 vint
= event_get_version_number();
1944 tt_str_op(vstr
, ==, LIBEVENT_VERSION
);
1945 tt_int_op(vint
, ==, LIBEVENT_VERSION_NUMBER
);
1947 n
= sscanf(vstr
, "%d.%d.%d", &major
, &minor
, &patch
);
1949 tt_int_op((vint
&0xffffff00), ==, ((major
<<24)|(minor
<<16)|(patch
<<8)));
1955 test_base_features(void *arg
)
1957 struct event_base
*base
= NULL
;
1958 struct event_config
*cfg
= NULL
;
1960 cfg
= event_config_new();
1962 tt_assert(0 == event_config_require_features(cfg
, EV_FEATURE_ET
));
1964 base
= event_base_new_with_config(cfg
);
1966 tt_int_op(EV_FEATURE_ET
, ==,
1967 event_base_get_features(base
) & EV_FEATURE_ET
);
1969 base
= event_base_new();
1970 tt_int_op(0, ==, event_base_get_features(base
) & EV_FEATURE_ET
);
1975 event_base_free(base
);
1977 event_config_free(cfg
);
1980 #ifdef _EVENT_HAVE_SETENV
1982 #elif !defined(_EVENT_HAVE_SETENV) && defined(_EVENT_HAVE_PUTENV)
1983 static void setenv(const char *k
, const char *v
, int _o
)
1986 evutil_snprintf(b
, sizeof(b
), "%s=%s",k
,v
);
1992 #ifdef _EVENT_HAVE_UNSETENV
1994 #elif !defined(_EVENT_HAVE_UNSETENV) && defined(_EVENT_HAVE_PUTENV)
1995 static void unsetenv(const char *k
)
1998 evutil_snprintf(b
, sizeof(b
), "%s=",k
);
2004 #if defined(SETENV_OK) && defined(UNSETENV_OK)
2006 methodname_to_envvar(const char *mname
, char *buf
, size_t buflen
)
2009 evutil_snprintf(buf
, buflen
, "EVENT_NO%s", mname
);
2010 for (cp
= buf
; *cp
; ++cp
) {
2011 *cp
= EVUTIL_TOUPPER(*cp
);
2017 test_base_environ(void *arg
)
2019 struct event_base
*base
= NULL
;
2020 struct event_config
*cfg
= NULL
;
2022 #if defined(SETENV_OK) && defined(UNSETENV_OK)
2023 const char **basenames
;
2026 const char *defaultname
, *ignoreenvname
;
2028 /* See if unsetenv works before we rely on it. */
2029 setenv("EVENT_NOWAFFLES", "1", 1);
2030 unsetenv("EVENT_NOWAFFLES");
2031 if (getenv("EVENT_NOWAFFLES") != NULL
) {
2032 #ifndef _EVENT_HAVE_UNSETENV
2033 TT_DECLARE("NOTE", ("Can't fake unsetenv; skipping test"));
2035 TT_DECLARE("NOTE", ("unsetenv doesn't work; skipping test"));
2040 basenames
= event_get_supported_methods();
2041 for (i
= 0; basenames
[i
]; ++i
) {
2042 methodname_to_envvar(basenames
[i
], varbuf
, sizeof(varbuf
));
2047 base
= event_base_new();
2050 defaultname
= event_base_get_method(base
);
2051 TT_BLATHER(("default is <%s>", defaultname
));
2052 event_base_free(base
);
2055 /* Can we disable the method with EVENT_NOfoo ? */
2056 if (!strcmp(defaultname
, "epoll (with changelist)")) {
2057 setenv("EVENT_NOEPOLL", "1", 1);
2058 ignoreenvname
= "epoll";
2060 methodname_to_envvar(defaultname
, varbuf
, sizeof(varbuf
));
2061 setenv(varbuf
, "1", 1);
2062 ignoreenvname
= defaultname
;
2065 /* Use an empty cfg rather than NULL so a failure doesn't exit() */
2066 cfg
= event_config_new();
2067 base
= event_base_new_with_config(cfg
);
2068 event_config_free(cfg
);
2070 if (n_methods
== 1) {
2074 tt_str_op(defaultname
, !=, event_base_get_method(base
));
2075 event_base_free(base
);
2079 /* Can we disable looking at the environment with IGNORE_ENV ? */
2080 cfg
= event_config_new();
2081 event_config_set_flag(cfg
, EVENT_BASE_FLAG_IGNORE_ENV
);
2082 base
= event_base_new_with_config(cfg
);
2084 tt_str_op(ignoreenvname
, ==, event_base_get_method(base
));
2091 event_base_free(base
);
2093 event_config_free(cfg
);
2097 read_called_once_cb(evutil_socket_t fd
, short event
, void *arg
)
2099 tt_int_op(event
, ==, EV_READ
);
2106 timeout_called_once_cb(evutil_socket_t fd
, short event
, void *arg
)
2108 tt_int_op(event
, ==, EV_TIMEOUT
);
2115 test_event_once(void *ptr
)
2117 struct basic_test_data
*data
= ptr
;
2122 tv
.tv_usec
= 50*1000;
2124 r
= event_base_once(data
->base
, data
->pair
[0], EV_READ
,
2125 read_called_once_cb
, NULL
, NULL
);
2126 tt_int_op(r
, ==, 0);
2127 r
= event_base_once(data
->base
, -1, EV_TIMEOUT
,
2128 timeout_called_once_cb
, NULL
, &tv
);
2129 tt_int_op(r
, ==, 0);
2130 r
= event_base_once(data
->base
, -1, 0, NULL
, NULL
, NULL
);
2133 if (write(data
->pair
[1], TEST1
, strlen(TEST1
)+1) < 0) {
2134 tt_fail_perror("write");
2137 shutdown(data
->pair
[1], SHUT_WR
);
2139 event_base_dispatch(data
->base
);
2141 tt_int_op(called
, ==, 101);
2147 test_event_pending(void *ptr
)
2149 struct basic_test_data
*data
= ptr
;
2150 struct event
*r
=NULL
, *w
=NULL
, *t
=NULL
;
2151 struct timeval tv
, now
, tv2
, diff
;
2154 tv
.tv_usec
= 500 * 1000;
2155 r
= event_new(data
->base
, data
->pair
[0], EV_READ
, simple_read_cb
,
2157 w
= event_new(data
->base
, data
->pair
[1], EV_WRITE
, simple_write_cb
,
2159 t
= evtimer_new(data
->base
, timeout_cb
, NULL
);
2165 evutil_gettimeofday(&now
, NULL
);
2169 tt_assert( event_pending(r
, EV_READ
, NULL
));
2170 tt_assert(!event_pending(w
, EV_WRITE
, NULL
));
2171 tt_assert(!event_pending(r
, EV_WRITE
, NULL
));
2172 tt_assert( event_pending(r
, EV_READ
|EV_WRITE
, NULL
));
2173 tt_assert(!event_pending(r
, EV_TIMEOUT
, NULL
));
2174 tt_assert( event_pending(t
, EV_TIMEOUT
, NULL
));
2175 tt_assert( event_pending(t
, EV_TIMEOUT
, &tv2
));
2177 tt_assert(evutil_timercmp(&tv2
, &now
, >));
2178 evutil_timeradd(&now
, &tv
, &tv
);
2179 evutil_timersub(&tv2
, &tv
, &diff
);
2180 tt_int_op(diff
.tv_sec
, ==, 0);
2181 tt_int_op(labs(diff
.tv_usec
), <, 1000);
2199 /* You can't do this test on windows, since dup2 doesn't work on sockets */
2202 dfd_cb(evutil_socket_t fd
, short e
, void *data
)
2204 *(int*)data
= (int)e
;
2207 /* Regression test for our workaround for a fun epoll/linux related bug
2208 * where fd2 = dup(fd1); add(fd2); close(fd2); dup2(fd1,fd2); add(fd2)
2209 * will get you an EEXIST */
2211 test_dup_fd(void *arg
)
2213 struct basic_test_data
*data
= arg
;
2214 struct event_base
*base
= data
->base
;
2215 struct event
*ev1
=NULL
, *ev2
=NULL
;
2217 int ev1_got
, ev2_got
;
2219 tt_int_op(write(data
->pair
[0], "Hello world",
2220 strlen("Hello world")), >, 0);
2224 tt_int_op(dfd
, >=, 0);
2226 ev1
= event_new(base
, fd
, EV_READ
|EV_PERSIST
, dfd_cb
, &ev1_got
);
2227 ev2
= event_new(base
, dfd
, EV_READ
|EV_PERSIST
, dfd_cb
, &ev2_got
);
2228 ev1_got
= ev2_got
= 0;
2229 event_add(ev1
, NULL
);
2230 event_add(ev2
, NULL
);
2231 event_base_loop(base
, EVLOOP_ONCE
);
2232 tt_int_op(ev1_got
, ==, EV_READ
);
2233 tt_int_op(ev2_got
, ==, EV_READ
);
2235 /* Now close and delete dfd then dispatch. We need to do the
2236 * dispatch here so that when we add it later, we think there
2237 * was an intermediate delete. */
2240 ev1_got
= ev2_got
= 0;
2241 event_base_loop(base
, EVLOOP_ONCE
);
2242 tt_want_int_op(ev1_got
, ==, EV_READ
);
2243 tt_int_op(ev2_got
, ==, 0);
2245 /* Re-duplicate the fd. We need to get the same duplicated
2246 * value that we closed to provoke the epoll quirk. Also, we
2247 * need to change the events to write, or else the old lingering
2248 * read event will make the test pass whether the change was
2249 * successful or not. */
2250 tt_int_op(dup2(fd
, dfd
), ==, dfd
);
2252 ev2
= event_new(base
, dfd
, EV_WRITE
|EV_PERSIST
, dfd_cb
, &ev2_got
);
2253 event_add(ev2
, NULL
);
2254 ev1_got
= ev2_got
= 0;
2255 event_base_loop(base
, EVLOOP_ONCE
);
2256 tt_want_int_op(ev1_got
, ==, EV_READ
);
2257 tt_int_op(ev2_got
, ==, EV_WRITE
);
2269 #ifdef _EVENT_DISABLE_MM_REPLACEMENT
2271 test_mm_functions(void *arg
)
2273 _tinytest_set_test_skipped();
2277 check_dummy_mem_ok(void *_mem
)
2281 return !memcmp(mem
, "{[<guardedram>]}", 16);
2285 dummy_malloc(size_t len
)
2287 char *mem
= malloc(len
+16);
2288 memcpy(mem
, "{[<guardedram>]}", 16);
2293 dummy_realloc(void *_mem
, size_t len
)
2297 return dummy_malloc(len
);
2298 tt_want(check_dummy_mem_ok(_mem
));
2300 mem
= realloc(mem
, len
+16);
2305 dummy_free(void *_mem
)
2308 tt_want(check_dummy_mem_ok(_mem
));
2314 test_mm_functions(void *arg
)
2316 struct event_base
*b
= NULL
;
2317 struct event_config
*cfg
= NULL
;
2318 event_set_mem_functions(dummy_malloc
, dummy_realloc
, dummy_free
);
2319 cfg
= event_config_new();
2320 event_config_avoid_method(cfg
, "Nonesuch");
2321 b
= event_base_new_with_config(cfg
);
2323 tt_assert(check_dummy_mem_ok(b
));
2326 event_config_free(cfg
);
2333 many_event_cb(evutil_socket_t fd
, short event
, void *arg
)
2340 test_many_events(void *arg
)
2342 /* Try 70 events that should all be ready at once. This will
2343 * exercise the "resize" code on most of the backends, and will make
2344 * sure that we can get past the 64-handle limit of some windows
2348 struct basic_test_data
*data
= arg
;
2349 struct event_base
*base
= data
->base
;
2350 int one_at_a_time
= data
->setup_data
!= NULL
;
2351 evutil_socket_t sock
[MANY
];
2352 struct event
*ev
[MANY
];
2355 int loopflags
= EVLOOP_NONBLOCK
, evflags
=0;
2356 const int is_evport
= !strcmp(event_base_get_method(base
),"evport");
2357 if (one_at_a_time
) {
2358 loopflags
|= EVLOOP_ONCE
;
2359 evflags
= EV_PERSIST
;
2362 memset(sock
, 0xff, sizeof(sock
));
2363 memset(ev
, 0, sizeof(ev
));
2364 memset(xcalled
, 0, sizeof(xcalled
));
2365 if (is_evport
&& one_at_a_time
) {
2366 TT_DECLARE("NOTE", ("evport can't pass this in 2.0; skipping\n"));
2370 for (i
= 0; i
< MANY
; ++i
) {
2371 /* We need an event that will hit the backend, and that will
2372 * be ready immediately. "Send a datagram" is an easy
2373 * instance of that. */
2374 sock
[i
] = socket(AF_INET
, SOCK_DGRAM
, 0);
2375 tt_assert(sock
[i
] >= 0);
2377 ev
[i
] = event_new(base
, sock
[i
], EV_WRITE
|evflags
,
2378 many_event_cb
, &xcalled
[i
]);
2379 event_add(ev
[i
], NULL
);
2381 event_base_loop(base
, EVLOOP_NONBLOCK
|EVLOOP_ONCE
);
2384 event_base_loop(base
, loopflags
);
2386 for (i
= 0; i
< MANY
; ++i
) {
2388 tt_int_op(xcalled
[i
], ==, MANY
- i
+ 1);
2390 tt_int_op(xcalled
[i
], ==, 1);
2394 for (i
= 0; i
< MANY
; ++i
) {
2398 evutil_closesocket(sock
[i
]);
2404 test_struct_event_size(void *arg
)
2406 tt_int_op(event_get_struct_event_size(), <=, sizeof(struct event
));
2411 struct testcase_t main_testcases
[] = {
2412 /* Some converted-over tests */
2413 { "methods", test_methods
, TT_FORK
, NULL
, NULL
},
2414 { "version", test_version
, 0, NULL
, NULL
},
2415 BASIC(base_features
, TT_FORK
|TT_NO_LOGS
),
2416 { "base_environ", test_base_environ
, TT_FORK
, NULL
, NULL
},
2418 BASIC(event_base_new
, TT_FORK
|TT_NEED_SOCKETPAIR
),
2419 BASIC(free_active_base
, TT_FORK
|TT_NEED_SOCKETPAIR
),
2421 BASIC(manipulate_active_events
, TT_FORK
|TT_NEED_BASE
),
2423 BASIC(bad_assign
, TT_FORK
|TT_NEED_BASE
|TT_NO_LOGS
),
2424 BASIC(bad_reentrant
, TT_FORK
|TT_NEED_BASE
|TT_NO_LOGS
),
2426 LEGACY(persistent_timeout
, TT_FORK
|TT_NEED_BASE
),
2427 { "persistent_timeout_jump", test_persistent_timeout_jump
, TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
2428 { "persistent_active_timeout", test_persistent_active_timeout
,
2429 TT_FORK
|TT_NEED_BASE
, &basic_setup
, NULL
},
2430 LEGACY(priorities
, TT_FORK
|TT_NEED_BASE
),
2431 BASIC(priority_active_inversion
, TT_FORK
|TT_NEED_BASE
),
2432 { "common_timeout", test_common_timeout
, TT_FORK
|TT_NEED_BASE
,
2433 &basic_setup
, NULL
},
2435 /* These legacy tests may not all need all of these flags. */
2436 LEGACY(simpleread
, TT_ISOLATED
),
2437 LEGACY(simpleread_multiple
, TT_ISOLATED
),
2438 LEGACY(simplewrite
, TT_ISOLATED
),
2439 { "simpleclose", test_simpleclose
, TT_FORK
, &basic_setup
,
2441 LEGACY(multiple
, TT_ISOLATED
),
2442 LEGACY(persistent
, TT_ISOLATED
),
2443 LEGACY(combined
, TT_ISOLATED
),
2444 LEGACY(simpletimeout
, TT_ISOLATED
),
2445 LEGACY(loopbreak
, TT_ISOLATED
),
2446 LEGACY(loopexit
, TT_ISOLATED
),
2447 LEGACY(loopexit_multiple
, TT_ISOLATED
),
2448 LEGACY(nonpersist_readd
, TT_ISOLATED
),
2449 LEGACY(multiple_events_for_same_fd
, TT_ISOLATED
),
2450 LEGACY(want_only_once
, TT_ISOLATED
),
2451 { "event_once", test_event_once
, TT_ISOLATED
, &basic_setup
, NULL
},
2452 { "event_pending", test_event_pending
, TT_ISOLATED
, &basic_setup
,
2455 { "dup_fd", test_dup_fd
, TT_ISOLATED
, &basic_setup
, NULL
},
2457 { "mm_functions", test_mm_functions
, TT_FORK
, NULL
, NULL
},
2458 { "many_events", test_many_events
, TT_ISOLATED
, &basic_setup
, NULL
},
2459 { "many_events_slow_add", test_many_events
, TT_ISOLATED
, &basic_setup
, (void*)1 },
2461 { "struct_event_size", test_struct_event_size
, 0, NULL
, NULL
},
2464 LEGACY(fork
, TT_ISOLATED
),
2469 struct testcase_t evtag_testcases
[] = {
2470 { "int", evtag_int_test
, TT_FORK
, NULL
, NULL
},
2471 { "fuzz", evtag_fuzz
, TT_FORK
, NULL
, NULL
},
2472 { "encoding", evtag_tag_encoding
, TT_FORK
, NULL
, NULL
},
2473 { "peek", evtag_test_peek
, 0, NULL
, NULL
},
2478 struct testcase_t signal_testcases
[] = {
2480 LEGACY(simplesignal
, TT_ISOLATED
),
2481 LEGACY(multiplesignal
, TT_ISOLATED
),
2482 LEGACY(immediatesignal
, TT_ISOLATED
),
2483 LEGACY(signal_dealloc
, TT_ISOLATED
),
2484 LEGACY(signal_pipeloss
, TT_ISOLATED
),
2485 LEGACY(signal_switchbase
, TT_ISOLATED
|TT_NO_LOGS
),
2486 LEGACY(signal_restore
, TT_ISOLATED
),
2487 LEGACY(signal_assert
, TT_ISOLATED
),
2488 LEGACY(signal_while_processing
, TT_ISOLATED
),