Skip to content

Commit c184057

Browse files
tests: Add fuzzing harness for various hex related functions
1 parent 526dd78 commit c184057

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ FUZZ_TARGETS = \
2828
test/fuzz/eval_script \
2929
test/fuzz/fee_rate_deserialize \
3030
test/fuzz/flat_file_pos_deserialize \
31+
test/fuzz/hex \
3132
test/fuzz/integer \
3233
test/fuzz/inv_deserialize \
3334
test/fuzz/key_origin_info_deserialize \
@@ -363,6 +364,12 @@ test_fuzz_address_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
363364
test_fuzz_address_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
364365
test_fuzz_address_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
365366

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+
366373
test_fuzz_inv_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
367374
test_fuzz_inv_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DINV_DESERIALIZE=1
368375
test_fuzz_inv_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

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+
}

0 commit comments

Comments
 (0)