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
This commit is contained in:
Green Sky
2026-01-11 14:42:31 +01:00
parent e95f2cbb1c
commit 565efa4f39
328 changed files with 19057 additions and 13982 deletions

View File

@@ -0,0 +1,28 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_CLOCK_H
#define C_TOXCORE_TESTING_SUPPORT_CLOCK_H
#include <cstdint>
namespace tox::test {
/**
* @brief Abstraction over the system's monotonic clock.
*/
class ClockSystem {
public:
virtual ~ClockSystem();
/**
* @brief Returns current monotonic time in milliseconds.
*/
virtual uint64_t current_time_ms() const = 0;
/**
* @brief Returns current monotonic time in seconds.
*/
virtual uint64_t current_time_s() const = 0;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_CLOCK_H

View File

@@ -0,0 +1,47 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_ENVIRONMENT_H
#define C_TOXCORE_TESTING_SUPPORT_ENVIRONMENT_H
#include <memory>
namespace tox::test {
class NetworkSystem;
class ClockSystem;
class RandomSystem;
class MemorySystem;
/**
* @brief Service locator for system resources in tests.
*
* This interface allows tests to access system resources (Network, Time, RNG,
* memory) in a way that can be swapped between real implementations (for
* integration tests) and simulated ones (for unit/fuzz tests).
*/
class Environment {
public:
virtual ~Environment();
/**
* @brief Access the network subsystem.
*/
virtual NetworkSystem &network() = 0;
/**
* @brief Access the monotonic clock and timer subsystem.
*/
virtual ClockSystem &clock() = 0;
/**
* @brief Access the random number generator.
*/
virtual RandomSystem &random() = 0;
/**
* @brief Access the memory allocator.
*/
virtual MemorySystem &memory() = 0;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_ENVIRONMENT_H

View File

@@ -0,0 +1,183 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_DATA_H
#define C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_DATA_H
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>
namespace tox::test {
struct Fuzz_Data {
static constexpr bool FUZZ_DEBUG = false;
static constexpr std::size_t TRACE_TRAP = -1;
private:
const uint8_t *data_;
const uint8_t *base_;
std::size_t size_;
public:
Fuzz_Data(const uint8_t *input_data, std::size_t input_size)
: data_(input_data)
, base_(input_data)
, size_(input_size)
{
}
Fuzz_Data &operator=(const Fuzz_Data &rhs) = delete;
Fuzz_Data(const Fuzz_Data &rhs) = delete;
struct Consumer {
const char *func;
Fuzz_Data &fd;
operator bool()
{
if (fd.empty())
return false;
const bool val = fd.data_[0];
if (FUZZ_DEBUG) {
std::printf("consume@%zu(%s): bool %s\n", fd.pos(), func, val ? "true" : "false");
}
++fd.data_;
--fd.size_;
return val;
}
template <typename T>
operator T()
{
if (sizeof(T) > fd.size())
return T{};
const uint8_t *bytes = fd.consume(func, sizeof(T));
T val;
std::memcpy(&val, bytes, sizeof(T));
return val;
}
};
Consumer consume1(const char *func) { return Consumer{func, *this}; }
template <typename T>
T consume_integral()
{
return consume1("consume_integral");
}
template <typename T>
T consume_integral_in_range(T min, T max)
{
if (min >= max)
return min;
T val = consume_integral<T>();
return min + (val % (max - min + 1));
}
std::size_t remaining_bytes() const { return size(); }
std::vector<uint8_t> consume_bytes(std::size_t count)
{
if (count == 0 || count > size_)
return {};
const uint8_t *start = consume("consume_bytes", count);
if (!start)
return {};
return std::vector<uint8_t>(start, start + count);
}
std::vector<uint8_t> consume_remaining_bytes()
{
if (empty())
return {};
std::size_t count = size();
const uint8_t *start = consume("consume_remaining_bytes", count);
return std::vector<uint8_t>(start, start + count);
}
std::size_t size() const { return size_; }
std::size_t pos() const { return data_ - base_; }
const uint8_t *data() const { return data_; }
bool empty() const { return size_ == 0; }
const uint8_t *consume(const char *func, std::size_t count)
{
if (count > size_)
return nullptr;
const uint8_t *val = data_;
if (FUZZ_DEBUG) {
if (count == 1) {
std::printf("consume@%zu(%s): %d (0x%02x)\n", pos(), func, val[0], val[0]);
} else if (count != 0) {
std::printf("consume@%zu(%s): %02x..%02x[%zu]\n", pos(), func, val[0],
val[count - 1], count);
}
}
data_ += count;
size_ -= count;
return val;
}
};
#define CONSUME1_OR_RETURN(TYPE, NAME, INPUT) \
if ((INPUT).size() < sizeof(TYPE)) { \
return; \
} \
TYPE NAME = (INPUT).consume1(__func__)
#define CONSUME1_OR_RETURN_VAL(TYPE, NAME, INPUT, VAL) \
if ((INPUT).size() < sizeof(TYPE)) { \
return (VAL); \
} \
TYPE NAME = (INPUT).consume1(__func__)
#define CONSUME_OR_RETURN(DECL, INPUT, SIZE) \
if ((INPUT).size() < (SIZE)) { \
return; \
} \
DECL = (INPUT).consume(__func__, (SIZE))
#define CONSUME_OR_RETURN_VAL(DECL, INPUT, SIZE, VAL) \
if ((INPUT).size() < (SIZE)) { \
return (VAL); \
} \
DECL = (INPUT).consume(__func__, (SIZE))
using Fuzz_Target = void (*)(Fuzz_Data &input);
template <Fuzz_Target... Args>
struct Fuzz_Target_Selector;
template <Fuzz_Target Arg, Fuzz_Target... Args>
struct Fuzz_Target_Selector<Arg, Args...> {
static void select(uint8_t selector, Fuzz_Data &input)
{
if (selector == sizeof...(Args)) {
return Arg(input);
}
return Fuzz_Target_Selector<Args...>::select(selector, input);
}
};
template <>
struct Fuzz_Target_Selector<> {
static void select(uint8_t selector, Fuzz_Data &input)
{
// The selector selected no function, so we do nothing and rely on the
// fuzzer to come up with a better selector.
}
};
template <Fuzz_Target... Args>
void fuzz_select_target(const uint8_t *data, std::size_t size)
{
Fuzz_Data input{data, size};
CONSUME1_OR_RETURN(const uint8_t, selector, input);
return Fuzz_Target_Selector<Args...>::select(selector, input);
}
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_DATA_H

View File

@@ -0,0 +1,23 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_HELPERS_H
#define C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_HELPERS_H
#include "../doubles/fake_memory.hh"
#include "../doubles/fake_random.hh"
#include "../doubles/fake_sockets.hh"
#include "fuzz_data.hh"
namespace tox::test {
// Configures the socket to pull packets from the Fuzz_Data stream
// mimicking the legacy Fuzz_System behavior (2-byte length prefix).
void configure_fuzz_packet_source(FakeUdpSocket &socket, Fuzz_Data &input);
// Configures memory allocator to consume failure decisions from Fuzz_Data.
void configure_fuzz_memory_source(FakeMemory &memory, Fuzz_Data &input);
// Configures random generator to consume bytes from Fuzz_Data.
void configure_fuzz_random_source(FakeRandom &random, Fuzz_Data &input);
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_PUBLIC_FUZZ_HELPERS_H

View File

@@ -0,0 +1,23 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_PUBLIC_MEMORY_H
#define C_TOXCORE_TESTING_SUPPORT_PUBLIC_MEMORY_H
#include <cstddef>
#include <cstdint>
namespace tox::test {
/**
* @brief Abstraction over the memory allocator.
*/
class MemorySystem {
public:
virtual ~MemorySystem();
virtual void *malloc(size_t size) = 0;
virtual void *realloc(void *ptr, size_t size) = 0;
virtual void free(void *ptr) = 0;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_PUBLIC_MEMORY_H

View File

@@ -0,0 +1,51 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_NETWORK_H
#define C_TOXCORE_TESTING_SUPPORT_NETWORK_H
#include <cstdint>
#include <vector>
#include "../../../toxcore/network.h"
namespace tox::test {
/**
* @brief Abstraction over the network subsystem (sockets).
*/
class NetworkSystem {
public:
virtual ~NetworkSystem();
virtual Socket socket(int domain, int type, int protocol) = 0;
virtual int bind(Socket sock, const IP_Port *addr) = 0;
virtual int close(Socket sock) = 0;
virtual int sendto(Socket sock, const uint8_t *buf, size_t len, const IP_Port *addr) = 0;
virtual int recvfrom(Socket sock, uint8_t *buf, size_t len, IP_Port *addr) = 0;
// TCP Support
virtual int listen(Socket sock, int backlog) = 0;
virtual Socket accept(Socket sock) = 0;
virtual int connect(Socket sock, const IP_Port *addr) = 0;
virtual int send(Socket sock, const uint8_t *buf, size_t len) = 0;
virtual int recv(Socket sock, uint8_t *buf, size_t len) = 0;
virtual int recvbuf(Socket sock) = 0;
// Auxiliary
virtual int socket_nonblock(Socket sock, bool nonblock) = 0;
virtual int getsockopt(Socket sock, int level, int optname, void *optval, size_t *optlen) = 0;
virtual int setsockopt(Socket sock, int level, int optname, const void *optval, size_t optlen)
= 0;
};
/**
* @brief Helper to create an IPv4 IP struct from a host-byte-order address.
*/
IP make_ip(uint32_t ipv4);
/**
* @brief Helper to create a unique node IP in the 10.x.y.z range.
*/
IP make_node_ip(uint32_t node_id);
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_NETWORK_H

View File

@@ -0,0 +1,22 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_RANDOM_H
#define C_TOXCORE_TESTING_SUPPORT_RANDOM_H
#include <cstdint>
#include <vector>
namespace tox::test {
/**
* @brief Abstraction over the random number generator.
*/
class RandomSystem {
public:
virtual ~RandomSystem();
virtual uint32_t uniform(uint32_t upper_bound) = 0;
virtual void bytes(uint8_t *out, size_t count) = 0;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_RANDOM_H

View File

@@ -0,0 +1,78 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_SIMULATED_ENVIRONMENT_H
#define C_TOXCORE_TESTING_SUPPORT_SIMULATED_ENVIRONMENT_H
#include <memory>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <winsock2.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "../../../toxcore/tox_memory_impl.h"
#include "../../../toxcore/tox_private.h"
#include "../../../toxcore/tox_random_impl.h"
#include "../doubles/fake_clock.hh"
#include "../doubles/fake_memory.hh"
#include "../doubles/fake_random.hh"
#include "environment.hh"
#include "simulation.hh"
namespace tox::test {
struct ScopedToxSystem {
// The underlying node in the simulation
std::unique_ptr<SimulatedNode> node;
// Direct access to primary socket (for fuzzer injection)
FakeUdpSocket *endpoint;
// C structs
struct Network c_network;
struct Tox_Random c_random;
struct Tox_Memory c_memory;
// The main struct passed to tox_new
Tox_System system;
};
class SimulatedEnvironment : public Environment {
public:
SimulatedEnvironment();
~SimulatedEnvironment() override;
NetworkSystem &network() override;
ClockSystem &clock() override;
RandomSystem &random() override;
MemorySystem &memory() override;
FakeClock &fake_clock();
FakeRandom &fake_random();
FakeMemory &fake_memory();
Simulation &simulation() { return *sim_; }
/**
* @brief Creates a new virtual node in the simulation bound to the specified port.
*/
std::unique_ptr<ScopedToxSystem> create_node(uint16_t port);
void advance_time(uint64_t ms);
private:
std::unique_ptr<Simulation> sim_;
// Global instances for Environment interface compliance.
std::unique_ptr<FakeRandom> global_random_;
std::unique_ptr<FakeMemory> global_memory_;
// For network(), we can't return a per-node stack.
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_SIMULATED_ENVIRONMENT_H

View File

@@ -0,0 +1,112 @@
#ifndef C_TOXCORE_TESTING_SUPPORT_SIMULATION_H
#define C_TOXCORE_TESTING_SUPPORT_SIMULATION_H
#include <functional>
#include <memory>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <winsock2.h>
#else
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "../../../toxcore/tox.h"
#include "../../../toxcore/tox_memory_impl.h"
#include "../../../toxcore/tox_private.h"
#include "../../../toxcore/tox_random_impl.h"
#include "../doubles/fake_clock.hh"
#include "../doubles/fake_memory.hh"
#include "../doubles/fake_network_stack.hh"
#include "../doubles/fake_random.hh"
#include "../doubles/network_universe.hh"
#include "environment.hh"
namespace tox::test {
class SimulatedNode;
/**
* @brief The Simulation World.
* Holds the Clock and the Universe.
*/
class Simulation {
public:
Simulation();
~Simulation();
// Time Control
void advance_time(uint64_t ms);
void run_until(std::function<bool()> condition, uint64_t timeout_ms = 5000);
// Global Access
FakeClock &clock() { return *clock_; }
NetworkUniverse &net() { return *net_; }
// Node Factory
std::unique_ptr<SimulatedNode> create_node();
private:
std::unique_ptr<FakeClock> clock_;
std::unique_ptr<NetworkUniverse> net_;
uint32_t node_count_ = 0;
};
/**
* @brief Represents a single node in the simulation.
* Implements the Environment interface for dependency injection.
*/
class SimulatedNode : public Environment {
public:
explicit SimulatedNode(Simulation &sim, uint32_t node_id);
~SimulatedNode() override;
// Environment Interface
NetworkSystem &network() override;
ClockSystem &clock() override;
RandomSystem &random() override;
MemorySystem &memory() override;
// Direct Access to Fakes
FakeNetworkStack &fake_network() { return *network_; }
FakeRandom &fake_random() { return *random_; }
FakeMemory &fake_memory() { return *memory_; }
// Tox Creation Helper
// Returns a configured Tox instance bound to this node's environment.
// The user owns the Tox instance.
struct ToxDeleter {
void operator()(Tox *t) const { tox_kill(t); }
};
using ToxPtr = std::unique_ptr<Tox, ToxDeleter>;
ToxPtr create_tox(const Tox_Options *options = nullptr);
// Helper to get C structs for manual injection
struct Network get_c_network() { return network_->get_c_network(); }
struct Tox_Random get_c_random() { return random_->get_c_random(); }
struct Tox_Memory get_c_memory() { return memory_->get_c_memory(); }
// For fuzzing compatibility (exposes first bound UDP socket as "endpoint")
FakeUdpSocket *get_primary_socket();
private:
Simulation &sim_;
std::unique_ptr<FakeNetworkStack> network_;
std::unique_ptr<FakeRandom> random_;
std::unique_ptr<FakeMemory> memory_;
// C-compatible views (must stay valid for the lifetime of Tox)
public:
struct Network c_network;
struct Tox_Random c_random;
struct Tox_Memory c_memory;
struct IP ip;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_SIMULATION_H

View File

@@ -0,0 +1,82 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright © 2026 The TokTok team.
*/
#ifndef C_TOXCORE_TESTING_SUPPORT_TOX_NETWORK_H
#define C_TOXCORE_TESTING_SUPPORT_TOX_NETWORK_H
#include <vector>
#include "simulation.hh"
namespace tox::test {
struct ConnectedFriend {
std::unique_ptr<SimulatedNode> node;
SimulatedNode::ToxPtr tox;
uint32_t friend_number;
ConnectedFriend(std::unique_ptr<SimulatedNode> node_in, SimulatedNode::ToxPtr tox_in,
uint32_t friend_number_in)
: node(std::move(node_in))
, tox(std::move(tox_in))
, friend_number(friend_number_in)
{
}
ConnectedFriend(ConnectedFriend &&) = default;
ConnectedFriend &operator=(ConnectedFriend &&) = default;
~ConnectedFriend();
};
/**
* @brief Sets up a network of connected Tox instances.
*
* This function creates num_friends Tox instances, adds them as friends to main_tox,
* and bootstraps them to main_node. It then runs the simulation until all friends
* are connected to main_tox.
*
* @param sim The simulation to run.
* @param main_tox The main Tox instance to which friends will connect.
* @param main_node The simulated node hosting the main Tox instance.
* @param num_friends The number of friends to create and connect.
* @param options Optional Tox_Options to use for the friend Tox instances.
* @return A vector of ConnectedFriend structures, each representing a friend.
*/
std::vector<ConnectedFriend> setup_connected_friends(Simulation &sim, Tox *main_tox,
SimulatedNode &main_node, int num_friends, const Tox_Options *options = nullptr);
/**
* @brief Connects two existing Tox instances as friends.
*
* This function adds each Tox instance as a friend to the other, bootstraps
* them to each other, and runs the simulation until they are connected.
*
* @param sim The simulation to run.
* @param node1 The simulated node hosting the first Tox instance.
* @param tox1 The first Tox instance.
* @param node2 The simulated node hosting the second Tox instance.
* @param tox2 The second Tox instance.
* @return True if connected successfully, false otherwise.
*/
bool connect_friends(
Simulation &sim, SimulatedNode &node1, Tox *tox1, SimulatedNode &node2, Tox *tox2);
/**
* @brief Sets up a group and has all friends join it.
*
* This function creates a new public group on main_tox, invites all friends in the
* provided vector, and runs the simulation until all friends have joined and
* main_tox sees all of them.
*
* @param sim The simulation to run.
* @param main_tox The main Tox instance that creates the group.
* @param friends The friends to invite to the group.
* @return The group number on the main Tox instance, or UINT32_MAX on failure.
*/
uint32_t setup_connected_group(
Simulation &sim, Tox *main_tox, const std::vector<ConnectedFriend> &friends);
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_TOX_NETWORK_H