Use json value_to<string> to do the string convert (#1172)

Fix #953
This commit is contained in:
cyan317
2024-02-14 13:26:00 +00:00
committed by GitHub
parent cce695c570
commit b89cdb26f2
44 changed files with 299 additions and 217 deletions

View File

@@ -25,6 +25,7 @@
#include <boost/json/conversion.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/value.hpp>
#include <boost/json/value_to.hpp>
#include <gtest/gtest.h>
#include <cstdint>
@@ -201,7 +202,7 @@ struct Custom {
{
ASSERT(value.is_object(), "Value must be an object");
auto const& obj = value.as_object();
return {obj.at("str").as_string().c_str(), obj.at("int").as_int64(), obj.at("bool").as_bool()};
return {boost::json::value_to<std::string>(obj.at("str")), obj.at("int").as_int64(), obj.at("bool").as_bool()};
}
};

View File

@@ -24,11 +24,13 @@
#include "util/MockPrometheus.hpp"
#include "util/prometheus/Counter.hpp"
#include <boost/json/value_to.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <ripple/protocol/jss.h>
#include <chrono>
#include <string>
using namespace rpc;
@@ -59,47 +61,49 @@ TEST_F(RPCCountersTest, CheckThatCountersAddUp)
auto const report = counters.report();
auto const& rpc = report.at(JS(rpc)).as_object();
EXPECT_STREQ(rpc.at("error").as_object().at(JS(started)).as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("error").as_object().at(JS(finished)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("error").as_object().at(JS(errored)).as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("error").as_object().at("forwarded").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("error").as_object().at("failed_forward").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("error").as_object().at(JS(failed)).as_string().c_str(), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at(JS(started))), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at(JS(finished))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at(JS(errored))), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at("forwarded")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at("failed_forward")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("error").as_object().at(JS(failed))), "0");
EXPECT_STREQ(rpc.at("complete").as_object().at(JS(started)).as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("complete").as_object().at(JS(finished)).as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("complete").as_object().at(JS(errored)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("complete").as_object().at("forwarded").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("complete").as_object().at("failed_forward").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("complete").as_object().at(JS(failed)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("complete").as_object().at(JS(duration_us)).as_string().c_str(), "512000"); // 1000 per call
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at(JS(started))), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at(JS(finished))), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at(JS(errored))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at("forwarded")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at("failed_forward")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("complete").as_object().at(JS(failed))), "0");
EXPECT_EQ(
boost::json::value_to<std::string>(rpc.at("complete").as_object().at(JS(duration_us))), "512000"
); // 1000 per call
EXPECT_STREQ(rpc.at("forward").as_object().at(JS(started)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("forward").as_object().at(JS(finished)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("forward").as_object().at(JS(errored)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("forward").as_object().at("forwarded").as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("forward").as_object().at("failed_forward").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("forward").as_object().at(JS(failed)).as_string().c_str(), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at(JS(started))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at(JS(finished))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at(JS(errored))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at("forwarded")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at("failed_forward")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("forward").as_object().at(JS(failed))), "0");
EXPECT_STREQ(rpc.at("failed").as_object().at(JS(started)).as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("failed").as_object().at(JS(finished)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failed").as_object().at(JS(errored)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failed").as_object().at("forwarded").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failed").as_object().at("failed_forward").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failed").as_object().at(JS(failed)).as_string().c_str(), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at(JS(started))), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at(JS(finished))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at(JS(errored))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at("forwarded")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at("failed_forward")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failed").as_object().at(JS(failed))), "512");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at(JS(started)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at(JS(finished)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at(JS(errored)).as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at("forwarded").as_string().c_str(), "0");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at("failed_forward").as_string().c_str(), "512");
EXPECT_STREQ(rpc.at("failedToForward").as_object().at(JS(failed)).as_string().c_str(), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at(JS(started))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at(JS(finished))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at(JS(errored))), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at("forwarded")), "0");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at("failed_forward")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(rpc.at("failedToForward").as_object().at(JS(failed))), "0");
EXPECT_STREQ(report.at("too_busy_errors").as_string().c_str(), "512");
EXPECT_STREQ(report.at("not_ready_errors").as_string().c_str(), "512");
EXPECT_STREQ(report.at("bad_syntax_errors").as_string().c_str(), "512");
EXPECT_STREQ(report.at("unknown_command_errors").as_string().c_str(), "512");
EXPECT_STREQ(report.at("internal_errors").as_string().c_str(), "512");
EXPECT_EQ(boost::json::value_to<std::string>(report.at("too_busy_errors")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(report.at("not_ready_errors")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(report.at("bad_syntax_errors")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(report.at("unknown_command_errors")), "512");
EXPECT_EQ(boost::json::value_to<std::string>(report.at("internal_errors")), "512");
EXPECT_EQ(report.at("work_queue"), queue.report()); // Counters report includes queue report
}

View File

@@ -19,11 +19,14 @@
#include "rpc/Errors.hpp"
#include <boost/json/fwd.hpp>
#include <boost/json/object.hpp>
#include <boost/json/value_to.hpp>
#include <gtest/gtest.h>
#include <ripple/protocol/ErrorCodes.h>
#include <cstdint>
#include <string>
#include <string_view>
using namespace rpc;
@@ -45,12 +48,12 @@ check(boost::json::object const& j, std::string_view error, uint32_t errorCode,
EXPECT_TRUE(j.at("status").is_string());
EXPECT_TRUE(j.at("type").is_string());
EXPECT_STREQ(j.at("status").as_string().c_str(), "error");
EXPECT_STREQ(j.at("type").as_string().c_str(), "response");
EXPECT_EQ(boost::json::value_to<std::string>(j.at("status")), "error");
EXPECT_EQ(boost::json::value_to<std::string>(j.at("type")), "response");
EXPECT_STREQ(j.at("error").as_string().c_str(), error.data());
EXPECT_EQ(boost::json::value_to<std::string>(j.at("error")), error.data());
EXPECT_EQ(j.at("error_code").as_uint64(), errorCode);
EXPECT_STREQ(j.at("error_message").as_string().c_str(), errorMessage.data());
EXPECT_EQ(boost::json::value_to<std::string>(j.at("error_message")), errorMessage.data());
}
} // namespace
@@ -89,7 +92,7 @@ TEST(RPCErrorsTest, RippledErrorToJSON)
TEST(RPCErrorsTest, RippledErrorFromStringToJSON)
{
auto const j = makeError(Status{"veryCustomError"});
EXPECT_STREQ(j.at("error").as_string().c_str(), "veryCustomError");
EXPECT_EQ(boost::json::value_to<std::string>(j.at("error")), "veryCustomError");
}
TEST(RPCErrorsTest, RippledErrorToJSONCustomMessage)
@@ -137,7 +140,7 @@ TEST(RPCErrorsTest, WarningToJSON)
EXPECT_TRUE(j.at("message").is_string());
EXPECT_EQ(j.at("id").as_int64(), static_cast<uint32_t>(WarningCode::warnRPC_OUTDATED));
EXPECT_STREQ(j.at("message").as_string().c_str(), "This server may be out of date");
EXPECT_EQ(boost::json::value_to<std::string>(j.at("message")), "This server may be out of date");
}
TEST(RPCErrorsTest, InvalidWarningToJSON)

View File

@@ -539,7 +539,7 @@ TEST_F(RPCAccountChannelsHandlerTest, UseLimit)
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("channels").as_array().size(), 20);
EXPECT_THAT((*output).as_object().at("marker").as_string().c_str(), EndsWith(",0"));
EXPECT_THAT(boost::json::value_to<std::string>((*output).as_object().at("marker")), EndsWith(",0"));
});
runSpawn([this](auto yield) {
@@ -811,7 +811,10 @@ TEST_F(RPCAccountChannelsHandlerTest, MarkerOutput)
auto handler = AnyHandler{AccountChannelsHandler{this->backend}};
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("marker").as_string().c_str(), fmt::format("{},{}", INDEX1, nextPage));
EXPECT_EQ(
boost::json::value_to<std::string>((*output).as_object().at("marker")),
fmt::format("{},{}", INDEX1, nextPage)
);
EXPECT_EQ((*output).as_object().at("channels").as_array().size(), 15);
});
}

View File

@@ -26,6 +26,7 @@
#include "util/TestObject.hpp"
#include <boost/json/parse.hpp>
#include <boost/json/value_to.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -37,6 +38,7 @@
#include <ripple/protocol/STObject.h>
#include <optional>
#include <string>
#include <vector>
using namespace rpc;
@@ -587,7 +589,7 @@ TEST_F(RPCAccountLinesHandlerTest, UseLimit)
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("lines").as_array().size(), 20);
EXPECT_THAT((*output).as_object().at("marker").as_string().c_str(), EndsWith(",0"));
EXPECT_THAT(boost::json::value_to<std::string>((*output).as_object().at("marker")), EndsWith(",0"));
});
runSpawn([this](auto yield) {
@@ -863,7 +865,10 @@ TEST_F(RPCAccountLinesHandlerTest, MarkerOutput)
auto handler = AnyHandler{AccountLinesHandler{this->backend}};
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("marker").as_string().c_str(), fmt::format("{},{}", INDEX1, nextPage));
EXPECT_EQ(
boost::json::value_to<std::string>((*output).as_object().at("marker")),
fmt::format("{},{}", INDEX1, nextPage)
);
EXPECT_EQ((*output).as_object().at("lines").as_array().size(), 15);
});
}

View File

@@ -26,6 +26,7 @@
#include "util/TestObject.hpp"
#include <boost/json/parse.hpp>
#include <boost/json/value_to.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -2155,7 +2156,10 @@ TEST_P(RPCLedgerEntryNormalPathTest, NormalPath)
output.value().at("node_binary").as_string(),
ripple::strHex(testBundle.mockedEntity.getSerializer().peekData())
);
EXPECT_EQ(ripple::uint256(output.value().at("index").as_string().c_str()), testBundle.expectedIndex);
EXPECT_EQ(
ripple::uint256(boost::json::value_to<std::string>(output.value().at("index")).data()),
testBundle.expectedIndex
);
});
}

View File

@@ -27,6 +27,7 @@
#include <boost/asio/spawn.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/value_to.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -36,6 +37,7 @@
#include <ripple/protocol/STObject.h>
#include <optional>
#include <string>
#include <vector>
using namespace rpc;
@@ -484,8 +486,9 @@ TEST_F(RPCNFTBuyOffersHandlerTest, MultipleResultsWithMarkerAndLimitOutput)
ASSERT_TRUE(output);
EXPECT_EQ(output->at("offers").as_array().size(), 50);
EXPECT_EQ(output->at("limit").as_uint64(), 50);
EXPECT_STREQ(
output->at("marker").as_string().c_str(), "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC353"
EXPECT_EQ(
boost::json::value_to<std::string>(output->at("marker")),
"E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC353"
);
});
}
@@ -544,8 +547,9 @@ TEST_F(RPCNFTBuyOffersHandlerTest, ResultsForInputWithMarkerAndLimit)
EXPECT_EQ(output->at("offers").as_array().size(), 50);
EXPECT_EQ(output->at("limit").as_uint64(), 50);
// marker also progressed by 50
EXPECT_STREQ(
output->at("marker").as_string().c_str(), "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC385"
EXPECT_EQ(
boost::json::value_to<std::string>(output->at("marker")),
"E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC385"
);
});
}

View File

@@ -27,6 +27,7 @@
#include <boost/asio/spawn.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/value_to.hpp>
#include <fmt/core.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -36,6 +37,7 @@
#include <ripple/protocol/STObject.h>
#include <optional>
#include <string>
#include <vector>
using namespace rpc;
@@ -484,8 +486,9 @@ TEST_F(RPCNFTSellOffersHandlerTest, MultipleResultsWithMarkerAndLimitOutput)
ASSERT_TRUE(output);
EXPECT_EQ(output->at("offers").as_array().size(), 50);
EXPECT_EQ(output->at("limit").as_uint64(), 50);
EXPECT_STREQ(
output->at("marker").as_string().c_str(), "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC353"
EXPECT_EQ(
boost::json::value_to<std::string>(output->at("marker")),
"E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC353"
);
});
}
@@ -544,8 +547,9 @@ TEST_F(RPCNFTSellOffersHandlerTest, ResultsForInputWithMarkerAndLimit)
EXPECT_EQ(output->at("offers").as_array().size(), 50);
EXPECT_EQ(output->at("limit").as_uint64(), 50);
// marker also progressed by 50
EXPECT_STREQ(
output->at("marker").as_string().c_str(), "E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC385"
EXPECT_EQ(
boost::json::value_to<std::string>(output->at("marker")),
"E6DBAFC99223B42257915A63DFC6B0C032D4070F9A574B255AD97466726FC385"
);
});
}

View File

@@ -31,11 +31,13 @@
#include <boost/json/object.hpp>
#include <boost/json/parse.hpp>
#include <boost/json/serialize.hpp>
#include <boost/json/value_to.hpp>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <chrono>
#include <optional>
#include <string>
using namespace rpc;
namespace json = boost::json;
@@ -81,7 +83,7 @@ protected:
auto const& info = result.at("info").as_object();
EXPECT_TRUE(info.contains("complete_ledgers"));
EXPECT_STREQ(info.at("complete_ledgers").as_string().c_str(), "10-30");
EXPECT_EQ(boost::json::value_to<std::string>(info.at("complete_ledgers")), "10-30");
EXPECT_TRUE(info.contains("load_factor"));
EXPECT_TRUE(info.contains("clio_version"));
EXPECT_TRUE(info.contains("libxrpl_version"));
@@ -93,7 +95,7 @@ protected:
EXPECT_TRUE(validated.contains("age"));
EXPECT_EQ(validated.at("age").as_uint64(), 3u);
EXPECT_TRUE(validated.contains("hash"));
EXPECT_STREQ(validated.at("hash").as_string().c_str(), LEDGERHASH);
EXPECT_EQ(boost::json::value_to<std::string>(validated.at("hash")), LEDGERHASH);
EXPECT_TRUE(validated.contains("seq"));
EXPECT_EQ(validated.at("seq").as_uint64(), 30u);
EXPECT_TRUE(validated.contains("base_fee_xrp"));
@@ -135,7 +137,7 @@ protected:
EXPECT_TRUE(info.contains("validation_quorum"));
EXPECT_EQ(info.at("validation_quorum").as_int64(), 456);
EXPECT_TRUE(info.contains("rippled_version"));
EXPECT_STREQ(info.at("rippled_version").as_string().c_str(), "1234");
EXPECT_EQ(boost::json::value_to<std::string>(info.at("rippled_version")), "1234");
EXPECT_TRUE(info.contains("network_id"));
EXPECT_EQ(info.at("network_id").as_int64(), 2);
}

View File

@@ -56,7 +56,7 @@ tag_invoke(boost::json::value_to_tag<TestInput>, boost::json::value const& jv)
if (jv.as_object().contains("limit"))
optLimit = jv.at("limit").as_int64();
return {jv.as_object().at("hello").as_string().c_str(), optLimit};
return {boost::json::value_to<std::string>(jv.as_object().at("hello")), optLimit};
}
// must be implemented as per rpc/common/Concepts.h
@@ -145,7 +145,7 @@ struct InOutFake {
inline InOutFake
tag_invoke(boost::json::value_to_tag<InOutFake>, boost::json::value const& jv)
{
return {jv.as_object().at("something").as_string().c_str()};
return {boost::json::value_to<std::string>(jv.as_object().at("something"))};
}
// must be implemented as per rpc/common/Concepts.h