commit 89194d2372d7d75ab599b1330424782c3d267bb0
Author: Luke Gallagher <l...@hypergeometric.net>
Date:   Sun Sep 8 15:45:25 2013 +1000

    Tests: add config-file tests
    
    Signed-off-by: David Goulet <dgou...@ev0ke.net>
---
 .gitignore                     |    1 +
 tests/test_list                |    1 +
 tests/unit/Makefile.am         |   11 ++-
 tests/unit/fixtures/config0    |   19 +++++
 tests/unit/fixtures/config1    |    1 +
 tests/unit/fixtures/config2    |    1 +
 tests/unit/fixtures/config3    |    1 +
 tests/unit/fixtures/config4    |    2 +
 tests/unit/fixtures/config5    |    2 +
 tests/unit/fixtures/config6    |    2 +
 tests/unit/fixtures/config7    |    2 +
 tests/unit/fixtures/config8    |    2 +
 tests/unit/fixtures/config9_32 |    2 +
 tests/unit/fixtures/config9_64 |    2 +
 tests/unit/test_config-file.c  |  159 ++++++++++++++++++++++++++++++++++++++++
 tests/utils/fixtures.h         |   47 ++++++++++++
 16 files changed, 253 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 5bd2399..9739a6f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,3 +45,4 @@ tests/test_dns
 tests/unit/test_onion
 tests/unit/test_connection
 tests/unit/test_utils
+tests/unit/test_config-file
diff --git a/tests/test_list b/tests/test_list
index d433edb..e1fa93b 100644
--- a/tests/test_list
+++ b/tests/test_list
@@ -2,3 +2,4 @@
 ./unit/test_onion
 ./unit/test_connection
 ./unit/test_utils
+./unit/test_config-file
diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am
index 3fb1150..a48450c 100644
--- a/tests/unit/Makefile.am
+++ b/tests/unit/Makefile.am
@@ -1,10 +1,14 @@
-AM_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src 
-I$(top_srcdir)/tests/utils/ -I$(srcdir)
+AM_CFLAGS = -I$(top_srcdir)/include \
+                       -I$(top_srcdir)/src \
+                       -I$(top_srcdir)/tests/utils/ \
+                       -I$(srcdir) \
+                       -DTORSOCKS_FIXTURE_PATH=\"`pwd`/fixtures/\"
 
 LIBTAP=$(top_builddir)/tests/utils/tap/libtap.la
 
 LIBCOMMON=$(top_builddir)/src/common/libcommon.la
 
-noinst_PROGRAMS = test_onion test_connection test_utils
+noinst_PROGRAMS = test_onion test_connection test_utils test_config-file
 
 test_onion_SOURCES = test_onion.c
 test_onion_LDADD = $(LIBTAP) $(LIBCOMMON)
@@ -14,3 +18,6 @@ test_connection_LDADD = $(LIBTAP) $(LIBCOMMON)
 
 test_utils_SOURCES = test_utils.c
 test_utils_LDADD = $(LIBTAP) $(LIBCOMMON)
+
+test_config_file_SOURCES = test_config-file.c
+test_config_file_LDADD = $(LIBTAP) $(LIBCOMMON)
diff --git a/tests/unit/fixtures/config0 b/tests/unit/fixtures/config0
new file mode 100644
index 0000000..f5b4924
--- /dev/null
+++ b/tests/unit/fixtures/config0
@@ -0,0 +1,19 @@
+# This is the configuration for libtorsocks (transparent socks) for use
+# with tor, which is providing a socks server on port 9050 by default.
+#
+# Lines beginning with # and blank lines are ignored
+# Much more documentation than provided in these comments can be found in
+#
+# torsocks.conf(5), torsocks(1) and torsocks(8) manpages.
+
+# Default Tor address and port. By default, Tor will listen on localhost for
+# any SOCKS connection and relay the traffic on the Tor network.
+TorAddress 127.0.0.1
+TorPort 9050
+
+# Tor hidden sites do not have real IP addresses. This specifies what range of
+# IP addresses will be handed to the application as "cookies" for .onion names.
+# Of course, you should pick a block of addresses which you aren't going to
+# ever need to actually connect to. This is similar to the MapAddress feature
+# of the main tor daemon.
+OnionAddrRange 127.42.42.0/24
diff --git a/tests/unit/fixtures/config1 b/tests/unit/fixtures/config1
new file mode 100644
index 0000000..8f5d225
--- /dev/null
+++ b/tests/unit/fixtures/config1
@@ -0,0 +1 @@
+# Empty config
diff --git a/tests/unit/fixtures/config2 b/tests/unit/fixtures/config2
new file mode 100644
index 0000000..37aff43
--- /dev/null
+++ b/tests/unit/fixtures/config2
@@ -0,0 +1 @@
+TorPort 65536
diff --git a/tests/unit/fixtures/config3 b/tests/unit/fixtures/config3
new file mode 100644
index 0000000..5c62206
--- /dev/null
+++ b/tests/unit/fixtures/config3
@@ -0,0 +1 @@
+TorPort 0
diff --git a/tests/unit/fixtures/config4 b/tests/unit/fixtures/config4
new file mode 100644
index 0000000..d5413a7
--- /dev/null
+++ b/tests/unit/fixtures/config4
@@ -0,0 +1,2 @@
+# invalid IPv4
+TorAddress 0.0
diff --git a/tests/unit/fixtures/config5 b/tests/unit/fixtures/config5
new file mode 100644
index 0000000..bee314c
--- /dev/null
+++ b/tests/unit/fixtures/config5
@@ -0,0 +1,2 @@
+# invalid IPv6
+TorAddress 2001:::1
diff --git a/tests/unit/fixtures/config6 b/tests/unit/fixtures/config6
new file mode 100644
index 0000000..a8cd5c7
--- /dev/null
+++ b/tests/unit/fixtures/config6
@@ -0,0 +1,2 @@
+# invalid onion IPv4 range
+OnionAddrRange 127.42.42.0
diff --git a/tests/unit/fixtures/config7 b/tests/unit/fixtures/config7
new file mode 100644
index 0000000..fdc9dea
--- /dev/null
+++ b/tests/unit/fixtures/config7
@@ -0,0 +1,2 @@
+# invalid onion IPv4 address
+OnionAddrRange 127.42.42.256/24
diff --git a/tests/unit/fixtures/config8 b/tests/unit/fixtures/config8
new file mode 100644
index 0000000..c4c2e43
--- /dev/null
+++ b/tests/unit/fixtures/config8
@@ -0,0 +1,2 @@
+# invalid onion IPv6 address
+OnionAddrRange 2001:::1/64
diff --git a/tests/unit/fixtures/config9_32 b/tests/unit/fixtures/config9_32
new file mode 100644
index 0000000..0658bea
--- /dev/null
+++ b/tests/unit/fixtures/config9_32
@@ -0,0 +1,2 @@
+# invalid onion mask
+OnionAddrRange 127.42.42.0/4294967295
diff --git a/tests/unit/fixtures/config9_64 b/tests/unit/fixtures/config9_64
new file mode 100644
index 0000000..4934a60
--- /dev/null
+++ b/tests/unit/fixtures/config9_64
@@ -0,0 +1,2 @@
+# invalid onion mask
+OnionAddrRange 127.42.42.0/18446744073709551615
diff --git a/tests/unit/test_config-file.c b/tests/unit/test_config-file.c
new file mode 100644
index 0000000..59e3115
--- /dev/null
+++ b/tests/unit/test_config-file.c
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2013 - David Goulet <dgou...@ev0ke.net>
+ *                      Luke Gallagher <l...@hypergeometric.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 <stdio.h>
+#include <arpa/inet.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include <common/utils.h>
+#include <common/defaults.h>
+#include <common/config-file.h>
+
+#include <tap/tap.h>
+#include <fixtures.h>
+
+#define NUM_TESTS 11
+
+static void test_config_file_read_none(void)
+{
+       int ret = 0;
+       struct configuration config;
+       char buf[DEFAULT_DOMAIN_NAME_SIZE];
+
+       diag("Config file read none");
+
+       ret = config_file_read(NULL, &config);
+       inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
+       ok(ret == 0 &&
+               config.conf_file.tor_port == DEFAULT_TOR_PORT &&
+               strcmp(config.conf_file.tor_address, DEFAULT_TOR_ADDRESS) == 0 
&&
+               strcmp(buf, DEFAULT_ONION_ADDR_RANGE) == 0 &&
+               config.conf_file.onion_mask == strtoul(DEFAULT_ONION_ADDR_MASK, 
NULL, 0),
+               "Use default when no config file");
+}
+
+static void test_config_file_read_valid(void)
+{
+       int ret = 0;
+       struct configuration config;
+       char buf[DEFAULT_DOMAIN_NAME_SIZE];
+
+       diag("Config file read valid");
+
+       ret = config_file_read(fixture("config0"), &config);
+       inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
+       ok(ret == 0 &&
+               config.conf_file.tor_port == DEFAULT_TOR_PORT &&
+               strcmp(config.conf_file.tor_address, DEFAULT_TOR_ADDRESS) == 0 
&&
+               strcmp(buf, DEFAULT_ONION_ADDR_RANGE) == 0 &&
+               config.conf_file.onion_mask == strtoul(DEFAULT_ONION_ADDR_MASK, 
NULL, 0),
+               "Read valid config file");
+}
+
+static void test_config_file_read_empty(void)
+{
+       int ret = 0;
+       struct configuration config;
+       char buf[DEFAULT_DOMAIN_NAME_SIZE];
+
+       diag("Config file read empty");
+
+       ret = config_file_read(fixture("config1"), &config);
+       inet_ntop(AF_INET, &config.conf_file.onion_base, buf, sizeof(buf));
+       ok(ret == 0 &&
+               config.conf_file.tor_port == 0 &&
+               config.conf_file.tor_address == NULL &&
+               strcmp(buf, "0.0.0.0") == 0 &&
+               config.conf_file.onion_mask == 0,
+               "Read empty config file");
+}
+
+static void test_config_file_read_invalid_values(void)
+{
+       int ret = 0;
+       struct configuration config;
+
+       diag("Config file read invalid values");
+
+       ret = config_file_read(fixture("config2"), &config);
+       ok(ret == -EINVAL &&
+               config.conf_file.tor_port == 0,
+               "TorPort 65536 returns -EINVAL");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config3"), &config);
+       ok(ret == -EINVAL &&
+               config.conf_file.tor_port == 0,
+               "TorPort 0 returns -EINVAL");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config4"), &config);
+       ok(ret == -1 &&
+               config.conf_file.tor_address == NULL,
+               "TorAddress invalid IPv4 returns -1");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config5"), &config);
+       ok(ret == -1 &&
+               config.conf_file.tor_address == NULL,
+               "TorAddress invalid IPv6 returns -1");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config6"), &config);
+       ok(ret == -EINVAL &&
+               config.conf_file.onion_mask == 0,
+               "OnionAdrRange invalid range returns -EINVAL");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config7"), &config);
+       ok(ret == -EINVAL &&
+               config.conf_file.onion_base == 0,
+               "OnionAdrRange invalid IPv4 address returns -EINVAL");
+
+       memset(&config, 0x0, sizeof(config));
+       ret = config_file_read(fixture("config8"), &config);
+       ok(ret == -EINVAL &&
+               config.conf_file.onion_base == 0,
+               "OnionAdrRange invalid IPv6 address returns -EINVAL");
+
+       memset(&config, 0x0, sizeof(config));
+#if (defined(__LP64__))
+       ret = config_file_read(fixture("config9_64"), &config);
+#else
+       ret = config_file_read(fixture("config9_32"), &config);
+#endif
+       ok(ret == -EINVAL &&
+               config.conf_file.onion_base == 0,
+               "OnionAdrRange invalid mask returns -EINVAL");
+}
+
+int main(int argc, char **argv)
+{
+       /* Libtap call for the number of tests planned. */
+       plan_tests(NUM_TESTS);
+
+       test_config_file_read_none();
+       skip_start(0 == TORSOCKS_FIXTURE_PATH, 10, "TORSOCKS_FIXTURE_PATH not 
defined");
+       test_config_file_read_valid();
+       test_config_file_read_empty();
+       test_config_file_read_invalid_values();
+       skip_end();
+
+       return exit_status();
+}
diff --git a/tests/utils/fixtures.h b/tests/utils/fixtures.h
new file mode 100644
index 0000000..bbcd2f5
--- /dev/null
+++ b/tests/utils/fixtures.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2000-2008 - Shaun Clowes <del...@progsoc.org>
+ *               2008-2011 - Robert Hogan <rob...@roberthogan.net>
+ *               2013 - David Goulet <dgou...@ev0ke.net>
+ *                      Luke Gallagher <l...@hypergeometric.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 FIXTURES_H
+#define FIXTURES_H
+
+#include <string.h>
+
+#if !defined(TORSOCKS_FIXTURE_PATH)
+#define TORSOCKS_FIXTURE_PATH  0
+#endif /* TORSOCKS_FIXTURE_PATH */
+
+static const char *fixture_path(const char *base, const char *filename)
+{
+       static char path[1024];
+       size_t path_len;
+
+       path_len = strlen(base);
+       strncpy(path, base, path_len);
+       strncpy(path + path_len, filename, sizeof(path) - path_len);
+
+       return path;
+}
+
+const char *fixture(const char *fixture_name)
+{
+       return fixture_path(TORSOCKS_FIXTURE_PATH, fixture_name);
+}
+
+#endif /* FIXTURES_H */



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

Reply via email to