Skip to content

Commit d7930c4

Browse files
tests: Add fuzzing harness for functions/classes in protocol.h
1 parent 7f9dedb commit d7930c4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ FUZZ_TARGETS = \
8383
test/fuzz/process_message_tx \
8484
test/fuzz/process_message_verack \
8585
test/fuzz/process_message_version \
86+
test/fuzz/protocol \
8687
test/fuzz/psbt \
8788
test/fuzz/psbt_input_deserialize \
8889
test/fuzz/psbt_output_deserialize \
@@ -766,6 +767,12 @@ test_fuzz_process_message_version_LDADD = $(FUZZ_SUITE_LD_COMMON)
766767
test_fuzz_process_message_version_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
767768
test_fuzz_process_message_version_SOURCES = $(FUZZ_SUITE) test/fuzz/process_message.cpp
768769

770+
test_fuzz_protocol_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
771+
test_fuzz_protocol_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
772+
test_fuzz_protocol_LDADD = $(FUZZ_SUITE_LD_COMMON)
773+
test_fuzz_protocol_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
774+
test_fuzz_protocol_SOURCES = $(FUZZ_SUITE) test/fuzz/protocol.cpp
775+
769776
test_fuzz_psbt_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
770777
test_fuzz_psbt_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
771778
test_fuzz_psbt_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/protocol.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2020 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 <optional.h>
6+
#include <protocol.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
11+
#include <cstdint>
12+
#include <stdexcept>
13+
#include <vector>
14+
15+
void test_one_input(const std::vector<uint8_t>& buffer)
16+
{
17+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
18+
const Optional<CInv> inv = ConsumeDeserializable<CInv>(fuzzed_data_provider);
19+
if (!inv) {
20+
return;
21+
}
22+
try {
23+
(void)inv->GetCommand();
24+
} catch (const std::out_of_range&) {
25+
}
26+
(void)inv->ToString();
27+
const Optional<CInv> another_inv = ConsumeDeserializable<CInv>(fuzzed_data_provider);
28+
if (!another_inv) {
29+
return;
30+
}
31+
(void)(*inv < *another_inv);
32+
}

0 commit comments

Comments
 (0)