/ src / test / serialize_tests.cpp
serialize_tests.cpp
  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>
  9  #include <test/util/setup_common.h>
 10  #include <util/strencodings.h>
 11  
 12  #include <cstdint>
 13  #include <string>
 14  
 15  #include <boost/test/unit_test.hpp>
 16  
 17  BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
 18  
 19  // For testing move-semantics, declare a version of datastream that can be moved
 20  // but is not copyable.
 21  class UncopyableStream : public DataStream
 22  {
 23  public:
 24      using DataStream::DataStream;
 25      UncopyableStream(const UncopyableStream&) = delete;
 26      UncopyableStream& operator=(const UncopyableStream&) = delete;
 27      UncopyableStream(UncopyableStream&&) noexcept = default;
 28      UncopyableStream& operator=(UncopyableStream&&) noexcept = default;
 29  };
 30  
 31  class CSerializeMethodsTestSingle
 32  {
 33  protected:
 34      int intval;
 35      bool boolval;
 36      std::string stringval;
 37      char charstrval[16];
 38      CTransactionRef txval;
 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  
 46      SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
 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  
 65  class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
 66  {
 67  public:
 68      using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
 69  
 70      SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
 71      {
 72          READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));
 73      }
 74  };
 75  
 76  BOOST_AUTO_TEST_CASE(sizes)
 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  
105  BOOST_AUTO_TEST_CASE(varints)
106  {
107      // encode
108  
109      DataStream ss{};
110      DataStream::size_type size = 0;
111      for (int i = 0; i < 100000; i++) {
112          ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
113          size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED));
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;
126          ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);
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  
158  BOOST_AUTO_TEST_CASE(compactsize)
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  
188  BOOST_AUTO_TEST_CASE(vector_bool)
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  
197  BOOST_AUTO_TEST_CASE(array)
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 {
285          RAW,
286          HEX,
287      } m_base_format;
288      SER_PARAMS_OPFUNC
289  };
290  constexpr BaseFormat RAW{BaseFormat::RAW};
291  constexpr BaseFormat HEX{BaseFormat::HEX};
292  
293  /// (Un)serialize a number as raw byte or 2 hexadecimal chars.
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  
325  class DerivedAndBaseFormat
326  {
327  public:
328      BaseFormat m_base_format;
329  
330      enum class DerivedFormat {
331          LOWER,
332          UPPER,
333      } m_derived_format;
334  
335      SER_PARAMS_OPFUNC
336  };
337  
338  class Derived : public Base
339  {
340  public:
341      std::string m_derived_data;
342  
343      SERIALIZE_METHODS(Derived, obj)
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;
362      SER_PARAMS_OPFUNC
363  };
364  
365  //! Checker for value of OtherParam. When being serialized, serializes the
366  //! param to the stream. When being unserialized, verifies the value in the
367  //! stream matches the param.
368  class OtherParamChecker
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  
388  //! Test creating a stream with multiple parameters and making sure
389  //! serialization code requiring different parameters can retrieve them. Also
390  //! test that earlier parameters take precedence if the same parameter type is
391  //! specified twice. (Choice of whether earlier or later values take precedence
392  //! or multiple values of the same type are allowed was arbitrary, and just
393  //! decided based on what would require smallest amount of ugly C++ template
394  //! code. Intent of the test is to just ensure there is no unexpected behavior.)
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  
413  //! Test creating a ParamsStream that moves from a stream argument.
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  
481  constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER};
482  constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER};
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  
500  BOOST_AUTO_TEST_SUITE_END()