michael@0: /* michael@0: * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifdef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #include michael@0: michael@0: #ifndef WIN32 michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_NETINET_IN6_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SYS_WAIT_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/event.h" michael@0: #include "event2/util.h" michael@0: #include "../ipv6-internal.h" michael@0: #include "../util-internal.h" michael@0: #include "../log-internal.h" michael@0: #include "../strlcpy-internal.h" michael@0: michael@0: #include "regress.h" michael@0: michael@0: enum entry_status { NORMAL, CANONICAL, BAD }; michael@0: michael@0: /* This is a big table of results we expect from generating and parsing */ michael@0: static struct ipv4_entry { michael@0: const char *addr; michael@0: ev_uint32_t res; michael@0: enum entry_status status; michael@0: } ipv4_entries[] = { michael@0: { "1.2.3.4", 0x01020304u, CANONICAL }, michael@0: { "255.255.255.255", 0xffffffffu, CANONICAL }, michael@0: { "256.0.0.0", 0, BAD }, michael@0: { "ABC", 0, BAD }, michael@0: { "1.2.3.4.5", 0, BAD }, michael@0: { "176.192.208.244", 0xb0c0d0f4, CANONICAL }, michael@0: { NULL, 0, BAD }, michael@0: }; michael@0: michael@0: static struct ipv6_entry { michael@0: const char *addr; michael@0: ev_uint32_t res[4]; michael@0: enum entry_status status; michael@0: } ipv6_entries[] = { michael@0: { "::", { 0, 0, 0, 0, }, CANONICAL }, michael@0: { "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL }, michael@0: { "::1", { 0, 0, 0, 1, }, CANONICAL }, michael@0: { "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL }, michael@0: { "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL }, michael@0: { "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL }, michael@0: { "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL }, michael@0: { "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL }, michael@0: { "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL }, michael@0: { "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL }, michael@0: { "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL }, michael@0: { "foobar.", { 0, 0, 0, 0 }, BAD }, michael@0: { "foobar", { 0, 0, 0, 0 }, BAD }, michael@0: { "fo:obar", { 0, 0, 0, 0 }, BAD }, michael@0: { "ffff", { 0, 0, 0, 0 }, BAD }, michael@0: { "fffff::", { 0, 0, 0, 0 }, BAD }, michael@0: { "fffff::", { 0, 0, 0, 0 }, BAD }, michael@0: { "::1.0.1.1000", { 0, 0, 0, 0 }, BAD }, michael@0: { "1:2:33333:4::", { 0, 0, 0, 0 }, BAD }, michael@0: { "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD }, michael@0: { "1::2::3", { 0, 0, 0, 0 }, BAD }, michael@0: { ":::1", { 0, 0, 0, 0 }, BAD }, michael@0: { NULL, { 0, 0, 0, 0, }, BAD }, michael@0: }; michael@0: michael@0: static void michael@0: regress_ipv4_parse(void *ptr) michael@0: { michael@0: int i; michael@0: for (i = 0; ipv4_entries[i].addr; ++i) { michael@0: char written[128]; michael@0: struct ipv4_entry *ent = &ipv4_entries[i]; michael@0: struct in_addr in; michael@0: int r; michael@0: r = evutil_inet_pton(AF_INET, ent->addr, &in); michael@0: if (r == 0) { michael@0: if (ent->status != BAD) { michael@0: TT_FAIL(("%s did not parse, but it's a good address!", michael@0: ent->addr)); michael@0: } michael@0: continue; michael@0: } michael@0: if (ent->status == BAD) { michael@0: TT_FAIL(("%s parsed, but we expected an error", ent->addr)); michael@0: continue; michael@0: } michael@0: if (ntohl(in.s_addr) != ent->res) { michael@0: TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr, michael@0: (unsigned long)ntohl(in.s_addr), michael@0: (unsigned long)ent->res)); michael@0: continue; michael@0: } michael@0: if (ent->status == CANONICAL) { michael@0: const char *w = evutil_inet_ntop(AF_INET, &in, written, michael@0: sizeof(written)); michael@0: if (!w) { michael@0: TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); michael@0: continue; michael@0: } michael@0: if (strcmp(written, ent->addr)) { michael@0: TT_FAIL(("Tried to write out %s; got %s", michael@0: ent->addr, written)); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: } michael@0: michael@0: } michael@0: michael@0: static void michael@0: regress_ipv6_parse(void *ptr) michael@0: { michael@0: #ifdef AF_INET6 michael@0: int i, j; michael@0: michael@0: for (i = 0; ipv6_entries[i].addr; ++i) { michael@0: char written[128]; michael@0: struct ipv6_entry *ent = &ipv6_entries[i]; michael@0: struct in6_addr in6; michael@0: int r; michael@0: r = evutil_inet_pton(AF_INET6, ent->addr, &in6); michael@0: if (r == 0) { michael@0: if (ent->status != BAD) michael@0: TT_FAIL(("%s did not parse, but it's a good address!", michael@0: ent->addr)); michael@0: continue; michael@0: } michael@0: if (ent->status == BAD) { michael@0: TT_FAIL(("%s parsed, but we expected an error", ent->addr)); michael@0: continue; michael@0: } michael@0: for (j = 0; j < 4; ++j) { michael@0: /* Can't use s6_addr32 here; some don't have it. */ michael@0: ev_uint32_t u = michael@0: (in6.s6_addr[j*4 ] << 24) | michael@0: (in6.s6_addr[j*4+1] << 16) | michael@0: (in6.s6_addr[j*4+2] << 8) | michael@0: (in6.s6_addr[j*4+3]); michael@0: if (u != ent->res[j]) { michael@0: TT_FAIL(("%s did not parse as expected.", ent->addr)); michael@0: continue; michael@0: } michael@0: } michael@0: if (ent->status == CANONICAL) { michael@0: const char *w = evutil_inet_ntop(AF_INET6, &in6, written, michael@0: sizeof(written)); michael@0: if (!w) { michael@0: TT_FAIL(("Tried to write out %s; got NULL.", ent->addr)); michael@0: continue; michael@0: } michael@0: if (strcmp(written, ent->addr)) { michael@0: TT_FAIL(("Tried to write out %s; got %s", ent->addr, written)); michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: } michael@0: #else michael@0: TT_BLATHER(("Skipping IPv6 address parsing.")); michael@0: #endif michael@0: } michael@0: michael@0: static struct sa_port_ent { michael@0: const char *parse; michael@0: int safamily; michael@0: const char *addr; michael@0: int port; michael@0: } sa_port_ents[] = { michael@0: { "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 }, michael@0: { "[ffff::1]", AF_INET6, "ffff::1", 0 }, michael@0: { "[ffff::1", 0, NULL, 0 }, michael@0: { "[ffff::1]:65599", 0, NULL, 0 }, michael@0: { "[ffff::1]:0", 0, NULL, 0 }, michael@0: { "[ffff::1]:-1", 0, NULL, 0 }, michael@0: { "::1", AF_INET6, "::1", 0 }, michael@0: { "1:2::1", AF_INET6, "1:2::1", 0 }, michael@0: { "192.168.0.1:50", AF_INET, "192.168.0.1", 50 }, michael@0: { "1.2.3.4", AF_INET, "1.2.3.4", 0 }, michael@0: { NULL, 0, NULL, 0 }, michael@0: }; michael@0: michael@0: static void michael@0: regress_sockaddr_port_parse(void *ptr) michael@0: { michael@0: struct sockaddr_storage ss; michael@0: int i, r; michael@0: michael@0: for (i = 0; sa_port_ents[i].parse; ++i) { michael@0: struct sa_port_ent *ent = &sa_port_ents[i]; michael@0: int len = sizeof(ss); michael@0: memset(&ss, 0, sizeof(ss)); michael@0: r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); michael@0: if (r < 0) { michael@0: if (ent->safamily) michael@0: TT_FAIL(("Couldn't parse %s!", ent->parse)); michael@0: continue; michael@0: } else if (! ent->safamily) { michael@0: TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse)); michael@0: continue; michael@0: } michael@0: if (ent->safamily == AF_INET) { michael@0: struct sockaddr_in sin; michael@0: memset(&sin, 0, sizeof(sin)); michael@0: #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN_SIN_LEN michael@0: sin.sin_len = sizeof(sin); michael@0: #endif michael@0: sin.sin_family = AF_INET; michael@0: sin.sin_port = htons(ent->port); michael@0: r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr); michael@0: if (1 != r) { michael@0: TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr)); michael@0: } else if (memcmp(&sin, &ss, sizeof(sin))) { michael@0: TT_FAIL(("Parse for %s was not as expected.", ent->parse)); michael@0: } else if (len != sizeof(sin)) { michael@0: TT_FAIL(("Length for %s not as expected.",ent->parse)); michael@0: } michael@0: } else { michael@0: struct sockaddr_in6 sin6; michael@0: memset(&sin6, 0, sizeof(sin6)); michael@0: #ifdef _EVENT_HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN michael@0: sin6.sin6_len = sizeof(sin6); michael@0: #endif michael@0: sin6.sin6_family = AF_INET6; michael@0: sin6.sin6_port = htons(ent->port); michael@0: r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr); michael@0: if (1 != r) { michael@0: TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr)); michael@0: } else if (memcmp(&sin6, &ss, sizeof(sin6))) { michael@0: TT_FAIL(("Parse for %s was not as expected.", ent->parse)); michael@0: } else if (len != sizeof(sin6)) { michael@0: TT_FAIL(("Length for %s not as expected.",ent->parse)); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: static void michael@0: regress_sockaddr_port_format(void *ptr) michael@0: { michael@0: struct sockaddr_storage ss; michael@0: int len; michael@0: const char *cp; michael@0: char cbuf[128]; michael@0: int r; michael@0: michael@0: len = sizeof(ss); michael@0: r = evutil_parse_sockaddr_port("192.168.1.1:80", michael@0: (struct sockaddr*)&ss, &len); michael@0: tt_int_op(r,==,0); michael@0: cp = evutil_format_sockaddr_port( michael@0: (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); michael@0: tt_ptr_op(cp,==,cbuf); michael@0: tt_str_op(cp,==,"192.168.1.1:80"); michael@0: michael@0: len = sizeof(ss); michael@0: r = evutil_parse_sockaddr_port("[ff00::8010]:999", michael@0: (struct sockaddr*)&ss, &len); michael@0: tt_int_op(r,==,0); michael@0: cp = evutil_format_sockaddr_port( michael@0: (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); michael@0: tt_ptr_op(cp,==,cbuf); michael@0: tt_str_op(cp,==,"[ff00::8010]:999"); michael@0: michael@0: ss.ss_family=99; michael@0: cp = evutil_format_sockaddr_port( michael@0: (struct sockaddr*)&ss, cbuf, sizeof(cbuf)); michael@0: tt_ptr_op(cp,==,cbuf); michael@0: tt_str_op(cp,==,""); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static struct sa_pred_ent { michael@0: const char *parse; michael@0: michael@0: int is_loopback; michael@0: } sa_pred_entries[] = { michael@0: { "127.0.0.1", 1 }, michael@0: { "127.0.3.2", 1 }, michael@0: { "128.1.2.3", 0 }, michael@0: { "18.0.0.1", 0 }, michael@0: { "129.168.1.1", 0 }, michael@0: michael@0: { "::1", 1 }, michael@0: { "::0", 0 }, michael@0: { "f::1", 0 }, michael@0: { "::501", 0 }, michael@0: { NULL, 0 }, michael@0: michael@0: }; michael@0: michael@0: static void michael@0: test_evutil_sockaddr_predicates(void *ptr) michael@0: { michael@0: struct sockaddr_storage ss; michael@0: int r, i; michael@0: michael@0: for (i=0; sa_pred_entries[i].parse; ++i) { michael@0: struct sa_pred_ent *ent = &sa_pred_entries[i]; michael@0: int len = sizeof(ss); michael@0: michael@0: r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len); michael@0: michael@0: if (r<0) { michael@0: TT_FAIL(("Couldn't parse %s!", ent->parse)); michael@0: continue; michael@0: } michael@0: michael@0: /* sockaddr_is_loopback */ michael@0: if (ent->is_loopback != evutil_sockaddr_is_loopback((struct sockaddr*)&ss)) { michael@0: TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected", michael@0: ent->parse)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void michael@0: test_evutil_strtoll(void *ptr) michael@0: { michael@0: const char *s; michael@0: char *endptr; michael@0: michael@0: tt_want(evutil_strtoll("5000000000", NULL, 10) == michael@0: ((ev_int64_t)5000000)*1000); michael@0: tt_want(evutil_strtoll("-5000000000", NULL, 10) == michael@0: ((ev_int64_t)5000000)*-1000); michael@0: s = " 99999stuff"; michael@0: tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999); michael@0: tt_want(endptr == s+6); michael@0: tt_want(evutil_strtoll("foo", NULL, 10) == 0); michael@0: } michael@0: michael@0: static void michael@0: test_evutil_snprintf(void *ptr) michael@0: { michael@0: char buf[16]; michael@0: int r; michael@0: ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200; michael@0: ev_int64_t i64 = -1 * (ev_int64_t) u64; michael@0: size_t size = 8000; michael@0: ev_ssize_t ssize = -9000; michael@0: michael@0: r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100); michael@0: tt_str_op(buf, ==, "50 100"); michael@0: tt_int_op(r, ==, 6); michael@0: michael@0: r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890); michael@0: tt_str_op(buf, ==, "longish 1234567"); michael@0: tt_int_op(r, ==, 18); michael@0: michael@0: r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64)); michael@0: tt_str_op(buf, ==, "200000000000"); michael@0: tt_int_op(r, ==, 12); michael@0: michael@0: r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64)); michael@0: tt_str_op(buf, ==, "-200000000000"); michael@0: tt_int_op(r, ==, 13); michael@0: michael@0: r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT, michael@0: EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize)); michael@0: tt_str_op(buf, ==, "8000 -9000"); michael@0: tt_int_op(r, ==, 10); michael@0: michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: test_evutil_casecmp(void *ptr) michael@0: { michael@0: tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0); michael@0: tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0); michael@0: tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0); michael@0: tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0); michael@0: tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0); michael@0: michael@0: tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0); michael@0: tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static int logsev = 0; michael@0: static char *logmsg = NULL; michael@0: michael@0: static void michael@0: logfn(int severity, const char *msg) michael@0: { michael@0: logsev = severity; michael@0: tt_want(msg); michael@0: if (msg) { michael@0: if (logmsg) michael@0: free(logmsg); michael@0: logmsg = strdup(msg); michael@0: } michael@0: } michael@0: michael@0: static int fatal_want_severity = 0; michael@0: static const char *fatal_want_message = NULL; michael@0: static void michael@0: fatalfn(int exitcode) michael@0: { michael@0: if (logsev != fatal_want_severity || michael@0: !logmsg || michael@0: strcmp(logmsg, fatal_want_message)) michael@0: exit(0); michael@0: else michael@0: exit(exitcode); michael@0: } michael@0: michael@0: #ifndef WIN32 michael@0: #define CAN_CHECK_ERR michael@0: static void michael@0: check_error_logging(void (*fn)(void), int wantexitcode, michael@0: int wantseverity, const char *wantmsg) michael@0: { michael@0: pid_t pid; michael@0: int status = 0, exitcode; michael@0: fatal_want_severity = wantseverity; michael@0: fatal_want_message = wantmsg; michael@0: if ((pid = regress_fork()) == 0) { michael@0: /* child process */ michael@0: fn(); michael@0: exit(0); /* should be unreachable. */ michael@0: } else { michael@0: wait(&status); michael@0: exitcode = WEXITSTATUS(status); michael@0: tt_int_op(wantexitcode, ==, exitcode); michael@0: } michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: errx_fn(void) michael@0: { michael@0: event_errx(2, "Fatal error; too many kumquats (%d)", 5); michael@0: } michael@0: michael@0: static void michael@0: err_fn(void) michael@0: { michael@0: errno = ENOENT; michael@0: event_err(5,"Couldn't open %s", "/very/bad/file"); michael@0: } michael@0: michael@0: static void michael@0: sock_err_fn(void) michael@0: { michael@0: evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0); michael@0: #ifdef WIN32 michael@0: EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); michael@0: #else michael@0: errno = EAGAIN; michael@0: #endif michael@0: event_sock_err(20, fd, "Unhappy socket"); michael@0: } michael@0: #endif michael@0: michael@0: static void michael@0: test_evutil_log(void *ptr) michael@0: { michael@0: evutil_socket_t fd = -1; michael@0: char buf[128]; michael@0: michael@0: event_set_log_callback(logfn); michael@0: event_set_fatal_callback(fatalfn); michael@0: #define RESET() do { \ michael@0: logsev = 0; \ michael@0: if (logmsg) free(logmsg); \ michael@0: logmsg = NULL; \ michael@0: } while (0) michael@0: #define LOGEQ(sev,msg) do { \ michael@0: tt_int_op(logsev,==,sev); \ michael@0: tt_assert(logmsg != NULL); \ michael@0: tt_str_op(logmsg,==,msg); \ michael@0: } while (0) michael@0: michael@0: #ifdef CAN_CHECK_ERR michael@0: /* We need to disable these tests for now. Previously, the logging michael@0: * module didn't enforce the requirement that a fatal callback michael@0: * actually exit. Now, it exits no matter what, so if we wan to michael@0: * reinstate these tests, we'll need to fork for each one. */ michael@0: check_error_logging(errx_fn, 2, _EVENT_LOG_ERR, michael@0: "Fatal error; too many kumquats (5)"); michael@0: RESET(); michael@0: #endif michael@0: michael@0: event_warnx("Far too many %s (%d)", "wombats", 99); michael@0: LOGEQ(_EVENT_LOG_WARN, "Far too many wombats (99)"); michael@0: RESET(); michael@0: michael@0: event_msgx("Connecting lime to coconut"); michael@0: LOGEQ(_EVENT_LOG_MSG, "Connecting lime to coconut"); michael@0: RESET(); michael@0: michael@0: event_debug(("A millisecond passed! We should log that!")); michael@0: #ifdef USE_DEBUG michael@0: LOGEQ(_EVENT_LOG_DEBUG, "A millisecond passed! We should log that!"); michael@0: #else michael@0: tt_int_op(logsev,==,0); michael@0: tt_ptr_op(logmsg,==,NULL); michael@0: #endif michael@0: RESET(); michael@0: michael@0: /* Try with an errno. */ michael@0: errno = ENOENT; michael@0: event_warn("Couldn't open %s", "/bad/file"); michael@0: evutil_snprintf(buf, sizeof(buf), michael@0: "Couldn't open /bad/file: %s",strerror(ENOENT)); michael@0: LOGEQ(_EVENT_LOG_WARN,buf); michael@0: RESET(); michael@0: michael@0: #ifdef CAN_CHECK_ERR michael@0: evutil_snprintf(buf, sizeof(buf), michael@0: "Couldn't open /very/bad/file: %s",strerror(ENOENT)); michael@0: check_error_logging(err_fn, 5, _EVENT_LOG_ERR, buf); michael@0: RESET(); michael@0: #endif michael@0: michael@0: /* Try with a socket errno. */ michael@0: fd = socket(AF_INET, SOCK_STREAM, 0); michael@0: #ifdef WIN32 michael@0: evutil_snprintf(buf, sizeof(buf), michael@0: "Unhappy socket: %s", michael@0: evutil_socket_error_to_string(WSAEWOULDBLOCK)); michael@0: EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); michael@0: #else michael@0: evutil_snprintf(buf, sizeof(buf), michael@0: "Unhappy socket: %s", strerror(EAGAIN)); michael@0: errno = EAGAIN; michael@0: #endif michael@0: event_sock_warn(fd, "Unhappy socket"); michael@0: LOGEQ(_EVENT_LOG_WARN, buf); michael@0: RESET(); michael@0: michael@0: #ifdef CAN_CHECK_ERR michael@0: check_error_logging(sock_err_fn, 20, _EVENT_LOG_ERR, buf); michael@0: RESET(); michael@0: #endif michael@0: michael@0: #undef RESET michael@0: #undef LOGEQ michael@0: end: michael@0: if (logmsg) michael@0: free(logmsg); michael@0: if (fd >= 0) michael@0: evutil_closesocket(fd); michael@0: } michael@0: michael@0: static void michael@0: test_evutil_strlcpy(void *arg) michael@0: { michael@0: char buf[8]; michael@0: michael@0: /* Successful case. */ michael@0: tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf))); michael@0: tt_str_op(buf, ==, "Hello"); michael@0: michael@0: /* Overflow by a lot. */ michael@0: tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf))); michael@0: tt_str_op(buf, ==, "pentasy"); michael@0: michael@0: /* Overflow by exactly one. */ michael@0: tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf))); michael@0: tt_str_op(buf, ==, "overlon"); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: struct example_struct { michael@0: const char *a; michael@0: const char *b; michael@0: long c; michael@0: }; michael@0: michael@0: static void michael@0: test_evutil_upcast(void *arg) michael@0: { michael@0: struct example_struct es1; michael@0: const char **cp; michael@0: es1.a = "World"; michael@0: es1.b = "Hello"; michael@0: es1.c = -99; michael@0: michael@0: tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*)); michael@0: michael@0: cp = &es1.b; michael@0: tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1); michael@0: michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: static void michael@0: test_evutil_integers(void *arg) michael@0: { michael@0: ev_int64_t i64; michael@0: ev_uint64_t u64; michael@0: ev_int32_t i32; michael@0: ev_uint32_t u32; michael@0: ev_int16_t i16; michael@0: ev_uint16_t u16; michael@0: ev_int8_t i8; michael@0: ev_uint8_t u8; michael@0: michael@0: void *ptr; michael@0: ev_intptr_t iptr; michael@0: ev_uintptr_t uptr; michael@0: michael@0: ev_ssize_t ssize; michael@0: michael@0: tt_int_op(sizeof(u64), ==, 8); michael@0: tt_int_op(sizeof(i64), ==, 8); michael@0: tt_int_op(sizeof(u32), ==, 4); michael@0: tt_int_op(sizeof(i32), ==, 4); michael@0: tt_int_op(sizeof(u16), ==, 2); michael@0: tt_int_op(sizeof(i16), ==, 2); michael@0: tt_int_op(sizeof(u8), ==, 1); michael@0: tt_int_op(sizeof(i8), ==, 1); michael@0: michael@0: tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t)); michael@0: tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *)); michael@0: tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t)); michael@0: michael@0: u64 = 1000000000; michael@0: u64 *= 1000000000; michael@0: tt_assert(u64 / 1000000000 == 1000000000); michael@0: i64 = -1000000000; michael@0: i64 *= 1000000000; michael@0: tt_assert(i64 / 1000000000 == -1000000000); michael@0: michael@0: u64 = EV_UINT64_MAX; michael@0: i64 = EV_INT64_MAX; michael@0: tt_assert(u64 > 0); michael@0: tt_assert(i64 > 0); michael@0: u64++; michael@0: i64++; michael@0: tt_assert(u64 == 0); michael@0: tt_assert(i64 == EV_INT64_MIN); michael@0: tt_assert(i64 < 0); michael@0: michael@0: u32 = EV_UINT32_MAX; michael@0: i32 = EV_INT32_MAX; michael@0: tt_assert(u32 > 0); michael@0: tt_assert(i32 > 0); michael@0: u32++; michael@0: i32++; michael@0: tt_assert(u32 == 0); michael@0: tt_assert(i32 == EV_INT32_MIN); michael@0: tt_assert(i32 < 0); michael@0: michael@0: u16 = EV_UINT16_MAX; michael@0: i16 = EV_INT16_MAX; michael@0: tt_assert(u16 > 0); michael@0: tt_assert(i16 > 0); michael@0: u16++; michael@0: i16++; michael@0: tt_assert(u16 == 0); michael@0: tt_assert(i16 == EV_INT16_MIN); michael@0: tt_assert(i16 < 0); michael@0: michael@0: u8 = EV_UINT8_MAX; michael@0: i8 = EV_INT8_MAX; michael@0: tt_assert(u8 > 0); michael@0: tt_assert(i8 > 0); michael@0: u8++; michael@0: i8++; michael@0: tt_assert(u8 == 0); michael@0: tt_assert(i8 == EV_INT8_MIN); michael@0: tt_assert(i8 < 0); michael@0: michael@0: ssize = EV_SSIZE_MAX; michael@0: tt_assert(ssize > 0); michael@0: ssize++; michael@0: tt_assert(ssize < 0); michael@0: tt_assert(ssize == EV_SSIZE_MIN); michael@0: michael@0: ptr = &ssize; michael@0: iptr = (ev_intptr_t)ptr; michael@0: uptr = (ev_uintptr_t)ptr; michael@0: ptr = (void *)iptr; michael@0: tt_assert(ptr == &ssize); michael@0: ptr = (void *)uptr; michael@0: tt_assert(ptr == &ssize); michael@0: michael@0: iptr = -1; michael@0: tt_assert(iptr < 0); michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: struct evutil_addrinfo * michael@0: ai_find_by_family(struct evutil_addrinfo *ai, int family) michael@0: { michael@0: while (ai) { michael@0: if (ai->ai_family == family) michael@0: return ai; michael@0: ai = ai->ai_next; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: struct evutil_addrinfo * michael@0: ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol) michael@0: { michael@0: while (ai) { michael@0: if (ai->ai_protocol == protocol) michael@0: return ai; michael@0: ai = ai->ai_next; michael@0: } michael@0: return NULL; michael@0: } michael@0: michael@0: michael@0: int michael@0: _test_ai_eq(const struct evutil_addrinfo *ai, const char *sockaddr_port, michael@0: int socktype, int protocol, int line) michael@0: { michael@0: struct sockaddr_storage ss; michael@0: int slen = sizeof(ss); michael@0: int gotport; michael@0: char buf[128]; michael@0: memset(&ss, 0, sizeof(ss)); michael@0: if (socktype > 0) michael@0: tt_int_op(ai->ai_socktype, ==, socktype); michael@0: if (protocol > 0) michael@0: tt_int_op(ai->ai_protocol, ==, protocol); michael@0: michael@0: if (evutil_parse_sockaddr_port( michael@0: sockaddr_port, (struct sockaddr*)&ss, &slen)<0) { michael@0: TT_FAIL(("Couldn't parse expected address %s on line %d", michael@0: sockaddr_port, line)); michael@0: return -1; michael@0: } michael@0: if (ai->ai_family != ss.ss_family) { michael@0: TT_FAIL(("Address family %d did not match %d on line %d", michael@0: ai->ai_family, ss.ss_family, line)); michael@0: return -1; michael@0: } michael@0: if (ai->ai_addr->sa_family == AF_INET) { michael@0: struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr; michael@0: evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); michael@0: gotport = ntohs(sin->sin_port); michael@0: if (ai->ai_addrlen != sizeof(struct sockaddr_in)) { michael@0: TT_FAIL(("Addr size mismatch on line %d", line)); michael@0: return -1; michael@0: } michael@0: } else { michael@0: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr; michael@0: evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf)); michael@0: gotport = ntohs(sin6->sin6_port); michael@0: if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) { michael@0: TT_FAIL(("Addr size mismatch on line %d", line)); michael@0: return -1; michael@0: } michael@0: } michael@0: if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) { michael@0: TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port, michael@0: buf, gotport, line)); michael@0: return -1; michael@0: } else { michael@0: TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port, michael@0: buf, gotport, line)); michael@0: } michael@0: return 0; michael@0: end: michael@0: TT_FAIL(("Test failed on line %d", line)); michael@0: return -1; michael@0: } michael@0: michael@0: static void michael@0: test_evutil_rand(void *arg) michael@0: { michael@0: char buf1[32]; michael@0: char buf2[32]; michael@0: int counts[256]; michael@0: int i, j, k, n=0; michael@0: michael@0: memset(buf2, 0, sizeof(buf2)); michael@0: memset(counts, 0, sizeof(counts)); michael@0: michael@0: for (k=0;k<32;++k) { michael@0: /* Try a few different start and end points; try to catch michael@0: * the various misaligned cases of arc4random_buf */ michael@0: int startpoint = _evutil_weakrand() % 4; michael@0: int endpoint = 32 - (_evutil_weakrand() % 4); michael@0: michael@0: memset(buf2, 0, sizeof(buf2)); michael@0: michael@0: /* Do 6 runs over buf1, or-ing the result into buf2 each michael@0: * time, to make sure we're setting each byte that we mean michael@0: * to set. */ michael@0: for (i=0;i<8;++i) { michael@0: memset(buf1, 0, sizeof(buf1)); michael@0: evutil_secure_rng_get_bytes(buf1 + startpoint, michael@0: endpoint-startpoint); michael@0: n += endpoint - startpoint; michael@0: for (j=0; j<32; ++j) { michael@0: if (j >= startpoint && j < endpoint) { michael@0: buf2[j] |= buf1[j]; michael@0: ++counts[(unsigned char)buf1[j]]; michael@0: } else { michael@0: tt_assert(buf1[j] == 0); michael@0: tt_int_op(buf1[j], ==, 0); michael@0: michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* This will give a false positive with P=(256**8)==(2**64) michael@0: * for each character. */ michael@0: for (j=startpoint;jai_next, ==, NULL); /* no ambiguity */ michael@0: test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_protocol = IPPROTO_UDP; michael@0: r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai); michael@0: tt_int_op(r, ==, 0); michael@0: tt_assert(ai); michael@0: tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */ michael@0: test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: /* Try out the behavior of nodename=NULL */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_INET; michael@0: hints.ai_protocol = IPPROTO_TCP; michael@0: hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */ michael@0: r = evutil_getaddrinfo(NULL, "9999", &hints, &ai); michael@0: tt_int_op(r,==,0); michael@0: tt_assert(ai); michael@0: tt_ptr_op(ai->ai_next, ==, NULL); michael@0: test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: hints.ai_flags = 0; /* as if for connect */ michael@0: r = evutil_getaddrinfo(NULL, "9998", &hints, &ai); michael@0: tt_assert(ai); michael@0: tt_int_op(r,==,0); michael@0: test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP); michael@0: tt_ptr_op(ai->ai_next, ==, NULL); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: hints.ai_flags = 0; /* as if for connect */ michael@0: hints.ai_family = PF_INET6; michael@0: r = evutil_getaddrinfo(NULL, "9997", &hints, &ai); michael@0: tt_assert(ai); michael@0: tt_int_op(r,==,0); michael@0: tt_ptr_op(ai->ai_next, ==, NULL); michael@0: test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */ michael@0: hints.ai_family = PF_INET6; michael@0: r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); michael@0: tt_assert(ai); michael@0: tt_int_op(r,==,0); michael@0: tt_ptr_op(ai->ai_next, ==, NULL); michael@0: test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: /* Now try an unspec one. We should get a v6 and a v4. */ michael@0: hints.ai_family = PF_UNSPEC; michael@0: r = evutil_getaddrinfo(NULL, "9996", &hints, &ai); michael@0: tt_assert(ai); michael@0: tt_int_op(r,==,0); michael@0: a = ai_find_by_family(ai, PF_INET6); michael@0: tt_assert(a); michael@0: test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP); michael@0: a = ai_find_by_family(ai, PF_INET); michael@0: tt_assert(a); michael@0: test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: /* Try out AI_NUMERICHOST: successful case. Also try michael@0: * multiprotocol. */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_flags = EVUTIL_AI_NUMERICHOST; michael@0: r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai); michael@0: tt_int_op(r, ==, 0); michael@0: a = ai_find_by_protocol(ai, IPPROTO_TCP); michael@0: tt_assert(a); michael@0: test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP); michael@0: a = ai_find_by_protocol(ai, IPPROTO_UDP); michael@0: tt_assert(a); michael@0: test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: michael@0: /* Try the failing case of AI_NUMERICHOST */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_flags = EVUTIL_AI_NUMERICHOST; michael@0: r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); michael@0: tt_int_op(r, ==, EVUTIL_EAI_NONAME); michael@0: tt_ptr_op(ai, ==, NULL); michael@0: michael@0: /* Try symbolic service names wit AI_NUMERICSERV */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_socktype = SOCK_STREAM; michael@0: hints.ai_flags = EVUTIL_AI_NUMERICSERV; michael@0: r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); michael@0: tt_int_op(r,==,EVUTIL_EAI_NONAME); michael@0: michael@0: /* Try symbolic service names */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_UNSPEC; michael@0: hints.ai_socktype = SOCK_STREAM; michael@0: r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai); michael@0: if (r!=0) { michael@0: TT_DECLARE("SKIP", ("Symbolic service names seem broken.")); michael@0: } else { michael@0: tt_assert(ai); michael@0: test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: } michael@0: michael@0: /* Now do some actual lookups. */ michael@0: memset(&hints, 0, sizeof(hints)); michael@0: hints.ai_family = PF_INET; michael@0: hints.ai_protocol = IPPROTO_TCP; michael@0: hints.ai_socktype = SOCK_STREAM; michael@0: r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai); michael@0: if (r != 0) { michael@0: TT_DECLARE("SKIP", ("Couldn't resolve www.google.com")); michael@0: } else { michael@0: tt_assert(ai); michael@0: tt_int_op(ai->ai_family, ==, PF_INET); michael@0: tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP); michael@0: tt_int_op(ai->ai_socktype, ==, SOCK_STREAM); michael@0: tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in)); michael@0: sin = (struct sockaddr_in*)ai->ai_addr; michael@0: tt_int_op(sin->sin_family, ==, AF_INET); michael@0: tt_int_op(sin->sin_port, ==, htons(80)); michael@0: tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff); michael@0: michael@0: cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)); michael@0: TT_BLATHER(("www.google.com resolved to %s", michael@0: cp?cp:"")); michael@0: evutil_freeaddrinfo(ai); michael@0: ai = NULL; michael@0: } michael@0: michael@0: hints.ai_family = PF_INET6; michael@0: r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai); michael@0: if (r != 0) { michael@0: TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com")); michael@0: } else { michael@0: tt_assert(ai); michael@0: tt_int_op(ai->ai_family, ==, PF_INET6); michael@0: tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6)); michael@0: sin6 = (struct sockaddr_in6*)ai->ai_addr; michael@0: tt_int_op(sin6->sin6_port, ==, htons(80)); michael@0: michael@0: cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, michael@0: sizeof(buf)); michael@0: TT_BLATHER(("ipv6.google.com resolved to %s", michael@0: cp?cp:"")); michael@0: } michael@0: michael@0: end: michael@0: if (ai) michael@0: evutil_freeaddrinfo(ai); michael@0: } michael@0: michael@0: #ifdef WIN32 michael@0: static void michael@0: test_evutil_loadsyslib(void *arg) michael@0: { michael@0: HANDLE h=NULL; michael@0: michael@0: h = evutil_load_windows_system_library(TEXT("kernel32.dll")); michael@0: tt_assert(h); michael@0: michael@0: end: michael@0: if (h) michael@0: CloseHandle(h); michael@0: michael@0: } michael@0: #endif michael@0: michael@0: struct testcase_t util_testcases[] = { michael@0: { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL }, michael@0: { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL }, michael@0: { "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL }, michael@0: { "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL }, michael@0: { "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL }, michael@0: { "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL }, michael@0: { "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL }, michael@0: { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL }, michael@0: { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL }, michael@0: { "log", test_evutil_log, TT_FORK, NULL, NULL }, michael@0: { "upcast", test_evutil_upcast, 0, NULL, NULL }, michael@0: { "integers", test_evutil_integers, 0, NULL, NULL }, michael@0: { "rand", test_evutil_rand, TT_FORK, NULL, NULL }, michael@0: { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL }, michael@0: #ifdef WIN32 michael@0: { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL }, michael@0: #endif michael@0: END_OF_TESTCASES, michael@0: }; michael@0: