1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <consensus/consensus.h>
6 #include <net.h>
7 #include <net_processing.h>
8 #include <protocol.h>
9 #include <test/fuzz/FuzzedDataProvider.h>
10 #include <test/fuzz/fuzz.h>
11 #include <test/fuzz/util.h>
12 #include <test/util/mining.h>
13 #include <test/util/net.h>
14 #include <test/util/setup_common.h>
15 #include <test/util/validation.h>
16 #include <util/memory.h>
17 #include <validation.h>
18 #include <validationinterface.h>
19 
20 const TestingSetup* g_setup;
21 
initialize()22 void initialize()
23 {
24     static TestingSetup setup{
25         CBaseChainParams::REGTEST,
26         {
27             "-nodebuglogfile",
28         },
29     };
30     g_setup = &setup;
31 
32     for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
33         MineBlock(g_setup->m_node, CScript() << OP_TRUE);
34     }
35     SyncWithValidationInterfaceQueue();
36 }
37 
test_one_input(const std::vector<uint8_t> & buffer)38 void test_one_input(const std::vector<uint8_t>& buffer)
39 {
40     FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
41 
42     ConnmanTestMsg& connman = *(ConnmanTestMsg*)g_setup->m_node.connman.get();
43     TestChainState& chainstate = *(TestChainState*)&g_setup->m_node.chainman->ActiveChainstate();
44     chainstate.ResetIbd();
45     std::vector<CNode*> peers;
46     bool jump_out_of_ibd{false};
47 
48     const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
49     for (int i = 0; i < num_peers_to_add; ++i) {
50         const ServiceFlags service_flags = ServiceFlags(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
51         const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH});
52         peers.push_back(MakeUnique<CNode>(i, service_flags, 0, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, conn_type).release());
53         CNode& p2p_node = *peers.back();
54 
55         p2p_node.fSuccessfullyConnected = true;
56         p2p_node.fPauseSend = false;
57         p2p_node.nVersion = PROTOCOL_VERSION;
58         p2p_node.SetCommonVersion(PROTOCOL_VERSION);
59         g_setup->m_node.peerman->InitializeNode(&p2p_node);
60 
61         connman.AddTestNode(p2p_node);
62     }
63 
64     while (fuzzed_data_provider.ConsumeBool()) {
65         if (!jump_out_of_ibd) jump_out_of_ibd = fuzzed_data_provider.ConsumeBool();
66         if (jump_out_of_ibd && chainstate.IsInitialBlockDownload()) chainstate.JumpOutOfIbd();
67         const std::string random_message_type{fuzzed_data_provider.ConsumeBytesAsString(CMessageHeader::COMMAND_SIZE).c_str()};
68 
69         CSerializedNetMsg net_msg;
70         net_msg.m_type = random_message_type;
71         net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
72 
73         CNode& random_node = *peers.at(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, peers.size() - 1));
74 
75         (void)connman.ReceiveMsgFrom(random_node, net_msg);
76         random_node.fPauseSend = false;
77 
78         try {
79             connman.ProcessMessagesOnce(random_node);
80         } catch (const std::ios_base::failure&) {
81         }
82     }
83     SyncWithValidationInterfaceQueue();
84     LOCK2(::cs_main, g_cs_orphans); // See init.cpp for rationale for implicit locking order requirement
85     g_setup->m_node.connman->StopNodes();
86 }
87