LCOV - code coverage report
Current view: top level - src/test - streams_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.2 % 548 538
Test Date: 2025-12-01 05:03:50 Functions: 100.0 % 37 37
Branches: 49.6 % 2797 1388

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

Generated by: LCOV version 2.0-1