-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwalletdb_tests.cpp
More file actions
123 lines (98 loc) · 3.92 KB
/
Copy pathwalletdb_tests.cpp
File metadata and controls
123 lines (98 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2017-2020 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <wallet/walletdb.h>
#include <chainparams.h>
#include <interfaces/chain.h>
#include <wallet/wallet.h>
#include <test/setup_common.h>
#include <wallet/test/wallet_test_fixture.h>
#include <boost/test/unit_test.hpp>
#include <memory>
namespace {
static std::unique_ptr<CWallet> LoadWallet(WalletBatch &batch) {
auto chain = interfaces::MakeChain();
std::unique_ptr<CWallet> wallet = std::make_unique<CWallet>(
Params(), *chain, WalletLocation(), WalletDatabase::CreateDummy());
DBErrors res = batch.LoadWallet(wallet.get());
BOOST_CHECK(res == DBErrors::LOAD_OK);
return wallet;
}
} // namespace
BOOST_FIXTURE_TEST_SUITE(walletdb_tests, WalletTestingSetup)
BOOST_AUTO_TEST_CASE(write_erase_name) {
WalletBatch batch(m_wallet.GetDBHandle(), "cr+");
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
CTxDestination dst2 = CKeyID(uint160S("f00d"));
BOOST_CHECK(batch.WriteName(dst1, "name1"));
BOOST_CHECK(batch.WriteName(dst2, "name2"));
{
auto w = LoadWallet(batch);
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst1));
BOOST_CHECK_EQUAL("name1", w->mapAddressBook[dst1].name);
BOOST_CHECK_EQUAL("name2", w->mapAddressBook[dst2].name);
}
batch.EraseName(dst1);
{
auto w = LoadWallet(batch);
BOOST_CHECK_EQUAL(0, w->mapAddressBook.count(dst1));
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst2));
}
}
BOOST_AUTO_TEST_CASE(write_erase_purpose) {
WalletBatch batch(m_wallet.GetDBHandle(), "cr+");
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
CTxDestination dst2 = CKeyID(uint160S("f00d"));
BOOST_CHECK(batch.WritePurpose(dst1, "purpose1"));
BOOST_CHECK(batch.WritePurpose(dst2, "purpose2"));
{
auto w = LoadWallet(batch);
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst1));
BOOST_CHECK_EQUAL("purpose1", w->mapAddressBook[dst1].purpose);
BOOST_CHECK_EQUAL("purpose2", w->mapAddressBook[dst2].purpose);
}
batch.ErasePurpose(dst1);
{
auto w = LoadWallet(batch);
BOOST_CHECK_EQUAL(0, w->mapAddressBook.count(dst1));
BOOST_CHECK_EQUAL(1, w->mapAddressBook.count(dst2));
}
}
BOOST_AUTO_TEST_CASE(write_erase_destdata) {
WalletBatch batch(m_wallet.GetDBHandle(), "cr+");
CTxDestination dst1 = CKeyID(uint160S("c0ffee"));
CTxDestination dst2 = CKeyID(uint160S("f00d"));
BOOST_CHECK(batch.WriteDestData(dst1, "key1", "value1"));
BOOST_CHECK(batch.WriteDestData(dst1, "key2", "value2"));
BOOST_CHECK(batch.WriteDestData(dst2, "key1", "value3"));
BOOST_CHECK(batch.WriteDestData(dst2, "key2", "value4"));
{
auto w = LoadWallet(batch);
std::string val;
BOOST_CHECK(w->GetDestData(dst1, "key1", &val));
BOOST_CHECK_EQUAL("value1", val);
BOOST_CHECK(w->GetDestData(dst1, "key2", &val));
BOOST_CHECK_EQUAL("value2", val);
BOOST_CHECK(w->GetDestData(dst2, "key1", &val));
BOOST_CHECK_EQUAL("value3", val);
BOOST_CHECK(w->GetDestData(dst2, "key2", &val));
BOOST_CHECK_EQUAL("value4", val);
}
batch.EraseDestData(dst1, "key2");
{
auto w = LoadWallet(batch);
std::string dummy;
BOOST_CHECK(w->GetDestData(dst1, "key1", &dummy));
BOOST_CHECK(!w->GetDestData(dst1, "key2", &dummy));
BOOST_CHECK(w->GetDestData(dst2, "key1", &dummy));
BOOST_CHECK(w->GetDestData(dst2, "key2", &dummy));
}
}
BOOST_AUTO_TEST_CASE(no_dest_fails) {
WalletBatch batch(m_wallet.GetDBHandle(), "cr+");
CTxDestination dst = CNoDestination{};
BOOST_CHECK(!batch.WriteName(dst, "name"));
BOOST_CHECK(!batch.WritePurpose(dst, "purpose"));
BOOST_CHECK(!batch.WriteDestData(dst, "key", "value"));
}
BOOST_AUTO_TEST_SUITE_END()