Skip to content

Commit

Permalink
Merge branch 'ticket33919'
Browse files Browse the repository at this point in the history
  • Loading branch information
nmathewson committed Jul 13, 2020
2 parents b119c55 + 5913305 commit 0c91c63
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 9 deletions.
3 changes: 3 additions & 0 deletions changes/ticket33919
@@ -0,0 +1,3 @@
o Minor features (testing):
- Added unit tests for channel_matches_target_addr_for_extend().
Closes Ticket 33919. Patch by MrSquanchee.
9 changes: 1 addition & 8 deletions src/core/or/channel.c
Expand Up @@ -83,13 +83,6 @@

#include "core/or/cell_queue_st.h"

/* Static function prototypes */

static bool channel_matches_target_addr_for_extend(
channel_t *chan,
const tor_addr_t *target_ipv4_addr,
const tor_addr_t *target_ipv6_addr);

/* Global lists of channels */

/* All channel_t instances */
Expand Down Expand Up @@ -3317,7 +3310,7 @@ channel_matches_extend_info(channel_t *chan, extend_info_t *extend_info)
* This function calls into the lower layer and asks if this channel thinks
* it matches the target addresses for circuit extension purposes.
*/
static bool
STATIC bool
channel_matches_target_addr_for_extend(channel_t *chan,
const tor_addr_t *target_ipv4_addr,
const tor_addr_t *target_ipv6_addr)
Expand Down
5 changes: 4 additions & 1 deletion src/core/or/channel.h
Expand Up @@ -562,7 +562,10 @@ void channel_listener_dumpstats(int severity);
#ifdef CHANNEL_FILE_PRIVATE

STATIC void channel_add_to_digest_map(channel_t *chan);

STATIC bool channel_matches_target_addr_for_extend(
channel_t *chan,
const tor_addr_t *target_ipv4_addr,
const tor_addr_t *target_ipv6_addr);
#endif /* defined(CHANNEL_FILE_PRIVATE) */

/* Channel operations for subclasses and internal use only */
Expand Down
56 changes: 56 additions & 0 deletions src/test/test_channel.c
Expand Up @@ -16,6 +16,10 @@
/* For packed_cell stuff */
#define RELAY_PRIVATE
#include "core/or/relay.h"
/* For channel_tls_t object and private functions. */
#define CHANNEL_OBJECT_PRIVATE
#define CHANNELTLS_PRIVATE
#include "core/or/channeltls.h"
/* For init/free stuff */
#include "core/or/scheduler.h"
#include "feature/nodelist/networkstatus.h"
Expand All @@ -25,6 +29,8 @@
#include "core/or/origin_circuit_st.h"
#include "feature/nodelist/routerstatus_st.h"
#include "core/or/var_cell_st.h"
#include "core/or/or_connection_st.h"
#include "lib/net/inaddr.h"

/* Test suite stuff */
#include "test/log_test_helpers.h"
Expand Down Expand Up @@ -1537,6 +1543,54 @@ test_channel_listener(void *arg)
channel_free_all();
}

#define TEST_SETUP_MATCHES_ADDR(orcon, addr, src, rv) STMT_BEGIN \
rv = tor_inet_pton(addr.family, src, &addr.addr); \
tt_int_op(rv, OP_EQ, 1); \
orcon->real_addr = addr; \
STMT_END;

#define TEST_MATCHES_ADDR(chan, addr4, addr6, rv, exp) STMT_BEGIN \
rv = channel_matches_target_addr_for_extend(chan, addr4, addr6); \
tt_int_op(rv, OP_EQ, exp); \
STMT_END;

static void
test_channel_matches_target_addr_for_extend(void *arg)
{
(void) arg;

channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
or_connection_t *orcon = tor_malloc_zero(sizeof(*orcon));
channel_t *chan = &(tlschan->base_);
tor_addr_t addr;
int rv;

tlschan->conn = orcon;
channel_tls_common_init(tlschan);

/* Test for IPv4 addresses. */
addr.family = AF_INET;
TEST_SETUP_MATCHES_ADDR(orcon, addr, "1.2.3.4", rv);
TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 1);

tor_inet_pton(addr.family, "2.5.3.4", &addr.addr);
TEST_MATCHES_ADDR(chan, &addr, NULL, rv, 0);

/* Test for IPv6 addresses. */
addr.family = AF_INET6;
TEST_SETUP_MATCHES_ADDR(orcon, addr, "3:4:7:1:9:8:09:10", rv);
TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 1);

tor_inet_pton(addr.family, "::", &addr.addr);
TEST_MATCHES_ADDR(chan, NULL, &addr, rv, 0);

done:
circuitmux_clear_policy(chan->cmux);
circuitmux_free(chan->cmux);
tor_free(orcon);
tor_free(tlschan);
}

struct testcase_t channel_tests[] = {
{ "inbound_cell", test_channel_inbound_cell, TT_FORK,
NULL, NULL },
Expand All @@ -1558,5 +1612,7 @@ struct testcase_t channel_tests[] = {
NULL, NULL },
{ "listener", test_channel_listener, TT_FORK,
NULL, NULL },
{ "matches_target", test_channel_matches_target_addr_for_extend, TT_FORK,
NULL, NULL },
END_OF_TESTCASES
};

0 comments on commit 0c91c63

Please sign in to comment.