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 >= PIPE_BUF) {
487 * On Linux we become writable once we've read
488 * one byte. On Solaris we only become writable
489 * again once we've read 4096 bytes. PIPE_BUF
490 * is probably a safe bet to test against.
492 * There should be room to write a byte again
494 if (!(flags & TEVENT_FD_WRITE)) {
495 state->finished = true;
496 state->error = __location__;
501 if ((flags & TEVENT_FD_WRITE) && !cur_sock->got_full) {
502 v = (uint8_t)cur_sock->num_written;
503 ret = write(cur_sock->fd, &v, 1);
505 state->finished = true;
506 state->error = __location__;
509 cur_sock->num_written++;
510 if (cur_sock->num_written > 0x80000000) {
511 state->finished = true;
512 state->error = __location__;
518 if (!cur_sock->got_full) {
519 cur_sock->got_full = true;
521 if (!oth_sock->got_full) {
524 * lets wait for oth_sock
527 tevent_fd_set_flags(cur_sock->fde, 0);
532 * oth_sock waited for cur_sock,
535 tevent_fd_set_flags(oth_sock->fde,
536 TEVENT_FD_READ|TEVENT_FD_WRITE);
539 ret = read(cur_sock->fd, &v, 1);
541 state->finished = true;
542 state->error = __location__;
545 c = (uint8_t)cur_sock->num_read;
547 state->finished = true;
548 state->error = __location__;
551 cur_sock->num_read++;
553 if (cur_sock->num_read < oth_sock->num_written) {
554 /* there is more to read */
558 * we read everything, we need to remove TEVENT_FD_WRITE
561 TEVENT_FD_NOT_WRITEABLE(cur_sock->fde);
563 if (oth_sock->num_read == cur_sock->num_written) {
565 * both directions are finished
567 state->finished = true;
573 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
574 struct tevent_timer *te,
578 struct test_event_fd2_state *state =
579 (struct test_event_fd2_state *)private_data;
582 * this should never be triggered
584 state->finished = true;
585 state->error = __location__;
588 static bool test_event_fd2(struct torture_context *tctx,
589 const void *test_data)
591 struct test_event_fd2_state state;
597 state.backend = (const char *)test_data;
599 state.ev = tevent_context_init_byname(tctx, state.backend);
600 if (state.ev == NULL) {
601 torture_skip(tctx, talloc_asprintf(tctx,
602 "event backend '%s' not supported\n",
607 tevent_set_debug_stderr(state.ev);
608 torture_comment(tctx, "backend '%s' - %s\n",
609 state.backend, __FUNCTION__);
612 * This tests the following
614 * - We write 1 byte to each socket
615 * - We wait for TEVENT_FD_READ/WRITE on both sockets
616 * - When we get TEVENT_FD_WRITE we write 1 byte
617 * until both socket buffers are full, which
618 * means both sockets only get TEVENT_FD_READ.
619 * - Then we read 1 byte until we have consumed
620 * all bytes the other end has written.
624 socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
627 * the timer should never expire
629 state.te = tevent_add_timer(state.ev, state.ev,
630 timeval_current_ofs(600, 0),
631 test_event_fd2_finished, &state);
632 state.sock0.state = &state;
633 state.sock0.fd = sock[0];
634 state.sock0.fde = tevent_add_fd(state.ev, state.ev,
636 TEVENT_FD_READ | TEVENT_FD_WRITE,
637 test_event_fd2_sock_handler,
639 state.sock1.state = &state;
640 state.sock1.fd = sock[1];
641 state.sock1.fde = tevent_add_fd(state.ev, state.ev,
643 TEVENT_FD_READ | TEVENT_FD_WRITE,
644 test_event_fd2_sock_handler,
647 tevent_fd_set_auto_close(state.sock0.fde);
648 tevent_fd_set_auto_close(state.sock1.fde);
650 write(state.sock0.fd, &c, 1);
651 state.sock0.num_written++;
652 write(state.sock1.fd, &c, 1);
653 state.sock1.num_written++;
655 while (!state.finished) {
657 if (tevent_loop_once(state.ev) == -1) {
658 talloc_free(state.ev);
659 torture_fail(tctx, talloc_asprintf(tctx,
660 "Failed event loop %s\n",
665 talloc_free(state.ev);
667 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
675 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
676 static bool do_shutdown = false;
678 static void test_event_threaded_lock(void)
681 ret = pthread_mutex_lock(&threaded_mutex);
685 static void test_event_threaded_unlock(void)
688 ret = pthread_mutex_unlock(&threaded_mutex);
692 static void test_event_threaded_trace(enum tevent_trace_point point,
696 case TEVENT_TRACE_BEFORE_WAIT:
697 test_event_threaded_unlock();
699 case TEVENT_TRACE_AFTER_WAIT:
700 test_event_threaded_lock();
702 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
703 case TEVENT_TRACE_AFTER_LOOP_ONCE:
708 static void test_event_threaded_timer(struct tevent_context *ev,
709 struct tevent_timer *te,
710 struct timeval current_time,
716 static void *test_event_poll_thread(void *private_data)
718 struct tevent_context *ev = (struct tevent_context *)private_data;
720 test_event_threaded_lock();
724 ret = tevent_loop_once(ev);
727 test_event_threaded_unlock();
734 static void test_event_threaded_read_handler(struct tevent_context *ev,
735 struct tevent_fd *fde,
739 int *pfd = (int *)private_data;
743 if ((flags & TEVENT_FD_READ) == 0) {
748 nread = read(*pfd, &c, 1);
749 } while ((nread == -1) && (errno == EINTR));
754 static bool test_event_context_threaded(struct torture_context *test,
755 const void *test_data)
757 struct tevent_context *ev;
758 struct tevent_timer *te;
759 struct tevent_fd *fde;
760 pthread_t poll_thread;
765 ev = tevent_context_init_byname(test, "poll_mt");
766 torture_assert(test, ev != NULL, "poll_mt not supported");
768 tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
770 te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
771 test_event_threaded_timer, NULL);
772 torture_assert(test, te != NULL, "Could not add timer");
774 ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
775 torture_assert(test, ret == 0, "Could not create poll thread");
778 torture_assert(test, ret == 0, "Could not create pipe");
782 test_event_threaded_lock();
784 fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
785 test_event_threaded_read_handler, &fds[0]);
786 torture_assert(test, fde != NULL, "Could not add fd event");
788 test_event_threaded_unlock();
792 write(fds[1], &c, 1);
796 test_event_threaded_lock();
798 test_event_threaded_unlock();
800 write(fds[1], &c, 1);
802 ret = pthread_join(poll_thread, NULL);
803 torture_assert(test, ret == 0, "pthread_join failed");
810 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
812 struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
813 const char **list = tevent_backend_list(suite);
816 for (i=0;list && list[i];i++) {
817 struct torture_suite *backend_suite;
819 backend_suite = torture_suite_create(mem_ctx, list[i]);
821 torture_suite_add_simple_tcase_const(backend_suite,
824 (const void *)list[i]);
825 torture_suite_add_simple_tcase_const(backend_suite,
828 (const void *)list[i]);
829 torture_suite_add_simple_tcase_const(backend_suite,
832 (const void *)list[i]);
834 torture_suite_add_suite(suite, backend_suite);
838 torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
839 test_event_context_threaded,