[torsocks/master] Tests: add connection object unit test

commit fc928e4dc81da50b32c4ac70e658c5a5c1359552 Author: David Goulet <dgoulet@ev0ke.net> Date: Sat Aug 31 12:55:12 2013 -0400 Tests: add connection object unit test Signed-off-by: David Goulet <dgoulet@ev0ke.net> --- .gitignore | 1 + tests/test_list | 1 + tests/unit/Makefile.am | 5 +- tests/unit/test_connection.c | 148 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a293c55..e04cf8d 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ src/bin/torsocks tests/test_dns tests/unit/test_onion +tests/unit/test_connection diff --git a/tests/test_list b/tests/test_list index ee705d6..7564c96 100644 --- a/tests/test_list +++ b/tests/test_list @@ -1,2 +1,3 @@ ./test_dns ./unit/test_onion +./unit/test_connection diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index ad8b361..9dd1bae 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -4,7 +4,10 @@ LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la LIBCOMMON=$(top_builddir)/src/common/libcommon.la -noinst_PROGRAMS = test_onion +noinst_PROGRAMS = test_onion test_connection test_onion_SOURCES = test_onion.c test_onion_LDADD = $(LIBTAP) $(LIBCOMMON) + +test_connection_SOURCES = test_connection.c +test_connection_LDADD = $(LIBTAP) $(LIBCOMMON) diff --git a/tests/unit/test_connection.c b/tests/unit/test_connection.c new file mode 100644 index 0000000..508e7db --- /dev/null +++ b/tests/unit/test_connection.c @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2013 - David Goulet <dgoulet@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 <common/connection.h> +#include <common/defaults.h> + +#include <tap/tap.h> + +#define NUM_TESTS 13 + +static void test_connection_usage(void) +{ + int ret; + struct connection *conn, *conn2, *l_conn; + struct connection_addr c_addr; + + diag("Connection subsystem creation test"); + + ret = connection_addr_set(CONNECTION_DOMAIN_INET, "127.0.0.1", 9050, + &c_addr); + ok(ret == 0 && + c_addr.domain == CONNECTION_DOMAIN_INET && + c_addr.u.sin.sin_family == AF_INET && + c_addr.u.sin.sin_port == htons(9050), + "Valid connection address creation"); + + conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin); + ok(conn && + conn->fd == 42 && + conn->dest_addr.domain == CONNECTION_DOMAIN_INET && + conn->refcount.count == 1, + "Valid connection creation"); + + conn2 = connection_create(43, (struct sockaddr *) &c_addr.u.sin); + ok(conn2 && + conn2->fd == 43 && + conn2->dest_addr.domain == CONNECTION_DOMAIN_INET && + conn2->refcount.count == 1, + "Valid second connection creation"); + + connection_registry_lock(); + connection_insert(conn); + l_conn = connection_find(conn->fd); + ok(conn == l_conn, "Valid connection insert/find"); + + connection_insert(conn2); + l_conn = connection_find(conn2->fd); + ok(conn2 == l_conn, "Valid second connection insert/find"); + + connection_remove(conn); + l_conn = connection_find(conn->fd); + ok(conn != l_conn, "Valid connection remove/find"); + + connection_remove(conn2); + l_conn = connection_find(conn2->fd); + ok(conn2 != l_conn, "Valid second connection remove/find"); + connection_registry_unlock(); + + connection_destroy(conn); + connection_destroy(conn2); +} + +static void test_connection_creation(void) +{ + int ret; + struct connection *conn; + struct connection_addr c_addr; + + diag("Connection subsystem creation test"); + + ret = connection_addr_set(CONNECTION_DOMAIN_INET, "127.0.0.1", 9050, + &c_addr); + ok(ret == 0 && + c_addr.domain == CONNECTION_DOMAIN_INET && + c_addr.u.sin.sin_family == AF_INET && + c_addr.u.sin.sin_port == htons(9050), + "Valid connection address creation"); + + conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin); + ok(conn && + conn->fd == 42 && + conn->dest_addr.domain == CONNECTION_DOMAIN_INET && + conn->refcount.count == 1, + "Valid connection creation"); + connection_destroy(conn); + + conn = connection_create(-1, (struct sockaddr *) &c_addr.u.sin); + ok(conn && + conn->fd == -1 && + conn->dest_addr.domain == CONNECTION_DOMAIN_INET && + conn->refcount.count == 1, + "Valid connection creation with fd -1"); + connection_destroy(conn); + + conn = connection_create(42, NULL); + ok(conn && + conn->fd == 42 && + conn->dest_addr.domain == 0 && + conn->refcount.count == 1, + "Valid connection creation with sockaddr NULL"); + connection_destroy(conn); + + ret = connection_addr_set(CONNECTION_DOMAIN_INET6, "::1", 9050, + &c_addr); + ok(ret == 0 && + c_addr.domain == CONNECTION_DOMAIN_INET6 && + c_addr.u.sin.sin_family == AF_INET6 && + c_addr.u.sin.sin_port == htons(9050), + "Valid connection address creation for IPv6"); + + conn = connection_create(42, (struct sockaddr *) &c_addr.u.sin6); + ok(conn && + conn->fd == 42 && + conn->dest_addr.domain == CONNECTION_DOMAIN_INET6 && + conn->refcount.count == 1, + "Valid connection creation for IPv6"); + connection_destroy(conn); +} + +int main(int argc, char **argv) +{ + /* Libtap call for the number of tests planned. */ + plan_tests(NUM_TESTS); + + test_connection_creation(); + test_connection_usage(); + + return 0; +}
participants (1)
-
dgoulet@torproject.org