]> Git Repo - VerusCoin.git/blame - src/test/bech32_tests.cpp
Set vrsctest rewards to 12 to match mainnet
[VerusCoin.git] / src / test / bech32_tests.cpp
CommitLineData
10e5357e
PW
1// Copyright (c) 2017 Pieter Wuille
2// Distributed under the MIT software license, see the accompanying
bc909a7a 3// file COPYING or https://www.opensource.org/licenses/mit-license.php .
10e5357e
PW
4
5#include "bech32.h"
6#include "test/test_bitcoin.h"
7
8#include <boost/test/unit_test.hpp>
9
10BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup)
11
12bool 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
25BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
26{
27 static const std::string CASES[] = {
28 "A12UEL5L",
29 "a12uel5l",
30 "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
281b51e5 31 "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
10e5357e
PW
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
46BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
47{
48 static const std::string CASES[] = {
49 " 1nwldj5",
50 "\x7f""1axkwrx",
51 "\x80""1eym55h",
10e5357e
PW
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
c8511dfc
JG
67BOOST_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
10e5357e 100BOOST_AUTO_TEST_SUITE_END()
This page took 0.102794 seconds and 4 git commands to generate.