2017-06-05 04:58:28 +08:00
|
|
|
#include "check_compat.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2018-03-28 21:36:14 +08:00
|
|
|
#include "../toxcore/crypto_core.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
#ifndef DHT_C_INCLUDED
|
2014-08-08 20:40:21 +08:00
|
|
|
#include "../toxcore/DHT.c"
|
2018-01-29 05:30:39 +08:00
|
|
|
#endif // DHT_C_INCLUDED
|
2016-09-01 07:33:20 +08:00
|
|
|
#include "../toxcore/tox.h"
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
|
|
|
|
// These tests currently fail.
|
2016-12-19 10:47:42 +08:00
|
|
|
static bool enable_broken_tests = false;
|
|
|
|
|
2017-11-15 16:13:31 +08:00
|
|
|
#ifndef USE_IPV6
|
|
|
|
#define USE_IPV6 1
|
|
|
|
#endif
|
|
|
|
|
2018-09-08 15:29:03 +08:00
|
|
|
static inline IP get_loopback(void)
|
2017-11-15 16:13:31 +08:00
|
|
|
{
|
|
|
|
IP ip;
|
|
|
|
#if USE_IPV6
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv6;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v6 = get_ip6_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#else
|
2018-02-27 09:07:46 +08:00
|
|
|
ip.family = net_family_ipv4;
|
2018-02-15 04:51:55 +08:00
|
|
|
ip.ip.v4 = get_ip4_loopback();
|
2017-11-15 16:13:31 +08:00
|
|
|
#endif
|
|
|
|
return ip;
|
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void mark_bad(const Mono_Time *mono_time, IPPTsPng *ipptp)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
ipptp->timestamp = mono_time_get(mono_time) - 2 * BAD_NODE_TIMEOUT;
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void mark_good(const Mono_Time *mono_time, IPPTsPng *ipptp)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-08-02 07:02:13 +08:00
|
|
|
ipptp->timestamp = mono_time_get(mono_time);
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
static void mark_all_good(const Mono_Time *mono_time, Client_data *list, uint32_t length, uint8_t ipv6)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2014-08-09 18:23:59 +08:00
|
|
|
uint32_t i;
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
for (i = 0; i < length; ++i) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ipv6) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_good(mono_time, &list[i].assoc6);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_good(mono_time, &list[i].assoc4);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
/* Returns 1 if public_key has a furthest distance to comp_client_id
|
|
|
|
than all public_key's in the list */
|
2016-09-05 23:10:48 +08:00
|
|
|
static uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2014-08-09 18:23:59 +08:00
|
|
|
uint32_t i;
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
if (id_closest(comp_client_id, public_key, list[i].public_key) == 1) {
|
2014-08-08 20:40:21 +08:00
|
|
|
return 0;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-07-16 08:09:13 +08:00
|
|
|
uint32_t i;
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2018-07-16 09:12:16 +08:00
|
|
|
for (i = 0; i < length; ++i) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (id_equal(public_key, list[i].public_key)) {
|
2014-08-08 20:40:21 +08:00
|
|
|
return i;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void test_addto_lists_update(DHT *dht,
|
|
|
|
Client_data *list,
|
|
|
|
uint32_t length,
|
|
|
|
IP_Port *ip_port)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-07-16 08:09:13 +08:00
|
|
|
uint32_t used, test, test1, test2, found;
|
2014-08-08 20:40:21 +08:00
|
|
|
IP_Port test_ipp;
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t test_id[CRYPTO_PUBLIC_KEY_SIZE];
|
2018-02-27 09:07:46 +08:00
|
|
|
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2014-08-09 18:23:59 +08:00
|
|
|
// check id update for existing ip_port
|
2018-03-28 21:36:14 +08:00
|
|
|
test = random_u32() % length;
|
2014-08-08 20:40:21 +08:00
|
|
|
ipport_copy(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port);
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(test_id, sizeof(test_id));
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &test_ipp, test_id);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
2014-08-09 18:23:59 +08:00
|
|
|
// it is possible to have ip_port duplicates in the list, so ip_port @ found not always equal to ip_port @ test
|
|
|
|
found = client_in_list(list, length, test_id);
|
|
|
|
ck_assert_msg(found >= 0, "Client id is not in the list");
|
2014-11-26 04:31:46 +08:00
|
|
|
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[found].assoc6.ip_port : &list[found].assoc4.ip_port),
|
|
|
|
"Client IP_Port is incorrect");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check ip_port update for existing id
|
2018-03-28 21:36:14 +08:00
|
|
|
test = random_u32() % length;
|
|
|
|
test_ipp.port = random_u32() % TOX_PORT_DEFAULT;
|
2015-07-30 10:39:56 +08:00
|
|
|
id_copy(test_id, list[test].public_key);
|
2014-08-09 18:23:59 +08:00
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &test_ipp, test_id);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
2014-08-09 18:23:59 +08:00
|
|
|
// it is not possible to have id duplicates in the list, so id @ found must be equal id @ test
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(client_in_list(list, length, test_id) == test, "Client id is not in the list");
|
2014-11-26 04:31:46 +08:00
|
|
|
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test].assoc6.ip_port : &list[test].assoc4.ip_port),
|
|
|
|
"Client IP_Port is incorrect");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check ip_port update for existing id and ip_port (... port ... id ...)
|
2018-03-28 21:36:14 +08:00
|
|
|
test1 = random_u32() % (length / 2);
|
|
|
|
test2 = random_u32() % (length / 2) + length / 2;
|
2014-08-09 18:23:59 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
ipport_copy(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port);
|
2015-07-30 10:39:56 +08:00
|
|
|
id_copy(test_id, list[test2].public_key);
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ipv6) {
|
|
|
|
list[test2].assoc6.ip_port.port = -1;
|
|
|
|
} else {
|
|
|
|
list[test2].assoc4.ip_port.port = -1;
|
|
|
|
}
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &test_ipp, test_id);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
|
|
|
ck_assert_msg(client_in_list(list, length, test_id) == test2, "Client id is not in the list");
|
2014-11-26 04:31:46 +08:00
|
|
|
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port),
|
|
|
|
"Client IP_Port is incorrect");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check ip_port update for existing id and ip_port (... id ... port ...)
|
2018-03-28 21:36:14 +08:00
|
|
|
test1 = random_u32() % (length / 2);
|
|
|
|
test2 = random_u32() % (length / 2) + length / 2;
|
2014-08-09 18:23:59 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
ipport_copy(&test_ipp, ipv6 ? &list[test2].assoc6.ip_port : &list[test2].assoc4.ip_port);
|
2015-07-30 10:39:56 +08:00
|
|
|
id_copy(test_id, list[test1].public_key);
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ipv6) {
|
|
|
|
list[test1].assoc6.ip_port.port = -1;
|
|
|
|
} else {
|
|
|
|
list[test1].assoc4.ip_port.port = -1;
|
|
|
|
}
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &test_ipp, test_id);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
|
|
|
ck_assert_msg(client_in_list(list, length, test_id) == test1, "Client id is not in the list");
|
2014-11-26 04:31:46 +08:00
|
|
|
ck_assert_msg(ipport_equal(&test_ipp, ipv6 ? &list[test1].assoc6.ip_port : &list[test1].assoc4.ip_port),
|
|
|
|
"Client IP_Port is incorrect");
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void test_addto_lists_bad(DHT *dht,
|
|
|
|
Client_data *list,
|
|
|
|
uint32_t length,
|
|
|
|
IP_Port *ip_port)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
|
|
|
// check "bad" clients replacement
|
2014-08-09 18:23:59 +08:00
|
|
|
int used, test1, test2, test3;
|
2016-12-19 10:47:42 +08:00
|
|
|
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];
|
2018-02-27 09:07:46 +08:00
|
|
|
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(public_key, sizeof(public_key));
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_all_good(dht->mono_time, list, length, ipv6);
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-03-28 21:36:14 +08:00
|
|
|
test1 = random_u32() % (length / 3);
|
|
|
|
test2 = random_u32() % (length / 3) + length / 3;
|
|
|
|
test3 = random_u32() % (length / 3) + 2 * length / 3;
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(!(test1 == test2 || test1 == test3 || test2 == test3), "Wrong test indices are chosen");
|
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
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);
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// mark nodes as "bad"
|
|
|
|
if (ipv6) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_bad(dht->mono_time, &list[test1].assoc6);
|
|
|
|
mark_bad(dht->mono_time, &list[test2].assoc6);
|
|
|
|
mark_bad(dht->mono_time, &list[test3].assoc6);
|
2014-08-08 20:40:21 +08:00
|
|
|
} else {
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_bad(dht->mono_time, &list[test1].assoc4);
|
|
|
|
mark_bad(dht->mono_time, &list[test2].assoc4);
|
|
|
|
mark_bad(dht->mono_time, &list[test3].assoc4);
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ip_port->port += 1;
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, ip_port, public_key);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
|
|
|
|
2015-07-30 10:39:56 +08:00
|
|
|
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Client id is not in the list");
|
2014-12-16 08:04:56 +08:00
|
|
|
ck_assert_msg(client_in_list(list, length, test_id2) >= 0, "Wrong bad client removed");
|
|
|
|
ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed");
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void test_addto_lists_good(DHT *dht,
|
|
|
|
Client_data *list,
|
|
|
|
uint32_t length,
|
|
|
|
IP_Port *ip_port,
|
|
|
|
const uint8_t *comp_client_id)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
2018-02-27 09:07:46 +08:00
|
|
|
uint8_t ipv6 = net_family_is_ipv6(ip_port->ip.family) ? 1 : 0;
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
mark_all_good(dht->mono_time, list, length, ipv6);
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check "good" client id replacement
|
|
|
|
do {
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(public_key, sizeof(public_key));
|
2015-07-30 10:39:56 +08:00
|
|
|
} while (is_furthest(comp_client_id, list, length, public_key));
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2014-08-08 20:40:21 +08:00
|
|
|
ip_port->port += 1;
|
2022-02-04 00:17:29 +08:00
|
|
|
addto_lists(dht, ip_port, public_key);
|
2015-07-30 10:39:56 +08:00
|
|
|
ck_assert_msg(client_in_list(list, length, public_key) >= 0, "Good client id is not in the list");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check "good" client id skip
|
|
|
|
do {
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(public_key, sizeof(public_key));
|
2015-07-30 10:39:56 +08:00
|
|
|
} while (!is_furthest(comp_client_id, list, length, public_key));
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
ip_port->port += 1;
|
2022-02-04 00:17:29 +08:00
|
|
|
addto_lists(dht, ip_port, public_key);
|
2015-07-30 10:39:56 +08:00
|
|
|
ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 23:27:46 +08:00
|
|
|
#ifndef MAX
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
#endif
|
|
|
|
|
2016-08-18 07:37:45 +08:00
|
|
|
static void test_addto_lists(IP ip)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-03-17 22:48:34 +08:00
|
|
|
Logger *log = logger_new();
|
|
|
|
uint32_t index = 1;
|
|
|
|
logger_callback_log(log, (logger_cb *)print_debug_log, nullptr, &index);
|
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_time = mono_time_new();
|
|
|
|
ck_assert_msg(mono_time != nullptr, "Failed to create Mono_Time");
|
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
Networking_Core *net = new_networking(log, &ip, TOX_PORT_DEFAULT);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(net != nullptr, "Failed to create Networking_Core");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
DHT *dht = new_dht(log, mono_time, net, true);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(dht != nullptr, "Failed to create DHT");
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
IP_Port ip_port;
|
|
|
|
ip_port.ip = ip;
|
|
|
|
ip_port.port = TOX_PORT_DEFAULT;
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t i, used;
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check lists filling
|
|
|
|
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(public_key, sizeof(public_key));
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &ip_port, public_key);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
|
|
|
|
ip_port.port += 1;
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &ip_port, public_key);
|
2015-07-30 10:39:56 +08:00
|
|
|
ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing public_key");
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) {
|
|
|
|
ip_port.port += 1;
|
2016-12-19 10:47:42 +08:00
|
|
|
random_bytes(public_key, sizeof(public_key));
|
2022-02-04 00:17:29 +08:00
|
|
|
used = addto_lists(dht, &ip_port, public_key);
|
2014-08-08 20:40:21 +08:00
|
|
|
ck_assert_msg(used >= 1, "Wrong number of added clients");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*check: Current behavior if there are two clients with the same id is
|
|
|
|
* to replace the first ip by the second. */
|
|
|
|
test_addto_lists_update(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port);
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
for (i = 0; i < dht->num_friends; ++i) {
|
2014-08-08 20:40:21 +08:00
|
|
|
test_addto_lists_update(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port);
|
2016-09-13 04:37:58 +08:00
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check "bad" entries
|
|
|
|
test_addto_lists_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port);
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2016-09-13 04:37:58 +08:00
|
|
|
for (i = 0; i < dht->num_friends; ++i) {
|
2014-08-08 20:40:21 +08:00
|
|
|
test_addto_lists_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port);
|
2016-09-13 04:37:58 +08:00
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
|
|
|
// check "good" entries
|
|
|
|
test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key);
|
2014-11-26 04:31:46 +08:00
|
|
|
|
2018-07-20 05:39:13 +08:00
|
|
|
for (i = 0; i < dht->num_friends; ++i) {
|
2014-11-26 04:31:46 +08:00
|
|
|
test_addto_lists_good(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port,
|
2015-07-30 10:39:56 +08:00
|
|
|
dht->friends_list[i].public_key);
|
2018-07-20 05:39:13 +08:00
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dht);
|
2014-08-08 20:40:21 +08:00
|
|
|
kill_networking(net);
|
2018-06-28 22:27:15 +08:00
|
|
|
logger_kill(log);
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_addto_lists_ipv4(void)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, 0);
|
|
|
|
test_addto_lists(ip);
|
|
|
|
}
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_addto_lists_ipv6(void)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, 1);
|
|
|
|
test_addto_lists(ip);
|
|
|
|
}
|
|
|
|
|
2018-06-18 07:41:42 +08:00
|
|
|
#define DHT_DEFAULT_PORT (TOX_PORT_DEFAULT + 1000)
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void print_pk(uint8_t *public_key)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
|
|
|
uint32_t j;
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
for (j = 0; j < CRYPTO_PUBLIC_KEY_SIZE; j++) {
|
2018-02-24 01:11:00 +08:00
|
|
|
printf("%02X", public_key[j]);
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
static void test_add_to_list(uint8_t cmp_list[][CRYPTO_PUBLIC_KEY_SIZE + 1],
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t length, const uint8_t *pk,
|
2016-09-05 23:10:48 +08:00
|
|
|
const uint8_t *cmp_pk)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t p_b[CRYPTO_PUBLIC_KEY_SIZE];
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t i;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
|
|
|
for (i = 0; i < length; ++i) {
|
2016-12-19 10:47:42 +08:00
|
|
|
if (!cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE]) {
|
|
|
|
memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE);
|
|
|
|
cmp_list[i][CRYPTO_PUBLIC_KEY_SIZE] = 1;
|
2015-12-12 06:50:36 +08:00
|
|
|
return;
|
2016-09-01 03:40:20 +08:00
|
|
|
}
|
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE) == 0) {
|
2016-09-01 03:40:20 +08:00
|
|
|
return;
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
if (id_closest(cmp_pk, cmp_list[i], pk) == 2) {
|
2016-12-19 10:47:42 +08:00
|
|
|
memcpy(p_b, cmp_list[i], CRYPTO_PUBLIC_KEY_SIZE);
|
|
|
|
memcpy(cmp_list[i], pk, CRYPTO_PUBLIC_KEY_SIZE);
|
2015-12-12 06:50:36 +08:00
|
|
|
test_add_to_list(cmp_list, length, p_b, cmp_pk);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
#define NUM_DHT 100
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void test_list_main(void)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
2016-01-05 11:59:29 +08:00
|
|
|
DHT *dhts[NUM_DHT];
|
2018-06-28 22:27:15 +08:00
|
|
|
Logger *logs[NUM_DHT];
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_times[NUM_DHT];
|
2018-03-17 22:48:34 +08:00
|
|
|
uint32_t index[NUM_DHT];
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][CRYPTO_PUBLIC_KEY_SIZE + 1];
|
2015-12-12 06:50:36 +08:00
|
|
|
memset(cmp_list1, 0, sizeof(cmp_list1));
|
2016-01-05 11:59:29 +08:00
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t i, j, k, l;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, 1);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-06-28 22:27:15 +08:00
|
|
|
logs[i] = logger_new();
|
2018-03-17 22:48:34 +08:00
|
|
|
index[i] = i + 1;
|
2018-06-28 22:27:15 +08:00
|
|
|
logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]);
|
2018-03-17 22:48:34 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_times[i] = mono_time_new();
|
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
|
2018-06-18 07:41:42 +08:00
|
|
|
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i,
|
|
|
|
"Bound to wrong port: %d", net_port(dhts[i]->net));
|
2016-01-05 11:59:29 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
|
|
|
for (j = 1; j < NUM_DHT; ++j) {
|
|
|
|
test_add_to_list(cmp_list1[i], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[i]->self_public_key);
|
2016-01-05 11:59:29 +08:00
|
|
|
}
|
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
|
|
|
for (j = 0; j < NUM_DHT; ++j) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (i == j) {
|
2016-01-05 11:59:29 +08:00
|
|
|
continue;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2016-01-05 11:59:29 +08:00
|
|
|
|
|
|
|
IP_Port ip_port;
|
|
|
|
ip_init(&ip_port.ip, 0);
|
2018-03-28 21:36:14 +08:00
|
|
|
ip_port.ip.ip.v4.uint32 = random_u32();
|
|
|
|
ip_port.port = random_u32() % (UINT16_MAX - 1);
|
2016-01-05 11:59:29 +08:00
|
|
|
++ip_port.port;
|
2022-02-04 00:17:29 +08:00
|
|
|
addto_lists(dhts[i], &ip_port, dhts[j]->self_public_key);
|
2016-01-05 11:59:29 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 22:48:34 +08:00
|
|
|
#if 0
|
|
|
|
print_pk(dhts[0]->self_public_key);
|
2016-01-05 11:59:29 +08:00
|
|
|
|
2018-03-17 22:48:34 +08:00
|
|
|
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
|
|
|
|
printf("----Entry %u----\n", i);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-03-17 22:48:34 +08:00
|
|
|
print_pk(cmp_list1[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t m_count = 0;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (l = 0; l < NUM_DHT; ++l) {
|
|
|
|
for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) {
|
|
|
|
for (j = 1; j < NUM_DHT; ++j) {
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, CRYPTO_PUBLIC_KEY_SIZE) != 0) {
|
2016-01-05 11:59:29 +08:00
|
|
|
continue;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t count = 0;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (k = 0; k < LCLIENT_LIST; ++k) {
|
|
|
|
if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key,
|
2016-12-19 10:47:42 +08:00
|
|
|
CRYPTO_PUBLIC_KEY_SIZE) == 0) {
|
2016-01-05 11:59:29 +08:00
|
|
|
++count;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2016-01-05 11:59:29 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
if (count != 1) {
|
|
|
|
print_pk(dhts[l]->self_public_key);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) {
|
|
|
|
printf("----Entry %u----\n", k);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
print_pk(cmp_list1[l][k]);
|
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (k = 0; k < LCLIENT_LIST; ++k) {
|
|
|
|
printf("----Closel %u----\n", k);
|
|
|
|
print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key);
|
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
print_pk(dhts[(l + j) % NUM_DHT]->self_public_key);
|
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
Node_format ln[MAX_SENT_NODES];
|
2020-12-30 08:00:00 +08:00
|
|
|
uint16_t n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, net_family_unspec, 1);
|
2016-01-05 11:59:29 +08:00
|
|
|
ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j);
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
count = 0;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (k = 0; k < MAX_SENT_NODES; ++k) {
|
2016-12-19 10:47:42 +08:00
|
|
|
if (memcmp(dhts[l]->self_public_key, ln[k].public_key, CRYPTO_PUBLIC_KEY_SIZE) == 0) {
|
2016-01-05 11:59:29 +08:00
|
|
|
++count;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2016-01-05 11:59:29 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count);
|
2016-09-18 08:31:55 +08:00
|
|
|
#if 0
|
|
|
|
|
|
|
|
for (k = 0; k < MAX_SENT_NODES; ++k) {
|
|
|
|
printf("----gn %u----\n", k);
|
|
|
|
print_pk(ln[k].public_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2016-01-05 11:59:29 +08:00
|
|
|
++m_count;
|
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count,
|
|
|
|
(NUM_DHT) * (MAX_FRIEND_CLIENTS));
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-01-05 11:59:29 +08:00
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
2016-09-18 08:31:55 +08:00
|
|
|
Networking_Core *n = dhts[i]->net;
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dhts[i]);
|
2016-01-05 11:59:29 +08:00
|
|
|
kill_networking(n);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_times[i]);
|
2018-06-28 22:27:15 +08:00
|
|
|
logger_kill(logs[i]);
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_list(void)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
2018-07-16 08:09:13 +08:00
|
|
|
uint8_t i;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
for (i = 0; i < 10; ++i) {
|
2015-12-12 06:50:36 +08:00
|
|
|
test_list_main();
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
2022-02-03 04:02:23 +08:00
|
|
|
static void ip_callback(void *data, int32_t number, const IP_Port *ip_port)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NUM_DHT_FRIENDS 20
|
|
|
|
|
2018-08-27 04:34:26 +08:00
|
|
|
static uint64_t get_clock_callback(Mono_Time *mono_time, void *user_data)
|
2018-08-18 02:34:25 +08:00
|
|
|
{
|
2018-08-27 04:34:26 +08:00
|
|
|
const uint64_t *clock = (const uint64_t *)user_data;
|
|
|
|
return *clock;
|
2018-08-18 02:34:25 +08:00
|
|
|
}
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_DHT_test(void)
|
2015-12-12 06:50:36 +08:00
|
|
|
{
|
|
|
|
uint32_t to_comp = 8394782;
|
|
|
|
DHT *dhts[NUM_DHT];
|
2018-06-28 22:27:15 +08:00
|
|
|
Logger *logs[NUM_DHT];
|
2018-08-02 07:02:13 +08:00
|
|
|
Mono_Time *mono_times[NUM_DHT];
|
2018-08-18 02:34:25 +08:00
|
|
|
uint64_t clock[NUM_DHT];
|
2018-03-17 22:48:34 +08:00
|
|
|
uint32_t index[NUM_DHT];
|
2015-12-12 06:50:36 +08:00
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
uint32_t i, j;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
|
|
|
IP ip;
|
|
|
|
ip_init(&ip, 1);
|
|
|
|
|
2018-06-28 22:27:15 +08:00
|
|
|
logs[i] = logger_new();
|
2018-03-17 22:48:34 +08:00
|
|
|
index[i] = i + 1;
|
2018-06-28 22:27:15 +08:00
|
|
|
logger_callback_log(logs[i], (logger_cb *)print_debug_log, nullptr, &index[i]);
|
2018-03-17 22:48:34 +08:00
|
|
|
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_times[i] = mono_time_new();
|
2018-08-18 02:34:25 +08:00
|
|
|
clock[i] = current_time_monotonic(mono_times[i]);
|
|
|
|
mono_time_set_current_time_callback(mono_times[i], get_clock_callback, &clock[i]);
|
2018-08-02 07:02:13 +08:00
|
|
|
|
2022-02-04 00:17:29 +08:00
|
|
|
dhts[i] = new_dht(logs[i], mono_times[i], new_networking(logs[i], &ip, DHT_DEFAULT_PORT + i), true);
|
2018-01-29 05:30:39 +08:00
|
|
|
ck_assert_msg(dhts[i] != nullptr, "Failed to create dht instances %u", i);
|
2018-01-11 00:44:49 +08:00
|
|
|
ck_assert_msg(net_port(dhts[i]->net) != DHT_DEFAULT_PORT + i, "Bound to wrong port");
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t tox1;
|
|
|
|
uint16_t tox2;
|
|
|
|
} pairs[NUM_DHT_FRIENDS];
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT_FRIENDS; ++i) {
|
2018-07-20 06:00:21 +08:00
|
|
|
// TODO(hugbubby): remove use of goto.
|
2015-12-12 06:50:36 +08:00
|
|
|
loop_top:
|
2018-03-28 21:36:14 +08:00
|
|
|
pairs[i].tox1 = random_u32() % NUM_DHT;
|
|
|
|
pairs[i].tox2 = (pairs[i].tox1 + (random_u32() % (NUM_DHT - 1)) + 1) % NUM_DHT;
|
2015-12-12 06:50:36 +08:00
|
|
|
|
|
|
|
for (j = 0; j < i; ++j) {
|
2016-09-01 02:12:19 +08:00
|
|
|
if (pairs[j].tox2 == pairs[i].tox2 && pairs[j].tox1 == pairs[i].tox1) {
|
2015-12-12 06:50:36 +08:00
|
|
|
goto loop_top;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t lock_count = 0;
|
2018-07-05 18:31:29 +08:00
|
|
|
ck_assert_msg(dht_addfriend(dhts[pairs[i].tox2], dhts[pairs[i].tox1]->self_public_key, &ip_callback, &to_comp, 1337,
|
2015-12-12 06:50:36 +08:00
|
|
|
&lock_count) == 0, "Failed to add friend");
|
|
|
|
ck_assert_msg(lock_count == 1, "bad lock count: %u %u", lock_count, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
|
|
|
IP_Port ip_port;
|
2017-11-15 16:13:31 +08:00
|
|
|
ip_port.ip = get_loopback();
|
2017-01-07 16:28:53 +08:00
|
|
|
ip_port.port = net_htons(DHT_DEFAULT_PORT + i);
|
2022-02-03 04:02:23 +08:00
|
|
|
dht_bootstrap(dhts[(i - 1) % NUM_DHT], &ip_port, dhts[i]->self_public_key);
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
while (true) {
|
2015-12-12 06:50:36 +08:00
|
|
|
uint16_t counter = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT_FRIENDS; ++i) {
|
|
|
|
IP_Port a;
|
|
|
|
|
2018-07-05 18:31:29 +08:00
|
|
|
if (dht_getfriendip(dhts[pairs[i].tox2], dhts[pairs[i].tox1]->self_public_key, &a) == 1) {
|
2015-12-12 06:50:36 +08:00
|
|
|
++counter;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (counter == NUM_DHT_FRIENDS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_update(mono_times[i]);
|
2018-01-29 05:30:39 +08:00
|
|
|
networking_poll(dhts[i]->net, nullptr);
|
2018-07-05 18:31:29 +08:00
|
|
|
do_dht(dhts[i]);
|
2018-08-18 02:34:25 +08:00
|
|
|
clock[i] += 500;
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
2018-08-18 02:34:25 +08:00
|
|
|
c_sleep(20);
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_DHT; ++i) {
|
2016-09-18 08:31:55 +08:00
|
|
|
Networking_Core *n = dhts[i]->net;
|
2018-07-05 18:31:29 +08:00
|
|
|
kill_dht(dhts[i]);
|
2015-12-12 06:50:36 +08:00
|
|
|
kill_networking(n);
|
2018-08-02 07:02:13 +08:00
|
|
|
mono_time_free(mono_times[i]);
|
2018-06-28 22:27:15 +08:00
|
|
|
logger_kill(logs[i]);
|
2015-12-12 06:50:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_dht_create_packet(void)
|
2016-11-07 12:01:50 +08:00
|
|
|
{
|
|
|
|
uint8_t plain[100] = {0};
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t pkt[1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE];
|
2016-11-07 12:01:50 +08:00
|
|
|
|
2016-12-19 10:47:42 +08:00
|
|
|
uint8_t key[CRYPTO_SYMMETRIC_KEY_SIZE];
|
2016-11-07 12:01:50 +08:00
|
|
|
new_symmetric_key(key);
|
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t length = dht_create_packet(key, key, NET_PACKET_GET_NODES, plain, sizeof(plain), pkt);
|
2016-11-07 12:01:50 +08:00
|
|
|
|
|
|
|
ck_assert_msg(pkt[0] == NET_PACKET_GET_NODES, "Malformed packet.");
|
2016-12-19 10:47:42 +08:00
|
|
|
ck_assert_msg(memcmp(pkt + 1, key, CRYPTO_SYMMETRIC_KEY_SIZE) == 0, "Malformed packet.");
|
|
|
|
ck_assert_msg(length == 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE + sizeof(plain) + CRYPTO_MAC_SIZE,
|
2018-02-06 05:38:58 +08:00
|
|
|
"Invalid size. Should be %u got %d", (unsigned)sizeof(pkt), length);
|
2016-11-07 12:01:50 +08:00
|
|
|
|
|
|
|
printf("Create Packet Successful!\n");
|
|
|
|
}
|
|
|
|
|
2017-03-11 14:21:40 +08:00
|
|
|
#define MAX_COUNT 3
|
|
|
|
|
|
|
|
static void dht_pack_unpack(const Node_format *nodes, size_t size, uint8_t *data, size_t length)
|
|
|
|
{
|
2018-07-16 09:12:16 +08:00
|
|
|
int16_t packed_size = pack_nodes(data, length, nodes, size);
|
2017-03-11 14:21:40 +08:00
|
|
|
ck_assert_msg(packed_size != -1, "Wrong pack_nodes result");
|
|
|
|
|
|
|
|
uint16_t processed = 0;
|
|
|
|
VLA(Node_format, nodes_unpacked, size);
|
|
|
|
const uint8_t tcp_enabled = 1;
|
|
|
|
|
2018-07-16 08:09:13 +08:00
|
|
|
uint16_t unpacked_count = unpack_nodes(nodes_unpacked, size, &processed, data, length, tcp_enabled);
|
2017-03-11 14:21:40 +08:00
|
|
|
ck_assert_msg(unpacked_count == size, "Wrong unpack_nodes result");
|
|
|
|
ck_assert_msg(processed == packed_size, "unpack_nodes did not process all data");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
const IP_Port *ipp1 = &nodes[i].ip_port;
|
|
|
|
const IP_Port *ipp2 = &nodes_unpacked[i].ip_port;
|
|
|
|
ck_assert_msg(ip_equal(&ipp1->ip, &ipp2->ip), "Unsuccessful ip unpack");
|
|
|
|
ck_assert_msg(ipp1->port == ipp2->port, "Unsuccessful port unpack");
|
|
|
|
|
|
|
|
const uint8_t *pk1 = nodes[i].public_key;
|
|
|
|
const uint8_t *pk2 = nodes_unpacked[i].public_key;
|
|
|
|
ck_assert_msg(!memcmp(pk1, pk2, CRYPTO_PUBLIC_KEY_SIZE), "Unsuccessful pk unpack");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void random_ip(IP_Port *ipp, int family)
|
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
uint8_t *ip = nullptr;
|
2017-03-11 14:21:40 +08:00
|
|
|
size_t size;
|
|
|
|
|
2017-03-03 04:15:51 +08:00
|
|
|
if (family == TOX_AF_INET || family == TCP_INET) {
|
2018-02-15 04:51:55 +08:00
|
|
|
ip = (uint8_t *)&ipp->ip.ip.v4;
|
|
|
|
size = sizeof(ipp->ip.ip.v4);
|
2017-03-03 04:15:51 +08:00
|
|
|
} else if (family == TOX_AF_INET6 || family == TCP_INET6) {
|
2018-02-15 04:51:55 +08:00
|
|
|
ip = (uint8_t *)&ipp->ip.ip.v6;
|
|
|
|
size = sizeof(ipp->ip.ip.v6);
|
2017-03-11 14:21:40 +08:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *port = (uint8_t *)&ipp->port;
|
|
|
|
random_bytes(port, sizeof(ipp->port));
|
|
|
|
random_bytes(ip, size);
|
2018-02-27 09:07:46 +08:00
|
|
|
// TODO(iphydf): Pass the net_family variant to random_ip.
|
|
|
|
ipp->ip.family.value = family;
|
2017-03-11 14:21:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PACKED_NODES_SIZE (SIZE_IPPORT + CRYPTO_PUBLIC_KEY_SIZE)
|
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
static void test_dht_node_packing(void)
|
2017-03-11 14:21:40 +08:00
|
|
|
{
|
|
|
|
const uint16_t length = MAX_COUNT * PACKED_NODES_SIZE;
|
|
|
|
uint8_t *data = (uint8_t *)malloc(length);
|
|
|
|
|
|
|
|
Node_format nodes[MAX_COUNT];
|
|
|
|
const size_t pk_size = sizeof(nodes[0].public_key);
|
|
|
|
|
|
|
|
random_bytes(nodes[0].public_key, pk_size);
|
|
|
|
random_bytes(nodes[1].public_key, pk_size);
|
|
|
|
random_bytes(nodes[2].public_key, pk_size);
|
|
|
|
|
2017-03-03 04:15:51 +08:00
|
|
|
random_ip(&nodes[0].ip_port, TOX_AF_INET);
|
|
|
|
random_ip(&nodes[1].ip_port, TOX_AF_INET);
|
|
|
|
random_ip(&nodes[2].ip_port, TOX_AF_INET);
|
2017-03-11 14:21:40 +08:00
|
|
|
dht_pack_unpack(nodes, 3, data, length);
|
|
|
|
|
2017-03-03 04:15:51 +08:00
|
|
|
random_ip(&nodes[0].ip_port, TOX_AF_INET);
|
|
|
|
random_ip(&nodes[1].ip_port, TOX_AF_INET);
|
2017-03-11 14:21:40 +08:00
|
|
|
random_ip(&nodes[2].ip_port, TCP_INET);
|
|
|
|
dht_pack_unpack(nodes, 3, data, length);
|
|
|
|
|
2017-03-03 04:15:51 +08:00
|
|
|
random_ip(&nodes[0].ip_port, TOX_AF_INET);
|
|
|
|
random_ip(&nodes[1].ip_port, TOX_AF_INET6);
|
2017-03-11 14:21:40 +08:00
|
|
|
random_ip(&nodes[2].ip_port, TCP_INET6);
|
|
|
|
dht_pack_unpack(nodes, 3, data, length);
|
|
|
|
|
|
|
|
random_ip(&nodes[0].ip_port, TCP_INET);
|
|
|
|
random_ip(&nodes[1].ip_port, TCP_INET6);
|
|
|
|
random_ip(&nodes[2].ip_port, TCP_INET);
|
|
|
|
dht_pack_unpack(nodes, 3, data, length);
|
|
|
|
|
2017-03-03 04:15:51 +08:00
|
|
|
random_ip(&nodes[0].ip_port, TOX_AF_INET6);
|
|
|
|
random_ip(&nodes[1].ip_port, TOX_AF_INET6);
|
|
|
|
random_ip(&nodes[2].ip_port, TOX_AF_INET6);
|
2017-03-11 14:21:40 +08:00
|
|
|
dht_pack_unpack(nodes, 3, data, length);
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2014-08-08 20:40:21 +08:00
|
|
|
{
|
2018-02-19 01:50:50 +08:00
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
test_dht_create_packet();
|
|
|
|
test_dht_node_packing();
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
test_list();
|
|
|
|
test_DHT_test();
|
|
|
|
|
|
|
|
if (enable_broken_tests) {
|
|
|
|
test_addto_lists_ipv4();
|
|
|
|
test_addto_lists_ipv6();
|
|
|
|
}
|
2014-08-08 20:40:21 +08:00
|
|
|
|
2018-08-10 07:53:39 +08:00
|
|
|
return 0;
|
2014-08-08 20:40:21 +08:00
|
|
|
}
|