lib: Add interim tdb_wrap_open_ without lp_ctx
[Samba/wip.git] / lib / tevent / testsuite.c
blob7851c6c92db92e6ebf710b4f5633abf8d9be5b23
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 >= 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__;
497 return;
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);
504 if (ret != 1) {
505 state->finished = true;
506 state->error = __location__;
507 return;
509 cur_sock->num_written++;
510 if (cur_sock->num_written > 0x80000000) {
511 state->finished = true;
512 state->error = __location__;
513 return;
515 return;
518 if (!cur_sock->got_full) {
519 cur_sock->got_full = true;
521 if (!oth_sock->got_full) {
523 * cur_sock is full,
524 * lets wait for oth_sock
525 * to be filled
527 tevent_fd_set_flags(cur_sock->fde, 0);
528 return;
532 * oth_sock waited for cur_sock,
533 * lets restart it
535 tevent_fd_set_flags(oth_sock->fde,
536 TEVENT_FD_READ|TEVENT_FD_WRITE);
539 ret = read(cur_sock->fd, &v, 1);
540 if (ret != 1) {
541 state->finished = true;
542 state->error = __location__;
543 return;
545 c = (uint8_t)cur_sock->num_read;
546 if (c != v) {
547 state->finished = true;
548 state->error = __location__;
549 return;
551 cur_sock->num_read++;
553 if (cur_sock->num_read < oth_sock->num_written) {
554 /* there is more to read */
555 return;
558 * we read everything, we need to remove TEVENT_FD_WRITE
559 * to avoid spinning
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;
570 return;
573 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
574 struct tevent_timer *te,
575 struct timeval tval,
576 void *private_data)
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;
592 int sock[2];
593 uint8_t c = 0;
595 ZERO_STRUCT(state);
596 state.tctx = tctx;
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",
603 state.backend));
604 return true;
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.
622 sock[0] = -1;
623 sock[1] = -1;
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,
635 state.sock0.fd,
636 TEVENT_FD_READ | TEVENT_FD_WRITE,
637 test_event_fd2_sock_handler,
638 &state.sock0);
639 state.sock1.state = &state;
640 state.sock1.fd = sock[1];
641 state.sock1.fde = tevent_add_fd(state.ev, state.ev,
642 state.sock1.fd,
643 TEVENT_FD_READ | TEVENT_FD_WRITE,
644 test_event_fd2_sock_handler,
645 &state.sock1);
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) {
656 errno = 0;
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",
661 strerror(errno)));
665 talloc_free(state.ev);
667 torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
668 "%s", state.error));
670 return true;
673 #ifdef HAVE_PTHREAD
675 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
676 static bool do_shutdown = false;
678 static void test_event_threaded_lock(void)
680 int ret;
681 ret = pthread_mutex_lock(&threaded_mutex);
682 assert(ret == 0);
685 static void test_event_threaded_unlock(void)
687 int ret;
688 ret = pthread_mutex_unlock(&threaded_mutex);
689 assert(ret == 0);
692 static void test_event_threaded_trace(enum tevent_trace_point point,
693 void *private_data)
695 switch (point) {
696 case TEVENT_TRACE_BEFORE_WAIT:
697 test_event_threaded_unlock();
698 break;
699 case TEVENT_TRACE_AFTER_WAIT:
700 test_event_threaded_lock();
701 break;
702 case TEVENT_TRACE_BEFORE_LOOP_ONCE:
703 case TEVENT_TRACE_AFTER_LOOP_ONCE:
704 break;
708 static void test_event_threaded_timer(struct tevent_context *ev,
709 struct tevent_timer *te,
710 struct timeval current_time,
711 void *private_data)
713 return;
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();
722 while (true) {
723 int ret;
724 ret = tevent_loop_once(ev);
725 assert(ret == 0);
726 if (do_shutdown) {
727 test_event_threaded_unlock();
728 return NULL;
734 static void test_event_threaded_read_handler(struct tevent_context *ev,
735 struct tevent_fd *fde,
736 uint16_t flags,
737 void *private_data)
739 int *pfd = (int *)private_data;
740 char c;
741 ssize_t nread;
743 if ((flags & TEVENT_FD_READ) == 0) {
744 return;
747 do {
748 nread = read(*pfd, &c, 1);
749 } while ((nread == -1) && (errno == EINTR));
751 assert(nread == 1);
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;
761 int fds[2];
762 int ret;
763 char c = 0;
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");
777 ret = pipe(fds);
778 torture_assert(test, ret == 0, "Could not create pipe");
780 poll(NULL, 0, 100);
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();
790 poll(NULL, 0, 100);
792 write(fds[1], &c, 1);
794 poll(NULL, 0, 100);
796 test_event_threaded_lock();
797 do_shutdown = true;
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");
805 return true;
808 #endif
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);
814 int i;
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,
822 "context",
823 test_event_context,
824 (const void *)list[i]);
825 torture_suite_add_simple_tcase_const(backend_suite,
826 "fd1",
827 test_event_fd1,
828 (const void *)list[i]);
829 torture_suite_add_simple_tcase_const(backend_suite,
830 "fd2",
831 test_event_fd2,
832 (const void *)list[i]);
834 torture_suite_add_suite(suite, backend_suite);
837 #ifdef HAVE_PTHREAD
838 torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
839 test_event_context_threaded,
840 NULL);
841 #endif
843 return suite;