17#include <boost/test/unit_test.hpp>
26 for (
const bool obfuscate : {
false,
true}) {
28 const fs::path path{m_args.GetDataDirBase() /
"dbwrapper"};
31 std::vector<std::pair<uint8_t, uint256>>
key_values{};
45 dbw.Write(key, value);
77 for (
bool obfuscate : {
false,
true}) {
78 fs::path
ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_1_obfuscate_true" :
"dbwrapper_1_obfuscate_false");
79 CDBWrapper dbw({.path =
ph, .cache_bytes = 1
_MiB, .memory_only =
false, .wipe_data =
true, .obfuscate = obfuscate});
89 std::string
key_block =
"b" + m_rng.rand256().ToString();
113 std::string
key_utxo =
"c" + m_rng.rand256().ToString();
144 std::string filename =
"randomfilename";
158 for (
const bool obfuscate : {
false,
true}) {
159 fs::path
ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_batch_obfuscate_true" :
"dbwrapper_batch_obfuscate_false");
160 CDBWrapper dbw({.path =
ph, .cache_bytes = 1
_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
172 batch.
Write(key, in);
179 dbw.WriteBatch(batch);
191 dbw.WriteBatch(batch);
202 for (
const bool obfuscate : {
false,
true}) {
203 const fs::path path{m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_deser_obf" :
"dbwrapper_deser_noobf")};
204 CDBWrapper dbw({.path = path, .cache_bytes = 1 << 20, .wipe_data =
true, .obfuscate = obfuscate});
223 const fs::path path{m_args.GetDataDirBase() /
"dbwrapper_db_error"};
226 const auto make_db = [] (
const fs::path& path,
const bool force_compact) {
227 return CDBWrapper({.path = path, .cache_bytes = 1 << 20, .obfuscate =
false,
228 .options = {.force_compact = force_compact}});
239 for (
const auto& entry : fs::directory_iterator(path)) {
240 if (entry.path().extension() ==
".ldb") {
241 std::ofstream{entry.path(), std::ios::binary | std::ios::trunc}
255 BOOST_CHECK(status.
error().err_msg.find(
"Fatal LevelDB error") != std::string::npos);
262 for (
const bool obfuscate : {
false,
true}) {
263 const fs::path path{m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_tryread_obf" :
"dbwrapper_tryread_noobf")};
264 CDBWrapper dbw({.path = path, .cache_bytes = 1 << 20, .wipe_data =
true, .obfuscate = obfuscate});
305 for (
const bool obfuscate : {
false,
true}) {
306 fs::path
ph = m_args.GetDataDirBase() / (obfuscate ?
"dbwrapper_iterator_obfuscate_true" :
"dbwrapper_iterator_obfuscate_false");
307 CDBWrapper dbw({.path =
ph, .cache_bytes = 1
_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate = obfuscate});
317 std::unique_ptr<CDBIterator> it(
dbw.NewIterator());
368 fs::path
ph = m_args.GetDataDirBase() /
"existing_data_no_obfuscate";
369 fs::create_directories(
ph);
372 std::unique_ptr<CDBWrapper>
dbw = std::make_unique<CDBWrapper>(
DBParams{.
path =
ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
385 CDBWrapper odbw({.path =
ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
true});
409 fs::path
ph = m_args.GetDataDirBase() /
"existing_data_reindex";
410 fs::create_directories(
ph);
413 std::unique_ptr<CDBWrapper>
dbw = std::make_unique<CDBWrapper>(
DBParams{.
path =
ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
false, .obfuscate =
false});
426 CDBWrapper odbw({.path =
ph, .cache_bytes = 1 << 10, .memory_only =
false, .wipe_data =
true, .obfuscate =
true});
444 fs::path
ph = m_args.GetDataDirBase() /
"iterator_ordering";
445 CDBWrapper dbw({.path =
ph, .cache_bytes = 1
_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
446 for (
int x=0x00; x<256; ++x) {
449 if (!(x & 1))
dbw.
Write(key, value);
453 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(
dbw).NewIterator());
455 for (
unsigned int x=0x00; x<256; ++x) {
458 if (x & 1)
dbw.Write(key, value);
490 template<
typename Stream>
493 for (
size_t i = 0; i <
str.size(); i++) {
498 template<
typename Stream>
512 fs::path
ph = m_args.GetDataDirBase() /
"iterator_string_ordering";
513 CDBWrapper dbw({.path =
ph, .cache_bytes = 1
_MiB, .memory_only =
true, .wipe_data =
false, .obfuscate =
false});
514 for (
int x = 0; x < 10; ++x) {
515 for (
int y = 0; y < 10; ++y) {
516 std::string key{ToString(x)};
517 for (
int z = 0; z < y; ++z)
524 std::unique_ptr<CDBIterator> it(
const_cast<CDBWrapper&
>(
dbw).NewIterator());
527 for (
unsigned int x =
seek_start; x < 10; ++x) {
528 for (
int y = 0; y < 10; ++y) {
529 std::string
exp_key{ToString(x)};
530 for (
int z = 0; z < y; ++z)
554 fs::path
ph = m_args.GetDataDirBase() /
"test_runner_₿_🏃_20191128_104644";
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
bool Read(const K &key, V &value) const
Wrapper around TryRead() that preserves the original Read() semantics: returns true on success,...
void Write(const K &key, const V &value, bool fSync=false)
ReadStatus TryRead(const K &key, V &value) const
Read and deserialize a value from the database, with explicit error discrimination.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
std::string ToString() const
The util::Expected class provides a standard way for low-level functions to return either error value...
constexpr const T & value() const &LIFETIMEBOUND
constexpr const E & error() const &noexcept LIFETIMEBOUND
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)
BOOST_CHECK_EXCEPTION(HTTPHeaders{}.Read(reader), std::runtime_error, HasReason{"Empty HTTP header name"})
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)
@ DeserializationError
Key exists but value could not be deserialized.
@ DatabaseError
Unexpected internal DB error.
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)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.