Changeset View
Changeset View
Standalone View
Standalone View
src/test/dbwrapper_tests.cpp
Show All 20 Lines | bool is_null_key(const std::vector<uint8_t> &key) { | ||||
return isnull; | return isnull; | ||||
} | } | ||||
BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(dbwrapper) { | BOOST_AUTO_TEST_CASE(dbwrapper) { | ||||
// Perform tests both obfuscated and non-obfuscated. | // Perform tests both obfuscated and non-obfuscated. | ||||
for (bool obfuscate : {false, true}) { | for (bool obfuscate : {false, true}) { | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir( | ||||
std::string("dbwrapper").append(obfuscate ? "_true" : "_false")); | |||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | ||||
char key = 'k'; | char key = 'k'; | ||||
uint256 in = InsecureRand256(); | uint256 in = InsecureRand256(); | ||||
uint256 res; | uint256 res; | ||||
// Ensure that we're doing real obfuscation when obfuscate=true | // Ensure that we're doing real obfuscation when obfuscate=true | ||||
BOOST_CHECK(obfuscate != | BOOST_CHECK(obfuscate != | ||||
is_null_key(dbwrapper_private::GetObfuscateKey(dbw))); | is_null_key(dbwrapper_private::GetObfuscateKey(dbw))); | ||||
BOOST_CHECK(dbw.Write(key, in)); | BOOST_CHECK(dbw.Write(key, in)); | ||||
BOOST_CHECK(dbw.Read(key, res)); | BOOST_CHECK(dbw.Read(key, res)); | ||||
BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); | BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); | ||||
} | } | ||||
} | } | ||||
// Test batch operations | // Test batch operations | ||||
BOOST_AUTO_TEST_CASE(dbwrapper_batch) { | BOOST_AUTO_TEST_CASE(dbwrapper_batch) { | ||||
// Perform tests both obfuscated and non-obfuscated. | // Perform tests both obfuscated and non-obfuscated. | ||||
for (bool obfuscate : {false, true}) { | for (bool obfuscate : {false, true}) { | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir(std::string("dbwrapper_batch") | ||||
.append(obfuscate ? "_true" : "_false")); | |||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | ||||
char key = 'i'; | char key = 'i'; | ||||
uint256 in = InsecureRand256(); | uint256 in = InsecureRand256(); | ||||
char key2 = 'j'; | char key2 = 'j'; | ||||
uint256 in2 = InsecureRand256(); | uint256 in2 = InsecureRand256(); | ||||
char key3 = 'k'; | char key3 = 'k'; | ||||
uint256 in3 = InsecureRand256(); | uint256 in3 = InsecureRand256(); | ||||
Show All 18 Lines | for (bool obfuscate : {false, true}) { | ||||
// key3 should've never been written | // key3 should've never been written | ||||
BOOST_CHECK(dbw.Read(key3, res) == false); | BOOST_CHECK(dbw.Read(key3, res) == false); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(dbwrapper_iterator) { | BOOST_AUTO_TEST_CASE(dbwrapper_iterator) { | ||||
// Perform tests both obfuscated and non-obfuscated. | // Perform tests both obfuscated and non-obfuscated. | ||||
for (bool obfuscate : {false, true}) { | for (bool obfuscate : {false, true}) { | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir(std::string("dbwrapper_iterator") | ||||
.append(obfuscate ? "_true" : "_false")); | |||||
CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); | ||||
// The two keys are intentionally chosen for ordering | // The two keys are intentionally chosen for ordering | ||||
char key = 'j'; | char key = 'j'; | ||||
uint256 in = InsecureRand256(); | uint256 in = InsecureRand256(); | ||||
BOOST_CHECK(dbw.Write(key, in)); | BOOST_CHECK(dbw.Write(key, in)); | ||||
char key2 = 'k'; | char key2 = 'k'; | ||||
uint256 in2 = InsecureRand256(); | uint256 in2 = InsecureRand256(); | ||||
Show All 23 Lines | for (bool obfuscate : {false, true}) { | ||||
it->Next(); | it->Next(); | ||||
BOOST_CHECK_EQUAL(it->Valid(), false); | BOOST_CHECK_EQUAL(it->Valid(), false); | ||||
} | } | ||||
} | } | ||||
// Test that we do not obfuscation if there is existing data. | // Test that we do not obfuscation if there is existing data. | ||||
BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) { | BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) { | ||||
// We're going to share this fs::path between two wrappers | // We're going to share this fs::path between two wrappers | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir("existing_data_no_obfuscate"); | ||||
create_directories(ph); | create_directories(ph); | ||||
// Set up a non-obfuscated wrapper to write some initial data. | // Set up a non-obfuscated wrapper to write some initial data. | ||||
std::unique_ptr<CDBWrapper> dbw = | std::unique_ptr<CDBWrapper> dbw = | ||||
std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); | std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); | ||||
char key = 'k'; | char key = 'k'; | ||||
uint256 in = InsecureRand256(); | uint256 in = InsecureRand256(); | ||||
uint256 res; | uint256 res; | ||||
Show All 26 Lines | BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) { | ||||
BOOST_CHECK(odbw.Write(key, in2)); | BOOST_CHECK(odbw.Write(key, in2)); | ||||
BOOST_CHECK(odbw.Read(key, res3)); | BOOST_CHECK(odbw.Read(key, res3)); | ||||
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); | BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); | ||||
} | } | ||||
// Ensure that we start obfuscating during a reindex. | // Ensure that we start obfuscating during a reindex. | ||||
BOOST_AUTO_TEST_CASE(existing_data_reindex) { | BOOST_AUTO_TEST_CASE(existing_data_reindex) { | ||||
// We're going to share this fs::path between two wrappers | // We're going to share this fs::path between two wrappers | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir("existing_data_reindex"); | ||||
create_directories(ph); | create_directories(ph); | ||||
// Set up a non-obfuscated wrapper to write some initial data. | // Set up a non-obfuscated wrapper to write some initial data. | ||||
std::unique_ptr<CDBWrapper> dbw = | std::unique_ptr<CDBWrapper> dbw = | ||||
std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); | std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false); | ||||
char key = 'k'; | char key = 'k'; | ||||
uint256 in = InsecureRand256(); | uint256 in = InsecureRand256(); | ||||
uint256 res; | uint256 res; | ||||
Show All 18 Lines | BOOST_AUTO_TEST_CASE(existing_data_reindex) { | ||||
// Check that we can write successfully | // Check that we can write successfully | ||||
BOOST_CHECK(odbw.Write(key, in2)); | BOOST_CHECK(odbw.Write(key, in2)); | ||||
BOOST_CHECK(odbw.Read(key, res3)); | BOOST_CHECK(odbw.Read(key, res3)); | ||||
BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); | BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(iterator_ordering) { | BOOST_AUTO_TEST_CASE(iterator_ordering) { | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir("iterator_ordering"); | ||||
CDBWrapper dbw(ph, (1 << 20), true, false, false); | CDBWrapper dbw(ph, (1 << 20), true, false, false); | ||||
for (int x = 0x00; x < 256; ++x) { | for (int x = 0x00; x < 256; ++x) { | ||||
uint8_t key = x; | uint8_t key = x; | ||||
uint32_t value = x * x; | uint32_t value = x * x; | ||||
BOOST_CHECK(dbw.Write(key, value)); | BOOST_CHECK(dbw.Write(key, value)); | ||||
} | } | ||||
std::unique_ptr<CDBIterator> it( | std::unique_ptr<CDBIterator> it( | ||||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Lines | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(str[i]); | READWRITE(str[i]); | ||||
} | } | ||||
} | } | ||||
}; | }; | ||||
BOOST_AUTO_TEST_CASE(iterator_string_ordering) { | BOOST_AUTO_TEST_CASE(iterator_string_ordering) { | ||||
char buf[10]; | char buf[10]; | ||||
fs::path ph = fs::temp_directory_path() / fs::unique_path(); | fs::path ph = SetDataDir("iterator_string_ordering"); | ||||
CDBWrapper dbw(ph, (1 << 20), true, false, false); | CDBWrapper dbw(ph, (1 << 20), true, false, false); | ||||
for (int x = 0x00; x < 10; ++x) { | for (int x = 0x00; x < 10; ++x) { | ||||
for (int y = 0; y < 10; y++) { | for (int y = 0; y < 10; y++) { | ||||
snprintf(buf, sizeof(buf), "%d", x); | snprintf(buf, sizeof(buf), "%d", x); | ||||
StringContentsSerializer key(buf); | StringContentsSerializer key(buf); | ||||
for (int z = 0; z < y; z++) | for (int z = 0; z < y; z++) | ||||
key += key; | key += key; | ||||
uint32_t value = x * x; | uint32_t value = x * x; | ||||
Show All 34 Lines |