Bitcoin Core  22.99.0
P2P Digital Currency
serialize_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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(char), GetSerializeSize(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(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("\xfd\x00\x00", 3);
190  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
191 
192  // 0xfc encoded with three bytes:
193  ss.write("\xfd\xfc\x00", 3);
194  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
195 
196  // 0xfd encoded with three bytes is OK:
197  ss.write("\xfd\xfd\x00", 3);
198  n = ReadCompactSize(ss);
199  BOOST_CHECK(n == 0xfd);
200 
201  // zero encoded with five bytes:
202  ss.write("\xfe\x00\x00\x00\x00", 5);
203  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
204 
205  // 0xffff encoded with five bytes:
206  ss.write("\xfe\xff\xff\x00\x00", 5);
207  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
208 
209  // zero encoded with nine bytes:
210  ss.write("\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9);
211  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
212 
213  // 0x01ffffff encoded with nine bytes:
214  ss.write("\xff\xff\xff\xff\x01\x00\x00\x00\x00", 9);
215  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
216 }
217 
218 BOOST_AUTO_TEST_CASE(insert_delete)
219 {
220  // Test inserting/deleting bytes.
221  CDataStream ss(SER_DISK, 0);
222  BOOST_CHECK_EQUAL(ss.size(), 0U);
223 
224  ss.write("\x00\x01\x02\xff", 4);
225  BOOST_CHECK_EQUAL(ss.size(), 4U);
226 
227  char c = (char)11;
228 
229  // Inserting at beginning/end/middle:
230  ss.insert(ss.begin(), c);
231  BOOST_CHECK_EQUAL(ss.size(), 5U);
232  BOOST_CHECK_EQUAL(ss[0], c);
233  BOOST_CHECK_EQUAL(ss[1], 0);
234 
235  ss.insert(ss.end(), c);
236  BOOST_CHECK_EQUAL(ss.size(), 6U);
237  BOOST_CHECK_EQUAL(ss[4], 0xff);
238  BOOST_CHECK_EQUAL(ss[5], c);
239 
240  ss.insert(ss.begin()+2, c);
241  BOOST_CHECK_EQUAL(ss.size(), 7U);
242  BOOST_CHECK_EQUAL(ss[2], c);
243 
244  // Delete at beginning/end/middle
245  ss.erase(ss.begin());
246  BOOST_CHECK_EQUAL(ss.size(), 6U);
247  BOOST_CHECK_EQUAL(ss[0], 0);
248 
249  ss.erase(ss.begin()+ss.size()-1);
250  BOOST_CHECK_EQUAL(ss.size(), 5U);
251  BOOST_CHECK_EQUAL(ss[4], 0xff);
252 
253  ss.erase(ss.begin()+1);
254  BOOST_CHECK_EQUAL(ss.size(), 4U);
255  BOOST_CHECK_EQUAL(ss[0], 0);
256  BOOST_CHECK_EQUAL(ss[1], 1);
257  BOOST_CHECK_EQUAL(ss[2], 2);
258  BOOST_CHECK_EQUAL(ss[3], 0xff);
259 }
260 
261 BOOST_AUTO_TEST_CASE(class_methods)
262 {
263  int intval(100);
264  bool boolval(true);
265  std::string stringval("testing");
266  const uint8_t charstrval[16]{"testing charstr"};
267  CMutableTransaction txval;
268  CTransactionRef tx_ref{MakeTransactionRef(txval)};
269  CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
270  CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
271  CSerializeMethodsTestSingle methodtest3;
272  CSerializeMethodsTestMany methodtest4;
274  BOOST_CHECK(methodtest1 == methodtest2);
275  ss << methodtest1;
276  ss >> methodtest4;
277  ss << methodtest2;
278  ss >> methodtest3;
279  BOOST_CHECK(methodtest1 == methodtest2);
280  BOOST_CHECK(methodtest2 == methodtest3);
281  BOOST_CHECK(methodtest3 == methodtest4);
282 
283  CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, charstrval, txval);
284  ss2 >> methodtest3;
285  BOOST_CHECK(methodtest3 == methodtest4);
286 }
287 
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1080
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:192
CDataStream::size_type
vector_type::size_type size_type
Definition: streams.h:216
CDataStream::insert
iterator insert(iterator it, const uint8_t x)
Definition: streams.h:262
SER_DISK
@ SER_DISK
Definition: serialize.h:139
CSerializeMethodsTestSingle
Definition: serialize_tests.cpp:17
isCanonicalException
static bool isCanonicalException(const std::ios_base::failure &ex)
Definition: serialize_tests.cpp:161
streams.h
CDataStream::begin
const_iterator begin() const
Definition: streams.h:251
setup_common.h
VARINT_MODE
#define VARINT_MODE(obj, mode)
Definition: serialize.h:442
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:387
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:282
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
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(const char *pch, size_t nSize)
Definition: streams.h:402
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:443
WriteCompactSize
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1074
CSerializeMethodsTestMany::SERIALIZE_METHODS
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
Definition: serialize_tests.cpp:56
CDataStream::end
const_iterator end() const
Definition: streams.h:253
CDataStream::erase
iterator erase(iterator it)
Definition: streams.h:295
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
CSerializeMethodsTestSingle::boolval
bool boolval
Definition: serialize_tests.cpp:21
CSerializeMethodsTestSingle::intval
int intval
Definition: serialize_tests.cpp:20
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
CDataStream::size
size_type size() const
Definition: streams.h:255
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:261
CSerializeMethodsTestSingle::stringval
std::string stringval
Definition: serialize_tests.cpp:22
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:204
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
HexStr
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: strencodings.cpp:594
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