Skip to content

Commit 50591f6

Browse files
author
MarcoFalke
committed
Merge #17357: tests: Add fuzzing harness for Bech32 encoding/decoding
b754170 tests: Add fuzzing harness for Bech32 encoding/decoding (practicalswift) 85a34b1 tests: Move CaseInsensitiveEqual to test/util/str (practicalswift) Pull request description: Add fuzzing harness for Bech32 encoding/decoding. **Testing this PR** Run: ``` $ make distclean $ ./autogen.sh $ CC=clang CXX=clang++ ./configure --enable-fuzz \ --with-sanitizers=address,fuzzer,undefined $ make $ src/test/fuzz/bech32 -max_total_time=60 … ``` ACKs for top commit: jonatack: ACK b754170 Tree-SHA512: ade01d30c6886a083b806dbfff08999cc0d08e687701c670c895e261ed242c789e8a0062d4ebbe8f82676b8f168dc37e83351a88822c9c0eab478572a9e1ec02
2 parents c7e6b3b + b754170 commit 50591f6

File tree

5 files changed

+92
-17
lines changed

5 files changed

+92
-17
lines changed

src/Makefile.test.include

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ FUZZ_TARGETS = \
77
test/fuzz/address_deserialize \
88
test/fuzz/addrman_deserialize \
99
test/fuzz/banentry_deserialize \
10+
test/fuzz/bech32 \
1011
test/fuzz/block_deserialize \
1112
test/fuzz/blockheader_deserialize \
1213
test/fuzz/blocklocator_deserialize \
@@ -62,14 +63,18 @@ BITCOIN_TEST_SUITE = \
6263
test/lib/transaction_utils.h \
6364
test/main.cpp \
6465
test/setup_common.h \
65-
test/setup_common.cpp
66+
test/setup_common.cpp \
67+
test/util/str.h \
68+
test/util/str.cpp
6669

6770
FUZZ_SUITE = \
68-
test/setup_common.h \
69-
test/setup_common.cpp \
7071
test/fuzz/fuzz.cpp \
7172
test/fuzz/fuzz.h \
72-
test/fuzz/FuzzedDataProvider.h
73+
test/fuzz/FuzzedDataProvider.h \
74+
test/setup_common.cpp \
75+
test/setup_common.h \
76+
test/util/str.cpp \
77+
test/util/str.h
7378

7479
FUZZ_SUITE_LD_COMMON = \
7580
$(LIBBITCOIN_SERVER) \
@@ -242,6 +247,12 @@ test_fuzz_banentry_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
242247
test_fuzz_banentry_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
243248
test_fuzz_banentry_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
244249

250+
test_fuzz_bech32_SOURCES = $(FUZZ_SUITE) test/fuzz/bech32.cpp
251+
test_fuzz_bech32_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
252+
test_fuzz_bech32_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
253+
test_fuzz_bech32_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
254+
test_fuzz_bech32_LDADD = $(FUZZ_SUITE_LD_COMMON)
255+
245256
test_fuzz_txundo_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
246257
test_fuzz_txundo_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXUNDO_DESERIALIZE=1
247258
test_fuzz_txundo_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/bech32_tests.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@
44

55
#include <bech32.h>
66
#include <test/setup_common.h>
7+
#include <test/util/str.h>
78

89
#include <boost/test/unit_test.hpp>
910

1011
BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup)
1112

12-
static bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
13-
{
14-
if (s1.size() != s2.size()) return false;
15-
for (size_t i = 0; i < s1.size(); ++i) {
16-
char c1 = s1[i];
17-
if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
18-
char c2 = s2[i];
19-
if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
20-
if (c1 != c2) return false;
21-
}
22-
return true;
23-
}
24-
2513
BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
2614
{
2715
static const std::string CASES[] = {

src/test/fuzz/bech32.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 <bech32.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <test/util/str.h>
8+
#include <util/strencodings.h>
9+
10+
#include <cassert>
11+
#include <cstdint>
12+
#include <string>
13+
#include <utility>
14+
#include <vector>
15+
16+
void test_one_input(const std::vector<uint8_t>& buffer)
17+
{
18+
const std::string random_string(buffer.begin(), buffer.end());
19+
const std::pair<std::string, std::vector<uint8_t>> r1 = bech32::Decode(random_string);
20+
if (r1.first.empty()) {
21+
assert(r1.second.empty());
22+
} else {
23+
const std::string& hrp = r1.first;
24+
const std::vector<uint8_t>& data = r1.second;
25+
const std::string reencoded = bech32::Encode(hrp, data);
26+
assert(CaseInsensitiveEqual(random_string, reencoded));
27+
}
28+
29+
std::vector<unsigned char> input;
30+
ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());
31+
const std::string encoded = bech32::Encode("bc", input);
32+
assert(!encoded.empty());
33+
34+
const std::pair<std::string, std::vector<uint8_t>> r2 = bech32::Decode(encoded);
35+
if (r2.first.empty()) {
36+
assert(r2.second.empty());
37+
} else {
38+
const std::string& hrp = r2.first;
39+
const std::vector<uint8_t>& data = r2.second;
40+
assert(hrp == "bc");
41+
assert(data == input);
42+
}
43+
}

src/test/util/str.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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/util/str.h>
6+
7+
#include <cstdint>
8+
#include <string>
9+
10+
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2)
11+
{
12+
if (s1.size() != s2.size()) return false;
13+
for (size_t i = 0; i < s1.size(); ++i) {
14+
char c1 = s1[i];
15+
if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
16+
char c2 = s2[i];
17+
if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
18+
if (c1 != c2) return false;
19+
}
20+
return true;
21+
}

src/test/util/str.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
#ifndef BITCOIN_TEST_UTIL_STR_H
6+
#define BITCOIN_TEST_UTIL_STR_H
7+
8+
#include <string>
9+
10+
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2);
11+
12+
#endif // BITCOIN_TEST_UTIL_STR_H

0 commit comments

Comments
 (0)