|
| 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