LCOV - code coverage report
Current view: top level - src/test - dbwrapper_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 95.8 % 260 249
Test Date: 2025-08-01 05:08:13 Functions: 100.0 % 20 20
Branches: 51.9 % 1356 704

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

Generated by: LCOV version 2.0-1