Skip to content

Commit 6c8bde6

Browse files
committed
test: move coverage on ParseNonRFCJSONValue() to UniValue::read()
Preparation to deprecate ParseNonRFCJSONValue() but keep test coverage on the underlying UniValue::read() unaffected. The test coverage on AmountFromValue is no longer included, since that is already tested in the rpc_parse_monetary_values test case. Fuzzing coverage on ParseNonRFCJSONValue() was duplicated between string.cpp and parse_univalue.cpp, only the one in parse_univalue.cpp is kept.
1 parent 460e394 commit 6c8bde6

File tree

4 files changed

+31
-40
lines changed

4 files changed

+31
-40
lines changed

src/test/fuzz/parse_univalue.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ FUZZ_TARGET_INIT(parse_univalue, initialize_parse_univalue)
2121
const std::string random_string(buffer.begin(), buffer.end());
2222
bool valid = true;
2323
const UniValue univalue = [&] {
24-
try {
25-
return ParseNonRFCJSONValue(random_string);
26-
} catch (const std::runtime_error&) {
27-
valid = false;
28-
return UniValue{};
29-
}
24+
UniValue uv;
25+
if (!uv.read(random_string)) valid = false;
26+
return valid ? uv : UniValue{};
3027
}();
3128
if (!valid) {
3229
return;

src/test/fuzz/string.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,6 @@ FUZZ_TARGET(string)
159159
const util::Settings settings;
160160
(void)OnlyHasDefaultSectionSetting(settings, random_string_1, random_string_2);
161161
(void)ParseNetwork(random_string_1);
162-
try {
163-
(void)ParseNonRFCJSONValue(random_string_1);
164-
} catch (const std::runtime_error&) {
165-
}
166162
(void)ParseOutputType(random_string_1);
167163
(void)RemovePrefix(random_string_1, random_string_2);
168164
(void)ResolveErrMsg(random_string_1, random_string_2);

src/test/rpc_tests.cpp

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -278,43 +278,14 @@ BOOST_AUTO_TEST_CASE(rpc_parse_monetary_values)
278278
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.00000001000000")), 1LL); //should pass, cut trailing 0
279279
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("19e-9")), UniValue); //should fail
280280
BOOST_CHECK_EQUAL(AmountFromValue(ValueFromString("0.19e-6")), 19); //should pass, leading 0 is present
281+
BOOST_CHECK_EXCEPTION(AmountFromValue(".19e-6"), UniValue, HasJSON(R"({"code":-3,"message":"Invalid amount"})")); //should fail, no leading 0
281282

282283
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("92233720368.54775808")), UniValue); //overflow error
283284
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e+11")), UniValue); //overflow error
284285
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("1e11")), UniValue); //overflow error signless
285286
BOOST_CHECK_THROW(AmountFromValue(ValueFromString("93e+9")), UniValue); //overflow error
286287
}
287288

288-
BOOST_AUTO_TEST_CASE(json_parse_errors)
289-
{
290-
// Valid
291-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0").get_real(), 1.0);
292-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("true").get_bool(), true);
293-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("[false]")[0].get_bool(), false);
294-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("{\"a\": true}")["a"].get_bool(), true);
295-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("{\"1\": \"true\"}")["1"].get_str(), "true");
296-
// Valid, with leading or trailing whitespace
297-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue(" 1.0").get_real(), 1.0);
298-
BOOST_CHECK_EQUAL(ParseNonRFCJSONValue("1.0 ").get_real(), 1.0);
299-
300-
BOOST_CHECK_THROW(AmountFromValue(ParseNonRFCJSONValue(".19e-6")), std::runtime_error); //should fail, missing leading 0, therefore invalid JSON
301-
BOOST_CHECK_EQUAL(AmountFromValue(ParseNonRFCJSONValue("0.00000000000000000000000000000000000001e+30 ")), 1);
302-
// Invalid, initial garbage
303-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("[1.0"), std::runtime_error);
304-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("a1.0"), std::runtime_error);
305-
// Invalid, trailing garbage
306-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0sds"), std::runtime_error);
307-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("1.0]"), std::runtime_error);
308-
// Invalid, keys have to be names
309-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("{1: \"true\"}"), std::runtime_error);
310-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("{true: 1}"), std::runtime_error);
311-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("{[1]: 1}"), std::runtime_error);
312-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("{{\"a\": \"a\"}: 1}"), std::runtime_error);
313-
// BTC addresses should fail parsing
314-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"), std::runtime_error);
315-
BOOST_CHECK_THROW(ParseNonRFCJSONValue("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNL"), std::runtime_error);
316-
}
317-
318289
BOOST_AUTO_TEST_CASE(rpc_ban)
319290
{
320291
BOOST_CHECK_NO_THROW(CallRPC(std::string("clearbanned")));

src/univalue/test/object.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,33 @@ void univalue_readwrite()
412412

413413
BOOST_CHECK_EQUAL(strJson1, v.write());
414414

415+
// Valid
416+
BOOST_CHECK(v.read("1.0") && (v.get_real() == 1.0));
417+
BOOST_CHECK(v.read("true") && v.get_bool());
418+
BOOST_CHECK(v.read("[false]") && !v[0].get_bool());
419+
BOOST_CHECK(v.read("{\"a\": true}") && v["a"].get_bool());
420+
BOOST_CHECK(v.read("{\"1\": \"true\"}") && (v["1"].get_str() == "true"));
421+
// Valid, with leading or trailing whitespace
422+
BOOST_CHECK(v.read(" 1.0") && (v.get_real() == 1.0));
423+
BOOST_CHECK(v.read("1.0 ") && (v.get_real() == 1.0));
424+
BOOST_CHECK(v.read("0.00000000000000000000000000000000000001e+30 ") && v.get_real() == 1e-8);
425+
426+
BOOST_CHECK(!v.read(".19e-6")); //should fail, missing leading 0, therefore invalid JSON
427+
// Invalid, initial garbage
428+
BOOST_CHECK(!v.read("[1.0"));
429+
BOOST_CHECK(!v.read("a1.0"));
430+
// Invalid, trailing garbage
431+
BOOST_CHECK(!v.read("1.0sds"));
432+
BOOST_CHECK(!v.read("1.0]"));
433+
// Invalid, keys have to be names
434+
BOOST_CHECK(!v.read("{1: \"true\"}"));
435+
BOOST_CHECK(!v.read("{true: 1}"));
436+
BOOST_CHECK(!v.read("{[1]: 1}"));
437+
BOOST_CHECK(!v.read("{{\"a\": \"a\"}: 1}"));
438+
// BTC addresses should fail parsing
439+
BOOST_CHECK(!v.read("175tWpb8K1S7NmH4Zx6rewF9WQrcZv245W"));
440+
BOOST_CHECK(!v.read("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNL"));
441+
415442
/* Check for (correctly reporting) a parsing error if the initial
416443
JSON construct is followed by more stuff. Note that whitespace
417444
is, of course, exempt. */

0 commit comments

Comments
 (0)