commit 0b0d4b4ebcfe3711a8bcd27cbbdc05cd02530f38 Author: Qingping Hou dave2008713@gmail.com Date: Wed Dec 18 01:40:05 2013 -0500
add test case for hidden service async events --- src/test/Makefile.nmake | 3 +- src/test/include.am | 1 + src/test/test.c | 2 + src/test/test_hs.c | 115 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-)
diff --git a/src/test/Makefile.nmake b/src/test/Makefile.nmake index 6479f9d..822431f 100644 --- a/src/test/Makefile.nmake +++ b/src/test/Makefile.nmake @@ -14,7 +14,8 @@ LIBS = ......\build-alpha\lib\libevent.lib \ TEST_OBJECTS = test.obj test_addr.obj test_containers.obj \ test_controller_events.ogj test_crypto.obj test_data.obj test_dir.obj \ test_microdesc.obj test_pt.obj test_util.obj test_config.obj \ - test_cell_formats.obj test_replay.obj test_introduce.obj tinytest.obj + test_cell_formats.obj test_replay.obj test_introduce.obj tinytest.obj \ + test_hs.obj
tinytest.obj: ..\ext\tinytest.c $(CC) $(CFLAGS) /D snprintf=_snprintf /c ..\ext\tinytest.c diff --git a/src/test/include.am b/src/test/include.am index c16dd14..fe30fa4 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -38,6 +38,7 @@ src_test_test_SOURCES = \ src/test/test_socks.c \ src/test/test_util.c \ src/test/test_config.c \ + src/test/test_hs.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 9b474e9..3f8e9c6 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1625,6 +1625,7 @@ extern struct testcase_t extorport_tests[]; 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[];
static struct testgroup_t testgroups[] = { { "", test_array }, @@ -1648,6 +1649,7 @@ static struct testgroup_t testgroups[] = { { "options/", options_tests }, { "extorport/", extorport_tests }, { "control/", controller_event_tests }, + { "hs/", hs_tests }, END_OF_GROUPS };
diff --git a/src/test/test_hs.c b/src/test/test_hs.c new file mode 100644 index 0000000..a9e6644 --- /dev/null +++ b/src/test/test_hs.c @@ -0,0 +1,115 @@ +/* Copyright (c) 2007-2013, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file test_hs.c + * \brief Unit tests for hidden service. + **/ + +#define CONTROL_PRIVATE +#include "or.h" +#include "test.h" +#include "control.h" + +/* 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 + * send_control_event_string function. + * + * Always free it after use! */ +static char *received_msg = NULL; + +/** Mock function for send_control_event_string + */ +static void +send_control_event_string_replacement(uint16_t event, event_format_t which, + const char *msg) +{ + int msg_len; + + (void) event; + (void) which; + msg_len = strlen(msg); + received_msg = tor_malloc_zero(msg_len+1); + strncpy(received_msg, msg, msg_len); +} + +/** Make sure each hidden service descriptor async event generation + * + * function generates the message in expected format. + */ +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; + const char *expected_msg; + + (void) arg; + MOCK(send_control_event_string, + send_control_event_string_replacement); + + /* setup rend_query struct */ + strncpy(rend_query.onion_address, STR_HS_ADDR, + REND_SERVICE_ID_LEN_BASE32+1); + rend_query.auth_type = 0; + + /* test request event */ + control_event_hs_descriptor_requested(&rend_query, STR_HS_DIR_LONGNAME, + STR_HS_ID); + expected_msg = + "650 HS_DESC REQUESTED "STR_HS_ADDR" NO_AUTH "STR_HS_DIR_LONGNAME\ + " "STR_HS_ID"\r\n"; + test_assert(received_msg); + test_streq(received_msg, expected_msg); + tor_free(received_msg); + received_msg = NULL; + + /* 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"; + test_assert(received_msg); + test_streq(received_msg, expected_msg); + tor_free(received_msg); + received_msg = NULL; + + /* 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"; + test_assert(received_msg); + test_streq(received_msg, expected_msg); + tor_free(received_msg); + received_msg = NULL; + + /* 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"; + test_assert(received_msg); + test_streq(received_msg, expected_msg); + tor_free(received_msg); + received_msg = NULL; + + done: + UNMOCK(send_control_event_string); + if (received_msg) { + tor_free(received_msg); + received_msg = NULL; + } +} + +struct testcase_t hs_tests[] = { + { "hs_desc_event", test_hs_desc_event, TT_FORK, + NULL, NULL }, + END_OF_TESTCASES +}; +
tor-commits@lists.torproject.org