From 340add223a89bef3a8fbeb4408587d768325c9a4 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 2 Feb 2021 15:54:25 +0000 Subject: [PATCH] test: Convert Bech32 test vectors into known-answer test vectors This enables other projects to confirm independently that their encoding or decoding functions are consistent, instead of merely that they are round-trip correct. --- src/test/bech32_tests.cpp | 45 ++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/src/test/bech32_tests.cpp b/src/test/bech32_tests.cpp index ac4779006..8d81644c1 100644 --- a/src/test/bech32_tests.cpp +++ b/src/test/bech32_tests.cpp @@ -4,6 +4,7 @@ #include "bech32.h" #include "test/test_bitcoin.h" +#include "utilstrencodings.h" #include @@ -24,19 +25,43 @@ bool CaseInsensitiveEqual(const std::string &s1, const std::string &s2) BOOST_AUTO_TEST_CASE(bip173_testvectors_valid) { - static const std::string CASES[] = { - "A12UEL5L", - "a12uel5l", - "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", - "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", - "abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", - "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", - "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", - "?1ezyfcl", + static const std::tuple CASES[] = { + {"A12UEL5L", "a", ""}, + {"a12uel5l", "a", ""}, + { + "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1tt5tgs", + "an83characterlonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio", + "", + }, + { + "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", + "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio", + "", + }, + {"abcdef1qpzry9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", "abcdef", "00443214c74254b635cf84653a56d7c675be77df"}, + { + "11qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqc8247j", + "1", + "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", + }, + { + "split1checkupstagehandshakeupstreamerranterredcaperred2y9e3w", + "split", + "c5f38b70305f519bf66d85fb6cf03058f3dde463ecd7918f2dc743918f2d", + }, + {"?1ezyfcl", "?", ""}, }; - for (const std::string& str : CASES) { + for (const auto& [str, hrp, data] : CASES) { auto ret = bech32::Decode(str); BOOST_CHECK(!ret.first.empty()); + BOOST_CHECK_EQUAL(ret.first, hrp); + + std::vector decoded; + decoded.reserve((ret.second.size() * 5) / 8); + auto success = ConvertBits<5, 8, false>([&](unsigned char c) { decoded.push_back(c); }, ret.second.begin(), ret.second.end()); + BOOST_CHECK(success); + BOOST_CHECK_EQUAL(HexStr(decoded.begin(), decoded.end()), data); + std::string recode = bech32::Encode(ret.first, ret.second); BOOST_CHECK(!recode.empty()); BOOST_CHECK(CaseInsensitiveEqual(str, recode));