mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
tests: Add fuzzing harness for functions taking floating-point types as input
This commit is contained in:
parent
c2bd588860
commit
3c82b92d2e
2 changed files with 49 additions and 0 deletions
|
@ -29,6 +29,7 @@ FUZZ_TARGETS = \
|
|||
test/fuzz/eval_script \
|
||||
test/fuzz/fee_rate_deserialize \
|
||||
test/fuzz/flat_file_pos_deserialize \
|
||||
test/fuzz/float \
|
||||
test/fuzz/hex \
|
||||
test/fuzz/integer \
|
||||
test/fuzz/inv_deserialize \
|
||||
|
@ -387,6 +388,12 @@ test_fuzz_flat_file_pos_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
|||
test_fuzz_flat_file_pos_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_flat_file_pos_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
|
||||
|
||||
test_fuzz_float_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_float_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_float_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
test_fuzz_float_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
|
||||
test_fuzz_float_SOURCES = $(FUZZ_SUITE) test/fuzz/float.cpp
|
||||
|
||||
test_fuzz_hex_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
|
||||
test_fuzz_hex_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||
test_fuzz_hex_LDADD = $(FUZZ_SUITE_LD_COMMON)
|
||||
|
|
42
src/test/fuzz/float.cpp
Normal file
42
src/test/fuzz/float.cpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
// 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 <memusage.h>
|
||||
#include <serialize.h>
|
||||
#include <streams.h>
|
||||
#include <test/fuzz/FuzzedDataProvider.h>
|
||||
#include <test/fuzz/fuzz.h>
|
||||
#include <version.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
void test_one_input(const std::vector<uint8_t>& buffer)
|
||||
{
|
||||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
|
||||
|
||||
{
|
||||
const double d = fuzzed_data_provider.ConsumeFloatingPoint<double>();
|
||||
(void)memusage::DynamicUsage(d);
|
||||
assert(ser_uint64_to_double(ser_double_to_uint64(d)) == d);
|
||||
|
||||
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
|
||||
stream << d;
|
||||
double d_deserialized;
|
||||
stream >> d_deserialized;
|
||||
assert(d == d_deserialized);
|
||||
}
|
||||
|
||||
{
|
||||
const float f = fuzzed_data_provider.ConsumeFloatingPoint<float>();
|
||||
(void)memusage::DynamicUsage(f);
|
||||
assert(ser_uint32_to_float(ser_float_to_uint32(f)) == f);
|
||||
|
||||
CDataStream stream(SER_NETWORK, INIT_PROTO_VERSION);
|
||||
stream << f;
|
||||
float f_deserialized;
|
||||
stream >> f_deserialized;
|
||||
assert(f == f_deserialized);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue