PIVX Core  5.6.99
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/test_pivx.h"
7 
8 #include <boost/test/unit_test.hpp>
9 
11 
12 bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
13 {
14  if (s1.size() != s2.size()) return false;
15  for (size_t i = 0; i < s1.size(); ++i) {
16  char c1 = s1[i];
17  if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
18  char c2 = s2[i];
19  if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
20  if (c1 != c2) return false;
21  }
22  return true;
23 }
24 
25 BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
26 {
27  static const std::string CASES[] = {
28  "A12UEL5L",
29  "a12uel5l",
30  "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
31  "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
32  "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
33  "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
34  "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
35  "?1ezyfcl",
36  };
37  for (const std::string& str : CASES) {
38  auto ret = bech32::Decode(str);
39  BOOST_CHECK(!ret.first.empty());
40  std::string recode = bech32::Encode(ret.first, ret.second);
41  BOOST_CHECK(!recode.empty());
42  BOOST_CHECK(CaseInsensitiveEqual(str, recode));
43  }
44 }
45 
46 BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
47 {
48  static const std::string CASES[] = {
49  " 1nwldj5",
50  "\x7f""1axkwrx",
51  "\x80""1eym55h",
52  "pzry9x0s0muk",
53  "1pzry9x0s0muk",
54  "x1b4n0q5v",
55  "li1dgmt3",
56  "de1lg7wt\xff",
57  "A1G7SGD8",
58  "10a06t8",
59  "1qzzfhee",
60  };
61  for (const std::string& str : CASES) {
62  auto ret = bech32::Decode(str);
63  BOOST_CHECK(ret.first.empty());
64  }
65 }
66 
67 BOOST_AUTO_TEST_CASE(bech32_deterministic_valid)
68 {
69  for (size_t i = 0; i < 255; i++) {
70  std::vector<unsigned char> input(32, i);
71  auto encoded = bech32::Encode("a", input);
72  if (i < 32) {
73  // Valid input
74  BOOST_CHECK(!encoded.empty());
75  auto ret = bech32::Decode(encoded);
76  BOOST_CHECK(ret.first == "a");
77  BOOST_CHECK(ret.second == input);
78  } else {
79  // Invalid input
80  BOOST_CHECK(encoded.empty());
81  }
82  }
83 
84  for (size_t i = 0; i < 255; i++) {
85  std::vector<unsigned char> input(43, i);
86  auto encoded = bech32::Encode("a", input);
87  if (i < 32) {
88  // Valid input
89  BOOST_CHECK(!encoded.empty());
90  auto ret = bech32::Decode(encoded);
91  BOOST_CHECK(ret.first == "a");
92  BOOST_CHECK(ret.second == input);
93  } else {
94  // Invalid input
95  BOOST_CHECK(encoded.empty());
96  }
97  }
98 }
99 
BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
BOOST_AUTO_TEST_SUITE_END()
std::pair< std::string, data > Decode(const std::string &str)
Decode a Bech32 string.
Definition: bech32.cpp:158
std::string Encode(const std::string &hrp, const data &values)
Encode a Bech32 string.
Definition: bech32.cpp:143
Definition: uint256.h:212
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Basic testing setup.
Definition: test_pivx.h:51