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.3 % 301 299
Test Date: 2026-06-27 07:07:37 Functions: 100.0 % 53 53
Branches: 50.1 % 1806 904

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

Generated by: LCOV version 2.0-1