smbd: Convert msg_file_was_renamed to synthetic_smb_fname
[nivanova/samba-autobuild/.git] / lib / tevent / testsuite.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    testing of the events subsystem
5
6    Copyright (C) Stefan Metzmacher 2006-2009
7    Copyright (C) Jeremy Allison    2013
8
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
12
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.
17
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.
22
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/>.
25 */
26
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
37
38 static int fde_count;
39
40 static void fde_handler_read(struct tevent_context *ev_ctx, struct tevent_fd *f,
41                         uint16_t flags, void *private_data)
42 {
43         int *fd = (int *)private_data;
44         char c;
45 #ifdef SA_SIGINFO
46         kill(getpid(), SIGUSR1);
47 #endif
48         kill(getpid(), SIGALRM);
49
50         read(fd[0], &c, 1);
51         fde_count++;
52 }
53
54 static void fde_handler_write(struct tevent_context *ev_ctx, struct tevent_fd *f,
55                         uint16_t flags, void *private_data)
56 {
57         int *fd = (int *)private_data;
58         char c = 0;
59         write(fd[1], &c, 1);
60 }
61
62
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)
66 {
67         int *fd = (int *)private_data;
68         char c;
69 #ifdef SA_SIGINFO
70         kill(getpid(), SIGUSR1);
71 #endif
72         kill(getpid(), SIGALRM);
73
74         read(fd[1], &c, 1);
75         fde_count++;
76 }
77
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)
81 {
82         int *fd = (int *)private_data;
83         char c = 0;
84         write(fd[0], &c, 1);
85 }
86
87 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
88                              struct timeval tval, void *private_data)
89 {
90         int *finished = (int *)private_data;
91         (*finished) = 1;
92 }
93
94 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
95                           int signum, int count, void *info, void *private_data)
96 {
97         int *countp = (int *)private_data;
98         (*countp) += count;
99 }
100
101 static bool test_event_context(struct torture_context *test,
102                                const void *test_data)
103 {
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;
123
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;
128         }
129
130         torture_comment(test, "backend '%s' - %s\n",
131                         backend, __FUNCTION__);
132
133         /* reset globals */
134         fde_count = 0;
135
136         /* create a pipe */
137         pipe(fd);
138
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);
143
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);
148
149         tevent_fd_set_auto_close(fde_read);
150         tevent_fd_set_auto_close(fde_write);
151
152         tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
153                          finished_handler, &finished);
154
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
167
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)));
174                 }
175         }
176
177         talloc_free(fde_read);
178         talloc_free(fde_write);
179         talloc_free(fde_read_1);
180         talloc_free(fde_write_1);
181
182         while (alarm_count < fde_count+1) {
183                 if (tevent_loop_once(ev_ctx) == -1) {
184                         break;
185                 }
186         }
187
188         torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
189
190 #ifdef SA_RESTART
191         talloc_free(se1);
192 #endif
193
194         torture_assert_int_equal(test, alarm_count, 1+fde_count, "alarm count mismatch");
195
196 #ifdef SA_RESETHAND
197         /*
198          * we do not call talloc_free(se2)
199          * because it is already gone,
200          * after triggering the event handler.
201          */
202 #endif
203
204 #ifdef SA_SIGINFO
205         talloc_free(se3);
206         torture_assert_int_equal(test, info_count, fde_count, "info count mismatch");
207 #endif
208
209         talloc_free(ev_ctx);
210
211         return true;
212 }
213
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;
229 };
230
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)
235 {
236         struct test_event_fd1_state *state =
237                 (struct test_event_fd1_state *)private_data;
238
239         if (state->drain_done) {
240                 state->finished = true;
241                 state->error = __location__;
242                 return;
243         }
244
245         if (state->drain) {
246                 ssize_t ret;
247                 uint8_t c = 0;
248
249                 if (!(flags & TEVENT_FD_READ)) {
250                         state->finished = true;
251                         state->error = __location__;
252                         return;
253                 }
254
255                 ret = read(state->sock[0], &c, 1);
256                 if (ret == 1) {
257                         return;
258                 }
259
260                 /*
261                  * end of test...
262                  */
263                 tevent_fd_set_flags(fde, 0);
264                 state->drain_done = true;
265                 return;
266         }
267
268         if (!state->got_write) {
269                 uint8_t c = 0;
270
271                 if (flags != TEVENT_FD_WRITE) {
272                         state->finished = true;
273                         state->error = __location__;
274                         return;
275                 }
276                 state->got_write = true;
277
278                 /*
279                  * we write to the other socket...
280                  */
281                 write(state->sock[1], &c, 1);
282                 TEVENT_FD_NOT_WRITEABLE(fde);
283                 TEVENT_FD_READABLE(fde);
284                 return;
285         }
286
287         if (!state->got_read) {
288                 if (flags != TEVENT_FD_READ) {
289                         state->finished = true;
290                         state->error = __location__;
291                         return;
292                 }
293                 state->got_read = true;
294
295                 TEVENT_FD_NOT_READABLE(fde);
296                 return;
297         }
298
299         state->finished = true;
300         state->error = __location__;
301         return;
302 }
303
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)
308 {
309         struct test_event_fd1_state *state =
310                 (struct test_event_fd1_state *)private_data;
311
312         if (state->drain_done) {
313                 state->finished = true;
314                 return;
315         }
316
317         if (!state->got_write) {
318                 state->finished = true;
319                 state->error = __location__;
320                 return;
321         }
322
323         if (!state->got_read) {
324                 state->finished = true;
325                 state->error = __location__;
326                 return;
327         }
328
329         state->loop_count++;
330         if (state->loop_count > 3) {
331                 state->finished = true;
332                 state->error = __location__;
333                 return;
334         }
335
336         state->got_write = false;
337         state->got_read = false;
338
339         tevent_fd_set_flags(state->fde0, TEVENT_FD_WRITE);
340
341         if (state->loop_count > 2) {
342                 state->drain = true;
343                 TALLOC_FREE(state->fde1);
344                 TEVENT_FD_READABLE(state->fde0);
345         }
346
347         state->te = tevent_add_timer(state->ev, state->ev,
348                                     timeval_current_ofs(0,2000),
349                                     test_event_fd1_finished, state);
350 }
351
352 static bool test_event_fd1(struct torture_context *tctx,
353                            const void *test_data)
354 {
355         struct test_event_fd1_state state;
356
357         ZERO_STRUCT(state);
358         state.tctx = tctx;
359         state.backend = (const char *)test_data;
360
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;
367         }
368
369         tevent_set_debug_stderr(state.ev);
370         torture_comment(tctx, "backend '%s' - %s\n",
371                         state.backend, __FUNCTION__);
372
373         /*
374          * This tests the following:
375          *
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.
379          *
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
393          */
394         state.sock[0] = -1;
395         state.sock[1] = -1;
396         socketpair(AF_UNIX, SOCK_STREAM, 0, state.sock);
397
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);
408
409         tevent_fd_set_auto_close(state.fde0);
410         tevent_fd_set_auto_close(state.fde1);
411
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)));
419                 }
420         }
421
422         talloc_free(state.ev);
423
424         torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
425                        "%s", state.error));
426
427         return true;
428 }
429
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;
445 };
446
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)
451 {
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;
458
459         if (cur_sock == &state->sock0) {
460                 oth_sock = &state->sock1;
461         } else {
462                 oth_sock = &state->sock0;
463         }
464
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;
470                 }
471         }
472
473         if (cur_sock->num_read == oth_sock->num_written) {
474                 state->finished = true;
475                 state->error = __location__;
476                 return;
477         }
478
479         if (!(flags & TEVENT_FD_READ)) {
480                 state->finished = true;
481                 state->error = __location__;
482                 return;
483         }
484
485         if (oth_sock->num_read >= PIPE_BUF) {
486                 /*
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.
491                  *
492                  * There should be room to write a byte again
493                  */
494                 if (!(flags & TEVENT_FD_WRITE)) {
495                         state->finished = true;
496                         state->error = __location__;
497                         return;
498                 }
499         }
500
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;
508                 }
509                 cur_sock->num_written++;
510                 if (cur_sock->num_written > 0x80000000) {
511                         state->finished = true;
512                         state->error = __location__;
513                         return;
514                 }
515                 return;
516         }
517
518         if (!cur_sock->got_full) {
519                 cur_sock->got_full = true;
520
521                 if (!oth_sock->got_full) {
522                         /*
523                          * cur_sock is full,
524                          * lets wait for oth_sock
525                          * to be filled
526                          */
527                         tevent_fd_set_flags(cur_sock->fde, 0);
528                         return;
529                 }
530
531                 /*
532                  * oth_sock waited for cur_sock,
533                  * lets restart it
534                  */
535                 tevent_fd_set_flags(oth_sock->fde,
536                                     TEVENT_FD_READ|TEVENT_FD_WRITE);
537         }
538
539         ret = read(cur_sock->fd, &v, 1);
540         if (ret != 1) {
541                 state->finished = true;
542                 state->error = __location__;
543                 return;
544         }
545         c = (uint8_t)cur_sock->num_read;
546         if (c != v) {
547                 state->finished = true;
548                 state->error = __location__;
549                 return;
550         }
551         cur_sock->num_read++;
552
553         if (cur_sock->num_read < oth_sock->num_written) {
554                 /* there is more to read */
555                 return;
556         }
557         /*
558          * we read everything, we need to remove TEVENT_FD_WRITE
559          * to avoid spinning
560          */
561         TEVENT_FD_NOT_WRITEABLE(cur_sock->fde);
562
563         if (oth_sock->num_read == cur_sock->num_written) {
564                 /*
565                  * both directions are finished
566                  */
567                 state->finished = true;
568         }
569
570         return;
571 }
572
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)
577 {
578         struct test_event_fd2_state *state =
579                 (struct test_event_fd2_state *)private_data;
580
581         /*
582          * this should never be triggered
583          */
584         state->finished = true;
585         state->error = __location__;
586 }
587
588 static bool test_event_fd2(struct torture_context *tctx,
589                            const void *test_data)
590 {
591         struct test_event_fd2_state state;
592         int sock[2];
593         uint8_t c = 0;
594
595         ZERO_STRUCT(state);
596         state.tctx = tctx;
597         state.backend = (const char *)test_data;
598
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;
605         }
606
607         tevent_set_debug_stderr(state.ev);
608         torture_comment(tctx, "backend '%s' - %s\n",
609                         state.backend, __FUNCTION__);
610
611         /*
612          * This tests the following
613          *
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.
621          */
622         sock[0] = -1;
623         sock[1] = -1;
624         socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
625
626         /*
627          * the timer should never expire
628          */
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);
646
647         tevent_fd_set_auto_close(state.sock0.fde);
648         tevent_fd_set_auto_close(state.sock1.fde);
649
650         write(state.sock0.fd, &c, 1);
651         state.sock0.num_written++;
652         write(state.sock1.fd, &c, 1);
653         state.sock1.num_written++;
654
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)));
662                 }
663         }
664
665         talloc_free(state.ev);
666
667         torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
668                        "%s", state.error));
669
670         return true;
671 }
672
673 #ifdef HAVE_PTHREAD
674
675 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
676 static bool do_shutdown = false;
677
678 static void test_event_threaded_lock(void)
679 {
680         int ret;
681         ret = pthread_mutex_lock(&threaded_mutex);
682         assert(ret == 0);
683 }
684
685 static void test_event_threaded_unlock(void)
686 {
687         int ret;
688         ret = pthread_mutex_unlock(&threaded_mutex);
689         assert(ret == 0);
690 }
691
692 static void test_event_threaded_trace(enum tevent_trace_point point,
693                                       void *private_data)
694 {
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;
705         }
706 }
707
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)
712 {
713         return;
714 }
715
716 static void *test_event_poll_thread(void *private_data)
717 {
718         struct tevent_context *ev = (struct tevent_context *)private_data;
719
720         test_event_threaded_lock();
721
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;
729                 }
730         }
731
732 }
733
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)
738 {
739         int *pfd = (int *)private_data;
740         char c;
741         ssize_t nread;
742
743         if ((flags & TEVENT_FD_READ) == 0) {
744                 return;
745         }
746
747         do {
748                 nread = read(*pfd, &c, 1);
749         } while ((nread == -1) && (errno == EINTR));
750
751         assert(nread == 1);
752 }
753
754 static bool test_event_context_threaded(struct torture_context *test,
755                                         const void *test_data)
756 {
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;
764
765         ev = tevent_context_init_byname(test, "poll_mt");
766         torture_assert(test, ev != NULL, "poll_mt not supported");
767
768         tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
769
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");
773
774         ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
775         torture_assert(test, ret == 0, "Could not create poll thread");
776
777         ret = pipe(fds);
778         torture_assert(test, ret == 0, "Could not create pipe");
779
780         poll(NULL, 0, 100);
781
782         test_event_threaded_lock();
783
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");
787
788         test_event_threaded_unlock();
789
790         poll(NULL, 0, 100);
791
792         write(fds[1], &c, 1);
793
794         poll(NULL, 0, 100);
795
796         test_event_threaded_lock();
797         do_shutdown = true;
798         test_event_threaded_unlock();
799
800         write(fds[1], &c, 1);
801
802         ret = pthread_join(poll_thread, NULL);
803         torture_assert(test, ret == 0, "pthread_join failed");
804
805         return true;
806 }
807
808 #endif
809
810 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
811 {
812         struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
813         const char **list = tevent_backend_list(suite);
814         int i;
815
816         for (i=0;list && list[i];i++) {
817                 struct torture_suite *backend_suite;
818
819                 backend_suite = torture_suite_create(mem_ctx, list[i]);
820
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]);
833
834                 torture_suite_add_suite(suite, backend_suite);
835         }
836
837 #ifdef HAVE_PTHREAD
838         torture_suite_add_simple_tcase_const(suite, "threaded_poll_mt",
839                                              test_event_context_threaded,
840                                              NULL);
841 #endif
842
843         return suite;
844 }