[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [torsocks/master] Tests: add onion pool subsystem unit test



commit 1ed979d7f08eef29c51570a88517551e41e42732
Author: David Goulet <dgoulet@xxxxxxxxx>
Date:   Fri Aug 30 20:15:20 2013 -0400

    Tests: add onion pool subsystem unit test
    
    Signed-off-by: David Goulet <dgoulet@xxxxxxxxx>
---
 .gitignore              |    1 +
 configure.ac            |    1 +
 tests/Makefile.am       |    2 +-
 tests/test_list         |    1 +
 tests/unit/Makefile.am  |   10 +++
 tests/unit/test_onion.c |  175 +++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 189 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 6e728a0..a293c55 100644
--- a/.gitignore
+++ b/.gitignore
@@ -42,3 +42,4 @@ doc/usewithtor.1
 src/bin/torsocks
 
 tests/test_dns
+tests/unit/test_onion
diff --git a/configure.ac b/configure.ac
index 8f2f392..6c89912 100644
--- a/configure.ac
+++ b/configure.ac
@@ -360,6 +360,7 @@ AC_CONFIG_FILES([
 	src/common/Makefile
 	src/lib/Makefile
 	tests/Makefile
+	tests/unit/Makefile
 	tests/utils/Makefile
 	tests/utils/tap/Makefile
 	doc/Makefile
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 66f3dde..18eb5a9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,4 +1,4 @@
-SUBDIRS = utils
+SUBDIRS = utils unit
 
 AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_srcdir)/tests/utils/ -I$(srcdir)
 
diff --git a/tests/test_list b/tests/test_list
index 963e59b..ee705d6 100644
--- a/tests/test_list
+++ b/tests/test_list
@@ -1 +1,2 @@
 ./test_dns
+./unit/test_onion
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
new file mode 100644
index 0000000..ad8b361
--- /dev/null
+++ b/tests/unit/Makefile.am
@@ -0,0 +1,10 @@
+AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(top_srcdir)/tests/utils/ -I$(srcdir)
+
+LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
+
+LIBCOMMON=$(top_builddir)/src/common/libcommon.la
+
+noinst_PROGRAMS = test_onion
+
+test_onion_SOURCES = test_onion.c
+test_onion_LDADD = $(LIBTAP) $(LIBCOMMON)
diff --git a/tests/unit/test_onion.c b/tests/unit/test_onion.c
new file mode 100644
index 0000000..0c91f71
--- /dev/null
+++ b/tests/unit/test_onion.c
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2013 - David Goulet <dgoulet@xxxxxxxxx>
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License, version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <stdio.h>
+#include <sys/socket.h>
+
+#include <common/onion.h>
+#include <common/defaults.h>
+
+#include <tap/tap.h>
+
+#define NUM_TESTS 12
+
+static void test_onion_entry(struct onion_pool *pool)
+{
+	int ret;
+	struct onion_entry *entry;
+	const char *onion_addr1 = "87idq6tnejk5plpn.onion";
+	const char *onion_addr2 = "97idq6tnejk5plpn.onion";
+	const char *onion_addr1_typo = "87idq6tnejk5plpn.onio";
+
+	diag("Onion entry subsystem initialization test");
+
+	/* Create valid onion pool from default values. */
+	ret = onion_pool_init(pool, inet_addr(DEFAULT_ONION_ADDR_RANGE),
+			(uint8_t) atoi(DEFAULT_ONION_ADDR_MASK));
+	ok(ret == 0 &&
+		pool->base == 0 &&
+		pool->max_pos == 255 &&
+		pool->size == 8 &&
+		pool->count == 0 &&
+		pool->next_entry_pos == 0,
+		"Valid onion pool created");
+
+	entry = onion_entry_create(pool, onion_addr1);
+	ok(entry &&
+		pool->count == 1 &&
+		pool->next_entry_pos == 1 &&
+		strcmp(entry->hostname, onion_addr1) == 0 &&
+		strcmp(DEFAULT_ONION_ADDR_RANGE,
+			inet_ntoa(*((struct in_addr *) &entry->ip))) == 0,
+		"Valid onion entry %s created", onion_addr1);
+
+	entry = onion_entry_find_by_name("meh", pool);
+	ok(!entry, "Onion entry not found");
+
+	entry = onion_entry_find_by_name(onion_addr1_typo, pool);
+	ok(!entry, "Onion entry with typo not found");
+
+	entry = onion_entry_find_by_name(onion_addr1, pool);
+	ok(entry &&
+		pool->count == 1 &&
+		strcmp(entry->hostname, onion_addr1) == 0 &&
+		strcmp(DEFAULT_ONION_ADDR_RANGE,
+			inet_ntoa(*((struct in_addr *) &entry->ip))) == 0,
+		"Valid onion entry found by name");
+
+	entry = onion_entry_find_by_ip(inet_addr(DEFAULT_ONION_ADDR_RANGE), pool);
+	ok(entry &&
+		pool->count == 1 &&
+		strcmp(entry->hostname, onion_addr1) == 0 &&
+		strcmp(DEFAULT_ONION_ADDR_RANGE,
+			inet_ntoa(*((struct in_addr *) &entry->ip))) == 0,
+		"Valid onion entry found by IP");
+
+	entry = onion_entry_create(pool, onion_addr2);
+	ok(entry &&
+		pool->count == 2 &&
+		pool->next_entry_pos == 2 &&
+		strcmp(entry->hostname, onion_addr2) == 0 &&
+		strcmp("127.42.42.1",
+			inet_ntoa(*((struct in_addr *) &entry->ip))) == 0,
+		"Valid onion entry %s created", onion_addr2);
+
+	onion_pool_destroy(pool);
+}
+
+static void test_onion_init(struct onion_pool *pool)
+{
+	int ret;
+	uint8_t mask;
+	in_addr_t base;
+
+	diag("Onion subsystem initialization test");
+
+	/* Valid default configuration test. */
+	base = inet_addr(DEFAULT_ONION_ADDR_RANGE);
+	mask = (uint8_t) atoi(DEFAULT_ONION_ADDR_MASK);
+	ret = onion_pool_init(pool, base, mask);
+	ok(ret == 0 &&
+		pool->entries &&
+		pool->base == 0 &&
+		pool->max_pos == 255 &&
+		pool->size == 8 &&
+		pool->count == 0 &&
+		pool->next_entry_pos == 0,
+		"Valid onion pool of %s/%d", DEFAULT_ONION_ADDR_RANGE, mask);
+
+	/* Valid test. */
+	base = inet_addr("127.42.42.64");
+	mask = 27;
+	ret = onion_pool_init(pool, base, mask);
+	ok(ret == 0 &&
+		pool->entries &&
+		pool->base == 64 &&
+		pool->max_pos == 95 &&
+		pool->size == 8 &&
+		pool->count == 0 &&
+		pool->next_entry_pos == 0,
+		"Valid onion pool of 127.42.42.64/27");
+
+	/* Valid test. */
+	base = inet_addr("127.42.42.64");
+	mask = 17;
+	ret = onion_pool_init(pool, base, mask);
+	ok(ret == 0 &&
+		pool->entries &&
+		pool->base == 0 &&
+		pool->max_pos == 32767 &&
+		pool->size == 8 &&
+		pool->count == 0 &&
+		pool->next_entry_pos == 0,
+		"Valid onion pool of 127.42.42.64/17");
+
+	/* Valid test with size less than default. */
+	base = inet_addr("127.42.42.0");
+	mask = 32;
+	ret = onion_pool_init(pool, base, mask);
+	ok(ret == 0 &&
+		pool->entries &&
+		pool->base == 0 &&
+		pool->max_pos == 0 &&
+		pool->size == 1 &&
+		pool->count == 0 &&
+		pool->next_entry_pos == 0,
+		"Valid onion pool of 127.42.42.0/32");
+
+	/* Invalid test. */
+	base = inet_addr("127.42.42.64");
+	mask = 42;
+	ret = onion_pool_init(pool, base, mask);
+	ok(ret == -EINVAL,
+		"Invalid onion pool of mask 42");
+
+	onion_pool_destroy(pool);
+}
+
+int main(int argc, char **argv)
+{
+	struct onion_pool pool;
+
+	/* Libtap call for the number of tests planned. */
+	plan_tests(NUM_TESTS);
+
+	test_onion_init(&pool);
+	test_onion_entry(&pool);
+
+    return 0;
+}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits