LCOV - code coverage report
Current view: top level - src/test - serialize_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 155 155 100.0 %
Date: 2022-04-21 14:51:19 Functions: 15 15 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 17 22 77.3 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2012-2021 The Bitcoin Core developers
#       2                 :            : // Distributed under the MIT software license, see the accompanying
#       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       4                 :            : 
#       5                 :            : #include <hash.h>
#       6                 :            : #include <serialize.h>
#       7                 :            : #include <streams.h>
#       8                 :            : #include <test/util/setup_common.h>
#       9                 :            : #include <util/strencodings.h>
#      10                 :            : 
#      11                 :            : #include <stdint.h>
#      12                 :            : 
#      13                 :            : #include <boost/test/unit_test.hpp>
#      14                 :            : 
#      15                 :            : BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
#      16                 :            : 
#      17                 :            : class CSerializeMethodsTestSingle
#      18                 :            : {
#      19                 :            : protected:
#      20                 :            :     int intval;
#      21                 :            :     bool boolval;
#      22                 :            :     std::string stringval;
#      23                 :            :     char charstrval[16];
#      24                 :            :     CTransactionRef txval;
#      25                 :            : public:
#      26                 :          4 :     CSerializeMethodsTestSingle() = default;
#      27                 :            :     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)
#      28                 :          4 :     {
#      29                 :          4 :         memcpy(charstrval, charstrvalin, sizeof(charstrval));
#      30                 :          4 :     }
#      31                 :            : 
#      32                 :            :     SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
#      33                 :          6 :     {
#      34                 :          6 :         READWRITE(obj.intval);
#      35                 :          6 :         READWRITE(obj.boolval);
#      36                 :          6 :         READWRITE(obj.stringval);
#      37                 :          6 :         READWRITE(obj.charstrval);
#      38                 :          6 :         READWRITE(obj.txval);
#      39                 :          6 :     }
#      40                 :            : 
#      41                 :            :     bool operator==(const CSerializeMethodsTestSingle& rhs) const
#      42                 :         10 :     {
#      43         [ +  - ]:         10 :         return intval == rhs.intval &&
#      44         [ +  - ]:         10 :                boolval == rhs.boolval &&
#      45         [ +  - ]:         10 :                stringval == rhs.stringval &&
#      46         [ +  - ]:         10 :                strcmp(charstrval, rhs.charstrval) == 0 &&
#      47         [ +  - ]:         10 :                *txval == *rhs.txval;
#      48                 :         10 :     }
#      49                 :            : };
#      50                 :            : 
#      51                 :            : class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
#      52                 :            : {
#      53                 :            : public:
#      54                 :            :     using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
#      55                 :            : 
#      56                 :            :     SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
#      57                 :          4 :     {
#      58                 :          4 :         READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, obj.txval);
#      59                 :          4 :     }
#      60                 :            : };
#      61                 :            : 
#      62                 :            : BOOST_AUTO_TEST_CASE(sizes)
#      63                 :          2 : {
#      64                 :          2 :     BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0, 0));
#      65                 :          2 :     BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0), 0));
#      66                 :          2 :     BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0), 0));
#      67                 :          2 :     BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0), 0));
#      68                 :          2 :     BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0), 0));
#      69                 :          2 :     BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0), 0));
#      70                 :          2 :     BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
#      71                 :          2 :     BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
#      72                 :          2 :     BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
#      73                 :            :     // Bool is serialized as uint8_t
#      74                 :          2 :     BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0), 0));
#      75                 :            : 
#      76                 :            :     // Sanity-check GetSerializeSize and c++ type matching
#      77                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0, 0), 1U);
#      78                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0), 0), 1U);
#      79                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0), 0), 1U);
#      80                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0), 0), 2U);
#      81                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0), 0), 2U);
#      82                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0), 0), 4U);
#      83                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4U);
#      84                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8U);
#      85                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8U);
#      86                 :          2 :     BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1U);
#      87                 :          2 : }
#      88                 :            : 
#      89                 :            : BOOST_AUTO_TEST_CASE(varints)
#      90                 :          2 : {
#      91                 :            :     // encode
#      92                 :            : 
#      93                 :          2 :     CDataStream ss(SER_DISK, 0);
#      94                 :          2 :     CDataStream::size_type size = 0;
#      95         [ +  + ]:     200002 :     for (int i = 0; i < 100000; i++) {
#      96                 :     200000 :         ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
#      97                 :     200000 :         size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED), 0);
#      98                 :     200000 :         BOOST_CHECK(size == ss.size());
#      99                 :     200000 :     }
#     100                 :            : 
#     101         [ +  + ]:        204 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
#     102                 :        202 :         ss << VARINT(i);
#     103                 :        202 :         size += ::GetSerializeSize(VARINT(i), 0);
#     104                 :        202 :         BOOST_CHECK(size == ss.size());
#     105                 :        202 :     }
#     106                 :            : 
#     107                 :            :     // decode
#     108         [ +  + ]:     200002 :     for (int i = 0; i < 100000; i++) {
#     109                 :     200000 :         int j = -1;
#     110                 :     200000 :         ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);
#     111                 :     200000 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
#     112                 :     200000 :     }
#     113                 :            : 
#     114         [ +  + ]:        204 :     for (uint64_t i = 0;  i < 100000000000ULL; i += 999999937) {
#     115                 :        202 :         uint64_t j = std::numeric_limits<uint64_t>::max();
#     116                 :        202 :         ss >> VARINT(j);
#     117                 :        202 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
#     118                 :        202 :     }
#     119                 :          2 : }
#     120                 :            : 
#     121                 :            : BOOST_AUTO_TEST_CASE(varints_bitpatterns)
#     122                 :          2 : {
#     123                 :          2 :     CDataStream ss(SER_DISK, 0);
#     124                 :          2 :     ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
#     125                 :          2 :     ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
#     126                 :          2 :     ss << VARINT_MODE((int8_t)0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
#     127                 :          2 :     ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
#     128                 :          2 :     ss << VARINT((uint8_t)0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
#     129                 :          2 :     ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
#     130                 :          2 :     ss << VARINT_MODE((int16_t)0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
#     131                 :          2 :     ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
#     132                 :          2 :     ss << VARINT((uint16_t)0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
#     133                 :          2 :     ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
#     134                 :          2 :     ss << VARINT_MODE((int32_t)0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
#     135                 :          2 :     ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
#     136                 :          2 :     ss << VARINT((uint32_t)0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
#     137                 :          2 :     ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
#     138                 :          2 :     ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
#     139                 :          2 :     ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
#     140                 :          2 : }
#     141                 :            : 
#     142                 :            : BOOST_AUTO_TEST_CASE(compactsize)
#     143                 :          2 : {
#     144                 :          2 :     CDataStream ss(SER_DISK, 0);
#     145                 :          2 :     std::vector<char>::size_type i, j;
#     146                 :            : 
#     147         [ +  + ]:         54 :     for (i = 1; i <= MAX_SIZE; i *= 2)
#     148                 :         52 :     {
#     149                 :         52 :         WriteCompactSize(ss, i-1);
#     150                 :         52 :         WriteCompactSize(ss, i);
#     151                 :         52 :     }
#     152         [ +  + ]:         54 :     for (i = 1; i <= MAX_SIZE; i *= 2)
#     153                 :         52 :     {
#     154                 :         52 :         j = ReadCompactSize(ss);
#     155                 :         52 :         BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
#     156                 :         52 :         j = ReadCompactSize(ss);
#     157                 :         52 :         BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
#     158                 :         52 :     }
#     159                 :          2 : }
#     160                 :            : 
#     161                 :            : static bool isCanonicalException(const std::ios_base::failure& ex)
#     162                 :         12 : {
#     163                 :         12 :     std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
#     164                 :            : 
#     165                 :            :     // The string returned by what() can be different for different platforms.
#     166                 :            :     // Instead of directly comparing the ex.what() with an expected string,
#     167                 :            :     // create an instance of exception to see if ex.what() matches
#     168                 :            :     // the expected explanatory string returned by the exception instance.
#     169                 :         12 :     return strcmp(expectedException.what(), ex.what()) == 0;
#     170                 :         12 : }
#     171                 :            : 
#     172                 :            : BOOST_AUTO_TEST_CASE(vector_bool)
#     173                 :          2 : {
#     174                 :          2 :     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};
#     175                 :          2 :     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};
#     176                 :            : 
#     177                 :          2 :     BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
#     178                 :          2 :     BOOST_CHECK(SerializeHash(vec1) == SerializeHash(vec2));
#     179                 :          2 : }
#     180                 :            : 
#     181                 :            : BOOST_AUTO_TEST_CASE(noncanonical)
#     182                 :          2 : {
#     183                 :            :     // Write some non-canonical CompactSize encodings, and
#     184                 :            :     // make sure an exception is thrown when read back.
#     185                 :          2 :     CDataStream ss(SER_DISK, 0);
#     186                 :          2 :     std::vector<char>::size_type n;
#     187                 :            : 
#     188                 :            :     // zero encoded with three bytes:
#     189                 :          2 :     ss.write(MakeByteSpan("\xfd\x00\x00").first(3));
#     190                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     191                 :            : 
#     192                 :            :     // 0xfc encoded with three bytes:
#     193                 :          2 :     ss.write(MakeByteSpan("\xfd\xfc\x00").first(3));
#     194                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     195                 :            : 
#     196                 :            :     // 0xfd encoded with three bytes is OK:
#     197                 :          2 :     ss.write(MakeByteSpan("\xfd\xfd\x00").first(3));
#     198                 :          2 :     n = ReadCompactSize(ss);
#     199                 :          2 :     BOOST_CHECK(n == 0xfd);
#     200                 :            : 
#     201                 :            :     // zero encoded with five bytes:
#     202                 :          2 :     ss.write(MakeByteSpan("\xfe\x00\x00\x00\x00").first(5));
#     203                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     204                 :            : 
#     205                 :            :     // 0xffff encoded with five bytes:
#     206                 :          2 :     ss.write(MakeByteSpan("\xfe\xff\xff\x00\x00").first(5));
#     207                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     208                 :            : 
#     209                 :            :     // zero encoded with nine bytes:
#     210                 :          2 :     ss.write(MakeByteSpan("\xff\x00\x00\x00\x00\x00\x00\x00\x00").first(9));
#     211                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     212                 :            : 
#     213                 :            :     // 0x01ffffff encoded with nine bytes:
#     214                 :          2 :     ss.write(MakeByteSpan("\xff\xff\xff\xff\x01\x00\x00\x00\x00").first(9));
#     215                 :          2 :     BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
#     216                 :          2 : }
#     217                 :            : 
#     218                 :            : BOOST_AUTO_TEST_CASE(class_methods)
#     219                 :          2 : {
#     220                 :          2 :     int intval(100);
#     221                 :          2 :     bool boolval(true);
#     222                 :          2 :     std::string stringval("testing");
#     223                 :          2 :     const uint8_t charstrval[16]{"testing charstr"};
#     224                 :          2 :     CMutableTransaction txval;
#     225                 :          2 :     CTransactionRef tx_ref{MakeTransactionRef(txval)};
#     226                 :          2 :     CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
#     227                 :          2 :     CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
#     228                 :          2 :     CSerializeMethodsTestSingle methodtest3;
#     229                 :          2 :     CSerializeMethodsTestMany methodtest4;
#     230                 :          2 :     CDataStream ss(SER_DISK, PROTOCOL_VERSION);
#     231                 :          2 :     BOOST_CHECK(methodtest1 == methodtest2);
#     232                 :          2 :     ss << methodtest1;
#     233                 :          2 :     ss >> methodtest4;
#     234                 :          2 :     ss << methodtest2;
#     235                 :          2 :     ss >> methodtest3;
#     236                 :          2 :     BOOST_CHECK(methodtest1 == methodtest2);
#     237                 :          2 :     BOOST_CHECK(methodtest2 == methodtest3);
#     238                 :          2 :     BOOST_CHECK(methodtest3 == methodtest4);
#     239                 :            : 
#     240                 :          2 :     CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, charstrval, txval);
#     241                 :          2 :     ss2 >> methodtest3;
#     242                 :          2 :     BOOST_CHECK(methodtest3 == methodtest4);
#     243                 :          2 : }
#     244                 :            : 
#     245                 :            : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a