1 /*
2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include "util-internal.h"
28
29 #ifdef _WIN32
30 #include <winsock2.h>
31 #include <windows.h>
32 #endif
33
34 #ifdef EVENT__HAVE_PTHREADS
35 #include <pthread.h>
36 #endif
37
38 #include "event2/event-config.h"
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #ifdef EVENT__HAVE_SYS_TIME_H
43 #include <sys/time.h>
44 #endif
45 #include <sys/queue.h>
46 #ifndef _WIN32
47 #include <sys/socket.h>
48 #include <sys/wait.h>
49 #include <limits.h>
50 #include <signal.h>
51 #include <unistd.h>
52 #include <netdb.h>
53 #endif
54 #include <fcntl.h>
55 #include <signal.h>
56 #include <stdlib.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <errno.h>
60 #include <assert.h>
61 #include <ctype.h>
62
63 #include "event2/event.h"
64 #include "event2/event_struct.h"
65 #include "event2/event_compat.h"
66 #include "event2/tag.h"
67 #include "event2/buffer.h"
68 #include "event2/buffer_compat.h"
69 #include "event2/util.h"
70 #include "event-internal.h"
71 #include "evthread-internal.h"
72 #include "log-internal.h"
73 #include "time-internal.h"
74
75 #include "regress.h"
76
77 #ifndef _WIN32
78 #include "regress.gen.h"
79 #endif
80
81 evutil_socket_t pair[2];
82 int test_ok;
83 int called;
84 struct event_base *global_base;
85
86 static char wbuf[4096];
87 static char rbuf[4096];
88 static int woff;
89 static int roff;
90 static int usepersist;
91 static struct timeval tset;
92 static struct timeval tcalled;
93
94
95 #define TEST1 "this is a test"
96
97 #ifdef _WIN32
98 #define write(fd,buf,len) send((fd),(buf),(int)(len),0)
99 #define read(fd,buf,len) recv((fd),(buf),(int)(len),0)
100 #endif
101
102 struct basic_cb_args
103 {
104 struct event_base *eb;
105 struct event *ev;
106 unsigned int callcount;
107 };
108
109 static void
simple_read_cb(evutil_socket_t fd,short event,void * arg)110 simple_read_cb(evutil_socket_t fd, short event, void *arg)
111 {
112 char buf[256];
113 int len;
114
115 len = read(fd, buf, sizeof(buf));
116
117 if (len) {
118 if (!called) {
119 if (event_add(arg, NULL) == -1)
120 exit(1);
121 }
122 } else if (called == 1)
123 test_ok = 1;
124
125 called++;
126 }
127
128 static void
basic_read_cb(evutil_socket_t fd,short event,void * data)129 basic_read_cb(evutil_socket_t fd, short event, void *data)
130 {
131 char buf[256];
132 int len;
133 struct basic_cb_args *arg = data;
134
135 len = read(fd, buf, sizeof(buf));
136
137 if (len < 0) {
138 tt_fail_perror("read (callback)");
139 } else {
140 switch (arg->callcount++) {
141 case 0: /* first call: expect to read data; cycle */
142 if (len > 0)
143 return;
144
145 tt_fail_msg("EOF before data read");
146 break;
147
148 case 1: /* second call: expect EOF; stop */
149 if (len > 0)
150 tt_fail_msg("not all data read on first cycle");
151 break;
152
153 default: /* third call: should not happen */
154 tt_fail_msg("too many cycles");
155 }
156 }
157
158 event_del(arg->ev);
159 event_base_loopexit(arg->eb, NULL);
160 }
161
162 static void
dummy_read_cb(evutil_socket_t fd,short event,void * arg)163 dummy_read_cb(evutil_socket_t fd, short event, void *arg)
164 {
165 }
166
167 static void
simple_write_cb(evutil_socket_t fd,short event,void * arg)168 simple_write_cb(evutil_socket_t fd, short event, void *arg)
169 {
170 int len;
171
172 len = write(fd, TEST1, strlen(TEST1) + 1);
173 if (len == -1)
174 test_ok = 0;
175 else
176 test_ok = 1;
177 }
178
179 static void
multiple_write_cb(evutil_socket_t fd,short event,void * arg)180 multiple_write_cb(evutil_socket_t fd, short event, void *arg)
181 {
182 struct event *ev = arg;
183 int len;
184
185 len = 128;
186 if (woff + len >= (int)sizeof(wbuf))
187 len = sizeof(wbuf) - woff;
188
189 len = write(fd, wbuf + woff, len);
190 if (len == -1) {
191 fprintf(stderr, "%s: write\n", __func__);
192 if (usepersist)
193 event_del(ev);
194 return;
195 }
196
197 woff += len;
198
199 if (woff >= (int)sizeof(wbuf)) {
200 shutdown(fd, EVUTIL_SHUT_WR);
201 if (usepersist)
202 event_del(ev);
203 return;
204 }
205
206 if (!usepersist) {
207 if (event_add(ev, NULL) == -1)
208 exit(1);
209 }
210 }
211
212 static void
multiple_read_cb(evutil_socket_t fd,short event,void * arg)213 multiple_read_cb(evutil_socket_t fd, short event, void *arg)
214 {
215 struct event *ev = arg;
216 int len;
217
218 len = read(fd, rbuf + roff, sizeof(rbuf) - roff);
219 if (len == -1)
220 fprintf(stderr, "%s: read\n", __func__);
221 if (len <= 0) {
222 if (usepersist)
223 event_del(ev);
224 return;
225 }
226
227 roff += len;
228 if (!usepersist) {
229 if (event_add(ev, NULL) == -1)
230 exit(1);
231 }
232 }
233
234 static void
timeout_cb(evutil_socket_t fd,short event,void * arg)235 timeout_cb(evutil_socket_t fd, short event, void *arg)
236 {
237 evutil_gettimeofday(&tcalled, NULL);
238 }
239
240 struct both {
241 struct event ev;
242 int nread;
243 };
244
245 static void
combined_read_cb(evutil_socket_t fd,short event,void * arg)246 combined_read_cb(evutil_socket_t fd, short event, void *arg)
247 {
248 struct both *both = arg;
249 char buf[128];
250 int len;
251
252 len = read(fd, buf, sizeof(buf));
253 if (len == -1)
254 fprintf(stderr, "%s: read\n", __func__);
255 if (len <= 0)
256 return;
257
258 both->nread += len;
259 if (event_add(&both->ev, NULL) == -1)
260 exit(1);
261 }
262
263 static void
combined_write_cb(evutil_socket_t fd,short event,void * arg)264 combined_write_cb(evutil_socket_t fd, short event, void *arg)
265 {
266 struct both *both = arg;
267 char buf[128];
268 int len;
269
270 len = sizeof(buf);
271 if (len > both->nread)
272 len = both->nread;
273
274 memset(buf, 'q', len);
275
276 len = write(fd, buf, len);
277 if (len == -1)
278 fprintf(stderr, "%s: write\n", __func__);
279 if (len <= 0) {
280 shutdown(fd, EVUTIL_SHUT_WR);
281 return;
282 }
283
284 both->nread -= len;
285 if (event_add(&both->ev, NULL) == -1)
286 exit(1);
287 }
288
289 /* These macros used to replicate the work of the legacy test wrapper code */
290 #define setup_test(x) do { \
291 if (!in_legacy_test_wrapper) { \
292 TT_FAIL(("Legacy test %s not wrapped properly", x)); \
293 return; \
294 } \
295 } while (0)
296 #define cleanup_test() setup_test("cleanup")
297
298 static void
test_simpleread(void)299 test_simpleread(void)
300 {
301 struct event ev;
302
303 /* Very simple read test */
304 setup_test("Simple read: ");
305
306 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
307 tt_fail_perror("write");
308 }
309
310 shutdown(pair[0], EVUTIL_SHUT_WR);
311
312 event_set(&ev, pair[1], EV_READ, simple_read_cb, &ev);
313 if (event_add(&ev, NULL) == -1)
314 exit(1);
315 event_dispatch();
316
317 cleanup_test();
318 }
319
320 static void
test_simplewrite(void)321 test_simplewrite(void)
322 {
323 struct event ev;
324
325 /* Very simple write test */
326 setup_test("Simple write: ");
327
328 event_set(&ev, pair[0], EV_WRITE, simple_write_cb, &ev);
329 if (event_add(&ev, NULL) == -1)
330 exit(1);
331 event_dispatch();
332
333 cleanup_test();
334 }
335
336 static void
simpleread_multiple_cb(evutil_socket_t fd,short event,void * arg)337 simpleread_multiple_cb(evutil_socket_t fd, short event, void *arg)
338 {
339 if (++called == 2)
340 test_ok = 1;
341 }
342
343 static void
test_simpleread_multiple(void)344 test_simpleread_multiple(void)
345 {
346 struct event one, two;
347
348 /* Very simple read test */
349 setup_test("Simple read to multiple evens: ");
350
351 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
352 tt_fail_perror("write");
353 }
354
355 shutdown(pair[0], EVUTIL_SHUT_WR);
356
357 event_set(&one, pair[1], EV_READ, simpleread_multiple_cb, NULL);
358 if (event_add(&one, NULL) == -1)
359 exit(1);
360 event_set(&two, pair[1], EV_READ, simpleread_multiple_cb, NULL);
361 if (event_add(&two, NULL) == -1)
362 exit(1);
363 event_dispatch();
364
365 cleanup_test();
366 }
367
368 static int have_closed = 0;
369 static int premature_event = 0;
370 static void
simpleclose_close_fd_cb(evutil_socket_t s,short what,void * ptr)371 simpleclose_close_fd_cb(evutil_socket_t s, short what, void *ptr)
372 {
373 evutil_socket_t **fds = ptr;
374 TT_BLATHER(("Closing"));
375 evutil_closesocket(*fds[0]);
376 evutil_closesocket(*fds[1]);
377 *fds[0] = -1;
378 *fds[1] = -1;
379 have_closed = 1;
380 }
381
382 static void
record_event_cb(evutil_socket_t s,short what,void * ptr)383 record_event_cb(evutil_socket_t s, short what, void *ptr)
384 {
385 short *whatp = ptr;
386 if (!have_closed)
387 premature_event = 1;
388 *whatp = what;
389 TT_BLATHER(("Recorded %d on socket %d", (int)what, (int)s));
390 }
391
392 static void
test_simpleclose(void * ptr)393 test_simpleclose(void *ptr)
394 {
395 /* Test that a close of FD is detected as a read and as a write. */
396 struct event_base *base = event_base_new();
397 evutil_socket_t pair1[2]={-1,-1}, pair2[2] = {-1, -1};
398 evutil_socket_t *to_close[2];
399 struct event *rev=NULL, *wev=NULL, *closeev=NULL;
400 struct timeval tv;
401 short got_read_on_close = 0, got_write_on_close = 0;
402 char buf[1024];
403 memset(buf, 99, sizeof(buf));
404 #ifdef _WIN32
405 #define LOCAL_SOCKETPAIR_AF AF_INET
406 #else
407 #define LOCAL_SOCKETPAIR_AF AF_UNIX
408 #endif
409 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair1)<0)
410 TT_DIE(("socketpair: %s", strerror(errno)));
411 if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, pair2)<0)
412 TT_DIE(("socketpair: %s", strerror(errno)));
413 if (evutil_make_socket_nonblocking(pair1[1]) < 0)
414 TT_DIE(("make_socket_nonblocking"));
415 if (evutil_make_socket_nonblocking(pair2[1]) < 0)
416 TT_DIE(("make_socket_nonblocking"));
417
418 /** Stuff pair2[1] full of data, until write fails */
419 while (1) {
420 int r = write(pair2[1], buf, sizeof(buf));
421 if (r<0) {
422 int err = evutil_socket_geterror(pair2[1]);
423 if (! EVUTIL_ERR_RW_RETRIABLE(err))
424 TT_DIE(("write failed strangely: %s",
425 evutil_socket_error_to_string(err)));
426 break;
427 }
428 }
429 to_close[0] = &pair1[0];
430 to_close[1] = &pair2[0];
431
432 closeev = event_new(base, -1, EV_TIMEOUT, simpleclose_close_fd_cb,
433 to_close);
434 rev = event_new(base, pair1[1], EV_READ, record_event_cb,
435 &got_read_on_close);
436 TT_BLATHER(("Waiting for read on %d", (int)pair1[1]));
437 wev = event_new(base, pair2[1], EV_WRITE, record_event_cb,
438 &got_write_on_close);
439 TT_BLATHER(("Waiting for write on %d", (int)pair2[1]));
440 tv.tv_sec = 0;
441 tv.tv_usec = 100*1000; /* Close pair1[0] after a little while, and make
442 * sure we get a read event. */
443 event_add(closeev, &tv);
444 event_add(rev, NULL);
445 event_add(wev, NULL);
446 /* Don't let the test go on too long. */
447 tv.tv_sec = 0;
448 tv.tv_usec = 200*1000;
449 event_base_loopexit(base, &tv);
450 event_base_loop(base, 0);
451
452 tt_int_op(got_read_on_close, ==, EV_READ);
453 tt_int_op(got_write_on_close, ==, EV_WRITE);
454 tt_int_op(premature_event, ==, 0);
455
456 end:
457 if (pair1[0] >= 0)
458 evutil_closesocket(pair1[0]);
459 if (pair1[1] >= 0)
460 evutil_closesocket(pair1[1]);
461 if (pair2[0] >= 0)
462 evutil_closesocket(pair2[0]);
463 if (pair2[1] >= 0)
464 evutil_closesocket(pair2[1]);
465 if (rev)
466 event_free(rev);
467 if (wev)
468 event_free(wev);
469 if (closeev)
470 event_free(closeev);
471 if (base)
472 event_base_free(base);
473 }
474
475
476 static void
test_multiple(void)477 test_multiple(void)
478 {
479 struct event ev, ev2;
480 int i;
481
482 /* Multiple read and write test */
483 setup_test("Multiple read/write: ");
484 memset(rbuf, 0, sizeof(rbuf));
485 for (i = 0; i < (int)sizeof(wbuf); i++)
486 wbuf[i] = i;
487
488 roff = woff = 0;
489 usepersist = 0;
490
491 event_set(&ev, pair[0], EV_WRITE, multiple_write_cb, &ev);
492 if (event_add(&ev, NULL) == -1)
493 exit(1);
494 event_set(&ev2, pair[1], EV_READ, multiple_read_cb, &ev2);
495 if (event_add(&ev2, NULL) == -1)
496 exit(1);
497 event_dispatch();
498
499 if (roff == woff)
500 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
501
502 cleanup_test();
503 }
504
505 static void
test_persistent(void)506 test_persistent(void)
507 {
508 struct event ev, ev2;
509 int i;
510
511 /* Multiple read and write test with persist */
512 setup_test("Persist read/write: ");
513 memset(rbuf, 0, sizeof(rbuf));
514 for (i = 0; i < (int)sizeof(wbuf); i++)
515 wbuf[i] = i;
516
517 roff = woff = 0;
518 usepersist = 1;
519
520 event_set(&ev, pair[0], EV_WRITE|EV_PERSIST, multiple_write_cb, &ev);
521 if (event_add(&ev, NULL) == -1)
522 exit(1);
523 event_set(&ev2, pair[1], EV_READ|EV_PERSIST, multiple_read_cb, &ev2);
524 if (event_add(&ev2, NULL) == -1)
525 exit(1);
526 event_dispatch();
527
528 if (roff == woff)
529 test_ok = memcmp(rbuf, wbuf, sizeof(wbuf)) == 0;
530
531 cleanup_test();
532 }
533
534 static void
test_combined(void)535 test_combined(void)
536 {
537 struct both r1, r2, w1, w2;
538
539 setup_test("Combined read/write: ");
540 memset(&r1, 0, sizeof(r1));
541 memset(&r2, 0, sizeof(r2));
542 memset(&w1, 0, sizeof(w1));
543 memset(&w2, 0, sizeof(w2));
544
545 w1.nread = 4096;
546 w2.nread = 8192;
547
548 event_set(&r1.ev, pair[0], EV_READ, combined_read_cb, &r1);
549 event_set(&w1.ev, pair[0], EV_WRITE, combined_write_cb, &w1);
550 event_set(&r2.ev, pair[1], EV_READ, combined_read_cb, &r2);
551 event_set(&w2.ev, pair[1], EV_WRITE, combined_write_cb, &w2);
552 tt_assert(event_add(&r1.ev, NULL) != -1);
553 tt_assert(!event_add(&w1.ev, NULL));
554 tt_assert(!event_add(&r2.ev, NULL));
555 tt_assert(!event_add(&w2.ev, NULL));
556 event_dispatch();
557
558 if (r1.nread == 8192 && r2.nread == 4096)
559 test_ok = 1;
560
561 end:
562 cleanup_test();
563 }
564
565 static void
test_simpletimeout(void)566 test_simpletimeout(void)
567 {
568 struct timeval tv;
569 struct event ev;
570
571 setup_test("Simple timeout: ");
572
573 tv.tv_usec = 200*1000;
574 tv.tv_sec = 0;
575 evutil_timerclear(&tcalled);
576 evtimer_set(&ev, timeout_cb, NULL);
577 evtimer_add(&ev, &tv);
578
579 evutil_gettimeofday(&tset, NULL);
580 event_dispatch();
581 test_timeval_diff_eq(&tset, &tcalled, 200);
582
583 test_ok = 1;
584 end:
585 cleanup_test();
586 }
587
588 static void
periodic_timeout_cb(evutil_socket_t fd,short event,void * arg)589 periodic_timeout_cb(evutil_socket_t fd, short event, void *arg)
590 {
591 int *count = arg;
592
593 (*count)++;
594 if (*count == 6) {
595 /* call loopexit only once - on slow machines(?), it is
596 * apparently possible for this to get called twice. */
597 test_ok = 1;
598 event_base_loopexit(global_base, NULL);
599 }
600 }
601
602 static void
test_persistent_timeout(void)603 test_persistent_timeout(void)
604 {
605 struct timeval tv;
606 struct event ev;
607 int count = 0;
608
609 evutil_timerclear(&tv);
610 tv.tv_usec = 10000;
611
612 event_assign(&ev, global_base, -1, EV_TIMEOUT|EV_PERSIST,
613 periodic_timeout_cb, &count);
614 event_add(&ev, &tv);
615
616 event_dispatch();
617
618 event_del(&ev);
619 }
620
621 static void
test_persistent_timeout_jump(void * ptr)622 test_persistent_timeout_jump(void *ptr)
623 {
624 struct basic_test_data *data = ptr;
625 struct event ev;
626 int count = 0;
627 struct timeval msec100 = { 0, 100 * 1000 };
628 struct timeval msec50 = { 0, 50 * 1000 };
629 struct timeval msec300 = { 0, 300 * 1000 };
630
631 event_assign(&ev, data->base, -1, EV_PERSIST, periodic_timeout_cb, &count);
632 event_add(&ev, &msec100);
633 /* Wait for a bit */
634 evutil_usleep_(&msec300);
635 event_base_loopexit(data->base, &msec50);
636 event_base_dispatch(data->base);
637 tt_int_op(count, ==, 1);
638
639 end:
640 event_del(&ev);
641 }
642
643 struct persist_active_timeout_called {
644 int n;
645 short events[16];
646 struct timeval tvs[16];
647 };
648
649 static void
activate_cb(evutil_socket_t fd,short event,void * arg)650 activate_cb(evutil_socket_t fd, short event, void *arg)
651 {
652 struct event *ev = arg;
653 event_active(ev, EV_READ, 1);
654 }
655
656 static void
persist_active_timeout_cb(evutil_socket_t fd,short event,void * arg)657 persist_active_timeout_cb(evutil_socket_t fd, short event, void *arg)
658 {
659 struct persist_active_timeout_called *c = arg;
660 if (c->n < 15) {
661 c->events[c->n] = event;
662 evutil_gettimeofday(&c->tvs[c->n], NULL);
663 ++c->n;
664 }
665 }
666
667 static void
test_persistent_active_timeout(void * ptr)668 test_persistent_active_timeout(void *ptr)
669 {
670 struct timeval tv, tv2, tv_exit, start;
671 struct event ev;
672 struct persist_active_timeout_called res;
673
674 struct basic_test_data *data = ptr;
675 struct event_base *base = data->base;
676
677 memset(&res, 0, sizeof(res));
678
679 tv.tv_sec = 0;
680 tv.tv_usec = 200 * 1000;
681 event_assign(&ev, base, -1, EV_TIMEOUT|EV_PERSIST,
682 persist_active_timeout_cb, &res);
683 event_add(&ev, &tv);
684
685 tv2.tv_sec = 0;
686 tv2.tv_usec = 100 * 1000;
687 event_base_once(base, -1, EV_TIMEOUT, activate_cb, &ev, &tv2);
688
689 tv_exit.tv_sec = 0;
690 tv_exit.tv_usec = 600 * 1000;
691 event_base_loopexit(base, &tv_exit);
692
693 event_base_assert_ok_(base);
694 evutil_gettimeofday(&start, NULL);
695
696 event_base_dispatch(base);
697 event_base_assert_ok_(base);
698
699 tt_int_op(res.n, ==, 3);
700 tt_int_op(res.events[0], ==, EV_READ);
701 tt_int_op(res.events[1], ==, EV_TIMEOUT);
702 tt_int_op(res.events[2], ==, EV_TIMEOUT);
703 test_timeval_diff_eq(&start, &res.tvs[0], 100);
704 test_timeval_diff_eq(&start, &res.tvs[1], 300);
705 test_timeval_diff_eq(&start, &res.tvs[2], 500);
706 end:
707 event_del(&ev);
708 }
709
710 struct common_timeout_info {
711 struct event ev;
712 struct timeval called_at;
713 int which;
714 int count;
715 };
716
717 static void
common_timeout_cb(evutil_socket_t fd,short event,void * arg)718 common_timeout_cb(evutil_socket_t fd, short event, void *arg)
719 {
720 struct common_timeout_info *ti = arg;
721 ++ti->count;
722 evutil_gettimeofday(&ti->called_at, NULL);
723 if (ti->count >= 4)
724 event_del(&ti->ev);
725 }
726
727 static void
test_common_timeout(void * ptr)728 test_common_timeout(void *ptr)
729 {
730 struct basic_test_data *data = ptr;
731
732 struct event_base *base = data->base;
733 int i;
734 struct common_timeout_info info[100];
735
736 struct timeval start;
737 struct timeval tmp_100_ms = { 0, 100*1000 };
738 struct timeval tmp_200_ms = { 0, 200*1000 };
739 struct timeval tmp_5_sec = { 5, 0 };
740 struct timeval tmp_5M_usec = { 0, 5*1000*1000 };
741
742 const struct timeval *ms_100, *ms_200, *sec_5;
743
744 ms_100 = event_base_init_common_timeout(base, &tmp_100_ms);
745 ms_200 = event_base_init_common_timeout(base, &tmp_200_ms);
746 sec_5 = event_base_init_common_timeout(base, &tmp_5_sec);
747 tt_assert(ms_100);
748 tt_assert(ms_200);
749 tt_assert(sec_5);
750 tt_ptr_op(event_base_init_common_timeout(base, &tmp_200_ms),
751 ==, ms_200);
752 tt_ptr_op(event_base_init_common_timeout(base, ms_200), ==, ms_200);
753 tt_ptr_op(event_base_init_common_timeout(base, &tmp_5M_usec), ==, sec_5);
754 tt_int_op(ms_100->tv_sec, ==, 0);
755 tt_int_op(ms_200->tv_sec, ==, 0);
756 tt_int_op(sec_5->tv_sec, ==, 5);
757 tt_int_op(ms_100->tv_usec, ==, 100000|0x50000000);
758 tt_int_op(ms_200->tv_usec, ==, 200000|0x50100000);
759 tt_int_op(sec_5->tv_usec, ==, 0|0x50200000);
760
761 memset(info, 0, sizeof(info));
762
763 for (i=0; i<100; ++i) {
764 info[i].which = i;
765 event_assign(&info[i].ev, base, -1, EV_TIMEOUT|EV_PERSIST,
766 common_timeout_cb, &info[i]);
767 if (i % 2) {
768 if ((i%20)==1) {
769 /* Glass-box test: Make sure we survive the
770 * transition to non-common timeouts. It's
771 * a little tricky. */
772 event_add(&info[i].ev, ms_200);
773 event_add(&info[i].ev, &tmp_100_ms);
774 } else if ((i%20)==3) {
775 /* Check heap-to-common too. */
776 event_add(&info[i].ev, &tmp_200_ms);
777 event_add(&info[i].ev, ms_100);
778 } else if ((i%20)==5) {
779 /* Also check common-to-common. */
780 event_add(&info[i].ev, ms_200);
781 event_add(&info[i].ev, ms_100);
782 } else {
783 event_add(&info[i].ev, ms_100);
784 }
785 } else {
786 event_add(&info[i].ev, ms_200);
787 }
788 }
789
790 event_base_assert_ok_(base);
791 evutil_gettimeofday(&start, NULL);
792 event_base_dispatch(base);
793
794 event_base_assert_ok_(base);
795
796 for (i=0; i<10; ++i) {
797 tt_int_op(info[i].count, ==, 4);
798 if (i % 2) {
799 test_timeval_diff_eq(&start, &info[i].called_at, 400);
800 } else {
801 test_timeval_diff_eq(&start, &info[i].called_at, 800);
802 }
803 }
804
805 /* Make sure we can free the base with some events in. */
806 for (i=0; i<100; ++i) {
807 if (i % 2) {
808 event_add(&info[i].ev, ms_100);
809 } else {
810 event_add(&info[i].ev, ms_200);
811 }
812 }
813
814 end:
815 event_base_free(data->base); /* need to do this here before info is
816 * out-of-scope */
817 data->base = NULL;
818 }
819
820 #ifndef _WIN32
821
822 #define current_base event_global_current_base_
823 extern struct event_base *current_base;
824
825 static void
fork_signal_cb(evutil_socket_t fd,short events,void * arg)826 fork_signal_cb(evutil_socket_t fd, short events, void *arg)
827 {
828 event_del(arg);
829 }
830
831 int child_pair[2] = { -1, -1 };
832 static void
simple_child_read_cb(evutil_socket_t fd,short event,void * arg)833 simple_child_read_cb(evutil_socket_t fd, short event, void *arg)
834 {
835 char buf[256];
836 int len;
837
838 len = read(fd, buf, sizeof(buf));
839 if (write(child_pair[0], "", 1) < 0)
840 tt_fail_perror("write");
841
842 if (len) {
843 if (!called) {
844 if (event_add(arg, NULL) == -1)
845 exit(1);
846 }
847 } else if (called == 1)
848 test_ok = 1;
849
850 called++;
851 }
852
853 #define TEST_FORK_EXIT_SUCCESS 76
fork_wait_check(int pid)854 static void fork_wait_check(int pid)
855 {
856 int status;
857
858 TT_BLATHER(("Before waitpid"));
859
860 #ifdef WNOWAIT
861 if ((waitpid(pid, &status, WNOWAIT) == -1 && errno == EINVAL) &&
862 #else
863 if (
864 #endif
865 waitpid(pid, &status, 0) == -1) {
866 perror("waitpid");
867 exit(1);
868 }
869 TT_BLATHER(("After waitpid"));
870
871 if (WEXITSTATUS(status) != TEST_FORK_EXIT_SUCCESS) {
872 fprintf(stdout, "FAILED (exit): %d\n", WEXITSTATUS(status));
873 exit(1);
874 }
875 }
876 static void
test_fork(void)877 test_fork(void)
878 {
879 char c;
880 struct event ev, sig_ev, usr_ev, existing_ev;
881 pid_t pid;
882
883 setup_test("After fork: ");
884
885 {
886 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, child_pair) == -1) {
887 fprintf(stderr, "%s: socketpair\n", __func__);
888 exit(1);
889 }
890
891 if (evutil_make_socket_nonblocking(child_pair[0]) == -1) {
892 fprintf(stderr, "fcntl(O_NONBLOCK)");
893 exit(1);
894 }
895 }
896
897 tt_assert(current_base);
898 evthread_make_base_notifiable(current_base);
899
900 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
901 tt_fail_perror("write");
902 }
903
904 event_set(&ev, pair[1], EV_READ, simple_child_read_cb, &ev);
905 if (event_add(&ev, NULL) == -1)
906 exit(1);
907
908 evsignal_set(&sig_ev, SIGCHLD, fork_signal_cb, &sig_ev);
909 evsignal_add(&sig_ev, NULL);
910
911 evsignal_set(&existing_ev, SIGUSR2, fork_signal_cb, &existing_ev);
912 evsignal_add(&existing_ev, NULL);
913
914 event_base_assert_ok_(current_base);
915 TT_BLATHER(("Before fork"));
916 if ((pid = regress_fork()) == 0) {
917 /* in the child */
918 TT_BLATHER(("In child, before reinit"));
919 event_base_assert_ok_(current_base);
920 if (event_reinit(current_base) == -1) {
921 fprintf(stdout, "FAILED (reinit)\n");
922 exit(1);
923 }
924 TT_BLATHER(("After reinit"));
925 event_base_assert_ok_(current_base);
926 TT_BLATHER(("After assert-ok"));
927
928 evsignal_del(&sig_ev);
929
930 evsignal_set(&usr_ev, SIGUSR1, fork_signal_cb, &usr_ev);
931 evsignal_add(&usr_ev, NULL);
932 kill(getpid(), SIGUSR1);
933 kill(getpid(), SIGUSR2);
934
935 called = 0;
936
937 event_dispatch();
938
939 event_base_free(current_base);
940
941 /* we do not send an EOF; simple_read_cb requires an EOF
942 * to set test_ok. we just verify that the callback was
943 * called. */
944 exit(test_ok != 0 || called != 2 ? -2 : TEST_FORK_EXIT_SUCCESS);
945 }
946
947 /** wait until client read first message */
948 if (read(child_pair[1], &c, 1) < 0) {
949 tt_fail_perror("read");
950 }
951 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
952 tt_fail_perror("write");
953 }
954
955 fork_wait_check(pid);
956
957 /* test that the current event loop still works */
958 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
959 fprintf(stderr, "%s: write\n", __func__);
960 }
961
962 shutdown(pair[0], EVUTIL_SHUT_WR);
963
964 evsignal_set(&usr_ev, SIGUSR1, fork_signal_cb, &usr_ev);
965 evsignal_add(&usr_ev, NULL);
966 kill(getpid(), SIGUSR1);
967 kill(getpid(), SIGUSR2);
968
969 event_dispatch();
970
971 evsignal_del(&sig_ev);
972 tt_int_op(test_ok, ==, 1);
973
974 end:
975 cleanup_test();
976 if (child_pair[0] != -1)
977 evutil_closesocket(child_pair[0]);
978 if (child_pair[1] != -1)
979 evutil_closesocket(child_pair[1]);
980 }
981
982 #ifdef EVENT__HAVE_PTHREADS
del_wait_thread(void * arg)983 static void* del_wait_thread(void *arg)
984 {
985 struct timeval tv_start, tv_end;
986
987 evutil_gettimeofday(&tv_start, NULL);
988 event_dispatch();
989 evutil_gettimeofday(&tv_end, NULL);
990
991 test_timeval_diff_eq(&tv_start, &tv_end, 300);
992
993 end:
994 return NULL;
995 }
996
997 static void
del_wait_cb(evutil_socket_t fd,short event,void * arg)998 del_wait_cb(evutil_socket_t fd, short event, void *arg)
999 {
1000 struct timeval delay = { 0, 300*1000 };
1001 TT_BLATHER(("Sleeping: %i", test_ok));
1002 evutil_usleep_(&delay);
1003 ++test_ok;
1004 }
1005
1006 static void
test_del_wait(void)1007 test_del_wait(void)
1008 {
1009 struct event ev;
1010 pthread_t thread;
1011
1012 setup_test("event_del will wait: ");
1013
1014 event_set(&ev, pair[1], EV_READ|EV_PERSIST, del_wait_cb, &ev);
1015 event_add(&ev, NULL);
1016
1017 pthread_create(&thread, NULL, del_wait_thread, NULL);
1018
1019 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
1020 tt_fail_perror("write");
1021 }
1022
1023 {
1024 struct timeval delay = { 0, 30*1000 };
1025 evutil_usleep_(&delay);
1026 }
1027
1028 {
1029 struct timeval tv_start, tv_end;
1030 evutil_gettimeofday(&tv_start, NULL);
1031 event_del(&ev);
1032 evutil_gettimeofday(&tv_end, NULL);
1033 test_timeval_diff_eq(&tv_start, &tv_end, 270);
1034 }
1035
1036 pthread_join(thread, NULL);
1037
1038 tt_int_op(test_ok, ==, 1);
1039
1040 end:
1041 ;
1042 }
1043
null_cb(evutil_socket_t fd,short what,void * arg)1044 static void null_cb(evutil_socket_t fd, short what, void *arg) {}
test_del_notify_thread(void * arg)1045 static void* test_del_notify_thread(void *arg)
1046 {
1047 event_dispatch();
1048 return NULL;
1049 }
1050 static void
test_del_notify(void)1051 test_del_notify(void)
1052 {
1053 struct event ev;
1054 pthread_t thread;
1055
1056 test_ok = 1;
1057
1058 event_set(&ev, -1, EV_READ, null_cb, &ev);
1059 event_add(&ev, NULL);
1060
1061 pthread_create(&thread, NULL, test_del_notify_thread, NULL);
1062
1063 {
1064 struct timeval delay = { 0, 1000 };
1065 evutil_usleep_(&delay);
1066 }
1067
1068 event_del(&ev);
1069 pthread_join(thread, NULL);
1070 }
1071 #endif
1072
1073 static void
signal_cb_sa(int sig)1074 signal_cb_sa(int sig)
1075 {
1076 test_ok = 2;
1077 }
1078
1079 static void
signal_cb(evutil_socket_t fd,short event,void * arg)1080 signal_cb(evutil_socket_t fd, short event, void *arg)
1081 {
1082 struct event *ev = arg;
1083
1084 evsignal_del(ev);
1085 test_ok = 1;
1086 }
1087
1088 static void
test_simplesignal_impl(int find_reorder)1089 test_simplesignal_impl(int find_reorder)
1090 {
1091 struct event ev;
1092 struct itimerval itv;
1093
1094 evsignal_set(&ev, SIGALRM, signal_cb, &ev);
1095 evsignal_add(&ev, NULL);
1096 /* find bugs in which operations are re-ordered */
1097 if (find_reorder) {
1098 evsignal_del(&ev);
1099 evsignal_add(&ev, NULL);
1100 }
1101
1102 memset(&itv, 0, sizeof(itv));
1103 itv.it_value.tv_sec = 0;
1104 itv.it_value.tv_usec = 100000;
1105 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1106 goto skip_simplesignal;
1107
1108 event_dispatch();
1109 skip_simplesignal:
1110 if (evsignal_del(&ev) == -1)
1111 test_ok = 0;
1112
1113 cleanup_test();
1114 }
1115
1116 static void
test_simplestsignal(void)1117 test_simplestsignal(void)
1118 {
1119 setup_test("Simplest one signal: ");
1120 test_simplesignal_impl(0);
1121 }
1122
1123 static void
test_simplesignal(void)1124 test_simplesignal(void)
1125 {
1126 setup_test("Simple signal: ");
1127 test_simplesignal_impl(1);
1128 }
1129
1130 static void
test_multiplesignal(void)1131 test_multiplesignal(void)
1132 {
1133 struct event ev_one, ev_two;
1134 struct itimerval itv;
1135
1136 setup_test("Multiple signal: ");
1137
1138 evsignal_set(&ev_one, SIGALRM, signal_cb, &ev_one);
1139 evsignal_add(&ev_one, NULL);
1140
1141 evsignal_set(&ev_two, SIGALRM, signal_cb, &ev_two);
1142 evsignal_add(&ev_two, NULL);
1143
1144 memset(&itv, 0, sizeof(itv));
1145 itv.it_value.tv_sec = 0;
1146 itv.it_value.tv_usec = 100000;
1147 if (setitimer(ITIMER_REAL, &itv, NULL) == -1)
1148 goto skip_simplesignal;
1149
1150 event_dispatch();
1151
1152 skip_simplesignal:
1153 if (evsignal_del(&ev_one) == -1)
1154 test_ok = 0;
1155 if (evsignal_del(&ev_two) == -1)
1156 test_ok = 0;
1157
1158 cleanup_test();
1159 }
1160
1161 static void
test_immediatesignal(void)1162 test_immediatesignal(void)
1163 {
1164 struct event ev;
1165
1166 test_ok = 0;
1167 evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1168 evsignal_add(&ev, NULL);
1169 kill(getpid(), SIGUSR1);
1170 event_loop(EVLOOP_NONBLOCK);
1171 evsignal_del(&ev);
1172 cleanup_test();
1173 }
1174
1175 static void
test_signal_dealloc(void)1176 test_signal_dealloc(void)
1177 {
1178 /* make sure that evsignal_event is event_del'ed and pipe closed */
1179 struct event ev;
1180 struct event_base *base = event_init();
1181 evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1182 evsignal_add(&ev, NULL);
1183 evsignal_del(&ev);
1184 event_base_free(base);
1185 /* If we got here without asserting, we're fine. */
1186 test_ok = 1;
1187 cleanup_test();
1188 }
1189
1190 static void
test_signal_pipeloss(void)1191 test_signal_pipeloss(void)
1192 {
1193 /* make sure that the base1 pipe is closed correctly. */
1194 struct event_base *base1, *base2;
1195 int pipe1;
1196 test_ok = 0;
1197 base1 = event_init();
1198 pipe1 = base1->sig.ev_signal_pair[0];
1199 base2 = event_init();
1200 event_base_free(base2);
1201 event_base_free(base1);
1202 if (close(pipe1) != -1 || errno!=EBADF) {
1203 /* fd must be closed, so second close gives -1, EBADF */
1204 printf("signal pipe not closed. ");
1205 test_ok = 0;
1206 } else {
1207 test_ok = 1;
1208 }
1209 cleanup_test();
1210 }
1211
1212 /*
1213 * make two bases to catch signals, use both of them. this only works
1214 * for event mechanisms that use our signal pipe trick. kqueue handles
1215 * signals internally, and all interested kqueues get all the signals.
1216 */
1217 static void
test_signal_switchbase(void)1218 test_signal_switchbase(void)
1219 {
1220 struct event ev1, ev2;
1221 struct event_base *base1, *base2;
1222 int is_kqueue;
1223 test_ok = 0;
1224 base1 = event_init();
1225 base2 = event_init();
1226 is_kqueue = !strcmp(event_get_method(),"kqueue");
1227 evsignal_set(&ev1, SIGUSR1, signal_cb, &ev1);
1228 evsignal_set(&ev2, SIGUSR1, signal_cb, &ev2);
1229 if (event_base_set(base1, &ev1) ||
1230 event_base_set(base2, &ev2) ||
1231 event_add(&ev1, NULL) ||
1232 event_add(&ev2, NULL)) {
1233 fprintf(stderr, "%s: cannot set base, add\n", __func__);
1234 exit(1);
1235 }
1236
1237 tt_ptr_op(event_get_base(&ev1), ==, base1);
1238 tt_ptr_op(event_get_base(&ev2), ==, base2);
1239
1240 test_ok = 0;
1241 /* can handle signal before loop is called */
1242 kill(getpid(), SIGUSR1);
1243 event_base_loop(base2, EVLOOP_NONBLOCK);
1244 if (is_kqueue) {
1245 if (!test_ok)
1246 goto end;
1247 test_ok = 0;
1248 }
1249 event_base_loop(base1, EVLOOP_NONBLOCK);
1250 if (test_ok && !is_kqueue) {
1251 test_ok = 0;
1252
1253 /* set base1 to handle signals */
1254 event_base_loop(base1, EVLOOP_NONBLOCK);
1255 kill(getpid(), SIGUSR1);
1256 event_base_loop(base1, EVLOOP_NONBLOCK);
1257 event_base_loop(base2, EVLOOP_NONBLOCK);
1258 }
1259 end:
1260 event_base_free(base1);
1261 event_base_free(base2);
1262 cleanup_test();
1263 }
1264
1265 /*
1266 * assert that a signal event removed from the event queue really is
1267 * removed - with no possibility of it's parent handler being fired.
1268 */
1269 static void
test_signal_assert(void)1270 test_signal_assert(void)
1271 {
1272 struct event ev;
1273 struct event_base *base = event_init();
1274 test_ok = 0;
1275 /* use SIGCONT so we don't kill ourselves when we signal to nowhere */
1276 evsignal_set(&ev, SIGCONT, signal_cb, &ev);
1277 evsignal_add(&ev, NULL);
1278 /*
1279 * if evsignal_del() fails to reset the handler, it's current handler
1280 * will still point to evsig_handler().
1281 */
1282 evsignal_del(&ev);
1283
1284 kill(getpid(), SIGCONT);
1285 #if 0
1286 /* only way to verify we were in evsig_handler() */
1287 /* XXXX Now there's no longer a good way. */
1288 if (base->sig.evsig_caught)
1289 test_ok = 0;
1290 else
1291 test_ok = 1;
1292 #else
1293 test_ok = 1;
1294 #endif
1295
1296 event_base_free(base);
1297 cleanup_test();
1298 return;
1299 }
1300
1301 /*
1302 * assert that we restore our previous signal handler properly.
1303 */
1304 static void
test_signal_restore(void)1305 test_signal_restore(void)
1306 {
1307 struct event ev;
1308 struct event_base *base = event_init();
1309 #ifdef EVENT__HAVE_SIGACTION
1310 struct sigaction sa;
1311 #endif
1312
1313 test_ok = 0;
1314 #ifdef EVENT__HAVE_SIGACTION
1315 sa.sa_handler = signal_cb_sa;
1316 sa.sa_flags = 0x0;
1317 sigemptyset(&sa.sa_mask);
1318 if (sigaction(SIGUSR1, &sa, NULL) == -1)
1319 goto out;
1320 #else
1321 if (signal(SIGUSR1, signal_cb_sa) == SIG_ERR)
1322 goto out;
1323 #endif
1324 evsignal_set(&ev, SIGUSR1, signal_cb, &ev);
1325 evsignal_add(&ev, NULL);
1326 evsignal_del(&ev);
1327
1328 kill(getpid(), SIGUSR1);
1329 /* 1 == signal_cb, 2 == signal_cb_sa, we want our previous handler */
1330 if (test_ok != 2)
1331 test_ok = 0;
1332 out:
1333 event_base_free(base);
1334 cleanup_test();
1335 return;
1336 }
1337
1338 static void
signal_cb_swp(int sig,short event,void * arg)1339 signal_cb_swp(int sig, short event, void *arg)
1340 {
1341 called++;
1342 if (called < 5)
1343 kill(getpid(), sig);
1344 else
1345 event_loopexit(NULL);
1346 }
1347 static void
timeout_cb_swp(evutil_socket_t fd,short event,void * arg)1348 timeout_cb_swp(evutil_socket_t fd, short event, void *arg)
1349 {
1350 if (called == -1) {
1351 struct timeval tv = {5, 0};
1352
1353 called = 0;
1354 evtimer_add((struct event *)arg, &tv);
1355 kill(getpid(), SIGUSR1);
1356 return;
1357 }
1358 test_ok = 0;
1359 event_loopexit(NULL);
1360 }
1361
1362 static void
test_signal_while_processing(void)1363 test_signal_while_processing(void)
1364 {
1365 struct event_base *base = event_init();
1366 struct event ev, ev_timer;
1367 struct timeval tv = {0, 0};
1368
1369 setup_test("Receiving a signal while processing other signal: ");
1370
1371 called = -1;
1372 test_ok = 1;
1373 signal_set(&ev, SIGUSR1, signal_cb_swp, NULL);
1374 signal_add(&ev, NULL);
1375 evtimer_set(&ev_timer, timeout_cb_swp, &ev_timer);
1376 evtimer_add(&ev_timer, &tv);
1377 event_dispatch();
1378
1379 event_base_free(base);
1380 cleanup_test();
1381 return;
1382 }
1383 #endif
1384
1385 static void
test_free_active_base(void * ptr)1386 test_free_active_base(void *ptr)
1387 {
1388 struct basic_test_data *data = ptr;
1389 struct event_base *base1;
1390 struct event ev1;
1391
1392 base1 = event_init();
1393 tt_assert(base1);
1394 event_assign(&ev1, base1, data->pair[1], EV_READ, dummy_read_cb, NULL);
1395 event_add(&ev1, NULL);
1396 event_base_free(base1); /* should not crash */
1397
1398 base1 = event_init();
1399 tt_assert(base1);
1400 event_assign(&ev1, base1, data->pair[0], 0, dummy_read_cb, NULL);
1401 event_active(&ev1, EV_READ, 1);
1402 event_base_free(base1);
1403 end:
1404 ;
1405 }
1406
1407 static void
test_manipulate_active_events(void * ptr)1408 test_manipulate_active_events(void *ptr)
1409 {
1410 struct basic_test_data *data = ptr;
1411 struct event_base *base = data->base;
1412 struct event ev1;
1413
1414 event_assign(&ev1, base, -1, EV_TIMEOUT, dummy_read_cb, NULL);
1415
1416 /* Make sure an active event is pending. */
1417 event_active(&ev1, EV_READ, 1);
1418 tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL),
1419 ==, EV_READ);
1420
1421 /* Make sure that activating an event twice works. */
1422 event_active(&ev1, EV_WRITE, 1);
1423 tt_int_op(event_pending(&ev1, EV_READ|EV_TIMEOUT|EV_WRITE, NULL),
1424 ==, EV_READ|EV_WRITE);
1425
1426 end:
1427 event_del(&ev1);
1428 }
1429
1430 static void
event_selfarg_cb(evutil_socket_t fd,short event,void * arg)1431 event_selfarg_cb(evutil_socket_t fd, short event, void *arg)
1432 {
1433 struct event *ev = arg;
1434 struct event_base *base = event_get_base(ev);
1435 event_base_assert_ok_(base);
1436 event_base_loopexit(base, NULL);
1437 tt_want(ev == event_base_get_running_event(base));
1438 }
1439
1440 static void
test_event_new_selfarg(void * ptr)1441 test_event_new_selfarg(void *ptr)
1442 {
1443 struct basic_test_data *data = ptr;
1444 struct event_base *base = data->base;
1445 struct event *ev = event_new(base, -1, EV_READ, event_selfarg_cb,
1446 event_self_cbarg());
1447
1448 event_active(ev, EV_READ, 1);
1449 event_base_dispatch(base);
1450
1451 event_free(ev);
1452 }
1453
1454 static void
test_event_assign_selfarg(void * ptr)1455 test_event_assign_selfarg(void *ptr)
1456 {
1457 struct basic_test_data *data = ptr;
1458 struct event_base *base = data->base;
1459 struct event ev;
1460
1461 event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1462 event_self_cbarg());
1463 event_active(&ev, EV_READ, 1);
1464 event_base_dispatch(base);
1465 }
1466
1467 static void
test_event_base_get_num_events(void * ptr)1468 test_event_base_get_num_events(void *ptr)
1469 {
1470 struct basic_test_data *data = ptr;
1471 struct event_base *base = data->base;
1472 struct event ev;
1473 int event_count_active;
1474 int event_count_virtual;
1475 int event_count_added;
1476 int event_count_active_virtual;
1477 int event_count_active_added;
1478 int event_count_virtual_added;
1479 int event_count_active_added_virtual;
1480
1481 struct timeval qsec = {0, 100000};
1482
1483 event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1484 event_self_cbarg());
1485
1486 event_add(&ev, &qsec);
1487 event_count_active = event_base_get_num_events(base,
1488 EVENT_BASE_COUNT_ACTIVE);
1489 event_count_virtual = event_base_get_num_events(base,
1490 EVENT_BASE_COUNT_VIRTUAL);
1491 event_count_added = event_base_get_num_events(base,
1492 EVENT_BASE_COUNT_ADDED);
1493 event_count_active_virtual = event_base_get_num_events(base,
1494 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1495 event_count_active_added = event_base_get_num_events(base,
1496 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1497 event_count_virtual_added = event_base_get_num_events(base,
1498 EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1499 event_count_active_added_virtual = event_base_get_num_events(base,
1500 EVENT_BASE_COUNT_ACTIVE|
1501 EVENT_BASE_COUNT_ADDED|
1502 EVENT_BASE_COUNT_VIRTUAL);
1503 tt_int_op(event_count_active, ==, 0);
1504 tt_int_op(event_count_virtual, ==, 0);
1505 /* libevent itself adds a timeout event, so the event_count is 2 here */
1506 tt_int_op(event_count_added, ==, 2);
1507 tt_int_op(event_count_active_virtual, ==, 0);
1508 tt_int_op(event_count_active_added, ==, 2);
1509 tt_int_op(event_count_virtual_added, ==, 2);
1510 tt_int_op(event_count_active_added_virtual, ==, 2);
1511
1512 event_active(&ev, EV_READ, 1);
1513 event_count_active = event_base_get_num_events(base,
1514 EVENT_BASE_COUNT_ACTIVE);
1515 event_count_virtual = event_base_get_num_events(base,
1516 EVENT_BASE_COUNT_VIRTUAL);
1517 event_count_added = event_base_get_num_events(base,
1518 EVENT_BASE_COUNT_ADDED);
1519 event_count_active_virtual = event_base_get_num_events(base,
1520 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1521 event_count_active_added = event_base_get_num_events(base,
1522 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1523 event_count_virtual_added = event_base_get_num_events(base,
1524 EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1525 event_count_active_added_virtual = event_base_get_num_events(base,
1526 EVENT_BASE_COUNT_ACTIVE|
1527 EVENT_BASE_COUNT_ADDED|
1528 EVENT_BASE_COUNT_VIRTUAL);
1529 tt_int_op(event_count_active, ==, 1);
1530 tt_int_op(event_count_virtual, ==, 0);
1531 tt_int_op(event_count_added, ==, 3);
1532 tt_int_op(event_count_active_virtual, ==, 1);
1533 tt_int_op(event_count_active_added, ==, 4);
1534 tt_int_op(event_count_virtual_added, ==, 3);
1535 tt_int_op(event_count_active_added_virtual, ==, 4);
1536
1537 event_base_loop(base, 0);
1538 event_count_active = event_base_get_num_events(base,
1539 EVENT_BASE_COUNT_ACTIVE);
1540 event_count_virtual = event_base_get_num_events(base,
1541 EVENT_BASE_COUNT_VIRTUAL);
1542 event_count_added = event_base_get_num_events(base,
1543 EVENT_BASE_COUNT_ADDED);
1544 event_count_active_virtual = event_base_get_num_events(base,
1545 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1546 event_count_active_added = event_base_get_num_events(base,
1547 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1548 event_count_virtual_added = event_base_get_num_events(base,
1549 EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1550 event_count_active_added_virtual = event_base_get_num_events(base,
1551 EVENT_BASE_COUNT_ACTIVE|
1552 EVENT_BASE_COUNT_ADDED|
1553 EVENT_BASE_COUNT_VIRTUAL);
1554 tt_int_op(event_count_active, ==, 0);
1555 tt_int_op(event_count_virtual, ==, 0);
1556 tt_int_op(event_count_added, ==, 0);
1557 tt_int_op(event_count_active_virtual, ==, 0);
1558 tt_int_op(event_count_active_added, ==, 0);
1559 tt_int_op(event_count_virtual_added, ==, 0);
1560 tt_int_op(event_count_active_added_virtual, ==, 0);
1561
1562 event_base_add_virtual_(base);
1563 event_count_active = event_base_get_num_events(base,
1564 EVENT_BASE_COUNT_ACTIVE);
1565 event_count_virtual = event_base_get_num_events(base,
1566 EVENT_BASE_COUNT_VIRTUAL);
1567 event_count_added = event_base_get_num_events(base,
1568 EVENT_BASE_COUNT_ADDED);
1569 event_count_active_virtual = event_base_get_num_events(base,
1570 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
1571 event_count_active_added = event_base_get_num_events(base,
1572 EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
1573 event_count_virtual_added = event_base_get_num_events(base,
1574 EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
1575 event_count_active_added_virtual = event_base_get_num_events(base,
1576 EVENT_BASE_COUNT_ACTIVE|
1577 EVENT_BASE_COUNT_ADDED|
1578 EVENT_BASE_COUNT_VIRTUAL);
1579 tt_int_op(event_count_active, ==, 0);
1580 tt_int_op(event_count_virtual, ==, 1);
1581 tt_int_op(event_count_added, ==, 0);
1582 tt_int_op(event_count_active_virtual, ==, 1);
1583 tt_int_op(event_count_active_added, ==, 0);
1584 tt_int_op(event_count_virtual_added, ==, 1);
1585 tt_int_op(event_count_active_added_virtual, ==, 1);
1586
1587 end:
1588 ;
1589 }
1590
1591 static void
test_event_base_get_max_events(void * ptr)1592 test_event_base_get_max_events(void *ptr)
1593 {
1594 struct basic_test_data *data = ptr;
1595 struct event_base *base = data->base;
1596 struct event ev;
1597 struct event ev2;
1598 int event_count_active;
1599 int event_count_virtual;
1600 int event_count_added;
1601 int event_count_active_virtual;
1602 int event_count_active_added;
1603 int event_count_virtual_added;
1604 int event_count_active_added_virtual;
1605
1606 struct timeval qsec = {0, 100000};
1607
1608 event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
1609 event_self_cbarg());
1610 event_assign(&ev2, base, -1, EV_READ, event_selfarg_cb,
1611 event_self_cbarg());
1612
1613 event_add(&ev, &qsec);
1614 event_add(&ev2, &qsec);
1615 event_del(&ev2);
1616
1617 event_count_active = event_base_get_max_events(base,
1618 EVENT_BASE_COUNT_ACTIVE, 0);
1619 event_count_virtual = event_base_get_max_events(base,
1620 EVENT_BASE_COUNT_VIRTUAL, 0);
1621 event_count_added = event_base_get_max_events(base,
1622 EVENT_BASE_COUNT_ADDED, 0);
1623 event_count_active_virtual = event_base_get_max_events(base,
1624 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1625 event_count_active_added = event_base_get_max_events(base,
1626 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1627 event_count_virtual_added = event_base_get_max_events(base,
1628 EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1629 event_count_active_added_virtual = event_base_get_max_events(base,
1630 EVENT_BASE_COUNT_ACTIVE |
1631 EVENT_BASE_COUNT_ADDED |
1632 EVENT_BASE_COUNT_VIRTUAL, 0);
1633
1634 tt_int_op(event_count_active, ==, 0);
1635 tt_int_op(event_count_virtual, ==, 0);
1636 /* libevent itself adds a timeout event, so the event_count is 4 here */
1637 tt_int_op(event_count_added, ==, 4);
1638 tt_int_op(event_count_active_virtual, ==, 0);
1639 tt_int_op(event_count_active_added, ==, 4);
1640 tt_int_op(event_count_virtual_added, ==, 4);
1641 tt_int_op(event_count_active_added_virtual, ==, 4);
1642
1643 event_active(&ev, EV_READ, 1);
1644 event_count_active = event_base_get_max_events(base,
1645 EVENT_BASE_COUNT_ACTIVE, 0);
1646 event_count_virtual = event_base_get_max_events(base,
1647 EVENT_BASE_COUNT_VIRTUAL, 0);
1648 event_count_added = event_base_get_max_events(base,
1649 EVENT_BASE_COUNT_ADDED, 0);
1650 event_count_active_virtual = event_base_get_max_events(base,
1651 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1652 event_count_active_added = event_base_get_max_events(base,
1653 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1654 event_count_virtual_added = event_base_get_max_events(base,
1655 EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1656 event_count_active_added_virtual = event_base_get_max_events(base,
1657 EVENT_BASE_COUNT_ACTIVE |
1658 EVENT_BASE_COUNT_ADDED |
1659 EVENT_BASE_COUNT_VIRTUAL, 0);
1660
1661 tt_int_op(event_count_active, ==, 1);
1662 tt_int_op(event_count_virtual, ==, 0);
1663 tt_int_op(event_count_added, ==, 4);
1664 tt_int_op(event_count_active_virtual, ==, 1);
1665 tt_int_op(event_count_active_added, ==, 5);
1666 tt_int_op(event_count_virtual_added, ==, 4);
1667 tt_int_op(event_count_active_added_virtual, ==, 5);
1668
1669 event_base_loop(base, 0);
1670 event_count_active = event_base_get_max_events(base,
1671 EVENT_BASE_COUNT_ACTIVE, 1);
1672 event_count_virtual = event_base_get_max_events(base,
1673 EVENT_BASE_COUNT_VIRTUAL, 1);
1674 event_count_added = event_base_get_max_events(base,
1675 EVENT_BASE_COUNT_ADDED, 1);
1676 event_count_active_virtual = event_base_get_max_events(base,
1677 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1678 event_count_active_added = event_base_get_max_events(base,
1679 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1680 event_count_virtual_added = event_base_get_max_events(base,
1681 EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1682 event_count_active_added_virtual = event_base_get_max_events(base,
1683 EVENT_BASE_COUNT_ACTIVE |
1684 EVENT_BASE_COUNT_ADDED |
1685 EVENT_BASE_COUNT_VIRTUAL, 1);
1686
1687 tt_int_op(event_count_active, ==, 1);
1688 tt_int_op(event_count_virtual, ==, 0);
1689 tt_int_op(event_count_added, ==, 4);
1690 tt_int_op(event_count_active_virtual, ==, 0);
1691 tt_int_op(event_count_active_added, ==, 0);
1692 tt_int_op(event_count_virtual_added, ==, 0);
1693 tt_int_op(event_count_active_added_virtual, ==, 0);
1694
1695 event_count_active = event_base_get_max_events(base,
1696 EVENT_BASE_COUNT_ACTIVE, 0);
1697 event_count_virtual = event_base_get_max_events(base,
1698 EVENT_BASE_COUNT_VIRTUAL, 0);
1699 event_count_added = event_base_get_max_events(base,
1700 EVENT_BASE_COUNT_ADDED, 0);
1701 tt_int_op(event_count_active, ==, 0);
1702 tt_int_op(event_count_virtual, ==, 0);
1703 tt_int_op(event_count_added, ==, 0);
1704
1705 event_base_add_virtual_(base);
1706 event_count_active = event_base_get_max_events(base,
1707 EVENT_BASE_COUNT_ACTIVE, 0);
1708 event_count_virtual = event_base_get_max_events(base,
1709 EVENT_BASE_COUNT_VIRTUAL, 0);
1710 event_count_added = event_base_get_max_events(base,
1711 EVENT_BASE_COUNT_ADDED, 0);
1712 event_count_active_virtual = event_base_get_max_events(base,
1713 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_VIRTUAL, 0);
1714 event_count_active_added = event_base_get_max_events(base,
1715 EVENT_BASE_COUNT_ACTIVE | EVENT_BASE_COUNT_ADDED, 0);
1716 event_count_virtual_added = event_base_get_max_events(base,
1717 EVENT_BASE_COUNT_VIRTUAL | EVENT_BASE_COUNT_ADDED, 0);
1718 event_count_active_added_virtual = event_base_get_max_events(base,
1719 EVENT_BASE_COUNT_ACTIVE |
1720 EVENT_BASE_COUNT_ADDED |
1721 EVENT_BASE_COUNT_VIRTUAL, 0);
1722
1723 tt_int_op(event_count_active, ==, 0);
1724 tt_int_op(event_count_virtual, ==, 1);
1725 tt_int_op(event_count_added, ==, 0);
1726 tt_int_op(event_count_active_virtual, ==, 1);
1727 tt_int_op(event_count_active_added, ==, 0);
1728 tt_int_op(event_count_virtual_added, ==, 1);
1729 tt_int_op(event_count_active_added_virtual, ==, 1);
1730
1731 end:
1732 ;
1733 }
1734
1735 static void
test_bad_assign(void * ptr)1736 test_bad_assign(void *ptr)
1737 {
1738 struct event ev;
1739 int r;
1740 /* READ|SIGNAL is not allowed */
1741 r = event_assign(&ev, NULL, -1, EV_SIGNAL|EV_READ, dummy_read_cb, NULL);
1742 tt_int_op(r,==,-1);
1743
1744 end:
1745 ;
1746 }
1747
1748 static int reentrant_cb_run = 0;
1749
1750 static void
bad_reentrant_run_loop_cb(evutil_socket_t fd,short what,void * ptr)1751 bad_reentrant_run_loop_cb(evutil_socket_t fd, short what, void *ptr)
1752 {
1753 struct event_base *base = ptr;
1754 int r;
1755 reentrant_cb_run = 1;
1756 /* This reentrant call to event_base_loop should be detected and
1757 * should fail */
1758 r = event_base_loop(base, 0);
1759 tt_int_op(r, ==, -1);
1760 end:
1761 ;
1762 }
1763
1764 static void
test_bad_reentrant(void * ptr)1765 test_bad_reentrant(void *ptr)
1766 {
1767 struct basic_test_data *data = ptr;
1768 struct event_base *base = data->base;
1769 struct event ev;
1770 int r;
1771 event_assign(&ev, base, -1,
1772 0, bad_reentrant_run_loop_cb, base);
1773
1774 event_active(&ev, EV_WRITE, 1);
1775 r = event_base_loop(base, 0);
1776 tt_int_op(r, ==, 1);
1777 tt_int_op(reentrant_cb_run, ==, 1);
1778 end:
1779 ;
1780 }
1781
1782 static int n_write_a_byte_cb=0;
1783 static int n_read_and_drain_cb=0;
1784 static int n_activate_other_event_cb=0;
1785 static void
write_a_byte_cb(evutil_socket_t fd,short what,void * arg)1786 write_a_byte_cb(evutil_socket_t fd, short what, void *arg)
1787 {
1788 char buf[] = "x";
1789 if (write(fd, buf, 1) == 1)
1790 ++n_write_a_byte_cb;
1791 }
1792 static void
read_and_drain_cb(evutil_socket_t fd,short what,void * arg)1793 read_and_drain_cb(evutil_socket_t fd, short what, void *arg)
1794 {
1795 char buf[128];
1796 int n;
1797 ++n_read_and_drain_cb;
1798 while ((n = read(fd, buf, sizeof(buf))) > 0)
1799 ;
1800 }
1801
1802 static void
activate_other_event_cb(evutil_socket_t fd,short what,void * other_)1803 activate_other_event_cb(evutil_socket_t fd, short what, void *other_)
1804 {
1805 struct event *ev_activate = other_;
1806 ++n_activate_other_event_cb;
1807 event_active_later_(ev_activate, EV_READ);
1808 }
1809
1810 static void
test_active_later(void * ptr)1811 test_active_later(void *ptr)
1812 {
1813 struct basic_test_data *data = ptr;
1814 struct event *ev1 = NULL, *ev2 = NULL;
1815 struct event ev3, ev4;
1816 struct timeval qsec = {0, 100000};
1817 ev1 = event_new(data->base, data->pair[0], EV_READ|EV_PERSIST, read_and_drain_cb, NULL);
1818 ev2 = event_new(data->base, data->pair[1], EV_WRITE|EV_PERSIST, write_a_byte_cb, NULL);
1819 event_assign(&ev3, data->base, -1, 0, activate_other_event_cb, &ev4);
1820 event_assign(&ev4, data->base, -1, 0, activate_other_event_cb, &ev3);
1821 event_add(ev1, NULL);
1822 event_add(ev2, NULL);
1823 event_active_later_(&ev3, EV_READ);
1824
1825 event_base_loopexit(data->base, &qsec);
1826
1827 event_base_loop(data->base, 0);
1828
1829 TT_BLATHER(("%d write calls, %d read calls, %d activate-other calls.",
1830 n_write_a_byte_cb, n_read_and_drain_cb, n_activate_other_event_cb));
1831 event_del(&ev3);
1832 event_del(&ev4);
1833
1834 tt_int_op(n_write_a_byte_cb, ==, n_activate_other_event_cb);
1835 tt_int_op(n_write_a_byte_cb, >, 100);
1836 tt_int_op(n_read_and_drain_cb, >, 100);
1837 tt_int_op(n_activate_other_event_cb, >, 100);
1838
1839 event_active_later_(&ev4, EV_READ);
1840 event_active(&ev4, EV_READ, 1); /* This should make the event
1841 active immediately. */
1842 tt_assert((ev4.ev_flags & EVLIST_ACTIVE) != 0);
1843 tt_assert((ev4.ev_flags & EVLIST_ACTIVE_LATER) == 0);
1844
1845 /* Now leave this one around, so that event_free sees it and removes
1846 * it. */
1847 event_active_later_(&ev3, EV_READ);
1848 event_base_assert_ok_(data->base);
1849
1850 end:
1851 if (ev1)
1852 event_free(ev1);
1853 if (ev2)
1854 event_free(ev2);
1855
1856 event_base_free(data->base);
1857 data->base = NULL;
1858 }
1859
1860
incr_arg_cb(evutil_socket_t fd,short what,void * arg)1861 static void incr_arg_cb(evutil_socket_t fd, short what, void *arg)
1862 {
1863 int *intptr = arg;
1864 (void) fd; (void) what;
1865 ++*intptr;
1866 }
remove_timers_cb(evutil_socket_t fd,short what,void * arg)1867 static void remove_timers_cb(evutil_socket_t fd, short what, void *arg)
1868 {
1869 struct event **ep = arg;
1870 (void) fd; (void) what;
1871 event_remove_timer(ep[0]);
1872 event_remove_timer(ep[1]);
1873 }
send_a_byte_cb(evutil_socket_t fd,short what,void * arg)1874 static void send_a_byte_cb(evutil_socket_t fd, short what, void *arg)
1875 {
1876 evutil_socket_t *sockp = arg;
1877 (void) fd; (void) what;
1878 if (write(*sockp, "A", 1) < 0)
1879 tt_fail_perror("write");
1880 }
1881 struct read_not_timeout_param
1882 {
1883 struct event **ev;
1884 int events;
1885 int count;
1886 };
read_not_timeout_cb(evutil_socket_t fd,short what,void * arg)1887 static void read_not_timeout_cb(evutil_socket_t fd, short what, void *arg)
1888 {
1889 struct read_not_timeout_param *rntp = arg;
1890 char c;
1891 ev_ssize_t n;
1892 (void) fd; (void) what;
1893 n = read(fd, &c, 1);
1894 tt_int_op(n, ==, 1);
1895 rntp->events |= what;
1896 ++rntp->count;
1897 if(2 == rntp->count) event_del(rntp->ev[0]);
1898 end:
1899 ;
1900 }
1901
1902 static void
test_event_remove_timeout(void * ptr)1903 test_event_remove_timeout(void *ptr)
1904 {
1905 struct basic_test_data *data = ptr;
1906 struct event_base *base = data->base;
1907 struct event *ev[5];
1908 int ev1_fired=0;
1909 struct timeval ms25 = { 0, 25*1000 },
1910 ms40 = { 0, 40*1000 },
1911 ms75 = { 0, 75*1000 },
1912 ms125 = { 0, 125*1000 };
1913 struct read_not_timeout_param rntp = { ev, 0, 0 };
1914
1915 event_base_assert_ok_(base);
1916
1917 ev[0] = event_new(base, data->pair[0], EV_READ|EV_PERSIST,
1918 read_not_timeout_cb, &rntp);
1919 ev[1] = evtimer_new(base, incr_arg_cb, &ev1_fired);
1920 ev[2] = evtimer_new(base, remove_timers_cb, ev);
1921 ev[3] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
1922 ev[4] = evtimer_new(base, send_a_byte_cb, &data->pair[1]);
1923 tt_assert(base);
1924 event_add(ev[2], &ms25); /* remove timers */
1925 event_add(ev[4], &ms40); /* write to test if timer re-activates */
1926 event_add(ev[0], &ms75); /* read */
1927 event_add(ev[1], &ms75); /* timer */
1928 event_add(ev[3], &ms125); /* timeout. */
1929 event_base_assert_ok_(base);
1930
1931 event_base_dispatch(base);
1932
1933 tt_int_op(ev1_fired, ==, 0);
1934 tt_int_op(rntp.events, ==, EV_READ);
1935
1936 event_base_assert_ok_(base);
1937 end:
1938 event_free(ev[0]);
1939 event_free(ev[1]);
1940 event_free(ev[2]);
1941 event_free(ev[3]);
1942 event_free(ev[4]);
1943 }
1944
1945 static void
test_event_base_new(void * ptr)1946 test_event_base_new(void *ptr)
1947 {
1948 struct basic_test_data *data = ptr;
1949 struct event_base *base = 0;
1950 struct event ev1;
1951 struct basic_cb_args args;
1952
1953 int towrite = (int)strlen(TEST1)+1;
1954 int len = write(data->pair[0], TEST1, towrite);
1955
1956 if (len < 0)
1957 tt_abort_perror("initial write");
1958 else if (len != towrite)
1959 tt_abort_printf(("initial write fell short (%d of %d bytes)",
1960 len, towrite));
1961
1962 if (shutdown(data->pair[0], EVUTIL_SHUT_WR))
1963 tt_abort_perror("initial write shutdown");
1964
1965 base = event_base_new();
1966 if (!base)
1967 tt_abort_msg("failed to create event base");
1968
1969 args.eb = base;
1970 args.ev = &ev1;
1971 args.callcount = 0;
1972 event_assign(&ev1, base, data->pair[1],
1973 EV_READ|EV_PERSIST, basic_read_cb, &args);
1974
1975 if (event_add(&ev1, NULL))
1976 tt_abort_perror("initial event_add");
1977
1978 if (event_base_loop(base, 0))
1979 tt_abort_msg("unsuccessful exit from event loop");
1980
1981 end:
1982 if (base)
1983 event_base_free(base);
1984 }
1985
1986 static void
test_loopexit(void)1987 test_loopexit(void)
1988 {
1989 struct timeval tv, tv_start, tv_end;
1990 struct event ev;
1991
1992 setup_test("Loop exit: ");
1993
1994 tv.tv_usec = 0;
1995 tv.tv_sec = 60*60*24;
1996 evtimer_set(&ev, timeout_cb, NULL);
1997 evtimer_add(&ev, &tv);
1998
1999 tv.tv_usec = 300*1000;
2000 tv.tv_sec = 0;
2001 event_loopexit(&tv);
2002
2003 evutil_gettimeofday(&tv_start, NULL);
2004 event_dispatch();
2005 evutil_gettimeofday(&tv_end, NULL);
2006
2007 evtimer_del(&ev);
2008
2009 tt_assert(event_base_got_exit(global_base));
2010 tt_assert(!event_base_got_break(global_base));
2011
2012 test_timeval_diff_eq(&tv_start, &tv_end, 300);
2013
2014 test_ok = 1;
2015 end:
2016 cleanup_test();
2017 }
2018
2019 static void
test_loopexit_multiple(void)2020 test_loopexit_multiple(void)
2021 {
2022 struct timeval tv, tv_start, tv_end;
2023 struct event_base *base;
2024
2025 setup_test("Loop Multiple exit: ");
2026
2027 base = event_base_new();
2028
2029 tv.tv_usec = 200*1000;
2030 tv.tv_sec = 0;
2031 event_base_loopexit(base, &tv);
2032
2033 tv.tv_usec = 0;
2034 tv.tv_sec = 3;
2035 event_base_loopexit(base, &tv);
2036
2037 evutil_gettimeofday(&tv_start, NULL);
2038 event_base_dispatch(base);
2039 evutil_gettimeofday(&tv_end, NULL);
2040
2041 tt_assert(event_base_got_exit(base));
2042 tt_assert(!event_base_got_break(base));
2043
2044 event_base_free(base);
2045
2046 test_timeval_diff_eq(&tv_start, &tv_end, 200);
2047
2048 test_ok = 1;
2049
2050 end:
2051 cleanup_test();
2052 }
2053
2054 static void
break_cb(evutil_socket_t fd,short events,void * arg)2055 break_cb(evutil_socket_t fd, short events, void *arg)
2056 {
2057 test_ok = 1;
2058 event_loopbreak();
2059 }
2060
2061 static void
fail_cb(evutil_socket_t fd,short events,void * arg)2062 fail_cb(evutil_socket_t fd, short events, void *arg)
2063 {
2064 test_ok = 0;
2065 }
2066
2067 static void
test_loopbreak(void)2068 test_loopbreak(void)
2069 {
2070 struct event ev1, ev2;
2071 struct timeval tv;
2072
2073 setup_test("Loop break: ");
2074
2075 tv.tv_sec = 0;
2076 tv.tv_usec = 0;
2077 evtimer_set(&ev1, break_cb, NULL);
2078 evtimer_add(&ev1, &tv);
2079 evtimer_set(&ev2, fail_cb, NULL);
2080 evtimer_add(&ev2, &tv);
2081
2082 event_dispatch();
2083
2084 tt_assert(!event_base_got_exit(global_base));
2085 tt_assert(event_base_got_break(global_base));
2086
2087 evtimer_del(&ev1);
2088 evtimer_del(&ev2);
2089
2090 end:
2091 cleanup_test();
2092 }
2093
2094 static struct event *readd_test_event_last_added = NULL;
2095 static void
re_add_read_cb(evutil_socket_t fd,short event,void * arg)2096 re_add_read_cb(evutil_socket_t fd, short event, void *arg)
2097 {
2098 char buf[256];
2099 struct event *ev_other = arg;
2100 ev_ssize_t n_read;
2101
2102 readd_test_event_last_added = ev_other;
2103
2104 n_read = read(fd, buf, sizeof(buf));
2105
2106 if (n_read < 0) {
2107 tt_fail_perror("read");
2108 event_base_loopbreak(event_get_base(ev_other));
2109 } else {
2110 event_add(ev_other, NULL);
2111 ++test_ok;
2112 }
2113 }
2114 static void
test_nonpersist_readd(void * _data)2115 test_nonpersist_readd(void *_data)
2116 {
2117 struct event ev1, ev2;
2118 struct basic_test_data *data = _data;
2119
2120 memset(&ev1, 0, sizeof(ev1));
2121 memset(&ev2, 0, sizeof(ev2));
2122
2123 tt_assert(!event_assign(&ev1, data->base, data->pair[0], EV_READ, re_add_read_cb, &ev2));
2124 tt_assert(!event_assign(&ev2, data->base, data->pair[1], EV_READ, re_add_read_cb, &ev1));
2125
2126 tt_int_op(write(data->pair[0], "Hello", 5), ==, 5);
2127 tt_int_op(write(data->pair[1], "Hello", 5), ==, 5);
2128
2129 tt_int_op(event_add(&ev1, NULL), ==, 0);
2130 tt_int_op(event_add(&ev2, NULL), ==, 0);
2131 tt_int_op(event_base_loop(data->base, EVLOOP_ONCE), ==, 0);
2132 tt_int_op(test_ok, ==, 2);
2133
2134 /* At this point, we executed both callbacks. Whichever one got
2135 * called first added the second, but the second then immediately got
2136 * deleted before its callback was called. At this point, though, it
2137 * re-added the first.
2138 */
2139 tt_assert(readd_test_event_last_added);
2140 if (readd_test_event_last_added == &ev1) {
2141 tt_assert(event_pending(&ev1, EV_READ, NULL) && !event_pending(&ev2, EV_READ, NULL));
2142 } else {
2143 tt_assert(event_pending(&ev2, EV_READ, NULL) && !event_pending(&ev1, EV_READ, NULL));
2144 }
2145
2146 end:
2147 if (event_initialized(&ev1))
2148 event_del(&ev1);
2149 if (event_initialized(&ev2))
2150 event_del(&ev2);
2151 }
2152
2153 struct test_pri_event {
2154 struct event ev;
2155 int count;
2156 };
2157
2158 static void
test_priorities_cb(evutil_socket_t fd,short what,void * arg)2159 test_priorities_cb(evutil_socket_t fd, short what, void *arg)
2160 {
2161 struct test_pri_event *pri = arg;
2162 struct timeval tv;
2163
2164 if (pri->count == 3) {
2165 event_loopexit(NULL);
2166 return;
2167 }
2168
2169 pri->count++;
2170
2171 evutil_timerclear(&tv);
2172 event_add(&pri->ev, &tv);
2173 }
2174
2175 static void
test_priorities_impl(int npriorities)2176 test_priorities_impl(int npriorities)
2177 {
2178 struct test_pri_event one, two;
2179 struct timeval tv;
2180
2181 TT_BLATHER(("Testing Priorities %d: ", npriorities));
2182
2183 event_base_priority_init(global_base, npriorities);
2184
2185 memset(&one, 0, sizeof(one));
2186 memset(&two, 0, sizeof(two));
2187
2188 timeout_set(&one.ev, test_priorities_cb, &one);
2189 if (event_priority_set(&one.ev, 0) == -1) {
2190 fprintf(stderr, "%s: failed to set priority", __func__);
2191 exit(1);
2192 }
2193
2194 timeout_set(&two.ev, test_priorities_cb, &two);
2195 if (event_priority_set(&two.ev, npriorities - 1) == -1) {
2196 fprintf(stderr, "%s: failed to set priority", __func__);
2197 exit(1);
2198 }
2199
2200 evutil_timerclear(&tv);
2201
2202 if (event_add(&one.ev, &tv) == -1)
2203 exit(1);
2204 if (event_add(&two.ev, &tv) == -1)
2205 exit(1);
2206
2207 event_dispatch();
2208
2209 event_del(&one.ev);
2210 event_del(&two.ev);
2211
2212 if (npriorities == 1) {
2213 if (one.count == 3 && two.count == 3)
2214 test_ok = 1;
2215 } else if (npriorities == 2) {
2216 /* Two is called once because event_loopexit is priority 1 */
2217 if (one.count == 3 && two.count == 1)
2218 test_ok = 1;
2219 } else {
2220 if (one.count == 3 && two.count == 0)
2221 test_ok = 1;
2222 }
2223 }
2224
2225 static void
test_priorities(void)2226 test_priorities(void)
2227 {
2228 test_priorities_impl(1);
2229 if (test_ok)
2230 test_priorities_impl(2);
2231 if (test_ok)
2232 test_priorities_impl(3);
2233 }
2234
2235 /* priority-active-inversion: activate a higher-priority event, and make sure
2236 * it keeps us from running a lower-priority event first. */
2237 static int n_pai_calls = 0;
2238 static struct event pai_events[3];
2239
2240 static void
prio_active_inversion_cb(evutil_socket_t fd,short what,void * arg)2241 prio_active_inversion_cb(evutil_socket_t fd, short what, void *arg)
2242 {
2243 int *call_order = arg;
2244 *call_order = n_pai_calls++;
2245 if (n_pai_calls == 1) {
2246 /* This should activate later, even though it shares a
2247 priority with us. */
2248 event_active(&pai_events[1], EV_READ, 1);
2249 /* This should activate next, since its priority is higher,
2250 even though we activated it second. */
2251 event_active(&pai_events[2], EV_TIMEOUT, 1);
2252 }
2253 }
2254
2255 static void
test_priority_active_inversion(void * data_)2256 test_priority_active_inversion(void *data_)
2257 {
2258 struct basic_test_data *data = data_;
2259 struct event_base *base = data->base;
2260 int call_order[3];
2261 int i;
2262 tt_int_op(event_base_priority_init(base, 8), ==, 0);
2263
2264 n_pai_calls = 0;
2265 memset(call_order, 0, sizeof(call_order));
2266
2267 for (i=0;i<3;++i) {
2268 event_assign(&pai_events[i], data->base, -1, 0,
2269 prio_active_inversion_cb, &call_order[i]);
2270 }
2271
2272 event_priority_set(&pai_events[0], 4);
2273 event_priority_set(&pai_events[1], 4);
2274 event_priority_set(&pai_events[2], 0);
2275
2276 event_active(&pai_events[0], EV_WRITE, 1);
2277
2278 event_base_dispatch(base);
2279 tt_int_op(n_pai_calls, ==, 3);
2280 tt_int_op(call_order[0], ==, 0);
2281 tt_int_op(call_order[1], ==, 2);
2282 tt_int_op(call_order[2], ==, 1);
2283 end:
2284 ;
2285 }
2286
2287
2288 static void
test_multiple_cb(evutil_socket_t fd,short event,void * arg)2289 test_multiple_cb(evutil_socket_t fd, short event, void *arg)
2290 {
2291 if (event & EV_READ)
2292 test_ok |= 1;
2293 else if (event & EV_WRITE)
2294 test_ok |= 2;
2295 }
2296
2297 static void
test_multiple_events_for_same_fd(void)2298 test_multiple_events_for_same_fd(void)
2299 {
2300 struct event e1, e2;
2301
2302 setup_test("Multiple events for same fd: ");
2303
2304 event_set(&e1, pair[0], EV_READ, test_multiple_cb, NULL);
2305 event_add(&e1, NULL);
2306 event_set(&e2, pair[0], EV_WRITE, test_multiple_cb, NULL);
2307 event_add(&e2, NULL);
2308 event_loop(EVLOOP_ONCE);
2309 event_del(&e2);
2310
2311 if (write(pair[1], TEST1, strlen(TEST1)+1) < 0) {
2312 tt_fail_perror("write");
2313 }
2314
2315 event_loop(EVLOOP_ONCE);
2316 event_del(&e1);
2317
2318 if (test_ok != 3)
2319 test_ok = 0;
2320
2321 cleanup_test();
2322 }
2323
2324 int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
2325 int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf);
2326 int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t number);
2327 int evtag_decode_tag(ev_uint32_t *pnumber, struct evbuffer *evbuf);
2328
2329 static void
read_once_cb(evutil_socket_t fd,short event,void * arg)2330 read_once_cb(evutil_socket_t fd, short event, void *arg)
2331 {
2332 char buf[256];
2333 int len;
2334
2335 len = read(fd, buf, sizeof(buf));
2336
2337 if (called) {
2338 test_ok = 0;
2339 } else if (len) {
2340 /* Assumes global pair[0] can be used for writing */
2341 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
2342 tt_fail_perror("write");
2343 test_ok = 0;
2344 } else {
2345 test_ok = 1;
2346 }
2347 }
2348
2349 called++;
2350 }
2351
2352 static void
test_want_only_once(void)2353 test_want_only_once(void)
2354 {
2355 struct event ev;
2356 struct timeval tv;
2357
2358 /* Very simple read test */
2359 setup_test("Want read only once: ");
2360
2361 if (write(pair[0], TEST1, strlen(TEST1)+1) < 0) {
2362 tt_fail_perror("write");
2363 }
2364
2365 /* Setup the loop termination */
2366 evutil_timerclear(&tv);
2367 tv.tv_usec = 300*1000;
2368 event_loopexit(&tv);
2369
2370 event_set(&ev, pair[1], EV_READ, read_once_cb, &ev);
2371 if (event_add(&ev, NULL) == -1)
2372 exit(1);
2373 event_dispatch();
2374
2375 cleanup_test();
2376 }
2377
2378 #define TEST_MAX_INT 6
2379
2380 static void
evtag_int_test(void * ptr)2381 evtag_int_test(void *ptr)
2382 {
2383 struct evbuffer *tmp = evbuffer_new();
2384 ev_uint32_t integers[TEST_MAX_INT] = {
2385 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
2386 };
2387 ev_uint32_t integer;
2388 ev_uint64_t big_int;
2389 int i;
2390
2391 evtag_init();
2392
2393 for (i = 0; i < TEST_MAX_INT; i++) {
2394 int oldlen, newlen;
2395 oldlen = (int)EVBUFFER_LENGTH(tmp);
2396 evtag_encode_int(tmp, integers[i]);
2397 newlen = (int)EVBUFFER_LENGTH(tmp);
2398 TT_BLATHER(("encoded 0x%08x with %d bytes",
2399 (unsigned)integers[i], newlen - oldlen));
2400 big_int = integers[i];
2401 big_int *= 1000000000; /* 1 billion */
2402 evtag_encode_int64(tmp, big_int);
2403 }
2404
2405 for (i = 0; i < TEST_MAX_INT; i++) {
2406 tt_int_op(evtag_decode_int(&integer, tmp), !=, -1);
2407 tt_uint_op(integer, ==, integers[i]);
2408 tt_int_op(evtag_decode_int64(&big_int, tmp), !=, -1);
2409 tt_assert((big_int / 1000000000) == integers[i]);
2410 }
2411
2412 tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0);
2413 end:
2414 evbuffer_free(tmp);
2415 }
2416
2417 static void
evtag_fuzz(void * ptr)2418 evtag_fuzz(void *ptr)
2419 {
2420 unsigned char buffer[4096];
2421 struct evbuffer *tmp = evbuffer_new();
2422 struct timeval tv;
2423 int i, j;
2424
2425 int not_failed = 0;
2426
2427 evtag_init();
2428
2429 for (j = 0; j < 100; j++) {
2430 for (i = 0; i < (int)sizeof(buffer); i++)
2431 buffer[i] = test_weakrand();
2432 evbuffer_drain(tmp, -1);
2433 evbuffer_add(tmp, buffer, sizeof(buffer));
2434
2435 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1)
2436 not_failed++;
2437 }
2438
2439 /* The majority of decodes should fail */
2440 tt_int_op(not_failed, <, 10);
2441
2442 /* Now insert some corruption into the tag length field */
2443 evbuffer_drain(tmp, -1);
2444 evutil_timerclear(&tv);
2445 tv.tv_sec = 1;
2446 evtag_marshal_timeval(tmp, 0, &tv);
2447 evbuffer_add(tmp, buffer, sizeof(buffer));
2448
2449 ((char *)EVBUFFER_DATA(tmp))[1] = '\xff';
2450 if (evtag_unmarshal_timeval(tmp, 0, &tv) != -1) {
2451 tt_abort_msg("evtag_unmarshal_timeval should have failed");
2452 }
2453
2454 end:
2455 evbuffer_free(tmp);
2456 }
2457
2458 static void
evtag_tag_encoding(void * ptr)2459 evtag_tag_encoding(void *ptr)
2460 {
2461 struct evbuffer *tmp = evbuffer_new();
2462 ev_uint32_t integers[TEST_MAX_INT] = {
2463 0xaf0, 0x1000, 0x1, 0xdeadbeef, 0x00, 0xbef000
2464 };
2465 ev_uint32_t integer;
2466 int i;
2467
2468 evtag_init();
2469
2470 for (i = 0; i < TEST_MAX_INT; i++) {
2471 int oldlen, newlen;
2472 oldlen = (int)EVBUFFER_LENGTH(tmp);
2473 evtag_encode_tag(tmp, integers[i]);
2474 newlen = (int)EVBUFFER_LENGTH(tmp);
2475 TT_BLATHER(("encoded 0x%08x with %d bytes",
2476 (unsigned)integers[i], newlen - oldlen));
2477 }
2478
2479 for (i = 0; i < TEST_MAX_INT; i++) {
2480 tt_int_op(evtag_decode_tag(&integer, tmp), !=, -1);
2481 tt_uint_op(integer, ==, integers[i]);
2482 }
2483
2484 tt_uint_op(EVBUFFER_LENGTH(tmp), ==, 0);
2485
2486 end:
2487 evbuffer_free(tmp);
2488 }
2489
2490 static void
evtag_test_peek(void * ptr)2491 evtag_test_peek(void *ptr)
2492 {
2493 struct evbuffer *tmp = evbuffer_new();
2494 ev_uint32_t u32;
2495
2496 evtag_marshal_int(tmp, 30, 0);
2497 evtag_marshal_string(tmp, 40, "Hello world");
2498
2499 tt_int_op(evtag_peek(tmp, &u32), ==, 1);
2500 tt_int_op(u32, ==, 30);
2501 tt_int_op(evtag_peek_length(tmp, &u32), ==, 0);
2502 tt_int_op(u32, ==, 1+1+1);
2503 tt_int_op(evtag_consume(tmp), ==, 0);
2504
2505 tt_int_op(evtag_peek(tmp, &u32), ==, 1);
2506 tt_int_op(u32, ==, 40);
2507 tt_int_op(evtag_peek_length(tmp, &u32), ==, 0);
2508 tt_int_op(u32, ==, 1+1+11);
2509 tt_int_op(evtag_payload_length(tmp, &u32), ==, 0);
2510 tt_int_op(u32, ==, 11);
2511
2512 end:
2513 evbuffer_free(tmp);
2514 }
2515
2516
2517 static void
test_methods(void * ptr)2518 test_methods(void *ptr)
2519 {
2520 const char **methods = event_get_supported_methods();
2521 struct event_config *cfg = NULL;
2522 struct event_base *base = NULL;
2523 const char *backend;
2524 int n_methods = 0;
2525
2526 tt_assert(methods);
2527
2528 backend = methods[0];
2529 while (*methods != NULL) {
2530 TT_BLATHER(("Support method: %s", *methods));
2531 ++methods;
2532 ++n_methods;
2533 }
2534
2535 cfg = event_config_new();
2536 assert(cfg != NULL);
2537
2538 tt_int_op(event_config_avoid_method(cfg, backend), ==, 0);
2539 event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
2540
2541 base = event_base_new_with_config(cfg);
2542 if (n_methods > 1) {
2543 tt_assert(base);
2544 tt_str_op(backend, !=, event_base_get_method(base));
2545 } else {
2546 tt_assert(base == NULL);
2547 }
2548
2549 end:
2550 if (base)
2551 event_base_free(base);
2552 if (cfg)
2553 event_config_free(cfg);
2554 }
2555
2556 static void
test_version(void * arg)2557 test_version(void *arg)
2558 {
2559 const char *vstr;
2560 ev_uint32_t vint;
2561 int major, minor, patch, n;
2562
2563 vstr = event_get_version();
2564 vint = event_get_version_number();
2565
2566 tt_assert(vstr);
2567 tt_assert(vint);
2568
2569 tt_str_op(vstr, ==, LIBEVENT_VERSION);
2570 tt_int_op(vint, ==, LIBEVENT_VERSION_NUMBER);
2571
2572 n = sscanf(vstr, "%d.%d.%d", &major, &minor, &patch);
2573 tt_assert(3 == n);
2574 tt_int_op((vint&0xffffff00), ==, ((major<<24)|(minor<<16)|(patch<<8)));
2575 end:
2576 ;
2577 }
2578
2579 static void
test_base_features(void * arg)2580 test_base_features(void *arg)
2581 {
2582 struct event_base *base = NULL;
2583 struct event_config *cfg = NULL;
2584
2585 cfg = event_config_new();
2586
2587 tt_assert(0 == event_config_require_features(cfg, EV_FEATURE_ET));
2588
2589 base = event_base_new_with_config(cfg);
2590 if (base) {
2591 tt_int_op(EV_FEATURE_ET, ==,
2592 event_base_get_features(base) & EV_FEATURE_ET);
2593 } else {
2594 base = event_base_new();
2595 tt_int_op(0, ==, event_base_get_features(base) & EV_FEATURE_ET);
2596 }
2597
2598 end:
2599 if (base)
2600 event_base_free(base);
2601 if (cfg)
2602 event_config_free(cfg);
2603 }
2604
2605 #ifdef EVENT__HAVE_SETENV
2606 #define SETENV_OK
2607 #elif !defined(EVENT__HAVE_SETENV) && defined(EVENT__HAVE_PUTENV)
setenv(const char * k,const char * v,int o_)2608 static void setenv(const char *k, const char *v, int o_)
2609 {
2610 char b[256];
2611 evutil_snprintf(b, sizeof(b), "%s=%s",k,v);
2612 putenv(b);
2613 }
2614 #define SETENV_OK
2615 #endif
2616
2617 #ifdef EVENT__HAVE_UNSETENV
2618 #define UNSETENV_OK
2619 #elif !defined(EVENT__HAVE_UNSETENV) && defined(EVENT__HAVE_PUTENV)
unsetenv(const char * k)2620 static void unsetenv(const char *k)
2621 {
2622 char b[256];
2623 evutil_snprintf(b, sizeof(b), "%s=",k);
2624 putenv(b);
2625 }
2626 #define UNSETENV_OK
2627 #endif
2628
2629 #if defined(SETENV_OK) && defined(UNSETENV_OK)
2630 static void
methodname_to_envvar(const char * mname,char * buf,size_t buflen)2631 methodname_to_envvar(const char *mname, char *buf, size_t buflen)
2632 {
2633 char *cp;
2634 evutil_snprintf(buf, buflen, "EVENT_NO%s", mname);
2635 for (cp = buf; *cp; ++cp) {
2636 *cp = EVUTIL_TOUPPER_(*cp);
2637 }
2638 }
2639 #endif
2640
2641 static void
test_base_environ(void * arg)2642 test_base_environ(void *arg)
2643 {
2644 struct event_base *base = NULL;
2645 struct event_config *cfg = NULL;
2646
2647 #if defined(SETENV_OK) && defined(UNSETENV_OK)
2648 const char **basenames;
2649 int i, n_methods=0;
2650 char varbuf[128];
2651 const char *defaultname, *ignoreenvname;
2652
2653 /* See if unsetenv works before we rely on it. */
2654 setenv("EVENT_NOWAFFLES", "1", 1);
2655 unsetenv("EVENT_NOWAFFLES");
2656 if (getenv("EVENT_NOWAFFLES") != NULL) {
2657 #ifndef EVENT__HAVE_UNSETENV
2658 TT_DECLARE("NOTE", ("Can't fake unsetenv; skipping test"));
2659 #else
2660 TT_DECLARE("NOTE", ("unsetenv doesn't work; skipping test"));
2661 #endif
2662 tt_skip();
2663 }
2664
2665 basenames = event_get_supported_methods();
2666 for (i = 0; basenames[i]; ++i) {
2667 methodname_to_envvar(basenames[i], varbuf, sizeof(varbuf));
2668 unsetenv(varbuf);
2669 ++n_methods;
2670 }
2671
2672 base = event_base_new();
2673 tt_assert(base);
2674
2675 defaultname = event_base_get_method(base);
2676 TT_BLATHER(("default is <%s>", defaultname));
2677 event_base_free(base);
2678 base = NULL;
2679
2680 /* Can we disable the method with EVENT_NOfoo ? */
2681 if (!strcmp(defaultname, "epoll (with changelist)")) {
2682 setenv("EVENT_NOEPOLL", "1", 1);
2683 ignoreenvname = "epoll";
2684 } else {
2685 methodname_to_envvar(defaultname, varbuf, sizeof(varbuf));
2686 setenv(varbuf, "1", 1);
2687 ignoreenvname = defaultname;
2688 }
2689
2690 /* Use an empty cfg rather than NULL so a failure doesn't exit() */
2691 cfg = event_config_new();
2692 base = event_base_new_with_config(cfg);
2693 event_config_free(cfg);
2694 cfg = NULL;
2695 if (n_methods == 1) {
2696 tt_assert(!base);
2697 } else {
2698 tt_assert(base);
2699 tt_str_op(defaultname, !=, event_base_get_method(base));
2700 event_base_free(base);
2701 base = NULL;
2702 }
2703
2704 /* Can we disable looking at the environment with IGNORE_ENV ? */
2705 cfg = event_config_new();
2706 event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
2707 base = event_base_new_with_config(cfg);
2708 tt_assert(base);
2709 tt_str_op(ignoreenvname, ==, event_base_get_method(base));
2710 #else
2711 tt_skip();
2712 #endif
2713
2714 end:
2715 if (base)
2716 event_base_free(base);
2717 if (cfg)
2718 event_config_free(cfg);
2719 }
2720
2721 static void
read_called_once_cb(evutil_socket_t fd,short event,void * arg)2722 read_called_once_cb(evutil_socket_t fd, short event, void *arg)
2723 {
2724 tt_int_op(event, ==, EV_READ);
2725 called += 1;
2726 end:
2727 ;
2728 }
2729
2730 static void
timeout_called_once_cb(evutil_socket_t fd,short event,void * arg)2731 timeout_called_once_cb(evutil_socket_t fd, short event, void *arg)
2732 {
2733 tt_int_op(event, ==, EV_TIMEOUT);
2734 called += 100;
2735 end:
2736 ;
2737 }
2738
2739 static void
immediate_called_twice_cb(evutil_socket_t fd,short event,void * arg)2740 immediate_called_twice_cb(evutil_socket_t fd, short event, void *arg)
2741 {
2742 tt_int_op(event, ==, EV_TIMEOUT);
2743 called += 1000;
2744 end:
2745 ;
2746 }
2747
2748 static void
test_event_once(void * ptr)2749 test_event_once(void *ptr)
2750 {
2751 struct basic_test_data *data = ptr;
2752 struct timeval tv;
2753 int r;
2754
2755 tv.tv_sec = 0;
2756 tv.tv_usec = 50*1000;
2757 called = 0;
2758 r = event_base_once(data->base, data->pair[0], EV_READ,
2759 read_called_once_cb, NULL, NULL);
2760 tt_int_op(r, ==, 0);
2761 r = event_base_once(data->base, -1, EV_TIMEOUT,
2762 timeout_called_once_cb, NULL, &tv);
2763 tt_int_op(r, ==, 0);
2764 r = event_base_once(data->base, -1, 0, NULL, NULL, NULL);
2765 tt_int_op(r, <, 0);
2766 r = event_base_once(data->base, -1, EV_TIMEOUT,
2767 immediate_called_twice_cb, NULL, NULL);
2768 tt_int_op(r, ==, 0);
2769 tv.tv_sec = 0;
2770 tv.tv_usec = 0;
2771 r = event_base_once(data->base, -1, EV_TIMEOUT,
2772 immediate_called_twice_cb, NULL, &tv);
2773 tt_int_op(r, ==, 0);
2774
2775 if (write(data->pair[1], TEST1, strlen(TEST1)+1) < 0) {
2776 tt_fail_perror("write");
2777 }
2778
2779 shutdown(data->pair[1], EVUTIL_SHUT_WR);
2780
2781 event_base_dispatch(data->base);
2782
2783 tt_int_op(called, ==, 2101);
2784 end:
2785 ;
2786 }
2787
2788 static void
test_event_once_never(void * ptr)2789 test_event_once_never(void *ptr)
2790 {
2791 struct basic_test_data *data = ptr;
2792 struct timeval tv;
2793
2794 /* Have one trigger in 10 seconds (don't worry, because) */
2795 tv.tv_sec = 10;
2796 tv.tv_usec = 0;
2797 called = 0;
2798 event_base_once(data->base, -1, EV_TIMEOUT,
2799 timeout_called_once_cb, NULL, &tv);
2800
2801 /* But shut down the base in 75 msec. */
2802 tv.tv_sec = 0;
2803 tv.tv_usec = 75*1000;
2804 event_base_loopexit(data->base, &tv);
2805
2806 event_base_dispatch(data->base);
2807
2808 tt_int_op(called, ==, 0);
2809 end:
2810 ;
2811 }
2812
2813 static void
test_event_pending(void * ptr)2814 test_event_pending(void *ptr)
2815 {
2816 struct basic_test_data *data = ptr;
2817 struct event *r=NULL, *w=NULL, *t=NULL;
2818 struct timeval tv, now, tv2;
2819
2820 tv.tv_sec = 0;
2821 tv.tv_usec = 500 * 1000;
2822 r = event_new(data->base, data->pair[0], EV_READ, simple_read_cb,
2823 NULL);
2824 w = event_new(data->base, data->pair[1], EV_WRITE, simple_write_cb,
2825 NULL);
2826 t = evtimer_new(data->base, timeout_cb, NULL);
2827
2828 tt_assert(r);
2829 tt_assert(w);
2830 tt_assert(t);
2831
2832 evutil_gettimeofday(&now, NULL);
2833 event_add(r, NULL);
2834 event_add(t, &tv);
2835
2836 tt_assert( event_pending(r, EV_READ, NULL));
2837 tt_assert(!event_pending(w, EV_WRITE, NULL));
2838 tt_assert(!event_pending(r, EV_WRITE, NULL));
2839 tt_assert( event_pending(r, EV_READ|EV_WRITE, NULL));
2840 tt_assert(!event_pending(r, EV_TIMEOUT, NULL));
2841 tt_assert( event_pending(t, EV_TIMEOUT, NULL));
2842 tt_assert( event_pending(t, EV_TIMEOUT, &tv2));
2843
2844 tt_assert(evutil_timercmp(&tv2, &now, >));
2845
2846 test_timeval_diff_eq(&now, &tv2, 500);
2847
2848 end:
2849 if (r) {
2850 event_del(r);
2851 event_free(r);
2852 }
2853 if (w) {
2854 event_del(w);
2855 event_free(w);
2856 }
2857 if (t) {
2858 event_del(t);
2859 event_free(t);
2860 }
2861 }
2862
2863 static void
dfd_cb(evutil_socket_t fd,short e,void * data)2864 dfd_cb(evutil_socket_t fd, short e, void *data)
2865 {
2866 *(int*)data = (int)e;
2867 }
2868
2869 static void
test_event_closed_fd_poll(void * arg)2870 test_event_closed_fd_poll(void *arg)
2871 {
2872 struct timeval tv;
2873 struct event *e;
2874 struct basic_test_data *data = (struct basic_test_data *)arg;
2875 int i = 0;
2876
2877 if (strcmp(event_base_get_method(data->base), "poll")) {
2878 tinytest_set_test_skipped_();
2879 return;
2880 }
2881
2882 e = event_new(data->base, data->pair[0], EV_READ, dfd_cb, &i);
2883 tt_assert(e);
2884
2885 tv.tv_sec = 0;
2886 tv.tv_usec = 500 * 1000;
2887 event_add(e, &tv);
2888 tt_assert(event_pending(e, EV_READ, NULL));
2889 close(data->pair[0]);
2890 data->pair[0] = -1; /** avoids double-close */
2891 event_base_loop(data->base, EVLOOP_ONCE);
2892 tt_int_op(i, ==, EV_READ);
2893
2894 end:
2895 if (e) {
2896 event_del(e);
2897 event_free(e);
2898 }
2899 }
2900
2901 #ifndef _WIN32
2902 /* You can't do this test on windows, since dup2 doesn't work on sockets */
2903
2904 /* Regression test for our workaround for a fun epoll/linux related bug
2905 * where fd2 = dup(fd1); add(fd2); close(fd2); dup2(fd1,fd2); add(fd2)
2906 * will get you an EEXIST */
2907 static void
test_dup_fd(void * arg)2908 test_dup_fd(void *arg)
2909 {
2910 struct basic_test_data *data = arg;
2911 struct event_base *base = data->base;
2912 struct event *ev1=NULL, *ev2=NULL;
2913 int fd, dfd=-1;
2914 int ev1_got, ev2_got;
2915
2916 tt_int_op(write(data->pair[0], "Hello world",
2917 strlen("Hello world")), >, 0);
2918 fd = data->pair[1];
2919
2920 dfd = dup(fd);
2921 tt_int_op(dfd, >=, 0);
2922
2923 ev1 = event_new(base, fd, EV_READ|EV_PERSIST, dfd_cb, &ev1_got);
2924 ev2 = event_new(base, dfd, EV_READ|EV_PERSIST, dfd_cb, &ev2_got);
2925 ev1_got = ev2_got = 0;
2926 event_add(ev1, NULL);
2927 event_add(ev2, NULL);
2928 event_base_loop(base, EVLOOP_ONCE);
2929 tt_int_op(ev1_got, ==, EV_READ);
2930 tt_int_op(ev2_got, ==, EV_READ);
2931
2932 /* Now close and delete dfd then dispatch. We need to do the
2933 * dispatch here so that when we add it later, we think there
2934 * was an intermediate delete. */
2935 close(dfd);
2936 event_del(ev2);
2937 ev1_got = ev2_got = 0;
2938 event_base_loop(base, EVLOOP_ONCE);
2939 tt_want_int_op(ev1_got, ==, EV_READ);
2940 tt_int_op(ev2_got, ==, 0);
2941
2942 /* Re-duplicate the fd. We need to get the same duplicated
2943 * value that we closed to provoke the epoll quirk. Also, we
2944 * need to change the events to write, or else the old lingering
2945 * read event will make the test pass whether the change was
2946 * successful or not. */
2947 tt_int_op(dup2(fd, dfd), ==, dfd);
2948 event_free(ev2);
2949 ev2 = event_new(base, dfd, EV_WRITE|EV_PERSIST, dfd_cb, &ev2_got);
2950 event_add(ev2, NULL);
2951 ev1_got = ev2_got = 0;
2952 event_base_loop(base, EVLOOP_ONCE);
2953 tt_want_int_op(ev1_got, ==, EV_READ);
2954 tt_int_op(ev2_got, ==, EV_WRITE);
2955
2956 end:
2957 if (ev1)
2958 event_free(ev1);
2959 if (ev2)
2960 event_free(ev2);
2961 if (dfd >= 0)
2962 close(dfd);
2963 }
2964 #endif
2965
2966 #ifdef EVENT__DISABLE_MM_REPLACEMENT
2967 static void
test_mm_functions(void * arg)2968 test_mm_functions(void *arg)
2969 {
2970 tinytest_set_test_skipped_();
2971 }
2972 #else
2973 static int
check_dummy_mem_ok(void * mem_)2974 check_dummy_mem_ok(void *mem_)
2975 {
2976 char *mem = mem_;
2977 mem -= 16;
2978 return !memcmp(mem, "{[<guardedram>]}", 16);
2979 }
2980
2981 static void *
dummy_malloc(size_t len)2982 dummy_malloc(size_t len)
2983 {
2984 char *mem = malloc(len+16);
2985 memcpy(mem, "{[<guardedram>]}", 16);
2986 return mem+16;
2987 }
2988
2989 static void *
dummy_realloc(void * mem_,size_t len)2990 dummy_realloc(void *mem_, size_t len)
2991 {
2992 char *mem = mem_;
2993 if (!mem)
2994 return dummy_malloc(len);
2995 tt_want(check_dummy_mem_ok(mem_));
2996 mem -= 16;
2997 mem = realloc(mem, len+16);
2998 return mem+16;
2999 }
3000
3001 static void
dummy_free(void * mem_)3002 dummy_free(void *mem_)
3003 {
3004 char *mem = mem_;
3005 tt_want(check_dummy_mem_ok(mem_));
3006 mem -= 16;
3007 free(mem);
3008 }
3009
3010 static void
test_mm_functions(void * arg)3011 test_mm_functions(void *arg)
3012 {
3013 struct event_base *b = NULL;
3014 struct event_config *cfg = NULL;
3015 event_set_mem_functions(dummy_malloc, dummy_realloc, dummy_free);
3016 cfg = event_config_new();
3017 event_config_avoid_method(cfg, "Nonesuch");
3018 b = event_base_new_with_config(cfg);
3019 tt_assert(b);
3020 tt_assert(check_dummy_mem_ok(b));
3021 end:
3022 if (cfg)
3023 event_config_free(cfg);
3024 if (b)
3025 event_base_free(b);
3026 }
3027 #endif
3028
3029 static void
many_event_cb(evutil_socket_t fd,short event,void * arg)3030 many_event_cb(evutil_socket_t fd, short event, void *arg)
3031 {
3032 int *calledp = arg;
3033 *calledp += 1;
3034 }
3035
3036 static void
test_many_events(void * arg)3037 test_many_events(void *arg)
3038 {
3039 /* Try 70 events that should all be ready at once. This will
3040 * exercise the "resize" code on most of the backends, and will make
3041 * sure that we can get past the 64-handle limit of some windows
3042 * functions. */
3043 #define MANY 70
3044
3045 struct basic_test_data *data = arg;
3046 struct event_base *base = data->base;
3047 int one_at_a_time = data->setup_data != NULL;
3048 evutil_socket_t sock[MANY];
3049 struct event *ev[MANY];
3050 int called[MANY];
3051 int i;
3052 int loopflags = EVLOOP_NONBLOCK, evflags=0;
3053 if (one_at_a_time) {
3054 loopflags |= EVLOOP_ONCE;
3055 evflags = EV_PERSIST;
3056 }
3057
3058 memset(sock, 0xff, sizeof(sock));
3059 memset(ev, 0, sizeof(ev));
3060 memset(called, 0, sizeof(called));
3061
3062 for (i = 0; i < MANY; ++i) {
3063 /* We need an event that will hit the backend, and that will
3064 * be ready immediately. "Send a datagram" is an easy
3065 * instance of that. */
3066 sock[i] = socket(AF_INET, SOCK_DGRAM, 0);
3067 tt_assert(sock[i] >= 0);
3068 tt_assert(!evutil_make_socket_nonblocking(sock[i]));
3069 called[i] = 0;
3070 ev[i] = event_new(base, sock[i], EV_WRITE|evflags,
3071 many_event_cb, &called[i]);
3072 event_add(ev[i], NULL);
3073 if (one_at_a_time)
3074 event_base_loop(base, EVLOOP_NONBLOCK|EVLOOP_ONCE);
3075 }
3076
3077 event_base_loop(base, loopflags);
3078
3079 for (i = 0; i < MANY; ++i) {
3080 if (one_at_a_time)
3081 tt_int_op(called[i], ==, MANY - i + 1);
3082 else
3083 tt_int_op(called[i], ==, 1);
3084 }
3085
3086 end:
3087 for (i = 0; i < MANY; ++i) {
3088 if (ev[i])
3089 event_free(ev[i]);
3090 if (sock[i] >= 0)
3091 evutil_closesocket(sock[i]);
3092 }
3093 #undef MANY
3094 }
3095
3096 static void
test_struct_event_size(void * arg)3097 test_struct_event_size(void *arg)
3098 {
3099 tt_int_op(event_get_struct_event_size(), <=, sizeof(struct event));
3100 end:
3101 ;
3102 }
3103
3104 static void
test_get_assignment(void * arg)3105 test_get_assignment(void *arg)
3106 {
3107 struct basic_test_data *data = arg;
3108 struct event_base *base = data->base;
3109 struct event *ev1 = NULL;
3110 const char *str = "foo";
3111
3112 struct event_base *b;
3113 evutil_socket_t s;
3114 short what;
3115 event_callback_fn cb;
3116 void *cb_arg;
3117
3118 ev1 = event_new(base, data->pair[1], EV_READ, dummy_read_cb, (void*)str);
3119 event_get_assignment(ev1, &b, &s, &what, &cb, &cb_arg);
3120
3121 tt_ptr_op(b, ==, base);
3122 tt_fd_op(s, ==, data->pair[1]);
3123 tt_int_op(what, ==, EV_READ);
3124 tt_ptr_op(cb, ==, dummy_read_cb);
3125 tt_ptr_op(cb_arg, ==, str);
3126
3127 /* Now make sure this doesn't crash. */
3128 event_get_assignment(ev1, NULL, NULL, NULL, NULL, NULL);
3129
3130 end:
3131 if (ev1)
3132 event_free(ev1);
3133 }
3134
3135 struct foreach_helper {
3136 int count;
3137 const struct event *ev;
3138 };
3139
3140 static int
foreach_count_cb(const struct event_base * base,const struct event * ev,void * arg)3141 foreach_count_cb(const struct event_base *base, const struct event *ev, void *arg)
3142 {
3143 struct foreach_helper *h = event_get_callback_arg(ev);
3144 struct timeval *tv = arg;
3145 if (event_get_callback(ev) != timeout_cb)
3146 return 0;
3147 tt_ptr_op(event_get_base(ev), ==, base);
3148 tt_int_op(tv->tv_sec, ==, 10);
3149 h->ev = ev;
3150 h->count++;
3151 return 0;
3152 end:
3153 return -1;
3154 }
3155
3156 static int
foreach_find_cb(const struct event_base * base,const struct event * ev,void * arg)3157 foreach_find_cb(const struct event_base *base, const struct event *ev, void *arg)
3158 {
3159 const struct event **ev_out = arg;
3160 struct foreach_helper *h = event_get_callback_arg(ev);
3161 if (event_get_callback(ev) != timeout_cb)
3162 return 0;
3163 if (h->count == 99) {
3164 *ev_out = ev;
3165 return 101;
3166 }
3167 return 0;
3168 }
3169
3170 static void
test_event_foreach(void * arg)3171 test_event_foreach(void *arg)
3172 {
3173 struct basic_test_data *data = arg;
3174 struct event_base *base = data->base;
3175 struct event *ev[5];
3176 struct foreach_helper visited[5];
3177 int i;
3178 struct timeval ten_sec = {10,0};
3179 const struct event *ev_found = NULL;
3180
3181 for (i = 0; i < 5; ++i) {
3182 visited[i].count = 0;
3183 visited[i].ev = NULL;
3184 ev[i] = event_new(base, -1, 0, timeout_cb, &visited[i]);
3185 }
3186
3187 tt_int_op(-1, ==, event_base_foreach_event(NULL, foreach_count_cb, NULL));
3188 tt_int_op(-1, ==, event_base_foreach_event(base, NULL, NULL));
3189
3190 event_add(ev[0], &ten_sec);
3191 event_add(ev[1], &ten_sec);
3192 event_active(ev[1], EV_TIMEOUT, 1);
3193 event_active(ev[2], EV_TIMEOUT, 1);
3194 event_add(ev[3], &ten_sec);
3195 /* Don't touch ev[4]. */
3196
3197 tt_int_op(0, ==, event_base_foreach_event(base, foreach_count_cb,
3198 &ten_sec));
3199 tt_int_op(1, ==, visited[0].count);
3200 tt_int_op(1, ==, visited[1].count);
3201 tt_int_op(1, ==, visited[2].count);
3202 tt_int_op(1, ==, visited[3].count);
3203 tt_ptr_op(ev[0], ==, visited[0].ev);
3204 tt_ptr_op(ev[1], ==, visited[1].ev);
3205 tt_ptr_op(ev[2], ==, visited[2].ev);
3206 tt_ptr_op(ev[3], ==, visited[3].ev);
3207
3208 visited[2].count = 99;
3209 tt_int_op(101, ==, event_base_foreach_event(base, foreach_find_cb,
3210 &ev_found));
3211 tt_ptr_op(ev_found, ==, ev[2]);
3212
3213 end:
3214 for (i=0; i<5; ++i) {
3215 event_free(ev[i]);
3216 }
3217 }
3218
3219 static struct event_base *cached_time_base = NULL;
3220 static int cached_time_reset = 0;
3221 static int cached_time_sleep = 0;
3222 static void
cache_time_cb(evutil_socket_t fd,short what,void * arg)3223 cache_time_cb(evutil_socket_t fd, short what, void *arg)
3224 {
3225 struct timeval *tv = arg;
3226 tt_int_op(0, ==, event_base_gettimeofday_cached(cached_time_base, tv));
3227 if (cached_time_sleep) {
3228 struct timeval delay = { 0, 30*1000 };
3229 evutil_usleep_(&delay);
3230 }
3231 if (cached_time_reset) {
3232 event_base_update_cache_time(cached_time_base);
3233 }
3234 end:
3235 ;
3236 }
3237
3238 static void
test_gettimeofday_cached(void * arg)3239 test_gettimeofday_cached(void *arg)
3240 {
3241 struct basic_test_data *data = arg;
3242 struct event_config *cfg = NULL;
3243 struct event_base *base = NULL;
3244 struct timeval tv1, tv2, tv3, now;
3245 struct event *ev1=NULL, *ev2=NULL, *ev3=NULL;
3246 int cached_time_disable = strstr(data->setup_data, "disable") != NULL;
3247
3248 cfg = event_config_new();
3249 if (cached_time_disable) {
3250 event_config_set_flag(cfg, EVENT_BASE_FLAG_NO_CACHE_TIME);
3251 }
3252 cached_time_base = base = event_base_new_with_config(cfg);
3253 tt_assert(base);
3254
3255 /* Try gettimeofday_cached outside of an event loop. */
3256 evutil_gettimeofday(&now, NULL);
3257 tt_int_op(0, ==, event_base_gettimeofday_cached(NULL, &tv1));
3258 tt_int_op(0, ==, event_base_gettimeofday_cached(base, &tv2));
3259 tt_int_op(timeval_msec_diff(&tv1, &tv2), <, 10);
3260 tt_int_op(timeval_msec_diff(&tv1, &now), <, 10);
3261
3262 cached_time_reset = strstr(data->setup_data, "reset") != NULL;
3263 cached_time_sleep = strstr(data->setup_data, "sleep") != NULL;
3264
3265 ev1 = event_new(base, -1, 0, cache_time_cb, &tv1);
3266 ev2 = event_new(base, -1, 0, cache_time_cb, &tv2);
3267 ev3 = event_new(base, -1, 0, cache_time_cb, &tv3);
3268
3269 event_active(ev1, EV_TIMEOUT, 1);
3270 event_active(ev2, EV_TIMEOUT, 1);
3271 event_active(ev3, EV_TIMEOUT, 1);
3272
3273 event_base_dispatch(base);
3274
3275 if (cached_time_reset && cached_time_sleep) {
3276 tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
3277 tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
3278 } else if (cached_time_disable && cached_time_sleep) {
3279 tt_int_op(labs(timeval_msec_diff(&tv1,&tv2)), >, 10);
3280 tt_int_op(labs(timeval_msec_diff(&tv2,&tv3)), >, 10);
3281 } else if (! cached_time_disable) {
3282 tt_assert(evutil_timercmp(&tv1, &tv2, ==));
3283 tt_assert(evutil_timercmp(&tv2, &tv3, ==));
3284 }
3285
3286 end:
3287 if (ev1)
3288 event_free(ev1);
3289 if (ev2)
3290 event_free(ev2);
3291 if (ev3)
3292 event_free(ev3);
3293 if (base)
3294 event_base_free(base);
3295 if (cfg)
3296 event_config_free(cfg);
3297 }
3298
3299 static void
tabf_cb(evutil_socket_t fd,short what,void * arg)3300 tabf_cb(evutil_socket_t fd, short what, void *arg)
3301 {
3302 int *ptr = arg;
3303 *ptr = what;
3304 *ptr += 0x10000;
3305 }
3306
3307 static void
test_evmap_invalid_slots(void * arg)3308 test_evmap_invalid_slots(void *arg)
3309 {
3310 struct basic_test_data *data = arg;
3311 struct event_base *base = data->base;
3312 struct event *ev1 = NULL, *ev2 = NULL;
3313 int e1, e2;
3314 #ifndef _WIN32
3315 struct event *ev3 = NULL, *ev4 = NULL;
3316 int e3, e4;
3317 #endif
3318
3319 ev1 = evsignal_new(base, -1, dummy_read_cb, (void *)base);
3320 ev2 = evsignal_new(base, NSIG, dummy_read_cb, (void *)base);
3321 tt_assert(ev1);
3322 tt_assert(ev2);
3323 e1 = event_add(ev1, NULL);
3324 e2 = event_add(ev2, NULL);
3325 tt_int_op(e1, !=, 0);
3326 tt_int_op(e2, !=, 0);
3327 #ifndef _WIN32
3328 ev3 = event_new(base, INT_MAX, EV_READ, dummy_read_cb, (void *)base);
3329 ev4 = event_new(base, INT_MAX / 2, EV_READ, dummy_read_cb, (void *)base);
3330 tt_assert(ev3);
3331 tt_assert(ev4);
3332 e3 = event_add(ev3, NULL);
3333 e4 = event_add(ev4, NULL);
3334 tt_int_op(e3, !=, 0);
3335 tt_int_op(e4, !=, 0);
3336 #endif
3337
3338 end:
3339 event_free(ev1);
3340 event_free(ev2);
3341 #ifndef _WIN32
3342 event_free(ev3);
3343 event_free(ev4);
3344 #endif
3345 }
3346
3347 static void
test_active_by_fd(void * arg)3348 test_active_by_fd(void *arg)
3349 {
3350 struct basic_test_data *data = arg;
3351 struct event_base *base = data->base;
3352 struct event *ev1 = NULL, *ev2 = NULL, *ev3 = NULL, *ev4 = NULL;
3353 int e1,e2,e3,e4;
3354 #ifndef _WIN32
3355 struct event *evsig = NULL;
3356 int es;
3357 #endif
3358 struct timeval tenmin = { 600, 0 };
3359
3360 /* Ensure no crash on nonexistent FD. */
3361 event_base_active_by_fd(base, 1000, EV_READ);
3362
3363 /* Ensure no crash on bogus FD. */
3364 event_base_active_by_fd(base, -1, EV_READ);
3365
3366 /* Ensure no crash on nonexistent/bogus signal. */
3367 event_base_active_by_signal(base, 1000);
3368 event_base_active_by_signal(base, -1);
3369
3370 event_base_assert_ok_(base);
3371
3372 e1 = e2 = e3 = e4 = 0;
3373 ev1 = event_new(base, data->pair[0], EV_READ, tabf_cb, &e1);
3374 ev2 = event_new(base, data->pair[0], EV_WRITE, tabf_cb, &e2);
3375 ev3 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e3);
3376 ev4 = event_new(base, data->pair[1], EV_READ, tabf_cb, &e4);
3377 tt_assert(ev1);
3378 tt_assert(ev2);
3379 tt_assert(ev3);
3380 tt_assert(ev4);
3381 #ifndef _WIN32
3382 evsig = event_new(base, SIGHUP, EV_SIGNAL, tabf_cb, &es);
3383 tt_assert(evsig);
3384 event_add(evsig, &tenmin);
3385 #endif
3386
3387 event_add(ev1, &tenmin);
3388 event_add(ev2, NULL);
3389 event_add(ev3, NULL);
3390 event_add(ev4, &tenmin);
3391
3392
3393 event_base_assert_ok_(base);
3394
3395 /* Trigger 2, 3, 4 */
3396 event_base_active_by_fd(base, data->pair[0], EV_WRITE);
3397 event_base_active_by_fd(base, data->pair[1], EV_READ);
3398 event_base_active_by_fd(base, data->pair[1], EV_TIMEOUT);
3399 #ifndef _WIN32
3400 event_base_active_by_signal(base, SIGHUP);
3401 #endif
3402
3403 event_base_assert_ok_(base);
3404
3405 event_base_loop(base, EVLOOP_ONCE);
3406
3407 tt_int_op(e1, ==, 0);
3408 tt_int_op(e2, ==, EV_WRITE | 0x10000);
3409 tt_int_op(e3, ==, EV_READ | 0x10000);
3410 /* Mask out EV_WRITE here, since it could be genuinely writeable. */
3411 tt_int_op((e4 & ~EV_WRITE), ==, EV_READ | EV_TIMEOUT | 0x10000);
3412 #ifndef _WIN32
3413 tt_int_op(es, ==, EV_SIGNAL | 0x10000);
3414 #endif
3415
3416 end:
3417 if (ev1)
3418 event_free(ev1);
3419 if (ev2)
3420 event_free(ev2);
3421 if (ev3)
3422 event_free(ev3);
3423 if (ev4)
3424 event_free(ev4);
3425 #ifndef _WIN32
3426 if (evsig)
3427 event_free(evsig);
3428 #endif
3429 }
3430
3431 struct testcase_t main_testcases[] = {
3432 /* Some converted-over tests */
3433 { "methods", test_methods, TT_FORK, NULL, NULL },
3434 { "version", test_version, 0, NULL, NULL },
3435 BASIC(base_features, TT_FORK|TT_NO_LOGS),
3436 { "base_environ", test_base_environ, TT_FORK, NULL, NULL },
3437
3438 BASIC(event_base_new, TT_FORK|TT_NEED_SOCKETPAIR),
3439 BASIC(free_active_base, TT_FORK|TT_NEED_SOCKETPAIR),
3440
3441 BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE),
3442 BASIC(event_new_selfarg, TT_FORK|TT_NEED_BASE),
3443 BASIC(event_assign_selfarg, TT_FORK|TT_NEED_BASE),
3444 BASIC(event_base_get_num_events, TT_FORK|TT_NEED_BASE),
3445 BASIC(event_base_get_max_events, TT_FORK|TT_NEED_BASE),
3446 BASIC(evmap_invalid_slots, TT_FORK|TT_NEED_BASE),
3447
3448 BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
3449 BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
3450 BASIC(active_later, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR|TT_RETRIABLE),
3451 BASIC(event_remove_timeout, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3452
3453 /* These are still using the old API */
3454 LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE),
3455 { "persistent_timeout_jump", test_persistent_timeout_jump, TT_FORK|TT_NEED_BASE, &basic_setup, NULL },
3456 { "persistent_active_timeout", test_persistent_active_timeout,
3457 TT_FORK|TT_NEED_BASE|TT_RETRIABLE, &basic_setup, NULL },
3458 LEGACY(priorities, TT_FORK|TT_NEED_BASE),
3459 BASIC(priority_active_inversion, TT_FORK|TT_NEED_BASE),
3460 { "common_timeout", test_common_timeout, TT_FORK|TT_NEED_BASE,
3461 &basic_setup, NULL },
3462
3463 /* These legacy tests may not all need all of these flags. */
3464 LEGACY(simpleread, TT_ISOLATED),
3465 LEGACY(simpleread_multiple, TT_ISOLATED),
3466 LEGACY(simplewrite, TT_ISOLATED),
3467 { "simpleclose", test_simpleclose, TT_FORK, &basic_setup,
3468 NULL },
3469 LEGACY(multiple, TT_ISOLATED),
3470 LEGACY(persistent, TT_ISOLATED),
3471 LEGACY(combined, TT_ISOLATED),
3472 LEGACY(simpletimeout, TT_ISOLATED),
3473 LEGACY(loopbreak, TT_ISOLATED),
3474 LEGACY(loopexit, TT_ISOLATED),
3475 LEGACY(loopexit_multiple, TT_ISOLATED),
3476 { "nonpersist_readd", test_nonpersist_readd, TT_FORK|TT_NEED_SOCKETPAIR|TT_NEED_BASE, &basic_setup, NULL },
3477 LEGACY(multiple_events_for_same_fd, TT_ISOLATED),
3478 LEGACY(want_only_once, TT_ISOLATED),
3479 { "event_once", test_event_once, TT_ISOLATED, &basic_setup, NULL },
3480 { "event_once_never", test_event_once_never, TT_ISOLATED, &basic_setup, NULL },
3481 { "event_pending", test_event_pending, TT_ISOLATED, &basic_setup,
3482 NULL },
3483 { "event_closed_fd_poll", test_event_closed_fd_poll, TT_ISOLATED, &basic_setup,
3484 NULL },
3485
3486 #ifndef _WIN32
3487 { "dup_fd", test_dup_fd, TT_ISOLATED, &basic_setup, NULL },
3488 #endif
3489 { "mm_functions", test_mm_functions, TT_FORK, NULL, NULL },
3490 { "many_events", test_many_events, TT_ISOLATED, &basic_setup, NULL },
3491 { "many_events_slow_add", test_many_events, TT_ISOLATED, &basic_setup, (void*)1 },
3492
3493 { "struct_event_size", test_struct_event_size, 0, NULL, NULL },
3494 BASIC(get_assignment, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3495
3496 BASIC(event_foreach, TT_FORK|TT_NEED_BASE),
3497 { "gettimeofday_cached", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"" },
3498 { "gettimeofday_cached_sleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep" },
3499 { "gettimeofday_cached_reset", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep reset" },
3500 { "gettimeofday_cached_disabled", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"sleep disable" },
3501 { "gettimeofday_cached_disabled_nosleep", test_gettimeofday_cached, TT_FORK, &basic_setup, (void*)"disable" },
3502
3503 BASIC(active_by_fd, TT_FORK|TT_NEED_BASE|TT_NEED_SOCKETPAIR),
3504
3505 #ifndef _WIN32
3506 LEGACY(fork, TT_ISOLATED),
3507 #endif
3508 #ifdef EVENT__HAVE_PTHREADS
3509 /** TODO: support win32 */
3510 LEGACY(del_wait, TT_ISOLATED|TT_NEED_THREADS|TT_RETRIABLE),
3511 LEGACY(del_notify, TT_ISOLATED|TT_NEED_THREADS),
3512 #endif
3513
3514 END_OF_TESTCASES
3515 };
3516
3517 struct testcase_t evtag_testcases[] = {
3518 { "int", evtag_int_test, TT_FORK, NULL, NULL },
3519 { "fuzz", evtag_fuzz, TT_FORK, NULL, NULL },
3520 { "encoding", evtag_tag_encoding, TT_FORK, NULL, NULL },
3521 { "peek", evtag_test_peek, 0, NULL, NULL },
3522
3523 END_OF_TESTCASES
3524 };
3525
3526 struct testcase_t signal_testcases[] = {
3527 #ifndef _WIN32
3528 LEGACY(simplestsignal, TT_ISOLATED),
3529 LEGACY(simplesignal, TT_ISOLATED),
3530 LEGACY(multiplesignal, TT_ISOLATED),
3531 LEGACY(immediatesignal, TT_ISOLATED),
3532 LEGACY(signal_dealloc, TT_ISOLATED),
3533 LEGACY(signal_pipeloss, TT_ISOLATED),
3534 LEGACY(signal_switchbase, TT_ISOLATED|TT_NO_LOGS),
3535 LEGACY(signal_restore, TT_ISOLATED),
3536 LEGACY(signal_assert, TT_ISOLATED),
3537 LEGACY(signal_while_processing, TT_ISOLATED),
3538 #endif
3539 END_OF_TESTCASES
3540 };
3541
3542