1 // Copyright (c) 2017 Pieter Wuille
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "test/test_bitcoin.h"
8 #include <boost/test/unit_test.hpp>
10 BOOST_FIXTURE_TEST_SUITE(bech32_tests
, BasicTestingSetup
)
12 bool CaseInsensitiveEqual(const std::string
&s1
, const std::string
&s2
)
14 if (s1
.size() != s2
.size()) return false;
15 for (size_t i
= 0; i
< s1
.size(); ++i
) {
17 if (c1
>= 'A' && c1
<= 'Z') c1
-= ('A' - 'a');
19 if (c2
>= 'A' && c2
<= 'Z') c2
-= ('A' - 'a');
20 if (c1
!= c2
) return false;
25 BOOST_AUTO_TEST_CASE(bip173_testvectors_valid
)
27 static const std::string CASES
[] = {
30 "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
31 "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
32 "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
33 "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
36 for (const std::string
& str
: CASES
) {
37 auto ret
= bech32::Decode(str
);
38 BOOST_CHECK(!ret
.first
.empty());
39 std::string recode
= bech32::Encode(ret
.first
, ret
.second
);
40 BOOST_CHECK(!recode
.empty());
41 BOOST_CHECK(CaseInsensitiveEqual(str
, recode
));
45 BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid
)
47 static const std::string CASES
[] = {
51 "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
61 for (const std::string
& str
: CASES
) {
62 auto ret
= bech32::Decode(str
);
63 BOOST_CHECK(ret
.first
.empty());
67 BOOST_AUTO_TEST_SUITE_END()