Skip to content

Commit c7ea12d

Browse files
tests: Add key_io fuzzing harness
1 parent 3d28c88 commit c7ea12d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ FUZZ_TARGETS = \
3636
test/fuzz/integer \
3737
test/fuzz/inv_deserialize \
3838
test/fuzz/key \
39+
test/fuzz/key_io \
3940
test/fuzz/key_origin_info_deserialize \
4041
test/fuzz/locale \
4142
test/fuzz/merkle_block_deserialize \
@@ -438,6 +439,12 @@ test_fuzz_key_LDADD = $(FUZZ_SUITE_LD_COMMON)
438439
test_fuzz_key_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
439440
test_fuzz_key_SOURCES = $(FUZZ_SUITE) test/fuzz/key.cpp
440441

442+
test_fuzz_key_io_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
443+
test_fuzz_key_io_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
444+
test_fuzz_key_io_LDADD = $(FUZZ_SUITE_LD_COMMON)
445+
test_fuzz_key_io_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
446+
test_fuzz_key_io_SOURCES = $(FUZZ_SUITE) test/fuzz/key_io.cpp
447+
441448
test_fuzz_key_origin_info_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DKEY_ORIGIN_INFO_DESERIALIZE=1
442449
test_fuzz_key_origin_info_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
443450
test_fuzz_key_origin_info_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/key_io.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 <chainparams.h>
6+
#include <key_io.h>
7+
#include <rpc/util.h>
8+
#include <script/signingprovider.h>
9+
#include <script/standard.h>
10+
#include <test/fuzz/fuzz.h>
11+
12+
#include <cassert>
13+
#include <cstdint>
14+
#include <string>
15+
#include <vector>
16+
17+
void initialize()
18+
{
19+
SelectParams(CBaseChainParams::REGTEST);
20+
}
21+
22+
void test_one_input(const std::vector<uint8_t>& buffer)
23+
{
24+
const std::string random_string(buffer.begin(), buffer.end());
25+
26+
const CKey key = DecodeSecret(random_string);
27+
if (key.IsValid()) {
28+
assert(key == DecodeSecret(EncodeSecret(key)));
29+
}
30+
31+
const CExtKey ext_key = DecodeExtKey(random_string);
32+
if (ext_key.key.size() == 32) {
33+
assert(ext_key == DecodeExtKey(EncodeExtKey(ext_key)));
34+
}
35+
36+
const CExtPubKey ext_pub_key = DecodeExtPubKey(random_string);
37+
if (ext_pub_key.pubkey.size() == CPubKey::COMPRESSED_SIZE) {
38+
assert(ext_pub_key == DecodeExtPubKey(EncodeExtPubKey(ext_pub_key)));
39+
}
40+
41+
const CTxDestination tx_destination = DecodeDestination(random_string);
42+
(void)DescribeAddress(tx_destination);
43+
(void)GetKeyForDestination(/* store */ {}, tx_destination);
44+
(void)GetScriptForDestination(tx_destination);
45+
(void)IsValidDestination(tx_destination);
46+
47+
(void)IsValidDestinationString(random_string);
48+
}

test/fuzz/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"flat_file_pos_deserialize",
2828
"float",
2929
"hex",
30+
"key_io",
3031
"integer",
3132
"key",
3233
"key_origin_info_deserialize",

0 commit comments

Comments
 (0)