Files
tomato-testing/testing/support/doubles/fake_sockets.hh
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

166 lines
4.7 KiB
C++

#ifndef C_TOXCORE_TESTING_SUPPORT_DOUBLES_FAKE_SOCKETS_H
#define C_TOXCORE_TESTING_SUPPORT_DOUBLES_FAKE_SOCKETS_H
#include <atomic>
#include <cstdint>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <vector>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <winsock2.h>
#else
#include <sys/socket.h>
#endif
#include "../../../toxcore/network.h"
namespace tox::test {
class NetworkUniverse;
struct Packet;
/**
* @brief Abstract base class for all fake sockets.
*/
class FakeSocket {
public:
FakeSocket(NetworkUniverse &universe, int type);
virtual ~FakeSocket();
virtual int bind(const IP_Port *addr) = 0;
virtual int connect(const IP_Port *addr) = 0;
virtual int listen(int backlog) = 0;
virtual std::unique_ptr<FakeSocket> accept(IP_Port *addr) = 0;
virtual int send(const uint8_t *buf, size_t len) = 0;
virtual int recv(uint8_t *buf, size_t len) = 0;
virtual size_t recv_buffer_size() { return 0; }
virtual int sendto(const uint8_t *buf, size_t len, const IP_Port *addr) = 0;
virtual int recvfrom(uint8_t *buf, size_t len, IP_Port *addr) = 0;
virtual int getsockopt(int level, int optname, void *optval, size_t *optlen);
virtual int setsockopt(int level, int optname, const void *optval, size_t optlen);
virtual int socket_nonblock(bool nonblock);
virtual int close();
bool is_nonblocking() const { return nonblocking_; }
int type() const { return type_; }
uint16_t local_port() const { return local_port_; }
const IP &ip_address() const { return ip_; }
void set_ip(const IP &ip) { ip_ = ip; }
protected:
NetworkUniverse &universe_;
int type_;
bool nonblocking_ = false;
uint16_t local_port_ = 0;
IP ip_;
std::mutex mutex_; // Use regular mutex (recursive_mutex not fully supported in MinGW)
};
/**
* @brief Implements UDP logic.
*/
class FakeUdpSocket : public FakeSocket {
public:
explicit FakeUdpSocket(NetworkUniverse &universe);
~FakeUdpSocket() override;
int bind(const IP_Port *addr) override;
int connect(const IP_Port *addr) override;
int listen(int backlog) override;
std::unique_ptr<FakeSocket> accept(IP_Port *addr) override;
int close() override;
int send(const uint8_t *buf, size_t len) override;
int recv(uint8_t *buf, size_t len) override;
int sendto(const uint8_t *buf, size_t len, const IP_Port *addr) override;
int recvfrom(uint8_t *buf, size_t len, IP_Port *addr) override;
// Called by Universe to deliver a packet
void push_packet(std::vector<uint8_t> data, IP_Port from);
using PacketSource = std::function<bool(std::vector<uint8_t> &data, IP_Port &from)>;
void set_packet_source(PacketSource source);
using RecvObserver = std::function<void(const std::vector<uint8_t> &data, const IP_Port &from)>;
void set_recv_observer(RecvObserver observer);
private:
void close_impl();
struct PendingPacket {
std::vector<uint8_t> data;
IP_Port from;
};
std::deque<PendingPacket> recv_queue_;
PacketSource packet_source_;
RecvObserver recv_observer_;
};
/**
* @brief Implements TCP logic (Simplified for simulation).
*
* Supports:
* - 3-way handshake simulation (instant or delayed)
* - Stream buffering
* - Connection states
*/
class FakeTcpSocket : public FakeSocket {
public:
enum State { CLOSED, LISTEN, SYN_SENT, SYN_RECEIVED, ESTABLISHED, CLOSE_WAIT };
explicit FakeTcpSocket(NetworkUniverse &universe);
~FakeTcpSocket() override;
int bind(const IP_Port *addr) override;
int connect(const IP_Port *addr) override;
int listen(int backlog) override;
std::unique_ptr<FakeSocket> accept(IP_Port *addr) override;
int close() override;
int send(const uint8_t *buf, size_t len) override;
int recv(uint8_t *buf, size_t len) override;
size_t recv_buffer_size() override;
int sendto(const uint8_t *buf, size_t len, const IP_Port *addr) override;
int recvfrom(uint8_t *buf, size_t len, IP_Port *addr) override;
// Internal events
void handle_packet(const Packet &p);
State state() const { return state_; }
const IP_Port &remote_addr() const { return remote_addr_; }
// Factory for accepting connections
static std::unique_ptr<FakeTcpSocket> create_connected(
NetworkUniverse &universe, const IP_Port &remote, uint16_t local_port);
private:
void close_impl();
State state_ = CLOSED;
IP_Port remote_addr_;
std::deque<uint8_t> recv_buffer_;
std::deque<std::unique_ptr<FakeTcpSocket>> pending_connections_;
int backlog_ = 0;
uint32_t next_seq_ = 0;
uint32_t last_ack_ = 0;
};
} // namespace tox::test
#endif // C_TOXCORE_TESTING_SUPPORT_DOUBLES_FAKE_SOCKETS_H