5#include <bitcoin-build-config.h>
7#include <boost/test/unit_test.hpp>
25inline std::ostream&
operator<<(std::ostream& os,
const std::pair<const SerializeData, SerializeData>& kv)
27 std::span key{kv.first}, value{kv.second};
28 os <<
"(\"" << std::string_view{
reinterpret_cast<const char*
>(key.data()), key.size()} <<
"\", \""
29 << std::string_view{
reinterpret_cast<const char*
>(value.data()), value.size()} <<
"\")";
35inline std::span<const std::byte>
StringBytes(std::string_view str)
37 return std::as_bytes(std::span{str});
58 BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end());
64static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(
const fs::path& path,
fs::path& database_filename)
67 database_filename = data_file.
filename();
73 fs::path test_name =
"test_name.dat";
74 const fs::path datadir = m_args.GetDataDirNet();
75 fs::path file_path = datadir / test_name;
76 std::ofstream f{file_path};
80 std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
87 fs::path expected_name =
"wallet.dat";
88 const fs::path datadir = m_args.GetDataDirNet();
91 std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
98 fs::path datadir = m_args.GetDataDirNet() /
"1";
99 fs::path datadir_2 = m_args.GetDataDirNet() /
"2";
102 std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename);
103 std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename);
104 std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename);
116 std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename);
117 std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename);
120 std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename);
121 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));
148 std::vector<std::string> prefixes = {
"",
"FIRST",
"SECOND",
"P\xfe\xff",
"P\xff\x01",
"\xff\xff"};
150 std::unique_ptr<DatabaseBatch>
handler =
Assert(database)->MakeBatch();
156 for (
unsigned int i = 0; i < 10; i++) {
157 for (
const auto&
prefix : prefixes) {
164 for (
const auto&
prefix : prefixes) {
167 std::unique_ptr<DatabaseCursor> cursor =
handler->GetNewPrefixCursor(s_prefix);
170 for (
int i = 0; i < 10; i++) {
174 std::string key_back;
176 key >> key_back >> i_back;
179 unsigned int value_back;
198 const MockableData::value_type
207 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
214 for (
const auto& [
k, v] : {e, p, ps, f,
fs, ff, ffs}) {
215 batch->Write(std::span{
k}, std::span{v});
239 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
240 std::string key =
"key";
241 std::string value =
"value";
242 std::string value2 =
"value_2";
249 std::string read_value;
259 const std::string key =
"key";
260 const std::string key2 =
"key2";
261 const std::string value =
"value";
262 const std::string value2 =
"value_2";
263 auto make_key = [](std::string type, std::string id) {
return std::make_pair(type,
id); };
270 std::unique_ptr<DatabaseBatch> batch = database->MakeBatch();
274 BOOST_CHECK(batch->Write(make_key(key, value), value));
275 BOOST_CHECK(batch->Write(make_key(key, value2), value2));
276 BOOST_CHECK(batch->Write(make_key(key2, value), value));
277 BOOST_CHECK(batch->Write(make_key(value, key), value));
285 BOOST_CHECK(!batch->Exists(make_key(key, value2)));
315 std::unique_ptr<SQLiteDatabase> database =
MakeSQLiteDatabase(m_path_root /
"sqlite", options, status, error);
317 std::string key =
"key";
318 std::string value =
"value";
320 std::unique_ptr<SQLiteBatch> batch = std::make_unique<SQLiteBatch>(*database);
325 batch->SetExecHandler(std::make_unique<DbExecBlocker>(std::set<std::string>{
"ROLLBACK TRANSACTION"}));
334 std::string key2 =
"key2";
335 std::unique_ptr<SQLiteBatch> batch2 = std::make_unique<SQLiteBatch>(*database);
343 std::string key =
"key";
344 std::string value =
"value";
345 std::string value2 =
"value_2";
350 const auto& database =
MakeSQLiteDatabase(m_path_root /
"sqlite", options, status, error);
352 std::unique_ptr<DatabaseBatch>
handler =
Assert(database)->MakeBatch();
361 std::unique_ptr<DatabaseBatch> handler2 =
Assert(database)->MakeBatch();
372 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
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(std::span< const std::byte > prefix)=0
SQliteExecHandler m_base_exec
DbExecBlocker(std::set< std::string > blocked_statements)
int Exec(SQLiteDatabase &database, const std::string &statement) override
std::set< std::string > m_blocked_statements
An instance of this class represents one SQLite3 database.
Class responsible for executing SQL statements in SQLite databases.
virtual int Exec(SQLiteDatabase &database, const std::string &statement)
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 SerializeData StringData(std::string_view str)
std::unique_ptr< SQLiteDatabase > MakeSQLiteDatabase(const fs::path &path, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error)
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 void CheckPrefix(DatabaseBatch &batch, std::span< const std::byte > prefix, MockableData expected)
std::span< const std::byte > StringBytes(std::string_view str)
BOOST_AUTO_TEST_CASE(bnb_search_test)
std::map< SerializeData, SerializeData, std::less<> > MockableData
constexpr int TEST_SQLITE_ERROR
#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.