[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] [tor/master] Split general util and address tests into their own files.
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date: Tue, 22 Sep 2009 13:29:55 -0400
Subject: Split general util and address tests into their own files.
Commit: d9d0813809e8fac42d2e7604342e606273d91dc1
---
src/test/Makefile.am | 9 +-
src/test/test.c | 1473 +-------------------------------------------------
src/test/test_addr.c | 440 +++++++++++++++
src/test/test_util.c | 1062 ++++++++++++++++++++++++++++++++++++
4 files changed, 1514 insertions(+), 1470 deletions(-)
create mode 100644 src/test/test_addr.c
create mode 100644 src/test/test_util.c
diff --git a/src/test/Makefile.am b/src/test/Makefile.am
index e31ab70..d907f55 100644
--- a/src/test/Makefile.am
+++ b/src/test/Makefile.am
@@ -12,7 +12,14 @@ AM_CFLAGS = -I../or
# This seems to matter nowhere but on windows, but I assure you that it
# matters a lot there, and is quite hard to debug if you forget to do it.
-test_SOURCES = test_data.c test.c test_crypto.c test_containers.c tinytest.c
+test_SOURCES = \
+ test_data.c \
+ test.c \
+ test_addr.c \
+ test_crypto.c \
+ test_containers.c \
+ test_util.c \
+ tinytest.c
test_LDFLAGS = @TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ \
@TOR_LDFLAGS_libevent@
diff --git a/src/test/test.c b/src/test/test.c
index ff15c46..41402fd 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -30,11 +30,9 @@ const char tor_git_revision[] = "";
* are typically file-private. */
#define BUFFERS_PRIVATE
#define CONFIG_PRIVATE
-#define CONTROL_PRIVATE
#define DIRSERV_PRIVATE
#define DIRVOTE_PRIVATE
#define GEOIP_PRIVATE
-#define MEMPOOL_PRIVATE
#define ROUTER_PRIVATE
#define CIRCUIT_PRIVATE
@@ -408,1242 +406,6 @@ test_buffers(void)
buf_free(buf2);
}
-/** Test basic utility functionality. */
-static void
-test_util(void)
-{
- struct timeval start, end;
- struct tm a_time;
- char timestr[RFC1123_TIME_LEN+1];
- char buf[1024];
- time_t t_res;
- int i;
- uint32_t u32;
- uint16_t u16;
- char *cp, *k, *v;
- const char *str;
-
- start.tv_sec = 5;
- start.tv_usec = 5000;
-
- end.tv_sec = 5;
- end.tv_usec = 5000;
-
- test_eq(0L, tv_udiff(&start, &end));
-
- end.tv_usec = 7000;
-
- test_eq(2000L, tv_udiff(&start, &end));
-
- end.tv_sec = 6;
-
- test_eq(1002000L, tv_udiff(&start, &end));
-
- end.tv_usec = 0;
-
- test_eq(995000L, tv_udiff(&start, &end));
-
- end.tv_sec = 4;
-
- test_eq(-1005000L, tv_udiff(&start, &end));
-
- end.tv_usec = 999990;
- start.tv_sec = 1;
- start.tv_usec = 500;
-
- /* The test values here are confirmed to be correct on a platform
- * with a working timegm. */
- a_time.tm_year = 2003-1900;
- a_time.tm_mon = 7;
- a_time.tm_mday = 30;
- a_time.tm_hour = 6;
- a_time.tm_min = 14;
- a_time.tm_sec = 55;
- test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
- a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
- test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
- a_time.tm_mon = 1; /* Try a leap year, in feb. */
- a_time.tm_mday = 10;
- test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
-
- format_rfc1123_time(timestr, 0);
- test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
- format_rfc1123_time(timestr, (time_t)1091580502UL);
- test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
-
- t_res = 0;
- i = parse_rfc1123_time(timestr, &t_res);
- test_eq(i,0);
- test_eq(t_res, (time_t)1091580502UL);
- test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
- tor_gettimeofday(&start);
-
- /* Tests for corner cases of strl operations */
- test_eq(5, strlcpy(buf, "Hello", 0));
- strlcpy(buf, "Hello", sizeof(buf));
- test_eq(10, strlcat(buf, "Hello", 5));
-
- /* Test tor_strstrip() */
- strlcpy(buf, "Testing 1 2 3", sizeof(buf));
- tor_strstrip(buf, ",!");
- test_streq(buf, "Testing 1 2 3");
- strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
- tor_strstrip(buf, "!? ");
- test_streq(buf, "Testing123");
-
- /* Test parse_addr_port */
- cp = NULL; u32 = 3; u16 = 3;
- test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
- test_streq(cp, "1.2.3.4");
- test_eq(u32, 0x01020304u);
- test_eq(u16, 0);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
- test_streq(cp, "4.3.2.1");
- test_eq(u32, 0x04030201u);
- test_eq(u16, 99);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
- &cp, NULL, &u16));
- test_streq(cp, "nonexistent.address");
- test_eq(u16, 4040);
- tor_free(cp);
- test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
- test_streq(cp, "localhost");
- test_eq(u32, 0x7f000001u);
- test_eq(u16, 9999);
- tor_free(cp);
- u32 = 3;
- test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
- test_eq(cp, NULL);
- test_eq(u32, 0x7f000001u);
- test_eq(u16, 0);
- tor_free(cp);
- test_eq(0, addr_mask_get_bits(0x0u));
- test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
- test_eq(16, addr_mask_get_bits(0xFFFF0000u));
- test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
- test_eq(1, addr_mask_get_bits(0x80000000u));
-
- /* Test tor_parse_long. */
- test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
- test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
- test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
-
- /* Test tor_parse_ulong */
- test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
- test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
-
- /* Test tor_parse_uint64. */
- test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
- test_assert(i == 1);
- test_streq(cp, " x");
- test_assert(U64_LITERAL(12345678901) ==
- tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
- test_assert(i == 1);
- test_streq(cp, "");
- test_assert(U64_LITERAL(0) ==
- tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
- test_assert(i == 0);
-
- {
- /* Test tor_parse_double. */
- double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
- test_assert(i == 1);
- test_assert(DBL_TO_U64(d) == 10);
- d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
- test_assert(i == 1);
- test_assert(DBL_TO_U64(d) == 0);
- d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
- test_assert(i == 0);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
- test_assert(i == 0);
- d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
- test_assert(i == 1);
- d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
- test_assert(i == 1);
- }
-
- /* Test failing snprintf cases */
- test_eq(-1, tor_snprintf(buf, 0, "Foo"));
- test_eq(-1, tor_snprintf(buf, 2, "Foo"));
-
- /* Test printf with uint64 */
- tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
- U64_PRINTF_ARG(U64_LITERAL(12345678901)));
- test_streq(buf, "x!12345678901!x");
-
- /* Test parse_config_line_from_str */
- strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
- "k2\n"
- "k3 \n" "\n" " \n" "#comment\n"
- "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
- "kseven \"a quoted 'string\"\n"
- "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
- , sizeof(buf));
- str = buf;
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k");
- test_streq(v, "v");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "key value with"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "key");
- test_streq(v, "value with spaces");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "keykey"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "keykey");
- test_streq(v, "val");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "k2\n"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k2");
- test_streq(v, "");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "k3 \n"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k3");
- test_streq(v, "");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "#comment"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k4");
- test_streq(v, "");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "k5#abc"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k5");
- test_streq(v, "");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "k6"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k6");
- test_streq(v, "val");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "kseven"));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "kseven");
- test_streq(v, "a quoted \'string");
- tor_free(k); tor_free(v);
- test_assert(!strcmpstart(str, "k8 "));
-
- str = parse_config_line_from_str(str, &k, &v);
- test_streq(k, "k8");
- test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
- tor_free(k); tor_free(v);
- test_streq(str, "");
-
- /* Test for strcmpstart and strcmpend. */
- test_assert(strcmpstart("abcdef", "abcdef")==0);
- test_assert(strcmpstart("abcdef", "abc")==0);
- test_assert(strcmpstart("abcdef", "abd")<0);
- test_assert(strcmpstart("abcdef", "abb")>0);
- test_assert(strcmpstart("ab", "abb")<0);
-
- test_assert(strcmpend("abcdef", "abcdef")==0);
- test_assert(strcmpend("abcdef", "def")==0);
- test_assert(strcmpend("abcdef", "deg")<0);
- test_assert(strcmpend("abcdef", "dee")>0);
- test_assert(strcmpend("ab", "abb")<0);
-
- test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
- test_assert(strcasecmpend("abcdef", "dEF")==0);
- test_assert(strcasecmpend("abcDEf", "deg")<0);
- test_assert(strcasecmpend("abcdef", "DEE")>0);
- test_assert(strcasecmpend("ab", "abB")<0);
-
- /* Test mem_is_zero */
- memset(buf,0,128);
- buf[128] = 'x';
- test_assert(tor_digest_is_zero(buf));
- test_assert(tor_mem_is_zero(buf, 10));
- test_assert(tor_mem_is_zero(buf, 20));
- test_assert(tor_mem_is_zero(buf, 128));
- test_assert(!tor_mem_is_zero(buf, 129));
- buf[60] = (char)255;
- test_assert(!tor_mem_is_zero(buf, 128));
- buf[0] = (char)1;
- test_assert(!tor_mem_is_zero(buf, 10));
-
- /* Test inet_ntop */
- {
- char tmpbuf[TOR_ADDR_BUF_LEN];
- const char *ip = "176.192.208.224";
- struct in_addr in;
- tor_inet_pton(AF_INET, ip, &in);
- tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf));
- test_streq(tmpbuf, ip);
- }
-
- /* Test 'escaped' */
- test_streq("\"\"", escaped(""));
- test_streq("\"abcd\"", escaped("abcd"));
- test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
- test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
- test_assert(NULL == escaped(NULL));
-
- /* Test strndup and memdup */
- {
- const char *s = "abcdefghijklmnopqrstuvwxyz";
- cp = tor_strndup(s, 30);
- test_streq(cp, s); /* same string, */
- test_neq(cp, s); /* but different pointers. */
- tor_free(cp);
-
- cp = tor_strndup(s, 5);
- test_streq(cp, "abcde");
- tor_free(cp);
-
- s = "a\0b\0c\0d\0e\0";
- cp = tor_memdup(s,10);
- test_memeq(cp, s, 10); /* same ram, */
- test_neq(cp, s); /* but different pointers. */
- tor_free(cp);
- }
-
- /* Test str-foo functions */
- cp = tor_strdup("abcdef");
- test_assert(tor_strisnonupper(cp));
- cp[3] = 'D';
- test_assert(!tor_strisnonupper(cp));
- tor_strupper(cp);
- test_streq(cp, "ABCDEF");
- test_assert(tor_strisprint(cp));
- cp[3] = 3;
- test_assert(!tor_strisprint(cp));
- tor_free(cp);
-
- /* Test eat_whitespace. */
- {
- const char *s = " \n a";
- test_eq_ptr(eat_whitespace(s), s+4);
- s = "abcd";
- test_eq_ptr(eat_whitespace(s), s);
- s = "#xyz\nab";
- test_eq_ptr(eat_whitespace(s), s+5);
- }
-
- /* Test memmem and memstr */
- {
- const char *haystack = "abcde";
- tor_assert(!tor_memmem(haystack, 5, "ef", 2));
- test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
- test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
- haystack = "ababcad";
- test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
- test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
- test_assert(!tor_memstr(haystack, 7, "fe"));
- test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
- }
-
- /* Test wrap_string */
- {
- smartlist_t *sl = smartlist_create();
- wrap_string(sl, "This is a test of string wrapping functionality: woot.",
- 10, "", "");
- cp = smartlist_join_strings(sl, "", 0, NULL);
- test_streq(cp,
- "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
- tor_free(cp);
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
- smartlist_clear(sl);
-
- wrap_string(sl, "This is a test of string wrapping functionality: woot.",
- 16, "### ", "# ");
- cp = smartlist_join_strings(sl, "", 0, NULL);
- test_streq(cp,
- "### This is a\n# test of string\n# wrapping\n# functionality:\n"
- "# woot.\n");
-
- tor_free(cp);
- SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
- smartlist_free(sl);
- }
-
- /* now make sure time works. */
- tor_gettimeofday(&end);
- /* We might've timewarped a little. */
- test_assert(tv_udiff(&start, &end) >= -5000);
-
- /* Test tor_log2(). */
- test_eq(tor_log2(64), 6);
- test_eq(tor_log2(65), 6);
- test_eq(tor_log2(63), 5);
- test_eq(tor_log2(1), 0);
- test_eq(tor_log2(2), 1);
- test_eq(tor_log2(3), 1);
- test_eq(tor_log2(4), 2);
- test_eq(tor_log2(5), 2);
- test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
- test_eq(tor_log2(UINT64_MAX), 63);
-
- /* Test round_to_power_of_2 */
- test_eq(round_to_power_of_2(120), 128);
- test_eq(round_to_power_of_2(128), 128);
- test_eq(round_to_power_of_2(130), 128);
- test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
- U64_LITERAL(1)<<55);
- test_eq(round_to_power_of_2(0), 2);
-
- done:
- ;
-}
-
-#define _test_op_ip6(a,op,b,e1,e2) \
- STMT_BEGIN \
- tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
- (memcmp(_val1->s6_addr, _val2->s6_addr, 16) op 0), \
- char *, "%s", \
- { int i; char *cp; \
- cp = _print = tor_malloc(64); \
- for (i=0;i<16;++i) { \
- tor_snprintf(cp, 3,"%02x", (unsigned)_value->s6_addr[i]);\
- cp += 2; \
- if (i != 15) *cp++ = ':'; \
- } \
- }, { tor_free(_print); } \
- ); \
- STMT_END
-
-/** Helper: Assert that two strings both decode as IPv6 addresses with
- * tor_inet_pton(), and both decode to the same address. */
-#define test_pton6_same(a,b) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
- test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
- _test_op_ip6(&a1,==,&a2,#a,#b); \
- STMT_END
-
-/** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
- * tor_inet_pton(). */
-#define test_pton6_bad(a) \
- test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
-
-/** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
- * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
- * the same value as <b>a</b>. */
-#define test_ntop6_reduces(a,b) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
- test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
- test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
- _test_op_ip6(&a1, ==, &a2, a, b); \
- STMT_END
-
-/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
- * passes tor_addr_is_internal() with <b>for_listening</b>. */
-#define test_internal_ip(a,for_listening) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
- t1.family = AF_INET6; \
- if (!tor_addr_is_internal(&t1, for_listening)) \
- test_fail_msg( a "was not internal."); \
- STMT_END
-
-/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
- * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
-#define test_external_ip(a,for_listening) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
- t1.family = AF_INET6; \
- if (tor_addr_is_internal(&t1, for_listening)) \
- test_fail_msg(a "was not external."); \
- STMT_END
-
-/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
- * tor_inet_pton(), give addresses that compare in the order defined by
- * <b>op</b> with tor_addr_compare(). */
-#define test_addr_compare(a, op, b) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
- test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
- t1.family = t2.family = AF_INET6; \
- r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
- if (!(r op 0)) \
- test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
- STMT_END
-
-/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
- * tor_inet_pton(), give addresses that compare in the order defined by
- * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
-#define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
- test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
- test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
- t1.family = t2.family = AF_INET6; \
- r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
- if (!(r op 0)) \
- test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
- STMT_END
-
-/** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
- * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
- * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
- * as <b>pt1..pt2</b>. */
-#define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
- STMT_BEGIN \
- test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f); \
- p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
- test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
- test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
- test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
- test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
- test_eq(mask, mm); \
- test_eq(port1, pt1); \
- test_eq(port2, pt2); \
- STMT_END
-
-/** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
-static void
-test_util_ip6_helpers(void)
-{
- char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
- struct in6_addr a1, a2;
- tor_addr_t t1, t2;
- int r, i;
- uint16_t port1, port2;
- maskbits_t mask;
- const char *p1;
- struct sockaddr_storage sa_storage;
- struct sockaddr_in *sin;
- struct sockaddr_in6 *sin6;
-
- // struct in_addr b1, b2;
- /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
-
- /* ==== Converting to and from sockaddr_t. */
- sin = (struct sockaddr_in *)&sa_storage;
- sin->sin_family = AF_INET;
- sin->sin_port = 9090;
- sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
- tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
- test_eq(tor_addr_family(&t1), AF_INET);
- test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
-
- memset(&sa_storage, 0, sizeof(sa_storage));
- test_eq(sizeof(struct sockaddr_in),
- tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
- sizeof(sa_storage)));
- test_eq(1234, ntohs(sin->sin_port));
- test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
-
- memset(&sa_storage, 0, sizeof(sa_storage));
- sin6 = (struct sockaddr_in6 *)&sa_storage;
- sin6->sin6_family = AF_INET6;
- sin6->sin6_port = htons(7070);
- sin6->sin6_addr.s6_addr[0] = 128;
- tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
- test_eq(tor_addr_family(&t1), AF_INET6);
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
- test_streq(p1, "8000::");
-
- memset(&sa_storage, 0, sizeof(sa_storage));
- test_eq(sizeof(struct sockaddr_in6),
- tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
- sizeof(sa_storage)));
- test_eq(AF_INET6, sin6->sin6_family);
- test_eq(9999, ntohs(sin6->sin6_port));
- test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
-
- /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
- * have a good resolver. */
- test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
- test_eq(AF_INET, tor_addr_family(&t1));
- test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
-
- test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
- test_eq(AF_INET6, tor_addr_family(&t1));
- test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
- test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
- test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
-
- /* === Test pton: valid af_inet6 */
- /* Simple, valid parsing. */
- r = tor_inet_pton(AF_INET6,
- "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
- test_assert(r==1);
- for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
- /* ipv4 ending. */
- test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
- "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
- /* shortened words. */
- test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
- "1:99:BEEF:0:0123:FFFF:1:1");
- /* zeros at the beginning */
- test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
- "::9:c0a8:1:1");
- test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
- "::9:c0a8:0.1.0.1");
- /* zeros in the middle. */
- test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
- "fe80::202:1111:1:1");
- /* zeros at the end. */
- test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
- "1000:1:0:7::");
-
- /* === Test ntop: af_inet6 */
- test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
-
- test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
- "1:99:beef:6:123:ffff:1:1");
-
- //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
- test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
- test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
- test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
- test_ntop6_reduces("008:0::0", "8::");
- test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
- test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
- test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
- "::9:c0a8:1:1");
- test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
- "fe80::202:1111:1:1");
- test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
- "1000:1:0:7::");
-
- /* === Test pton: invalid in6. */
- test_pton6_bad("foobar.");
- test_pton6_bad("55555::");
- test_pton6_bad("9:-60::");
- test_pton6_bad("1:2:33333:4:0002:3::");
- //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
- test_pton6_bad("1:2:3333:4:fish:3::");
- test_pton6_bad("1:2:3:4:5:6:7:8:9");
- test_pton6_bad("1:2:3:4:5:6:7");
- test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
- test_pton6_bad("1:2:3:4:5:6:1.2.3");
- test_pton6_bad("::1.2.3");
- test_pton6_bad("::1.2.3.4.5");
- test_pton6_bad("99");
- test_pton6_bad("");
- test_pton6_bad("1::2::3:4");
- test_pton6_bad("a:::b:c");
- test_pton6_bad(":::a:b:c");
- test_pton6_bad("a:b:c:::");
-
- /* test internal checking */
- test_external_ip("fbff:ffff::2:7", 0);
- test_internal_ip("fc01::2:7", 0);
- test_internal_ip("fdff:ffff::f:f", 0);
- test_external_ip("fe00::3:f", 0);
-
- test_external_ip("fe7f:ffff::2:7", 0);
- test_internal_ip("fe80::2:7", 0);
- test_internal_ip("febf:ffff::f:f", 0);
-
- test_internal_ip("fec0::2:7:7", 0);
- test_internal_ip("feff:ffff::e:7:7", 0);
- test_external_ip("ff00::e:7:7", 0);
-
- test_internal_ip("::", 0);
- test_internal_ip("::1", 0);
- test_internal_ip("::1", 1);
- test_internal_ip("::", 0);
- test_external_ip("::", 1);
- test_external_ip("::2", 0);
- test_external_ip("2001::", 0);
- test_external_ip("ffff::", 0);
-
- test_external_ip("::ffff:0.0.0.0", 1);
- test_internal_ip("::ffff:0.0.0.0", 0);
- test_internal_ip("::ffff:0.255.255.255", 0);
- test_external_ip("::ffff:1.0.0.0", 0);
-
- test_external_ip("::ffff:9.255.255.255", 0);
- test_internal_ip("::ffff:10.0.0.0", 0);
- test_internal_ip("::ffff:10.255.255.255", 0);
- test_external_ip("::ffff:11.0.0.0", 0);
-
- test_external_ip("::ffff:126.255.255.255", 0);
- test_internal_ip("::ffff:127.0.0.0", 0);
- test_internal_ip("::ffff:127.255.255.255", 0);
- test_external_ip("::ffff:128.0.0.0", 0);
-
- test_external_ip("::ffff:172.15.255.255", 0);
- test_internal_ip("::ffff:172.16.0.0", 0);
- test_internal_ip("::ffff:172.31.255.255", 0);
- test_external_ip("::ffff:172.32.0.0", 0);
-
- test_external_ip("::ffff:192.167.255.255", 0);
- test_internal_ip("::ffff:192.168.0.0", 0);
- test_internal_ip("::ffff:192.168.255.255", 0);
- test_external_ip("::ffff:192.169.0.0", 0);
-
- test_external_ip("::ffff:169.253.255.255", 0);
- test_internal_ip("::ffff:169.254.0.0", 0);
- test_internal_ip("::ffff:169.254.255.255", 0);
- test_external_ip("::ffff:169.255.0.0", 0);
- test_assert(is_internal_IP(0x7f000001, 0));
-
- /* tor_addr_compare(tor_addr_t x2) */
- test_addr_compare("ffff::", ==, "ffff::0");
- test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
- test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
- test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
- test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
- tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1, NULL, NULL, NULL);
- tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
- test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
- tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1, NULL, NULL, NULL);
- tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
- test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
-
- /* test compare_masked */
- test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
- test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
- test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
- test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
-
- /* Test decorated addr_to_string. */
- test_eq(AF_INET6, tor_addr_from_str(&t1, "[123:45:6789::5005:11]"));
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
- test_streq(p1, "[123:45:6789::5005:11]");
- test_eq(AF_INET, tor_addr_from_str(&t1, "18.0.0.1"));
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
- test_streq(p1, "18.0.0.1");
-
- /* Test tor_addr_parse_reverse_lookup_name */
- i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
- test_eq(0, i);
- i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
- test_eq(0, i);
- i = tor_addr_parse_reverse_lookup_name(&t1, "1.0.168.192.in-addr.arpa",
- AF_UNSPEC, 1);
- test_eq(1, i);
- test_eq(tor_addr_family(&t1), AF_INET);
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
- test_streq(p1, "192.168.0.1");
- i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
- test_eq(0, i);
- i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
- test_eq(1, i);
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
- test_streq(p1, "192.168.0.99");
- memset(&t1, 0, sizeof(t1));
- i = tor_addr_parse_reverse_lookup_name(&t1,
- "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
- "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
- "ip6.ARPA",
- AF_UNSPEC, 0);
- test_eq(1, i);
- p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
- test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
- /* Failing cases. */
- i = tor_addr_parse_reverse_lookup_name(&t1,
- "6.7.8.9.a.b.c.d.e.f."
- "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
- "ip6.ARPA",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1,
- "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
- "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
- "ip6.ARPA",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1,
- "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
- "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
- "ip6.ARPA",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1, "32.1.1.in-addr.arpa",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1, ".in-addr.arpa",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
- AF_UNSPEC, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
- AF_INET6, 0);
- test_eq(i, -1);
- i = tor_addr_parse_reverse_lookup_name(&t1,
- "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
- "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
- "ip6.ARPA",
- AF_INET, 0);
- test_eq(i, -1);
-
- /* test tor_addr_parse_mask_ports */
- test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
- 0, 0, 0, 0x0000000f, 17, 47, 95);
- //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
- //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
- test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
- 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
- test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
- 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
-
- r=tor_addr_parse_mask_ports("[fefef::]/112", &t1, NULL, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("efef::/112", &t1, NULL, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1, NULL, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
- test_assert(r == -1);
- /* Test for V4-mapped address with mask < 96. (arguably not valid) */
- r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1, &mask, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("1.1.2.2/33", &t1, &mask, NULL, NULL);
- test_assert(r == -1);
- r=tor_addr_parse_mask_ports("1.1.2.2/31", &t1, &mask, NULL, NULL);
- test_assert(r == AF_INET);
- r=tor_addr_parse_mask_ports("[efef::]/112", &t1, &mask, &port1, &port2);
- test_assert(r == AF_INET6);
- test_assert(port1 == 1);
- test_assert(port2 == 65535);
-
- /* make sure inet address lengths >= max */
- test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
- test_assert(TOR_ADDR_BUF_LEN >=
- sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
-
- test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
-
- /* get interface addresses */
- r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
- i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
-#if 0
- tor_inet_ntop(AF_INET, &t1.sa.sin_addr, buf, sizeof(buf));
- printf("\nv4 address: %s (family=%i)", buf, IN_FAMILY(&t1));
- tor_inet_ntop(AF_INET6, &t2.sa6.sin6_addr, buf, sizeof(buf));
- printf("\nv6 address: %s (family=%i)", buf, IN_FAMILY(&t2));
-#endif
-
- done:
- ;
-}
-
-/** mutex for thread test to stop the threads hitting data at the same time. */
-static tor_mutex_t *_thread_test_mutex = NULL;
-/** mutexes for the thread test to make sure that the threads have to
- * interleave somewhat. */
-static tor_mutex_t *_thread_test_start1 = NULL,
- *_thread_test_start2 = NULL;
-/** Shared strmap for the thread test. */
-static strmap_t *_thread_test_strmap = NULL;
-/** The name of thread1 for the thread test */
-static char *_thread1_name = NULL;
-/** The name of thread2 for the thread test */
-static char *_thread2_name = NULL;
-
-static void _thread_test_func(void* _s) ATTR_NORETURN;
-
-/** How many iterations have the threads in the unit test run? */
-static int t1_count = 0, t2_count = 0;
-
-/** Helper function for threading unit tests: This function runs in a
- * subthread. It grabs its own mutex (start1 or start2) to make sure that it
- * should start, then it repeatedly alters _test_thread_strmap protected by
- * _thread_test_mutex. */
-static void
-_thread_test_func(void* _s)
-{
- char *s = _s;
- int i, *count;
- tor_mutex_t *m;
- char buf[64];
- char **cp;
- if (!strcmp(s, "thread 1")) {
- m = _thread_test_start1;
- cp = &_thread1_name;
- count = &t1_count;
- } else {
- m = _thread_test_start2;
- cp = &_thread2_name;
- count = &t2_count;
- }
- tor_mutex_acquire(m);
-
- tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
- *cp = tor_strdup(buf);
-
- for (i=0; i<10000; ++i) {
- tor_mutex_acquire(_thread_test_mutex);
- strmap_set(_thread_test_strmap, "last to run", *cp);
- ++*count;
- tor_mutex_release(_thread_test_mutex);
- }
- tor_mutex_acquire(_thread_test_mutex);
- strmap_set(_thread_test_strmap, s, *cp);
- tor_mutex_release(_thread_test_mutex);
-
- tor_mutex_release(m);
-
- spawn_exit();
-}
-
-/** Run unit tests for threading logic. */
-static void
-test_util_threads(void)
-{
- char *s1 = NULL, *s2 = NULL;
- int done = 0, timedout = 0;
- time_t started;
-#ifndef TOR_IS_MULTITHREADED
- /* Skip this test if we aren't threading. We should be threading most
- * everywhere by now. */
- if (1)
- return;
-#endif
- _thread_test_mutex = tor_mutex_new();
- _thread_test_start1 = tor_mutex_new();
- _thread_test_start2 = tor_mutex_new();
- _thread_test_strmap = strmap_new();
- s1 = tor_strdup("thread 1");
- s2 = tor_strdup("thread 2");
- tor_mutex_acquire(_thread_test_start1);
- tor_mutex_acquire(_thread_test_start2);
- spawn_func(_thread_test_func, s1);
- spawn_func(_thread_test_func, s2);
- tor_mutex_release(_thread_test_start2);
- tor_mutex_release(_thread_test_start1);
- started = time(NULL);
- while (!done) {
- tor_mutex_acquire(_thread_test_mutex);
- strmap_assert_ok(_thread_test_strmap);
- if (strmap_get(_thread_test_strmap, "thread 1") &&
- strmap_get(_thread_test_strmap, "thread 2")) {
- done = 1;
- } else if (time(NULL) > started + 25) {
- timedout = done = 1;
- }
- tor_mutex_release(_thread_test_mutex);
- }
- tor_mutex_free(_thread_test_mutex);
-
- tor_mutex_acquire(_thread_test_start1);
- tor_mutex_release(_thread_test_start1);
- tor_mutex_acquire(_thread_test_start2);
- tor_mutex_release(_thread_test_start2);
-
- if (timedout) {
- printf("\nTimed out: %d %d", t1_count, t2_count);
- test_assert(strmap_get(_thread_test_strmap, "thread 1"));
- test_assert(strmap_get(_thread_test_strmap, "thread 2"));
- test_assert(!timedout);
- }
-
- /* different thread IDs. */
- test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
- strmap_get(_thread_test_strmap, "thread 2")));
- test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
- strmap_get(_thread_test_strmap, "last to run")) ||
- !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
- strmap_get(_thread_test_strmap, "last to run")));
-
- done:
- tor_free(s1);
- tor_free(s2);
- tor_free(_thread1_name);
- tor_free(_thread2_name);
- if (_thread_test_strmap)
- strmap_free(_thread_test_strmap, NULL);
- if (_thread_test_start1)
- tor_mutex_free(_thread_test_start1);
- if (_thread_test_start2)
- tor_mutex_free(_thread_test_start2);
-}
-
-/** Run unit tests for compression functions */
-static void
-test_util_gzip(void)
-{
- char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
- const char *ccp2;
- size_t len1, len2;
- tor_zlib_state_t *state = NULL;
-
- buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
- test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
- if (is_gzip_supported()) {
- test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- GZIP_METHOD));
- test_assert(buf2);
- test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
- test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
-
- test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- GZIP_METHOD, 1, LOG_INFO));
- test_assert(buf3);
- test_streq(buf1,buf3);
-
- tor_free(buf2);
- tor_free(buf3);
- }
-
- test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
- test_assert(buf2);
- test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
- test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
-
- test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
- ZLIB_METHOD, 1, LOG_INFO));
- test_assert(buf3);
- test_streq(buf1,buf3);
-
- /* Check whether we can uncompress concatenated, compressed strings. */
- tor_free(buf3);
- buf2 = tor_realloc(buf2, len1*2);
- memcpy(buf2+len1, buf2, len1);
- test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
- ZLIB_METHOD, 1, LOG_INFO));
- test_eq(len2, (strlen(buf1)+1)*2);
- test_memeq(buf3,
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
- "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
- (strlen(buf1)+1)*2);
-
- tor_free(buf1);
- tor_free(buf2);
- tor_free(buf3);
-
- /* Check whether we can uncompress partial strings. */
- buf1 =
- tor_strdup("String with low redundancy that won't be compressed much.");
- test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
- ZLIB_METHOD));
- tor_assert(len1>16);
- /* when we allow an incomplete string, we should succeed.*/
- tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 0, LOG_INFO));
- buf3[len2]='\0';
- tor_assert(len2 > 5);
- tor_assert(!strcmpstart(buf1, buf3));
-
- /* when we demand a complete string, this must fail. */
- tor_free(buf3);
- tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
- ZLIB_METHOD, 1, LOG_INFO));
- tor_assert(!buf3);
-
- /* Now, try streaming compression. */
- tor_free(buf1);
- tor_free(buf2);
- tor_free(buf3);
- state = tor_zlib_new(1, ZLIB_METHOD);
- tor_assert(state);
- cp1 = buf1 = tor_malloc(1024);
- len1 = 1024;
- ccp2 = "ABCDEFGHIJABCDEFGHIJ";
- len2 = 21;
- test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
- == TOR_ZLIB_OK);
- test_eq(len2, 0); /* Make sure we compressed it all. */
- test_assert(cp1 > buf1);
-
- len2 = 0;
- cp2 = cp1;
- test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
- == TOR_ZLIB_DONE);
- test_eq(len2, 0);
- test_assert(cp1 > cp2); /* Make sure we really added something. */
-
- tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
- ZLIB_METHOD, 1, LOG_WARN));
- test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
-
- done:
- if (state)
- tor_zlib_free(state);
- tor_free(buf2);
- tor_free(buf3);
- tor_free(buf1);
-}
-
-/** Run unit tests for mmap() wrapper functionality. */
-static void
-test_util_mmap(void)
-{
- char *fname1 = tor_strdup(get_fname("mapped_1"));
- char *fname2 = tor_strdup(get_fname("mapped_2"));
- char *fname3 = tor_strdup(get_fname("mapped_3"));
- const size_t buflen = 17000;
- char *buf = tor_malloc(17000);
- tor_mmap_t *mapping = NULL;
-
- crypto_rand(buf, buflen);
-
- mapping = tor_mmap_file(fname1);
- test_assert(! mapping);
-
- write_str_to_file(fname1, "Short file.", 1);
- write_bytes_to_file(fname2, buf, buflen, 1);
- write_bytes_to_file(fname3, buf, 16384, 1);
-
- mapping = tor_mmap_file(fname1);
- test_assert(mapping);
- test_eq(mapping->size, strlen("Short file."));
- test_streq(mapping->data, "Short file.");
-#ifdef MS_WINDOWS
- tor_munmap_file(mapping);
- mapping = NULL;
- test_assert(unlink(fname1) == 0);
-#else
- /* make sure we can unlink. */
- test_assert(unlink(fname1) == 0);
- test_streq(mapping->data, "Short file.");
- tor_munmap_file(mapping);
- mapping = NULL;
-#endif
-
- /* Now a zero-length file. */
- write_str_to_file(fname1, "", 1);
- mapping = tor_mmap_file(fname1);
- test_eq(mapping, NULL);
- test_eq(ERANGE, errno);
- unlink(fname1);
-
- /* Make sure that we fail to map a no-longer-existent file. */
- mapping = tor_mmap_file(fname1);
- test_assert(mapping == NULL);
-
- /* Now try a big file that stretches across a few pages and isn't aligned */
- mapping = tor_mmap_file(fname2);
- test_assert(mapping);
- test_eq(mapping->size, buflen);
- test_memeq(mapping->data, buf, buflen);
- tor_munmap_file(mapping);
- mapping = NULL;
-
- /* Now try a big aligned file. */
- mapping = tor_mmap_file(fname3);
- test_assert(mapping);
- test_eq(mapping->size, 16384);
- test_memeq(mapping->data, buf, 16384);
- tor_munmap_file(mapping);
- mapping = NULL;
-
- done:
- unlink(fname1);
- unlink(fname2);
- unlink(fname3);
-
- tor_free(fname1);
- tor_free(fname2);
- tor_free(fname3);
- tor_free(buf);
-
- if (mapping)
- tor_munmap_file(mapping);
-}
-
-/** Run unit tests for escaping/unescaping data for use by controllers. */
-static void
-test_util_control_formats(void)
-{
- char *out = NULL;
- const char *inp =
- "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
- size_t sz;
-
- sz = read_escaped_data(inp, strlen(inp), &out);
- test_streq(out,
- ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
- test_eq(sz, strlen(out));
-
- done:
- tor_free(out);
-}
-
-static void
-test_util_sscanf(void)
-{
- unsigned u1, u2, u3;
- char s1[10], s2[10], s3[10], ch;
- int r;
-
- r = tor_sscanf("hello world", "hello world"); /* String match: success */
- test_eq(r, 0);
- r = tor_sscanf("hello world 3", "hello worlb %u", &u1); /* String fail */
- test_eq(r, 0);
- r = tor_sscanf("12345", "%u", &u1); /* Simple number */
- test_eq(r, 1);
- test_eq(u1, 12345u);
- r = tor_sscanf("", "%u", &u1); /* absent number */
- test_eq(r, 0);
- r = tor_sscanf("A", "%u", &u1); /* bogus number */
- test_eq(r, 0);
- r = tor_sscanf("4294967295", "%u", &u1); /* UINT32_MAX should work. */
- test_eq(r, 1);
- test_eq(u1, 4294967295u);
- r = tor_sscanf("4294967296", "%u", &u1); /* Always say -1 at 32 bits. */
- test_eq(r, 0);
- r = tor_sscanf("123456", "%2u%u", &u1, &u2); /* Width */
- test_eq(r, 2);
- test_eq(u1, 12u);
- test_eq(u2, 3456u);
- r = tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3); /* separators */
- test_eq(r, 3);
- test_eq(u1, 12u);
- test_eq(u2, 3u);
- test_eq(u3, 456u);
- r = tor_sscanf("12:3:045", "%2u:%2u:%3u", &u1, &u2, &u3); /* 0s */
- test_eq(r, 3);
- test_eq(u1, 12u);
- test_eq(u2, 3u);
- test_eq(u3, 45u);
- /* %u does not match space.*/
- r = tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3);
- test_eq(r, 2);
- /* %u does not match negative numbers. */
- r = tor_sscanf("12:3:-4", "%2u:%2u:%3u", &u1, &u2, &u3);
- test_eq(r, 2);
- /* Arbitrary amounts of 0-padding are okay */
- r = tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3);
- test_eq(r, 3);
- test_eq(u1, 12u);
- test_eq(u2, 3u);
- test_eq(u3, 99u);
-
- r = tor_sscanf("99% fresh", "%3u%% fresh", &u1); /* percents are scannable.*/
- test_eq(r, 1);
- test_eq(u1, 99);
-
- r = tor_sscanf("hello", "%s", s1); /* %s needs a number. */
- test_eq(r, -1);
-
- r = tor_sscanf("hello", "%3s%7s", s1, s2); /* %s matches characters. */
- test_eq(r, 2);
- test_streq(s1, "hel");
- test_streq(s2, "lo");
- r = tor_sscanf("WD40", "%2s%u", s3, &u1); /* %s%u */
- test_eq(r, 2);
- test_streq(s3, "WD");
- test_eq(u1, 40);
- r = tor_sscanf("76trombones", "%6u%9s", &u1, s1); /* %u%s */
- test_eq(r, 2);
- test_eq(u1, 76);
- test_streq(s1, "trombones");
- r = tor_sscanf("hello world", "%9s %9s", s1, s2); /* %s doesn't eat space. */
- test_eq(r, 2);
- test_streq(s1, "hello");
- test_streq(s2, "world");
- r = tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3); /* %s can be empty. */
- test_eq(r, 3);
- test_streq(s1, "hi");
- test_streq(s2, "");
- test_streq(s3, "");
-
- r = tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
- test_eq(r, 3);
- r = tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
- test_eq(r, 4);
-
- done:
- ;
-}
-
/** Run unit tests for the onion handshake code. */
static void
test_onion_handshake(void)
@@ -3194,226 +1956,6 @@ bench_dmap(void)
smartlist_free(sl2);
}
-/** Run unittests for memory pool allocator */
-static void
-test_util_mempool(void)
-{
- mp_pool_t *pool = NULL;
- smartlist_t *allocated = NULL;
- int i;
-
- pool = mp_pool_new(1, 100);
- test_assert(pool);
- test_assert(pool->new_chunk_capacity >= 100);
- test_assert(pool->item_alloc_size >= sizeof(void*)+1);
- mp_pool_destroy(pool);
- pool = NULL;
-
- pool = mp_pool_new(241, 2500);
- test_assert(pool);
- test_assert(pool->new_chunk_capacity >= 10);
- test_assert(pool->item_alloc_size >= sizeof(void*)+241);
- test_eq(pool->item_alloc_size & 0x03, 0);
- test_assert(pool->new_chunk_capacity < 60);
-
- allocated = smartlist_create();
- for (i = 0; i < 20000; ++i) {
- if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
- void *m = mp_pool_get(pool);
- memset(m, 0x09, 241);
- smartlist_add(allocated, m);
- //printf("%d: %p\n", i, m);
- //mp_pool_assert_ok(pool);
- } else {
- int idx = crypto_rand_int(smartlist_len(allocated));
- void *m = smartlist_get(allocated, idx);
- //printf("%d: free %p\n", i, m);
- smartlist_del(allocated, idx);
- mp_pool_release(m);
- //mp_pool_assert_ok(pool);
- }
- if (crypto_rand_int(777)==0)
- mp_pool_clean(pool, 1, 1);
-
- if (i % 777)
- mp_pool_assert_ok(pool);
- }
-
- done:
- if (allocated) {
- SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
- mp_pool_assert_ok(pool);
- mp_pool_clean(pool, 0, 0);
- mp_pool_assert_ok(pool);
- smartlist_free(allocated);
- }
-
- if (pool)
- mp_pool_destroy(pool);
-}
-
-/** Run unittests for memory area allocator */
-static void
-test_util_memarea(void)
-{
- memarea_t *area = memarea_new();
- char *p1, *p2, *p3, *p1_orig;
- void *malloced_ptr = NULL;
- int i;
-
- test_assert(area);
-
- p1_orig = p1 = memarea_alloc(area,64);
- p2 = memarea_alloc_zero(area,52);
- p3 = memarea_alloc(area,11);
-
- test_assert(memarea_owns_ptr(area, p1));
- test_assert(memarea_owns_ptr(area, p2));
- test_assert(memarea_owns_ptr(area, p3));
- /* Make sure we left enough space. */
- test_assert(p1+64 <= p2);
- test_assert(p2+52 <= p3);
- /* Make sure we aligned. */
- test_eq(((uintptr_t)p1) % sizeof(void*), 0);
- test_eq(((uintptr_t)p2) % sizeof(void*), 0);
- test_eq(((uintptr_t)p3) % sizeof(void*), 0);
- test_assert(!memarea_owns_ptr(area, p3+8192));
- test_assert(!memarea_owns_ptr(area, p3+30));
- test_assert(tor_mem_is_zero(p2, 52));
- /* Make sure we don't overalign. */
- p1 = memarea_alloc(area, 1);
- p2 = memarea_alloc(area, 1);
- test_eq(p1+sizeof(void*), p2);
- {
- malloced_ptr = tor_malloc(64);
- test_assert(!memarea_owns_ptr(area, malloced_ptr));
- tor_free(malloced_ptr);
- }
-
- /* memarea_memdup */
- {
- malloced_ptr = tor_malloc(64);
- crypto_rand((char*)malloced_ptr, 64);
- p1 = memarea_memdup(area, malloced_ptr, 64);
- test_assert(p1 != malloced_ptr);
- test_memeq(p1, malloced_ptr, 64);
- tor_free(malloced_ptr);
- }
-
- /* memarea_strdup. */
- p1 = memarea_strdup(area,"");
- p2 = memarea_strdup(area, "abcd");
- test_assert(p1);
- test_assert(p2);
- test_streq(p1, "");
- test_streq(p2, "abcd");
-
- /* memarea_strndup. */
- {
- const char *s = "Ad ogni porta batte la morte e grida: il nome!";
- /* (From Turandot, act 3.) */
- size_t len = strlen(s);
- p1 = memarea_strndup(area, s, 1000);
- p2 = memarea_strndup(area, s, 10);
- test_streq(p1, s);
- test_assert(p2 >= p1 + len + 1);
- test_memeq(s, p2, 10);
- test_eq(p2[10], '\0');
- p3 = memarea_strndup(area, s, len);
- test_streq(p3, s);
- p3 = memarea_strndup(area, s, len-1);
- test_memeq(s, p3, len-1);
- test_eq(p3[len-1], '\0');
- }
-
- memarea_clear(area);
- p1 = memarea_alloc(area, 1);
- test_eq(p1, p1_orig);
- memarea_clear(area);
-
- /* Check for running over an area's size. */
- for (i = 0; i < 512; ++i) {
- p1 = memarea_alloc(area, crypto_rand_int(5)+1);
- test_assert(memarea_owns_ptr(area, p1));
- }
- memarea_assert_ok(area);
- /* Make sure we can allocate a too-big object. */
- p1 = memarea_alloc_zero(area, 9000);
- p2 = memarea_alloc_zero(area, 16);
- test_assert(memarea_owns_ptr(area, p1));
- test_assert(memarea_owns_ptr(area, p2));
-
- done:
- memarea_drop_all(area);
- tor_free(malloced_ptr);
-}
-
-/** Run unit tests for utility functions to get file names relative to
- * the data directory. */
-static void
-test_util_datadir(void)
-{
- char buf[1024];
- char *f = NULL;
-
- f = get_datadir_fname(NULL);
- test_streq(f, temp_dir);
- tor_free(f);
- f = get_datadir_fname("state");
- tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
- test_streq(f, buf);
- tor_free(f);
- f = get_datadir_fname2("cache", "thingy");
- tor_snprintf(buf, sizeof(buf),
- "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
- test_streq(f, buf);
- tor_free(f);
- f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
- tor_snprintf(buf, sizeof(buf),
- "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
- test_streq(f, buf);
- tor_free(f);
- f = get_datadir_fname_suffix("cache", ".foo");
- tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
- temp_dir);
- test_streq(f, buf);
-
- done:
- tor_free(f);
-}
-
-static void
-test_util_strtok(void)
-{
- char buf[128];
- char buf2[128];
- char *cp1, *cp2;
- strlcpy(buf, "Graved on the dark in gestures of descent", sizeof(buf));
- strlcpy(buf2, "they.seemed;their!own;most.perfect;monument", sizeof(buf2));
- /* -- "Year's End", Richard Wilbur */
-
- test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
- test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
-#define S1() tor_strtok_r_impl(NULL, " ", &cp1)
-#define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
- test_streq("on", S1());
- test_streq("the", S1());
- test_streq("dark", S1());
- test_streq("seemed", S2());
- test_streq("their", S2());
- test_streq("own", S2());
- test_streq("in", S1());
- test_streq("gestures", S1());
- test_streq("of", S1());
- test_streq("most", S2());
- test_streq("perfect", S2());
- test_streq("descent", S1());
- test_streq("monument", S2());
- test_assert(NULL == S1());
- test_assert(NULL == S2());
- done:
- ;
-}
/** Test encoding and parsing of rendezvous service descriptors. */
static void
@@ -3625,17 +2167,6 @@ const struct testcase_setup_t legacy_setup = {
static struct testcase_t test_array[] = {
ENT(buffers),
- ENT(util),
- SUBENT(util, ip6_helpers),
- SUBENT(util, gzip),
- SUBENT(util, datadir),
- SUBENT(util, mempool),
- SUBENT(util, memarea),
- SUBENT(util, control_formats),
- SUBENT(util, mmap),
- SUBENT(util, threads),
- SUBENT(util, sscanf),
- SUBENT(util, strtok),
ENT(onion_handshake),
ENT(dir_format),
ENT(dirutil),
@@ -3652,13 +2183,17 @@ static struct testcase_t test_array[] = {
END_OF_TESTCASES
};
+extern struct testcase_t addr_tests[];
extern struct testcase_t crypto_tests[];
extern struct testcase_t container_tests[];
+extern struct testcase_t util_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
+ { "addr/", addr_tests },
{ "crypto/", crypto_tests },
{ "container/", container_tests },
+ { "util/", util_tests },
END_OF_GROUPS
};
diff --git a/src/test/test_addr.c b/src/test/test_addr.c
new file mode 100644
index 0000000..1d057ff
--- /dev/null
+++ b/src/test/test_addr.c
@@ -0,0 +1,440 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2009, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#include "or.h"
+#include "test.h"
+
+#define _test_op_ip6(a,op,b,e1,e2) \
+ STMT_BEGIN \
+ tt_assert_test_fmt_type(a,b,e1" "#op" "e2,struct in6_addr*, \
+ (memcmp(_val1->s6_addr, _val2->s6_addr, 16) op 0), \
+ char *, "%s", \
+ { int i; char *cp; \
+ cp = _print = tor_malloc(64); \
+ for (i=0;i<16;++i) { \
+ tor_snprintf(cp, 3,"%02x", (unsigned)_value->s6_addr[i]);\
+ cp += 2; \
+ if (i != 15) *cp++ = ':'; \
+ } \
+ }, { tor_free(_print); } \
+ ); \
+ STMT_END
+
+/** Helper: Assert that two strings both decode as IPv6 addresses with
+ * tor_inet_pton(), and both decode to the same address. */
+#define test_pton6_same(a,b) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
+ test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
+ _test_op_ip6(&a1,==,&a2,#a,#b); \
+ STMT_END
+
+/** Helper: Assert that <b>a</b> is recognized as a bad IPv6 address by
+ * tor_inet_pton(). */
+#define test_pton6_bad(a) \
+ test_eq(0, tor_inet_pton(AF_INET6, a, &a1))
+
+/** Helper: assert that <b>a</b>, when parsed by tor_inet_pton() and displayed
+ * with tor_inet_ntop(), yields <b>b</b>. Also assert that <b>b</b> parses to
+ * the same value as <b>a</b>. */
+#define test_ntop6_reduces(a,b) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &a1), 1); \
+ test_streq(tor_inet_ntop(AF_INET6, &a1, buf, sizeof(buf)), b); \
+ test_eq(tor_inet_pton(AF_INET6, b, &a2), 1); \
+ _test_op_ip6(&a1, ==, &a2, a, b); \
+ STMT_END
+
+/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
+ * passes tor_addr_is_internal() with <b>for_listening</b>. */
+#define test_internal_ip(a,for_listening) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
+ t1.family = AF_INET6; \
+ if (!tor_addr_is_internal(&t1, for_listening)) \
+ test_fail_msg( a "was not internal."); \
+ STMT_END
+
+/** Helper: assert that <b>a</b> parses by tor_inet_pton() into a address that
+ * does not pass tor_addr_is_internal() with <b>for_listening</b>. */
+#define test_external_ip(a,for_listening) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
+ t1.family = AF_INET6; \
+ if (tor_addr_is_internal(&t1, for_listening)) \
+ test_fail_msg(a "was not external."); \
+ STMT_END
+
+/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
+ * tor_inet_pton(), give addresses that compare in the order defined by
+ * <b>op</b> with tor_addr_compare(). */
+#define test_addr_compare(a, op, b) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
+ test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
+ t1.family = t2.family = AF_INET6; \
+ r = tor_addr_compare(&t1,&t2,CMP_SEMANTIC); \
+ if (!(r op 0)) \
+ test_fail_msg("failed: tor_addr_compare("a","b") "#op" 0"); \
+ STMT_END
+
+/** Helper: Assert that <b>a</b> and <b>b</b>, when parsed by
+ * tor_inet_pton(), give addresses that compare in the order defined by
+ * <b>op</b> with tor_addr_compare_masked() with <b>m</b> masked. */
+#define test_addr_compare_masked(a, op, b, m) STMT_BEGIN \
+ test_eq(tor_inet_pton(AF_INET6, a, &t1.addr.in6_addr), 1); \
+ test_eq(tor_inet_pton(AF_INET6, b, &t2.addr.in6_addr), 1); \
+ t1.family = t2.family = AF_INET6; \
+ r = tor_addr_compare_masked(&t1,&t2,m,CMP_SEMANTIC); \
+ if (!(r op 0)) \
+ test_fail_msg("failed: tor_addr_compare_masked("a","b","#m") "#op" 0"); \
+ STMT_END
+
+/** Helper: assert that <b>xx</b> is parseable as a masked IPv6 address with
+ * ports by tor_parse_mask_addr_ports(), with family <b>f</b>, IP address
+ * as 4 32-bit words <b>ip1...ip4</b>, mask bits as <b>mm</b>, and port range
+ * as <b>pt1..pt2</b>. */
+#define test_addr_mask_ports_parse(xx, f, ip1, ip2, ip3, ip4, mm, pt1, pt2) \
+ STMT_BEGIN \
+ test_eq(tor_addr_parse_mask_ports(xx, &t1, &mask, &port1, &port2), f); \
+ p1=tor_inet_ntop(AF_INET6, &t1.addr.in6_addr, bug, sizeof(bug)); \
+ test_eq(htonl(ip1), tor_addr_to_in6_addr32(&t1)[0]); \
+ test_eq(htonl(ip2), tor_addr_to_in6_addr32(&t1)[1]); \
+ test_eq(htonl(ip3), tor_addr_to_in6_addr32(&t1)[2]); \
+ test_eq(htonl(ip4), tor_addr_to_in6_addr32(&t1)[3]); \
+ test_eq(mask, mm); \
+ test_eq(port1, pt1); \
+ test_eq(port2, pt2); \
+ STMT_END
+
+/** Run unit tests for IPv6 encoding/decoding/manipulation functions. */
+static void
+test_addr_ip6_helpers(void)
+{
+ char buf[TOR_ADDR_BUF_LEN], bug[TOR_ADDR_BUF_LEN];
+ struct in6_addr a1, a2;
+ tor_addr_t t1, t2;
+ int r, i;
+ uint16_t port1, port2;
+ maskbits_t mask;
+ const char *p1;
+ struct sockaddr_storage sa_storage;
+ struct sockaddr_in *sin;
+ struct sockaddr_in6 *sin6;
+
+ // struct in_addr b1, b2;
+ /* Test tor_inet_ntop and tor_inet_pton: IPv6 */
+
+ /* ==== Converting to and from sockaddr_t. */
+ sin = (struct sockaddr_in *)&sa_storage;
+ sin->sin_family = AF_INET;
+ sin->sin_port = 9090;
+ sin->sin_addr.s_addr = htonl(0x7f7f0102); /*127.127.1.2*/
+ tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin, NULL);
+ test_eq(tor_addr_family(&t1), AF_INET);
+ test_eq(tor_addr_to_ipv4h(&t1), 0x7f7f0102);
+
+ memset(&sa_storage, 0, sizeof(sa_storage));
+ test_eq(sizeof(struct sockaddr_in),
+ tor_addr_to_sockaddr(&t1, 1234, (struct sockaddr *)&sa_storage,
+ sizeof(sa_storage)));
+ test_eq(1234, ntohs(sin->sin_port));
+ test_eq(0x7f7f0102, ntohl(sin->sin_addr.s_addr));
+
+ memset(&sa_storage, 0, sizeof(sa_storage));
+ sin6 = (struct sockaddr_in6 *)&sa_storage;
+ sin6->sin6_family = AF_INET6;
+ sin6->sin6_port = htons(7070);
+ sin6->sin6_addr.s6_addr[0] = 128;
+ tor_addr_from_sockaddr(&t1, (struct sockaddr *)sin6, NULL);
+ test_eq(tor_addr_family(&t1), AF_INET6);
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
+ test_streq(p1, "8000::");
+
+ memset(&sa_storage, 0, sizeof(sa_storage));
+ test_eq(sizeof(struct sockaddr_in6),
+ tor_addr_to_sockaddr(&t1, 9999, (struct sockaddr *)&sa_storage,
+ sizeof(sa_storage)));
+ test_eq(AF_INET6, sin6->sin6_family);
+ test_eq(9999, ntohs(sin6->sin6_port));
+ test_eq(0x80000000, ntohl(S6_ADDR32(sin6->sin6_addr)[0]));
+
+ /* ==== tor_addr_lookup: static cases. (Can't test dns without knowing we
+ * have a good resolver. */
+ test_eq(0, tor_addr_lookup("127.128.129.130", AF_UNSPEC, &t1));
+ test_eq(AF_INET, tor_addr_family(&t1));
+ test_eq(tor_addr_to_ipv4h(&t1), 0x7f808182);
+
+ test_eq(0, tor_addr_lookup("9000::5", AF_UNSPEC, &t1));
+ test_eq(AF_INET6, tor_addr_family(&t1));
+ test_eq(0x90, tor_addr_to_in6_addr8(&t1)[0]);
+ test_assert(tor_mem_is_zero((char*)tor_addr_to_in6_addr8(&t1)+1, 14));
+ test_eq(0x05, tor_addr_to_in6_addr8(&t1)[15]);
+
+ /* === Test pton: valid af_inet6 */
+ /* Simple, valid parsing. */
+ r = tor_inet_pton(AF_INET6,
+ "0102:0304:0506:0708:090A:0B0C:0D0E:0F10", &a1);
+ test_assert(r==1);
+ for (i=0;i<16;++i) { test_eq(i+1, (int)a1.s6_addr[i]); }
+ /* ipv4 ending. */
+ test_pton6_same("0102:0304:0506:0708:090A:0B0C:0D0E:0F10",
+ "0102:0304:0506:0708:090A:0B0C:13.14.15.16");
+ /* shortened words. */
+ test_pton6_same("0001:0099:BEEF:0000:0123:FFFF:0001:0001",
+ "1:99:BEEF:0:0123:FFFF:1:1");
+ /* zeros at the beginning */
+ test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
+ "::9:c0a8:1:1");
+ test_pton6_same("0000:0000:0000:0000:0009:C0A8:0001:0001",
+ "::9:c0a8:0.1.0.1");
+ /* zeros in the middle. */
+ test_pton6_same("fe80:0000:0000:0000:0202:1111:0001:0001",
+ "fe80::202:1111:1:1");
+ /* zeros at the end. */
+ test_pton6_same("1000:0001:0000:0007:0000:0000:0000:0000",
+ "1000:1:0:7::");
+
+ /* === Test ntop: af_inet6 */
+ test_ntop6_reduces("0:0:0:0:0:0:0:0", "::");
+
+ test_ntop6_reduces("0001:0099:BEEF:0006:0123:FFFF:0001:0001",
+ "1:99:beef:6:123:ffff:1:1");
+
+ //test_ntop6_reduces("0:0:0:0:0:0:c0a8:0101", "::192.168.1.1");
+ test_ntop6_reduces("0:0:0:0:0:ffff:c0a8:0101", "::ffff:192.168.1.1");
+ test_ntop6_reduces("002:0:0000:0:3::4", "2::3:0:0:4");
+ test_ntop6_reduces("0:0::1:0:3", "::1:0:3");
+ test_ntop6_reduces("008:0::0", "8::");
+ test_ntop6_reduces("0:0:0:0:0:ffff::1", "::ffff:0.0.0.1");
+ test_ntop6_reduces("abcd:0:0:0:0:0:7f00::", "abcd::7f00:0");
+ test_ntop6_reduces("0000:0000:0000:0000:0009:C0A8:0001:0001",
+ "::9:c0a8:1:1");
+ test_ntop6_reduces("fe80:0000:0000:0000:0202:1111:0001:0001",
+ "fe80::202:1111:1:1");
+ test_ntop6_reduces("1000:0001:0000:0007:0000:0000:0000:0000",
+ "1000:1:0:7::");
+
+ /* === Test pton: invalid in6. */
+ test_pton6_bad("foobar.");
+ test_pton6_bad("55555::");
+ test_pton6_bad("9:-60::");
+ test_pton6_bad("1:2:33333:4:0002:3::");
+ //test_pton6_bad("1:2:3333:4:00002:3::");// BAD, but glibc doesn't say so.
+ test_pton6_bad("1:2:3333:4:fish:3::");
+ test_pton6_bad("1:2:3:4:5:6:7:8:9");
+ test_pton6_bad("1:2:3:4:5:6:7");
+ test_pton6_bad("1:2:3:4:5:6:1.2.3.4.5");
+ test_pton6_bad("1:2:3:4:5:6:1.2.3");
+ test_pton6_bad("::1.2.3");
+ test_pton6_bad("::1.2.3.4.5");
+ test_pton6_bad("99");
+ test_pton6_bad("");
+ test_pton6_bad("1::2::3:4");
+ test_pton6_bad("a:::b:c");
+ test_pton6_bad(":::a:b:c");
+ test_pton6_bad("a:b:c:::");
+
+ /* test internal checking */
+ test_external_ip("fbff:ffff::2:7", 0);
+ test_internal_ip("fc01::2:7", 0);
+ test_internal_ip("fdff:ffff::f:f", 0);
+ test_external_ip("fe00::3:f", 0);
+
+ test_external_ip("fe7f:ffff::2:7", 0);
+ test_internal_ip("fe80::2:7", 0);
+ test_internal_ip("febf:ffff::f:f", 0);
+
+ test_internal_ip("fec0::2:7:7", 0);
+ test_internal_ip("feff:ffff::e:7:7", 0);
+ test_external_ip("ff00::e:7:7", 0);
+
+ test_internal_ip("::", 0);
+ test_internal_ip("::1", 0);
+ test_internal_ip("::1", 1);
+ test_internal_ip("::", 0);
+ test_external_ip("::", 1);
+ test_external_ip("::2", 0);
+ test_external_ip("2001::", 0);
+ test_external_ip("ffff::", 0);
+
+ test_external_ip("::ffff:0.0.0.0", 1);
+ test_internal_ip("::ffff:0.0.0.0", 0);
+ test_internal_ip("::ffff:0.255.255.255", 0);
+ test_external_ip("::ffff:1.0.0.0", 0);
+
+ test_external_ip("::ffff:9.255.255.255", 0);
+ test_internal_ip("::ffff:10.0.0.0", 0);
+ test_internal_ip("::ffff:10.255.255.255", 0);
+ test_external_ip("::ffff:11.0.0.0", 0);
+
+ test_external_ip("::ffff:126.255.255.255", 0);
+ test_internal_ip("::ffff:127.0.0.0", 0);
+ test_internal_ip("::ffff:127.255.255.255", 0);
+ test_external_ip("::ffff:128.0.0.0", 0);
+
+ test_external_ip("::ffff:172.15.255.255", 0);
+ test_internal_ip("::ffff:172.16.0.0", 0);
+ test_internal_ip("::ffff:172.31.255.255", 0);
+ test_external_ip("::ffff:172.32.0.0", 0);
+
+ test_external_ip("::ffff:192.167.255.255", 0);
+ test_internal_ip("::ffff:192.168.0.0", 0);
+ test_internal_ip("::ffff:192.168.255.255", 0);
+ test_external_ip("::ffff:192.169.0.0", 0);
+
+ test_external_ip("::ffff:169.253.255.255", 0);
+ test_internal_ip("::ffff:169.254.0.0", 0);
+ test_internal_ip("::ffff:169.254.255.255", 0);
+ test_external_ip("::ffff:169.255.0.0", 0);
+ test_assert(is_internal_IP(0x7f000001, 0));
+
+ /* tor_addr_compare(tor_addr_t x2) */
+ test_addr_compare("ffff::", ==, "ffff::0");
+ test_addr_compare("0::3:2:1", <, "0::ffff:0.3.2.1");
+ test_addr_compare("0::2:2:1", <, "0::ffff:0.3.2.1");
+ test_addr_compare("0::ffff:0.3.2.1", >, "0::0:0:0");
+ test_addr_compare("0::ffff:5.2.2.1", <, "::ffff:6.0.0.0"); /* XXXX wrong. */
+ tor_addr_parse_mask_ports("[::ffff:2.3.4.5]", &t1, NULL, NULL, NULL);
+ tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
+ test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) == 0);
+ tor_addr_parse_mask_ports("[::ffff:2.3.4.4]", &t1, NULL, NULL, NULL);
+ tor_addr_parse_mask_ports("2.3.4.5", &t2, NULL, NULL, NULL);
+ test_assert(tor_addr_compare(&t1, &t2, CMP_SEMANTIC) < 0);
+
+ /* test compare_masked */
+ test_addr_compare_masked("ffff::", ==, "ffff::0", 128);
+ test_addr_compare_masked("ffff::", ==, "ffff::0", 64);
+ test_addr_compare_masked("0::2:2:1", <, "0::8000:2:1", 81);
+ test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
+
+ /* Test decorated addr_to_string. */
+ test_eq(AF_INET6, tor_addr_from_str(&t1, "[123:45:6789::5005:11]"));
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
+ test_streq(p1, "[123:45:6789::5005:11]");
+ test_eq(AF_INET, tor_addr_from_str(&t1, "18.0.0.1"));
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
+ test_streq(p1, "18.0.0.1");
+
+ /* Test tor_addr_parse_reverse_lookup_name */
+ i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
+ test_eq(0, i);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "Foobar.baz", AF_UNSPEC, 1);
+ test_eq(0, i);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "1.0.168.192.in-addr.arpa",
+ AF_UNSPEC, 1);
+ test_eq(1, i);
+ test_eq(tor_addr_family(&t1), AF_INET);
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
+ test_streq(p1, "192.168.0.1");
+ i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 0);
+ test_eq(0, i);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "192.168.0.99", AF_UNSPEC, 1);
+ test_eq(1, i);
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
+ test_streq(p1, "192.168.0.99");
+ memset(&t1, 0, sizeof(t1));
+ i = tor_addr_parse_reverse_lookup_name(&t1,
+ "0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f."
+ "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
+ "ip6.ARPA",
+ AF_UNSPEC, 0);
+ test_eq(1, i);
+ p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
+ test_streq(p1, "[9dee:effe:ebe1:beef:fedc:ba98:7654:3210]");
+ /* Failing cases. */
+ i = tor_addr_parse_reverse_lookup_name(&t1,
+ "6.7.8.9.a.b.c.d.e.f."
+ "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
+ "ip6.ARPA",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1,
+ "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.f.0."
+ "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
+ "ip6.ARPA",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1,
+ "6.7.8.9.a.b.c.d.e.f.X.0.0.0.0.9."
+ "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
+ "ip6.ARPA",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "32.1.1.in-addr.arpa",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1, ".in-addr.arpa",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
+ AF_UNSPEC, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1, "1.2.3.4.5.in-addr.arpa",
+ AF_INET6, 0);
+ test_eq(i, -1);
+ i = tor_addr_parse_reverse_lookup_name(&t1,
+ "6.7.8.9.a.b.c.d.e.f.a.b.c.d.e.0."
+ "f.e.e.b.1.e.b.e.e.f.f.e.e.e.d.9."
+ "ip6.ARPA",
+ AF_INET, 0);
+ test_eq(i, -1);
+
+ /* test tor_addr_parse_mask_ports */
+ test_addr_mask_ports_parse("[::f]/17:47-95", AF_INET6,
+ 0, 0, 0, 0x0000000f, 17, 47, 95);
+ //test_addr_parse("[::fefe:4.1.1.7/120]:999-1000");
+ //test_addr_parse_check("::fefe:401:107", 120, 999, 1000);
+ test_addr_mask_ports_parse("[::ffff:4.1.1.7]/120:443", AF_INET6,
+ 0, 0, 0x0000ffff, 0x04010107, 120, 443, 443);
+ test_addr_mask_ports_parse("[abcd:2::44a:0]:2-65000", AF_INET6,
+ 0xabcd0002, 0, 0, 0x044a0000, 128, 2, 65000);
+
+ r=tor_addr_parse_mask_ports("[fefef::]/112", &t1, NULL, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("efef::/112", &t1, NULL, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f::]", &t1, NULL, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("[::f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("[f:f:f:f:f:f:f:f:f]", &t1, NULL, NULL, NULL);
+ test_assert(r == -1);
+ /* Test for V4-mapped address with mask < 96. (arguably not valid) */
+ r=tor_addr_parse_mask_ports("[::ffff:1.1.2.2/33]", &t1, &mask, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("1.1.2.2/33", &t1, &mask, NULL, NULL);
+ test_assert(r == -1);
+ r=tor_addr_parse_mask_ports("1.1.2.2/31", &t1, &mask, NULL, NULL);
+ test_assert(r == AF_INET);
+ r=tor_addr_parse_mask_ports("[efef::]/112", &t1, &mask, &port1, &port2);
+ test_assert(r == AF_INET6);
+ test_assert(port1 == 1);
+ test_assert(port2 == 65535);
+
+ /* make sure inet address lengths >= max */
+ test_assert(INET_NTOA_BUF_LEN >= sizeof("255.255.255.255"));
+ test_assert(TOR_ADDR_BUF_LEN >=
+ sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"));
+
+ test_assert(sizeof(tor_addr_t) >= sizeof(struct in6_addr));
+
+ /* get interface addresses */
+ r = get_interface_address6(LOG_DEBUG, AF_INET, &t1);
+ i = get_interface_address6(LOG_DEBUG, AF_INET6, &t2);
+#if 0
+ tor_inet_ntop(AF_INET, &t1.sa.sin_addr, buf, sizeof(buf));
+ printf("\nv4 address: %s (family=%i)", buf, IN_FAMILY(&t1));
+ tor_inet_ntop(AF_INET6, &t2.sa6.sin6_addr, buf, sizeof(buf));
+ printf("\nv6 address: %s (family=%i)", buf, IN_FAMILY(&t2));
+#endif
+
+ done:
+ ;
+}
+
+#define ADDR_LEGACY(name) \
+ { #name, legacy_test_helper, 0, &legacy_setup, test_addr_ ## name }
+
+struct testcase_t addr_tests[] = {
+ ADDR_LEGACY(ip6_helpers),
+ END_OF_TESTCASES
+};
diff --git a/src/test/test_util.c b/src/test/test_util.c
new file mode 100644
index 0000000..28b42c1
--- /dev/null
+++ b/src/test/test_util.c
@@ -0,0 +1,1062 @@
+/* Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2009, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#include "orconfig.h"
+#define CONTROL_PRIVATE
+#define MEMPOOL_PRIVATE
+#include "or.h"
+#include "test.h"
+#include "mempool.h"
+#include "memarea.h"
+
+/** Test basic utility functionality. */
+static void
+test_util_misc(void)
+{
+ struct timeval start, end;
+ struct tm a_time;
+ char timestr[RFC1123_TIME_LEN+1];
+ char buf[1024];
+ time_t t_res;
+ int i;
+ uint32_t u32;
+ uint16_t u16;
+ char *cp, *k, *v;
+ const char *str;
+
+ start.tv_sec = 5;
+ start.tv_usec = 5000;
+
+ end.tv_sec = 5;
+ end.tv_usec = 5000;
+
+ test_eq(0L, tv_udiff(&start, &end));
+
+ end.tv_usec = 7000;
+
+ test_eq(2000L, tv_udiff(&start, &end));
+
+ end.tv_sec = 6;
+
+ test_eq(1002000L, tv_udiff(&start, &end));
+
+ end.tv_usec = 0;
+
+ test_eq(995000L, tv_udiff(&start, &end));
+
+ end.tv_sec = 4;
+
+ test_eq(-1005000L, tv_udiff(&start, &end));
+
+ end.tv_usec = 999990;
+ start.tv_sec = 1;
+ start.tv_usec = 500;
+
+ /* The test values here are confirmed to be correct on a platform
+ * with a working timegm. */
+ a_time.tm_year = 2003-1900;
+ a_time.tm_mon = 7;
+ a_time.tm_mday = 30;
+ a_time.tm_hour = 6;
+ a_time.tm_min = 14;
+ a_time.tm_sec = 55;
+ test_eq((time_t) 1062224095UL, tor_timegm(&a_time));
+ a_time.tm_year = 2004-1900; /* Try a leap year, after feb. */
+ test_eq((time_t) 1093846495UL, tor_timegm(&a_time));
+ a_time.tm_mon = 1; /* Try a leap year, in feb. */
+ a_time.tm_mday = 10;
+ test_eq((time_t) 1076393695UL, tor_timegm(&a_time));
+
+ format_rfc1123_time(timestr, 0);
+ test_streq("Thu, 01 Jan 1970 00:00:00 GMT", timestr);
+ format_rfc1123_time(timestr, (time_t)1091580502UL);
+ test_streq("Wed, 04 Aug 2004 00:48:22 GMT", timestr);
+
+ t_res = 0;
+ i = parse_rfc1123_time(timestr, &t_res);
+ test_eq(i,0);
+ test_eq(t_res, (time_t)1091580502UL);
+ test_eq(-1, parse_rfc1123_time("Wed, zz Aug 2004 99-99x99 GMT", &t_res));
+ tor_gettimeofday(&start);
+
+ /* Tests for corner cases of strl operations */
+ test_eq(5, strlcpy(buf, "Hello", 0));
+ strlcpy(buf, "Hello", sizeof(buf));
+ test_eq(10, strlcat(buf, "Hello", 5));
+
+ /* Test tor_strstrip() */
+ strlcpy(buf, "Testing 1 2 3", sizeof(buf));
+ tor_strstrip(buf, ",!");
+ test_streq(buf, "Testing 1 2 3");
+ strlcpy(buf, "!Testing 1 2 3?", sizeof(buf));
+ tor_strstrip(buf, "!? ");
+ test_streq(buf, "Testing123");
+
+ /* Test parse_addr_port */
+ cp = NULL; u32 = 3; u16 = 3;
+ test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
+ test_streq(cp, "1.2.3.4");
+ test_eq(u32, 0x01020304u);
+ test_eq(u16, 0);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
+ test_streq(cp, "4.3.2.1");
+ test_eq(u32, 0x04030201u);
+ test_eq(u16, 99);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
+ &cp, NULL, &u16));
+ test_streq(cp, "nonexistent.address");
+ test_eq(u16, 4040);
+ tor_free(cp);
+ test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
+ test_streq(cp, "localhost");
+ test_eq(u32, 0x7f000001u);
+ test_eq(u16, 9999);
+ tor_free(cp);
+ u32 = 3;
+ test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
+ test_eq(cp, NULL);
+ test_eq(u32, 0x7f000001u);
+ test_eq(u16, 0);
+ tor_free(cp);
+ test_eq(0, addr_mask_get_bits(0x0u));
+ test_eq(32, addr_mask_get_bits(0xFFFFFFFFu));
+ test_eq(16, addr_mask_get_bits(0xFFFF0000u));
+ test_eq(31, addr_mask_get_bits(0xFFFFFFFEu));
+ test_eq(1, addr_mask_get_bits(0x80000000u));
+
+ /* Test tor_parse_long. */
+ test_eq(10L, tor_parse_long("10",10,0,100,NULL,NULL));
+ test_eq(0L, tor_parse_long("10",10,50,100,NULL,NULL));
+ test_eq(-50L, tor_parse_long("-50",10,-100,100,NULL,NULL));
+
+ /* Test tor_parse_ulong */
+ test_eq(10UL, tor_parse_ulong("10",10,0,100,NULL,NULL));
+ test_eq(0UL, tor_parse_ulong("10",10,50,100,NULL,NULL));
+
+ /* Test tor_parse_uint64. */
+ test_assert(U64_LITERAL(10) == tor_parse_uint64("10 x",10,0,100, &i, &cp));
+ test_assert(i == 1);
+ test_streq(cp, " x");
+ test_assert(U64_LITERAL(12345678901) ==
+ tor_parse_uint64("12345678901",10,0,UINT64_MAX, &i, &cp));
+ test_assert(i == 1);
+ test_streq(cp, "");
+ test_assert(U64_LITERAL(0) ==
+ tor_parse_uint64("12345678901",10,500,INT32_MAX, &i, &cp));
+ test_assert(i == 0);
+
+ {
+ /* Test tor_parse_double. */
+ double d = tor_parse_double("10", 0, UINT64_MAX,&i,NULL);
+ test_assert(i == 1);
+ test_assert(DBL_TO_U64(d) == 10);
+ d = tor_parse_double("0", 0, UINT64_MAX,&i,NULL);
+ test_assert(i == 1);
+ test_assert(DBL_TO_U64(d) == 0);
+ d = tor_parse_double(" ", 0, UINT64_MAX,&i,NULL);
+ test_assert(i == 0);
+ d = tor_parse_double(".0a", 0, UINT64_MAX,&i,NULL);
+ test_assert(i == 0);
+ d = tor_parse_double(".0a", 0, UINT64_MAX,&i,&cp);
+ test_assert(i == 1);
+ d = tor_parse_double("-.0", 0, UINT64_MAX,&i,NULL);
+ test_assert(i == 1);
+ }
+
+ /* Test failing snprintf cases */
+ test_eq(-1, tor_snprintf(buf, 0, "Foo"));
+ test_eq(-1, tor_snprintf(buf, 2, "Foo"));
+
+ /* Test printf with uint64 */
+ tor_snprintf(buf, sizeof(buf), "x!"U64_FORMAT"!x",
+ U64_PRINTF_ARG(U64_LITERAL(12345678901)));
+ test_streq(buf, "x!12345678901!x");
+
+ /* Test parse_config_line_from_str */
+ strlcpy(buf, "k v\n" " key value with spaces \n" "keykey val\n"
+ "k2\n"
+ "k3 \n" "\n" " \n" "#comment\n"
+ "k4#a\n" "k5#abc\n" "k6 val #with comment\n"
+ "kseven \"a quoted 'string\"\n"
+ "k8 \"a \\x71uoted\\n\\\"str\\\\ing\\t\\001\\01\\1\\\"\"\n"
+ , sizeof(buf));
+ str = buf;
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k");
+ test_streq(v, "v");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "key value with"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "key");
+ test_streq(v, "value with spaces");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "keykey"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "keykey");
+ test_streq(v, "val");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "k2\n"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k2");
+ test_streq(v, "");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "k3 \n"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k3");
+ test_streq(v, "");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "#comment"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k4");
+ test_streq(v, "");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "k5#abc"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k5");
+ test_streq(v, "");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "k6"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k6");
+ test_streq(v, "val");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "kseven"));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "kseven");
+ test_streq(v, "a quoted \'string");
+ tor_free(k); tor_free(v);
+ test_assert(!strcmpstart(str, "k8 "));
+
+ str = parse_config_line_from_str(str, &k, &v);
+ test_streq(k, "k8");
+ test_streq(v, "a quoted\n\"str\\ing\t\x01\x01\x01\"");
+ tor_free(k); tor_free(v);
+ test_streq(str, "");
+
+ /* Test for strcmpstart and strcmpend. */
+ test_assert(strcmpstart("abcdef", "abcdef")==0);
+ test_assert(strcmpstart("abcdef", "abc")==0);
+ test_assert(strcmpstart("abcdef", "abd")<0);
+ test_assert(strcmpstart("abcdef", "abb")>0);
+ test_assert(strcmpstart("ab", "abb")<0);
+
+ test_assert(strcmpend("abcdef", "abcdef")==0);
+ test_assert(strcmpend("abcdef", "def")==0);
+ test_assert(strcmpend("abcdef", "deg")<0);
+ test_assert(strcmpend("abcdef", "dee")>0);
+ test_assert(strcmpend("ab", "abb")<0);
+
+ test_assert(strcasecmpend("AbcDEF", "abcdef")==0);
+ test_assert(strcasecmpend("abcdef", "dEF")==0);
+ test_assert(strcasecmpend("abcDEf", "deg")<0);
+ test_assert(strcasecmpend("abcdef", "DEE")>0);
+ test_assert(strcasecmpend("ab", "abB")<0);
+
+ /* Test mem_is_zero */
+ memset(buf,0,128);
+ buf[128] = 'x';
+ test_assert(tor_digest_is_zero(buf));
+ test_assert(tor_mem_is_zero(buf, 10));
+ test_assert(tor_mem_is_zero(buf, 20));
+ test_assert(tor_mem_is_zero(buf, 128));
+ test_assert(!tor_mem_is_zero(buf, 129));
+ buf[60] = (char)255;
+ test_assert(!tor_mem_is_zero(buf, 128));
+ buf[0] = (char)1;
+ test_assert(!tor_mem_is_zero(buf, 10));
+
+ /* Test inet_ntop */
+ {
+ char tmpbuf[TOR_ADDR_BUF_LEN];
+ const char *ip = "176.192.208.224";
+ struct in_addr in;
+ tor_inet_pton(AF_INET, ip, &in);
+ tor_inet_ntop(AF_INET, &in, tmpbuf, sizeof(tmpbuf));
+ test_streq(tmpbuf, ip);
+ }
+
+ /* Test 'escaped' */
+ test_streq("\"\"", escaped(""));
+ test_streq("\"abcd\"", escaped("abcd"));
+ test_streq("\"\\\\\\n\\r\\t\\\"\\'\"", escaped("\\\n\r\t\"\'"));
+ test_streq("\"z\\001abc\\277d\"", escaped("z\001abc\277d"));
+ test_assert(NULL == escaped(NULL));
+
+ /* Test strndup and memdup */
+ {
+ const char *s = "abcdefghijklmnopqrstuvwxyz";
+ cp = tor_strndup(s, 30);
+ test_streq(cp, s); /* same string, */
+ test_neq(cp, s); /* but different pointers. */
+ tor_free(cp);
+
+ cp = tor_strndup(s, 5);
+ test_streq(cp, "abcde");
+ tor_free(cp);
+
+ s = "a\0b\0c\0d\0e\0";
+ cp = tor_memdup(s,10);
+ test_memeq(cp, s, 10); /* same ram, */
+ test_neq(cp, s); /* but different pointers. */
+ tor_free(cp);
+ }
+
+ /* Test str-foo functions */
+ cp = tor_strdup("abcdef");
+ test_assert(tor_strisnonupper(cp));
+ cp[3] = 'D';
+ test_assert(!tor_strisnonupper(cp));
+ tor_strupper(cp);
+ test_streq(cp, "ABCDEF");
+ test_assert(tor_strisprint(cp));
+ cp[3] = 3;
+ test_assert(!tor_strisprint(cp));
+ tor_free(cp);
+
+ /* Test eat_whitespace. */
+ {
+ const char *s = " \n a";
+ test_eq_ptr(eat_whitespace(s), s+4);
+ s = "abcd";
+ test_eq_ptr(eat_whitespace(s), s);
+ s = "#xyz\nab";
+ test_eq_ptr(eat_whitespace(s), s+5);
+ }
+
+ /* Test memmem and memstr */
+ {
+ const char *haystack = "abcde";
+ tor_assert(!tor_memmem(haystack, 5, "ef", 2));
+ test_eq_ptr(tor_memmem(haystack, 5, "cd", 2), haystack + 2);
+ test_eq_ptr(tor_memmem(haystack, 5, "cde", 3), haystack + 2);
+ haystack = "ababcad";
+ test_eq_ptr(tor_memmem(haystack, 7, "abc", 3), haystack + 2);
+ test_eq_ptr(tor_memstr(haystack, 7, "abc"), haystack + 2);
+ test_assert(!tor_memstr(haystack, 7, "fe"));
+ test_assert(!tor_memstr(haystack, 7, "longerthantheoriginal"));
+ }
+
+ /* Test wrap_string */
+ {
+ smartlist_t *sl = smartlist_create();
+ wrap_string(sl, "This is a test of string wrapping functionality: woot.",
+ 10, "", "");
+ cp = smartlist_join_strings(sl, "", 0, NULL);
+ test_streq(cp,
+ "This is a\ntest of\nstring\nwrapping\nfunctional\nity: woot.\n");
+ tor_free(cp);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_clear(sl);
+
+ wrap_string(sl, "This is a test of string wrapping functionality: woot.",
+ 16, "### ", "# ");
+ cp = smartlist_join_strings(sl, "", 0, NULL);
+ test_streq(cp,
+ "### This is a\n# test of string\n# wrapping\n# functionality:\n"
+ "# woot.\n");
+
+ tor_free(cp);
+ SMARTLIST_FOREACH(sl, char *, cp, tor_free(cp));
+ smartlist_free(sl);
+ }
+
+ /* now make sure time works. */
+ tor_gettimeofday(&end);
+ /* We might've timewarped a little. */
+ test_assert(tv_udiff(&start, &end) >= -5000);
+
+ /* Test tor_log2(). */
+ test_eq(tor_log2(64), 6);
+ test_eq(tor_log2(65), 6);
+ test_eq(tor_log2(63), 5);
+ test_eq(tor_log2(1), 0);
+ test_eq(tor_log2(2), 1);
+ test_eq(tor_log2(3), 1);
+ test_eq(tor_log2(4), 2);
+ test_eq(tor_log2(5), 2);
+ test_eq(tor_log2(U64_LITERAL(40000000000000000)), 55);
+ test_eq(tor_log2(UINT64_MAX), 63);
+
+ /* Test round_to_power_of_2 */
+ test_eq(round_to_power_of_2(120), 128);
+ test_eq(round_to_power_of_2(128), 128);
+ test_eq(round_to_power_of_2(130), 128);
+ test_eq(round_to_power_of_2(U64_LITERAL(40000000000000000)),
+ U64_LITERAL(1)<<55);
+ test_eq(round_to_power_of_2(0), 2);
+
+ done:
+ ;
+}
+
+/** mutex for thread test to stop the threads hitting data at the same time. */
+static tor_mutex_t *_thread_test_mutex = NULL;
+/** mutexes for the thread test to make sure that the threads have to
+ * interleave somewhat. */
+static tor_mutex_t *_thread_test_start1 = NULL,
+ *_thread_test_start2 = NULL;
+/** Shared strmap for the thread test. */
+static strmap_t *_thread_test_strmap = NULL;
+/** The name of thread1 for the thread test */
+static char *_thread1_name = NULL;
+/** The name of thread2 for the thread test */
+static char *_thread2_name = NULL;
+
+static void _thread_test_func(void* _s) ATTR_NORETURN;
+
+/** How many iterations have the threads in the unit test run? */
+static int t1_count = 0, t2_count = 0;
+
+/** Helper function for threading unit tests: This function runs in a
+ * subthread. It grabs its own mutex (start1 or start2) to make sure that it
+ * should start, then it repeatedly alters _test_thread_strmap protected by
+ * _thread_test_mutex. */
+static void
+_thread_test_func(void* _s)
+{
+ char *s = _s;
+ int i, *count;
+ tor_mutex_t *m;
+ char buf[64];
+ char **cp;
+ if (!strcmp(s, "thread 1")) {
+ m = _thread_test_start1;
+ cp = &_thread1_name;
+ count = &t1_count;
+ } else {
+ m = _thread_test_start2;
+ cp = &_thread2_name;
+ count = &t2_count;
+ }
+ tor_mutex_acquire(m);
+
+ tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
+ *cp = tor_strdup(buf);
+
+ for (i=0; i<10000; ++i) {
+ tor_mutex_acquire(_thread_test_mutex);
+ strmap_set(_thread_test_strmap, "last to run", *cp);
+ ++*count;
+ tor_mutex_release(_thread_test_mutex);
+ }
+ tor_mutex_acquire(_thread_test_mutex);
+ strmap_set(_thread_test_strmap, s, *cp);
+ tor_mutex_release(_thread_test_mutex);
+
+ tor_mutex_release(m);
+
+ spawn_exit();
+}
+
+/** Run unit tests for threading logic. */
+static void
+test_util_threads(void)
+{
+ char *s1 = NULL, *s2 = NULL;
+ int done = 0, timedout = 0;
+ time_t started;
+#ifndef TOR_IS_MULTITHREADED
+ /* Skip this test if we aren't threading. We should be threading most
+ * everywhere by now. */
+ if (1)
+ return;
+#endif
+ _thread_test_mutex = tor_mutex_new();
+ _thread_test_start1 = tor_mutex_new();
+ _thread_test_start2 = tor_mutex_new();
+ _thread_test_strmap = strmap_new();
+ s1 = tor_strdup("thread 1");
+ s2 = tor_strdup("thread 2");
+ tor_mutex_acquire(_thread_test_start1);
+ tor_mutex_acquire(_thread_test_start2);
+ spawn_func(_thread_test_func, s1);
+ spawn_func(_thread_test_func, s2);
+ tor_mutex_release(_thread_test_start2);
+ tor_mutex_release(_thread_test_start1);
+ started = time(NULL);
+ while (!done) {
+ tor_mutex_acquire(_thread_test_mutex);
+ strmap_assert_ok(_thread_test_strmap);
+ if (strmap_get(_thread_test_strmap, "thread 1") &&
+ strmap_get(_thread_test_strmap, "thread 2")) {
+ done = 1;
+ } else if (time(NULL) > started + 25) {
+ timedout = done = 1;
+ }
+ tor_mutex_release(_thread_test_mutex);
+ }
+ tor_mutex_free(_thread_test_mutex);
+
+ tor_mutex_acquire(_thread_test_start1);
+ tor_mutex_release(_thread_test_start1);
+ tor_mutex_acquire(_thread_test_start2);
+ tor_mutex_release(_thread_test_start2);
+
+ if (timedout) {
+ printf("\nTimed out: %d %d", t1_count, t2_count);
+ test_assert(strmap_get(_thread_test_strmap, "thread 1"));
+ test_assert(strmap_get(_thread_test_strmap, "thread 2"));
+ test_assert(!timedout);
+ }
+
+ /* different thread IDs. */
+ test_assert(strcmp(strmap_get(_thread_test_strmap, "thread 1"),
+ strmap_get(_thread_test_strmap, "thread 2")));
+ test_assert(!strcmp(strmap_get(_thread_test_strmap, "thread 1"),
+ strmap_get(_thread_test_strmap, "last to run")) ||
+ !strcmp(strmap_get(_thread_test_strmap, "thread 2"),
+ strmap_get(_thread_test_strmap, "last to run")));
+
+ done:
+ tor_free(s1);
+ tor_free(s2);
+ tor_free(_thread1_name);
+ tor_free(_thread2_name);
+ if (_thread_test_strmap)
+ strmap_free(_thread_test_strmap, NULL);
+ if (_thread_test_start1)
+ tor_mutex_free(_thread_test_start1);
+ if (_thread_test_start2)
+ tor_mutex_free(_thread_test_start2);
+}
+
+/** Run unit tests for compression functions */
+static void
+test_util_gzip(void)
+{
+ char *buf1=NULL, *buf2=NULL, *buf3=NULL, *cp1, *cp2;
+ const char *ccp2;
+ size_t len1, len2;
+ tor_zlib_state_t *state = NULL;
+
+ buf1 = tor_strdup("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ");
+ test_assert(detect_compression_method(buf1, strlen(buf1)) == UNKNOWN_METHOD);
+ if (is_gzip_supported()) {
+ test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ GZIP_METHOD));
+ test_assert(buf2);
+ test_assert(!memcmp(buf2, "\037\213", 2)); /* Gzip magic. */
+ test_assert(detect_compression_method(buf2, len1) == GZIP_METHOD);
+
+ test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
+ GZIP_METHOD, 1, LOG_INFO));
+ test_assert(buf3);
+ test_streq(buf1,buf3);
+
+ tor_free(buf2);
+ tor_free(buf3);
+ }
+
+ test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZLIB_METHOD));
+ test_assert(buf2);
+ test_assert(!memcmp(buf2, "\x78\xDA", 2)); /* deflate magic. */
+ test_assert(detect_compression_method(buf2, len1) == ZLIB_METHOD);
+
+ test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1,
+ ZLIB_METHOD, 1, LOG_INFO));
+ test_assert(buf3);
+ test_streq(buf1,buf3);
+
+ /* Check whether we can uncompress concatenated, compressed strings. */
+ tor_free(buf3);
+ buf2 = tor_realloc(buf2, len1*2);
+ memcpy(buf2+len1, buf2, len1);
+ test_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1*2,
+ ZLIB_METHOD, 1, LOG_INFO));
+ test_eq(len2, (strlen(buf1)+1)*2);
+ test_memeq(buf3,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZAAAAAAAAAAAAAAAAAAAZ\0",
+ (strlen(buf1)+1)*2);
+
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+
+ /* Check whether we can uncompress partial strings. */
+ buf1 =
+ tor_strdup("String with low redundancy that won't be compressed much.");
+ test_assert(!tor_gzip_compress(&buf2, &len1, buf1, strlen(buf1)+1,
+ ZLIB_METHOD));
+ tor_assert(len1>16);
+ /* when we allow an incomplete string, we should succeed.*/
+ tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
+ ZLIB_METHOD, 0, LOG_INFO));
+ buf3[len2]='\0';
+ tor_assert(len2 > 5);
+ tor_assert(!strcmpstart(buf1, buf3));
+
+ /* when we demand a complete string, this must fail. */
+ tor_free(buf3);
+ tor_assert(tor_gzip_uncompress(&buf3, &len2, buf2, len1-16,
+ ZLIB_METHOD, 1, LOG_INFO));
+ tor_assert(!buf3);
+
+ /* Now, try streaming compression. */
+ tor_free(buf1);
+ tor_free(buf2);
+ tor_free(buf3);
+ state = tor_zlib_new(1, ZLIB_METHOD);
+ tor_assert(state);
+ cp1 = buf1 = tor_malloc(1024);
+ len1 = 1024;
+ ccp2 = "ABCDEFGHIJABCDEFGHIJ";
+ len2 = 21;
+ test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 0)
+ == TOR_ZLIB_OK);
+ test_eq(len2, 0); /* Make sure we compressed it all. */
+ test_assert(cp1 > buf1);
+
+ len2 = 0;
+ cp2 = cp1;
+ test_assert(tor_zlib_process(state, &cp1, &len1, &ccp2, &len2, 1)
+ == TOR_ZLIB_DONE);
+ test_eq(len2, 0);
+ test_assert(cp1 > cp2); /* Make sure we really added something. */
+
+ tor_assert(!tor_gzip_uncompress(&buf3, &len2, buf1, 1024-len1,
+ ZLIB_METHOD, 1, LOG_WARN));
+ test_streq(buf3, "ABCDEFGHIJABCDEFGHIJ"); /*Make sure it compressed right.*/
+
+ done:
+ if (state)
+ tor_zlib_free(state);
+ tor_free(buf2);
+ tor_free(buf3);
+ tor_free(buf1);
+}
+
+/** Run unit tests for mmap() wrapper functionality. */
+static void
+test_util_mmap(void)
+{
+ char *fname1 = tor_strdup(get_fname("mapped_1"));
+ char *fname2 = tor_strdup(get_fname("mapped_2"));
+ char *fname3 = tor_strdup(get_fname("mapped_3"));
+ const size_t buflen = 17000;
+ char *buf = tor_malloc(17000);
+ tor_mmap_t *mapping = NULL;
+
+ crypto_rand(buf, buflen);
+
+ mapping = tor_mmap_file(fname1);
+ test_assert(! mapping);
+
+ write_str_to_file(fname1, "Short file.", 1);
+ write_bytes_to_file(fname2, buf, buflen, 1);
+ write_bytes_to_file(fname3, buf, 16384, 1);
+
+ mapping = tor_mmap_file(fname1);
+ test_assert(mapping);
+ test_eq(mapping->size, strlen("Short file."));
+ test_streq(mapping->data, "Short file.");
+#ifdef MS_WINDOWS
+ tor_munmap_file(mapping);
+ mapping = NULL;
+ test_assert(unlink(fname1) == 0);
+#else
+ /* make sure we can unlink. */
+ test_assert(unlink(fname1) == 0);
+ test_streq(mapping->data, "Short file.");
+ tor_munmap_file(mapping);
+ mapping = NULL;
+#endif
+
+ /* Now a zero-length file. */
+ write_str_to_file(fname1, "", 1);
+ mapping = tor_mmap_file(fname1);
+ test_eq(mapping, NULL);
+ test_eq(ERANGE, errno);
+ unlink(fname1);
+
+ /* Make sure that we fail to map a no-longer-existent file. */
+ mapping = tor_mmap_file(fname1);
+ test_assert(mapping == NULL);
+
+ /* Now try a big file that stretches across a few pages and isn't aligned */
+ mapping = tor_mmap_file(fname2);
+ test_assert(mapping);
+ test_eq(mapping->size, buflen);
+ test_memeq(mapping->data, buf, buflen);
+ tor_munmap_file(mapping);
+ mapping = NULL;
+
+ /* Now try a big aligned file. */
+ mapping = tor_mmap_file(fname3);
+ test_assert(mapping);
+ test_eq(mapping->size, 16384);
+ test_memeq(mapping->data, buf, 16384);
+ tor_munmap_file(mapping);
+ mapping = NULL;
+
+ done:
+ unlink(fname1);
+ unlink(fname2);
+ unlink(fname3);
+
+ tor_free(fname1);
+ tor_free(fname2);
+ tor_free(fname3);
+ tor_free(buf);
+
+ if (mapping)
+ tor_munmap_file(mapping);
+}
+
+/** Run unit tests for escaping/unescaping data for use by controllers. */
+static void
+test_util_control_formats(void)
+{
+ char *out = NULL;
+ const char *inp =
+ "..This is a test\r\nof the emergency \nbroadcast\r\n..system.\r\nZ.\r\n";
+ size_t sz;
+
+ sz = read_escaped_data(inp, strlen(inp), &out);
+ test_streq(out,
+ ".This is a test\nof the emergency \nbroadcast\n.system.\nZ.\n");
+ test_eq(sz, strlen(out));
+
+ done:
+ tor_free(out);
+}
+
+static void
+test_util_sscanf(void)
+{
+ unsigned u1, u2, u3;
+ char s1[10], s2[10], s3[10], ch;
+ int r;
+
+ r = tor_sscanf("hello world", "hello world"); /* String match: success */
+ test_eq(r, 0);
+ r = tor_sscanf("hello world 3", "hello worlb %u", &u1); /* String fail */
+ test_eq(r, 0);
+ r = tor_sscanf("12345", "%u", &u1); /* Simple number */
+ test_eq(r, 1);
+ test_eq(u1, 12345u);
+ r = tor_sscanf("", "%u", &u1); /* absent number */
+ test_eq(r, 0);
+ r = tor_sscanf("A", "%u", &u1); /* bogus number */
+ test_eq(r, 0);
+ r = tor_sscanf("4294967295", "%u", &u1); /* UINT32_MAX should work. */
+ test_eq(r, 1);
+ test_eq(u1, 4294967295u);
+ r = tor_sscanf("4294967296", "%u", &u1); /* Always say -1 at 32 bits. */
+ test_eq(r, 0);
+ r = tor_sscanf("123456", "%2u%u", &u1, &u2); /* Width */
+ test_eq(r, 2);
+ test_eq(u1, 12u);
+ test_eq(u2, 3456u);
+ r = tor_sscanf("!12:3:456", "!%2u:%2u:%3u", &u1, &u2, &u3); /* separators */
+ test_eq(r, 3);
+ test_eq(u1, 12u);
+ test_eq(u2, 3u);
+ test_eq(u3, 456u);
+ r = tor_sscanf("12:3:045", "%2u:%2u:%3u", &u1, &u2, &u3); /* 0s */
+ test_eq(r, 3);
+ test_eq(u1, 12u);
+ test_eq(u2, 3u);
+ test_eq(u3, 45u);
+ /* %u does not match space.*/
+ r = tor_sscanf("12:3: 45", "%2u:%2u:%3u", &u1, &u2, &u3);
+ test_eq(r, 2);
+ /* %u does not match negative numbers. */
+ r = tor_sscanf("12:3:-4", "%2u:%2u:%3u", &u1, &u2, &u3);
+ test_eq(r, 2);
+ /* Arbitrary amounts of 0-padding are okay */
+ r = tor_sscanf("12:03:000000000000000099", "%2u:%2u:%u", &u1, &u2, &u3);
+ test_eq(r, 3);
+ test_eq(u1, 12u);
+ test_eq(u2, 3u);
+ test_eq(u3, 99u);
+
+ r = tor_sscanf("99% fresh", "%3u%% fresh", &u1); /* percents are scannable.*/
+ test_eq(r, 1);
+ test_eq(u1, 99);
+
+ r = tor_sscanf("hello", "%s", s1); /* %s needs a number. */
+ test_eq(r, -1);
+
+ r = tor_sscanf("hello", "%3s%7s", s1, s2); /* %s matches characters. */
+ test_eq(r, 2);
+ test_streq(s1, "hel");
+ test_streq(s2, "lo");
+ r = tor_sscanf("WD40", "%2s%u", s3, &u1); /* %s%u */
+ test_eq(r, 2);
+ test_streq(s3, "WD");
+ test_eq(u1, 40);
+ r = tor_sscanf("76trombones", "%6u%9s", &u1, s1); /* %u%s */
+ test_eq(r, 2);
+ test_eq(u1, 76);
+ test_streq(s1, "trombones");
+ r = tor_sscanf("hello world", "%9s %9s", s1, s2); /* %s doesn't eat space. */
+ test_eq(r, 2);
+ test_streq(s1, "hello");
+ test_streq(s2, "world");
+ r = tor_sscanf("hi", "%9s%9s%3s", s1, s2, s3); /* %s can be empty. */
+ test_eq(r, 3);
+ test_streq(s1, "hi");
+ test_streq(s2, "");
+ test_streq(s3, "");
+
+ r = tor_sscanf("1.2.3", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
+ test_eq(r, 3);
+ r = tor_sscanf("1.2.3 foobar", "%u.%u.%u%c", &u1, &u2, &u3, &ch);
+ test_eq(r, 4);
+
+ done:
+ ;
+}
+
+/** Run unittests for memory pool allocator */
+static void
+test_util_mempool(void)
+{
+ mp_pool_t *pool = NULL;
+ smartlist_t *allocated = NULL;
+ int i;
+
+ pool = mp_pool_new(1, 100);
+ test_assert(pool);
+ test_assert(pool->new_chunk_capacity >= 100);
+ test_assert(pool->item_alloc_size >= sizeof(void*)+1);
+ mp_pool_destroy(pool);
+ pool = NULL;
+
+ pool = mp_pool_new(241, 2500);
+ test_assert(pool);
+ test_assert(pool->new_chunk_capacity >= 10);
+ test_assert(pool->item_alloc_size >= sizeof(void*)+241);
+ test_eq(pool->item_alloc_size & 0x03, 0);
+ test_assert(pool->new_chunk_capacity < 60);
+
+ allocated = smartlist_create();
+ for (i = 0; i < 20000; ++i) {
+ if (smartlist_len(allocated) < 20 || crypto_rand_int(2)) {
+ void *m = mp_pool_get(pool);
+ memset(m, 0x09, 241);
+ smartlist_add(allocated, m);
+ //printf("%d: %p\n", i, m);
+ //mp_pool_assert_ok(pool);
+ } else {
+ int idx = crypto_rand_int(smartlist_len(allocated));
+ void *m = smartlist_get(allocated, idx);
+ //printf("%d: free %p\n", i, m);
+ smartlist_del(allocated, idx);
+ mp_pool_release(m);
+ //mp_pool_assert_ok(pool);
+ }
+ if (crypto_rand_int(777)==0)
+ mp_pool_clean(pool, 1, 1);
+
+ if (i % 777)
+ mp_pool_assert_ok(pool);
+ }
+
+ done:
+ if (allocated) {
+ SMARTLIST_FOREACH(allocated, void *, m, mp_pool_release(m));
+ mp_pool_assert_ok(pool);
+ mp_pool_clean(pool, 0, 0);
+ mp_pool_assert_ok(pool);
+ smartlist_free(allocated);
+ }
+
+ if (pool)
+ mp_pool_destroy(pool);
+}
+
+/** Run unittests for memory area allocator */
+static void
+test_util_memarea(void)
+{
+ memarea_t *area = memarea_new();
+ char *p1, *p2, *p3, *p1_orig;
+ void *malloced_ptr = NULL;
+ int i;
+
+ test_assert(area);
+
+ p1_orig = p1 = memarea_alloc(area,64);
+ p2 = memarea_alloc_zero(area,52);
+ p3 = memarea_alloc(area,11);
+
+ test_assert(memarea_owns_ptr(area, p1));
+ test_assert(memarea_owns_ptr(area, p2));
+ test_assert(memarea_owns_ptr(area, p3));
+ /* Make sure we left enough space. */
+ test_assert(p1+64 <= p2);
+ test_assert(p2+52 <= p3);
+ /* Make sure we aligned. */
+ test_eq(((uintptr_t)p1) % sizeof(void*), 0);
+ test_eq(((uintptr_t)p2) % sizeof(void*), 0);
+ test_eq(((uintptr_t)p3) % sizeof(void*), 0);
+ test_assert(!memarea_owns_ptr(area, p3+8192));
+ test_assert(!memarea_owns_ptr(area, p3+30));
+ test_assert(tor_mem_is_zero(p2, 52));
+ /* Make sure we don't overalign. */
+ p1 = memarea_alloc(area, 1);
+ p2 = memarea_alloc(area, 1);
+ test_eq(p1+sizeof(void*), p2);
+ {
+ malloced_ptr = tor_malloc(64);
+ test_assert(!memarea_owns_ptr(area, malloced_ptr));
+ tor_free(malloced_ptr);
+ }
+
+ /* memarea_memdup */
+ {
+ malloced_ptr = tor_malloc(64);
+ crypto_rand((char*)malloced_ptr, 64);
+ p1 = memarea_memdup(area, malloced_ptr, 64);
+ test_assert(p1 != malloced_ptr);
+ test_memeq(p1, malloced_ptr, 64);
+ tor_free(malloced_ptr);
+ }
+
+ /* memarea_strdup. */
+ p1 = memarea_strdup(area,"");
+ p2 = memarea_strdup(area, "abcd");
+ test_assert(p1);
+ test_assert(p2);
+ test_streq(p1, "");
+ test_streq(p2, "abcd");
+
+ /* memarea_strndup. */
+ {
+ const char *s = "Ad ogni porta batte la morte e grida: il nome!";
+ /* (From Turandot, act 3.) */
+ size_t len = strlen(s);
+ p1 = memarea_strndup(area, s, 1000);
+ p2 = memarea_strndup(area, s, 10);
+ test_streq(p1, s);
+ test_assert(p2 >= p1 + len + 1);
+ test_memeq(s, p2, 10);
+ test_eq(p2[10], '\0');
+ p3 = memarea_strndup(area, s, len);
+ test_streq(p3, s);
+ p3 = memarea_strndup(area, s, len-1);
+ test_memeq(s, p3, len-1);
+ test_eq(p3[len-1], '\0');
+ }
+
+ memarea_clear(area);
+ p1 = memarea_alloc(area, 1);
+ test_eq(p1, p1_orig);
+ memarea_clear(area);
+
+ /* Check for running over an area's size. */
+ for (i = 0; i < 512; ++i) {
+ p1 = memarea_alloc(area, crypto_rand_int(5)+1);
+ test_assert(memarea_owns_ptr(area, p1));
+ }
+ memarea_assert_ok(area);
+ /* Make sure we can allocate a too-big object. */
+ p1 = memarea_alloc_zero(area, 9000);
+ p2 = memarea_alloc_zero(area, 16);
+ test_assert(memarea_owns_ptr(area, p1));
+ test_assert(memarea_owns_ptr(area, p2));
+
+ done:
+ memarea_drop_all(area);
+ tor_free(malloced_ptr);
+}
+
+/** Run unit tests for utility functions to get file names relative to
+ * the data directory. */
+static void
+test_util_datadir(void)
+{
+ char buf[1024];
+ char *f = NULL;
+ char *temp_dir = NULL;
+
+ temp_dir = get_datadir_fname(NULL);
+ f = get_datadir_fname("state");
+ tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"state", temp_dir);
+ test_streq(f, buf);
+ tor_free(f);
+ f = get_datadir_fname2("cache", "thingy");
+ tor_snprintf(buf, sizeof(buf),
+ "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy", temp_dir);
+ test_streq(f, buf);
+ tor_free(f);
+ f = get_datadir_fname2_suffix("cache", "thingy", ".foo");
+ tor_snprintf(buf, sizeof(buf),
+ "%s"PATH_SEPARATOR"cache"PATH_SEPARATOR"thingy.foo", temp_dir);
+ test_streq(f, buf);
+ tor_free(f);
+ f = get_datadir_fname_suffix("cache", ".foo");
+ tor_snprintf(buf, sizeof(buf), "%s"PATH_SEPARATOR"cache.foo",
+ temp_dir);
+ test_streq(f, buf);
+
+ done:
+ tor_free(f);
+ tor_free(temp_dir);
+}
+
+static void
+test_util_strtok(void)
+{
+ char buf[128];
+ char buf2[128];
+ char *cp1, *cp2;
+ strlcpy(buf, "Graved on the dark in gestures of descent", sizeof(buf));
+ strlcpy(buf2, "they.seemed;their!own;most.perfect;monument", sizeof(buf2));
+ /* -- "Year's End", Richard Wilbur */
+
+ test_streq("Graved", tor_strtok_r_impl(buf, " ", &cp1));
+ test_streq("they", tor_strtok_r_impl(buf2, ".!..;!", &cp2));
+#define S1() tor_strtok_r_impl(NULL, " ", &cp1)
+#define S2() tor_strtok_r_impl(NULL, ".!..;!", &cp2)
+ test_streq("on", S1());
+ test_streq("the", S1());
+ test_streq("dark", S1());
+ test_streq("seemed", S2());
+ test_streq("their", S2());
+ test_streq("own", S2());
+ test_streq("in", S1());
+ test_streq("gestures", S1());
+ test_streq("of", S1());
+ test_streq("most", S2());
+ test_streq("perfect", S2());
+ test_streq("descent", S1());
+ test_streq("monument", S2());
+ test_assert(NULL == S1());
+ test_assert(NULL == S2());
+ done:
+ ;
+}
+
+#define UTIL_LEGACY(name) \
+ { #name, legacy_test_helper, 0, &legacy_setup, test_util_ ## name }
+
+struct testcase_t util_tests[] = {
+ UTIL_LEGACY(misc),
+ UTIL_LEGACY(gzip),
+ UTIL_LEGACY(datadir),
+ UTIL_LEGACY(mempool),
+ UTIL_LEGACY(memarea),
+ UTIL_LEGACY(control_formats),
+ UTIL_LEGACY(mmap),
+ UTIL_LEGACY(threads),
+ UTIL_LEGACY(sscanf),
+ UTIL_LEGACY(strtok),
+ END_OF_TESTCASES
+};
--
1.5.6.5