Skip to content

Commit 8f40271

Browse files
committed
Merge bitcoin/bitcoin#27902: fuzz: wallet, add target for CoinControl
40b333e fuzz: wallet, add target for CoinControl (Ayush Singh) Pull request description: This PR adds fuzz coverage for `wallet/coincontrol`. Motivation: Issue [#27272](bitcoin/bitcoin#27272 (comment)) The idea is to create different/unique instances of `COutPoint` by placing it inside the `CallOneOf` function, which may or may not be consumed by all of the `CoinControl` file's methods. This is my first PR on Bitcoin Core, and I will try my best to address any reviews/changes ASAP. I'm also working on fuzz harness files for other files in the wallet and plan to open PR for them soon. ACKs for top commit: kevkevinpal: reACK [40b333e](bitcoin/bitcoin@40b333e) MarcoFalke: lgtm ACK 40b333e achow101: ACK 40b333e brunoerg: crACK 40b333e dergoegge: ACK 40b333e Tree-SHA512: 174769f4e86df8590b532b85480fd620082587e84e50e49ca9b52f0588a219355362cefd66250dd9942e86019d27af4ca599b45e871e9f147d2cc0ba97c4aa7b
2 parents 7f0b79e + 40b333e commit 8f40271

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)