15#include <boost/test/unit_test.hpp>
24 for (
const bool obfuscate : {
false,
true}) {
25 constexpr size_t CACHE_SIZE{1_MiB};
26 const fs::path path{m_args.GetDataDirBase() /
"dbwrapper"};
29 std::vector<std::pair<uint8_t, uint256>> key_values{};
33 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data =
true, .obfuscate = obfuscate}};
40 for (uint8_t
k{0};
k < 10; ++
k) {
43 dbw.Write(key, value);
44 key_values.emplace_back(key, value);
50 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate =
false}};
56 CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
63 for (
const auto& [key, expected_value] : key_values) {
75 for (
bool obfuscate : {
false,
true}) {
76 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_1_obfuscate_true" :
"dbwrapper_1_obfuscate_false");
77 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only =
false, .wipe_data =
true, .obfuscate = obfuscate});
87 std::string key_block =
"b" + m_rng.rand256().ToString();
89 uint256 in_block = m_rng.rand256();
90 dbw.Write(key_block, in_block);
95 std::string key_file =
strprintf(
"f%04x", m_rng.rand32());
97 uint256 in_file_info = m_rng.rand256();
98 dbw.Write(key_file, in_file_info);
103 std::string key_transaction =
"t" + m_rng.rand256().ToString();
105 uint256 in_transaction = m_rng.rand256();
106 dbw.Write(key_transaction, in_transaction);
111 std::string key_utxo =
"c" + m_rng.rand256().ToString();
113 uint256 in_utxo = m_rng.rand256();
114 dbw.Write(key_utxo, in_utxo);
119 uint8_t key_last_blockfile_number{
'l'};
120 uint32_t lastblockfilenumber = m_rng.rand32();
121 dbw.Write(key_last_blockfile_number, lastblockfilenumber);
122 BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
126 uint8_t key_IsReindexing{
'R'};
127 bool isInReindexing = m_rng.randbool();
128 dbw.Write(key_IsReindexing, isInReindexing);
133 uint8_t key_lastblockhash_uxto{
'B'};
134 uint256 lastblock_hash = m_rng.rand256();
135 dbw.Write(key_lastblockhash_uxto, lastblock_hash);
136 BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
140 std::string file_option_tag =
"F";
141 uint8_t filename_length = m_rng.randbits(8);
142 std::string filename =
"randomfilename";
143 std::string key_file_option =
strprintf(
"%s%01x%s", file_option_tag, filename_length, filename);
145 bool in_file_bool = m_rng.randbool();
146 dbw.Write(key_file_option, in_file_bool);
156 for (
const bool obfuscate : {
false,
true}) {
157 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_batch_obfuscate_true" :
"dbwrapper_batch_obfuscate_false");
158 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
170 batch.
Write(key, in);
171 batch.
Write(key2, in2);
172 batch.
Write(key3, in3);
177 dbw.WriteBatch(batch);
192 for (
const bool obfuscate : {
false,
true}) {
193 fs::path ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_iterator_obfuscate_true" :
"dbwrapper_iterator_obfuscate_false");
194 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
202 dbw.Write(key2, in2);
204 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
212 BOOST_REQUIRE(it->GetKey(key_res));
213 BOOST_REQUIRE(it->GetValue(val_res));
219 BOOST_REQUIRE(it->GetKey(key_res));
220 BOOST_REQUIRE(it->GetValue(val_res));
233 fs::path ph = m_args.GetDataDirBase() /
"existing_data_no_obfuscate";
234 fs::create_directories(ph);
237 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
250 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
true});
265 odbw.Write(key, in2);
274 fs::path ph = m_args.GetDataDirBase() /
"existing_data_reindex";
275 fs::create_directories(ph);
278 std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(
DBParams{.
path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
291 CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
true, .obfuscate =
true});
302 odbw.Write(key, in2);
309 fs::path ph = m_args.GetDataDirBase() /
"iterator_ordering";
310 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
311 for (
int x=0x00; x<256; ++x) {
313 uint32_t value = x*x;
314 if (!(x & 1)) dbw.
Write(key, value);
318 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
320 for (
unsigned int x=0x00; x<256; ++x) {
322 uint32_t value = x*x;
323 if (x & 1) dbw.Write(key, value);
326 for (
const int seek_start : {0x00, 0x80}) {
327 it->Seek((uint8_t)seek_start);
328 for (
unsigned int x=seek_start; x<255; ++x) {
355 template<
typename Stream>
358 for (
size_t i = 0; i <
str.size(); i++) {
359 s << uint8_t(
str[i]);
363 template<
typename Stream>
377 fs::path ph = m_args.GetDataDirBase() /
"iterator_string_ordering";
378 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
379 for (
int x = 0; x < 10; ++x) {
380 for (
int y = 0; y < 10; ++y) {
382 for (
int z = 0; z < y; ++z)
384 uint32_t value = x*x;
389 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(dbw).NewIterator());
390 for (
const int seek_start : {0, 5}) {
392 for (
unsigned int x = seek_start; x < 10; ++x) {
393 for (
int y = 0; y < 10; ++y) {
395 for (
int z = 0; z < y; ++z)
419 fs::path ph = m_args.GetDataDirBase() /
"test_runner_₿_🏃_20191128_104644";
420 CDBWrapper dbw({.path = ph, .cache_bytes = 1 << 20});
422 fs::path lockPath = ph /
"LOCK";
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
void Write(const K &key, const V &value, bool fSync=false)
std::string ToString() const
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(dbwrapper)
static bool exists(const path &p)
const Obfuscation & GetObfuscation(const CDBWrapper &w)
Work around circular dependency, as well as for testing in dbwrapper_tests.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
StringContentsSerializer()=default
void Unserialize(Stream &s)
void Serialize(Stream &s) const
StringContentsSerializer(const std::string &inp)