1 /* $NetBSD: regress_main.c,v 1.4 2021/04/07 03:36:48 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 /* move_pthread_to_realtime_scheduling_class() */
39 #ifdef EVENT__HAVE_MACH_MACH_H
40 #include <mach/mach.h>
41 #endif
42 #ifdef EVENT__HAVE_MACH_MACH_TIME_H
43 #include <mach/mach_time.h>
44 #endif
45
46 #if defined(__APPLE__) && defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
47 #if (__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1060 && \
48 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)
49 #define FORK_BREAKS_GCOV
50 #include <vproc.h>
51 #endif
52 #endif
53
54 #include "event2/event-config.h"
55 #include <sys/cdefs.h>
56 __RCSID("$NetBSD: regress_main.c,v 1.4 2021/04/07 03:36:48 christos Exp $");
57
58 #if 0
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #ifdef EVENT__HAVE_SYS_TIME_H
62 #include <sys/time.h>
63 #endif
64 #include <sys/queue.h>
65 #include <signal.h>
66 #include <errno.h>
67 #endif
68
69 #include <sys/types.h>
70 #ifdef EVENT__HAVE_SYS_STAT_H
71 #include <sys/stat.h>
72 #endif
73
74 #ifndef _WIN32
75 #include <sys/socket.h>
76 #include <sys/wait.h>
77 #include <signal.h>
78 #include <unistd.h>
79 #include <netdb.h>
80 #endif
81
82 #include <stdlib.h>
83 #include <stdio.h>
84 #include <string.h>
85 #include <assert.h>
86
87 #include "event2/util.h"
88 #include "event2/event.h"
89 #include "event2/event_compat.h"
90 #include "event2/dns.h"
91 #include "event2/dns_compat.h"
92 #include "event2/thread.h"
93
94 #include "event2/event-config.h"
95 #include <sys/cdefs.h>
96 #include "regress.h"
97 #include "regress_thread.h"
98 #include "tinytest.h"
99 #include "tinytest_macros.h"
100 #include "../iocp-internal.h"
101 #include "../event-internal.h"
102 #include "../evthread-internal.h"
103
104 struct evutil_weakrand_state test_weakrand_state;
105
106 long
timeval_msec_diff(const struct timeval * start,const struct timeval * end)107 timeval_msec_diff(const struct timeval *start, const struct timeval *end)
108 {
109 long ms = end->tv_sec - start->tv_sec;
110 ms *= 1000;
111 ms += ((end->tv_usec - start->tv_usec)+500) / 1000;
112 return ms;
113 }
114
115 /* ============================================================ */
116 /* Code to wrap up old legacy test cases that used setup() and cleanup().
117 *
118 * Not all of the tests designated "legacy" are ones that used setup() and
119 * cleanup(), of course. A test is legacy it it uses setup()/cleanup(), OR
120 * if it wants to find its event base/socketpair in global variables (ugh),
121 * OR if it wants to communicate success/failure through test_ok.
122 */
123
124 /* This is set to true if we're inside a legacy test wrapper. It lets the
125 setup() and cleanup() functions in regress.c know they're not needed.
126 */
127 int in_legacy_test_wrapper = 0;
128
dnslogcb(int w,const char * m)129 static void dnslogcb(int w, const char *m)
130 {
131 TT_BLATHER(("%s", m));
132 }
133
134 /* creates a temporary file with the data in it. If *filename_out gets set,
135 * the caller should try to unlink it. */
136 int
regress_make_tmpfile(const void * data,size_t datalen,char ** filename_out)137 regress_make_tmpfile(const void *data, size_t datalen, char **filename_out)
138 {
139 #ifndef _WIN32
140 char tmpfilename[32];
141 int fd;
142 *filename_out = NULL;
143 strcpy(tmpfilename, "/tmp/eventtmp.XXXXXX");
144 #ifdef EVENT__HAVE_UMASK
145 umask(0077);
146 #endif
147 fd = mkstemp(tmpfilename);
148 if (fd == -1)
149 return (-1);
150 if (write(fd, data, datalen) != (int)datalen) {
151 close(fd);
152 return (-1);
153 }
154 lseek(fd, 0, SEEK_SET);
155 /* remove it from the file system */
156 unlink(tmpfilename);
157 return (fd);
158 #else
159 /* XXXX actually delete the file later */
160 char tmpfilepath[MAX_PATH];
161 char tmpfilename[MAX_PATH];
162 DWORD r, written;
163 int tries = 16;
164 HANDLE h;
165 r = GetTempPathA(MAX_PATH, tmpfilepath);
166 if (r > MAX_PATH || r == 0)
167 return (-1);
168 for (; tries > 0; --tries) {
169 r = GetTempFileNameA(tmpfilepath, "LIBEVENT", 0, tmpfilename);
170 if (r == 0)
171 return (-1);
172 h = CreateFileA(tmpfilename, GENERIC_READ|GENERIC_WRITE,
173 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
174 if (h != INVALID_HANDLE_VALUE)
175 break;
176 }
177 if (tries == 0)
178 return (-1);
179 written = 0;
180 *filename_out = strdup(tmpfilename);
181 WriteFile(h, data, (DWORD)datalen, &written, NULL);
182 /* Closing the fd returned by this function will indeed close h. */
183 return _open_osfhandle((intptr_t)h,_O_RDONLY);
184 #endif
185 }
186
187 #ifndef _WIN32
188 pid_t
regress_fork(void)189 regress_fork(void)
190 {
191 pid_t pid = fork();
192 #ifdef FORK_BREAKS_GCOV
193 vproc_transaction_begin(0);
194 #endif
195 return pid;
196 }
197 #endif
198
199 static void
ignore_log_cb(int s,const char * msg)200 ignore_log_cb(int s, const char *msg)
201 {
202 }
203
204 /**
205 * Put into the real time scheduling class for better timers latency.
206 * https://developer.apple.com/library/archive/technotes/tn2169/_index.html#//apple_ref/doc/uid/DTS40013172-CH1-TNTAG6000
207 */
208 #if defined(__APPLE__)
move_pthread_to_realtime_scheduling_class(pthread_t pthread)209 static void move_pthread_to_realtime_scheduling_class(pthread_t pthread)
210 {
211 mach_timebase_info_data_t info;
212 mach_timebase_info(&info);
213
214 const uint64_t NANOS_PER_MSEC = 1000000ULL;
215 double clock2abs =
216 ((double)info.denom / (double)info.numer) * NANOS_PER_MSEC;
217
218 thread_time_constraint_policy_data_t policy;
219 policy.period = 0;
220 policy.computation = (uint32_t)(5 * clock2abs); // 5 ms of work
221 policy.constraint = (uint32_t)(10 * clock2abs);
222 policy.preemptible = FALSE;
223
224 int kr = thread_policy_set(pthread_mach_thread_np(pthread),
225 THREAD_TIME_CONSTRAINT_POLICY,
226 (thread_policy_t)&policy,
227 THREAD_TIME_CONSTRAINT_POLICY_COUNT);
228 if (kr != KERN_SUCCESS) {
229 mach_error("thread_policy_set:", kr);
230 exit(1);
231 }
232 }
233
thread_setup(THREAD_T pthread)234 void thread_setup(THREAD_T pthread)
235 {
236 move_pthread_to_realtime_scheduling_class(pthread);
237 }
238 #else /** \__APPLE__ */
thread_setup(THREAD_T pthread)239 void thread_setup(THREAD_T pthread) {}
240 #endif /** \!__APPLE__ */
241
242
243 void *
basic_test_setup(const struct testcase_t * testcase)244 basic_test_setup(const struct testcase_t *testcase)
245 {
246 struct event_base *base = NULL;
247 evutil_socket_t spair[2] = { -1, -1 };
248 struct basic_test_data *data = NULL;
249
250 thread_setup(THREAD_SELF());
251
252 #ifndef _WIN32
253 if (testcase->flags & TT_ENABLE_IOCP_FLAG)
254 return (void*)TT_SKIP;
255 #endif
256
257 if (testcase->flags & TT_ENABLE_DEBUG_MODE &&
258 !libevent_tests_running_in_debug_mode) {
259 event_enable_debug_mode();
260 libevent_tests_running_in_debug_mode = 1;
261 }
262
263 if (testcase->flags & TT_NEED_THREADS) {
264 if (!(testcase->flags & TT_FORK))
265 return NULL;
266 #if defined(EVTHREAD_USE_PTHREADS_IMPLEMENTED)
267 if (evthread_use_pthreads())
268 exit(1);
269 #elif defined(EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED)
270 if (evthread_use_windows_threads())
271 exit(1);
272 #else
273 return (void*)TT_SKIP;
274 #endif
275 }
276
277 if (testcase->flags & TT_NEED_SOCKETPAIR) {
278 if (evutil_socketpair(AF_UNIX, SOCK_STREAM, 0, spair) == -1) {
279 fprintf(stderr, "%s: socketpair\n", __func__);
280 exit(1);
281 }
282
283 if (evutil_make_socket_nonblocking(spair[0]) == -1) {
284 fprintf(stderr, "fcntl(O_NONBLOCK)");
285 exit(1);
286 }
287
288 if (evutil_make_socket_nonblocking(spair[1]) == -1) {
289 fprintf(stderr, "fcntl(O_NONBLOCK)");
290 exit(1);
291 }
292 }
293 if (testcase->flags & TT_NEED_BASE) {
294 if (testcase->flags & TT_LEGACY)
295 base = event_init();
296 else
297 base = event_base_new();
298 if (!base)
299 exit(1);
300 }
301 if (testcase->flags & TT_ENABLE_IOCP_FLAG) {
302 if (event_base_start_iocp_(base, 0)<0) {
303 event_base_free(base);
304 return (void*)TT_SKIP;
305 }
306 }
307
308 if (testcase->flags & TT_NEED_DNS) {
309 evdns_set_log_fn(dnslogcb);
310 if (evdns_init())
311 return NULL; /* fast failure */ /*XXX asserts. */
312 }
313
314 if (testcase->flags & TT_NO_LOGS)
315 event_set_log_callback(ignore_log_cb);
316
317 data = calloc(1, sizeof(*data));
318 if (!data)
319 exit(1);
320 data->base = base;
321 data->pair[0] = spair[0];
322 data->pair[1] = spair[1];
323 data->setup_data = testcase->setup_data;
324 return data;
325 }
326
327 int
basic_test_cleanup(const struct testcase_t * testcase,void * ptr)328 basic_test_cleanup(const struct testcase_t *testcase, void *ptr)
329 {
330 struct basic_test_data *data = ptr;
331
332 if (testcase->flags & TT_NO_LOGS)
333 event_set_log_callback(NULL);
334
335 if (testcase->flags & TT_NEED_SOCKETPAIR) {
336 if (data->pair[0] != -1)
337 evutil_closesocket(data->pair[0]);
338 if (data->pair[1] != -1)
339 evutil_closesocket(data->pair[1]);
340 }
341
342 if (testcase->flags & TT_NEED_DNS) {
343 evdns_shutdown(0);
344 }
345
346 if (testcase->flags & TT_NEED_BASE) {
347 if (data->base) {
348 event_base_assert_ok_(data->base);
349 event_base_free(data->base);
350 }
351 }
352
353 if (testcase->flags & TT_FORK)
354 libevent_global_shutdown();
355
356 free(data);
357
358 return 1;
359 }
360
361 const struct testcase_setup_t basic_setup = {
362 basic_test_setup, basic_test_cleanup
363 };
364
365 /* The "data" for a legacy test is just a pointer to the void fn(void)
366 function implementing the test case. We need to set up some globals,
367 though, since that's where legacy tests expect to find a socketpair
368 (sometimes) and a global event_base (sometimes).
369 */
370 static void *
legacy_test_setup(const struct testcase_t * testcase)371 legacy_test_setup(const struct testcase_t *testcase)
372 {
373 struct basic_test_data *data = basic_test_setup(testcase);
374 if (data == (void*)TT_SKIP || data == NULL)
375 return data;
376 global_base = data->base;
377 pair[0] = data->pair[0];
378 pair[1] = data->pair[1];
379 data->legacy_test_fn = testcase->setup_data;
380 return data;
381 }
382
383 /* This function is the implementation of every legacy test case. It
384 sets test_ok to 0, invokes the test function, and tells tinytest that
385 the test failed if the test didn't set test_ok to 1.
386 */
387 void
run_legacy_test_fn(void * ptr)388 run_legacy_test_fn(void *ptr)
389 {
390 struct basic_test_data *data = ptr;
391 test_ok = called = 0;
392
393 in_legacy_test_wrapper = 1;
394 data->legacy_test_fn(); /* This part actually calls the test */
395 in_legacy_test_wrapper = 0;
396
397 if (!test_ok)
398 tt_abort_msg("Legacy unit test failed");
399
400 end:
401 test_ok = 0;
402 }
403
404 /* This function doesn't have to clean up ptr (which is just a pointer
405 to the test function), but it may need to close the socketpair or
406 free the event_base.
407 */
408 static int
legacy_test_cleanup(const struct testcase_t * testcase,void * ptr)409 legacy_test_cleanup(const struct testcase_t *testcase, void *ptr)
410 {
411 int r = basic_test_cleanup(testcase, ptr);
412 pair[0] = pair[1] = -1;
413 global_base = NULL;
414 return r;
415 }
416
417 const struct testcase_setup_t legacy_setup = {
418 legacy_test_setup, legacy_test_cleanup
419 };
420
421 /* ============================================================ */
422
423 #if (!defined(EVENT__HAVE_PTHREADS) && !defined(_WIN32)) || defined(EVENT__DISABLE_THREAD_SUPPORT)
424 struct testcase_t thread_testcases[] = {
425 { "basic", NULL, TT_SKIP, NULL, NULL },
426 END_OF_TESTCASES
427 };
428 #endif
429
430 struct testgroup_t testgroups[] = {
431 { "main/", main_testcases },
432 { "heap/", minheap_testcases },
433 { "et/", edgetriggered_testcases },
434 { "finalize/", finalize_testcases },
435 { "evbuffer/", evbuffer_testcases },
436 { "signal/", signal_testcases },
437 { "util/", util_testcases },
438 { "bufferevent/", bufferevent_testcases },
439 { "http/", http_testcases },
440 { "dns/", dns_testcases },
441 { "evtag/", evtag_testcases },
442 { "rpc/", rpc_testcases },
443 { "thread/", thread_testcases },
444 { "listener/", listener_testcases },
445 #ifdef _WIN32
446 { "iocp/", iocp_testcases },
447 { "iocp/bufferevent/", bufferevent_iocp_testcases },
448 { "iocp/listener/", listener_iocp_testcases },
449 { "iocp/http/", http_iocp_testcases },
450 #endif
451 #ifdef EVENT__HAVE_OPENSSL
452 { "ssl/", ssl_testcases },
453 #endif
454 END_OF_GROUPS
455 };
456
457 const char *alltests[] = { "+..", NULL };
458 const char *livenettests[] = {
459 "+util/getaddrinfo_live",
460 "+dns/gethostby..",
461 "+dns/resolve_reverse",
462 NULL
463 };
464 const char *finetimetests[] = {
465 "+util/monotonic_res_precise",
466 "+util/monotonic_res_fallback",
467 "+thread/deferred_cb_skew",
468 "+http/connection_retry",
469 "+http/https_connection_retry",
470 NULL
471 };
472 struct testlist_alias_t testaliases[] = {
473 { "all", alltests },
474 { "live_net", livenettests },
475 { "fine_timing", finetimetests },
476 END_OF_ALIASES
477 };
478
479 int libevent_tests_running_in_debug_mode = 0;
480
481 int
main(int argc,const char ** argv)482 main(int argc, const char **argv)
483 {
484 #ifdef _WIN32
485 WORD wVersionRequested;
486 WSADATA wsaData;
487
488 wVersionRequested = MAKEWORD(2, 2);
489
490 (void) WSAStartup(wVersionRequested, &wsaData);
491 #endif
492
493 #ifndef _WIN32
494 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
495 return 1;
496 #endif
497
498 #ifdef _WIN32
499 tinytest_skip(testgroups, "http/connection_retry");
500 tinytest_skip(testgroups, "http/https_connection_retry");
501 tinytest_skip(testgroups, "http/read_on_write_error");
502 #endif
503
504 #ifndef EVENT__DISABLE_THREAD_SUPPORT
505 if (!getenv("EVENT_NO_DEBUG_LOCKS"))
506 evthread_enable_lock_debugging();
507 #endif
508
509 if (getenv("EVENT_DEBUG_MODE")) {
510 event_enable_debug_mode();
511 libevent_tests_running_in_debug_mode = 1;
512 }
513 if (getenv("EVENT_DEBUG_LOGGING_ALL")) {
514 event_enable_debug_logging(EVENT_DBG_ALL);
515 }
516
517 tinytest_set_aliases(testaliases);
518
519 evutil_weakrand_seed_(&test_weakrand_state, 0);
520
521 if (getenv("EVENT_NO_FILE_BUFFERING")) {
522 setbuf(stdout, NULL);
523 setbuf(stderr, NULL);
524 }
525
526 if (tinytest_main(argc,argv,testgroups))
527 return 1;
528
529 libevent_global_shutdown();
530
531 return 0;
532 }
533
534