Bitcoin Core  24.99.0
P2P Digital Currency
dbwrapper_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <dbwrapper.h>
7 #include <uint256.h>
8 #include <util/string.h>
9 
10 #include <memory>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 // Test if a string consists entirely of null characters
15 static bool is_null_key(const std::vector<unsigned char>& key) {
16  bool isnull = true;
17 
18  for (unsigned int i = 0; i < key.size(); i++)
19  isnull &= (key[i] == '\x00');
20 
21  return isnull;
22 }
23 
24 BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
25 
27 {
28  // Perform tests both obfuscated and non-obfuscated.
29  for (const bool obfuscate : {false, true}) {
30  fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_obfuscate_true" : "dbwrapper_obfuscate_false");
31  CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
32  uint8_t key{'k'};
33  uint256 in = InsecureRand256();
34  uint256 res;
35 
36  // Ensure that we're doing real obfuscation when obfuscate=true
38 
39  BOOST_CHECK(dbw.Write(key, in));
40  BOOST_CHECK(dbw.Read(key, res));
41  BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
42  }
43 }
44 
45 BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
46 {
47  // Perform tests both obfuscated and non-obfuscated.
48  for (bool obfuscate : {false, true}) {
49  fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
50  CDBWrapper dbw(ph, (1 << 20), false, true, obfuscate);
51 
52  uint256 res;
53  uint32_t res_uint_32;
54  bool res_bool;
55 
56  // Ensure that we're doing real obfuscation when obfuscate=true
58 
59  //Simulate block raw data - "b + block hash"
60  std::string key_block = "b" + InsecureRand256().ToString();
61 
62  uint256 in_block = InsecureRand256();
63  BOOST_CHECK(dbw.Write(key_block, in_block));
64  BOOST_CHECK(dbw.Read(key_block, res));
65  BOOST_CHECK_EQUAL(res.ToString(), in_block.ToString());
66 
67  //Simulate file raw data - "f + file_number"
68  std::string key_file = strprintf("f%04x", InsecureRand32());
69 
70  uint256 in_file_info = InsecureRand256();
71  BOOST_CHECK(dbw.Write(key_file, in_file_info));
72  BOOST_CHECK(dbw.Read(key_file, res));
73  BOOST_CHECK_EQUAL(res.ToString(), in_file_info.ToString());
74 
75  //Simulate transaction raw data - "t + transaction hash"
76  std::string key_transaction = "t" + InsecureRand256().ToString();
77 
78  uint256 in_transaction = InsecureRand256();
79  BOOST_CHECK(dbw.Write(key_transaction, in_transaction));
80  BOOST_CHECK(dbw.Read(key_transaction, res));
81  BOOST_CHECK_EQUAL(res.ToString(), in_transaction.ToString());
82 
83  //Simulate UTXO raw data - "c + transaction hash"
84  std::string key_utxo = "c" + InsecureRand256().ToString();
85 
86  uint256 in_utxo = InsecureRand256();
87  BOOST_CHECK(dbw.Write(key_utxo, in_utxo));
88  BOOST_CHECK(dbw.Read(key_utxo, res));
89  BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
90 
91  //Simulate last block file number - "l"
92  uint8_t key_last_blockfile_number{'l'};
93  uint32_t lastblockfilenumber = InsecureRand32();
94  BOOST_CHECK(dbw.Write(key_last_blockfile_number, lastblockfilenumber));
95  BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
96  BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
97 
98  //Simulate Is Reindexing - "R"
99  uint8_t key_IsReindexing{'R'};
100  bool isInReindexing = InsecureRandBool();
101  BOOST_CHECK(dbw.Write(key_IsReindexing, isInReindexing));
102  BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
103  BOOST_CHECK_EQUAL(isInReindexing, res_bool);
104 
105  //Simulate last block hash up to which UXTO covers - 'B'
106  uint8_t key_lastblockhash_uxto{'B'};
107  uint256 lastblock_hash = InsecureRand256();
108  BOOST_CHECK(dbw.Write(key_lastblockhash_uxto, lastblock_hash));
109  BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
110  BOOST_CHECK_EQUAL(lastblock_hash, res);
111 
112  //Simulate file raw data - "F + filename_number + filename"
113  std::string file_option_tag = "F";
114  uint8_t filename_length = InsecureRandBits(8);
115  std::string filename = "randomfilename";
116  std::string key_file_option = strprintf("%s%01x%s", file_option_tag,filename_length,filename);
117 
118  bool in_file_bool = InsecureRandBool();
119  BOOST_CHECK(dbw.Write(key_file_option, in_file_bool));
120  BOOST_CHECK(dbw.Read(key_file_option, res_bool));
121  BOOST_CHECK_EQUAL(res_bool, in_file_bool);
122  }
123 }
124 
125 // Test batch operations
126 BOOST_AUTO_TEST_CASE(dbwrapper_batch)
127 {
128  // Perform tests both obfuscated and non-obfuscated.
129  for (const bool obfuscate : {false, true}) {
130  fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
131  CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
132 
133  uint8_t key{'i'};
134  uint256 in = InsecureRand256();
135  uint8_t key2{'j'};
136  uint256 in2 = InsecureRand256();
137  uint8_t key3{'k'};
138  uint256 in3 = InsecureRand256();
139 
140  uint256 res;
141  CDBBatch batch(dbw);
142 
143  batch.Write(key, in);
144  batch.Write(key2, in2);
145  batch.Write(key3, in3);
146 
147  // Remove key3 before it's even been written
148  batch.Erase(key3);
149 
150  BOOST_CHECK(dbw.WriteBatch(batch));
151 
152  BOOST_CHECK(dbw.Read(key, res));
153  BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
154  BOOST_CHECK(dbw.Read(key2, res));
155  BOOST_CHECK_EQUAL(res.ToString(), in2.ToString());
156 
157  // key3 should've never been written
158  BOOST_CHECK(dbw.Read(key3, res) == false);
159  }
160 }
161 
162 BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
163 {
164  // Perform tests both obfuscated and non-obfuscated.
165  for (const bool obfuscate : {false, true}) {
166  fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
167  CDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
168 
169  // The two keys are intentionally chosen for ordering
170  uint8_t key{'j'};
171  uint256 in = InsecureRand256();
172  BOOST_CHECK(dbw.Write(key, in));
173  uint8_t key2{'k'};
174  uint256 in2 = InsecureRand256();
175  BOOST_CHECK(dbw.Write(key2, in2));
176 
177  std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
178 
179  // Be sure to seek past the obfuscation key (if it exists)
180  it->Seek(key);
181 
182  uint8_t key_res;
183  uint256 val_res;
184 
185  BOOST_REQUIRE(it->GetKey(key_res));
186  BOOST_REQUIRE(it->GetValue(val_res));
187  BOOST_CHECK_EQUAL(key_res, key);
188  BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
189 
190  it->Next();
191 
192  BOOST_REQUIRE(it->GetKey(key_res));
193  BOOST_REQUIRE(it->GetValue(val_res));
194  BOOST_CHECK_EQUAL(key_res, key2);
195  BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
196 
197  it->Next();
198  BOOST_CHECK_EQUAL(it->Valid(), false);
199  }
200 }
201 
202 // Test that we do not obfuscation if there is existing data.
203 BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
204 {
205  // We're going to share this fs::path between two wrappers
206  fs::path ph = m_args.GetDataDirBase() / "existing_data_no_obfuscate";
208 
209  // Set up a non-obfuscated wrapper to write some initial data.
210  std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false);
211  uint8_t key{'k'};
212  uint256 in = InsecureRand256();
213  uint256 res;
214 
215  BOOST_CHECK(dbw->Write(key, in));
216  BOOST_CHECK(dbw->Read(key, res));
217  BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
218 
219  // Call the destructor to free leveldb LOCK
220  dbw.reset();
221 
222  // Now, set up another wrapper that wants to obfuscate the same directory
223  CDBWrapper odbw(ph, (1 << 10), false, false, true);
224 
225  // Check that the key/val we wrote with unobfuscated wrapper exists and
226  // is readable.
227  uint256 res2;
228  BOOST_CHECK(odbw.Read(key, res2));
229  BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
230 
231  BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
232  BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
233 
234  uint256 in2 = InsecureRand256();
235  uint256 res3;
236 
237  // Check that we can write successfully
238  BOOST_CHECK(odbw.Write(key, in2));
239  BOOST_CHECK(odbw.Read(key, res3));
240  BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
241 }
242 
243 // Ensure that we start obfuscating during a reindex.
244 BOOST_AUTO_TEST_CASE(existing_data_reindex)
245 {
246  // We're going to share this fs::path between two wrappers
247  fs::path ph = m_args.GetDataDirBase() / "existing_data_reindex";
249 
250  // Set up a non-obfuscated wrapper to write some initial data.
251  std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(ph, (1 << 10), false, false, false);
252  uint8_t key{'k'};
253  uint256 in = InsecureRand256();
254  uint256 res;
255 
256  BOOST_CHECK(dbw->Write(key, in));
257  BOOST_CHECK(dbw->Read(key, res));
258  BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
259 
260  // Call the destructor to free leveldb LOCK
261  dbw.reset();
262 
263  // Simulate a -reindex by wiping the existing data store
264  CDBWrapper odbw(ph, (1 << 10), false, true, true);
265 
266  // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
267  uint256 res2;
268  BOOST_CHECK(!odbw.Read(key, res2));
270 
271  uint256 in2 = InsecureRand256();
272  uint256 res3;
273 
274  // Check that we can write successfully
275  BOOST_CHECK(odbw.Write(key, in2));
276  BOOST_CHECK(odbw.Read(key, res3));
277  BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
278 }
279 
280 BOOST_AUTO_TEST_CASE(iterator_ordering)
281 {
282  fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
283  CDBWrapper dbw(ph, (1 << 20), true, false, false);
284  for (int x=0x00; x<256; ++x) {
285  uint8_t key = x;
286  uint32_t value = x*x;
287  if (!(x & 1)) BOOST_CHECK(dbw.Write(key, value));
288  }
289 
290  // Check that creating an iterator creates a snapshot
291  std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
292 
293  for (unsigned int x=0x00; x<256; ++x) {
294  uint8_t key = x;
295  uint32_t value = x*x;
296  if (x & 1) BOOST_CHECK(dbw.Write(key, value));
297  }
298 
299  for (const int seek_start : {0x00, 0x80}) {
300  it->Seek((uint8_t)seek_start);
301  for (unsigned int x=seek_start; x<255; ++x) {
302  uint8_t key;
303  uint32_t value;
304  BOOST_CHECK(it->Valid());
305  if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
306  break;
307  BOOST_CHECK(it->GetKey(key));
308  if (x & 1) {
309  BOOST_CHECK_EQUAL(key, x + 1);
310  continue;
311  }
312  BOOST_CHECK(it->GetValue(value));
313  BOOST_CHECK_EQUAL(key, x);
314  BOOST_CHECK_EQUAL(value, x*x);
315  it->Next();
316  }
317  BOOST_CHECK(!it->Valid());
318  }
319 }
320 
322  // Used to make two serialized objects the same while letting them have different lengths
323  // This is a terrible idea
324  std::string str;
326  explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
327 
328  template<typename Stream>
329  void Serialize(Stream& s) const
330  {
331  for (size_t i = 0; i < str.size(); i++) {
332  s << uint8_t(str[i]);
333  }
334  }
335 
336  template<typename Stream>
337  void Unserialize(Stream& s)
338  {
339  str.clear();
340  uint8_t c{0};
341  while (!s.eof()) {
342  s >> c;
343  str.push_back(c);
344  }
345  }
346 };
347 
348 BOOST_AUTO_TEST_CASE(iterator_string_ordering)
349 {
350  fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
351  CDBWrapper dbw(ph, (1 << 20), true, false, false);
352  for (int x = 0; x < 10; ++x) {
353  for (int y = 0; y < 10; ++y) {
354  std::string key{ToString(x)};
355  for (int z = 0; z < y; ++z)
356  key += key;
357  uint32_t value = x*x;
358  BOOST_CHECK(dbw.Write(StringContentsSerializer{key}, value));
359  }
360  }
361 
362  std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
363  for (const int seek_start : {0, 5}) {
364  it->Seek(StringContentsSerializer{ToString(seek_start)});
365  for (unsigned int x = seek_start; x < 10; ++x) {
366  for (int y = 0; y < 10; ++y) {
367  std::string exp_key{ToString(x)};
368  for (int z = 0; z < y; ++z)
369  exp_key += exp_key;
371  uint32_t value;
372  BOOST_CHECK(it->Valid());
373  if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
374  break;
375  BOOST_CHECK(it->GetKey(key));
376  BOOST_CHECK(it->GetValue(value));
377  BOOST_CHECK_EQUAL(key.str, exp_key);
378  BOOST_CHECK_EQUAL(value, x*x);
379  it->Next();
380  }
381  }
382  BOOST_CHECK(!it->Valid());
383  }
384 }
385 
387 {
388  // Attempt to create a database with a UTF8 character in the path.
389  // On Windows this test will fail if the directory is created using
390  // the ANSI CreateDirectoryA call and the code page isn't UTF8.
391  // It will succeed if created with CreateDirectoryW.
392  fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
393  CDBWrapper dbw(ph, (1 << 20));
394 
395  fs::path lockPath = ph / "LOCK";
396  BOOST_CHECK(fs::exists(lockPath));
397 }
398 
399 
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:64
void Erase(const K &key)
Definition: dbwrapper.h:114
void Write(const K &key, const V &value)
Definition: dbwrapper.h:89
bool WriteBatch(CDBBatch &batch, bool fSync=false)
Definition: dbwrapper.cpp:200
bool Read(const K &key, V &value) const
Definition: dbwrapper.h:248
bool Write(const K &key, const V &value, bool fSync=false)
Definition: dbwrapper.h:274
bool IsEmpty()
Return true if the database managed by this class contains no entries.
Definition: dbwrapper.cpp:247
std::string ToString() const
Definition: uint256.cpp:55
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
256-bit opaque blob.
Definition: uint256.h:105
BOOST_AUTO_TEST_SUITE_END()
static bool is_null_key(const std::vector< unsigned char > &key)
BOOST_AUTO_TEST_CASE(dbwrapper)
const std::vector< unsigned char > & GetObfuscateKey(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
Definition: dbwrapper.cpp:271
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:188
static bool exists(const path &p)
Definition: fs.h:88
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static uint256 InsecureRand256()
Definition: setup_common.h:75
static uint64_t InsecureRandBits(int bits)
Definition: setup_common.h:76
static uint32_t InsecureRand32()
Definition: setup_common.h:74
static bool InsecureRandBool()
Definition: setup_common.h:78
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:109
Basic testing setup.
Definition: setup_common.h:85
StringContentsSerializer()=default
void Serialize(Stream &s) const
StringContentsSerializer(const std::string &inp)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162