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 
BOOST_FIXTURE_TEST_SUITE(db_tests,BasicTestingSetup)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 
BOOST_AUTO_TEST_CASE(getwalletenv_file)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 
BOOST_AUTO_TEST_CASE(getwalletenv_directory)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 
BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple)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 
BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance)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 
83 BOOST_AUTO_TEST_SUITE_END()
84