Files
tomato/testing/support/bootstrap_scaling_test.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

110 lines
3.8 KiB
C++

// clang-format off
#include "public/simulation.hh"
// clang-format on
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <vector>
#include "../../toxcore/network.h"
#include "../../toxcore/tox.h"
namespace tox::test {
namespace {
class BootstrapScalingTest : public ::testing::Test {
protected:
Simulation sim;
};
TEST_F(BootstrapScalingTest, TwentyNodes)
{
sim.net().set_verbose(false);
const int num_nodes = 20;
std::vector<std::unique_ptr<SimulatedNode>> nodes;
std::vector<SimulatedNode::ToxPtr> toxes;
std::cerr << "[Test] Creating " << num_nodes << " nodes..." << std::endl;
// Create all nodes and tox instances
for (int i = 0; i < num_nodes; ++i) {
auto node = sim.create_node();
auto tox = node->create_tox();
ASSERT_NE(tox, nullptr) << "Failed to create Tox instance " << i;
nodes.push_back(std::move(node));
toxes.push_back(std::move(tox));
}
// Node 0 is the bootstrap target
uint8_t bootstrap_dht_id[TOX_PUBLIC_KEY_SIZE];
tox_self_get_dht_id(toxes[0].get(), bootstrap_dht_id);
FakeUdpSocket *main_sock = nodes[0]->get_primary_socket();
ASSERT_NE(main_sock, nullptr) << "Node 0 has no primary socket";
uint16_t bootstrap_port = main_sock->local_port();
char bootstrap_ip[TOX_INET_ADDRSTRLEN];
ip_parse_addr(&nodes[0]->ip, bootstrap_ip, sizeof(bootstrap_ip));
std::cerr << "[Test] Bootstrapping to Node 0 at " << bootstrap_ip << ":" << bootstrap_port
<< std::endl;
size_t total_packets = 0;
sim.net().add_observer([&](const Packet &) { total_packets++; });
// Everyone else bootstraps to Node 0
for (int i = 1; i < num_nodes; ++i) {
Tox_Err_Bootstrap err;
bool success = tox_bootstrap(
toxes[i].get(), bootstrap_ip, bootstrap_port, bootstrap_dht_id, &err);
ASSERT_TRUE(success) << "Bootstrap call failed for node " << i << ": " << err;
}
// Run simulation until everyone is connected or timeout
const uint64_t virtual_timeout_ms = 300000;
bool all_connected = false;
sim.run_until(
[&]() {
all_connected = true;
size_t connected_count = 0;
for (int i = 0; i < num_nodes; ++i) {
tox_iterate(toxes[i].get(), nullptr);
if (tox_self_get_connection_status(toxes[i].get()) != TOX_CONNECTION_NONE) {
connected_count++;
} else {
all_connected = false;
}
}
static uint64_t last_print = 0;
if (sim.clock().current_time_ms() - last_print > 5000) {
std::cerr << "[Test] DHT connected: " << connected_count << "/" << num_nodes
<< " at " << sim.clock().current_time_ms()
<< "ms. Total packets: " << total_packets << std::endl;
last_print = sim.clock().current_time_ms();
}
return all_connected;
},
virtual_timeout_ms);
if (!all_connected) {
std::cerr << "[Test] Bootstrap failed. Status of nodes:" << std::endl;
for (int i = 0; i < num_nodes; ++i) {
Tox_Connection status = tox_self_get_connection_status(toxes[i].get());
if (status == TOX_CONNECTION_NONE) {
std::cerr << " Node " << i << " is NOT connected." << std::endl;
}
}
}
EXPECT_TRUE(all_connected)
<< "Only " << (num_nodes - (all_connected ? 0 : 1)) << " nodes connected? Check logs.";
}
} // namespace
} // namespace tox::test