Bitcoin Core  22.99.0
P2P Digital Currency
bech32_tests.cpp
Go to the documentation of this file.
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 BOOST_AUTO_TEST_SUITE(bech32_tests)
13 
14 BOOST_AUTO_TEST_CASE(bech32_testvectors_valid)
15 {
16  static const std::string CASES[] = {
17  "A12UEL5L",
18  "a12uel5l",
19  "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
20  "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
21  "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
22  "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
23  "?1ezyfcl",
24  };
25  for (const std::string& str : CASES) {
26  const auto dec = bech32::Decode(str);
27  BOOST_CHECK(dec.encoding == bech32::Encoding::BECH32);
28  std::string recode = bech32::Encode(bech32::Encoding::BECH32, dec.hrp, dec.data);
29  BOOST_CHECK(!recode.empty());
30  BOOST_CHECK(CaseInsensitiveEqual(str, recode));
31  }
32 }
33 
34 BOOST_AUTO_TEST_CASE(bech32m_testvectors_valid)
35 {
36  static const std::string CASES[] = {
37  "A1LQFN3A",
38  "a1lqfn3a",
39  "an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11sg7hg6",
40  "abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqzd3ryx",
41  "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllludsr8",
42  "split1checkupstagehandshakeupstreamerranterredcaperredlc445v",
43  "?1v759aa"
44  };
45  for (const std::string& str : CASES) {
46  const auto dec = bech32::Decode(str);
47  BOOST_CHECK(dec.encoding == bech32::Encoding::BECH32M);
48  std::string recode = bech32::Encode(bech32::Encoding::BECH32M, dec.hrp, dec.data);
49  BOOST_CHECK(!recode.empty());
50  BOOST_CHECK(CaseInsensitiveEqual(str, recode));
51  }
52 }
53 
54 BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
55 {
56  static const std::string CASES[] = {
57  " 1nwldj5",
58  "\x7f""1axkwrx",
59  "\x80""1eym55h",
60  "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
61  "pzry9x0s0muk",
62  "1pzry9x0s0muk",
63  "x1b4n0q5v",
64  "li1dgmt3",
65  "de1lg7wt\xff",
66  "A1G7SGD8",
67  "10a06t8",
68  "1qzzfhee",
69  "a12UEL5L",
70  "A12uEL5L",
71  };
72  for (const std::string& str : CASES) {
73  const auto dec = bech32::Decode(str);
74  BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
75  }
76 }
77 
78 BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
79 {
80  static const std::string CASES[] = {
81  " 1xj0phk",
82  "\x7f""1g6xzxy",
83  "\x80""1vctc34",
84  "an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4",
85  "qyrz8wqd2c9m",
86  "1qyrz8wqd2c9m",
87  "y1b0jsk6g",
88  "lt1igcx5c0",
89  "in1muywd",
90  "mm1crxm3i",
91  "au1s5cgom",
92  "M1VUXWEZ",
93  "16plkw9",
94  "1p2gdwpf"
95  };
96  for (const std::string& str : CASES) {
97  const auto dec = bech32::Decode(str);
98  BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
99  }
100 }
101 
str.h
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
bech32::Decode
DecodeResult Decode(const std::string &str)
Decode a Bech32 or Bech32m string.
Definition: bech32.cpp:168
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(bech32_testvectors_valid)
Definition: bech32_tests.cpp:14
bech32::Encoding::BECH32
@ BECH32
Bech32 encoding as defined in BIP173.
bech32::Encoding::INVALID
@ INVALID
Failed decoding.
CaseInsensitiveEqual
bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
Definition: str.cpp:10
bech32::Encoding::BECH32M
@ BECH32M
Bech32m encoding as defined in BIP350.
bech32.h
bech32::Encode
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
Definition: bech32.cpp:152
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17