Bitcoin Core  22.99.0
P2P Digital Currency
serialize_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-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 <hash.h>
6 #include <serialize.h>
7 #include <streams.h>
9 #include <util/strencodings.h>
10 
11 #include <stdint.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
16 
18 {
19 protected:
20  int intval;
21  bool boolval;
22  std::string stringval;
23  char charstrval[16];
25 public:
26  CSerializeMethodsTestSingle() = default;
27  CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin)
28  {
29  memcpy(charstrval, charstrvalin, sizeof(charstrval));
30  }
31 
33  {
34  READWRITE(obj.intval);
35  READWRITE(obj.boolval);
36  READWRITE(obj.stringval);
37  READWRITE(obj.charstrval);
38  READWRITE(obj.txval);
39  }
40 
42  {
43  return intval == rhs.intval && \
44  boolval == rhs.boolval && \
45  stringval == rhs.stringval && \
46  strcmp(charstrval, rhs.charstrval) == 0 && \
47  *txval == *rhs.txval;
48  }
49 };
50 
52 {
53 public:
55 
57  {
58  READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, obj.txval);
59  }
60 };
61 
63 {
64  BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0, 0));
65  BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0), 0));
66  BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0), 0));
67  BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0), 0));
68  BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0), 0));
69  BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0), 0));
70  BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
71  BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
72  BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
73  // Bool is serialized as uint8_t
74  BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0));
75 
76  // Sanity-check GetSerializeSize and c++ type matching
77  BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0, 0), 1U);
78  BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0), 0), 1U);
79  BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0), 0), 1U);
80  BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0), 0), 2U);
81  BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0), 0), 2U);
82  BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0), 0), 4U);
83  BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4U);
84  BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
85  BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
86  BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
87 }
88 
90 {
91  // encode
92 
93  CDataStream ss(SER_DISK, 0);
94  CDataStream::size_type size = 0;
95  for (int i = 0; i < 100000; i++) {
98  BOOST_CHECK(size == ss.size());
99  }
100 
101  for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
102  ss << VARINT(i);
103  size += ::GetSerializeSize(VARINT(i), 0);
104  BOOST_CHECK(size == ss.size());
105  }
106 
107  // decode
108  for (int i = 0; i < 100000; i++) {
109  int j = -1;
111  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
112  }
113 
114  for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
115  uint64_t j = std::numeric_limits<uint64_t>::max();
116  ss >> VARINT(j);
117  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
118  }
119 }
120 
121 BOOST_AUTO_TEST_CASE(varints_bitpatterns)
122 {
123  CDataStream ss(SER_DISK, 0);
126  ss << VARINT_MODE((int8_t)0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
128  ss << VARINT((uint8_t)0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
129  ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
130  ss << VARINT_MODE((int16_t)0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
131  ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
132  ss << VARINT((uint16_t)0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
133  ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
134  ss << VARINT_MODE((int32_t)0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
135  ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
136  ss << VARINT((uint32_t)0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
137  ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
138  ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
139  ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
140 }
141 
143 {
144  CDataStream ss(SER_DISK, 0);
145  std::vector<char>::size_type i, j;
146 
147  for (i = 1; i <= MAX_SIZE; i *= 2)
148  {
149  WriteCompactSize(ss, i-1);
150  WriteCompactSize(ss, i);
151  }
152  for (i = 1; i <= MAX_SIZE; i *= 2)
153  {
154  j = ReadCompactSize(ss);
155  BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
156  j = ReadCompactSize(ss);
157  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
158  }
159 }
160 
161 static bool isCanonicalException(const std::ios_base::failure& ex)
162 {
163  std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
164 
165  // The string returned by what() can be different for different platforms.
166  // Instead of directly comparing the ex.what() with an expected string,
167  // create an instance of exception to see if ex.what() matches
168  // the expected explanatory string returned by the exception instance.
169  return strcmp(expectedException.what(), ex.what()) == 0;
170 }
171 
173 {
174  std::vector<uint8_t> vec1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
175  std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
176 
177  BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
178  BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
179 }
180 
181 BOOST_AUTO_TEST_CASE(noncanonical)
182 {
183  // Write some non-canonical CompactSize encodings, and
184  // make sure an exception is thrown when read back.
185  CDataStream ss(SER_DISK, 0);
186  std::vector<char>::size_type n;
187 
188  // zero encoded with three bytes:
189  ss.write(MakeByteSpan("\xfd\x00\x00").first(3));
190  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
191 
192  // 0xfc encoded with three bytes:
193  ss.write(MakeByteSpan("\xfd\xfc\x00").first(3));
194  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
195 
196  // 0xfd encoded with three bytes is OK:
197  ss.write(MakeByteSpan("\xfd\xfd\x00").first(3));
198  n = ReadCompactSize(ss);
199  BOOST_CHECK(n == 0xfd);
200 
201  // zero encoded with five bytes:
202  ss.write(MakeByteSpan("\xfe\x00\x00\x00\x00").first(5));
203  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
204 
205  // 0xffff encoded with five bytes:
206  ss.write(MakeByteSpan("\xfe\xff\xff\x00\x00").first(5));
207  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
208 
209  // zero encoded with nine bytes:
210  ss.write(MakeByteSpan("\xff\x00\x00\x00\x00\x00\x00\x00\x00").first(9));
211  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
212 
213  // 0x01ffffff encoded with nine bytes:
214  ss.write(MakeByteSpan("\xff\xff\xff\xff\x01\x00\x00\x00\x00").first(9));
215  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
216 }
217 
218 BOOST_AUTO_TEST_CASE(class_methods)
219 {
220  int intval(100);
221  bool boolval(true);
222  std::string stringval("testing");
223  const uint8_t charstrval[16]{"testing charstr"};
224  CMutableTransaction txval;
225  CTransactionRef tx_ref{MakeTransactionRef(txval)};
226  CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
227  CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
228  CSerializeMethodsTestSingle methodtest3;
229  CSerializeMethodsTestMany methodtest4;
231  BOOST_CHECK(methodtest1 == methodtest2);
232  ss << methodtest1;
233  ss >> methodtest4;
234  ss << methodtest2;
235  ss >> methodtest3;
236  BOOST_CHECK(methodtest1 == methodtest2);
237  BOOST_CHECK(methodtest2 == methodtest3);
238  BOOST_CHECK(methodtest3 == methodtest4);
239 
240  CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, charstrval, txval);
241  ss2 >> methodtest3;
242  BOOST_CHECK(methodtest3 == methodtest4);
243 }
244 
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1086
MakeByteSpan
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Definition: span.h:264
SerializeHash
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:193
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:196
SER_DISK
@ SER_DISK
Definition: serialize.h:132
CSerializeMethodsTestSingle
Definition: serialize_tests.cpp:17
isCanonicalException
static bool isCanonicalException(const std::ios_base::failure &ex)
Definition: serialize_tests.cpp:161
streams.h
setup_common.h
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:435
CSerializeMethodsTestSingle::CSerializeMethodsTestSingle
CSerializeMethodsTestSingle()=default
CSerializeMethodsTestSingle::SERIALIZE_METHODS
SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
Definition: serialize_tests.cpp:32
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:407
VarIntMode::NONNEGATIVE_SIGNED
@ NONNEGATIVE_SIGNED
CSerializeMethodsTestMany
Definition: serialize_tests.cpp:51
ReadCompactSize
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:275
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:406
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
CDataStream::write
void write(Span< const value_type > src)
Definition: streams.h:316
strencodings.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(sizes)
Definition: serialize_tests.cpp:62
MAX_SIZE
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
Definition: serialize.h:31
VARINT
#define VARINT(obj)
Definition: serialize.h:436
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1080
CSerializeMethodsTestMany::SERIALIZE_METHODS
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
Definition: serialize_tests.cpp:56
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:83
CSerializeMethodsTestSingle::boolval
bool boolval
Definition: serialize_tests.cpp:21
CSerializeMethodsTestSingle::intval
int intval
Definition: serialize_tests.cpp:20
READWRITE
#define READWRITE(...)
Definition: serialize.h:140
CDataStream::size
size_type size() const
Definition: streams.h:236
CSerializeMethodsTestSingle::operator==
bool operator==(const CSerializeMethodsTestSingle &rhs)
Definition: serialize_tests.cpp:41
CSerializeMethodsTestSingle::txval
CTransactionRef txval
Definition: serialize_tests.cpp:24
CDataStream::clear
void clear()
Definition: streams.h:242
CSerializeMethodsTestSingle::stringval
std::string stringval
Definition: serialize_tests.cpp:22
std
Definition: setup_common.h:33
CSerializeMethodsTestSingle::CSerializeMethodsTestSingle
CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t *charstrvalin, const CTransactionRef &txvalin)
Definition: serialize_tests.cpp:27
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:184
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:364
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:511
CSerializeMethodsTestSingle::charstrval
char charstrval[16]
Definition: serialize_tests.cpp:23
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12