Skip to content

Commit 40b333e

Browse files
fuzz: wallet, add target for CoinControl
1 parent 1ecdf6e commit 40b333e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

src/Makefile.test.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ BITCOIN_TESTS += wallet/test/db_tests.cpp
194194
endif
195195

196196
FUZZ_WALLET_SRC = \
197+
wallet/test/fuzz/coincontrol.cpp \
197198
wallet/test/fuzz/coinselection.cpp \
198199
wallet/test/fuzz/fees.cpp \
199200
wallet/test/fuzz/parse_iso8601.cpp

src/wallet/test/fuzz/coincontrol.cpp

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright (c) 2022 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <test/fuzz/FuzzedDataProvider.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <test/fuzz/util.h>
8+
#include <test/util/setup_common.h>
9+
#include <wallet/coincontrol.h>
10+
#include <wallet/test/util.h>
11+
12+
namespace wallet {
13+
namespace {
14+
15+
const TestingSetup* g_setup;
16+
17+
void initialize_coincontrol()
18+
{
19+
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
20+
g_setup = testing_setup.get();
21+
}
22+
23+
FUZZ_TARGET_INIT(coincontrol, initialize_coincontrol)
24+
{
25+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26+
const auto& node = g_setup->m_node;
27+
ArgsManager& args = *node.args;
28+
29+
// for GetBoolArg to return true sometimes
30+
args.ForceSetArg("-avoidpartialspends", fuzzed_data_provider.ConsumeBool()?"1":"0");
31+
32+
CCoinControl coin_control;
33+
COutPoint out_point;
34+
35+
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
36+
{
37+
CallOneOf(
38+
fuzzed_data_provider,
39+
[&] {
40+
std::optional<COutPoint> optional_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
41+
if (!optional_out_point) {
42+
return;
43+
}
44+
out_point = *optional_out_point;
45+
},
46+
[&] {
47+
(void)coin_control.HasSelected();
48+
},
49+
[&] {
50+
(void)coin_control.IsSelected(out_point);
51+
},
52+
[&] {
53+
(void)coin_control.IsExternalSelected(out_point);
54+
},
55+
[&] {
56+
(void)coin_control.GetExternalOutput(out_point);
57+
},
58+
[&] {
59+
(void)coin_control.Select(out_point);
60+
},
61+
[&] {
62+
const CTxOut tx_out{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)};
63+
(void)coin_control.SelectExternal(out_point, tx_out);
64+
},
65+
[&] {
66+
(void)coin_control.UnSelect(out_point);
67+
},
68+
[&] {
69+
(void)coin_control.UnSelectAll();
70+
},
71+
[&] {
72+
(void)coin_control.ListSelected();
73+
},
74+
[&] {
75+
int64_t weight{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
76+
(void)coin_control.SetInputWeight(out_point, weight);
77+
},
78+
[&] {
79+
// Condition to avoid the assertion in GetInputWeight
80+
if (coin_control.HasInputWeight(out_point)) {
81+
(void)coin_control.GetInputWeight(out_point);
82+
}
83+
});
84+
}
85+
}
86+
} // namespace
87+
} // namespace wallet

0 commit comments

Comments
 (0)