ipc/chromium/src/third_party/libevent/test/regress_util.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/test/regress_util.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1094 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 3. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +#ifdef WIN32
    1.30 +#include <winsock2.h>
    1.31 +#include <windows.h>
    1.32 +#include <ws2tcpip.h>
    1.33 +#endif
    1.34 +
    1.35 +#include "event2/event-config.h"
    1.36 +
    1.37 +#include <sys/types.h>
    1.38 +
    1.39 +#ifndef WIN32
    1.40 +#include <sys/socket.h>
    1.41 +#include <netinet/in.h>
    1.42 +#include <arpa/inet.h>
    1.43 +#include <unistd.h>
    1.44 +#endif
    1.45 +#ifdef _EVENT_HAVE_NETINET_IN6_H
    1.46 +#include <netinet/in6.h>
    1.47 +#endif
    1.48 +#ifdef _EVENT_HAVE_SYS_WAIT_H
    1.49 +#include <sys/wait.h>
    1.50 +#endif
    1.51 +#include <signal.h>
    1.52 +#include <stdio.h>
    1.53 +#include <stdlib.h>
    1.54 +#include <string.h>
    1.55 +
    1.56 +#include "event2/event.h"
    1.57 +#include "event2/util.h"
    1.58 +#include "../ipv6-internal.h"
    1.59 +#include "../util-internal.h"
    1.60 +#include "../log-internal.h"
    1.61 +#include "../strlcpy-internal.h"
    1.62 +
    1.63 +#include "regress.h"
    1.64 +
    1.65 +enum entry_status { NORMAL, CANONICAL, BAD };
    1.66 +
    1.67 +/* This is a big table of results we expect from generating and parsing */
    1.68 +static struct ipv4_entry {
    1.69 +	const char *addr;
    1.70 +	ev_uint32_t res;
    1.71 +	enum entry_status status;
    1.72 +} ipv4_entries[] = {
    1.73 +	{ "1.2.3.4", 0x01020304u, CANONICAL },
    1.74 +	{ "255.255.255.255", 0xffffffffu, CANONICAL },
    1.75 +	{ "256.0.0.0", 0, BAD },
    1.76 +	{ "ABC", 0, BAD },
    1.77 +	{ "1.2.3.4.5", 0, BAD },
    1.78 +	{ "176.192.208.244", 0xb0c0d0f4, CANONICAL },
    1.79 +	{ NULL, 0, BAD },
    1.80 +};
    1.81 +
    1.82 +static struct ipv6_entry {
    1.83 +	const char *addr;
    1.84 +	ev_uint32_t res[4];
    1.85 +	enum entry_status status;
    1.86 +} ipv6_entries[] = {
    1.87 +	{ "::", { 0, 0, 0, 0, }, CANONICAL },
    1.88 +	{ "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
    1.89 +	{ "::1", { 0, 0, 0, 1, }, CANONICAL },
    1.90 +	{ "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
    1.91 +	{ "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
    1.92 +	{ "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
    1.93 +	{ "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
    1.94 +	{ "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
    1.95 +	{ "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
    1.96 +	{ "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
    1.97 +	{ "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
    1.98 +	{ "foobar.", { 0, 0, 0, 0 }, BAD },
    1.99 +	{ "foobar", { 0, 0, 0, 0 }, BAD },
   1.100 +	{ "fo:obar", { 0, 0, 0, 0 }, BAD },
   1.101 +	{ "ffff", { 0, 0, 0, 0 }, BAD },
   1.102 +	{ "fffff::", { 0, 0, 0, 0 }, BAD },
   1.103 +	{ "fffff::", { 0, 0, 0, 0 }, BAD },
   1.104 +	{ "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
   1.105 +	{ "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
   1.106 +	{ "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
   1.107 +	{ "1::2::3", { 0, 0, 0, 0 }, BAD },
   1.108 +	{ ":::1", { 0, 0, 0, 0 }, BAD },
   1.109 +	{ NULL, { 0, 0, 0, 0,  }, BAD },
   1.110 +};
   1.111 +
   1.112 +static void
   1.113 +regress_ipv4_parse(void *ptr)
   1.114 +{
   1.115 +	int i;
   1.116 +	for (i = 0; ipv4_entries[i].addr; ++i) {
   1.117 +		char written[128];
   1.118 +		struct ipv4_entry *ent = &ipv4_entries[i];
   1.119 +		struct in_addr in;
   1.120 +		int r;
   1.121 +		r = evutil_inet_pton(AF_INET, ent->addr, &in);
   1.122 +		if (r == 0) {
   1.123 +			if (ent->status != BAD) {
   1.124 +				TT_FAIL(("%s did not parse, but it's a good address!",
   1.125 +					ent->addr));
   1.126 +			}
   1.127 +			continue;
   1.128 +		}
   1.129 +		if (ent->status == BAD) {
   1.130 +			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
   1.131 +			continue;
   1.132 +		}
   1.133 +		if (ntohl(in.s_addr) != ent->res) {
   1.134 +			TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
   1.135 +				(unsigned long)ntohl(in.s_addr),
   1.136 +				(unsigned long)ent->res));
   1.137 +			continue;
   1.138 +		}
   1.139 +		if (ent->status == CANONICAL) {
   1.140 +			const char *w = evutil_inet_ntop(AF_INET, &in, written,
   1.141 +											 sizeof(written));
   1.142 +			if (!w) {
   1.143 +				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
   1.144 +				continue;
   1.145 +			}
   1.146 +			if (strcmp(written, ent->addr)) {
   1.147 +				TT_FAIL(("Tried to write out %s; got %s",
   1.148 +					ent->addr, written));
   1.149 +				continue;
   1.150 +			}
   1.151 +		}
   1.152 +
   1.153 +	}
   1.154 +
   1.155 +}
   1.156 +
   1.157 +static void
   1.158 +regress_ipv6_parse(void *ptr)
   1.159 +{
   1.160 +#ifdef AF_INET6
   1.161 +	int i, j;
   1.162 +
   1.163 +	for (i = 0; ipv6_entries[i].addr; ++i) {
   1.164 +		char written[128];
   1.165 +		struct ipv6_entry *ent = &ipv6_entries[i];
   1.166 +		struct in6_addr in6;
   1.167 +		int r;
   1.168 +		r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
   1.169 +		if (r == 0) {
   1.170 +			if (ent->status != BAD)
   1.171 +				TT_FAIL(("%s did not parse, but it's a good address!",
   1.172 +					ent->addr));
   1.173 +			continue;
   1.174 +		}
   1.175 +		if (ent->status == BAD) {
   1.176 +			TT_FAIL(("%s parsed, but we expected an error", ent->addr));
   1.177 +			continue;
   1.178 +		}
   1.179 +		for (j = 0; j < 4; ++j) {
   1.180 +			/* Can't use s6_addr32 here; some don't have it. */
   1.181 +			ev_uint32_t u =
   1.182 +				(in6.s6_addr[j*4  ] << 24) |
   1.183 +				(in6.s6_addr[j*4+1] << 16) |
   1.184 +				(in6.s6_addr[j*4+2] << 8) |
   1.185 +				(in6.s6_addr[j*4+3]);
   1.186 +			if (u != ent->res[j]) {
   1.187 +				TT_FAIL(("%s did not parse as expected.", ent->addr));
   1.188 +				continue;
   1.189 +			}
   1.190 +		}
   1.191 +		if (ent->status == CANONICAL) {
   1.192 +			const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
   1.193 +											 sizeof(written));
   1.194 +			if (!w) {
   1.195 +				TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
   1.196 +				continue;
   1.197 +			}
   1.198 +			if (strcmp(written, ent->addr)) {
   1.199 +				TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
   1.200 +				continue;
   1.201 +			}
   1.202 +		}
   1.203 +
   1.204 +	}
   1.205 +#else
   1.206 +	TT_BLATHER(("Skipping IPv6 address parsing."));
   1.207 +#endif
   1.208 +}
   1.209 +
   1.210 +static struct sa_port_ent {
   1.211 +	const char *parse;
   1.212 +	int safamily;
   1.213 +	const char *addr;
   1.214 +	int port;
   1.215 +} sa_port_ents[] = {
   1.216 +	{ "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
   1.217 +	{ "[ffff::1]", AF_INET6, "ffff::1", 0 },
   1.218 +	{ "[ffff::1", 0, NULL, 0 },
   1.219 +	{ "[ffff::1]:65599", 0, NULL, 0 },
   1.220 +	{ "[ffff::1]:0", 0, NULL, 0 },
   1.221 +	{ "[ffff::1]:-1", 0, NULL, 0 },
   1.222 +	{ "::1", AF_INET6, "::1", 0 },
   1.223 +	{ "1:2::1", AF_INET6, "1:2::1", 0 },
   1.224 +	{ "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
   1.225 +	{ "1.2.3.4", AF_INET, "1.2.3.4", 0 },
   1.226 +	{ NULL, 0, NULL, 0 },
   1.227 +};
   1.228 +
   1.229 +static void
   1.230 +regress_sockaddr_port_parse(void *ptr)
   1.231 +{
   1.232 +	struct sockaddr_storage ss;
   1.233 +	int i, r;
   1.234 +
   1.235 +	for (i = 0; sa_port_ents[i].parse; ++i) {
   1.236 +		struct sa_port_ent *ent = &sa_port_ents[i];
   1.237 +		int len = sizeof(ss);
   1.238 +		memset(&ss, 0, sizeof(ss));
   1.239 +		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
   1.240 +		if (r < 0) {
   1.241 +			if (ent->safamily)
   1.242 +				TT_FAIL(("Couldn't parse %s!", ent->parse));
   1.243 +			continue;
   1.244 +		} else if (! ent->safamily) {
   1.245 +			TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
   1.246 +			continue;
   1.247 +		}
   1.248 +		if (ent->safamily == AF_INET) {
   1.249 +			struct sockaddr_in sin;
   1.250 +			memset(&sin, 0, sizeof(sin));
   1.251 +#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
   1.252 +			sin.sin_len = sizeof(sin);
   1.253 +#endif
   1.254 +			sin.sin_family = AF_INET;
   1.255 +			sin.sin_port = htons(ent->port);
   1.256 +			r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
   1.257 +			if (1 != r) {
   1.258 +				TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
   1.259 +			} else if (memcmp(&sin, &ss, sizeof(sin))) {
   1.260 +				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
   1.261 +			} else if (len != sizeof(sin)) {
   1.262 +				TT_FAIL(("Length for %s not as expected.",ent->parse));
   1.263 +			}
   1.264 +		} else {
   1.265 +			struct sockaddr_in6 sin6;
   1.266 +			memset(&sin6, 0, sizeof(sin6));
   1.267 +#ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
   1.268 +			sin6.sin6_len = sizeof(sin6);
   1.269 +#endif
   1.270 +			sin6.sin6_family = AF_INET6;
   1.271 +			sin6.sin6_port = htons(ent->port);
   1.272 +			r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
   1.273 +			if (1 != r) {
   1.274 +				TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
   1.275 +			} else if (memcmp(&sin6, &ss, sizeof(sin6))) {
   1.276 +				TT_FAIL(("Parse for %s was not as expected.", ent->parse));
   1.277 +			} else if (len != sizeof(sin6)) {
   1.278 +				TT_FAIL(("Length for %s not as expected.",ent->parse));
   1.279 +			}
   1.280 +		}
   1.281 +	}
   1.282 +}
   1.283 +
   1.284 +
   1.285 +static void
   1.286 +regress_sockaddr_port_format(void *ptr)
   1.287 +{
   1.288 +	struct sockaddr_storage ss;
   1.289 +	int len;
   1.290 +	const char *cp;
   1.291 +	char cbuf[128];
   1.292 +	int r;
   1.293 +
   1.294 +	len = sizeof(ss);
   1.295 +	r = evutil_parse_sockaddr_port("192.168.1.1:80",
   1.296 +	    (struct sockaddr*)&ss, &len);
   1.297 +	tt_int_op(r,==,0);
   1.298 +	cp = evutil_format_sockaddr_port(
   1.299 +		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
   1.300 +	tt_ptr_op(cp,==,cbuf);
   1.301 +	tt_str_op(cp,==,"192.168.1.1:80");
   1.302 +
   1.303 +	len = sizeof(ss);
   1.304 +	r = evutil_parse_sockaddr_port("[ff00::8010]:999",
   1.305 +	    (struct sockaddr*)&ss, &len);
   1.306 +	tt_int_op(r,==,0);
   1.307 +	cp = evutil_format_sockaddr_port(
   1.308 +		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
   1.309 +	tt_ptr_op(cp,==,cbuf);
   1.310 +	tt_str_op(cp,==,"[ff00::8010]:999");
   1.311 +
   1.312 +	ss.ss_family=99;
   1.313 +	cp = evutil_format_sockaddr_port(
   1.314 +		(struct sockaddr*)&ss, cbuf, sizeof(cbuf));
   1.315 +	tt_ptr_op(cp,==,cbuf);
   1.316 +	tt_str_op(cp,==,"<addr with socktype 99>");
   1.317 +end:
   1.318 +	;
   1.319 +}
   1.320 +
   1.321 +static struct sa_pred_ent {
   1.322 +	const char *parse;
   1.323 +
   1.324 +	int is_loopback;
   1.325 +} sa_pred_entries[] = {
   1.326 +	{ "127.0.0.1",	 1 },
   1.327 +	{ "127.0.3.2",	 1 },
   1.328 +	{ "128.1.2.3",	 0 },
   1.329 +	{ "18.0.0.1",	 0 },
   1.330 +	{ "129.168.1.1", 0 },
   1.331 +
   1.332 +	{ "::1",	 1 },
   1.333 +	{ "::0",	 0 },
   1.334 +	{ "f::1",	 0 },
   1.335 +	{ "::501",	 0 },
   1.336 +	{ NULL,		 0 },
   1.337 +
   1.338 +};
   1.339 +
   1.340 +static void
   1.341 +test_evutil_sockaddr_predicates(void *ptr)
   1.342 +{
   1.343 +	struct sockaddr_storage ss;
   1.344 +	int r, i;
   1.345 +
   1.346 +	for (i=0; sa_pred_entries[i].parse; ++i) {
   1.347 +		struct sa_pred_ent *ent = &sa_pred_entries[i];
   1.348 +		int len = sizeof(ss);
   1.349 +
   1.350 +		r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
   1.351 +
   1.352 +		if (r<0) {
   1.353 +			TT_FAIL(("Couldn't parse %s!", ent->parse));
   1.354 +			continue;
   1.355 +		}
   1.356 +
   1.357 +		/* sockaddr_is_loopback */
   1.358 +		if (ent->is_loopback != evutil_sockaddr_is_loopback((struct sockaddr*)&ss)) {
   1.359 +			TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
   1.360 +				ent->parse));
   1.361 +		}
   1.362 +	}
   1.363 +}
   1.364 +
   1.365 +static void
   1.366 +test_evutil_strtoll(void *ptr)
   1.367 +{
   1.368 +	const char *s;
   1.369 +	char *endptr;
   1.370 +
   1.371 +	tt_want(evutil_strtoll("5000000000", NULL, 10) ==
   1.372 +		((ev_int64_t)5000000)*1000);
   1.373 +	tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
   1.374 +		((ev_int64_t)5000000)*-1000);
   1.375 +	s = " 99999stuff";
   1.376 +	tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
   1.377 +	tt_want(endptr == s+6);
   1.378 +	tt_want(evutil_strtoll("foo", NULL, 10) == 0);
   1.379 + }
   1.380 +
   1.381 +static void
   1.382 +test_evutil_snprintf(void *ptr)
   1.383 +{
   1.384 +	char buf[16];
   1.385 +	int r;
   1.386 +	ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
   1.387 +	ev_int64_t i64 = -1 * (ev_int64_t) u64;
   1.388 +	size_t size = 8000;
   1.389 +	ev_ssize_t ssize = -9000;
   1.390 +
   1.391 +	r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
   1.392 +	tt_str_op(buf, ==, "50 100");
   1.393 +	tt_int_op(r, ==, 6);
   1.394 +
   1.395 +	r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
   1.396 +	tt_str_op(buf, ==, "longish 1234567");
   1.397 +	tt_int_op(r, ==, 18);
   1.398 +
   1.399 +	r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
   1.400 +	tt_str_op(buf, ==, "200000000000");
   1.401 +	tt_int_op(r, ==, 12);
   1.402 +
   1.403 +	r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
   1.404 +	tt_str_op(buf, ==, "-200000000000");
   1.405 +	tt_int_op(r, ==, 13);
   1.406 +
   1.407 +	r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
   1.408 +	    EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
   1.409 +	tt_str_op(buf, ==, "8000 -9000");
   1.410 +	tt_int_op(r, ==, 10);
   1.411 +
   1.412 +      end:
   1.413 +	;
   1.414 +}
   1.415 +
   1.416 +static void
   1.417 +test_evutil_casecmp(void *ptr)
   1.418 +{
   1.419 +	tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
   1.420 +	tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
   1.421 +	tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
   1.422 +	tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
   1.423 +	tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
   1.424 +
   1.425 +	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
   1.426 +	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
   1.427 +	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
   1.428 +	tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
   1.429 +	tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
   1.430 +	tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
   1.431 +	tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
   1.432 +	tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
   1.433 +end:
   1.434 +	;
   1.435 +}
   1.436 +
   1.437 +static int logsev = 0;
   1.438 +static char *logmsg = NULL;
   1.439 +
   1.440 +static void
   1.441 +logfn(int severity, const char *msg)
   1.442 +{
   1.443 +	logsev = severity;
   1.444 +	tt_want(msg);
   1.445 +	if (msg) {
   1.446 +		if (logmsg)
   1.447 +			free(logmsg);
   1.448 +		logmsg = strdup(msg);
   1.449 +	}
   1.450 +}
   1.451 +
   1.452 +static int fatal_want_severity = 0;
   1.453 +static const char *fatal_want_message = NULL;
   1.454 +static void
   1.455 +fatalfn(int exitcode)
   1.456 +{
   1.457 +	if (logsev != fatal_want_severity ||
   1.458 +	    !logmsg ||
   1.459 +	    strcmp(logmsg, fatal_want_message))
   1.460 +		exit(0);
   1.461 +	else
   1.462 +		exit(exitcode);
   1.463 +}
   1.464 +
   1.465 +#ifndef WIN32
   1.466 +#define CAN_CHECK_ERR
   1.467 +static void
   1.468 +check_error_logging(void (*fn)(void), int wantexitcode,
   1.469 +    int wantseverity, const char *wantmsg)
   1.470 +{
   1.471 +	pid_t pid;
   1.472 +	int status = 0, exitcode;
   1.473 +	fatal_want_severity = wantseverity;
   1.474 +	fatal_want_message = wantmsg;
   1.475 +	if ((pid = regress_fork()) == 0) {
   1.476 +		/* child process */
   1.477 +		fn();
   1.478 +		exit(0); /* should be unreachable. */
   1.479 +	} else {
   1.480 +		wait(&status);
   1.481 +		exitcode = WEXITSTATUS(status);
   1.482 +		tt_int_op(wantexitcode, ==, exitcode);
   1.483 +	}
   1.484 +end:
   1.485 +	;
   1.486 +}
   1.487 +
   1.488 +static void
   1.489 +errx_fn(void)
   1.490 +{
   1.491 +	event_errx(2, "Fatal error; too many kumquats (%d)", 5);
   1.492 +}
   1.493 +
   1.494 +static void
   1.495 +err_fn(void)
   1.496 +{
   1.497 +	errno = ENOENT;
   1.498 +	event_err(5,"Couldn't open %s", "/very/bad/file");
   1.499 +}
   1.500 +
   1.501 +static void
   1.502 +sock_err_fn(void)
   1.503 +{
   1.504 +	evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
   1.505 +#ifdef WIN32
   1.506 +	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
   1.507 +#else
   1.508 +	errno = EAGAIN;
   1.509 +#endif
   1.510 +	event_sock_err(20, fd, "Unhappy socket");
   1.511 +}
   1.512 +#endif
   1.513 +
   1.514 +static void
   1.515 +test_evutil_log(void *ptr)
   1.516 +{
   1.517 +	evutil_socket_t fd = -1;
   1.518 +	char buf[128];
   1.519 +
   1.520 +	event_set_log_callback(logfn);
   1.521 +	event_set_fatal_callback(fatalfn);
   1.522 +#define RESET() do {				\
   1.523 +		logsev = 0;	\
   1.524 +		if (logmsg) free(logmsg);	\
   1.525 +		logmsg = NULL;			\
   1.526 +	} while (0)
   1.527 +#define LOGEQ(sev,msg) do {			\
   1.528 +		tt_int_op(logsev,==,sev);	\
   1.529 +		tt_assert(logmsg != NULL);	\
   1.530 +		tt_str_op(logmsg,==,msg);	\
   1.531 +	} while (0)
   1.532 +
   1.533 +#ifdef CAN_CHECK_ERR
   1.534 +	/* We need to disable these tests for now.  Previously, the logging
   1.535 +	 * module didn't enforce the requirement that a fatal callback
   1.536 +	 * actually exit.  Now, it exits no matter what, so if we wan to
   1.537 +	 * reinstate these tests, we'll need to fork for each one. */
   1.538 +	check_error_logging(errx_fn, 2, _EVENT_LOG_ERR,
   1.539 +	    "Fatal error; too many kumquats (5)");
   1.540 +	RESET();
   1.541 +#endif
   1.542 +
   1.543 +	event_warnx("Far too many %s (%d)", "wombats", 99);
   1.544 +	LOGEQ(_EVENT_LOG_WARN, "Far too many wombats (99)");
   1.545 +	RESET();
   1.546 +
   1.547 +	event_msgx("Connecting lime to coconut");
   1.548 +	LOGEQ(_EVENT_LOG_MSG, "Connecting lime to coconut");
   1.549 +	RESET();
   1.550 +
   1.551 +	event_debug(("A millisecond passed! We should log that!"));
   1.552 +#ifdef USE_DEBUG
   1.553 +	LOGEQ(_EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
   1.554 +#else
   1.555 +	tt_int_op(logsev,==,0);
   1.556 +	tt_ptr_op(logmsg,==,NULL);
   1.557 +#endif
   1.558 +	RESET();
   1.559 +
   1.560 +	/* Try with an errno. */
   1.561 +	errno = ENOENT;
   1.562 +	event_warn("Couldn't open %s", "/bad/file");
   1.563 +	evutil_snprintf(buf, sizeof(buf),
   1.564 +	    "Couldn't open /bad/file: %s",strerror(ENOENT));
   1.565 +	LOGEQ(_EVENT_LOG_WARN,buf);
   1.566 +	RESET();
   1.567 +
   1.568 +#ifdef CAN_CHECK_ERR
   1.569 +	evutil_snprintf(buf, sizeof(buf),
   1.570 +	    "Couldn't open /very/bad/file: %s",strerror(ENOENT));
   1.571 +	check_error_logging(err_fn, 5, _EVENT_LOG_ERR, buf);
   1.572 +	RESET();
   1.573 +#endif
   1.574 +
   1.575 +	/* Try with a socket errno. */
   1.576 +	fd = socket(AF_INET, SOCK_STREAM, 0);
   1.577 +#ifdef WIN32
   1.578 +	evutil_snprintf(buf, sizeof(buf),
   1.579 +	    "Unhappy socket: %s",
   1.580 +	    evutil_socket_error_to_string(WSAEWOULDBLOCK));
   1.581 +	EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
   1.582 +#else
   1.583 +	evutil_snprintf(buf, sizeof(buf),
   1.584 +	    "Unhappy socket: %s", strerror(EAGAIN));
   1.585 +	errno = EAGAIN;
   1.586 +#endif
   1.587 +	event_sock_warn(fd, "Unhappy socket");
   1.588 +	LOGEQ(_EVENT_LOG_WARN, buf);
   1.589 +	RESET();
   1.590 +
   1.591 +#ifdef CAN_CHECK_ERR
   1.592 +	check_error_logging(sock_err_fn, 20, _EVENT_LOG_ERR, buf);
   1.593 +	RESET();
   1.594 +#endif
   1.595 +
   1.596 +#undef RESET
   1.597 +#undef LOGEQ
   1.598 +end:
   1.599 +	if (logmsg)
   1.600 +		free(logmsg);
   1.601 +	if (fd >= 0)
   1.602 +		evutil_closesocket(fd);
   1.603 +}
   1.604 +
   1.605 +static void
   1.606 +test_evutil_strlcpy(void *arg)
   1.607 +{
   1.608 +	char buf[8];
   1.609 +
   1.610 +	/* Successful case. */
   1.611 +	tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
   1.612 +	tt_str_op(buf, ==, "Hello");
   1.613 +
   1.614 +	/* Overflow by a lot. */
   1.615 +	tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
   1.616 +	tt_str_op(buf, ==, "pentasy");
   1.617 +
   1.618 +	/* Overflow by exactly one. */
   1.619 +	tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
   1.620 +	tt_str_op(buf, ==, "overlon");
   1.621 +end:
   1.622 +	;
   1.623 +}
   1.624 +
   1.625 +struct example_struct {
   1.626 +	const char *a;
   1.627 +	const char *b;
   1.628 +	long c;
   1.629 +};
   1.630 +
   1.631 +static void
   1.632 +test_evutil_upcast(void *arg)
   1.633 +{
   1.634 +	struct example_struct es1;
   1.635 +	const char **cp;
   1.636 +	es1.a = "World";
   1.637 +	es1.b = "Hello";
   1.638 +	es1.c = -99;
   1.639 +
   1.640 +	tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
   1.641 +
   1.642 +	cp = &es1.b;
   1.643 +	tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
   1.644 +
   1.645 +end:
   1.646 +	;
   1.647 +}
   1.648 +
   1.649 +static void
   1.650 +test_evutil_integers(void *arg)
   1.651 +{
   1.652 +	ev_int64_t i64;
   1.653 +	ev_uint64_t u64;
   1.654 +	ev_int32_t i32;
   1.655 +	ev_uint32_t u32;
   1.656 +	ev_int16_t i16;
   1.657 +	ev_uint16_t u16;
   1.658 +	ev_int8_t  i8;
   1.659 +	ev_uint8_t  u8;
   1.660 +
   1.661 +	void *ptr;
   1.662 +	ev_intptr_t iptr;
   1.663 +	ev_uintptr_t uptr;
   1.664 +
   1.665 +	ev_ssize_t ssize;
   1.666 +
   1.667 +	tt_int_op(sizeof(u64), ==, 8);
   1.668 +	tt_int_op(sizeof(i64), ==, 8);
   1.669 +	tt_int_op(sizeof(u32), ==, 4);
   1.670 +	tt_int_op(sizeof(i32), ==, 4);
   1.671 +	tt_int_op(sizeof(u16), ==, 2);
   1.672 +	tt_int_op(sizeof(i16), ==, 2);
   1.673 +	tt_int_op(sizeof(u8), ==,  1);
   1.674 +	tt_int_op(sizeof(i8), ==,  1);
   1.675 +
   1.676 +	tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
   1.677 +	tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
   1.678 +	tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
   1.679 +
   1.680 +	u64 = 1000000000;
   1.681 +	u64 *= 1000000000;
   1.682 +	tt_assert(u64 / 1000000000 == 1000000000);
   1.683 +	i64 = -1000000000;
   1.684 +	i64 *= 1000000000;
   1.685 +	tt_assert(i64 / 1000000000 == -1000000000);
   1.686 +
   1.687 +	u64 = EV_UINT64_MAX;
   1.688 +	i64 = EV_INT64_MAX;
   1.689 +	tt_assert(u64 > 0);
   1.690 +	tt_assert(i64 > 0);
   1.691 +	u64++;
   1.692 +	i64++;
   1.693 +	tt_assert(u64 == 0);
   1.694 +	tt_assert(i64 == EV_INT64_MIN);
   1.695 +	tt_assert(i64 < 0);
   1.696 +
   1.697 +	u32 = EV_UINT32_MAX;
   1.698 +	i32 = EV_INT32_MAX;
   1.699 +	tt_assert(u32 > 0);
   1.700 +	tt_assert(i32 > 0);
   1.701 +	u32++;
   1.702 +	i32++;
   1.703 +	tt_assert(u32 == 0);
   1.704 +	tt_assert(i32 == EV_INT32_MIN);
   1.705 +	tt_assert(i32 < 0);
   1.706 +
   1.707 +	u16 = EV_UINT16_MAX;
   1.708 +	i16 = EV_INT16_MAX;
   1.709 +	tt_assert(u16 > 0);
   1.710 +	tt_assert(i16 > 0);
   1.711 +	u16++;
   1.712 +	i16++;
   1.713 +	tt_assert(u16 == 0);
   1.714 +	tt_assert(i16 == EV_INT16_MIN);
   1.715 +	tt_assert(i16 < 0);
   1.716 +
   1.717 +	u8 = EV_UINT8_MAX;
   1.718 +	i8 = EV_INT8_MAX;
   1.719 +	tt_assert(u8 > 0);
   1.720 +	tt_assert(i8 > 0);
   1.721 +	u8++;
   1.722 +	i8++;
   1.723 +	tt_assert(u8 == 0);
   1.724 +	tt_assert(i8 == EV_INT8_MIN);
   1.725 +	tt_assert(i8 < 0);
   1.726 +
   1.727 +	ssize = EV_SSIZE_MAX;
   1.728 +	tt_assert(ssize > 0);
   1.729 +	ssize++;
   1.730 +	tt_assert(ssize < 0);
   1.731 +	tt_assert(ssize == EV_SSIZE_MIN);
   1.732 +
   1.733 +	ptr = &ssize;
   1.734 +	iptr = (ev_intptr_t)ptr;
   1.735 +	uptr = (ev_uintptr_t)ptr;
   1.736 +	ptr = (void *)iptr;
   1.737 +	tt_assert(ptr == &ssize);
   1.738 +	ptr = (void *)uptr;
   1.739 +	tt_assert(ptr == &ssize);
   1.740 +
   1.741 +	iptr = -1;
   1.742 +	tt_assert(iptr < 0);
   1.743 +end:
   1.744 +	;
   1.745 +}
   1.746 +
   1.747 +struct evutil_addrinfo *
   1.748 +ai_find_by_family(struct evutil_addrinfo *ai, int family)
   1.749 +{
   1.750 +	while (ai) {
   1.751 +		if (ai->ai_family == family)
   1.752 +			return ai;
   1.753 +		ai = ai->ai_next;
   1.754 +	}
   1.755 +	return NULL;
   1.756 +}
   1.757 +
   1.758 +struct evutil_addrinfo *
   1.759 +ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
   1.760 +{
   1.761 +	while (ai) {
   1.762 +		if (ai->ai_protocol == protocol)
   1.763 +			return ai;
   1.764 +		ai = ai->ai_next;
   1.765 +	}
   1.766 +	return NULL;
   1.767 +}
   1.768 +
   1.769 +
   1.770 +int
   1.771 +_test_ai_eq(const struct evutil_addrinfo *ai, const char *sockaddr_port,
   1.772 +    int socktype, int protocol, int line)
   1.773 +{
   1.774 +	struct sockaddr_storage ss;
   1.775 +	int slen = sizeof(ss);
   1.776 +	int gotport;
   1.777 +	char buf[128];
   1.778 +	memset(&ss, 0, sizeof(ss));
   1.779 +	if (socktype > 0)
   1.780 +		tt_int_op(ai->ai_socktype, ==, socktype);
   1.781 +	if (protocol > 0)
   1.782 +		tt_int_op(ai->ai_protocol, ==, protocol);
   1.783 +
   1.784 +	if (evutil_parse_sockaddr_port(
   1.785 +		    sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
   1.786 +		TT_FAIL(("Couldn't parse expected address %s on line %d",
   1.787 +			sockaddr_port, line));
   1.788 +		return -1;
   1.789 +	}
   1.790 +	if (ai->ai_family != ss.ss_family) {
   1.791 +		TT_FAIL(("Address family %d did not match %d on line %d",
   1.792 +			ai->ai_family, ss.ss_family, line));
   1.793 +		return -1;
   1.794 +	}
   1.795 +	if (ai->ai_addr->sa_family == AF_INET) {
   1.796 +		struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
   1.797 +		evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
   1.798 +		gotport = ntohs(sin->sin_port);
   1.799 +		if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
   1.800 +			TT_FAIL(("Addr size mismatch on line %d", line));
   1.801 +			return -1;
   1.802 +		}
   1.803 +	} else {
   1.804 +		struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
   1.805 +		evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
   1.806 +		gotport = ntohs(sin6->sin6_port);
   1.807 +		if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
   1.808 +			TT_FAIL(("Addr size mismatch on line %d", line));
   1.809 +			return -1;
   1.810 +		}
   1.811 +	}
   1.812 +	if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
   1.813 +		TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
   1.814 +			buf, gotport, line));
   1.815 +		return -1;
   1.816 +	} else {
   1.817 +		TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
   1.818 +			buf, gotport, line));
   1.819 +	}
   1.820 +	return 0;
   1.821 +end:
   1.822 +	TT_FAIL(("Test failed on line %d", line));
   1.823 +	return -1;
   1.824 +}
   1.825 +
   1.826 +static void
   1.827 +test_evutil_rand(void *arg)
   1.828 +{
   1.829 +	char buf1[32];
   1.830 +	char buf2[32];
   1.831 +	int counts[256];
   1.832 +	int i, j, k, n=0;
   1.833 +
   1.834 +	memset(buf2, 0, sizeof(buf2));
   1.835 +	memset(counts, 0, sizeof(counts));
   1.836 +
   1.837 +	for (k=0;k<32;++k) {
   1.838 +		/* Try a few different start and end points; try to catch
   1.839 +		 * the various misaligned cases of arc4random_buf */
   1.840 +		int startpoint = _evutil_weakrand() % 4;
   1.841 +		int endpoint = 32 - (_evutil_weakrand() % 4);
   1.842 +
   1.843 +		memset(buf2, 0, sizeof(buf2));
   1.844 +
   1.845 +		/* Do 6 runs over buf1, or-ing the result into buf2 each
   1.846 +		 * time, to make sure we're setting each byte that we mean
   1.847 +		 * to set. */
   1.848 +		for (i=0;i<8;++i) {
   1.849 +			memset(buf1, 0, sizeof(buf1));
   1.850 +			evutil_secure_rng_get_bytes(buf1 + startpoint,
   1.851 +			    endpoint-startpoint);
   1.852 +			n += endpoint - startpoint;
   1.853 +			for (j=0; j<32; ++j) {
   1.854 +				if (j >= startpoint && j < endpoint) {
   1.855 +					buf2[j] |= buf1[j];
   1.856 +					++counts[(unsigned char)buf1[j]];
   1.857 +				} else {
   1.858 +					tt_assert(buf1[j] == 0);
   1.859 +					tt_int_op(buf1[j], ==, 0);
   1.860 +
   1.861 +				}
   1.862 +			}
   1.863 +		}
   1.864 +
   1.865 +		/* This will give a false positive with P=(256**8)==(2**64)
   1.866 +		 * for each character. */
   1.867 +		for (j=startpoint;j<endpoint;++j) {
   1.868 +			tt_int_op(buf2[j], !=, 0);
   1.869 +		}
   1.870 +	}
   1.871 +
   1.872 +	/* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
   1.873 +end:
   1.874 +	;
   1.875 +}
   1.876 +
   1.877 +static void
   1.878 +test_evutil_getaddrinfo(void *arg)
   1.879 +{
   1.880 +	struct evutil_addrinfo *ai = NULL, *a;
   1.881 +	struct evutil_addrinfo hints;
   1.882 +
   1.883 +	struct sockaddr_in6 *sin6;
   1.884 +	struct sockaddr_in *sin;
   1.885 +	char buf[128];
   1.886 +	const char *cp;
   1.887 +	int r;
   1.888 +
   1.889 +	/* Try using it as a pton. */
   1.890 +	memset(&hints, 0, sizeof(hints));
   1.891 +	hints.ai_family = PF_UNSPEC;
   1.892 +	hints.ai_socktype = SOCK_STREAM;
   1.893 +	r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
   1.894 +	tt_int_op(r, ==, 0);
   1.895 +	tt_assert(ai);
   1.896 +	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
   1.897 +	test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
   1.898 +	evutil_freeaddrinfo(ai);
   1.899 +	ai = NULL;
   1.900 +
   1.901 +	memset(&hints, 0, sizeof(hints));
   1.902 +	hints.ai_family = PF_UNSPEC;
   1.903 +	hints.ai_protocol = IPPROTO_UDP;
   1.904 +	r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
   1.905 +	tt_int_op(r, ==, 0);
   1.906 +	tt_assert(ai);
   1.907 +	tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
   1.908 +	test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
   1.909 +	evutil_freeaddrinfo(ai);
   1.910 +	ai = NULL;
   1.911 +
   1.912 +	/* Try out the behavior of nodename=NULL */
   1.913 +	memset(&hints, 0, sizeof(hints));
   1.914 +	hints.ai_family = PF_INET;
   1.915 +	hints.ai_protocol = IPPROTO_TCP;
   1.916 +	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
   1.917 +	r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
   1.918 +	tt_int_op(r,==,0);
   1.919 +	tt_assert(ai);
   1.920 +	tt_ptr_op(ai->ai_next, ==, NULL);
   1.921 +	test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
   1.922 +	evutil_freeaddrinfo(ai);
   1.923 +	ai = NULL;
   1.924 +	hints.ai_flags = 0; /* as if for connect */
   1.925 +	r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
   1.926 +	tt_assert(ai);
   1.927 +	tt_int_op(r,==,0);
   1.928 +	test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
   1.929 +	tt_ptr_op(ai->ai_next, ==, NULL);
   1.930 +	evutil_freeaddrinfo(ai);
   1.931 +	ai = NULL;
   1.932 +
   1.933 +	hints.ai_flags = 0; /* as if for connect */
   1.934 +	hints.ai_family = PF_INET6;
   1.935 +	r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
   1.936 +	tt_assert(ai);
   1.937 +	tt_int_op(r,==,0);
   1.938 +	tt_ptr_op(ai->ai_next, ==, NULL);
   1.939 +	test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
   1.940 +	evutil_freeaddrinfo(ai);
   1.941 +	ai = NULL;
   1.942 +
   1.943 +	hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
   1.944 +	hints.ai_family = PF_INET6;
   1.945 +	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
   1.946 +	tt_assert(ai);
   1.947 +	tt_int_op(r,==,0);
   1.948 +	tt_ptr_op(ai->ai_next, ==, NULL);
   1.949 +	test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
   1.950 +	evutil_freeaddrinfo(ai);
   1.951 +	ai = NULL;
   1.952 +
   1.953 +	/* Now try an unspec one. We should get a v6 and a v4. */
   1.954 +	hints.ai_family = PF_UNSPEC;
   1.955 +	r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
   1.956 +	tt_assert(ai);
   1.957 +	tt_int_op(r,==,0);
   1.958 +	a = ai_find_by_family(ai, PF_INET6);
   1.959 +	tt_assert(a);
   1.960 +	test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
   1.961 +	a = ai_find_by_family(ai, PF_INET);
   1.962 +	tt_assert(a);
   1.963 +	test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
   1.964 +	evutil_freeaddrinfo(ai);
   1.965 +	ai = NULL;
   1.966 +
   1.967 +	/* Try out AI_NUMERICHOST: successful case.  Also try
   1.968 +	 * multiprotocol. */
   1.969 +	memset(&hints, 0, sizeof(hints));
   1.970 +	hints.ai_family = PF_UNSPEC;
   1.971 +	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
   1.972 +	r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
   1.973 +	tt_int_op(r, ==, 0);
   1.974 +	a = ai_find_by_protocol(ai, IPPROTO_TCP);
   1.975 +	tt_assert(a);
   1.976 +	test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
   1.977 +	a = ai_find_by_protocol(ai, IPPROTO_UDP);
   1.978 +	tt_assert(a);
   1.979 +	test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
   1.980 +	evutil_freeaddrinfo(ai);
   1.981 +	ai = NULL;
   1.982 +
   1.983 +	/* Try the failing case of AI_NUMERICHOST */
   1.984 +	memset(&hints, 0, sizeof(hints));
   1.985 +	hints.ai_family = PF_UNSPEC;
   1.986 +	hints.ai_flags = EVUTIL_AI_NUMERICHOST;
   1.987 +	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
   1.988 +	tt_int_op(r, ==, EVUTIL_EAI_NONAME);
   1.989 +	tt_ptr_op(ai, ==, NULL);
   1.990 +
   1.991 +	/* Try symbolic service names wit AI_NUMERICSERV */
   1.992 +	memset(&hints, 0, sizeof(hints));
   1.993 +	hints.ai_family = PF_UNSPEC;
   1.994 +	hints.ai_socktype = SOCK_STREAM;
   1.995 +	hints.ai_flags = EVUTIL_AI_NUMERICSERV;
   1.996 +	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
   1.997 +	tt_int_op(r,==,EVUTIL_EAI_NONAME);
   1.998 +
   1.999 +	/* Try symbolic service names */
  1.1000 +	memset(&hints, 0, sizeof(hints));
  1.1001 +	hints.ai_family = PF_UNSPEC;
  1.1002 +	hints.ai_socktype = SOCK_STREAM;
  1.1003 +	r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
  1.1004 +	if (r!=0) {
  1.1005 +		TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
  1.1006 +	} else {
  1.1007 +		tt_assert(ai);
  1.1008 +		test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
  1.1009 +		evutil_freeaddrinfo(ai);
  1.1010 +		ai = NULL;
  1.1011 +	}
  1.1012 +
  1.1013 +	/* Now do some actual lookups. */
  1.1014 +	memset(&hints, 0, sizeof(hints));
  1.1015 +	hints.ai_family = PF_INET;
  1.1016 +	hints.ai_protocol = IPPROTO_TCP;
  1.1017 +	hints.ai_socktype = SOCK_STREAM;
  1.1018 +	r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
  1.1019 +	if (r != 0) {
  1.1020 +		TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
  1.1021 +	} else {
  1.1022 +		tt_assert(ai);
  1.1023 +		tt_int_op(ai->ai_family, ==, PF_INET);
  1.1024 +		tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
  1.1025 +		tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
  1.1026 +		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
  1.1027 +		sin = (struct sockaddr_in*)ai->ai_addr;
  1.1028 +		tt_int_op(sin->sin_family, ==, AF_INET);
  1.1029 +		tt_int_op(sin->sin_port, ==, htons(80));
  1.1030 +		tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
  1.1031 +
  1.1032 +		cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
  1.1033 +		TT_BLATHER(("www.google.com resolved to %s",
  1.1034 +			cp?cp:"<unwriteable>"));
  1.1035 +		evutil_freeaddrinfo(ai);
  1.1036 +		ai = NULL;
  1.1037 +	}
  1.1038 +
  1.1039 +	hints.ai_family = PF_INET6;
  1.1040 +	r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
  1.1041 +	if (r != 0) {
  1.1042 +		TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
  1.1043 +	} else {
  1.1044 +		tt_assert(ai);
  1.1045 +		tt_int_op(ai->ai_family, ==, PF_INET6);
  1.1046 +		tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
  1.1047 +		sin6 = (struct sockaddr_in6*)ai->ai_addr;
  1.1048 +		tt_int_op(sin6->sin6_port, ==, htons(80));
  1.1049 +
  1.1050 +		cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
  1.1051 +		    sizeof(buf));
  1.1052 +		TT_BLATHER(("ipv6.google.com resolved to %s",
  1.1053 +			cp?cp:"<unwriteable>"));
  1.1054 +	}
  1.1055 +
  1.1056 +end:
  1.1057 +	if (ai)
  1.1058 +		evutil_freeaddrinfo(ai);
  1.1059 +}
  1.1060 +
  1.1061 +#ifdef WIN32
  1.1062 +static void
  1.1063 +test_evutil_loadsyslib(void *arg)
  1.1064 +{
  1.1065 +	HANDLE h=NULL;
  1.1066 +
  1.1067 +	h = evutil_load_windows_system_library(TEXT("kernel32.dll"));
  1.1068 +	tt_assert(h);
  1.1069 +
  1.1070 +end:
  1.1071 +	if (h)
  1.1072 +		CloseHandle(h);
  1.1073 +
  1.1074 +}
  1.1075 +#endif
  1.1076 +
  1.1077 +struct testcase_t util_testcases[] = {
  1.1078 +	{ "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
  1.1079 +	{ "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
  1.1080 +	{ "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
  1.1081 +	{ "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
  1.1082 +	{ "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
  1.1083 +	{ "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
  1.1084 +	{ "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
  1.1085 +	{ "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
  1.1086 +	{ "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
  1.1087 +	{ "log", test_evutil_log, TT_FORK, NULL, NULL },
  1.1088 +	{ "upcast", test_evutil_upcast, 0, NULL, NULL },
  1.1089 +	{ "integers", test_evutil_integers, 0, NULL, NULL },
  1.1090 +	{ "rand", test_evutil_rand, TT_FORK, NULL, NULL },
  1.1091 +	{ "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
  1.1092 +#ifdef WIN32
  1.1093 +	{ "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
  1.1094 +#endif
  1.1095 +	END_OF_TESTCASES,
  1.1096 +};
  1.1097 +

mercurial