Skip to content

Commit fa80117

Browse files
tests: Add fuzzing harness for functions in script/descriptor.h
1 parent 43fb8f0 commit fa80117

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ FUZZ_TARGETS = \
111111
test/fuzz/rolling_bloom_filter \
112112
test/fuzz/script \
113113
test/fuzz/script_bitcoin_consensus \
114+
test/fuzz/script_descriptor_cache \
114115
test/fuzz/script_deserialize \
115116
test/fuzz/script_flags \
116117
test/fuzz/script_ops \
@@ -948,6 +949,12 @@ test_fuzz_script_bitcoin_consensus_LDADD = $(FUZZ_SUITE_LD_COMMON)
948949
test_fuzz_script_bitcoin_consensus_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
949950
test_fuzz_script_bitcoin_consensus_SOURCES = test/fuzz/script_bitcoin_consensus.cpp
950951

952+
test_fuzz_script_descriptor_cache_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
953+
test_fuzz_script_descriptor_cache_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
954+
test_fuzz_script_descriptor_cache_LDADD = $(FUZZ_SUITE_LD_COMMON)
955+
test_fuzz_script_descriptor_cache_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
956+
test_fuzz_script_descriptor_cache_SOURCES = test/fuzz/script_descriptor_cache.cpp
957+
951958
test_fuzz_script_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DSCRIPT_DESERIALIZE=1
952959
test_fuzz_script_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
953960
test_fuzz_script_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 <optional.h>
6+
#include <pubkey.h>
7+
#include <script/descriptor.h>
8+
#include <test/fuzz/FuzzedDataProvider.h>
9+
#include <test/fuzz/fuzz.h>
10+
#include <test/fuzz/util.h>
11+
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
19+
DescriptorCache descriptor_cache;
20+
while (fuzzed_data_provider.ConsumeBool()) {
21+
const std::vector<uint8_t> code = fuzzed_data_provider.ConsumeBytes<uint8_t>(BIP32_EXTKEY_SIZE);
22+
if (code.size() == BIP32_EXTKEY_SIZE) {
23+
CExtPubKey xpub;
24+
xpub.Decode(code.data());
25+
const uint32_t key_exp_pos = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
26+
CExtPubKey xpub_fetched;
27+
if (fuzzed_data_provider.ConsumeBool()) {
28+
(void)descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched);
29+
descriptor_cache.CacheParentExtPubKey(key_exp_pos, xpub);
30+
assert(descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched));
31+
} else {
32+
const uint32_t der_index = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
33+
(void)descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched);
34+
descriptor_cache.CacheDerivedExtPubKey(key_exp_pos, der_index, xpub);
35+
assert(descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched));
36+
}
37+
assert(xpub == xpub_fetched);
38+
}
39+
(void)descriptor_cache.GetCachedParentExtPubKeys();
40+
(void)descriptor_cache.GetCachedDerivedExtPubKeys();
41+
}
42+
}

0 commit comments

Comments
 (0)