diff options
Diffstat (limited to 'src/test/bech32_tests.cpp')
-rw-r--r-- | src/test/bech32_tests.cpp | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/src/test/bech32_tests.cpp b/src/test/bech32_tests.cpp index c0344b3cbb..51a1d1199e 100644 --- a/src/test/bech32_tests.cpp +++ b/src/test/bech32_tests.cpp @@ -1,4 +1,5 @@ // Copyright (c) 2017 Pieter Wuille +// Copyright (c) 2021 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -68,10 +69,38 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid) "1qzzfhee", "a12UEL5L", "A12uEL5L", + "abcdef1qpzrz9x8gf2tvdw0s3jn54khce6mua7lmqqqxw", + "test1zg69w7y6hn0aqy352euf40x77qddq3dc", }; + static const std::pair<std::string, std::vector<int>> ERRORS[] = { + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Bech32 string too long", {90}}, + {"Missing separator", {}}, + {"Invalid separator position", {0}}, + {"Invalid Base 32 character", {2}}, + {"Invalid separator position", {2}}, + {"Invalid character or mixed case", {8}}, + {"Invalid checksum", {}}, // The checksum is calculated using the uppercase form so the entire string is invalid, not just a few characters + {"Invalid separator position", {0}}, + {"Invalid separator position", {0}}, + {"Invalid character or mixed case", {3, 4, 5, 7}}, + {"Invalid character or mixed case", {3}}, + {"Invalid Bech32 checksum", {11}}, + {"Invalid Bech32 checksum", {9, 16}}, + }; + static_assert(std::size(CASES) == std::size(ERRORS), "Bech32 CASES and ERRORS should have the same length"); + + int i = 0; for (const std::string& str : CASES) { + const auto& err = ERRORS[i]; const auto dec = bech32::Decode(str); BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID); + auto [error, error_locations] = bech32::LocateErrors(str); + BOOST_CHECK_EQUAL(err.first, error); + BOOST_CHECK(err.second == error_locations); + i++; } } @@ -91,11 +120,39 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid) "au1s5cgom", "M1VUXWEZ", "16plkw9", - "1p2gdwpf" + "1p2gdwpf", + "abcdef1l7aum6echk45nj2s0wdvt2fg8x9yrzpqzd3ryx", + "test1zg69v7y60n00qy352euf40x77qcusag6", }; + static const std::pair<std::string, std::vector<int>> ERRORS[] = { + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Invalid character or mixed case", {0}}, + {"Bech32 string too long", {90}}, + {"Missing separator", {}}, + {"Invalid separator position", {0}}, + {"Invalid Base 32 character", {2}}, + {"Invalid Base 32 character", {3}}, + {"Invalid separator position", {2}}, + {"Invalid Base 32 character", {8}}, + {"Invalid Base 32 character", {7}}, + {"Invalid checksum", {}}, + {"Invalid separator position", {0}}, + {"Invalid separator position", {0}}, + {"Invalid Bech32m checksum", {21}}, + {"Invalid Bech32m checksum", {13, 32}}, + }; + static_assert(std::size(CASES) == std::size(ERRORS), "Bech32m CASES and ERRORS should have the same length"); + + int i = 0; for (const std::string& str : CASES) { + const auto& err = ERRORS[i]; const auto dec = bech32::Decode(str); BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID); + auto [error, error_locations] = bech32::LocateErrors(str); + BOOST_CHECK_EQUAL(err.first, error); + BOOST_CHECK(err.second == error_locations); + i++; } } |