Skip to content

Commit 3052130

Browse files
author
MarcoFalke
committed
Merge #17136: tests: Add fuzzing harness for various PSBT related functions
49f4c7f tests: Add fuzzing harness for various PSBT related functions (practicalswift) Pull request description: Add fuzzing harness for various PSBT related functions. **Testing this PR** Run: ``` $ CC=clang CXX=clang++ ./configure --enable-fuzz \ --with-sanitizers=address,fuzzer,undefined $ make $ src/test/fuzz/psbt ``` ACKs for top commit: MarcoFalke: re-ACK 49f4c7f 🐟 Tree-SHA512: 4cebe62bd8c244ee40a43e829f5bd175ab40e1dfbbab1affb1529374858225820d6c9fa9ba45862bf56c1522845422fd96d620cedbdec52a67ac1449dec4e1b2
2 parents 397c6d3 + 49f4c7f commit 3052130

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ FUZZ_TARGETS = \
2424
test/fuzz/messageheader_deserialize \
2525
test/fuzz/netaddr_deserialize \
2626
test/fuzz/parse_iso8601 \
27+
test/fuzz/psbt \
2728
test/fuzz/script \
2829
test/fuzz/script_flags \
2930
test/fuzz/service_deserialize \
@@ -293,6 +294,12 @@ test_fuzz_parse_iso8601_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
293294
test_fuzz_parse_iso8601_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
294295
test_fuzz_parse_iso8601_LDADD = $(FUZZ_SUITE_LD_COMMON)
295296

297+
test_fuzz_psbt_SOURCES = $(FUZZ_SUITE) test/fuzz/psbt.cpp
298+
test_fuzz_psbt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
299+
test_fuzz_psbt_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
300+
test_fuzz_psbt_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
301+
test_fuzz_psbt_LDADD = $(FUZZ_SUITE_LD_COMMON)
302+
296303
test_fuzz_script_SOURCES = $(FUZZ_SUITE) test/fuzz/script.cpp
297304
test_fuzz_script_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
298305
test_fuzz_script_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/fuzz/psbt.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2019 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/fuzz.h>
6+
7+
#include <node/psbt.h>
8+
#include <optional.h>
9+
#include <psbt.h>
10+
#include <pubkey.h>
11+
#include <script/script.h>
12+
#include <streams.h>
13+
#include <util/memory.h>
14+
#include <version.h>
15+
16+
#include <cstdint>
17+
#include <string>
18+
#include <vector>
19+
20+
void initialize()
21+
{
22+
static const auto verify_handle = MakeUnique<ECCVerifyHandle>();
23+
}
24+
25+
void test_one_input(const std::vector<uint8_t>& buffer)
26+
{
27+
PartiallySignedTransaction psbt_mut;
28+
const std::string raw_psbt{buffer.begin(), buffer.end()};
29+
std::string error;
30+
if (!DecodeRawPSBT(psbt_mut, raw_psbt, error)) {
31+
return;
32+
}
33+
const PartiallySignedTransaction psbt = psbt_mut;
34+
35+
const PSBTAnalysis analysis = AnalyzePSBT(psbt);
36+
(void)PSBTRoleName(analysis.next);
37+
for (const PSBTInputAnalysis& input_analysis : analysis.inputs) {
38+
(void)PSBTRoleName(input_analysis.next);
39+
}
40+
41+
(void)psbt.IsNull();
42+
(void)psbt.IsSane();
43+
44+
Optional<CMutableTransaction> tx = psbt.tx;
45+
if (tx) {
46+
const CMutableTransaction& mtx = *tx;
47+
const PartiallySignedTransaction psbt_from_tx{mtx};
48+
}
49+
50+
for (const PSBTInput& input : psbt.inputs) {
51+
(void)PSBTInputSigned(input);
52+
(void)input.IsNull();
53+
(void)input.IsSane();
54+
}
55+
56+
for (const PSBTOutput& output : psbt.outputs) {
57+
(void)output.IsNull();
58+
}
59+
60+
for (size_t i = 0; i < psbt.tx->vin.size(); ++i) {
61+
CTxOut tx_out;
62+
if (psbt.GetInputUTXO(tx_out, i)) {
63+
(void)tx_out.IsNull();
64+
(void)tx_out.ToString();
65+
}
66+
}
67+
68+
psbt_mut = psbt;
69+
(void)FinalizePSBT(psbt_mut);
70+
71+
psbt_mut = psbt;
72+
CMutableTransaction result;
73+
if (FinalizeAndExtractPSBT(psbt_mut, result)) {
74+
const PartiallySignedTransaction psbt_from_tx{result};
75+
}
76+
77+
psbt_mut = psbt;
78+
(void)psbt_mut.Merge(psbt);
79+
}

0 commit comments

Comments
 (0)