2017-08-26 03:12:39 +02:00
|
|
|
// Copyright (c) 2017 Pieter Wuille
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <bech32.h>
|
|
|
|
#include <test/test_bitcoin.h>
|
2017-08-26 03:12:39 +02:00
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup)
|
|
|
|
|
2018-05-02 17:14:48 +02:00
|
|
|
static bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
|
2017-08-26 03:12:39 +02:00
|
|
|
{
|
|
|
|
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[] = {
|
|
|
|
"A12UEL5L",
|
|
|
|
"a12uel5l",
|
|
|
|
"an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
|
|
|
|
"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
|
|
|
|
"11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
|
|
|
|
"split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
|
|
|
|
"?1ezyfcl",
|
|
|
|
};
|
|
|
|
for (const std::string& str : CASES) {
|
|
|
|
auto ret = bech32::Decode(str);
|
|
|
|
BOOST_CHECK(!ret.first.empty());
|
|
|
|
std::string recode = bech32::Encode(ret.first, ret.second);
|
|
|
|
BOOST_CHECK(!recode.empty());
|
|
|
|
BOOST_CHECK(CaseInsensitiveEqual(str, recode));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
|
|
|
|
{
|
|
|
|
static const std::string CASES[] = {
|
|
|
|
" 1nwldj5",
|
|
|
|
"\x7f""1axkwrx",
|
|
|
|
"\x80""1eym55h",
|
|
|
|
"an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
|
|
|
|
"pzry9x0s0muk",
|
|
|
|
"1pzry9x0s0muk",
|
|
|
|
"x1b4n0q5v",
|
|
|
|
"li1dgmt3",
|
|
|
|
"de1lg7wt\xff",
|
|
|
|
"A1G7SGD8",
|
|
|
|
"10a06t8",
|
|
|
|
"1qzzfhee",
|
2018-04-04 12:24:25 +02:00
|
|
|
"a12UEL5L",
|
|
|
|
"A12uEL5L",
|
2017-08-26 03:12:39 +02:00
|
|
|
};
|
|
|
|
for (const std::string& str : CASES) {
|
|
|
|
auto ret = bech32::Decode(str);
|
|
|
|
BOOST_CHECK(ret.first.empty());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|