Bitcoin ABC 0.32.5
P2P Digital Currency
message_writer_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 The Bitcoin 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 <hash.h>
6#include <primitives/block.h>
7#include <protocol.h>
9#include <streams.h>
10#include <util/chaintype.h>
11#include <version.h>
12
13#include <boost/test/unit_test.hpp>
14
15#include <string>
16#include <vector>
17
18BOOST_AUTO_TEST_SUITE(message_writer_tests)
19
20template <typename... Args>
21static void CheckMessage(CDataStream &expectedMessage, std::string command,
22 Args &&...args) {
24 MessageWriter::WriteMessage(message, command, std::forward<Args>(args)...);
25 BOOST_CHECK_EQUAL(message.size(), expectedMessage.size());
26 for (size_t i = 0; i < message.size(); i++) {
27 BOOST_CHECK_EQUAL(uint8_t(message[i]), uint8_t(expectedMessage[i]));
28 }
29}
30
31BOOST_AUTO_TEST_CASE(simple_header_and_payload_message_writer_test) {
33 int64_t now = GetTime();
34 uint64_t nonce = 0;
35 uint64_t serviceFlags = uint64_t(ServiceFlags(NODE_NETWORK));
36 CService service;
37 CAddress addrTo(service, ServiceFlags(NODE_NETWORK));
38 CAddress addrFrom(service, ServiceFlags(NODE_NETWORK));
39 std::string user_agent = "/Bitcoin ABC:0.0.0(seeder)/";
40 int start_height = 1;
41
43 // The following .reserve() call is a workaround for a spurious
44 // [-Werror=stringop-overflow=] warning in gcc <= 12.2.
45 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100366#c20
46 versionPayload.reserve(200);
47 versionPayload << PROTOCOL_VERSION << serviceFlags << now << addrTo
48 << addrFrom << nonce << user_agent << start_height;
49
50 CMessageHeader versionhdr(Params().NetMagic(), NetMsgType::VERSION,
51 versionPayload.size());
52 uint256 hash = Hash(versionPayload);
53 memcpy(versionhdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
54
56 expectedVersion << versionhdr;
57 expectedVersion.write(versionPayload);
58
60 serviceFlags, now, addrTo, addrFrom, nonce, user_agent,
61 start_height);
62}
63
64BOOST_AUTO_TEST_CASE(header_empty_payload_message_writer_test) {
66 CMessageHeader verackHeader(Params().NetMagic(), NetMsgType::VERACK, 0);
68 // This is an empty payload, but is still necessary for the checksum
69 std::vector<uint8_t> payload;
70 uint256 hash = Hash(payload);
71 memcpy(verackHeader.pchChecksum, hash.begin(),
73 expectedVerack << verackHeader;
74
75 CheckMessage(expectedVerack, NetMsgType::VERACK);
76}
77
78BOOST_AUTO_TEST_CASE(write_getheaders_message_test) {
81 BlockHash bhash(uint256S(
82 "0000000099f5509b5f36b1926bcf82b21d936ebeadee811030dfbbb7fae915d7"));
83 std::vector<BlockHash> vlocator(1, bhash);
84 CBlockLocator locatorhash(std::move(vlocator));
85 payload << locatorhash << uint256();
86 uint256 hash = Hash(payload);
87
88 CMessageHeader msgHeader(Params().NetMagic(), NetMsgType::GETHEADERS,
89 payload.size());
90 memcpy(msgHeader.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
91
93 expectedMsg << msgHeader;
94 expectedMsg.write(payload);
95
96 CheckMessage(expectedMsg, NetMsgType::GETHEADERS, locatorhash, uint256());
97}
98
99BOOST_AUTO_TEST_SUITE_END()
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given BIP70 chain name.
Definition: chainparams.cpp:50
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:21
A CService with information about it as peer.
Definition: protocol.h:443
Message header.
Definition: protocol.h:34
static constexpr size_t CHECKSUM_SIZE
Definition: protocol.h:39
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:73
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:569
void write(Span< const value_type > src)
Definition: streams.h:346
size_type size() const
Definition: streams.h:207
void reserve(size_type n)
Definition: streams.h:212
uint8_t * begin()
Definition: uint256.h:85
256-bit opaque blob.
Definition: uint256.h:129
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
static void CheckMessage(CDataStream &expectedMessage, std::string command, Args &&...args)
BOOST_AUTO_TEST_CASE(simple_header_and_payload_message_writer_test)
static void WriteMessage(CDataStream &stream, std::string command, Args &&...args)
Definition: messagewriter.h:15
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:27
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
ServiceFlags
nServices flags.
Definition: protocol.h:336
@ NODE_NETWORK
Definition: protocol.h:343
@ SER_NETWORK
Definition: serialize.h:155
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:108
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:105
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:143
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11