PageRenderTime 1700ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/bech32_tests.cpp

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