diff --git a/src/or/connection.c b/src/or/connection.c index 57a9c5838bd4e90af14fc7d2003c2fe95fe5745f..f1d7961a172811d13f18dc6f2f9a94c82c244dd0 100644 --- a/src/or/connection.c +++ b/src/or/connection.c @@ -10,6 +10,7 @@ * on connections. **/ +#define CONNECTION_PRIVATE #include "or.h" #include "buffers.h" /* @@ -458,7 +459,7 @@ connection_link_connections(connection_t *conn_a, connection_t *conn_b) * necessary, close its socket if necessary, and mark the directory as dirty * if <b>conn</b> is an OR or OP connection. */ -static void +STATIC void connection_free_(connection_t *conn) { void *mem; diff --git a/src/or/connection.h b/src/or/connection.h index 5ca8ca34303f0cdfec3dea641ebcc4003cd166c0..19f11c7439c137c8df7a7de17bdf0556ce33d003 100644 --- a/src/or/connection.h +++ b/src/or/connection.h @@ -214,5 +214,9 @@ void connection_enable_rate_limiting(connection_t *conn); #define connection_type_uses_bufferevent(c) (0) #endif +#ifdef CONNECTION_PRIVATE +STATIC void connection_free_(connection_t *conn); +#endif + #endif diff --git a/src/or/connection_or.c b/src/or/connection_or.c index 3711cfeb331c55567d62383164f7d168d23847f5..a55ca3aa01839ecf975f451aedd3eb816f002eed 100644 --- a/src/or/connection_or.c +++ b/src/or/connection_or.c @@ -197,6 +197,16 @@ connection_or_remove_from_ext_or_id_map(or_connection_t *conn) memset(conn->ext_or_conn_id, 0, EXT_OR_CONN_ID_LEN); } +/** Return the connection whose ext_or_id is <b>id</b>. Return NULL if no such + * connection is found. */ +or_connection_t * +connection_or_get_by_ext_or_id(const char *id) +{ + if (!orconn_ext_or_id_map) + return NULL; + return digestmap_get(orconn_ext_or_id_map, id); +} + /** Deallocate the global Extended ORPort identifier list */ void connection_or_clear_ext_or_id_map(void) diff --git a/src/or/ext_orport.h b/src/or/ext_orport.h index 89c3032dfc64b8d920381bef657464a8bf143a9f..92ace7779c3508aa2f6f2d03a8f8fb71c67e9f96 100644 --- a/src/or/ext_orport.h +++ b/src/or/ext_orport.h @@ -14,6 +14,7 @@ void ext_or_cmd_free(ext_or_cmd_t *cmd); void connection_or_set_ext_or_identifier(or_connection_t *conn); void connection_or_remove_from_ext_or_id_map(or_connection_t *conn); void connection_or_clear_ext_or_id_map(void); +or_connection_t *connection_or_get_by_ext_or_id(const char *id); int connection_ext_or_finished_flushing(or_connection_t *conn); int connection_ext_or_process_inbuf(or_connection_t *or_conn); diff --git a/src/test/include.am b/src/test/include.am index 8718ce7c9163e722eade81bdd7d89a9656d0d75d..74311ac19958102d1956fd3b66ccde51902b38c6 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -26,6 +26,7 @@ src_test_test_SOURCES = \ src/test/test_cell_queue.c \ src/test/test_data.c \ src/test/test_dir.c \ + src/test/test_extorport.c \ src/test/test_introduce.c \ src/test/test_microdesc.c \ src/test/test_options.c \ diff --git a/src/test/test.c b/src/test/test.c index a436688c572d2a1569501355f90295eeb42af891..851ddf026a531039de735382ff70b6b762ec14db 100644 --- a/src/test/test.c +++ b/src/test/test.c @@ -1569,6 +1569,7 @@ extern struct testcase_t circuitlist_tests[]; extern struct testcase_t cell_queue_tests[]; extern struct testcase_t options_tests[]; extern struct testcase_t socks_tests[]; +extern struct testcase_t extorport_tests[]; static struct testgroup_t testgroups[] = { { "", test_array }, @@ -1588,6 +1589,7 @@ static struct testgroup_t testgroups[] = { { "introduce/", introduce_tests }, { "circuitlist/", circuitlist_tests }, { "options/", options_tests }, + { "extorport/", extorport_tests }, END_OF_GROUPS }; diff --git a/src/test/test_extorport.c b/src/test/test_extorport.c new file mode 100644 index 0000000000000000000000000000000000000000..cfe810ef0bc53f026e65b7c609d330a1e9b24b51 --- /dev/null +++ b/src/test/test_extorport.c @@ -0,0 +1,65 @@ +/* Copyright (c) 2013, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#define CONNECTION_PRIVATE +#include "or.h" +#include "connection.h" +#include "ext_orport.h" +#include "test.h" + +/* Test connection_or_remove_from_ext_or_id_map and + * connection_or_set_ext_or_identifier */ +static void +test_ext_or_id_map(void *arg) +{ + or_connection_t *c1 = NULL, *c2 = NULL, *c3 = NULL; + char *idp = NULL, *idp2 = NULL; + (void)arg; + + /* pre-initialization */ + tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx")); + + c1 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET); + c2 = or_connection_new(CONN_TYPE_EXT_OR, AF_INET); + c3 = or_connection_new(CONN_TYPE_OR, AF_INET); + + tt_ptr_op(c1->ext_or_conn_id, !=, NULL); + tt_ptr_op(c2->ext_or_conn_id, !=, NULL); + tt_ptr_op(c3->ext_or_conn_id, ==, NULL); + + tt_ptr_op(c1, ==, connection_or_get_by_ext_or_id(c1->ext_or_conn_id)); + tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(c2->ext_or_conn_id)); + tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id("xxxxxxxxxxxxxxxxxxxx")); + + idp = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN); + + /* Give c2 a new ID. */ + connection_or_set_ext_or_identifier(c2); + test_mem_op(idp, !=, c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN); + idp2 = tor_memdup(c2->ext_or_conn_id, EXT_OR_CONN_ID_LEN); + tt_assert(!tor_digest_is_zero(idp2)); + + tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp)); + tt_ptr_op(c2, ==, connection_or_get_by_ext_or_id(idp2)); + + /* Now remove it. */ + connection_or_remove_from_ext_or_id_map(c2); + tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp)); + tt_ptr_op(NULL, ==, connection_or_get_by_ext_or_id(idp2)); + + done: + if (c1) + connection_free_(TO_CONN(c1)); + if (c2) + connection_free_(TO_CONN(c2)); + if (c3) + connection_free_(TO_CONN(c3)); + tor_free(idp); + tor_free(idp2); + connection_or_clear_ext_or_id_map(); +} + +struct testcase_t extorport_tests[] = { + { "id_map", test_ext_or_id_map, TT_FORK, NULL, NULL }, + END_OF_TESTCASES +}; diff --git a/src/test/test_options.c b/src/test/test_options.c index 6beff2567e6eb3f3fa08026f9d3665f6cf68b3e0..737f658e2ccb5f67339d77df271252de1f930c1a 100644 --- a/src/test/test_options.c +++ b/src/test/test_options.c @@ -148,6 +148,8 @@ test_options_validate(void *arg) (void)arg; setup_log_callback(); + WANT_ERR("ExtORPort 500000", "Invalid ExtORPort"); + WANT_ERR_LOG("ServerTransportOptions trebuchet", "ServerTransportOptions did not parse", LOG_WARN, "Too few arguments"); @@ -157,7 +159,6 @@ test_options_validate(void *arg) "ServerTransportOptions did not parse", LOG_WARN, "\"slingsnappy\" is not a k=v"); -// done: clear_log_messages(); return; }