-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
5 changed files
with
92 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |