Bitcoin Core  31.0.0
P2P Digital Currency
serialize_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-present 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>
8 #include <test/util/common.h>
10 #include <util/strencodings.h>
11 
12 #include <cstdint>
13 #include <string>
14 
15 #include <boost/test/unit_test.hpp>
16 
18 
19 // For testing move-semantics, declare a version of datastream that can be moved
20 // but is not copyable.
22 {
23 public:
25  UncopyableStream(const UncopyableStream&) = delete;
26  UncopyableStream& operator=(const UncopyableStream&) = delete;
27  UncopyableStream(UncopyableStream&&) noexcept = default;
28  UncopyableStream& operator=(UncopyableStream&&) noexcept = default;
29 };
30 
32 {
33 protected:
34  int intval;
35  bool boolval;
36  std::string stringval;
37  char charstrval[16];
39 public:
40  CSerializeMethodsTestSingle() = default;
41  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)
42  {
43  memcpy(charstrval, charstrvalin, sizeof(charstrval));
44  }
45 
47  {
48  READWRITE(obj.intval);
49  READWRITE(obj.boolval);
50  READWRITE(obj.stringval);
51  READWRITE(obj.charstrval);
52  READWRITE(TX_WITH_WITNESS(obj.txval));
53  }
54 
55  bool operator==(const CSerializeMethodsTestSingle& rhs) const
56  {
57  return intval == rhs.intval &&
58  boolval == rhs.boolval &&
59  stringval == rhs.stringval &&
60  strcmp(charstrval, rhs.charstrval) == 0 &&
61  *txval == *rhs.txval;
62  }
63 };
64 
66 {
67 public:
69 
71  {
72  READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));
73  }
74 };
75 
77 {
78  BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0));
79  BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0)));
80  BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0)));
81  BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0)));
82  BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0)));
83  BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0)));
84  BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0)));
85  BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0)));
86  BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0)));
87  // Bool is serialized as uint8_t
88  BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0)));
89 
90  // Sanity-check GetSerializeSize and c++ type matching
91  BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0), 1U);
92  BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0)), 1U);
93  BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0)), 1U);
94  BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0)), 2U);
95  BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0)), 2U);
96  BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0)), 4U);
97  BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0)), 4U);
98  BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0)), 8U);
99  BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0)), 8U);
100  BOOST_CHECK_EQUAL(GetSerializeSize(bool(0)), 1U);
101  BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}), 1U);
102  BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}), 2U);
103 }
104 
106 {
107  // encode
108 
109  DataStream ss{};
110  DataStream::size_type size = 0;
111  for (int i = 0; i < 100000; i++) {
114  BOOST_CHECK(size == ss.size());
115  }
116 
117  for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
118  ss << VARINT(i);
119  size += ::GetSerializeSize(VARINT(i));
120  BOOST_CHECK(size == ss.size());
121  }
122 
123  // decode
124  for (int i = 0; i < 100000; i++) {
125  int j = -1;
127  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
128  }
129 
130  for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
131  uint64_t j = std::numeric_limits<uint64_t>::max();
132  ss >> VARINT(j);
133  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
134  }
135 }
136 
137 BOOST_AUTO_TEST_CASE(varints_bitpatterns)
138 {
139  DataStream ss{};
140  ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
141  ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
142  ss << VARINT_MODE(int8_t{0x7f}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
143  ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
144  ss << VARINT(uint8_t{0x80}); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
145  ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
146  ss << VARINT_MODE(int16_t{0x1234}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
147  ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
148  ss << VARINT(uint16_t{0xffff}); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
149  ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
150  ss << VARINT_MODE(int32_t{0x123456}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
151  ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
152  ss << VARINT(uint32_t{0x80123456U}); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
153  ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
154  ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
155  ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
156 }
157 
159 {
160  DataStream ss{};
161  std::vector<char>::size_type i, j;
162 
163  for (i = 1; i <= MAX_SIZE; i *= 2)
164  {
165  WriteCompactSize(ss, i-1);
166  WriteCompactSize(ss, i);
167  }
168  for (i = 1; i <= MAX_SIZE; i *= 2)
169  {
170  j = ReadCompactSize(ss);
171  BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
172  j = ReadCompactSize(ss);
173  BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
174  }
175 }
176 
177 static bool isCanonicalException(const std::ios_base::failure& ex)
178 {
179  std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
180 
181  // The string returned by what() can be different for different platforms.
182  // Instead of directly comparing the ex.what() with an expected string,
183  // create an instance of exception to see if ex.what() matches
184  // the expected explanatory string returned by the exception instance.
185  return strcmp(expectedException.what(), ex.what()) == 0;
186 }
187 
189 {
190  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};
191  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};
192 
193  BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
194  BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
195 }
196 
198 {
199  std::array<uint8_t, 32> array1{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};
200  DataStream ds;
201  ds << array1;
202  std::array<uint8_t, 32> array2;
203  ds >> array2;
204  BOOST_CHECK(array1 == array2);
205 }
206 
207 BOOST_AUTO_TEST_CASE(noncanonical)
208 {
209  // Write some non-canonical CompactSize encodings, and
210  // make sure an exception is thrown when read back.
211  DataStream ss{};
212  std::vector<char>::size_type n;
213 
214  // zero encoded with three bytes:
215  ss << std::span{"\xfd\x00\x00"}.first(3);
216  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
217 
218  // 0xfc encoded with three bytes:
219  ss << std::span{"\xfd\xfc\x00"}.first(3);
220  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
221 
222  // 0xfd encoded with three bytes is OK:
223  ss << std::span{"\xfd\xfd\x00"}.first(3);
224  n = ReadCompactSize(ss);
225  BOOST_CHECK(n == 0xfd);
226 
227  // zero encoded with five bytes:
228  ss << std::span{"\xfe\x00\x00\x00\x00"}.first(5);
229  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
230 
231  // 0xffff encoded with five bytes:
232  ss << std::span{"\xfe\xff\xff\x00\x00"}.first(5);
233  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
234 
235  // zero encoded with nine bytes:
236  ss << std::span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
237  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
238 
239  // 0x01ffffff encoded with nine bytes:
240  ss << std::span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
241  BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
242 }
243 
244 BOOST_AUTO_TEST_CASE(class_methods)
245 {
246  int intval(100);
247  bool boolval(true);
248  std::string stringval("testing");
249  const uint8_t charstrval[16]{"testing charstr"};
250  CMutableTransaction txval;
251  CTransactionRef tx_ref{MakeTransactionRef(txval)};
252  CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
253  CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
254  CSerializeMethodsTestSingle methodtest3;
255  CSerializeMethodsTestMany methodtest4;
256  DataStream ss;
257  BOOST_CHECK(methodtest1 == methodtest2);
258  ss << methodtest1;
259  ss >> methodtest4;
260  ss << methodtest2;
261  ss >> methodtest3;
262  BOOST_CHECK(methodtest1 == methodtest2);
263  BOOST_CHECK(methodtest2 == methodtest3);
264  BOOST_CHECK(methodtest3 == methodtest4);
265 
266  DataStream ss2;
267  ss2 << intval << boolval << stringval << charstrval << TX_WITH_WITNESS(txval);
268  ss2 >> methodtest3;
269  BOOST_CHECK(methodtest3 == methodtest4);
270  {
271  DataStream ds;
272  const std::string in{"ab"};
273  ds << std::span{in} << std::byte{'c'};
274  std::array<std::byte, 2> out;
275  std::byte out_3;
276  ds >> std::span{out} >> out_3;
277  BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
278  BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
279  BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
280  }
281 }
282 
283 struct BaseFormat {
284  const enum {
287  } m_base_format;
289 };
292 
294 class Base
295 {
296 public:
297  uint8_t m_base_data;
298 
299  Base() : m_base_data(17) {}
300  explicit Base(uint8_t data) : m_base_data(data) {}
301 
302  template <typename Stream>
303  void Serialize(Stream& s) const
304  {
305  if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
306  s << m_base_data;
307  } else {
308  s << std::span<const char>{HexStr(std::span{&m_base_data, 1})};
309  }
310  }
311 
312  template <typename Stream>
313  void Unserialize(Stream& s)
314  {
315  if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
316  s >> m_base_data;
317  } else {
318  std::string hex{"aa"};
319  s >> std::span{hex}.first(hex.size());
320  m_base_data = TryParseHex<uint8_t>(hex).value().at(0);
321  }
322  }
323 };
324 
326 {
327 public:
329 
330  enum class DerivedFormat {
331  LOWER,
332  UPPER,
334 
336 };
337 
338 class Derived : public Base
339 {
340 public:
341  std::string m_derived_data;
342 
344  {
345  auto& fmt = SER_PARAMS(DerivedAndBaseFormat);
346  READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
347 
348  if (ser_action.ForRead()) {
349  std::string str;
350  s >> str;
351  SER_READ(obj, obj.m_derived_data = str);
352  } else {
353  s << (fmt.m_derived_format == DerivedAndBaseFormat::DerivedFormat::LOWER ?
354  ToLower(obj.m_derived_data) :
355  ToUpper(obj.m_derived_data));
356  }
357  }
358 };
359 
360 struct OtherParam {
361  uint8_t param;
363 };
364 
369 {
370 public:
371  template <typename Stream>
372  void Serialize(Stream& s) const
373  {
374  const uint8_t param = s.template GetParams<OtherParam>().param;
375  s << param;
376  }
377 
378  template <typename Stream>
379  void Unserialize(Stream& s) const
380  {
381  const uint8_t param = s.template GetParams<OtherParam>().param;
382  uint8_t value;
383  s >> value;
384  BOOST_CHECK_EQUAL(value, param);
385  }
386 };
387 
395 BOOST_AUTO_TEST_CASE(with_params_multi)
396 {
397  const OtherParam other_param_used{.param = 0x10};
398  const OtherParam other_param_ignored{.param = 0x11};
399  const OtherParam other_param_override{.param = 0x12};
400  const OtherParamChecker check;
401  DataStream stream;
402  ParamsStream pstream{stream, RAW, other_param_used, other_param_ignored};
403 
404  Base base1{0x20};
405  pstream << base1 << check << other_param_override(check);
406  BOOST_CHECK_EQUAL(stream.str(), "\x20\x10\x12");
407 
408  Base base2;
409  pstream >> base2 >> check >> other_param_override(check);
410  BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
411 }
412 
414 BOOST_AUTO_TEST_CASE(with_params_move)
415 {
416  UncopyableStream stream{MakeByteSpan(std::string_view{"abc"})};
417  ParamsStream pstream{std::move(stream), RAW, HEX, RAW};
418  BOOST_CHECK_EQUAL(pstream.GetStream().str(), "abc");
419  pstream.GetStream().clear();
420 
421  Base base1{0x20};
422  pstream << base1;
423  BOOST_CHECK_EQUAL(pstream.GetStream().str(), "\x20");
424 
425  Base base2;
426  pstream >> base2;
427  BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
428 }
429 
430 BOOST_AUTO_TEST_CASE(with_params_base)
431 {
432  Base b{0x0F};
433 
434  DataStream stream;
435 
436  stream << RAW(b);
437  BOOST_CHECK_EQUAL(stream.str(), "\x0F");
438 
439  b.m_base_data = 0;
440  stream >> RAW(b);
441  BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
442 
443  stream.clear();
444 
445  stream << HEX(b);
446  BOOST_CHECK_EQUAL(stream.str(), "0f");
447 
448  b.m_base_data = 0;
449  stream >> HEX(b);
450  BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
451 }
452 
453 BOOST_AUTO_TEST_CASE(with_params_vector_of_base)
454 {
455  std::vector<Base> v{Base{0x0F}, Base{0xFF}};
456 
457  DataStream stream;
458 
459  stream << RAW(v);
460  BOOST_CHECK_EQUAL(stream.str(), "\x02\x0F\xFF");
461 
462  v[0].m_base_data = 0;
463  v[1].m_base_data = 0;
464  stream >> RAW(v);
465  BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
466  BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
467 
468  stream.clear();
469 
470  stream << HEX(v);
471  BOOST_CHECK_EQUAL(stream.str(), "\x02"
472  "0fff");
473 
474  v[0].m_base_data = 0;
475  v[1].m_base_data = 0;
476  stream >> HEX(v);
477  BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
478  BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
479 }
480 
483 
484 BOOST_AUTO_TEST_CASE(with_params_derived)
485 {
486  Derived d;
487  d.m_base_data = 0x0F;
488  d.m_derived_data = "xY";
489 
490  DataStream stream;
491 
492  stream << RAW_LOWER(d);
493 
494  stream << HEX_UPPER(d);
495 
496  BOOST_CHECK_EQUAL(stream.str(), "\x0F\x02xy"
497  "0f\x02XY");
498 }
499 
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Wrapper that overrides the GetParams() function of a stream.
Definition: serialize.h:1106
#define VARINT(obj)
Definition: serialize.h:491
auto MakeByteSpan(const V &v) noexcept
Definition: span.h:84
DataStream()=default
constexpr BaseFormat HEX
(Un)serialize a number as raw byte or 2 hexadecimal chars.
void Serialize(Stream &s) const
BOOST_AUTO_TEST_CASE(sizes)
T check(T ptr)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:330
SERIALIZE_METHODS(Derived, obj)
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
void Unserialize(Stream &s)
constexpr DerivedAndBaseFormat RAW_LOWER
Definition: common.h:29
memcpy(result.begin(), stream.data(), stream.size())
#define SER_PARAMS(type)
Formatter methods can retrieve parameters attached to a stream using the SER_PARAMS(type) macro as lo...
Definition: serialize.h:206
Checker for value of OtherParam.
Basic testing setup.
Definition: setup_common.h:64
static bool isCanonicalException(const std::ios_base::failure &ex)
bool operator==(const CSerializeMethodsTestSingle &rhs) const
vector_type::size_type size_type
Definition: streams.h:141
#define VARINT_MODE(obj, mode)
Definition: serialize.h:490
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:132
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1089
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
BOOST_AUTO_TEST_SUITE_END()
uint64_t GetSerializeSize(const T &t)
Definition: serialize.h:1095
#define SER_PARAMS_OPFUNC
Helper macro for SerParams structs.
Definition: serialize.h:1212
enum DerivedAndBaseFormat::DerivedFormat m_derived_format
enum BaseFormat::@14 m_base_format
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
void Serialize(Stream &s) const
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
void Unserialize(Stream &s) const
std::string str() const
Definition: streams.h:154
void clear()
Definition: streams.h:173
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
std::string m_derived_data
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:32
constexpr BaseFormat RAW
A mutable version of CTransaction.
Definition: transaction.h:357
uint8_t m_base_data
#define SER_READ(obj, code)
Definition: serialize.h:146
constexpr DerivedAndBaseFormat HEX_UPPER
#define READWRITE(...)
Definition: serialize.h:145
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t *charstrvalin, const CTransactionRef &txvalin)
SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:180
#define BOOST_CHECK(expr)
Definition: object.cpp:16
Base(uint8_t data)
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.