dd972414b3f47460d044694961639de21a0fd117
[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 #ifdef HAVE_PTHREAD
356
357 static pthread_mutex_t threaded_mutex = PTHREAD_MUTEX_INITIALIZER;
358 static bool do_shutdown = false;
359
360 static void test_event_threaded_lock(void)
361 {
362         int ret;
363         ret = pthread_mutex_lock(&threaded_mutex);
364         assert(ret == 0);
365 }
366
367 static void test_event_threaded_unlock(void)
368 {
369         int ret;
370         ret = pthread_mutex_unlock(&threaded_mutex);
371         assert(ret == 0);
372 }
373
374 static void test_event_threaded_trace(enum tevent_trace_point point,
375                                       void *private_data)
376 {
377         switch (point) {
378         case TEVENT_TRACE_BEFORE_WAIT:
379                 test_event_threaded_unlock();
380                 break;
381         case TEVENT_TRACE_AFTER_WAIT:
382                 test_event_threaded_lock();
383                 break;
384         case TEVENT_TRACE_BEFORE_LOOP_ONCE:
385         case TEVENT_TRACE_AFTER_LOOP_ONCE:
386                 break;
387         }
388 }
389
390 static void test_event_threaded_timer(struct tevent_context *ev,
391                                       struct tevent_timer *te,
392                                       struct timeval current_time,
393                                       void *private_data)
394 {
395         return;
396 }
397
398 static void *test_event_poll_thread(void *private_data)
399 {
400         struct tevent_context *ev = (struct tevent_context *)private_data;
401
402         test_event_threaded_lock();
403
404         while (true) {
405                 int ret;
406                 ret = tevent_loop_once(ev);
407                 assert(ret == 0);
408                 if (do_shutdown) {
409                         test_event_threaded_unlock();
410                         return NULL;
411                 }
412         }
413
414 }
415
416 static void test_event_threaded_read_handler(struct tevent_context *ev,
417                                              struct tevent_fd *fde,
418                                              uint16_t flags,
419                                              void *private_data)
420 {
421         int *pfd = (int *)private_data;
422         char c;
423         ssize_t nread;
424
425         if ((flags & TEVENT_FD_READ) == 0) {
426                 return;
427         }
428
429         do {
430                 nread = read(*pfd, &c, 1);
431         } while ((nread == -1) && (errno == EINTR));
432
433         assert(nread == 1);
434 }
435
436 static bool test_event_context_threaded(struct torture_context *test,
437                                         const void *test_data)
438 {
439         struct tevent_context *ev;
440         struct tevent_timer *te;
441         struct tevent_fd *fde;
442         pthread_t poll_thread;
443         int fds[2];
444         int ret;
445         char c = 0;
446
447         ev = tevent_context_init_byname(test, "poll_mt");
448         torture_assert(test, ev != NULL, "poll_mt not supported");
449
450         tevent_set_trace_callback(ev, test_event_threaded_trace, NULL);
451
452         te = tevent_add_timer(ev, ev, timeval_current_ofs(5, 0),
453                               test_event_threaded_timer, NULL);
454         torture_assert(test, te != NULL, "Could not add timer");
455
456         ret = pthread_create(&poll_thread, NULL, test_event_poll_thread, ev);
457         torture_assert(test, ret == 0, "Could not create poll thread");
458
459         ret = pipe(fds);
460         torture_assert(test, ret == 0, "Could not create pipe");
461
462         poll(NULL, 0, 100);
463
464         test_event_threaded_lock();
465
466         fde = tevent_add_fd(ev, ev, fds[0], TEVENT_FD_READ,
467                             test_event_threaded_read_handler, &fds[0]);
468         torture_assert(test, fde != NULL, "Could not add fd event");
469
470         test_event_threaded_unlock();
471
472         poll(NULL, 0, 100);
473
474         write(fds[1], &c, 1);
475
476         poll(NULL, 0, 100);
477
478         test_event_threaded_lock();
479         do_shutdown = true;
480         test_event_threaded_unlock();
481
482         write(fds[1], &c, 1);
483
484         ret = pthread_join(poll_thread, NULL);
485         torture_assert(test, ret == 0, "pthread_join failed");
486
487         return true;
488 }
489
490 #endif
491
492 struct torture_suite *torture_local_event(TALLOC_CTX *mem_ctx)
493 {
494         struct torture_suite *suite = torture_suite_create(mem_ctx, "event");
495         const char **list = tevent_backend_list(suite);
496         int i;
497
498         for (i=0;list && list[i];i++) {
499                 torture_suite_add_simple_tcase_const(suite, list[i],
500                                                test_event_context,
501                                                (const void *)list[i]);
502                 torture_suite_add_simple_tcase_const(suite, list[i],
503                                                test_event_fd1,
504                                                (const void *)list[i]);
505         }
506
507 #ifdef HAVE_PTHREAD
508         torture_suite_add_simple_tcase_const(suite, "poll_mt_threaded",
509                                              test_event_context_threaded,
510                                              NULL);
511 #endif
512
513         return suite;
514 }