Files
tomato-testing/toxcore/DHT_test_util.cc
Green Sky 565efa4f39 Squashed 'external/toxcore/c-toxcore/' changes from 1828c5356..c9cdae001
c9cdae001 fix(toxav): remove extra copy of video frame on encode
4f6d4546b test: Improve the fake network library.
a2581e700 refactor(toxcore): generate `Friend_Request` and `Dht_Nodes_Response`
2aaa11770 refactor(toxcore): use Tox_Memory in generated events
5c367452b test(toxcore): fix incorrect mutex in tox_scenario_get_time
8f92e710f perf: Add a timed limit of number of cookie requests.
695b6417a test: Add some more simulated network support.
815ae9ce9 test(toxcore): fix thread-safety in scenario framework
6d85c754e test(toxcore): add unit tests for net_crypto
9c22e79cc test(support): add SimulatedEnvironment for deterministic testing
f34fcb195 chore: Update windows Dockerfile to debian stable (trixie).
ece0e8980 fix(group_moderation): allow validating unsorted sanction list signatures
a4fa754d7 refactor: rename struct Packet to struct Net_Packet
d6f330f85 cleanup: Fix some warnings from coverity.
e206bffa2 fix(group_chats): fix sync packets reverting topics
0e4715598 test: Add new scenario testing framework.
668291f44 refactor(toxcore): decouple Network_Funcs from sockaddr via IP_Port
fc4396cef fix: potential division by zero in toxav and unsafe hex parsing
8e8b352ab refactor: Add nullable annotations to struct members.
7740bb421 refactor: decouple net_crypto from DHT
1936d4296 test: add benchmark for toxav audio and video
46bfdc2df fix: correct printf format specifiers for unsigned integers
REVERT: 1828c5356 fix(toxav): remove extra copy of video frame on encode

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: c9cdae001341e701fca980c9bb9febfeb95d2902
2026-01-11 14:42:31 +01:00

175 lines
5.5 KiB
C++

#include "DHT_test_util.hh"
#include <cassert>
#include <cstring>
#include <iomanip>
#include "../testing/support/public/simulated_environment.hh"
#include "DHT.h"
#include "crypto_core.h"
#include "crypto_core_test_util.hh"
#include "network.h"
#include "network_test_util.hh"
using tox::test::FakeClock;
// --- Mock DHT Implementation ---
MockDHT::MockDHT(const Random *rng) { crypto_new_keypair(rng, self_public_key, self_secret_key); }
const uint8_t *MockDHT::get_shared_key(const uint8_t *pk)
{
std::array<uint8_t, CRYPTO_PUBLIC_KEY_SIZE> pk_arr;
std::copy(pk, pk + CRYPTO_PUBLIC_KEY_SIZE, pk_arr.begin());
auto it = shared_keys.find(pk_arr);
if (it != shared_keys.end()) {
return it->second.data();
}
++computation_count;
// Compute new shared key
std::array<uint8_t, CRYPTO_SHARED_KEY_SIZE> sk;
encrypt_precompute(pk, self_secret_key, sk.data());
shared_keys[pk_arr] = sk;
return shared_keys[pk_arr].data();
}
const Net_Crypto_DHT_Funcs MockDHT::funcs = {
[](void *obj, const uint8_t *public_key) {
return static_cast<MockDHT *>(obj)->get_shared_key(public_key);
},
[](const void *obj) { return static_cast<const MockDHT *>(obj)->self_public_key; },
[](const void *obj) { return static_cast<const MockDHT *>(obj)->self_secret_key; },
};
// --- WrappedMockDHT Implementation ---
WrappedMockDHT::WrappedMockDHT(tox::test::SimulatedEnvironment &env, uint16_t port)
: node_(env.create_node(0))
, logger_(logger_new(&node_->c_memory), [](Logger *l) { logger_kill(l); })
, mono_time_(mono_time_new(
&node_->c_memory,
[](void *ud) -> uint64_t {
return static_cast<tox::test::FakeClock *>(ud)->current_time_ms();
},
&env.fake_clock()),
[mem = &node_->c_memory](Mono_Time *t) { mono_time_free(mem, t); })
, networking_(nullptr, [](Networking_Core *n) { kill_networking(n); })
, dht_(&node_->c_random)
{
// Setup Networking
IP ip;
ip_init(&ip, false);
unsigned int error = 0;
networking_.reset(new_networking_ex(
logger_.get(), &node_->c_memory, &node_->c_network, &ip, port, port + 1, &error));
assert(error == 0);
node_->endpoint = node_->node->get_primary_socket();
assert(node_->endpoint != nullptr);
assert(node_->endpoint->local_port() == port);
}
WrappedMockDHT::~WrappedMockDHT() = default;
IP_Port WrappedMockDHT::get_ip_port() const
{
IP_Port ip_port;
ip_port.ip = node_->node->ip;
ip_port.port = net_htons(node_->endpoint->local_port());
return ip_port;
}
void WrappedMockDHT::poll()
{
mono_time_update(mono_time_.get());
networking_poll(networking_.get(), nullptr);
}
const Net_Crypto_DHT_Funcs WrappedMockDHT::funcs = MockDHT::funcs;
// --- WrappedDHT Implementation ---
WrappedDHT::WrappedDHT(tox::test::SimulatedEnvironment &env, uint16_t port)
: node_(env.create_node(0))
, logger_(logger_new(&node_->c_memory), [](Logger *l) { logger_kill(l); })
, mono_time_(
mono_time_new(
&node_->c_memory,
[](void *ud) -> uint64_t { return static_cast<FakeClock *>(ud)->current_time_ms(); },
&env.fake_clock()),
[mem = &node_->c_memory](Mono_Time *t) { mono_time_free(mem, t); })
, networking_(nullptr, [](Networking_Core *n) { kill_networking(n); })
, dht_(nullptr, [](DHT *d) { kill_dht(d); })
{
// Setup Networking
IP ip;
ip_init(&ip, false);
unsigned int error = 0;
networking_.reset(new_networking_ex(
logger_.get(), &node_->c_memory, &node_->c_network, &ip, port, port + 1, &error));
assert(error == 0);
node_->endpoint = node_->node->get_primary_socket();
assert(node_->endpoint != nullptr);
assert(node_->endpoint->local_port() == port);
// Setup DHT
dht_.reset(new_dht(logger_.get(), &node_->c_memory, &node_->c_random, &node_->c_network,
mono_time_.get(), networking_.get(), true, true));
}
WrappedDHT::~WrappedDHT() = default;
const uint8_t *WrappedDHT::dht_public_key() const { return dht_get_self_public_key(dht_.get()); }
const uint8_t *WrappedDHT::dht_secret_key() const { return dht_get_self_secret_key(dht_.get()); }
IP_Port WrappedDHT::get_ip_port() const
{
IP_Port ip_port;
ip_port.ip = node_->node->ip;
ip_port.port = net_htons(node_->endpoint->local_port());
return ip_port;
}
void WrappedDHT::poll()
{
mono_time_update(mono_time_.get());
networking_poll(networking_.get(), nullptr);
do_dht(dht_.get());
}
const Net_Crypto_DHT_Funcs WrappedDHT::funcs = {
[](void *obj, const uint8_t *public_key) {
return dht_get_shared_key_sent(static_cast<DHT *>(obj), public_key);
},
[](const void *obj) { return dht_get_self_public_key(static_cast<const DHT *>(obj)); },
[](const void *obj) { return dht_get_self_secret_key(static_cast<const DHT *>(obj)); },
};
// --- Test Util Functions ---
Node_format random_node_format(const Random *rng)
{
Node_format node;
auto const pk = random_pk(rng);
std::copy(pk.begin(), pk.end(), node.public_key);
node.ip_port = random_ip_port(rng);
return node;
}
bool operator==(Node_format const &a, Node_format const &b)
{
return std::memcmp(a.public_key, b.public_key, sizeof(a.public_key)) == 0
&& a.ip_port == b.ip_port;
}
std::ostream &operator<<(std::ostream &out, Node_format const &v)
{
return out << "\n Node_format{\n"
<< " public_key = " << PublicKey(v.public_key) << ",\n"
<< " ip_port = " << v.ip_port << " }";
}