Merge #17357: tests: Add fuzzing harness for Bech32 encoding/decoding
pull/764/headb7541705d0
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: ACKb7541705d0
Tree-SHA512: ade01d30c6886a083b806dbfff08999cc0d08e687701c670c895e261ed242c789e8a0062d4ebbe8f82676b8f168dc37e83351a88822c9c0eab478572a9e1ec02
commit
50591f6ec6
@ -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…
Reference in new issue