Bitcoin Core  0.20.99
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2019 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 BOOST_AUTO_TEST_CASE(getwalletenv_file)
17 {
18  std::string test_name = "test_name.dat";
19  const fs::path datadir = GetDataDir();
20  fs::path file_path = datadir / test_name;
21  std::ofstream f(file_path.BOOST_FILESYSTEM_C_STR);
22  f.close();
23 
24  std::string filename;
25  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename);
26  BOOST_CHECK(filename == test_name);
27  BOOST_CHECK(env->Directory() == datadir);
28 }
29 
30 BOOST_AUTO_TEST_CASE(getwalletenv_directory)
31 {
32  std::string expected_name = "wallet.dat";
33  const fs::path datadir = GetDataDir();
34 
35  std::string filename;
36  std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
37  BOOST_CHECK(filename == expected_name);
38  BOOST_CHECK(env->Directory() == datadir);
39 }
40 
41 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)
42 {
43  fs::path datadir = GetDataDir() / "1";
44  fs::path datadir_2 = GetDataDir() / "2";
45  std::string filename;
46 
47  std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename);
48  std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename);
49  std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename);
50 
51  BOOST_CHECK(env_1 == env_2);
52  BOOST_CHECK(env_2 != env_3);
53 }
54 
55 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)
56 {
57  fs::path datadir = GetDataDir() / "1";
58  fs::path datadir_2 = GetDataDir() / "2";
59  std::string filename;
60 
61  std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename);
62  std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename);
63  env_1_a.reset();
64 
65  std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename);
66  std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename);
67 
68  BOOST_CHECK(env_1_a != env_1_b);
69  BOOST_CHECK(env_2_a == env_2_b);
70 }
71 
BOOST_AUTO_TEST_CASE(getwalletenv_file)
Definition: db_tests.cpp:16
std::shared_ptr< BerkeleyEnvironment > GetWalletEnv(const fs::path &wallet_path, std::string &database_filename)
Get BerkeleyEnvironment and database filename given a wallet path.
Definition: bdb.cpp:62
fs::ofstream ofstream
Definition: fs.h:91
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:698
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK(expr)
Definition: object.cpp:17