|
6 | 6 | #include <streams.h>
|
7 | 7 | #include <test/util/setup_common.h>
|
8 | 8 | #include <uint256.h>
|
| 9 | +#include <util/strencodings.h> |
| 10 | +#include <util/transaction_identifier.h> |
9 | 11 |
|
10 | 12 | #include <boost/test/unit_test.hpp>
|
11 | 13 |
|
12 | 14 | #include <iomanip>
|
13 | 15 | #include <sstream>
|
14 | 16 | #include <string>
|
| 17 | +#include <string_view> |
15 | 18 | #include <vector>
|
16 | 19 |
|
17 | 20 | BOOST_AUTO_TEST_SUITE(uint256_tests)
|
@@ -330,6 +333,59 @@ BOOST_AUTO_TEST_CASE(parse)
|
330 | 333 | }
|
331 | 334 | }
|
332 | 335 |
|
| 336 | +/** |
| 337 | + * Implemented as a templated function so it can be reused by other classes that have a FromHex() |
| 338 | + * method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex(). |
| 339 | + */ |
| 340 | +template <typename T> |
| 341 | +void TestFromHex() |
| 342 | +{ |
| 343 | + constexpr unsigned int num_chars{T::size() * 2}; |
| 344 | + static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars |
| 345 | + const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"}; |
| 346 | + const auto valid_input{valid_64char_input.substr(0, num_chars)}; |
| 347 | + { |
| 348 | + // check that lower and upper case hex characters are accepted |
| 349 | + auto valid_result{T::FromHex(valid_input)}; |
| 350 | + BOOST_REQUIRE(valid_result); |
| 351 | + BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input)); |
| 352 | + } |
| 353 | + { |
| 354 | + // check that only strings of size num_chars are accepted |
| 355 | + BOOST_CHECK(!T::FromHex("")); |
| 356 | + BOOST_CHECK(!T::FromHex("0")); |
| 357 | + BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2))); |
| 358 | + BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1))); |
| 359 | + BOOST_CHECK(!T::FromHex(valid_input + "0")); |
| 360 | + } |
| 361 | + { |
| 362 | + // check that non-hex characters are not accepted |
| 363 | + std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"}; |
| 364 | + for (auto c : invalid_chars) { |
| 365 | + BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c)); |
| 366 | + } |
| 367 | + // 0x prefixes are invalid |
| 368 | + std::string invalid_prefix{"0x" + valid_input}; |
| 369 | + BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars))); |
| 370 | + BOOST_CHECK(!T::FromHex(invalid_prefix)); |
| 371 | + } |
| 372 | + { |
| 373 | + // check that string_view length is respected |
| 374 | + std::string chars_68{valid_64char_input + "0123"}; |
| 375 | + BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input)); |
| 376 | + BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short |
| 377 | + BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long |
| 378 | + } |
| 379 | +} |
| 380 | + |
| 381 | +BOOST_AUTO_TEST_CASE(from_hex) |
| 382 | +{ |
| 383 | + TestFromHex<uint160>(); |
| 384 | + TestFromHex<uint256>(); |
| 385 | + TestFromHex<Txid>(); |
| 386 | + TestFromHex<Wtxid>(); |
| 387 | +} |
| 388 | + |
333 | 389 | BOOST_AUTO_TEST_CASE( check_ONE )
|
334 | 390 | {
|
335 | 391 | uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");
|
|
0 commit comments