summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2015-02-11 15:06:04 -0500
committerNick Mathewson <nickm@torproject.org>2015-02-11 15:06:04 -0500
commitcaf28519d9a4f7f0a5fa36a0cd94fe9c0ee292dc (patch)
treedb5e6003cc8cec5f27ba0e91449348518ea98455 /src/test
parent0c81dfa848da160b02818771626a425cd27b86bc (diff)
parent0899f51bc6abf535c6c55b81b98b959235b0f7c7 (diff)
downloadtor-caf28519d9a4f7f0a5fa36a0cd94fe9c0ee292dc.tar.gz
tor-caf28519d9a4f7f0a5fa36a0cd94fe9c0ee292dc.zip
Merge branch 'bug12844'
Conflicts: src/or/circuituse.c src/test/include.am src/test/test_entrynodes.c
Diffstat (limited to 'src/test')
-rw-r--r--src/test/include.am5
-rw-r--r--src/test/test_entrynodes.c68
-rw-r--r--src/test/test_hs.c80
-rw-r--r--src/test/testhelper.c67
-rw-r--r--src/test/testhelper.h10
5 files changed, 167 insertions, 63 deletions
diff --git a/src/test/include.am b/src/test/include.am
index 595be0b7c2..71041b1237 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -62,6 +62,7 @@ src_test_test_SOURCES = \
src/test/test_threads.c \
src/test/test_util.c \
src/test/testing_common.c \
+ src/test/testhelper.c \
src/ext/tinytest.c
src_test_test_slow_SOURCES = \
@@ -119,10 +120,12 @@ src_test_test_workqueue_LDADD = src/or/libtor-testing.a \
noinst_HEADERS+= \
src/test/fakechans.h \
src/test/test.h \
+ src/test/testhelper.h \
src/test/test_descriptors.inc \
src/test/example_extrainfo.inc \
src/test/failing_routerdescs.inc \
- src/test/ed25519_vectors.inc
+ src/test/ed25519_vectors.inc \
+ src/test/test_descriptors.inc
noinst_PROGRAMS+= src/test/test-ntor-cl
src_test_test_ntor_cl_SOURCES = src/test/test_ntor_cl.c
diff --git a/src/test/test_entrynodes.c b/src/test/test_entrynodes.c
index 19071a1550..5f2051c48f 100644
--- a/src/test/test_entrynodes.c
+++ b/src/test/test_entrynodes.c
@@ -18,7 +18,7 @@
#include "statefile.h"
#include "config.h"
-#include "test_descriptors.inc"
+#include "testhelper.h"
/* TODO:
* choose_random_entry() test with state set.
@@ -37,62 +37,6 @@ get_or_state_replacement(void)
return dummy_state;
}
-/* NOP replacement for router_descriptor_is_older_than() */
-static int
-router_descriptor_is_older_than_replacement(const routerinfo_t *router,
- int seconds)
-{
- (void) router;
- (void) seconds;
- return 0;
-}
-
-/* Number of descriptors contained in test_descriptors.txt. */
-#define NUMBER_OF_DESCRIPTORS 8
-
-/** Parse a file containing router descriptors and load them to our
- routerlist. This function is used to setup an artificial network
- so that we can conduct entry guard tests. */
-static void
-setup_fake_routerlist(void)
-{
- int retval;
- routerlist_t *our_routerlist = NULL;
- smartlist_t *our_nodelist = NULL;
-
- /* Read the file that contains our test descriptors. */
-
- /* We need to mock this function otherwise the descriptors will not
- accepted as they are too old. */
- MOCK(router_descriptor_is_older_than,
- router_descriptor_is_older_than_replacement);
-
- /* Load all the test descriptors to the routerlist. */
- retval = router_load_routers_from_string(TEST_DESCRIPTORS,
- NULL, SAVED_IN_JOURNAL,
- NULL, 0, NULL);
- tt_int_op(retval, OP_EQ, NUMBER_OF_DESCRIPTORS);
-
- /* Sanity checking of routerlist and nodelist. */
- our_routerlist = router_get_routerlist();
- tt_int_op(smartlist_len(our_routerlist->routers), OP_EQ,
- NUMBER_OF_DESCRIPTORS);
- routerlist_assert_ok(our_routerlist);
-
- our_nodelist = nodelist_get_list();
- tt_int_op(smartlist_len(our_nodelist), OP_EQ, NUMBER_OF_DESCRIPTORS);
-
- /* Mark all routers as non-guards but up and running! */
- SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
- node->is_running = 1;
- node->is_valid = 1;
- node->is_possible_guard = 0;
- } SMARTLIST_FOREACH_END(node);
-
- done:
- UNMOCK(router_descriptor_is_older_than);
-}
-
/* Unittest cleanup function: Cleanup the fake network. */
static int
fake_network_cleanup(const struct testcase_t *testcase, void *ptr)
@@ -120,7 +64,7 @@ fake_network_setup(const struct testcase_t *testcase)
get_or_state_replacement);
/* Setup fake routerlist. */
- setup_fake_routerlist();
+ helper_setup_fake_routerlist();
/* Return anything but NULL (it's interpreted as test fail) */
return dummy_state;
@@ -197,7 +141,7 @@ populate_live_entry_guards_test_helper(int num_needed)
/* Walk the nodelist and add all nodes as entry guards. */
our_nodelist = nodelist_get_list();
- tt_int_op(smartlist_len(our_nodelist), OP_EQ, NUMBER_OF_DESCRIPTORS);
+ tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
const node_t *node_tmp;
@@ -206,7 +150,7 @@ populate_live_entry_guards_test_helper(int num_needed)
} SMARTLIST_FOREACH_END(node);
/* Make sure the nodes were added as entry guards. */
- tt_int_op(smartlist_len(all_entry_guards), OP_EQ, NUMBER_OF_DESCRIPTORS);
+ tt_int_op(smartlist_len(all_entry_guards), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
/* Ensure that all the possible entry guards are enough to satisfy us. */
tt_int_op(smartlist_len(all_entry_guards), OP_GE, num_needed);
@@ -647,7 +591,7 @@ test_entry_is_live(void *arg)
/* Walk the nodelist and add all nodes as entry guards. */
our_nodelist = nodelist_get_list();
- tt_int_op(smartlist_len(our_nodelist), OP_EQ, NUMBER_OF_DESCRIPTORS);
+ tt_int_op(smartlist_len(our_nodelist), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
SMARTLIST_FOREACH_BEGIN(our_nodelist, const node_t *, node) {
const node_t *node_tmp;
@@ -659,7 +603,7 @@ test_entry_is_live(void *arg)
} SMARTLIST_FOREACH_END(node);
/* Make sure the nodes were added as entry guards. */
- tt_int_op(smartlist_len(all_entry_guards), OP_EQ, NUMBER_OF_DESCRIPTORS);
+ tt_int_op(smartlist_len(all_entry_guards), OP_EQ, HELPER_NUMBER_OF_DESCRIPTORS);
/* Now get a random test entry that we will use for this unit test. */
which_node = 3; /* (chosen by fair dice roll) */
diff --git a/src/test/test_hs.c b/src/test/test_hs.c
index 0246eaf648..e70ca5ee96 100644
--- a/src/test/test_hs.c
+++ b/src/test/test_hs.c
@@ -7,9 +7,15 @@
**/
#define CONTROL_PRIVATE
+#define CIRCUITBUILD_PRIVATE
+
#include "or.h"
#include "test.h"
#include "control.h"
+#include "testhelper.h"
+#include "config.h"
+#include "routerset.h"
+#include "circuitbuild.h"
/* mock ID digest and longname for node that's in nodelist */
#define HSDIR_EXIST_ID "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" \
@@ -123,9 +129,83 @@ test_hs_desc_event(void *arg)
tor_free(received_msg);
}
+/* Make sure we always pick the right RP, given a well formatted
+ * Tor2webRendezvousPoints value. */
+static void
+test_pick_tor2web_rendezvous_node(void *arg)
+{
+ or_options_t *options = get_options_mutable();
+ const node_t *chosen_rp = NULL;
+ router_crn_flags_t flags = CRN_NEED_DESC;
+ int retval, i;
+ const char *tor2web_rendezvous_str = "test003r";
+
+ (void) arg;
+
+ /* Setup fake routerlist. */
+ helper_setup_fake_routerlist();
+
+ /* Parse Tor2webRendezvousPoints as a routerset. */
+ options->Tor2webRendezvousPoints = routerset_new();
+ retval = routerset_parse(options->Tor2webRendezvousPoints,
+ tor2web_rendezvous_str,
+ "test_tor2web_rp");
+ tt_int_op(retval, >=, 0);
+
+ /* Pick rendezvous point. Make sure the correct one is
+ picked. Repeat many times to make sure it works properly. */
+ for (i = 0; i < 50 ; i++) {
+ chosen_rp = pick_tor2web_rendezvous_node(flags, options);
+ tt_assert(chosen_rp);
+ tt_str_op(chosen_rp->ri->nickname, ==, tor2web_rendezvous_str);
+ }
+
+ done:
+ routerset_free(options->Tor2webRendezvousPoints);
+}
+
+/* Make sure we never pick an RP if Tor2webRendezvousPoints doesn't
+ * correspond to an actual node. */
+static void
+test_pick_bad_tor2web_rendezvous_node(void *arg)
+{
+ or_options_t *options = get_options_mutable();
+ const node_t *chosen_rp = NULL;
+ router_crn_flags_t flags = CRN_NEED_DESC;
+ int retval, i;
+ const char *tor2web_rendezvous_str = "dummy";
+
+ (void) arg;
+
+ /* Setup fake routerlist. */
+ helper_setup_fake_routerlist();
+
+ /* Parse Tor2webRendezvousPoints as a routerset. */
+ options->Tor2webRendezvousPoints = routerset_new();
+ retval = routerset_parse(options->Tor2webRendezvousPoints,
+ tor2web_rendezvous_str,
+ "test_tor2web_rp");
+ tt_int_op(retval, >=, 0);
+
+ /* Pick rendezvous point. Since Tor2webRendezvousPoints was set to a
+ dummy value, we shouldn't find any eligible RPs. */
+ for (i = 0; i < 50 ; i++) {
+ chosen_rp = pick_tor2web_rendezvous_node(flags, options);
+ tt_assert(!chosen_rp);
+ }
+
+ done:
+ routerset_free(options->Tor2webRendezvousPoints);
+}
+
struct testcase_t hs_tests[] = {
{ "hs_desc_event", test_hs_desc_event, TT_FORK,
NULL, NULL },
+ { "pick_tor2web_rendezvous_node", test_pick_tor2web_rendezvous_node, TT_FORK,
+ NULL, NULL },
+ { "pick_bad_tor2web_rendezvous_node",
+ test_pick_bad_tor2web_rendezvous_node, TT_FORK,
+ NULL, NULL },
END_OF_TESTCASES
};
diff --git a/src/test/testhelper.c b/src/test/testhelper.c
new file mode 100644
index 0000000000..8bbc192a6c
--- /dev/null
+++ b/src/test/testhelper.c
@@ -0,0 +1,67 @@
+/* Copyright (c) 2014, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#define ROUTERLIST_PRIVATE
+
+#include "or.h"
+#include "routerlist.h"
+#include "testhelper.h"
+#include "nodelist.h"
+
+#include "test.h"
+
+#include "test_descriptors.inc"
+
+/* NOP replacement for router_descriptor_is_older_than() */
+static int
+router_descriptor_is_older_than_replacement(const routerinfo_t *router,
+ int seconds)
+{
+ (void) router;
+ (void) seconds;
+ return 0;
+}
+
+/** Parse a file containing router descriptors and load them to our
+ routerlist. This function is used to setup an artificial network
+ so that we can conduct tests on it. */
+void
+helper_setup_fake_routerlist(void)
+{
+ int retval;
+ routerlist_t *our_routerlist = NULL;
+ smartlist_t *our_nodelist = NULL;
+
+ /* Read the file that contains our test descriptors. */
+
+ /* We need to mock this function otherwise the descriptors will not
+ accepted as they are too old. */
+ MOCK(router_descriptor_is_older_than,
+ router_descriptor_is_older_than_replacement);
+
+ /* Load all the test descriptors to the routerlist. */
+ retval = router_load_routers_from_string(TEST_DESCRIPTORS,
+ NULL, SAVED_IN_JOURNAL,
+ NULL, 0, NULL);
+ tt_int_op(retval, ==, HELPER_NUMBER_OF_DESCRIPTORS);
+
+ /* Sanity checking of routerlist and nodelist. */
+ our_routerlist = router_get_routerlist();
+ tt_int_op(smartlist_len(our_routerlist->routers), ==,
+ HELPER_NUMBER_OF_DESCRIPTORS);
+ routerlist_assert_ok(our_routerlist);
+
+ our_nodelist = nodelist_get_list();
+ tt_int_op(smartlist_len(our_nodelist), ==, HELPER_NUMBER_OF_DESCRIPTORS);
+
+ /* Mark all routers as non-guards but up and running! */
+ SMARTLIST_FOREACH_BEGIN(our_nodelist, node_t *, node) {
+ node->is_running = 1;
+ node->is_valid = 1;
+ node->is_possible_guard = 0;
+ } SMARTLIST_FOREACH_END(node);
+
+ done:
+ UNMOCK(router_descriptor_is_older_than);
+}
+
diff --git a/src/test/testhelper.h b/src/test/testhelper.h
new file mode 100644
index 0000000000..6b1bde6822
--- /dev/null
+++ b/src/test/testhelper.h
@@ -0,0 +1,10 @@
+#ifndef TOR_TEST_HELPER_H
+#define TOR_TEST_HELPER_H
+
+/* Number of descriptors contained in test_descriptors.txt. */
+#define HELPER_NUMBER_OF_DESCRIPTORS 8
+
+void helper_setup_fake_routerlist(void);
+
+#endif
+