From d13fb4994b692ad9b88ef71757bd7451aaebcaa3 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 9 Aug 2021 21:54:29 +0000 Subject: [PATCH] blech32: add test vectors for blech32 and blech32m Converted the bech32 valid test vectors. Did not convert the invalid test vectors because it was not obvious how. --- src/test/blech32_tests.cpp | 89 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/test/blech32_tests.cpp b/src/test/blech32_tests.cpp index 95ba4fbf2c..d812657399 100644 --- a/src/test/blech32_tests.cpp +++ b/src/test/blech32_tests.cpp @@ -6,11 +6,100 @@ #include #include #include +#include #include BOOST_FIXTURE_TEST_SUITE(blech32_tests, BasicTestingSetup) +BOOST_AUTO_TEST_CASE(blech32_testvectors_valid) +{ + static const std::string CASES[] = { + "A133NZFWEYK7UT", + "a133nzfweyk7ut", + "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio195jhgldwsn5j", + "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lgmcn7l7t7xve", + "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xldwlutcw2l", + "split1checkupstagehandshakeupstreamerranterredcaperredegneyqml9esp", + "?19dv34t3p4s35", + }; + for (const std::string& str : CASES) { + const auto dec = blech32::Decode(str); + BOOST_CHECK(dec.encoding == blech32::Encoding::BLECH32); + std::string recode = blech32::Encode(blech32::Encoding::BLECH32, dec.hrp, dec.data); + BOOST_CHECK(!recode.empty()); + BOOST_CHECK(CaseInsensitiveEqual(str, recode)); + } +} + +BOOST_AUTO_TEST_CASE(blech32m_testvectors_valid) +{ + static const std::string CASES[] = { + "A1EYL4VXQ3HRPT", + "a1eyl4vxq3hrpt", + "an83characterlonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11yatn6l85muud", + "abcdef1l7aum6echk45nj3s0wdvt2fg8x9yrzpqg8m5pqg67zq3", + "11llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll09wxh8ajdvxv", + "split1checkupstagehandshakeupstreamerranterredcaperred3alwpgz2yydp", + "?1dcqxsrg55dv5" + }; + for (const std::string& str : CASES) { + const auto dec = blech32::Decode(str); + BOOST_CHECK(dec.encoding == blech32::Encoding::BLECH32M); + std::string recode = blech32::Encode(blech32::Encoding::BLECH32M, dec.hrp, dec.data); + BOOST_CHECK(!recode.empty()); + BOOST_CHECK(CaseInsensitiveEqual(str, recode)); + } +} + +BOOST_AUTO_TEST_CASE(blech32m_testvectors_invalid) +{ + static const std::string CASES[] = { + " 1xj0phk", + "\x7f""1g6xzxy", + "\x80""1vctc34", + "an84characterslonghumanreadablepartthatcontainsthetheexcludedcharactersbioandnumber11d6pts4", + "qyrz8wqd2c9m", + "1qyrz8wqd2c9m", + "y1b0jsk6g", + "lt1igcx5c0", + "in1muywd", + "mm1crxm3i", + "au1s5cgom", + "M1VUXWEZ", + "16plkw9", + "1p2gdwpf" + }; + for (const std::string& str : CASES) { + const auto dec = blech32::Decode(str); + BOOST_CHECK(dec.encoding == blech32::Encoding::INVALID); + } +} + +BOOST_AUTO_TEST_CASE(blech32_testvectors_invalid) +{ + static const std::string CASES[] = { + " 1nwldj5", + "\x7f""1axkwrx", + "\x80""1eym55h", + "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", + "pzry9x0s0muk", + "1pzry9x0s0muk", + "x1b4n0q5v", + "li1dgmt3", + "de1lg7wt\xff", + "A1G7SGD8", + "10a06t8", + "1qzzfhee", + "a12UEL5L", + "A12uEL5L", + }; + for (const std::string& str : CASES) { + const auto dec = blech32::Decode(str); + BOOST_CHECK(dec.encoding == blech32::Encoding::INVALID); + } +} + BOOST_AUTO_TEST_CASE(blech32_polymod_sanity) { std::vector data(40);