merge bitcoin#21438: add ParseUInt8() test coverage

This commit is contained in:
Kittywhiskers Van Gogh 2022-12-19 16:30:36 +05:30 committed by PastaPastaPasta
parent 8327611d63
commit 0f7204cf60
2 changed files with 49 additions and 16 deletions

View File

@ -18,6 +18,9 @@ FUZZ_TARGET(parse_numbers)
double d; double d;
(void)ParseDouble(random_string, &d); (void)ParseDouble(random_string, &d);
uint8_t u8;
(void)ParseUInt8(random_string, &u8);
int32_t i32; int32_t i32;
(void)ParseInt32(random_string, &i32); (void)ParseInt32(random_string, &i32);
(void)atoi(random_string); (void)atoi(random_string);

View File

@ -35,6 +35,10 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
using namespace std::string_literals;
static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};
/* defined in logging.cpp */ /* defined in logging.cpp */
namespace BCLog { namespace BCLog {
std::string LogEscapeMessage(const std::string& str); std::string LogEscapeMessage(const std::string& str);
@ -1130,7 +1134,7 @@ BOOST_AUTO_TEST_CASE(util_ParseMoney)
// Parsing strings with embedded NUL characters should fail // Parsing strings with embedded NUL characters should fail
BOOST_CHECK(!ParseMoney(std::string("\0-1", 3), ret)); BOOST_CHECK(!ParseMoney(std::string("\0-1", 3), ret));
BOOST_CHECK(!ParseMoney(std::string("\01", 2), ret)); BOOST_CHECK(!ParseMoney(STRING_WITH_EMBEDDED_NULL_CHAR, ret));
BOOST_CHECK(!ParseMoney(std::string("1\0", 2), ret)); BOOST_CHECK(!ParseMoney(std::string("1\0", 2), ret));
} }
@ -1308,9 +1312,7 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
BOOST_CHECK(!ParseInt32("aap", &n)); BOOST_CHECK(!ParseInt32("aap", &n));
BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseInt32("0x1", &n)); // no hex
const char test_bytes[] = {'1', 0, '1'}; BOOST_CHECK(!ParseInt32(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
std::string teststr(test_bytes, sizeof(test_bytes));
BOOST_CHECK(!ParseInt32(teststr, &n)); // no embedded NULs
// Overflow and underflow // Overflow and underflow
BOOST_CHECK(!ParseInt32("-2147483649", nullptr)); BOOST_CHECK(!ParseInt32("-2147483649", nullptr));
BOOST_CHECK(!ParseInt32("2147483648", nullptr)); BOOST_CHECK(!ParseInt32("2147483648", nullptr));
@ -1413,9 +1415,7 @@ BOOST_AUTO_TEST_CASE(test_ParseInt64)
BOOST_CHECK(!ParseInt64("1a", &n)); BOOST_CHECK(!ParseInt64("1a", &n));
BOOST_CHECK(!ParseInt64("aap", &n)); BOOST_CHECK(!ParseInt64("aap", &n));
BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex BOOST_CHECK(!ParseInt64("0x1", &n)); // no hex
const char test_bytes[] = {'1', 0, '1'}; BOOST_CHECK(!ParseInt64(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
std::string teststr(test_bytes, sizeof(test_bytes));
BOOST_CHECK(!ParseInt64(teststr, &n)); // no embedded NULs
// Overflow and underflow // Overflow and underflow
BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr)); BOOST_CHECK(!ParseInt64("-9223372036854775809", nullptr));
BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr)); BOOST_CHECK(!ParseInt64("9223372036854775808", nullptr));
@ -1423,6 +1423,42 @@ BOOST_AUTO_TEST_CASE(test_ParseInt64)
BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr)); BOOST_CHECK(!ParseInt64("32482348723847471234", nullptr));
} }
BOOST_AUTO_TEST_CASE(test_ParseUInt8)
{
uint8_t n;
// Valid values
BOOST_CHECK(ParseUInt8("255", nullptr));
BOOST_CHECK(ParseUInt8("0", &n) && n == 0);
BOOST_CHECK(ParseUInt8("255", &n) && n == 255);
BOOST_CHECK(ParseUInt8("0255", &n) && n == 255); // no octal
BOOST_CHECK(ParseUInt8("255", &n) && n == static_cast<uint8_t>(255));
BOOST_CHECK(ParseUInt8("+255", &n) && n == 255);
BOOST_CHECK(ParseUInt8("00000000000000000012", &n) && n == 12);
BOOST_CHECK(ParseUInt8("00000000000000000000", &n) && n == 0);
// Invalid values
BOOST_CHECK(!ParseUInt8("-00000000000000000000", &n));
BOOST_CHECK(!ParseUInt8("", &n));
BOOST_CHECK(!ParseUInt8(" 1", &n)); // no padding inside
BOOST_CHECK(!ParseUInt8(" -1", &n));
BOOST_CHECK(!ParseUInt8("++1", &n));
BOOST_CHECK(!ParseUInt8("+-1", &n));
BOOST_CHECK(!ParseUInt8("-+1", &n));
BOOST_CHECK(!ParseUInt8("--1", &n));
BOOST_CHECK(!ParseUInt8("-1", &n));
BOOST_CHECK(!ParseUInt8("1 ", &n));
BOOST_CHECK(!ParseUInt8("1a", &n));
BOOST_CHECK(!ParseUInt8("aap", &n));
BOOST_CHECK(!ParseUInt8("0x1", &n)); // no hex
BOOST_CHECK(!ParseUInt8("0x1", &n)); // no hex
BOOST_CHECK(!ParseUInt8(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
// Overflow and underflow
BOOST_CHECK(!ParseUInt8("-255", &n));
BOOST_CHECK(!ParseUInt8("256", &n));
BOOST_CHECK(!ParseUInt8("-123", &n));
BOOST_CHECK(!ParseUInt8("-123", nullptr));
BOOST_CHECK(!ParseUInt8("256", nullptr));
}
BOOST_AUTO_TEST_CASE(test_ParseUInt32) BOOST_AUTO_TEST_CASE(test_ParseUInt32)
{ {
uint32_t n; uint32_t n;
@ -1452,9 +1488,7 @@ BOOST_AUTO_TEST_CASE(test_ParseUInt32)
BOOST_CHECK(!ParseUInt32("aap", &n)); BOOST_CHECK(!ParseUInt32("aap", &n));
BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex BOOST_CHECK(!ParseUInt32("0x1", &n)); // no hex
const char test_bytes[] = {'1', 0, '1'}; BOOST_CHECK(!ParseUInt32(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
std::string teststr(test_bytes, sizeof(test_bytes));
BOOST_CHECK(!ParseUInt32(teststr, &n)); // no embedded NULs
// Overflow and underflow // Overflow and underflow
BOOST_CHECK(!ParseUInt32("-2147483648", &n)); BOOST_CHECK(!ParseUInt32("-2147483648", &n));
BOOST_CHECK(!ParseUInt32("4294967296", &n)); BOOST_CHECK(!ParseUInt32("4294967296", &n));
@ -1483,9 +1517,7 @@ BOOST_AUTO_TEST_CASE(test_ParseUInt64)
BOOST_CHECK(!ParseUInt64("1a", &n)); BOOST_CHECK(!ParseUInt64("1a", &n));
BOOST_CHECK(!ParseUInt64("aap", &n)); BOOST_CHECK(!ParseUInt64("aap", &n));
BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex BOOST_CHECK(!ParseUInt64("0x1", &n)); // no hex
const char test_bytes[] = {'1', 0, '1'}; BOOST_CHECK(!ParseUInt64(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
std::string teststr(test_bytes, sizeof(test_bytes));
BOOST_CHECK(!ParseUInt64(teststr, &n)); // no embedded NULs
// Overflow and underflow // Overflow and underflow
BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr)); BOOST_CHECK(!ParseUInt64("-9223372036854775809", nullptr));
BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr)); BOOST_CHECK(!ParseUInt64("18446744073709551616", nullptr));
@ -1515,9 +1547,7 @@ BOOST_AUTO_TEST_CASE(test_ParseDouble)
BOOST_CHECK(!ParseDouble("1a", &n)); BOOST_CHECK(!ParseDouble("1a", &n));
BOOST_CHECK(!ParseDouble("aap", &n)); BOOST_CHECK(!ParseDouble("aap", &n));
BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
const char test_bytes[] = {'1', 0, '1'}; BOOST_CHECK(!ParseDouble(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
std::string teststr(test_bytes, sizeof(test_bytes));
BOOST_CHECK(!ParseDouble(teststr, &n)); // no embedded NULs
// Overflow and underflow // Overflow and underflow
BOOST_CHECK(!ParseDouble("-1e10000", nullptr)); BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
BOOST_CHECK(!ParseDouble("1e10000", nullptr)); BOOST_CHECK(!ParseDouble("1e10000", nullptr));