xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/test/regress_util.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: regress_util.c,v 1.7 2024/08/18 20:47:23 christos Exp $	*/
28585484eSchristos 
38585484eSchristos /*
48585484eSchristos  * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
58585484eSchristos  *
68585484eSchristos  * Redistribution and use in source and binary forms, with or without
78585484eSchristos  * modification, are permitted provided that the following conditions
88585484eSchristos  * are met:
98585484eSchristos  * 1. Redistributions of source code must retain the above copyright
108585484eSchristos  *    notice, this list of conditions and the following disclaimer.
118585484eSchristos  * 2. Redistributions in binary form must reproduce the above copyright
128585484eSchristos  *    notice, this list of conditions and the following disclaimer in the
138585484eSchristos  *    documentation and/or other materials provided with the distribution.
148585484eSchristos  * 3. The name of the author may not be used to endorse or promote products
158585484eSchristos  *    derived from this software without specific prior written permission.
168585484eSchristos  *
178585484eSchristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
188585484eSchristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
198585484eSchristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
208585484eSchristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
218585484eSchristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
228585484eSchristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
238585484eSchristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
248585484eSchristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
258585484eSchristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
268585484eSchristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
278585484eSchristos  */
28*eabc0478Schristos 
29*eabc0478Schristos /** For event_debug() usage/coverage */
30*eabc0478Schristos #define EVENT_VISIBILITY_WANT_DLLIMPORT
31*eabc0478Schristos 
328585484eSchristos #include "../util-internal.h"
338585484eSchristos 
348585484eSchristos #ifdef _WIN32
358585484eSchristos #include <winsock2.h>
368585484eSchristos #include <windows.h>
378585484eSchristos #include <ws2tcpip.h>
388585484eSchristos #endif
398585484eSchristos 
408585484eSchristos #include "event2/event-config.h"
418585484eSchristos 
428585484eSchristos #include <sys/types.h>
438585484eSchristos 
448585484eSchristos #ifndef _WIN32
458585484eSchristos #include <sys/socket.h>
468585484eSchristos #include <netinet/in.h>
478585484eSchristos #include <arpa/inet.h>
488585484eSchristos #include <unistd.h>
498585484eSchristos #endif
508585484eSchristos #ifdef EVENT__HAVE_NETINET_IN6_H
518585484eSchristos #include <netinet/in6.h>
528585484eSchristos #endif
538585484eSchristos #ifdef EVENT__HAVE_SYS_WAIT_H
548585484eSchristos #include <sys/wait.h>
558585484eSchristos #endif
568585484eSchristos #include <signal.h>
578585484eSchristos #include <stdio.h>
588585484eSchristos #include <stdlib.h>
598585484eSchristos #include <string.h>
608585484eSchristos 
618585484eSchristos #include "event2/event.h"
628585484eSchristos #include "event2/util.h"
638585484eSchristos #include "../ipv6-internal.h"
648585484eSchristos #include "../log-internal.h"
658585484eSchristos #include "../strlcpy-internal.h"
668585484eSchristos #include "../mm-internal.h"
678585484eSchristos #include "../time-internal.h"
688585484eSchristos 
698585484eSchristos #include "regress.h"
708585484eSchristos 
718585484eSchristos enum entry_status { NORMAL, CANONICAL, BAD };
728585484eSchristos 
738585484eSchristos /* This is a big table of results we expect from generating and parsing */
748585484eSchristos static struct ipv4_entry {
758585484eSchristos 	const char *addr;
768585484eSchristos 	ev_uint32_t res;
778585484eSchristos 	enum entry_status status;
788585484eSchristos } ipv4_entries[] = {
798585484eSchristos 	{ "1.2.3.4", 0x01020304u, CANONICAL },
808585484eSchristos 	{ "255.255.255.255", 0xffffffffu, CANONICAL },
818585484eSchristos 	{ "256.0.0.0", 0, BAD },
828585484eSchristos 	{ "ABC", 0, BAD },
838585484eSchristos 	{ "1.2.3.4.5", 0, BAD },
848585484eSchristos 	{ "176.192.208.244", 0xb0c0d0f4, CANONICAL },
858585484eSchristos 	{ NULL, 0, BAD },
868585484eSchristos };
878585484eSchristos 
888585484eSchristos static struct ipv6_entry {
898585484eSchristos 	const char *addr;
908585484eSchristos 	ev_uint32_t res[4];
918585484eSchristos 	enum entry_status status;
928585484eSchristos } ipv6_entries[] = {
938585484eSchristos 	{ "::", { 0, 0, 0, 0, }, CANONICAL },
948585484eSchristos 	{ "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
958585484eSchristos 	{ "::1", { 0, 0, 0, 1, }, CANONICAL },
968585484eSchristos 	{ "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
978585484eSchristos 	{ "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
988585484eSchristos 	{ "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
998585484eSchristos 	{ "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
1008585484eSchristos 	{ "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
1018585484eSchristos 	{ "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
1028585484eSchristos 	{ "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
1038585484eSchristos 	{ "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
1048585484eSchristos 	{ "foobar.", { 0, 0, 0, 0 }, BAD },
1058585484eSchristos 	{ "foobar", { 0, 0, 0, 0 }, BAD },
1068585484eSchristos 	{ "fo:obar", { 0, 0, 0, 0 }, BAD },
1078585484eSchristos 	{ "ffff", { 0, 0, 0, 0 }, BAD },
1088585484eSchristos 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
1098585484eSchristos 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
1108585484eSchristos 	{ "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
1118585484eSchristos 	{ "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
1128585484eSchristos 	{ "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
1138585484eSchristos 	{ "1::2::3", { 0, 0, 0, 0 }, BAD },
1148585484eSchristos 	{ ":::1", { 0, 0, 0, 0 }, BAD },
1158585484eSchristos 	{ NULL, { 0, 0, 0, 0,  }, BAD },
1168585484eSchristos };
1178585484eSchristos 
1188585484eSchristos static void
1198585484eSchristos regress_ipv4_parse(void *ptr)
1208585484eSchristos {
1218585484eSchristos 	int i;
1228585484eSchristos 	for (i = 0; ipv4_entries[i].addr; ++i) {
1238585484eSchristos 		char written[128];
1248585484eSchristos 		struct ipv4_entry *ent = &ipv4_entries[i];
1258585484eSchristos 		struct in_addr in;
1268585484eSchristos 		int r;
1278585484eSchristos 		r = evutil_inet_pton(AF_INET, ent->addr, &in);
1288585484eSchristos 		if (r == 0) {
1298585484eSchristos 			if (ent->status != BAD) {
1308585484eSchristos 				TT_FAIL(("%s did not parse, but it's a good address!",
1318585484eSchristos 					ent->addr));
1328585484eSchristos 			}
1338585484eSchristos 			continue;
1348585484eSchristos 		}
1358585484eSchristos 		if (ent->status == BAD) {
1368585484eSchristos 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
1378585484eSchristos 			continue;
1388585484eSchristos 		}
1398585484eSchristos 		if (ntohl(in.s_addr) != ent->res) {
1408585484eSchristos 			TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
1418585484eSchristos 				(unsigned long)ntohl(in.s_addr),
1428585484eSchristos 				(unsigned long)ent->res));
1438585484eSchristos 			continue;
1448585484eSchristos 		}
1458585484eSchristos 		if (ent->status == CANONICAL) {
1468585484eSchristos 			const char *w = evutil_inet_ntop(AF_INET, &in, written,
1478585484eSchristos 											 sizeof(written));
1488585484eSchristos 			if (!w) {
1498585484eSchristos 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
1508585484eSchristos 				continue;
1518585484eSchristos 			}
1528585484eSchristos 			if (strcmp(written, ent->addr)) {
1538585484eSchristos 				TT_FAIL(("Tried to write out %s; got %s",
1548585484eSchristos 					ent->addr, written));
1558585484eSchristos 				continue;
1568585484eSchristos 			}
1578585484eSchristos 		}
1588585484eSchristos 
1598585484eSchristos 	}
1608585484eSchristos 
1618585484eSchristos }
1628585484eSchristos 
1638585484eSchristos static void
1648585484eSchristos regress_ipv6_parse(void *ptr)
1658585484eSchristos {
1668585484eSchristos #ifdef AF_INET6
1678585484eSchristos 	int i, j;
1688585484eSchristos 
1698585484eSchristos 	for (i = 0; ipv6_entries[i].addr; ++i) {
1708585484eSchristos 		char written[128];
1718585484eSchristos 		struct ipv6_entry *ent = &ipv6_entries[i];
1728585484eSchristos 		struct in6_addr in6;
1738585484eSchristos 		int r;
1748585484eSchristos 		r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
1758585484eSchristos 		if (r == 0) {
1768585484eSchristos 			if (ent->status != BAD)
1778585484eSchristos 				TT_FAIL(("%s did not parse, but it's a good address!",
1788585484eSchristos 					ent->addr));
1798585484eSchristos 			continue;
1808585484eSchristos 		}
1818585484eSchristos 		if (ent->status == BAD) {
1828585484eSchristos 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
1838585484eSchristos 			continue;
1848585484eSchristos 		}
1858585484eSchristos 		for (j = 0; j < 4; ++j) {
1868585484eSchristos 			/* Can't use s6_addr32 here; some don't have it. */
1878585484eSchristos 			ev_uint32_t u =
1887476e6e4Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4  ] << 24) |
1897476e6e4Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
1907476e6e4Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
1917476e6e4Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+3]);
1928585484eSchristos 			if (u != ent->res[j]) {
1938585484eSchristos 				TT_FAIL(("%s did not parse as expected.", ent->addr));
1948585484eSchristos 				continue;
1958585484eSchristos 			}
1968585484eSchristos 		}
1978585484eSchristos 		if (ent->status == CANONICAL) {
1988585484eSchristos 			const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
1998585484eSchristos 											 sizeof(written));
2008585484eSchristos 			if (!w) {
2018585484eSchristos 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
2028585484eSchristos 				continue;
2038585484eSchristos 			}
2048585484eSchristos 			if (strcmp(written, ent->addr)) {
2058585484eSchristos 				TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
2068585484eSchristos 				continue;
2078585484eSchristos 			}
2088585484eSchristos 		}
2098585484eSchristos 
2108585484eSchristos 	}
2118585484eSchristos #else
2128585484eSchristos 	TT_BLATHER(("Skipping IPv6 address parsing."));
2138585484eSchristos #endif
2148585484eSchristos }
2158585484eSchristos 
216*eabc0478Schristos static struct ipv6_entry_scope {
217*eabc0478Schristos 	const char *addr;
218*eabc0478Schristos 	ev_uint32_t res[4];
219*eabc0478Schristos 	unsigned scope;
220*eabc0478Schristos 	enum entry_status status;
221*eabc0478Schristos } ipv6_entries_scope[] = {
222*eabc0478Schristos 	{ "2001:DB8::", { 0x20010db8, 0, 0 }, 0, NORMAL },
223*eabc0478Schristos 	{ "2001:DB8::%0", { 0x20010db8, 0, 0, 0 }, 0, NORMAL },
224*eabc0478Schristos 	{ "2001:DB8::%1", { 0x20010db8, 0, 0, 0 }, 1, NORMAL },
225*eabc0478Schristos 	{ "foobar.", { 0, 0, 0, 0 }, 0, BAD },
226*eabc0478Schristos 	{ "2001:DB8::%does-not-exist", { 0, 0, 0, 0 }, 0, BAD },
227*eabc0478Schristos 	{ NULL, { 0, 0, 0, 0,  }, 0, BAD },
228*eabc0478Schristos };
229*eabc0478Schristos static void
230*eabc0478Schristos regress_ipv6_parse_scope(void *ptr)
231*eabc0478Schristos {
232*eabc0478Schristos #ifdef AF_INET6
233*eabc0478Schristos 	int i, j;
234*eabc0478Schristos 	unsigned if_scope;
235*eabc0478Schristos 
236*eabc0478Schristos 	for (i = 0; ipv6_entries_scope[i].addr; ++i) {
237*eabc0478Schristos 		struct ipv6_entry_scope *ent = &ipv6_entries_scope[i];
238*eabc0478Schristos 		struct in6_addr in6;
239*eabc0478Schristos 		int r;
240*eabc0478Schristos 		r = evutil_inet_pton_scope(AF_INET6, ent->addr, &in6,
241*eabc0478Schristos 			&if_scope);
242*eabc0478Schristos 		if (r == 0) {
243*eabc0478Schristos 			if (ent->status != BAD)
244*eabc0478Schristos 				TT_FAIL(("%s did not parse, but it's a good address!",
245*eabc0478Schristos 					ent->addr));
246*eabc0478Schristos 			continue;
247*eabc0478Schristos 		}
248*eabc0478Schristos 		if (ent->status == BAD) {
249*eabc0478Schristos 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
250*eabc0478Schristos 			continue;
251*eabc0478Schristos 		}
252*eabc0478Schristos 		for (j = 0; j < 4; ++j) {
253*eabc0478Schristos 			/* Can't use s6_addr32 here; some don't have it. */
254*eabc0478Schristos 			ev_uint32_t u =
255*eabc0478Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4  ] << 24) |
256*eabc0478Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
257*eabc0478Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
258*eabc0478Schristos 			    ((ev_uint32_t)in6.s6_addr[j*4+3]);
259*eabc0478Schristos 			if (u != ent->res[j]) {
260*eabc0478Schristos 				TT_FAIL(("%s did not parse as expected.", ent->addr));
261*eabc0478Schristos 				continue;
262*eabc0478Schristos 			}
263*eabc0478Schristos 		}
264*eabc0478Schristos 		if (if_scope != ent->scope) {
265*eabc0478Schristos 			TT_FAIL(("%s did not parse as expected.", ent->addr));
266*eabc0478Schristos 			continue;
267*eabc0478Schristos 		}
268*eabc0478Schristos 	}
269*eabc0478Schristos #else
270*eabc0478Schristos 	TT_BLATHER(("Skipping IPv6 address parsing."));
271*eabc0478Schristos #endif
272*eabc0478Schristos }
273*eabc0478Schristos 
274*eabc0478Schristos 
2758585484eSchristos static struct sa_port_ent {
2768585484eSchristos 	const char *parse;
2778585484eSchristos 	int safamily;
2788585484eSchristos 	const char *addr;
2798585484eSchristos 	int port;
2808585484eSchristos } sa_port_ents[] = {
2818585484eSchristos 	{ "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
2828585484eSchristos 	{ "[ffff::1]", AF_INET6, "ffff::1", 0 },
2838585484eSchristos 	{ "[ffff::1", 0, NULL, 0 },
2848585484eSchristos 	{ "[ffff::1]:65599", 0, NULL, 0 },
2858585484eSchristos 	{ "[ffff::1]:0", 0, NULL, 0 },
2868585484eSchristos 	{ "[ffff::1]:-1", 0, NULL, 0 },
2878585484eSchristos 	{ "::1", AF_INET6, "::1", 0 },
2888585484eSchristos 	{ "1:2::1", AF_INET6, "1:2::1", 0 },
2898585484eSchristos 	{ "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
2908585484eSchristos 	{ "1.2.3.4", AF_INET, "1.2.3.4", 0 },
2918585484eSchristos 	{ NULL, 0, NULL, 0 },
2928585484eSchristos };
2938585484eSchristos 
2948585484eSchristos static void
2958585484eSchristos regress_sockaddr_port_parse(void *ptr)
2968585484eSchristos {
2978585484eSchristos 	struct sockaddr_storage ss;
2988585484eSchristos 	int i, r;
2998585484eSchristos 
3008585484eSchristos 	for (i = 0; sa_port_ents[i].parse; ++i) {
3018585484eSchristos 		struct sa_port_ent *ent = &sa_port_ents[i];
3028585484eSchristos 		int len = sizeof(ss);
3038585484eSchristos 		memset(&ss, 0, sizeof(ss));
3048585484eSchristos 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
3058585484eSchristos 		if (r < 0) {
3068585484eSchristos 			if (ent->safamily)
3078585484eSchristos 				TT_FAIL(("Couldn't parse %s!", ent->parse));
3088585484eSchristos 			continue;
3098585484eSchristos 		} else if (! ent->safamily) {
3108585484eSchristos 			TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
3118585484eSchristos 			continue;
3128585484eSchristos 		}
3138585484eSchristos 		if (ent->safamily == AF_INET) {
3148585484eSchristos 			struct sockaddr_in sin;
3158585484eSchristos 			memset(&sin, 0, sizeof(sin));
3168585484eSchristos #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
3178585484eSchristos 			sin.sin_len = sizeof(sin);
3188585484eSchristos #endif
3198585484eSchristos 			sin.sin_family = AF_INET;
3208585484eSchristos 			sin.sin_port = htons(ent->port);
3218585484eSchristos 			r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
3228585484eSchristos 			if (1 != r) {
3238585484eSchristos 				TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
3248585484eSchristos 			} else if (memcmp(&sin, &ss, sizeof(sin))) {
3258585484eSchristos 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
3268585484eSchristos 			} else if (len != sizeof(sin)) {
3278585484eSchristos 				TT_FAIL(("Length for %s not as expected.",ent->parse));
3288585484eSchristos 			}
3298585484eSchristos 		} else {
3308585484eSchristos 			struct sockaddr_in6 sin6;
3318585484eSchristos 			memset(&sin6, 0, sizeof(sin6));
3328585484eSchristos #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
3338585484eSchristos 			sin6.sin6_len = sizeof(sin6);
3348585484eSchristos #endif
3358585484eSchristos 			sin6.sin6_family = AF_INET6;
3368585484eSchristos 			sin6.sin6_port = htons(ent->port);
3378585484eSchristos 			r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
3388585484eSchristos 			if (1 != r) {
3398585484eSchristos 				TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
3408585484eSchristos 			} else if (memcmp(&sin6, &ss, sizeof(sin6))) {
3418585484eSchristos 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
3428585484eSchristos 			} else if (len != sizeof(sin6)) {
3438585484eSchristos 				TT_FAIL(("Length for %s not as expected.",ent->parse));
3448585484eSchristos 			}
3458585484eSchristos 		}
3468585484eSchristos 	}
3478585484eSchristos }
3488585484eSchristos 
3498585484eSchristos 
3508585484eSchristos static void
3518585484eSchristos regress_sockaddr_port_format(void *ptr)
3528585484eSchristos {
3538585484eSchristos 	struct sockaddr_storage ss;
3548585484eSchristos 	int len;
3558585484eSchristos 	const char *cp;
3568585484eSchristos 	char cbuf[128];
3578585484eSchristos 	int r;
3588585484eSchristos 
3598585484eSchristos 	len = sizeof(ss);
3608585484eSchristos 	r = evutil_parse_sockaddr_port("192.168.1.1:80",
3618585484eSchristos 	    (struct sockaddr*)&ss, &len);
3628585484eSchristos 	tt_int_op(r,==,0);
3638585484eSchristos 	cp = evutil_format_sockaddr_port_(
3648585484eSchristos 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3658585484eSchristos 	tt_ptr_op(cp,==,cbuf);
3668585484eSchristos 	tt_str_op(cp,==,"192.168.1.1:80");
3678585484eSchristos 
3688585484eSchristos 	len = sizeof(ss);
3698585484eSchristos 	r = evutil_parse_sockaddr_port("[ff00::8010]:999",
3708585484eSchristos 	    (struct sockaddr*)&ss, &len);
3718585484eSchristos 	tt_int_op(r,==,0);
3728585484eSchristos 	cp = evutil_format_sockaddr_port_(
3738585484eSchristos 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3748585484eSchristos 	tt_ptr_op(cp,==,cbuf);
3758585484eSchristos 	tt_str_op(cp,==,"[ff00::8010]:999");
3768585484eSchristos 
3778585484eSchristos 	ss.ss_family=99;
3788585484eSchristos 	cp = evutil_format_sockaddr_port_(
3798585484eSchristos 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3808585484eSchristos 	tt_ptr_op(cp,==,cbuf);
3818585484eSchristos 	tt_str_op(cp,==,"<addr with socktype 99>");
3828585484eSchristos end:
3838585484eSchristos 	;
3848585484eSchristos }
3858585484eSchristos 
3868585484eSchristos static struct sa_pred_ent {
3878585484eSchristos 	const char *parse;
3888585484eSchristos 
3898585484eSchristos 	int is_loopback;
3908585484eSchristos } sa_pred_entries[] = {
3918585484eSchristos 	{ "127.0.0.1",	 1 },
3928585484eSchristos 	{ "127.0.3.2",	 1 },
3938585484eSchristos 	{ "128.1.2.3",	 0 },
3948585484eSchristos 	{ "18.0.0.1",	 0 },
3958585484eSchristos 	{ "129.168.1.1", 0 },
3968585484eSchristos 
3978585484eSchristos 	{ "::1",	 1 },
3988585484eSchristos 	{ "::0",	 0 },
3998585484eSchristos 	{ "f::1",	 0 },
4008585484eSchristos 	{ "::501",	 0 },
4018585484eSchristos 	{ NULL,		 0 },
4028585484eSchristos 
4038585484eSchristos };
4048585484eSchristos 
4058585484eSchristos static void
4068585484eSchristos test_evutil_sockaddr_predicates(void *ptr)
4078585484eSchristos {
4088585484eSchristos 	struct sockaddr_storage ss;
4098585484eSchristos 	int r, i;
4108585484eSchristos 
4118585484eSchristos 	for (i=0; sa_pred_entries[i].parse; ++i) {
4128585484eSchristos 		struct sa_pred_ent *ent = &sa_pred_entries[i];
4138585484eSchristos 		int len = sizeof(ss);
4148585484eSchristos 
4158585484eSchristos 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
4168585484eSchristos 
4178585484eSchristos 		if (r<0) {
4188585484eSchristos 			TT_FAIL(("Couldn't parse %s!", ent->parse));
4198585484eSchristos 			continue;
4208585484eSchristos 		}
4218585484eSchristos 
4228585484eSchristos 		/* sockaddr_is_loopback */
4238585484eSchristos 		if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
4248585484eSchristos 			TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
4258585484eSchristos 				ent->parse));
4268585484eSchristos 		}
4278585484eSchristos 	}
4288585484eSchristos }
4298585484eSchristos 
4308585484eSchristos static void
4318585484eSchristos test_evutil_strtoll(void *ptr)
4328585484eSchristos {
4338585484eSchristos 	const char *s;
4348585484eSchristos 	char *endptr;
4358585484eSchristos 
4368585484eSchristos 	tt_want(evutil_strtoll("5000000000", NULL, 10) ==
4378585484eSchristos 		((ev_int64_t)5000000)*1000);
4388585484eSchristos 	tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
4398585484eSchristos 		((ev_int64_t)5000000)*-1000);
4408585484eSchristos 	s = " 99999stuff";
4418585484eSchristos 	tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
4428585484eSchristos 	tt_want(endptr == s+6);
4438585484eSchristos 	tt_want(evutil_strtoll("foo", NULL, 10) == 0);
4448585484eSchristos  }
4458585484eSchristos 
4468585484eSchristos static void
4478585484eSchristos test_evutil_snprintf(void *ptr)
4488585484eSchristos {
4498585484eSchristos 	char buf[16];
4508585484eSchristos 	int r;
4518585484eSchristos 	ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
4528585484eSchristos 	ev_int64_t i64 = -1 * (ev_int64_t) u64;
4538585484eSchristos 	size_t size = 8000;
4548585484eSchristos 	ev_ssize_t ssize = -9000;
4558585484eSchristos 
4568585484eSchristos 	r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
4578585484eSchristos 	tt_str_op(buf, ==, "50 100");
4588585484eSchristos 	tt_int_op(r, ==, 6);
4598585484eSchristos 
4608585484eSchristos 	r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
4618585484eSchristos 	tt_str_op(buf, ==, "longish 1234567");
4628585484eSchristos 	tt_int_op(r, ==, 18);
4638585484eSchristos 
4648585484eSchristos 	r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
4658585484eSchristos 	tt_str_op(buf, ==, "200000000000");
4668585484eSchristos 	tt_int_op(r, ==, 12);
4678585484eSchristos 
4688585484eSchristos 	r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
4698585484eSchristos 	tt_str_op(buf, ==, "-200000000000");
4708585484eSchristos 	tt_int_op(r, ==, 13);
4718585484eSchristos 
4728585484eSchristos 	r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
4738585484eSchristos 	    EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
4748585484eSchristos 	tt_str_op(buf, ==, "8000 -9000");
4758585484eSchristos 	tt_int_op(r, ==, 10);
4768585484eSchristos 
4778585484eSchristos       end:
4788585484eSchristos 	;
4798585484eSchristos }
4808585484eSchristos 
4818585484eSchristos static void
4828585484eSchristos test_evutil_casecmp(void *ptr)
4838585484eSchristos {
4848585484eSchristos 	tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
4858585484eSchristos 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
4868585484eSchristos 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
4878585484eSchristos 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
4888585484eSchristos 	tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
4898585484eSchristos 
4908585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
4918585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
4928585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
4938585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
4948585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
4958585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
4968585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
4978585484eSchristos 	tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
4988585484eSchristos end:
4998585484eSchristos 	;
5008585484eSchristos }
5018585484eSchristos 
5028585484eSchristos static void
5038585484eSchristos test_evutil_rtrim(void *ptr)
5048585484eSchristos {
5058585484eSchristos #define TEST_TRIM(s, result) \
5068585484eSchristos 	do {						\
5078585484eSchristos 	    if (cp) mm_free(cp);			\
5088585484eSchristos 	    cp = mm_strdup(s);				\
509b8ecfcfeSchristos 	    tt_assert(cp);				\
5108585484eSchristos 	    evutil_rtrim_lws_(cp);			\
5118585484eSchristos 	    tt_str_op(cp, ==, result);			\
5128585484eSchristos 	} while(0)
5138585484eSchristos 
5148585484eSchristos 	char *cp = NULL;
5158585484eSchristos 	(void) ptr;
5168585484eSchristos 
5178585484eSchristos 	TEST_TRIM("", "");
5188585484eSchristos 	TEST_TRIM("a", "a");
5198585484eSchristos 	TEST_TRIM("abcdef ghi", "abcdef ghi");
5208585484eSchristos 
5218585484eSchristos 	TEST_TRIM(" ", "");
5228585484eSchristos 	TEST_TRIM("  ", "");
5238585484eSchristos 	TEST_TRIM("a ", "a");
5248585484eSchristos 	TEST_TRIM("abcdef  gH       ", "abcdef  gH");
5258585484eSchristos 
5268585484eSchristos 	TEST_TRIM("\t\t", "");
5278585484eSchristos 	TEST_TRIM(" \t", "");
5288585484eSchristos 	TEST_TRIM("\t", "");
5298585484eSchristos 	TEST_TRIM("a \t", "a");
5308585484eSchristos 	TEST_TRIM("a\t ", "a");
5318585484eSchristos 	TEST_TRIM("a\t", "a");
5328585484eSchristos 	TEST_TRIM("abcdef  gH    \t  ", "abcdef  gH");
5338585484eSchristos 
5348585484eSchristos end:
5358585484eSchristos 	if (cp)
5368585484eSchristos 		mm_free(cp);
5378585484eSchristos }
5388585484eSchristos 
5398585484eSchristos static int logsev = 0;
5408585484eSchristos static char *logmsg = NULL;
5418585484eSchristos 
5428585484eSchristos static void
5438585484eSchristos logfn(int severity, const char *msg)
5448585484eSchristos {
5458585484eSchristos 	logsev = severity;
5468585484eSchristos 	tt_want(msg);
5478585484eSchristos 	if (msg) {
5488585484eSchristos 		if (logmsg)
5498585484eSchristos 			free(logmsg);
5508585484eSchristos 		logmsg = strdup(msg);
5518585484eSchristos 	}
5528585484eSchristos }
5538585484eSchristos 
5548585484eSchristos static int fatal_want_severity = 0;
5558585484eSchristos static const char *fatal_want_message = NULL;
5568585484eSchristos static void
5578585484eSchristos fatalfn(int exitcode)
5588585484eSchristos {
5598585484eSchristos 	if (logsev != fatal_want_severity ||
5608585484eSchristos 	    !logmsg ||
5618585484eSchristos 	    strcmp(logmsg, fatal_want_message))
5628585484eSchristos 		exit(0);
5638585484eSchristos 	else
5648585484eSchristos 		exit(exitcode);
5658585484eSchristos }
5668585484eSchristos 
5678585484eSchristos #ifndef _WIN32
5688585484eSchristos #define CAN_CHECK_ERR
5698585484eSchristos static void
5708585484eSchristos check_error_logging(void (*fn)(void), int wantexitcode,
5718585484eSchristos     int wantseverity, const char *wantmsg)
5728585484eSchristos {
5738585484eSchristos 	pid_t pid;
5748585484eSchristos 	int status = 0, exitcode;
5758585484eSchristos 	fatal_want_severity = wantseverity;
5768585484eSchristos 	fatal_want_message = wantmsg;
5778585484eSchristos 	if ((pid = regress_fork()) == 0) {
5788585484eSchristos 		/* child process */
5798585484eSchristos 		fn();
5808585484eSchristos 		exit(0); /* should be unreachable. */
5818585484eSchristos 	} else {
5828585484eSchristos 		wait(&status);
5838585484eSchristos 		exitcode = WEXITSTATUS(status);
5848585484eSchristos 		tt_int_op(wantexitcode, ==, exitcode);
5858585484eSchristos 	}
5868585484eSchristos end:
5878585484eSchristos 	;
5888585484eSchristos }
5898585484eSchristos 
5908585484eSchristos static void
5918585484eSchristos errx_fn(void)
5928585484eSchristos {
5938585484eSchristos 	event_errx(2, "Fatal error; too many kumquats (%d)", 5);
5948585484eSchristos }
5958585484eSchristos 
5968585484eSchristos static void
5978585484eSchristos err_fn(void)
5988585484eSchristos {
5998585484eSchristos 	errno = ENOENT;
6008585484eSchristos 	event_err(5,"Couldn't open %s", "/very/bad/file");
6018585484eSchristos }
6028585484eSchristos 
6038585484eSchristos static void
6048585484eSchristos sock_err_fn(void)
6058585484eSchristos {
6068585484eSchristos 	evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
6078585484eSchristos #ifdef _WIN32
6088585484eSchristos 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
6098585484eSchristos #else
6108585484eSchristos 	errno = EAGAIN;
6118585484eSchristos #endif
6128585484eSchristos 	event_sock_err(20, fd, "Unhappy socket");
6138585484eSchristos }
6148585484eSchristos #endif
6158585484eSchristos 
6168585484eSchristos static void
6178585484eSchristos test_evutil_log(void *ptr)
6188585484eSchristos {
6198585484eSchristos 	evutil_socket_t fd = -1;
6208585484eSchristos 	char buf[128];
6218585484eSchristos 
6228585484eSchristos 	event_set_log_callback(logfn);
6238585484eSchristos 	event_set_fatal_callback(fatalfn);
6248585484eSchristos #define RESET() do {				\
6258585484eSchristos 		logsev = 0;	\
6268585484eSchristos 		if (logmsg) free(logmsg);	\
6278585484eSchristos 		logmsg = NULL;			\
6288585484eSchristos 	} while (0)
6298585484eSchristos #define LOGEQ(sev,msg) do {			\
6308585484eSchristos 		tt_int_op(logsev,==,sev);	\
6318585484eSchristos 		tt_assert(logmsg != NULL);	\
6328585484eSchristos 		tt_str_op(logmsg,==,msg);	\
6338585484eSchristos 	} while (0)
6348585484eSchristos 
6358585484eSchristos #ifdef CAN_CHECK_ERR
6368585484eSchristos 	/* We need to disable these tests for now.  Previously, the logging
6378585484eSchristos 	 * module didn't enforce the requirement that a fatal callback
6388585484eSchristos 	 * actually exit.  Now, it exits no matter what, so if we wan to
6398585484eSchristos 	 * reinstate these tests, we'll need to fork for each one. */
6408585484eSchristos 	check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
6418585484eSchristos 	    "Fatal error; too many kumquats (5)");
6428585484eSchristos 	RESET();
6438585484eSchristos #endif
6448585484eSchristos 
6458585484eSchristos 	event_warnx("Far too many %s (%d)", "wombats", 99);
6468585484eSchristos 	LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
6478585484eSchristos 	RESET();
6488585484eSchristos 
6498585484eSchristos 	event_msgx("Connecting lime to coconut");
6508585484eSchristos 	LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
6518585484eSchristos 	RESET();
6528585484eSchristos 
6538585484eSchristos 	event_debug(("A millisecond passed! We should log that!"));
6548585484eSchristos #ifdef USE_DEBUG
6558585484eSchristos 	LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
6568585484eSchristos #else
6578585484eSchristos 	tt_int_op(logsev,==,0);
6588585484eSchristos 	tt_ptr_op(logmsg,==,NULL);
6598585484eSchristos #endif
6608585484eSchristos 	RESET();
6618585484eSchristos 
6628585484eSchristos 	/* Try with an errno. */
6638585484eSchristos 	errno = ENOENT;
6648585484eSchristos 	event_warn("Couldn't open %s", "/bad/file");
6658585484eSchristos 	evutil_snprintf(buf, sizeof(buf),
6668585484eSchristos 	    "Couldn't open /bad/file: %s",strerror(ENOENT));
6678585484eSchristos 	LOGEQ(EVENT_LOG_WARN,buf);
6688585484eSchristos 	RESET();
6698585484eSchristos 
6708585484eSchristos #ifdef CAN_CHECK_ERR
6718585484eSchristos 	evutil_snprintf(buf, sizeof(buf),
6728585484eSchristos 	    "Couldn't open /very/bad/file: %s",strerror(ENOENT));
6738585484eSchristos 	check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
6748585484eSchristos 	RESET();
6758585484eSchristos #endif
6768585484eSchristos 
6778585484eSchristos 	/* Try with a socket errno. */
6788585484eSchristos 	fd = socket(AF_INET, SOCK_STREAM, 0);
6798585484eSchristos #ifdef _WIN32
6808585484eSchristos 	evutil_snprintf(buf, sizeof(buf),
6818585484eSchristos 	    "Unhappy socket: %s",
6828585484eSchristos 	    evutil_socket_error_to_string(WSAEWOULDBLOCK));
6838585484eSchristos 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
6848585484eSchristos #else
6858585484eSchristos 	evutil_snprintf(buf, sizeof(buf),
6868585484eSchristos 	    "Unhappy socket: %s", strerror(EAGAIN));
6878585484eSchristos 	errno = EAGAIN;
6888585484eSchristos #endif
6898585484eSchristos 	event_sock_warn(fd, "Unhappy socket");
6908585484eSchristos 	LOGEQ(EVENT_LOG_WARN, buf);
6918585484eSchristos 	RESET();
6928585484eSchristos 
6938585484eSchristos #ifdef CAN_CHECK_ERR
6948585484eSchristos 	check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
6958585484eSchristos 	RESET();
6968585484eSchristos #endif
6978585484eSchristos 
6988585484eSchristos #undef RESET
6998585484eSchristos #undef LOGEQ
7008585484eSchristos end:
7018585484eSchristos 	if (logmsg)
7028585484eSchristos 		free(logmsg);
7038585484eSchristos 	if (fd >= 0)
7048585484eSchristos 		evutil_closesocket(fd);
7058585484eSchristos }
7068585484eSchristos 
7078585484eSchristos static void
7088585484eSchristos test_evutil_strlcpy(void *arg)
7098585484eSchristos {
7108585484eSchristos 	char buf[8];
7118585484eSchristos 
7128585484eSchristos 	/* Successful case. */
7138585484eSchristos 	tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
7148585484eSchristos 	tt_str_op(buf, ==, "Hello");
7158585484eSchristos 
7168585484eSchristos 	/* Overflow by a lot. */
7178585484eSchristos 	tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
7188585484eSchristos 	tt_str_op(buf, ==, "pentasy");
7198585484eSchristos 
7208585484eSchristos 	/* Overflow by exactly one. */
7218585484eSchristos 	tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
7228585484eSchristos 	tt_str_op(buf, ==, "overlon");
7238585484eSchristos end:
7248585484eSchristos 	;
7258585484eSchristos }
7268585484eSchristos 
7278585484eSchristos struct example_struct {
7288585484eSchristos 	const char *a;
7298585484eSchristos 	const char *b;
7308585484eSchristos 	long c;
7318585484eSchristos };
7328585484eSchristos 
7338585484eSchristos static void
7348585484eSchristos test_evutil_upcast(void *arg)
7358585484eSchristos {
7368585484eSchristos 	struct example_struct es1;
7378585484eSchristos 	const char **cp;
7388585484eSchristos 	es1.a = "World";
7398585484eSchristos 	es1.b = "Hello";
7408585484eSchristos 	es1.c = -99;
7418585484eSchristos 
7428585484eSchristos 	tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
7438585484eSchristos 
7448585484eSchristos 	cp = &es1.b;
7458585484eSchristos 	tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
7468585484eSchristos 
7478585484eSchristos end:
7488585484eSchristos 	;
7498585484eSchristos }
7508585484eSchristos 
7518585484eSchristos static void
7528585484eSchristos test_evutil_integers(void *arg)
7538585484eSchristos {
7548585484eSchristos 	ev_int64_t i64;
7558585484eSchristos 	ev_uint64_t u64;
7568585484eSchristos 	ev_int32_t i32;
7578585484eSchristos 	ev_uint32_t u32;
7588585484eSchristos 	ev_int16_t i16;
7598585484eSchristos 	ev_uint16_t u16;
7608585484eSchristos 	ev_int8_t  i8;
7618585484eSchristos 	ev_uint8_t  u8;
7628585484eSchristos 
7638585484eSchristos 	void *ptr;
7648585484eSchristos 	ev_intptr_t iptr;
7658585484eSchristos 	ev_uintptr_t uptr;
7668585484eSchristos 
7678585484eSchristos 	ev_ssize_t ssize;
7688585484eSchristos 
7698585484eSchristos 	tt_int_op(sizeof(u64), ==, 8);
7708585484eSchristos 	tt_int_op(sizeof(i64), ==, 8);
7718585484eSchristos 	tt_int_op(sizeof(u32), ==, 4);
7728585484eSchristos 	tt_int_op(sizeof(i32), ==, 4);
7738585484eSchristos 	tt_int_op(sizeof(u16), ==, 2);
7748585484eSchristos 	tt_int_op(sizeof(i16), ==, 2);
7758585484eSchristos 	tt_int_op(sizeof(u8), ==,  1);
7768585484eSchristos 	tt_int_op(sizeof(i8), ==,  1);
7778585484eSchristos 
7788585484eSchristos 	tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
7798585484eSchristos 	tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
7808585484eSchristos 	tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
7818585484eSchristos 
7828585484eSchristos 	u64 = 1000000000;
7838585484eSchristos 	u64 *= 1000000000;
7848585484eSchristos 	tt_assert(u64 / 1000000000 == 1000000000);
7858585484eSchristos 	i64 = -1000000000;
7868585484eSchristos 	i64 *= 1000000000;
7878585484eSchristos 	tt_assert(i64 / 1000000000 == -1000000000);
7888585484eSchristos 
7898585484eSchristos 	u64 = EV_UINT64_MAX;
7908585484eSchristos 	i64 = EV_INT64_MAX;
7918585484eSchristos 	tt_assert(u64 > 0);
7928585484eSchristos 	tt_assert(i64 > 0);
7938585484eSchristos 	u64++;
7947476e6e4Schristos /*	i64++; */
7958585484eSchristos 	tt_assert(u64 == 0);
7967476e6e4Schristos /*	tt_assert(i64 == EV_INT64_MIN); */
7977476e6e4Schristos /*	tt_assert(i64 < 0); */
7988585484eSchristos 
7998585484eSchristos 	u32 = EV_UINT32_MAX;
8008585484eSchristos 	i32 = EV_INT32_MAX;
8018585484eSchristos 	tt_assert(u32 > 0);
8028585484eSchristos 	tt_assert(i32 > 0);
8038585484eSchristos 	u32++;
8047476e6e4Schristos /*	i32++; */
8058585484eSchristos 	tt_assert(u32 == 0);
8067476e6e4Schristos /*	tt_assert(i32 == EV_INT32_MIN); */
8077476e6e4Schristos /*	tt_assert(i32 < 0); */
8088585484eSchristos 
8098585484eSchristos 	u16 = EV_UINT16_MAX;
8108585484eSchristos 	i16 = EV_INT16_MAX;
8118585484eSchristos 	tt_assert(u16 > 0);
8128585484eSchristos 	tt_assert(i16 > 0);
8138585484eSchristos 	u16++;
8147476e6e4Schristos /*	i16++; */
8158585484eSchristos 	tt_assert(u16 == 0);
8167476e6e4Schristos /*	tt_assert(i16 == EV_INT16_MIN); */
8177476e6e4Schristos /* 	tt_assert(i16 < 0); */
8188585484eSchristos 
8198585484eSchristos 	u8 = EV_UINT8_MAX;
8208585484eSchristos 	i8 = EV_INT8_MAX;
8218585484eSchristos 	tt_assert(u8 > 0);
8228585484eSchristos 	tt_assert(i8 > 0);
8238585484eSchristos 	u8++;
8247476e6e4Schristos /*	i8++;*/
8258585484eSchristos 	tt_assert(u8 == 0);
8267476e6e4Schristos /*	tt_assert(i8 == EV_INT8_MIN); */
8277476e6e4Schristos /*	tt_assert(i8 < 0); */
8288585484eSchristos 
8297476e6e4Schristos /*
8308585484eSchristos 	ssize = EV_SSIZE_MAX;
8318585484eSchristos 	tt_assert(ssize > 0);
8328585484eSchristos 	ssize++;
8338585484eSchristos 	tt_assert(ssize < 0);
8348585484eSchristos 	tt_assert(ssize == EV_SSIZE_MIN);
8357476e6e4Schristos */
8368585484eSchristos 
8378585484eSchristos 	ptr = &ssize;
8388585484eSchristos 	iptr = (ev_intptr_t)ptr;
8398585484eSchristos 	uptr = (ev_uintptr_t)ptr;
8408585484eSchristos 	ptr = (void *)iptr;
8418585484eSchristos 	tt_assert(ptr == &ssize);
8428585484eSchristos 	ptr = (void *)uptr;
8438585484eSchristos 	tt_assert(ptr == &ssize);
8448585484eSchristos 
8458585484eSchristos 	iptr = -1;
8468585484eSchristos 	tt_assert(iptr < 0);
8478585484eSchristos end:
8488585484eSchristos 	;
8498585484eSchristos }
8508585484eSchristos 
8518585484eSchristos struct evutil_addrinfo *
8528585484eSchristos ai_find_by_family(struct evutil_addrinfo *ai, int family)
8538585484eSchristos {
8548585484eSchristos 	while (ai) {
8558585484eSchristos 		if (ai->ai_family == family)
8568585484eSchristos 			return ai;
8578585484eSchristos 		ai = ai->ai_next;
8588585484eSchristos 	}
8598585484eSchristos 	return NULL;
8608585484eSchristos }
8618585484eSchristos 
8628585484eSchristos struct evutil_addrinfo *
8638585484eSchristos ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
8648585484eSchristos {
8658585484eSchristos 	while (ai) {
8668585484eSchristos 		if (ai->ai_protocol == protocol)
8678585484eSchristos 			return ai;
8688585484eSchristos 		ai = ai->ai_next;
8698585484eSchristos 	}
8708585484eSchristos 	return NULL;
8718585484eSchristos }
8728585484eSchristos 
8738585484eSchristos 
8748585484eSchristos int
8758585484eSchristos test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
8768585484eSchristos     int socktype, int protocol, int line)
8778585484eSchristos {
8788585484eSchristos 	struct sockaddr_storage ss;
8798585484eSchristos 	int slen = sizeof(ss);
8808585484eSchristos 	int gotport;
8818585484eSchristos 	char buf[128];
8828585484eSchristos 	memset(&ss, 0, sizeof(ss));
8838585484eSchristos 	if (socktype > 0)
8848585484eSchristos 		tt_int_op(ai->ai_socktype, ==, socktype);
8858585484eSchristos 	if (protocol > 0)
8868585484eSchristos 		tt_int_op(ai->ai_protocol, ==, protocol);
8878585484eSchristos 
8888585484eSchristos 	if (evutil_parse_sockaddr_port(
8898585484eSchristos 		    sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
8908585484eSchristos 		TT_FAIL(("Couldn't parse expected address %s on line %d",
8918585484eSchristos 			sockaddr_port, line));
8928585484eSchristos 		return -1;
8938585484eSchristos 	}
8948585484eSchristos 	if (ai->ai_family != ss.ss_family) {
8958585484eSchristos 		TT_FAIL(("Address family %d did not match %d on line %d",
8968585484eSchristos 			ai->ai_family, ss.ss_family, line));
8978585484eSchristos 		return -1;
8988585484eSchristos 	}
8998585484eSchristos 	if (ai->ai_addr->sa_family == AF_INET) {
9008585484eSchristos 		struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
9018585484eSchristos 		evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
9028585484eSchristos 		gotport = ntohs(sin->sin_port);
9038585484eSchristos 		if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
9048585484eSchristos 			TT_FAIL(("Addr size mismatch on line %d", line));
9058585484eSchristos 			return -1;
9068585484eSchristos 		}
9078585484eSchristos 	} else {
9088585484eSchristos 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
9098585484eSchristos 		evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
9108585484eSchristos 		gotport = ntohs(sin6->sin6_port);
9118585484eSchristos 		if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
9128585484eSchristos 			TT_FAIL(("Addr size mismatch on line %d", line));
9138585484eSchristos 			return -1;
9148585484eSchristos 		}
9158585484eSchristos 	}
9168585484eSchristos 	if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
9178585484eSchristos 		TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
9188585484eSchristos 			buf, gotport, line));
9198585484eSchristos 		return -1;
9208585484eSchristos 	} else {
9218585484eSchristos 		TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
9228585484eSchristos 			buf, gotport, line));
9238585484eSchristos 	}
9248585484eSchristos 	return 0;
9258585484eSchristos end:
9268585484eSchristos 	TT_FAIL(("Test failed on line %d", line));
9278585484eSchristos 	return -1;
9288585484eSchristos }
9298585484eSchristos 
9308585484eSchristos static void
9318585484eSchristos test_evutil_rand(void *arg)
9328585484eSchristos {
9338585484eSchristos 	char buf1[32];
9348585484eSchristos 	char buf2[32];
9358585484eSchristos 	int counts[256];
9368585484eSchristos 	int i, j, k, n=0;
9378585484eSchristos 	struct evutil_weakrand_state seed = { 12346789U };
9388585484eSchristos 
9398585484eSchristos 	memset(buf2, 0, sizeof(buf2));
9408585484eSchristos 	memset(counts, 0, sizeof(counts));
9418585484eSchristos 
9428585484eSchristos 	for (k=0;k<32;++k) {
9438585484eSchristos 		/* Try a few different start and end points; try to catch
9448585484eSchristos 		 * the various misaligned cases of arc4random_buf */
9458585484eSchristos 		int startpoint = evutil_weakrand_(&seed) % 4;
9468585484eSchristos 		int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
9478585484eSchristos 
9488585484eSchristos 		memset(buf2, 0, sizeof(buf2));
9498585484eSchristos 
9508585484eSchristos 		/* Do 6 runs over buf1, or-ing the result into buf2 each
9518585484eSchristos 		 * time, to make sure we're setting each byte that we mean
9528585484eSchristos 		 * to set. */
9538585484eSchristos 		for (i=0;i<8;++i) {
9548585484eSchristos 			memset(buf1, 0, sizeof(buf1));
9558585484eSchristos 			evutil_secure_rng_get_bytes(buf1 + startpoint,
9568585484eSchristos 			    endpoint-startpoint);
9578585484eSchristos 			n += endpoint - startpoint;
9588585484eSchristos 			for (j=0; j<32; ++j) {
9598585484eSchristos 				if (j >= startpoint && j < endpoint) {
9608585484eSchristos 					buf2[j] |= buf1[j];
9618585484eSchristos 					++counts[(unsigned char)buf1[j]];
9628585484eSchristos 				} else {
9638585484eSchristos 					tt_assert(buf1[j] == 0);
9648585484eSchristos 					tt_int_op(buf1[j], ==, 0);
9658585484eSchristos 
9668585484eSchristos 				}
9678585484eSchristos 			}
9688585484eSchristos 		}
9698585484eSchristos 
9708585484eSchristos 		/* This will give a false positive with P=(256**8)==(2**64)
9718585484eSchristos 		 * for each character. */
9728585484eSchristos 		for (j=startpoint;j<endpoint;++j) {
9738585484eSchristos 			tt_int_op(buf2[j], !=, 0);
9748585484eSchristos 		}
9758585484eSchristos 	}
9768585484eSchristos 
9778585484eSchristos 	evutil_weakrand_seed_(&seed, 0);
9788585484eSchristos 	for (i = 0; i < 10000; ++i) {
9798585484eSchristos 		ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
9808585484eSchristos 		tt_int_op(0, <=, r);
9818585484eSchristos 		tt_int_op(r, <, 9999);
9828585484eSchristos 	}
9838585484eSchristos 
9848585484eSchristos 	/* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
9858585484eSchristos end:
9868585484eSchristos 	;
9878585484eSchristos }
9888585484eSchristos 
9898585484eSchristos static void
990*eabc0478Schristos test_EVUTIL_IS_(void *arg)
991*eabc0478Schristos {
992*eabc0478Schristos 	tt_int_op(EVUTIL_ISDIGIT_('0'), ==, 1);
993*eabc0478Schristos 	tt_int_op(EVUTIL_ISDIGIT_('a'), ==, 0);
994*eabc0478Schristos 	tt_int_op(EVUTIL_ISDIGIT_('\xff'), ==, 0);
995*eabc0478Schristos end:
996*eabc0478Schristos 	;
997*eabc0478Schristos }
998*eabc0478Schristos 
999*eabc0478Schristos static void
10008585484eSchristos test_evutil_getaddrinfo(void *arg)
10018585484eSchristos {
10028585484eSchristos 	struct evutil_addrinfo *ai = NULL, *a;
10038585484eSchristos 	struct evutil_addrinfo hints;
10048585484eSchristos 	int r;
10058585484eSchristos 
10068585484eSchristos 	/* Try using it as a pton. */
10078585484eSchristos 	memset(&hints, 0, sizeof(hints));
10088585484eSchristos 	hints.ai_family = PF_UNSPEC;
10098585484eSchristos 	hints.ai_socktype = SOCK_STREAM;
10108585484eSchristos 	r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
10118585484eSchristos 	tt_int_op(r, ==, 0);
10128585484eSchristos 	tt_assert(ai);
10138585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
10148585484eSchristos 	test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
10158585484eSchristos 	evutil_freeaddrinfo(ai);
10168585484eSchristos 	ai = NULL;
10178585484eSchristos 
10188585484eSchristos 	memset(&hints, 0, sizeof(hints));
10198585484eSchristos 	hints.ai_family = PF_UNSPEC;
10208585484eSchristos 	hints.ai_protocol = IPPROTO_UDP;
10218585484eSchristos 	r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
10228585484eSchristos 	tt_int_op(r, ==, 0);
10238585484eSchristos 	tt_assert(ai);
10248585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
10258585484eSchristos 	test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
10268585484eSchristos 	evutil_freeaddrinfo(ai);
10278585484eSchristos 	ai = NULL;
10288585484eSchristos 
10298585484eSchristos 	/* Try out the behavior of nodename=NULL */
10308585484eSchristos 	memset(&hints, 0, sizeof(hints));
10318585484eSchristos 	hints.ai_family = PF_INET;
10328585484eSchristos 	hints.ai_protocol = IPPROTO_TCP;
10338585484eSchristos 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
10348585484eSchristos 	r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
10358585484eSchristos 	tt_int_op(r,==,0);
10368585484eSchristos 	tt_assert(ai);
10378585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL);
10388585484eSchristos 	test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
10398585484eSchristos 	evutil_freeaddrinfo(ai);
10408585484eSchristos 	ai = NULL;
10418585484eSchristos 	hints.ai_flags = 0; /* as if for connect */
10428585484eSchristos 	r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
10438585484eSchristos 	tt_assert(ai);
10448585484eSchristos 	tt_int_op(r,==,0);
10458585484eSchristos 	test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
10468585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL);
10478585484eSchristos 	evutil_freeaddrinfo(ai);
10488585484eSchristos 	ai = NULL;
10498585484eSchristos 
10508585484eSchristos 	hints.ai_flags = 0; /* as if for connect */
10518585484eSchristos 	hints.ai_family = PF_INET6;
10528585484eSchristos 	r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
10538585484eSchristos 	tt_assert(ai);
10548585484eSchristos 	tt_int_op(r,==,0);
10558585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL);
10568585484eSchristos 	test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
10578585484eSchristos 	evutil_freeaddrinfo(ai);
10588585484eSchristos 	ai = NULL;
10598585484eSchristos 
10608585484eSchristos 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
10618585484eSchristos 	hints.ai_family = PF_INET6;
10628585484eSchristos 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
10638585484eSchristos 	tt_assert(ai);
10648585484eSchristos 	tt_int_op(r,==,0);
10658585484eSchristos 	tt_ptr_op(ai->ai_next, ==, NULL);
10668585484eSchristos 	test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
10678585484eSchristos 	evutil_freeaddrinfo(ai);
10688585484eSchristos 	ai = NULL;
10698585484eSchristos 
10708585484eSchristos 	/* Now try an unspec one. We should get a v6 and a v4. */
10718585484eSchristos 	hints.ai_family = PF_UNSPEC;
10728585484eSchristos 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
10738585484eSchristos 	tt_assert(ai);
10748585484eSchristos 	tt_int_op(r,==,0);
10758585484eSchristos 	a = ai_find_by_family(ai, PF_INET6);
10768585484eSchristos 	tt_assert(a);
10778585484eSchristos 	test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
10788585484eSchristos 	a = ai_find_by_family(ai, PF_INET);
10798585484eSchristos 	tt_assert(a);
10808585484eSchristos 	test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
10818585484eSchristos 	evutil_freeaddrinfo(ai);
10828585484eSchristos 	ai = NULL;
10838585484eSchristos 
10848585484eSchristos 	/* Try out AI_NUMERICHOST: successful case.  Also try
10858585484eSchristos 	 * multiprotocol. */
10868585484eSchristos 	memset(&hints, 0, sizeof(hints));
10878585484eSchristos 	hints.ai_family = PF_UNSPEC;
10888585484eSchristos 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
10898585484eSchristos 	r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
10908585484eSchristos 	tt_int_op(r, ==, 0);
10918585484eSchristos 	a = ai_find_by_protocol(ai, IPPROTO_TCP);
10928585484eSchristos 	tt_assert(a);
10938585484eSchristos 	test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
10948585484eSchristos 	a = ai_find_by_protocol(ai, IPPROTO_UDP);
10958585484eSchristos 	tt_assert(a);
10968585484eSchristos 	test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
10978585484eSchristos 	evutil_freeaddrinfo(ai);
10988585484eSchristos 	ai = NULL;
10998585484eSchristos 
11008585484eSchristos 	/* Try the failing case of AI_NUMERICHOST */
11018585484eSchristos 	memset(&hints, 0, sizeof(hints));
11028585484eSchristos 	hints.ai_family = PF_UNSPEC;
11038585484eSchristos 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
11048585484eSchristos 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
11058585484eSchristos 	tt_int_op(r, ==, EVUTIL_EAI_NONAME);
11068585484eSchristos 	tt_ptr_op(ai, ==, NULL);
11078585484eSchristos 
11088585484eSchristos 	/* Try symbolic service names wit AI_NUMERICSERV */
11098585484eSchristos 	memset(&hints, 0, sizeof(hints));
11108585484eSchristos 	hints.ai_family = PF_UNSPEC;
11118585484eSchristos 	hints.ai_socktype = SOCK_STREAM;
11128585484eSchristos 	hints.ai_flags = EVUTIL_AI_NUMERICSERV;
11138585484eSchristos 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
11148585484eSchristos 	tt_int_op(r,==,EVUTIL_EAI_NONAME);
11158585484eSchristos 
11168585484eSchristos 	/* Try symbolic service names */
11178585484eSchristos 	memset(&hints, 0, sizeof(hints));
11188585484eSchristos 	hints.ai_family = PF_UNSPEC;
11198585484eSchristos 	hints.ai_socktype = SOCK_STREAM;
11208585484eSchristos 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
11218585484eSchristos 	if (r!=0) {
11228585484eSchristos 		TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
11238585484eSchristos 	} else {
11248585484eSchristos 		tt_assert(ai);
11258585484eSchristos 		test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
11268585484eSchristos 		evutil_freeaddrinfo(ai);
11278585484eSchristos 		ai = NULL;
11288585484eSchristos 	}
11298585484eSchristos 
11308585484eSchristos end:
11318585484eSchristos 	if (ai)
11328585484eSchristos 		evutil_freeaddrinfo(ai);
11338585484eSchristos }
11348585484eSchristos 
11358585484eSchristos static void
11368585484eSchristos test_evutil_getaddrinfo_live(void *arg)
11378585484eSchristos {
11388585484eSchristos 	struct evutil_addrinfo *ai = NULL;
11398585484eSchristos 	struct evutil_addrinfo hints;
11408585484eSchristos 
11418585484eSchristos 	struct sockaddr_in6 *sin6;
11428585484eSchristos 	struct sockaddr_in *sin;
11438585484eSchristos 	char buf[128];
11448585484eSchristos 	const char *cp;
11458585484eSchristos 	int r;
11468585484eSchristos 
11478585484eSchristos 	/* Now do some actual lookups. */
11488585484eSchristos 	memset(&hints, 0, sizeof(hints));
11498585484eSchristos 	hints.ai_family = PF_INET;
11508585484eSchristos 	hints.ai_protocol = IPPROTO_TCP;
11518585484eSchristos 	hints.ai_socktype = SOCK_STREAM;
11528585484eSchristos 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
11538585484eSchristos 	if (r != 0) {
11548585484eSchristos 		TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
11558585484eSchristos 	} else {
11568585484eSchristos 		tt_assert(ai);
11578585484eSchristos 		tt_int_op(ai->ai_family, ==, PF_INET);
11588585484eSchristos 		tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
11598585484eSchristos 		tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
11608585484eSchristos 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
11618585484eSchristos 		sin = (struct sockaddr_in*)ai->ai_addr;
11628585484eSchristos 		tt_int_op(sin->sin_family, ==, AF_INET);
11638585484eSchristos 		tt_int_op(sin->sin_port, ==, htons(80));
11648585484eSchristos 		tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
11658585484eSchristos 
11668585484eSchristos 		cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
11678585484eSchristos 		TT_BLATHER(("www.google.com resolved to %s",
11688585484eSchristos 			cp?cp:"<unwriteable>"));
11698585484eSchristos 		evutil_freeaddrinfo(ai);
11708585484eSchristos 		ai = NULL;
11718585484eSchristos 	}
11728585484eSchristos 
11738585484eSchristos 	hints.ai_family = PF_INET6;
11748585484eSchristos 	r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
11758585484eSchristos 	if (r != 0) {
11768585484eSchristos 		TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
11778585484eSchristos 	} else {
11788585484eSchristos 		tt_assert(ai);
11798585484eSchristos 		tt_int_op(ai->ai_family, ==, PF_INET6);
11808585484eSchristos 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
11818585484eSchristos 		sin6 = (struct sockaddr_in6*)ai->ai_addr;
11828585484eSchristos 		tt_int_op(sin6->sin6_port, ==, htons(80));
11838585484eSchristos 
11848585484eSchristos 		cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
11858585484eSchristos 		    sizeof(buf));
11868585484eSchristos 		TT_BLATHER(("ipv6.google.com resolved to %s",
11878585484eSchristos 			cp?cp:"<unwriteable>"));
11888585484eSchristos 	}
11898585484eSchristos 
11908585484eSchristos end:
11918585484eSchristos 	if (ai)
11928585484eSchristos 		evutil_freeaddrinfo(ai);
11938585484eSchristos }
11948585484eSchristos 
1195*eabc0478Schristos static void
1196*eabc0478Schristos test_evutil_getaddrinfo_AI_ADDRCONFIG(void *arg)
1197*eabc0478Schristos {
1198*eabc0478Schristos 	struct evutil_addrinfo *ai = NULL;
1199*eabc0478Schristos 	struct evutil_addrinfo hints;
1200*eabc0478Schristos 	int r;
1201*eabc0478Schristos 
1202*eabc0478Schristos 	memset(&hints, 0, sizeof(hints));
1203*eabc0478Schristos 	hints.ai_family = AF_UNSPEC;
1204*eabc0478Schristos 	hints.ai_socktype = SOCK_STREAM;
1205*eabc0478Schristos 	hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
1206*eabc0478Schristos 
1207*eabc0478Schristos 	/* IPv4 */
1208*eabc0478Schristos 	r = evutil_getaddrinfo("127.0.0.1", "80", &hints, &ai);
1209*eabc0478Schristos 	tt_int_op(r, ==, 0);
1210*eabc0478Schristos 	tt_assert(ai);
1211*eabc0478Schristos 	tt_ptr_op(ai->ai_next, ==, NULL);
1212*eabc0478Schristos 	test_ai_eq(ai, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP);
1213*eabc0478Schristos 	evutil_freeaddrinfo(ai);
1214*eabc0478Schristos 	ai = NULL;
1215*eabc0478Schristos 
1216*eabc0478Schristos 	/* IPv6 */
1217*eabc0478Schristos 	r = evutil_getaddrinfo("::1", "80", &hints, &ai);
1218*eabc0478Schristos 	tt_int_op(r, ==, 0);
1219*eabc0478Schristos 	tt_assert(ai);
1220*eabc0478Schristos 	tt_ptr_op(ai->ai_next, ==, NULL);
1221*eabc0478Schristos 	test_ai_eq(ai, "[::1]:80", SOCK_STREAM, IPPROTO_TCP);
1222*eabc0478Schristos 	evutil_freeaddrinfo(ai);
1223*eabc0478Schristos 	ai = NULL;
1224*eabc0478Schristos 
1225*eabc0478Schristos end:
1226*eabc0478Schristos 	if (ai)
1227*eabc0478Schristos 		evutil_freeaddrinfo(ai);
1228*eabc0478Schristos }
1229*eabc0478Schristos 
12308585484eSchristos #ifdef _WIN32
12318585484eSchristos static void
12328585484eSchristos test_evutil_loadsyslib(void *arg)
12338585484eSchristos {
12347476e6e4Schristos 	HMODULE h=NULL;
12358585484eSchristos 
12368585484eSchristos 	h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
12378585484eSchristos 	tt_assert(h);
12388585484eSchristos 
12398585484eSchristos end:
12408585484eSchristos 	if (h)
12418585484eSchristos 		CloseHandle(h);
12428585484eSchristos 
12438585484eSchristos }
12448585484eSchristos #endif
12458585484eSchristos 
12468585484eSchristos /** Test mm_malloc(). */
12478585484eSchristos static void
12488585484eSchristos test_event_malloc(void *arg)
12498585484eSchristos {
12508585484eSchristos 	void *p = NULL;
12518585484eSchristos 	(void)arg;
12528585484eSchristos 
12538585484eSchristos 	/* mm_malloc(0) should simply return NULL. */
12548585484eSchristos #ifndef EVENT__DISABLE_MM_REPLACEMENT
12558585484eSchristos 	errno = 0;
12568585484eSchristos 	p = mm_malloc(0);
12578585484eSchristos 	tt_assert(p == NULL);
12588585484eSchristos 	tt_int_op(errno, ==, 0);
12598585484eSchristos #endif
12608585484eSchristos 
12618585484eSchristos 	/* Trivial case. */
12628585484eSchristos 	errno = 0;
12638585484eSchristos 	p = mm_malloc(8);
12648585484eSchristos 	tt_assert(p != NULL);
12658585484eSchristos 	tt_int_op(errno, ==, 0);
12668585484eSchristos 	mm_free(p);
12678585484eSchristos 
12688585484eSchristos  end:
12698585484eSchristos 	errno = 0;
12708585484eSchristos 	return;
12718585484eSchristos }
12728585484eSchristos 
12738585484eSchristos static void
12748585484eSchristos test_event_calloc(void *arg)
12758585484eSchristos {
12768585484eSchristos 	void *p = NULL;
12778585484eSchristos 	(void)arg;
12788585484eSchristos 
12798585484eSchristos #ifndef EVENT__DISABLE_MM_REPLACEMENT
12808585484eSchristos 	/* mm_calloc() should simply return NULL
12818585484eSchristos 	 * if either argument is zero. */
12828585484eSchristos 	errno = 0;
12838585484eSchristos 	p = mm_calloc(0, 0);
12848585484eSchristos 	tt_assert(p == NULL);
12858585484eSchristos 	tt_int_op(errno, ==, 0);
12868585484eSchristos 	errno = 0;
12878585484eSchristos 	p = mm_calloc(0, 1);
12888585484eSchristos 	tt_assert(p == NULL);
12898585484eSchristos 	tt_int_op(errno, ==, 0);
12908585484eSchristos 	errno = 0;
12918585484eSchristos 	p = mm_calloc(1, 0);
12928585484eSchristos 	tt_assert(p == NULL);
12938585484eSchristos 	tt_int_op(errno, ==, 0);
12948585484eSchristos #endif
12958585484eSchristos 
12968585484eSchristos 	/* Trivial case. */
12978585484eSchristos 	errno = 0;
12988585484eSchristos 	p = mm_calloc(8, 8);
12998585484eSchristos 	tt_assert(p != NULL);
13008585484eSchristos 	tt_int_op(errno, ==, 0);
13018585484eSchristos 	mm_free(p);
13028585484eSchristos 	p = NULL;
13038585484eSchristos 
13048585484eSchristos 	/* mm_calloc() should set errno = ENOMEM and return NULL
13058585484eSchristos 	 * in case of potential overflow. */
13068585484eSchristos 	errno = 0;
13078585484eSchristos 	p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
13088585484eSchristos 	tt_assert(p == NULL);
13098585484eSchristos 	tt_int_op(errno, ==, ENOMEM);
13108585484eSchristos 
13118585484eSchristos  end:
13128585484eSchristos 	errno = 0;
13138585484eSchristos 	if (p)
13148585484eSchristos 		mm_free(p);
13158585484eSchristos 
13168585484eSchristos 	return;
13178585484eSchristos }
13188585484eSchristos 
13198585484eSchristos static void
13208585484eSchristos test_event_strdup(void *arg)
13218585484eSchristos {
13228585484eSchristos 	void *p = NULL;
13238585484eSchristos 	(void)arg;
13248585484eSchristos 
13258585484eSchristos #ifndef EVENT__DISABLE_MM_REPLACEMENT
13268585484eSchristos 	/* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
13278585484eSchristos 	errno = 0;
13288585484eSchristos 	p = mm_strdup(NULL);
13298585484eSchristos 	tt_assert(p == NULL);
13308585484eSchristos 	tt_int_op(errno, ==, EINVAL);
13318585484eSchristos #endif
13328585484eSchristos 
13338585484eSchristos 	/* Trivial cases. */
13348585484eSchristos 
13358585484eSchristos 	errno = 0;
13368585484eSchristos 	p = mm_strdup("");
13378585484eSchristos 	tt_assert(p != NULL);
13388585484eSchristos 	tt_int_op(errno, ==, 0);
13398585484eSchristos 	tt_str_op(p, ==, "");
13408585484eSchristos 	mm_free(p);
13418585484eSchristos 
13428585484eSchristos 	errno = 0;
13438585484eSchristos 	p = mm_strdup("foo");
13448585484eSchristos 	tt_assert(p != NULL);
13458585484eSchristos 	tt_int_op(errno, ==, 0);
13468585484eSchristos 	tt_str_op(p, ==, "foo");
13478585484eSchristos 	mm_free(p);
13488585484eSchristos 
13498585484eSchristos 	/* XXX
13508585484eSchristos 	 * mm_strdup(str) where str is a string of length EV_SIZE_MAX
13518585484eSchristos 	 * should set errno = ENOMEM and return NULL. */
13528585484eSchristos 
13538585484eSchristos  end:
13548585484eSchristos 	errno = 0;
13558585484eSchristos 	return;
13568585484eSchristos }
13578585484eSchristos 
13588585484eSchristos static void
13598585484eSchristos test_evutil_usleep(void *arg)
13608585484eSchristos {
13618585484eSchristos 	struct timeval tv1, tv2, tv3, diff1, diff2;
13628585484eSchristos 	const struct timeval quarter_sec = {0, 250*1000};
13638585484eSchristos 	const struct timeval tenth_sec = {0, 100*1000};
13648585484eSchristos 	long usec1, usec2;
13658585484eSchristos 
13668585484eSchristos 	evutil_gettimeofday(&tv1, NULL);
13678585484eSchristos 	evutil_usleep_(&quarter_sec);
13688585484eSchristos 	evutil_gettimeofday(&tv2, NULL);
13698585484eSchristos 	evutil_usleep_(&tenth_sec);
13708585484eSchristos 	evutil_gettimeofday(&tv3, NULL);
13718585484eSchristos 
13728585484eSchristos 	evutil_timersub(&tv2, &tv1, &diff1);
13738585484eSchristos 	evutil_timersub(&tv3, &tv2, &diff2);
13748585484eSchristos 	usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
13758585484eSchristos 	usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
13768585484eSchristos 
13778585484eSchristos 	tt_int_op(usec1, >, 200000);
13788585484eSchristos 	tt_int_op(usec1, <, 300000);
13798585484eSchristos 	tt_int_op(usec2, >,  80000);
13808585484eSchristos 	tt_int_op(usec2, <, 120000);
13818585484eSchristos 
13828585484eSchristos end:
13838585484eSchristos 	;
13848585484eSchristos }
13858585484eSchristos 
13868585484eSchristos static void
13878585484eSchristos test_evutil_monotonic_res(void *data_)
13888585484eSchristos {
13898585484eSchristos 	/* Basic santity-test for monotonic timers.  What we'd really like
13908585484eSchristos 	 * to do is make sure that they can't go backwards even when the
13918585484eSchristos 	 * system clock goes backwards. But we haven't got a good way to
13928585484eSchristos 	 * move the system clock backwards.
13938585484eSchristos 	 */
13948585484eSchristos 	struct basic_test_data *data = data_;
13958585484eSchristos 	struct evutil_monotonic_timer timer;
13968585484eSchristos 	const int precise = strstr(data->setup_data, "precise") != NULL;
13978585484eSchristos 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
13988585484eSchristos 	struct timeval tv[10], delay;
13998585484eSchristos 	int total_diff = 0;
14008585484eSchristos 
14018585484eSchristos 	int flags = 0, wantres, acceptdiff, i;
14028585484eSchristos 	if (precise)
14038585484eSchristos 		flags |= EV_MONOT_PRECISE;
14048585484eSchristos 	if (fallback)
14058585484eSchristos 		flags |= EV_MONOT_FALLBACK;
14068585484eSchristos 	if (precise || fallback) {
14078585484eSchristos #ifdef _WIN32
14088585484eSchristos 		wantres = 10*1000;
14098585484eSchristos 		acceptdiff = 1000;
14108585484eSchristos #else
14118585484eSchristos 		wantres = 1000;
14128585484eSchristos 		acceptdiff = 300;
14138585484eSchristos #endif
14148585484eSchristos 	} else {
14158585484eSchristos 		wantres = 40*1000;
14168585484eSchristos 		acceptdiff = 20*1000;
14178585484eSchristos 	}
14188585484eSchristos 
14198585484eSchristos 	TT_BLATHER(("Precise = %d", precise));
14208585484eSchristos 	TT_BLATHER(("Fallback = %d", fallback));
14218585484eSchristos 
14228585484eSchristos 	/* First, make sure we match up with usleep. */
14238585484eSchristos 
14248585484eSchristos 	delay.tv_sec = 0;
14258585484eSchristos 	delay.tv_usec = wantres;
14268585484eSchristos 
14278585484eSchristos 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
14288585484eSchristos 
14298585484eSchristos 	for (i = 0; i < 10; ++i) {
14308585484eSchristos 		evutil_gettime_monotonic_(&timer, &tv[i]);
14318585484eSchristos 		evutil_usleep_(&delay);
14328585484eSchristos 	}
14338585484eSchristos 
14348585484eSchristos 	for (i = 0; i < 9; ++i) {
14358585484eSchristos 		struct timeval diff;
14368585484eSchristos 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
14378585484eSchristos 		evutil_timersub(&tv[i+1], &tv[i], &diff);
14388585484eSchristos 		tt_int_op(diff.tv_sec, ==, 0);
14398585484eSchristos 		total_diff += diff.tv_usec;
14408585484eSchristos 		TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
14418585484eSchristos 	}
14428585484eSchristos 	tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
14438585484eSchristos 
14448585484eSchristos end:
14458585484eSchristos 	;
14468585484eSchristos }
14478585484eSchristos 
14488585484eSchristos static void
14498585484eSchristos test_evutil_monotonic_prc(void *data_)
14508585484eSchristos {
14518585484eSchristos 	struct basic_test_data *data = data_;
14528585484eSchristos 	struct evutil_monotonic_timer timer;
14538585484eSchristos 	const int precise = strstr(data->setup_data, "precise") != NULL;
14548585484eSchristos 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
14558585484eSchristos 	struct timeval tv[10];
14568585484eSchristos 	int total_diff = 0;
14578585484eSchristos 	int i, maxstep = 25*1000,flags=0;
14588585484eSchristos 	if (precise)
14598585484eSchristos 		maxstep = 500;
14608585484eSchristos 	if (precise)
14618585484eSchristos 		flags |= EV_MONOT_PRECISE;
14628585484eSchristos 	if (fallback)
14638585484eSchristos 		flags |= EV_MONOT_FALLBACK;
14648585484eSchristos 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
14658585484eSchristos 
14668585484eSchristos 	/* find out what precision we actually see. */
14678585484eSchristos 
14688585484eSchristos 	evutil_gettime_monotonic_(&timer, &tv[0]);
14698585484eSchristos 	for (i = 1; i < 10; ++i) {
14708585484eSchristos 		do {
14718585484eSchristos 			evutil_gettime_monotonic_(&timer, &tv[i]);
14728585484eSchristos 		} while (evutil_timercmp(&tv[i-1], &tv[i], ==));
14738585484eSchristos 	}
14748585484eSchristos 
14758585484eSchristos 	total_diff = 0;
14768585484eSchristos 	for (i = 0; i < 9; ++i) {
14778585484eSchristos 		struct timeval diff;
14788585484eSchristos 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
14798585484eSchristos 		evutil_timersub(&tv[i+1], &tv[i], &diff);
14808585484eSchristos 		tt_int_op(diff.tv_sec, ==, 0);
14818585484eSchristos 		total_diff += diff.tv_usec;
14828585484eSchristos 		TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
14838585484eSchristos 	}
14848585484eSchristos 	TT_BLATHER(("Average step difference = %d", total_diff / 9));
14858585484eSchristos 	tt_int_op(total_diff/9, <, maxstep);
14868585484eSchristos 
14878585484eSchristos end:
14888585484eSchristos 	;
14898585484eSchristos }
14908585484eSchristos 
1491*eabc0478Schristos static void
1492*eabc0478Schristos create_tm_from_unix_epoch(struct tm *cur_p, const time_t t)
1493*eabc0478Schristos {
1494*eabc0478Schristos #ifdef _WIN32
1495*eabc0478Schristos 	struct tm *tmp = gmtime(&t);
1496*eabc0478Schristos 	if (!tmp) {
1497*eabc0478Schristos 		fprintf(stderr, "gmtime: %s (%i)", strerror(errno), (int)t);
1498*eabc0478Schristos 		exit(1);
1499*eabc0478Schristos 	}
1500*eabc0478Schristos 	*cur_p = *tmp;
1501*eabc0478Schristos #else
1502*eabc0478Schristos 	gmtime_r(&t, cur_p);
1503*eabc0478Schristos #endif
1504*eabc0478Schristos }
1505*eabc0478Schristos 
1506*eabc0478Schristos static struct date_rfc1123_case {
1507*eabc0478Schristos 	time_t t;
1508*eabc0478Schristos 	char date[30];
1509*eabc0478Schristos } date_rfc1123_cases[] = {
1510*eabc0478Schristos 	{           0, "Thu, 01 Jan 1970 00:00:00 GMT"} /* UNIX time of zero */,
1511*eabc0478Schristos 	{   946684799, "Fri, 31 Dec 1999 23:59:59 GMT"} /* the last moment of the 20th century */,
1512*eabc0478Schristos 	{   946684800, "Sat, 01 Jan 2000 00:00:00 GMT"} /* the first moment of the 21st century */,
1513*eabc0478Schristos 	{   981072000, "Fri, 02 Feb 2001 00:00:00 GMT"},
1514*eabc0478Schristos 	{  1015113600, "Sun, 03 Mar 2002 00:00:00 GMT"},
1515*eabc0478Schristos 	{  1049414400, "Fri, 04 Apr 2003 00:00:00 GMT"},
1516*eabc0478Schristos 	{  1083715200, "Wed, 05 May 2004 00:00:00 GMT"},
1517*eabc0478Schristos 	{  1118016000, "Mon, 06 Jun 2005 00:00:00 GMT"},
1518*eabc0478Schristos 	{  1152230400, "Fri, 07 Jul 2006 00:00:00 GMT"},
1519*eabc0478Schristos 	{  1186531200, "Wed, 08 Aug 2007 00:00:00 GMT"},
1520*eabc0478Schristos 	{  1220918400, "Tue, 09 Sep 2008 00:00:00 GMT"},
1521*eabc0478Schristos 	{  1255132800, "Sat, 10 Oct 2009 00:00:00 GMT"},
1522*eabc0478Schristos 	{  1289433600, "Thu, 11 Nov 2010 00:00:00 GMT"},
1523*eabc0478Schristos 	{  1323648000, "Mon, 12 Dec 2011 00:00:00 GMT"},
1524*eabc0478Schristos #ifndef _WIN32
1525*eabc0478Schristos #if EVENT__SIZEOF_TIME_T > 4
1526*eabc0478Schristos 	/** In win32 case we have max   "23:59:59 January 18, 2038, UTC" for time32 */
1527*eabc0478Schristos 	{  4294967296, "Sun, 07 Feb 2106 06:28:16 GMT"} /* 2^32 */,
1528*eabc0478Schristos 	/** In win32 case we have max "23:59:59, December 31, 3000, UTC" for time64 */
1529*eabc0478Schristos 	{253402300799, "Fri, 31 Dec 9999 23:59:59 GMT"} /* long long future no one can imagine */,
1530*eabc0478Schristos #endif /* time_t != 32bit */
1531*eabc0478Schristos 	{  1456704000, "Mon, 29 Feb 2016 00:00:00 GMT"} /* leap year */,
1532*eabc0478Schristos #endif
1533*eabc0478Schristos 	{  1435708800, "Wed, 01 Jul 2015 00:00:00 GMT"} /* leap second */,
1534*eabc0478Schristos 	{  1481866376, "Fri, 16 Dec 2016 05:32:56 GMT"} /* the time this test case is generated */,
1535*eabc0478Schristos 	{0, ""} /* end of test cases. */
1536*eabc0478Schristos };
1537*eabc0478Schristos 
1538*eabc0478Schristos static void
1539*eabc0478Schristos test_evutil_date_rfc1123(void *arg)
1540*eabc0478Schristos {
1541*eabc0478Schristos 	struct tm query;
1542*eabc0478Schristos 	char result[30];
1543*eabc0478Schristos 	size_t i = 0;
1544*eabc0478Schristos 
1545*eabc0478Schristos 	/* Checks if too small buffers are safely accepted. */
1546*eabc0478Schristos 	{
1547*eabc0478Schristos 		create_tm_from_unix_epoch(&query, 0);
1548*eabc0478Schristos 		evutil_date_rfc1123(result, 8, &query);
1549*eabc0478Schristos 		tt_str_op(result, ==, "Thu, 01");
1550*eabc0478Schristos 	}
1551*eabc0478Schristos 
1552*eabc0478Schristos 	/* Checks for testcases. */
1553*eabc0478Schristos 	for (i = 0; ; i++) {
1554*eabc0478Schristos 		struct date_rfc1123_case c = date_rfc1123_cases[i];
1555*eabc0478Schristos 
1556*eabc0478Schristos 		if (strlen(c.date) == 0)
1557*eabc0478Schristos 			break;
1558*eabc0478Schristos 
1559*eabc0478Schristos 		create_tm_from_unix_epoch(&query, c.t);
1560*eabc0478Schristos 		evutil_date_rfc1123(result, sizeof(result), &query);
1561*eabc0478Schristos 		tt_str_op(result, ==, c.date);
1562*eabc0478Schristos 	}
1563*eabc0478Schristos 
1564*eabc0478Schristos end:
1565*eabc0478Schristos 	;
1566*eabc0478Schristos }
1567*eabc0478Schristos 
1568*eabc0478Schristos static void
1569*eabc0478Schristos test_evutil_v4addr_is_local(void *arg)
1570*eabc0478Schristos {
1571*eabc0478Schristos 	struct sockaddr_in sin;
1572*eabc0478Schristos 	sin.sin_family = AF_INET;
1573*eabc0478Schristos 
1574*eabc0478Schristos 	/* we use evutil_inet_pton() here to fill in network-byte order */
1575*eabc0478Schristos #define LOCAL(str, yes) do {                                              \
1576*eabc0478Schristos 	tt_int_op(evutil_inet_pton(AF_INET, str, &sin.sin_addr), ==, 1);  \
1577*eabc0478Schristos 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, yes);       \
1578*eabc0478Schristos } while (0)
1579*eabc0478Schristos 
1580*eabc0478Schristos 	/** any */
1581*eabc0478Schristos 	sin.sin_addr.s_addr = INADDR_ANY;
1582*eabc0478Schristos 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1583*eabc0478Schristos 
1584*eabc0478Schristos 	/** loopback */
1585*eabc0478Schristos 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1586*eabc0478Schristos 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1587*eabc0478Schristos 	LOCAL("127.0.0.1", 1);
1588*eabc0478Schristos 	LOCAL("127.255.255.255", 1);
1589*eabc0478Schristos 	LOCAL("121.0.0.1", 0);
1590*eabc0478Schristos 
1591*eabc0478Schristos 	/** link-local */
1592*eabc0478Schristos 	LOCAL("169.254.0.1", 1);
1593*eabc0478Schristos 	LOCAL("169.254.255.255", 1);
1594*eabc0478Schristos 	LOCAL("170.0.0.0", 0);
1595*eabc0478Schristos 
1596*eabc0478Schristos 	/** Multicast */
1597*eabc0478Schristos 	LOCAL("224.0.0.0", 1);
1598*eabc0478Schristos 	LOCAL("239.255.255.255", 1);
1599*eabc0478Schristos 	LOCAL("240.0.0.0", 0);
1600*eabc0478Schristos end:
1601*eabc0478Schristos 	;
1602*eabc0478Schristos }
1603*eabc0478Schristos 
1604*eabc0478Schristos static void
1605*eabc0478Schristos test_evutil_v6addr_is_local(void *arg)
1606*eabc0478Schristos {
1607*eabc0478Schristos 	struct sockaddr_in6 sin6;
1608*eabc0478Schristos 	struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
1609*eabc0478Schristos 	struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
1610*eabc0478Schristos 
1611*eabc0478Schristos 	sin6.sin6_family = AF_INET6;
1612*eabc0478Schristos #define LOCAL6(str, yes) do {                                              \
1613*eabc0478Schristos 	tt_int_op(evutil_inet_pton(AF_INET6, str, &sin6.sin6_addr), ==, 1);\
1614*eabc0478Schristos 	tt_int_op(evutil_v6addr_is_local_(&sin6.sin6_addr), ==, yes);      \
1615*eabc0478Schristos } while (0)
1616*eabc0478Schristos 
1617*eabc0478Schristos 	/** any */
1618*eabc0478Schristos 	tt_int_op(evutil_v6addr_is_local_(&anyaddr), ==, 1);
1619*eabc0478Schristos 	LOCAL6("::0", 1);
1620*eabc0478Schristos 
1621*eabc0478Schristos 	/** loopback */
1622*eabc0478Schristos 	tt_int_op(evutil_v6addr_is_local_(&loopback), ==, 1);
1623*eabc0478Schristos 	LOCAL6("::1", 1);
1624*eabc0478Schristos 
1625*eabc0478Schristos 	/** IPV4 mapped */
1626*eabc0478Schristos 	LOCAL6("::ffff:0:0", 1);
1627*eabc0478Schristos 	/** IPv4 translated */
1628*eabc0478Schristos 	LOCAL6("::ffff:0:0:0", 1);
1629*eabc0478Schristos 	/** IPv4/IPv6 translation */
1630*eabc0478Schristos 	LOCAL6("64:ff9b::", 0);
1631*eabc0478Schristos 	/** Link-local */
1632*eabc0478Schristos 	LOCAL6("fe80::", 1);
1633*eabc0478Schristos 	/** Multicast */
1634*eabc0478Schristos 	LOCAL6("ff00::", 1);
1635*eabc0478Schristos 	/** Unspecified */
1636*eabc0478Schristos 	LOCAL6("::", 1);
1637*eabc0478Schristos 
1638*eabc0478Schristos 	/** Global Internet */
1639*eabc0478Schristos 	LOCAL6("2001::", 0);
1640*eabc0478Schristos 	LOCAL6("2001:4860:4802:32::1b", 0);
1641*eabc0478Schristos end:
1642*eabc0478Schristos 	;
1643*eabc0478Schristos }
1644*eabc0478Schristos 
16458585484eSchristos struct testcase_t util_testcases[] = {
16468585484eSchristos 	{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
16478585484eSchristos 	{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
1648*eabc0478Schristos 	{ "ipv6_parse_scope", regress_ipv6_parse_scope, 0, NULL, NULL },
16498585484eSchristos 	{ "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
16508585484eSchristos 	{ "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
16518585484eSchristos 	{ "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
16528585484eSchristos 	{ "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
16538585484eSchristos 	{ "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
16548585484eSchristos 	{ "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
16558585484eSchristos 	{ "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
16568585484eSchristos 	{ "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
16578585484eSchristos 	{ "log", test_evutil_log, TT_FORK, NULL, NULL },
16588585484eSchristos 	{ "upcast", test_evutil_upcast, 0, NULL, NULL },
16598585484eSchristos 	{ "integers", test_evutil_integers, 0, NULL, NULL },
16608585484eSchristos 	{ "rand", test_evutil_rand, TT_FORK, NULL, NULL },
1661*eabc0478Schristos 	{ "EVUTIL_IS_", test_EVUTIL_IS_, 0, NULL, NULL },
16628585484eSchristos 	{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
16638585484eSchristos 	{ "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1664*eabc0478Schristos 	{ "getaddrinfo_AI_ADDRCONFIG", test_evutil_getaddrinfo_AI_ADDRCONFIG, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
16658585484eSchristos #ifdef _WIN32
16668585484eSchristos 	{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
16678585484eSchristos #endif
16688585484eSchristos 	{ "mm_malloc", test_event_malloc, 0, NULL, NULL },
16698585484eSchristos 	{ "mm_calloc", test_event_calloc, 0, NULL, NULL },
16708585484eSchristos 	{ "mm_strdup", test_event_strdup, 0, NULL, NULL },
1671*eabc0478Schristos 	{ "usleep", test_evutil_usleep, TT_RETRIABLE, NULL, NULL },
16728585484eSchristos 	{ "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
16738585484eSchristos 	{ "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
16748585484eSchristos 	{ "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
16758585484eSchristos 	{ "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
1676*eabc0478Schristos 	{ "monotonic_prc_precise", test_evutil_monotonic_prc, TT_RETRIABLE, &basic_setup, (void*)"precise" },
16778585484eSchristos 	{ "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
1678*eabc0478Schristos 	{ "date_rfc1123", test_evutil_date_rfc1123, 0, NULL, NULL },
1679*eabc0478Schristos 	{ "evutil_v4addr_is_local", test_evutil_v4addr_is_local, 0, NULL, NULL },
1680*eabc0478Schristos 	{ "evutil_v6addr_is_local", test_evutil_v6addr_is_local, 0, NULL, NULL },
16818585484eSchristos 	END_OF_TESTCASES,
16828585484eSchristos };
16838585484eSchristos 
1684