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:
MarcoFalke 2021-11-09 20:17:48 +01:00 committed by MacroFake
parent f0a834e2f1
commit fabdf81983
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 9 additions and 2 deletions

View file

@ -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,
0x5f
};
BOOST_AUTO_TEST_CASE(util_ParseHex)
BOOST_AUTO_TEST_CASE(parse_hex)
{
std::vector<unsigned char> result;
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");
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
result = ParseHex("1234 invalid 1234");
BOOST_CHECK(result.size() == 2 && result[0] == 0x12 && result[1] == 0x34);

View file

@ -78,7 +78,6 @@ bool IsHexNumber(std::string_view str)
std::vector<unsigned char> ParseHex(std::string_view str)
{
// convert hex dump to vector
std::vector<unsigned char> vch;
auto it = str.begin();
while (it != str.end() && it + 1 != str.end()) {