]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_main.c
New service controller process netatalk
[netatalk.git] / libevent / test / regress_main.c
1 /*
2  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifdef WIN32
29 #include <winsock2.h>
30 #include <windows.h>
31 #include <io.h>
32 #include <fcntl.h>
33 #endif
34
35 #include "event2/event-config.h"
36
37 #ifdef _EVENT___func__
38 #define __func__ _EVENT___func__
39 #endif
40
41 #if 0
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #ifdef _EVENT_HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47 #include <sys/queue.h>
48 #include <signal.h>
49 #include <errno.h>
50 #endif
51
52 #include <sys/types.h>
53
54 #ifndef WIN32
55 #include <sys/socket.h>
56 #include <sys/wait.h>
57 #include <signal.h>
58 #include <unistd.h>
59 #include <netdb.h>
60 #endif
61
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <string.h>
65 #include <assert.h>
66
67 #include "event2/util.h"
68 #include "event2/event.h"
69 #include "event2/event_compat.h"
70 #include "event2/dns.h"
71 #include "event2/dns_compat.h"
72 #include "event2/thread.h"
73
74 #include "event2/event-config.h"
75 #include "regress.h"
76 #include "tinytest.h"
77 #include "tinytest_macros.h"
78 #include "../iocp-internal.h"
79 #include "../event-internal.h"
80
81 long
82 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
83 {
84         long ms = end->tv_sec - start->tv_sec;
85         ms *= 1000;
86         ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
87         return ms;
88
89 }
90
91 /* ============================================================ */
92 /* Code to wrap up old legacy test cases that used setup() and cleanup().
93  *
94  * Not all of the tests designated "legacy" are ones that used setup() and
95  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
96  * if it wants to find its event base/socketpair in global variables (ugh),
97  * OR if it wants to communicate success/failure through test_ok.
98  */
99
100 /* This is set to true if we're inside a legacy test wrapper.  It lets the
101    setup() and cleanup() functions in regress.c know they're not needed.
102  */
103 int in_legacy_test_wrapper = 0;
104
105 static void dnslogcb(int w, const char *m)
106 {
107         TT_BLATHER(("%s", m));
108 }
109
110 /* creates a temporary file with the data in it */
111 int
112 regress_make_tmpfile(const void *data, size_t datalen)
113 {
114 #ifndef WIN32
115         char tmpfilename[32];
116         int fd;
117         strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
118         fd = mkstemp(tmpfilename);
119         if (fd == -1)
120                 return (-1);
121         if (write(fd, data, datalen) != (int)datalen) {
122                 close(fd);
123                 return (-1);
124         }
125         lseek(fd, 0, SEEK_SET);
126         /* remove it from the file system */
127         unlink(tmpfilename);
128         return (fd);
129 #else
130         /* XXXX actually delete the file later */
131         char tmpfilepath[MAX_PATH];
132         char tmpfilename[MAX_PATH];
133         DWORD r, written;
134         int tries = 16;
135         HANDLE h;
136         r = GetTempPathA(MAX_PATH, tmpfilepath);
137         if (r > MAX_PATH || r == 0)
138                 return (-1);
139         for (; tries > 0; --tries) {
140                 r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
141                 if (r == 0)
142                         return (-1);
143                 h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
144                     0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
145                 if (h != INVALID_HANDLE_VALUE)
146                         break;
147         }
148         if (tries == 0)
149                 return (-1);
150         written = 0;
151         WriteFile(h, data, (DWORD)datalen, &written, NULL);
152         /* Closing the fd returned by this function will indeed close h. */
153         return _open_osfhandle((intptr_t)h,_O_RDONLY);
154 #endif
155 }
156
157 static void
158 ignore_log_cb(int s, const char *msg)
159 {
160 }
161
162 static void *
163 basic_test_setup(const struct testcase_t *testcase)
164 {
165         struct event_base *base = NULL;
166         evutil_socket_t spair[2] = { -1, -1 };
167         struct basic_test_data *data = NULL;
168
169 #ifndef WIN32
170         if (testcase->flags & TT_ENABLE_IOCP_FLAG)
171                 return (void*)TT_SKIP;
172 #endif
173
174         if (testcase->flags & TT_NEED_THREADS) {
175                 if (!(testcase->flags & TT_FORK))
176                         return NULL;
177 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
178                 if (evthread_use_pthreads())
179                         exit(1);
180 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
181                 if (evthread_use_windows_threads())
182                         exit(1);
183 #else
184                 return (void*)TT_SKIP;
185 #endif
186         }
187
188         if (testcase->flags & TT_NEED_SOCKETPAIR) {
189                 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
190                         fprintf(stderr, "%s: socketpair\n", __func__);
191                         exit(1);
192                 }
193
194                 if (evutil_make_socket_nonblocking(spair[0]) == -1) {
195                         fprintf(stderr, "fcntl(O_NONBLOCK)");
196                         exit(1);
197                 }
198
199                 if (evutil_make_socket_nonblocking(spair[1]) == -1) {
200                         fprintf(stderr, "fcntl(O_NONBLOCK)");
201                         exit(1);
202                 }
203         }
204         if (testcase->flags & TT_NEED_BASE) {
205                 if (testcase->flags & TT_LEGACY)
206                         base = event_init();
207                 else
208                         base = event_base_new();
209                 if (!base)
210                         exit(1);
211         }
212         if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
213                 if (event_base_start_iocp(base, 0)<0) {
214                         event_base_free(base);
215                         return (void*)TT_SKIP;
216                 }
217         }
218
219         if (testcase->flags & TT_NEED_DNS) {
220                 evdns_set_log_fn(dnslogcb);
221                 if (evdns_init())
222                         return NULL; /* fast failure */ /*XXX asserts. */
223         }
224
225         if (testcase->flags & TT_NO_LOGS)
226                 event_set_log_callback(ignore_log_cb);
227
228         data = calloc(1, sizeof(*data));
229         if (!data)
230                 exit(1);
231         data->base = base;
232         data->pair[0] = spair[0];
233         data->pair[1] = spair[1];
234         data->setup_data = testcase->setup_data;
235         return data;
236 }
237
238 static int
239 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
240 {
241         struct basic_test_data *data = ptr;
242
243         if (testcase->flags & TT_NO_LOGS)
244                 event_set_log_callback(NULL);
245
246         if (testcase->flags & TT_NEED_SOCKETPAIR) {
247                 if (data->pair[0] != -1)
248                         evutil_closesocket(data->pair[0]);
249                 if (data->pair[1] != -1)
250                         evutil_closesocket(data->pair[1]);
251         }
252
253         if (testcase->flags & TT_NEED_DNS) {
254                 evdns_shutdown(0);
255         }
256
257         if (testcase->flags & TT_NEED_BASE) {
258                 if (data->base) {
259                         event_base_assert_ok(data->base);
260                         event_base_free(data->base);
261                 }
262         }
263
264         free(data);
265
266         return 1;
267 }
268
269 const struct testcase_setup_t basic_setup = {
270         basic_test_setup, basic_test_cleanup
271 };
272
273 /* The "data" for a legacy test is just a pointer to the void fn(void)
274    function implementing the test case.  We need to set up some globals,
275    though, since that's where legacy tests expect to find a socketpair
276    (sometimes) and a global event_base (sometimes).
277  */
278 static void *
279 legacy_test_setup(const struct testcase_t *testcase)
280 {
281         struct basic_test_data *data = basic_test_setup(testcase);
282         if (data == (void*)TT_SKIP)
283                 return data;
284         global_base = data->base;
285         pair[0] = data->pair[0];
286         pair[1] = data->pair[1];
287         data->legacy_test_fn = testcase->setup_data;
288         return data;
289 }
290
291 /* This function is the implementation of every legacy test case.  It
292    sets test_ok to 0, invokes the test function, and tells tinytest that
293    the test failed if the test didn't set test_ok to 1.
294  */
295 void
296 run_legacy_test_fn(void *ptr)
297 {
298         struct basic_test_data *data = ptr;
299         test_ok = called = 0;
300
301         in_legacy_test_wrapper = 1;
302         data->legacy_test_fn(); /* This part actually calls the test */
303         in_legacy_test_wrapper = 0;
304
305         if (!test_ok)
306                 tt_abort_msg("Legacy unit test failed");
307
308 end:
309         test_ok = 0;
310 }
311
312 /* This function doesn't have to clean up ptr (which is just a pointer
313    to the test function), but it may need to close the socketpair or
314    free the event_base.
315  */
316 static int
317 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
318 {
319         int r = basic_test_cleanup(testcase, ptr);
320         pair[0] = pair[1] = -1;
321         global_base = NULL;
322         return r;
323 }
324
325 const struct testcase_setup_t legacy_setup = {
326         legacy_test_setup, legacy_test_cleanup
327 };
328
329 /* ============================================================ */
330
331 #if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT)
332 struct testcase_t thread_testcases[] = {
333         { "basic", NULL, TT_SKIP, NULL, NULL },
334         END_OF_TESTCASES
335 };
336 #endif
337
338 struct testgroup_t testgroups[] = {
339         { "main/", main_testcases },
340         { "heap/", minheap_testcases },
341         { "et/", edgetriggered_testcases },
342         { "evbuffer/", evbuffer_testcases },
343         { "signal/", signal_testcases },
344         { "util/", util_testcases },
345         { "bufferevent/", bufferevent_testcases },
346         { "http/", http_testcases },
347         { "dns/", dns_testcases },
348         { "evtag/", evtag_testcases },
349         { "rpc/", rpc_testcases },
350         { "thread/", thread_testcases },
351         { "listener/", listener_testcases },
352 #ifdef WIN32
353         { "iocp/", iocp_testcases },
354         { "iocp/bufferevent/", bufferevent_iocp_testcases },
355         { "iocp/listener/", listener_iocp_testcases },
356 #endif
357 #ifdef _EVENT_HAVE_OPENSSL
358         { "ssl/", ssl_testcases },
359 #endif
360         END_OF_GROUPS
361 };
362
363 int
364 main(int argc, const char **argv)
365 {
366 #ifdef WIN32
367         WORD wVersionRequested;
368         WSADATA wsaData;
369         int     err;
370
371         wVersionRequested = MAKEWORD(2, 2);
372
373         err = WSAStartup(wVersionRequested, &wsaData);
374 #endif
375
376 #ifndef WIN32
377         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
378                 return 1;
379 #endif
380
381 #ifdef WIN32
382         tinytest_skip(testgroups, "http/connection_retry");
383 #endif
384
385 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
386         if (!getenv("EVENT_NO_DEBUG_LOCKS"))
387                 evthread_enable_lock_debuging();
388 #endif
389
390         if (tinytest_main(argc,argv,testgroups))
391                 return 1;
392
393         return 0;
394 }
395