99#include < util/string.h>
1010
1111#include < memory>
12+ #include < ranges>
1213
1314#include < boost/test/unit_test.hpp>
1415
1516using util::ToString;
1617
17- // Test if a string consists entirely of null characters
18- static bool is_null_key (const std::vector<unsigned char >& key) {
19- bool isnull = true ;
20-
21- for (unsigned int i = 0 ; i < key.size (); i++)
22- isnull &= (key[i] == ' \x00 ' );
23-
24- return isnull;
25- }
26-
2718BOOST_FIXTURE_TEST_SUITE (dbwrapper_tests, BasicTestingSetup)
2819
2920BOOST_AUTO_TEST_CASE(dbwrapper)
@@ -33,7 +24,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
3324 constexpr size_t CACHE_SIZE{1_MiB};
3425 const fs::path path{m_args.GetDataDirBase () / " dbwrapper" };
3526
36- std::vector< uint8_t > obfuscation_key{} ;
27+ Obfuscation obfuscation ;
3728 std::vector<std::pair<uint8_t , uint256>> key_values{};
3829
3930 // Write values
@@ -42,8 +33,8 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
4233 BOOST_CHECK_EQUAL (obfuscate, !dbw.IsEmpty ());
4334
4435 // Ensure that we're doing real obfuscation when obfuscate=true
45- obfuscation_key = dbwrapper_private::GetObfuscation (dbw);
46- BOOST_CHECK_EQUAL (obfuscate, ! is_null_key (obfuscation_key ));
36+ obfuscation = dbwrapper_private::GetObfuscation (dbw);
37+ BOOST_CHECK_EQUAL (obfuscate, dbwrapper_private::GetObfuscation (dbw ));
4738
4839 for (uint8_t k{0 }; k < 10 ; ++k) {
4940 uint8_t key{k};
@@ -56,16 +47,16 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
5647 // Verify that the obfuscation key is never obfuscated
5748 {
5849 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false }};
59- BOOST_CHECK (obfuscation_key == dbwrapper_private::GetObfuscation (dbw));
50+ BOOST_CHECK_EQUAL (obfuscation, dbwrapper_private::GetObfuscation (dbw));
6051 }
6152
6253 // Read back the values
6354 {
6455 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
6556
6657 // Ensure obfuscation is read back correctly
67- BOOST_CHECK (obfuscation_key == dbwrapper_private::GetObfuscation (dbw));
68- BOOST_CHECK_EQUAL (obfuscate, ! is_null_key (obfuscation_key ));
58+ BOOST_CHECK_EQUAL (obfuscation, dbwrapper_private::GetObfuscation (dbw));
59+ BOOST_CHECK_EQUAL (obfuscate, dbwrapper_private::GetObfuscation (dbw ));
6960
7061 // Verify all written values
7162 for (const auto & [key, expected_value] : key_values) {
@@ -89,7 +80,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
8980 bool res_bool;
9081
9182 // Ensure that we're doing real obfuscation when obfuscate=true
92- BOOST_CHECK_EQUAL (obfuscate, ! is_null_key ( dbwrapper_private::GetObfuscation (dbw) ));
83+ BOOST_CHECK_EQUAL (obfuscate, dbwrapper_private::GetObfuscation (dbw));
9384
9485 // Simulate block raw data - "b + block hash"
9586 std::string key_block = " b" + m_rng.rand256 ().ToString ();
@@ -264,7 +255,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
264255 BOOST_CHECK_EQUAL (res2.ToString (), in.ToString ());
265256
266257 BOOST_CHECK (!odbw.IsEmpty ());
267- BOOST_CHECK (is_null_key ( dbwrapper_private::GetObfuscation (odbw) )); // The key should be an empty string
258+ BOOST_CHECK (! dbwrapper_private::GetObfuscation (odbw)); // The key should be an empty string
268259
269260 uint256 in2 = m_rng.rand256 ();
270261 uint256 res3;
@@ -301,7 +292,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
301292 // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
302293 uint256 res2;
303294 BOOST_CHECK (!odbw.Read (key, res2));
304- BOOST_CHECK (! is_null_key ( dbwrapper_private::GetObfuscation (odbw) ));
295+ BOOST_CHECK (dbwrapper_private::GetObfuscation (odbw));
305296
306297 uint256 in2 = m_rng.rand256 ();
307298 uint256 res3;
0 commit comments