2 * Copyright (c) 2009-2012 Nick Mathewson and Niels Provos
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "../util-internal.h"
34 #include "event2/event-config.h"
36 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
44 #ifdef EVENT__HAVE_NETINET_IN6_H
45 #include <netinet/in6.h>
47 #ifdef EVENT__HAVE_SYS_WAIT_H
55 #include "event2/event.h"
56 #include "event2/util.h"
57 #include "../ipv6-internal.h"
58 #include "../log-internal.h"
59 #include "../strlcpy-internal.h"
60 #include "../mm-internal.h"
61 #include "../time-internal.h"
65 enum entry_status { NORMAL, CANONICAL, BAD };
67 /* This is a big table of results we expect from generating and parsing */
68 static struct ipv4_entry {
71 enum entry_status status;
73 { "1.2.3.4", 0x01020304u, CANONICAL },
74 { "255.255.255.255", 0xffffffffu, CANONICAL },
75 { "256.0.0.0", 0, BAD },
77 { "1.2.3.4.5", 0, BAD },
78 { "176.192.208.244", 0xb0c0d0f4, CANONICAL },
82 static struct ipv6_entry {
85 enum entry_status status;
87 { "::", { 0, 0, 0, 0, }, CANONICAL },
88 { "0:0:0:0:0:0:0:0", { 0, 0, 0, 0, }, NORMAL },
89 { "::1", { 0, 0, 0, 1, }, CANONICAL },
90 { "::1.2.3.4", { 0, 0, 0, 0x01020304, }, CANONICAL },
91 { "ffff:1::", { 0xffff0001u, 0, 0, 0, }, CANONICAL },
92 { "ffff:0000::", { 0xffff0000u, 0, 0, 0, }, NORMAL },
93 { "ffff::1234", { 0xffff0000u, 0, 0, 0x1234, }, CANONICAL },
94 { "0102::1.2.3.4", {0x01020000u, 0, 0, 0x01020304u }, NORMAL },
95 { "::9:c0a8:1:1", { 0, 0, 0x0009c0a8u, 0x00010001u }, CANONICAL },
96 { "::ffff:1.2.3.4", { 0, 0, 0x000ffffu, 0x01020304u }, CANONICAL },
97 { "FFFF::", { 0xffff0000u, 0, 0, 0 }, NORMAL },
98 { "foobar.", { 0, 0, 0, 0 }, BAD },
99 { "foobar", { 0, 0, 0, 0 }, BAD },
100 { "fo:obar", { 0, 0, 0, 0 }, BAD },
101 { "ffff", { 0, 0, 0, 0 }, BAD },
102 { "fffff::", { 0, 0, 0, 0 }, BAD },
103 { "fffff::", { 0, 0, 0, 0 }, BAD },
104 { "::1.0.1.1000", { 0, 0, 0, 0 }, BAD },
105 { "1:2:33333:4::", { 0, 0, 0, 0 }, BAD },
106 { "1:2:3:4:5:6:7:8:9", { 0, 0, 0, 0 }, BAD },
107 { "1::2::3", { 0, 0, 0, 0 }, BAD },
108 { ":::1", { 0, 0, 0, 0 }, BAD },
109 { NULL, { 0, 0, 0, 0, }, BAD },
113 regress_ipv4_parse(void *ptr)
116 for (i = 0; ipv4_entries[i].addr; ++i) {
118 struct ipv4_entry *ent = &ipv4_entries[i];
121 r = evutil_inet_pton(AF_INET, ent->addr, &in);
123 if (ent->status != BAD) {
124 TT_FAIL(("%s did not parse, but it's a good address!",
129 if (ent->status == BAD) {
130 TT_FAIL(("%s parsed, but we expected an error", ent->addr));
133 if (ntohl(in.s_addr) != ent->res) {
134 TT_FAIL(("%s parsed to %lx, but we expected %lx", ent->addr,
135 (unsigned long)ntohl(in.s_addr),
136 (unsigned long)ent->res));
139 if (ent->status == CANONICAL) {
140 const char *w = evutil_inet_ntop(AF_INET, &in, written,
143 TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
146 if (strcmp(written, ent->addr)) {
147 TT_FAIL(("Tried to write out %s; got %s",
148 ent->addr, written));
158 regress_ipv6_parse(void *ptr)
163 for (i = 0; ipv6_entries[i].addr; ++i) {
165 struct ipv6_entry *ent = &ipv6_entries[i];
168 r = evutil_inet_pton(AF_INET6, ent->addr, &in6);
170 if (ent->status != BAD)
171 TT_FAIL(("%s did not parse, but it's a good address!",
175 if (ent->status == BAD) {
176 TT_FAIL(("%s parsed, but we expected an error", ent->addr));
179 for (j = 0; j < 4; ++j) {
180 /* Can't use s6_addr32 here; some don't have it. */
182 ((ev_uint32_t)in6.s6_addr[j*4 ] << 24) |
183 ((ev_uint32_t)in6.s6_addr[j*4+1] << 16) |
184 ((ev_uint32_t)in6.s6_addr[j*4+2] << 8) |
185 ((ev_uint32_t)in6.s6_addr[j*4+3]);
186 if (u != ent->res[j]) {
187 TT_FAIL(("%s did not parse as expected.", ent->addr));
191 if (ent->status == CANONICAL) {
192 const char *w = evutil_inet_ntop(AF_INET6, &in6, written,
195 TT_FAIL(("Tried to write out %s; got NULL.", ent->addr));
198 if (strcmp(written, ent->addr)) {
199 TT_FAIL(("Tried to write out %s; got %s", ent->addr, written));
206 TT_BLATHER(("Skipping IPv6 address parsing."));
210 static struct sa_port_ent {
216 { "[ffff::1]:1000", AF_INET6, "ffff::1", 1000 },
217 { "[ffff::1]", AF_INET6, "ffff::1", 0 },
218 { "[ffff::1", 0, NULL, 0 },
219 { "[ffff::1]:65599", 0, NULL, 0 },
220 { "[ffff::1]:0", 0, NULL, 0 },
221 { "[ffff::1]:-1", 0, NULL, 0 },
222 { "::1", AF_INET6, "::1", 0 },
223 { "1:2::1", AF_INET6, "1:2::1", 0 },
224 { "192.168.0.1:50", AF_INET, "192.168.0.1", 50 },
225 { "1.2.3.4", AF_INET, "1.2.3.4", 0 },
226 { NULL, 0, NULL, 0 },
230 regress_sockaddr_port_parse(void *ptr)
232 struct sockaddr_storage ss;
235 for (i = 0; sa_port_ents[i].parse; ++i) {
236 struct sa_port_ent *ent = &sa_port_ents[i];
237 int len = sizeof(ss);
238 memset(&ss, 0, sizeof(ss));
239 r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
242 TT_FAIL(("Couldn't parse %s!", ent->parse));
244 } else if (! ent->safamily) {
245 TT_FAIL(("Shouldn't have been able to parse %s!", ent->parse));
248 if (ent->safamily == AF_INET) {
249 struct sockaddr_in sin;
250 memset(&sin, 0, sizeof(sin));
251 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
252 sin.sin_len = sizeof(sin);
254 sin.sin_family = AF_INET;
255 sin.sin_port = htons(ent->port);
256 r = evutil_inet_pton(AF_INET, ent->addr, &sin.sin_addr);
258 TT_FAIL(("Couldn't parse ipv4 target %s.", ent->addr));
259 } else if (memcmp(&sin, &ss, sizeof(sin))) {
260 TT_FAIL(("Parse for %s was not as expected.", ent->parse));
261 } else if (len != sizeof(sin)) {
262 TT_FAIL(("Length for %s not as expected.",ent->parse));
265 struct sockaddr_in6 sin6;
266 memset(&sin6, 0, sizeof(sin6));
267 #ifdef EVENT__HAVE_STRUCT_SOCKADDR_IN6_SIN6_LEN
268 sin6.sin6_len = sizeof(sin6);
270 sin6.sin6_family = AF_INET6;
271 sin6.sin6_port = htons(ent->port);
272 r = evutil_inet_pton(AF_INET6, ent->addr, &sin6.sin6_addr);
274 TT_FAIL(("Couldn't parse ipv6 target %s.", ent->addr));
275 } else if (memcmp(&sin6, &ss, sizeof(sin6))) {
276 TT_FAIL(("Parse for %s was not as expected.", ent->parse));
277 } else if (len != sizeof(sin6)) {
278 TT_FAIL(("Length for %s not as expected.",ent->parse));
286 regress_sockaddr_port_format(void *ptr)
288 struct sockaddr_storage ss;
295 r = evutil_parse_sockaddr_port("192.168.1.1:80",
296 (struct sockaddr*)&ss, &len);
298 cp = evutil_format_sockaddr_port_(
299 (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
300 tt_ptr_op(cp,==,cbuf);
301 tt_str_op(cp,==,"192.168.1.1:80");
304 r = evutil_parse_sockaddr_port("[ff00::8010]:999",
305 (struct sockaddr*)&ss, &len);
307 cp = evutil_format_sockaddr_port_(
308 (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
309 tt_ptr_op(cp,==,cbuf);
310 tt_str_op(cp,==,"[ff00::8010]:999");
313 cp = evutil_format_sockaddr_port_(
314 (struct sockaddr*)&ss, cbuf, sizeof(cbuf));
315 tt_ptr_op(cp,==,cbuf);
316 tt_str_op(cp,==,"<addr with socktype 99>");
321 static struct sa_pred_ent {
325 } sa_pred_entries[] = {
330 { "129.168.1.1", 0 },
341 test_evutil_sockaddr_predicates(void *ptr)
343 struct sockaddr_storage ss;
346 for (i=0; sa_pred_entries[i].parse; ++i) {
347 struct sa_pred_ent *ent = &sa_pred_entries[i];
348 int len = sizeof(ss);
350 r = evutil_parse_sockaddr_port(ent->parse, (struct sockaddr*)&ss, &len);
353 TT_FAIL(("Couldn't parse %s!", ent->parse));
357 /* sockaddr_is_loopback */
358 if (ent->is_loopback != evutil_sockaddr_is_loopback_((struct sockaddr*)&ss)) {
359 TT_FAIL(("evutil_sockaddr_loopback(%s) not as expected",
366 test_evutil_strtoll(void *ptr)
371 tt_want(evutil_strtoll("5000000000", NULL, 10) ==
372 ((ev_int64_t)5000000)*1000);
373 tt_want(evutil_strtoll("-5000000000", NULL, 10) ==
374 ((ev_int64_t)5000000)*-1000);
376 tt_want(evutil_strtoll(s, &endptr, 10) == (ev_int64_t)99999);
377 tt_want(endptr == s+6);
378 tt_want(evutil_strtoll("foo", NULL, 10) == 0);
382 test_evutil_snprintf(void *ptr)
386 ev_uint64_t u64 = ((ev_uint64_t)1000000000)*200;
387 ev_int64_t i64 = -1 * (ev_int64_t) u64;
389 ev_ssize_t ssize = -9000;
391 r = evutil_snprintf(buf, sizeof(buf), "%d %d", 50, 100);
392 tt_str_op(buf, ==, "50 100");
395 r = evutil_snprintf(buf, sizeof(buf), "longish %d", 1234567890);
396 tt_str_op(buf, ==, "longish 1234567");
397 tt_int_op(r, ==, 18);
399 r = evutil_snprintf(buf, sizeof(buf), EV_U64_FMT, EV_U64_ARG(u64));
400 tt_str_op(buf, ==, "200000000000");
401 tt_int_op(r, ==, 12);
403 r = evutil_snprintf(buf, sizeof(buf), EV_I64_FMT, EV_I64_ARG(i64));
404 tt_str_op(buf, ==, "-200000000000");
405 tt_int_op(r, ==, 13);
407 r = evutil_snprintf(buf, sizeof(buf), EV_SIZE_FMT" "EV_SSIZE_FMT,
408 EV_SIZE_ARG(size), EV_SSIZE_ARG(ssize));
409 tt_str_op(buf, ==, "8000 -9000");
410 tt_int_op(r, ==, 10);
417 test_evutil_casecmp(void *ptr)
419 tt_int_op(evutil_ascii_strcasecmp("ABC", "ABC"), ==, 0);
420 tt_int_op(evutil_ascii_strcasecmp("ABC", "abc"), ==, 0);
421 tt_int_op(evutil_ascii_strcasecmp("ABC", "abcd"), <, 0);
422 tt_int_op(evutil_ascii_strcasecmp("ABC", "abb"), >, 0);
423 tt_int_op(evutil_ascii_strcasecmp("ABCd", "abc"), >, 0);
425 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 100), ==, 0);
426 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEvEnT", 4), ==, 0);
427 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibEXXXX", 4), ==, 0);
428 tt_int_op(evutil_ascii_strncasecmp("Libevent", "LibE", 4), ==, 0);
429 tt_int_op(evutil_ascii_strncasecmp("Libe", "LibEvEnT", 4), ==, 0);
430 tt_int_op(evutil_ascii_strncasecmp("Lib", "LibEvEnT", 4), <, 0);
431 tt_int_op(evutil_ascii_strncasecmp("abc", "def", 99), <, 0);
432 tt_int_op(evutil_ascii_strncasecmp("Z", "qrst", 1), >, 0);
438 test_evutil_rtrim(void *ptr)
440 #define TEST_TRIM(s, result) \
442 if (cp) mm_free(cp); \
445 evutil_rtrim_lws_(cp); \
446 tt_str_op(cp, ==, result); \
454 TEST_TRIM("abcdef ghi", "abcdef ghi");
458 TEST_TRIM("a ", "a");
459 TEST_TRIM("abcdef gH ", "abcdef gH");
461 TEST_TRIM("\t\t", "");
462 TEST_TRIM(" \t", "");
464 TEST_TRIM("a \t", "a");
465 TEST_TRIM("a\t ", "a");
466 TEST_TRIM("a\t", "a");
467 TEST_TRIM("abcdef gH \t ", "abcdef gH");
474 static int logsev = 0;
475 static char *logmsg = NULL;
478 logfn(int severity, const char *msg)
485 logmsg = strdup(msg);
489 static int fatal_want_severity = 0;
490 static const char *fatal_want_message = NULL;
492 fatalfn(int exitcode)
494 if (logsev != fatal_want_severity ||
496 strcmp(logmsg, fatal_want_message))
503 #define CAN_CHECK_ERR
505 check_error_logging(void (*fn)(void), int wantexitcode,
506 int wantseverity, const char *wantmsg)
509 int status = 0, exitcode;
510 fatal_want_severity = wantseverity;
511 fatal_want_message = wantmsg;
512 if ((pid = regress_fork()) == 0) {
515 exit(0); /* should be unreachable. */
518 exitcode = WEXITSTATUS(status);
519 tt_int_op(wantexitcode, ==, exitcode);
528 event_errx(2, "Fatal error; too many kumquats (%d)", 5);
535 event_err(5,"Couldn't open %s", "/very/bad/file");
541 evutil_socket_t fd = socket(AF_INET, SOCK_STREAM, 0);
543 EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
547 event_sock_err(20, fd, "Unhappy socket");
552 test_evutil_log(void *ptr)
554 evutil_socket_t fd = -1;
557 event_set_log_callback(logfn);
558 event_set_fatal_callback(fatalfn);
559 #define RESET() do { \
561 if (logmsg) free(logmsg); \
564 #define LOGEQ(sev,msg) do { \
565 tt_int_op(logsev,==,sev); \
566 tt_assert(logmsg != NULL); \
567 tt_str_op(logmsg,==,msg); \
571 /* We need to disable these tests for now. Previously, the logging
572 * module didn't enforce the requirement that a fatal callback
573 * actually exit. Now, it exits no matter what, so if we wan to
574 * reinstate these tests, we'll need to fork for each one. */
575 check_error_logging(errx_fn, 2, EVENT_LOG_ERR,
576 "Fatal error; too many kumquats (5)");
580 event_warnx("Far too many %s (%d)", "wombats", 99);
581 LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)");
584 event_msgx("Connecting lime to coconut");
585 LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut");
588 event_debug(("A millisecond passed! We should log that!"));
590 LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!");
592 tt_int_op(logsev,==,0);
593 tt_ptr_op(logmsg,==,NULL);
597 /* Try with an errno. */
599 event_warn("Couldn't open %s", "/bad/file");
600 evutil_snprintf(buf, sizeof(buf),
601 "Couldn't open /bad/file: %s",strerror(ENOENT));
602 LOGEQ(EVENT_LOG_WARN,buf);
606 evutil_snprintf(buf, sizeof(buf),
607 "Couldn't open /very/bad/file: %s",strerror(ENOENT));
608 check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf);
612 /* Try with a socket errno. */
613 fd = socket(AF_INET, SOCK_STREAM, 0);
615 evutil_snprintf(buf, sizeof(buf),
616 "Unhappy socket: %s",
617 evutil_socket_error_to_string(WSAEWOULDBLOCK));
618 EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK);
620 evutil_snprintf(buf, sizeof(buf),
621 "Unhappy socket: %s", strerror(EAGAIN));
624 event_sock_warn(fd, "Unhappy socket");
625 LOGEQ(EVENT_LOG_WARN, buf);
629 check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf);
639 evutil_closesocket(fd);
643 test_evutil_strlcpy(void *arg)
647 /* Successful case. */
648 tt_int_op(5, ==, strlcpy(buf, "Hello", sizeof(buf)));
649 tt_str_op(buf, ==, "Hello");
651 /* Overflow by a lot. */
652 tt_int_op(13, ==, strlcpy(buf, "pentasyllabic", sizeof(buf)));
653 tt_str_op(buf, ==, "pentasy");
655 /* Overflow by exactly one. */
656 tt_int_op(8, ==, strlcpy(buf, "overlong", sizeof(buf)));
657 tt_str_op(buf, ==, "overlon");
662 struct example_struct {
669 test_evutil_upcast(void *arg)
671 struct example_struct es1;
677 tt_int_op(evutil_offsetof(struct example_struct, b), ==, sizeof(char*));
680 tt_ptr_op(EVUTIL_UPCAST(cp, struct example_struct, b), ==, &es1);
687 test_evutil_integers(void *arg)
704 tt_int_op(sizeof(u64), ==, 8);
705 tt_int_op(sizeof(i64), ==, 8);
706 tt_int_op(sizeof(u32), ==, 4);
707 tt_int_op(sizeof(i32), ==, 4);
708 tt_int_op(sizeof(u16), ==, 2);
709 tt_int_op(sizeof(i16), ==, 2);
710 tt_int_op(sizeof(u8), ==, 1);
711 tt_int_op(sizeof(i8), ==, 1);
713 tt_int_op(sizeof(ev_ssize_t), ==, sizeof(size_t));
714 tt_int_op(sizeof(ev_intptr_t), >=, sizeof(void *));
715 tt_int_op(sizeof(ev_uintptr_t), ==, sizeof(intptr_t));
719 tt_assert(u64 / 1000000000 == 1000000000);
722 tt_assert(i64 / 1000000000 == -1000000000);
731 /* tt_assert(i64 == EV_INT64_MIN); */
732 /* tt_assert(i64 < 0); */
741 /* tt_assert(i32 == EV_INT32_MIN); */
742 /* tt_assert(i32 < 0); */
751 /* tt_assert(i16 == EV_INT16_MIN); */
752 /* tt_assert(i16 < 0); */
761 /* tt_assert(i8 == EV_INT8_MIN); */
762 /* tt_assert(i8 < 0); */
765 ssize = EV_SSIZE_MAX;
766 tt_assert(ssize > 0);
768 tt_assert(ssize < 0);
769 tt_assert(ssize == EV_SSIZE_MIN);
773 iptr = (ev_intptr_t)ptr;
774 uptr = (ev_uintptr_t)ptr;
776 tt_assert(ptr == &ssize);
778 tt_assert(ptr == &ssize);
786 struct evutil_addrinfo *
787 ai_find_by_family(struct evutil_addrinfo *ai, int family)
790 if (ai->ai_family == family)
797 struct evutil_addrinfo *
798 ai_find_by_protocol(struct evutil_addrinfo *ai, int protocol)
801 if (ai->ai_protocol == protocol)
810 test_ai_eq_(const struct evutil_addrinfo *ai, const char *sockaddr_port,
811 int socktype, int protocol, int line)
813 struct sockaddr_storage ss;
814 int slen = sizeof(ss);
817 memset(&ss, 0, sizeof(ss));
819 tt_int_op(ai->ai_socktype, ==, socktype);
821 tt_int_op(ai->ai_protocol, ==, protocol);
823 if (evutil_parse_sockaddr_port(
824 sockaddr_port, (struct sockaddr*)&ss, &slen)<0) {
825 TT_FAIL(("Couldn't parse expected address %s on line %d",
826 sockaddr_port, line));
829 if (ai->ai_family != ss.ss_family) {
830 TT_FAIL(("Address family %d did not match %d on line %d",
831 ai->ai_family, ss.ss_family, line));
834 if (ai->ai_addr->sa_family == AF_INET) {
835 struct sockaddr_in *sin = (struct sockaddr_in*)ai->ai_addr;
836 evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
837 gotport = ntohs(sin->sin_port);
838 if (ai->ai_addrlen != sizeof(struct sockaddr_in)) {
839 TT_FAIL(("Addr size mismatch on line %d", line));
843 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
844 evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, sizeof(buf));
845 gotport = ntohs(sin6->sin6_port);
846 if (ai->ai_addrlen != sizeof(struct sockaddr_in6)) {
847 TT_FAIL(("Addr size mismatch on line %d", line));
851 if (evutil_sockaddr_cmp(ai->ai_addr, (struct sockaddr*)&ss, 1)) {
852 TT_FAIL(("Wanted %s, got %s:%d on line %d", sockaddr_port,
853 buf, gotport, line));
856 TT_BLATHER(("Wanted %s, got %s:%d on line %d", sockaddr_port,
857 buf, gotport, line));
861 TT_FAIL(("Test failed on line %d", line));
866 test_evutil_rand(void *arg)
872 struct evutil_weakrand_state seed = { 12346789U };
874 memset(buf2, 0, sizeof(buf2));
875 memset(counts, 0, sizeof(counts));
878 /* Try a few different start and end points; try to catch
879 * the various misaligned cases of arc4random_buf */
880 int startpoint = evutil_weakrand_(&seed) % 4;
881 int endpoint = 32 - (evutil_weakrand_(&seed) % 4);
883 memset(buf2, 0, sizeof(buf2));
885 /* Do 6 runs over buf1, or-ing the result into buf2 each
886 * time, to make sure we're setting each byte that we mean
889 memset(buf1, 0, sizeof(buf1));
890 evutil_secure_rng_get_bytes(buf1 + startpoint,
891 endpoint-startpoint);
892 n += endpoint - startpoint;
893 for (j=0; j<32; ++j) {
894 if (j >= startpoint && j < endpoint) {
896 ++counts[(unsigned char)buf1[j]];
898 tt_assert(buf1[j] == 0);
899 tt_int_op(buf1[j], ==, 0);
905 /* This will give a false positive with P=(256**8)==(2**64)
906 * for each character. */
907 for (j=startpoint;j<endpoint;++j) {
908 tt_int_op(buf2[j], !=, 0);
912 evutil_weakrand_seed_(&seed, 0);
913 for (i = 0; i < 10000; ++i) {
914 ev_int32_t r = evutil_weakrand_range_(&seed, 9999);
916 tt_int_op(r, <, 9999);
919 /* for (i=0;i<256;++i) { printf("%3d %2d\n", i, counts[i]); } */
925 test_evutil_getaddrinfo(void *arg)
927 struct evutil_addrinfo *ai = NULL, *a;
928 struct evutil_addrinfo hints;
931 /* Try using it as a pton. */
932 memset(&hints, 0, sizeof(hints));
933 hints.ai_family = PF_UNSPEC;
934 hints.ai_socktype = SOCK_STREAM;
935 r = evutil_getaddrinfo("1.2.3.4", "8080", &hints, &ai);
938 tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
939 test_ai_eq(ai, "1.2.3.4:8080", SOCK_STREAM, IPPROTO_TCP);
940 evutil_freeaddrinfo(ai);
943 memset(&hints, 0, sizeof(hints));
944 hints.ai_family = PF_UNSPEC;
945 hints.ai_protocol = IPPROTO_UDP;
946 r = evutil_getaddrinfo("1001:b0b::f00f", "4321", &hints, &ai);
949 tt_ptr_op(ai->ai_next, ==, NULL); /* no ambiguity */
950 test_ai_eq(ai, "[1001:b0b::f00f]:4321", SOCK_DGRAM, IPPROTO_UDP);
951 evutil_freeaddrinfo(ai);
954 /* Try out the behavior of nodename=NULL */
955 memset(&hints, 0, sizeof(hints));
956 hints.ai_family = PF_INET;
957 hints.ai_protocol = IPPROTO_TCP;
958 hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind */
959 r = evutil_getaddrinfo(NULL, "9999", &hints, &ai);
962 tt_ptr_op(ai->ai_next, ==, NULL);
963 test_ai_eq(ai, "0.0.0.0:9999", SOCK_STREAM, IPPROTO_TCP);
964 evutil_freeaddrinfo(ai);
966 hints.ai_flags = 0; /* as if for connect */
967 r = evutil_getaddrinfo(NULL, "9998", &hints, &ai);
970 test_ai_eq(ai, "127.0.0.1:9998", SOCK_STREAM, IPPROTO_TCP);
971 tt_ptr_op(ai->ai_next, ==, NULL);
972 evutil_freeaddrinfo(ai);
975 hints.ai_flags = 0; /* as if for connect */
976 hints.ai_family = PF_INET6;
977 r = evutil_getaddrinfo(NULL, "9997", &hints, &ai);
980 tt_ptr_op(ai->ai_next, ==, NULL);
981 test_ai_eq(ai, "[::1]:9997", SOCK_STREAM, IPPROTO_TCP);
982 evutil_freeaddrinfo(ai);
985 hints.ai_flags = EVUTIL_AI_PASSIVE; /* as if for bind. */
986 hints.ai_family = PF_INET6;
987 r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
990 tt_ptr_op(ai->ai_next, ==, NULL);
991 test_ai_eq(ai, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
992 evutil_freeaddrinfo(ai);
995 /* Now try an unspec one. We should get a v6 and a v4. */
996 hints.ai_family = PF_UNSPEC;
997 r = evutil_getaddrinfo(NULL, "9996", &hints, &ai);
1000 a = ai_find_by_family(ai, PF_INET6);
1002 test_ai_eq(a, "[::]:9996", SOCK_STREAM, IPPROTO_TCP);
1003 a = ai_find_by_family(ai, PF_INET);
1005 test_ai_eq(a, "0.0.0.0:9996", SOCK_STREAM, IPPROTO_TCP);
1006 evutil_freeaddrinfo(ai);
1009 /* Try out AI_NUMERICHOST: successful case. Also try
1011 memset(&hints, 0, sizeof(hints));
1012 hints.ai_family = PF_UNSPEC;
1013 hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1014 r = evutil_getaddrinfo("1.2.3.4", NULL, &hints, &ai);
1015 tt_int_op(r, ==, 0);
1016 a = ai_find_by_protocol(ai, IPPROTO_TCP);
1018 test_ai_eq(a, "1.2.3.4", SOCK_STREAM, IPPROTO_TCP);
1019 a = ai_find_by_protocol(ai, IPPROTO_UDP);
1021 test_ai_eq(a, "1.2.3.4", SOCK_DGRAM, IPPROTO_UDP);
1022 evutil_freeaddrinfo(ai);
1025 /* Try the failing case of AI_NUMERICHOST */
1026 memset(&hints, 0, sizeof(hints));
1027 hints.ai_family = PF_UNSPEC;
1028 hints.ai_flags = EVUTIL_AI_NUMERICHOST;
1029 r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1030 tt_int_op(r, ==, EVUTIL_EAI_NONAME);
1031 tt_ptr_op(ai, ==, NULL);
1033 /* Try symbolic service names wit AI_NUMERICSERV */
1034 memset(&hints, 0, sizeof(hints));
1035 hints.ai_family = PF_UNSPEC;
1036 hints.ai_socktype = SOCK_STREAM;
1037 hints.ai_flags = EVUTIL_AI_NUMERICSERV;
1038 r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1039 tt_int_op(r,==,EVUTIL_EAI_NONAME);
1041 /* Try symbolic service names */
1042 memset(&hints, 0, sizeof(hints));
1043 hints.ai_family = PF_UNSPEC;
1044 hints.ai_socktype = SOCK_STREAM;
1045 r = evutil_getaddrinfo("1.2.3.4", "http", &hints, &ai);
1047 TT_DECLARE("SKIP", ("Symbolic service names seem broken."));
1050 test_ai_eq(ai, "1.2.3.4:80", SOCK_STREAM, IPPROTO_TCP);
1051 evutil_freeaddrinfo(ai);
1057 evutil_freeaddrinfo(ai);
1061 test_evutil_getaddrinfo_live(void *arg)
1063 struct evutil_addrinfo *ai = NULL;
1064 struct evutil_addrinfo hints;
1066 struct sockaddr_in6 *sin6;
1067 struct sockaddr_in *sin;
1072 /* Now do some actual lookups. */
1073 memset(&hints, 0, sizeof(hints));
1074 hints.ai_family = PF_INET;
1075 hints.ai_protocol = IPPROTO_TCP;
1076 hints.ai_socktype = SOCK_STREAM;
1077 r = evutil_getaddrinfo("www.google.com", "80", &hints, &ai);
1079 TT_DECLARE("SKIP", ("Couldn't resolve www.google.com"));
1082 tt_int_op(ai->ai_family, ==, PF_INET);
1083 tt_int_op(ai->ai_protocol, ==, IPPROTO_TCP);
1084 tt_int_op(ai->ai_socktype, ==, SOCK_STREAM);
1085 tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in));
1086 sin = (struct sockaddr_in*)ai->ai_addr;
1087 tt_int_op(sin->sin_family, ==, AF_INET);
1088 tt_int_op(sin->sin_port, ==, htons(80));
1089 tt_int_op(sin->sin_addr.s_addr, !=, 0xffffffff);
1091 cp = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf));
1092 TT_BLATHER(("www.google.com resolved to %s",
1093 cp?cp:"<unwriteable>"));
1094 evutil_freeaddrinfo(ai);
1098 hints.ai_family = PF_INET6;
1099 r = evutil_getaddrinfo("ipv6.google.com", "80", &hints, &ai);
1101 TT_BLATHER(("Couldn't do an ipv6 lookup for ipv6.google.com"));
1104 tt_int_op(ai->ai_family, ==, PF_INET6);
1105 tt_int_op(ai->ai_addrlen, ==, sizeof(struct sockaddr_in6));
1106 sin6 = (struct sockaddr_in6*)ai->ai_addr;
1107 tt_int_op(sin6->sin6_port, ==, htons(80));
1109 cp = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf,
1111 TT_BLATHER(("ipv6.google.com resolved to %s",
1112 cp?cp:"<unwriteable>"));
1117 evutil_freeaddrinfo(ai);
1122 test_evutil_loadsyslib(void *arg)
1126 h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
1136 /** Test mm_malloc(). */
1138 test_event_malloc(void *arg)
1143 /* mm_malloc(0) should simply return NULL. */
1144 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1147 tt_assert(p == NULL);
1148 tt_int_op(errno, ==, 0);
1154 tt_assert(p != NULL);
1155 tt_int_op(errno, ==, 0);
1164 test_event_calloc(void *arg)
1169 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1170 /* mm_calloc() should simply return NULL
1171 * if either argument is zero. */
1173 p = mm_calloc(0, 0);
1174 tt_assert(p == NULL);
1175 tt_int_op(errno, ==, 0);
1177 p = mm_calloc(0, 1);
1178 tt_assert(p == NULL);
1179 tt_int_op(errno, ==, 0);
1181 p = mm_calloc(1, 0);
1182 tt_assert(p == NULL);
1183 tt_int_op(errno, ==, 0);
1188 p = mm_calloc(8, 8);
1189 tt_assert(p != NULL);
1190 tt_int_op(errno, ==, 0);
1194 /* mm_calloc() should set errno = ENOMEM and return NULL
1195 * in case of potential overflow. */
1197 p = mm_calloc(EV_SIZE_MAX/2, EV_SIZE_MAX/2 + 8);
1198 tt_assert(p == NULL);
1199 tt_int_op(errno, ==, ENOMEM);
1210 test_event_strdup(void *arg)
1215 #ifndef EVENT__DISABLE_MM_REPLACEMENT
1216 /* mm_strdup(NULL) should set errno = EINVAL and return NULL. */
1218 p = mm_strdup(NULL);
1219 tt_assert(p == NULL);
1220 tt_int_op(errno, ==, EINVAL);
1223 /* Trivial cases. */
1227 tt_assert(p != NULL);
1228 tt_int_op(errno, ==, 0);
1229 tt_str_op(p, ==, "");
1233 p = mm_strdup("foo");
1234 tt_assert(p != NULL);
1235 tt_int_op(errno, ==, 0);
1236 tt_str_op(p, ==, "foo");
1240 * mm_strdup(str) where str is a string of length EV_SIZE_MAX
1241 * should set errno = ENOMEM and return NULL. */
1249 test_evutil_usleep(void *arg)
1251 struct timeval tv1, tv2, tv3, diff1, diff2;
1252 const struct timeval quarter_sec = {0, 250*1000};
1253 const struct timeval tenth_sec = {0, 100*1000};
1256 evutil_gettimeofday(&tv1, NULL);
1257 evutil_usleep_(&quarter_sec);
1258 evutil_gettimeofday(&tv2, NULL);
1259 evutil_usleep_(&tenth_sec);
1260 evutil_gettimeofday(&tv3, NULL);
1262 evutil_timersub(&tv2, &tv1, &diff1);
1263 evutil_timersub(&tv3, &tv2, &diff2);
1264 usec1 = diff1.tv_sec * 1000000 + diff1.tv_usec;
1265 usec2 = diff2.tv_sec * 1000000 + diff2.tv_usec;
1267 tt_int_op(usec1, >, 200000);
1268 tt_int_op(usec1, <, 300000);
1269 tt_int_op(usec2, >, 80000);
1270 tt_int_op(usec2, <, 120000);
1277 test_evutil_monotonic_res(void *data_)
1279 /* Basic santity-test for monotonic timers. What we'd really like
1280 * to do is make sure that they can't go backwards even when the
1281 * system clock goes backwards. But we haven't got a good way to
1282 * move the system clock backwards.
1284 struct basic_test_data *data = data_;
1285 struct evutil_monotonic_timer timer;
1286 const int precise = strstr(data->setup_data, "precise") != NULL;
1287 const int fallback = strstr(data->setup_data, "fallback") != NULL;
1288 struct timeval tv[10], delay;
1291 int flags = 0, wantres, acceptdiff, i;
1293 flags |= EV_MONOT_PRECISE;
1295 flags |= EV_MONOT_FALLBACK;
1296 if (precise || fallback) {
1306 acceptdiff = 20*1000;
1309 TT_BLATHER(("Precise = %d", precise));
1310 TT_BLATHER(("Fallback = %d", fallback));
1312 /* First, make sure we match up with usleep. */
1315 delay.tv_usec = wantres;
1317 tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1319 for (i = 0; i < 10; ++i) {
1320 evutil_gettime_monotonic_(&timer, &tv[i]);
1321 evutil_usleep_(&delay);
1324 for (i = 0; i < 9; ++i) {
1325 struct timeval diff;
1326 tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1327 evutil_timersub(&tv[i+1], &tv[i], &diff);
1328 tt_int_op(diff.tv_sec, ==, 0);
1329 total_diff += diff.tv_usec;
1330 TT_BLATHER(("Difference = %d", (int)diff.tv_usec));
1332 tt_int_op(abs(total_diff/9 - wantres), <, acceptdiff);
1339 test_evutil_monotonic_prc(void *data_)
1341 struct basic_test_data *data = data_;
1342 struct evutil_monotonic_timer timer;
1343 const int precise = strstr(data->setup_data, "precise") != NULL;
1344 const int fallback = strstr(data->setup_data, "fallback") != NULL;
1345 struct timeval tv[10];
1347 int i, maxstep = 25*1000,flags=0;
1351 flags |= EV_MONOT_PRECISE;
1353 flags |= EV_MONOT_FALLBACK;
1354 tt_int_op(evutil_configure_monotonic_time_(&timer, flags), ==, 0);
1356 /* find out what precision we actually see. */
1358 evutil_gettime_monotonic_(&timer, &tv[0]);
1359 for (i = 1; i < 10; ++i) {
1361 evutil_gettime_monotonic_(&timer, &tv[i]);
1362 } while (evutil_timercmp(&tv[i-1], &tv[i], ==));
1366 for (i = 0; i < 9; ++i) {
1367 struct timeval diff;
1368 tt_assert(evutil_timercmp(&tv[i], &tv[i+1], <));
1369 evutil_timersub(&tv[i+1], &tv[i], &diff);
1370 tt_int_op(diff.tv_sec, ==, 0);
1371 total_diff += diff.tv_usec;
1372 TT_BLATHER(("Step difference = %d", (int)diff.tv_usec));
1374 TT_BLATHER(("Average step difference = %d", total_diff / 9));
1375 tt_int_op(total_diff/9, <, maxstep);
1382 create_tm_from_unix_epoch(struct tm *cur_p, const time_t t)
1385 struct tm *tmp = gmtime(&t);
1387 fprintf(stderr, "gmtime: %s (%i)", strerror(errno), (int)t);
1392 gmtime_r(&t, cur_p);
1396 static struct date_rfc1123_case {
1399 } date_rfc1123_cases[] = {
1400 { 0, "Thu, 01 Jan 1970 00:00:00 GMT"} /* UNIX time of zero */,
1401 { 946684799, "Fri, 31 Dec 1999 23:59:59 GMT"} /* the last moment of the 20th century */,
1402 { 946684800, "Sat, 01 Jan 2000 00:00:00 GMT"} /* the first moment of the 21st century */,
1403 { 981072000, "Fri, 02 Feb 2001 00:00:00 GMT"},
1404 { 1015113600, "Sun, 03 Mar 2002 00:00:00 GMT"},
1405 { 1049414400, "Fri, 04 Apr 2003 00:00:00 GMT"},
1406 { 1083715200, "Wed, 05 May 2004 00:00:00 GMT"},
1407 { 1118016000, "Mon, 06 Jun 2005 00:00:00 GMT"},
1408 { 1152230400, "Fri, 07 Jul 2006 00:00:00 GMT"},
1409 { 1186531200, "Wed, 08 Aug 2007 00:00:00 GMT"},
1410 { 1220918400, "Tue, 09 Sep 2008 00:00:00 GMT"},
1411 { 1255132800, "Sat, 10 Oct 2009 00:00:00 GMT"},
1412 { 1289433600, "Thu, 11 Nov 2010 00:00:00 GMT"},
1413 { 1323648000, "Mon, 12 Dec 2011 00:00:00 GMT"},
1415 /** In win32 case we have max "23:59:59 January 18, 2038, UTC" for time32 */
1416 { 4294967296, "Sun, 07 Feb 2106 06:28:16 GMT"} /* 2^32 */,
1417 /** In win32 case we have max "23:59:59, December 31, 3000, UTC" for time64 */
1418 {253402300799, "Fri, 31 Dec 9999 23:59:59 GMT"} /* long long future no one can imagine */,
1419 { 1456704000, "Mon, 29 Feb 2016 00:00:00 GMT"} /* leap year */,
1421 { 1435708800, "Wed, 01 Jul 2015 00:00:00 GMT"} /* leap second */,
1422 { 1481866376, "Fri, 16 Dec 2016 05:32:56 GMT"} /* the time this test case is generated */,
1423 {0, ""} /* end of test cases. */
1427 test_evutil_date_rfc1123(void *arg)
1433 /* Checks if too small buffers are safely accepted. */
1435 create_tm_from_unix_epoch(&query, 0);
1436 evutil_date_rfc1123(result, 8, &query);
1437 tt_str_op(result, ==, "Thu, 01");
1440 /* Checks for testcases. */
1441 for (i = 0; ; i++) {
1442 struct date_rfc1123_case c = date_rfc1123_cases[i];
1444 if (strlen(c.date) == 0)
1447 create_tm_from_unix_epoch(&query, c.t);
1448 evutil_date_rfc1123(result, sizeof(result), &query);
1449 tt_str_op(result, ==, c.date);
1456 struct testcase_t util_testcases[] = {
1457 { "ipv4_parse", regress_ipv4_parse, 0, NULL, NULL },
1458 { "ipv6_parse", regress_ipv6_parse, 0, NULL, NULL },
1459 { "sockaddr_port_parse", regress_sockaddr_port_parse, 0, NULL, NULL },
1460 { "sockaddr_port_format", regress_sockaddr_port_format, 0, NULL, NULL },
1461 { "sockaddr_predicates", test_evutil_sockaddr_predicates, 0,NULL,NULL },
1462 { "evutil_snprintf", test_evutil_snprintf, 0, NULL, NULL },
1463 { "evutil_strtoll", test_evutil_strtoll, 0, NULL, NULL },
1464 { "evutil_casecmp", test_evutil_casecmp, 0, NULL, NULL },
1465 { "evutil_rtrim", test_evutil_rtrim, 0, NULL, NULL },
1466 { "strlcpy", test_evutil_strlcpy, 0, NULL, NULL },
1467 { "log", test_evutil_log, TT_FORK, NULL, NULL },
1468 { "upcast", test_evutil_upcast, 0, NULL, NULL },
1469 { "integers", test_evutil_integers, 0, NULL, NULL },
1470 { "rand", test_evutil_rand, TT_FORK, NULL, NULL },
1471 { "getaddrinfo", test_evutil_getaddrinfo, TT_FORK, NULL, NULL },
1472 { "getaddrinfo_live", test_evutil_getaddrinfo_live, TT_FORK|TT_OFF_BY_DEFAULT, NULL, NULL },
1474 { "loadsyslib", test_evutil_loadsyslib, TT_FORK, NULL, NULL },
1476 { "mm_malloc", test_event_malloc, 0, NULL, NULL },
1477 { "mm_calloc", test_event_calloc, 0, NULL, NULL },
1478 { "mm_strdup", test_event_strdup, 0, NULL, NULL },
1479 { "usleep", test_evutil_usleep, 0, NULL, NULL },
1480 { "monotonic_res", test_evutil_monotonic_res, 0, &basic_setup, (void*)"" },
1481 { "monotonic_res_precise", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"precise" },
1482 { "monotonic_res_fallback", test_evutil_monotonic_res, TT_OFF_BY_DEFAULT, &basic_setup, (void*)"fallback" },
1483 { "monotonic_prc", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"" },
1484 { "monotonic_prc_precise", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"precise" },
1485 { "monotonic_prc_fallback", test_evutil_monotonic_prc, 0, &basic_setup, (void*)"fallback" },
1486 { "date_rfc1123", test_evutil_date_rfc1123, 0, NULL, NULL },