Solaris/Illumos/Nexenta creates pipes that are bi-directional by default.
[Samba/gebeck_regimport.git] / lib / tevent / testsuite.c
blob1fcfa1cac3e5a8541dca73afb980281549512be1
1 /*
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
11 ** under the LGPL
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/>.
27 #include "includes.h"
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"
33 #ifdef HAVE_PTHREAD
34 #include <pthread.h>
35 #include <assert.h>
36 #endif
38 static int fde_count;
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;
44 char c;
45 #ifdef SA_SIGINFO
46 kill(getpid(), SIGUSR1);
47 #endif
48 kill(getpid(), SIGALRM);
50 read(fd[0], &c, 1);
51 fde_count++;
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;
58 char c = 0;
59 write(fd[1], &c, 1);
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;
68 char c;
69 #ifdef SA_SIGINFO
70 kill(getpid(), SIGUSR1);
71 #endif
72 kill(getpid(), SIGALRM);
74 read(fd[1], &c, 1);
75 fde_count++;
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;
83 char c = 0;
84 write(fd[0], &c, 1);
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;
91 (*finished) = 1;
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;
98 (*countp) += count;
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;
112 #ifdef SA_RESTART
113 struct tevent_signal *se1 = NULL;
114 #endif
115 #ifdef SA_RESETHAND
116 struct tevent_signal *se2 = NULL;
117 #endif
118 #ifdef SA_SIGINFO
119 struct tevent_signal *se3 = NULL;
120 #endif
121 int finished=0;
122 struct timeval t;
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);
127 return true;
130 torture_comment(test, "backend '%s' - %s\n",
131 backend, __FUNCTION__);
133 /* reset globals */
134 fde_count = 0;
136 /* create a pipe */
137 pipe(fd);
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);
155 #ifdef SA_RESTART
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");
158 #endif
159 #ifdef SA_RESETHAND
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");
162 #endif
163 #ifdef SA_SIGINFO
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");
166 #endif
168 t = timeval_current();
169 while (!finished) {
170 errno = 0;
171 if (tevent_loop_once(ev_ctx) == -1) {
172 talloc_free(ev_ctx);
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) {
184 break;
188 torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
190 #ifdef SA_RESTART
191 talloc_free(se1);
192 #endif
194 torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
196 #ifdef SA_RESETHAND
198 * we do not call talloc_free(se2)
199 * because it is already gone,
200 * after triggering the event handler.
202 #endif
204 #ifdef SA_SIGINFO
205 talloc_free(se3);
206 torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
207 #endif
209 talloc_free(ev_ctx);
211 return true;
214 struct test_event_fd1_state {
215 struct torture_context *tctx;
216 const char *backend;
217 struct tevent_context *ev;
218 int sock[2];
219 struct tevent_timer *te;
220 struct tevent_fd *fde0;
221 struct tevent_fd *fde1;
222 bool got_write;
223 bool got_read;
224 bool drain;
225 bool drain_done;
226 unsigned loop_count;
227 bool finished;
228 const char *error;
231 static void test_event_fd1_fde_handler(struct tevent_context *ev_ctx,
232 struct tevent_fd *fde,
233 uint16_t flags,
234 void *private_data)
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__;
242 return;
245 if (state->drain) {
246 ssize_t ret;
247 uint8_t c = 0;
249 if (!(flags & TEVENT_FD_READ)) {
250 state->finished = true;
251 state->error = __location__;
252 return;
255 ret = read(state->sock[0], &c, 1);
256 if (ret == 1) {
257 return;
261 * end of test...
263 tevent_fd_set_flags(fde, 0);
264 state->drain_done = true;
265 return;
268 if (!state->got_write) {
269 uint8_t c = 0;
271 if (flags != TEVENT_FD_WRITE) {
272 state->finished = true;
273 state->error = __location__;
274 return;
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);
284 return;
287 if (!state->got_read) {
288 if (flags != TEVENT_FD_READ) {
289 state->finished = true;
290 state->error = __location__;
291 return;
293 state->got_read = true;
295 TEVENT_FD_NOT_READABLE(fde);
296 return;
299 state->finished = true;
300 state->error = __location__;
301 return;
304 static void test_event_fd1_finished(struct tevent_context *ev_ctx,
305 struct tevent_timer *te,
306 struct timeval tval,
307 void *private_data)
309 struct test_event_fd1_state *state =
310 (struct test_event_fd1_state *)private_data;
312 if (state->drain_done) {
313 state->finished = true;
314 return;
317 if (!state->got_write) {
318 state->finished = true;
319 state->error = __location__;
320 return;
323 if (!state->got_read) {
324 state->finished = true;
325 state->error = __location__;
326 return;
329 state->loop_count++;
330 if (state->loop_count > 3) {
331 state->finished = true;
332 state->error = __location__;
333 return;
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) {
342 state->drain = true;
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;
357 ZERO_STRUCT(state);
358 state.tctx = tctx;
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",
365 state.backend));
366 return true;
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.
380 * We have a loop:
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]
387 * We have a loop:
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
394 state.sock[0] = -1;
395 state.sock[1] = -1;
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,
406 state.sock[1], 0,
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) {
413 errno = 0;
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",
418 strerror(errno)));
422 talloc_free(state.ev);
424 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
425 "%s", state.error));
427 return true;
430 struct test_event_fd2_state {
431 struct torture_context *tctx;
432 const char *backend;
433 struct tevent_context *ev;
434 struct tevent_timer *te;
435 struct test_event_fd2_sock {
436 struct test_event_fd2_state *state;
437 int fd;
438 struct tevent_fd *fde;
439 size_t num_written;
440 size_t num_read;
441 bool got_full;
442 } sock0, sock1;
443 bool finished;
444 const char *error;
447 static void test_event_fd2_sock_handler(struct tevent_context *ev_ctx,
448 struct tevent_fd *fde,
449 uint16_t flags,
450 void *private_data)
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;
456 uint8_t v = 0, c;
457 ssize_t ret;
459 if (cur_sock == &state->sock0) {
460 oth_sock = &state->sock1;
461 } else {
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__;
469 return;
473 if (cur_sock->num_read == oth_sock->num_written) {
474 state->finished = true;
475 state->error = __location__;
476 return;
479 if (!(flags & TEVENT_FD_READ)) {
480 state->finished = true;
481 state->error = __location__;
482 return;
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__;
492 return;
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);
499 if (ret != 1) {
500 state->finished = true;
501 state->error = __location__;
502 return;
504 cur_sock->num_written++;
505 if (cur_sock->num_written > 0x80000000) {
506 state->finished = true;
507 state->error = __location__;
508 return;
510 return;
513 if (!cur_sock->got_full) {
514 cur_sock->got_full = true;
516 if (!oth_sock->got_full) {
518 * cur_sock is full,
519 * lets wait for oth_sock
520 * to be filled
522 tevent_fd_set_flags(cur_sock->fde, 0);
523 return;
527 * oth_sock waited for cur_sock,
528 * lets restart it
530 tevent_fd_set_flags(oth_sock->fde,
531 TEVENT_FD_READ|TEVENT_FD_WRITE);
534 ret = read(cur_sock->fd, &v, 1);
535 if (ret != 1) {
536 state->finished = true;
537 state->error = __location__;
538 return;
540 c = (uint8_t)cur_sock->num_read;
541 if (c != v) {
542 state->finished = true;
543 state->error = __location__;
544 return;
546 cur_sock->num_read++;
548 if (cur_sock->num_read < oth_sock->num_written) {
549 /* there is more to read */
550 return;
553 * we read everything, we need to remove TEVENT_FD_WRITE
554 * to avoid spinning
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;
565 return;
568 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
569 struct tevent_timer *te,
570 struct timeval tval,
571 void *private_data)
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;
587 int sock[2];
588 uint8_t c = 0;
590 ZERO_STRUCT(state);
591 state.tctx = tctx;
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",
598 state.backend));
599 return true;
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.
617 sock[0] = -1;
618 sock[1] = -1;
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,
630 state.sock0.fd,
631 TEVENT_FD_READ | TEVENT_FD_WRITE,
632 test_event_fd2_sock_handler,
633 &state.sock0);
634 state.sock1.state = &state;
635 state.sock1.fd = sock[1];
636 state.sock1.fde = tevent_add_fd(state.ev, state.ev,
637 state.sock1.fd,
638 TEVENT_FD_READ | TEVENT_FD_WRITE,
639 test_event_fd2_sock_handler,
640 &state.sock1);
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) {
651 errno = 0;
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",
656 strerror(errno)));
660 talloc_free(state.ev);
662 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
663 "%s", state.error));
665 return true;
668 #ifdef HAVE_PTHREAD
670 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
671 static bool do_shutdown = false;
673 static void test_event_threaded_lock(void)
675 int ret;
676 ret = pthread_mutex_lock(&threaded_mutex);
677 assert(ret == 0);
680 static void test_event_threaded_unlock(void)
682 int ret;
683 ret = pthread_mutex_unlock(&threaded_mutex);
684 assert(ret == 0);
687 static void test_event_threaded_trace(enum tevent_trace_point point,
688 void *private_data)
690 switch (point) {
691 case TEVENT_TRACE_BEFORE_WAIT:
692 test_event_threaded_unlock();
693 break;
694 case TEVENT_TRACE_AFTER_WAIT:
695 test_event_threaded_lock();
696 break;
697 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
698 case TEVENT_TRACE_AFTER_LOOP_ONCE:
699 break;
703 static void test_event_threaded_timer(struct tevent_context *ev,
704 struct tevent_timer *te,
705 struct timeval current_time,
706 void *private_data)
708 return;
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();
717 while (true) {
718 int ret;
719 ret = tevent_loop_once(ev);
720 assert(ret == 0);
721 if (do_shutdown) {
722 test_event_threaded_unlock();
723 return NULL;
729 static void test_event_threaded_read_handler(struct tevent_context *ev,
730 struct tevent_fd *fde,
731 uint16_t flags,
732 void *private_data)
734 int *pfd = (int *)private_data;
735 char c;
736 ssize_t nread;
738 if ((flags & TEVENT_FD_READ) == 0) {
739 return;
742 do {
743 nread = read(*pfd, &c, 1);
744 } while ((nread == -1) && (errno == EINTR));
746 assert(nread == 1);
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;
756 int fds[2];
757 int ret;
758 char c = 0;
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");
772 ret = pipe(fds);
773 torture_assert(test, ret == 0, "Could not create pipe");
775 poll(NULL, 0, 100);
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();
785 poll(NULL, 0, 100);
787 write(fds[1], &c, 1);
789 poll(NULL, 0, 100);
791 test_event_threaded_lock();
792 do_shutdown = true;
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");
800 return true;
803 #endif
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);
809 int i;
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,
817 "context",
818 test_event_context,
819 (const void *)list[i]);
820 torture_suite_add_simple_tcase_const(backend_suite,
821 "fd1",
822 test_event_fd1,
823 (const void *)list[i]);
824 torture_suite_add_simple_tcase_const(backend_suite,
825 "fd2",
826 test_event_fd2,
827 (const void *)list[i]);
829 torture_suite_add_suite(suite, backend_suite);
832 #ifdef HAVE_PTHREAD
833 torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
834 test_event_context_threaded,
835 NULL);
836 #endif
838 return suite;