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