LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.1 % 518 508
Test Date: 2025-08-13 04:26:42 Functions: 100.0 % 35 35
Branches: 49.7 % 2627 1306

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

Generated by: LCOV version 2.0-1