Skip to content

Commit ad6c348

Browse files
tests: Add fuzzing harness for CBlockPolicyEstimator::{Read,Write} (policy/fees.h)
1 parent 614e080 commit ad6c348

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ FUZZ_TARGETS = \
7777
test/fuzz/partial_merkle_tree_deserialize \
7878
test/fuzz/partially_signed_transaction_deserialize \
7979
test/fuzz/policy_estimator \
80+
test/fuzz/policy_estimator_io \
8081
test/fuzz/pow \
8182
test/fuzz/prefilled_transaction_deserialize \
8283
test/fuzz/prevector \
@@ -761,6 +762,12 @@ test_fuzz_policy_estimator_LDADD = $(FUZZ_SUITE_LD_COMMON)
761762
test_fuzz_policy_estimator_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
762763
test_fuzz_policy_estimator_SOURCES = test/fuzz/policy_estimator.cpp
763764

765+
test_fuzz_policy_estimator_io_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
766+
test_fuzz_policy_estimator_io_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
767+
test_fuzz_policy_estimator_io_LDADD = $(FUZZ_SUITE_LD_COMMON)
768+
test_fuzz_policy_estimator_io_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
769+
test_fuzz_policy_estimator_io_SOURCES = test/fuzz/policy_estimator_io.cpp
770+
764771
test_fuzz_pow_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
765772
test_fuzz_pow_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
766773
test_fuzz_pow_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/policy_estimator.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <string>
1515
#include <vector>
1616

17+
void initialize()
18+
{
19+
InitializeFuzzingContext();
20+
}
21+
1722
void test_one_input(const std::vector<uint8_t>& buffer)
1823
{
1924
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
@@ -66,4 +71,10 @@ void test_one_input(const std::vector<uint8_t>& buffer)
6671
(void)block_policy_estimator.estimateSmartFee(fuzzed_data_provider.ConsumeIntegral<int>(), fuzzed_data_provider.ConsumeBool() ? &fee_calculation : nullptr, fuzzed_data_provider.ConsumeBool());
6772
(void)block_policy_estimator.HighestTargetTracked(fuzzed_data_provider.PickValueInArray({FeeEstimateHorizon::SHORT_HALFLIFE, FeeEstimateHorizon::MED_HALFLIFE, FeeEstimateHorizon::LONG_HALFLIFE}));
6873
}
74+
{
75+
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
76+
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
77+
block_policy_estimator.Write(fuzzed_auto_file);
78+
block_policy_estimator.Read(fuzzed_auto_file);
79+
}
6980
}

src/test/fuzz/policy_estimator_io.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 <policy/fees.h>
6+
#include <test/fuzz/FuzzedDataProvider.h>
7+
#include <test/fuzz/fuzz.h>
8+
#include <test/fuzz/util.h>
9+
10+
#include <cstdint>
11+
#include <vector>
12+
13+
void initialize()
14+
{
15+
InitializeFuzzingContext();
16+
}
17+
18+
void test_one_input(const std::vector<uint8_t>& buffer)
19+
{
20+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
21+
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
22+
CAutoFile fuzzed_auto_file = fuzzed_auto_file_provider.open();
23+
// Re-using block_policy_estimator across runs to avoid costly creation of CBlockPolicyEstimator object.
24+
static CBlockPolicyEstimator block_policy_estimator;
25+
if (block_policy_estimator.Read(fuzzed_auto_file)) {
26+
block_policy_estimator.Write(fuzzed_auto_file);
27+
}
28+
}

0 commit comments

Comments
 (0)