aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-02-07 12:22:56 -0500
committerNick Mathewson <nickm@torproject.org>2014-02-07 12:22:56 -0500
commitf1682a615f9af50f4380bf1bb44070f299422a0a (patch)
tree950ff987bed7c36dad216fa89bba09ae50df4fe5 /src/test
parent040b478692c2355515eec626044ea2e3c37ca9c5 (diff)
parent57da1a5057a3d95a9093384a3797d5a404863c5a (diff)
downloadtor-f1682a615f9af50f4380bf1bb44070f299422a0a.tar
tor-f1682a615f9af50f4380bf1bb44070f299422a0a.tar.gz
Merge remote-tracking branch 'houqp/hs_control_fix'
Diffstat (limited to 'src/test')
-rw-r--r--src/test/include.am2
-rw-r--r--src/test/test.c4
-rw-r--r--src/test/test_hs.c54
-rw-r--r--src/test/test_nodelist.c71
-rw-r--r--src/test/test_router.c37
5 files changed, 113 insertions, 55 deletions
diff --git a/src/test/include.am b/src/test/include.am
index 9f6c3e0d5..5f978b518 100644
--- a/src/test/include.am
+++ b/src/test/include.am
@@ -39,7 +39,7 @@ src_test_test_SOURCES = \
src/test/test_util.c \
src/test/test_config.c \
src/test/test_hs.c \
- src/test/test_router.c \
+ src/test/test_nodelist.c \
src/ext/tinytest.c
src_test_test_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
diff --git a/src/test/test.c b/src/test/test.c
index 8b2a5ad67..522f2af64 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1626,7 +1626,7 @@ extern struct testcase_t controller_event_tests[];
extern struct testcase_t logging_tests[];
extern struct testcase_t backtrace_tests[];
extern struct testcase_t hs_tests[];
-extern struct testcase_t router_tests[];
+extern struct testcase_t nodelist_tests[];
static struct testgroup_t testgroups[] = {
{ "", test_array },
@@ -1651,7 +1651,7 @@ static struct testgroup_t testgroups[] = {
{ "extorport/", extorport_tests },
{ "control/", controller_event_tests },
{ "hs/", hs_tests },
- { "router/", router_tests },
+ { "nodelist/", nodelist_tests },
END_OF_GROUPS
};
diff --git a/src/test/test_hs.c b/src/test/test_hs.c
index 75c9b6287..99ef7dd57 100644
--- a/src/test/test_hs.c
+++ b/src/test/test_hs.c
@@ -11,6 +11,17 @@
#include "test.h"
#include "control.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" \
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+#define STR_HSDIR_EXIST_LONGNAME \
+ "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir"
+/* mock ID digest and longname for node that's not in nodelist */
+#define HSDIR_NONE_EXIST_ID "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB" \
+ "\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB\xBB"
+#define STR_HSDIR_NONE_EXIST_LONGNAME \
+ "$BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
+
/* Helper global variable for hidden service descriptor event test.
* It's used as a pointer to dynamically created message buffer in
* send_control_event_string_replacement function, which mocks
@@ -31,6 +42,19 @@ send_control_event_string_replacement(uint16_t event, event_format_t which,
received_msg = tor_strdup(msg);
}
+/** Mock function for node_describe_longname_by_id, it returns either
+ * STR_HSDIR_EXIST_LONGNAME or STR_HSDIR_NONE_EXIST_LONGNAME
+ */
+static const char *
+node_describe_longname_by_id_replacement(const char *id_digest)
+{
+ if (!strcmp(id_digest, HSDIR_EXIST_ID)) {
+ return STR_HSDIR_EXIST_LONGNAME;
+ } else {
+ return STR_HSDIR_NONE_EXIST_LONGNAME;
+ }
+}
+
/** Make sure each hidden service descriptor async event generation
*
* function generates the message in expected format.
@@ -39,8 +63,6 @@ static void
test_hs_desc_event(void *arg)
{
#define STR_HS_ADDR "ajhb7kljbiru65qo"
- #define STR_HS_DIR_LONGNAME \
- "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=TestDir at 1.2.3.4"
#define STR_HS_ID "b3oeducbhjmbqmgw2i3jtz4fekkrinwj"
rend_data_t rend_query;
@@ -49,6 +71,8 @@ test_hs_desc_event(void *arg)
(void) arg;
MOCK(send_control_event_string,
send_control_event_string_replacement);
+ MOCK(node_describe_longname_by_id,
+ node_describe_longname_by_id_replacement);
/* setup rend_query struct */
strncpy(rend_query.onion_address, STR_HS_ADDR,
@@ -56,44 +80,44 @@ test_hs_desc_event(void *arg)
rend_query.auth_type = 0;
/* test request event */
- control_event_hs_descriptor_requested(&rend_query, STR_HS_DIR_LONGNAME,
+ control_event_hs_descriptor_requested(&rend_query, HSDIR_EXIST_ID,
STR_HS_ID);
- expected_msg =
- "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "STR_HS_DIR_LONGNAME\
- " "STR_HS_ID"\r\n";
+ expected_msg = "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "\
+ STR_HSDIR_EXIST_LONGNAME" "STR_HS_ID"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tor_free(received_msg);
/* test received event */
rend_query.auth_type = 1;
- control_event_hs_descriptor_received(&rend_query, STR_HS_DIR_LONGNAME);
- expected_msg =
- "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "STR_HS_DIR_LONGNAME"\r\n";
+ control_event_hs_descriptor_received(&rend_query, HSDIR_EXIST_ID);
+ expected_msg = "650 HS_DESC RECEIVED "STR_HS_ADDR" BASIC_AUTH "\
+ STR_HSDIR_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tor_free(received_msg);
/* test failed event */
rend_query.auth_type = 2;
- control_event_hs_descriptor_failed(&rend_query, STR_HS_DIR_LONGNAME);
- expected_msg =
- "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "STR_HS_DIR_LONGNAME"\r\n";
+ control_event_hs_descriptor_failed(&rend_query, HSDIR_NONE_EXIST_ID);
+ expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" STEALTH_AUTH "\
+ STR_HSDIR_NONE_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tor_free(received_msg);
/* test invalid auth type */
rend_query.auth_type = 999;
- control_event_hs_descriptor_failed(&rend_query, STR_HS_DIR_LONGNAME);
- expected_msg =
- "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "STR_HS_DIR_LONGNAME"\r\n";
+ control_event_hs_descriptor_failed(&rend_query, HSDIR_EXIST_ID);
+ expected_msg = "650 HS_DESC FAILED "STR_HS_ADDR" UNKNOWN "\
+ STR_HSDIR_EXIST_LONGNAME"\r\n";
test_assert(received_msg);
test_streq(received_msg, expected_msg);
tor_free(received_msg);
done:
UNMOCK(send_control_event_string);
+ UNMOCK(node_describe_longname_by_id);
tor_free(received_msg);
}
diff --git a/src/test/test_nodelist.c b/src/test/test_nodelist.c
new file mode 100644
index 000000000..cbdc4a72b
--- /dev/null
+++ b/src/test/test_nodelist.c
@@ -0,0 +1,71 @@
+/* Copyright (c) 2007-2013, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file test_nodelist.c
+ * \brief Unit tests for nodelist related functions.
+ **/
+
+#include "or.h"
+#include "nodelist.h"
+#include "test.h"
+
+/** Tese the case when node_get_by_id() returns NULL,
+ * node_get_verbose_nickname_by_id should return the base 16 encoding
+ * of the id.
+ */
+static void
+test_nodelist_node_get_verbose_nickname_by_id_null_node(void *arg)
+{
+ char vname[MAX_VERBOSE_NICKNAME_LEN+1];
+ const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
+ (void) arg;
+
+ /* make sure node_get_by_id returns NULL */
+ test_assert(!node_get_by_id(ID));
+ node_get_verbose_nickname_by_id(ID, vname);
+ test_streq(vname, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+ done:
+ return;
+}
+
+/** For routers without named flag, get_verbose_nickname should return
+ * "Fingerprint~Nickname"
+ */
+static void
+test_nodelist_node_get_verbose_nickname_not_named(void *arg)
+{
+ node_t mock_node;
+ routerstatus_t mock_rs;
+
+ char vname[MAX_VERBOSE_NICKNAME_LEN+1];
+
+ (void) arg;
+
+ memset(&mock_node, 0, sizeof(node_t));
+ memset(&mock_rs, 0, sizeof(routerstatus_t));
+
+ /* verbose nickname should use ~ instead of = for unnamed routers */
+ strncpy(mock_rs.nickname, "TestOR", 6);
+ mock_node.rs = &mock_rs;
+ strncpy(mock_node.identity,
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
+ "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA",
+ DIGEST_LEN);
+ node_get_verbose_nickname(&mock_node, vname);
+ test_streq(vname, "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA~TestOR");
+
+ done:
+ return;
+}
+
+#define NODE(name, flags) \
+ { #name, test_nodelist_##name, (flags), NULL, NULL }
+
+struct testcase_t nodelist_tests[] = {
+ NODE(node_get_verbose_nickname_by_id_null_node, TT_FORK),
+ NODE(node_get_verbose_nickname_not_named, TT_FORK),
+ END_OF_TESTCASES
+};
+
diff --git a/src/test/test_router.c b/src/test/test_router.c
deleted file mode 100644
index 5e816564d..000000000
--- a/src/test/test_router.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/* Copyright (c) 2007-2013, The Tor Project, Inc. */
-/* See LICENSE for licensing information */
-
-/**
- * \file test_router.c
- * \brief Unit tests for router related functions.
- **/
-
-#include "or.h"
-#include "nodelist.h"
-#include "router.h"
-#include "test.h"
-
-/** Tese the case when node_get_by_id() returns NULL, node_describe_by_id
- * should return the base 16 encoding of the id.
- */
-static void
-test_node_describe_by_id_null_node(void *arg)
-{
- const char ID[] = "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA";
- (void) arg;
-
- /* make sure node_get_by_id returns NULL */
- test_assert(!node_get_by_id(ID));
- test_streq(node_describe_by_id(ID),
- "$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
- done:
- return;
-}
-
-struct testcase_t router_tests[] = {
- { "node_get_by_id_null_node", test_node_describe_by_id_null_node, TT_FORK,
- NULL, NULL },
- END_OF_TESTCASES
-};
-