Bitcoin ABC  0.24.7
P2P Digital Currency
db_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018 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 <wallet/bdb.h>
6 
7 #include <fs.h>
8 
9 #include <test/util/setup_common.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 #include <memory>
14 
15 BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
16 
17 BOOST_AUTO_TEST_CASE(getwalletenv_file) {
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 =
26  GetWalletEnv(file_path, filename);
27  BOOST_CHECK(filename == test_name);
28  BOOST_CHECK(env->Directory() == datadir);
29 }
30 
31 BOOST_AUTO_TEST_CASE(getwalletenv_directory) {
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  fs::path datadir = GetDataDir() / "1";
43  fs::path datadir_2 = GetDataDir() / "2";
44  std::string filename;
45 
46  std::shared_ptr<BerkeleyEnvironment> env_1 =
47  GetWalletEnv(datadir, filename);
48  std::shared_ptr<BerkeleyEnvironment> env_2 =
49  GetWalletEnv(datadir, filename);
50  std::shared_ptr<BerkeleyEnvironment> env_3 =
51  GetWalletEnv(datadir_2, filename);
52 
53  BOOST_CHECK(env_1 == env_2);
54  BOOST_CHECK(env_2 != env_3);
55 }
56 
57 BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance) {
58  fs::path datadir = GetDataDir() / "1";
59  fs::path datadir_2 = GetDataDir() / "2";
60  std::string filename;
61 
62  std::shared_ptr<BerkeleyEnvironment> env_1_a =
63  GetWalletEnv(datadir, filename);
64  std::shared_ptr<BerkeleyEnvironment> env_2_a =
65  GetWalletEnv(datadir_2, filename);
66  env_1_a.reset();
67 
68  std::shared_ptr<BerkeleyEnvironment> env_1_b =
69  GetWalletEnv(datadir, filename);
70  std::shared_ptr<BerkeleyEnvironment> env_2_b =
71  GetWalletEnv(datadir_2, filename);
72 
73  BOOST_CHECK(env_1_a != env_1_b);
74  BOOST_CHECK(env_2_a == env_2_b);
75 }
76 
GetDataDir
const fs::path & GetDataDir(bool fNetSpecific)
Definition: system.cpp:779
fs.h
GetWalletEnv
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:77
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:99
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(getwalletenv_file)
Definition: db_tests.cpp:17
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