mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 07:09:15 +01:00
refactor: encapsulate vector/array keys into Obfuscation
This commit is contained in:
@@ -9,21 +9,12 @@
|
||||
#include <util/string.h>
|
||||
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
using util::ToString;
|
||||
|
||||
// Test if a string consists entirely of null characters
|
||||
static bool is_null_key(const std::vector<unsigned char>& key) {
|
||||
bool isnull = true;
|
||||
|
||||
for (unsigned int i = 0; i < key.size(); i++)
|
||||
isnull &= (key[i] == '\x00');
|
||||
|
||||
return isnull;
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
@@ -33,7 +24,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
constexpr size_t CACHE_SIZE{1_MiB};
|
||||
const fs::path path{m_args.GetDataDirBase() / "dbwrapper"};
|
||||
|
||||
std::vector<uint8_t> obfuscation_key{};
|
||||
Obfuscation obfuscation;
|
||||
std::vector<std::pair<uint8_t, uint256>> key_values{};
|
||||
|
||||
// Write values
|
||||
@@ -42,8 +33,8 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
|
||||
|
||||
// Ensure that we're doing real obfuscation when obfuscate=true
|
||||
obfuscation_key = dbwrapper_private::GetObfuscation(dbw);
|
||||
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
|
||||
obfuscation = dbwrapper_private::GetObfuscation(dbw);
|
||||
BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
|
||||
|
||||
for (uint8_t k{0}; k < 10; ++k) {
|
||||
uint8_t key{k};
|
||||
@@ -56,7 +47,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
// Verify that the obfuscation key is never obfuscated
|
||||
{
|
||||
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
|
||||
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
|
||||
BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
|
||||
}
|
||||
|
||||
// Read back the values
|
||||
@@ -64,8 +55,8 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
|
||||
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
|
||||
|
||||
// Ensure obfuscation is read back correctly
|
||||
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
|
||||
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
|
||||
BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
|
||||
BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
|
||||
|
||||
// Verify all written values
|
||||
for (const auto& [key, expected_value] : key_values) {
|
||||
@@ -89,7 +80,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
|
||||
bool res_bool;
|
||||
|
||||
// Ensure that we're doing real obfuscation when obfuscate=true
|
||||
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscation(dbw)));
|
||||
BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
|
||||
|
||||
//Simulate block raw data - "b + block hash"
|
||||
std::string key_block = "b" + m_rng.rand256().ToString();
|
||||
@@ -264,7 +255,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
|
||||
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
|
||||
|
||||
BOOST_CHECK(!odbw.IsEmpty());
|
||||
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscation(odbw))); // The key should be an empty string
|
||||
BOOST_CHECK(!dbwrapper_private::GetObfuscation(odbw)); // The key should be an empty string
|
||||
|
||||
uint256 in2 = m_rng.rand256();
|
||||
uint256 res3;
|
||||
@@ -301,7 +292,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
|
||||
// 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::GetObfuscation(odbw)));
|
||||
BOOST_CHECK(dbwrapper_private::GetObfuscation(odbw));
|
||||
|
||||
uint256 in2 = m_rng.rand256();
|
||||
uint256 res3;
|
||||
|
||||
Reference in New Issue
Block a user