Line data Source code
1 : // Copyright (c) 2018-2021 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 <boost/test/unit_test.hpp> 6 : 7 : #include <fs.h> 8 : #include <test/util/setup_common.h> 9 : #include <wallet/bdb.h> 10 : 11 : #include <fstream> 12 : #include <memory> 13 : #include <string> 14 : 15 : namespace wallet { 16 146 : BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup) 17 : 18 9 : static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, fs::path& database_filename) 19 : { 20 9 : fs::path data_file = BDBDataFile(path); 21 9 : database_filename = data_file.filename(); 22 9 : return GetBerkeleyEnv(data_file.parent_path(), false); 23 9 : } 24 : 25 149 : BOOST_AUTO_TEST_CASE(getwalletenv_file) 26 : { 27 1 : fs::path test_name = "test_name.dat"; 28 1 : const fs::path datadir = m_args.GetDataDirNet(); 29 1 : fs::path file_path = datadir / test_name; 30 1 : std::ofstream f{file_path}; 31 1 : f.close(); 32 : 33 1 : fs::path filename; 34 1 : std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(file_path, filename); 35 1 : BOOST_CHECK_EQUAL(filename, test_name); 36 1 : BOOST_CHECK_EQUAL(env->Directory(), datadir); 37 1 : } 38 : 39 149 : BOOST_AUTO_TEST_CASE(getwalletenv_directory) 40 : { 41 1 : fs::path expected_name = "wallet.dat"; 42 1 : const fs::path datadir = m_args.GetDataDirNet(); 43 : 44 1 : fs::path filename; 45 1 : std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename); 46 1 : BOOST_CHECK_EQUAL(filename, expected_name); 47 1 : BOOST_CHECK_EQUAL(env->Directory(), datadir); 48 1 : } 49 : 50 149 : BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple) 51 : { 52 1 : fs::path datadir = m_args.GetDataDirNet() / "1"; 53 1 : fs::path datadir_2 = m_args.GetDataDirNet() / "2"; 54 1 : fs::path filename; 55 : 56 1 : std::shared_ptr<BerkeleyEnvironment> env_1 = GetWalletEnv(datadir, filename); 57 1 : std::shared_ptr<BerkeleyEnvironment> env_2 = GetWalletEnv(datadir, filename); 58 1 : std::shared_ptr<BerkeleyEnvironment> env_3 = GetWalletEnv(datadir_2, filename); 59 : 60 1 : BOOST_CHECK(env_1 == env_2); 61 1 : BOOST_CHECK(env_2 != env_3); 62 1 : } 63 : 64 149 : BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance) 65 : { 66 1 : fs::path datadir = gArgs.GetDataDirNet() / "1"; 67 1 : fs::path datadir_2 = gArgs.GetDataDirNet() / "2"; 68 1 : fs::path filename; 69 : 70 1 : std::shared_ptr <BerkeleyEnvironment> env_1_a = GetWalletEnv(datadir, filename); 71 1 : std::shared_ptr <BerkeleyEnvironment> env_2_a = GetWalletEnv(datadir_2, filename); 72 1 : env_1_a.reset(); 73 : 74 1 : std::shared_ptr<BerkeleyEnvironment> env_1_b = GetWalletEnv(datadir, filename); 75 1 : std::shared_ptr<BerkeleyEnvironment> env_2_b = GetWalletEnv(datadir_2, filename); 76 : 77 1 : BOOST_CHECK(env_1_a != env_1_b); 78 1 : BOOST_CHECK(env_2_a == env_2_b); 79 1 : } 80 : 81 146 : BOOST_AUTO_TEST_SUITE_END() 82 : } // namespace wallet