Skip to content

Commit

Permalink
Remove hardening code from DHT
Browse files Browse the repository at this point in the history
  • Loading branch information
zugz committed Dec 30, 2021
1 parent eb4dc43 commit 141fe1e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 493 deletions.
91 changes: 1 addition & 90 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,11 @@ static inline IP get_loopback(void)
static void mark_bad(const Mono_Time *mono_time, IPPTsPng *ipptp)
{
ipptp->timestamp = mono_time_get(mono_time) - 2 * BAD_NODE_TIMEOUT;
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}

static void mark_possible_bad(const Mono_Time *mono_time, IPPTsPng *ipptp)
{
ipptp->timestamp = mono_time_get(mono_time);
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}

static void mark_good(const Mono_Time *mono_time, IPPTsPng *ipptp)
{
ipptp->timestamp = mono_time_get(mono_time);
ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1;
ipptp->hardening.send_nodes_ok = (HARDENING_ALL_OK >> 1) & 1;
ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1;
}

static void mark_all_good(const Mono_Time *mono_time, Client_data *list, uint32_t length, uint8_t ipv6)
Expand Down Expand Up @@ -210,71 +196,6 @@ static void test_addto_lists_bad(DHT *dht,
ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed");
}

static void test_addto_lists_possible_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
{
// check "possibly bad" clients replacement
uint32_t used, test1, test2, test3;
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], test_id1[CRYPTO_PUBLIC_KEY_SIZE], test_id2[CRYPTO_PUBLIC_KEY_SIZE],
test_id3[CRYPTO_PUBLIC_KEY_SIZE];
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;

random_bytes(public_key, sizeof(public_key));
mark_all_good(dht->mono_time, list, length, ipv6);

test1 = random_u32() % (length / 3);
test2 = random_u32() % (length / 3) + length / 3;
test3 = random_u32() % (length / 3) + 2 * length / 3;
ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen");

id_copy((uint8_t *)&test_id1, list[test1].public_key);
id_copy((uint8_t *)&test_id2, list[test2].public_key);
id_copy((uint8_t *)&test_id3, list[test3].public_key);

// mark nodes as "possibly bad"
if (ipv6) {
mark_possible_bad(dht->mono_time, &list[test1].assoc6);
mark_possible_bad(dht->mono_time, &list[test2].assoc6);
mark_possible_bad(dht->mono_time, &list[test3].assoc6);
} else {
mark_possible_bad(dht->mono_time, &list[test1].assoc4);
mark_possible_bad(dht->mono_time, &list[test2].assoc4);
mark_possible_bad(dht->mono_time, &list[test3].assoc4);
}

ip_port->port += 1;
used = addto_lists(dht, *ip_port, public_key);
ck_assert_msg(used >= 1, "Wrong number of added clients");

ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");

bool inlist_id1 = client_in_list(list, length, test_id1) >= 0;
bool inlist_id2 = client_in_list(list, length, test_id2) >= 0;
bool inlist_id3 = client_in_list(list, length, test_id3) >= 0;

ck_assert_msg(inlist_id1 + inlist_id2 + inlist_id3 == 2, "Wrong client removed");

if (!inlist_id1) {
ck_assert_msg(id_closest(comp_client_id, test_id2, test_id1) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id3, test_id1) == 1,
"Id has been removed but is closer to than another one");
} else if (!inlist_id2) {
ck_assert_msg(id_closest(comp_client_id, test_id1, test_id2) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id3, test_id2) == 1,
"Id has been removed but is closer to than another one");
} else if (!inlist_id3) {
ck_assert_msg(id_closest(comp_client_id, test_id1, test_id3) == 1,
"Id has been removed but is closer to than another one");
ck_assert_msg(id_closest(comp_client_id, test_id2, test_id3) == 1,
"Id has been removed but is closer to than another one");
}
}

static void test_addto_lists_good(DHT *dht,
Client_data *list,
uint32_t length,
Expand Down Expand Up @@ -365,16 +286,6 @@ static void test_addto_lists(IP ip)
test_addto_lists_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port);
}

// check "possibly bad" entries
if (enable_broken_tests) {
test_addto_lists_possible_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key);

for (i = 0; i < dht->num_friends; ++i) {
test_addto_lists_possible_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port,
dht->friends_list[i].public_key);
}
}

// check "good" entries
test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key);

Expand Down Expand Up @@ -543,7 +454,7 @@ static void test_list_main(void)
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);

Node_format ln[MAX_SENT_NODES];
uint16_t n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1, 0);
uint16_t n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1);
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);

count = 0;
Expand Down
19 changes: 0 additions & 19 deletions testing/DHT_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,6 @@ static void print_client_id(const uint8_t *public_key)
}
}

static void print_hardening(const Hardening *h)
{
printf("Hardening:\n");
printf("routes_requests_ok: %u\n", h->routes_requests_ok);
printf("routes_requests_timestamp: %llu\n", (long long unsigned int)h->routes_requests_timestamp);
printf("routes_requests_pingedid: ");
print_client_id(h->routes_requests_pingedid);
printf("\nsend_nodes_ok: %u\n", h->send_nodes_ok);
printf("send_nodes_timestamp: %llu\n", (long long unsigned int)h->send_nodes_timestamp);
printf("send_nodes_pingedid: ");
print_client_id(h->send_nodes_pingedid);
printf("\ntesting_requests: %u\n", h->testing_requests);
printf("testing_timestamp: %llu\n", (long long unsigned int)h->testing_timestamp);
printf("testing_pingedid: ");
print_client_id(h->testing_pingedid);
printf("\n\n");
}

static void print_assoc(const IPPTsPng *assoc, uint8_t ours)
{
const IP_Port *ipp = &assoc->ip_port;
Expand All @@ -79,7 +61,6 @@ static void print_assoc(const IPPTsPng *assoc, uint8_t ours)
}

printf("Timestamp: %llu\n", (long long unsigned int) assoc->ret_timestamp);
print_hardening(&assoc->hardening);
}

static void print_clientlist(DHT *dht)
Expand Down
Loading

0 comments on commit 141fe1e

Please sign in to comment.