Bitcoin ABC 0.30.5
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 <util/fs.h>
8
9#include <test/util/setup_common.h>
10
11#include <boost/test/unit_test.hpp>
12
13#include <fstream>
14#include <memory>
15#include <string>
16
17BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup)
18
19BOOST_AUTO_TEST_CASE(getwalletenv_file) {
20 std::string test_name = "test_name.dat";
21 const fs::path datadir = gArgs.GetDataDirNet();
22 fs::path file_path = datadir / test_name;
23 std::ofstream f{file_path};
24 f.close();
25
26 std::string filename;
27 std::shared_ptr<BerkeleyEnvironment> env =
28 GetWalletEnv(file_path, filename);
29 BOOST_CHECK(filename == test_name);
30 BOOST_CHECK(env->Directory() == datadir);
31}
32
33BOOST_AUTO_TEST_CASE(getwalletenv_directory) {
34 std::string expected_name = "wallet.dat";
35 const fs::path datadir = gArgs.GetDataDirNet();
36
37 std::string filename;
38 std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(datadir, filename);
39 BOOST_CHECK(filename == expected_name);
40 BOOST_CHECK(env->Directory() == datadir);
41}
42
43BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_multiple) {
44 fs::path datadir = gArgs.GetDataDirNet() / "1";
45 fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
46 std::string filename;
47
48 std::shared_ptr<BerkeleyEnvironment> env_1 =
49 GetWalletEnv(datadir, filename);
50 std::shared_ptr<BerkeleyEnvironment> env_2 =
51 GetWalletEnv(datadir, filename);
52 std::shared_ptr<BerkeleyEnvironment> env_3 =
53 GetWalletEnv(datadir_2, filename);
54
55 BOOST_CHECK(env_1 == env_2);
56 BOOST_CHECK(env_2 != env_3);
57}
58
59BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance) {
60 fs::path datadir = gArgs.GetDataDirNet() / "1";
61 fs::path datadir_2 = gArgs.GetDataDirNet() / "2";
62 std::string filename;
63
64 std::shared_ptr<BerkeleyEnvironment> env_1_a =
65 GetWalletEnv(datadir, filename);
66 std::shared_ptr<BerkeleyEnvironment> env_2_a =
67 GetWalletEnv(datadir_2, filename);
68 env_1_a.reset();
69
70 std::shared_ptr<BerkeleyEnvironment> env_1_b =
71 GetWalletEnv(datadir, filename);
72 std::shared_ptr<BerkeleyEnvironment> env_2_b =
73 GetWalletEnv(datadir_2, filename);
74
75 BOOST_CHECK(env_1_a != env_1_b);
76 BOOST_CHECK(env_2_a == env_2_b);
77}
78
79BOOST_AUTO_TEST_SUITE_END()
ArgsManager gArgs
Definition: args.cpp:38
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:83
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:215
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_CASE(seederaddrinfo_test)
Definition: db_tests.cpp:56