#define CIRCUITLIST_PRIVATE
#define NETWORKSTATUS_PRIVATE
#define HS_DOS_PRIVATE
#define HS_INTROPOINT_PRIVATE
#include "test/test.h"
#include "test/test_helpers.h"
#include "test/log_test_helpers.h"
#include "app/config/config.h"
#include "core/or/circuitlist.h"
#include "core/or/circuituse.h"
#include "core/or/or_circuit_st.h"
#include "feature/hs/hs_dos.h"
#include "feature/hs/hs_intropoint.h"
#include "feature/nodelist/networkstatus.h"
static void
setup_mock_consensus(void)
{
current_ns_consensus = tor_malloc_zero(sizeof(networkstatus_t));
current_ns_consensus->net_params = smartlist_new();
smartlist_add(current_ns_consensus->net_params,
(void *) "HiddenServiceEnableIntroDoSDefense=1");
hs_dos_consensus_has_changed(current_ns_consensus);
}
static void
free_mock_consensus(void)
{
smartlist_free(current_ns_consensus->net_params);
tor_free(current_ns_consensus);
}
static void
test_can_send_intro2(void *arg)
{
uint32_t now = (uint32_t) approx_time();
or_circuit_t *or_circ = NULL;
(void) arg;
hs_init();
hs_dos_init();
get_options_mutable()->ORPort_set = 1;
setup_mock_consensus();
or_circ = or_circuit_new(1, NULL);
circuit_change_purpose(TO_CIRCUIT(or_circ), CIRCUIT_PURPOSE_INTRO_POINT);
hs_dos_setup_default_intro2_defenses(or_circ);
or_circ->introduce2_dos_defense_enabled = 1;
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
update_approx_time(++now);
for (int i = 0; i < 10; i++) {
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
}
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
get_intro2_burst_consensus_param(NULL) - 10);
update_approx_time(++now);
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
get_intro2_burst_consensus_param(NULL) - 1);
for (int i = 0; i < 10; i++) {
update_approx_time(++now);
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
}
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
get_intro2_burst_consensus_param(NULL) - 1);
token_bucket_ctr_reset(&or_circ->introduce2_bucket, now);
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
get_intro2_burst_consensus_param(NULL));
for (uint32_t i = 0; i < get_intro2_burst_consensus_param(NULL) - 1; i++) {
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
}
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 1);
tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
for (int i = 0; i < 100; i++) {
tt_int_op(false, OP_EQ, hs_dos_can_send_intro2(or_circ));
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ, 0);
}
update_approx_time(++now);
tt_int_op(true, OP_EQ, hs_dos_can_send_intro2(or_circ));
tt_uint_op(token_bucket_ctr_get(&or_circ->introduce2_bucket), OP_EQ,
get_intro2_rate_consensus_param(NULL) - 1);
done:
circuit_free_(TO_CIRCUIT(or_circ));
hs_free_all();
free_mock_consensus();
}
static void
test_validate_dos_extension_params(void *arg)
{
bool ret;
(void) arg;
ret = cell_dos_extension_parameters_are_valid(
get_intro2_rate_consensus_param(NULL),
get_intro2_burst_consensus_param(NULL));
tt_assert(ret);
ret = cell_dos_extension_parameters_are_valid(17, 42);
tt_assert(ret);
ret = cell_dos_extension_parameters_are_valid(INT32_MAX, INT32_MAX);
tt_assert(ret);
ret = cell_dos_extension_parameters_are_valid(UINT64_MAX, 42);
tt_assert(!ret);
ret = cell_dos_extension_parameters_are_valid(42, UINT64_MAX);
tt_assert(!ret);
ret = cell_dos_extension_parameters_are_valid(0, 0);
tt_assert(ret);
ret = cell_dos_extension_parameters_are_valid(42, 40);
tt_assert(!ret);
done:
return;
}
struct testcase_t hs_dos_tests[] = {
{ "can_send_intro2", test_can_send_intro2, TT_FORK,
NULL, NULL },
{ "validate_dos_extension_params", test_validate_dos_extension_params,
TT_FORK, NULL, NULL },
END_OF_TESTCASES
};