LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.3 % 577 567
Test Date: 2026-05-17 07:22:50 Functions: 100.0 % 41 41
Branches: 49.5 % 3001 1486

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

Generated by: LCOV version 2.0-1