LCOV - code coverage report
Current view: top level - src/test - dbwrapper_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 96.2 % 260 250
Test Date: 2026-03-16 05:20:51 Functions: 100.0 % 20 20
Branches: 51.9 % 1340 696

             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 <dbwrapper.h>
       6                 :             : #include <test/util/common.h>
       7                 :             : #include <test/util/random.h>
       8                 :             : #include <test/util/setup_common.h>
       9                 :             : #include <uint256.h>
      10                 :             : #include <util/string.h>
      11                 :             : 
      12                 :             : #include <memory>
      13                 :             : #include <ranges>
      14                 :             : 
      15                 :             : #include <boost/test/unit_test.hpp>
      16                 :             : 
      17                 :             : using util::ToString;
      18                 :             : 
      19                 :             : BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
      20                 :             : 
      21   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(dbwrapper)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      22                 :             : {
      23                 :             :     // Perform tests both obfuscated and non-obfuscated.
      24         [ +  + ]:           3 :     for (const bool obfuscate : {false, true}) {
      25                 :           2 :         constexpr size_t CACHE_SIZE{1_MiB};
      26         [ +  - ]:           4 :         const fs::path path{m_args.GetDataDirBase() / "dbwrapper"};
      27                 :             : 
      28                 :           2 :         Obfuscation obfuscation;
      29                 :           2 :         std::vector<std::pair<uint8_t, uint256>> key_values{};
      30                 :             : 
      31                 :             :         // Write values
      32                 :           2 :         {
      33                 :           4 :             CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data = true, .obfuscate = obfuscate}};
      34   [ +  -  +  -  :           2 :             BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
                   +  - ]
      35                 :             : 
      36                 :             :             // Ensure that we're doing real obfuscation when obfuscate=true
      37         [ +  - ]:           2 :             obfuscation = dbwrapper_private::GetObfuscation(dbw);
      38   [ +  -  +  -  :           2 :             BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
                   +  - ]
      39                 :             : 
      40         [ +  + ]:          22 :             for (uint8_t k{0}; k < 10; ++k) {
      41                 :          20 :                 uint8_t key{k};
      42                 :          20 :                 uint256 value{m_rng.rand256()};
      43         [ +  - ]:          20 :                 dbw.Write(key, value);
      44         [ +  - ]:          20 :                 key_values.emplace_back(key, value);
      45                 :             :             }
      46                 :           2 :         }
      47                 :             : 
      48                 :             :         // Verify that the obfuscation key is never obfuscated
      49                 :           2 :         {
      50                 :           0 :             CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
      51   [ +  -  +  -  :           2 :             BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
                   +  - ]
      52                 :           2 :         }
      53                 :             : 
      54                 :             :         // Read back the values
      55                 :           2 :         {
      56                 :           0 :             CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
      57                 :             : 
      58                 :             :             // Ensure obfuscation is read back correctly
      59   [ +  -  +  -  :           2 :             BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
                   +  - ]
      60   [ +  -  +  -  :           2 :             BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
                   +  - ]
      61                 :             : 
      62                 :             :             // Verify all written values
      63   [ +  -  +  + ]:          22 :             for (const auto& [key, expected_value] : key_values) {
      64                 :          20 :                 uint256 read_value{};
      65   [ +  -  +  -  :          40 :                 BOOST_CHECK(dbw.Read(key, read_value));
             +  -  +  - ]
      66   [ +  -  +  - ]:          20 :                 BOOST_CHECK_EQUAL(read_value, expected_value);
      67                 :             :             }
      68                 :           2 :         }
      69                 :           4 :     }
      70   [ +  -  +  -  :           7 : }
          +  -  +  -  +  
                -  +  - ]
      71                 :             : 
      72   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      73                 :             : {
      74                 :             :     // Perform tests both obfuscated and non-obfuscated.
      75         [ +  + ]:           3 :     for (bool obfuscate : {false, true}) {
      76   [ +  +  +  - ]:           5 :         fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
      77                 :           0 :         CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
      78                 :             : 
      79                 :           2 :         uint256 res;
      80                 :           2 :         uint32_t res_uint_32;
      81                 :           2 :         bool res_bool;
      82                 :             : 
      83                 :             :         // Ensure that we're doing real obfuscation when obfuscate=true
      84   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
                   +  - ]
      85                 :             : 
      86                 :             :         //Simulate block raw data - "b + block hash"
      87   [ +  -  +  - ]:           2 :         std::string key_block = "b" + m_rng.rand256().ToString();
      88                 :             : 
      89                 :           2 :         uint256 in_block = m_rng.rand256();
      90         [ +  - ]:           2 :         dbw.Write(key_block, in_block);
      91   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_block, res));
             +  -  +  - ]
      92   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in_block.ToString());
             +  -  +  - ]
      93                 :             : 
      94                 :             :         //Simulate file raw data - "f + file_number"
      95         [ +  - ]:           2 :         std::string key_file = strprintf("f%04x", m_rng.rand32());
      96                 :             : 
      97                 :           2 :         uint256 in_file_info = m_rng.rand256();
      98         [ +  - ]:           2 :         dbw.Write(key_file, in_file_info);
      99   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_file, res));
             +  -  +  - ]
     100   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in_file_info.ToString());
             +  -  +  - ]
     101                 :             : 
     102                 :             :         //Simulate transaction raw data - "t + transaction hash"
     103   [ +  -  +  - ]:           2 :         std::string key_transaction = "t" + m_rng.rand256().ToString();
     104                 :             : 
     105                 :           2 :         uint256 in_transaction = m_rng.rand256();
     106         [ +  - ]:           2 :         dbw.Write(key_transaction, in_transaction);
     107   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_transaction, res));
             +  -  +  - ]
     108   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in_transaction.ToString());
             +  -  +  - ]
     109                 :             : 
     110                 :             :         //Simulate UTXO raw data - "c + transaction hash"
     111   [ +  -  +  - ]:           2 :         std::string key_utxo = "c" + m_rng.rand256().ToString();
     112                 :             : 
     113                 :           2 :         uint256 in_utxo = m_rng.rand256();
     114         [ +  - ]:           2 :         dbw.Write(key_utxo, in_utxo);
     115   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_utxo, res));
             +  -  +  - ]
     116   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
             +  -  +  - ]
     117                 :             : 
     118                 :             :         //Simulate last block file number - "l"
     119                 :           2 :         uint8_t key_last_blockfile_number{'l'};
     120                 :           2 :         uint32_t lastblockfilenumber = m_rng.rand32();
     121         [ +  - ]:           2 :         dbw.Write(key_last_blockfile_number, lastblockfilenumber);
     122   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
             +  -  +  - ]
     123   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
     124                 :             : 
     125                 :             :         //Simulate Is Reindexing - "R"
     126                 :           2 :         uint8_t key_IsReindexing{'R'};
     127                 :           2 :         bool isInReindexing = m_rng.randbool();
     128         [ +  - ]:           2 :         dbw.Write(key_IsReindexing, isInReindexing);
     129   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
             +  -  +  - ]
     130   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(isInReindexing, res_bool);
     131                 :             : 
     132                 :             :         //Simulate last block hash up to which UXTO covers - 'B'
     133                 :           2 :         uint8_t key_lastblockhash_uxto{'B'};
     134                 :           2 :         uint256 lastblock_hash = m_rng.rand256();
     135         [ +  - ]:           2 :         dbw.Write(key_lastblockhash_uxto, lastblock_hash);
     136   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
             +  -  +  - ]
     137   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(lastblock_hash, res);
     138                 :             : 
     139                 :             :         //Simulate file raw data - "F + filename_number + filename"
     140         [ +  - ]:           2 :         std::string file_option_tag = "F";
     141                 :           2 :         uint8_t filename_length = m_rng.randbits(8);
     142         [ +  - ]:           2 :         std::string filename = "randomfilename";
     143         [ +  - ]:           2 :         std::string key_file_option = strprintf("%s%01x%s", file_option_tag, filename_length, filename);
     144                 :             : 
     145                 :           2 :         bool in_file_bool = m_rng.randbool();
     146         [ +  - ]:           2 :         dbw.Write(key_file_option, in_file_bool);
     147   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key_file_option, res_bool));
             +  -  +  - ]
     148   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(res_bool, in_file_bool);
     149                 :           4 :     }
     150   [ +  -  +  - ]:           3 : }
     151                 :             : 
     152                 :             : // Test batch operations
     153   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(dbwrapper_batch)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     154                 :             : {
     155                 :             :     // Perform tests both obfuscated and non-obfuscated.
     156         [ +  + ]:           3 :     for (const bool obfuscate : {false, true}) {
     157   [ +  +  +  - ]:           5 :         fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
     158                 :           0 :         CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
     159                 :             : 
     160                 :           2 :         uint8_t key{'i'};
     161                 :           2 :         uint256 in = m_rng.rand256();
     162                 :           2 :         uint8_t key2{'j'};
     163                 :           2 :         uint256 in2 = m_rng.rand256();
     164                 :           2 :         uint8_t key3{'k'};
     165                 :           2 :         uint256 in3 = m_rng.rand256();
     166                 :             : 
     167                 :           2 :         uint256 res;
     168         [ +  - ]:           2 :         CDBBatch batch(dbw);
     169                 :             : 
     170         [ +  - ]:           2 :         batch.Write(key, in);
     171         [ +  - ]:           2 :         batch.Write(key2, in2);
     172         [ +  - ]:           2 :         batch.Write(key3, in3);
     173                 :             : 
     174                 :             :         // Remove key3 before it's even been written
     175         [ +  - ]:           2 :         batch.Erase(key3);
     176                 :             : 
     177         [ +  - ]:           2 :         dbw.WriteBatch(batch);
     178                 :             : 
     179   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key, res));
             +  -  +  - ]
     180   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
             +  -  +  - ]
     181   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key2, res));
             +  -  +  - ]
     182   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(res.ToString(), in2.ToString());
             +  -  +  - ]
     183                 :             : 
     184                 :             :         // key3 should've never been written
     185   [ +  -  +  -  :           4 :         BOOST_CHECK(dbw.Read(key3, res) == false);
                   +  - ]
     186                 :           4 :     }
     187   [ +  -  +  - ]:           3 : }
     188                 :             : 
     189   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     190                 :             : {
     191                 :             :     // Perform tests both obfuscated and non-obfuscated.
     192         [ +  + ]:           3 :     for (const bool obfuscate : {false, true}) {
     193   [ +  +  +  - ]:           5 :         fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
     194                 :           0 :         CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
     195                 :             : 
     196                 :             :         // The two keys are intentionally chosen for ordering
     197                 :           2 :         uint8_t key{'j'};
     198                 :           2 :         uint256 in = m_rng.rand256();
     199         [ +  - ]:           2 :         dbw.Write(key, in);
     200                 :           2 :         uint8_t key2{'k'};
     201                 :           2 :         uint256 in2 = m_rng.rand256();
     202         [ +  - ]:           2 :         dbw.Write(key2, in2);
     203                 :             : 
     204   [ +  -  +  - ]:           2 :         std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
     205                 :             : 
     206                 :             :         // Be sure to seek past the obfuscation key (if it exists)
     207         [ +  - ]:           2 :         it->Seek(key);
     208                 :             : 
     209                 :           2 :         uint8_t key_res;
     210                 :           2 :         uint256 val_res;
     211                 :             : 
     212   [ +  -  +  -  :           4 :         BOOST_REQUIRE(it->GetKey(key_res));
             +  -  +  - ]
     213   [ +  -  +  -  :           4 :         BOOST_REQUIRE(it->GetValue(val_res));
             +  -  +  - ]
     214   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(key_res, key);
     215   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
             +  -  +  - ]
     216                 :             : 
     217         [ +  - ]:           2 :         it->Next();
     218                 :             : 
     219   [ +  -  +  -  :           4 :         BOOST_REQUIRE(it->GetKey(key_res));
             +  -  +  - ]
     220   [ +  -  +  -  :           4 :         BOOST_REQUIRE(it->GetValue(val_res));
             +  -  +  - ]
     221   [ +  -  +  - ]:           2 :         BOOST_CHECK_EQUAL(key_res, key2);
     222   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
             +  -  +  - ]
     223                 :             : 
     224         [ +  - ]:           2 :         it->Next();
     225   [ +  -  +  -  :           2 :         BOOST_CHECK_EQUAL(it->Valid(), false);
                   +  - ]
     226                 :           4 :     }
     227   [ +  -  +  - ]:           3 : }
     228                 :             : 
     229                 :             : // Test that we do not obfuscation if there is existing data.
     230   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     231                 :             : {
     232                 :             :     // We're going to share this fs::path between two wrappers
     233         [ +  - ]:           2 :     fs::path ph = m_args.GetDataDirBase() / "existing_data_no_obfuscate";
     234         [ +  - ]:           1 :     fs::create_directories(ph);
     235                 :             : 
     236                 :             :     // Set up a non-obfuscated wrapper to write some initial data.
     237   [ +  -  +  - ]:           1 :     std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
     238                 :           1 :     uint8_t key{'k'};
     239                 :           1 :     uint256 in = m_rng.rand256();
     240                 :           1 :     uint256 res;
     241                 :             : 
     242         [ +  - ]:           1 :     dbw->Write(key, in);
     243   [ +  -  +  -  :           2 :     BOOST_CHECK(dbw->Read(key, res));
             +  -  +  - ]
     244   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
             +  -  +  - ]
     245                 :             : 
     246                 :             :     // Call the destructor to free leveldb LOCK
     247         [ +  - ]:           1 :     dbw.reset();
     248                 :             : 
     249                 :             :     // Now, set up another wrapper that wants to obfuscate the same directory
     250                 :           0 :     CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = true});
     251                 :             : 
     252                 :             :     // Check that the key/val we wrote with unobfuscated wrapper exists and
     253                 :             :     // is readable.
     254                 :           1 :     uint256 res2;
     255   [ +  -  +  -  :           2 :     BOOST_CHECK(odbw.Read(key, res2));
             +  -  +  - ]
     256   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
             +  -  +  - ]
     257                 :             : 
     258   [ +  -  +  -  :           2 :     BOOST_CHECK(!odbw.IsEmpty());
             +  -  +  - ]
     259   [ +  -  +  -  :           2 :     BOOST_CHECK(!dbwrapper_private::GetObfuscation(odbw)); // The key should be an empty string
                   +  - ]
     260                 :             : 
     261                 :           1 :     uint256 in2 = m_rng.rand256();
     262                 :           1 :     uint256 res3;
     263                 :             : 
     264                 :             :     // Check that we can write successfully
     265         [ +  - ]:           1 :     odbw.Write(key, in2);
     266   [ +  -  +  -  :           2 :     BOOST_CHECK(odbw.Read(key, res3));
             +  -  +  - ]
     267   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
             +  -  +  - ]
     268   [ +  -  +  - ]:           3 : }
     269                 :             : 
     270                 :             : // Ensure that we start obfuscating during a reindex.
     271   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(existing_data_reindex)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     272                 :             : {
     273                 :             :     // We're going to share this fs::path between two wrappers
     274         [ +  - ]:           2 :     fs::path ph = m_args.GetDataDirBase() / "existing_data_reindex";
     275         [ +  - ]:           1 :     fs::create_directories(ph);
     276                 :             : 
     277                 :             :     // Set up a non-obfuscated wrapper to write some initial data.
     278   [ +  -  +  - ]:           1 :     std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
     279                 :           1 :     uint8_t key{'k'};
     280                 :           1 :     uint256 in = m_rng.rand256();
     281                 :           1 :     uint256 res;
     282                 :             : 
     283         [ +  - ]:           1 :     dbw->Write(key, in);
     284   [ +  -  +  -  :           2 :     BOOST_CHECK(dbw->Read(key, res));
             +  -  +  - ]
     285   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
             +  -  +  - ]
     286                 :             : 
     287                 :             :     // Call the destructor to free leveldb LOCK
     288         [ +  - ]:           1 :     dbw.reset();
     289                 :             : 
     290                 :             :     // Simulate a -reindex by wiping the existing data store
     291                 :           0 :     CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = true, .obfuscate = true});
     292                 :             : 
     293                 :             :     // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
     294                 :           1 :     uint256 res2;
     295   [ +  -  +  -  :           2 :     BOOST_CHECK(!odbw.Read(key, res2));
             +  -  +  - ]
     296   [ +  -  +  -  :           2 :     BOOST_CHECK(dbwrapper_private::GetObfuscation(odbw));
                   +  - ]
     297                 :             : 
     298                 :           1 :     uint256 in2 = m_rng.rand256();
     299                 :           1 :     uint256 res3;
     300                 :             : 
     301                 :             :     // Check that we can write successfully
     302         [ +  - ]:           1 :     odbw.Write(key, in2);
     303   [ +  -  +  -  :           2 :     BOOST_CHECK(odbw.Read(key, res3));
             +  -  +  - ]
     304   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
             +  -  +  - ]
     305   [ +  -  +  - ]:           3 : }
     306                 :             : 
     307   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(iterator_ordering)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     308                 :             : {
     309         [ +  - ]:           2 :     fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
     310                 :           0 :     CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
     311         [ +  + ]:         257 :     for (int x=0x00; x<256; ++x) {
     312                 :         256 :         uint8_t key = x;
     313                 :         256 :         uint32_t value = x*x;
     314   [ +  +  +  - ]:         256 :         if (!(x & 1)) dbw.Write(key, value);
     315                 :             :     }
     316                 :             : 
     317                 :             :     // Check that creating an iterator creates a snapshot
     318         [ +  - ]:           1 :     std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
     319                 :             : 
     320         [ +  + ]:         257 :     for (unsigned int x=0x00; x<256; ++x) {
     321                 :         256 :         uint8_t key = x;
     322                 :         256 :         uint32_t value = x*x;
     323   [ +  +  +  - ]:         256 :         if (x & 1) dbw.Write(key, value);
     324                 :             :     }
     325                 :             : 
     326         [ +  + ]:           3 :     for (const int seek_start : {0x00, 0x80}) {
     327         [ +  - ]:           2 :         it->Seek((uint8_t)seek_start);
     328         [ +  + ]:         384 :         for (unsigned int x=seek_start; x<255; ++x) {
     329                 :         382 :             uint8_t key;
     330                 :         382 :             uint32_t value;
     331   [ +  -  +  -  :         764 :             BOOST_CHECK(it->Valid());
             +  -  +  - ]
     332   [ +  -  +  - ]:         382 :             if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
     333                 :             :                 break;
     334   [ +  -  +  -  :         764 :             BOOST_CHECK(it->GetKey(key));
             +  -  +  + ]
     335         [ +  + ]:         382 :             if (x & 1) {
     336   [ +  -  +  - ]:         190 :                 BOOST_CHECK_EQUAL(key, x + 1);
     337                 :         190 :                 continue;
     338                 :             :             }
     339   [ +  -  +  -  :         384 :             BOOST_CHECK(it->GetValue(value));
             +  -  +  - ]
     340   [ +  -  +  - ]:         192 :             BOOST_CHECK_EQUAL(key, x);
     341   [ +  -  +  - ]:         192 :             BOOST_CHECK_EQUAL(value, x*x);
     342         [ +  - ]:         192 :             it->Next();
     343                 :             :         }
     344   [ +  -  +  -  :           4 :         BOOST_CHECK(!it->Valid());
                   +  - ]
     345                 :             :     }
     346   [ +  -  +  - ]:           3 : }
     347                 :             : 
     348                 :         252 : struct StringContentsSerializer {
     349                 :             :     // Used to make two serialized objects the same while letting them have different lengths
     350                 :             :     // This is a terrible idea
     351                 :             :     std::string str;
     352                 :         150 :     StringContentsSerializer() = default;
     353         [ -  + ]:         104 :     explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
     354                 :             : 
     355                 :             :     template<typename Stream>
     356                 :         102 :     void Serialize(Stream& s) const
     357                 :             :     {
     358   [ -  +  +  + ]:       10334 :         for (size_t i = 0; i < str.size(); i++) {
     359                 :       10232 :             s << uint8_t(str[i]);
     360                 :             :         }
     361                 :         102 :     }
     362                 :             : 
     363                 :             :     template<typename Stream>
     364                 :         150 :     void Unserialize(Stream& s)
     365                 :             :     {
     366                 :         150 :         str.clear();
     367                 :         150 :         uint8_t c{0};
     368   [ -  +  +  + ]:       15495 :         while (!s.empty()) {
     369                 :       15345 :             s >> c;
     370                 :       15345 :             str.push_back(c);
     371                 :             :         }
     372                 :         150 :     }
     373                 :             : };
     374                 :             : 
     375   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(iterator_string_ordering)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     376                 :             : {
     377         [ +  - ]:           2 :     fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
     378                 :           0 :     CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
     379         [ +  + ]:          11 :     for (int x = 0; x < 10; ++x) {
     380         [ +  + ]:         110 :         for (int y = 0; y < 10; ++y) {
     381         [ +  - ]:         100 :             std::string key{ToString(x)};
     382         [ +  + ]:         550 :             for (int z = 0; z < y; ++z)
     383         [ -  + ]:         900 :                 key += key;
     384                 :         100 :             uint32_t value = x*x;
     385   [ -  +  +  - ]:         300 :             dbw.Write(StringContentsSerializer{key}, value);
     386                 :         100 :         }
     387                 :             :     }
     388                 :             : 
     389         [ +  - ]:           1 :     std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
     390         [ +  + ]:           3 :     for (const int seek_start : {0, 5}) {
     391   [ +  -  +  - ]:           6 :         it->Seek(StringContentsSerializer{ToString(seek_start)});
     392         [ +  + ]:          17 :         for (unsigned int x = seek_start; x < 10; ++x) {
     393         [ +  + ]:         165 :             for (int y = 0; y < 10; ++y) {
     394         [ +  - ]:         150 :                 std::string exp_key{ToString(x)};
     395         [ +  + ]:         825 :                 for (int z = 0; z < y; ++z)
     396         [ -  + ]:        1350 :                     exp_key += exp_key;
     397         [ +  - ]:         150 :                 StringContentsSerializer key;
     398                 :         150 :                 uint32_t value;
     399   [ +  -  +  -  :         300 :                 BOOST_CHECK(it->Valid());
             +  -  +  - ]
     400   [ +  -  +  - ]:         150 :                 if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
     401                 :             :                     break;
     402   [ +  -  +  -  :         300 :                 BOOST_CHECK(it->GetKey(key));
             +  -  +  - ]
     403   [ +  -  +  -  :         300 :                 BOOST_CHECK(it->GetValue(value));
             +  -  +  - ]
     404   [ +  -  +  - ]:         150 :                 BOOST_CHECK_EQUAL(key.str, exp_key);
     405   [ +  -  +  - ]:         150 :                 BOOST_CHECK_EQUAL(value, x*x);
     406         [ +  - ]:         150 :                 it->Next();
     407                 :         150 :             }
     408                 :             :         }
     409   [ +  -  +  -  :           4 :         BOOST_CHECK(!it->Valid());
                   +  - ]
     410                 :             :     }
     411   [ +  -  +  - ]:           3 : }
     412                 :             : 
     413   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(unicodepath)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     414                 :             : {
     415                 :             :     // Attempt to create a database with a UTF8 character in the path.
     416                 :             :     // On Windows this test will fail if the directory is created using
     417                 :             :     // the ANSI CreateDirectoryA call and the code page isn't UTF8.
     418                 :             :     // It will succeed if created with CreateDirectoryW.
     419         [ +  - ]:           2 :     fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
     420                 :           0 :     CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20});
     421                 :             : 
     422   [ +  -  +  - ]:           2 :     fs::path lockPath = ph / "LOCK";
     423   [ +  -  +  -  :           2 :     BOOST_CHECK(fs::exists(lockPath));
                   +  - ]
     424   [ +  -  +  - ]:           3 : }
     425                 :             : 
     426                 :             : 
     427                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1