|
| 1 | +// Copyright (c) 2019 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <bech32.h> |
| 6 | +#include <test/fuzz/fuzz.h> |
| 7 | +#include <test/util/str.h> |
| 8 | +#include <util/strencodings.h> |
| 9 | + |
| 10 | +#include <cassert> |
| 11 | +#include <cstdint> |
| 12 | +#include <string> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +void test_one_input(const std::vector<uint8_t>& buffer) |
| 17 | +{ |
| 18 | + const std::string random_string(buffer.begin(), buffer.end()); |
| 19 | + const std::pair<std::string, std::vector<uint8_t>> r1 = bech32::Decode(random_string); |
| 20 | + if (r1.first.empty()) { |
| 21 | + assert(r1.second.empty()); |
| 22 | + } else { |
| 23 | + const std::string& hrp = r1.first; |
| 24 | + const std::vector<uint8_t>& data = r1.second; |
| 25 | + const std::string reencoded = bech32::Encode(hrp, data); |
| 26 | + assert(CaseInsensitiveEqual(random_string, reencoded)); |
| 27 | + } |
| 28 | + |
| 29 | + std::vector<unsigned char> input; |
| 30 | + ConvertBits<8, 5, true>([&](unsigned char c) { input.push_back(c); }, buffer.begin(), buffer.end()); |
| 31 | + const std::string encoded = bech32::Encode("bc", input); |
| 32 | + assert(!encoded.empty()); |
| 33 | + |
| 34 | + const std::pair<std::string, std::vector<uint8_t>> r2 = bech32::Decode(encoded); |
| 35 | + if (r2.first.empty()) { |
| 36 | + assert(r2.second.empty()); |
| 37 | + } else { |
| 38 | + const std::string& hrp = r2.first; |
| 39 | + const std::vector<uint8_t>& data = r2.second; |
| 40 | + assert(hrp == "bc"); |
| 41 | + assert(data == input); |
| 42 | + } |
| 43 | +} |
0 commit comments