Skip to content

Commit 62cc465

Browse files
committed
test: remove test-only uint256S
uint256S was previously deprecated for being unsafe. All non-test usage has already been removed in earlier commits. 1. Tests now use uint256::FromHex() or other constructors wherever possible without further modification. 2. Tests that can't use uint256::FromHex() because they use input with non-hex digit characters are a) modified by dropping the non-hex digit characters if that provides useful test coverage. b) dropped if the test without non-hex digit characters does not provide useful test coverage, e.g. because it is now duplicated. Additionally, use BOOST_CHECK_EQUAL where relevant on touched lines to make error messages more readable.
1 parent adc00ad commit 62cc465

File tree

4 files changed

+12
-57
lines changed

4 files changed

+12
-57
lines changed

src/test/fuzz/hex.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ FUZZ_TARGET(hex)
3737
if (const auto result{uint256::FromUserHex(random_hex_string)}) {
3838
assert(uint256::FromHex(result->ToString()));
3939
}
40-
(void)uint256S(random_hex_string);
4140
try {
4241
(void)HexToPubKey(random_hex_string);
4342
} catch (const UniValue&) {

src/test/fuzz/integer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ FUZZ_TARGET(integer, .init = initialize_integer)
140140

141141
const arith_uint256 au256 = UintToArith256(u256);
142142
assert(ArithToUint256(au256) == u256);
143-
assert(uint256S(au256.GetHex()) == u256);
143+
assert(uint256::FromHex(au256.GetHex()).value() == u256);
144144
(void)au256.bits();
145145
(void)au256.GetCompact(/* fNegative= */ false);
146146
(void)au256.GetCompact(/* fNegative= */ true);

src/test/uint256_tests.cpp

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,12 @@ BOOST_AUTO_TEST_CASE( basics ) // constructors, equality, inequality
9292
BOOST_CHECK_NE(MaxL, ZeroL); BOOST_CHECK_NE(MaxS, ZeroS);
9393

9494
// String Constructor and Copy Constructor
95-
BOOST_CHECK_EQUAL(uint256S("0x"+R1L.ToString()), R1L);
96-
BOOST_CHECK_EQUAL(uint256S("0x"+R2L.ToString()), R2L);
97-
BOOST_CHECK_EQUAL(uint256S("0x"+ZeroL.ToString()), ZeroL);
98-
BOOST_CHECK_EQUAL(uint256S("0x"+OneL.ToString()), OneL);
99-
BOOST_CHECK_EQUAL(uint256S("0x"+MaxL.ToString()), MaxL);
100-
BOOST_CHECK_EQUAL(uint256S(R1L.ToString()), R1L);
101-
BOOST_CHECK_EQUAL(uint256S(" 0x"+R1L.ToString()+" "), R1L);
102-
BOOST_CHECK_EQUAL(uint256S(" 0x"+R1L.ToString()+"-trash;%^& "), R1L);
103-
BOOST_CHECK_EQUAL(uint256S("\t \n \n \f\n\r\t\v\t 0x"+R1L.ToString()+" \t \n \n \f\n\r\t\v\t "), R1L);
104-
BOOST_CHECK_EQUAL(uint256S(""), ZeroL);
105-
BOOST_CHECK_EQUAL(uint256S("1"), OneL);
106-
BOOST_CHECK_EQUAL(R1L, uint256S(R1ArrayHex));
95+
BOOST_CHECK_EQUAL(uint256::FromHex(R1L.ToString()).value(), R1L);
96+
BOOST_CHECK_EQUAL(uint256::FromHex(R2L.ToString()).value(), R2L);
97+
BOOST_CHECK_EQUAL(uint256::FromHex(ZeroL.ToString()).value(), ZeroL);
98+
BOOST_CHECK_EQUAL(uint256::FromHex(OneL.ToString()).value(), OneL);
99+
BOOST_CHECK_EQUAL(uint256::FromHex(MaxL.ToString()).value(), MaxL);
100+
BOOST_CHECK_EQUAL(uint256::FromHex(R1ArrayHex).value(), R1L);
107101
BOOST_CHECK_EQUAL(uint256(R1L), R1L);
108102
BOOST_CHECK_EQUAL(uint256(ZeroL), ZeroL);
109103
BOOST_CHECK_EQUAL(uint256(OneL), OneL);
@@ -282,48 +276,20 @@ BOOST_AUTO_TEST_CASE( operator_with_self )
282276
# pragma clang diagnostic push
283277
# pragma clang diagnostic ignored "-Wself-assign-overloaded"
284278
#endif
285-
arith_uint256 v = UintToArith256(uint256S("02"));
279+
arith_uint256 v{2};
286280
v *= v;
287-
BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("04")));
281+
BOOST_CHECK_EQUAL(v, arith_uint256{4});
288282
v /= v;
289-
BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("01")));
283+
BOOST_CHECK_EQUAL(v, arith_uint256{1});
290284
v += v;
291-
BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("02")));
285+
BOOST_CHECK_EQUAL(v, arith_uint256{2});
292286
v -= v;
293-
BOOST_CHECK_EQUAL(v, UintToArith256(uint256S("0")));
287+
BOOST_CHECK_EQUAL(v, arith_uint256{0});
294288
#if defined(__clang__)
295289
# pragma clang diagnostic pop
296290
#endif
297291
}
298292

299-
BOOST_AUTO_TEST_CASE(parse)
300-
{
301-
{
302-
std::string s_12{"0000000000000000000000000000000000000000000000000000000000000012"};
303-
BOOST_CHECK_EQUAL(uint256S("12\0").GetHex(), s_12);
304-
BOOST_CHECK_EQUAL(uint256S(std::string_view{"12\0", 3}).GetHex(), s_12);
305-
BOOST_CHECK_EQUAL(uint256S("0x12").GetHex(), s_12);
306-
BOOST_CHECK_EQUAL(uint256S(" 0x12").GetHex(), s_12);
307-
BOOST_CHECK_EQUAL(uint256S(" 12").GetHex(), s_12);
308-
}
309-
{
310-
std::string s_1{uint256::ONE.GetHex()};
311-
BOOST_CHECK_EQUAL(uint256S("1\0").GetHex(), s_1);
312-
BOOST_CHECK_EQUAL(uint256S(std::string_view{"1\0", 2}).GetHex(), s_1);
313-
BOOST_CHECK_EQUAL(uint256S("0x1").GetHex(), s_1);
314-
BOOST_CHECK_EQUAL(uint256S(" 0x1").GetHex(), s_1);
315-
BOOST_CHECK_EQUAL(uint256S(" 1").GetHex(), s_1);
316-
}
317-
{
318-
std::string s_0{uint256::ZERO.GetHex()};
319-
BOOST_CHECK_EQUAL(uint256S("\0").GetHex(), s_0);
320-
BOOST_CHECK_EQUAL(uint256S(std::string_view{"\0", 1}).GetHex(), s_0);
321-
BOOST_CHECK_EQUAL(uint256S("0x").GetHex(), s_0);
322-
BOOST_CHECK_EQUAL(uint256S(" 0x").GetHex(), s_0);
323-
BOOST_CHECK_EQUAL(uint256S(" ").GetHex(), s_0);
324-
}
325-
}
326-
327293
/**
328294
* Implemented as a templated function so it can be reused by other classes that have a FromHex()
329295
* method that wraps base_blob::FromHex(), such as transaction_identifier::FromHex().

src/uint256.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,4 @@ class uint256 : public base_blob<256> {
199199
static const uint256 ONE;
200200
};
201201

202-
/* uint256 from std::string_view, containing byte-reversed hex encoding.
203-
* DEPRECATED. Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated!
204-
*/
205-
inline uint256 uint256S(std::string_view str)
206-
{
207-
uint256 rv;
208-
rv.SetHexDeprecated(str);
209-
return rv;
210-
}
211-
212202
#endif // BITCOIN_UINT256_H

0 commit comments

Comments
 (0)