Branch data Line data Source code
1 : : // Copyright (c) 2020-present 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 <pubkey.h>
6 : : #include <script/descriptor.h>
7 : : #include <streams.h>
8 : : #include <test/fuzz/FuzzedDataProvider.h>
9 : : #include <test/fuzz/fuzz.h>
10 : : #include <test/fuzz/util.h>
11 : : #include <util/check.h>
12 : :
13 : : #include <cstdint>
14 : : #include <optional>
15 : : #include <string>
16 : : #include <vector>
17 : :
18 [ + - ]: 645 : FUZZ_TARGET(script_descriptor_cache)
19 : : {
20 : 169 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
21 : 169 : DescriptorCache descriptor_cache;
22 [ + + + + ]: 59067 : LIMITED_WHILE (fuzzed_data_provider.ConsumeBool(), 10000) {
23 [ + - ]: 58898 : const std::vector<uint8_t> code = fuzzed_data_provider.ConsumeBytes<uint8_t>(BIP32_EXTKEY_SIZE);
24 [ - + + + ]: 58898 : if (code.size() == BIP32_EXTKEY_SIZE) {
25 : 58831 : CExtPubKey xpub;
26 [ + - ]: 58831 : SpanReader{code} >> xpub;
27 : 58831 : const uint32_t key_exp_pos = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
28 : 58831 : CExtPubKey xpub_fetched;
29 [ + + ]: 58831 : if (fuzzed_data_provider.ConsumeBool()) {
30 [ + - ]: 52825 : (void)descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched);
31 [ + - ]: 52825 : descriptor_cache.CacheParentExtPubKey(key_exp_pos, xpub);
32 [ + - - + ]: 52825 : assert(descriptor_cache.GetCachedParentExtPubKey(key_exp_pos, xpub_fetched));
33 : : } else {
34 : 6006 : const uint32_t der_index = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
35 [ + - ]: 6006 : (void)descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched);
36 [ + - ]: 6006 : descriptor_cache.CacheDerivedExtPubKey(key_exp_pos, der_index, xpub);
37 [ + - - + ]: 6006 : assert(descriptor_cache.GetCachedDerivedExtPubKey(key_exp_pos, der_index, xpub_fetched));
38 : : }
39 [ - + ]: 58831 : assert(xpub == xpub_fetched);
40 : 58831 : }
41 [ + - ]: 58898 : (void)descriptor_cache.GetCachedParentExtPubKeys();
42 [ + - ]: 117796 : (void)descriptor_cache.GetCachedDerivedExtPubKeys();
43 : 58898 : }
44 : 169 : }
|