-
Notifications
You must be signed in to change notification settings - Fork 381
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge c3b4715 into merged_master (Bitcoin PR #18206)
- Loading branch information
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 <bloom.h> | ||
#include <optional.h> | ||
#include <primitives/transaction.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
#include <uint256.h> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
|
||
CBloomFilter bloom_filter{ | ||
fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 10000000), | ||
1.0 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max()), | ||
fuzzed_data_provider.ConsumeIntegral<unsigned int>(), | ||
static_cast<unsigned char>(fuzzed_data_provider.PickValueInArray({BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))}; | ||
while (fuzzed_data_provider.remaining_bytes() > 0) { | ||
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 6)) { | ||
case 0: { | ||
const std::vector<unsigned char>& b = ConsumeRandomLengthByteVector(fuzzed_data_provider); | ||
(void)bloom_filter.contains(b); | ||
bloom_filter.insert(b); | ||
const bool present = bloom_filter.contains(b); | ||
assert(present); | ||
break; | ||
} | ||
case 1: { | ||
const Optional<COutPoint> out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider); | ||
if (!out_point) { | ||
break; | ||
} | ||
(void)bloom_filter.contains(*out_point); | ||
bloom_filter.insert(*out_point); | ||
const bool present = bloom_filter.contains(*out_point); | ||
assert(present); | ||
break; | ||
} | ||
case 2: { | ||
const Optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider); | ||
if (!u256) { | ||
break; | ||
} | ||
(void)bloom_filter.contains(*u256); | ||
bloom_filter.insert(*u256); | ||
const bool present = bloom_filter.contains(*u256); | ||
assert(present); | ||
break; | ||
} | ||
case 3: | ||
bloom_filter.clear(); | ||
break; | ||
case 4: | ||
bloom_filter.reset(fuzzed_data_provider.ConsumeIntegral<unsigned int>()); | ||
break; | ||
case 5: { | ||
const Optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); | ||
if (!mut_tx) { | ||
break; | ||
} | ||
const CTransaction tx{*mut_tx}; | ||
(void)bloom_filter.IsRelevantAndUpdate(tx); | ||
break; | ||
} | ||
case 6: | ||
bloom_filter.UpdateEmptyFull(); | ||
break; | ||
} | ||
(void)bloom_filter.IsWithinSizeConstraints(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 <bloom.h> | ||
#include <optional.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <test/fuzz/fuzz.h> | ||
#include <test/fuzz/util.h> | ||
#include <uint256.h> | ||
|
||
#include <cassert> | ||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
void test_one_input(const std::vector<uint8_t>& buffer) | ||
{ | ||
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); | ||
|
||
CRollingBloomFilter rolling_bloom_filter{ | ||
fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, 1000), | ||
0.999 / fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(1, std::numeric_limits<unsigned int>::max())}; | ||
while (fuzzed_data_provider.remaining_bytes() > 0) { | ||
switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 2)) { | ||
case 0: { | ||
const std::vector<unsigned char>& b = ConsumeRandomLengthByteVector(fuzzed_data_provider); | ||
(void)rolling_bloom_filter.contains(b); | ||
rolling_bloom_filter.insert(b); | ||
const bool present = rolling_bloom_filter.contains(b); | ||
assert(present); | ||
break; | ||
} | ||
case 1: { | ||
const Optional<uint256> u256 = ConsumeDeserializable<uint256>(fuzzed_data_provider); | ||
if (!u256) { | ||
break; | ||
} | ||
(void)rolling_bloom_filter.contains(*u256); | ||
rolling_bloom_filter.insert(*u256); | ||
const bool present = rolling_bloom_filter.contains(*u256); | ||
assert(present); | ||
break; | ||
} | ||
case 2: | ||
rolling_bloom_filter.reset(); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) 2009-2019 The Bitcoin Core developers | ||
// Distributed under the MIT software license, see the accompanying | ||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
||
#ifndef BITCOIN_TEST_FUZZ_UTIL_H | ||
#define BITCOIN_TEST_FUZZ_UTIL_H | ||
|
||
#include <attributes.h> | ||
#include <optional.h> | ||
#include <serialize.h> | ||
#include <streams.h> | ||
#include <test/fuzz/FuzzedDataProvider.h> | ||
#include <version.h> | ||
|
||
#include <cstdint> | ||
#include <string> | ||
#include <vector> | ||
|
||
NODISCARD inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, size_t max_length = 4096) noexcept | ||
{ | ||
const std::string s = fuzzed_data_provider.ConsumeRandomLengthString(max_length); | ||
return {s.begin(), s.end()}; | ||
} | ||
|
||
template <typename T> | ||
NODISCARD inline Optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, size_t max_length = 4096) noexcept | ||
{ | ||
const std::vector<uint8_t>& buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length); | ||
CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION}; | ||
T obj; | ||
try { | ||
ds >> obj; | ||
} catch (const std::ios_base::failure&) { | ||
return nullopt; | ||
} | ||
return obj; | ||
} | ||
|
||
#endif // BITCOIN_TEST_FUZZ_UTIL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters