[tor-commits] [tor/master] Unit tests for tor_addr_to_str

nickm at torproject.org nickm at torproject.org
Fri Nov 11 16:08:46 UTC 2011


commit ca1e88a0db2029ca9b1603d27a2c42fa75583790
Author: Anders Sundman <anders at 4zm.org>
Date:   Fri Nov 11 07:55:20 2011 +0100

    Unit tests for tor_addr_to_str
---
 src/test/test_addr.c |   36 +++++++++++++++++++++++++++++++++++-
 1 files changed, 35 insertions(+), 1 deletions(-)

diff --git a/src/test/test_addr.c b/src/test/test_addr.c
index ec59e5b..b879efd 100644
--- a/src/test/test_addr.c
+++ b/src/test/test_addr.c
@@ -394,7 +394,15 @@ test_addr_ip6_helpers(void)
   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 undecorated tor_addr_to_str */
+  test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
+  p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
+  test_streq(p1, "123:45:6789::5005:11");
+  test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
+  p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 0);
+  test_streq(p1, "18.0.0.1");
+
+  /* Test decorated tor_addr_to_str */
   test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
   test_streq(p1, "[123:45:6789::5005:11]");
@@ -402,6 +410,32 @@ test_addr_ip6_helpers(void)
   p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
   test_streq(p1, "18.0.0.1");
 
+  /* Test buffer bounds checking of tor_addr_to_str */
+  test_eq(AF_INET6, tor_addr_parse(&t1, "::")); /* 2 + \0 */
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 2, 0), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 3, 0), "::");
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 4, 1), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 5, 1), "[::]");
+
+  test_eq(AF_INET6, tor_addr_parse(&t1, "2000::1337")); /* 10 + \0 */
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 10, 0), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 11, 0), "2000::1337");
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 12, 1), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 13, 1), "[2000::1337]");
+
+  test_eq(AF_INET, tor_addr_parse(&t1, "1.2.3.4")); /* 7 + \0 */
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 7, 0), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 8, 0), "1.2.3.4");
+
+  test_eq(AF_INET, tor_addr_parse(&t1, "255.255.255.255")); /* 15 + \0 */
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 0), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 16, 0), "255.255.255.255");
+  test_eq_ptr(tor_addr_to_str(buf, &t1, 15, 1), NULL); /* too short buf */
+  test_streq(tor_addr_to_str(buf, &t1, 16, 1), "255.255.255.255");
+
+  t1.family = AF_UNSPEC;
+  test_eq_ptr(tor_addr_to_str(buf, &t1, sizeof(buf), 0), NULL);
+
   /* Test tor_addr_parse_PTR_name */
   i = tor_addr_parse_PTR_name(&t1, "Foobar.baz", AF_UNSPEC, 0);
   test_eq(0, i);





More information about the tor-commits mailing list