Bitcoin Core  24.99.0
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2022 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 {
16 BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
17 
18 static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, fs::path& database_filename)
19 {
20  fs::path data_file = BDBDataFile(path);
21  database_filename = data_file.filename();
22  return GetBerkeleyEnv(data_file.parent_path(), false);
23 }
24 
25 BOOST_AUTO_TEST_CASE(getwalletenv_file)
26 {
27  fs::path test_name = "test_name.dat";
28  const fs::path datadir = m_args.GetDataDirNet();
29  fs::path file_path = datadir / test_name;
30  std::ofstream f{file_path};
31  f.close();
32 
33  fs::path 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  fs::path expected_name = "wallet.dat";
42  const fs::path datadir = m_args.GetDataDirNet();
43 
44  fs::path 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 = m_args.GetDataDirNet() / "1";
53  fs::path datadir_2 = m_args.GetDataDirNet() / "2";
54  fs::path 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  fs::path 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
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:295
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
path filename() const
Definition: fs.h:67
BOOST_AUTO_TEST_SUITE_END()
Filesystem operations and types.
Definition: fs.h:20
Definition: node.h:39
static std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &path, fs::path &database_filename)
Definition: db_tests.cpp:18
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:64
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory, bool use_shared_memory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:65
BOOST_AUTO_TEST_CASE(bnb_search_test)
#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:85
ArgsManager gArgs
Definition: system.cpp:73