Files
tomato-testing/auto_tests/dht_nodes_response_api_test.c
Green Sky 596ea37298 Squashed 'external/toxcore/c-toxcore/' changes from 640e6cace..e58eb27a8
e58eb27a8 fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.
206ea3530 refactor: Explicitly pass dependencies to constructors.
7cefa93cf fix(toxencryptsave): Wipe salt and passkey after usage.
7c3be2342 refactor: Add file/line to tox-bootstrapd logging.
f84e8cdce refactor: Move loglogdata out of network.c.
390f7db06 refactor: Move random and memory OS-specifics to `os_*` files.
REVERT: 640e6cace fix(toxav): remove extra copy of video frame on encode Tested and works, but there might be alignment issues and other stuff.

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: e58eb27a84f9fa0cd996868e079f39e90a5c04b6
2025-11-04 21:18:05 +01:00

171 lines
5.0 KiB
C

/**
* This autotest creates a small local DHT and makes sure that each peer can crawl
* the entire DHT using the DHT nodes request/response api functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../toxcore/tox.h"
#include "../toxcore/tox_private.h"
#include "auto_test_support.h"
#include "check_compat.h"
#define NUM_TOXES 30
// Maximum number of iterations to wait for all nodes to be crawled. 5 should
// be enough. We pick 10 in case things are slow. This makes the test take
// less time in case it completely fails, so we can retry it.
#define MAX_ITERATIONS 10
typedef struct Dht_Node {
uint8_t public_key[TOX_DHT_NODE_PUBLIC_KEY_SIZE];
char ip[TOX_DHT_NODE_IP_STRING_SIZE];
uint16_t port;
} Dht_Node;
typedef struct State {
Dht_Node **nodes;
size_t num_nodes;
uint8_t **public_key_list;
} State;
static void free_nodes(Dht_Node **nodes, size_t num_nodes)
{
for (size_t i = 0; i < num_nodes; ++i) {
free(nodes[i]);
}
free(nodes);
}
static bool node_crawled(Dht_Node **nodes, size_t num_nodes, const uint8_t *public_key)
{
for (size_t i = 0; i < num_nodes; ++i) {
if (memcmp(nodes[i]->public_key, public_key, TOX_DHT_NODE_PUBLIC_KEY_SIZE) == 0) {
return true;
}
}
return false;
}
static bool all_nodes_crawled(const AutoTox *autotoxes, uint32_t num_toxes, uint8_t **public_key_list)
{
for (uint32_t i = 0; i < num_toxes; ++i) {
const State *state = (const State *)autotoxes[i].state;
// make sure each peer has crawled the correct number of nodes
if (state->num_nodes < num_toxes) {
return false;
}
}
for (uint32_t i = 0; i < num_toxes; ++i) {
const State *state = (const State *)autotoxes[i].state;
// make sure each peer has the full list of public keys
for (uint32_t j = 0; j < num_toxes; ++j) {
if (!node_crawled(state->nodes, state->num_nodes, public_key_list[j])) {
return false;
}
}
}
return true;
}
static void nodes_response_cb(const Tox_Event_Dht_Nodes_Response *event, void *user_data)
{
ck_assert(user_data != nullptr);
AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;
const uint8_t *public_key = tox_event_dht_nodes_response_get_public_key(event);
const char *ip = (const char *)tox_event_dht_nodes_response_get_ip(event);
const uint16_t port = tox_event_dht_nodes_response_get_port(event);
if (node_crawled(state->nodes, state->num_nodes, public_key)) {
return;
}
ck_assert(state->num_nodes < NUM_TOXES);
Dht_Node *node = (Dht_Node *)calloc(1, sizeof(Dht_Node));
ck_assert(node != nullptr);
memcpy(node->public_key, public_key, TOX_DHT_NODE_PUBLIC_KEY_SIZE);
snprintf(node->ip, sizeof(node->ip), "%s", ip);
node->port = port;
state->nodes[state->num_nodes] = node;
++state->num_nodes;
// ask new node to give us their close nodes to every public key
for (size_t i = 0; i < NUM_TOXES; ++i) {
tox_dht_send_nodes_request(autotox->tox, public_key, ip, port, state->public_key_list[i], nullptr);
}
}
static void test_dht_nodes_request(AutoTox *autotoxes)
{
ck_assert(NUM_TOXES >= 2);
uint8_t **public_key_list = (uint8_t **)calloc(NUM_TOXES, sizeof(uint8_t *));
ck_assert(public_key_list != nullptr);
for (size_t i = 0; i < NUM_TOXES; ++i) {
State *state = (State *)autotoxes[i].state;
state->nodes = (Dht_Node **)calloc(NUM_TOXES, sizeof(Dht_Node *));
ck_assert(state->nodes != nullptr);
state->num_nodes = 0;
state->public_key_list = public_key_list;
public_key_list[i] = (uint8_t *)malloc(sizeof(uint8_t) * TOX_PUBLIC_KEY_SIZE);
ck_assert(public_key_list[i] != nullptr);
tox_self_get_dht_id(autotoxes[i].tox, public_key_list[i]);
tox_events_callback_dht_nodes_response(autotoxes[i].dispatch, nodes_response_cb);
printf("Peer %zu dht closenode count total/announce-capable: %d/%d\n",
i,
tox_dht_get_num_closelist(autotoxes[i].tox),
tox_dht_get_num_closelist_announce_capable(autotoxes[i].tox));
}
bool success = false;
for (size_t i = 0; i < MAX_ITERATIONS; ++i) {
if (all_nodes_crawled(autotoxes, NUM_TOXES, public_key_list)) {
success = true;
break;
}
iterate_all_wait(autotoxes, NUM_TOXES, ITERATION_INTERVAL);
}
ck_assert_msg(success, "Failed to crawl all nodes within %d iterations", MAX_ITERATIONS);
for (size_t i = 0; i < NUM_TOXES; ++i) {
State *state = (State *)autotoxes[i].state;
free_nodes(state->nodes, state->num_nodes);
free(public_key_list[i]);
}
free(public_key_list);
}
int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);
Run_Auto_Options options = default_run_auto_options();
options.graph = GRAPH_LINEAR;
run_auto_test(nullptr, NUM_TOXES, test_dht_nodes_request, sizeof(State), &options);
return 0;
}
#undef NUM_TOXES