Skip to content

Commit e066763

Browse files
committed
wallet: coverage for loading an unknown descriptor
Previously, this was crashing the wallet.
1 parent d26c3cc commit e066763

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ BITCOIN_TESTS += \
174174
wallet/test/availablecoins_tests.cpp \
175175
wallet/test/init_tests.cpp \
176176
wallet/test/ismine_tests.cpp \
177-
wallet/test/scriptpubkeyman_tests.cpp
177+
wallet/test/scriptpubkeyman_tests.cpp \
178+
wallet/test/walletload_tests.cpp
178179

179180
FUZZ_SUITE_LD_COMMON +=\
180181
$(SQLITE_LIBS) \

src/wallet/test/walletload_tests.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <wallet/wallet.h>
6+
#include <test/util/setup_common.h>
7+
8+
#include <boost/test/unit_test.hpp>
9+
10+
namespace wallet {
11+
12+
BOOST_AUTO_TEST_SUITE(walletload_tests)
13+
14+
class DummyDescriptor final : public Descriptor {
15+
private:
16+
std::string desc;
17+
public:
18+
explicit DummyDescriptor(const std::string& descriptor) : desc(descriptor) {};
19+
~DummyDescriptor() = default;
20+
21+
std::string ToString() const override { return desc; }
22+
std::optional<OutputType> GetOutputType() const override { return OutputType::UNKNOWN; }
23+
24+
bool IsRange() const override { return false; }
25+
bool IsSolvable() const override { return false; }
26+
bool IsSingleType() const override { return true; }
27+
bool ToPrivateString(const SigningProvider& provider, std::string& out) const override { return false; }
28+
bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const override { return false; }
29+
bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const override { return false; };
30+
bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const override { return false; }
31+
void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const override {}
32+
};
33+
34+
BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup)
35+
{
36+
std::unique_ptr<WalletDatabase> database = CreateMockWalletDatabase();
37+
{
38+
// Write unknown active descriptor
39+
WalletBatch batch(*database, false);
40+
std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
41+
WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
42+
BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
43+
BOOST_CHECK(batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(OutputType::UNKNOWN), uint256(), false));
44+
}
45+
46+
{
47+
// Now try to load the wallet and verify the error.
48+
const std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(database)));
49+
BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::UNKNOWN_DESCRIPTOR);
50+
}
51+
}
52+
53+
BOOST_AUTO_TEST_SUITE_END()
54+
} // namespace wallet

0 commit comments

Comments
 (0)