refactor: Simplify STParsedJSON with some helper functions (#5591)

- Add code coverage for STParsedJSON edge cases

Co-authored-by: Denis Angell <dangell@transia.co>
This commit is contained in:
Ed Hennis
2025-09-18 15:04:40 -04:00
committed by GitHub
parent 3cbdf818a7
commit ffeabc9642
3 changed files with 514 additions and 333 deletions

View File

@@ -19,223 +19,11 @@
#include <test/jtx.h>
#include <xrpl/beast/unit_test.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/json/to_string.h>
#include <xrpl/protocol/SecretKey.h>
#include <xrpl/protocol/jss.h>
#include <xrpl/protocol/st.h>
#include <array>
#include <memory>
#include <type_traits>
namespace ripple {
class STObject_test : public beast::unit_test::suite
{
public:
bool
parseJSONString(std::string const& json, Json::Value& to)
{
Json::Reader reader;
return reader.parse(json, to) && to.isObject();
}
void
testParseJSONArrayWithInvalidChildrenObjects()
{
testcase("parse json array invalid children");
try
{
/*
STArray/STObject constructs don't really map perfectly to json
arrays/objects.
STObject is an associative container, mapping fields to value, but
an STObject may also have a Field as its name, stored outside the
associative structure. The name is important, so to maintain
fidelity, it will take TWO json objects to represent them.
*/
std::string faulty(
"{\"Template\":[{"
"\"ModifiedNode\":{\"Sequence\":1}, "
"\"DeletedNode\":{\"Sequence\":1}"
"}]}");
std::unique_ptr<STObject> so;
Json::Value faultyJson;
bool parsedOK(parseJSONString(faulty, faultyJson));
unexpected(!parsedOK, "failed to parse");
STParsedJSONObject parsed("test", faultyJson);
BEAST_EXPECT(!parsed.object);
}
catch (std::runtime_error& e)
{
std::string what(e.what());
unexpected(what.find("First level children of `Template`") != 0);
}
}
void
testParseJSONArray()
{
testcase("parse json array");
std::string const json(
"{\"Template\":[{\"ModifiedNode\":{\"Sequence\":1}}]}");
Json::Value jsonObject;
bool parsedOK(parseJSONString(json, jsonObject));
if (parsedOK)
{
STParsedJSONObject parsed("test", jsonObject);
BEAST_EXPECT(parsed.object);
std::string const& serialized(
to_string(parsed.object->getJson(JsonOptions::none)));
BEAST_EXPECT(serialized == json);
}
else
{
fail("Couldn't parse json: " + json);
}
}
void
testParseJSONEdgeCases()
{
testcase("parse json object");
{
std::string const goodJson(R"({"CloseResolution":19,"Method":250,)"
R"("TransactionResult":"tecFROZEN"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
{
STParsedJSONObject parsed("test", jv);
if (BEAST_EXPECT(parsed.object))
{
std::string const& serialized(
to_string(parsed.object->getJson(JsonOptions::none)));
BEAST_EXPECT(serialized == goodJson);
}
}
}
{
std::string const goodJson(
R"({"CloseResolution":19,"Method":"250",)"
R"("TransactionResult":"tecFROZEN"})");
std::string const expectedJson(
R"({"CloseResolution":19,"Method":250,)"
R"("TransactionResult":"tecFROZEN"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(goodJson, jv)))
{
// Integer values are always parsed as int,
// unless they're too big. We want a small uint.
jv["CloseResolution"] = Json::UInt(19);
STParsedJSONObject parsed("test", jv);
if (BEAST_EXPECT(parsed.object))
{
std::string const& serialized(
to_string(parsed.object->getJson(JsonOptions::none)));
BEAST_EXPECT(serialized == expectedJson);
}
}
}
{
std::string const json(R"({"CloseResolution":19,"Method":250,)"
R"("TransactionResult":"terQUEUED"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(json, jv)))
{
STParsedJSONObject parsed("test", jv);
BEAST_EXPECT(!parsed.object);
BEAST_EXPECT(parsed.error);
BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
BEAST_EXPECT(
parsed.error[jss::error_message] ==
"Field 'test.TransactionResult' is out of range.");
}
}
{
std::string const json(R"({"CloseResolution":19,"Method":"pony",)"
R"("TransactionResult":"tesSUCCESS"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(json, jv)))
{
STParsedJSONObject parsed("test", jv);
BEAST_EXPECT(!parsed.object);
BEAST_EXPECT(parsed.error);
BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
BEAST_EXPECT(
parsed.error[jss::error_message] ==
"Field 'test.Method' has bad type.");
}
}
{
std::string const json(
R"({"CloseResolution":19,"Method":3294967296,)"
R"("TransactionResult":"tesSUCCESS"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(json, jv)))
{
STParsedJSONObject parsed("test", jv);
BEAST_EXPECT(!parsed.object);
BEAST_EXPECT(parsed.error);
BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
BEAST_EXPECT(
parsed.error[jss::error_message] ==
"Field 'test.Method' is out of range.");
}
}
{
std::string const json(R"({"CloseResolution":-10,"Method":42,)"
R"("TransactionResult":"tesSUCCESS"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(json, jv)))
{
STParsedJSONObject parsed("test", jv);
BEAST_EXPECT(!parsed.object);
BEAST_EXPECT(parsed.error);
BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
BEAST_EXPECT(
parsed.error[jss::error_message] ==
"Field 'test.CloseResolution' is out of range.");
}
}
{
std::string const json(
R"({"CloseResolution":19,"Method":3.141592653,)"
R"("TransactionResult":"tesSUCCESS"})");
Json::Value jv;
if (BEAST_EXPECT(parseJSONString(json, jv)))
{
STParsedJSONObject parsed("test", jv);
BEAST_EXPECT(!parsed.object);
BEAST_EXPECT(parsed.error);
BEAST_EXPECT(parsed.error[jss::error] == "invalidParams");
BEAST_EXPECT(
parsed.error[jss::error_message] ==
"Field 'test.Method' has bad type.");
}
}
}
void
testSerialization()
{
@@ -730,9 +518,6 @@ public:
testFields();
testSerialization();
testParseJSONArray();
testParseJSONArrayWithInvalidChildrenObjects();
testParseJSONEdgeCases();
testMalformed();
}
};