mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-04-23 13:29:43 +02:00
Merge #21387: p2p: Refactor sock to add I2P fuzz and unit tests
40316a37cbtest: add I2P test for a runaway SAM proxy (Vasil Dimov)2d8ac77970fuzz: add tests for the I2P Session public interface (Vasil Dimov)9947e44de0i2p: use pointers to Sock to accommodate mocking (Vasil Dimov)82d360b5a8net: change ConnectSocketDirectly() to take a Sock argument (Vasil Dimov)b5861100f8net: add connect() and getsockopt() wrappers to Sock (Vasil Dimov)5a887d49b2fuzz: avoid FuzzedSock::Recv() repeated errors with EAGAIN (Vasil Dimov)3088f83d01fuzz: extend FuzzedSock::Recv() to support MSG_PEEK (Vasil Dimov)9b05c49adefuzz: implement unimplemented FuzzedSock methods (Vasil Dimov) Pull request description: Change the networking code and the I2P code to be fully mockable and use `FuzzedSocket` to fuzz the I2P methods `Listen()`, `Accept()` and `Connect()`. Add a mocked `Sock` implementation that returns a predefined data on reads and use it for a regression unit test for the bug fixed in https://github.com/bitcoin/bitcoin/pull/21407. ACKs for top commit: practicalswift: Tested ACK40316a37cbMarcoFalke: Concept ACK40316a37cbjonatack: re-ACK40316a37cbreviewed `git range-diff01bb3afb23c861d 40316a3` and the new unit test commit, debug built, ran unit tests, ran bitcoind with an I2P service and network operation with seven I2P peers (2 in, 5 out) is looking nominal laanwj: Code review ACK40316a37cbTree-SHA512: 7fc4f129849e16e0c7e16662d9f4d35dfcc369bb31450ee369a2b97bdca95285533bee7787983e881e5a3d248f912afb42b4a2299d5860ace7129b0b19623cc8
This commit is contained in:
@@ -5,7 +5,13 @@
|
||||
#ifndef BITCOIN_TEST_UTIL_NET_H
|
||||
#define BITCOIN_TEST_UTIL_NET_H
|
||||
|
||||
#include <compat.h>
|
||||
#include <net.h>
|
||||
#include <util/sock.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
struct ConnmanTestMsg : public CConnman {
|
||||
using CConnman::CConnman;
|
||||
@@ -61,4 +67,67 @@ constexpr ConnectionType ALL_CONNECTION_TYPES[]{
|
||||
ConnectionType::ADDR_FETCH,
|
||||
};
|
||||
|
||||
/**
|
||||
* A mocked Sock alternative that returns a statically contained data upon read and succeeds
|
||||
* and ignores all writes. The data to be returned is given to the constructor and when it is
|
||||
* exhausted an EOF is returned by further reads.
|
||||
*/
|
||||
class StaticContentsSock : public Sock
|
||||
{
|
||||
public:
|
||||
explicit StaticContentsSock(const std::string& contents) : m_contents{contents}, m_consumed{0}
|
||||
{
|
||||
// Just a dummy number that is not INVALID_SOCKET.
|
||||
static_assert(INVALID_SOCKET != 1000);
|
||||
m_socket = 1000;
|
||||
}
|
||||
|
||||
~StaticContentsSock() override { Reset(); }
|
||||
|
||||
StaticContentsSock& operator=(Sock&& other) override
|
||||
{
|
||||
assert(false && "Move of Sock into MockSock not allowed.");
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Reset() override
|
||||
{
|
||||
m_socket = INVALID_SOCKET;
|
||||
}
|
||||
|
||||
ssize_t Send(const void*, size_t len, int) const override { return len; }
|
||||
|
||||
ssize_t Recv(void* buf, size_t len, int flags) const override
|
||||
{
|
||||
const size_t consume_bytes{std::min(len, m_contents.size() - m_consumed)};
|
||||
std::memcpy(buf, m_contents.data() + m_consumed, consume_bytes);
|
||||
if ((flags & MSG_PEEK) == 0) {
|
||||
m_consumed += consume_bytes;
|
||||
}
|
||||
return consume_bytes;
|
||||
}
|
||||
|
||||
int Connect(const sockaddr*, socklen_t) const override { return 0; }
|
||||
|
||||
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
|
||||
{
|
||||
std::memset(opt_val, 0x0, *opt_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Wait(std::chrono::milliseconds timeout,
|
||||
Event requested,
|
||||
Event* occurred = nullptr) const override
|
||||
{
|
||||
if (occurred != nullptr) {
|
||||
*occurred = requested;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
const std::string m_contents;
|
||||
mutable size_t m_consumed;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_TEST_UTIL_NET_H
|
||||
|
||||
Reference in New Issue
Block a user