2017-03-15 21:13:17 +01:00
|
|
|
/* Copyright (c) 2014-2017, The Tor Project, Inc. */
|
2014-01-23 01:51:44 +01:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
2016-07-28 16:47:46 +02:00
|
|
|
#include "orconfig.h"
|
|
|
|
|
2014-01-23 14:59:46 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2014-01-23 01:51:44 +01:00
|
|
|
#define TOR_CHANNEL_INTERNAL_
|
|
|
|
#include "or.h"
|
2014-01-23 14:59:46 +01:00
|
|
|
#include "address.h"
|
2014-01-23 15:56:18 +01:00
|
|
|
#include "buffers.h"
|
2014-01-23 01:51:44 +01:00
|
|
|
#include "channel.h"
|
|
|
|
#include "channeltls.h"
|
2014-01-23 14:59:46 +01:00
|
|
|
#include "connection_or.h"
|
|
|
|
#include "config.h"
|
2014-01-23 01:51:44 +01:00
|
|
|
/* For init/free stuff */
|
|
|
|
#include "scheduler.h"
|
2014-01-23 14:59:46 +01:00
|
|
|
#include "tortls.h"
|
2014-01-23 01:51:44 +01:00
|
|
|
|
|
|
|
/* Test suite stuff */
|
|
|
|
#include "test.h"
|
|
|
|
#include "fakechans.h"
|
|
|
|
|
2014-01-23 14:59:46 +01:00
|
|
|
/* The channeltls unit tests */
|
|
|
|
static void test_channeltls_create(void *arg);
|
2014-01-23 15:56:18 +01:00
|
|
|
static void test_channeltls_num_bytes_queued(void *arg);
|
2014-01-23 14:59:46 +01:00
|
|
|
static void test_channeltls_overhead_estimate(void *arg);
|
|
|
|
|
|
|
|
/* Mocks used by channeltls unit tests */
|
2014-01-23 15:56:18 +01:00
|
|
|
static size_t tlschan_buf_datalen_mock(const buf_t *buf);
|
2014-01-23 14:59:46 +01:00
|
|
|
static or_connection_t * tlschan_connection_or_connect_mock(
|
|
|
|
const tor_addr_t *addr,
|
|
|
|
uint16_t port,
|
|
|
|
const char *digest,
|
2016-08-30 15:44:42 +02:00
|
|
|
const ed25519_public_key_t *ed_id,
|
2014-01-23 14:59:46 +01:00
|
|
|
channel_tls_t *tlschan);
|
|
|
|
static int tlschan_is_local_addr_mock(const tor_addr_t *addr);
|
|
|
|
|
|
|
|
/* Fake close method */
|
|
|
|
static void tlschan_fake_close_method(channel_t *chan);
|
|
|
|
|
|
|
|
/* Flags controlling behavior of channeltls unit test mocks */
|
|
|
|
static int tlschan_local = 0;
|
2014-01-23 15:56:18 +01:00
|
|
|
static const buf_t * tlschan_buf_datalen_mock_target = NULL;
|
|
|
|
static size_t tlschan_buf_datalen_mock_size = 0;
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
/* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
|
|
|
|
static int fake_tortls = 0; /* Bleh... */
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_channeltls_create(void *arg)
|
|
|
|
{
|
|
|
|
tor_addr_t test_addr;
|
|
|
|
channel_t *ch = NULL;
|
|
|
|
const char test_digest[DIGEST_LEN] = {
|
|
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
|
|
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Set up a fake address to fake-connect to */
|
|
|
|
test_addr.family = AF_INET;
|
|
|
|
test_addr.addr.in_addr.s_addr = htonl(0x01020304);
|
|
|
|
|
|
|
|
/* For this test we always want the address to be treated as non-local */
|
|
|
|
tlschan_local = 0;
|
|
|
|
/* Install is_local_addr() mock */
|
|
|
|
MOCK(is_local_addr, tlschan_is_local_addr_mock);
|
|
|
|
|
|
|
|
/* Install mock for connection_or_connect() */
|
|
|
|
MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
|
|
|
|
|
|
|
|
/* Try connecting */
|
2016-08-30 15:44:42 +02:00
|
|
|
ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (ch) {
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
/*
|
|
|
|
* Use fake close method that doesn't try to do too much to fake
|
|
|
|
* orconn
|
|
|
|
*/
|
|
|
|
ch->close = tlschan_fake_close_method;
|
|
|
|
channel_mark_for_close(ch);
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(ch);
|
2014-01-23 14:59:46 +01:00
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNMOCK(connection_or_connect);
|
|
|
|
UNMOCK(is_local_addr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 15:56:18 +01:00
|
|
|
static void
|
|
|
|
test_channeltls_num_bytes_queued(void *arg)
|
|
|
|
{
|
|
|
|
tor_addr_t test_addr;
|
|
|
|
channel_t *ch = NULL;
|
|
|
|
const char test_digest[DIGEST_LEN] = {
|
|
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
|
|
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
|
|
|
|
channel_tls_t *tlschan = NULL;
|
|
|
|
size_t len;
|
2014-01-23 16:11:13 +01:00
|
|
|
int fake_outbuf = 0, n;
|
2014-01-23 15:56:18 +01:00
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Set up a fake address to fake-connect to */
|
|
|
|
test_addr.family = AF_INET;
|
|
|
|
test_addr.addr.in_addr.s_addr = htonl(0x01020304);
|
|
|
|
|
|
|
|
/* For this test we always want the address to be treated as non-local */
|
|
|
|
tlschan_local = 0;
|
|
|
|
/* Install is_local_addr() mock */
|
|
|
|
MOCK(is_local_addr, tlschan_is_local_addr_mock);
|
|
|
|
|
|
|
|
/* Install mock for connection_or_connect() */
|
|
|
|
MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
|
|
|
|
|
|
|
|
/* Try connecting */
|
2016-08-30 15:44:42 +02:00
|
|
|
ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-01-23 15:56:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, we have to test ch->num_bytes_queued, which is
|
|
|
|
* channel_tls_num_bytes_queued_method. We can't mock
|
2015-12-10 16:19:43 +01:00
|
|
|
* connection_get_outbuf_len() directly because it's static inline
|
2016-08-02 19:59:47 +02:00
|
|
|
* in connection.h, but we can mock buf_datalen().
|
2014-01-23 15:56:18 +01:00
|
|
|
*/
|
|
|
|
|
2014-09-23 16:18:57 +02:00
|
|
|
tt_assert(ch->num_bytes_queued != NULL);
|
2014-01-23 15:56:18 +01:00
|
|
|
tlschan = BASE_CHAN_TO_TLS(ch);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(tlschan, OP_NE, NULL);
|
2014-01-23 15:56:18 +01:00
|
|
|
if (TO_CONN(tlschan->conn)->outbuf == NULL) {
|
|
|
|
/* We need an outbuf to make sure buf_datalen() gets called */
|
|
|
|
fake_outbuf = 1;
|
|
|
|
TO_CONN(tlschan->conn)->outbuf = buf_new();
|
|
|
|
}
|
|
|
|
tlschan_buf_datalen_mock_target = TO_CONN(tlschan->conn)->outbuf;
|
|
|
|
tlschan_buf_datalen_mock_size = 1024;
|
|
|
|
MOCK(buf_datalen, tlschan_buf_datalen_mock);
|
|
|
|
len = ch->num_bytes_queued(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(len, OP_EQ, tlschan_buf_datalen_mock_size);
|
2014-01-23 16:11:13 +01:00
|
|
|
/*
|
|
|
|
* We also cover num_cells_writeable here; since wide_circ_ids = 0 on
|
|
|
|
* the fake tlschans, cell_network_size returns 512, and so with
|
|
|
|
* tlschan_buf_datalen_mock_size == 1024, we should be able to write
|
|
|
|
* ceil((OR_CONN_HIGHWATER - 1024) / 512) = ceil(OR_CONN_HIGHWATER / 512)
|
|
|
|
* - 2 cells.
|
|
|
|
*/
|
|
|
|
n = ch->num_cells_writeable(ch);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(n, OP_EQ, CEIL_DIV(OR_CONN_HIGHWATER, 512) - 2);
|
2014-01-23 15:56:18 +01:00
|
|
|
UNMOCK(buf_datalen);
|
|
|
|
tlschan_buf_datalen_mock_target = NULL;
|
|
|
|
tlschan_buf_datalen_mock_size = 0;
|
|
|
|
if (fake_outbuf) {
|
|
|
|
buf_free(TO_CONN(tlschan->conn)->outbuf);
|
|
|
|
TO_CONN(tlschan->conn)->outbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (ch) {
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
/*
|
|
|
|
* Use fake close method that doesn't try to do too much to fake
|
|
|
|
* orconn
|
|
|
|
*/
|
|
|
|
ch->close = tlschan_fake_close_method;
|
|
|
|
channel_mark_for_close(ch);
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(ch);
|
2014-01-23 15:56:18 +01:00
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNMOCK(connection_or_connect);
|
|
|
|
UNMOCK(is_local_addr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 14:59:46 +01:00
|
|
|
static void
|
|
|
|
test_channeltls_overhead_estimate(void *arg)
|
|
|
|
{
|
|
|
|
tor_addr_t test_addr;
|
|
|
|
channel_t *ch = NULL;
|
|
|
|
const char test_digest[DIGEST_LEN] = {
|
|
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
|
|
0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
|
2016-05-30 18:54:31 +02:00
|
|
|
double r;
|
2014-01-23 14:59:46 +01:00
|
|
|
channel_tls_t *tlschan = NULL;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
/* Set up a fake address to fake-connect to */
|
|
|
|
test_addr.family = AF_INET;
|
|
|
|
test_addr.addr.in_addr.s_addr = htonl(0x01020304);
|
|
|
|
|
|
|
|
/* For this test we always want the address to be treated as non-local */
|
|
|
|
tlschan_local = 0;
|
|
|
|
/* Install is_local_addr() mock */
|
|
|
|
MOCK(is_local_addr, tlschan_is_local_addr_mock);
|
|
|
|
|
|
|
|
/* Install mock for connection_or_connect() */
|
|
|
|
MOCK(connection_or_connect, tlschan_connection_or_connect_mock);
|
|
|
|
|
|
|
|
/* Try connecting */
|
2016-08-30 15:44:42 +02:00
|
|
|
ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(ch, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
2016-05-30 19:57:32 +02:00
|
|
|
/* First case: silly low ratios should get clamped to 1.0 */
|
2014-01-23 14:59:46 +01:00
|
|
|
tlschan = BASE_CHAN_TO_TLS(ch);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(tlschan, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
tlschan->conn->bytes_xmitted = 128;
|
|
|
|
tlschan->conn->bytes_xmitted_by_tls = 64;
|
|
|
|
r = ch->get_overhead_estimate(ch);
|
2016-05-30 19:57:32 +02:00
|
|
|
tt_assert(fabs(r - 1.0) < 1E-12);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
tlschan->conn->bytes_xmitted_by_tls = 127;
|
|
|
|
r = ch->get_overhead_estimate(ch);
|
2016-05-30 19:57:32 +02:00
|
|
|
tt_assert(fabs(r - 1.0) < 1E-12);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
/* Now middle of the range */
|
|
|
|
tlschan->conn->bytes_xmitted_by_tls = 192;
|
|
|
|
r = ch->get_overhead_estimate(ch);
|
2016-05-30 19:57:32 +02:00
|
|
|
tt_assert(fabs(r - 1.5) < 1E-12);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
2016-05-30 19:57:32 +02:00
|
|
|
/* Now above the 2.0 clamp */
|
2014-01-23 14:59:46 +01:00
|
|
|
tlschan->conn->bytes_xmitted_by_tls = 257;
|
|
|
|
r = ch->get_overhead_estimate(ch);
|
2016-05-30 19:57:32 +02:00
|
|
|
tt_assert(fabs(r - 2.0) < 1E-12);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
tlschan->conn->bytes_xmitted_by_tls = 512;
|
|
|
|
r = ch->get_overhead_estimate(ch);
|
2016-05-30 19:57:32 +02:00
|
|
|
tt_assert(fabs(r - 2.0) < 1E-12);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
if (ch) {
|
|
|
|
MOCK(scheduler_release_channel, scheduler_release_channel_mock);
|
|
|
|
/*
|
|
|
|
* Use fake close method that doesn't try to do too much to fake
|
|
|
|
* orconn
|
|
|
|
*/
|
|
|
|
ch->close = tlschan_fake_close_method;
|
|
|
|
channel_mark_for_close(ch);
|
2014-12-22 18:27:26 +01:00
|
|
|
free_fake_channel(ch);
|
2014-01-23 14:59:46 +01:00
|
|
|
UNMOCK(scheduler_release_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
UNMOCK(connection_or_connect);
|
|
|
|
UNMOCK(is_local_addr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-23 15:56:18 +01:00
|
|
|
static size_t
|
|
|
|
tlschan_buf_datalen_mock(const buf_t *buf)
|
|
|
|
{
|
|
|
|
if (buf != NULL && buf == tlschan_buf_datalen_mock_target) {
|
|
|
|
return tlschan_buf_datalen_mock_size;
|
2014-09-23 16:18:57 +02:00
|
|
|
} else {
|
2014-01-23 15:56:18 +01:00
|
|
|
return buf_datalen__real(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-23 14:59:46 +01:00
|
|
|
static or_connection_t *
|
|
|
|
tlschan_connection_or_connect_mock(const tor_addr_t *addr,
|
|
|
|
uint16_t port,
|
|
|
|
const char *digest,
|
2016-08-30 15:44:42 +02:00
|
|
|
const ed25519_public_key_t *ed_id,
|
2014-01-23 14:59:46 +01:00
|
|
|
channel_tls_t *tlschan)
|
|
|
|
{
|
|
|
|
or_connection_t *result = NULL;
|
2016-08-30 15:44:42 +02:00
|
|
|
(void) ed_id; // XXXX Not yet used.
|
2014-01-23 14:59:46 +01:00
|
|
|
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(addr, OP_NE, NULL);
|
2017-08-24 21:49:59 +02:00
|
|
|
tt_uint_op(port, OP_NE, 0);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(digest, OP_NE, NULL);
|
|
|
|
tt_ptr_op(tlschan, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
/* Make a fake orconn */
|
|
|
|
result = tor_malloc_zero(sizeof(*result));
|
|
|
|
result->base_.magic = OR_CONNECTION_MAGIC;
|
|
|
|
result->base_.state = OR_CONN_STATE_OPEN;
|
|
|
|
result->base_.type = CONN_TYPE_OR;
|
|
|
|
result->base_.socket_family = addr->family;
|
|
|
|
result->base_.address = tor_strdup("<fake>");
|
|
|
|
memcpy(&(result->base_.addr), addr, sizeof(tor_addr_t));
|
|
|
|
result->base_.port = port;
|
|
|
|
memcpy(result->identity_digest, digest, DIGEST_LEN);
|
|
|
|
result->chan = tlschan;
|
|
|
|
memcpy(&(result->real_addr), addr, sizeof(tor_addr_t));
|
|
|
|
result->tls = (tor_tls_t *)((void *)(&fake_tortls));
|
|
|
|
|
|
|
|
done:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tlschan_fake_close_method(channel_t *chan)
|
|
|
|
{
|
|
|
|
channel_tls_t *tlschan = NULL;
|
|
|
|
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(chan, OP_NE, NULL);
|
2017-06-05 16:23:02 +02:00
|
|
|
tt_int_op(chan->magic, OP_EQ, TLS_CHAN_MAGIC);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
tlschan = BASE_CHAN_TO_TLS(chan);
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(tlschan, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
/* Just free the fake orconn */
|
2014-12-22 18:27:26 +01:00
|
|
|
tor_free(tlschan->conn->base_.address);
|
2014-01-23 14:59:46 +01:00
|
|
|
tor_free(tlschan->conn);
|
|
|
|
|
|
|
|
channel_closed(chan);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tlschan_is_local_addr_mock(const tor_addr_t *addr)
|
|
|
|
{
|
2017-08-24 21:55:27 +02:00
|
|
|
tt_ptr_op(addr, OP_NE, NULL);
|
2014-01-23 14:59:46 +01:00
|
|
|
|
|
|
|
done:
|
|
|
|
return tlschan_local;
|
|
|
|
}
|
|
|
|
|
2014-01-23 01:51:44 +01:00
|
|
|
struct testcase_t channeltls_tests[] = {
|
2014-01-23 14:59:46 +01:00
|
|
|
{ "create", test_channeltls_create, TT_FORK, NULL, NULL },
|
2014-01-23 15:56:18 +01:00
|
|
|
{ "num_bytes_queued", test_channeltls_num_bytes_queued,
|
|
|
|
TT_FORK, NULL, NULL },
|
2014-01-23 14:59:46 +01:00
|
|
|
{ "overhead_estimate", test_channeltls_overhead_estimate,
|
|
|
|
TT_FORK, NULL, NULL },
|
2014-01-23 01:51:44 +01:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|
|
|
|
|