|
| 1 | +// Copyright (c) 2019-2021 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 <crypto/chacha_poly_aead.h> |
| 6 | +#include <key.h> |
| 7 | +#include <net.h> |
| 8 | +#include <netmessagemaker.h> |
| 9 | +#include <test/fuzz/fuzz.h> |
| 10 | + |
| 11 | +#include <cassert> |
| 12 | + |
| 13 | +FUZZ_TARGET(p2p_v2_transport_serialization) |
| 14 | +{ |
| 15 | + const CPrivKey k1(32, 0); |
| 16 | + const CPrivKey k2(32, 0); |
| 17 | + |
| 18 | + // Construct deserializer, with a dummy NodeId |
| 19 | + V2TransportDeserializer deserializer{(NodeId)0, k1, k2}; |
| 20 | + V2TransportSerializer serializer{k1, k2}; |
| 21 | + |
| 22 | + while (buffer.size() > 0) { |
| 23 | + const int handled = deserializer.Read(buffer); |
| 24 | + if (handled < 0) { |
| 25 | + break; |
| 26 | + } |
| 27 | + if (deserializer.Complete()) { |
| 28 | + const std::chrono::microseconds m_time{std::numeric_limits<int64_t>::max()}; |
| 29 | + bool reject_message{true}; |
| 30 | + bool disconnect{true}; |
| 31 | + CNetMessage result{deserializer.GetMessage(m_time, reject_message, disconnect)}; |
| 32 | + if (!reject_message) { |
| 33 | + assert(result.m_command.size() <= CMessageHeader::COMMAND_SIZE); |
| 34 | + assert(result.m_raw_message_size <= buffer.size()); |
| 35 | + assert(result.m_raw_message_size == CHACHA20_POLY1305_AEAD_AAD_LEN + result.m_message_size + CHACHA20_POLY1305_AEAD_TAG_LEN); |
| 36 | + assert(result.m_time == m_time); |
| 37 | + |
| 38 | + std::vector<unsigned char> header; |
| 39 | + auto msg = CNetMsgMaker{result.m_recv.GetVersion()}.Make(result.m_command, MakeUCharSpan(result.m_recv)); |
| 40 | + serializer.prepareForTransport(msg, header); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | +} |
0 commit comments