TODO fix commit messages: tevent: add fd2 test which fill the socket kernel buffer
[metze/samba/wip.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 static int fde_wcount;
40
41 static void fde_handler_readwrite(struct tevent_context *ev_ctx, struct tevent_fd *f,
42                         uint16_t flags, void *private_data)
43 {
44         int *fd = (int *)private_data;
45         char c = 0;
46 #ifdef SA_SIGINFO
47         kill(getpid(), SIGUSR1);
48 #endif
49         kill(getpid(), SIGALRM);
50
51         if ((flags & TEVENT_FD_WRITE) && (fde_wcount - fde_count < 256)) {
52                 /* Don't fill the pipe and block... */
53                 write(fd[1], &c, 1);
54                 fde_wcount++;
55         } else {
56                 read(fd[0], &c, 1);
57                 fde_count++;
58         }
59 }
60
61 static void finished_handler(struct tevent_context *ev_ctx, struct tevent_timer *te,
62                              struct timeval tval, void *private_data)
63 {
64         int *finished = (int *)private_data;
65         (*finished) = 1;
66 }
67
68 static void count_handler(struct tevent_context *ev_ctx, struct tevent_signal *te,
69                           int signum, int count, void *info, void *private_data)
70 {
71         int *countp = (int *)private_data;
72         (*countp) += count;
73 }
74
75 static bool test_event_context(struct torture_context *test,
76                                const void *test_data)
77 {
78         struct tevent_context *ev_ctx;
79         int fd[2] = { -1, -1 };
80         const char *backend = (const char *)test_data;
81         int alarm_count=0, info_count=0;
82         struct tevent_fd *fde_read;
83         struct tevent_fd *fde_read_1;
84         struct tevent_fd *fde_write;
85         struct tevent_fd *fde_write_1;
86 #ifdef SA_RESTART
87         struct tevent_signal *se1 = NULL;
88 #endif
89 #ifdef SA_RESETHAND
90         struct tevent_signal *se2 = NULL;
91 #endif
92 #ifdef SA_SIGINFO
93         struct tevent_signal *se3 = NULL;
94 #endif
95         int finished=0;
96         struct timeval t;
97
98         ev_ctx = tevent_context_init_byname(test, backend);
99         if (ev_ctx == NULL) {
100                 torture_comment(test, "event backend '%s' not supported\n", backend);
101                 return true;
102         }
103
104         torture_comment(test, "backend '%s' - %s\n",
105                         backend, __FUNCTION__);
106
107         /* reset globals */
108         fde_count = 0;
109         fde_wcount = 0;
110
111         /* create a pipe */
112         pipe(fd);
113
114         fde_read = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_READ,
115                             fde_handler_readwrite, fd);
116         fde_write_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[0], TEVENT_FD_WRITE,
117                             fde_handler_readwrite, fd);
118
119         fde_write = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_WRITE,
120                             fde_handler_readwrite, fd);
121         fde_read_1 = tevent_add_fd(ev_ctx, ev_ctx, fd[1], TEVENT_FD_READ,
122                             fde_handler_readwrite, fd);
123
124         tevent_fd_set_auto_close(fde_read);
125         tevent_fd_set_auto_close(fde_write);
126
127         tevent_add_timer(ev_ctx, ev_ctx, timeval_current_ofs(2,0),
128                          finished_handler, &finished);
129
130 #ifdef SA_RESTART
131         se1 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESTART, count_handler, &alarm_count);
132         torture_assert(test, se1 != NULL, "failed to setup se1");
133 #endif
134 #ifdef SA_RESETHAND
135         se2 = tevent_add_signal(ev_ctx, ev_ctx, SIGALRM, SA_RESETHAND, count_handler, &alarm_count);
136         torture_assert(test, se2 != NULL, "failed to setup se2");
137 #endif
138 #ifdef SA_SIGINFO
139         se3 = tevent_add_signal(ev_ctx, ev_ctx, SIGUSR1, SA_SIGINFO, count_handler, &info_count);
140         torture_assert(test, se3 != NULL, "failed to setup se3");
141 #endif
142
143         t = timeval_current();
144         while (!finished) {
145                 errno = 0;
146                 if (tevent_loop_once(ev_ctx) == -1) {
147                         talloc_free(ev_ctx);
148                         torture_fail(test, talloc_asprintf(test, "Failed event loop %s\n", strerror(errno)));
149                 }
150         }
151
152         talloc_free(fde_read);
153         talloc_free(fde_write);
154         talloc_free(fde_read_1);
155         talloc_free(fde_write_1);
156
157         while (alarm_count < fde_count+1) {
158                 if (tevent_loop_once(ev_ctx) == -1) {
159                         break;
160                 }
161         }
162
163         torture_comment(test, "Got %.2f pipe events/sec\n", fde_count/timeval_elapsed(&t));
164
165 #ifdef SA_RESTART
166         talloc_free(se1);
167 #endif
168
169         torture_assert_int_equal(test, alarm_count, 1+fde_count+fde_wcount, "alarm count mismatch");
170
171 #ifdef SA_RESETHAND
172         /*
173          * we do not call talloc_free(se2)
174          * because it is already gone,
175          * after triggering the event handler.
176          */
177 #endif
178
179 #ifdef SA_SIGINFO
180         talloc_free(se3);
181         torture_assert_int_equal(test, info_count, fde_count+fde_wcount, "info count mismatch");
182 #endif
183
184         talloc_free(ev_ctx);
185
186         return true;
187 }
188
189 struct test_event_fd1_state {
190         struct torture_context *tctx;
191         const char *backend;
192         struct tevent_context *ev;
193         int sock[2];
194         struct tevent_timer *te;
195         struct tevent_fd *fde0;
196         struct tevent_fd *fde1;
197         bool got_write;
198         bool got_read;
199         unsigned loop_count;
200         bool finished;
201         const char *error;
202 };
203
204 static void test_event_fd1_fde_handler(struct tevent_context *ev_ctx,
205                                        struct tevent_fd *fde,
206                                        uint16_t flags,
207                                        void *private_data)
208 {
209         struct test_event_fd1_state *state =
210                 (struct test_event_fd1_state *)private_data;
211
212         if (!state->got_write) {
213                 uint8_t c = 0;
214
215                 if (flags != TEVENT_FD_WRITE) {
216                         state->finished = true;
217                         state->error = __location__;
218                         return;
219                 }
220                 state->got_write = true;
221
222                 /*
223                  * we write to the other socket...
224                  */
225                 write(state->sock[1], &c, 1);
226                 TEVENT_FD_NOT_WRITEABLE(fde);
227                 TEVENT_FD_READABLE(fde);
228                 return;
229         }
230
231         if (!state->got_read) {
232                 if (flags != TEVENT_FD_READ) {
233                         state->finished = true;
234                         state->error = __location__;
235                         return;
236                 }
237                 state->got_read = true;
238
239                 TEVENT_FD_NOT_READABLE(fde);
240                 return;
241         }
242
243         state->finished = true;
244         state->error = __location__;
245         return;
246 }
247
248 static void test_event_fd1_finished(struct tevent_context *ev_ctx,
249                                     struct tevent_timer *te,
250                                     struct timeval tval,
251                                     void *private_data)
252 {
253         struct test_event_fd1_state *state =
254                 (struct test_event_fd1_state *)private_data;
255
256         if (!state->got_write) {
257                 state->finished = true;
258                 state->error = __location__;
259                 return;
260         }
261
262         if (!state->got_read) {
263                 state->finished = true;
264                 state->error = __location__;
265                 return;
266         }
267
268         state->loop_count++;
269         if (state->loop_count > 2) {
270                 state->finished = true;
271                 return;
272         }
273
274         state->got_write = false;
275         state->got_read = false;
276
277         tevent_fd_set_flags(state->fde0, TEVENT_FD_WRITE);
278
279         state->te = tevent_add_timer(state->ev, state->ev,
280                                     timeval_current_ofs(0,1000),
281                                     test_event_fd1_finished, state);
282 }
283
284 static bool test_event_fd1(struct torture_context *tctx,
285                            const void *test_data)
286 {
287         struct test_event_fd1_state state;
288
289         ZERO_STRUCT(state);
290         state.tctx = tctx;
291         state.backend = (const char *)test_data;
292
293         state.ev = tevent_context_init_byname(tctx, state.backend);
294         if (state.ev == NULL) {
295                 torture_skip(tctx, talloc_asprintf(tctx,
296                              "event backend '%s' not supported\n",
297                              state.backend));
298                 return true;
299         }
300
301         tevent_set_debug_stderr(state.ev);
302         torture_comment(tctx, "backend '%s' - %s\n",
303                         state.backend, __FUNCTION__);
304
305         /*
306          * This tests the following:
307          *
308          * It monitors the state of state.sock[0]
309          * with tevent_fd, but we never read/write on state.sock[0]
310          * while state.sock[1] * is only used to write a few bytes.
311          *
312          * We have a loop:
313          *   - we wait only for TEVENT_FD_WRITE on state.sock[0]
314          *   - we write 1 byte to state.sock[1]
315          *   - we wait only for TEVENT_FD_READ on state.sock[0]
316          *   - we disable events on state.sock[0]
317          *   - the timer event restarts the loop
318          */
319         state.sock[0] = -1;
320         state.sock[1] = -1;
321         socketpair(AF_UNIX, SOCK_STREAM, 0, state.sock);
322
323         state.te = tevent_add_timer(state.ev, state.ev,
324                                     timeval_current_ofs(0,1000),
325                                     test_event_fd1_finished, &state);
326         state.fde0 = tevent_add_fd(state.ev, state.ev,
327                                    state.sock[0], TEVENT_FD_WRITE,
328                                    test_event_fd1_fde_handler, &state);
329         /* state.fde1 is only used to auto close */
330         state.fde1 = tevent_add_fd(state.ev, state.ev,
331                                    state.sock[1], 0,
332                                    test_event_fd1_fde_handler, &state);
333
334         tevent_fd_set_auto_close(state.fde0);
335         tevent_fd_set_auto_close(state.fde1);
336
337         while (!state.finished) {
338                 errno = 0;
339                 if (tevent_loop_once(state.ev) == -1) {
340                         talloc_free(state.ev);
341                         torture_fail(tctx, talloc_asprintf(tctx,
342                                      "Failed event loop %s\n",
343                                      strerror(errno)));
344                 }
345         }
346
347         talloc_free(state.ev);
348
349         torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
350                        "%s", state.error));
351
352         return true;
353 }
354
355 struct test_event_fd2_state {
356         struct torture_context *tctx;
357         const char *backend;
358         struct tevent_context *ev;
359         struct tevent_timer *te;
360         struct test_event_fd2_sock {
361                 struct test_event_fd2_state *state;
362                 int fd;
363                 struct tevent_fd *fde;
364                 size_t num_written;
365                 size_t num_read;
366                 bool got_full;
367         } sock0, sock1;
368         bool finished;
369         const char *error;
370 };
371
372 static void test_event_fd2_sock_handler(struct tevent_context *ev_ctx,
373                                         struct tevent_fd *fde,
374                                         uint16_t flags,
375                                         void *private_data)
376 {
377         struct test_event_fd2_sock *cur_sock =
378                 (struct test_event_fd2_sock *)private_data;
379         struct test_event_fd2_state *state = cur_sock->state;
380         struct test_event_fd2_sock *oth_sock = NULL;
381         uint8_t v = 0, c;
382         ssize_t ret;
383
384         if (cur_sock == &state->sock0) {
385                 oth_sock = &state->sock1;
386         } else {
387                 oth_sock = &state->sock0;
388         }
389
390         if (oth_sock->num_written == 1) {
391                 if (flags != (TEVENT_FD_READ | TEVENT_FD_WRITE)) {
392                         state->finished = true;
393                         state->error = __location__;
394                         return;
395                 }
396         }
397
398         if (cur_sock->num_read == oth_sock->num_written) {
399                 state->finished = true;
400                 state->error = __location__;
401                 return;
402         }
403
404         if (!(flags & TEVENT_FD_READ)) {
405                 state->finished = true;
406                 state->error = __location__;
407                 return;
408         }
409
410         if (oth_sock->num_read > 0) {
411                 /*
412                  * There should be room to write a byte again
413                  */
414                 if (!(flags & TEVENT_FD_WRITE)) {
415                         state->finished = true;
416                         state->error = __location__;
417                         return;
418                 }
419         }
420
421         if ((flags & TEVENT_FD_WRITE) && !cur_sock->got_full) {
422                 v = (uint8_t)cur_sock->num_written;
423                 ret = write(cur_sock->fd, &v, 1);
424                 if (ret != 1) {
425                         state->finished = true;
426                         state->error = __location__;
427                         return;
428                 }
429                 cur_sock->num_written++;
430                 if (cur_sock->num_written > 0x80000000) {
431                         state->finished = true;
432                         state->error = __location__;
433                         return;
434                 }
435                 return;
436         }
437
438         if (!cur_sock->got_full) {
439                 cur_sock->got_full = true;
440
441                 if (!oth_sock->got_full) {
442                         /*
443                          * cur_sock is full,
444                          * lets wait for oth_sock
445                          * to be filled
446                          */
447                         tevent_fd_set_flags(cur_sock->fde, 0);
448                         return;
449                 }
450
451                 /*
452                  * oth_sock waited for cur_sock,
453                  * lets restart it
454                  */
455                 tevent_fd_set_flags(oth_sock->fde,
456                                     TEVENT_FD_READ|TEVENT_FD_WRITE);
457         }
458
459         ret = read(cur_sock->fd, &v, 1);
460         if (ret != 1) {
461                 state->finished = true;
462                 state->error = __location__;
463                 return;
464         }
465         c = (uint8_t)cur_sock->num_read;
466         if (c != v) {
467                 state->finished = true;
468                 state->error = __location__;
469                 return;
470         }
471         cur_sock->num_read++;
472
473         if (cur_sock->num_read < oth_sock->num_written) {
474                 /* there is more to read */
475                 return;
476         }
477         /*
478          * we read everything, we need to remove TEVENT_FD_WRITE
479          * to avoid spinning
480          */
481         TEVENT_FD_NOT_WRITEABLE(cur_sock->fde);
482
483         if (oth_sock->num_read == cur_sock->num_written) {
484                 /*
485                  * both directions are finished
486                  */
487                 state->finished = true;
488         }
489
490         return;
491 }
492
493 static void test_event_fd2_finished(struct tevent_context *ev_ctx,
494                                     struct tevent_timer *te,
495                                     struct timeval tval,
496                                     void *private_data)
497 {
498         struct test_event_fd2_state *state =
499                 (struct test_event_fd2_state *)private_data;
500
501         /*
502          * this should never be triggered
503          */
504         state->finished = true;
505         state->error = __location__;
506 }
507
508 static bool test_event_fd2(struct torture_context *tctx,
509                            const void *test_data)
510 {
511         struct test_event_fd2_state state;
512         int sock[2];
513         uint8_t c = 0;
514
515         ZERO_STRUCT(state);
516         state.tctx = tctx;
517         state.backend = (const char *)test_data;
518
519         state.ev = tevent_context_init_byname(tctx, state.backend);
520         if (state.ev == NULL) {
521                 torture_skip(tctx, talloc_asprintf(tctx,
522                              "event backend '%s' not supported\n",
523                              state.backend));
524                 return true;
525         }
526
527         tevent_set_debug_stderr(state.ev);
528         torture_comment(tctx, "backend '%s' - %s\n",
529                         state.backend, __FUNCTION__);
530
531         /*
532          * This tests the following
533          *
534          * - We write 1 byte to each socket
535          * - We wait for TEVENT_FD_READ/WRITE on both sockets
536          * - When we get TEVENT_FD_WRITE we write 1 byte
537          */
538         sock[0] = -1;
539         sock[1] = -1;
540         socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
541
542         /*
543          * the timer should never expire
544          */
545         state.te = tevent_add_timer(state.ev, state.ev,
546                                     timeval_current_ofs(600, 0),
547                                     test_event_fd2_finished, &state);
548         state.sock0.state = &state;
549         state.sock0.fd = sock[0];
550         state.sock0.fde = tevent_add_fd(state.ev, state.ev,
551                                         state.sock0.fd,
552                                         TEVENT_FD_READ | TEVENT_FD_WRITE,
553                                         test_event_fd2_sock_handler,
554                                         &state.sock0);
555         state.sock1.state = &state;
556         state.sock1.fd = sock[1];
557         state.sock1.fde = tevent_add_fd(state.ev, state.ev,
558                                         state.sock1.fd,
559                                         TEVENT_FD_READ | TEVENT_FD_WRITE,
560                                         test_event_fd2_sock_handler,
561                                         &state.sock1);
562
563         tevent_fd_set_auto_close(state.sock0.fde);
564         tevent_fd_set_auto_close(state.sock1.fde);
565
566         write(state.sock0.fd, &c, 1);
567         state.sock0.num_written++;
568         write(state.sock1.fd, &c, 1);
569         state.sock1.num_written++;
570
571         while (!state.finished) {
572                 errno = 0;
573                 if (tevent_loop_once(state.ev) == -1) {
574                         talloc_free(state.ev);
575                         torture_fail(tctx, talloc_asprintf(tctx,
576                                      "Failed event loop %s\n",
577                                      strerror(errno)));
578                 }
579         }
580
581         talloc_free(state.ev);
582
583         torture_assert(tctx, state.error == NULL, talloc_asprintf(tctx,
584                        "%s", state.error));
585
586         return true;
587 }
588
589 #ifdef HAVE_PTHREAD
590
591 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
592 static bool do_shutdown = false;
593
594 static void test_event_threaded_lock(void)
595 {
596         int ret;
597         ret = pthread_mutex_lock(&threaded_mutex);
598         assert(ret == 0);
599 }
600
601 static void test_event_threaded_unlock(void)
602 {
603         int ret;
604         ret = pthread_mutex_unlock(&threaded_mutex);
605         assert(ret == 0);
606 }
607
608 static void test_event_threaded_trace(enum tevent_trace_point point,
609                                       void *private_data)
610 {
611         switch (point) {
612         case TEVENT_TRACE_BEFORE_WAIT:
613                 test_event_threaded_unlock();
614                 break;
615         case TEVENT_TRACE_AFTER_WAIT:
616                 test_event_threaded_lock();
617                 break;
618         case TEVENT_TRACE_BEFORE_LOOP_ONCE:
619         case TEVENT_TRACE_AFTER_LOOP_ONCE:
620                 break;
621         }
622 }
623
624 static void test_event_threaded_timer(struct tevent_context *ev,
625                                       struct tevent_timer *te,
626                                       struct timeval current_time,
627                                       void *private_data)
628 {
629         return;
630 }
631
632 static void *test_event_poll_thread(void *private_data)
633 {
634         struct tevent_context *ev = (struct tevent_context *)private_data;
635
636         test_event_threaded_lock();
637
638         while (true) {
639                 int ret;
640                 ret = tevent_loop_once(ev);
641                 assert(ret == 0);
642                 if (do_shutdown) {
643                         test_event_threaded_unlock();
644                         return NULL;
645                 }
646         }
647
648 }
649
650 static void test_event_threaded_read_handler(struct tevent_context *ev,
651                                              struct tevent_fd *fde,
652                                              uint16_t flags,
653                                              void *private_data)
654 {
655         int *pfd = (int *)private_data;
656         char c;
657         ssize_t nread;
658
659         if ((flags & TEVENT_FD_READ) == 0) {
660                 return;
661         }
662
663         do {
664                 nread = read(*pfd, &c, 1);
665         } while ((nread == -1) && (errno == EINTR));
666
667         assert(nread == 1);
668 }
669
670 static bool test_event_context_threaded(struct torture_context *test,
671                                         const void *test_data)
672 {
673         struct tevent_context *ev;
674         struct tevent_timer *te;
675         struct tevent_fd *fde;
676         pthread_t poll_thread;
677         int fds[2];
678         int ret;
679         char c = 0;
680
681         ev = tevent_context_init_byname(test, "poll_mt");
682         torture_assert(test, ev != NULL, "poll_mt not supported");
683
684         tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
685
686         te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
687                               test_event_threaded_timer, NULL);
688         torture_assert(test, te != NULL, "Could not add timer");
689
690         ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
691         torture_assert(test, ret == 0, "Could not create poll thread");
692
693         ret = pipe(fds);
694         torture_assert(test, ret == 0, "Could not create pipe");
695
696         poll(NULL, 0, 100);
697
698         test_event_threaded_lock();
699
700         fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
701                             test_event_threaded_read_handler, &fds[0]);
702         torture_assert(test, fde != NULL, "Could not add fd event");
703
704         test_event_threaded_unlock();
705
706         poll(NULL, 0, 100);
707
708         write(fds[1], &c, 1);
709
710         poll(NULL, 0, 100);
711
712         test_event_threaded_lock();
713         do_shutdown = true;
714         test_event_threaded_unlock();
715
716         write(fds[1], &c, 1);
717
718         ret = pthread_join(poll_thread, NULL);
719         torture_assert(test, ret == 0, "pthread_join failed");
720
721         return true;
722 }
723
724 #endif
725
726 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
727 {
728         struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
729         const char **list = tevent_backend_list(suite);
730         int i;
731
732         for (i=0;list && list[i];i++) {
733                 torture_suite_add_simple_tcase_const(suite,
734                                                talloc_asprintf(suite,
735                                                "%s.%s", list[i], "context"),
736                                                test_event_context,
737                                                (const void *)list[i]);
738                 torture_suite_add_simple_tcase_const(suite,
739                                                talloc_asprintf(suite,
740                                                "%s.%s", list[i], "fd1"),
741                                                test_event_fd1,
742                                                (const void *)list[i]);
743                 torture_suite_add_simple_tcase_const(suite,
744                                                talloc_asprintf(suite,
745                                                "%s.%s", list[i], "fd2"),
746                                                test_event_fd2,
747                                                (const void *)list[i]);
748         }
749
750 #ifdef HAVE_PTHREAD
751         torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
752                                              test_event_context_threaded,
753                                              NULL);
754 #endif
755
756         return suite;
757 }