mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
tests: Add fuzzing harness for functions/classes in span.h
This commit is contained in:
parent
9718f38f54
commit
f205cf7fef
2 changed files with 46 additions and 0 deletions
|
@ -100,6 +100,7 @@ FUZZ_TARGETS = \
|
|||
test/fuzz/service_deserialize \
|
||||
test/fuzz/signature_checker \
|
||||
test/fuzz/snapshotmetadata_deserialize \
|
||||
test/fuzz/span \
|
||||
test/fuzz/spanparsing \
|
||||
test/fuzz/string \
|
||||
test/fuzz/strprintf \
|
||||
|
@ -872,6 +873,12 @@ test_fuzz_snapshotmetadata_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
|||
test_fuzz_snapshotmetadata_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_snapshotmetadata_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
|
||||
|
||||
test_fuzz_span_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_span_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_span_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
test_fuzz_span_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_span_SOURCES = $(FUZZ_SUITE) test/fuzz/span.cpp
|
||||
|
||||
test_fuzz_spanparsing_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_spanparsing_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_spanparsing_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
|
|
39
src/test/fuzz/span.cpp
Normal file
39
src/test/fuzz/span.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright (c) 2020 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <span.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <test/fuzz/util.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
|
||||
std::string str = fuzzed_data_provider.ConsumeBytesAsString(32);
|
||||
const Span<const char> span = MakeSpan(str);
|
||||
(void)span.data();
|
||||
(void)span.begin();
|
||||
(void)span.end();
|
||||
if (span.size() > 0) {
|
||||
const std::ptrdiff_t idx = fuzzed_data_provider.ConsumeIntegralInRange<std::ptrdiff_t>(0U, span.size() - 1U);
|
||||
(void)span.first(idx);
|
||||
(void)span.last(idx);
|
||||
(void)span.subspan(idx);
|
||||
(void)span.subspan(idx, span.size() - idx);
|
||||
(void)span[idx];
|
||||
}
|
||||
|
||||
std::string another_str = fuzzed_data_provider.ConsumeBytesAsString(32);
|
||||
const Span<const char> another_span = MakeSpan(another_str);
|
||||
assert((span <= another_span) != (span > another_span));
|
||||
assert((span == another_span) != (span != another_span));
|
||||
assert((span >= another_span) != (span < another_span));
|
||||
}
|
Loading…
Add table
Reference in a new issue