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>
10 #include <test/util/setup_common.h>
11 #include <wallet/bdb.h>
12 
13 
15 
16 static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, std::string& database_filename)
17 {
18  fs::path data_file = BDBDataFile(path);
19  database_filename = data_file.filename().string();
20  return GetBerkeleyEnv(data_file.parent_path());
21 }
22 
23 BOOST_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 #if BOOST_VERSION >= 107700
29  std::ofstream f(BOOST_FILESYSTEM_C_STR(file_path));
30 #else
31  std::ofstream f(file_path.BOOST_FILESYSTEM_C_STR);
32 #endif // BOOST_VERSION >= 107700
33  f.close();
34 
35  std::string filename;
36  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
37  BOOST_CHECK_EQUAL(filename, test_name);
38  BOOST_CHECK_EQUAL(env->Directory(), datadir);
39 }
40 
41 BOOST_AUTO_TEST_CASE(getwalletenv_directory)
42 {
43  std::string expected_name = "wallet.dat";
44  const fs::path datadir = gArgs.GetDataDirNet();
45 
46  std::string filename;
47  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
48  BOOST_CHECK_EQUAL(filename, expected_name);
49  BOOST_CHECK_EQUAL(env->Directory(), datadir);
50 }
51 
52 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)
53 {
54  fs::path datadir = gArgs.GetDataDirNet() / "1";
55  fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
56  std::string filename;
57 
58  std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename);
59  std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename);
60  std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename);
61 
62  BOOST_CHECK(env_1 == env_2);
63  BOOST_CHECK(env_2 != env_3);
64 }
65 
66 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
67 {
68  fs::path datadir = gArgs.GetDataDirNet() / "1";
69  fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
70  std::string filename;
71 
72  std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename);
73  std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename);
74  env_1_a.reset();
75 
76  std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename);
77  std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename);
78 
79  BOOST_CHECK(env_1_a != env_1_b);
80  BOOST_CHECK(env_2_a == env_2_b);
81 }
82 
fs.h
ArgsManager::GetDataDirNet
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: system.h:282
setup_common.h
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()
fsbridge::ofstream
fs::ofstream ofstream
Definition: fs.h:102
BDBDataFile
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:61
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
gArgs
ArgsManager gArgs
Definition: system.cpp:84
GetBerkeleyEnv
std::shared_ptr< BerkeleyEnvironment > GetBerkeleyEnv(const fs::path &env_directory)
Get BerkeleyEnvironment given a directory path.
Definition: bdb.cpp:61
GetWalletEnv
static std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &path, std::string &database_filename)
Definition: db_tests.cpp:16
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(getwalletenv_file)
Definition: db_tests.cpp:23
BerkeleyEnvironment
Definition: bdb.h:44
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18