Bitcoin ABC 0.32.4
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 versionPayload << PROTOCOL_VERSION << serviceFlags << now << addrTo
44 << addrFrom << nonce << user_agent << start_height;
45
46 CMessageHeader versionhdr(Params().NetMagic(), NetMsgType::VERSION,
47 versionPayload.size());
48 uint256 hash = Hash(versionPayload);
49 memcpy(versionhdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
50
52 expectedVersion << versionhdr << versionPayload;
53
55 serviceFlags, now, addrTo, addrFrom, nonce, user_agent,
56 start_height);
57}
58
59BOOST_AUTO_TEST_CASE(header_empty_payload_message_writer_test) {
61 CMessageHeader verackHeader(Params().NetMagic(), NetMsgType::VERACK, 0);
63 // This is an empty payload, but is still necessary for the checksum
64 std::vector<uint8_t> payload;
65 uint256 hash = Hash(payload);
66 memcpy(verackHeader.pchChecksum, hash.begin(),
68 expectedVerack << verackHeader;
69
70 CheckMessage(expectedVerack, NetMsgType::VERACK);
71}
72
73BOOST_AUTO_TEST_CASE(write_getheaders_message_test) {
76 BlockHash bhash(uint256S(
77 "0000000099f5509b5f36b1926bcf82b21d936ebeadee811030dfbbb7fae915d7"));
78 std::vector<BlockHash> vlocator(1, bhash);
79 CBlockLocator locatorhash(std::move(vlocator));
80 payload << locatorhash << uint256();
81 uint256 hash = Hash(payload);
82
83 CMessageHeader msgHeader(Params().NetMagic(), NetMsgType::GETHEADERS,
84 payload.size());
85 memcpy(msgHeader.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE);
86
88 expectedMsg << msgHeader << payload;
89
90 CheckMessage(expectedMsg, NetMsgType::GETHEADERS, locatorhash, uint256());
91}
92
93BOOST_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:545
size_type size() const
Definition: streams.h:206
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:154
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:109
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