LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 97.6 % 375 366
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 20 20
Branches: 49.6 % 1683 835

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2012-2022 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 <streams.h>
       6                 :             : #include <test/util/random.h>
       7                 :             : #include <test/util/setup_common.h>
       8                 :             : #include <util/fs.h>
       9                 :             : #include <util/strencodings.h>
      10                 :             : 
      11                 :             : #include <boost/test/unit_test.hpp>
      12                 :             : 
      13                 :             : using namespace std::string_literals;
      14                 :             : 
      15                 :             : BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
      16                 :             : 
      17   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(xor_file)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      18                 :             : {
      19         [ +  - ]:           2 :     fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"};
      20                 :           6 :     auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }};
      21         [ +  - ]:           1 :     const std::vector<uint8_t> test1{1, 2, 3};
      22         [ +  - ]:           1 :     const std::vector<uint8_t> test2{4, 5};
      23         [ +  - ]:           1 :     const std::vector<std::byte> xor_pat{std::byte{0xff}, std::byte{0x00}};
      24                 :           1 :     {
      25                 :             :         // Check errors for missing file
      26   [ +  -  +  - ]:           1 :         AutoFile xor_file{raw_file("rb"), xor_pat};
      27   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      28   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      29   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      30                 :           0 :     }
      31                 :           1 :     {
      32                 :             : #ifdef __MINGW64__
      33                 :             :         // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
      34                 :             :         const char* mode = "wb";
      35                 :             : #else
      36                 :           1 :         const char* mode = "wbx";
      37                 :             : #endif
      38   [ +  -  +  - ]:           1 :         AutoFile xor_file{raw_file(mode), xor_pat};
      39   [ +  -  +  - ]:           2 :         xor_file << test1 << test2;
      40                 :           0 :     }
      41                 :           1 :     {
      42                 :             :         // Read raw from disk
      43         [ +  - ]:           1 :         AutoFile non_xor_file{raw_file("rb")};
      44         [ +  - ]:           1 :         std::vector<std::byte> raw(7);
      45         [ +  - ]:           1 :         non_xor_file >> Span{raw};
      46   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
                   +  - ]
      47                 :             :         // Check that no padding exists
      48   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      49                 :           1 :     }
      50                 :           1 :     {
      51   [ +  -  +  - ]:           1 :         AutoFile xor_file{raw_file("rb"), xor_pat};
      52                 :           1 :         std::vector<std::byte> read1, read2;
      53   [ +  -  +  - ]:           1 :         xor_file >> read1 >> read2;
      54   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1));
             +  -  +  - ]
      55   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
             +  -  +  - ]
      56                 :             :         // Check that eof was reached
      57   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      58                 :           1 :     }
      59                 :           1 :     {
      60   [ +  -  +  - ]:           1 :         AutoFile xor_file{raw_file("rb"), xor_pat};
      61                 :           1 :         std::vector<std::byte> read2;
      62                 :             :         // Check that ignore works
      63         [ +  - ]:           1 :         xor_file.ignore(4);
      64         [ +  - ]:           1 :         xor_file >> read2;
      65   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
             +  -  +  - ]
      66                 :             :         // Check that ignore and read fail now
      67   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      68   [ +  -  -  +  :           3 :         BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
             +  -  +  - ]
      69                 :           2 :     }
      70                 :           2 : }
      71                 :             : 
      72   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_writer)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      73                 :             : {
      74                 :           1 :     unsigned char a(1);
      75                 :           1 :     unsigned char b(2);
      76                 :           1 :     unsigned char bytes[] = { 3, 4, 5, 6 };
      77                 :           1 :     std::vector<unsigned char> vch;
      78                 :             : 
      79                 :             :     // Each test runs twice. Serializing a second time at the same starting
      80                 :             :     // point should yield the same results, even if the first test grew the
      81                 :             :     // vector.
      82                 :             : 
      83         [ +  - ]:           1 :     VectorWriter{vch, 0, a, b};
      84   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
             +  -  +  - ]
      85         [ +  - ]:           1 :     VectorWriter{vch, 0, a, b};
      86   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
             +  -  +  - ]
      87         [ +  - ]:           1 :     vch.clear();
      88                 :             : 
      89         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      90   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
             +  -  +  - ]
      91         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      92   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
             +  -  +  - ]
      93         [ +  - ]:           1 :     vch.clear();
      94                 :             : 
      95         [ +  - ]:           1 :     vch.resize(5, 0);
      96         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      97   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
             +  -  +  - ]
      98         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      99   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
             +  -  +  - ]
     100         [ +  - ]:           1 :     vch.clear();
     101                 :             : 
     102         [ +  - ]:           1 :     vch.resize(4, 0);
     103         [ +  - ]:           1 :     VectorWriter{vch, 3, a, b};
     104   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
             +  -  +  - ]
     105         [ +  - ]:           1 :     VectorWriter{vch, 3, a, b};
     106   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
             +  -  +  - ]
     107         [ +  - ]:           1 :     vch.clear();
     108                 :             : 
     109         [ +  - ]:           1 :     vch.resize(4, 0);
     110         [ +  - ]:           1 :     VectorWriter{vch, 4, a, b};
     111   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
             +  -  +  - ]
     112         [ +  - ]:           1 :     VectorWriter{vch, 4, a, b};
     113   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
             +  -  +  - ]
     114         [ +  - ]:           1 :     vch.clear();
     115                 :             : 
     116         [ +  - ]:           1 :     VectorWriter{vch, 0, bytes};
     117   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
             +  -  +  - ]
     118         [ +  - ]:           1 :     VectorWriter{vch, 0, bytes};
     119   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
             +  -  +  - ]
     120         [ +  - ]:           1 :     vch.clear();
     121                 :             : 
     122         [ +  - ]:           1 :     vch.resize(4, 8);
     123         [ +  - ]:           1 :     VectorWriter{vch, 2, a, bytes, b};
     124   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
             +  -  +  - ]
     125         [ +  - ]:           1 :     VectorWriter{vch, 2, a, bytes, b};
     126   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
             +  -  +  - ]
     127         [ +  - ]:           2 :     vch.clear();
     128                 :           1 : }
     129                 :             : 
     130   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_reader)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     131                 :             : {
     132                 :           1 :     std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
     133                 :             : 
     134         [ +  - ]:           1 :     SpanReader reader{vch};
     135   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 6U);
     136   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     137                 :             : 
     138                 :             :     // Read a single byte as an unsigned char.
     139                 :           1 :     unsigned char a;
     140         [ +  - ]:           1 :     reader >> a;
     141   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(a, 1);
     142   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 5U);
     143   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     144                 :             : 
     145                 :             :     // Read a single byte as a int8_t.
     146                 :           1 :     int8_t b;
     147         [ +  - ]:           1 :     reader >> b;
     148   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(b, -1);
     149   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 4U);
     150   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     151                 :             : 
     152                 :             :     // Read a 4 bytes as an unsigned int.
     153                 :           1 :     unsigned int c;
     154         [ +  - ]:           1 :     reader >> c;
     155   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c, 100992003U); // 3,4,5,6 in little-endian base-256
     156   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 0U);
     157   [ +  -  +  -  :           2 :     BOOST_CHECK(reader.empty());
                   +  - ]
     158                 :             : 
     159                 :             :     // Reading after end of byte vector throws an error.
     160                 :           1 :     signed int d;
     161   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     162                 :             : 
     163                 :             :     // Read a 4 bytes as a signed int from the beginning of the buffer.
     164         [ +  - ]:           1 :     SpanReader new_reader{vch};
     165         [ +  - ]:           1 :     new_reader >> d;
     166   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
     167   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(new_reader.size(), 2U);
     168   [ +  -  +  -  :           2 :     BOOST_CHECK(!new_reader.empty());
                   +  - ]
     169                 :             : 
     170                 :             :     // Reading after end of byte vector throws an error even if the reader is
     171                 :             :     // not totally empty.
     172   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     173                 :           1 : }
     174                 :             : 
     175   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     176                 :             : {
     177                 :           1 :     std::vector<uint8_t> data{0x82, 0xa7, 0x31};
     178         [ +  - ]:           1 :     SpanReader reader{data};
     179                 :           1 :     uint32_t varint = 0;
     180                 :             :     // Deserialize into r-value
     181         [ +  - ]:           1 :     reader >> VARINT(varint);
     182   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(varint, 54321U);
     183   [ +  -  +  - ]:           2 :     BOOST_CHECK(reader.empty());
     184                 :           1 : }
     185                 :             : 
     186   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     187                 :             : {
     188                 :           1 :     DataStream data{};
     189                 :             : 
     190         [ +  - ]:           1 :     BitStreamWriter bit_writer{data};
     191         [ +  - ]:           1 :     bit_writer.Write(0, 1);
     192         [ +  - ]:           1 :     bit_writer.Write(2, 2);
     193         [ +  - ]:           1 :     bit_writer.Write(6, 3);
     194         [ +  - ]:           1 :     bit_writer.Write(11, 4);
     195         [ +  - ]:           1 :     bit_writer.Write(1, 5);
     196         [ +  - ]:           1 :     bit_writer.Write(32, 6);
     197         [ +  - ]:           1 :     bit_writer.Write(7, 7);
     198         [ +  - ]:           1 :     bit_writer.Write(30497, 16);
     199         [ +  - ]:           1 :     bit_writer.Flush();
     200                 :             : 
     201         [ +  - ]:           1 :     DataStream data_copy{data};
     202                 :           1 :     uint32_t serialized_int1;
     203         [ +  - ]:           1 :     data >> serialized_int1;
     204   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(serialized_int1, uint32_t{0x7700C35A}); // NOTE: Serialized as LE
     205                 :           1 :     uint16_t serialized_int2;
     206         [ +  - ]:           1 :     data >> serialized_int2;
     207   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(serialized_int2, uint16_t{0x1072}); // NOTE: Serialized as LE
     208                 :             : 
     209         [ +  - ]:           1 :     BitStreamReader bit_reader{data_copy};
     210   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U);
                   +  - ]
     211   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U);
                   +  - ]
     212   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U);
                   +  - ]
     213   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U);
                   +  - ]
     214   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U);
                   +  - ]
     215   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U);
                   +  - ]
     216   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U);
                   +  - ]
     217   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U);
                   +  - ]
     218   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     219                 :           1 : }
     220                 :             : 
     221   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     222                 :             : {
     223                 :           1 :     std::vector<std::byte> in;
     224                 :             : 
     225                 :             :     // Degenerate case
     226                 :           1 :     {
     227         [ +  - ]:           1 :         DataStream ds{in};
     228   [ +  -  +  - ]:           1 :         ds.Xor({0x00, 0x00});
     229   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(""s, ds.str());
                   +  - ]
     230                 :           0 :     }
     231                 :             : 
     232         [ +  - ]:           1 :     in.push_back(std::byte{0x0f});
     233         [ +  - ]:           1 :     in.push_back(std::byte{0xf0});
     234                 :             : 
     235                 :             :     // Single character key
     236                 :           1 :     {
     237         [ +  - ]:           1 :         DataStream ds{in};
     238   [ +  -  +  - ]:           1 :         ds.Xor({0xff});
     239   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
                   +  - ]
     240                 :           0 :     }
     241                 :             : 
     242                 :             :     // Multi character key
     243                 :             : 
     244         [ +  - ]:           1 :     in.clear();
     245         [ +  - ]:           1 :     in.push_back(std::byte{0xf0});
     246         [ +  - ]:           1 :     in.push_back(std::byte{0x0f});
     247                 :             : 
     248                 :           1 :     {
     249         [ +  - ]:           1 :         DataStream ds{in};
     250   [ +  -  +  - ]:           1 :         ds.Xor({0xff, 0x0f});
     251   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
                   +  - ]
     252                 :           1 :     }
     253                 :           1 : }
     254                 :             : 
     255   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_buffered_file)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     256                 :             : {
     257         [ +  - ]:           2 :     fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
     258         [ +  - ]:           1 :     AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
     259                 :             : 
     260                 :             :     // The value at each offset is the offset.
     261         [ +  + ]:          41 :     for (uint8_t j = 0; j < 40; ++j) {
     262         [ +  - ]:          80 :         file << j;
     263                 :             :     }
     264         [ +  - ]:           1 :     std::rewind(file.Get());
     265                 :             : 
     266                 :             :     // The buffer size (second arg) must be greater than the rewind
     267                 :             :     // amount (third arg).
     268                 :           1 :     try {
     269         [ -  + ]:           1 :         BufferedFile bfbad{file, 25, 25};
     270   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     271         [ -  + ]:           1 :     } catch (const std::exception& e) {
     272   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     273                 :             :                         "Rewind limit must be less than buffer size") != nullptr);
     274                 :           1 :     }
     275                 :             : 
     276                 :             :     // The buffer is 25 bytes, allow rewinding 10 bytes.
     277         [ +  - ]:           1 :     BufferedFile bf{file, 25, 10};
     278   [ +  -  +  -  :           2 :     BOOST_CHECK(!bf.eof());
                   +  - ]
     279                 :             : 
     280                 :           1 :     uint8_t i;
     281         [ +  - ]:           1 :     bf >> i;
     282   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     283         [ +  - ]:           1 :     bf >> i;
     284   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 1);
     285                 :             : 
     286                 :             :     // After reading bytes 0 and 1, we're positioned at 2.
     287   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 2U);
     288                 :             : 
     289                 :             :     // Rewind to offset 0, ok (within the 10 byte window).
     290   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(0));
             +  -  +  - ]
     291         [ +  - ]:           1 :     bf >> i;
     292   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     293                 :             : 
     294                 :             :     // We can go forward to where we've been, but beyond may fail.
     295   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(2));
             +  -  +  - ]
     296         [ +  - ]:           1 :     bf >> i;
     297   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 2);
     298                 :             : 
     299                 :             :     // If you know the maximum number of bytes that should be
     300                 :             :     // read to deserialize the variable, you can limit the read
     301                 :             :     // extent. The current file offset is 3, so the following
     302                 :             :     // SetLimit() allows zero bytes to be read.
     303   [ +  -  +  -  :           3 :     BOOST_CHECK(bf.SetLimit(3));
             +  -  -  + ]
     304                 :           1 :     try {
     305         [ -  + ]:           1 :         bf >> i;
     306   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     307         [ -  + ]:           1 :     } catch (const std::exception& e) {
     308   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     309                 :             :                            "Attempt to position past buffer limit") != nullptr);
     310                 :           1 :     }
     311                 :             :     // The default argument removes the limit completely.
     312   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.SetLimit());
                   +  - ]
     313                 :             :     // The read position should still be at 3 (no change).
     314   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 3U);
     315                 :             : 
     316                 :             :     // Read from current offset, 3, forward until position 10.
     317         [ +  + ]:           8 :     for (uint8_t j = 3; j < 10; ++j) {
     318         [ +  - ]:           7 :         bf >> i;
     319   [ +  -  +  - ]:           7 :         BOOST_CHECK_EQUAL(i, j);
     320                 :             :     }
     321   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 10U);
     322                 :             : 
     323                 :             :     // We're guaranteed (just barely) to be able to rewind to zero.
     324   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(0));
             +  -  +  - ]
     325   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 0U);
     326         [ +  - ]:           1 :     bf >> i;
     327   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     328                 :             : 
     329                 :             :     // We can set the position forward again up to the farthest
     330                 :             :     // into the stream we've been, but no farther. (Attempting
     331                 :             :     // to go farther may succeed, but it's not guaranteed.)
     332   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(10));
             +  -  +  - ]
     333         [ +  - ]:           1 :     bf >> i;
     334   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 10);
     335   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 11U);
     336                 :             : 
     337                 :             :     // Now it's only guaranteed that we can rewind to offset 1
     338                 :             :     // (current read position, 11, minus rewind amount, 10).
     339   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(1));
             +  -  +  - ]
     340   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 1U);
     341         [ +  - ]:           1 :     bf >> i;
     342   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 1);
     343                 :             : 
     344                 :             :     // We can stream into large variables, even larger than
     345                 :             :     // the buffer size.
     346   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(11));
             +  -  +  - ]
     347                 :           1 :     {
     348                 :           1 :         uint8_t a[40 - 11];
     349         [ +  - ]:           1 :         bf >> a;
     350         [ +  + ]:          30 :         for (uint8_t j = 0; j < sizeof(a); ++j) {
     351   [ +  -  +  - ]:          29 :             BOOST_CHECK_EQUAL(a[j], 11 + j);
     352                 :             :         }
     353                 :             :     }
     354   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
     355                 :             : 
     356                 :             :     // We've read the entire file, the next read should throw.
     357                 :           1 :     try {
     358         [ -  + ]:           1 :         bf >> i;
     359   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     360         [ -  + ]:           1 :     } catch (const std::exception& e) {
     361   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     362                 :             :                         "BufferedFile::Fill: end of file") != nullptr);
     363                 :           1 :     }
     364                 :             :     // Attempting to read beyond the end sets the EOF indicator.
     365   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.eof());
                   +  - ]
     366                 :             : 
     367                 :             :     // Still at offset 40, we can go back 10, to 30.
     368   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
     369   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(30));
             +  -  +  - ]
     370         [ +  - ]:           1 :     bf >> i;
     371   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 30);
     372   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 31U);
     373                 :             : 
     374                 :             :     // We're too far to rewind to position zero.
     375   [ +  -  +  -  :           3 :     BOOST_CHECK(!bf.SetPos(0));
             +  -  +  - ]
     376                 :             :     // But we should now be positioned at least as far back as allowed
     377                 :             :     // by the rewind window (relative to our farthest read position, 40).
     378   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.GetPos() <= 30U);
                   +  - ]
     379                 :             : 
     380                 :             :     // We can explicitly close the file, or the destructor will do it.
     381         [ +  - ]:           1 :     file.fclose();
     382                 :             : 
     383         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     384                 :           3 : }
     385                 :             : 
     386   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     387                 :             : {
     388         [ +  - ]:           2 :     fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
     389         [ +  - ]:           1 :     AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
     390                 :             :     // The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
     391         [ +  + ]:          41 :     for (uint8_t j = 0; j < 40; ++j) {
     392         [ +  - ]:          80 :         file << j;
     393                 :             :     }
     394         [ +  - ]:           1 :     std::rewind(file.Get());
     395                 :             : 
     396                 :             :     // The buffer is 25 bytes, allow rewinding 10 bytes.
     397         [ +  - ]:           1 :     BufferedFile bf{file, 25, 10};
     398                 :             : 
     399                 :           1 :     uint8_t i;
     400                 :             :     // This is like bf >> (7-byte-variable), in that it will cause data
     401                 :             :     // to be read from the file into memory, but it's not copied to us.
     402         [ +  - ]:           1 :     bf.SkipTo(7);
     403   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 7U);
     404         [ +  - ]:           1 :     bf >> i;
     405   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 7);
     406                 :             : 
     407                 :             :     // The bytes in the buffer up to offset 7 are valid and can be read.
     408   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(0));
             +  -  +  - ]
     409         [ +  - ]:           1 :     bf >> i;
     410   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     411         [ +  - ]:           1 :     bf >> i;
     412   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 1);
     413                 :             : 
     414         [ +  - ]:           1 :     bf.SkipTo(11);
     415         [ +  - ]:           1 :     bf >> i;
     416   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 11);
     417                 :             : 
     418                 :             :     // SkipTo() honors the transfer limit; we can't position beyond the limit.
     419         [ +  - ]:           1 :     bf.SetLimit(13);
     420                 :           1 :     try {
     421         [ -  + ]:           1 :         bf.SkipTo(14);
     422   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     423         [ -  + ]:           1 :     } catch (const std::exception& e) {
     424   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(), "Attempt to position past buffer limit") != nullptr);
     425                 :           1 :     }
     426                 :             : 
     427                 :             :     // We can position exactly to the transfer limit.
     428         [ +  - ]:           1 :     bf.SkipTo(13);
     429   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 13U);
     430                 :             : 
     431         [ +  - ]:           1 :     file.fclose();
     432         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     433                 :           3 : }
     434                 :             : 
     435   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     436                 :             : {
     437                 :             :     // Make this test deterministic.
     438                 :           1 :     SeedRandomForTest(SeedRand::ZEROS);
     439                 :             : 
     440         [ +  - ]:           2 :     fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
     441         [ +  + ]:          51 :     for (int rep = 0; rep < 50; ++rep) {
     442         [ +  - ]:          50 :         AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
     443                 :          50 :         size_t fileSize = InsecureRandRange(256);
     444         [ +  + ]:        6363 :         for (uint8_t i = 0; i < fileSize; ++i) {
     445         [ +  - ]:       12626 :             file << i;
     446                 :             :         }
     447         [ +  - ]:          50 :         std::rewind(file.Get());
     448                 :             : 
     449                 :          50 :         size_t bufSize = InsecureRandRange(300) + 1;
     450                 :          50 :         size_t rewindSize = InsecureRandRange(bufSize);
     451         [ +  - ]:          50 :         BufferedFile bf{file, bufSize, rewindSize};
     452                 :          50 :         size_t currentPos = 0;
     453                 :          50 :         size_t maxPos = 0;
     454         [ +  + ]:        3930 :         for (int step = 0; step < 100; ++step) {
     455         [ +  + ]:        3901 :             if (currentPos >= fileSize)
     456                 :             :                 break;
     457                 :             : 
     458                 :             :             // We haven't read to the end of the file yet.
     459   [ +  -  +  -  :        7760 :             BOOST_CHECK(!bf.eof());
                   +  - ]
     460   [ +  -  +  - ]:        3880 :             BOOST_CHECK_EQUAL(bf.GetPos(), currentPos);
     461                 :             : 
     462                 :             :             // Pretend the file consists of a series of objects of varying
     463                 :             :             // sizes; the boundaries of the objects can interact arbitrarily
     464                 :             :             // with the CBufferFile's internal buffer. These first three
     465                 :             :             // cases simulate objects of various sizes (1, 2, 5 bytes).
     466   [ +  +  +  +  :        3880 :             switch (InsecureRandRange(6)) {
                +  +  - ]
     467                 :         644 :             case 0: {
     468                 :         644 :                 uint8_t a[1];
     469         [ -  + ]:         644 :                 if (currentPos + 1 > fileSize)
     470                 :           0 :                     continue;
     471         [ +  - ]:         644 :                 bf.SetLimit(currentPos + 1);
     472         [ +  - ]:         644 :                 bf >> a;
     473         [ +  + ]:        1288 :                 for (uint8_t i = 0; i < 1; ++i) {
     474   [ +  -  +  - ]:         644 :                     BOOST_CHECK_EQUAL(a[i], currentPos);
     475                 :         644 :                     currentPos++;
     476                 :             :                 }
     477                 :             :                 break;
     478                 :             :             }
     479                 :         641 :             case 1: {
     480                 :         641 :                 uint8_t a[2];
     481         [ +  + ]:         641 :                 if (currentPos + 2 > fileSize)
     482                 :           6 :                     continue;
     483         [ +  - ]:         635 :                 bf.SetLimit(currentPos + 2);
     484         [ +  - ]:         635 :                 bf >> a;
     485         [ +  + ]:        1905 :                 for (uint8_t i = 0; i < 2; ++i) {
     486   [ +  -  +  - ]:        1270 :                     BOOST_CHECK_EQUAL(a[i], currentPos);
     487                 :        1270 :                     currentPos++;
     488                 :             :                 }
     489                 :             :                 break;
     490                 :             :             }
     491                 :         666 :             case 2: {
     492                 :         666 :                 uint8_t a[5];
     493         [ +  + ]:         666 :                 if (currentPos + 5 > fileSize)
     494                 :          19 :                     continue;
     495         [ +  - ]:         647 :                 bf.SetLimit(currentPos + 5);
     496         [ +  - ]:         647 :                 bf >> a;
     497         [ +  + ]:        3882 :                 for (uint8_t i = 0; i < 5; ++i) {
     498   [ +  -  +  - ]:        3235 :                     BOOST_CHECK_EQUAL(a[i], currentPos);
     499                 :        3235 :                     currentPos++;
     500                 :             :                 }
     501                 :             :                 break;
     502                 :             :             }
     503                 :         620 :             case 3: {
     504                 :             :                 // SkipTo is similar to the "read" cases above, except
     505                 :             :                 // we don't receive the data.
     506                 :         620 :                 size_t skip_length{static_cast<size_t>(InsecureRandRange(5))};
     507         [ +  + ]:         620 :                 if (currentPos + skip_length > fileSize) continue;
     508         [ +  - ]:         616 :                 bf.SetLimit(currentPos + skip_length);
     509         [ +  - ]:         616 :                 bf.SkipTo(currentPos + skip_length);
     510                 :         616 :                 currentPos += skip_length;
     511                 :         616 :                 break;
     512                 :             :             }
     513                 :         650 :             case 4: {
     514                 :             :                 // Find a byte value (that is at or ahead of the current position).
     515                 :         650 :                 size_t find = currentPos + InsecureRandRange(8);
     516         [ +  + ]:         650 :                 if (find >= fileSize)
     517                 :           8 :                     find = fileSize - 1;
     518         [ +  - ]:         650 :                 bf.FindByte(std::byte(find));
     519                 :             :                 // The value at each offset is the offset.
     520   [ +  -  +  - ]:         650 :                 BOOST_CHECK_EQUAL(bf.GetPos(), find);
     521                 :         650 :                 currentPos = find;
     522                 :             : 
     523         [ +  - ]:         650 :                 bf.SetLimit(currentPos + 1);
     524                 :         650 :                 uint8_t i;
     525         [ +  - ]:         650 :                 bf >> i;
     526   [ +  -  +  - ]:         650 :                 BOOST_CHECK_EQUAL(i, currentPos);
     527                 :         650 :                 currentPos++;
     528                 :         650 :                 break;
     529                 :             :             }
     530                 :         659 :             case 5: {
     531                 :         659 :                 size_t requestPos = InsecureRandRange(maxPos + 4);
     532         [ +  + ]:         659 :                 bool okay = bf.SetPos(requestPos);
     533                 :             :                 // The new position may differ from the requested position
     534                 :             :                 // because we may not be able to rewind beyond the rewind
     535                 :             :                 // window, and we may not be able to move forward beyond the
     536                 :             :                 // farthest position we've reached so far.
     537         [ +  - ]:         659 :                 currentPos = bf.GetPos();
     538   [ +  -  +  - ]:         659 :                 BOOST_CHECK_EQUAL(okay, currentPos == requestPos);
     539                 :             :                 // Check that we can position within the rewind window.
     540                 :         659 :                 if (requestPos <= maxPos &&
     541         [ +  + ]:         659 :                     maxPos > rewindSize &&
     542         [ +  + ]:         327 :                     requestPos >= maxPos - rewindSize) {
     543                 :             :                     // We requested a position within the rewind window.
     544   [ +  -  +  - ]:         288 :                     BOOST_CHECK(okay);
     545                 :             :                 }
     546                 :             :                 break;
     547                 :             :             }
     548                 :             :             }
     549         [ +  + ]:        3851 :             if (maxPos < currentPos)
     550                 :        1413 :                 maxPos = currentPos;
     551                 :             :         }
     552                 :         100 :     }
     553         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     554                 :           1 : }
     555                 :             : 
     556   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_hashed)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     557                 :             : {
     558                 :           1 :     DataStream stream{};
     559         [ +  - ]:           1 :     HashedSourceWriter hash_writer{stream};
     560         [ +  - ]:           1 :     const std::string data{"bitcoin"};
     561         [ +  - ]:           1 :     hash_writer << data;
     562                 :             : 
     563         [ +  - ]:           1 :     HashVerifier hash_verifier{stream};
     564         [ +  - ]:           1 :     std::string result;
     565         [ +  - ]:           1 :     hash_verifier >> result;
     566   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(data, result);
     567   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
             +  -  +  - ]
     568                 :           1 : }
     569                 :             : 
     570                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1