[tor-commits] [obfsproxy/master] Add obfs_asprintf() unit tests.

asn at torproject.org asn at torproject.org
Mon Mar 19 19:51:43 UTC 2012


commit 48c54ccec2ab50407fd4ee40f618c90ff23e4c1f
Author: George Kadianakis <desnacked at riseup.net>
Date:   Wed Feb 22 12:21:49 2012 -0800

    Add obfs_asprintf() unit tests.
    
    Take tor_asprintf() unit tests off
    tor.git:src/tests/test_util.c:test_util_asprintf().
    
    Replace 'tor_asprintf' with 'obfs_asprintf', and 'tor_free(x);' with
    'free(x); x=NULL;'.
---
 Makefile.am              |    3 +-
 src/test/unittest.c      |    2 +
 src/test/unittest_util.c |   59 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+), 1 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index a703a99..a230b95 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -36,7 +36,8 @@ unittests_SOURCES = \
 	src/test/unittest_socks.c \
 	src/test/unittest_dummy.c \
 	src/test/unittest_managed.c \
-	src/test/unittest_obfs2.c
+	src/test/unittest_obfs2.c \
+	src/test/unittest_util.c
 
 noinst_HEADERS = \
 	src/container.h \
diff --git a/src/test/unittest.c b/src/test/unittest.c
index 26d5566..f871924 100644
--- a/src/test/unittest.c
+++ b/src/test/unittest.c
@@ -11,6 +11,7 @@ extern struct testcase_t socks_tests[];
 extern struct testcase_t dummy_tests[];
 extern struct testcase_t obfs2_tests[];
 extern struct testcase_t managed_tests[];
+extern struct testcase_t util_tests[];
 
 struct testgroup_t groups[] = {
   { "container/", container_tests },
@@ -19,6 +20,7 @@ struct testgroup_t groups[] = {
   { "dummy/", dummy_tests },
   { "obfs2/", obfs2_tests },
   { "managed/", managed_tests },
+  { "util/", util_tests },
   END_OF_GROUPS
 };
 
diff --git a/src/test/unittest_util.c b/src/test/unittest_util.c
new file mode 100644
index 0000000..8aafa58
--- /dev/null
+++ b/src/test/unittest_util.c
@@ -0,0 +1,59 @@
+/* Copyright 2011 Nick Mathewson, George Kadianakis
+   See LICENSE for other credits and copying information
+*/
+
+#include "util.h"
+#include "protocol.h"
+#include "tinytest_macros.h"
+
+static void
+test_util_asprintf(void *unused)
+{
+#define LOREMIPSUM                                              \
+  "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
+  char *cp=NULL, *cp2=NULL;
+  int r;
+
+  /* empty string. */
+  r = obfs_asprintf(&cp, "%s", "");
+  tt_assert(cp);
+  tt_int_op(r, ==, strlen(cp));
+  tt_str_op(cp, ==, "");
+
+  /* Short string with some printing in it. */
+  r = obfs_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
+  tt_assert(cp2);
+  tt_int_op(r, ==, strlen(cp2));
+  tt_str_op(cp2, ==, "First=101, Second=202");
+  tt_assert(cp != cp2);
+  free(cp); cp = NULL;
+  free(cp2); cp2 = NULL;
+
+  /* Glass-box test: a string exactly 128 characters long. */
+  r = obfs_asprintf(&cp, "Lorem1: %sLorem2: %s", LOREMIPSUM, LOREMIPSUM);
+  tt_assert(cp);
+  tt_int_op(r, ==, 128);
+  tt_assert(cp[128] == '\0');
+  tt_str_op(cp, ==,
+            "Lorem1: "LOREMIPSUM"Lorem2: "LOREMIPSUM);
+  free(cp); cp = NULL;
+
+  /* String longer than 128 characters */
+  r = obfs_asprintf(&cp, "1: %s 2: %s 3: %s",
+                   LOREMIPSUM, LOREMIPSUM, LOREMIPSUM);
+  tt_assert(cp);
+  tt_int_op(r, ==, strlen(cp));
+  tt_str_op(cp, ==, "1: "LOREMIPSUM" 2: "LOREMIPSUM" 3: "LOREMIPSUM);
+
+ end:
+  free(cp);
+  free(cp2);
+}
+
+#define T(name) \
+  { #name, test_util_##name, 0, NULL, NULL }
+
+struct testcase_t util_tests[] = {
+  T(asprintf),
+  END_OF_TESTCASES
+};





More information about the tor-commits mailing list