Branch data Line data Source code
1 : : // Copyright (c) 2022-present 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 : 1 : void initialize_coincontrol()
18 : : {
19 [ + - + - : 2 : static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
+ - ]
20 : 1 : g_setup = testing_setup.get();
21 : 1 : }
22 : :
23 [ + - ]: 1111 : FUZZ_TARGET(coincontrol, .init = initialize_coincontrol)
24 : : {
25 : 637 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
26 : 637 : const auto& node = g_setup->m_node;
27 : 637 : ArgsManager& args = *node.args;
28 : :
29 : : // for GetBoolArg to return true sometimes
30 [ + + + - : 1471 : args.ForceSetArg("-avoidpartialspends", fuzzed_data_provider.ConsumeBool()?"1":"0");
+ - ]
31 : :
32 : 637 : CCoinControl coin_control;
33 : 637 : COutPoint out_point;
34 : :
35 [ + + + + ]: 354460 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
36 [ + - ]: 353823 : CallOneOf(
37 : : fuzzed_data_provider,
38 : 14984 : [&] {
39 : 14984 : std::optional<COutPoint> optional_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
40 [ + + ]: 14984 : if (!optional_out_point) {
41 : : return;
42 : : }
43 : 2935 : out_point = *optional_out_point;
44 : : },
45 : 17489 : [&] {
46 : 17489 : (void)coin_control.HasSelected();
47 : 17489 : },
48 : 2460 : [&] {
49 : 2460 : (void)coin_control.IsSelected(out_point);
50 : 2460 : },
51 : 10489 : [&] {
52 : 10489 : (void)coin_control.IsExternalSelected(out_point);
53 : 10489 : },
54 : 51678 : [&] {
55 : 51678 : (void)coin_control.GetExternalOutput(out_point);
56 : 51678 : },
57 : 45954 : [&] {
58 : 45954 : (void)coin_control.Select(out_point);
59 [ - + ]: 45954 : assert(coin_control.IsSelected(out_point));
60 : 45954 : },
61 : 16533 : [&] {
62 [ + - ]: 16533 : const CTxOut tx_out{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)};
63 [ + - ]: 16533 : auto& input = coin_control.Select(out_point);
64 : 16533 : const auto set_tx_out{fuzzed_data_provider.ConsumeBool()};
65 [ + + ]: 16533 : if (set_tx_out) {
66 [ + - ]: 14965 : input.SetTxOut(tx_out);
67 : : }
68 [ + - ]: 16533 : auto has_tx_out{input.HasTxOut()};
69 [ + - ]: 16533 : auto is_external_selected{coin_control.IsExternalSelected(out_point)};
70 [ + + ]: 16533 : if (set_tx_out) {
71 [ - + ]: 14965 : assert(has_tx_out);
72 [ + - - + ]: 14965 : assert(input.GetTxOut() == tx_out);
73 [ - + ]: 14965 : assert(is_external_selected);
74 [ + + ]: 1568 : } else if (!has_tx_out) {
75 [ - + ]: 342 : assert(!is_external_selected);
76 : : }
77 : 16533 : },
78 : 14386 : [&] {
79 : 14386 : coin_control.UnSelect(out_point);
80 [ - + ]: 14386 : assert(!coin_control.IsSelected(out_point));
81 : 14386 : },
82 : 16644 : [&] {
83 : 16644 : coin_control.UnSelectAll();
84 [ - + ]: 16644 : assert(!coin_control.HasSelected());
85 : 16644 : },
86 : 3434 : [&] {
87 : 3434 : const std::vector<COutPoint> selected = coin_control.ListSelected();
88 [ + + ]: 10065 : for (const auto& out : selected) {
89 [ + - - + ]: 6631 : assert(coin_control.IsSelected(out));
90 : : }
91 : 3434 : },
92 : 4513 : [&] {
93 : 4513 : int64_t weight{fuzzed_data_provider.ConsumeIntegral<int64_t>()};
94 : 4513 : coin_control.SetInputWeight(out_point, weight);
95 [ + - ]: 4513 : assert(coin_control.GetInputWeight(out_point) == weight);
96 : 4513 : },
97 : 5120 : [&] {
98 : 5120 : const bool is_selected = coin_control.IsSelected(out_point);
99 [ + + - + ]: 5120 : assert(!coin_control.GetInputWeight(out_point) || is_selected);
100 [ + + - + ]: 5120 : assert(!coin_control.GetSequence(out_point) || is_selected);
101 : 5120 : },
102 : 62990 : [&] {
103 : 62990 : const auto scripts = coin_control.GetScripts(out_point);
104 [ + - + + : 62990 : assert(coin_control.IsSelected(out_point) || (!scripts.first && !scripts.second));
+ - - + ]
105 : 62990 : },
106 : 11627 : [&] {
107 [ + + - + ]: 11627 : assert(coin_control.HasSelectedOrder() || !coin_control.GetSelectionPos(out_point));
108 : 11627 : },
109 : 4145 : [&] {
110 [ + + - + ]: 4145 : assert(!coin_control.GetSelectionPos(out_point) || coin_control.IsSelected(out_point));
111 : 4145 : },
112 : 20250 : [&] {
113 : 20250 : auto& input = coin_control.Select(out_point);
114 : 20250 : uint32_t sequence{fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
115 : 20250 : input.SetSequence(sequence);
116 [ + - ]: 20250 : assert(input.GetSequence() == sequence);
117 [ + - ]: 20250 : assert(coin_control.GetSequence(out_point) == sequence);
118 : 20250 : },
119 : 34428 : [&] {
120 : 34428 : auto& input = coin_control.Select(out_point);
121 : 34428 : const CScript script{ConsumeScript(fuzzed_data_provider)};
122 [ + - ]: 34428 : input.SetScriptSig(script);
123 [ + - - + ]: 34428 : assert(input.HasScripts());
124 [ + - - + ]: 34428 : assert(input.GetScripts().first == script);
125 [ + - - + ]: 68856 : assert(coin_control.GetScripts(out_point).first == script);
126 : 34428 : },
127 : 12809 : [&] {
128 : 12809 : auto& input = coin_control.Select(out_point);
129 : 12809 : const CScriptWitness script_wit{ConsumeScriptWitness(fuzzed_data_provider)};
130 [ + - ]: 12809 : input.SetScriptWitness(script_wit);
131 [ + - - + ]: 12809 : assert(input.HasScripts());
132 [ + - - + ]: 12809 : assert(input.GetScripts().second->stack == script_wit.stack);
133 [ + - - + ]: 25618 : assert(coin_control.GetScripts(out_point).second->stack == script_wit.stack);
134 : 12809 : },
135 : 3890 : [&] {
136 : 3890 : auto& input = coin_control.Select(out_point);
137 : 3890 : unsigned int pos{fuzzed_data_provider.ConsumeIntegral<unsigned int>()};
138 : 3890 : input.SetPosition(pos);
139 [ + - ]: 3890 : assert(input.GetPosition() == pos);
140 [ + - ]: 3890 : assert(coin_control.GetSelectionPos(out_point) == pos);
141 : 3890 : });
142 : : }
143 : 637 : }
144 : : } // namespace
145 : : } // namespace wallet
|