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