[tor-commits] [torsocks/master] tests: Add a check for a running Tor

dgoulet at torproject.org dgoulet at torproject.org
Fri Feb 24 18:47:30 UTC 2017


commit c5cde94da6cc627b7538d54d7388038e857de631
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Fri Feb 24 13:46:24 2017 -0500

    tests: Add a check for a running Tor
    
    With this check, we can skip tests that need a running tor when none is
    present.
    
    Also update the maatuska hostname for the DNS test.
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 tests/Makefile.am        |  6 ++++--
 tests/helpers.c          | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 tests/helpers.h          | 23 +++++++++++++++++++++++
 tests/test_dns.c         | 14 +++++++++++---
 tests/test_getpeername.c |  8 ++++++++
 5 files changed, 94 insertions(+), 5 deletions(-)

diff --git a/tests/Makefile.am b/tests/Makefile.am
index a1ee8b1..e703168 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -17,9 +17,11 @@ LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
 
 LIBTORSOCKS=$(top_builddir)/src/lib/libtorsocks.la
 
+HELPER_SOURCES = helpers.c helpers.h
+
 noinst_PROGRAMS = test_dns test_socket test_connect test_fd_passing test_getpeername
 
-test_dns_SOURCES = test_dns.c
+test_dns_SOURCES = test_dns.c $(HELPER_SOURCES)
 test_dns_LDADD = $(LIBTAP) $(LIBTORSOCKS)
 
 test_socket_SOURCES = test_socket.c
@@ -31,5 +33,5 @@ test_connect_LDADD = $(LIBTAP) $(LIBTORSOCKS)
 test_fd_passing_SOURCES = test_fd_passing.c
 test_fd_passing_LDADD = $(LIBTAP) $(LIBTORSOCKS) -lpthread
 
-test_getpeername_SOURCES = test_getpeername.c
+test_getpeername_SOURCES = test_getpeername.c $(HELPER_SOURCES)
 test_getpeername_LDADD = $(LIBTAP) $(LIBTORSOCKS)
diff --git a/tests/helpers.c b/tests/helpers.c
new file mode 100644
index 0000000..1029e0a
--- /dev/null
+++ b/tests/helpers.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2017 - 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 "lib/torsocks.h"
+
+#include "helpers.h"
+
+/* Try to connect to SocksPort localhost:9050 and if we can't skip. This is
+ * to avoid to have failing test if no tor daemon is available. Return 1 if
+ * true else 0. */
+int
+helper_is_default_tor_running(void)
+{
+  int ret, fd;
+  struct sockaddr_in sa;
+
+  fd = tsocks_libc_socket(AF_INET, SOCK_STREAM, 0);
+  if (fd < 0) {
+    goto end;
+  }
+  sa.sin_family = AF_INET;
+  sa.sin_port = htons(9050);
+  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+  ret = tsocks_libc_connect(fd, (const struct sockaddr *) &sa, sizeof(sa));
+  close(fd);
+  if (ret < 0) {
+    goto end;
+  }
+  return 1;
+end:
+  return 0;
+}
diff --git a/tests/helpers.h b/tests/helpers.h
new file mode 100644
index 0000000..8cd0adb
--- /dev/null
+++ b/tests/helpers.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2017 - 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.
+ */
+
+#ifndef TORSOCKS_HELPERS_H
+#define TORSOCKS_HELPERS_H
+
+int helper_is_default_tor_running(void);
+
+#endif /* TORSOCKS_HELPERS_H */
diff --git a/tests/test_dns.c b/tests/test_dns.c
index 1ca3827..2d87744 100644
--- a/tests/test_dns.c
+++ b/tests/test_dns.c
@@ -24,6 +24,7 @@
 #include <lib/torsocks.h>
 
 #include <tap/tap.h>
+#include "helpers.h"
 
 #define NUM_TESTS 4
 
@@ -46,7 +47,7 @@ static const struct test_host tor_dir_auth1 = {
 
 /* maatuska directory authority. */
 static const struct test_host tor_dir_auth2 = {
-	.name = "ehlo.4711.se",
+	.name = "maatuska.4711.se",
 	.ip = "171.25.193.9",
 };
 
@@ -135,13 +136,20 @@ static void test_getaddrinfo(const struct test_host *host)
 
 int main(int argc, char **argv)
 {
+	/* Try to connect to SocksPort localhost:9050 and if we can't skip. This is
+	 * to avoid to have failing test if no tor daemon is available. */
+	if (!helper_is_default_tor_running()) {
+		goto end;
+	}
+
 	/* Libtap call for the number of tests planned. */
 	plan_tests(NUM_TESTS);
 
 	test_getaddrinfo(&tor_check);
-    test_gethostbyname(&tor_dir_auth1);
+	test_gethostbyname(&tor_dir_auth1);
 	test_gethostbyaddr(&tor_dir_auth2);
 	test_getaddrinfo(&tor_localhost);
 
-    return 0;
+end:
+	return 0;
 }
diff --git a/tests/test_getpeername.c b/tests/test_getpeername.c
index faf4149..c09e487 100644
--- a/tests/test_getpeername.c
+++ b/tests/test_getpeername.c
@@ -23,6 +23,7 @@
 #include <lib/torsocks.h>
 
 #include <tap/tap.h>
+#include "helpers.h"
 
 #define NUM_TESTS 7
 
@@ -102,10 +103,17 @@ error:
 
 int main(int argc, char **argv)
 {
+	/* Try to connect to SocksPort localhost:9050 and if we can't skip. This is
+	 * to avoid to have failing test if no tor daemon is available. */
+	if (!helper_is_default_tor_running()) {
+		goto end;
+	}
+
 	/* Libtap call for the number of tests planned. */
 	plan_tests(NUM_TESTS);
 
 	test_getpeername();
 
+end:
     return 0;
 }



More information about the tor-commits mailing list