dbwrapper_tests.cpp raw

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