2 Unix SMB/CIFS implementation.
4 testing of the events subsystem
6 Copyright (C) Stefan Metzmacher 2006-2009
7 Copyright (C) Jeremy Allison 2013
9 ** NOTE! The following LGPL license applies to the tevent
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
28 #include "lib/tevent/tevent.h"
29 #include "system/filesys.h"
30 #include "system/select.h"
31 #include "system/network.h"
32 #include "torture/torture.h"
40 static void fde_handler_read(struct tevent_context
*ev_ctx
, struct tevent_fd
*f
,
41 uint16_t flags
, void *private_data
)
43 int *fd
= (int *)private_data
;
46 kill(getpid(), SIGUSR1
);
48 kill(getpid(), SIGALRM
);
54 static void fde_handler_write(struct tevent_context
*ev_ctx
, struct tevent_fd
*f
,
55 uint16_t flags
, void *private_data
)
57 int *fd
= (int *)private_data
;
63 /* This will only fire if the fd's returned from pipe() are bi-directional. */
64 static void fde_handler_read_1(struct tevent_context
*ev_ctx
, struct tevent_fd
*f
,
65 uint16_t flags
, void *private_data
)
67 int *fd
= (int *)private_data
;
70 kill(getpid(), SIGUSR1
);
72 kill(getpid(), SIGALRM
);
78 /* This will only fire if the fd's returned from pipe() are bi-directional. */
79 static void fde_handler_write_1(struct tevent_context
*ev_ctx
, struct tevent_fd
*f
,
80 uint16_t flags
, void *private_data
)
82 int *fd
= (int *)private_data
;
87 static void finished_handler(struct tevent_context
*ev_ctx
, struct tevent_timer
*te
,
88 struct timeval tval
, void *private_data
)
90 int *finished
= (int *)private_data
;
94 static void count_handler(struct tevent_context
*ev_ctx
, struct tevent_signal
*te
,
95 int signum
, int count
, void *info
, void *private_data
)
97 int *countp
= (int *)private_data
;
101 static bool test_event_context(struct torture_context
*test
,
102 const void *test_data
)
104 struct tevent_context
*ev_ctx
;
105 int fd
[2] = { -1, -1 };
106 const char *backend
= (const char *)test_data
;
107 int alarm_count
=0, info_count
=0;
108 struct tevent_fd
*fde_read
;
109 struct tevent_fd
*fde_read_1
;
110 struct tevent_fd
*fde_write
;
111 struct tevent_fd
*fde_write_1
;
113 struct tevent_signal
*se1
= NULL
;
116 struct tevent_signal
*se2
= NULL
;
119 struct tevent_signal
*se3
= NULL
;
124 ev_ctx
= tevent_context_init_byname(test
, backend
);
125 if (ev_ctx
== NULL
) {
126 torture_comment(test
, "event backend '%s' not supported\n", backend
);
130 torture_comment(test
, "backend '%s' - %s\n",
131 backend
, __FUNCTION__
);
139 fde_read
= tevent_add_fd(ev_ctx
, ev_ctx
, fd
[0], TEVENT_FD_READ
,
140 fde_handler_read
, fd
);
141 fde_write_1
= tevent_add_fd(ev_ctx
, ev_ctx
, fd
[0], TEVENT_FD_WRITE
,
142 fde_handler_write_1
, fd
);
144 fde_write
= tevent_add_fd(ev_ctx
, ev_ctx
, fd
[1], TEVENT_FD_WRITE
,
145 fde_handler_write
, fd
);
146 fde_read_1
= tevent_add_fd(ev_ctx
, ev_ctx
, fd
[1], TEVENT_FD_READ
,
147 fde_handler_read_1
, fd
);
149 tevent_fd_set_auto_close(fde_read
);
150 tevent_fd_set_auto_close(fde_write
);
152 tevent_add_timer(ev_ctx
, ev_ctx
, timeval_current_ofs(2,0),
153 finished_handler
, &finished
);
156 se1
= tevent_add_signal(ev_ctx
, ev_ctx
, SIGALRM
, SA_RESTART
, count_handler
, &alarm_count
);
157 torture_assert(test
, se1
!= NULL
, "failed to setup se1");
160 se2
= tevent_add_signal(ev_ctx
, ev_ctx
, SIGALRM
, SA_RESETHAND
, count_handler
, &alarm_count
);
161 torture_assert(test
, se2
!= NULL
, "failed to setup se2");
164 se3
= tevent_add_signal(ev_ctx
, ev_ctx
, SIGUSR1
, SA_SIGINFO
, count_handler
, &info_count
);
165 torture_assert(test
, se3
!= NULL
, "failed to setup se3");
168 t
= timeval_current();
171 if (tevent_loop_once(ev_ctx
) == -1) {
173 torture_fail(test
, talloc_asprintf(test
, "Failed event loop %s\n", strerror(errno
)));
177 talloc_free(fde_read
);
178 talloc_free(fde_write
);
179 talloc_free(fde_read_1
);
180 talloc_free(fde_write_1
);
182 while (alarm_count
< fde_count
+1) {
183 if (tevent_loop_once(ev_ctx
) == -1) {
188 torture_comment(test
, "Got %.2f pipe events/sec\n", fde_count
/timeval_elapsed(&t
));
194 torture_assert_int_equal(test
, alarm_count
, 1+fde_count
, "alarm count mismatch");
198 * we do not call talloc_free(se2)
199 * because it is already gone,
200 * after triggering the event handler.
206 torture_assert_int_equal(test
, info_count
, fde_count
, "info count mismatch");
214 struct test_event_fd1_state
{
215 struct torture_context
*tctx
;
217 struct tevent_context
*ev
;
219 struct tevent_timer
*te
;
220 struct tevent_fd
*fde0
;
221 struct tevent_fd
*fde1
;
231 static void test_event_fd1_fde_handler(struct tevent_context
*ev_ctx
,
232 struct tevent_fd
*fde
,
236 struct test_event_fd1_state
*state
=
237 (struct test_event_fd1_state
*)private_data
;
239 if (state
->drain_done
) {
240 state
->finished
= true;
241 state
->error
= __location__
;
249 if (!(flags
& TEVENT_FD_READ
)) {
250 state
->finished
= true;
251 state
->error
= __location__
;
255 ret
= read(state
->sock
[0], &c
, 1);
263 tevent_fd_set_flags(fde
, 0);
264 state
->drain_done
= true;
268 if (!state
->got_write
) {
271 if (flags
!= TEVENT_FD_WRITE
) {
272 state
->finished
= true;
273 state
->error
= __location__
;
276 state
->got_write
= true;
279 * we write to the other socket...
281 write(state
->sock
[1], &c
, 1);
282 TEVENT_FD_NOT_WRITEABLE(fde
);
283 TEVENT_FD_READABLE(fde
);
287 if (!state
->got_read
) {
288 if (flags
!= TEVENT_FD_READ
) {
289 state
->finished
= true;
290 state
->error
= __location__
;
293 state
->got_read
= true;
295 TEVENT_FD_NOT_READABLE(fde
);
299 state
->finished
= true;
300 state
->error
= __location__
;
304 static void test_event_fd1_finished(struct tevent_context
*ev_ctx
,
305 struct tevent_timer
*te
,
309 struct test_event_fd1_state
*state
=
310 (struct test_event_fd1_state
*)private_data
;
312 if (state
->drain_done
) {
313 state
->finished
= true;
317 if (!state
->got_write
) {
318 state
->finished
= true;
319 state
->error
= __location__
;
323 if (!state
->got_read
) {
324 state
->finished
= true;
325 state
->error
= __location__
;
330 if (state
->loop_count
> 3) {
331 state
->finished
= true;
332 state
->error
= __location__
;
336 state
->got_write
= false;
337 state
->got_read
= false;
339 tevent_fd_set_flags(state
->fde0
, TEVENT_FD_WRITE
);
341 if (state
->loop_count
> 2) {
343 TALLOC_FREE(state
->fde1
);
344 TEVENT_FD_READABLE(state
->fde0
);
347 state
->te
= tevent_add_timer(state
->ev
, state
->ev
,
348 timeval_current_ofs(0,2000),
349 test_event_fd1_finished
, state
);
352 static bool test_event_fd1(struct torture_context
*tctx
,
353 const void *test_data
)
355 struct test_event_fd1_state state
;
359 state
.backend
= (const char *)test_data
;
361 state
.ev
= tevent_context_init_byname(tctx
, state
.backend
);
362 if (state
.ev
== NULL
) {
363 torture_skip(tctx
, talloc_asprintf(tctx
,
364 "event backend '%s' not supported\n",
369 tevent_set_debug_stderr(state
.ev
);
370 torture_comment(tctx
, "backend '%s' - %s\n",
371 state
.backend
, __FUNCTION__
);
374 * This tests the following:
376 * It monitors the state of state.sock[0]
377 * with tevent_fd, but we never read/write on state.sock[0]
378 * while state.sock[1] * is only used to write a few bytes.
381 * - we wait only for TEVENT_FD_WRITE on state.sock[0]
382 * - we write 1 byte to state.sock[1]
383 * - we wait only for TEVENT_FD_READ on state.sock[0]
384 * - we disable events on state.sock[0]
385 * - the timer event restarts the loop
386 * Then we close state.sock[1]
388 * - we wait for TEVENT_FD_READ/WRITE on state.sock[0]
389 * - we try to read 1 byte
390 * - if the read gets an error of returns 0
391 * we disable the event handler
392 * - the timer finishes the test
396 socketpair(AF_UNIX
, SOCK_STREAM
, 0, state
.sock
);
398 state
.te
= tevent_add_timer(state
.ev
, state
.ev
,
399 timeval_current_ofs(0,1000),
400 test_event_fd1_finished
, &state
);
401 state
.fde0
= tevent_add_fd(state
.ev
, state
.ev
,
402 state
.sock
[0], TEVENT_FD_WRITE
,
403 test_event_fd1_fde_handler
, &state
);
404 /* state.fde1 is only used to auto close */
405 state
.fde1
= tevent_add_fd(state
.ev
, state
.ev
,
407 test_event_fd1_fde_handler
, &state
);
409 tevent_fd_set_auto_close(state
.fde0
);
410 tevent_fd_set_auto_close(state
.fde1
);
412 while (!state
.finished
) {
414 if (tevent_loop_once(state
.ev
) == -1) {
415 talloc_free(state
.ev
);
416 torture_fail(tctx
, talloc_asprintf(tctx
,
417 "Failed event loop %s\n",
422 talloc_free(state
.ev
);
424 torture_assert(tctx
, state
.error
== NULL
, talloc_asprintf(tctx
,
430 struct test_event_fd2_state
{
431 struct torture_context
*tctx
;
433 struct tevent_context
*ev
;
434 struct tevent_timer
*te
;
435 struct test_event_fd2_sock
{
436 struct test_event_fd2_state
*state
;
438 struct tevent_fd
*fde
;
447 static void test_event_fd2_sock_handler(struct tevent_context
*ev_ctx
,
448 struct tevent_fd
*fde
,
452 struct test_event_fd2_sock
*cur_sock
=
453 (struct test_event_fd2_sock
*)private_data
;
454 struct test_event_fd2_state
*state
= cur_sock
->state
;
455 struct test_event_fd2_sock
*oth_sock
= NULL
;
459 if (cur_sock
== &state
->sock0
) {
460 oth_sock
= &state
->sock1
;
462 oth_sock
= &state
->sock0
;
465 if (oth_sock
->num_written
== 1) {
466 if (flags
!= (TEVENT_FD_READ
| TEVENT_FD_WRITE
)) {
467 state
->finished
= true;
468 state
->error
= __location__
;
473 if (cur_sock
->num_read
== oth_sock
->num_written
) {
474 state
->finished
= true;
475 state
->error
= __location__
;
479 if (!(flags
& TEVENT_FD_READ
)) {
480 state
->finished
= true;
481 state
->error
= __location__
;
485 if (oth_sock
->num_read
> 0) {
487 * There should be room to write a byte again
489 if (!(flags
& TEVENT_FD_WRITE
)) {
490 state
->finished
= true;
491 state
->error
= __location__
;
496 if ((flags
& TEVENT_FD_WRITE
) && !cur_sock
->got_full
) {
497 v
= (uint8_t)cur_sock
->num_written
;
498 ret
= write(cur_sock
->fd
, &v
, 1);
500 state
->finished
= true;
501 state
->error
= __location__
;
504 cur_sock
->num_written
++;
505 if (cur_sock
->num_written
> 0x80000000) {
506 state
->finished
= true;
507 state
->error
= __location__
;
513 if (!cur_sock
->got_full
) {
514 cur_sock
->got_full
= true;
516 if (!oth_sock
->got_full
) {
519 * lets wait for oth_sock
522 tevent_fd_set_flags(cur_sock
->fde
, 0);
527 * oth_sock waited for cur_sock,
530 tevent_fd_set_flags(oth_sock
->fde
,
531 TEVENT_FD_READ
|TEVENT_FD_WRITE
);
534 ret
= read(cur_sock
->fd
, &v
, 1);
536 state
->finished
= true;
537 state
->error
= __location__
;
540 c
= (uint8_t)cur_sock
->num_read
;
542 state
->finished
= true;
543 state
->error
= __location__
;
546 cur_sock
->num_read
++;
548 if (cur_sock
->num_read
< oth_sock
->num_written
) {
549 /* there is more to read */
553 * we read everything, we need to remove TEVENT_FD_WRITE
556 TEVENT_FD_NOT_WRITEABLE(cur_sock
->fde
);
558 if (oth_sock
->num_read
== cur_sock
->num_written
) {
560 * both directions are finished
562 state
->finished
= true;
568 static void test_event_fd2_finished(struct tevent_context
*ev_ctx
,
569 struct tevent_timer
*te
,
573 struct test_event_fd2_state
*state
=
574 (struct test_event_fd2_state
*)private_data
;
577 * this should never be triggered
579 state
->finished
= true;
580 state
->error
= __location__
;
583 static bool test_event_fd2(struct torture_context
*tctx
,
584 const void *test_data
)
586 struct test_event_fd2_state state
;
592 state
.backend
= (const char *)test_data
;
594 state
.ev
= tevent_context_init_byname(tctx
, state
.backend
);
595 if (state
.ev
== NULL
) {
596 torture_skip(tctx
, talloc_asprintf(tctx
,
597 "event backend '%s' not supported\n",
602 tevent_set_debug_stderr(state
.ev
);
603 torture_comment(tctx
, "backend '%s' - %s\n",
604 state
.backend
, __FUNCTION__
);
607 * This tests the following
609 * - We write 1 byte to each socket
610 * - We wait for TEVENT_FD_READ/WRITE on both sockets
611 * - When we get TEVENT_FD_WRITE we write 1 byte
612 * until both socket buffers are full, which
613 * means both sockets only get TEVENT_FD_READ.
614 * - Then we read 1 byte until we have consumed
615 * all bytes the other end has written.
619 socketpair(AF_UNIX
, SOCK_STREAM
, 0, sock
);
622 * the timer should never expire
624 state
.te
= tevent_add_timer(state
.ev
, state
.ev
,
625 timeval_current_ofs(600, 0),
626 test_event_fd2_finished
, &state
);
627 state
.sock0
.state
= &state
;
628 state
.sock0
.fd
= sock
[0];
629 state
.sock0
.fde
= tevent_add_fd(state
.ev
, state
.ev
,
631 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
632 test_event_fd2_sock_handler
,
634 state
.sock1
.state
= &state
;
635 state
.sock1
.fd
= sock
[1];
636 state
.sock1
.fde
= tevent_add_fd(state
.ev
, state
.ev
,
638 TEVENT_FD_READ
| TEVENT_FD_WRITE
,
639 test_event_fd2_sock_handler
,
642 tevent_fd_set_auto_close(state
.sock0
.fde
);
643 tevent_fd_set_auto_close(state
.sock1
.fde
);
645 write(state
.sock0
.fd
, &c
, 1);
646 state
.sock0
.num_written
++;
647 write(state
.sock1
.fd
, &c
, 1);
648 state
.sock1
.num_written
++;
650 while (!state
.finished
) {
652 if (tevent_loop_once(state
.ev
) == -1) {
653 talloc_free(state
.ev
);
654 torture_fail(tctx
, talloc_asprintf(tctx
,
655 "Failed event loop %s\n",
660 talloc_free(state
.ev
);
662 torture_assert(tctx
, state
.error
== NULL
, talloc_asprintf(tctx
,
670 static pthread_mutex_t threaded_mutex
= PTHREAD_MUTEX_INITIALIZER
;
671 static bool do_shutdown
= false;
673 static void test_event_threaded_lock(void)
676 ret
= pthread_mutex_lock(&threaded_mutex
);
680 static void test_event_threaded_unlock(void)
683 ret
= pthread_mutex_unlock(&threaded_mutex
);
687 static void test_event_threaded_trace(enum tevent_trace_point point
,
691 case TEVENT_TRACE_BEFORE_WAIT
:
692 test_event_threaded_unlock();
694 case TEVENT_TRACE_AFTER_WAIT
:
695 test_event_threaded_lock();
697 case TEVENT_TRACE_BEFORE_LOOP_ONCE
:
698 case TEVENT_TRACE_AFTER_LOOP_ONCE
:
703 static void test_event_threaded_timer(struct tevent_context
*ev
,
704 struct tevent_timer
*te
,
705 struct timeval current_time
,
711 static void *test_event_poll_thread(void *private_data
)
713 struct tevent_context
*ev
= (struct tevent_context
*)private_data
;
715 test_event_threaded_lock();
719 ret
= tevent_loop_once(ev
);
722 test_event_threaded_unlock();
729 static void test_event_threaded_read_handler(struct tevent_context
*ev
,
730 struct tevent_fd
*fde
,
734 int *pfd
= (int *)private_data
;
738 if ((flags
& TEVENT_FD_READ
) == 0) {
743 nread
= read(*pfd
, &c
, 1);
744 } while ((nread
== -1) && (errno
== EINTR
));
749 static bool test_event_context_threaded(struct torture_context
*test
,
750 const void *test_data
)
752 struct tevent_context
*ev
;
753 struct tevent_timer
*te
;
754 struct tevent_fd
*fde
;
755 pthread_t poll_thread
;
760 ev
= tevent_context_init_byname(test
, "poll_mt");
761 torture_assert(test
, ev
!= NULL
, "poll_mt not supported");
763 tevent_set_trace_callback(ev
, test_event_threaded_trace
, NULL
);
765 te
= tevent_add_timer(ev
, ev
, timeval_current_ofs(5, 0),
766 test_event_threaded_timer
, NULL
);
767 torture_assert(test
, te
!= NULL
, "Could not add timer");
769 ret
= pthread_create(&poll_thread
, NULL
, test_event_poll_thread
, ev
);
770 torture_assert(test
, ret
== 0, "Could not create poll thread");
773 torture_assert(test
, ret
== 0, "Could not create pipe");
777 test_event_threaded_lock();
779 fde
= tevent_add_fd(ev
, ev
, fds
[0], TEVENT_FD_READ
,
780 test_event_threaded_read_handler
, &fds
[0]);
781 torture_assert(test
, fde
!= NULL
, "Could not add fd event");
783 test_event_threaded_unlock();
787 write(fds
[1], &c
, 1);
791 test_event_threaded_lock();
793 test_event_threaded_unlock();
795 write(fds
[1], &c
, 1);
797 ret
= pthread_join(poll_thread
, NULL
);
798 torture_assert(test
, ret
== 0, "pthread_join failed");
805 struct torture_suite
*torture_local_event(TALLOC_CTX
*mem_ctx
)
807 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "event");
808 const char **list
= tevent_backend_list(suite
);
811 for (i
=0;list
&& list
[i
];i
++) {
812 struct torture_suite
*backend_suite
;
814 backend_suite
= torture_suite_create(mem_ctx
, list
[i
]);
816 torture_suite_add_simple_tcase_const(backend_suite
,
819 (const void *)list
[i
]);
820 torture_suite_add_simple_tcase_const(backend_suite
,
823 (const void *)list
[i
]);
824 torture_suite_add_simple_tcase_const(backend_suite
,
827 (const void *)list
[i
]);
829 torture_suite_add_suite(suite
, backend_suite
);
833 torture_suite_add_simple_tcase_const(suite
, "threaded_poll_mt",
834 test_event_context_threaded
,