LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.6 % 466 455
Test Date: 2025-04-28 05:00:59 Functions: 100.0 % 26 26
Branches: 49.4 % 2123 1049

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

Generated by: LCOV version 2.0-1