Skip to content

Commit 03dfa36

Browse files
author
MarcoFalke
committed
Merge #17229: tests: Add fuzzing harnesses for various Base{32,58,64} and hex related functions
c184057 tests: Add fuzzing harness for various hex related functions (practicalswift) 526dd78 tests: Add fuzzing harness for various Base{32,58,64} related functions (practicalswift) 32e2712 util: Move TrimString(...). Introduce default pattern (trims whitespace). Add NODISCARD. (practicalswift) 22d9bae tests: Add corpora suppression (FUZZERS_MISSING_CORPORA) for fuzzers missing in https://github.com/bitcoin-core/qa-assets/tree/master/fuzz_seed_corpus (practicalswift) Pull request description: Add fuzzing harnesses for various Base{32,58,64} and hex related functions. **Testing this PR** Run: ``` $ CC=clang CXX=clang++ ./configure --enable-fuzz \ --with-sanitizers=address,fuzzer,undefined $ make $ src/test/fuzz/base_encode_decode … $ src/test/fuzz/hex … ``` ACKs for top commit: MarcoFalke: ACK c184057 🔁 Tree-SHA512: 4fcbe4f641fc553e43fd5c3c40a6beec0d2ce90c5ffc718213b37fc18aba4c055e51e26f93d01ea1248fd89473d07c9dce77db7f014b47d3abd045f61b5f1905
2 parents 94c6f2b + c184057 commit 03dfa36

File tree

6 files changed

+96
-10
lines changed

6 files changed

+96
-10
lines changed

src/Makefile.test.include

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ FUZZ_TARGETS = \
88
test/fuzz/address_deserialize \
99
test/fuzz/addrman_deserialize \
1010
test/fuzz/banentry_deserialize \
11+
test/fuzz/base_encode_decode \
1112
test/fuzz/bech32 \
1213
test/fuzz/block \
1314
test/fuzz/block_deserialize \
@@ -27,6 +28,7 @@ FUZZ_TARGETS = \
2728
test/fuzz/eval_script \
2829
test/fuzz/fee_rate_deserialize \
2930
test/fuzz/flat_file_pos_deserialize \
31+
test/fuzz/hex \
3032
test/fuzz/integer \
3133
test/fuzz/inv_deserialize \
3234
test/fuzz/key_origin_info_deserialize \
@@ -278,6 +280,12 @@ test_fuzz_bech32_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
278280
test_fuzz_bech32_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
279281
test_fuzz_bech32_LDADD = $(FUZZ_SUITE_LD_COMMON)
280282

283+
test_fuzz_base_encode_decode_SOURCES = $(FUZZ_SUITE) test/fuzz/base_encode_decode.cpp
284+
test_fuzz_base_encode_decode_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
285+
test_fuzz_base_encode_decode_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
286+
test_fuzz_base_encode_decode_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
287+
test_fuzz_base_encode_decode_LDADD = $(FUZZ_SUITE_LD_COMMON)
288+
281289
test_fuzz_txundo_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
282290
test_fuzz_txundo_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXUNDO_DESERIALIZE=1
283291
test_fuzz_txundo_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
@@ -356,6 +364,12 @@ test_fuzz_address_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
356364
test_fuzz_address_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
357365
test_fuzz_address_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
358366

367+
test_fuzz_hex_SOURCES = $(FUZZ_SUITE) test/fuzz/hex.cpp
368+
test_fuzz_hex_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
369+
test_fuzz_hex_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
370+
test_fuzz_hex_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
371+
test_fuzz_hex_LDADD = $(FUZZ_SUITE_LD_COMMON)
372+
359373
test_fuzz_inv_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
360374
test_fuzz_inv_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DINV_DESERIALIZE=1
361375
test_fuzz_inv_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/fuzz/base_encode_decode.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) 2019 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 <test/fuzz/fuzz.h>
6+
7+
#include <base58.h>
8+
#include <util/string.h>
9+
#include <util/strencodings.h>
10+
11+
#include <cassert>
12+
#include <cstdint>
13+
#include <string>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
const std::string random_encoded_string(buffer.begin(), buffer.end());
19+
20+
std::vector<unsigned char> decoded;
21+
if (DecodeBase58(random_encoded_string, decoded, 100)) {
22+
const std::string encoded_string = EncodeBase58(decoded);
23+
assert(encoded_string == TrimString(encoded_string));
24+
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
25+
}
26+
27+
if (DecodeBase58Check(random_encoded_string, decoded, 100)) {
28+
const std::string encoded_string = EncodeBase58Check(decoded);
29+
assert(encoded_string == TrimString(encoded_string));
30+
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
31+
}
32+
33+
bool pf_invalid;
34+
std::string decoded_string = DecodeBase32(random_encoded_string, &pf_invalid);
35+
if (!pf_invalid) {
36+
const std::string encoded_string = EncodeBase32(decoded_string);
37+
assert(encoded_string == TrimString(encoded_string));
38+
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
39+
}
40+
41+
decoded_string = DecodeBase64(random_encoded_string, &pf_invalid);
42+
if (!pf_invalid) {
43+
const std::string encoded_string = EncodeBase64(decoded_string);
44+
assert(encoded_string == TrimString(encoded_string));
45+
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
46+
}
47+
}

src/test/fuzz/hex.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2019 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 <test/fuzz/fuzz.h>
6+
7+
#include <util/strencodings.h>
8+
9+
#include <cassert>
10+
#include <cstdint>
11+
#include <string>
12+
#include <vector>
13+
14+
void test_one_input(const std::vector<uint8_t>& buffer)
15+
{
16+
const std::string random_hex_string(buffer.begin(), buffer.end());
17+
const std::vector<unsigned char> data = ParseHex(random_hex_string);
18+
const std::string hex_data = HexStr(data);
19+
if (IsHex(random_hex_string)) {
20+
assert(ToLower(random_hex_string) == hex_data);
21+
}
22+
}

src/util/string.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
#include <string>
1212
#include <vector>
1313

14+
NODISCARD inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
15+
{
16+
std::string::size_type front = str.find_first_not_of(pattern);
17+
if (front == std::string::npos) {
18+
return std::string();
19+
}
20+
std::string::size_type end = str.find_last_not_of(pattern);
21+
return str.substr(front, end - front + 1);
22+
}
23+
1424
/**
1525
* Join a list of items
1626
*

src/util/system.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <chainparamsbase.h>
99
#include <util/strencodings.h>
10+
#include <util/string.h>
1011
#include <util/translation.h>
1112

1213

@@ -679,16 +680,6 @@ fs::path GetConfigFile(const std::string& confPath)
679680
return AbsPathForConfigVal(fs::path(confPath), false);
680681
}
681682

682-
static std::string TrimString(const std::string& str, const std::string& pattern)
683-
{
684-
std::string::size_type front = str.find_first_not_of(pattern);
685-
if (front == std::string::npos) {
686-
return std::string();
687-
}
688-
std::string::size_type end = str.find_last_not_of(pattern);
689-
return str.substr(front, end - front + 1);
690-
}
691-
692683
static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
693684
{
694685
std::string str, prefix;

test/fuzz/test_runner.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
# Fuzzers known to lack a seed corpus in https://github.com/bitcoin-core/qa-assets/tree/master/fuzz_seed_corpus
1616
FUZZERS_MISSING_CORPORA = [
1717
"addr_info_deserialize",
18+
"base_encode_decode",
1819
"block",
1920
"block_file_info_deserialize",
2021
"block_filter_deserialize",
2122
"block_header_and_short_txids_deserialize",
2223
"fee_rate_deserialize",
2324
"flat_file_pos_deserialize",
25+
"hex",
2426
"integer",
2527
"key_origin_info_deserialize",
2628
"merkle_block_deserialize",

0 commit comments

Comments
 (0)