Bitcoin Core 22.99.0
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2020 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 <memory>
6
7#include <boost/test/unit_test.hpp>
8
9#include <fs.h>
11#include <wallet/bdb.h>
12
13
15
16static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, std::string& database_filename)
17{
18 fs::path data_file = BDBDataFile(path);
19 database_filename = fs::PathToString(data_file.filename());
20 return GetBerkeleyEnv(data_file.parent_path());
21}
22
23BOOST_AUTO_TEST_CASE(getwalletenv_file)
24{
25 std::string test_name = "test_name.dat";
26 const fs::path datadir = gArgs.GetDataDirNet();
27 fs::path file_path = datadir / test_name;
28 fs::ofstream f(file_path);
29 f.close();
30
31 std::string filename;
32 std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
33 BOOST_CHECK_EQUAL(filename, test_name);
34 BOOST_CHECK_EQUAL(env->Directory(), datadir);
35}
36
37BOOST_AUTO_TEST_CASE(getwalletenv_directory)
38{
39 std::string expected_name = "wallet.dat";
40 const fs::path datadir = gArgs.GetDataDirNet();
41
42 std::string filename;
43 std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
44 BOOST_CHECK_EQUAL(filename, expected_name);
45 BOOST_CHECK_EQUAL(env->Directory(), datadir);
46}
47
48BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)
49{
50 fs::path datadir = gArgs.GetDataDirNet() / "1";
51 fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
52 std::string filename;
53
54 std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename);
55 std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename);
56 std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename);
57
58 BOOST_CHECK(env_1 == env_2);
59 BOOST_CHECK(env_2 != env_3);
60}
61
62BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
63{
64 fs::path datadir = gArgs.GetDataDirNet() / "1";
65 fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
66 std::string filename;
67
68 std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename);
69 std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename);
70 env_1_a.reset();
71
72 std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename);
73 std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename);
74
75 BOOST_CHECK(env_1_a != env_1_b);
76 BOOST_CHECK(env_2_a == env_2_b);
77}
78
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:61
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:288
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
BOOST_AUTO_TEST_SUITE_END()
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:58
static std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &path, std::string &database_filename)
Definition: db_tests.cpp:16
BOOST_AUTO_TEST_CASE(getwalletenv_file)
Definition: db_tests.cpp:23
Filesystem operations and types.
Definition: fs.h:19
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
fs::ofstream ofstream
Definition: fs.h:225
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Basic testing setup.
Definition: setup_common.h:76
ArgsManager gArgs
Definition: system.cpp:85