mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-06 03:02:37 +02:00
fuzz: Add process_messages harness
This commit is contained in:
39
src/test/util/net.cpp
Normal file
39
src/test/util/net.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <test/util/net.h>
|
||||
|
||||
#include <chainparams.h>
|
||||
#include <net.h>
|
||||
|
||||
void ConnmanTestMsg::NodeReceiveMsgBytes(CNode& node, const char* pch, unsigned int nBytes, bool& complete) const
|
||||
{
|
||||
assert(node.ReceiveMsgBytes(pch, nBytes, complete));
|
||||
if (complete) {
|
||||
size_t nSizeAdded = 0;
|
||||
auto it(node.vRecvMsg.begin());
|
||||
for (; it != node.vRecvMsg.end(); ++it) {
|
||||
// vRecvMsg contains only completed CNetMessage
|
||||
// the single possible partially deserialized message are held by TransportDeserializer
|
||||
nSizeAdded += it->m_raw_message_size;
|
||||
}
|
||||
{
|
||||
LOCK(node.cs_vProcessMsg);
|
||||
node.vProcessMsg.splice(node.vProcessMsg.end(), node.vRecvMsg, node.vRecvMsg.begin(), it);
|
||||
node.nProcessQueueSize += nSizeAdded;
|
||||
node.fPauseRecv = node.nProcessQueueSize > nReceiveFloodSize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ConnmanTestMsg::ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const
|
||||
{
|
||||
std::vector<unsigned char> ser_msg_header;
|
||||
node.m_serializer->prepareForTransport(ser_msg, ser_msg_header);
|
||||
|
||||
bool complete;
|
||||
NodeReceiveMsgBytes(node, (const char*)ser_msg_header.data(), ser_msg_header.size(), complete);
|
||||
NodeReceiveMsgBytes(node, (const char*)ser_msg.data.data(), ser_msg.data.size(), complete);
|
||||
return complete;
|
||||
}
|
||||
33
src/test/util/net.h
Normal file
33
src/test/util/net.h
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_TEST_UTIL_NET_H
|
||||
#define BITCOIN_TEST_UTIL_NET_H
|
||||
|
||||
#include <net.h>
|
||||
|
||||
struct ConnmanTestMsg : public CConnman {
|
||||
using CConnman::CConnman;
|
||||
void AddTestNode(CNode& node)
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
vNodes.push_back(&node);
|
||||
}
|
||||
void ClearTestNodes()
|
||||
{
|
||||
LOCK(cs_vNodes);
|
||||
for (CNode* node : vNodes) {
|
||||
delete node;
|
||||
}
|
||||
vNodes.clear();
|
||||
}
|
||||
|
||||
void ProcessMessagesOnce(CNode& node) { m_msgproc->ProcessMessages(&node, flagInterruptMsgProc); }
|
||||
|
||||
void NodeReceiveMsgBytes(CNode& node, const char* pch, unsigned int nBytes, bool& complete) const;
|
||||
|
||||
bool ReceiveMsgFrom(CNode& node, CSerializedNetMsg& ser_msg) const;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_TEST_UTIL_NET_H
|
||||
@@ -139,6 +139,11 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
|
||||
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
||||
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
|
||||
m_node.peer_logic = MakeUnique<PeerLogicValidation>(m_node.connman.get(), m_node.banman.get(), *m_node.scheduler, *m_node.mempool);
|
||||
{
|
||||
CConnman::Options options;
|
||||
options.m_msgproc = m_node.peer_logic.get();
|
||||
m_node.connman->Init(options);
|
||||
}
|
||||
}
|
||||
|
||||
TestingSetup::~TestingSetup()
|
||||
|
||||
Reference in New Issue
Block a user