xref: /freebsd/contrib/ntp/sntp/libevent/test/regress_util.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert  * are met:
72b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert  */
26*a466cc55SCy Schubert 
27*a466cc55SCy Schubert /** For event_debug() usage/coverage */
28*a466cc55SCy Schubert #define EVENT_VISIBILITY_WANT_DLLIMPORT
29*a466cc55SCy Schubert 
302b15cb3dSCy Schubert #include "../util-internal.h"
312b15cb3dSCy Schubert 
322b15cb3dSCy Schubert #ifdef _WIN32
332b15cb3dSCy Schubert #include <winsock2.h>
342b15cb3dSCy Schubert #include <windows.h>
352b15cb3dSCy Schubert #include <ws2tcpip.h>
362b15cb3dSCy Schubert #endif
372b15cb3dSCy Schubert 
382b15cb3dSCy Schubert #include "event2/event-config.h"
392b15cb3dSCy Schubert 
402b15cb3dSCy Schubert #include <sys/types.h>
412b15cb3dSCy Schubert 
422b15cb3dSCy Schubert #ifndef _WIN32
432b15cb3dSCy Schubert #include <sys/socket.h>
442b15cb3dSCy Schubert #include <netinet/in.h>
452b15cb3dSCy Schubert #include <arpa/inet.h>
462b15cb3dSCy Schubert #include <unistd.h>
472b15cb3dSCy Schubert #endif
482b15cb3dSCy Schubert #ifdef EVENT__HAVE_NETINET_IN6_H
492b15cb3dSCy Schubert #include <netinet/in6.h>
502b15cb3dSCy Schubert #endif
512b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_WAIT_H
522b15cb3dSCy Schubert #include <sys/wait.h>
532b15cb3dSCy Schubert #endif
542b15cb3dSCy Schubert #include <signal.h>
552b15cb3dSCy Schubert #include <stdio.h>
562b15cb3dSCy Schubert #include <stdlib.h>
572b15cb3dSCy Schubert #include <string.h>
582b15cb3dSCy Schubert 
592b15cb3dSCy Schubert #include "event2/event.h"
602b15cb3dSCy Schubert #include "event2/util.h"
612b15cb3dSCy Schubert #include "../ipv6-internal.h"
622b15cb3dSCy Schubert #include "../log-internal.h"
632b15cb3dSCy Schubert #include "../strlcpy-internal.h"
642b15cb3dSCy Schubert #include "../mm-internal.h"
652b15cb3dSCy Schubert #include "../time-internal.h"
662b15cb3dSCy Schubert 
672b15cb3dSCy Schubert #include "regress.h"
682b15cb3dSCy Schubert 
692b15cb3dSCy Schubert enum entry_status { NORMAL, CANONICAL, BAD };
702b15cb3dSCy Schubert 
712b15cb3dSCy Schubert /* This is a big table of results we expect from generating and parsing */
722b15cb3dSCy Schubert static struct ipv4_entry {
732b15cb3dSCy Schubert 	const char *addr;
742b15cb3dSCy Schubert 	ev_uint32_t res;
752b15cb3dSCy Schubert 	enum entry_status status;
762b15cb3dSCy Schubert } ipv4_entries[] = {
772b15cb3dSCy Schubert 	{ "1.2.3.4", 0x01020304u, CANONICAL },
782b15cb3dSCy Schubert 	{ "255.255.255.255", 0xffffffffu, CANONICAL },
792b15cb3dSCy Schubert 	{ "256.0.0.0", 0, BAD },
802b15cb3dSCy Schubert 	{ "ABC", 0, BAD },
812b15cb3dSCy Schubert 	{ "1.2.3.4.5", 0, BAD },
822b15cb3dSCy Schubert 	{ "176.192.208.244", 0xb0c0d0f4, CANONICAL },
832b15cb3dSCy Schubert 	{ NULL, 0, BAD },
842b15cb3dSCy Schubert };
852b15cb3dSCy Schubert 
862b15cb3dSCy Schubert static struct ipv6_entry {
872b15cb3dSCy Schubert 	const char *addr;
882b15cb3dSCy Schubert 	ev_uint32_t res[4];
892b15cb3dSCy Schubert 	enum entry_status status;
902b15cb3dSCy Schubert } ipv6_entries[] = {
912b15cb3dSCy Schubert 	{ "::", { 0, 0, 0, 0, }, CANONICAL },
922b15cb3dSCy Schubert 	{ "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
932b15cb3dSCy Schubert 	{ "::1", { 0, 0, 0, 1, }, CANONICAL },
942b15cb3dSCy Schubert 	{ "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
952b15cb3dSCy Schubert 	{ "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
962b15cb3dSCy Schubert 	{ "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
972b15cb3dSCy Schubert 	{ "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
982b15cb3dSCy Schubert 	{ "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
992b15cb3dSCy Schubert 	{ "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
1002b15cb3dSCy Schubert 	{ "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
1012b15cb3dSCy Schubert 	{ "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
1022b15cb3dSCy Schubert 	{ "foobar.", { 0, 0, 0, 0 }, BAD },
1032b15cb3dSCy Schubert 	{ "foobar", { 0, 0, 0, 0 }, BAD },
1042b15cb3dSCy Schubert 	{ "fo:obar", { 0, 0, 0, 0 }, BAD },
1052b15cb3dSCy Schubert 	{ "ffff", { 0, 0, 0, 0 }, BAD },
1062b15cb3dSCy Schubert 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
1072b15cb3dSCy Schubert 	{ "fffff::", { 0, 0, 0, 0 }, BAD },
1082b15cb3dSCy Schubert 	{ "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
1092b15cb3dSCy Schubert 	{ "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
1102b15cb3dSCy Schubert 	{ "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
1112b15cb3dSCy Schubert 	{ "1::2::3", { 0, 0, 0, 0 }, BAD },
1122b15cb3dSCy Schubert 	{ ":::1", { 0, 0, 0, 0 }, BAD },
1132b15cb3dSCy Schubert 	{ NULL, { 0, 0, 0, 0,  }, BAD },
1142b15cb3dSCy Schubert };
1152b15cb3dSCy Schubert 
1162b15cb3dSCy Schubert static void
regress_ipv4_parse(void * ptr)1172b15cb3dSCy Schubert regress_ipv4_parse(void *ptr)
1182b15cb3dSCy Schubert {
1192b15cb3dSCy Schubert 	int i;
1202b15cb3dSCy Schubert 	for (i = 0; ipv4_entries[i].addr; ++i) {
1212b15cb3dSCy Schubert 		char written[128];
1222b15cb3dSCy Schubert 		struct ipv4_entry *ent = &ipv4_entries[i];
1232b15cb3dSCy Schubert 		struct in_addr in;
1242b15cb3dSCy Schubert 		int r;
1252b15cb3dSCy Schubert 		r = evutil_inet_pton(AF_INET, ent->addr, &in);
1262b15cb3dSCy Schubert 		if (r == 0) {
1272b15cb3dSCy Schubert 			if (ent->status != BAD) {
1282b15cb3dSCy Schubert 				TT_FAIL(("%s did not parse, but it's a good address!",
1292b15cb3dSCy Schubert 					ent->addr));
1302b15cb3dSCy Schubert 			}
1312b15cb3dSCy Schubert 			continue;
1322b15cb3dSCy Schubert 		}
1332b15cb3dSCy Schubert 		if (ent->status == BAD) {
1342b15cb3dSCy Schubert 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
1352b15cb3dSCy Schubert 			continue;
1362b15cb3dSCy Schubert 		}
1372b15cb3dSCy Schubert 		if (ntohl(in.s_addr) != ent->res) {
1382b15cb3dSCy Schubert 			TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
1392b15cb3dSCy Schubert 				(unsigned long)ntohl(in.s_addr),
1402b15cb3dSCy Schubert 				(unsigned long)ent->res));
1412b15cb3dSCy Schubert 			continue;
1422b15cb3dSCy Schubert 		}
1432b15cb3dSCy Schubert 		if (ent->status == CANONICAL) {
1442b15cb3dSCy Schubert 			const char *w = evutil_inet_ntop(AF_INET, &in, written,
1452b15cb3dSCy Schubert 											 sizeof(written));
1462b15cb3dSCy Schubert 			if (!w) {
1472b15cb3dSCy Schubert 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
1482b15cb3dSCy Schubert 				continue;
1492b15cb3dSCy Schubert 			}
1502b15cb3dSCy Schubert 			if (strcmp(written, ent->addr)) {
1512b15cb3dSCy Schubert 				TT_FAIL(("Tried to write out %s; got %s",
1522b15cb3dSCy Schubert 					ent->addr, written));
1532b15cb3dSCy Schubert 				continue;
1542b15cb3dSCy Schubert 			}
1552b15cb3dSCy Schubert 		}
1562b15cb3dSCy Schubert 
1572b15cb3dSCy Schubert 	}
1582b15cb3dSCy Schubert 
1592b15cb3dSCy Schubert }
1602b15cb3dSCy Schubert 
1612b15cb3dSCy Schubert static void
regress_ipv6_parse(void * ptr)1622b15cb3dSCy Schubert regress_ipv6_parse(void *ptr)
1632b15cb3dSCy Schubert {
1642b15cb3dSCy Schubert #ifdef AF_INET6
1652b15cb3dSCy Schubert 	int i, j;
1662b15cb3dSCy Schubert 
1672b15cb3dSCy Schubert 	for (i = 0; ipv6_entries[i].addr; ++i) {
1682b15cb3dSCy Schubert 		char written[128];
1692b15cb3dSCy Schubert 		struct ipv6_entry *ent = &ipv6_entries[i];
1702b15cb3dSCy Schubert 		struct in6_addr in6;
1712b15cb3dSCy Schubert 		int r;
1722b15cb3dSCy Schubert 		r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
1732b15cb3dSCy Schubert 		if (r == 0) {
1742b15cb3dSCy Schubert 			if (ent->status != BAD)
1752b15cb3dSCy Schubert 				TT_FAIL(("%s did not parse, but it's a good address!",
1762b15cb3dSCy Schubert 					ent->addr));
1772b15cb3dSCy Schubert 			continue;
1782b15cb3dSCy Schubert 		}
1792b15cb3dSCy Schubert 		if (ent->status == BAD) {
1802b15cb3dSCy Schubert 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
1812b15cb3dSCy Schubert 			continue;
1822b15cb3dSCy Schubert 		}
1832b15cb3dSCy Schubert 		for (j = 0; j < 4; ++j) {
1842b15cb3dSCy Schubert 			/* Can't use s6_addr32 here; some don't have it. */
1852b15cb3dSCy Schubert 			ev_uint32_t u =
186a25439b6SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4  ] << 24) |
187a25439b6SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
188a25439b6SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
189a25439b6SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+3]);
1902b15cb3dSCy Schubert 			if (u != ent->res[j]) {
1912b15cb3dSCy Schubert 				TT_FAIL(("%s did not parse as expected.", ent->addr));
1922b15cb3dSCy Schubert 				continue;
1932b15cb3dSCy Schubert 			}
1942b15cb3dSCy Schubert 		}
1952b15cb3dSCy Schubert 		if (ent->status == CANONICAL) {
1962b15cb3dSCy Schubert 			const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
1972b15cb3dSCy Schubert 											 sizeof(written));
1982b15cb3dSCy Schubert 			if (!w) {
1992b15cb3dSCy Schubert 				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
2002b15cb3dSCy Schubert 				continue;
2012b15cb3dSCy Schubert 			}
2022b15cb3dSCy Schubert 			if (strcmp(written, ent->addr)) {
2032b15cb3dSCy Schubert 				TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
2042b15cb3dSCy Schubert 				continue;
2052b15cb3dSCy Schubert 			}
2062b15cb3dSCy Schubert 		}
2072b15cb3dSCy Schubert 
2082b15cb3dSCy Schubert 	}
2092b15cb3dSCy Schubert #else
2102b15cb3dSCy Schubert 	TT_BLATHER(("Skipping IPv6 address parsing."));
2112b15cb3dSCy Schubert #endif
2122b15cb3dSCy Schubert }
2132b15cb3dSCy Schubert 
214*a466cc55SCy Schubert static struct ipv6_entry_scope {
215*a466cc55SCy Schubert 	const char *addr;
216*a466cc55SCy Schubert 	ev_uint32_t res[4];
217*a466cc55SCy Schubert 	unsigned scope;
218*a466cc55SCy Schubert 	enum entry_status status;
219*a466cc55SCy Schubert } ipv6_entries_scope[] = {
220*a466cc55SCy Schubert 	{ "2001:DB8::", { 0x20010db8, 0, 0 }, 0, NORMAL },
221*a466cc55SCy Schubert 	{ "2001:DB8::%0", { 0x20010db8, 0, 0, 0 }, 0, NORMAL },
222*a466cc55SCy Schubert 	{ "2001:DB8::%1", { 0x20010db8, 0, 0, 0 }, 1, NORMAL },
223*a466cc55SCy Schubert 	{ "foobar.", { 0, 0, 0, 0 }, 0, BAD },
224*a466cc55SCy Schubert 	{ "2001:DB8::%does-not-exist", { 0, 0, 0, 0 }, 0, BAD },
225*a466cc55SCy Schubert 	{ NULL, { 0, 0, 0, 0,  }, 0, BAD },
226*a466cc55SCy Schubert };
227*a466cc55SCy Schubert static void
regress_ipv6_parse_scope(void * ptr)228*a466cc55SCy Schubert regress_ipv6_parse_scope(void *ptr)
229*a466cc55SCy Schubert {
230*a466cc55SCy Schubert #ifdef AF_INET6
231*a466cc55SCy Schubert 	int i, j;
232*a466cc55SCy Schubert 	unsigned if_scope;
233*a466cc55SCy Schubert 
234*a466cc55SCy Schubert 	for (i = 0; ipv6_entries_scope[i].addr; ++i) {
235*a466cc55SCy Schubert 		struct ipv6_entry_scope *ent = &ipv6_entries_scope[i];
236*a466cc55SCy Schubert 		struct in6_addr in6;
237*a466cc55SCy Schubert 		int r;
238*a466cc55SCy Schubert 		r = evutil_inet_pton_scope(AF_INET6, ent->addr, &in6,
239*a466cc55SCy Schubert 			&if_scope);
240*a466cc55SCy Schubert 		if (r == 0) {
241*a466cc55SCy Schubert 			if (ent->status != BAD)
242*a466cc55SCy Schubert 				TT_FAIL(("%s did not parse, but it's a good address!",
243*a466cc55SCy Schubert 					ent->addr));
244*a466cc55SCy Schubert 			continue;
245*a466cc55SCy Schubert 		}
246*a466cc55SCy Schubert 		if (ent->status == BAD) {
247*a466cc55SCy Schubert 			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
248*a466cc55SCy Schubert 			continue;
249*a466cc55SCy Schubert 		}
250*a466cc55SCy Schubert 		for (j = 0; j < 4; ++j) {
251*a466cc55SCy Schubert 			/* Can't use s6_addr32 here; some don't have it. */
252*a466cc55SCy Schubert 			ev_uint32_t u =
253*a466cc55SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4  ] << 24) |
254*a466cc55SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
255*a466cc55SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
256*a466cc55SCy Schubert 			    ((ev_uint32_t)in6.s6_addr[j*4+3]);
257*a466cc55SCy Schubert 			if (u != ent->res[j]) {
258*a466cc55SCy Schubert 				TT_FAIL(("%s did not parse as expected.", ent->addr));
259*a466cc55SCy Schubert 				continue;
260*a466cc55SCy Schubert 			}
261*a466cc55SCy Schubert 		}
262*a466cc55SCy Schubert 		if (if_scope != ent->scope) {
263*a466cc55SCy Schubert 			TT_FAIL(("%s did not parse as expected.", ent->addr));
264*a466cc55SCy Schubert 			continue;
265*a466cc55SCy Schubert 		}
266*a466cc55SCy Schubert 	}
267*a466cc55SCy Schubert #else
268*a466cc55SCy Schubert 	TT_BLATHER(("Skipping IPv6 address parsing."));
269*a466cc55SCy Schubert #endif
270*a466cc55SCy Schubert }
271*a466cc55SCy Schubert 
272*a466cc55SCy Schubert 
2732b15cb3dSCy Schubert static struct sa_port_ent {
2742b15cb3dSCy Schubert 	const char *parse;
2752b15cb3dSCy Schubert 	int safamily;
2762b15cb3dSCy Schubert 	const char *addr;
2772b15cb3dSCy Schubert 	int port;
2782b15cb3dSCy Schubert } sa_port_ents[] = {
2792b15cb3dSCy Schubert 	{ "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
2802b15cb3dSCy Schubert 	{ "[ffff::1]", AF_INET6, "ffff::1", 0 },
2812b15cb3dSCy Schubert 	{ "[ffff::1", 0, NULL, 0 },
2822b15cb3dSCy Schubert 	{ "[ffff::1]:65599", 0, NULL, 0 },
2832b15cb3dSCy Schubert 	{ "[ffff::1]:0", 0, NULL, 0 },
2842b15cb3dSCy Schubert 	{ "[ffff::1]:-1", 0, NULL, 0 },
2852b15cb3dSCy Schubert 	{ "::1", AF_INET6, "::1", 0 },
2862b15cb3dSCy Schubert 	{ "1:2::1", AF_INET6, "1:2::1", 0 },
2872b15cb3dSCy Schubert 	{ "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
2882b15cb3dSCy Schubert 	{ "1.2.3.4", AF_INET, "1.2.3.4", 0 },
2892b15cb3dSCy Schubert 	{ NULL, 0, NULL, 0 },
2902b15cb3dSCy Schubert };
2912b15cb3dSCy Schubert 
2922b15cb3dSCy Schubert static void
regress_sockaddr_port_parse(void * ptr)2932b15cb3dSCy Schubert regress_sockaddr_port_parse(void *ptr)
2942b15cb3dSCy Schubert {
2952b15cb3dSCy Schubert 	struct sockaddr_storage ss;
2962b15cb3dSCy Schubert 	int i, r;
2972b15cb3dSCy Schubert 
2982b15cb3dSCy Schubert 	for (i = 0; sa_port_ents[i].parse; ++i) {
2992b15cb3dSCy Schubert 		struct sa_port_ent *ent = &sa_port_ents[i];
3002b15cb3dSCy Schubert 		int len = sizeof(ss);
3012b15cb3dSCy Schubert 		memset(&ss, 0, sizeof(ss));
3022b15cb3dSCy Schubert 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
3032b15cb3dSCy Schubert 		if (r < 0) {
3042b15cb3dSCy Schubert 			if (ent->safamily)
3052b15cb3dSCy Schubert 				TT_FAIL(("Couldn't parse %s!", ent->parse));
3062b15cb3dSCy Schubert 			continue;
3072b15cb3dSCy Schubert 		} else if (! ent->safamily) {
3082b15cb3dSCy Schubert 			TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
3092b15cb3dSCy Schubert 			continue;
3102b15cb3dSCy Schubert 		}
3112b15cb3dSCy Schubert 		if (ent->safamily == AF_INET) {
3122b15cb3dSCy Schubert 			struct sockaddr_in sin;
3132b15cb3dSCy Schubert 			memset(&sin, 0, sizeof(sin));
3142b15cb3dSCy Schubert #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
3152b15cb3dSCy Schubert 			sin.sin_len = sizeof(sin);
3162b15cb3dSCy Schubert #endif
3172b15cb3dSCy Schubert 			sin.sin_family = AF_INET;
3182b15cb3dSCy Schubert 			sin.sin_port = htons(ent->port);
3192b15cb3dSCy Schubert 			r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
3202b15cb3dSCy Schubert 			if (1 != r) {
3212b15cb3dSCy Schubert 				TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
3222b15cb3dSCy Schubert 			} else if (memcmp(&sin, &ss, sizeof(sin))) {
3232b15cb3dSCy Schubert 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
3242b15cb3dSCy Schubert 			} else if (len != sizeof(sin)) {
3252b15cb3dSCy Schubert 				TT_FAIL(("Length for %s not as expected.",ent->parse));
3262b15cb3dSCy Schubert 			}
3272b15cb3dSCy Schubert 		} else {
3282b15cb3dSCy Schubert 			struct sockaddr_in6 sin6;
3292b15cb3dSCy Schubert 			memset(&sin6, 0, sizeof(sin6));
3302b15cb3dSCy Schubert #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
3312b15cb3dSCy Schubert 			sin6.sin6_len = sizeof(sin6);
3322b15cb3dSCy Schubert #endif
3332b15cb3dSCy Schubert 			sin6.sin6_family = AF_INET6;
3342b15cb3dSCy Schubert 			sin6.sin6_port = htons(ent->port);
3352b15cb3dSCy Schubert 			r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
3362b15cb3dSCy Schubert 			if (1 != r) {
3372b15cb3dSCy Schubert 				TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
3382b15cb3dSCy Schubert 			} else if (memcmp(&sin6, &ss, sizeof(sin6))) {
3392b15cb3dSCy Schubert 				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
3402b15cb3dSCy Schubert 			} else if (len != sizeof(sin6)) {
3412b15cb3dSCy Schubert 				TT_FAIL(("Length for %s not as expected.",ent->parse));
3422b15cb3dSCy Schubert 			}
3432b15cb3dSCy Schubert 		}
3442b15cb3dSCy Schubert 	}
3452b15cb3dSCy Schubert }
3462b15cb3dSCy Schubert 
3472b15cb3dSCy Schubert 
3482b15cb3dSCy Schubert static void
regress_sockaddr_port_format(void * ptr)3492b15cb3dSCy Schubert regress_sockaddr_port_format(void *ptr)
3502b15cb3dSCy Schubert {
3512b15cb3dSCy Schubert 	struct sockaddr_storage ss;
3522b15cb3dSCy Schubert 	int len;
3532b15cb3dSCy Schubert 	const char *cp;
3542b15cb3dSCy Schubert 	char cbuf[128];
3552b15cb3dSCy Schubert 	int r;
3562b15cb3dSCy Schubert 
3572b15cb3dSCy Schubert 	len = sizeof(ss);
3582b15cb3dSCy Schubert 	r = evutil_parse_sockaddr_port("192.168.1.1:80",
3592b15cb3dSCy Schubert 	    (struct sockaddr*)&ss, &len);
3602b15cb3dSCy Schubert 	tt_int_op(r,==,0);
3612b15cb3dSCy Schubert 	cp = evutil_format_sockaddr_port_(
3622b15cb3dSCy Schubert 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3632b15cb3dSCy Schubert 	tt_ptr_op(cp,==,cbuf);
3642b15cb3dSCy Schubert 	tt_str_op(cp,==,"192.168.1.1:80");
3652b15cb3dSCy Schubert 
3662b15cb3dSCy Schubert 	len = sizeof(ss);
3672b15cb3dSCy Schubert 	r = evutil_parse_sockaddr_port("[ff00::8010]:999",
3682b15cb3dSCy Schubert 	    (struct sockaddr*)&ss, &len);
3692b15cb3dSCy Schubert 	tt_int_op(r,==,0);
3702b15cb3dSCy Schubert 	cp = evutil_format_sockaddr_port_(
3712b15cb3dSCy Schubert 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3722b15cb3dSCy Schubert 	tt_ptr_op(cp,==,cbuf);
3732b15cb3dSCy Schubert 	tt_str_op(cp,==,"[ff00::8010]:999");
3742b15cb3dSCy Schubert 
3752b15cb3dSCy Schubert 	ss.ss_family=99;
3762b15cb3dSCy Schubert 	cp = evutil_format_sockaddr_port_(
3772b15cb3dSCy Schubert 		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
3782b15cb3dSCy Schubert 	tt_ptr_op(cp,==,cbuf);
3792b15cb3dSCy Schubert 	tt_str_op(cp,==,"<addr with socktype 99>");
3802b15cb3dSCy Schubert end:
3812b15cb3dSCy Schubert 	;
3822b15cb3dSCy Schubert }
3832b15cb3dSCy Schubert 
3842b15cb3dSCy Schubert static struct sa_pred_ent {
3852b15cb3dSCy Schubert 	const char *parse;
3862b15cb3dSCy Schubert 
3872b15cb3dSCy Schubert 	int is_loopback;
3882b15cb3dSCy Schubert } sa_pred_entries[] = {
3892b15cb3dSCy Schubert 	{ "127.0.0.1",	 1 },
3902b15cb3dSCy Schubert 	{ "127.0.3.2",	 1 },
3912b15cb3dSCy Schubert 	{ "128.1.2.3",	 0 },
3922b15cb3dSCy Schubert 	{ "18.0.0.1",	 0 },
3932b15cb3dSCy Schubert 	{ "129.168.1.1", 0 },
3942b15cb3dSCy Schubert 
3952b15cb3dSCy Schubert 	{ "::1",	 1 },
3962b15cb3dSCy Schubert 	{ "::0",	 0 },
3972b15cb3dSCy Schubert 	{ "f::1",	 0 },
3982b15cb3dSCy Schubert 	{ "::501",	 0 },
3992b15cb3dSCy Schubert 	{ NULL,		 0 },
4002b15cb3dSCy Schubert 
4012b15cb3dSCy Schubert };
4022b15cb3dSCy Schubert 
4032b15cb3dSCy Schubert static void
test_evutil_sockaddr_predicates(void * ptr)4042b15cb3dSCy Schubert test_evutil_sockaddr_predicates(void *ptr)
4052b15cb3dSCy Schubert {
4062b15cb3dSCy Schubert 	struct sockaddr_storage ss;
4072b15cb3dSCy Schubert 	int r, i;
4082b15cb3dSCy Schubert 
4092b15cb3dSCy Schubert 	for (i=0; sa_pred_entries[i].parse; ++i) {
4102b15cb3dSCy Schubert 		struct sa_pred_ent *ent = &sa_pred_entries[i];
4112b15cb3dSCy Schubert 		int len = sizeof(ss);
4122b15cb3dSCy Schubert 
4132b15cb3dSCy Schubert 		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
4142b15cb3dSCy Schubert 
4152b15cb3dSCy Schubert 		if (r<0) {
4162b15cb3dSCy Schubert 			TT_FAIL(("Couldn't parse %s!", ent->parse));
4172b15cb3dSCy Schubert 			continue;
4182b15cb3dSCy Schubert 		}
4192b15cb3dSCy Schubert 
4202b15cb3dSCy Schubert 		/* sockaddr_is_loopback */
4212b15cb3dSCy Schubert 		if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
4222b15cb3dSCy Schubert 			TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
4232b15cb3dSCy Schubert 				ent->parse));
4242b15cb3dSCy Schubert 		}
4252b15cb3dSCy Schubert 	}
4262b15cb3dSCy Schubert }
4272b15cb3dSCy Schubert 
4282b15cb3dSCy Schubert static void
test_evutil_strtoll(void * ptr)4292b15cb3dSCy Schubert test_evutil_strtoll(void *ptr)
4302b15cb3dSCy Schubert {
4312b15cb3dSCy Schubert 	const char *s;
4322b15cb3dSCy Schubert 	char *endptr;
4332b15cb3dSCy Schubert 
4342b15cb3dSCy Schubert 	tt_want(evutil_strtoll("5000000000", NULL, 10) ==
4352b15cb3dSCy Schubert 		((ev_int64_t)5000000)*1000);
4362b15cb3dSCy Schubert 	tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
4372b15cb3dSCy Schubert 		((ev_int64_t)5000000)*-1000);
4382b15cb3dSCy Schubert 	s = " 99999stuff";
4392b15cb3dSCy Schubert 	tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
4402b15cb3dSCy Schubert 	tt_want(endptr == s+6);
4412b15cb3dSCy Schubert 	tt_want(evutil_strtoll("foo", NULL, 10) == 0);
4422b15cb3dSCy Schubert  }
4432b15cb3dSCy Schubert 
4442b15cb3dSCy Schubert static void
test_evutil_snprintf(void * ptr)4452b15cb3dSCy Schubert test_evutil_snprintf(void *ptr)
4462b15cb3dSCy Schubert {
4472b15cb3dSCy Schubert 	char buf[16];
4482b15cb3dSCy Schubert 	int r;
4492b15cb3dSCy Schubert 	ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
4502b15cb3dSCy Schubert 	ev_int64_t i64 = -1 * (ev_int64_t) u64;
4512b15cb3dSCy Schubert 	size_t size = 8000;
4522b15cb3dSCy Schubert 	ev_ssize_t ssize = -9000;
4532b15cb3dSCy Schubert 
4542b15cb3dSCy Schubert 	r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
4552b15cb3dSCy Schubert 	tt_str_op(buf, ==, "50 100");
4562b15cb3dSCy Schubert 	tt_int_op(r, ==, 6);
4572b15cb3dSCy Schubert 
4582b15cb3dSCy Schubert 	r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
4592b15cb3dSCy Schubert 	tt_str_op(buf, ==, "longish 1234567");
4602b15cb3dSCy Schubert 	tt_int_op(r, ==, 18);
4612b15cb3dSCy Schubert 
4622b15cb3dSCy Schubert 	r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
4632b15cb3dSCy Schubert 	tt_str_op(buf, ==, "200000000000");
4642b15cb3dSCy Schubert 	tt_int_op(r, ==, 12);
4652b15cb3dSCy Schubert 
4662b15cb3dSCy Schubert 	r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
4672b15cb3dSCy Schubert 	tt_str_op(buf, ==, "-200000000000");
4682b15cb3dSCy Schubert 	tt_int_op(r, ==, 13);
4692b15cb3dSCy Schubert 
4702b15cb3dSCy Schubert 	r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
4712b15cb3dSCy Schubert 	    EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
4722b15cb3dSCy Schubert 	tt_str_op(buf, ==, "8000 -9000");
4732b15cb3dSCy Schubert 	tt_int_op(r, ==, 10);
4742b15cb3dSCy Schubert 
4752b15cb3dSCy Schubert       end:
4762b15cb3dSCy Schubert 	;
4772b15cb3dSCy Schubert }
4782b15cb3dSCy Schubert 
4792b15cb3dSCy Schubert static void
test_evutil_casecmp(void * ptr)4802b15cb3dSCy Schubert test_evutil_casecmp(void *ptr)
4812b15cb3dSCy Schubert {
4822b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
4832b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
4842b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
4852b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
4862b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
4872b15cb3dSCy Schubert 
4882b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
4892b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
4902b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
4912b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
4922b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
4932b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
4942b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
4952b15cb3dSCy Schubert 	tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
4962b15cb3dSCy Schubert end:
4972b15cb3dSCy Schubert 	;
4982b15cb3dSCy Schubert }
4992b15cb3dSCy Schubert 
5002b15cb3dSCy Schubert static void
test_evutil_rtrim(void * ptr)5012b15cb3dSCy Schubert test_evutil_rtrim(void *ptr)
5022b15cb3dSCy Schubert {
5032b15cb3dSCy Schubert #define TEST_TRIM(s, result) \
5042b15cb3dSCy Schubert 	do {						\
5052b15cb3dSCy Schubert 	    if (cp) mm_free(cp);			\
5062b15cb3dSCy Schubert 	    cp = mm_strdup(s);				\
5072b15cb3dSCy Schubert 	    tt_assert(cp);				\
5082b15cb3dSCy Schubert 	    evutil_rtrim_lws_(cp);			\
5092b15cb3dSCy Schubert 	    tt_str_op(cp, ==, result);			\
5102b15cb3dSCy Schubert 	} while(0)
5112b15cb3dSCy Schubert 
5122b15cb3dSCy Schubert 	char *cp = NULL;
5132b15cb3dSCy Schubert 	(void) ptr;
5142b15cb3dSCy Schubert 
5152b15cb3dSCy Schubert 	TEST_TRIM("", "");
5162b15cb3dSCy Schubert 	TEST_TRIM("a", "a");
5172b15cb3dSCy Schubert 	TEST_TRIM("abcdef ghi", "abcdef ghi");
5182b15cb3dSCy Schubert 
5192b15cb3dSCy Schubert 	TEST_TRIM(" ", "");
5202b15cb3dSCy Schubert 	TEST_TRIM("  ", "");
5212b15cb3dSCy Schubert 	TEST_TRIM("a ", "a");
5222b15cb3dSCy Schubert 	TEST_TRIM("abcdef  gH       ", "abcdef  gH");
5232b15cb3dSCy Schubert 
5242b15cb3dSCy Schubert 	TEST_TRIM("\t\t", "");
5252b15cb3dSCy Schubert 	TEST_TRIM(" \t", "");
5262b15cb3dSCy Schubert 	TEST_TRIM("\t", "");
5272b15cb3dSCy Schubert 	TEST_TRIM("a \t", "a");
5282b15cb3dSCy Schubert 	TEST_TRIM("a\t ", "a");
5292b15cb3dSCy Schubert 	TEST_TRIM("a\t", "a");
5302b15cb3dSCy Schubert 	TEST_TRIM("abcdef  gH    \t  ", "abcdef  gH");
5312b15cb3dSCy Schubert 
5322b15cb3dSCy Schubert end:
5332b15cb3dSCy Schubert 	if (cp)
5342b15cb3dSCy Schubert 		mm_free(cp);
5352b15cb3dSCy Schubert }
5362b15cb3dSCy Schubert 
5372b15cb3dSCy Schubert static int logsev = 0;
5382b15cb3dSCy Schubert static char *logmsg = NULL;
5392b15cb3dSCy Schubert 
5402b15cb3dSCy Schubert static void
logfn(int severity,const char * msg)5412b15cb3dSCy Schubert logfn(int severity, const char *msg)
5422b15cb3dSCy Schubert {
5432b15cb3dSCy Schubert 	logsev = severity;
5442b15cb3dSCy Schubert 	tt_want(msg);
5452b15cb3dSCy Schubert 	if (msg) {
5462b15cb3dSCy Schubert 		if (logmsg)
5472b15cb3dSCy Schubert 			free(logmsg);
5482b15cb3dSCy Schubert 		logmsg = strdup(msg);
5492b15cb3dSCy Schubert 	}
5502b15cb3dSCy Schubert }
5512b15cb3dSCy Schubert 
5522b15cb3dSCy Schubert static int fatal_want_severity = 0;
5532b15cb3dSCy Schubert static const char *fatal_want_message = NULL;
5542b15cb3dSCy Schubert static void
fatalfn(int exitcode)5552b15cb3dSCy Schubert fatalfn(int exitcode)
5562b15cb3dSCy Schubert {
5572b15cb3dSCy Schubert 	if (logsev != fatal_want_severity ||
5582b15cb3dSCy Schubert 	    !logmsg ||
5592b15cb3dSCy Schubert 	    strcmp(logmsg, fatal_want_message))
5602b15cb3dSCy Schubert 		exit(0);
5612b15cb3dSCy Schubert 	else
5622b15cb3dSCy Schubert 		exit(exitcode);
5632b15cb3dSCy Schubert }
5642b15cb3dSCy Schubert 
5652b15cb3dSCy Schubert #ifndef _WIN32
5662b15cb3dSCy Schubert #define CAN_CHECK_ERR
5672b15cb3dSCy Schubert static void
check_error_logging(void (* fn)(void),int wantexitcode,int wantseverity,const char * wantmsg)5682b15cb3dSCy Schubert check_error_logging(void (*fn)(void), int wantexitcode,
5692b15cb3dSCy Schubert     int wantseverity, const char *wantmsg)
5702b15cb3dSCy Schubert {
5712b15cb3dSCy Schubert 	pid_t pid;
5722b15cb3dSCy Schubert 	int status = 0, exitcode;
5732b15cb3dSCy Schubert 	fatal_want_severity = wantseverity;
5742b15cb3dSCy Schubert 	fatal_want_message = wantmsg;
5752b15cb3dSCy Schubert 	if ((pid = regress_fork()) == 0) {
5762b15cb3dSCy Schubert 		/* child process */
5772b15cb3dSCy Schubert 		fn();
5782b15cb3dSCy Schubert 		exit(0); /* should be unreachable. */
5792b15cb3dSCy Schubert 	} else {
5802b15cb3dSCy Schubert 		wait(&status);
5812b15cb3dSCy Schubert 		exitcode = WEXITSTATUS(status);
5822b15cb3dSCy Schubert 		tt_int_op(wantexitcode, ==, exitcode);
5832b15cb3dSCy Schubert 	}
5842b15cb3dSCy Schubert end:
5852b15cb3dSCy Schubert 	;
5862b15cb3dSCy Schubert }
5872b15cb3dSCy Schubert 
5882b15cb3dSCy Schubert static void
errx_fn(void)5892b15cb3dSCy Schubert errx_fn(void)
5902b15cb3dSCy Schubert {
5912b15cb3dSCy Schubert 	event_errx(2, "Fatal error; too many kumquats (%d)", 5);
5922b15cb3dSCy Schubert }
5932b15cb3dSCy Schubert 
5942b15cb3dSCy Schubert static void
err_fn(void)5952b15cb3dSCy Schubert err_fn(void)
5962b15cb3dSCy Schubert {
5972b15cb3dSCy Schubert 	errno = ENOENT;
5982b15cb3dSCy Schubert 	event_err(5,"Couldn't open %s", "/very/bad/file");
5992b15cb3dSCy Schubert }
6002b15cb3dSCy Schubert 
6012b15cb3dSCy Schubert static void
sock_err_fn(void)6022b15cb3dSCy Schubert sock_err_fn(void)
6032b15cb3dSCy Schubert {
6042b15cb3dSCy Schubert 	evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
6052b15cb3dSCy Schubert #ifdef _WIN32
6062b15cb3dSCy Schubert 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
6072b15cb3dSCy Schubert #else
6082b15cb3dSCy Schubert 	errno = EAGAIN;
6092b15cb3dSCy Schubert #endif
6102b15cb3dSCy Schubert 	event_sock_err(20, fd, "Unhappy socket");
6112b15cb3dSCy Schubert }
6122b15cb3dSCy Schubert #endif
6132b15cb3dSCy Schubert 
6142b15cb3dSCy Schubert static void
test_evutil_log(void * ptr)6152b15cb3dSCy Schubert test_evutil_log(void *ptr)
6162b15cb3dSCy Schubert {
6172b15cb3dSCy Schubert 	evutil_socket_t fd = -1;
6182b15cb3dSCy Schubert 	char buf[128];
6192b15cb3dSCy Schubert 
6202b15cb3dSCy Schubert 	event_set_log_callback(logfn);
6212b15cb3dSCy Schubert 	event_set_fatal_callback(fatalfn);
6222b15cb3dSCy Schubert #define RESET() do {				\
6232b15cb3dSCy Schubert 		logsev = 0;	\
6242b15cb3dSCy Schubert 		if (logmsg) free(logmsg);	\
6252b15cb3dSCy Schubert 		logmsg = NULL;			\
6262b15cb3dSCy Schubert 	} while (0)
6272b15cb3dSCy Schubert #define LOGEQ(sev,msg) do {			\
6282b15cb3dSCy Schubert 		tt_int_op(logsev,==,sev);	\
6292b15cb3dSCy Schubert 		tt_assert(logmsg != NULL);	\
6302b15cb3dSCy Schubert 		tt_str_op(logmsg,==,msg);	\
6312b15cb3dSCy Schubert 	} while (0)
6322b15cb3dSCy Schubert 
6332b15cb3dSCy Schubert #ifdef CAN_CHECK_ERR
6342b15cb3dSCy Schubert 	/* We need to disable these tests for now.  Previously, the logging
6352b15cb3dSCy Schubert 	 * module didn't enforce the requirement that a fatal callback
6362b15cb3dSCy Schubert 	 * actually exit.  Now, it exits no matter what, so if we wan to
6372b15cb3dSCy Schubert 	 * reinstate these tests, we'll need to fork for each one. */
6382b15cb3dSCy Schubert 	check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
6392b15cb3dSCy Schubert 	    "Fatal error; too many kumquats (5)");
6402b15cb3dSCy Schubert 	RESET();
6412b15cb3dSCy Schubert #endif
6422b15cb3dSCy Schubert 
6432b15cb3dSCy Schubert 	event_warnx("Far too many %s (%d)", "wombats", 99);
6442b15cb3dSCy Schubert 	LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
6452b15cb3dSCy Schubert 	RESET();
6462b15cb3dSCy Schubert 
6472b15cb3dSCy Schubert 	event_msgx("Connecting lime to coconut");
6482b15cb3dSCy Schubert 	LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
6492b15cb3dSCy Schubert 	RESET();
6502b15cb3dSCy Schubert 
6512b15cb3dSCy Schubert 	event_debug(("A millisecond passed! We should log that!"));
6522b15cb3dSCy Schubert #ifdef USE_DEBUG
6532b15cb3dSCy Schubert 	LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
6542b15cb3dSCy Schubert #else
6552b15cb3dSCy Schubert 	tt_int_op(logsev,==,0);
6562b15cb3dSCy Schubert 	tt_ptr_op(logmsg,==,NULL);
6572b15cb3dSCy Schubert #endif
6582b15cb3dSCy Schubert 	RESET();
6592b15cb3dSCy Schubert 
6602b15cb3dSCy Schubert 	/* Try with an errno. */
6612b15cb3dSCy Schubert 	errno = ENOENT;
6622b15cb3dSCy Schubert 	event_warn("Couldn't open %s", "/bad/file");
6632b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf),
6642b15cb3dSCy Schubert 	    "Couldn't open /bad/file: %s",strerror(ENOENT));
6652b15cb3dSCy Schubert 	LOGEQ(EVENT_LOG_WARN,buf);
6662b15cb3dSCy Schubert 	RESET();
6672b15cb3dSCy Schubert 
6682b15cb3dSCy Schubert #ifdef CAN_CHECK_ERR
6692b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf),
6702b15cb3dSCy Schubert 	    "Couldn't open /very/bad/file: %s",strerror(ENOENT));
6712b15cb3dSCy Schubert 	check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
6722b15cb3dSCy Schubert 	RESET();
6732b15cb3dSCy Schubert #endif
6742b15cb3dSCy Schubert 
6752b15cb3dSCy Schubert 	/* Try with a socket errno. */
6762b15cb3dSCy Schubert 	fd = socket(AF_INET, SOCK_STREAM, 0);
6772b15cb3dSCy Schubert #ifdef _WIN32
6782b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf),
6792b15cb3dSCy Schubert 	    "Unhappy socket: %s",
6802b15cb3dSCy Schubert 	    evutil_socket_error_to_string(WSAEWOULDBLOCK));
6812b15cb3dSCy Schubert 	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
6822b15cb3dSCy Schubert #else
6832b15cb3dSCy Schubert 	evutil_snprintf(buf, sizeof(buf),
6842b15cb3dSCy Schubert 	    "Unhappy socket: %s", strerror(EAGAIN));
6852b15cb3dSCy Schubert 	errno = EAGAIN;
6862b15cb3dSCy Schubert #endif
6872b15cb3dSCy Schubert 	event_sock_warn(fd, "Unhappy socket");
6882b15cb3dSCy Schubert 	LOGEQ(EVENT_LOG_WARN, buf);
6892b15cb3dSCy Schubert 	RESET();
6902b15cb3dSCy Schubert 
6912b15cb3dSCy Schubert #ifdef CAN_CHECK_ERR
6922b15cb3dSCy Schubert 	check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
6932b15cb3dSCy Schubert 	RESET();
6942b15cb3dSCy Schubert #endif
6952b15cb3dSCy Schubert 
6962b15cb3dSCy Schubert #undef RESET
6972b15cb3dSCy Schubert #undef LOGEQ
6982b15cb3dSCy Schubert end:
6992b15cb3dSCy Schubert 	if (logmsg)
7002b15cb3dSCy Schubert 		free(logmsg);
7012b15cb3dSCy Schubert 	if (fd >= 0)
7022b15cb3dSCy Schubert 		evutil_closesocket(fd);
7032b15cb3dSCy Schubert }
7042b15cb3dSCy Schubert 
7052b15cb3dSCy Schubert static void
test_evutil_strlcpy(void * arg)7062b15cb3dSCy Schubert test_evutil_strlcpy(void *arg)
7072b15cb3dSCy Schubert {
7082b15cb3dSCy Schubert 	char buf[8];
7092b15cb3dSCy Schubert 
7102b15cb3dSCy Schubert 	/* Successful case. */
7112b15cb3dSCy Schubert 	tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
7122b15cb3dSCy Schubert 	tt_str_op(buf, ==, "Hello");
7132b15cb3dSCy Schubert 
7142b15cb3dSCy Schubert 	/* Overflow by a lot. */
7152b15cb3dSCy Schubert 	tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
7162b15cb3dSCy Schubert 	tt_str_op(buf, ==, "pentasy");
7172b15cb3dSCy Schubert 
7182b15cb3dSCy Schubert 	/* Overflow by exactly one. */
7192b15cb3dSCy Schubert 	tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
7202b15cb3dSCy Schubert 	tt_str_op(buf, ==, "overlon");
7212b15cb3dSCy Schubert end:
7222b15cb3dSCy Schubert 	;
7232b15cb3dSCy Schubert }
7242b15cb3dSCy Schubert 
7252b15cb3dSCy Schubert struct example_struct {
7262b15cb3dSCy Schubert 	const char *a;
7272b15cb3dSCy Schubert 	const char *b;
7282b15cb3dSCy Schubert 	long c;
7292b15cb3dSCy Schubert };
7302b15cb3dSCy Schubert 
7312b15cb3dSCy Schubert static void
test_evutil_upcast(void * arg)7322b15cb3dSCy Schubert test_evutil_upcast(void *arg)
7332b15cb3dSCy Schubert {
7342b15cb3dSCy Schubert 	struct example_struct es1;
7352b15cb3dSCy Schubert 	const char **cp;
7362b15cb3dSCy Schubert 	es1.a = "World";
7372b15cb3dSCy Schubert 	es1.b = "Hello";
7382b15cb3dSCy Schubert 	es1.c = -99;
7392b15cb3dSCy Schubert 
7402b15cb3dSCy Schubert 	tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
7412b15cb3dSCy Schubert 
7422b15cb3dSCy Schubert 	cp = &es1.b;
7432b15cb3dSCy Schubert 	tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
7442b15cb3dSCy Schubert 
7452b15cb3dSCy Schubert end:
7462b15cb3dSCy Schubert 	;
7472b15cb3dSCy Schubert }
7482b15cb3dSCy Schubert 
7492b15cb3dSCy Schubert static void
test_evutil_integers(void * arg)7502b15cb3dSCy Schubert test_evutil_integers(void *arg)
7512b15cb3dSCy Schubert {
7522b15cb3dSCy Schubert 	ev_int64_t i64;
7532b15cb3dSCy Schubert 	ev_uint64_t u64;
7542b15cb3dSCy Schubert 	ev_int32_t i32;
7552b15cb3dSCy Schubert 	ev_uint32_t u32;
7562b15cb3dSCy Schubert 	ev_int16_t i16;
7572b15cb3dSCy Schubert 	ev_uint16_t u16;
7582b15cb3dSCy Schubert 	ev_int8_t  i8;
7592b15cb3dSCy Schubert 	ev_uint8_t  u8;
7602b15cb3dSCy Schubert 
7612b15cb3dSCy Schubert 	void *ptr;
7622b15cb3dSCy Schubert 	ev_intptr_t iptr;
7632b15cb3dSCy Schubert 	ev_uintptr_t uptr;
7642b15cb3dSCy Schubert 
7652b15cb3dSCy Schubert 	ev_ssize_t ssize;
7662b15cb3dSCy Schubert 
7672b15cb3dSCy Schubert 	tt_int_op(sizeof(u64), ==, 8);
7682b15cb3dSCy Schubert 	tt_int_op(sizeof(i64), ==, 8);
7692b15cb3dSCy Schubert 	tt_int_op(sizeof(u32), ==, 4);
7702b15cb3dSCy Schubert 	tt_int_op(sizeof(i32), ==, 4);
7712b15cb3dSCy Schubert 	tt_int_op(sizeof(u16), ==, 2);
7722b15cb3dSCy Schubert 	tt_int_op(sizeof(i16), ==, 2);
7732b15cb3dSCy Schubert 	tt_int_op(sizeof(u8), ==,  1);
7742b15cb3dSCy Schubert 	tt_int_op(sizeof(i8), ==,  1);
7752b15cb3dSCy Schubert 
7762b15cb3dSCy Schubert 	tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
7772b15cb3dSCy Schubert 	tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
7782b15cb3dSCy Schubert 	tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
7792b15cb3dSCy Schubert 
7802b15cb3dSCy Schubert 	u64 = 1000000000;
7812b15cb3dSCy Schubert 	u64 *= 1000000000;
7822b15cb3dSCy Schubert 	tt_assert(u64 / 1000000000 == 1000000000);
7832b15cb3dSCy Schubert 	i64 = -1000000000;
7842b15cb3dSCy Schubert 	i64 *= 1000000000;
7852b15cb3dSCy Schubert 	tt_assert(i64 / 1000000000 == -1000000000);
7862b15cb3dSCy Schubert 
7872b15cb3dSCy Schubert 	u64 = EV_UINT64_MAX;
7882b15cb3dSCy Schubert 	i64 = EV_INT64_MAX;
7892b15cb3dSCy Schubert 	tt_assert(u64 > 0);
7902b15cb3dSCy Schubert 	tt_assert(i64 > 0);
7912b15cb3dSCy Schubert 	u64++;
792a25439b6SCy Schubert /*	i64++; */
7932b15cb3dSCy Schubert 	tt_assert(u64 == 0);
794a25439b6SCy Schubert /*	tt_assert(i64 == EV_INT64_MIN); */
795a25439b6SCy Schubert /*	tt_assert(i64 < 0); */
7962b15cb3dSCy Schubert 
7972b15cb3dSCy Schubert 	u32 = EV_UINT32_MAX;
7982b15cb3dSCy Schubert 	i32 = EV_INT32_MAX;
7992b15cb3dSCy Schubert 	tt_assert(u32 > 0);
8002b15cb3dSCy Schubert 	tt_assert(i32 > 0);
8012b15cb3dSCy Schubert 	u32++;
802a25439b6SCy Schubert /*	i32++; */
8032b15cb3dSCy Schubert 	tt_assert(u32 == 0);
804a25439b6SCy Schubert /*	tt_assert(i32 == EV_INT32_MIN); */
805a25439b6SCy Schubert /*	tt_assert(i32 < 0); */
8062b15cb3dSCy Schubert 
8072b15cb3dSCy Schubert 	u16 = EV_UINT16_MAX;
8082b15cb3dSCy Schubert 	i16 = EV_INT16_MAX;
8092b15cb3dSCy Schubert 	tt_assert(u16 > 0);
8102b15cb3dSCy Schubert 	tt_assert(i16 > 0);
8112b15cb3dSCy Schubert 	u16++;
812a25439b6SCy Schubert /*	i16++; */
8132b15cb3dSCy Schubert 	tt_assert(u16 == 0);
814a25439b6SCy Schubert /*	tt_assert(i16 == EV_INT16_MIN); */
815a25439b6SCy Schubert /* 	tt_assert(i16 < 0); */
8162b15cb3dSCy Schubert 
8172b15cb3dSCy Schubert 	u8 = EV_UINT8_MAX;
8182b15cb3dSCy Schubert 	i8 = EV_INT8_MAX;
8192b15cb3dSCy Schubert 	tt_assert(u8 > 0);
8202b15cb3dSCy Schubert 	tt_assert(i8 > 0);
8212b15cb3dSCy Schubert 	u8++;
822a25439b6SCy Schubert /*	i8++;*/
8232b15cb3dSCy Schubert 	tt_assert(u8 == 0);
824a25439b6SCy Schubert /*	tt_assert(i8 == EV_INT8_MIN); */
825a25439b6SCy Schubert /*	tt_assert(i8 < 0); */
8262b15cb3dSCy Schubert 
827a25439b6SCy Schubert /*
8282b15cb3dSCy Schubert 	ssize = EV_SSIZE_MAX;
8292b15cb3dSCy Schubert 	tt_assert(ssize > 0);
8302b15cb3dSCy Schubert 	ssize++;
8312b15cb3dSCy Schubert 	tt_assert(ssize < 0);
8322b15cb3dSCy Schubert 	tt_assert(ssize == EV_SSIZE_MIN);
833a25439b6SCy Schubert */
8342b15cb3dSCy Schubert 
8352b15cb3dSCy Schubert 	ptr = &ssize;
8362b15cb3dSCy Schubert 	iptr = (ev_intptr_t)ptr;
8372b15cb3dSCy Schubert 	uptr = (ev_uintptr_t)ptr;
8382b15cb3dSCy Schubert 	ptr = (void *)iptr;
8392b15cb3dSCy Schubert 	tt_assert(ptr == &ssize);
8402b15cb3dSCy Schubert 	ptr = (void *)uptr;
8412b15cb3dSCy Schubert 	tt_assert(ptr == &ssize);
8422b15cb3dSCy Schubert 
8432b15cb3dSCy Schubert 	iptr = -1;
8442b15cb3dSCy Schubert 	tt_assert(iptr < 0);
8452b15cb3dSCy Schubert end:
8462b15cb3dSCy Schubert 	;
8472b15cb3dSCy Schubert }
8482b15cb3dSCy Schubert 
8492b15cb3dSCy Schubert struct evutil_addrinfo *
ai_find_by_family(struct evutil_addrinfo * ai,int family)8502b15cb3dSCy Schubert ai_find_by_family(struct evutil_addrinfo *ai, int family)
8512b15cb3dSCy Schubert {
8522b15cb3dSCy Schubert 	while (ai) {
8532b15cb3dSCy Schubert 		if (ai->ai_family == family)
8542b15cb3dSCy Schubert 			return ai;
8552b15cb3dSCy Schubert 		ai = ai->ai_next;
8562b15cb3dSCy Schubert 	}
8572b15cb3dSCy Schubert 	return NULL;
8582b15cb3dSCy Schubert }
8592b15cb3dSCy Schubert 
8602b15cb3dSCy Schubert struct evutil_addrinfo *
ai_find_by_protocol(struct evutil_addrinfo * ai,int protocol)8612b15cb3dSCy Schubert ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
8622b15cb3dSCy Schubert {
8632b15cb3dSCy Schubert 	while (ai) {
8642b15cb3dSCy Schubert 		if (ai->ai_protocol == protocol)
8652b15cb3dSCy Schubert 			return ai;
8662b15cb3dSCy Schubert 		ai = ai->ai_next;
8672b15cb3dSCy Schubert 	}
8682b15cb3dSCy Schubert 	return NULL;
8692b15cb3dSCy Schubert }
8702b15cb3dSCy Schubert 
8712b15cb3dSCy Schubert 
8722b15cb3dSCy Schubert int
test_ai_eq_(const struct evutil_addrinfo * ai,const char * sockaddr_port,int socktype,int protocol,int line)8732b15cb3dSCy Schubert test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
8742b15cb3dSCy Schubert     int socktype, int protocol, int line)
8752b15cb3dSCy Schubert {
8762b15cb3dSCy Schubert 	struct sockaddr_storage ss;
8772b15cb3dSCy Schubert 	int slen = sizeof(ss);
8782b15cb3dSCy Schubert 	int gotport;
8792b15cb3dSCy Schubert 	char buf[128];
8802b15cb3dSCy Schubert 	memset(&ss, 0, sizeof(ss));
8812b15cb3dSCy Schubert 	if (socktype > 0)
8822b15cb3dSCy Schubert 		tt_int_op(ai->ai_socktype, ==, socktype);
8832b15cb3dSCy Schubert 	if (protocol > 0)
8842b15cb3dSCy Schubert 		tt_int_op(ai->ai_protocol, ==, protocol);
8852b15cb3dSCy Schubert 
8862b15cb3dSCy Schubert 	if (evutil_parse_sockaddr_port(
8872b15cb3dSCy Schubert 		    sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
8882b15cb3dSCy Schubert 		TT_FAIL(("Couldn't parse expected address %s on line %d",
8892b15cb3dSCy Schubert 			sockaddr_port, line));
8902b15cb3dSCy Schubert 		return -1;
8912b15cb3dSCy Schubert 	}
8922b15cb3dSCy Schubert 	if (ai->ai_family != ss.ss_family) {
8932b15cb3dSCy Schubert 		TT_FAIL(("Address family %d did not match %d on line %d",
8942b15cb3dSCy Schubert 			ai->ai_family, ss.ss_family, line));
8952b15cb3dSCy Schubert 		return -1;
8962b15cb3dSCy Schubert 	}
8972b15cb3dSCy Schubert 	if (ai->ai_addr->sa_family == AF_INET) {
8982b15cb3dSCy Schubert 		struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
8992b15cb3dSCy Schubert 		evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
9002b15cb3dSCy Schubert 		gotport = ntohs(sin->sin_port);
9012b15cb3dSCy Schubert 		if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
9022b15cb3dSCy Schubert 			TT_FAIL(("Addr size mismatch on line %d", line));
9032b15cb3dSCy Schubert 			return -1;
9042b15cb3dSCy Schubert 		}
9052b15cb3dSCy Schubert 	} else {
9062b15cb3dSCy Schubert 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
9072b15cb3dSCy Schubert 		evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
9082b15cb3dSCy Schubert 		gotport = ntohs(sin6->sin6_port);
9092b15cb3dSCy Schubert 		if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
9102b15cb3dSCy Schubert 			TT_FAIL(("Addr size mismatch on line %d", line));
9112b15cb3dSCy Schubert 			return -1;
9122b15cb3dSCy Schubert 		}
9132b15cb3dSCy Schubert 	}
9142b15cb3dSCy Schubert 	if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
9152b15cb3dSCy Schubert 		TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
9162b15cb3dSCy Schubert 			buf, gotport, line));
9172b15cb3dSCy Schubert 		return -1;
9182b15cb3dSCy Schubert 	} else {
9192b15cb3dSCy Schubert 		TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
9202b15cb3dSCy Schubert 			buf, gotport, line));
9212b15cb3dSCy Schubert 	}
9222b15cb3dSCy Schubert 	return 0;
9232b15cb3dSCy Schubert end:
9242b15cb3dSCy Schubert 	TT_FAIL(("Test failed on line %d", line));
9252b15cb3dSCy Schubert 	return -1;
9262b15cb3dSCy Schubert }
9272b15cb3dSCy Schubert 
9282b15cb3dSCy Schubert static void
test_evutil_rand(void * arg)9292b15cb3dSCy Schubert test_evutil_rand(void *arg)
9302b15cb3dSCy Schubert {
9312b15cb3dSCy Schubert 	char buf1[32];
9322b15cb3dSCy Schubert 	char buf2[32];
9332b15cb3dSCy Schubert 	int counts[256];
9342b15cb3dSCy Schubert 	int i, j, k, n=0;
9352b15cb3dSCy Schubert 	struct evutil_weakrand_state seed = { 12346789U };
9362b15cb3dSCy Schubert 
9372b15cb3dSCy Schubert 	memset(buf2, 0, sizeof(buf2));
9382b15cb3dSCy Schubert 	memset(counts, 0, sizeof(counts));
9392b15cb3dSCy Schubert 
9402b15cb3dSCy Schubert 	for (k=0;k<32;++k) {
9412b15cb3dSCy Schubert 		/* Try a few different start and end points; try to catch
9422b15cb3dSCy Schubert 		 * the various misaligned cases of arc4random_buf */
9432b15cb3dSCy Schubert 		int startpoint = evutil_weakrand_(&seed) % 4;
9442b15cb3dSCy Schubert 		int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
9452b15cb3dSCy Schubert 
9462b15cb3dSCy Schubert 		memset(buf2, 0, sizeof(buf2));
9472b15cb3dSCy Schubert 
9482b15cb3dSCy Schubert 		/* Do 6 runs over buf1, or-ing the result into buf2 each
9492b15cb3dSCy Schubert 		 * time, to make sure we're setting each byte that we mean
9502b15cb3dSCy Schubert 		 * to set. */
9512b15cb3dSCy Schubert 		for (i=0;i<8;++i) {
9522b15cb3dSCy Schubert 			memset(buf1, 0, sizeof(buf1));
9532b15cb3dSCy Schubert 			evutil_secure_rng_get_bytes(buf1 + startpoint,
9542b15cb3dSCy Schubert 			    endpoint-startpoint);
9552b15cb3dSCy Schubert 			n += endpoint - startpoint;
9562b15cb3dSCy Schubert 			for (j=0; j<32; ++j) {
9572b15cb3dSCy Schubert 				if (j >= startpoint && j < endpoint) {
9582b15cb3dSCy Schubert 					buf2[j] |= buf1[j];
9592b15cb3dSCy Schubert 					++counts[(unsigned char)buf1[j]];
9602b15cb3dSCy Schubert 				} else {
9612b15cb3dSCy Schubert 					tt_assert(buf1[j] == 0);
9622b15cb3dSCy Schubert 					tt_int_op(buf1[j], ==, 0);
9632b15cb3dSCy Schubert 
9642b15cb3dSCy Schubert 				}
9652b15cb3dSCy Schubert 			}
9662b15cb3dSCy Schubert 		}
9672b15cb3dSCy Schubert 
9682b15cb3dSCy Schubert 		/* This will give a false positive with P=(256**8)==(2**64)
9692b15cb3dSCy Schubert 		 * for each character. */
9702b15cb3dSCy Schubert 		for (j=startpoint;j<endpoint;++j) {
9712b15cb3dSCy Schubert 			tt_int_op(buf2[j], !=, 0);
9722b15cb3dSCy Schubert 		}
9732b15cb3dSCy Schubert 	}
9742b15cb3dSCy Schubert 
9752b15cb3dSCy Schubert 	evutil_weakrand_seed_(&seed, 0);
9762b15cb3dSCy Schubert 	for (i = 0; i < 10000; ++i) {
9772b15cb3dSCy Schubert 		ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
9782b15cb3dSCy Schubert 		tt_int_op(0, <=, r);
9792b15cb3dSCy Schubert 		tt_int_op(r, <, 9999);
9802b15cb3dSCy Schubert 	}
9812b15cb3dSCy Schubert 
9822b15cb3dSCy Schubert 	/* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
9832b15cb3dSCy Schubert end:
9842b15cb3dSCy Schubert 	;
9852b15cb3dSCy Schubert }
9862b15cb3dSCy Schubert 
9872b15cb3dSCy Schubert static void
test_EVUTIL_IS_(void * arg)988*a466cc55SCy Schubert test_EVUTIL_IS_(void *arg)
989*a466cc55SCy Schubert {
990*a466cc55SCy Schubert 	tt_int_op(EVUTIL_ISDIGIT_('0'), ==, 1);
991*a466cc55SCy Schubert 	tt_int_op(EVUTIL_ISDIGIT_('a'), ==, 0);
992*a466cc55SCy Schubert 	tt_int_op(EVUTIL_ISDIGIT_('\xff'), ==, 0);
993*a466cc55SCy Schubert end:
994*a466cc55SCy Schubert 	;
995*a466cc55SCy Schubert }
996*a466cc55SCy Schubert 
997*a466cc55SCy Schubert static void
test_evutil_getaddrinfo(void * arg)9982b15cb3dSCy Schubert test_evutil_getaddrinfo(void *arg)
9992b15cb3dSCy Schubert {
10002b15cb3dSCy Schubert 	struct evutil_addrinfo *ai = NULL, *a;
10012b15cb3dSCy Schubert 	struct evutil_addrinfo hints;
10022b15cb3dSCy Schubert 	int r;
10032b15cb3dSCy Schubert 
10042b15cb3dSCy Schubert 	/* Try using it as a pton. */
10052b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
10062b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
10072b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
10082b15cb3dSCy Schubert 	r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
10092b15cb3dSCy Schubert 	tt_int_op(r, ==, 0);
10102b15cb3dSCy Schubert 	tt_assert(ai);
10112b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
10122b15cb3dSCy Schubert 	test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
10132b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10142b15cb3dSCy Schubert 	ai = NULL;
10152b15cb3dSCy Schubert 
10162b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
10172b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
10182b15cb3dSCy Schubert 	hints.ai_protocol = IPPROTO_UDP;
10192b15cb3dSCy Schubert 	r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
10202b15cb3dSCy Schubert 	tt_int_op(r, ==, 0);
10212b15cb3dSCy Schubert 	tt_assert(ai);
10222b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
10232b15cb3dSCy Schubert 	test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
10242b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10252b15cb3dSCy Schubert 	ai = NULL;
10262b15cb3dSCy Schubert 
10272b15cb3dSCy Schubert 	/* Try out the behavior of nodename=NULL */
10282b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
10292b15cb3dSCy Schubert 	hints.ai_family = PF_INET;
10302b15cb3dSCy Schubert 	hints.ai_protocol = IPPROTO_TCP;
10312b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
10322b15cb3dSCy Schubert 	r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
10332b15cb3dSCy Schubert 	tt_int_op(r,==,0);
10342b15cb3dSCy Schubert 	tt_assert(ai);
10352b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
10362b15cb3dSCy Schubert 	test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
10372b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10382b15cb3dSCy Schubert 	ai = NULL;
10392b15cb3dSCy Schubert 	hints.ai_flags = 0; /* as if for connect */
10402b15cb3dSCy Schubert 	r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
10412b15cb3dSCy Schubert 	tt_assert(ai);
10422b15cb3dSCy Schubert 	tt_int_op(r,==,0);
10432b15cb3dSCy Schubert 	test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
10442b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
10452b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10462b15cb3dSCy Schubert 	ai = NULL;
10472b15cb3dSCy Schubert 
10482b15cb3dSCy Schubert 	hints.ai_flags = 0; /* as if for connect */
10492b15cb3dSCy Schubert 	hints.ai_family = PF_INET6;
10502b15cb3dSCy Schubert 	r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
10512b15cb3dSCy Schubert 	tt_assert(ai);
10522b15cb3dSCy Schubert 	tt_int_op(r,==,0);
10532b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
10542b15cb3dSCy Schubert 	test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
10552b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10562b15cb3dSCy Schubert 	ai = NULL;
10572b15cb3dSCy Schubert 
10582b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
10592b15cb3dSCy Schubert 	hints.ai_family = PF_INET6;
10602b15cb3dSCy Schubert 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
10612b15cb3dSCy Schubert 	tt_assert(ai);
10622b15cb3dSCy Schubert 	tt_int_op(r,==,0);
10632b15cb3dSCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
10642b15cb3dSCy Schubert 	test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
10652b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10662b15cb3dSCy Schubert 	ai = NULL;
10672b15cb3dSCy Schubert 
10682b15cb3dSCy Schubert 	/* Now try an unspec one. We should get a v6 and a v4. */
10692b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
10702b15cb3dSCy Schubert 	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
10712b15cb3dSCy Schubert 	tt_assert(ai);
10722b15cb3dSCy Schubert 	tt_int_op(r,==,0);
10732b15cb3dSCy Schubert 	a = ai_find_by_family(ai, PF_INET6);
10742b15cb3dSCy Schubert 	tt_assert(a);
10752b15cb3dSCy Schubert 	test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
10762b15cb3dSCy Schubert 	a = ai_find_by_family(ai, PF_INET);
10772b15cb3dSCy Schubert 	tt_assert(a);
10782b15cb3dSCy Schubert 	test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
10792b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10802b15cb3dSCy Schubert 	ai = NULL;
10812b15cb3dSCy Schubert 
10822b15cb3dSCy Schubert 	/* Try out AI_NUMERICHOST: successful case.  Also try
10832b15cb3dSCy Schubert 	 * multiprotocol. */
10842b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
10852b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
10862b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
10872b15cb3dSCy Schubert 	r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
10882b15cb3dSCy Schubert 	tt_int_op(r, ==, 0);
10892b15cb3dSCy Schubert 	a = ai_find_by_protocol(ai, IPPROTO_TCP);
10902b15cb3dSCy Schubert 	tt_assert(a);
10912b15cb3dSCy Schubert 	test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
10922b15cb3dSCy Schubert 	a = ai_find_by_protocol(ai, IPPROTO_UDP);
10932b15cb3dSCy Schubert 	tt_assert(a);
10942b15cb3dSCy Schubert 	test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
10952b15cb3dSCy Schubert 	evutil_freeaddrinfo(ai);
10962b15cb3dSCy Schubert 	ai = NULL;
10972b15cb3dSCy Schubert 
10982b15cb3dSCy Schubert 	/* Try the failing case of AI_NUMERICHOST */
10992b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
11002b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
11012b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
11022b15cb3dSCy Schubert 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
11032b15cb3dSCy Schubert 	tt_int_op(r, ==, EVUTIL_EAI_NONAME);
11042b15cb3dSCy Schubert 	tt_ptr_op(ai, ==, NULL);
11052b15cb3dSCy Schubert 
11062b15cb3dSCy Schubert 	/* Try symbolic service names wit AI_NUMERICSERV */
11072b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
11082b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
11092b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
11102b15cb3dSCy Schubert 	hints.ai_flags = EVUTIL_AI_NUMERICSERV;
11112b15cb3dSCy Schubert 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
11122b15cb3dSCy Schubert 	tt_int_op(r,==,EVUTIL_EAI_NONAME);
11132b15cb3dSCy Schubert 
11142b15cb3dSCy Schubert 	/* Try symbolic service names */
11152b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
11162b15cb3dSCy Schubert 	hints.ai_family = PF_UNSPEC;
11172b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
11182b15cb3dSCy Schubert 	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
11192b15cb3dSCy Schubert 	if (r!=0) {
11202b15cb3dSCy Schubert 		TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
11212b15cb3dSCy Schubert 	} else {
11222b15cb3dSCy Schubert 		tt_assert(ai);
11232b15cb3dSCy Schubert 		test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
11242b15cb3dSCy Schubert 		evutil_freeaddrinfo(ai);
11252b15cb3dSCy Schubert 		ai = NULL;
11262b15cb3dSCy Schubert 	}
11272b15cb3dSCy Schubert 
11282b15cb3dSCy Schubert end:
11292b15cb3dSCy Schubert 	if (ai)
11302b15cb3dSCy Schubert 		evutil_freeaddrinfo(ai);
11312b15cb3dSCy Schubert }
11322b15cb3dSCy Schubert 
11332b15cb3dSCy Schubert static void
test_evutil_getaddrinfo_live(void * arg)11342b15cb3dSCy Schubert test_evutil_getaddrinfo_live(void *arg)
11352b15cb3dSCy Schubert {
11362b15cb3dSCy Schubert 	struct evutil_addrinfo *ai = NULL;
11372b15cb3dSCy Schubert 	struct evutil_addrinfo hints;
11382b15cb3dSCy Schubert 
11392b15cb3dSCy Schubert 	struct sockaddr_in6 *sin6;
11402b15cb3dSCy Schubert 	struct sockaddr_in *sin;
11412b15cb3dSCy Schubert 	char buf[128];
11422b15cb3dSCy Schubert 	const char *cp;
11432b15cb3dSCy Schubert 	int r;
11442b15cb3dSCy Schubert 
11452b15cb3dSCy Schubert 	/* Now do some actual lookups. */
11462b15cb3dSCy Schubert 	memset(&hints, 0, sizeof(hints));
11472b15cb3dSCy Schubert 	hints.ai_family = PF_INET;
11482b15cb3dSCy Schubert 	hints.ai_protocol = IPPROTO_TCP;
11492b15cb3dSCy Schubert 	hints.ai_socktype = SOCK_STREAM;
11502b15cb3dSCy Schubert 	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
11512b15cb3dSCy Schubert 	if (r != 0) {
11522b15cb3dSCy Schubert 		TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
11532b15cb3dSCy Schubert 	} else {
11542b15cb3dSCy Schubert 		tt_assert(ai);
11552b15cb3dSCy Schubert 		tt_int_op(ai->ai_family, ==, PF_INET);
11562b15cb3dSCy Schubert 		tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
11572b15cb3dSCy Schubert 		tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
11582b15cb3dSCy Schubert 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
11592b15cb3dSCy Schubert 		sin = (struct sockaddr_in*)ai->ai_addr;
11602b15cb3dSCy Schubert 		tt_int_op(sin->sin_family, ==, AF_INET);
11612b15cb3dSCy Schubert 		tt_int_op(sin->sin_port, ==, htons(80));
11622b15cb3dSCy Schubert 		tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
11632b15cb3dSCy Schubert 
11642b15cb3dSCy Schubert 		cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
11652b15cb3dSCy Schubert 		TT_BLATHER(("www.google.com resolved to %s",
11662b15cb3dSCy Schubert 			cp?cp:"<unwriteable>"));
11672b15cb3dSCy Schubert 		evutil_freeaddrinfo(ai);
11682b15cb3dSCy Schubert 		ai = NULL;
11692b15cb3dSCy Schubert 	}
11702b15cb3dSCy Schubert 
11712b15cb3dSCy Schubert 	hints.ai_family = PF_INET6;
11722b15cb3dSCy Schubert 	r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
11732b15cb3dSCy Schubert 	if (r != 0) {
11742b15cb3dSCy Schubert 		TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
11752b15cb3dSCy Schubert 	} else {
11762b15cb3dSCy Schubert 		tt_assert(ai);
11772b15cb3dSCy Schubert 		tt_int_op(ai->ai_family, ==, PF_INET6);
11782b15cb3dSCy Schubert 		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
11792b15cb3dSCy Schubert 		sin6 = (struct sockaddr_in6*)ai->ai_addr;
11802b15cb3dSCy Schubert 		tt_int_op(sin6->sin6_port, ==, htons(80));
11812b15cb3dSCy Schubert 
11822b15cb3dSCy Schubert 		cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
11832b15cb3dSCy Schubert 		    sizeof(buf));
11842b15cb3dSCy Schubert 		TT_BLATHER(("ipv6.google.com resolved to %s",
11852b15cb3dSCy Schubert 			cp?cp:"<unwriteable>"));
11862b15cb3dSCy Schubert 	}
11872b15cb3dSCy Schubert 
11882b15cb3dSCy Schubert end:
11892b15cb3dSCy Schubert 	if (ai)
11902b15cb3dSCy Schubert 		evutil_freeaddrinfo(ai);
11912b15cb3dSCy Schubert }
11922b15cb3dSCy Schubert 
1193*a466cc55SCy Schubert static void
test_evutil_getaddrinfo_AI_ADDRCONFIG(void * arg)1194*a466cc55SCy Schubert test_evutil_getaddrinfo_AI_ADDRCONFIG(void *arg)
1195*a466cc55SCy Schubert {
1196*a466cc55SCy Schubert 	struct evutil_addrinfo *ai = NULL;
1197*a466cc55SCy Schubert 	struct evutil_addrinfo hints;
1198*a466cc55SCy Schubert 	int r;
1199*a466cc55SCy Schubert 
1200*a466cc55SCy Schubert 	memset(&hints, 0, sizeof(hints));
1201*a466cc55SCy Schubert 	hints.ai_family = AF_UNSPEC;
1202*a466cc55SCy Schubert 	hints.ai_socktype = SOCK_STREAM;
1203*a466cc55SCy Schubert 	hints.ai_flags = EVUTIL_AI_PASSIVE|EVUTIL_AI_ADDRCONFIG;
1204*a466cc55SCy Schubert 
1205*a466cc55SCy Schubert 	/* IPv4 */
1206*a466cc55SCy Schubert 	r = evutil_getaddrinfo("127.0.0.1", "80", &hints, &ai);
1207*a466cc55SCy Schubert 	tt_int_op(r, ==, 0);
1208*a466cc55SCy Schubert 	tt_assert(ai);
1209*a466cc55SCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
1210*a466cc55SCy Schubert 	test_ai_eq(ai, "127.0.0.1:80", SOCK_STREAM, IPPROTO_TCP);
1211*a466cc55SCy Schubert 	evutil_freeaddrinfo(ai);
1212*a466cc55SCy Schubert 	ai = NULL;
1213*a466cc55SCy Schubert 
1214*a466cc55SCy Schubert 	/* IPv6 */
1215*a466cc55SCy Schubert 	r = evutil_getaddrinfo("::1", "80", &hints, &ai);
1216*a466cc55SCy Schubert 	tt_int_op(r, ==, 0);
1217*a466cc55SCy Schubert 	tt_assert(ai);
1218*a466cc55SCy Schubert 	tt_ptr_op(ai->ai_next, ==, NULL);
1219*a466cc55SCy Schubert 	test_ai_eq(ai, "[::1]:80", SOCK_STREAM, IPPROTO_TCP);
1220*a466cc55SCy Schubert 	evutil_freeaddrinfo(ai);
1221*a466cc55SCy Schubert 	ai = NULL;
1222*a466cc55SCy Schubert 
1223*a466cc55SCy Schubert end:
1224*a466cc55SCy Schubert 	if (ai)
1225*a466cc55SCy Schubert 		evutil_freeaddrinfo(ai);
1226*a466cc55SCy Schubert }
1227*a466cc55SCy Schubert 
12282b15cb3dSCy Schubert #ifdef _WIN32
12292b15cb3dSCy Schubert static void
test_evutil_loadsyslib(void * arg)12302b15cb3dSCy Schubert test_evutil_loadsyslib(void *arg)
12312b15cb3dSCy Schubert {
1232a25439b6SCy Schubert 	HMODULE h=NULL;
12332b15cb3dSCy Schubert 
12342b15cb3dSCy Schubert 	h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
12352b15cb3dSCy Schubert 	tt_assert(h);
12362b15cb3dSCy Schubert 
12372b15cb3dSCy Schubert end:
12382b15cb3dSCy Schubert 	if (h)
12392b15cb3dSCy Schubert 		CloseHandle(h);
12402b15cb3dSCy Schubert 
12412b15cb3dSCy Schubert }
12422b15cb3dSCy Schubert #endif
12432b15cb3dSCy Schubert 
12442b15cb3dSCy Schubert /** Test mm_malloc(). */
12452b15cb3dSCy Schubert static void
test_event_malloc(void * arg)12462b15cb3dSCy Schubert test_event_malloc(void *arg)
12472b15cb3dSCy Schubert {
12482b15cb3dSCy Schubert 	void *p = NULL;
12492b15cb3dSCy Schubert 	(void)arg;
12502b15cb3dSCy Schubert 
12512b15cb3dSCy Schubert 	/* mm_malloc(0) should simply return NULL. */
12522b15cb3dSCy Schubert #ifndef EVENT__DISABLE_MM_REPLACEMENT
12532b15cb3dSCy Schubert 	errno = 0;
12542b15cb3dSCy Schubert 	p = mm_malloc(0);
12552b15cb3dSCy Schubert 	tt_assert(p == NULL);
12562b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12572b15cb3dSCy Schubert #endif
12582b15cb3dSCy Schubert 
12592b15cb3dSCy Schubert 	/* Trivial case. */
12602b15cb3dSCy Schubert 	errno = 0;
12612b15cb3dSCy Schubert 	p = mm_malloc(8);
12622b15cb3dSCy Schubert 	tt_assert(p != NULL);
12632b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12642b15cb3dSCy Schubert 	mm_free(p);
12652b15cb3dSCy Schubert 
12662b15cb3dSCy Schubert  end:
12672b15cb3dSCy Schubert 	errno = 0;
12682b15cb3dSCy Schubert 	return;
12692b15cb3dSCy Schubert }
12702b15cb3dSCy Schubert 
12712b15cb3dSCy Schubert static void
test_event_calloc(void * arg)12722b15cb3dSCy Schubert test_event_calloc(void *arg)
12732b15cb3dSCy Schubert {
12742b15cb3dSCy Schubert 	void *p = NULL;
12752b15cb3dSCy Schubert 	(void)arg;
12762b15cb3dSCy Schubert 
12772b15cb3dSCy Schubert #ifndef EVENT__DISABLE_MM_REPLACEMENT
12782b15cb3dSCy Schubert 	/* mm_calloc() should simply return NULL
12792b15cb3dSCy Schubert 	 * if either argument is zero. */
12802b15cb3dSCy Schubert 	errno = 0;
12812b15cb3dSCy Schubert 	p = mm_calloc(0, 0);
12822b15cb3dSCy Schubert 	tt_assert(p == NULL);
12832b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12842b15cb3dSCy Schubert 	errno = 0;
12852b15cb3dSCy Schubert 	p = mm_calloc(0, 1);
12862b15cb3dSCy Schubert 	tt_assert(p == NULL);
12872b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12882b15cb3dSCy Schubert 	errno = 0;
12892b15cb3dSCy Schubert 	p = mm_calloc(1, 0);
12902b15cb3dSCy Schubert 	tt_assert(p == NULL);
12912b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12922b15cb3dSCy Schubert #endif
12932b15cb3dSCy Schubert 
12942b15cb3dSCy Schubert 	/* Trivial case. */
12952b15cb3dSCy Schubert 	errno = 0;
12962b15cb3dSCy Schubert 	p = mm_calloc(8, 8);
12972b15cb3dSCy Schubert 	tt_assert(p != NULL);
12982b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
12992b15cb3dSCy Schubert 	mm_free(p);
13002b15cb3dSCy Schubert 	p = NULL;
13012b15cb3dSCy Schubert 
13022b15cb3dSCy Schubert 	/* mm_calloc() should set errno = ENOMEM and return NULL
13032b15cb3dSCy Schubert 	 * in case of potential overflow. */
13042b15cb3dSCy Schubert 	errno = 0;
13052b15cb3dSCy Schubert 	p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
13062b15cb3dSCy Schubert 	tt_assert(p == NULL);
13072b15cb3dSCy Schubert 	tt_int_op(errno, ==, ENOMEM);
13082b15cb3dSCy Schubert 
13092b15cb3dSCy Schubert  end:
13102b15cb3dSCy Schubert 	errno = 0;
13112b15cb3dSCy Schubert 	if (p)
13122b15cb3dSCy Schubert 		mm_free(p);
13132b15cb3dSCy Schubert 
13142b15cb3dSCy Schubert 	return;
13152b15cb3dSCy Schubert }
13162b15cb3dSCy Schubert 
13172b15cb3dSCy Schubert static void
test_event_strdup(void * arg)13182b15cb3dSCy Schubert test_event_strdup(void *arg)
13192b15cb3dSCy Schubert {
13202b15cb3dSCy Schubert 	void *p = NULL;
13212b15cb3dSCy Schubert 	(void)arg;
13222b15cb3dSCy Schubert 
13232b15cb3dSCy Schubert #ifndef EVENT__DISABLE_MM_REPLACEMENT
13242b15cb3dSCy Schubert 	/* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
13252b15cb3dSCy Schubert 	errno = 0;
13262b15cb3dSCy Schubert 	p = mm_strdup(NULL);
13272b15cb3dSCy Schubert 	tt_assert(p == NULL);
13282b15cb3dSCy Schubert 	tt_int_op(errno, ==, EINVAL);
13292b15cb3dSCy Schubert #endif
13302b15cb3dSCy Schubert 
13312b15cb3dSCy Schubert 	/* Trivial cases. */
13322b15cb3dSCy Schubert 
13332b15cb3dSCy Schubert 	errno = 0;
13342b15cb3dSCy Schubert 	p = mm_strdup("");
13352b15cb3dSCy Schubert 	tt_assert(p != NULL);
13362b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
13372b15cb3dSCy Schubert 	tt_str_op(p, ==, "");
13382b15cb3dSCy Schubert 	mm_free(p);
13392b15cb3dSCy Schubert 
13402b15cb3dSCy Schubert 	errno = 0;
13412b15cb3dSCy Schubert 	p = mm_strdup("foo");
13422b15cb3dSCy Schubert 	tt_assert(p != NULL);
13432b15cb3dSCy Schubert 	tt_int_op(errno, ==, 0);
13442b15cb3dSCy Schubert 	tt_str_op(p, ==, "foo");
13452b15cb3dSCy Schubert 	mm_free(p);
13462b15cb3dSCy Schubert 
13472b15cb3dSCy Schubert 	/* XXX
13482b15cb3dSCy Schubert 	 * mm_strdup(str) where str is a string of length EV_SIZE_MAX
13492b15cb3dSCy Schubert 	 * should set errno = ENOMEM and return NULL. */
13502b15cb3dSCy Schubert 
13512b15cb3dSCy Schubert  end:
13522b15cb3dSCy Schubert 	errno = 0;
13532b15cb3dSCy Schubert 	return;
13542b15cb3dSCy Schubert }
13552b15cb3dSCy Schubert 
13562b15cb3dSCy Schubert static void
test_evutil_usleep(void * arg)13572b15cb3dSCy Schubert test_evutil_usleep(void *arg)
13582b15cb3dSCy Schubert {
13592b15cb3dSCy Schubert 	struct timeval tv1, tv2, tv3, diff1, diff2;
13602b15cb3dSCy Schubert 	const struct timeval quarter_sec = {0, 250*1000};
13612b15cb3dSCy Schubert 	const struct timeval tenth_sec = {0, 100*1000};
13622b15cb3dSCy Schubert 	long usec1, usec2;
13632b15cb3dSCy Schubert 
13642b15cb3dSCy Schubert 	evutil_gettimeofday(&tv1, NULL);
13652b15cb3dSCy Schubert 	evutil_usleep_(&quarter_sec);
13662b15cb3dSCy Schubert 	evutil_gettimeofday(&tv2, NULL);
13672b15cb3dSCy Schubert 	evutil_usleep_(&tenth_sec);
13682b15cb3dSCy Schubert 	evutil_gettimeofday(&tv3, NULL);
13692b15cb3dSCy Schubert 
13702b15cb3dSCy Schubert 	evutil_timersub(&tv2, &tv1, &diff1);
13712b15cb3dSCy Schubert 	evutil_timersub(&tv3, &tv2, &diff2);
13722b15cb3dSCy Schubert 	usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
13732b15cb3dSCy Schubert 	usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
13742b15cb3dSCy Schubert 
13752b15cb3dSCy Schubert 	tt_int_op(usec1, >, 200000);
13762b15cb3dSCy Schubert 	tt_int_op(usec1, <, 300000);
13772b15cb3dSCy Schubert 	tt_int_op(usec2, >,  80000);
13782b15cb3dSCy Schubert 	tt_int_op(usec2, <, 120000);
13792b15cb3dSCy Schubert 
13802b15cb3dSCy Schubert end:
13812b15cb3dSCy Schubert 	;
13822b15cb3dSCy Schubert }
13832b15cb3dSCy Schubert 
13842b15cb3dSCy Schubert static void
test_evutil_monotonic_res(void * data_)13852b15cb3dSCy Schubert test_evutil_monotonic_res(void *data_)
13862b15cb3dSCy Schubert {
13872b15cb3dSCy Schubert 	/* Basic santity-test for monotonic timers.  What we'd really like
13882b15cb3dSCy Schubert 	 * to do is make sure that they can't go backwards even when the
13892b15cb3dSCy Schubert 	 * system clock goes backwards. But we haven't got a good way to
13902b15cb3dSCy Schubert 	 * move the system clock backwards.
13912b15cb3dSCy Schubert 	 */
13922b15cb3dSCy Schubert 	struct basic_test_data *data = data_;
13932b15cb3dSCy Schubert 	struct evutil_monotonic_timer timer;
13942b15cb3dSCy Schubert 	const int precise = strstr(data->setup_data, "precise") != NULL;
13952b15cb3dSCy Schubert 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
13962b15cb3dSCy Schubert 	struct timeval tv[10], delay;
13972b15cb3dSCy Schubert 	int total_diff = 0;
13982b15cb3dSCy Schubert 
13992b15cb3dSCy Schubert 	int flags = 0, wantres, acceptdiff, i;
14002b15cb3dSCy Schubert 	if (precise)
14012b15cb3dSCy Schubert 		flags |= EV_MONOT_PRECISE;
14022b15cb3dSCy Schubert 	if (fallback)
14032b15cb3dSCy Schubert 		flags |= EV_MONOT_FALLBACK;
14042b15cb3dSCy Schubert 	if (precise || fallback) {
14052b15cb3dSCy Schubert #ifdef _WIN32
14062b15cb3dSCy Schubert 		wantres = 10*1000;
14072b15cb3dSCy Schubert 		acceptdiff = 1000;
14082b15cb3dSCy Schubert #else
14092b15cb3dSCy Schubert 		wantres = 1000;
14102b15cb3dSCy Schubert 		acceptdiff = 300;
14112b15cb3dSCy Schubert #endif
14122b15cb3dSCy Schubert 	} else {
14132b15cb3dSCy Schubert 		wantres = 40*1000;
14142b15cb3dSCy Schubert 		acceptdiff = 20*1000;
14152b15cb3dSCy Schubert 	}
14162b15cb3dSCy Schubert 
14172b15cb3dSCy Schubert 	TT_BLATHER(("Precise = %d", precise));
14182b15cb3dSCy Schubert 	TT_BLATHER(("Fallback = %d", fallback));
14192b15cb3dSCy Schubert 
14202b15cb3dSCy Schubert 	/* First, make sure we match up with usleep. */
14212b15cb3dSCy Schubert 
14222b15cb3dSCy Schubert 	delay.tv_sec = 0;
14232b15cb3dSCy Schubert 	delay.tv_usec = wantres;
14242b15cb3dSCy Schubert 
14252b15cb3dSCy Schubert 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
14262b15cb3dSCy Schubert 
14272b15cb3dSCy Schubert 	for (i = 0; i < 10; ++i) {
14282b15cb3dSCy Schubert 		evutil_gettime_monotonic_(&timer, &tv[i]);
14292b15cb3dSCy Schubert 		evutil_usleep_(&delay);
14302b15cb3dSCy Schubert 	}
14312b15cb3dSCy Schubert 
14322b15cb3dSCy Schubert 	for (i = 0; i < 9; ++i) {
14332b15cb3dSCy Schubert 		struct timeval diff;
14342b15cb3dSCy Schubert 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
14352b15cb3dSCy Schubert 		evutil_timersub(&tv[i+1], &tv[i], &diff);
14362b15cb3dSCy Schubert 		tt_int_op(diff.tv_sec, ==, 0);
14372b15cb3dSCy Schubert 		total_diff += diff.tv_usec;
14382b15cb3dSCy Schubert 		TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
14392b15cb3dSCy Schubert 	}
14402b15cb3dSCy Schubert 	tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
14412b15cb3dSCy Schubert 
14422b15cb3dSCy Schubert end:
14432b15cb3dSCy Schubert 	;
14442b15cb3dSCy Schubert }
14452b15cb3dSCy Schubert 
14462b15cb3dSCy Schubert static void
test_evutil_monotonic_prc(void * data_)14472b15cb3dSCy Schubert test_evutil_monotonic_prc(void *data_)
14482b15cb3dSCy Schubert {
14492b15cb3dSCy Schubert 	struct basic_test_data *data = data_;
14502b15cb3dSCy Schubert 	struct evutil_monotonic_timer timer;
14512b15cb3dSCy Schubert 	const int precise = strstr(data->setup_data, "precise") != NULL;
14522b15cb3dSCy Schubert 	const int fallback = strstr(data->setup_data, "fallback") != NULL;
14532b15cb3dSCy Schubert 	struct timeval tv[10];
14542b15cb3dSCy Schubert 	int total_diff = 0;
14552b15cb3dSCy Schubert 	int i, maxstep = 25*1000,flags=0;
14562b15cb3dSCy Schubert 	if (precise)
14572b15cb3dSCy Schubert 		maxstep = 500;
14582b15cb3dSCy Schubert 	if (precise)
14592b15cb3dSCy Schubert 		flags |= EV_MONOT_PRECISE;
14602b15cb3dSCy Schubert 	if (fallback)
14612b15cb3dSCy Schubert 		flags |= EV_MONOT_FALLBACK;
14622b15cb3dSCy Schubert 	tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
14632b15cb3dSCy Schubert 
14642b15cb3dSCy Schubert 	/* find out what precision we actually see. */
14652b15cb3dSCy Schubert 
14662b15cb3dSCy Schubert 	evutil_gettime_monotonic_(&timer, &tv[0]);
14672b15cb3dSCy Schubert 	for (i = 1; i < 10; ++i) {
14682b15cb3dSCy Schubert 		do {
14692b15cb3dSCy Schubert 			evutil_gettime_monotonic_(&timer, &tv[i]);
14702b15cb3dSCy Schubert 		} while (evutil_timercmp(&tv[i-1], &tv[i], ==));
14712b15cb3dSCy Schubert 	}
14722b15cb3dSCy Schubert 
14732b15cb3dSCy Schubert 	total_diff = 0;
14742b15cb3dSCy Schubert 	for (i = 0; i < 9; ++i) {
14752b15cb3dSCy Schubert 		struct timeval diff;
14762b15cb3dSCy Schubert 		tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
14772b15cb3dSCy Schubert 		evutil_timersub(&tv[i+1], &tv[i], &diff);
14782b15cb3dSCy Schubert 		tt_int_op(diff.tv_sec, ==, 0);
14792b15cb3dSCy Schubert 		total_diff += diff.tv_usec;
14802b15cb3dSCy Schubert 		TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
14812b15cb3dSCy Schubert 	}
14822b15cb3dSCy Schubert 	TT_BLATHER(("Average step difference = %d", total_diff / 9));
14832b15cb3dSCy Schubert 	tt_int_op(total_diff/9, <, maxstep);
14842b15cb3dSCy Schubert 
14852b15cb3dSCy Schubert end:
14862b15cb3dSCy Schubert 	;
14872b15cb3dSCy Schubert }
14882b15cb3dSCy Schubert 
1489*a466cc55SCy Schubert static void
create_tm_from_unix_epoch(struct tm * cur_p,const time_t t)1490*a466cc55SCy Schubert create_tm_from_unix_epoch(struct tm *cur_p, const time_t t)
1491*a466cc55SCy Schubert {
1492*a466cc55SCy Schubert #ifdef _WIN32
1493*a466cc55SCy Schubert 	struct tm *tmp = gmtime(&t);
1494*a466cc55SCy Schubert 	if (!tmp) {
1495*a466cc55SCy Schubert 		fprintf(stderr, "gmtime: %s (%i)", strerror(errno), (int)t);
1496*a466cc55SCy Schubert 		exit(1);
1497*a466cc55SCy Schubert 	}
1498*a466cc55SCy Schubert 	*cur_p = *tmp;
1499*a466cc55SCy Schubert #else
1500*a466cc55SCy Schubert 	gmtime_r(&t, cur_p);
1501*a466cc55SCy Schubert #endif
1502*a466cc55SCy Schubert }
1503*a466cc55SCy Schubert 
1504*a466cc55SCy Schubert static struct date_rfc1123_case {
1505*a466cc55SCy Schubert 	time_t t;
1506*a466cc55SCy Schubert 	char date[30];
1507*a466cc55SCy Schubert } date_rfc1123_cases[] = {
1508*a466cc55SCy Schubert 	{           0, "Thu, 01 Jan 1970 00:00:00 GMT"} /* UNIX time of zero */,
1509*a466cc55SCy Schubert 	{   946684799, "Fri, 31 Dec 1999 23:59:59 GMT"} /* the last moment of the 20th century */,
1510*a466cc55SCy Schubert 	{   946684800, "Sat, 01 Jan 2000 00:00:00 GMT"} /* the first moment of the 21st century */,
1511*a466cc55SCy Schubert 	{   981072000, "Fri, 02 Feb 2001 00:00:00 GMT"},
1512*a466cc55SCy Schubert 	{  1015113600, "Sun, 03 Mar 2002 00:00:00 GMT"},
1513*a466cc55SCy Schubert 	{  1049414400, "Fri, 04 Apr 2003 00:00:00 GMT"},
1514*a466cc55SCy Schubert 	{  1083715200, "Wed, 05 May 2004 00:00:00 GMT"},
1515*a466cc55SCy Schubert 	{  1118016000, "Mon, 06 Jun 2005 00:00:00 GMT"},
1516*a466cc55SCy Schubert 	{  1152230400, "Fri, 07 Jul 2006 00:00:00 GMT"},
1517*a466cc55SCy Schubert 	{  1186531200, "Wed, 08 Aug 2007 00:00:00 GMT"},
1518*a466cc55SCy Schubert 	{  1220918400, "Tue, 09 Sep 2008 00:00:00 GMT"},
1519*a466cc55SCy Schubert 	{  1255132800, "Sat, 10 Oct 2009 00:00:00 GMT"},
1520*a466cc55SCy Schubert 	{  1289433600, "Thu, 11 Nov 2010 00:00:00 GMT"},
1521*a466cc55SCy Schubert 	{  1323648000, "Mon, 12 Dec 2011 00:00:00 GMT"},
1522*a466cc55SCy Schubert #ifndef _WIN32
1523*a466cc55SCy Schubert #if EVENT__SIZEOF_TIME_T > 4
1524*a466cc55SCy Schubert 	/** In win32 case we have max   "23:59:59 January 18, 2038, UTC" for time32 */
1525*a466cc55SCy Schubert 	{  4294967296, "Sun, 07 Feb 2106 06:28:16 GMT"} /* 2^32 */,
1526*a466cc55SCy Schubert 	/** In win32 case we have max "23:59:59, December 31, 3000, UTC" for time64 */
1527*a466cc55SCy Schubert 	{253402300799, "Fri, 31 Dec 9999 23:59:59 GMT"} /* long long future no one can imagine */,
1528*a466cc55SCy Schubert #endif /* time_t != 32bit */
1529*a466cc55SCy Schubert 	{  1456704000, "Mon, 29 Feb 2016 00:00:00 GMT"} /* leap year */,
1530*a466cc55SCy Schubert #endif
1531*a466cc55SCy Schubert 	{  1435708800, "Wed, 01 Jul 2015 00:00:00 GMT"} /* leap second */,
1532*a466cc55SCy Schubert 	{  1481866376, "Fri, 16 Dec 2016 05:32:56 GMT"} /* the time this test case is generated */,
1533*a466cc55SCy Schubert 	{0, ""} /* end of test cases. */
1534*a466cc55SCy Schubert };
1535*a466cc55SCy Schubert 
1536*a466cc55SCy Schubert static void
test_evutil_date_rfc1123(void * arg)1537*a466cc55SCy Schubert test_evutil_date_rfc1123(void *arg)
1538*a466cc55SCy Schubert {
1539*a466cc55SCy Schubert 	struct tm query;
1540*a466cc55SCy Schubert 	char result[30];
1541*a466cc55SCy Schubert 	size_t i = 0;
1542*a466cc55SCy Schubert 
1543*a466cc55SCy Schubert 	/* Checks if too small buffers are safely accepted. */
1544*a466cc55SCy Schubert 	{
1545*a466cc55SCy Schubert 		create_tm_from_unix_epoch(&query, 0);
1546*a466cc55SCy Schubert 		evutil_date_rfc1123(result, 8, &query);
1547*a466cc55SCy Schubert 		tt_str_op(result, ==, "Thu, 01");
1548*a466cc55SCy Schubert 	}
1549*a466cc55SCy Schubert 
1550*a466cc55SCy Schubert 	/* Checks for testcases. */
1551*a466cc55SCy Schubert 	for (i = 0; ; i++) {
1552*a466cc55SCy Schubert 		struct date_rfc1123_case c = date_rfc1123_cases[i];
1553*a466cc55SCy Schubert 
1554*a466cc55SCy Schubert 		if (strlen(c.date) == 0)
1555*a466cc55SCy Schubert 			break;
1556*a466cc55SCy Schubert 
1557*a466cc55SCy Schubert 		create_tm_from_unix_epoch(&query, c.t);
1558*a466cc55SCy Schubert 		evutil_date_rfc1123(result, sizeof(result), &query);
1559*a466cc55SCy Schubert 		tt_str_op(result, ==, c.date);
1560*a466cc55SCy Schubert 	}
1561*a466cc55SCy Schubert 
1562*a466cc55SCy Schubert end:
1563*a466cc55SCy Schubert 	;
1564*a466cc55SCy Schubert }
1565*a466cc55SCy Schubert 
1566*a466cc55SCy Schubert static void
test_evutil_v4addr_is_local(void * arg)1567*a466cc55SCy Schubert test_evutil_v4addr_is_local(void *arg)
1568*a466cc55SCy Schubert {
1569*a466cc55SCy Schubert 	struct sockaddr_in sin;
1570*a466cc55SCy Schubert 	sin.sin_family = AF_INET;
1571*a466cc55SCy Schubert 
1572*a466cc55SCy Schubert 	/* we use evutil_inet_pton() here to fill in network-byte order */
1573*a466cc55SCy Schubert #define LOCAL(str, yes) do {                                              \
1574*a466cc55SCy Schubert 	tt_int_op(evutil_inet_pton(AF_INET, str, &sin.sin_addr), ==, 1);  \
1575*a466cc55SCy Schubert 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, yes);       \
1576*a466cc55SCy Schubert } while (0)
1577*a466cc55SCy Schubert 
1578*a466cc55SCy Schubert 	/** any */
1579*a466cc55SCy Schubert 	sin.sin_addr.s_addr = INADDR_ANY;
1580*a466cc55SCy Schubert 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1581*a466cc55SCy Schubert 
1582*a466cc55SCy Schubert 	/** loopback */
1583*a466cc55SCy Schubert 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1584*a466cc55SCy Schubert 	tt_int_op(evutil_v4addr_is_local_(&sin.sin_addr), ==, 1);
1585*a466cc55SCy Schubert 	LOCAL("127.0.0.1", 1);
1586*a466cc55SCy Schubert 	LOCAL("127.255.255.255", 1);
1587*a466cc55SCy Schubert 	LOCAL("121.0.0.1", 0);
1588*a466cc55SCy Schubert 
1589*a466cc55SCy Schubert 	/** link-local */
1590*a466cc55SCy Schubert 	LOCAL("169.254.0.1", 1);
1591*a466cc55SCy Schubert 	LOCAL("169.254.255.255", 1);
1592*a466cc55SCy Schubert 	LOCAL("170.0.0.0", 0);
1593*a466cc55SCy Schubert 
1594*a466cc55SCy Schubert 	/** Multicast */
1595*a466cc55SCy Schubert 	LOCAL("224.0.0.0", 1);
1596*a466cc55SCy Schubert 	LOCAL("239.255.255.255", 1);
1597*a466cc55SCy Schubert 	LOCAL("240.0.0.0", 0);
1598*a466cc55SCy Schubert end:
1599*a466cc55SCy Schubert 	;
1600*a466cc55SCy Schubert }
1601*a466cc55SCy Schubert 
1602*a466cc55SCy Schubert static void
test_evutil_v6addr_is_local(void * arg)1603*a466cc55SCy Schubert test_evutil_v6addr_is_local(void *arg)
1604*a466cc55SCy Schubert {
1605*a466cc55SCy Schubert 	struct sockaddr_in6 sin6;
1606*a466cc55SCy Schubert 	struct in6_addr anyaddr = IN6ADDR_ANY_INIT;
1607*a466cc55SCy Schubert 	struct in6_addr loopback = IN6ADDR_LOOPBACK_INIT;
1608*a466cc55SCy Schubert 
1609*a466cc55SCy Schubert 	sin6.sin6_family = AF_INET6;
1610*a466cc55SCy Schubert #define LOCAL6(str, yes) do {                                              \
1611*a466cc55SCy Schubert 	tt_int_op(evutil_inet_pton(AF_INET6, str, &sin6.sin6_addr), ==, 1);\
1612*a466cc55SCy Schubert 	tt_int_op(evutil_v6addr_is_local_(&sin6.sin6_addr), ==, yes);      \
1613*a466cc55SCy Schubert } while (0)
1614*a466cc55SCy Schubert 
1615*a466cc55SCy Schubert 	/** any */
1616*a466cc55SCy Schubert 	tt_int_op(evutil_v6addr_is_local_(&anyaddr), ==, 1);
1617*a466cc55SCy Schubert 	LOCAL6("::0", 1);
1618*a466cc55SCy Schubert 
1619*a466cc55SCy Schubert 	/** loopback */
1620*a466cc55SCy Schubert 	tt_int_op(evutil_v6addr_is_local_(&loopback), ==, 1);
1621*a466cc55SCy Schubert 	LOCAL6("::1", 1);
1622*a466cc55SCy Schubert 
1623*a466cc55SCy Schubert 	/** IPV4 mapped */
1624*a466cc55SCy Schubert 	LOCAL6("::ffff:0:0", 1);
1625*a466cc55SCy Schubert 	/** IPv4 translated */
1626*a466cc55SCy Schubert 	LOCAL6("::ffff:0:0:0", 1);
1627*a466cc55SCy Schubert 	/** IPv4/IPv6 translation */
1628*a466cc55SCy Schubert 	LOCAL6("64:ff9b::", 0);
1629*a466cc55SCy Schubert 	/** Link-local */
1630*a466cc55SCy Schubert 	LOCAL6("fe80::", 1);
1631*a466cc55SCy Schubert 	/** Multicast */
1632*a466cc55SCy Schubert 	LOCAL6("ff00::", 1);
1633*a466cc55SCy Schubert 	/** Unspecified */
1634*a466cc55SCy Schubert 	LOCAL6("::", 1);
1635*a466cc55SCy Schubert 
1636*a466cc55SCy Schubert 	/** Global Internet */
1637*a466cc55SCy Schubert 	LOCAL6("2001::", 0);
1638*a466cc55SCy Schubert 	LOCAL6("2001:4860:4802:32::1b", 0);
1639*a466cc55SCy Schubert end:
1640*a466cc55SCy Schubert 	;
1641*a466cc55SCy Schubert }
1642*a466cc55SCy Schubert 
16432b15cb3dSCy Schubert struct testcase_t util_testcases[] = {
16442b15cb3dSCy Schubert 	{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
16452b15cb3dSCy Schubert 	{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
1646*a466cc55SCy Schubert 	{ "ipv6_parse_scope", regress_ipv6_parse_scope, 0, NULL, NULL },
16472b15cb3dSCy Schubert 	{ "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
16482b15cb3dSCy Schubert 	{ "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
16492b15cb3dSCy Schubert 	{ "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
16502b15cb3dSCy Schubert 	{ "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
16512b15cb3dSCy Schubert 	{ "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
16522b15cb3dSCy Schubert 	{ "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
16532b15cb3dSCy Schubert 	{ "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
16542b15cb3dSCy Schubert 	{ "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
16552b15cb3dSCy Schubert 	{ "log", test_evutil_log, TT_FORK, NULL, NULL },
16562b15cb3dSCy Schubert 	{ "upcast", test_evutil_upcast, 0, NULL, NULL },
16572b15cb3dSCy Schubert 	{ "integers", test_evutil_integers, 0, NULL, NULL },
16582b15cb3dSCy Schubert 	{ "rand", test_evutil_rand, TT_FORK, NULL, NULL },
1659*a466cc55SCy Schubert 	{ "EVUTIL_IS_", test_EVUTIL_IS_, 0, NULL, NULL },
16602b15cb3dSCy Schubert 	{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
16612b15cb3dSCy Schubert 	{ "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1662*a466cc55SCy Schubert 	{ "getaddrinfo_AI_ADDRCONFIG", test_evutil_getaddrinfo_AI_ADDRCONFIG, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
16632b15cb3dSCy Schubert #ifdef _WIN32
16642b15cb3dSCy Schubert 	{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
16652b15cb3dSCy Schubert #endif
16662b15cb3dSCy Schubert 	{ "mm_malloc", test_event_malloc, 0, NULL, NULL },
16672b15cb3dSCy Schubert 	{ "mm_calloc", test_event_calloc, 0, NULL, NULL },
16682b15cb3dSCy Schubert 	{ "mm_strdup", test_event_strdup, 0, NULL, NULL },
1669*a466cc55SCy Schubert 	{ "usleep", test_evutil_usleep, TT_RETRIABLE, NULL, NULL },
16702b15cb3dSCy Schubert 	{ "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
16712b15cb3dSCy Schubert 	{ "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
16722b15cb3dSCy Schubert 	{ "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
16732b15cb3dSCy Schubert 	{ "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
1674*a466cc55SCy Schubert 	{ "monotonic_prc_precise", test_evutil_monotonic_prc, TT_RETRIABLE, &basic_setup, (void*)"precise" },
16752b15cb3dSCy Schubert 	{ "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
1676*a466cc55SCy Schubert 	{ "date_rfc1123", test_evutil_date_rfc1123, 0, NULL, NULL },
1677*a466cc55SCy Schubert 	{ "evutil_v4addr_is_local", test_evutil_v4addr_is_local, 0, NULL, NULL },
1678*a466cc55SCy Schubert 	{ "evutil_v6addr_is_local", test_evutil_v6addr_is_local, 0, NULL, NULL },
16792b15cb3dSCy Schubert 	END_OF_TESTCASES,
16802b15cb3dSCy Schubert };
16812b15cb3dSCy Schubert 
1682