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