2019-01-16 18:33:22 +01:00
|
|
|
/* Copyright (c) 2013-2019, The Tor Project, Inc. */
|
2013-07-10 21:07:32 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#define TOR_CHANNEL_INTERNAL_
|
2014-05-07 08:57:50 +02:00
|
|
|
#define CIRCUITBUILD_PRIVATE
|
2013-07-10 21:07:32 +02:00
|
|
|
#define CIRCUITLIST_PRIVATE
|
2016-09-05 17:54:59 +02:00
|
|
|
#define HS_CIRCUITMAP_PRIVATE
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or.h"
|
|
|
|
#include "core/or/channel.h"
|
|
|
|
#include "core/or/circuitbuild.h"
|
|
|
|
#include "core/or/circuitlist.h"
|
|
|
|
#include "core/or/circuitmux_ewma.h"
|
|
|
|
#include "feature/hs/hs_circuitmap.h"
|
2018-06-20 15:35:05 +02:00
|
|
|
#include "test/test.h"
|
|
|
|
#include "test/log_test_helpers.h"
|
2013-07-10 21:07:32 +02:00
|
|
|
|
2018-07-05 22:34:59 +02:00
|
|
|
#include "core/or/or_circuit_st.h"
|
|
|
|
#include "core/or/origin_circuit_st.h"
|
2018-06-15 17:34:33 +02:00
|
|
|
|
2018-06-21 22:48:54 +02:00
|
|
|
#include "lib/container/bitarray.h"
|
|
|
|
|
2013-07-10 21:07:32 +02:00
|
|
|
static channel_t *
|
|
|
|
new_fake_channel(void)
|
|
|
|
{
|
|
|
|
channel_t *chan = tor_malloc_zero(sizeof(channel_t));
|
|
|
|
channel_init(chan);
|
|
|
|
return chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int ncalls;
|
|
|
|
void *cmux;
|
|
|
|
void *circ;
|
|
|
|
cell_direction_t dir;
|
|
|
|
} cam;
|
|
|
|
|
|
|
|
static void
|
|
|
|
circuitmux_attach_mock(circuitmux_t *cmux, circuit_t *circ,
|
|
|
|
cell_direction_t dir)
|
|
|
|
{
|
|
|
|
++cam.ncalls;
|
|
|
|
cam.cmux = cmux;
|
|
|
|
cam.circ = circ;
|
|
|
|
cam.dir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
int ncalls;
|
|
|
|
void *cmux;
|
|
|
|
void *circ;
|
|
|
|
} cdm;
|
|
|
|
|
|
|
|
static void
|
|
|
|
circuitmux_detach_mock(circuitmux_t *cmux, circuit_t *circ)
|
|
|
|
{
|
|
|
|
++cdm.ncalls;
|
|
|
|
cdm.cmux = cmux;
|
|
|
|
cdm.circ = circ;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GOT_CMUX_ATTACH(mux_, circ_, dir_) do { \
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(cam.ncalls, OP_EQ, 1); \
|
|
|
|
tt_ptr_op(cam.cmux, OP_EQ, (mux_)); \
|
|
|
|
tt_ptr_op(cam.circ, OP_EQ, (circ_)); \
|
|
|
|
tt_int_op(cam.dir, OP_EQ, (dir_)); \
|
2013-07-10 21:07:32 +02:00
|
|
|
memset(&cam, 0, sizeof(cam)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define GOT_CMUX_DETACH(mux_, circ_) do { \
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(cdm.ncalls, OP_EQ, 1); \
|
|
|
|
tt_ptr_op(cdm.cmux, OP_EQ, (mux_)); \
|
|
|
|
tt_ptr_op(cdm.circ, OP_EQ, (circ_)); \
|
2013-07-10 21:07:32 +02:00
|
|
|
memset(&cdm, 0, sizeof(cdm)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_clist_maps(void *arg)
|
|
|
|
{
|
|
|
|
channel_t *ch1 = new_fake_channel();
|
|
|
|
channel_t *ch2 = new_fake_channel();
|
|
|
|
channel_t *ch3 = new_fake_channel();
|
|
|
|
or_circuit_t *or_c1=NULL, *or_c2=NULL;
|
|
|
|
|
2013-07-15 15:46:36 +02:00
|
|
|
(void) arg;
|
|
|
|
|
2013-07-10 21:07:32 +02:00
|
|
|
MOCK(circuitmux_attach_circuit, circuitmux_attach_mock);
|
|
|
|
MOCK(circuitmux_detach_circuit, circuitmux_detach_mock);
|
|
|
|
memset(&cam, 0, sizeof(cam));
|
|
|
|
memset(&cdm, 0, sizeof(cdm));
|
|
|
|
|
2014-09-03 16:58:11 +02:00
|
|
|
tt_assert(ch1);
|
|
|
|
tt_assert(ch2);
|
|
|
|
tt_assert(ch3);
|
|
|
|
|
2014-09-02 21:40:47 +02:00
|
|
|
ch1->cmux = tor_malloc(1);
|
|
|
|
ch2->cmux = tor_malloc(1);
|
|
|
|
ch3->cmux = tor_malloc(1);
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
or_c1 = or_circuit_new(100, ch2);
|
|
|
|
tt_assert(or_c1);
|
|
|
|
GOT_CMUX_ATTACH(ch2->cmux, or_c1, CELL_DIRECTION_IN);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(or_c1->p_circ_id, OP_EQ, 100);
|
|
|
|
tt_ptr_op(or_c1->p_chan, OP_EQ, ch2);
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
or_c2 = or_circuit_new(100, ch1);
|
|
|
|
tt_assert(or_c2);
|
|
|
|
GOT_CMUX_ATTACH(ch1->cmux, or_c2, CELL_DIRECTION_IN);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(or_c2->p_circ_id, OP_EQ, 100);
|
|
|
|
tt_ptr_op(or_c2->p_chan, OP_EQ, ch1);
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
circuit_set_n_circid_chan(TO_CIRCUIT(or_c1), 200, ch1);
|
|
|
|
GOT_CMUX_ATTACH(ch1->cmux, or_c1, CELL_DIRECTION_OUT);
|
|
|
|
|
|
|
|
circuit_set_n_circid_chan(TO_CIRCUIT(or_c2), 200, ch2);
|
|
|
|
GOT_CMUX_ATTACH(ch2->cmux, or_c2, CELL_DIRECTION_OUT);
|
|
|
|
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(200, ch1), OP_EQ, TO_CIRCUIT(or_c1));
|
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, TO_CIRCUIT(or_c2));
|
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, TO_CIRCUIT(or_c1));
|
2013-07-10 21:07:32 +02:00
|
|
|
/* Try the same thing again, to test the "fast" path. */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, TO_CIRCUIT(or_c1));
|
2013-07-10 21:07:32 +02:00
|
|
|
tt_assert(circuit_id_in_use_on_channel(100, ch2));
|
|
|
|
tt_assert(! circuit_id_in_use_on_channel(101, ch2));
|
|
|
|
|
|
|
|
/* Try changing the circuitid and channel of that circuit. */
|
|
|
|
circuit_set_p_circid_chan(or_c1, 500, ch3);
|
|
|
|
GOT_CMUX_DETACH(ch2->cmux, TO_CIRCUIT(or_c1));
|
|
|
|
GOT_CMUX_ATTACH(ch3->cmux, TO_CIRCUIT(or_c1), CELL_DIRECTION_IN);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch2), OP_EQ, NULL);
|
2013-07-10 21:07:32 +02:00
|
|
|
tt_assert(! circuit_id_in_use_on_channel(100, ch2));
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(500, ch3), OP_EQ, TO_CIRCUIT(or_c1));
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
/* Now let's see about destroy handling. */
|
|
|
|
tt_assert(! circuit_id_in_use_on_channel(205, ch2));
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(200, ch2));
|
|
|
|
channel_note_destroy_pending(ch2, 200);
|
|
|
|
channel_note_destroy_pending(ch2, 205);
|
|
|
|
channel_note_destroy_pending(ch1, 100);
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(205, ch2))
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(200, ch2));
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(100, ch1));
|
|
|
|
|
|
|
|
tt_assert(TO_CIRCUIT(or_c2)->n_delete_pending != 0);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, TO_CIRCUIT(or_c2));
|
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, TO_CIRCUIT(or_c2));
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
/* Okay, now free ch2 and make sure that the circuit ID is STILL not
|
|
|
|
* usable, because we haven't declared the destroy to be nonpending */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(cdm.ncalls, OP_EQ, 0);
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(or_c2));
|
2013-07-10 21:07:32 +02:00
|
|
|
or_c2 = NULL; /* prevent free */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(cdm.ncalls, OP_EQ, 2);
|
2013-07-10 21:07:32 +02:00
|
|
|
memset(&cdm, 0, sizeof(cdm));
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(200, ch2));
|
|
|
|
tt_assert(circuit_id_in_use_on_channel(100, ch1));
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, NULL);
|
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, NULL);
|
2013-07-10 21:07:32 +02:00
|
|
|
|
|
|
|
/* Now say that the destroy is nonpending */
|
|
|
|
channel_note_destroy_not_pending(ch2, 200);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(200, ch2), OP_EQ, NULL);
|
2013-07-10 21:07:32 +02:00
|
|
|
channel_note_destroy_not_pending(ch1, 100);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_ptr_op(circuit_get_by_circid_channel(100, ch1), OP_EQ, NULL);
|
2013-07-10 21:07:32 +02:00
|
|
|
tt_assert(! circuit_id_in_use_on_channel(200, ch2));
|
|
|
|
tt_assert(! circuit_id_in_use_on_channel(100, ch1));
|
|
|
|
|
2013-07-15 23:35:56 +02:00
|
|
|
done:
|
2013-07-10 21:07:32 +02:00
|
|
|
if (or_c1)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(or_c1));
|
2013-07-10 21:07:32 +02:00
|
|
|
if (or_c2)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(or_c2));
|
2014-09-02 21:40:47 +02:00
|
|
|
if (ch1)
|
|
|
|
tor_free(ch1->cmux);
|
|
|
|
if (ch2)
|
|
|
|
tor_free(ch2->cmux);
|
|
|
|
if (ch3)
|
2014-09-03 16:58:11 +02:00
|
|
|
tor_free(ch3->cmux);
|
2014-03-18 14:46:39 +01:00
|
|
|
tor_free(ch1);
|
|
|
|
tor_free(ch2);
|
|
|
|
tor_free(ch3);
|
2013-07-10 21:07:32 +02:00
|
|
|
UNMOCK(circuitmux_attach_circuit);
|
|
|
|
UNMOCK(circuitmux_detach_circuit);
|
|
|
|
}
|
|
|
|
|
2014-02-13 21:24:15 +01:00
|
|
|
static void
|
|
|
|
test_rend_token_maps(void *arg)
|
|
|
|
{
|
|
|
|
or_circuit_t *c1, *c2, *c3, *c4;
|
2017-07-25 11:05:02 +02:00
|
|
|
origin_circuit_t *c5;
|
2014-02-13 21:24:15 +01:00
|
|
|
const uint8_t tok1[REND_TOKEN_LEN] = "The cat can't tell y";
|
|
|
|
const uint8_t tok2[REND_TOKEN_LEN] = "ou its name, and it ";
|
|
|
|
const uint8_t tok3[REND_TOKEN_LEN] = "doesn't really care.";
|
|
|
|
/* -- Adapted from a quote by Fredrik Lundh. */
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
(void)tok1; //xxxx
|
2016-09-05 17:54:59 +02:00
|
|
|
|
|
|
|
hs_circuitmap_init();
|
|
|
|
|
2014-02-13 21:24:15 +01:00
|
|
|
c1 = or_circuit_new(0, NULL);
|
|
|
|
c2 = or_circuit_new(0, NULL);
|
|
|
|
c3 = or_circuit_new(0, NULL);
|
|
|
|
c4 = or_circuit_new(0, NULL);
|
2017-07-25 11:05:02 +02:00
|
|
|
c5 = origin_circuit_new();
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2014-04-09 15:20:25 +02:00
|
|
|
/* Make sure we really filled up the tok* variables */
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(tok1[REND_TOKEN_LEN-1], OP_EQ, 'y');
|
|
|
|
tt_int_op(tok2[REND_TOKEN_LEN-1], OP_EQ, ' ');
|
|
|
|
tt_int_op(tok3[REND_TOKEN_LEN-1], OP_EQ, '.');
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* No maps; nothing there. */
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok1));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2017-01-20 12:55:06 +01:00
|
|
|
hs_circuitmap_register_rend_circ_relay_side(c1, tok1);
|
|
|
|
hs_circuitmap_register_intro_circ_v2_relay_side(c2, tok2);
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok3));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok3));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok2));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok1));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* Without purpose set, we don't get the circuits */
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
c1->base_.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
|
|
|
|
c2->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
|
|
|
|
|
|
|
|
/* Okay, make sure they show up now. */
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(c1, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
|
|
|
tt_ptr_op(c2, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* Two items at the same place with the same token. */
|
|
|
|
c3->base_.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
|
2017-01-20 12:55:06 +01:00
|
|
|
hs_circuitmap_register_rend_circ_relay_side(c3, tok2);
|
|
|
|
tt_ptr_op(c2, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
|
|
|
|
tt_ptr_op(c3, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok2));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* Marking a circuit makes it not get returned any more */
|
|
|
|
circuit_mark_for_close(TO_CIRCUIT(c1), END_CIRC_REASON_FINISHED);
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c1));
|
2014-02-13 21:24:15 +01:00
|
|
|
c1 = NULL;
|
|
|
|
|
|
|
|
/* Freeing a circuit makes it not get returned any more. */
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c2));
|
2014-02-13 21:24:15 +01:00
|
|
|
c2 = NULL;
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* c3 -- are you still there? */
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(c3, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok2));
|
2014-02-13 21:24:15 +01:00
|
|
|
/* Change its cookie. This never happens in Tor per se, but hey. */
|
|
|
|
c3->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
|
2017-01-20 12:55:06 +01:00
|
|
|
hs_circuitmap_register_intro_circ_v2_relay_side(c3, tok3);
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok2));
|
|
|
|
tt_ptr_op(c3, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok3));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
|
|
|
/* Now replace c3 with c4. */
|
|
|
|
c4->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
|
2017-01-20 12:55:06 +01:00
|
|
|
hs_circuitmap_register_intro_circ_v2_relay_side(c4, tok3);
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(c4, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok3));
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2017-01-18 11:01:49 +01:00
|
|
|
tt_ptr_op(TO_CIRCUIT(c3)->hs_token, OP_EQ, NULL);
|
|
|
|
tt_ptr_op(TO_CIRCUIT(c4)->hs_token, OP_NE, NULL);
|
|
|
|
tt_mem_op(TO_CIRCUIT(c4)->hs_token->token, OP_EQ, tok3, REND_TOKEN_LEN);
|
2014-02-13 21:24:15 +01:00
|
|
|
|
2014-04-04 18:06:54 +02:00
|
|
|
/* Now clear c4's cookie. */
|
2017-01-18 11:01:49 +01:00
|
|
|
hs_circuitmap_remove_circuit(TO_CIRCUIT(c4));
|
|
|
|
tt_ptr_op(TO_CIRCUIT(c4)->hs_token, OP_EQ, NULL);
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok3));
|
2014-04-04 18:06:54 +02:00
|
|
|
|
2017-07-25 11:05:02 +02:00
|
|
|
/* Now let's do a check for the client-side rend circuitmap */
|
2017-08-24 14:49:24 +02:00
|
|
|
c5->base_.purpose = CIRCUIT_PURPOSE_C_ESTABLISH_REND;
|
2017-07-25 11:05:02 +02:00
|
|
|
hs_circuitmap_register_rend_circ_client_side(c5, tok1);
|
|
|
|
|
|
|
|
tt_ptr_op(c5, OP_EQ, hs_circuitmap_get_rend_circ_client_side(tok1));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_client_side(tok2));
|
|
|
|
|
2014-02-13 21:24:15 +01:00
|
|
|
done:
|
2014-04-17 07:03:10 +02:00
|
|
|
if (c1)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c1));
|
2014-04-17 07:03:10 +02:00
|
|
|
if (c2)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c2));
|
2014-04-17 07:03:10 +02:00
|
|
|
if (c3)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c3));
|
2014-04-17 07:03:10 +02:00
|
|
|
if (c4)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c4));
|
2017-07-25 11:05:02 +02:00
|
|
|
if (c5)
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(c5));
|
2014-02-13 21:24:15 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 20:04:55 +02:00
|
|
|
static void
|
|
|
|
mock_channel_dump_statistics(channel_t *chan, int severity)
|
|
|
|
{
|
|
|
|
(void)chan;
|
|
|
|
(void)severity;
|
|
|
|
}
|
|
|
|
|
2014-05-07 08:57:50 +02:00
|
|
|
static void
|
|
|
|
test_pick_circid(void *arg)
|
|
|
|
{
|
|
|
|
bitarray_t *ba = NULL;
|
|
|
|
channel_t *chan1, *chan2;
|
|
|
|
circid_t circid;
|
|
|
|
int i;
|
|
|
|
(void) arg;
|
2016-09-08 20:04:55 +02:00
|
|
|
|
|
|
|
MOCK(channel_dump_statistics, mock_channel_dump_statistics);
|
2014-05-07 08:57:50 +02:00
|
|
|
|
|
|
|
chan1 = tor_malloc_zero(sizeof(channel_t));
|
|
|
|
chan2 = tor_malloc_zero(sizeof(channel_t));
|
|
|
|
chan2->wide_circ_ids = 1;
|
|
|
|
|
2016-09-08 20:04:55 +02:00
|
|
|
chan1->cmux = circuitmux_alloc();
|
|
|
|
chan2->cmux = circuitmux_alloc();
|
|
|
|
|
|
|
|
/* CIRC_ID_TYPE_NEITHER is supposed to create a warning. */
|
2014-05-07 08:57:50 +02:00
|
|
|
chan1->circ_id_type = CIRC_ID_TYPE_NEITHER;
|
2016-09-08 21:01:32 +02:00
|
|
|
setup_full_capture_of_logs(LOG_WARN);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(0, OP_EQ, get_unique_circ_id_by_chan(chan1));
|
2016-09-08 20:04:55 +02:00
|
|
|
expect_single_log_msg_containing("Trying to pick a circuit ID for a "
|
|
|
|
"connection from a client with no identity.");
|
2016-09-08 21:01:32 +02:00
|
|
|
teardown_capture_of_logs();
|
2014-05-07 08:57:50 +02:00
|
|
|
|
|
|
|
/* Basic tests, with no collisions */
|
|
|
|
chan1->circ_id_type = CIRC_ID_TYPE_LOWER;
|
|
|
|
for (i = 0; i < 50; ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op(0, OP_LT, circid);
|
|
|
|
tt_uint_op(circid, OP_LT, (1<<15));
|
2014-05-07 08:57:50 +02:00
|
|
|
}
|
|
|
|
chan1->circ_id_type = CIRC_ID_TYPE_HIGHER;
|
|
|
|
for (i = 0; i < 50; ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan1);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op((1<<15), OP_LT, circid);
|
|
|
|
tt_uint_op(circid, OP_LT, (1<<16));
|
2014-05-07 08:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
chan2->circ_id_type = CIRC_ID_TYPE_LOWER;
|
|
|
|
for (i = 0; i < 50; ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan2);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op(0, OP_LT, circid);
|
|
|
|
tt_uint_op(circid, OP_LT, (1u<<31));
|
2014-05-07 08:57:50 +02:00
|
|
|
}
|
|
|
|
chan2->circ_id_type = CIRC_ID_TYPE_HIGHER;
|
|
|
|
for (i = 0; i < 50; ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan2);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op((1u<<31), OP_LT, circid);
|
2014-05-07 08:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now make sure that we can behave well when we are full up on circuits */
|
|
|
|
chan1->circ_id_type = CIRC_ID_TYPE_LOWER;
|
|
|
|
chan2->circ_id_type = CIRC_ID_TYPE_LOWER;
|
|
|
|
chan1->wide_circ_ids = chan2->wide_circ_ids = 0;
|
|
|
|
ba = bitarray_init_zero((1<<15));
|
|
|
|
for (i = 0; i < (1<<15); ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan1);
|
|
|
|
if (circid == 0) {
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(i, OP_GT, (1<<14));
|
2014-05-07 08:57:50 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op(circid, OP_LT, (1<<15));
|
2014-05-07 08:57:50 +02:00
|
|
|
tt_assert(! bitarray_is_set(ba, circid));
|
|
|
|
bitarray_set(ba, circid);
|
|
|
|
channel_mark_circid_unusable(chan1, circid);
|
|
|
|
}
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_int_op(i, OP_LT, (1<<15));
|
2014-05-07 08:57:50 +02:00
|
|
|
/* Make sure that being full on chan1 does not interfere with chan2 */
|
|
|
|
for (i = 0; i < 100; ++i) {
|
|
|
|
circid = get_unique_circ_id_by_chan(chan2);
|
2014-11-12 19:28:07 +01:00
|
|
|
tt_uint_op(circid, OP_GT, 0);
|
|
|
|
tt_uint_op(circid, OP_LT, (1<<15));
|
2014-05-07 08:57:50 +02:00
|
|
|
channel_mark_circid_unusable(chan2, circid);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2016-09-09 21:29:57 +02:00
|
|
|
circuitmux_free(chan1->cmux);
|
|
|
|
circuitmux_free(chan2->cmux);
|
2014-05-07 08:57:50 +02:00
|
|
|
tor_free(chan1);
|
|
|
|
tor_free(chan2);
|
|
|
|
bitarray_free(ba);
|
|
|
|
circuit_free_all();
|
2016-09-08 21:01:32 +02:00
|
|
|
teardown_capture_of_logs();
|
2016-09-08 20:04:55 +02:00
|
|
|
UNMOCK(channel_dump_statistics);
|
2014-05-07 08:57:50 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:55:06 +01:00
|
|
|
/** Test that the circuit pools of our HS circuitmap are isolated based on
|
|
|
|
* their token type. */
|
|
|
|
static void
|
|
|
|
test_hs_circuitmap_isolation(void *arg)
|
|
|
|
{
|
|
|
|
or_circuit_t *circ1 = NULL;
|
|
|
|
origin_circuit_t *circ2 = NULL;
|
|
|
|
or_circuit_t *circ3 = NULL;
|
|
|
|
origin_circuit_t *circ4 = NULL;
|
|
|
|
|
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
hs_circuitmap_init();
|
|
|
|
|
|
|
|
{
|
|
|
|
const uint8_t tok1[REND_TOKEN_LEN] = "bet i got some of th";
|
|
|
|
|
|
|
|
circ1 = or_circuit_new(0, NULL);
|
2017-04-17 15:36:55 +02:00
|
|
|
tt_assert(circ1);
|
2017-01-20 12:55:06 +01:00
|
|
|
circ1->base_.purpose = CIRCUIT_PURPOSE_REND_POINT_WAITING;
|
|
|
|
|
|
|
|
/* check that circuitmap is empty right? */
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
|
|
|
|
|
|
|
/* Register circ1 with tok1 as relay-side rend circ */
|
|
|
|
hs_circuitmap_register_rend_circ_relay_side(circ1, tok1);
|
|
|
|
|
|
|
|
/* check that service-side getters don't work */
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_service_side(tok1));
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_intro_circ_v2_service_side(tok1));
|
|
|
|
|
|
|
|
/* Check that the right getter works. */
|
|
|
|
tt_ptr_op(circ1, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok1));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const uint8_t tok2[REND_TOKEN_LEN] = "you dont know anythi";
|
|
|
|
|
|
|
|
circ2 = origin_circuit_new();
|
2017-04-17 15:36:55 +02:00
|
|
|
tt_assert(circ2);
|
2017-01-20 12:55:06 +01:00
|
|
|
circ2->base_.purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
|
|
|
|
circ3 = or_circuit_new(0, NULL);
|
2017-04-17 15:36:55 +02:00
|
|
|
tt_assert(circ3);
|
2017-01-20 12:55:06 +01:00
|
|
|
circ3->base_.purpose = CIRCUIT_PURPOSE_INTRO_POINT;
|
|
|
|
circ4 = origin_circuit_new();
|
2017-04-17 15:36:55 +02:00
|
|
|
tt_assert(circ4);
|
2017-01-20 12:55:06 +01:00
|
|
|
circ4->base_.purpose = CIRCUIT_PURPOSE_S_ESTABLISH_INTRO;
|
|
|
|
|
|
|
|
/* Register circ2 with tok2 as service-side intro v2 circ */
|
|
|
|
hs_circuitmap_register_intro_circ_v2_service_side(circ2, tok2);
|
|
|
|
/* Register circ3 with tok2 again but for different purpose */
|
|
|
|
hs_circuitmap_register_intro_circ_v2_relay_side(circ3, tok2);
|
|
|
|
|
|
|
|
/* Check that the getters work */
|
2017-04-13 20:36:57 +02:00
|
|
|
tt_ptr_op(circ2, OP_EQ,
|
|
|
|
hs_circuitmap_get_intro_circ_v2_service_side(tok2));
|
2017-01-20 12:55:06 +01:00
|
|
|
tt_ptr_op(circ3, OP_EQ, hs_circuitmap_get_intro_circ_v2_relay_side(tok2));
|
|
|
|
|
|
|
|
/* Register circ4 with tok2: it should override circ2 */
|
|
|
|
hs_circuitmap_register_intro_circ_v2_service_side(circ4, tok2);
|
|
|
|
|
|
|
|
/* check that relay-side getters don't work */
|
|
|
|
tt_ptr_op(NULL, OP_EQ, hs_circuitmap_get_rend_circ_relay_side(tok2));
|
|
|
|
|
|
|
|
/* Check that the getter returns circ4; the last circuit registered with
|
|
|
|
* that token. */
|
2017-04-13 20:36:57 +02:00
|
|
|
tt_ptr_op(circ4, OP_EQ,
|
|
|
|
hs_circuitmap_get_intro_circ_v2_service_side(tok2));
|
2017-01-20 12:55:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2017-11-21 14:29:42 +01:00
|
|
|
circuit_free_(TO_CIRCUIT(circ1));
|
|
|
|
circuit_free_(TO_CIRCUIT(circ2));
|
|
|
|
circuit_free_(TO_CIRCUIT(circ3));
|
|
|
|
circuit_free_(TO_CIRCUIT(circ4));
|
2017-01-20 12:55:06 +01:00
|
|
|
}
|
|
|
|
|
2013-07-10 21:07:32 +02:00
|
|
|
struct testcase_t circuitlist_tests[] = {
|
|
|
|
{ "maps", test_clist_maps, TT_FORK, NULL, NULL },
|
2014-02-13 21:24:15 +01:00
|
|
|
{ "rend_token_maps", test_rend_token_maps, TT_FORK, NULL, NULL },
|
2014-05-07 08:57:50 +02:00
|
|
|
{ "pick_circid", test_pick_circid, TT_FORK, NULL, NULL },
|
2017-01-20 12:55:06 +01:00
|
|
|
{ "hs_circuitmap_isolation", test_hs_circuitmap_isolation,
|
|
|
|
TT_FORK, NULL, NULL },
|
2013-07-10 21:07:32 +02:00
|
|
|
END_OF_TESTCASES
|
|
|
|
};
|