Line data Source code
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.
4 :
5 : #include <bech32.h>
6 : #include <test/util/str.h>
7 :
8 : #include <boost/test/unit_test.hpp>
9 :
10 : #include <string>
11 :
12 146 : BOOST_AUTO_TEST_SUITE(bech32_tests)
13 :
14 148 : BOOST_AUTO_TEST_CASE(bech32_testvectors_valid)
15 : {
16 1 : static const std::string CASES[] = {
17 1 : "A12UEL5L",
18 1 : "a12uel5l",
19 1 : "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
20 1 : "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
21 1 : "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
22 1 : "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
23 1 : "?1ezyfcl",
24 : };
25 8 : for (const std::string& str : CASES) {
26 7 : const auto dec = bech32::Decode(str);
27 7 : BOOST_CHECK(dec.encoding == bech32::Encoding::BECH32);
28 7 : std::string recode = bech32::Encode(bech32::Encoding::BECH32, dec.hrp, dec.data);
29 7 : BOOST_CHECK(!recode.empty());
30 7 : BOOST_CHECK(CaseInsensitiveEqual(str, recode));
31 7 : }
32 1 : }
33 :
34 148 : BOOST_AUTO_TEST_CASE(bech32m_testvectors_valid)
35 : {
36 1 : static const std::string CASES[] = {
37 1 : "A1LQFN3A",
38 1 : "a1lqfn3a",
39 1 : "an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6",
40 1 : "abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx",
41 1 : "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8",
42 1 : "split1checkupstagehandshakeupstreamerranterredcaperredlc445v",
43 1 : "?1v759aa"
44 : };
45 8 : for (const std::string& str : CASES) {
46 7 : const auto dec = bech32::Decode(str);
47 7 : BOOST_CHECK(dec.encoding == bech32::Encoding::BECH32M);
48 7 : std::string recode = bech32::Encode(bech32::Encoding::BECH32M, dec.hrp, dec.data);
49 7 : BOOST_CHECK(!recode.empty());
50 7 : BOOST_CHECK(CaseInsensitiveEqual(str, recode));
51 7 : }
52 1 : }
53 :
54 148 : BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
55 : {
56 1 : static const std::string CASES[] = {
57 1 : " 1nwldj5",
58 1 : "\x7f""1axkwrx",
59 1 : "\x80""1eym55h",
60 1 : "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
61 1 : "pzry9x0s0muk",
62 1 : "1pzry9x0s0muk",
63 1 : "x1b4n0q5v",
64 1 : "li1dgmt3",
65 1 : "de1lg7wt\xff",
66 1 : "A1G7SGD8",
67 1 : "10a06t8",
68 1 : "1qzzfhee",
69 1 : "a12UEL5L",
70 1 : "A12uEL5L",
71 : };
72 15 : for (const std::string& str : CASES) {
73 14 : const auto dec = bech32::Decode(str);
74 14 : BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
75 14 : }
76 1 : }
77 :
78 148 : BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
79 : {
80 1 : static const std::string CASES[] = {
81 1 : " 1xj0phk",
82 1 : "\x7f""1g6xzxy",
83 1 : "\x80""1vctc34",
84 1 : "an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4",
85 1 : "qyrz8wqd2c9m",
86 1 : "1qyrz8wqd2c9m",
87 1 : "y1b0jsk6g",
88 1 : "lt1igcx5c0",
89 1 : "in1muywd",
90 1 : "mm1crxm3i",
91 1 : "au1s5cgom",
92 1 : "M1VUXWEZ",
93 1 : "16plkw9",
94 1 : "1p2gdwpf"
95 : };
96 15 : for (const std::string& str : CASES) {
97 14 : const auto dec = bech32::Decode(str);
98 14 : BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
99 14 : }
100 1 : }
101 :
102 146 : BOOST_AUTO_TEST_SUITE_END()
|