Bitcoin Core  21.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 
14 BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
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 = GetDataDir();
27  fs::path file_path = datadir / test_name;
28  std::ofstream f(file_path.BOOST_FILESYSTEM_C_STR);
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 
37 BOOST_AUTO_TEST_CASE(getwalletenv_directory)
38 {
39  std::string expected_name = "wallet.dat";
40  const fs::path datadir = GetDataDir();
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 
48 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)
49 {
50  fs::path datadir = GetDataDir() / "1";
51  fs::path datadir_2 = GetDataDir() / "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 
62 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
63 {
64  fs::path datadir = GetDataDir() / "1";
65  fs::path datadir_2 = GetDataDir() / "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 
GetDataDir
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:805
fs.h
bdb.h
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
fsbridge::ofstream
fs::ofstream ofstream
Definition: fs.h:102
BDBDataFile
fs::path BDBDataFile(const fs::path &wallet_path)
Definition: db.cpp:56
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_AUTO_TEST_SUITE_END
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18