]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_main.c
Add libevent
[netatalk.git] / libevent / test / regress_main.c
1 /*
2  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2010 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
80 long
81 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
82 {
83         long ms = end->tv_sec - start->tv_sec;
84         ms *= 1000;
85         ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
86         return ms;
87
88 }
89
90 /* ============================================================ */
91 /* Code to wrap up old legacy test cases that used setup() and cleanup().
92  *
93  * Not all of the tests designated "legacy" are ones that used setup() and
94  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
95  * if it wants to find its event base/socketpair in global variables (ugh),
96  * OR if it wants to communicate success/failure through test_ok.
97  */
98
99 /* This is set to true if we're inside a legacy test wrapper.  It lets the
100    setup() and cleanup() functions in regress.c know they're not needed.
101  */
102 int in_legacy_test_wrapper = 0;
103
104 static void dnslogcb(int w, const char *m)
105 {
106         TT_BLATHER(("%s", m));
107 }
108
109 /* creates a temporary file with the data in it */
110 int
111 regress_make_tmpfile(const void *data, size_t datalen)
112 {
113 #ifndef WIN32
114         char tmpfilename[32];
115         int fd;
116         strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
117         fd = mkstemp(tmpfilename);
118         if (fd == -1)
119                 return (-1);
120         if (write(fd, data, datalen) != (int)datalen) {
121                 close(fd);
122                 return (-1);
123         }
124         lseek(fd, 0, SEEK_SET);
125         /* remove it from the file system */
126         unlink(tmpfilename);
127         return (fd);
128 #else
129         /* XXXX actually delete the file later */
130         char tmpfilepath[MAX_PATH];
131         char tmpfilename[MAX_PATH];
132         DWORD r, written;
133         int tries = 16;
134         HANDLE h;
135         r = GetTempPathA(MAX_PATH, tmpfilepath);
136         if (r > MAX_PATH || r == 0)
137                 return (-1);
138         for (; tries > 0; --tries) {
139                 r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
140                 if (r == 0)
141                         return (-1);
142                 h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
143                     0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
144                 if (h != INVALID_HANDLE_VALUE)
145                         break;
146         }
147         if (tries == 0)
148                 return (-1);
149         written = 0;
150         WriteFile(h, data, (DWORD)datalen, &written, NULL);
151         /* Closing the fd returned by this function will indeed close h. */
152         return _open_osfhandle((intptr_t)h,_O_RDONLY);
153 #endif
154 }
155
156 static void
157 ignore_log_cb(int s, const char *msg)
158 {
159 }
160
161 static void *
162 basic_test_setup(const struct testcase_t *testcase)
163 {
164         struct event_base *base = NULL;
165         evutil_socket_t spair[2] = { -1, -1 };
166         struct basic_test_data *data = NULL;
167
168 #ifndef WIN32
169         if (testcase->flags & TT_ENABLE_IOCP_FLAG)
170                 return (void*)TT_SKIP;
171 #endif
172
173         if (testcase->flags & TT_NEED_THREADS) {
174                 if (!(testcase->flags & TT_FORK))
175                         return NULL;
176 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
177                 if (evthread_use_pthreads())
178                         exit(1);
179 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
180                 if (evthread_use_windows_threads())
181                         exit(1);
182 #else
183                 return (void*)TT_SKIP;
184 #endif
185         }
186
187         if (testcase->flags & TT_NEED_SOCKETPAIR) {
188                 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
189                         fprintf(stderr, "%s: socketpair\n", __func__);
190                         exit(1);
191                 }
192
193                 if (evutil_make_socket_nonblocking(spair[0]) == -1) {
194                         fprintf(stderr, "fcntl(O_NONBLOCK)");
195                         exit(1);
196                 }
197
198                 if (evutil_make_socket_nonblocking(spair[1]) == -1) {
199                         fprintf(stderr, "fcntl(O_NONBLOCK)");
200                         exit(1);
201                 }
202         }
203         if (testcase->flags & TT_NEED_BASE) {
204                 if (testcase->flags & TT_LEGACY)
205                         base = event_init();
206                 else
207                         base = event_base_new();
208                 if (!base)
209                         exit(1);
210         }
211         if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
212                 if (event_base_start_iocp(base, 0)<0) {
213                         event_base_free(base);
214                         return (void*)TT_SKIP;
215                 }
216         }
217
218         if (testcase->flags & TT_NEED_DNS) {
219                 evdns_set_log_fn(dnslogcb);
220                 if (evdns_init())
221                         return NULL; /* fast failure */ /*XXX asserts. */
222         }
223
224         if (testcase->flags & TT_NO_LOGS)
225                 event_set_log_callback(ignore_log_cb);
226
227         data = calloc(1, sizeof(*data));
228         if (!data)
229                 exit(1);
230         data->base = base;
231         data->pair[0] = spair[0];
232         data->pair[1] = spair[1];
233         data->setup_data = testcase->setup_data;
234         return data;
235 }
236
237 static int
238 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
239 {
240         struct basic_test_data *data = ptr;
241
242         if (testcase->flags & TT_NO_LOGS)
243                 event_set_log_callback(NULL);
244
245         if (testcase->flags & TT_NEED_SOCKETPAIR) {
246                 if (data->pair[0] != -1)
247                         evutil_closesocket(data->pair[0]);
248                 if (data->pair[1] != -1)
249                         evutil_closesocket(data->pair[1]);
250         }
251
252         if (testcase->flags & TT_NEED_DNS) {
253                 evdns_shutdown(0);
254         }
255
256         if (testcase->flags & TT_NEED_BASE) {
257                 if (data->base)
258                         event_base_free(data->base);
259         }
260
261         free(data);
262
263         return 1;
264 }
265
266 const struct testcase_setup_t basic_setup = {
267         basic_test_setup, basic_test_cleanup
268 };
269
270 /* The "data" for a legacy test is just a pointer to the void fn(void)
271    function implementing the test case.  We need to set up some globals,
272    though, since that's where legacy tests expect to find a socketpair
273    (sometimes) and a global event_base (sometimes).
274  */
275 static void *
276 legacy_test_setup(const struct testcase_t *testcase)
277 {
278         struct basic_test_data *data = basic_test_setup(testcase);
279         if (data == (void*)TT_SKIP)
280                 return data;
281         global_base = data->base;
282         pair[0] = data->pair[0];
283         pair[1] = data->pair[1];
284         data->legacy_test_fn = testcase->setup_data;
285         return data;
286 }
287
288 /* This function is the implementation of every legacy test case.  It
289    sets test_ok to 0, invokes the test function, and tells tinytest that
290    the test failed if the test didn't set test_ok to 1.
291  */
292 void
293 run_legacy_test_fn(void *ptr)
294 {
295         struct basic_test_data *data = ptr;
296         test_ok = called = 0;
297
298         in_legacy_test_wrapper = 1;
299         data->legacy_test_fn(); /* This part actually calls the test */
300         in_legacy_test_wrapper = 0;
301
302         if (!test_ok)
303                 tt_abort_msg("Legacy unit test failed");
304
305 end:
306         test_ok = 0;
307 }
308
309 /* This function doesn't have to clean up ptr (which is just a pointer
310    to the test function), but it may need to close the socketpair or
311    free the event_base.
312  */
313 static int
314 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
315 {
316         int r = basic_test_cleanup(testcase, ptr);
317         pair[0] = pair[1] = -1;
318         global_base = NULL;
319         return r;
320 }
321
322 const struct testcase_setup_t legacy_setup = {
323         legacy_test_setup, legacy_test_cleanup
324 };
325
326 /* ============================================================ */
327
328 #if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT)
329 struct testcase_t thread_testcases[] = {
330         { "basic", NULL, TT_SKIP, NULL, NULL },
331         END_OF_TESTCASES
332 };
333 #endif
334
335 struct testgroup_t testgroups[] = {
336         { "main/", main_testcases },
337         { "heap/", minheap_testcases },
338         { "et/", edgetriggered_testcases },
339         { "evbuffer/", evbuffer_testcases },
340         { "signal/", signal_testcases },
341         { "util/", util_testcases },
342         { "bufferevent/", bufferevent_testcases },
343         { "http/", http_testcases },
344         { "dns/", dns_testcases },
345         { "evtag/", evtag_testcases },
346         { "rpc/", rpc_testcases },
347         { "thread/", thread_testcases },
348         { "listener/", listener_testcases },
349 #ifdef WIN32
350         { "iocp/", iocp_testcases },
351         { "iocp/bufferevent/", bufferevent_iocp_testcases },
352         { "iocp/listener/", listener_iocp_testcases },
353 #endif
354 #ifdef _EVENT_HAVE_OPENSSL
355         { "ssl/", ssl_testcases },
356 #endif
357         END_OF_GROUPS
358 };
359
360 int
361 main(int argc, const char **argv)
362 {
363 #ifdef WIN32
364         WORD wVersionRequested;
365         WSADATA wsaData;
366         int     err;
367
368         wVersionRequested = MAKEWORD(2, 2);
369
370         err = WSAStartup(wVersionRequested, &wsaData);
371 #endif
372
373 #ifndef WIN32
374         if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
375                 return 1;
376 #endif
377
378 #ifdef WIN32
379         tinytest_skip(testgroups, "http/connection_retry");
380 #endif
381
382 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
383         if (!getenv("EVENT_NO_DEBUG_LOCKS"))
384                 evthread_enable_lock_debuging();
385 #endif
386
387         if (tinytest_main(argc,argv,testgroups))
388                 return 1;
389
390         return 0;
391 }
392