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