Merge #17357: tests: Add fuzzing harness for Bech32 encoding/decoding

b7541705d0 tests: Add fuzzing harness for Bech32 encoding/decoding (practicalswift)
85a34b1683 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 b7541705d0

Tree-SHA512: ade01d30c6886a083b806dbfff08999cc0d08e687701c670c895e261ed242c789e8a0062d4ebbe8f82676b8f168dc37e83351a88822c9c0eab478572a9e1ec02
pull/764/head
MarcoFalke 5 years ago
commit 50591f6ec6
No known key found for this signature in database
GPG Key ID: D2EA4850E7528B25

@ -7,6 +7,7 @@ FUZZ_TARGETS = \
test/fuzz/address_deserialize \ test/fuzz/address_deserialize \
test/fuzz/addrman_deserialize \ test/fuzz/addrman_deserialize \
test/fuzz/banentry_deserialize \ test/fuzz/banentry_deserialize \
test/fuzz/bech32 \
test/fuzz/block_deserialize \ test/fuzz/block_deserialize \
test/fuzz/blockheader_deserialize \ test/fuzz/blockheader_deserialize \
test/fuzz/blocklocator_deserialize \ test/fuzz/blocklocator_deserialize \
@ -62,14 +63,18 @@ BITCOIN_TEST_SUITE = \
test/lib/transaction_utils.h \ test/lib/transaction_utils.h \
test/main.cpp \ test/main.cpp \
test/setup_common.h \ test/setup_common.h \
test/setup_common.cpp test/setup_common.cpp \
test/util/str.h \
test/util/str.cpp
FUZZ_SUITE = \ FUZZ_SUITE = \
test/setup_common.h \
test/setup_common.cpp \
test/fuzz/fuzz.cpp \ test/fuzz/fuzz.cpp \
test/fuzz/fuzz.h \ test/fuzz/fuzz.h \
test/fuzz/FuzzedDataProvider.h test/fuzz/FuzzedDataProvider.h \
test/setup_common.cpp \
test/setup_common.h \
test/util/str.cpp \
test/util/str.h
FUZZ_SUITE_LD_COMMON = \ FUZZ_SUITE_LD_COMMON = \
$(LIBBITCOIN_SERVER) \ $(LIBBITCOIN_SERVER) \
@ -242,6 +247,12 @@ test_fuzz_banentry_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
test_fuzz_banentry_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS) test_fuzz_banentry_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
test_fuzz_banentry_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON) test_fuzz_banentry_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
test_fuzz_bech32_SOURCES = $(FUZZ_SUITE) test/fuzz/bech32.cpp
test_fuzz_bech32_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
test_fuzz_bech32_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
test_fuzz_bech32_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
test_fuzz_bech32_LDADD = $(FUZZ_SUITE_LD_COMMON)
test_fuzz_txundo_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp test_fuzz_txundo_deserialize_SOURCES = $(FUZZ_SUITE) test/fuzz/deserialize.cpp
test_fuzz_txundo_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXUNDO_DESERIALIZE=1 test_fuzz_txundo_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXUNDO_DESERIALIZE=1
test_fuzz_txundo_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) test_fuzz_txundo_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

@ -4,24 +4,12 @@
#include <bech32.h> #include <bech32.h>
#include <test/setup_common.h> #include <test/setup_common.h>
#include <test/util/str.h>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup)
static bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
{
if (s1.size() != s2.size()) return false;
for (size_t i = 0; i < s1.size(); ++i) {
char c1 = s1[i];
if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
char c2 = s2[i];
if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
if (c1 != c2) return false;
}
return true;
}
BOOST_AUTO_TEST_CASE(bip173_testvectors_valid) BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
{ {
static const std::string CASES[] = { static const std::string CASES[] = {

@ -0,0 +1,43 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <bech32.h>
#include <test/fuzz/fuzz.h>
#include <test/util/str.h>
#include <util/strencodings.h>
#include <cassert>
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
void test_one_input(const std::vector<uint8_t>& buffer)
{
const std::string random_string(buffer.begin(), buffer.end());
const std::pair<std::string, std::vector<uint8_t>> r1 = bech32::Decode(random_string);
if (r1.first.empty()) {
assert(r1.second.empty());
} else {
const std::string& hrp = r1.first;
const std::vector<uint8_t>& data = r1.second;
const std::string reencoded = bech32::Encode(hrp, data);
assert(CaseInsensitiveEqual(random_string, reencoded));
}
std::vector<unsigned char> input;
ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end());
const std::string encoded = bech32::Encode("bc", input);
assert(!encoded.empty());
const std::pair<std::string, std::vector<uint8_t>> r2 = bech32::Decode(encoded);
if (r2.first.empty()) {
assert(r2.second.empty());
} else {
const std::string& hrp = r2.first;
const std::vector<uint8_t>& data = r2.second;
assert(hrp == "bc");
assert(data == input);
}
}

@ -0,0 +1,21 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/str.h>
#include <cstdint>
#include <string>
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2)
{
if (s1.size() != s2.size()) return false;
for (size_t i = 0; i < s1.size(); ++i) {
char c1 = s1[i];
if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
char c2 = s2[i];
if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
if (c1 != c2) return false;
}
return true;
}

@ -0,0 +1,12 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_UTIL_STR_H
#define BITCOIN_TEST_UTIL_STR_H
#include <string>
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2);
#endif // BITCOIN_TEST_UTIL_STR_H
Loading…
Cancel
Save