MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / TestFromHex

Function TestFromHex

src/test/uint256_tests.cpp:249–287  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

247 */
248template <typename T>
249void TestFromHex()
250{
251 constexpr unsigned int num_chars{T::size() * 2};
252 static_assert(num_chars <= 64); // this test needs to be modified to allow for more than 64 hex chars
253 const std::string valid_64char_input{"0123456789abcdef0123456789ABCDEF0123456789abcdef0123456789ABCDEF"};
254 const auto valid_input{valid_64char_input.substr(0, num_chars)};
255 {
256 // check that lower and upper case hex characters are accepted
257 auto valid_result{T::FromHex(valid_input)};
258 BOOST_REQUIRE(valid_result);
259 BOOST_CHECK_EQUAL(valid_result->ToString(), ToLower(valid_input));
260 }
261 {
262 // check that only strings of size num_chars are accepted
263 BOOST_CHECK(!T::FromHex(""));
264 BOOST_CHECK(!T::FromHex("0"));
265 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars / 2)));
266 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1)));
267 BOOST_CHECK(!T::FromHex(valid_input + "0"));
268 }
269 {
270 // check that non-hex characters are not accepted
271 std::string invalid_chars{R"( !"#$%&'()*+,-./:;<=>?@GHIJKLMNOPQRSTUVWXYZ[\]^_`ghijklmnopqrstuvwxyz{|}~)"};
272 for (auto c : invalid_chars) {
273 BOOST_CHECK(!T::FromHex(valid_input.substr(0, num_chars - 1) + c));
274 }
275 // 0x prefixes are invalid
276 std::string invalid_prefix{"0x" + valid_input};
277 BOOST_CHECK(!T::FromHex(std::string_view(invalid_prefix.data(), num_chars)));
278 BOOST_CHECK(!T::FromHex(invalid_prefix));
279 }
280 {
281 // check that string_view length is respected
282 std::string chars_68{valid_64char_input + "0123"};
283 BOOST_CHECK_EQUAL(T::FromHex(std::string_view(chars_68.data(), num_chars)).value().ToString(), ToLower(valid_input));
284 BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars - 1))); // too short
285 BOOST_CHECK(!T::FromHex(std::string_view(chars_68.data(), num_chars + 1))); // too long
286 }
287}
288
289BOOST_AUTO_TEST_CASE(from_hex)
290{

Callers

nothing calls this directly

Calls 6

sizeFunction · 0.85
FromHexFunction · 0.85
ToLowerFunction · 0.50
ToStringMethod · 0.45
dataMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected