/root/bitcoin/src/test/fuzz/psbt.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2019-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 <node/psbt.h> |
6 | | #include <psbt.h> |
7 | | #include <pubkey.h> |
8 | | #include <script/script.h> |
9 | | #include <streams.h> |
10 | | #include <test/fuzz/FuzzedDataProvider.h> |
11 | | #include <test/fuzz/fuzz.h> |
12 | | #include <test/util/random.h> |
13 | | #include <util/check.h> |
14 | | |
15 | | #include <cstdint> |
16 | | #include <optional> |
17 | | #include <string> |
18 | | #include <vector> |
19 | | |
20 | | using node::AnalyzePSBT; |
21 | | using node::PSBTAnalysis; |
22 | | using node::PSBTInputAnalysis; |
23 | | |
24 | | FUZZ_TARGET(psbt) |
25 | 0 | { |
26 | 0 | SeedRandomStateForTest(SeedRand::ZEROS); |
27 | 0 | FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; |
28 | 0 | PartiallySignedTransaction psbt_mut; |
29 | 0 | std::string error; |
30 | 0 | auto str = fuzzed_data_provider.ConsumeRandomLengthString(); |
31 | 0 | if (!DecodeRawPSBT(psbt_mut, MakeByteSpan(str), error)) { |
32 | 0 | return; |
33 | 0 | } |
34 | 0 | const PartiallySignedTransaction psbt = psbt_mut; |
35 | |
|
36 | 0 | const PSBTAnalysis analysis = AnalyzePSBT(psbt); |
37 | 0 | (void)PSBTRoleName(analysis.next); |
38 | 0 | for (const PSBTInputAnalysis& input_analysis : analysis.inputs) { |
39 | 0 | (void)PSBTRoleName(input_analysis.next); |
40 | 0 | } |
41 | |
|
42 | 0 | (void)psbt.IsNull(); |
43 | |
|
44 | 0 | std::optional<CMutableTransaction> tx = psbt.tx; |
45 | 0 | if (tx) { |
46 | 0 | const CMutableTransaction& mtx = *tx; |
47 | 0 | const PartiallySignedTransaction psbt_from_tx{mtx}; |
48 | 0 | } |
49 | |
|
50 | 0 | for (const PSBTInput& input : psbt.inputs) { |
51 | 0 | (void)PSBTInputSigned(input); |
52 | 0 | (void)input.IsNull(); |
53 | 0 | } |
54 | 0 | (void)CountPSBTUnsignedInputs(psbt); |
55 | |
|
56 | 0 | for (const PSBTOutput& output : psbt.outputs) { |
57 | 0 | (void)output.IsNull(); |
58 | 0 | } |
59 | |
|
60 | 0 | for (size_t i = 0; i < psbt.tx->vin.size(); ++i) { |
61 | 0 | CTxOut tx_out; |
62 | 0 | if (psbt.GetInputUTXO(tx_out, i)) { |
63 | 0 | (void)tx_out.IsNull(); |
64 | 0 | (void)tx_out.ToString(); |
65 | 0 | } |
66 | 0 | } |
67 | |
|
68 | 0 | psbt_mut = psbt; |
69 | 0 | (void)FinalizePSBT(psbt_mut); |
70 | |
|
71 | 0 | psbt_mut = psbt; |
72 | 0 | CMutableTransaction result; |
73 | 0 | if (FinalizeAndExtractPSBT(psbt_mut, result)) { |
74 | 0 | const PartiallySignedTransaction psbt_from_tx{result}; |
75 | 0 | } |
76 | |
|
77 | 0 | PartiallySignedTransaction psbt_merge; |
78 | 0 | str = fuzzed_data_provider.ConsumeRandomLengthString(); |
79 | 0 | if (!DecodeRawPSBT(psbt_merge, MakeByteSpan(str), error)) { |
80 | 0 | psbt_merge = psbt; |
81 | 0 | } |
82 | 0 | psbt_mut = psbt; |
83 | 0 | (void)psbt_mut.Merge(psbt_merge); |
84 | 0 | psbt_mut = psbt; |
85 | 0 | (void)CombinePSBTs(psbt_mut, {psbt_mut, psbt_merge}); |
86 | 0 | psbt_mut = psbt; |
87 | 0 | for (unsigned int i = 0; i < psbt_merge.tx->vin.size(); ++i) { |
88 | 0 | (void)psbt_mut.AddInput(psbt_merge.tx->vin[i], psbt_merge.inputs[i]); |
89 | 0 | } |
90 | 0 | for (unsigned int i = 0; i < psbt_merge.tx->vout.size(); ++i) { |
91 | 0 | Assert(psbt_mut.AddOutput(psbt_merge.tx->vout[i], psbt_merge.outputs[i])); |
92 | 0 | } |
93 | 0 | psbt_mut.unknown.insert(psbt_merge.unknown.begin(), psbt_merge.unknown.end()); |
94 | 0 | } |