LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.9 % 476 466
Test Date: 2025-07-10 05:20:26 Functions: 100.0 % 26 26
Branches: 49.5 % 2161 1070

             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                 :           1 :     }
      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   [ +  -  +  - ]:           1 :         xor_file << test1 << test2;
      42   [ +  -  +  -  :           2 :         BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0);
                   +  - ]
      43                 :           1 :     }
      44                 :           1 :     {
      45                 :             :         // Read raw from disk
      46   [ +  -  +  - ]:           1 :         AutoFile non_xor_file{raw_file("rb")};
      47         [ +  - ]:           1 :         std::vector<std::byte> raw(7);
      48         [ +  - ]:           1 :         non_xor_file >> std::span{raw};
      49   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
                   +  - ]
      50                 :             :         // Check that no padding exists
      51   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
      52                 :           1 :     }
      53                 :           1 :     {
      54   [ +  -  +  -  :           1 :         AutoFile xor_file{raw_file("rb"), xor_pat};
                   +  - ]
      55                 :           1 :         std::vector<std::byte> read1, read2;
      56   [ +  -  +  - ]:           1 :         xor_file >> read1 >> read2;
      57   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1));
             +  -  +  - ]
      58   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
             +  -  +  - ]
      59                 :             :         // Check that eof was reached
      60   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
      61                 :           1 :     }
      62                 :           1 :     {
      63   [ +  -  +  -  :           1 :         AutoFile xor_file{raw_file("rb"), xor_pat};
                   +  - ]
      64                 :           1 :         std::vector<std::byte> read2;
      65                 :             :         // Check that ignore works
      66         [ +  - ]:           1 :         xor_file.ignore(4);
      67         [ +  - ]:           1 :         xor_file >> read2;
      68   [ +  -  +  -  :           1 :         BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
             +  -  +  - ]
      69                 :             :         // Check that ignore and read fail now
      70   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
      71   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
      72                 :           1 :     }
      73                 :           2 : }
      74                 :             : 
      75   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_writer)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
      76                 :             : {
      77                 :           1 :     unsigned char a(1);
      78                 :           1 :     unsigned char b(2);
      79                 :           1 :     unsigned char bytes[] = { 3, 4, 5, 6 };
      80                 :           1 :     std::vector<unsigned char> vch;
      81                 :             : 
      82                 :             :     // Each test runs twice. Serializing a second time at the same starting
      83                 :             :     // point should yield the same results, even if the first test grew the
      84                 :             :     // vector.
      85                 :             : 
      86         [ +  - ]:           1 :     VectorWriter{vch, 0, a, b};
      87   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
             +  -  +  - ]
      88         [ +  - ]:           1 :     VectorWriter{vch, 0, a, b};
      89   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
             +  -  +  - ]
      90         [ +  - ]:           1 :     vch.clear();
      91                 :             : 
      92         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      93   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
             +  -  +  - ]
      94         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
      95   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
             +  -  +  - ]
      96         [ +  - ]:           1 :     vch.clear();
      97                 :             : 
      98         [ +  - ]:           1 :     vch.resize(5, 0);
      99         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
     100   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
             +  -  +  - ]
     101         [ +  - ]:           1 :     VectorWriter{vch, 2, a, b};
     102   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
             +  -  +  - ]
     103         [ +  - ]:           1 :     vch.clear();
     104                 :             : 
     105         [ +  - ]:           1 :     vch.resize(4, 0);
     106         [ +  - ]:           1 :     VectorWriter{vch, 3, a, b};
     107   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
             +  -  +  - ]
     108         [ +  - ]:           1 :     VectorWriter{vch, 3, a, b};
     109   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
             +  -  +  - ]
     110         [ +  - ]:           1 :     vch.clear();
     111                 :             : 
     112         [ +  - ]:           1 :     vch.resize(4, 0);
     113         [ +  - ]:           1 :     VectorWriter{vch, 4, a, b};
     114   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
             +  -  +  - ]
     115         [ +  - ]:           1 :     VectorWriter{vch, 4, a, b};
     116   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
             +  -  +  - ]
     117         [ +  - ]:           1 :     vch.clear();
     118                 :             : 
     119         [ +  - ]:           1 :     VectorWriter{vch, 0, bytes};
     120   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
             +  -  +  - ]
     121         [ +  - ]:           1 :     VectorWriter{vch, 0, bytes};
     122   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
             +  -  +  - ]
     123         [ +  - ]:           1 :     vch.clear();
     124                 :             : 
     125         [ +  - ]:           1 :     vch.resize(4, 8);
     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 :     VectorWriter{vch, 2, a, bytes, b};
     129   [ +  -  +  -  :           2 :     BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
             +  -  +  - ]
     130         [ +  - ]:           2 :     vch.clear();
     131                 :           1 : }
     132                 :             : 
     133   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_reader)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     134                 :             : {
     135                 :           1 :     std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
     136                 :             : 
     137                 :           1 :     SpanReader reader{vch};
     138   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 6U);
     139   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     140                 :             : 
     141                 :             :     // Read a single byte as an unsigned char.
     142                 :           1 :     unsigned char a;
     143         [ +  - ]:           1 :     reader >> a;
     144   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(a, 1);
     145   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 5U);
     146   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     147                 :             : 
     148                 :             :     // Read a single byte as a int8_t.
     149                 :           1 :     int8_t b;
     150         [ +  - ]:           1 :     reader >> b;
     151   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(b, -1);
     152   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 4U);
     153   [ +  -  +  -  :           2 :     BOOST_CHECK(!reader.empty());
                   +  - ]
     154                 :             : 
     155                 :             :     // Read a 4 bytes as an unsigned int.
     156                 :           1 :     unsigned int c;
     157         [ +  - ]:           1 :     reader >> c;
     158   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(c, 100992003U); // 3,4,5,6 in little-endian base-256
     159   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(reader.size(), 0U);
     160   [ +  -  +  -  :           2 :     BOOST_CHECK(reader.empty());
                   +  - ]
     161                 :             : 
     162                 :             :     // Reading after end of byte vector throws an error.
     163                 :           1 :     signed int d;
     164   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     165                 :             : 
     166                 :             :     // Read a 4 bytes as a signed int from the beginning of the buffer.
     167                 :           1 :     SpanReader new_reader{vch};
     168         [ +  - ]:           1 :     new_reader >> d;
     169   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
     170   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(new_reader.size(), 2U);
     171   [ +  -  +  -  :           2 :     BOOST_CHECK(!new_reader.empty());
                   +  - ]
     172                 :             : 
     173                 :             :     // Reading after end of byte vector throws an error even if the reader is
     174                 :             :     // not totally empty.
     175   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     176                 :           1 : }
     177                 :             : 
     178   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     179                 :             : {
     180                 :           1 :     std::vector<uint8_t> data{0x82, 0xa7, 0x31};
     181                 :           1 :     SpanReader reader{data};
     182                 :           1 :     uint32_t varint = 0;
     183                 :             :     // Deserialize into r-value
     184         [ +  - ]:           1 :     reader >> VARINT(varint);
     185   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(varint, 54321U);
     186   [ +  -  +  - ]:           2 :     BOOST_CHECK(reader.empty());
     187                 :           1 : }
     188                 :             : 
     189   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     190                 :             : {
     191                 :           1 :     DataStream data{};
     192                 :             : 
     193         [ +  - ]:           1 :     BitStreamWriter bit_writer{data};
     194         [ +  - ]:           1 :     bit_writer.Write(0, 1);
     195         [ +  - ]:           1 :     bit_writer.Write(2, 2);
     196         [ +  - ]:           1 :     bit_writer.Write(6, 3);
     197         [ +  - ]:           1 :     bit_writer.Write(11, 4);
     198         [ +  - ]:           1 :     bit_writer.Write(1, 5);
     199         [ +  - ]:           1 :     bit_writer.Write(32, 6);
     200         [ +  - ]:           1 :     bit_writer.Write(7, 7);
     201         [ +  - ]:           1 :     bit_writer.Write(30497, 16);
     202         [ +  - ]:           1 :     bit_writer.Flush();
     203                 :             : 
     204         [ +  - ]:           1 :     DataStream data_copy{data};
     205                 :           1 :     uint32_t serialized_int1;
     206         [ +  - ]:           1 :     data >> serialized_int1;
     207   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(serialized_int1, uint32_t{0x7700C35A}); // NOTE: Serialized as LE
     208                 :           1 :     uint16_t serialized_int2;
     209         [ +  - ]:           1 :     data >> serialized_int2;
     210   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(serialized_int2, uint16_t{0x1072}); // NOTE: Serialized as LE
     211                 :             : 
     212         [ +  - ]:           1 :     BitStreamReader bit_reader{data_copy};
     213   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U);
                   +  - ]
     214   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U);
                   +  - ]
     215   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U);
                   +  - ]
     216   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U);
                   +  - ]
     217   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U);
                   +  - ]
     218   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U);
                   +  - ]
     219   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U);
                   +  - ]
     220   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U);
                   +  - ]
     221   [ +  -  -  +  :           2 :     BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
          -  -  -  -  -  
             +  +  -  +  
                      - ]
     222                 :           1 : }
     223                 :             : 
     224   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     225                 :             : {
     226                 :           1 :     std::vector<std::byte> in;
     227                 :             : 
     228                 :             :     // Degenerate case
     229                 :           1 :     {
     230         [ +  - ]:           1 :         DataStream ds{in};
     231   [ +  -  +  - ]:           1 :         ds.Xor({0x00, 0x00});
     232   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(""s, ds.str());
                   +  - ]
     233                 :           0 :     }
     234                 :             : 
     235         [ +  - ]:           1 :     in.push_back(std::byte{0x0f});
     236         [ +  - ]:           1 :     in.push_back(std::byte{0xf0});
     237                 :             : 
     238                 :             :     // Single character key
     239                 :           1 :     {
     240         [ +  - ]:           1 :         DataStream ds{in};
     241   [ +  -  +  - ]:           1 :         ds.Xor({0xff});
     242   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
                   +  - ]
     243                 :           0 :     }
     244                 :             : 
     245                 :             :     // Multi character key
     246                 :             : 
     247         [ +  - ]:           1 :     in.clear();
     248         [ +  - ]:           1 :     in.push_back(std::byte{0xf0});
     249         [ +  - ]:           1 :     in.push_back(std::byte{0x0f});
     250                 :             : 
     251                 :           1 :     {
     252         [ +  - ]:           1 :         DataStream ds{in};
     253   [ +  -  +  - ]:           1 :         ds.Xor({0xff, 0x0f});
     254   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
                   +  - ]
     255                 :           1 :     }
     256                 :           1 : }
     257                 :             : 
     258   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_buffered_file)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     259                 :             : {
     260         [ +  - ]:           2 :     fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
     261   [ +  -  +  - ]:           1 :     AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
     262                 :             : 
     263                 :             :     // The value at each offset is the offset.
     264         [ +  + ]:          41 :     for (uint8_t j = 0; j < 40; ++j) {
     265         [ +  - ]:          80 :         file << j;
     266                 :             :     }
     267         [ +  - ]:           1 :     file.seek(0, SEEK_SET);
     268                 :             : 
     269                 :             :     // The buffer size (second arg) must be greater than the rewind
     270                 :             :     // amount (third arg).
     271                 :           1 :     try {
     272         [ -  + ]:           1 :         BufferedFile bfbad{file, 25, 25};
     273   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     274         [ -  + ]:           1 :     } catch (const std::exception& e) {
     275   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     276                 :             :                         "Rewind limit must be less than buffer size") != nullptr);
     277                 :           1 :     }
     278                 :             : 
     279                 :             :     // The buffer is 25 bytes, allow rewinding 10 bytes.
     280         [ +  - ]:           1 :     BufferedFile bf{file, 25, 10};
     281   [ +  -  +  -  :           2 :     BOOST_CHECK(!bf.eof());
                   +  - ]
     282                 :             : 
     283                 :           1 :     uint8_t i;
     284         [ +  - ]:           1 :     bf >> i;
     285   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     286         [ +  - ]:           1 :     bf >> i;
     287   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 1);
     288                 :             : 
     289                 :             :     // After reading bytes 0 and 1, we're positioned at 2.
     290   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 2U);
     291                 :             : 
     292                 :             :     // Rewind to offset 0, ok (within the 10 byte window).
     293   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(0));
             +  -  +  - ]
     294         [ +  - ]:           1 :     bf >> i;
     295   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     296                 :             : 
     297                 :             :     // We can go forward to where we've been, but beyond may fail.
     298   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(2));
             +  -  +  - ]
     299         [ +  - ]:           1 :     bf >> i;
     300   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 2);
     301                 :             : 
     302                 :             :     // If you know the maximum number of bytes that should be
     303                 :             :     // read to deserialize the variable, you can limit the read
     304                 :             :     // extent. The current file offset is 3, so the following
     305                 :             :     // SetLimit() allows zero bytes to be read.
     306   [ +  -  +  -  :           3 :     BOOST_CHECK(bf.SetLimit(3));
             +  -  -  + ]
     307                 :           1 :     try {
     308         [ -  + ]:           1 :         bf >> i;
     309   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     310         [ -  + ]:           1 :     } catch (const std::exception& e) {
     311   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     312                 :             :                            "Attempt to position past buffer limit") != nullptr);
     313                 :           1 :     }
     314                 :             :     // The default argument removes the limit completely.
     315   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.SetLimit());
                   +  - ]
     316                 :             :     // The read position should still be at 3 (no change).
     317   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 3U);
     318                 :             : 
     319                 :             :     // Read from current offset, 3, forward until position 10.
     320         [ +  + ]:           8 :     for (uint8_t j = 3; j < 10; ++j) {
     321         [ +  - ]:           7 :         bf >> i;
     322   [ +  -  +  - ]:           7 :         BOOST_CHECK_EQUAL(i, j);
     323                 :             :     }
     324   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 10U);
     325                 :             : 
     326                 :             :     // We're guaranteed (just barely) to be able to rewind to zero.
     327   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(0));
             +  -  +  - ]
     328   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 0U);
     329         [ +  - ]:           1 :     bf >> i;
     330   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 0);
     331                 :             : 
     332                 :             :     // We can set the position forward again up to the farthest
     333                 :             :     // into the stream we've been, but no farther. (Attempting
     334                 :             :     // to go farther may succeed, but it's not guaranteed.)
     335   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(10));
             +  -  +  - ]
     336         [ +  - ]:           1 :     bf >> i;
     337   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 10);
     338   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 11U);
     339                 :             : 
     340                 :             :     // Now it's only guaranteed that we can rewind to offset 1
     341                 :             :     // (current read position, 11, minus rewind amount, 10).
     342   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(1));
             +  -  +  - ]
     343   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 1U);
     344         [ +  - ]:           1 :     bf >> i;
     345   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 1);
     346                 :             : 
     347                 :             :     // We can stream into large variables, even larger than
     348                 :             :     // the buffer size.
     349   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(11));
             +  -  +  - ]
     350                 :           1 :     {
     351                 :           1 :         uint8_t a[40 - 11];
     352         [ +  - ]:           1 :         bf >> a;
     353         [ +  + ]:          30 :         for (uint8_t j = 0; j < sizeof(a); ++j) {
     354   [ +  -  +  - ]:          29 :             BOOST_CHECK_EQUAL(a[j], 11 + j);
     355                 :             :         }
     356                 :             :     }
     357   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
     358                 :             : 
     359                 :             :     // We've read the entire file, the next read should throw.
     360                 :           1 :     try {
     361         [ -  + ]:           1 :         bf >> i;
     362   [ #  #  #  # ]:           0 :         BOOST_CHECK(false);
     363         [ -  + ]:           1 :     } catch (const std::exception& e) {
     364   [ +  -  +  - ]:           2 :         BOOST_CHECK(strstr(e.what(),
     365                 :             :                         "BufferedFile::Fill: end of file") != nullptr);
     366                 :           1 :     }
     367                 :             :     // Attempting to read beyond the end sets the EOF indicator.
     368   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.eof());
                   +  - ]
     369                 :             : 
     370                 :             :     // Still at offset 40, we can go back 10, to 30.
     371   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
     372   [ +  -  -  +  :           3 :     BOOST_CHECK(bf.SetPos(30));
             +  -  +  - ]
     373         [ +  - ]:           1 :     bf >> i;
     374   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(i, 30);
     375   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(bf.GetPos(), 31U);
     376                 :             : 
     377                 :             :     // We're too far to rewind to position zero.
     378   [ +  -  +  -  :           3 :     BOOST_CHECK(!bf.SetPos(0));
             +  -  +  - ]
     379                 :             :     // But we should now be positioned at least as far back as allowed
     380                 :             :     // by the rewind window (relative to our farthest read position, 40).
     381   [ +  -  +  -  :           2 :     BOOST_CHECK(bf.GetPos() <= 30U);
                   +  - ]
     382                 :             : 
     383   [ +  -  +  -  :           2 :     BOOST_REQUIRE_EQUAL(file.fclose(), 0);
                   +  - ]
     384                 :             : 
     385         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     386                 :           2 : }
     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   [ +  -  +  -  :           2 :     BOOST_REQUIRE_EQUAL(file.fclose(), 0);
                   +  - ]
     434         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     435                 :           2 : }
     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 :         BOOST_REQUIRE_EQUAL(file.fclose(), 0);
                   +  - ]
     555                 :          50 :     }
     556         [ +  - ]:           1 :     fs::remove(streams_test_filename);
     557                 :           1 : }
     558                 :             : 
     559   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(buffered_reader_matches_autofile_random_content)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     560                 :             : {
     561                 :           1 :     const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
     562                 :           1 :     const size_t buf_size{1 + m_rng.randrange(file_size)};
     563                 :           1 :     const FlatFilePos pos{0, 0};
     564                 :             : 
     565         [ +  - ]:           1 :     const FlatFileSeq test_file{m_args.GetDataDirBase(), "buffered_file_test_random", node::BLOCKFILE_CHUNK_SIZE};
     566                 :           1 :     const std::vector obfuscation{m_rng.randbytes<std::byte>(8)};
     567                 :             : 
     568                 :             :     // Write out the file with random content
     569                 :           1 :     {
     570   [ +  -  +  -  :           1 :         AutoFile f{test_file.Open(pos, /*read_only=*/false), obfuscation};
                   +  - ]
     571         [ +  - ]:           1 :         f.write(m_rng.randbytes<std::byte>(file_size));
     572   [ +  -  +  -  :           2 :         BOOST_REQUIRE_EQUAL(f.fclose(), 0);
                   +  - ]
     573                 :           1 :     }
     574   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(fs::file_size(test_file.FileName(pos)), file_size);
             +  -  +  - ]
     575                 :             : 
     576                 :           1 :     {
     577   [ +  -  +  -  :           1 :         AutoFile direct_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
                   +  - ]
     578                 :             : 
     579   [ +  -  +  -  :           1 :         AutoFile buffered_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
                   +  - ]
     580         [ +  - ]:           1 :         BufferedReader buffered_reader{std::move(buffered_file), buf_size};
     581                 :             : 
     582         [ +  + ]:          13 :         for (size_t total_read{0}; total_read < file_size;) {
     583   [ +  +  +  +  :          13 :             const size_t read{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_read))};
                   +  - ]
     584                 :             : 
     585         [ +  - ]:          12 :             DataBuffer direct_file_buffer{read};
     586         [ +  - ]:          12 :             direct_file.read(direct_file_buffer);
     587                 :             : 
     588         [ +  - ]:          12 :             DataBuffer buffered_buffer{read};
     589         [ +  - ]:          12 :             buffered_reader.read(buffered_buffer);
     590                 :             : 
     591   [ +  -  +  -  :          24 :             BOOST_CHECK_EQUAL_COLLECTIONS(
                   +  - ]
     592                 :             :                 direct_file_buffer.begin(), direct_file_buffer.end(),
     593                 :             :                 buffered_buffer.begin(), buffered_buffer.end()
     594                 :             :             );
     595                 :             : 
     596                 :          12 :             total_read += read;
     597                 :          12 :         }
     598                 :             : 
     599                 :           1 :         {
     600         [ +  - ]:           1 :             DataBuffer excess_byte{1};
     601   [ +  -  -  +  :           2 :             BOOST_CHECK_EXCEPTION(direct_file.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     602                 :           0 :         }
     603                 :             : 
     604                 :           1 :         {
     605         [ +  - ]:           1 :             DataBuffer excess_byte{1};
     606   [ +  -  -  +  :           2 :             BOOST_CHECK_EXCEPTION(buffered_reader.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     607                 :           1 :         }
     608                 :           1 :     }
     609                 :             : 
     610   [ +  -  +  - ]:           2 :     fs::remove(test_file.FileName(pos));
     611                 :           2 : }
     612                 :             : 
     613   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(buffered_writer_matches_autofile_random_content)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     614                 :             : {
     615                 :           1 :     const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
     616                 :           1 :     const size_t buf_size{1 + m_rng.randrange(file_size)};
     617                 :           1 :     const FlatFilePos pos{0, 0};
     618                 :             : 
     619         [ +  - ]:           1 :     const FlatFileSeq test_buffered{m_args.GetDataDirBase(), "buffered_write_test", node::BLOCKFILE_CHUNK_SIZE};
     620   [ +  -  +  - ]:           1 :     const FlatFileSeq test_direct{m_args.GetDataDirBase(), "direct_write_test", node::BLOCKFILE_CHUNK_SIZE};
     621                 :           1 :     const std::vector obfuscation{m_rng.randbytes<std::byte>(8)};
     622                 :             : 
     623                 :           1 :     {
     624                 :           1 :         DataBuffer test_data{m_rng.randbytes<std::byte>(file_size)};
     625                 :             : 
     626   [ +  -  +  -  :           1 :         AutoFile direct_file{test_direct.Open(pos, /*read_only=*/false), obfuscation};
                   +  - ]
     627                 :             : 
     628   [ +  -  +  -  :           1 :         AutoFile buffered_file{test_buffered.Open(pos, /*read_only=*/false), obfuscation};
                   +  - ]
     629                 :           1 :         {
     630         [ +  - ]:           1 :             BufferedWriter buffered{buffered_file, buf_size};
     631                 :             : 
     632         [ +  + ]:          13 :             for (size_t total_written{0}; total_written < file_size;) {
     633   [ +  +  +  +  :          13 :                 const size_t write_size{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_written))};
                   +  - ]
     634                 :             : 
     635         [ -  + ]:          12 :                 auto current_span = std::span{test_data}.subspan(total_written, write_size);
     636         [ +  - ]:          12 :                 direct_file.write(current_span);
     637         [ +  - ]:          12 :                 buffered.write(current_span);
     638                 :             : 
     639                 :          12 :                 total_written += write_size;
     640                 :             :             }
     641                 :           0 :         }
     642   [ +  -  +  -  :           2 :         BOOST_REQUIRE_EQUAL(buffered_file.fclose(), 0);
                   +  - ]
     643   [ +  -  +  -  :           2 :         BOOST_REQUIRE_EQUAL(direct_file.fclose(), 0);
                   +  - ]
     644                 :           1 :     }
     645                 :             : 
     646                 :             :     // Compare the resulting files
     647         [ +  - ]:           1 :     DataBuffer direct_result{file_size};
     648                 :           1 :     {
     649   [ +  -  +  -  :           1 :         AutoFile verify_direct{test_direct.Open(pos, /*read_only=*/true), obfuscation};
                   +  - ]
     650         [ +  - ]:           1 :         verify_direct.read(direct_result);
     651                 :             : 
     652         [ +  - ]:           1 :         DataBuffer excess_byte{1};
     653   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(verify_direct.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     654                 :           1 :     }
     655                 :             : 
     656         [ +  - ]:           1 :     DataBuffer buffered_result{file_size};
     657                 :           1 :     {
     658   [ +  -  +  -  :           1 :         AutoFile verify_buffered{test_buffered.Open(pos, /*read_only=*/true), obfuscation};
                   +  - ]
     659         [ +  - ]:           1 :         verify_buffered.read(buffered_result);
     660                 :             : 
     661         [ +  - ]:           1 :         DataBuffer excess_byte{1};
     662   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(verify_buffered.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     663                 :           1 :     }
     664                 :             : 
     665   [ +  -  +  -  :           2 :     BOOST_CHECK_EQUAL_COLLECTIONS(
             +  -  +  - ]
     666                 :             :         direct_result.begin(), direct_result.end(),
     667                 :             :         buffered_result.begin(), buffered_result.end()
     668                 :             :     );
     669                 :             : 
     670   [ +  -  +  - ]:           1 :     fs::remove(test_direct.FileName(pos));
     671   [ +  -  +  - ]:           2 :     fs::remove(test_buffered.FileName(pos));
     672                 :           3 : }
     673                 :             : 
     674   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(buffered_writer_reader)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     675                 :             : {
     676                 :           1 :     const uint32_t v1{m_rng.rand32()}, v2{m_rng.rand32()}, v3{m_rng.rand32()};
     677         [ +  - ]:           2 :     const fs::path test_file{m_args.GetDataDirBase() / "test_buffered_write_read.bin"};
     678                 :             : 
     679                 :             :     // Write out the values through a precisely sized BufferedWriter
     680   [ +  -  +  - ]:           1 :     AutoFile file{fsbridge::fopen(test_file, "w+b")};
     681                 :           1 :     {
     682         [ +  - ]:           1 :         BufferedWriter f(file, sizeof(v1) + sizeof(v2) + sizeof(v3));
     683   [ +  -  +  - ]:           1 :         f << v1 << v2;
     684         [ +  - ]:           1 :         f.write(std::as_bytes(std::span{&v3, 1}));
     685                 :           0 :     }
     686   [ +  -  +  -  :           2 :     BOOST_REQUIRE_EQUAL(file.fclose(), 0);
                   +  - ]
     687                 :             : 
     688                 :             :     // Read back and verify using BufferedReader
     689                 :           1 :     {
     690                 :           1 :         uint32_t _v1{0}, _v2{0}, _v3{0};
     691   [ +  -  +  - ]:           1 :         AutoFile file{fsbridge::fopen(test_file, "rb")};
     692         [ +  - ]:           1 :         BufferedReader f(std::move(file), sizeof(v1) + sizeof(v2) + sizeof(v3));
     693   [ +  -  +  - ]:           1 :         f >> _v1 >> _v2;
     694         [ +  - ]:           1 :         f.read(std::as_writable_bytes(std::span{&_v3, 1}));
     695   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(_v1, v1);
     696   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(_v2, v2);
     697   [ +  -  +  - ]:           1 :         BOOST_CHECK_EQUAL(_v3, v3);
     698                 :             : 
     699         [ +  - ]:           1 :         DataBuffer excess_byte{1};
     700   [ +  -  -  +  :           2 :         BOOST_CHECK_EXCEPTION(f.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
          -  -  -  -  -  
          +  +  -  +  -  
                   +  - ]
     701                 :           1 :     }
     702                 :             : 
     703         [ +  - ]:           1 :     fs::remove(test_file);
     704                 :           2 : }
     705                 :             : 
     706   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(streams_hashed)
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                      - ]
     707                 :             : {
     708                 :           1 :     DataStream stream{};
     709         [ +  - ]:           1 :     HashedSourceWriter hash_writer{stream};
     710         [ +  - ]:           1 :     const std::string data{"bitcoin"};
     711         [ +  - ]:           1 :     hash_writer << data;
     712                 :             : 
     713         [ +  - ]:           1 :     HashVerifier hash_verifier{stream};
     714         [ +  - ]:           1 :     std::string result;
     715         [ +  - ]:           1 :     hash_verifier >> result;
     716   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(data, result);
     717   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
             +  -  +  - ]
     718                 :           1 : }
     719                 :             : 
     720                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1