Skip to content

Commit 43ff0d9

Browse files
tests: Add fuzzing harness for functions in timedata.h
1 parent a8695db commit 43ff0d9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ FUZZ_TARGETS = \
9898
test/fuzz/string \
9999
test/fuzz/strprintf \
100100
test/fuzz/sub_net_deserialize \
101+
test/fuzz/timedata \
101102
test/fuzz/transaction \
102103
test/fuzz/tx_in \
103104
test/fuzz/tx_in_deserialize \
@@ -853,6 +854,12 @@ test_fuzz_sub_net_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
853854
test_fuzz_sub_net_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
854855
test_fuzz_sub_net_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
855856

857+
test_fuzz_timedata_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
858+
test_fuzz_timedata_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
859+
test_fuzz_timedata_LDADD = $(FUZZ_SUITE_LD_COMMON)
860+
test_fuzz_timedata_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
861+
test_fuzz_timedata_SOURCES = $(FUZZ_SUITE) test/fuzz/timedata.cpp
862+
856863
test_fuzz_transaction_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
857864
test_fuzz_transaction_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
858865
test_fuzz_transaction_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/timedata.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 <test/fuzz/FuzzedDataProvider.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <test/fuzz/util.h>
8+
#include <timedata.h>
9+
10+
#include <cstdint>
11+
#include <string>
12+
#include <vector>
13+
14+
void test_one_input(const std::vector<uint8_t>& buffer)
15+
{
16+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
17+
const unsigned int max_size = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 1000);
18+
// Divide by 2 to avoid signed integer overflow in .median()
19+
const int64_t initial_value = fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2;
20+
CMedianFilter<int64_t> median_filter{max_size, initial_value};
21+
while (fuzzed_data_provider.remaining_bytes() > 0) {
22+
(void)median_filter.median();
23+
assert(median_filter.size() > 0);
24+
assert(static_cast<size_t>(median_filter.size()) == median_filter.sorted().size());
25+
assert(static_cast<unsigned int>(median_filter.size()) <= max_size || max_size == 0);
26+
// Divide by 2 to avoid signed integer overflow in .median()
27+
median_filter.input(fuzzed_data_provider.ConsumeIntegral<int64_t>() / 2);
28+
}
29+
}

0 commit comments

Comments
 (0)