LCOV - code coverage report
Current view: top level - src/test - bech32_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 45 45 100.0 %
Date: 2025-02-23 09:33:43 Functions: 7 7 100.0 %

          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/test_pivx.h"
       7             : 
       8             : #include <boost/test/unit_test.hpp>
       9             : 
      10             : BOOST_FIXTURE_TEST_SUITE(bech32_tests, BasicTestingSetup)
      11             : 
      12           8 : bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2)
      13             : {
      14           8 :     if (s1.size() != s2.size()) return false;
      15         408 :     for (size_t i = 0; i < s1.size(); ++i) {
      16         400 :         char c1 = s1[i];
      17         400 :         if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a');
      18         400 :         char c2 = s2[i];
      19         400 :         if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a');
      20         400 :         if (c1 != c2) return false;
      21             :     }
      22             :     return true;
      23             : }
      24             : 
      25           2 : BOOST_AUTO_TEST_CASE(bip173_testvectors_valid)
      26             : {
      27           1 :     static const std::string CASES[] = {
      28             :         "A12UEL5L",
      29             :         "a12uel5l",
      30             :         "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs",
      31             :         "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx",
      32             :         "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
      33             :         "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j",
      34             :         "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w",
      35             :         "?1ezyfcl",
      36           1 :     };
      37           9 :     for (const std::string& str : CASES) {
      38           8 :         auto ret = bech32::Decode(str);
      39           8 :         BOOST_CHECK(!ret.first.empty());
      40          16 :         std::string recode = bech32::Encode(ret.first, ret.second);
      41          16 :         BOOST_CHECK(!recode.empty());
      42          16 :         BOOST_CHECK(CaseInsensitiveEqual(str, recode));
      43             :     }
      44           1 : }
      45             : 
      46           2 : BOOST_AUTO_TEST_CASE(bip173_testvectors_invalid)
      47             : {
      48           1 :     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           1 :     };
      61          12 :     for (const std::string& str : CASES) {
      62          22 :         auto ret = bech32::Decode(str);
      63          22 :         BOOST_CHECK(ret.first.empty());
      64             :     }
      65           1 : }
      66             : 
      67           2 : BOOST_AUTO_TEST_CASE(bech32_deterministic_valid)
      68             : {
      69         256 :     for (size_t i = 0; i < 255; i++) {
      70         510 :         std::vector<unsigned char> input(32, i);
      71         510 :         auto encoded = bech32::Encode("a", input);
      72         255 :         if (i < 32) {
      73             :             // Valid input
      74          64 :             BOOST_CHECK(!encoded.empty());
      75          64 :             auto ret = bech32::Decode(encoded);
      76          64 :             BOOST_CHECK(ret.first == "a");
      77          64 :             BOOST_CHECK(ret.second == input);
      78             :         } else {
      79             :             // Invalid input
      80         446 :             BOOST_CHECK(encoded.empty());
      81             :         }
      82             :     }
      83             : 
      84         256 :     for (size_t i = 0; i < 255; i++) {
      85         510 :         std::vector<unsigned char> input(43, i);
      86         510 :         auto encoded = bech32::Encode("a", input);
      87         255 :         if (i < 32) {
      88             :             // Valid input
      89          64 :             BOOST_CHECK(!encoded.empty());
      90          64 :             auto ret = bech32::Decode(encoded);
      91          64 :             BOOST_CHECK(ret.first == "a");
      92          64 :             BOOST_CHECK(ret.second == input);
      93             :         } else {
      94             :             // Invalid input
      95         446 :             BOOST_CHECK(encoded.empty());
      96             :         }
      97             :     }
      98           1 : }
      99             : 
     100             : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14