[tor-commits] [torsocks/master] Add DNS resolution test

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:26 UTC 2014


commit 3f0e24d841c80ff92c6d6f339045df2ec4f5ba42
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Sat Aug 24 13:50:09 2013 -0400

    Add DNS resolution test
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 .gitignore        |    2 +-
 tests/Makefile.am |   11 +++++
 tests/test_dns.c  |  138 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 150 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index 7f13a81..6e728a0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -41,4 +41,4 @@ doc/usewithtor.1
 
 src/bin/torsocks
 
-test/test_torsocks
+tests/test_dns
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 97b7b2b..e0a3fba 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1 +1,12 @@
 SUBDIRS = utils
+
+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
+
+LIBTORSOCKS=$(top_builddir)/src/lib/libtorsocks.la
+
+noinst_PROGRAMS = test_dns
+
+test_dns_SOURCES = test_dns.c
+test_dns_LDADD = $(LIBTAP) $(LIBTORSOCKS)
diff --git a/tests/test_dns.c b/tests/test_dns.c
new file mode 100644
index 0000000..01bb5d9
--- /dev/null
+++ b/tests/test_dns.c
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2013 - David Goulet <dgoulet at ev0ke.net>
+ *
+ * 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 <lib/torsocks.h>
+
+#include <tap/tap.h>
+
+#define NUM_TESTS 3
+
+struct test_host {
+	const char *name;
+	const char *ip;
+};
+
+/* Tor check hostname/ip. */
+static const struct test_host tor_check = {
+	.name = "sergii.torproject.org",
+	.ip = "38.229.72.22",
+};
+
+/* moria1 directory authority. */
+static const struct test_host tor_dir_auth1 = {
+	.name = "belegost.csail.mit.edu",
+	.ip = "128.31.0.39",
+};
+
+/* maatuska directory authority. */
+static const struct test_host tor_dir_auth2 = {
+	.name = "ehlo.4711.se",
+	.ip = "171.25.193.9",
+};
+
+static void test_gethostbyname(const struct test_host *host)
+{
+    struct hostent *he;
+
+	assert(host);
+
+	diag("gethostbyname test");
+
+    he = gethostbyname(host->name);
+    if (he) {
+		char *addr = inet_ntoa(*((struct in_addr *) he->h_addr_list[0]));
+		ok(strcmp(addr, host->ip) == 0, "Resolving %s", host->name);
+    } else {
+		fail("Resolving %s", host->name);
+	}
+
+	return;
+}
+
+static void test_gethostbyaddr(const struct test_host *host)
+{
+	struct hostent *he;
+    in_addr_t addr;
+
+	assert(host);
+
+	diag("gethostbyaddr test");
+
+    addr = inet_addr(host->ip);
+
+    he = gethostbyaddr(&addr, INET_ADDRSTRLEN, AF_INET);
+    if (he) {
+		ok(strcmp(host->name, he->h_name) == 0,
+				"Resolving address %s", host->ip);
+    } else {
+		fail("Resolving address %s", host->ip);
+	}
+
+    return;
+}
+
+static void test_getaddrinfo(const struct test_host *host)
+{
+	int ret;
+    struct addrinfo hints;
+    struct addrinfo *result;
+
+	diag("getaddrinfo test");
+
+    memset(&hints, 0, sizeof(struct addrinfo));
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+    hints.ai_flags = AI_PASSIVE;    /* For wildcard IP address */
+    hints.ai_protocol = 0;          /* Any protocol */
+    hints.ai_canonname = NULL;
+    hints.ai_addr = NULL;
+    hints.ai_next = NULL;
+
+    ret = getaddrinfo(host->name, NULL, &hints, &result);
+    if (ret == 0) {
+		struct in_addr addr;
+		char *ip;
+
+		addr.s_addr = ((struct sockaddr_in *)(result->ai_addr))->sin_addr.s_addr;
+		ip = inet_ntoa(addr);
+
+		ok(strcmp(host->ip, ip) == 0,
+				"Resolving address %s with getaddrinfo", host->name);
+    } else {
+		printf("%s\n", gai_strerror(ret));
+		fail("Resolving address %s with getaddrinfo", host->name);
+	}
+
+    return;
+}
+
+int main(int argc, char **argv)
+{
+	/* Libtap call for the number of tests planned. */
+	plan_tests(NUM_TESTS);
+
+	test_getaddrinfo(&tor_check);
+    test_gethostbyname(&tor_dir_auth1);
+	test_gethostbyaddr(&tor_dir_auth2);
+
+    return 0;
+}





More information about the tor-commits mailing list