Repository navigation
Expand file tree
/
Copy pathwalletdb_tests.cpp
More file actions
284 lines (256 loc) · 13.2 KB
/
Copy pathwalletdb_tests.cpp
File metadata and controls
284 lines (256 loc) · 13.2 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2012-2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <clientversion.h>
#include <hash.h>
#include <key_io.h>
#include <script/descriptor.h>
#include <streams.h>
#include <uint256.h>
#include <util/message.h>
#include <util/strencodings.h>
#include <wallet/bip39.h>
#include <wallet/crypter.h>
#include <wallet/hdchain.h>
#include <wallet/test/util.h>
#include <wallet/test/wallet_test_fixture.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <boost/test/unit_test.hpp>
#include <functional>
#include <string>
#include <utility>
#include <vector>
namespace wallet {
BOOST_FIXTURE_TEST_SUITE(walletdb_tests, WalletTestingSetup)
BOOST_AUTO_TEST_CASE(walletdb_readkeyvalue)
{
/**
* When ReadKeyValue() reads from either a "key" or "wkey" it first reads the DataStream into a
* CPrivKey or CWalletKey respectively and then reads a hash of the pubkey and privkey into a uint256.
* Wallets from 0.8 or before do not store the pubkey/privkey hash, trying to read the hash from old
* wallets throws an exception, for backwards compatibility this read is wrapped in a try block to
* silently fail. The test here makes sure the type of exception thrown from DataStream::read()
* matches the type we expect, otherwise we need to update the "key"/"wkey" exception type caught.
*/
DataStream ssValue{};
uint256 dummy;
BOOST_CHECK_THROW(ssValue >> dummy, std::ios_base::failure);
}
// Helper: build the value stream for an HD chain DB record and run LoadHDChain.
static bool TryReadHDChainRecord(CWallet& wallet, const std::string& dbKey, bool fCrypted, std::string& strErrOut)
{
CHDChain chain;
chain.SetCrypted(fCrypted);
DataStream ssValue{};
ssValue << chain;
return LoadHDChain(&wallet, ssValue, strErrOut, /*crypted=*/dbKey == DBKeys::CRYPTED_HDCHAIN);
}
BOOST_AUTO_TEST_CASE(walletdb_hdchain_type_mismatch)
{
// Regression: a wallet record claiming HDCHAIN but carrying a crypted CHDChain
// (or vice versa) used to trigger an assert and abort the process. It must now
// surface as a graceful load error.
std::string strErr;
BOOST_CHECK(!TryReadHDChainRecord(m_wallet, DBKeys::HDCHAIN, /*fCrypted=*/true, strErr));
BOOST_CHECK_EQUAL(strErr, "Error reading wallet database: HD chain type mismatch");
strErr.clear();
BOOST_CHECK(!TryReadHDChainRecord(m_wallet, DBKeys::CRYPTED_HDCHAIN, /*fCrypted=*/false, strErr));
BOOST_CHECK_EQUAL(strErr, "Error reading wallet database: HD chain type mismatch");
}
BOOST_AUTO_TEST_CASE(walletdb_platform_data_records)
{
LOCK(m_wallet.cs_wallet);
// Round-trip through the in-memory map: write, prefix query, erase.
const std::vector<unsigned char> value_a{0x01, 0x02};
const std::vector<unsigned char> value_b{0x03};
BOOST_CHECK(m_wallet.WritePlatformData("platform/identity/0", value_a));
BOOST_CHECK(m_wallet.WritePlatformData("platform/identity/1", value_b));
BOOST_CHECK(m_wallet.WritePlatformData("platform/metadata", value_b));
auto records{m_wallet.GetPlatformData("platform/identity/")};
BOOST_CHECK_EQUAL(records.size(), 2U);
BOOST_CHECK(records.at("platform/identity/0") == value_a);
BOOST_CHECK(records.at("platform/identity/1") == value_b);
BOOST_CHECK_EQUAL(m_wallet.GetPlatformData("").size(), 3U);
BOOST_CHECK(m_wallet.GetPlatformData("platform/idem").empty());
// An empty value erases the record.
BOOST_CHECK(m_wallet.WritePlatformData("platform/identity/0", {}));
records = m_wallet.GetPlatformData("platform/identity/");
BOOST_CHECK_EQUAL(records.size(), 1U);
BOOST_CHECK_EQUAL(records.count("platform/identity/0"), 0U);
}
//! Load a wallet whose database holds a PLATFORM_DATA record under
//! `record_key` with a truncated value (claims five bytes, carries none),
//! next to an intact record, and report the load result and the Platform
//! data that made it into the wallet.
static DBErrors LoadWithCorruptPlatformRecord(const node::NodeContext& node, const std::string& record_key,
std::map<std::string, std::vector<unsigned char>>& platform_data_out)
{
CWallet wallet(node.chain.get(), "", CreateMockableWalletDatabase());
const std::vector<unsigned char> truncated{0x05};
const std::vector<unsigned char> intact_value{0x01, 0x02};
{
const std::unique_ptr<DatabaseBatch> batch{wallet.GetDatabase().MakeBatch()};
BOOST_REQUIRE(batch->Write(std::make_pair(DBKeys::PLATFORM_DATA, record_key), Span{truncated}));
BOOST_REQUIRE(batch->Write(std::make_pair(DBKeys::PLATFORM_DATA, std::string{"platform/intact"}), intact_value));
}
const DBErrors result{WalletBatch(wallet.GetDatabase()).LoadWallet(&wallet)};
platform_data_out = WITH_LOCK(wallet.cs_wallet, return wallet.GetPlatformData(""));
return result;
}
BOOST_AUTO_TEST_CASE(walletdb_platform_data_corruption_policy)
{
// Platform records are opaque cache/metadata. Damage is noncritical, the
// wallet still loads, and intact records survive. There is no reserved
// Platform seed-selection record.
std::map<std::string, std::vector<unsigned char>> platform_data;
BOOST_CHECK(LoadWithCorruptPlatformRecord(m_node, "platform/metadata", platform_data) ==
DBErrors::NONCRITICAL_ERROR);
BOOST_CHECK_EQUAL(platform_data.count("platform/metadata"), 0U);
BOOST_CHECK(platform_data.at("platform/intact") == std::vector<unsigned char>({0x01, 0x02}));
platform_data.clear();
BOOST_CHECK(LoadWithCorruptPlatformRecord(m_node, "platform/identity/0", platform_data) ==
DBErrors::NONCRITICAL_ERROR);
BOOST_CHECK_EQUAL(platform_data.count("platform/identity/0"), 0U);
BOOST_CHECK(platform_data.at("platform/intact") == std::vector<unsigned char>({0x01, 0x02}));
}
BOOST_AUTO_TEST_CASE(walletdb_dash_record_load_policy)
{
// Damage to an hdpubkey or g_object record is noncritical. An HD chain
// whose encryption state disagrees with its record type, or with the
// wallet's, is corrupt.
CHDChain plain_chain;
BOOST_REQUIRE(plain_chain.SetSeed(SecureVector(32, 0x01), /*fUpdateID=*/true));
CHDChain crypted_chain{plain_chain};
crypted_chain.SetCrypted(true);
const std::pair<std::string, unsigned int> mkey_key{DBKeys::MASTER_KEY, 1};
const std::vector<unsigned char> truncated{0x05};
CKey key;
key.MakeNewKey(/*fCompressed=*/true);
using WriteRecords = std::function<bool(DatabaseBatch&)>;
const std::vector<std::pair<WriteRecords, DBErrors>> rows{
{[&](DatabaseBatch& batch) { return batch.Write(DBKeys::HDCHAIN, plain_chain); }, DBErrors::LOAD_OK},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::CRYPTED_HDCHAIN, crypted_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::LOAD_OK},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::HDCHAIN, plain_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) { return batch.Write(DBKeys::CRYPTED_HDCHAIN, crypted_chain); }, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) {
return batch.Write(DBKeys::HDCHAIN, crypted_chain) && batch.Write(mkey_key, CMasterKey{});
}, DBErrors::CORRUPT},
{[&](DatabaseBatch& batch) {
return batch.Write(std::make_pair(DBKeys::HDPUBKEY, key.GetPubKey()), Span{truncated});
}, DBErrors::NONCRITICAL_ERROR},
{[&](DatabaseBatch& batch) {
return batch.Write(std::make_pair(DBKeys::G_OBJECT, uint256::ONE), Span{truncated});
}, DBErrors::NONCRITICAL_ERROR},
};
for (size_t i{0}; i < rows.size(); ++i) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
BOOST_REQUIRE(rows[i].first(*wallet.GetDatabase().MakeBatch()));
BOOST_CHECK_MESSAGE(WalletBatch(wallet.GetDatabase()).LoadWallet(&wallet) == rows[i].second, "row " << i);
}
}
BOOST_AUTO_TEST_CASE(walletdb_descriptor_orphan_records)
{
// A descriptor cache, key or encrypted key record whose descriptor ID has
// no descriptor record, or is the legacy manager's ID, is not loaded. An
// interrupted importdescriptors can leave such records behind.
CExtKey root;
const std::vector<unsigned char> seed(32, 1);
root.SetSeed(MakeByteSpan(seed));
const CPubKey pubkey{root.key.GetPubKey()};
for (const bool legacy_owner : {false, true}) {
for (int record = 0; record < 3; ++record) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
const uint256 owner{legacy_owner ? uint256::ONE : uint256::TWO};
{
WalletBatch batch(wallet.GetDatabase());
switch (record) {
case 0: BOOST_REQUIRE(batch.WriteDescriptorParentCache(root.Neuter(), owner, 0)); break;
case 1: BOOST_REQUIRE(batch.WriteDescriptorKey(owner, pubkey, root.key.GetPrivKey(), "", "")); break;
case 2: BOOST_REQUIRE(batch.WriteCryptedDescriptorKey(owner, pubkey, {1}, {}, {})); break;
}
}
BOOST_CHECK(wallet.LoadWallet() == DBErrors::LOAD_OK);
}
}
}
BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock)
{
// Exercises a db read write operation that shouldn't deadlock.
for (const DatabaseFormat& db_format : DATABASE_FORMATS) {
// Context setup
DatabaseOptions options;
options.require_format = db_format;
DatabaseStatus status;
bilingual_str error_string;
std::unique_ptr<WalletDatabase> db = MakeDatabase(m_path_root / strprintf("wallet_%d_.dat", db_format).c_str(), options, status, error_string);
BOOST_REQUIRE(status == DatabaseStatus::SUCCESS);
std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(db)));
wallet->m_keypool_size = 4;
// Create legacy spkm
LOCK(wallet->cs_wallet);
auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan();
BOOST_CHECK(legacy_spkm->CanGenerateKeys() && legacy_spkm->TopUp());
wallet->Flush();
// Now delete all records, which performs a read write operation.
BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords());
}
}
BOOST_AUTO_TEST_CASE(walletdb_unreadable_tx_record)
{
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
const std::vector<unsigned char> truncated{0x05};
BOOST_REQUIRE(wallet.GetDatabase().MakeBatch()->Write(std::make_pair(DBKeys::TX, uint256::ONE), Span{truncated}));
BOOST_CHECK(wallet.LoadWallet() == DBErrors::CORRUPT);
}
BOOST_AUTO_TEST_CASE(walletdb_descriptor_mnemonic_consistency)
{
const SecureString mnemonic{"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"};
const SecureString passphrase{"recovery passphrase"};
SecureVector seed;
CMnemonic::ToSeed(mnemonic, passphrase, seed);
CExtKey root;
root.SetSeed(MakeByteSpan(seed));
const CPubKey pubkey{root.key.GetPubKey()};
const CPrivKey privkey{root.key.GetPrivKey()};
enum class Record { NO_MNEMONIC, MATCHING, MISMATCHED };
for (const Record record : {Record::NO_MNEMONIC, Record::MATCHING, Record::MISMATCHED}) {
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
FlatSigningProvider provider;
std::string error;
auto parsed{Parse("pkh(" + HexStr(pubkey) + ")", provider, error, false)};
BOOST_REQUIRE_MESSAGE(parsed, error);
WalletDescriptor descriptor(std::move(parsed), 0, 0, 1, 0);
{
WalletBatch batch(wallet.GetDatabase());
BOOST_REQUIRE(batch.WriteWalletFlags(WALLET_FLAG_DESCRIPTORS));
BOOST_REQUIRE(batch.WriteDescriptor(descriptor.id, descriptor));
if (record == Record::NO_MNEMONIC) {
// Records written before mnemonics were stored end after the key hash.
std::vector<unsigned char> key_bytes(pubkey.begin(), pubkey.end());
key_bytes.insert(key_bytes.end(), privkey.begin(), privkey.end());
BOOST_REQUIRE(wallet.GetDatabase().MakeBatch()->Write(std::make_pair(DBKeys::WALLETDESCRIPTORKEY, std::make_pair(descriptor.id, pubkey)), std::make_pair(privkey, Hash(key_bytes))));
} else {
BOOST_REQUIRE(batch.WriteDescriptorKey(descriptor.id, pubkey, privkey, mnemonic, record == Record::MATCHING ? passphrase : SecureString{"wrong passphrase"}));
}
}
const auto result{wallet.LoadWallet()};
if (record == Record::MISMATCHED) {
BOOST_CHECK(result == DBErrors::CORRUPT);
continue;
}
BOOST_REQUIRE(result == DBErrors::LOAD_OK);
LOCK(wallet.cs_wallet);
BOOST_CHECK(wallet.IsMine(GetScriptForDestination(PKHash(pubkey))) == ISMINE_SPENDABLE);
std::string signature;
BOOST_CHECK(wallet.SignMessage("descriptor load regression", PKHash(pubkey), signature) == SigningResult::OK);
BOOST_CHECK(MessageVerify(EncodeDestination(PKHash(pubkey)), signature, "descriptor load regression") == MessageVerificationResult::OK);
}
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet