diff options
Diffstat (limited to 'src/test/util_tests.cpp')
-rw-r--r-- | src/test/util_tests.cpp | 107 |
1 files changed, 54 insertions, 53 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index bf1fc1ea0a..1624fb8b5b 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -7,6 +7,7 @@ #include <hash.h> // For Hash() #include <key.h> // For CKey #include <script/parsing.h> +#include <span.h> #include <sync.h> #include <test/util/random.h> #include <test/util/setup_common.h> @@ -45,6 +46,8 @@ #include <boost/test/unit_test.hpp> using namespace std::literals; +using namespace util::hex_literals; +using util::ConstevalHexDigit; using util::Join; using util::RemovePrefix; using util::RemovePrefixView; @@ -136,46 +139,69 @@ BOOST_AUTO_TEST_CASE(util_criticalsection) } while(0); } -static const unsigned char ParseHex_expected[65] = { +constexpr char HEX_PARSE_INPUT[] = "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"; +constexpr uint8_t HEX_PARSE_OUTPUT[] = { 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, 0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, 0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f }; +static_assert((sizeof(HEX_PARSE_INPUT) - 1) == 2 * sizeof(HEX_PARSE_OUTPUT)); BOOST_AUTO_TEST_CASE(parse_hex) { std::vector<unsigned char> result; - std::vector<unsigned char> expected(ParseHex_expected, ParseHex_expected + sizeof(ParseHex_expected)); + // Basic test vector - result = ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); + std::vector<unsigned char> expected(std::begin(HEX_PARSE_OUTPUT), std::end(HEX_PARSE_OUTPUT)); + constexpr std::array<std::byte, 65> hex_literal_array{operator""_hex<util::detail::Hex(HEX_PARSE_INPUT)>()}; + auto hex_literal_span{MakeUCharSpan(hex_literal_array)}; + BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); + + const std::vector<std::byte> hex_literal_vector{operator""_hex_v<util::detail::Hex(HEX_PARSE_INPUT)>()}; + hex_literal_span = MakeUCharSpan(hex_literal_vector); + BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_span.begin(), hex_literal_span.end(), expected.begin(), expected.end()); + + constexpr std::array<uint8_t, 65> hex_literal_array_uint8{operator""_hex_u8<util::detail::Hex(HEX_PARSE_INPUT)>()}; + BOOST_CHECK_EQUAL_COLLECTIONS(hex_literal_array_uint8.begin(), hex_literal_array_uint8.end(), expected.begin(), expected.end()); + + result = operator""_hex_v_u8<util::detail::Hex(HEX_PARSE_INPUT)>(); BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); - result = TryParseHex<uint8_t>("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").value(); + + result = ParseHex(HEX_PARSE_INPUT); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); + + result = TryParseHex<uint8_t>(HEX_PARSE_INPUT).value(); BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Spaces between bytes must be supported + expected = {0x12, 0x34, 0x56, 0x78}; result = ParseHex("12 34 56 78"); - BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>("12 34 56 78").value(); - BOOST_CHECK(result.size() == 4 && result[0] == 0x12 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Leading space must be supported (used in BerkeleyEnvironment::Salvage) + expected = {0x89, 0x34, 0x56, 0x78}; 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_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>(" 89 34 56 78").value(); - BOOST_CHECK(result.size() == 4 && result[0] == 0x89 && result[1] == 0x34 && result[2] == 0x56 && result[3] == 0x78); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Mixed case and spaces are supported + expected = {0xff, 0xaa}; result = ParseHex(" Ff aA "); - BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); result = TryParseHex<uint8_t>(" Ff aA ").value(); - BOOST_CHECK(result.size() == 2 && result[0] == 0xff && result[1] == 0xaa); + BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end()); // Empty string is supported - result = ParseHex(""); - BOOST_CHECK(result.size() == 0); - result = TryParseHex<uint8_t>("").value(); - BOOST_CHECK(result.size() == 0); + static_assert(""_hex.empty()); + static_assert(""_hex_u8.empty()); + BOOST_CHECK_EQUAL(""_hex_v.size(), 0); + BOOST_CHECK_EQUAL(""_hex_v_u8.size(), 0); + BOOST_CHECK_EQUAL(ParseHex("").size(), 0); + BOOST_CHECK_EQUAL(TryParseHex<uint8_t>("").value().size(), 0); // Spaces between nibbles is treated as invalid BOOST_CHECK_EQUAL(ParseHex("AAF F").size(), 0); @@ -198,25 +224,25 @@ BOOST_AUTO_TEST_CASE(parse_hex) BOOST_CHECK(!TryParseHex("12 3").has_value()); } -BOOST_AUTO_TEST_CASE(util_HexStr) +BOOST_AUTO_TEST_CASE(consteval_hex_digit) { - BOOST_CHECK_EQUAL( - HexStr(ParseHex_expected), - "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"); - - BOOST_CHECK_EQUAL( - HexStr(Span{ParseHex_expected}.last(0)), - ""); + BOOST_CHECK_EQUAL(ConstevalHexDigit('0'), 0); + BOOST_CHECK_EQUAL(ConstevalHexDigit('9'), 9); + BOOST_CHECK_EQUAL(ConstevalHexDigit('a'), 0xa); + BOOST_CHECK_EQUAL(ConstevalHexDigit('f'), 0xf); +} - BOOST_CHECK_EQUAL( - HexStr(Span{ParseHex_expected}.first(0)), - ""); +BOOST_AUTO_TEST_CASE(util_HexStr) +{ + BOOST_CHECK_EQUAL(HexStr(HEX_PARSE_OUTPUT), HEX_PARSE_INPUT); + BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.last(0)), ""); + BOOST_CHECK_EQUAL(HexStr(Span{HEX_PARSE_OUTPUT}.first(0)), ""); { - const std::vector<char> in_s{ParseHex_expected, ParseHex_expected + 5}; + constexpr std::string_view out_exp{"04678afdb0"}; + constexpr std::span in_s{HEX_PARSE_OUTPUT, out_exp.size() / 2}; const Span<const uint8_t> in_u{MakeUCharSpan(in_s)}; const Span<const std::byte> in_b{MakeByteSpan(in_s)}; - const std::string out_exp{"04678afdb0"}; BOOST_CHECK_EQUAL(HexStr(in_u), out_exp); BOOST_CHECK_EQUAL(HexStr(in_s), out_exp); @@ -432,31 +458,6 @@ BOOST_AUTO_TEST_CASE(util_IsHex) BOOST_CHECK(!IsHex("0x0000")); } -BOOST_AUTO_TEST_CASE(util_IsHexNumber) -{ - BOOST_CHECK(IsHexNumber("0x0")); - BOOST_CHECK(IsHexNumber("0")); - BOOST_CHECK(IsHexNumber("0x10")); - BOOST_CHECK(IsHexNumber("10")); - BOOST_CHECK(IsHexNumber("0xff")); - BOOST_CHECK(IsHexNumber("ff")); - BOOST_CHECK(IsHexNumber("0xFfa")); - BOOST_CHECK(IsHexNumber("Ffa")); - BOOST_CHECK(IsHexNumber("0x00112233445566778899aabbccddeeffAABBCCDDEEFF")); - BOOST_CHECK(IsHexNumber("00112233445566778899aabbccddeeffAABBCCDDEEFF")); - - BOOST_CHECK(!IsHexNumber("")); // empty string not allowed - BOOST_CHECK(!IsHexNumber("0x")); // empty string after prefix not allowed - BOOST_CHECK(!IsHexNumber("0x0 ")); // no spaces at end, - BOOST_CHECK(!IsHexNumber(" 0x0")); // or beginning, - BOOST_CHECK(!IsHexNumber("0x 0")); // or middle, - BOOST_CHECK(!IsHexNumber(" ")); // etc. - BOOST_CHECK(!IsHexNumber("0x0ga")); // invalid character - BOOST_CHECK(!IsHexNumber("x0")); // broken prefix - BOOST_CHECK(!IsHexNumber("0x0x00")); // two prefixes not allowed - -} - BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) { SeedRandomForTest(SeedRand::ZEROS); @@ -473,7 +474,7 @@ BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) for (int i = 0; i < 10000; i++) { uint32_t rval; do{ - rval=InsecureRand32()&mask; + rval=m_rng.rand32()&mask; }while(rval>=(uint32_t)mod); count += rval==0; } |