mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
Add boost tests for bech32 error detection
This commit is contained in:
parent
02a7bdee42
commit
c4979f77c1
1 changed files with 53 additions and 1 deletions
|
@ -68,10 +68,36 @@ BOOST_AUTO_TEST_CASE(bech32_testvectors_invalid)
|
||||||
"1qzzfhee",
|
"1qzzfhee",
|
||||||
"a12UEL5L",
|
"a12UEL5L",
|
||||||
"A12uEL5L",
|
"A12uEL5L",
|
||||||
|
"abcdef1qpzrz9x8gf2tvdw0s3jn54khce6mua7lmqqqxw",
|
||||||
};
|
};
|
||||||
|
static const std::pair<std::string, 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", -1},
|
||||||
|
{"Invalid separator position", 0},
|
||||||
|
{"Invalid Base 32 character", 2},
|
||||||
|
{"Invalid separator position", 2},
|
||||||
|
{"Invalid character or mixed case", 8},
|
||||||
|
{"Invalid checksum", -1}, // 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},
|
||||||
|
{"Invalid character or mixed case", 3},
|
||||||
|
{"Invalid checksum", 11}
|
||||||
|
};
|
||||||
|
int i = 0;
|
||||||
for (const std::string& str : CASES) {
|
for (const std::string& str : CASES) {
|
||||||
|
const auto& err = ERRORS[i];
|
||||||
const auto dec = bech32::Decode(str);
|
const auto dec = bech32::Decode(str);
|
||||||
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
|
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
|
||||||
|
std::vector<int> error_locations;
|
||||||
|
std::string error = bech32::LocateErrors(str, error_locations);
|
||||||
|
BOOST_CHECK_EQUAL(err.first, error);
|
||||||
|
if (err.second == -1) BOOST_CHECK(error_locations.empty());
|
||||||
|
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,11 +117,37 @@ BOOST_AUTO_TEST_CASE(bech32m_testvectors_invalid)
|
||||||
"au1s5cgom",
|
"au1s5cgom",
|
||||||
"M1VUXWEZ",
|
"M1VUXWEZ",
|
||||||
"16plkw9",
|
"16plkw9",
|
||||||
"1p2gdwpf"
|
"1p2gdwpf",
|
||||||
|
"abcdef1l7aum6echk45nj2s0wdvt2fg8x9yrzpqzd3ryx",
|
||||||
};
|
};
|
||||||
|
static const std::pair<std::string, 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", -1},
|
||||||
|
{"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", -1},
|
||||||
|
{"Invalid separator position", 0},
|
||||||
|
{"Invalid separator position", 0},
|
||||||
|
{"Invalid checksum", 21},
|
||||||
|
};
|
||||||
|
int i = 0;
|
||||||
for (const std::string& str : CASES) {
|
for (const std::string& str : CASES) {
|
||||||
|
const auto& err = ERRORS[i];
|
||||||
const auto dec = bech32::Decode(str);
|
const auto dec = bech32::Decode(str);
|
||||||
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
|
BOOST_CHECK(dec.encoding == bech32::Encoding::INVALID);
|
||||||
|
std::vector<int> error_locations;
|
||||||
|
std::string error = bech32::LocateErrors(str, error_locations);
|
||||||
|
BOOST_CHECK_EQUAL(err.first, error);
|
||||||
|
if (err.second == -1) BOOST_CHECK(error_locations.empty());
|
||||||
|
else BOOST_CHECK_EQUAL(err.second, error_locations[0]);
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue