aboutsummaryrefslogtreecommitdiff
path: root/src/test/fuzz/bech32.cpp
diff options
context:
space:
mode:
authorpracticalswift <practicalswift@users.noreply.github.com>2019-11-05 09:18:27 +0000
committerpracticalswift <practicalswift@users.noreply.github.com>2019-11-05 09:23:44 +0000
commitb7541705d0abfbddf682a0134f3fa8a8e1d06cdf (patch)
tree02b0e4db554798ba96d4f6932e9b6bfba3defbd2 /src/test/fuzz/bech32.cpp
parent85a34b1683233060c4500cfb8e3f5d84c8b8f0da (diff)
downloadbitcoin-b7541705d0abfbddf682a0134f3fa8a8e1d06cdf.tar.xz
tests: Add fuzzing harness for Bech32 encoding/decoding
Diffstat (limited to 'src/test/fuzz/bech32.cpp')
-rw-r--r--src/test/fuzz/bech32.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/test/fuzz/bech32.cpp b/src/test/fuzz/bech32.cpp
new file mode 100644
index 0000000000..8b91f9bc96
--- /dev/null
+++ b/src/test/fuzz/bech32.cpp
@@ -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);
+ }
+}