Squashed 'external/toxcore/c-toxcore/' changes from c9cdae001..9ed2fa80d
9ed2fa80d fix(toxav): remove extra copy of video frame on encode de30cf3ad docs: Add new file kinds, that should be useful to all clients. d5b5e879d fix(DHT): Correct node skipping logic timed out nodes. 30e71fe97 refactor: Generate event dispatch functions and add tox_events_dispatch. 8fdbb0b50 style: Format parameter lists in event handlers. d00dee12b refactor: Add warning logs when losing chat invites. b144e8db1 feat: Add a way to look up a file number by ID. 849281ea0 feat: Add a way to fetch groups by chat ID. a2c177396 refactor: Harden event system and improve type safety. 8f5caa656 refactor: Add MessagePack string support to bin_pack. 34e8d5ad5 chore: Add GitHub CodeQL workflow and local Docker runner. f7b068010 refactor: Add nullability annotations to event headers. 788abe651 refactor(toxav): Use system allocator for mutexes. 2e4b423eb refactor: Use specific typedefs for public API arrays. 2baf34775 docs(toxav): update idle iteration interval see 679444751876fa3882a717772918ebdc8f083354 2f87ac67b feat: Add Event Loop abstraction (Ev). f8dfc38d8 test: Fix data race in ToxScenario virtual_clock. 38313921e test(TCP): Add regression test for TCP priority queue integrity. f94a50d9a refactor(toxav): Replace mutable_mutex with dynamically allocated mutex. ad054511e refactor: Internalize DHT structs and add debug helpers. 8b467cc96 fix: Prevent potential integer overflow in group chat handshake. 4962bdbb8 test: Improve TCP simulation and add tests 5f0227093 refactor: Allow nullable data in group chat handlers. e97b18ea9 chore: Improve Windows Docker support. b14943bbd refactor: Move Logger out of Messenger into Tox. dd3136250 cleanup: Apply nullability qualifiers to C++ codebase. 1849f70fc refactor: Extract low-level networking code to net and os_network. 8fec75421 refactor: Delete tox_random, align on rng and os_random. a03ae8051 refactor: Delete tox_memory, align on mem and os_memory. 4c88fed2c refactor: Use `std::` prefixes more consistently in C++ code. 72452f2ae test: Add some more tests for onion and shared key cache. d5a51b09a cleanup: Use tox_attributes.h in tox_private.h and install it. b6f5b9fc5 test: Add some benchmarks for various high level things. 8a8d02785 test(support): Introduce threaded Tox runner and simulation barrier d68d1d095 perf(toxav): optimize audio and video intermediate buffers by keeping them around REVERT: c9cdae001 fix(toxav): remove extra copy of video frame on encode git-subtree-dir: external/toxcore/c-toxcore git-subtree-split: 9ed2fa80d582c714d6bdde6a7648220a92cddff8
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
struct Fuzz_Data {
|
||||
@@ -14,12 +16,12 @@ struct Fuzz_Data {
|
||||
static constexpr std::size_t TRACE_TRAP = -1;
|
||||
|
||||
private:
|
||||
const uint8_t *data_;
|
||||
const uint8_t *base_;
|
||||
const uint8_t *_Nonnull data_;
|
||||
const uint8_t *_Nonnull base_;
|
||||
std::size_t size_;
|
||||
|
||||
public:
|
||||
Fuzz_Data(const uint8_t *input_data, std::size_t input_size)
|
||||
Fuzz_Data(const uint8_t *_Nonnull input_data, std::size_t input_size)
|
||||
: data_(input_data)
|
||||
, base_(input_data)
|
||||
, size_(input_size)
|
||||
@@ -30,7 +32,7 @@ public:
|
||||
Fuzz_Data(const Fuzz_Data &rhs) = delete;
|
||||
|
||||
struct Consumer {
|
||||
const char *func;
|
||||
const char *_Nonnull func;
|
||||
Fuzz_Data &fd;
|
||||
|
||||
operator bool()
|
||||
@@ -51,14 +53,14 @@ public:
|
||||
{
|
||||
if (sizeof(T) > fd.size())
|
||||
return T{};
|
||||
const uint8_t *bytes = fd.consume(func, sizeof(T));
|
||||
const uint8_t *_Nonnull 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}; }
|
||||
Consumer consume1(const char *_Nonnull func) { return Consumer{func, *this}; }
|
||||
|
||||
template <typename T>
|
||||
T consume_integral()
|
||||
@@ -81,7 +83,7 @@ public:
|
||||
{
|
||||
if (count == 0 || count > size_)
|
||||
return {};
|
||||
const uint8_t *start = consume("consume_bytes", count);
|
||||
const uint8_t *_Nullable start = consume("consume_bytes", count);
|
||||
if (!start)
|
||||
return {};
|
||||
return std::vector<uint8_t>(start, start + count);
|
||||
@@ -92,20 +94,20 @@ public:
|
||||
if (empty())
|
||||
return {};
|
||||
std::size_t count = size();
|
||||
const uint8_t *start = consume("consume_remaining_bytes", count);
|
||||
const uint8_t *_Nonnull 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_; }
|
||||
const uint8_t *_Nonnull data() const { return data_; }
|
||||
bool empty() const { return size_ == 0; }
|
||||
|
||||
const uint8_t *consume(const char *func, std::size_t count)
|
||||
const uint8_t *_Nullable consume(const char *_Nonnull func, std::size_t count)
|
||||
{
|
||||
if (count > size_)
|
||||
return nullptr;
|
||||
const uint8_t *val = data_;
|
||||
const uint8_t *_Nonnull val = data_;
|
||||
if (FUZZ_DEBUG) {
|
||||
if (count == 1) {
|
||||
std::printf("consume@%zu(%s): %d (0x%02x)\n", pos(), func, val[0], val[0]);
|
||||
@@ -170,7 +172,7 @@ struct Fuzz_Target_Selector<> {
|
||||
};
|
||||
|
||||
template <Fuzz_Target... Args>
|
||||
void fuzz_select_target(const uint8_t *data, std::size_t size)
|
||||
void fuzz_select_target(const uint8_t *_Nonnull data, std::size_t size)
|
||||
{
|
||||
Fuzz_Data input{data, size};
|
||||
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
|
||||
// Forward declaration
|
||||
struct Memory;
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
/**
|
||||
@@ -13,9 +18,14 @@ 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;
|
||||
virtual void *_Nullable malloc(size_t size) = 0;
|
||||
virtual void *_Nullable realloc(void *_Nullable ptr, size_t size) = 0;
|
||||
virtual void free(void *_Nullable ptr) = 0;
|
||||
|
||||
/**
|
||||
* @brief Returns C-compatible Memory struct.
|
||||
*/
|
||||
virtual struct Memory c_memory() = 0;
|
||||
};
|
||||
|
||||
} // namespace tox::test
|
||||
|
||||
83
testing/support/public/mpsc_queue.hh
Normal file
83
testing/support/public/mpsc_queue.hh
Normal file
@@ -0,0 +1,83 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2026 The TokTok team.
|
||||
*/
|
||||
#ifndef C_TOXCORE_TESTING_SUPPORT_MPSC_QUEUE_H
|
||||
#define C_TOXCORE_TESTING_SUPPORT_MPSC_QUEUE_H
|
||||
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
/**
|
||||
* @brief Multiple Producer, Single Consumer Queue.
|
||||
*
|
||||
* This queue implementation provides thread-safe access for multiple producers
|
||||
* pushing items and a single consumer popping items. It uses a `std::mutex`
|
||||
* and `std::condition_variable` for synchronization.
|
||||
*
|
||||
* @tparam T The type of elements stored in the queue.
|
||||
*/
|
||||
template <typename T>
|
||||
class MpscQueue {
|
||||
public:
|
||||
MpscQueue() = default;
|
||||
~MpscQueue() = default;
|
||||
|
||||
// Disable copy/move to prevent accidental sharing/slicing issues
|
||||
MpscQueue(const MpscQueue &) = delete;
|
||||
MpscQueue &operator=(const MpscQueue &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Pushes a value onto the queue.
|
||||
* Thread-safe (Multiple Producers).
|
||||
*/
|
||||
void push(T value)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
queue_.push_back(std::move(value));
|
||||
}
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Pops a value from the queue, blocking if empty.
|
||||
* Thread-safe (Single Consumer).
|
||||
*/
|
||||
T pop()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait(lock, [this] { return !queue_.empty(); });
|
||||
T value = std::move(queue_.front());
|
||||
queue_.pop_front();
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tries to pop a value from the queue without blocking.
|
||||
* Thread-safe (Single Consumer).
|
||||
*
|
||||
* @param out Reference to store the popped value.
|
||||
* @return true if a value was popped, false if the queue was empty.
|
||||
*/
|
||||
bool try_pop(T &out)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (queue_.empty())
|
||||
return false;
|
||||
out = std::move(queue_.front());
|
||||
queue_.pop_front();
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
std::deque<T> queue_;
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
};
|
||||
|
||||
} // namespace tox::test
|
||||
|
||||
#endif // C_TOXCORE_TESTING_SUPPORT_MPSC_QUEUE_H
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
#include "../../../toxcore/net.h"
|
||||
#include "../../../toxcore/network.h"
|
||||
|
||||
namespace tox::test {
|
||||
@@ -16,24 +18,35 @@ 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 bind(Socket sock, const IP_Port *_Nonnull 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;
|
||||
virtual int sendto(
|
||||
Socket sock, const uint8_t *_Nonnull buf, size_t len, const IP_Port *_Nonnull addr)
|
||||
= 0;
|
||||
virtual int recvfrom(Socket sock, uint8_t *_Nonnull buf, size_t len, IP_Port *_Nonnull 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 connect(Socket sock, const IP_Port *_Nonnull addr) = 0;
|
||||
virtual int send(Socket sock, const uint8_t *_Nonnull buf, size_t len) = 0;
|
||||
virtual int recv(Socket sock, uint8_t *_Nonnull 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)
|
||||
virtual int getsockopt(
|
||||
Socket sock, int level, int optname, void *_Nonnull optval, size_t *_Nonnull optlen)
|
||||
= 0;
|
||||
virtual int setsockopt(
|
||||
Socket sock, int level, int optname, const void *_Nonnull optval, size_t optlen)
|
||||
= 0;
|
||||
|
||||
/**
|
||||
* @brief Returns C-compatible Network struct.
|
||||
*/
|
||||
virtual struct Network c_network() = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,11 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
|
||||
// Forward declaration
|
||||
struct Random;
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
/**
|
||||
@@ -14,7 +19,12 @@ public:
|
||||
virtual ~RandomSystem();
|
||||
|
||||
virtual uint32_t uniform(uint32_t upper_bound) = 0;
|
||||
virtual void bytes(uint8_t *out, size_t count) = 0;
|
||||
virtual void bytes(uint8_t *_Nonnull out, size_t count) = 0;
|
||||
|
||||
/**
|
||||
* @brief Returns C-compatible Random struct.
|
||||
*/
|
||||
virtual struct Random c_random() = 0;
|
||||
};
|
||||
|
||||
} // namespace tox::test
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include "../../../toxcore/tox_memory_impl.h"
|
||||
#include "../../../toxcore/attributes.h"
|
||||
#include "../../../toxcore/mem.h"
|
||||
#include "../../../toxcore/rng.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"
|
||||
@@ -29,12 +30,12 @@ struct ScopedToxSystem {
|
||||
std::unique_ptr<SimulatedNode> node;
|
||||
|
||||
// Direct access to primary socket (for fuzzer injection)
|
||||
FakeUdpSocket *endpoint;
|
||||
FakeUdpSocket *_Nullable endpoint;
|
||||
|
||||
// C structs
|
||||
struct Network c_network;
|
||||
struct Tox_Random c_random;
|
||||
struct Tox_Memory c_memory;
|
||||
struct Random c_random;
|
||||
struct Memory c_memory;
|
||||
|
||||
// The main struct passed to tox_new
|
||||
Tox_System system;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef C_TOXCORE_TESTING_SUPPORT_SIMULATION_H
|
||||
#define C_TOXCORE_TESTING_SUPPORT_SIMULATION_H
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@@ -14,10 +16,13 @@
|
||||
#include <sys/socket.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
#include "../../../toxcore/mem.h"
|
||||
#include "../../../toxcore/rng.h"
|
||||
#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"
|
||||
@@ -29,12 +34,61 @@ namespace tox::test {
|
||||
|
||||
class SimulatedNode;
|
||||
|
||||
struct LogMetadata {
|
||||
Tox_Log_Level level;
|
||||
const char *_Nonnull file;
|
||||
uint32_t line;
|
||||
const char *_Nonnull func;
|
||||
const char *_Nonnull message;
|
||||
uint32_t node_id;
|
||||
};
|
||||
|
||||
using LogPredicate = std::function<bool(const LogMetadata &)>;
|
||||
|
||||
struct LogFilter {
|
||||
LogPredicate pred;
|
||||
|
||||
LogFilter() = default;
|
||||
explicit LogFilter(LogPredicate p)
|
||||
: pred(std::move(p))
|
||||
{
|
||||
}
|
||||
|
||||
bool operator()(const LogMetadata &md) const { return !pred || pred(md); }
|
||||
};
|
||||
|
||||
LogFilter operator&&(const LogFilter &lhs, const LogFilter &rhs);
|
||||
LogFilter operator||(const LogFilter &lhs, const LogFilter &rhs);
|
||||
LogFilter operator!(const LogFilter &target);
|
||||
|
||||
namespace log_filter {
|
||||
LogFilter level(Tox_Log_Level min_level);
|
||||
|
||||
struct LevelPlaceholder {
|
||||
LogFilter operator>(Tox_Log_Level rhs) const;
|
||||
LogFilter operator>=(Tox_Log_Level rhs) const;
|
||||
LogFilter operator<(Tox_Log_Level rhs) const;
|
||||
LogFilter operator<=(Tox_Log_Level rhs) const;
|
||||
LogFilter operator==(Tox_Log_Level rhs) const;
|
||||
LogFilter operator!=(Tox_Log_Level rhs) const;
|
||||
};
|
||||
|
||||
LevelPlaceholder level();
|
||||
|
||||
LogFilter file(std::string pattern);
|
||||
LogFilter func(std::string pattern);
|
||||
LogFilter message(std::string pattern);
|
||||
LogFilter node(uint32_t id);
|
||||
} // namespace log_filter
|
||||
|
||||
/**
|
||||
* @brief The Simulation World.
|
||||
* Holds the Clock and the Universe.
|
||||
*/
|
||||
class Simulation {
|
||||
public:
|
||||
static constexpr uint32_t kDefaultTickIntervalMs = 50;
|
||||
|
||||
Simulation();
|
||||
~Simulation();
|
||||
|
||||
@@ -42,9 +96,66 @@ public:
|
||||
void advance_time(uint64_t ms);
|
||||
void run_until(std::function<bool()> condition, uint64_t timeout_ms = 5000);
|
||||
|
||||
// Logging
|
||||
void set_log_filter(LogFilter filter);
|
||||
const LogFilter &log_filter() const { return log_filter_; }
|
||||
|
||||
// Synchronization Barrier
|
||||
// These methods coordinate the lock-step execution of multiple Tox runners.
|
||||
|
||||
/**
|
||||
* @brief Registers a new runner with the simulation barrier.
|
||||
* @return The current generation ID of the simulation.
|
||||
*/
|
||||
uint64_t register_runner();
|
||||
|
||||
/**
|
||||
* @brief Unregisters a runner from the simulation barrier.
|
||||
*
|
||||
* This ensures the simulation does not block waiting for a terminated runner.
|
||||
*/
|
||||
void unregister_runner();
|
||||
|
||||
using TickListenerId = int;
|
||||
|
||||
/**
|
||||
* @brief Registers a callback to be invoked when a new simulation tick starts.
|
||||
*
|
||||
* @param listener The function to call with the new generation ID.
|
||||
* @return An ID handle for unregistering the listener.
|
||||
*/
|
||||
TickListenerId register_tick_listener(std::function<void(uint64_t)> listener);
|
||||
|
||||
/**
|
||||
* @brief Unregisters a tick listener.
|
||||
*/
|
||||
void unregister_tick_listener(TickListenerId id);
|
||||
|
||||
/**
|
||||
* @brief Blocks until the simulation advances to the next tick.
|
||||
*
|
||||
* Called by runner threads to wait for the global clock to advance.
|
||||
*
|
||||
* @param last_gen The generation ID of the last processed tick.
|
||||
* @param stop_token Atomic flag to signal termination while waiting.
|
||||
* @param timeout_ms Maximum time to wait for the tick.
|
||||
* @return The new generation ID, or `last_gen` on timeout/stop.
|
||||
*/
|
||||
uint64_t wait_for_tick(
|
||||
uint64_t last_gen, const std::atomic<bool> &stop_token, uint64_t timeout_ms = 10);
|
||||
|
||||
/**
|
||||
* @brief Signals that a runner has completed its work for the current tick.
|
||||
*
|
||||
* @param next_delay_ms The requested delay until the next tick (from `tox_iteration_interval`).
|
||||
*/
|
||||
void tick_complete(uint32_t next_delay_ms = kDefaultTickIntervalMs);
|
||||
|
||||
// Global Access
|
||||
FakeClock &clock() { return *clock_; }
|
||||
const FakeClock &clock() const { return *clock_; }
|
||||
NetworkUniverse &net() { return *net_; }
|
||||
const NetworkUniverse &net() const { return *net_; }
|
||||
|
||||
// Node Factory
|
||||
std::unique_ptr<SimulatedNode> create_node();
|
||||
@@ -52,7 +163,23 @@ public:
|
||||
private:
|
||||
std::unique_ptr<FakeClock> clock_;
|
||||
std::unique_ptr<NetworkUniverse> net_;
|
||||
LogFilter log_filter_;
|
||||
uint32_t node_count_ = 0;
|
||||
|
||||
// Barrier State
|
||||
std::mutex barrier_mutex_;
|
||||
std::condition_variable barrier_cv_;
|
||||
uint64_t current_generation_ = 0;
|
||||
int registered_runners_ = 0;
|
||||
std::atomic<int> active_runners_{0};
|
||||
std::atomic<uint32_t> next_step_min_{kDefaultTickIntervalMs};
|
||||
|
||||
struct TickListener {
|
||||
TickListenerId id;
|
||||
std::function<void(uint64_t)> callback;
|
||||
};
|
||||
std::vector<TickListener> tick_listeners_;
|
||||
TickListenerId next_listener_id_ = 0;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -79,19 +206,16 @@ public:
|
||||
// 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); }
|
||||
void operator()(Tox *_Nonnull t) const { tox_kill(t); }
|
||||
};
|
||||
using ToxPtr = std::unique_ptr<Tox, ToxDeleter>;
|
||||
|
||||
ToxPtr create_tox(const Tox_Options *options = nullptr);
|
||||
ToxPtr create_tox(const Tox_Options *_Nullable 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(); }
|
||||
Simulation &simulation() { return sim_; }
|
||||
|
||||
// For fuzzing compatibility (exposes first bound UDP socket as "endpoint")
|
||||
FakeUdpSocket *get_primary_socket();
|
||||
FakeUdpSocket *_Nullable get_primary_socket();
|
||||
|
||||
private:
|
||||
Simulation &sim_;
|
||||
@@ -102,8 +226,8 @@ private:
|
||||
// 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 Random c_random;
|
||||
struct Memory c_memory;
|
||||
struct IP ip;
|
||||
};
|
||||
|
||||
|
||||
@@ -5,21 +5,25 @@
|
||||
#ifndef C_TOXCORE_TESTING_SUPPORT_TOX_NETWORK_H
|
||||
#define C_TOXCORE_TESTING_SUPPORT_TOX_NETWORK_H
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
#include "simulation.hh"
|
||||
#include "tox_runner.hh"
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
struct ConnectedFriend {
|
||||
std::unique_ptr<SimulatedNode> node;
|
||||
SimulatedNode::ToxPtr tox;
|
||||
std::unique_ptr<ToxRunner> runner;
|
||||
uint32_t friend_number;
|
||||
|
||||
ConnectedFriend(std::unique_ptr<SimulatedNode> node_in, SimulatedNode::ToxPtr tox_in,
|
||||
ConnectedFriend(std::unique_ptr<SimulatedNode> node_in, std::unique_ptr<ToxRunner> runner_in,
|
||||
uint32_t friend_number_in)
|
||||
: node(std::move(node_in))
|
||||
, tox(std::move(tox_in))
|
||||
, runner(std::move(runner_in))
|
||||
, friend_number(friend_number_in)
|
||||
{
|
||||
}
|
||||
@@ -43,8 +47,9 @@ struct ConnectedFriend {
|
||||
* @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);
|
||||
std::vector<ConnectedFriend> setup_connected_friends(Simulation &sim, Tox *_Nonnull main_tox,
|
||||
SimulatedNode &main_node, int num_friends, const Tox_Options *_Nullable options = nullptr,
|
||||
bool verbose = false);
|
||||
|
||||
/**
|
||||
* @brief Connects two existing Tox instances as friends.
|
||||
@@ -59,8 +64,8 @@ std::vector<ConnectedFriend> setup_connected_friends(Simulation &sim, Tox *main_
|
||||
* @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);
|
||||
bool connect_friends(Simulation &sim, SimulatedNode &node1, Tox *_Nonnull tox1,
|
||||
SimulatedNode &node2, Tox *_Nonnull tox2);
|
||||
|
||||
/**
|
||||
* @brief Sets up a group and has all friends join it.
|
||||
@@ -75,7 +80,7 @@ bool connect_friends(
|
||||
* @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);
|
||||
Simulation &sim, Tox *_Nonnull main_tox, const std::vector<ConnectedFriend> &friends);
|
||||
|
||||
} // namespace tox::test
|
||||
|
||||
|
||||
137
testing/support/public/tox_runner.hh
Normal file
137
testing/support/public/tox_runner.hh
Normal file
@@ -0,0 +1,137 @@
|
||||
/* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
* Copyright © 2026 The TokTok team.
|
||||
*/
|
||||
|
||||
#ifndef C_TOXCORE_TESTING_SUPPORT_TOX_RUNNER_H
|
||||
#define C_TOXCORE_TESTING_SUPPORT_TOX_RUNNER_H
|
||||
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "../../../toxcore/attributes.h"
|
||||
#include "../../../toxcore/tox_events.h"
|
||||
#include "mpsc_queue.hh"
|
||||
#include "simulation.hh"
|
||||
|
||||
namespace tox::test {
|
||||
|
||||
class ToxRunner {
|
||||
public:
|
||||
explicit ToxRunner(SimulatedNode &node, const Tox_Options *_Nullable options = nullptr);
|
||||
~ToxRunner();
|
||||
|
||||
ToxRunner(const ToxRunner &) = delete;
|
||||
ToxRunner &operator=(const ToxRunner &) = delete;
|
||||
|
||||
struct ToxEventsDeleter {
|
||||
void operator()(Tox_Events *_Nonnull e) const { tox_events_free(e); }
|
||||
};
|
||||
using ToxEventsPtr = std::unique_ptr<Tox_Events, ToxEventsDeleter>;
|
||||
|
||||
/**
|
||||
* @brief Schedules a task for execution on the runner's thread.
|
||||
*
|
||||
* This method is thread-safe and non-blocking. The task is queued and will
|
||||
* be executed during the runner's event loop cycle.
|
||||
*
|
||||
* @param task The function to execute, taking a raw Tox pointer.
|
||||
*/
|
||||
void execute(std::function<void(Tox *_Nonnull)> task);
|
||||
|
||||
/**
|
||||
* @brief Executes a task on the runner's thread and waits for the result.
|
||||
*
|
||||
* This method blocks the calling thread until the task has been executed
|
||||
* by the runner. It automatically handles return value propagation and
|
||||
* exception safety (though exceptions are not currently propagated).
|
||||
*
|
||||
* @tparam Func The type of the callable object.
|
||||
* @param func The callable to execute, taking a raw Tox pointer.
|
||||
* @return The result of the callable execution.
|
||||
*/
|
||||
template <typename Func>
|
||||
auto invoke(Func &&func) -> std::invoke_result_t<Func, Tox *_Nonnull>
|
||||
{
|
||||
using R = std::invoke_result_t<Func, Tox *_Nonnull>;
|
||||
auto promise = std::make_shared<std::promise<R>>();
|
||||
auto future = promise->get_future();
|
||||
|
||||
execute([p = promise, f = std::forward<Func>(func)](Tox *_Nonnull tox) {
|
||||
if constexpr (std::is_void_v<R>) {
|
||||
f(tox);
|
||||
p->set_value();
|
||||
} else {
|
||||
p->set_value(f(tox));
|
||||
}
|
||||
});
|
||||
|
||||
return future.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Retrieves all accumulated Tox event batches.
|
||||
*
|
||||
* Returns a vector of unique pointers to Tox_Events structures that have
|
||||
* been collected by the runner since the last call. Ownership is transferred
|
||||
* to the caller. This method is thread-safe.
|
||||
*
|
||||
* @return A vector of Tox_Events pointers.
|
||||
*/
|
||||
std::vector<ToxEventsPtr> poll_events();
|
||||
|
||||
/**
|
||||
* @brief Accesses the underlying Tox instance directly.
|
||||
*
|
||||
* @warning Thread-Safety Violation: This method provides unsafe access to the
|
||||
* Tox instance. It should ONLY be used when the runner thread is known to be
|
||||
* idle (e.g., before the loop starts) or for accessing constant/read-only properties.
|
||||
* For all other operations, use `execute` or `invoke`.
|
||||
*/
|
||||
Tox *_Nullable unsafe_tox() { return tox_.get(); }
|
||||
|
||||
/**
|
||||
* @brief Temporarily stops the runner from participating in the simulation.
|
||||
*
|
||||
* Unregisters the runner and its tick listener from the simulation.
|
||||
* While paused, the runner will not call tox_iterate.
|
||||
*/
|
||||
void pause();
|
||||
|
||||
/**
|
||||
* @brief Resumes the runner's participation in the simulation.
|
||||
*/
|
||||
void resume();
|
||||
|
||||
/**
|
||||
* @brief Returns true if the runner is currently active.
|
||||
*/
|
||||
bool is_active() const { return active_; }
|
||||
|
||||
private:
|
||||
void loop();
|
||||
|
||||
SimulatedNode::ToxPtr tox_;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> active_{true};
|
||||
|
||||
struct Message {
|
||||
enum Type { Task, Tick, Stop } type;
|
||||
std::function<void(Tox *_Nonnull)> task;
|
||||
uint64_t generation = 0;
|
||||
};
|
||||
|
||||
MpscQueue<Message> queue_;
|
||||
MpscQueue<ToxEventsPtr> events_queue_;
|
||||
|
||||
Simulation::TickListenerId tick_listener_id_ = -1;
|
||||
SimulatedNode &node_;
|
||||
};
|
||||
|
||||
} // namespace tox::test
|
||||
|
||||
#endif // C_TOXCORE_TESTING_SUPPORT_TOX_RUNNER_H
|
||||
Reference in New Issue
Block a user