@@ -30,18 +30,50 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
30
30
{
31
31
// Perform tests both obfuscated and non-obfuscated.
32
32
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
+ }
38
55
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
+ }
41
61
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
+ }
45
77
}
46
78
}
47
79
@@ -57,7 +89,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
57
89
bool res_bool;
58
90
59
91
// 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)));
61
93
62
94
// Simulate block raw data - "b + block hash"
63
95
std::string key_block = " b" + m_rng.rand256 ().ToString ();
@@ -116,13 +148,13 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
116
148
std::string file_option_tag = " F" ;
117
149
uint8_t filename_length = m_rng.randbits (8 );
118
150
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);
120
152
121
153
bool in_file_bool = m_rng.randbool ();
122
154
BOOST_CHECK (dbw.Write (key_file_option, in_file_bool));
123
155
BOOST_CHECK (dbw.Read (key_file_option, res_bool));
124
156
BOOST_CHECK_EQUAL (res_bool, in_file_bool);
125
- }
157
+ }
126
158
}
127
159
128
160
// Test batch operations
@@ -231,7 +263,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
231
263
BOOST_CHECK (odbw.Read (key, res2));
232
264
BOOST_CHECK_EQUAL (res2.ToString (), in.ToString ());
233
265
234
- BOOST_CHECK (!odbw.IsEmpty ()); // There should be existing data
266
+ BOOST_CHECK (!odbw.IsEmpty ());
235
267
BOOST_CHECK (is_null_key (dbwrapper_private::GetObfuscateKey (odbw))); // The key should be an empty string
236
268
237
269
uint256 in2 = m_rng.rand256 ();
0 commit comments