LCOV - code coverage report
Current view: top level - src/test - serialize_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.4 % 323 321
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 59 59
Branches: 50.0 % 2014 1007

             Branch data     Line data    Source code
       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 <boost/test/unit_test.hpp>
      13                 :             : 
      14                 :             : #include <algorithm>
      15                 :             : #include <cstdint>
      16                 :             : #include <limits>
      17                 :             : #include <string>
      18                 :             : 
      19                 :             : BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
      20                 :             : 
      21                 :             : // For testing move-semantics, declare a version of datastream that can be moved
      22                 :             : // but is not copyable.
      23                 :           3 : class UncopyableStream : public DataStream
      24                 :             : {
      25                 :             : public:
      26                 :           1 :     using DataStream::DataStream;
      27                 :             :     UncopyableStream(const UncopyableStream&) = delete;
      28                 :             :     UncopyableStream& operator=(const UncopyableStream&) = delete;
      29                 :           2 :     UncopyableStream(UncopyableStream&&) noexcept = default;
      30                 :             :     UncopyableStream& operator=(UncopyableStream&&) noexcept = default;
      31                 :             : };
      32                 :             : 
      33                 :             : class CSerializeMethodsTestSingle
      34                 :             : {
      35                 :             : protected:
      36                 :             :     int intval;
      37                 :             :     bool boolval;
      38                 :             :     std::string stringval;
      39                 :             :     char charstrval[16];
      40                 :             :     CTransactionRef txval;
      41                 :             : public:
      42                 :           1 :     CSerializeMethodsTestSingle() = default;
      43         [ +  - ]:           2 :     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)
      44                 :             :     {
      45                 :           2 :         memcpy(charstrval, charstrvalin, sizeof(charstrval));
      46                 :           2 :     }
      47                 :             : 
      48                 :           6 :     SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
      49                 :             :     {
      50                 :           3 :         READWRITE(obj.intval);
      51                 :           3 :         READWRITE(obj.boolval);
      52                 :           3 :         READWRITE(obj.stringval);
      53                 :           3 :         READWRITE(obj.charstrval);
      54                 :           3 :         READWRITE(TX_WITH_WITNESS(obj.txval));
      55                 :           3 :     }
      56                 :             : 
      57                 :           5 :     bool operator==(const CSerializeMethodsTestSingle& rhs) const
      58                 :             :     {
      59                 :          10 :         return intval == rhs.intval &&
      60         [ +  - ]:           5 :                boolval == rhs.boolval &&
      61         [ +  - ]:           5 :                stringval == rhs.stringval &&
      62   [ +  -  +  -  :          15 :                strcmp(charstrval, rhs.charstrval) == 0 &&
                   -  + ]
      63                 :           5 :                txval->Equals(*rhs.txval);
      64                 :             :     }
      65                 :             : };
      66                 :             : 
      67                 :           0 : class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
      68                 :             : {
      69                 :             : public:
      70                 :           1 :     using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
      71                 :             : 
      72                 :           4 :     SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
      73                 :             :     {
      74                 :           2 :         READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));
      75                 :           2 :     }
      76                 :             : };
      77                 :             : 
      78   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(sizes)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      79                 :             : {
      80         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0));
      81         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0)));
      82         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0)));
      83   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0)));
      84         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0)));
      85         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0)));
      86         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0)));
      87         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0)));
      88         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0)));
      89                 :             :     // Bool is serialized as uint8_t
      90         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0)));
      91                 :             : 
      92                 :             :     // Sanity-check GetSerializeSize and c++ type matching
      93         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0), 1U);
      94         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0)), 1U);
      95         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0)), 1U);
      96         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0)), 2U);
      97         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0)), 2U);
      98         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0)), 4U);
      99         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0)), 4U);
     100         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0)), 8U);
     101         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0)), 8U);
     102         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(bool(0)), 1U);
     103         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}), 1U);
     104         [ +  - ]:           1 :     BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}), 2U);
     105                 :           1 : }
     106                 :             : 
     107   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(varints)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     108                 :             : {
     109                 :             :     // encode
     110                 :             : 
     111                 :           1 :     DataStream ss{};
     112                 :           1 :     DataStream::size_type size = 0;
     113         [ +  + ]:      100001 :     for (int i = 0; i < 100000; i++) {
     114         [ +  - ]:      100000 :         ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
     115                 :      100000 :         size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED));
     116   [ +  -  -  +  :      200000 :         BOOST_CHECK(size == ss.size());
                   +  - ]
     117                 :             :     }
     118                 :             : 
     119         [ +  + ]:         102 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
     120         [ +  - ]:         101 :         ss << VARINT(i);
     121                 :         101 :         size += ::GetSerializeSize(VARINT(i));
     122   [ +  -  -  +  :         202 :         BOOST_CHECK(size == ss.size());
                   +  - ]
     123                 :             :     }
     124                 :             : 
     125                 :             :     // decode
     126         [ +  + ]:      100001 :     for (int i = 0; i < 100000; i++) {
     127                 :      100000 :         int j = -1;
     128         [ +  - ]:      100000 :         ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);
     129   [ +  -  +  - ]:      200000 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     130                 :             :     }
     131                 :             : 
     132         [ +  + ]:         102 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
     133                 :         101 :         uint64_t j = std::numeric_limits<uint64_t>::max();
     134         [ +  - ]:         101 :         ss >> VARINT(j);
     135   [ +  -  +  - ]:         202 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     136                 :             :     }
     137                 :           1 : }
     138                 :             : 
     139   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(varints_overflow)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     140                 :             : {
     141                 :           9 :     const auto read_varint = [](uint64_t serialized, auto value) {
     142         [ +  - ]:           8 :         DataStream ss{};
     143   [ +  -  +  + ]:          16 :         ss << VARINT(serialized);
     144         [ +  + ]:           8 :         ss >> VARINT(value);
     145                 :           5 :         return value;
     146                 :           5 :     };
     147                 :             : 
     148                 :           7 :     for (const uint32_t small_value : {
     149                 :             :              uint32_t{0},
     150                 :             :              uint32_t{1},
     151                 :             :              std::numeric_limits<uint32_t>::max(),
     152         [ +  + ]:           4 :          }) {
     153         [ +  - ]:           3 :         BOOST_CHECK_EQUAL(read_varint(small_value, uint32_t{}), small_value);
     154                 :             :     }
     155                 :             : 
     156                 :           5 :     for (const uint64_t large_value : {
     157                 :             :              uint64_t{std::numeric_limits<uint32_t>::max()} + 1,
     158                 :             :              std::numeric_limits<uint64_t>::max(),
     159         [ +  + ]:           3 :          }) {
     160         [ +  - ]:           2 :         BOOST_CHECK_EQUAL(read_varint(large_value, uint64_t{}), large_value);
     161                 :             : 
     162   [ +  -  -  +  :           4 :         BOOST_CHECK_EXCEPTION(read_varint(large_value, uint32_t{}), std::ios_base::failure, HasReason{"VarInt exceeds destination range"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     163                 :             :     }
     164                 :             : 
     165   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(read_varint(uint64_t{1} << 39, uint32_t{}), std::ios_base::failure, HasReason{"VarInt increment exceeds destination range"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     166                 :           1 : }
     167                 :             : 
     168   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(varints_bitpatterns)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     169                 :             : {
     170                 :           1 :     DataStream ss{};
     171   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     172   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     173   [ +  -  +  -  :           2 :     ss << VARINT_MODE(int8_t{0x7f}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     174   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     175   [ +  -  +  -  :           2 :     ss << VARINT(uint8_t{0x80}); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     176   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     177   [ +  -  +  -  :           2 :     ss << VARINT_MODE(int16_t{0x1234}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     178   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     179   [ +  -  +  -  :           2 :     ss << VARINT(uint16_t{0xffff}); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     180   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     181   [ +  -  +  -  :           2 :     ss << VARINT_MODE(int32_t{0x123456}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     182   [ +  -  +  -  :           2 :     ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     183   [ +  -  +  -  :           2 :     ss << VARINT(uint32_t{0x80123456U}); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     184   [ +  -  +  -  :           2 :     ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     185   [ +  -  +  -  :           2 :     ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     186   [ +  -  +  -  :           3 :     ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
          -  +  +  -  +  
                -  +  - ]
     187                 :           1 : }
     188                 :             : 
     189   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(compactsize)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     190                 :             : {
     191                 :           1 :     DataStream ss{};
     192                 :           1 :     std::vector<char>::size_type i, j;
     193                 :             : 
     194         [ +  + ]:          27 :     for (i = 1; i <= MAX_SIZE; i *= 2)
     195                 :             :     {
     196         [ +  - ]:          26 :         WriteCompactSize(ss, i-1);
     197         [ +  - ]:          26 :         WriteCompactSize(ss, i);
     198                 :             :     }
     199         [ +  + ]:          27 :     for (i = 1; i <= MAX_SIZE; i *= 2)
     200                 :             :     {
     201         [ +  - ]:          26 :         j = ReadCompactSize(ss);
     202   [ +  -  +  -  :          52 :         BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
                   +  - ]
     203         [ +  - ]:          26 :         j = ReadCompactSize(ss);
     204   [ +  -  +  - ]:          52 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
     205                 :             :     }
     206                 :           1 : }
     207                 :             : 
     208                 :           6 : static bool isCanonicalException(const std::ios_base::failure& ex)
     209                 :             : {
     210                 :           6 :     std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
     211                 :             : 
     212                 :             :     // The string returned by what() can be different for different platforms.
     213                 :             :     // Instead of directly comparing the ex.what() with an expected string,
     214                 :             :     // create an instance of exception to see if ex.what() matches
     215                 :             :     // the expected explanatory string returned by the exception instance.
     216                 :           6 :     return strcmp(expectedException.what(), ex.what()) == 0;
     217                 :           6 : }
     218                 :             : 
     219   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(vector_bool)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     220                 :             : {
     221                 :           1 :     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};
     222         [ +  - ]:           1 :     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};
     223                 :             : 
     224   [ +  -  +  -  :           2 :     BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
             +  -  +  - ]
     225   [ +  -  +  -  :           3 :     BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     226                 :           1 : }
     227                 :             : 
     228   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(array)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     229                 :             : {
     230                 :           1 :     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};
     231                 :           1 :     DataStream ds;
     232         [ +  - ]:           1 :     ds << array1;
     233                 :           1 :     std::array<uint8_t, 32> array2;
     234         [ +  - ]:           1 :     ds >> array2;
     235   [ +  -  +  - ]:           2 :     BOOST_CHECK(array1 == array2);
     236                 :           1 : }
     237                 :             : 
     238   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(noncanonical)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     239                 :             : {
     240                 :             :     // Write some non-canonical CompactSize encodings, and
     241                 :             :     // make sure an exception is thrown when read back.
     242                 :           1 :     DataStream ss{};
     243                 :           1 :     std::vector<char>::size_type n;
     244                 :             : 
     245                 :             :     // zero encoded with three bytes:
     246         [ +  - ]:           1 :     ss << std::span{"\xfd\x00\x00"}.first(3);
     247   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     248                 :             : 
     249                 :             :     // 0xfc encoded with three bytes:
     250         [ +  - ]:           1 :     ss << std::span{"\xfd\xfc\x00"}.first(3);
     251   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     252                 :             : 
     253                 :             :     // 0xfd encoded with three bytes is OK:
     254         [ +  - ]:           1 :     ss << std::span{"\xfd\xfd\x00"}.first(3);
     255         [ +  - ]:           1 :     n = ReadCompactSize(ss);
     256   [ +  -  +  -  :           2 :     BOOST_CHECK(n == 0xfd);
                   +  - ]
     257                 :             : 
     258                 :             :     // zero encoded with five bytes:
     259         [ +  - ]:           1 :     ss << std::span{"\xfe\x00\x00\x00\x00"}.first(5);
     260   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     261                 :             : 
     262                 :             :     // 0xffff encoded with five bytes:
     263         [ +  - ]:           1 :     ss << std::span{"\xfe\xff\xff\x00\x00"}.first(5);
     264   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     265                 :             : 
     266                 :             :     // zero encoded with nine bytes:
     267         [ +  - ]:           1 :     ss << std::span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
     268   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     269                 :             : 
     270                 :             :     // 0x01ffffff encoded with nine bytes:
     271         [ +  - ]:           1 :     ss << std::span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
     272   [ +  -  -  +  :           2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     273                 :           1 : }
     274                 :             : 
     275   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(truncated_compact_size)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     276                 :             : {
     277                 :             :     // A CompactSize prefix followed by fewer bytes than it announces fails to read.
     278         [ +  + ]:           5 :     for (const char* hex : {"fd", "fd01", "fe010000", "ff01000000000000"}) {
     279   [ -  +  +  - ]:           4 :         DataStream ss{ParseHex(hex)};
     280   [ +  -  -  +  :           8 :         BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, HasReason{"end of data"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     281                 :           4 :     }
     282                 :           1 : }
     283                 :             : 
     284   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(string_view)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     285                 :             : {
     286         [ +  - ]:           1 :     const std::string_view sv{"hello, world"};
     287                 :           1 :     DataStream ss;
     288         [ +  - ]:           1 :     ss << sv;
     289         [ +  - ]:           1 :     std::string s;
     290         [ +  - ]:           1 :     ss >> s;
     291   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(sv, s);
     292                 :           1 : }
     293                 :             : 
     294   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(limited_vector)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     295                 :             : {
     296                 :           1 :     const std::vector<int> v = {1,2,3,4,-5,-6,-7,-8,-9,-10,10000,20000,-30000};
     297                 :             : 
     298                 :           7 :     auto check = [&]<size_t N>() {
     299                 :           6 :         DataStream ss;
     300         [ +  - ]:           6 :         ss << v;
     301                 :             :         try {
     302         [ +  + ]:           6 :             std::vector<int> r;
     303         [ +  + ]:           6 :             ss >> LIMITED_VECTOR(r, N);
     304   [ +  -  -  +  :           3 :             BOOST_CHECK_LE(r.size(), N);
                   +  - ]
     305   [ +  -  +  - ]:           6 :             BOOST_CHECK(std::ranges::equal(r, v));
     306         [ -  + ]:           6 :         } catch (const std::ios_base::failure&) {
     307   [ +  -  -  +  :           3 :             BOOST_CHECK_GT(v.size(), N);
                   +  - ]
     308                 :             :         }
     309                 :           7 :     };
     310         [ +  - ]:           1 :     check.operator()<0>();
     311         [ +  - ]:           1 :     check.operator()<10>();
     312         [ +  - ]:           1 :     check.operator()<12>();
     313         [ +  - ]:           1 :     check.operator()<13>();
     314         [ +  - ]:           1 :     check.operator()<14>();
     315         [ +  - ]:           1 :     check.operator()<100>();
     316                 :           1 : }
     317                 :             : 
     318   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(class_methods)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     319                 :             : {
     320                 :           1 :     int intval(100);
     321                 :           1 :     bool boolval(true);
     322                 :           1 :     std::string stringval("testing");
     323                 :           1 :     const uint8_t charstrval[16]{"testing charstr"};
     324         [ +  - ]:           1 :     CMutableTransaction txval;
     325         [ +  - ]:           1 :     CTransactionRef tx_ref{MakeTransactionRef(txval)};
     326         [ -  + ]:           2 :     CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
     327         [ -  + ]:           2 :     CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
     328         [ +  - ]:           1 :     CSerializeMethodsTestSingle methodtest3;
     329                 :           1 :     CSerializeMethodsTestMany methodtest4;
     330                 :           1 :     DataStream ss;
     331   [ +  -  +  -  :           2 :     BOOST_CHECK(methodtest1 == methodtest2);
                   +  - ]
     332         [ +  - ]:           1 :     ss << methodtest1;
     333         [ +  - ]:           1 :     ss >> methodtest4;
     334         [ +  - ]:           1 :     ss << methodtest2;
     335         [ +  - ]:           1 :     ss >> methodtest3;
     336   [ +  -  +  -  :           2 :     BOOST_CHECK(methodtest1 == methodtest2);
                   +  - ]
     337   [ +  -  +  -  :           2 :     BOOST_CHECK(methodtest2 == methodtest3);
                   +  - ]
     338   [ +  -  +  -  :           2 :     BOOST_CHECK(methodtest3 == methodtest4);
                   +  - ]
     339                 :             : 
     340                 :           1 :     DataStream ss2;
     341   [ +  -  +  -  :           1 :     ss2 << intval << boolval << stringval << charstrval << TX_WITH_WITNESS(txval);
          +  -  +  -  +  
                      - ]
     342         [ +  - ]:           1 :     ss2 >> methodtest3;
     343   [ +  -  +  -  :           2 :     BOOST_CHECK(methodtest3 == methodtest4);
                   +  - ]
     344                 :           1 :     {
     345                 :           1 :         DataStream ds;
     346         [ +  - ]:           1 :         const std::string in{"ab"};
     347   [ -  +  +  -  :           1 :         ds << std::span{in} << std::byte{'c'};
                   +  - ]
     348                 :           1 :         std::array<std::byte, 2> out;
     349                 :           1 :         std::byte out_3;
     350   [ +  -  +  - ]:           1 :         ds >> std::span{out} >> out_3;
     351   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
     352   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
     353   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
     354                 :           1 :     }
     355         [ +  - ]:           3 : }
     356                 :             : 
     357                 :             : struct BaseFormat {
     358                 :             :     const enum {
     359                 :             :         RAW,
     360                 :             :         HEX,
     361                 :             :     } m_base_format;
     362                 :          10 :     SER_PARAMS_OPFUNC
     363                 :             : };
     364                 :             : constexpr BaseFormat RAW{BaseFormat::RAW};
     365                 :             : constexpr BaseFormat HEX{BaseFormat::HEX};
     366                 :             : 
     367                 :             : /// (Un)serialize a number as raw byte or 2 hexadecimal chars.
     368                 :             : class Base
     369                 :             : {
     370                 :             : public:
     371                 :             :     uint8_t m_base_data;
     372                 :             : 
     373                 :           4 :     Base() : m_base_data(17) {}
     374                 :           4 :     explicit Base(uint8_t data) : m_base_data(data) {}
     375                 :             : 
     376                 :             :     template <typename Stream>
     377         [ +  + ]:          10 :     void Serialize(Stream& s) const
     378                 :             :     {
     379         [ +  + ]:          10 :         if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
     380                 :           6 :             s << m_base_data;
     381                 :             :         } else {
     382   [ -  +  +  - ]:           4 :             s << std::span<const char>{HexStr(std::span{&m_base_data, 1})};
     383                 :             :         }
     384                 :          10 :     }
     385                 :             : 
     386                 :             :     template <typename Stream>
     387         [ +  + ]:           8 :     void Unserialize(Stream& s)
     388                 :             :     {
     389         [ +  + ]:           8 :         if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
     390                 :           5 :             s >> m_base_data;
     391                 :             :         } else {
     392                 :           3 :             std::string hex{"aa"};
     393   [ +  -  -  + ]:           3 :             s >> std::span{hex}.first(hex.size());
     394   [ +  -  +  - ]:           6 :             m_base_data = TryParseHex<uint8_t>(hex).value().at(0);
     395                 :           3 :         }
     396                 :           8 :     }
     397                 :             : };
     398                 :             : 
     399                 :             : class DerivedAndBaseFormat
     400                 :             : {
     401                 :             : public:
     402                 :             :     BaseFormat m_base_format;
     403                 :             : 
     404                 :             :     enum class DerivedFormat {
     405                 :             :         LOWER,
     406                 :             :         UPPER,
     407                 :             :     } m_derived_format;
     408                 :             : 
     409                 :           2 :     SER_PARAMS_OPFUNC
     410                 :             : };
     411                 :             : 
     412                 :           0 : class Derived : public Base
     413                 :             : {
     414                 :             : public:
     415                 :             :     std::string m_derived_data;
     416                 :             : 
     417                 :           4 :     SERIALIZE_METHODS(Derived, obj)
     418                 :             :     {
     419                 :           2 :         auto& fmt = SER_PARAMS(DerivedAndBaseFormat);
     420                 :           2 :         READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
     421                 :             : 
     422                 :             :         if (ser_action.ForRead()) {
     423                 :             :             std::string str;
     424                 :             :             s >> str;
     425                 :             :             SER_READ(obj, obj.m_derived_data = str);
     426                 :             :         } else {
     427   [ +  +  -  +  :           4 :             s << (fmt.m_derived_format == DerivedAndBaseFormat::DerivedFormat::LOWER ?
                   -  + ]
     428                 :             :                       ToLower(obj.m_derived_data) :
     429                 :             :                       ToUpper(obj.m_derived_data));
     430                 :             :         }
     431                 :           2 :     }
     432                 :             : };
     433                 :             : 
     434                 :             : struct OtherParam {
     435                 :             :     uint8_t param;
     436                 :           2 :     SER_PARAMS_OPFUNC
     437                 :             : };
     438                 :             : 
     439                 :             : //! Checker for value of OtherParam. When being serialized, serializes the
     440                 :             : //! param to the stream. When being unserialized, verifies the value in the
     441                 :             : //! stream matches the param.
     442                 :             : class OtherParamChecker
     443                 :             : {
     444                 :             : public:
     445                 :             :     template <typename Stream>
     446                 :           2 :     void Serialize(Stream& s) const
     447                 :             :     {
     448                 :           2 :         const uint8_t param = s.template GetParams<OtherParam>().param;
     449                 :           2 :         s << param;
     450                 :             :     }
     451                 :             : 
     452                 :             :     template <typename Stream>
     453                 :           2 :     void Unserialize(Stream& s) const
     454                 :             :     {
     455                 :           2 :         const uint8_t param = s.template GetParams<OtherParam>().param;
     456                 :             :         uint8_t value;
     457                 :           2 :         s >> value;
     458         [ +  - ]:           2 :         BOOST_CHECK_EQUAL(value, param);
     459                 :           2 :     }
     460                 :             : };
     461                 :             : 
     462                 :             : //! Test creating a stream with multiple parameters and making sure
     463                 :             : //! serialization code requiring different parameters can retrieve them. Also
     464                 :             : //! test that earlier parameters take precedence if the same parameter type is
     465                 :             : //! specified twice. (Choice of whether earlier or later values take precedence
     466                 :             : //! or multiple values of the same type are allowed was arbitrary, and just
     467                 :             : //! decided based on what would require smallest amount of ugly C++ template
     468                 :             : //! code. Intent of the test is to just ensure there is no unexpected behavior.)
     469   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(with_params_multi)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     470                 :             : {
     471                 :           1 :     const OtherParam other_param_used{.param = 0x10};
     472                 :           1 :     const OtherParam other_param_ignored{.param = 0x11};
     473                 :           1 :     const OtherParam other_param_override{.param = 0x12};
     474                 :           1 :     const OtherParamChecker check;
     475                 :           1 :     DataStream stream;
     476         [ +  - ]:           1 :     ParamsStream pstream{stream, RAW, other_param_used, other_param_ignored};
     477                 :             : 
     478                 :           1 :     Base base1{0x20};
     479   [ +  -  +  -  :           1 :     pstream << base1 << check << other_param_override(check);
                   +  - ]
     480   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "\x20\x10\x12");
                   +  - ]
     481                 :             : 
     482                 :           1 :     Base base2;
     483   [ +  -  +  -  :           1 :     pstream >> base2 >> check >> other_param_override(check);
                   +  - ]
     484   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
     485                 :           1 : }
     486                 :             : 
     487                 :             : //! Test creating a ParamsStream that moves from a stream argument.
     488   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(with_params_move)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     489                 :             : {
     490                 :           1 :     UncopyableStream stream{MakeByteSpan(std::string_view{"abc"})};
     491                 :           1 :     ParamsStream pstream{std::move(stream), RAW, HEX, RAW};
     492   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pstream.GetStream().str(), "abc");
                   +  - ]
     493         [ +  - ]:           1 :     pstream.GetStream().clear();
     494                 :             : 
     495                 :           1 :     Base base1{0x20};
     496         [ +  - ]:           1 :     pstream << base1;
     497   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(pstream.GetStream().str(), "\x20");
                   +  - ]
     498                 :             : 
     499                 :           1 :     Base base2;
     500         [ +  - ]:           1 :     pstream >> base2;
     501   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
     502                 :           1 : }
     503                 :             : 
     504   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(with_params_base)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     505                 :             : {
     506                 :           1 :     Base b{0x0F};
     507                 :             : 
     508                 :           1 :     DataStream stream;
     509                 :             : 
     510         [ +  - ]:           1 :     stream << RAW(b);
     511   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "\x0F");
                   +  - ]
     512                 :             : 
     513                 :           1 :     b.m_base_data = 0;
     514         [ +  - ]:           1 :     stream >> RAW(b);
     515   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
     516                 :             : 
     517         [ -  + ]:           1 :     stream.clear();
     518                 :             : 
     519         [ +  - ]:           1 :     stream << HEX(b);
     520   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "0f");
                   +  - ]
     521                 :             : 
     522                 :           1 :     b.m_base_data = 0;
     523         [ +  - ]:           1 :     stream >> HEX(b);
     524   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
     525                 :           1 : }
     526                 :             : 
     527   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(with_params_vector_of_base)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     528                 :             : {
     529                 :           1 :     std::vector<Base> v{Base{0x0F}, Base{0xFF}};
     530                 :             : 
     531                 :           1 :     DataStream stream;
     532                 :             : 
     533         [ +  - ]:           1 :     stream << RAW(v);
     534   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "\x02\x0F\xFF");
                   +  - ]
     535                 :             : 
     536         [ +  - ]:           1 :     v[0].m_base_data = 0;
     537                 :           1 :     v[1].m_base_data = 0;
     538         [ +  - ]:           1 :     stream >> RAW(v);
     539   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
     540   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
     541                 :             : 
     542         [ -  + ]:           1 :     stream.clear();
     543                 :             : 
     544         [ +  - ]:           1 :     stream << HEX(v);
     545   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "\x02"
                   +  - ]
     546                 :             :                                     "0fff");
     547                 :             : 
     548         [ +  - ]:           1 :     v[0].m_base_data = 0;
     549                 :           1 :     v[1].m_base_data = 0;
     550         [ +  - ]:           1 :     stream >> HEX(v);
     551   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
     552   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
     553                 :           1 : }
     554                 :             : 
     555                 :             : constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER};
     556                 :             : constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER};
     557                 :             : 
     558   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(with_params_derived)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     559                 :             : {
     560         [ +  - ]:           1 :     Derived d;
     561                 :           1 :     d.m_base_data = 0x0F;
     562         [ +  - ]:           1 :     d.m_derived_data = "xY";
     563                 :             : 
     564                 :           1 :     DataStream stream;
     565                 :             : 
     566         [ +  - ]:           1 :     stream << RAW_LOWER(d);
     567                 :             : 
     568         [ +  - ]:           1 :     stream << HEX_UPPER(d);
     569                 :             : 
     570   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(stream.str(), "\x0F\x02xy"
                   +  - ]
     571                 :             :                                     "0f\x02XY");
     572                 :           1 : }
     573                 :             : 
     574                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1