Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libevent / test / regress_main.c
blob32557c1ee183fda1b0989363cef192e2e10452ea
1 /*
2 * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
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.
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.
28 #ifdef WIN32
29 #include <winsock2.h>
30 #include <windows.h>
31 #include <io.h>
32 #include <fcntl.h>
33 #endif
35 #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
36 #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
37 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
38 #define FORK_BREAKS_GCOV
39 #include <vproc.h>
40 #endif
41 #endif
43 #include "event2/event-config.h"
45 #ifdef _EVENT___func__
46 #define __func__ _EVENT___func__
47 #endif
49 #if 0
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #ifdef _EVENT_HAVE_SYS_TIME_H
53 #include <sys/time.h>
54 #endif
55 #include <sys/queue.h>
56 #include <signal.h>
57 #include <errno.h>
58 #endif
60 #include <sys/types.h>
62 #ifndef WIN32
63 #include <sys/socket.h>
64 #include <sys/wait.h>
65 #include <signal.h>
66 #include <unistd.h>
67 #include <netdb.h>
68 #endif
70 #include <stdlib.h>
71 #include <stdio.h>
72 #include <string.h>
73 #include <assert.h>
75 #include "event2/util.h"
76 #include "event2/event.h"
77 #include "event2/event_compat.h"
78 #include "event2/dns.h"
79 #include "event2/dns_compat.h"
80 #include "event2/thread.h"
82 #include "event2/event-config.h"
83 #include "regress.h"
84 #include "tinytest.h"
85 #include "tinytest_macros.h"
86 #include "../iocp-internal.h"
87 #include "../event-internal.h"
89 long
90 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
92 long ms = end->tv_sec - start->tv_sec;
93 ms *= 1000;
94 ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
95 return ms;
99 /* ============================================================ */
100 /* Code to wrap up old legacy test cases that used setup() and cleanup().
102 * Not all of the tests designated "legacy" are ones that used setup() and
103 * cleanup(), of course. A test is legacy it it uses setup()/cleanup(), OR
104 * if it wants to find its event base/socketpair in global variables (ugh),
105 * OR if it wants to communicate success/failure through test_ok.
108 /* This is set to true if we're inside a legacy test wrapper. It lets the
109 setup() and cleanup() functions in regress.c know they're not needed.
111 int in_legacy_test_wrapper = 0;
113 static void dnslogcb(int w, const char *m)
115 TT_BLATHER(("%s", m));
118 /* creates a temporary file with the data in it */
120 regress_make_tmpfile(const void *data, size_t datalen)
122 #ifndef WIN32
123 char tmpfilename[32];
124 int fd;
125 strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
126 fd = mkstemp(tmpfilename);
127 if (fd == -1)
128 return (-1);
129 if (write(fd, data, datalen) != (int)datalen) {
130 close(fd);
131 return (-1);
133 lseek(fd, 0, SEEK_SET);
134 /* remove it from the file system */
135 unlink(tmpfilename);
136 return (fd);
137 #else
138 /* XXXX actually delete the file later */
139 char tmpfilepath[MAX_PATH];
140 char tmpfilename[MAX_PATH];
141 DWORD r, written;
142 int tries = 16;
143 HANDLE h;
144 r = GetTempPathA(MAX_PATH, tmpfilepath);
145 if (r > MAX_PATH || r == 0)
146 return (-1);
147 for (; tries > 0; --tries) {
148 r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
149 if (r == 0)
150 return (-1);
151 h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
152 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
153 if (h != INVALID_HANDLE_VALUE)
154 break;
156 if (tries == 0)
157 return (-1);
158 written = 0;
159 WriteFile(h, data, (DWORD)datalen, &written, NULL);
160 /* Closing the fd returned by this function will indeed close h. */
161 return _open_osfhandle((intptr_t)h,_O_RDONLY);
162 #endif
165 #ifndef _WIN32
166 pid_t
167 regress_fork(void)
169 pid_t pid = fork();
170 #ifdef FORK_BREAKS_GCOV
171 vproc_transaction_begin(0);
172 #endif
173 return pid;
175 #endif
177 static void
178 ignore_log_cb(int s, const char *msg)
182 static void *
183 basic_test_setup(const struct testcase_t *testcase)
185 struct event_base *base = NULL;
186 evutil_socket_t spair[2] = { -1, -1 };
187 struct basic_test_data *data = NULL;
189 #ifndef WIN32
190 if (testcase->flags & TT_ENABLE_IOCP_FLAG)
191 return (void*)TT_SKIP;
192 #endif
194 if (testcase->flags & TT_NEED_THREADS) {
195 if (!(testcase->flags & TT_FORK))
196 return NULL;
197 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
198 if (evthread_use_pthreads())
199 exit(1);
200 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
201 if (evthread_use_windows_threads())
202 exit(1);
203 #else
204 return (void*)TT_SKIP;
205 #endif
208 if (testcase->flags & TT_NEED_SOCKETPAIR) {
209 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
210 fprintf(stderr, "%s: socketpair\n", __func__);
211 exit(1);
214 if (evutil_make_socket_nonblocking(spair[0]) == -1) {
215 fprintf(stderr, "fcntl(O_NONBLOCK)");
216 exit(1);
219 if (evutil_make_socket_nonblocking(spair[1]) == -1) {
220 fprintf(stderr, "fcntl(O_NONBLOCK)");
221 exit(1);
224 if (testcase->flags & TT_NEED_BASE) {
225 if (testcase->flags & TT_LEGACY)
226 base = event_init();
227 else
228 base = event_base_new();
229 if (!base)
230 exit(1);
232 if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
233 if (event_base_start_iocp(base, 0)<0) {
234 event_base_free(base);
235 return (void*)TT_SKIP;
239 if (testcase->flags & TT_NEED_DNS) {
240 evdns_set_log_fn(dnslogcb);
241 if (evdns_init())
242 return NULL; /* fast failure */ /*XXX asserts. */
245 if (testcase->flags & TT_NO_LOGS)
246 event_set_log_callback(ignore_log_cb);
248 data = calloc(1, sizeof(*data));
249 if (!data)
250 exit(1);
251 data->base = base;
252 data->pair[0] = spair[0];
253 data->pair[1] = spair[1];
254 data->setup_data = testcase->setup_data;
255 return data;
258 static int
259 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
261 struct basic_test_data *data = ptr;
263 if (testcase->flags & TT_NO_LOGS)
264 event_set_log_callback(NULL);
266 if (testcase->flags & TT_NEED_SOCKETPAIR) {
267 if (data->pair[0] != -1)
268 evutil_closesocket(data->pair[0]);
269 if (data->pair[1] != -1)
270 evutil_closesocket(data->pair[1]);
273 if (testcase->flags & TT_NEED_DNS) {
274 evdns_shutdown(0);
277 if (testcase->flags & TT_NEED_BASE) {
278 if (data->base) {
279 event_base_assert_ok(data->base);
280 event_base_free(data->base);
284 free(data);
286 return 1;
289 const struct testcase_setup_t basic_setup = {
290 basic_test_setup, basic_test_cleanup
293 /* The "data" for a legacy test is just a pointer to the void fn(void)
294 function implementing the test case. We need to set up some globals,
295 though, since that's where legacy tests expect to find a socketpair
296 (sometimes) and a global event_base (sometimes).
298 static void *
299 legacy_test_setup(const struct testcase_t *testcase)
301 struct basic_test_data *data = basic_test_setup(testcase);
302 if (data == (void*)TT_SKIP)
303 return data;
304 global_base = data->base;
305 pair[0] = data->pair[0];
306 pair[1] = data->pair[1];
307 data->legacy_test_fn = testcase->setup_data;
308 return data;
311 /* This function is the implementation of every legacy test case. It
312 sets test_ok to 0, invokes the test function, and tells tinytest that
313 the test failed if the test didn't set test_ok to 1.
315 void
316 run_legacy_test_fn(void *ptr)
318 struct basic_test_data *data = ptr;
319 test_ok = called = 0;
321 in_legacy_test_wrapper = 1;
322 data->legacy_test_fn(); /* This part actually calls the test */
323 in_legacy_test_wrapper = 0;
325 if (!test_ok)
326 tt_abort_msg("Legacy unit test failed");
328 end:
329 test_ok = 0;
332 /* This function doesn't have to clean up ptr (which is just a pointer
333 to the test function), but it may need to close the socketpair or
334 free the event_base.
336 static int
337 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
339 int r = basic_test_cleanup(testcase, ptr);
340 pair[0] = pair[1] = -1;
341 global_base = NULL;
342 return r;
345 const struct testcase_setup_t legacy_setup = {
346 legacy_test_setup, legacy_test_cleanup
349 /* ============================================================ */
351 #if (!defined(_EVENT_HAVE_PTHREADS) && !defined(WIN32)) || defined(_EVENT_DISABLE_THREAD_SUPPORT)
352 struct testcase_t thread_testcases[] = {
353 { "basic", NULL, TT_SKIP, NULL, NULL },
354 END_OF_TESTCASES
356 #endif
358 struct testgroup_t testgroups[] = {
359 { "main/", main_testcases },
360 { "heap/", minheap_testcases },
361 { "et/", edgetriggered_testcases },
362 { "evbuffer/", evbuffer_testcases },
363 { "signal/", signal_testcases },
364 { "util/", util_testcases },
365 { "bufferevent/", bufferevent_testcases },
366 { "http/", http_testcases },
367 { "dns/", dns_testcases },
368 { "evtag/", evtag_testcases },
369 { "rpc/", rpc_testcases },
370 { "thread/", thread_testcases },
371 { "listener/", listener_testcases },
372 #ifdef WIN32
373 { "iocp/", iocp_testcases },
374 { "iocp/bufferevent/", bufferevent_iocp_testcases },
375 { "iocp/listener/", listener_iocp_testcases },
376 #endif
377 #ifdef _EVENT_HAVE_OPENSSL
378 { "ssl/", ssl_testcases },
379 #endif
380 END_OF_GROUPS
384 main(int argc, const char **argv)
386 #ifdef WIN32
387 WORD wVersionRequested;
388 WSADATA wsaData;
389 int err;
391 wVersionRequested = MAKEWORD(2, 2);
393 err = WSAStartup(wVersionRequested, &wsaData);
394 #endif
396 #ifndef WIN32
397 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
398 return 1;
399 #endif
401 #ifdef WIN32
402 tinytest_skip(testgroups, "http/connection_retry");
403 #endif
405 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
406 if (!getenv("EVENT_NO_DEBUG_LOCKS"))
407 evthread_enable_lock_debuging();
408 #endif
410 if (tinytest_main(argc,argv,testgroups))
411 return 1;
413 return 0;