xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_util.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: regress_util.c,v 1.1.1.1 2013/12/27 23:31:29 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
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 <ws2tcpip.h>
34 #endif
35 
36 #include "event2/event-config.h"
37 
38 #include <sys/types.h>
39 
40 #ifndef _WIN32
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <unistd.h>
45 #endif
46 #ifdef EVENT__HAVE_NETINET_IN6_H
47 #include <netinet/in6.h>
48 #endif
49 #ifdef EVENT__HAVE_SYS_WAIT_H
50 #include <sys/wait.h>
51 #endif
52 #include <signal.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include "event2/event.h"
58 #include "event2/util.h"
59 #include "../ipv6-internal.h"
60 #include "../log-internal.h"
61 #include "../strlcpy-internal.h"
62 #include "../mm-internal.h"
63 #include "../time-internal.h"
64 
65 #include "regress.h"
66 
67 enum entry_status { NORMAL, CANONICAL, BAD };
68 
69 /* This is a big table of results we expect from generating and parsing */
70 static struct ipv4_entry {
71 	const char *addr;
72 	ev_uint32_t res;
73 	enum entry_status status;
74 } ipv4_entries[] = {
75 	{ "1.2.3.4", 0x01020304u, CANONICAL },
76 	{ "255.255.255.255", 0xffffffffu, CANONICAL },
77 	{ "256.0.0.0", 0, BAD },
78 	{ "ABC", 0, BAD },
79 	{ "1.2.3.4.5", 0, BAD },
80 	{ "176.192.208.244", 0xb0c0d0f4, CANONICAL },
81 	{ NULL, 0, BAD },
82 };
83 
84 static struct ipv6_entry {
85 	const char *addr;
86 	ev_uint32_t res[4];
87 	enum entry_status status;
88 } ipv6_entries[] = {
89 	{ "::", { 0, 0, 0, 0, }, CANONICAL },
90 	{ "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
91 	{ "::1", { 0, 0, 0, 1, }, CANONICAL },
92 	{ "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
93 	{ "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
94 	{ "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
95 	{ "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
96 	{ "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
97 	{ "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
98 	{ "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
99 	{ "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
100 	{ "foobar.", { 0, 0, 0, 0 }, BAD },
101 	{ "foobar", { 0, 0, 0, 0 }, BAD },
102 	{ "fo:obar", { 0, 0, 0, 0 }, BAD },
103 	{ "ffff", { 0, 0, 0, 0 }, BAD },
104 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
105 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
106 	{ "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
107 	{ "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
108 	{ "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
109 	{ "1::2::3", { 0, 0, 0, 0 }, BAD },
110 	{ ":::1", { 0, 0, 0, 0 }, BAD },
111 	{ NULL, { 0, 0, 0, 0,  }, BAD },
112 };
113 
114 static void
115 regress_ipv4_parse(void *ptr)
116 {
117 	int i;
118 	for (i = 0; ipv4_entries[i].addr; ++i) {
119 		char written[128];
120 		struct ipv4_entry *ent = &ipv4_entries[i];
121 		struct in_addr in;
122 		int r;
123 		r = evutil_inet_pton(AF_INET, ent->addr, &in);
124 		if (r == 0) {
125 			if (ent->status != BAD) {
126 				TT_FAIL(("%s did not parse, but it's a good address!",
127 					ent->addr));
128 			}
129 			continue;
130 		}
131 		if (ent->status == BAD) {
132 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
133 			continue;
134 		}
135 		if (ntohl(in.s_addr) != ent->res) {
136 			TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
137 				(unsigned long)ntohl(in.s_addr),
138 				(unsigned long)ent->res));
139 			continue;
140 		}
141 		if (ent->status == CANONICAL) {
142 			const char *w = evutil_inet_ntop(AF_INET, &in, written,
143 											 sizeof(written));
144 			if (!w) {
145 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
146 				continue;
147 			}
148 			if (strcmp(written, ent->addr)) {
149 				TT_FAIL(("Tried to write out %s; got %s",
150 					ent->addr, written));
151 				continue;
152 			}
153 		}
154 
155 	}
156 
157 }
158 
159 static void
160 regress_ipv6_parse(void *ptr)
161 {
162 #ifdef AF_INET6
163 	int i, j;
164 
165 	for (i = 0; ipv6_entries[i].addr; ++i) {
166 		char written[128];
167 		struct ipv6_entry *ent = &ipv6_entries[i];
168 		struct in6_addr in6;
169 		int r;
170 		r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
171 		if (r == 0) {
172 			if (ent->status != BAD)
173 				TT_FAIL(("%s did not parse, but it's a good address!",
174 					ent->addr));
175 			continue;
176 		}
177 		if (ent->status == BAD) {
178 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
179 			continue;
180 		}
181 		for (j = 0; j < 4; ++j) {
182 			/* Can't use s6_addr32 here; some don't have it. */
183 			ev_uint32_t u =
184 				(in6.s6_addr[j*4  ] << 24) |
185 				(in6.s6_addr[j*4+1] << 16) |
186 				(in6.s6_addr[j*4+2] << 8) |
187 				(in6.s6_addr[j*4+3]);
188 			if (u != ent->res[j]) {
189 				TT_FAIL(("%s did not parse as expected.", ent->addr));
190 				continue;
191 			}
192 		}
193 		if (ent->status == CANONICAL) {
194 			const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
195 											 sizeof(written));
196 			if (!w) {
197 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
198 				continue;
199 			}
200 			if (strcmp(written, ent->addr)) {
201 				TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
202 				continue;
203 			}
204 		}
205 
206 	}
207 #else
208 	TT_BLATHER(("Skipping IPv6 address parsing."));
209 #endif
210 }
211 
212 static struct sa_port_ent {
213 	const char *parse;
214 	int safamily;
215 	const char *addr;
216 	int port;
217 } sa_port_ents[] = {
218 	{ "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
219 	{ "[ffff::1]", AF_INET6, "ffff::1", 0 },
220 	{ "[ffff::1", 0, NULL, 0 },
221 	{ "[ffff::1]:65599", 0, NULL, 0 },
222 	{ "[ffff::1]:0", 0, NULL, 0 },
223 	{ "[ffff::1]:-1", 0, NULL, 0 },
224 	{ "::1", AF_INET6, "::1", 0 },
225 	{ "1:2::1", AF_INET6, "1:2::1", 0 },
226 	{ "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
227 	{ "1.2.3.4", AF_INET, "1.2.3.4", 0 },
228 	{ NULL, 0, NULL, 0 },
229 };
230 
231 static void
232 regress_sockaddr_port_parse(void *ptr)
233 {
234 	struct sockaddr_storage ss;
235 	int i, r;
236 
237 	for (i = 0; sa_port_ents[i].parse; ++i) {
238 		struct sa_port_ent *ent = &sa_port_ents[i];
239 		int len = sizeof(ss);
240 		memset(&ss, 0, sizeof(ss));
241 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
242 		if (r < 0) {
243 			if (ent->safamily)
244 				TT_FAIL(("Couldn't parse %s!", ent->parse));
245 			continue;
246 		} else if (! ent->safamily) {
247 			TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
248 			continue;
249 		}
250 		if (ent->safamily == AF_INET) {
251 			struct sockaddr_in sin;
252 			memset(&sin, 0, sizeof(sin));
253 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
254 			sin.sin_len = sizeof(sin);
255 #endif
256 			sin.sin_family = AF_INET;
257 			sin.sin_port = htons(ent->port);
258 			r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
259 			if (1 != r) {
260 				TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
261 			} else if (memcmp(&sin, &ss, sizeof(sin))) {
262 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
263 			} else if (len != sizeof(sin)) {
264 				TT_FAIL(("Length for %s not as expected.",ent->parse));
265 			}
266 		} else {
267 			struct sockaddr_in6 sin6;
268 			memset(&sin6, 0, sizeof(sin6));
269 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
270 			sin6.sin6_len = sizeof(sin6);
271 #endif
272 			sin6.sin6_family = AF_INET6;
273 			sin6.sin6_port = htons(ent->port);
274 			r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
275 			if (1 != r) {
276 				TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
277 			} else if (memcmp(&sin6, &ss, sizeof(sin6))) {
278 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
279 			} else if (len != sizeof(sin6)) {
280 				TT_FAIL(("Length for %s not as expected.",ent->parse));
281 			}
282 		}
283 	}
284 }
285 
286 
287 static void
288 regress_sockaddr_port_format(void *ptr)
289 {
290 	struct sockaddr_storage ss;
291 	int len;
292 	const char *cp;
293 	char cbuf[128];
294 	int r;
295 
296 	len = sizeof(ss);
297 	r = evutil_parse_sockaddr_port("192.168.1.1:80",
298 	    (struct sockaddr*)&ss, &len);
299 	tt_int_op(r,==,0);
300 	cp = evutil_format_sockaddr_port_(
301 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
302 	tt_ptr_op(cp,==,cbuf);
303 	tt_str_op(cp,==,"192.168.1.1:80");
304 
305 	len = sizeof(ss);
306 	r = evutil_parse_sockaddr_port("[ff00::8010]:999",
307 	    (struct sockaddr*)&ss, &len);
308 	tt_int_op(r,==,0);
309 	cp = evutil_format_sockaddr_port_(
310 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
311 	tt_ptr_op(cp,==,cbuf);
312 	tt_str_op(cp,==,"[ff00::8010]:999");
313 
314 	ss.ss_family=99;
315 	cp = evutil_format_sockaddr_port_(
316 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
317 	tt_ptr_op(cp,==,cbuf);
318 	tt_str_op(cp,==,"<addr with socktype 99>");
319 end:
320 	;
321 }
322 
323 static struct sa_pred_ent {
324 	const char *parse;
325 
326 	int is_loopback;
327 } sa_pred_entries[] = {
328 	{ "127.0.0.1",	 1 },
329 	{ "127.0.3.2",	 1 },
330 	{ "128.1.2.3",	 0 },
331 	{ "18.0.0.1",	 0 },
332 	{ "129.168.1.1", 0 },
333 
334 	{ "::1",	 1 },
335 	{ "::0",	 0 },
336 	{ "f::1",	 0 },
337 	{ "::501",	 0 },
338 	{ NULL,		 0 },
339 
340 };
341 
342 static void
343 test_evutil_sockaddr_predicates(void *ptr)
344 {
345 	struct sockaddr_storage ss;
346 	int r, i;
347 
348 	for (i=0; sa_pred_entries[i].parse; ++i) {
349 		struct sa_pred_ent *ent = &sa_pred_entries[i];
350 		int len = sizeof(ss);
351 
352 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
353 
354 		if (r<0) {
355 			TT_FAIL(("Couldn't parse %s!", ent->parse));
356 			continue;
357 		}
358 
359 		/* sockaddr_is_loopback */
360 		if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
361 			TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
362 				ent->parse));
363 		}
364 	}
365 }
366 
367 static void
368 test_evutil_strtoll(void *ptr)
369 {
370 	const char *s;
371 	char *endptr;
372 
373 	tt_want(evutil_strtoll("5000000000", NULL, 10) ==
374 		((ev_int64_t)5000000)*1000);
375 	tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
376 		((ev_int64_t)5000000)*-1000);
377 	s = " 99999stuff";
378 	tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
379 	tt_want(endptr == s+6);
380 	tt_want(evutil_strtoll("foo", NULL, 10) == 0);
381  }
382 
383 static void
384 test_evutil_snprintf(void *ptr)
385 {
386 	char buf[16];
387 	int r;
388 	ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
389 	ev_int64_t i64 = -1 * (ev_int64_t) u64;
390 	size_t size = 8000;
391 	ev_ssize_t ssize = -9000;
392 
393 	r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
394 	tt_str_op(buf, ==, "50 100");
395 	tt_int_op(r, ==, 6);
396 
397 	r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
398 	tt_str_op(buf, ==, "longish 1234567");
399 	tt_int_op(r, ==, 18);
400 
401 	r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
402 	tt_str_op(buf, ==, "200000000000");
403 	tt_int_op(r, ==, 12);
404 
405 	r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
406 	tt_str_op(buf, ==, "-200000000000");
407 	tt_int_op(r, ==, 13);
408 
409 	r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
410 	    EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
411 	tt_str_op(buf, ==, "8000 -9000");
412 	tt_int_op(r, ==, 10);
413 
414       end:
415 	;
416 }
417 
418 static void
419 test_evutil_casecmp(void *ptr)
420 {
421 	tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
422 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
423 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
424 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
425 	tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
426 
427 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
428 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
429 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
430 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
431 	tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
432 	tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
433 	tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
434 	tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
435 end:
436 	;
437 }
438 
439 static void
440 test_evutil_rtrim(void *ptr)
441 {
442 #define TEST_TRIM(s, result) \
443 	do {						\
444 	    if (cp) mm_free(cp);			\
445 	    cp = mm_strdup(s);				\
446 	    evutil_rtrim_lws_(cp);			\
447 	    tt_str_op(cp, ==, result);			\
448 	} while(0)
449 
450 	char *cp = NULL;
451 	(void) ptr;
452 
453 	TEST_TRIM("", "");
454 	TEST_TRIM("a", "a");
455 	TEST_TRIM("abcdef ghi", "abcdef ghi");
456 
457 	TEST_TRIM(" ", "");
458 	TEST_TRIM("  ", "");
459 	TEST_TRIM("a ", "a");
460 	TEST_TRIM("abcdef  gH       ", "abcdef  gH");
461 
462 	TEST_TRIM("\t\t", "");
463 	TEST_TRIM(" \t", "");
464 	TEST_TRIM("\t", "");
465 	TEST_TRIM("a \t", "a");
466 	TEST_TRIM("a\t ", "a");
467 	TEST_TRIM("a\t", "a");
468 	TEST_TRIM("abcdef  gH    \t  ", "abcdef  gH");
469 
470 end:
471 	if (cp)
472 		mm_free(cp);
473 }
474 
475 static int logsev = 0;
476 static char *logmsg = NULL;
477 
478 static void
479 logfn(int severity, const char *msg)
480 {
481 	logsev = severity;
482 	tt_want(msg);
483 	if (msg) {
484 		if (logmsg)
485 			free(logmsg);
486 		logmsg = strdup(msg);
487 	}
488 }
489 
490 static int fatal_want_severity = 0;
491 static const char *fatal_want_message = NULL;
492 static void
493 fatalfn(int exitcode)
494 {
495 	if (logsev != fatal_want_severity ||
496 	    !logmsg ||
497 	    strcmp(logmsg, fatal_want_message))
498 		exit(0);
499 	else
500 		exit(exitcode);
501 }
502 
503 #ifndef _WIN32
504 #define CAN_CHECK_ERR
505 static void
506 check_error_logging(void (*fn)(void), int wantexitcode,
507     int wantseverity, const char *wantmsg)
508 {
509 	pid_t pid;
510 	int status = 0, exitcode;
511 	fatal_want_severity = wantseverity;
512 	fatal_want_message = wantmsg;
513 	if ((pid = regress_fork()) == 0) {
514 		/* child process */
515 		fn();
516 		exit(0); /* should be unreachable. */
517 	} else {
518 		wait(&status);
519 		exitcode = WEXITSTATUS(status);
520 		tt_int_op(wantexitcode, ==, exitcode);
521 	}
522 end:
523 	;
524 }
525 
526 static void
527 errx_fn(void)
528 {
529 	event_errx(2, "Fatal error; too many kumquats (%d)", 5);
530 }
531 
532 static void
533 err_fn(void)
534 {
535 	errno = ENOENT;
536 	event_err(5,"Couldn't open %s", "/very/bad/file");
537 }
538 
539 static void
540 sock_err_fn(void)
541 {
542 	evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
543 #ifdef _WIN32
544 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
545 #else
546 	errno = EAGAIN;
547 #endif
548 	event_sock_err(20, fd, "Unhappy socket");
549 }
550 #endif
551 
552 static void
553 test_evutil_log(void *ptr)
554 {
555 	evutil_socket_t fd = -1;
556 	char buf[128];
557 
558 	event_set_log_callback(logfn);
559 	event_set_fatal_callback(fatalfn);
560 #define RESET() do {				\
561 		logsev = 0;	\
562 		if (logmsg) free(logmsg);	\
563 		logmsg = NULL;			\
564 	} while (0)
565 #define LOGEQ(sev,msg) do {			\
566 		tt_int_op(logsev,==,sev);	\
567 		tt_assert(logmsg != NULL);	\
568 		tt_str_op(logmsg,==,msg);	\
569 	} while (0)
570 
571 #ifdef CAN_CHECK_ERR
572 	/* We need to disable these tests for now.  Previously, the logging
573 	 * module didn't enforce the requirement that a fatal callback
574 	 * actually exit.  Now, it exits no matter what, so if we wan to
575 	 * reinstate these tests, we'll need to fork for each one. */
576 	check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
577 	    "Fatal error; too many kumquats (5)");
578 	RESET();
579 #endif
580 
581 	event_warnx("Far too many %s (%d)", "wombats", 99);
582 	LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
583 	RESET();
584 
585 	event_msgx("Connecting lime to coconut");
586 	LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
587 	RESET();
588 
589 	event_debug(("A millisecond passed! We should log that!"));
590 #ifdef USE_DEBUG
591 	LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
592 #else
593 	tt_int_op(logsev,==,0);
594 	tt_ptr_op(logmsg,==,NULL);
595 #endif
596 	RESET();
597 
598 	/* Try with an errno. */
599 	errno = ENOENT;
600 	event_warn("Couldn't open %s", "/bad/file");
601 	evutil_snprintf(buf, sizeof(buf),
602 	    "Couldn't open /bad/file: %s",strerror(ENOENT));
603 	LOGEQ(EVENT_LOG_WARN,buf);
604 	RESET();
605 
606 #ifdef CAN_CHECK_ERR
607 	evutil_snprintf(buf, sizeof(buf),
608 	    "Couldn't open /very/bad/file: %s",strerror(ENOENT));
609 	check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
610 	RESET();
611 #endif
612 
613 	/* Try with a socket errno. */
614 	fd = socket(AF_INET, SOCK_STREAM, 0);
615 #ifdef _WIN32
616 	evutil_snprintf(buf, sizeof(buf),
617 	    "Unhappy socket: %s",
618 	    evutil_socket_error_to_string(WSAEWOULDBLOCK));
619 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
620 #else
621 	evutil_snprintf(buf, sizeof(buf),
622 	    "Unhappy socket: %s", strerror(EAGAIN));
623 	errno = EAGAIN;
624 #endif
625 	event_sock_warn(fd, "Unhappy socket");
626 	LOGEQ(EVENT_LOG_WARN, buf);
627 	RESET();
628 
629 #ifdef CAN_CHECK_ERR
630 	check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
631 	RESET();
632 #endif
633 
634 #undef RESET
635 #undef LOGEQ
636 end:
637 	if (logmsg)
638 		free(logmsg);
639 	if (fd >= 0)
640 		evutil_closesocket(fd);
641 }
642 
643 static void
644 test_evutil_strlcpy(void *arg)
645 {
646 	char buf[8];
647 
648 	/* Successful case. */
649 	tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
650 	tt_str_op(buf, ==, "Hello");
651 
652 	/* Overflow by a lot. */
653 	tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
654 	tt_str_op(buf, ==, "pentasy");
655 
656 	/* Overflow by exactly one. */
657 	tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
658 	tt_str_op(buf, ==, "overlon");
659 end:
660 	;
661 }
662 
663 struct example_struct {
664 	const char *a;
665 	const char *b;
666 	long c;
667 };
668 
669 static void
670 test_evutil_upcast(void *arg)
671 {
672 	struct example_struct es1;
673 	const char **cp;
674 	es1.a = "World";
675 	es1.b = "Hello";
676 	es1.c = -99;
677 
678 	tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
679 
680 	cp = &es1.b;
681 	tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
682 
683 end:
684 	;
685 }
686 
687 static void
688 test_evutil_integers(void *arg)
689 {
690 	ev_int64_t i64;
691 	ev_uint64_t u64;
692 	ev_int32_t i32;
693 	ev_uint32_t u32;
694 	ev_int16_t i16;
695 	ev_uint16_t u16;
696 	ev_int8_t  i8;
697 	ev_uint8_t  u8;
698 
699 	void *ptr;
700 	ev_intptr_t iptr;
701 	ev_uintptr_t uptr;
702 
703 	ev_ssize_t ssize;
704 
705 	tt_int_op(sizeof(u64), ==, 8);
706 	tt_int_op(sizeof(i64), ==, 8);
707 	tt_int_op(sizeof(u32), ==, 4);
708 	tt_int_op(sizeof(i32), ==, 4);
709 	tt_int_op(sizeof(u16), ==, 2);
710 	tt_int_op(sizeof(i16), ==, 2);
711 	tt_int_op(sizeof(u8), ==,  1);
712 	tt_int_op(sizeof(i8), ==,  1);
713 
714 	tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
715 	tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
716 	tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
717 
718 	u64 = 1000000000;
719 	u64 *= 1000000000;
720 	tt_assert(u64 / 1000000000 == 1000000000);
721 	i64 = -1000000000;
722 	i64 *= 1000000000;
723 	tt_assert(i64 / 1000000000 == -1000000000);
724 
725 	u64 = EV_UINT64_MAX;
726 	i64 = EV_INT64_MAX;
727 	tt_assert(u64 > 0);
728 	tt_assert(i64 > 0);
729 	u64++;
730 	i64++;
731 	tt_assert(u64 == 0);
732 	tt_assert(i64 == EV_INT64_MIN);
733 	tt_assert(i64 < 0);
734 
735 	u32 = EV_UINT32_MAX;
736 	i32 = EV_INT32_MAX;
737 	tt_assert(u32 > 0);
738 	tt_assert(i32 > 0);
739 	u32++;
740 	i32++;
741 	tt_assert(u32 == 0);
742 	tt_assert(i32 == EV_INT32_MIN);
743 	tt_assert(i32 < 0);
744 
745 	u16 = EV_UINT16_MAX;
746 	i16 = EV_INT16_MAX;
747 	tt_assert(u16 > 0);
748 	tt_assert(i16 > 0);
749 	u16++;
750 	i16++;
751 	tt_assert(u16 == 0);
752 	tt_assert(i16 == EV_INT16_MIN);
753 	tt_assert(i16 < 0);
754 
755 	u8 = EV_UINT8_MAX;
756 	i8 = EV_INT8_MAX;
757 	tt_assert(u8 > 0);
758 	tt_assert(i8 > 0);
759 	u8++;
760 	i8++;
761 	tt_assert(u8 == 0);
762 	tt_assert(i8 == EV_INT8_MIN);
763 	tt_assert(i8 < 0);
764 
765 	ssize = EV_SSIZE_MAX;
766 	tt_assert(ssize > 0);
767 	ssize++;
768 	tt_assert(ssize < 0);
769 	tt_assert(ssize == EV_SSIZE_MIN);
770 
771 	ptr = &ssize;
772 	iptr = (ev_intptr_t)ptr;
773 	uptr = (ev_uintptr_t)ptr;
774 	ptr = (void *)iptr;
775 	tt_assert(ptr == &ssize);
776 	ptr = (void *)uptr;
777 	tt_assert(ptr == &ssize);
778 
779 	iptr = -1;
780 	tt_assert(iptr < 0);
781 end:
782 	;
783 }
784 
785 struct evutil_addrinfo *
786 ai_find_by_family(struct evutil_addrinfo *ai, int family)
787 {
788 	while (ai) {
789 		if (ai->ai_family == family)
790 			return ai;
791 		ai = ai->ai_next;
792 	}
793 	return NULL;
794 }
795 
796 struct evutil_addrinfo *
797 ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
798 {
799 	while (ai) {
800 		if (ai->ai_protocol == protocol)
801 			return ai;
802 		ai = ai->ai_next;
803 	}
804 	return NULL;
805 }
806 
807 
808 int
809 test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
810     int socktype, int protocol, int line)
811 {
812 	struct sockaddr_storage ss;
813 	int slen = sizeof(ss);
814 	int gotport;
815 	char buf[128];
816 	memset(&ss, 0, sizeof(ss));
817 	if (socktype > 0)
818 		tt_int_op(ai->ai_socktype, ==, socktype);
819 	if (protocol > 0)
820 		tt_int_op(ai->ai_protocol, ==, protocol);
821 
822 	if (evutil_parse_sockaddr_port(
823 		    sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
824 		TT_FAIL(("Couldn't parse expected address %s on line %d",
825 			sockaddr_port, line));
826 		return -1;
827 	}
828 	if (ai->ai_family != ss.ss_family) {
829 		TT_FAIL(("Address family %d did not match %d on line %d",
830 			ai->ai_family, ss.ss_family, line));
831 		return -1;
832 	}
833 	if (ai->ai_addr->sa_family == AF_INET) {
834 		struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
835 		evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
836 		gotport = ntohs(sin->sin_port);
837 		if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
838 			TT_FAIL(("Addr size mismatch on line %d", line));
839 			return -1;
840 		}
841 	} else {
842 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
843 		evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
844 		gotport = ntohs(sin6->sin6_port);
845 		if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
846 			TT_FAIL(("Addr size mismatch on line %d", line));
847 			return -1;
848 		}
849 	}
850 	if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
851 		TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
852 			buf, gotport, line));
853 		return -1;
854 	} else {
855 		TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
856 			buf, gotport, line));
857 	}
858 	return 0;
859 end:
860 	TT_FAIL(("Test failed on line %d", line));
861 	return -1;
862 }
863 
864 static void
865 test_evutil_rand(void *arg)
866 {
867 	char buf1[32];
868 	char buf2[32];
869 	int counts[256];
870 	int i, j, k, n=0;
871 	struct evutil_weakrand_state seed = { 12346789U };
872 
873 	memset(buf2, 0, sizeof(buf2));
874 	memset(counts, 0, sizeof(counts));
875 
876 	for (k=0;k<32;++k) {
877 		/* Try a few different start and end points; try to catch
878 		 * the various misaligned cases of arc4random_buf */
879 		int startpoint = evutil_weakrand_(&seed) % 4;
880 		int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
881 
882 		memset(buf2, 0, sizeof(buf2));
883 
884 		/* Do 6 runs over buf1, or-ing the result into buf2 each
885 		 * time, to make sure we're setting each byte that we mean
886 		 * to set. */
887 		for (i=0;i<8;++i) {
888 			memset(buf1, 0, sizeof(buf1));
889 			evutil_secure_rng_get_bytes(buf1 + startpoint,
890 			    endpoint-startpoint);
891 			n += endpoint - startpoint;
892 			for (j=0; j<32; ++j) {
893 				if (j >= startpoint && j < endpoint) {
894 					buf2[j] |= buf1[j];
895 					++counts[(unsigned char)buf1[j]];
896 				} else {
897 					tt_assert(buf1[j] == 0);
898 					tt_int_op(buf1[j], ==, 0);
899 
900 				}
901 			}
902 		}
903 
904 		/* This will give a false positive with P=(256**8)==(2**64)
905 		 * for each character. */
906 		for (j=startpoint;j<endpoint;++j) {
907 			tt_int_op(buf2[j], !=, 0);
908 		}
909 	}
910 
911 	evutil_weakrand_seed_(&seed, 0);
912 	for (i = 0; i < 10000; ++i) {
913 		ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
914 		tt_int_op(0, <=, r);
915 		tt_int_op(r, <, 9999);
916 	}
917 
918 	/* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
919 end:
920 	;
921 }
922 
923 static void
924 test_evutil_getaddrinfo(void *arg)
925 {
926 	struct evutil_addrinfo *ai = NULL, *a;
927 	struct evutil_addrinfo hints;
928 	int r;
929 
930 	/* Try using it as a pton. */
931 	memset(&hints, 0, sizeof(hints));
932 	hints.ai_family = PF_UNSPEC;
933 	hints.ai_socktype = SOCK_STREAM;
934 	r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
935 	tt_int_op(r, ==, 0);
936 	tt_assert(ai);
937 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
938 	test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
939 	evutil_freeaddrinfo(ai);
940 	ai = NULL;
941 
942 	memset(&hints, 0, sizeof(hints));
943 	hints.ai_family = PF_UNSPEC;
944 	hints.ai_protocol = IPPROTO_UDP;
945 	r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
946 	tt_int_op(r, ==, 0);
947 	tt_assert(ai);
948 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
949 	test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
950 	evutil_freeaddrinfo(ai);
951 	ai = NULL;
952 
953 	/* Try out the behavior of nodename=NULL */
954 	memset(&hints, 0, sizeof(hints));
955 	hints.ai_family = PF_INET;
956 	hints.ai_protocol = IPPROTO_TCP;
957 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
958 	r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
959 	tt_int_op(r,==,0);
960 	tt_assert(ai);
961 	tt_ptr_op(ai->ai_next, ==, NULL);
962 	test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
963 	evutil_freeaddrinfo(ai);
964 	ai = NULL;
965 	hints.ai_flags = 0; /* as if for connect */
966 	r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
967 	tt_assert(ai);
968 	tt_int_op(r,==,0);
969 	test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
970 	tt_ptr_op(ai->ai_next, ==, NULL);
971 	evutil_freeaddrinfo(ai);
972 	ai = NULL;
973 
974 	hints.ai_flags = 0; /* as if for connect */
975 	hints.ai_family = PF_INET6;
976 	r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
977 	tt_assert(ai);
978 	tt_int_op(r,==,0);
979 	tt_ptr_op(ai->ai_next, ==, NULL);
980 	test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
981 	evutil_freeaddrinfo(ai);
982 	ai = NULL;
983 
984 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
985 	hints.ai_family = PF_INET6;
986 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
987 	tt_assert(ai);
988 	tt_int_op(r,==,0);
989 	tt_ptr_op(ai->ai_next, ==, NULL);
990 	test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
991 	evutil_freeaddrinfo(ai);
992 	ai = NULL;
993 
994 	/* Now try an unspec one. We should get a v6 and a v4. */
995 	hints.ai_family = PF_UNSPEC;
996 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
997 	tt_assert(ai);
998 	tt_int_op(r,==,0);
999 	a = ai_find_by_family(ai, PF_INET6);
1000 	tt_assert(a);
1001 	test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
1002 	a = ai_find_by_family(ai, PF_INET);
1003 	tt_assert(a);
1004 	test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
1005 	evutil_freeaddrinfo(ai);
1006 	ai = NULL;
1007 
1008 	/* Try out AI_NUMERICHOST: successful case.  Also try
1009 	 * multiprotocol. */
1010 	memset(&hints, 0, sizeof(hints));
1011 	hints.ai_family = PF_UNSPEC;
1012 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1013 	r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
1014 	tt_int_op(r, ==, 0);
1015 	a = ai_find_by_protocol(ai, IPPROTO_TCP);
1016 	tt_assert(a);
1017 	test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
1018 	a = ai_find_by_protocol(ai, IPPROTO_UDP);
1019 	tt_assert(a);
1020 	test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
1021 	evutil_freeaddrinfo(ai);
1022 	ai = NULL;
1023 
1024 	/* Try the failing case of AI_NUMERICHOST */
1025 	memset(&hints, 0, sizeof(hints));
1026 	hints.ai_family = PF_UNSPEC;
1027 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1028 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1029 	tt_int_op(r, ==, EVUTIL_EAI_NONAME);
1030 	tt_ptr_op(ai, ==, NULL);
1031 
1032 	/* Try symbolic service names wit AI_NUMERICSERV */
1033 	memset(&hints, 0, sizeof(hints));
1034 	hints.ai_family = PF_UNSPEC;
1035 	hints.ai_socktype = SOCK_STREAM;
1036 	hints.ai_flags = EVUTIL_AI_NUMERICSERV;
1037 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1038 	tt_int_op(r,==,EVUTIL_EAI_NONAME);
1039 
1040 	/* Try symbolic service names */
1041 	memset(&hints, 0, sizeof(hints));
1042 	hints.ai_family = PF_UNSPEC;
1043 	hints.ai_socktype = SOCK_STREAM;
1044 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1045 	if (r!=0) {
1046 		TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
1047 	} else {
1048 		tt_assert(ai);
1049 		test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
1050 		evutil_freeaddrinfo(ai);
1051 		ai = NULL;
1052 	}
1053 
1054 end:
1055 	if (ai)
1056 		evutil_freeaddrinfo(ai);
1057 }
1058 
1059 static void
1060 test_evutil_getaddrinfo_live(void *arg)
1061 {
1062 	struct evutil_addrinfo *ai = NULL;
1063 	struct evutil_addrinfo hints;
1064 
1065 	struct sockaddr_in6 *sin6;
1066 	struct sockaddr_in *sin;
1067 	char buf[128];
1068 	const char *cp;
1069 	int r;
1070 
1071 	/* Now do some actual lookups. */
1072 	memset(&hints, 0, sizeof(hints));
1073 	hints.ai_family = PF_INET;
1074 	hints.ai_protocol = IPPROTO_TCP;
1075 	hints.ai_socktype = SOCK_STREAM;
1076 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1077 	if (r != 0) {
1078 		TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
1079 	} else {
1080 		tt_assert(ai);
1081 		tt_int_op(ai->ai_family, ==, PF_INET);
1082 		tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
1083 		tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
1084 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
1085 		sin = (struct sockaddr_in*)ai->ai_addr;
1086 		tt_int_op(sin->sin_family, ==, AF_INET);
1087 		tt_int_op(sin->sin_port, ==, htons(80));
1088 		tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
1089 
1090 		cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
1091 		TT_BLATHER(("www.google.com resolved to %s",
1092 			cp?cp:"<unwriteable>"));
1093 		evutil_freeaddrinfo(ai);
1094 		ai = NULL;
1095 	}
1096 
1097 	hints.ai_family = PF_INET6;
1098 	r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
1099 	if (r != 0) {
1100 		TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
1101 	} else {
1102 		tt_assert(ai);
1103 		tt_int_op(ai->ai_family, ==, PF_INET6);
1104 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
1105 		sin6 = (struct sockaddr_in6*)ai->ai_addr;
1106 		tt_int_op(sin6->sin6_port, ==, htons(80));
1107 
1108 		cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
1109 		    sizeof(buf));
1110 		TT_BLATHER(("ipv6.google.com resolved to %s",
1111 			cp?cp:"<unwriteable>"));
1112 	}
1113 
1114 end:
1115 	if (ai)
1116 		evutil_freeaddrinfo(ai);
1117 }
1118 
1119 #ifdef _WIN32
1120 static void
1121 test_evutil_loadsyslib(void *arg)
1122 {
1123 	HANDLE h=NULL;
1124 
1125 	h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
1126 	tt_assert(h);
1127 
1128 end:
1129 	if (h)
1130 		CloseHandle(h);
1131 
1132 }
1133 #endif
1134 
1135 /** Test mm_malloc(). */
1136 static void
1137 test_event_malloc(void *arg)
1138 {
1139 	void *p = NULL;
1140 	(void)arg;
1141 
1142 	/* mm_malloc(0) should simply return NULL. */
1143 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1144 	errno = 0;
1145 	p = mm_malloc(0);
1146 	tt_assert(p == NULL);
1147 	tt_int_op(errno, ==, 0);
1148 #endif
1149 
1150 	/* Trivial case. */
1151 	errno = 0;
1152 	p = mm_malloc(8);
1153 	tt_assert(p != NULL);
1154 	tt_int_op(errno, ==, 0);
1155 	mm_free(p);
1156 
1157  end:
1158 	errno = 0;
1159 	return;
1160 }
1161 
1162 static void
1163 test_event_calloc(void *arg)
1164 {
1165 	void *p = NULL;
1166 	(void)arg;
1167 
1168 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1169 	/* mm_calloc() should simply return NULL
1170 	 * if either argument is zero. */
1171 	errno = 0;
1172 	p = mm_calloc(0, 0);
1173 	tt_assert(p == NULL);
1174 	tt_int_op(errno, ==, 0);
1175 	errno = 0;
1176 	p = mm_calloc(0, 1);
1177 	tt_assert(p == NULL);
1178 	tt_int_op(errno, ==, 0);
1179 	errno = 0;
1180 	p = mm_calloc(1, 0);
1181 	tt_assert(p == NULL);
1182 	tt_int_op(errno, ==, 0);
1183 #endif
1184 
1185 	/* Trivial case. */
1186 	errno = 0;
1187 	p = mm_calloc(8, 8);
1188 	tt_assert(p != NULL);
1189 	tt_int_op(errno, ==, 0);
1190 	mm_free(p);
1191 	p = NULL;
1192 
1193 	/* mm_calloc() should set errno = ENOMEM and return NULL
1194 	 * in case of potential overflow. */
1195 	errno = 0;
1196 	p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
1197 	tt_assert(p == NULL);
1198 	tt_int_op(errno, ==, ENOMEM);
1199 
1200  end:
1201 	errno = 0;
1202 	if (p)
1203 		mm_free(p);
1204 
1205 	return;
1206 }
1207 
1208 static void
1209 test_event_strdup(void *arg)
1210 {
1211 	void *p = NULL;
1212 	(void)arg;
1213 
1214 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1215 	/* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
1216 	errno = 0;
1217 	p = mm_strdup(NULL);
1218 	tt_assert(p == NULL);
1219 	tt_int_op(errno, ==, EINVAL);
1220 #endif
1221 
1222 	/* Trivial cases. */
1223 
1224 	errno = 0;
1225 	p = mm_strdup("");
1226 	tt_assert(p != NULL);
1227 	tt_int_op(errno, ==, 0);
1228 	tt_str_op(p, ==, "");
1229 	mm_free(p);
1230 
1231 	errno = 0;
1232 	p = mm_strdup("foo");
1233 	tt_assert(p != NULL);
1234 	tt_int_op(errno, ==, 0);
1235 	tt_str_op(p, ==, "foo");
1236 	mm_free(p);
1237 
1238 	/* XXX
1239 	 * mm_strdup(str) where str is a string of length EV_SIZE_MAX
1240 	 * should set errno = ENOMEM and return NULL. */
1241 
1242  end:
1243 	errno = 0;
1244 	return;
1245 }
1246 
1247 static void
1248 test_evutil_usleep(void *arg)
1249 {
1250 	struct timeval tv1, tv2, tv3, diff1, diff2;
1251 	const struct timeval quarter_sec = {0, 250*1000};
1252 	const struct timeval tenth_sec = {0, 100*1000};
1253 	long usec1, usec2;
1254 
1255 	evutil_gettimeofday(&tv1, NULL);
1256 	evutil_usleep_(&quarter_sec);
1257 	evutil_gettimeofday(&tv2, NULL);
1258 	evutil_usleep_(&tenth_sec);
1259 	evutil_gettimeofday(&tv3, NULL);
1260 
1261 	evutil_timersub(&tv2, &tv1, &diff1);
1262 	evutil_timersub(&tv3, &tv2, &diff2);
1263 	usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
1264 	usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
1265 
1266 	tt_int_op(usec1, >, 200000);
1267 	tt_int_op(usec1, <, 300000);
1268 	tt_int_op(usec2, >,  80000);
1269 	tt_int_op(usec2, <, 120000);
1270 
1271 end:
1272 	;
1273 }
1274 
1275 static void
1276 test_evutil_monotonic_res(void *data_)
1277 {
1278 	/* Basic santity-test for monotonic timers.  What we'd really like
1279 	 * to do is make sure that they can't go backwards even when the
1280 	 * system clock goes backwards. But we haven't got a good way to
1281 	 * move the system clock backwards.
1282 	 */
1283 	struct basic_test_data *data = data_;
1284 	struct evutil_monotonic_timer timer;
1285 	const int precise = strstr(data->setup_data, "precise") != NULL;
1286 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
1287 	struct timeval tv[10], delay;
1288 	int total_diff = 0;
1289 
1290 	int flags = 0, wantres, acceptdiff, i;
1291 	if (precise)
1292 		flags |= EV_MONOT_PRECISE;
1293 	if (fallback)
1294 		flags |= EV_MONOT_FALLBACK;
1295 	if (precise || fallback) {
1296 #ifdef _WIN32
1297 		wantres = 10*1000;
1298 		acceptdiff = 1000;
1299 #else
1300 		wantres = 1000;
1301 		acceptdiff = 300;
1302 #endif
1303 	} else {
1304 		wantres = 40*1000;
1305 		acceptdiff = 20*1000;
1306 	}
1307 
1308 	TT_BLATHER(("Precise = %d", precise));
1309 	TT_BLATHER(("Fallback = %d", fallback));
1310 
1311 	/* First, make sure we match up with usleep. */
1312 
1313 	delay.tv_sec = 0;
1314 	delay.tv_usec = wantres;
1315 
1316 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1317 
1318 	for (i = 0; i < 10; ++i) {
1319 		evutil_gettime_monotonic_(&timer, &tv[i]);
1320 		evutil_usleep_(&delay);
1321 	}
1322 
1323 	for (i = 0; i < 9; ++i) {
1324 		struct timeval diff;
1325 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1326 		evutil_timersub(&tv[i+1], &tv[i], &diff);
1327 		tt_int_op(diff.tv_sec, ==, 0);
1328 		total_diff += diff.tv_usec;
1329 		TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
1330 	}
1331 	tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
1332 
1333 end:
1334 	;
1335 }
1336 
1337 static void
1338 test_evutil_monotonic_prc(void *data_)
1339 {
1340 	struct basic_test_data *data = data_;
1341 	struct evutil_monotonic_timer timer;
1342 	const int precise = strstr(data->setup_data, "precise") != NULL;
1343 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
1344 	struct timeval tv[10];
1345 	int total_diff = 0;
1346 	int i, maxstep = 25*1000,flags=0;
1347 	if (precise)
1348 		maxstep = 500;
1349 	if (precise)
1350 		flags |= EV_MONOT_PRECISE;
1351 	if (fallback)
1352 		flags |= EV_MONOT_FALLBACK;
1353 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1354 
1355 	/* find out what precision we actually see. */
1356 
1357 	evutil_gettime_monotonic_(&timer, &tv[0]);
1358 	for (i = 1; i < 10; ++i) {
1359 		do {
1360 			evutil_gettime_monotonic_(&timer, &tv[i]);
1361 		} while (evutil_timercmp(&tv[i-1], &tv[i], ==));
1362 	}
1363 
1364 	total_diff = 0;
1365 	for (i = 0; i < 9; ++i) {
1366 		struct timeval diff;
1367 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1368 		evutil_timersub(&tv[i+1], &tv[i], &diff);
1369 		tt_int_op(diff.tv_sec, ==, 0);
1370 		total_diff += diff.tv_usec;
1371 		TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
1372 	}
1373 	TT_BLATHER(("Average step difference = %d", total_diff / 9));
1374 	tt_int_op(total_diff/9, <, maxstep);
1375 
1376 end:
1377 	;
1378 }
1379 
1380 struct testcase_t util_testcases[] = {
1381 	{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
1382 	{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
1383 	{ "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
1384 	{ "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
1385 	{ "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
1386 	{ "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
1387 	{ "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
1388 	{ "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
1389 	{ "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
1390 	{ "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
1391 	{ "log", test_evutil_log, TT_FORK, NULL, NULL },
1392 	{ "upcast", test_evutil_upcast, 0, NULL, NULL },
1393 	{ "integers", test_evutil_integers, 0, NULL, NULL },
1394 	{ "rand", test_evutil_rand, TT_FORK, NULL, NULL },
1395 	{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
1396 	{ "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1397 #ifdef _WIN32
1398 	{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
1399 #endif
1400 	{ "mm_malloc", test_event_malloc, 0, NULL, NULL },
1401 	{ "mm_calloc", test_event_calloc, 0, NULL, NULL },
1402 	{ "mm_strdup", test_event_strdup, 0, NULL, NULL },
1403 	{ "usleep", test_evutil_usleep, 0, NULL, NULL },
1404 	{ "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
1405 	{ "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
1406 	{ "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
1407 	{ "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
1408 	{ "monotonic_prc_precise", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"precise" },
1409 	{ "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
1410 	END_OF_TESTCASES,
1411 };
1412 
1413