/root/bitcoin/src/wallet/test/fuzz/coincontrol.cpp
Line | Count | Source |
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 | | void initialize_coincontrol() |
18 | 0 | { |
19 | 0 | static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>(); |
20 | 0 | g_setup = testing_setup.get(); |
21 | 0 | } |
22 | | |
23 | | FUZZ_TARGET(coincontrol, .init = initialize_coincontrol) |
24 | 798 | { |
25 | 798 | FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); |
26 | 798 | const auto& node = g_setup->m_node; |
27 | 798 | ArgsManager& args = *node.args; |
28 | | |
29 | | // for GetBoolArg to return true sometimes |
30 | 798 | args.ForceSetArg("-avoidpartialspends", fuzzed_data_provider.ConsumeBool()?"1":"0"); Branch (30:45): [True: 567, False: 231]
|
31 | | |
32 | 798 | CCoinControl coin_control; |
33 | 798 | COutPoint out_point; |
34 | | |
35 | 531k | LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) { |
36 | 531k | CallOneOf( |
37 | 531k | fuzzed_data_provider, |
38 | 531k | [&] { |
39 | 22.8k | std::optional<COutPoint> optional_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider); |
40 | 22.8k | if (!optional_out_point) { Branch (40:21): [True: 17.1k, False: 5.70k]
|
41 | 17.1k | return; |
42 | 17.1k | } |
43 | 5.70k | out_point = *optional_out_point; |
44 | 5.70k | }, |
45 | 531k | [&] { |
46 | 27.5k | (void)coin_control.HasSelected(); |
47 | 27.5k | }, |
48 | 531k | [&] { |
49 | 5.49k | (void)coin_control.IsSelected(out_point); |
50 | 5.49k | }, |
51 | 531k | [&] { |
52 | 13.9k | (void)coin_control.IsExternalSelected(out_point); |
53 | 13.9k | }, |
54 | 531k | [&] { |
55 | 69.6k | (void)coin_control.GetExternalOutput(out_point); |
56 | 69.6k | }, |
57 | 531k | [&] { |
58 | 62.1k | (void)coin_control.Select(out_point); |
59 | 62.1k | assert(coin_control.IsSelected(out_point)); Branch (59:17): [True: 62.1k, False: 0]
|
60 | 62.1k | }, |
61 | 531k | [&] { |
62 | 23.9k | const CTxOut tx_out{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)}; |
63 | 23.9k | auto& input = coin_control.Select(out_point); |
64 | 23.9k | const auto set_tx_out{fuzzed_data_provider.ConsumeBool()}; |
65 | 23.9k | if (set_tx_out) { Branch (65:21): [True: 21.0k, False: 2.93k]
|
66 | 21.0k | input.SetTxOut(tx_out); |
67 | 21.0k | } |
68 | 23.9k | auto has_tx_out{input.HasTxOut()}; |
69 | 23.9k | auto is_external_selected{coin_control.IsExternalSelected(out_point)}; |
70 | 23.9k | if (set_tx_out) { Branch (70:21): [True: 21.0k, False: 2.93k]
|
71 | 21.0k | assert(has_tx_out); Branch (71:21): [True: 21.0k, False: 0]
|
72 | 21.0k | assert(input.GetTxOut() == tx_out); Branch (72:21): [True: 21.0k, False: 0]
|
73 | 21.0k | assert(is_external_selected); Branch (73:21): [True: 21.0k, False: 0]
|
74 | 21.0k | } else if (!has_tx_out) { Branch (74:28): [True: 704, False: 2.23k]
|
75 | 704 | assert(!is_external_selected); Branch (75:21): [True: 704, False: 0]
|
76 | 704 | } |
77 | 23.9k | }, |
78 | 531k | [&] { |
79 | 29.5k | coin_control.UnSelect(out_point); |
80 | 29.5k | assert(!coin_control.IsSelected(out_point)); Branch (80:17): [True: 29.5k, False: 0]
|
81 | 29.5k | }, |
82 | 531k | [&] { |
83 | 24.7k | coin_control.UnSelectAll(); |
84 | 24.7k | assert(!coin_control.HasSelected()); Branch (84:17): [True: 24.7k, False: 0]
|
85 | 24.7k | }, |
86 | 531k | [&] { |
87 | 5.16k | const std::vector<COutPoint> selected = coin_control.ListSelected(); |
88 | 10.0k | for (const auto& out : selected) { Branch (88:38): [True: 10.0k, False: 5.16k]
|
89 | 10.0k | assert(coin_control.IsSelected(out)); Branch (89:21): [True: 10.0k, False: 0]
|
90 | 10.0k | } |
91 | 5.16k | }, |
92 | 531k | [&] { |
93 | 7.52k | int64_t weight{fuzzed_data_provider.ConsumeIntegral<int64_t>()}; |
94 | 7.52k | coin_control.SetInputWeight(out_point, weight); |
95 | 7.52k | assert(coin_control.GetInputWeight(out_point) == weight); Branch (95:17): [True: 7.52k, False: 0]
|
96 | 7.52k | }, |
97 | 531k | [&] { |
98 | 7.08k | const bool is_selected = coin_control.IsSelected(out_point); |
99 | 7.08k | assert(!coin_control.GetInputWeight(out_point) || is_selected); Branch (99:17): [True: 6.27k, False: 809]
Branch (99:17): [True: 809, False: 0]
Branch (99:17): [True: 7.08k, False: 0]
|
100 | 7.08k | assert(!coin_control.GetSequence(out_point) || is_selected); Branch (100:17): [True: 6.18k, False: 899]
Branch (100:17): [True: 899, False: 0]
Branch (100:17): [True: 7.08k, False: 0]
|
101 | 7.08k | }, |
102 | 531k | [&] { |
103 | 79.7k | const auto scripts = coin_control.GetScripts(out_point); |
104 | 79.7k | assert(coin_control.IsSelected(out_point) || (!scripts.first && !scripts.second)); Branch (104:17): [True: 17.9k, False: 0]
Branch (104:17): [True: 17.9k, False: 0]
Branch (104:17): [True: 61.8k, False: 17.9k]
Branch (104:17): [True: 79.7k, False: 0]
|
105 | 79.7k | }, |
106 | 531k | [&] { |
107 | 27.5k | assert(coin_control.HasSelectedOrder() || !coin_control.GetSelectionPos(out_point)); Branch (107:17): [True: 24.9k, False: 2.52k]
Branch (107:17): [True: 2.52k, False: 0]
Branch (107:17): [True: 27.5k, False: 0]
|
108 | 27.5k | }, |
109 | 531k | [&] { |
110 | 13.1k | assert(!coin_control.GetSelectionPos(out_point) || coin_control.IsSelected(out_point)); Branch (110:17): [True: 8.39k, False: 4.75k]
Branch (110:17): [True: 4.75k, False: 0]
Branch (110:17): [True: 13.1k, False: 0]
|
111 | 13.1k | }, |
112 | 531k | [&] { |
113 | 30.9k | auto& input = coin_control.Select(out_point); |
114 | 30.9k | uint32_t sequence{fuzzed_data_provider.ConsumeIntegral<uint32_t>()}; |
115 | 30.9k | input.SetSequence(sequence); |
116 | 30.9k | assert(input.GetSequence() == sequence); Branch (116:17): [True: 30.9k, False: 0]
|
117 | 30.9k | assert(coin_control.GetSequence(out_point) == sequence); Branch (117:17): [True: 30.9k, False: 0]
|
118 | 30.9k | }, |
119 | 531k | [&] { |
120 | 51.6k | auto& input = coin_control.Select(out_point); |
121 | 51.6k | const CScript script{ConsumeScript(fuzzed_data_provider)}; |
122 | 51.6k | input.SetScriptSig(script); |
123 | 51.6k | assert(input.HasScripts()); Branch (123:17): [True: 51.6k, False: 0]
|
124 | 51.6k | assert(input.GetScripts().first == script); Branch (124:17): [True: 51.6k, False: 0]
|
125 | 51.6k | assert(coin_control.GetScripts(out_point).first == script); Branch (125:17): [True: 51.6k, False: 0]
|
126 | 51.6k | }, |
127 | 531k | [&] { |
128 | 23.9k | auto& input = coin_control.Select(out_point); |
129 | 23.9k | const CScriptWitness script_wit{ConsumeScriptWitness(fuzzed_data_provider)}; |
130 | 23.9k | input.SetScriptWitness(script_wit); |
131 | 23.9k | assert(input.HasScripts()); Branch (131:17): [True: 23.9k, False: 0]
|
132 | 23.9k | assert(input.GetScripts().second->stack == script_wit.stack); Branch (132:17): [True: 23.9k, False: 0]
|
133 | 23.9k | assert(coin_control.GetScripts(out_point).second->stack == script_wit.stack); Branch (133:17): [True: 23.9k, False: 0]
|
134 | 23.9k | }, |
135 | 531k | [&] { |
136 | 5.00k | auto& input = coin_control.Select(out_point); |
137 | 5.00k | unsigned int pos{fuzzed_data_provider.ConsumeIntegral<unsigned int>()}; |
138 | 5.00k | input.SetPosition(pos); |
139 | 5.00k | assert(input.GetPosition() == pos); Branch (139:17): [True: 5.00k, False: 0]
|
140 | 5.00k | assert(coin_control.GetSelectionPos(out_point) == pos); Branch (140:17): [True: 5.00k, False: 0]
|
141 | 5.00k | }); |
142 | 531k | } |
143 | 798 | } |
144 | | } // namespace |
145 | | } // namespace wallet |