Skip to content

Commit

Permalink
fuzz: add target for ScriptPubKeyMan (legacy)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoerg committed Jul 25, 2023
1 parent d23fda0 commit e951d31
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ FUZZ_WALLET_SRC = \
wallet/test/fuzz/coincontrol.cpp \
wallet/test/fuzz/coinselection.cpp \
wallet/test/fuzz/fees.cpp \
wallet/test/fuzz/parse_iso8601.cpp
wallet/test/fuzz/parse_iso8601.cpp \
wallet/test/fuzz/scriptpubkeyman.cpp

if USE_SQLITE
FUZZ_WALLET_SRC += \
Expand Down
94 changes: 94 additions & 0 deletions src/wallet/test/fuzz/scriptpubkeyman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2022 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/fuzz/FuzzedDataProvider.h>
#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/setup_common.h>
#include <wallet/context.h>
#include <wallet/scriptpubkeyman.h>
#include <wallet/wallet.h>
#include <wallet/test/util.h>
#include <validation.h>

namespace wallet {
namespace {
const TestingSetup* g_setup;
static std::unique_ptr<CWallet> g_wallet_ptr;

void initialize_spkm()
{
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
g_setup = testing_setup.get();
const auto& node{g_setup->m_node};
g_wallet_ptr = std::make_unique<CWallet>(node.chain.get(), "", CreateMockableWalletDatabase());
}

FUZZ_TARGET(scriptpubkeyman, .init = initialize_spkm)
{
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
const auto& node{g_setup->m_node};
Chainstate* chainstate = &node.chainman->ActiveChainstate();
CWallet& wallet = *g_wallet_ptr;
{
LOCK(wallet.cs_wallet);
wallet.SetLastBlockProcessed(chainstate->m_chain.Height(), chainstate->m_chain.Tip()->GetBlockHash());
}

LegacyScriptPubKeyMan& legacy_spkm{*wallet.GetOrCreateLegacyScriptPubKeyMan()};
SignatureData data;
CScript watch_script;

LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
CallOneOf(
fuzzed_data_provider,
[&] {
CKey key;
key.MakeNewKey(fuzzed_data_provider.ConsumeBool());
(void)legacy_spkm.AddKey(key);
},
[&] {
CScript script{ConsumeScript(fuzzed_data_provider)};
(void)legacy_spkm.AddCScript(script);
},
[&] {
CScript script{ConsumeScript(fuzzed_data_provider)};
(void)legacy_spkm.IsMine(script);
},
[&] {
CScript script{ConsumeScript(fuzzed_data_provider)};
(void)legacy_spkm.CanProvide(script, data);
},
[&] {
const OutputType output_type{fuzzed_data_provider.PickValueInArray(OUTPUT_TYPES)};
(void)legacy_spkm.GetNewDestination(output_type);
},
[&] {
CKeyID address(ConsumeUInt160(fuzzed_data_provider));
CKey key_out;
(void)legacy_spkm.GetKey(address, key_out);
},
[&] {
CKeyID address(ConsumeUInt160(fuzzed_data_provider));
CPubKey pub_key;
(void)legacy_spkm.GetWatchPubKey(address, pub_key);
},
[&] {
watch_script = ConsumeScript(fuzzed_data_provider);
},
[&] {
(void)legacy_spkm.LoadWatchOnly(watch_script);
},
[&] {
(void)legacy_spkm.RemoveWatchOnly(watch_script);
},
[&] {
(void)legacy_spkm.HaveWatchOnly(watch_script);
}
);
}
}

} // namespace
} // namespace wallet

0 comments on commit e951d31

Please sign in to comment.