LCOV - code coverage report
Current view: top level - src/test - dbwrapper_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.3 % 246 237
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 21 21
Branches: 52.0 % 1324 688

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

Generated by: LCOV version 2.0-1