Skip to content

Commit a5141cd

Browse files
committed
test: make sure dbwrapper obfuscation key is never obfuscated
1 parent 54ab0bd commit a5141cd

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/test/dbwrapper_tests.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,50 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
3030
{
3131
// Perform tests both obfuscated and non-obfuscated.
3232
for (const bool obfuscate : {false, true}) {
33-
fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false");
34-
CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
35-
uint8_t key{'k'};
36-
uint256 in = m_rng.rand256();
37-
uint256 res;
33+
constexpr size_t CACHE_SIZE{1_MiB};
34+
const fs::path path{m_args.GetDataDirBase() / "dbwrapper"};
35+
36+
std::vector<uint8_t> obfuscation_key{};
37+
std::vector<std::pair<uint8_t, uint256>> key_values{};
38+
39+
// Write values
40+
{
41+
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data = true, .obfuscate = obfuscate}};
42+
BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
43+
44+
// Ensure that we're doing real obfuscation when obfuscate=true
45+
obfuscation_key = dbwrapper_private::GetObfuscateKey(dbw);
46+
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
47+
48+
for (uint8_t k{0}; k < 10; ++k) {
49+
uint8_t key{k};
50+
uint256 value{m_rng.rand256()};
51+
BOOST_CHECK(dbw.Write(key, value));
52+
key_values.emplace_back(key, value);
53+
}
54+
}
3855

39-
// Ensure that we're doing real obfuscation when obfuscate=true
40-
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
56+
// Verify that the obfuscation key is never obfuscated
57+
{
58+
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
59+
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
60+
}
4161

42-
BOOST_CHECK(dbw.Write(key, in));
43-
BOOST_CHECK(dbw.Read(key, res));
44-
BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
62+
// Read back the values
63+
{
64+
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
65+
66+
// Ensure obfuscation is read back correctly
67+
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
68+
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
69+
70+
// Verify all written values
71+
for (const auto& [key, expected_value] : key_values) {
72+
uint256 read_value{};
73+
BOOST_CHECK(dbw.Read(key, read_value));
74+
BOOST_CHECK_EQUAL(read_value, expected_value);
75+
}
76+
}
4577
}
4678
}
4779

@@ -57,7 +89,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
5789
bool res_bool;
5890

5991
// Ensure that we're doing real obfuscation when obfuscate=true
60-
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
92+
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
6193

6294
//Simulate block raw data - "b + block hash"
6395
std::string key_block = "b" + m_rng.rand256().ToString();
@@ -116,13 +148,13 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
116148
std::string file_option_tag = "F";
117149
uint8_t filename_length = m_rng.randbits(8);
118150
std::string filename = "randomfilename";
119-
std::string key_file_option = strprintf("%s%01x%s", file_option_tag,filename_length,filename);
151+
std::string key_file_option = strprintf("%s%01x%s", file_option_tag, filename_length, filename);
120152

121153
bool in_file_bool = m_rng.randbool();
122154
BOOST_CHECK(dbw.Write(key_file_option, in_file_bool));
123155
BOOST_CHECK(dbw.Read(key_file_option, res_bool));
124156
BOOST_CHECK_EQUAL(res_bool, in_file_bool);
125-
}
157+
}
126158
}
127159

128160
// Test batch operations
@@ -231,7 +263,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
231263
BOOST_CHECK(odbw.Read(key, res2));
232264
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
233265

234-
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
266+
BOOST_CHECK(!odbw.IsEmpty());
235267
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
236268

237269
uint256 in2 = m_rng.rand256();

0 commit comments

Comments
 (0)