5#include <bitcoin-build-config.h>
7#include <boost/test/unit_test.hpp>
27inline std::ostream&
operator<<(std::ostream& os,
const std::pair<const SerializeData, SerializeData>& kv)
30 os <<
"(\"" << std::string_view{
reinterpret_cast<const char*
>(key.data()), key.size()} <<
"\", \""
31 << std::string_view{
reinterpret_cast<const char*
>(value.data()), value.size()} <<
"\")";
39 return AsBytes<const char>({str.data(), str.size()});
60 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
68 database_filename = data_file.
filename();
74 fs::path test_name =
"test_name.dat";
75 const fs::path datadir = m_args.GetDataDirNet();
76 fs::path file_path = datadir / test_name;
77 std::ofstream f{file_path};
81 std::shared_ptr<BerkeleyEnvironment> env =
GetWalletEnv(file_path, filename);
88 fs::path expected_name =
"wallet.dat";
89 const fs::path datadir = m_args.GetDataDirNet();
92 std::shared_ptr<BerkeleyEnvironment> env =
GetWalletEnv(datadir, filename);
99 fs::path datadir = m_args.GetDataDirNet() /
"1";
100 fs::path datadir_2 = m_args.GetDataDirNet() /
"2";
103 std::shared_ptr<BerkeleyEnvironment> env_1 =
GetWalletEnv(datadir, filename);
104 std::shared_ptr<BerkeleyEnvironment> env_2 =
GetWalletEnv(datadir, filename);
105 std::shared_ptr<BerkeleyEnvironment> env_3 =
GetWalletEnv(datadir_2, filename);
117 std::shared_ptr <BerkeleyEnvironment> env_1_a =
GetWalletEnv(datadir, filename);
118 std::shared_ptr <BerkeleyEnvironment> env_2_a =
GetWalletEnv(datadir_2, filename);
121 std::shared_ptr<BerkeleyEnvironment> env_1_b =
GetWalletEnv(datadir, filename);
122 std::shared_ptr<BerkeleyEnvironment> env_2_b =
GetWalletEnv(datadir_2, filename);
130 std::vector<std::unique_ptr<WalletDatabase>> dbs;
137 dbs.emplace_back(std::make_unique<BerkeleyRODatabase>(
BDBDataFile(path_root /
"bdb"),
false));
150 std::vector<std::string> prefixes = {
"",
"FIRST",
"SECOND",
"P\xfe\xff",
"P\xff\x01",
"\xff\xff"};
152 std::unique_ptr<DatabaseBatch>
handler =
Assert(database)->MakeBatch();
158 for (
unsigned int i = 0; i < 10; i++) {
159 for (
const auto&
prefix : prefixes) {
166 for (
const auto&
prefix : prefixes) {
169 std::unique_ptr<DatabaseCursor> cursor =
handler->GetNewPrefixCursor(s_prefix);
172 for (
int i = 0; i < 10; i++) {
176 std::string key_back;
178 key >> key_back >> i_back;
181 unsigned int value_back;
200 const MockableData::value_type
209 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
216 for (
const auto& [
k, v] : {e, p, ps, f,
fs, ff, ffs}) {
241 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
242 std::string key =
"key";
243 std::string value =
"value";
244 std::string value2 =
"value_2";
251 std::string read_value;
261 const std::string key =
"key";
262 const std::string key2 =
"key2";
263 const std::string value =
"value";
264 const std::string value2 =
"value_2";
265 auto make_key = [](std::string type, std::string id) {
return std::make_pair(type,
id); };
272 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
276 BOOST_CHECK(batch->Write(make_key(key, value), value));
277 BOOST_CHECK(batch->Write(make_key(key, value2), value2));
278 BOOST_CHECK(batch->Write(make_key(key2, value), value));
279 BOOST_CHECK(batch->Write(make_key(value, key), value));
287 BOOST_CHECK(!batch->Exists(make_key(key, value2)));
297constexpr int TEST_SQLITE_ERROR = -999;
299class DbExecBlocker :
public SQliteExecHandler
302 SQliteExecHandler m_base_exec;
303 std::set<std::string> m_blocked_statements;
305 DbExecBlocker(std::set<std::string> blocked_statements) : m_blocked_statements(blocked_statements) {}
306 int Exec(SQLiteDatabase& database,
const std::string& statement)
override {
307 if (m_blocked_statements.contains(statement))
return TEST_SQLITE_ERROR;
308 return m_base_exec.Exec(database, statement);
316 DatabaseOptions options;
319 std::unique_ptr<SQLiteDatabase> database =
MakeSQLiteDatabase(m_path_root /
"sqlite", options, status, error);
321 std::string key =
"key";
322 std::string value =
"value";
324 std::unique_ptr<SQLiteBatch> batch = std::make_unique<SQLiteBatch>(*database);
329 batch->SetExecHandler(std::make_unique<DbExecBlocker>(std::set<std::string>{
"ROLLBACK TRANSACTION"}));
338 std::string key2 =
"key2";
339 std::unique_ptr<SQLiteBatch> batch2 = std::make_unique<SQLiteBatch>(*database);
347 std::string key =
"key";
348 std::string value =
"value";
349 std::string value2 =
"value_2";
351 DatabaseOptions options;
354 const auto& database =
MakeSQLiteDatabase(m_path_root /
"sqlite", options, status, error);
356 std::unique_ptr<DatabaseBatch>
handler =
Assert(database)->MakeBatch();
365 std::unique_ptr<DatabaseBatch> handler2 =
Assert(database)->MakeBatch();
376 std::string read_value;
#define Assert(val)
Identity function.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Double ended buffer combining vector and stream-like interfaces.
const_iterator begin() const
const_iterator end() const
A Span is an object that can refer to a contiguous sequence of objects.
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
A class representing a BerkeleyDB file from which we can only read records.
RAII class that provides access to a WalletDatabase.
virtual std::unique_ptr< DatabaseCursor > GetNewPrefixCursor(Span< const std::byte > prefix)=0
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::ostream & operator<<(std::ostream &os, const std::pair< const SerializeData, SerializeData > &kv)
Filesystem operations and types.
std::unique_ptr< BerkeleyDatabase > MakeBerkeleyDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
Return object giving access to Berkeley database at specified path.
static void CheckPrefix(DatabaseBatch &batch, Span< const std::byte > prefix, MockableData expected)
static SerializeData StringData(std::string_view str)
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
static Span< const std::byte > StringBytes(std::string_view str)
fs::path BDBDataFile(const fs::path &wallet_path)
std::unique_ptr< WalletDatabase > CreateMockableWalletDatabase(MockableData records)
static std::vector< std::unique_ptr< WalletDatabase > > TestDatabases(const fs::path &path_root)
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory, bool use_shared_memory)
Get BerkeleyEnvironment given a directory path.
static std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &path, fs::path &database_filename)
BOOST_AUTO_TEST_CASE(bnb_search_test)
std::map< SerializeData, SerializeData, std::less<> > MockableData
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
bool(* handler)(const std::any &context, HTTPRequest *req, const std::string &strReq)
std::vector< std::byte, zero_after_free_allocator< std::byte > > SerializeData
Byte-vector that clears its contents before deletion.