Changeset View
Changeset View
Standalone View
Standalone View
src/test/fuzz/process_message.cpp
Show All 15 Lines | |||||
#include <version.h> | #include <version.h> | ||||
#include <test/fuzz/FuzzedDataProvider.h> | #include <test/fuzz/FuzzedDataProvider.h> | ||||
#include <test/fuzz/fuzz.h> | #include <test/fuzz/fuzz.h> | ||||
#include <test/util/mining.h> | #include <test/util/mining.h> | ||||
#include <test/util/net.h> | #include <test/util/net.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <algorithm> | |||||
#include <atomic> | #include <atomic> | ||||
#include <cassert> | #include <cassert> | ||||
#include <chrono> | #include <chrono> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <iosfwd> | #include <iosfwd> | ||||
#include <iostream> | #include <iostream> | ||||
#include <map> | |||||
#include <memory> | #include <memory> | ||||
#include <set> | |||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
namespace { | namespace { | ||||
#ifdef MESSAGE_TYPE | #ifdef MESSAGE_TYPE | ||||
#define TO_STRING_(s) #s | #define TO_STRING_(s) #s | ||||
#define TO_STRING(s) TO_STRING_(s) | #define TO_STRING(s) TO_STRING_(s) | ||||
const std::string LIMIT_TO_MESSAGE_TYPE{TO_STRING(MESSAGE_TYPE)}; | const std::string LIMIT_TO_MESSAGE_TYPE{TO_STRING(MESSAGE_TYPE)}; | ||||
#else | #else | ||||
const std::string LIMIT_TO_MESSAGE_TYPE; | const std::string LIMIT_TO_MESSAGE_TYPE; | ||||
#endif | #endif | ||||
const std::map<std::string, std::set<std::string>> | |||||
EXPECTED_DESERIALIZATION_EXCEPTIONS = { | |||||
{"CDataStream::read(): end of data: iostream error", | |||||
{"addr", "block", "blocktxn", "cmpctblock", "feefilter", "filteradd", | |||||
"filterload", "getblocks", "getblocktxn", "getdata", "getheaders", | |||||
"headers", "inv", "notfound", "ping", "sendcmpct", "tx"}}, | |||||
{"CompactSize exceeds limit of type: iostream error", {"cmpctblock"}}, | |||||
{"differential value overflow: iostream error", {"getblocktxn"}}, | |||||
{"index overflowed 16 bits: iostream error", {"getblocktxn"}}, | |||||
{"index overflowed 16-bits: iostream error", {"cmpctblock"}}, | |||||
{"indexes overflowed 16 bits: iostream error", {"getblocktxn"}}, | |||||
{"non-canonical ReadCompactSize(): iostream error", | |||||
{"addr", "block", "blocktxn", "cmpctblock", "filteradd", "filterload", | |||||
"getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", | |||||
"notfound", "tx"}}, | |||||
{"ReadCompactSize(): size too large: iostream error", | |||||
{"addr", "block", "blocktxn", "cmpctblock", "filteradd", "filterload", | |||||
"getblocks", "getblocktxn", "getdata", "getheaders", "headers", "inv", | |||||
"notfound", "tx"}}, | |||||
{"Superfluous witness record: iostream error", | |||||
{"block", "blocktxn", "cmpctblock", "tx"}}, | |||||
{"Unknown transaction optional data: iostream error", | |||||
{"block", "blocktxn", "cmpctblock", "tx"}}, | |||||
}; | |||||
const TestingSetup *g_setup; | const TestingSetup *g_setup; | ||||
} // namespace | } // namespace | ||||
void initialize() { | void initialize() { | ||||
static TestingSetup setup{ | static TestingSetup setup{ | ||||
CBaseChainParams::REGTEST, | CBaseChainParams::REGTEST, | ||||
{ | { | ||||
"-nodebuglogfile", | "-nodebuglogfile", | ||||
Show All 31 Lines | void test_one_input(const std::vector<uint8_t> &buffer) { | ||||
p2p_node.nVersion = PROTOCOL_VERSION; | p2p_node.nVersion = PROTOCOL_VERSION; | ||||
p2p_node.SetSendVersion(PROTOCOL_VERSION); | p2p_node.SetSendVersion(PROTOCOL_VERSION); | ||||
connman.AddTestNode(p2p_node); | connman.AddTestNode(p2p_node); | ||||
g_setup->m_node.peer_logic->InitializeNode(config, &p2p_node); | g_setup->m_node.peer_logic->InitializeNode(config, &p2p_node); | ||||
try { | try { | ||||
g_setup->m_node.peer_logic->ProcessMessage( | g_setup->m_node.peer_logic->ProcessMessage( | ||||
config, p2p_node, random_message_type, random_bytes_data_stream, | config, p2p_node, random_message_type, random_bytes_data_stream, | ||||
GetTimeMillis(), std::atomic<bool>{false}); | GetTimeMillis(), std::atomic<bool>{false}); | ||||
} catch (const std::ios_base::failure &e) { | } catch (const std::ios_base::failure &) { | ||||
const std::string exception_message{e.what()}; | |||||
const auto p = | |||||
EXPECTED_DESERIALIZATION_EXCEPTIONS.find(exception_message); | |||||
if (p == EXPECTED_DESERIALIZATION_EXCEPTIONS.cend() || | |||||
p->second.count(random_message_type) == 0) { | |||||
std::cout << "Unexpected exception when processing message type \"" | |||||
<< random_message_type << "\": " << exception_message | |||||
<< std::endl; | |||||
assert(false); | |||||
} | |||||
} | } | ||||
SyncWithValidationInterfaceQueue(); | SyncWithValidationInterfaceQueue(); | ||||
// See init.cpp for rationale for implicit locking order requirement | // See init.cpp for rationale for implicit locking order requirement | ||||
LOCK2(::cs_main, g_cs_orphans); | LOCK2(::cs_main, g_cs_orphans); | ||||
g_setup->m_node.connman->StopNodes(); | g_setup->m_node.connman->StopNodes(); | ||||
} | } |