xref: /netbsd-src/external/bsd/libevent/dist/test/regress_main.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: regress_main.c,v 1.3 2017/01/31 23:17:40 christos Exp $	*/
2 /*
3  * Copyright (c) 2003-2007 Niels Provos <provos@citi.umich.edu>
4  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5  *
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.
16  *
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.
27  */
28 #include "util-internal.h"
29 
30 #ifdef _WIN32
31 #include <winsock2.h>
32 #include <windows.h>
33 #include <io.h>
34 #include <fcntl.h>
35 #endif
36 
37 #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
38 #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
39     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
40 #define FORK_BREAKS_GCOV
41 #include <vproc.h>
42 #endif
43 #endif
44 
45 #include "event2/event-config.h"
46 #include <sys/cdefs.h>
47 __RCSID("$NetBSD: regress_main.c,v 1.3 2017/01/31 23:17:40 christos Exp $");
48 
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
59 
60 #include <sys/types.h>
61 #ifdef EVENT__HAVE_SYS_STAT_H
62 #include <sys/stat.h>
63 #endif
64 
65 #ifndef _WIN32
66 #include <sys/socket.h>
67 #include <sys/wait.h>
68 #include <signal.h>
69 #include <unistd.h>
70 #include <netdb.h>
71 #endif
72 
73 #include <stdlib.h>
74 #include <stdio.h>
75 #include <string.h>
76 #include <assert.h>
77 
78 #include "event2/util.h"
79 #include "event2/event.h"
80 #include "event2/event_compat.h"
81 #include "event2/dns.h"
82 #include "event2/dns_compat.h"
83 #include "event2/thread.h"
84 
85 #include "event2/event-config.h"
86 #include <sys/cdefs.h>
87 __RCSID("$NetBSD: regress_main.c,v 1.3 2017/01/31 23:17:40 christos Exp $");
88 #include "regress.h"
89 #include "tinytest.h"
90 #include "tinytest_macros.h"
91 #include "../iocp-internal.h"
92 #include "../event-internal.h"
93 
94 struct evutil_weakrand_state test_weakrand_state;
95 
96 long
97 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
98 {
99 	long ms = end->tv_sec - start->tv_sec;
100 	ms *= 1000;
101 	ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
102 	return ms;
103 }
104 
105 /* ============================================================ */
106 /* Code to wrap up old legacy test cases that used setup() and cleanup().
107  *
108  * Not all of the tests designated "legacy" are ones that used setup() and
109  * cleanup(), of course.  A test is legacy it it uses setup()/cleanup(), OR
110  * if it wants to find its event base/socketpair in global variables (ugh),
111  * OR if it wants to communicate success/failure through test_ok.
112  */
113 
114 /* This is set to true if we're inside a legacy test wrapper.  It lets the
115    setup() and cleanup() functions in regress.c know they're not needed.
116  */
117 int in_legacy_test_wrapper = 0;
118 
119 static void dnslogcb(int w, const char *m)
120 {
121 	TT_BLATHER(("%s", m));
122 }
123 
124 /* creates a temporary file with the data in it.  If *filename_out gets set,
125  * the caller should try to unlink it. */
126 int
127 regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
128 {
129 #ifndef _WIN32
130 	char tmpfilename[32];
131 	int fd;
132 	*filename_out = NULL;
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);
143 	}
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;
166 	}
167 	if (tries == 0)
168 		return (-1);
169 	written = 0;
170 	*filename_out = strdup(tmpfilename);
171 	WriteFile(h, data, (DWORD)datalen, &written, NULL);
172 	/* Closing the fd returned by this function will indeed close h. */
173 	return _open_osfhandle((intptr_t)h,_O_RDONLY);
174 #endif
175 }
176 
177 #ifndef _WIN32
178 pid_t
179 regress_fork(void)
180 {
181 	pid_t pid = fork();
182 #ifdef FORK_BREAKS_GCOV
183 	vproc_transaction_begin(0);
184 #endif
185 	return pid;
186 }
187 #endif
188 
189 static void
190 ignore_log_cb(int s, const char *msg)
191 {
192 }
193 
194 static void *
195 basic_test_setup(const struct testcase_t *testcase)
196 {
197 	struct event_base *base = NULL;
198 	evutil_socket_t spair[2] = { -1, -1 };
199 	struct basic_test_data *data = NULL;
200 
201 #ifndef _WIN32
202 	if (testcase->flags & TT_ENABLE_IOCP_FLAG)
203 		return (void*)TT_SKIP;
204 #endif
205 
206 	if (testcase->flags & TT_NEED_THREADS) {
207 		if (!(testcase->flags & TT_FORK))
208 			return NULL;
209 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
210 		if (evthread_use_pthreads())
211 			exit(1);
212 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
213 		if (evthread_use_windows_threads())
214 			exit(1);
215 #else
216 		return (void*)TT_SKIP;
217 #endif
218 	}
219 
220 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
221 		if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
222 			fprintf(stderr, "%s: socketpair\n", __func__);
223 			exit(1);
224 		}
225 
226 		if (evutil_make_socket_nonblocking(spair[0]) == -1) {
227 			fprintf(stderr, "fcntl(O_NONBLOCK)");
228 			exit(1);
229 		}
230 
231 		if (evutil_make_socket_nonblocking(spair[1]) == -1) {
232 			fprintf(stderr, "fcntl(O_NONBLOCK)");
233 			exit(1);
234 		}
235 	}
236 	if (testcase->flags & TT_NEED_BASE) {
237 		if (testcase->flags & TT_LEGACY)
238 			base = event_init();
239 		else
240 			base = event_base_new();
241 		if (!base)
242 			exit(1);
243 	}
244 	if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
245 		if (event_base_start_iocp_(base, 0)<0) {
246 			event_base_free(base);
247 			return (void*)TT_SKIP;
248 		}
249 	}
250 
251 	if (testcase->flags & TT_NEED_DNS) {
252 		evdns_set_log_fn(dnslogcb);
253 		if (evdns_init())
254 			return NULL; /* fast failure */ /*XXX asserts. */
255 	}
256 
257 	if (testcase->flags & TT_NO_LOGS)
258 		event_set_log_callback(ignore_log_cb);
259 
260 	data = calloc(1, sizeof(*data));
261 	if (!data)
262 		exit(1);
263 	data->base = base;
264 	data->pair[0] = spair[0];
265 	data->pair[1] = spair[1];
266 	data->setup_data = testcase->setup_data;
267 	return data;
268 }
269 
270 static int
271 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
272 {
273 	struct basic_test_data *data = ptr;
274 
275 	if (testcase->flags & TT_NO_LOGS)
276 		event_set_log_callback(NULL);
277 
278 	if (testcase->flags & TT_NEED_SOCKETPAIR) {
279 		if (data->pair[0] != -1)
280 			evutil_closesocket(data->pair[0]);
281 		if (data->pair[1] != -1)
282 			evutil_closesocket(data->pair[1]);
283 	}
284 
285 	if (testcase->flags & TT_NEED_DNS) {
286 		evdns_shutdown(0);
287 	}
288 
289 	if (testcase->flags & TT_NEED_BASE) {
290 		if (data->base) {
291 			event_base_assert_ok_(data->base);
292 			event_base_free(data->base);
293 		}
294 	}
295 
296 	if (testcase->flags & TT_FORK)
297 		libevent_global_shutdown();
298 
299 	free(data);
300 
301 	return 1;
302 }
303 
304 const struct testcase_setup_t basic_setup = {
305 	basic_test_setup, basic_test_cleanup
306 };
307 
308 /* The "data" for a legacy test is just a pointer to the void fn(void)
309    function implementing the test case.  We need to set up some globals,
310    though, since that's where legacy tests expect to find a socketpair
311    (sometimes) and a global event_base (sometimes).
312  */
313 static void *
314 legacy_test_setup(const struct testcase_t *testcase)
315 {
316 	struct basic_test_data *data = basic_test_setup(testcase);
317 	if (data == (void*)TT_SKIP || data == NULL)
318 		return data;
319 	global_base = data->base;
320 	pair[0] = data->pair[0];
321 	pair[1] = data->pair[1];
322 	data->legacy_test_fn = testcase->setup_data;
323 	return data;
324 }
325 
326 /* This function is the implementation of every legacy test case.  It
327    sets test_ok to 0, invokes the test function, and tells tinytest that
328    the test failed if the test didn't set test_ok to 1.
329  */
330 void
331 run_legacy_test_fn(void *ptr)
332 {
333 	struct basic_test_data *data = ptr;
334 	test_ok = called = 0;
335 
336 	in_legacy_test_wrapper = 1;
337 	data->legacy_test_fn(); /* This part actually calls the test */
338 	in_legacy_test_wrapper = 0;
339 
340 	if (!test_ok)
341 		tt_abort_msg("Legacy unit test failed");
342 
343 end:
344 	test_ok = 0;
345 }
346 
347 /* This function doesn't have to clean up ptr (which is just a pointer
348    to the test function), but it may need to close the socketpair or
349    free the event_base.
350  */
351 static int
352 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
353 {
354 	int r = basic_test_cleanup(testcase, ptr);
355 	pair[0] = pair[1] = -1;
356 	global_base = NULL;
357 	return r;
358 }
359 
360 const struct testcase_setup_t legacy_setup = {
361 	legacy_test_setup, legacy_test_cleanup
362 };
363 
364 /* ============================================================ */
365 
366 #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
367 struct testcase_t thread_testcases[] = {
368 	{ "basic", NULL, TT_SKIP, NULL, NULL },
369 	END_OF_TESTCASES
370 };
371 #endif
372 
373 struct testgroup_t testgroups[] = {
374 	{ "main/", main_testcases },
375 	{ "heap/", minheap_testcases },
376 	{ "et/", edgetriggered_testcases },
377 	{ "finalize/", finalize_testcases },
378 	{ "evbuffer/", evbuffer_testcases },
379 	{ "signal/", signal_testcases },
380 	{ "util/", util_testcases },
381 	{ "bufferevent/", bufferevent_testcases },
382 	{ "http/", http_testcases },
383 	{ "dns/", dns_testcases },
384 	{ "evtag/", evtag_testcases },
385 	{ "rpc/", rpc_testcases },
386 	{ "thread/", thread_testcases },
387 	{ "listener/", listener_testcases },
388 #ifdef _WIN32
389 	{ "iocp/", iocp_testcases },
390 	{ "iocp/bufferevent/", bufferevent_iocp_testcases },
391 	{ "iocp/listener/", listener_iocp_testcases },
392 #endif
393 #ifdef EVENT__HAVE_OPENSSL
394 	{ "ssl/", ssl_testcases },
395 #endif
396 	END_OF_GROUPS
397 };
398 
399 const char *alltests[] = { "+..", NULL };
400 const char *livenettests[] = {
401 	"+util/getaddrinfo_live",
402 	"+dns/gethostby..",
403 	"+dns/resolve_reverse",
404 	NULL
405 };
406 const char *finetimetests[] = {
407 	"+util/monotonic_res_precise",
408 	"+util/monotonic_res_fallback",
409 	"+thread/deferred_cb_skew",
410 	"+http/connection_retry",
411 	"+http/https_connection_retry",
412 	NULL
413 };
414 struct testlist_alias_t testaliases[] = {
415 	{ "all", alltests },
416 	{ "live_net", livenettests },
417 	{ "fine_timing", finetimetests },
418 	END_OF_ALIASES
419 };
420 
421 int libevent_tests_running_in_debug_mode = 0;
422 
423 int
424 main(int argc, const char **argv)
425 {
426 #ifdef _WIN32
427 	WORD wVersionRequested;
428 	WSADATA wsaData;
429 
430 	wVersionRequested = MAKEWORD(2, 2);
431 
432 	(void) WSAStartup(wVersionRequested, &wsaData);
433 #endif
434 
435 #ifndef _WIN32
436 	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
437 		return 1;
438 #endif
439 
440 #ifdef _WIN32
441 	tinytest_skip(testgroups, "http/connection_retry");
442 	tinytest_skip(testgroups, "http/https_connection_retry");
443 #endif
444 
445 #ifndef EVENT__DISABLE_THREAD_SUPPORT
446 	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
447 		evthread_enable_lock_debugging();
448 #endif
449 
450 	if (getenv("EVENT_DEBUG_MODE")) {
451 		event_enable_debug_mode();
452 		libevent_tests_running_in_debug_mode = 1;
453 	}
454 	if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
455 		event_enable_debug_logging(EVENT_DBG_ALL);
456 	}
457 
458 	tinytest_set_aliases(testaliases);
459 
460 	evutil_weakrand_seed_(&test_weakrand_state, 0);
461 
462 	if (tinytest_main(argc,argv,testgroups))
463 		return 1;
464 
465 	libevent_global_shutdown();
466 
467 	return 0;
468 }
469 
470