Skip to content

Commit 199e953

Browse files
stickies-vdenavila
authored andcommitted
test: add uint256::FromHex unittest coverage
Simultaneously cover transaction_identifier::FromHex()
1 parent 7a53f41 commit 199e953

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/uint256_tests.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#include <streams.h>
77
#include <test/util/setup_common.h>
88
#include <uint256.h>
9+
#include <util/strencodings.h>
10+
#include <util/transaction_identifier.h>
911

1012
#include <boost/test/unit_test.hpp>
1113

1214
#include <iomanip>
1315
#include <sstream>
1416
#include <string>
17+
#include <string_view>
1518
#include <vector>
1619

1720
BOOST_AUTO_TEST_SUITE(uint256_tests)
@@ -330,6 +333,59 @@ BOOST_AUTO_TEST_CASE(parse)
330333
}
331334
}
332335

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+
333389
BOOST_AUTO_TEST_CASE( check_ONE )
334390
{
335391
uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");

0 commit comments

Comments
 (0)