Skip to content

Commit

Permalink
Merge #17357: tests: Add fuzzing harness for Bech32 encoding/decoding
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoFalke committed Nov 5, 2019
2 parents c7e6b3b + b754170 commit 50591f6
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 17 deletions.
19 changes: 15 additions & 4 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FUZZ_TARGETS = \
test/fuzz/address_deserialize \
test/fuzz/addrman_deserialize \
test/fuzz/banentry_deserialize \
test/fuzz/bech32 \
test/fuzz/block_deserialize \
test/fuzz/blockheader_deserialize \
test/fuzz/blocklocator_deserialize \
Expand Down Expand Up @@ -62,14 +63,18 @@ BITCOIN_TEST_SUITE = \
test/lib/transaction_utils.h \
test/main.cpp \
test/setup_common.h \
test/setup_common.cpp
test/setup_common.cpp \
test/util/str.h \
test/util/str.cpp

FUZZ_SUITE = \
test/setup_common.h \
test/setup_common.cpp \
test/fuzz/fuzz.cpp \
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 = \
$(LIBBITCOIN_SERVER) \
Expand Down Expand Up @@ -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_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_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DTXUNDO_DESERIALIZE=1
test_fuzz_txundo_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
Expand Down
14 changes: 1 addition & 13 deletions src/test/bech32_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@

#include <bech32.h>
#include <test/setup_common.h>
#include <test/util/str.h>

#include <boost/test/unit_test.hpp>

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)
{
static const std::string CASES[] = {
Expand Down
43 changes: 43 additions & 0 deletions src/test/fuzz/bech32.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions src/test/util/str.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
12 changes: 12 additions & 0 deletions src/test/util/str.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 50591f6

Please sign in to comment.