[tor-commits] [torsocks/master] Tests: add connect() test

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


commit 4e142e770791c56d14af826a7a48d0a6f1bed44d
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Mon Mar 31 18:56:10 2014 -0400

    Tests: add connect() test
    
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 .gitignore           |    1 +
 tests/Makefile.am    |    5 ++-
 tests/test_connect.c |   97 ++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test_list      |    1 +
 4 files changed, 103 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index c474007..cc74d05 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,6 +43,7 @@ doc/usewithtor.1
 
 src/bin/torsocks
 
+tests/test_connect
 tests/test_dns
 tests/test_socket
 tests/unit/test_onion
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ae3c2d1..141ac5e 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -6,7 +6,7 @@ LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
 
 LIBTORSOCKS=$(top_builddir)/src/lib/libtorsocks.la
 
-noinst_PROGRAMS = test_dns test_socket
+noinst_PROGRAMS = test_dns test_socket test_connect
 
 test_dns_SOURCES = test_dns.c
 test_dns_LDADD = $(LIBTAP) $(LIBTORSOCKS)
@@ -14,6 +14,9 @@ test_dns_LDADD = $(LIBTAP) $(LIBTORSOCKS)
 test_socket_SOURCES = test_socket.c
 test_socket_LDADD = $(LIBTAP) $(LIBTORSOCKS)
 
+test_connect_SOURCES = test_connect.c
+test_connect_LDADD = $(LIBTAP) $(LIBTORSOCKS)
+
 check-am:
 	./run.sh test_list
 
diff --git a/tests/test_connect.c b/tests/test_connect.c
new file mode 100644
index 0000000..705219d
--- /dev/null
+++ b/tests/test_connect.c
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2014 - 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 8
+
+/* Suppress output messages. */
+int tsocks_loglevel = MSGNONE;
+//int tsocks_loglevel = MSGDEBUG;
+
+static void test_connect_deny(void)
+{
+	int fd, ret;
+	struct sockaddr_in sin;
+	struct sockaddr_in6 sin6;
+
+	fd = tsocks_libc_socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
+	ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
+	ok (ret == -1 && errno == EBADF, "Connect with RAW socket NOT valid");
+	close(fd);
+
+	sin.sin_family = AF_INET;
+	fd = tsocks_libc_socket(sin.sin_family, SOCK_DGRAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
+	ok (ret == -1 && errno == EBADF, "Connect with UDP socket NOT valid");
+	close(fd);
+
+	inet_pton(sin.sin_family, "0.0.0.0", &sin.sin_addr);
+	fd = tsocks_libc_socket(sin.sin_family, SOCK_STREAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
+	ok (ret == -1 && errno == EINVAL,
+			"Connect with ANY address is NOT valid.");
+	close(fd);
+
+	inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
+	fd = tsocks_libc_socket(sin.sin_family, SOCK_STREAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
+	ok (ret == -1 && errno == EPERM,
+			"Connect with local address is NOT valid.");
+	close(fd);
+
+	sin6.sin6_family = AF_INET6;
+	fd = tsocks_libc_socket(sin6.sin6_family, SOCK_DGRAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin6, sizeof(sin6));
+	ok (ret == -1 && errno == EBADF, "Connect with UDPv6 socket NOT valid");
+	close(fd);
+
+	inet_pton(sin6.sin6_family, "::", &sin6.sin6_addr);
+	fd = tsocks_libc_socket(sin6.sin6_family, SOCK_STREAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin6, sizeof(sin6));
+	ok (ret == -1 && errno == EINVAL,
+			"Connect with ANYv6 address is NOT valid.");
+	close(fd);
+
+	inet_pton(sin6.sin6_family, "::1", &sin6.sin6_addr);
+	fd = tsocks_libc_socket(sin6.sin6_family, SOCK_STREAM, 0);
+	ret = connect(fd, (struct sockaddr *) &sin6, sizeof(sin6));
+	ok (ret == -1 && errno == EPERM,
+			"Connect with local v6 address is NOT valid.");
+	close(fd);
+
+	/* Bad fd. */
+	ret = connect(42, NULL, 42);
+	ok (ret == -1 && errno == EBADF, "Bad socket FD");
+}
+
+int main(int argc, char **argv)
+{
+	/* Libtap call for the number of tests planned. */
+	plan_tests(NUM_TESTS);
+
+	test_connect_deny();
+
+    return 0;
+}
diff --git a/tests/test_list b/tests/test_list
index 542b93f..bb812b1 100644
--- a/tests/test_list
+++ b/tests/test_list
@@ -1,3 +1,4 @@
+./test_connect
 ./test_dns
 ./test_socket
 ./unit/test_onion





More information about the tor-commits mailing list