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/setup_common.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. 20 class UncopyableStream : public DataStream 21 { 22 public: 23 using DataStream::DataStream; 24 UncopyableStream(const UncopyableStream&) = delete; 25 UncopyableStream& operator=(const UncopyableStream&) = delete; 26 UncopyableStream(UncopyableStream&&) noexcept = default; 27 UncopyableStream& operator=(UncopyableStream&&) noexcept = default; 28 }; 29 30 class CSerializeMethodsTestSingle 31 { 32 protected: 33 int intval; 34 bool boolval; 35 std::string stringval; 36 char charstrval[16]; 37 CTransactionRef txval; 38 public: 39 CSerializeMethodsTestSingle() = default; 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 45 SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj) 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 64 class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle 65 { 66 public: 67 using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle; 68 69 SERIALIZE_METHODS(CSerializeMethodsTestMany, obj) 70 { 71 READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval)); 72 } 73 }; 74 75 BOOST_AUTO_TEST_CASE(sizes) 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 104 BOOST_AUTO_TEST_CASE(varints) 105 { 106 // encode 107 108 DataStream ss{}; 109 DataStream::size_type size = 0; 110 for (int i = 0; i < 100000; i++) { 111 ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED); 112 size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED)); 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; 125 ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED); 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 157 BOOST_AUTO_TEST_CASE(compactsize) 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 187 BOOST_AUTO_TEST_CASE(vector_bool) 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 196 BOOST_AUTO_TEST_CASE(array) 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 << std::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 << std::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 << std::span{"\xfd\xfd\x00"}.first(3); 223 n = ReadCompactSize(ss); 224 BOOST_CHECK(n == 0xfd); 225 226 // zero encoded with five bytes: 227 ss << std::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 << std::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 << std::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 << std::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 << std::span{in} << std::byte{'c'}; 273 std::array<std::byte, 2> out; 274 std::byte out_3; 275 ds >> std::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 { 284 RAW, 285 HEX, 286 } m_base_format; 287 SER_PARAMS_OPFUNC 288 }; 289 constexpr BaseFormat RAW{BaseFormat::RAW}; 290 constexpr BaseFormat HEX{BaseFormat::HEX}; 291 292 /// (Un)serialize a number as raw byte or 2 hexadecimal chars. 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 << std::span<const char>{HexStr(std::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 >> std::span{hex}.first(hex.size()); 319 m_base_data = TryParseHex<uint8_t>(hex).value().at(0); 320 } 321 } 322 }; 323 324 class DerivedAndBaseFormat 325 { 326 public: 327 BaseFormat m_base_format; 328 329 enum class DerivedFormat { 330 LOWER, 331 UPPER, 332 } m_derived_format; 333 334 SER_PARAMS_OPFUNC 335 }; 336 337 class Derived : public Base 338 { 339 public: 340 std::string m_derived_data; 341 342 SERIALIZE_METHODS(Derived, obj) 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; 361 SER_PARAMS_OPFUNC 362 }; 363 364 //! Checker for value of OtherParam. When being serialized, serializes the 365 //! param to the stream. When being unserialized, verifies the value in the 366 //! stream matches the param. 367 class OtherParamChecker 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 387 //! Test creating a stream with multiple parameters and making sure 388 //! serialization code requiring different parameters can retrieve them. Also 389 //! test that earlier parameters take precedence if the same parameter type is 390 //! specified twice. (Choice of whether earlier or later values take precedence 391 //! or multiple values of the same type are allowed was arbitrary, and just 392 //! decided based on what would require smallest amount of ugly C++ template 393 //! code. Intent of the test is to just ensure there is no unexpected behavior.) 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 412 //! Test creating a ParamsStream that moves from a stream argument. 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 480 constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER}; 481 constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER}; 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 499 BOOST_AUTO_TEST_SUITE_END()