LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.3 % 375 365
Test Date: 2024-07-07 05:05:19 Functions: 100.0 % 20 20
Branches: 49.6 % 1657 822

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

Generated by: LCOV version 2.0-1