1 // Copyright (c) 2019 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 <chainparams.h>
6 #include <net.h>
7 #include <protocol.h>
8 #include <test/fuzz/fuzz.h>
9 
10 #include <cassert>
11 #include <cstdint>
12 #include <limits>
13 #include <vector>
14 
initialize()15 void initialize()
16 {
17     SelectParams(CBaseChainParams::REGTEST);
18 }
19 
test_one_input(const std::vector<uint8_t> & buffer)20 void test_one_input(const std::vector<uint8_t>& buffer)
21 {
22     // Construct deserializer, with a dummy NodeId
23     V1TransportDeserializer deserializer{Params(), (NodeId)0, SER_NETWORK, INIT_PROTO_VERSION};
24     const char* pch = (const char*)buffer.data();
25     size_t n_bytes = buffer.size();
26     while (n_bytes > 0) {
27         const int handled = deserializer.Read(pch, n_bytes);
28         if (handled < 0) {
29             break;
30         }
31         pch += handled;
32         n_bytes -= handled;
33         if (deserializer.Complete()) {
34             const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()};
35             uint32_t out_err_raw_size{0};
36             Optional<CNetMessage> result{deserializer.GetMessage(m_time, out_err_raw_size)};
37             if (result) {
38                 assert(result->m_command.size() <= CMessageHeader::COMMAND_SIZE);
39                 assert(result->m_raw_message_size <= buffer.size());
40                 assert(result->m_raw_message_size == CMessageHeader::HEADER_SIZE + result->m_message_size);
41                 assert(result->m_time == m_time);
42             }
43         }
44     }
45 }
46