aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorstickies-v <stickies-v@protonmail.com>2024-07-29 17:16:54 +0100
committerstickies-v <stickies-v@protonmail.com>2024-07-31 16:47:37 +0100
commit526a87ba6b4f20592ad3c090b82e83ecff2107fc (patch)
tree8c18746e3a4d5f89c67702b2fef946c8a768c39e /src
parent5d280130446d57d653c749005a2e363265d87686 (diff)
test: add uint256::FromHex unittest coverage
Simultaneously cover transaction_identifier::FromHex()
Diffstat (limited to 'src')
-rw-r--r--src/test/uint256_tests.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/test/uint256_tests.cpp b/src/test/uint256_tests.cpp
index d280126cde..04f8682b69 100644
--- a/src/test/uint256_tests.cpp
+++ b/src/test/uint256_tests.cpp
@@ -6,12 +6,15 @@
#include <streams.h>
#include <test/util/setup_common.h>
#include <uint256.h>
+#include <util/strencodings.h>
+#include <util/transaction_identifier.h>
#include <boost/test/unit_test.hpp>
#include <iomanip>
#include <sstream>
#include <string>
+#include <string_view>
#include <vector>
BOOST_AUTO_TEST_SUITE(uint256_tests)
@@ -330,6 +333,59 @@ BOOST_AUTO_TEST_CASE(parse)
}
}
+/**
+ * Implemented as a templated function so it can be reused by other classes that have a FromHex()
+ * method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex().
+ */
+template <typename T>
+void TestFromHex()
+{
+ constexpr unsigned int num_chars{T::size() * 2};
+ static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars
+ const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
+ const auto valid_input{valid_64char_input.substr(0, num_chars)};
+ {
+ // check that lower and upper case hex characters are accepted
+ auto valid_result{T::FromHex(valid_input)};
+ BOOST_REQUIRE(valid_result);
+ BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));
+ }
+ {
+ // check that only strings of size num_chars are accepted
+ BOOST_CHECK(!T::FromHex(""));
+ BOOST_CHECK(!T::FromHex("0"));
+ BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));
+ BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));
+ BOOST_CHECK(!T::FromHex(valid_input + "0"));
+ }
+ {
+ // check that non-hex characters are not accepted
+ std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
+ for (auto c : invalid_chars) {
+ BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));
+ }
+ // 0x prefixes are invalid
+ std::string invalid_prefix{"0x" + valid_input};
+ BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));
+ BOOST_CHECK(!T::FromHex(invalid_prefix));
+ }
+ {
+ // check that string_view length is respected
+ std::string chars_68{valid_64char_input + "0123"};
+ BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));
+ BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short
+ BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long
+ }
+}
+
+BOOST_AUTO_TEST_CASE(from_hex)
+{
+ TestFromHex<uint160>();
+ TestFromHex<uint256>();
+ TestFromHex<Txid>();
+ TestFromHex<Wtxid>();
+}
+
BOOST_AUTO_TEST_CASE( check_ONE )
{
uint256 one = uint256S("0000000000000000000000000000000000000000000000000000000000000001");