Branch data Line data Source code
# 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 : 9 : { # 18 : 9 : fs::path data_file = BDBDataFile(path); # 19 : 9 : database_filename = data_file.filename().string(); # 20 : 9 : return GetBerkeleyEnv(data_file.parent_path()); # 21 : 9 : } # 22 : : # 23 : : BOOST_AUTO_TEST_CASE(getwalletenv_file) # 24 : 1 : { # 25 : 1 : std::string test_name = "test_name.dat"; # 26 : 1 : const fs::path datadir = gArgs.GetDataDirNet(); # 27 : 1 : fs::path file_path = datadir / test_name; # 28 : 1 : std::ofstream f(file_path.BOOST_FILESYSTEM_C_STR); # 29 : 1 : f.close(); # 30 : : # 31 : 1 : std::string filename; # 32 : 1 : std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename); # 33 : 1 : BOOST_CHECK_EQUAL(filename, test_name); # 34 : 1 : BOOST_CHECK_EQUAL(env->Directory(), datadir); # 35 : 1 : } # 36 : : # 37 : : BOOST_AUTO_TEST_CASE(getwalletenv_directory) # 38 : 1 : { # 39 : 1 : std::string expected_name = "wallet.dat"; # 40 : 1 : const fs::path datadir = gArgs.GetDataDirNet(); # 41 : : # 42 : 1 : std::string filename; # 43 : 1 : std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename); # 44 : 1 : BOOST_CHECK_EQUAL(filename, expected_name); # 45 : 1 : BOOST_CHECK_EQUAL(env->Directory(), datadir); # 46 : 1 : } # 47 : : # 48 : : BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple) # 49 : 1 : { # 50 : 1 : fs::path datadir = gArgs.GetDataDirNet() / "1"; # 51 : 1 : fs::path datadir_2 = gArgs.GetDataDirNet() / "2"; # 52 : 1 : std::string filename; # 53 : : # 54 : 1 : std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename); # 55 : 1 : std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename); # 56 : 1 : std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename); # 57 : : # 58 : 1 : BOOST_CHECK(env_1 == env_2); # 59 : 1 : BOOST_CHECK(env_2 != env_3); # 60 : 1 : } # 61 : : # 62 : : BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance) # 63 : 1 : { # 64 : 1 : fs::path datadir = gArgs.GetDataDirNet() / "1"; # 65 : 1 : fs::path datadir_2 = gArgs.GetDataDirNet() / "2"; # 66 : 1 : std::string filename; # 67 : : # 68 : 1 : std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename); # 69 : 1 : std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename); # 70 : 1 : env_1_a.reset(); # 71 : : # 72 : 1 : std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename); # 73 : 1 : std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename); # 74 : : # 75 : 1 : BOOST_CHECK(env_1_a != env_1_b); # 76 : 1 : BOOST_CHECK(env_2_a == env_2_b); # 77 : 1 : } # 78 : : # 79 : : BOOST_AUTO_TEST_SUITE_END()