mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-10 10:52:31 -05:00
test: Add test for embedded null in hex string
Also, fix style in the corresponding function. The style change can be reviewed with "--word-diff-regex=."
This commit is contained in:
parent
f0a834e2f1
commit
fabdf81983
2 changed files with 9 additions and 2 deletions
|
@ -153,7 +153,7 @@ static const unsigned char ParseHex_expected[65] = {
|
||||||
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
|
0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d,
|
||||||
0x5f
|
0x5f
|
||||||
};
|
};
|
||||||
BOOST_AUTO_TEST_CASE(util_ParseHex)
|
BOOST_AUTO_TEST_CASE(parse_hex)
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> result;
|
std::vector<unsigned char> result;
|
||||||
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
|
std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected));
|
||||||
|
@ -169,6 +169,14 @@ BOOST_AUTO_TEST_CASE(util_ParseHex)
|
||||||
result = ParseHex(" 89 34 56 78");
|
result = ParseHex(" 89 34 56 78");
|
||||||
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78);
|
||||||
|
|
||||||
|
// Embedded null is treated as end
|
||||||
|
const std::string with_embedded_null{" 11 "s
|
||||||
|
" \0 "
|
||||||
|
" 22 "s};
|
||||||
|
BOOST_CHECK_EQUAL(with_embedded_null.size(), 11);
|
||||||
|
result = ParseHex(with_embedded_null);
|
||||||
|
BOOST_CHECK(result.size() == 1 && result[0] == 0x11);
|
||||||
|
|
||||||
// Stop parsing at invalid value
|
// Stop parsing at invalid value
|
||||||
result = ParseHex("1234 invalid 1234");
|
result = ParseHex("1234 invalid 1234");
|
||||||
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
|
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);
|
||||||
|
|
|
@ -78,7 +78,6 @@ bool IsHexNumber(std::string_view str)
|
||||||
|
|
||||||
std::vector<unsigned char> ParseHex(std::string_view str)
|
std::vector<unsigned char> ParseHex(std::string_view str)
|
||||||
{
|
{
|
||||||
// convert hex dump to vector
|
|
||||||
std::vector<unsigned char> vch;
|
std::vector<unsigned char> vch;
|
||||||
auto it = str.begin();
|
auto it = str.begin();
|
||||||
while (it != str.end() && it + 1 != str.end()) {
|
while (it != str.end() && it + 1 != str.end()) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue