mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-04 11:55:51 +00:00
913 lines
37 KiB
C++
913 lines
37 KiB
C++
/*
|
|
This file is part of clio: https://github.com/XRPLF/clio
|
|
Copyright (c) 2023, the clio developers.
|
|
|
|
Permission to use, copy, modify, and distribute this software for any
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
//==============================================================================
|
|
|
|
#include "rpc/Errors.hpp"
|
|
#include "rpc/common/APIVersion.hpp"
|
|
#include "rpc/common/Types.hpp"
|
|
#include "util/AsioContextTestFixture.hpp"
|
|
#include "util/MockBackendTestFixture.hpp"
|
|
#include "util/MockETLService.hpp"
|
|
#include "util/MockPrometheus.hpp"
|
|
#include "util/MockRPCEngine.hpp"
|
|
#include "util/NameGenerator.hpp"
|
|
#include "util/Taggable.hpp"
|
|
#include "util/config/Config.hpp"
|
|
#include "web/RPCServerHandler.hpp"
|
|
#include "web/SubscriptionContextInterface.hpp"
|
|
#include "web/interface/ConnectionBase.hpp"
|
|
|
|
#include <boost/beast/http/status.hpp>
|
|
#include <boost/json/parse.hpp>
|
|
#include <fmt/core.h>
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using namespace web;
|
|
|
|
namespace {
|
|
|
|
constexpr auto MINSEQ = 10;
|
|
constexpr auto MAXSEQ = 30;
|
|
|
|
struct MockWsBase : public web::ConnectionBase {
|
|
std::string message;
|
|
boost::beast::http::status lastStatus = boost::beast::http::status::unknown;
|
|
|
|
void
|
|
send(std::shared_ptr<std::string> msg_type) override
|
|
{
|
|
message += std::string(*msg_type);
|
|
lastStatus = boost::beast::http::status::ok;
|
|
}
|
|
|
|
void
|
|
// NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
|
|
send(std::string&& msg, boost::beast::http::status status = boost::beast::http::status::ok) override
|
|
{
|
|
message += msg;
|
|
lastStatus = status;
|
|
}
|
|
|
|
SubscriptionContextPtr
|
|
makeSubscriptionContext(util::TagDecoratorFactory const&) override
|
|
{
|
|
return {};
|
|
}
|
|
|
|
MockWsBase(util::TagDecoratorFactory const& factory) : web::ConnectionBase(factory, "localhost.fake.ip")
|
|
{
|
|
}
|
|
};
|
|
|
|
struct WebRPCServerHandlerTest : util::prometheus::WithPrometheus, MockBackendTest, SyncAsioContextTest {
|
|
util::Config cfg;
|
|
std::shared_ptr<MockAsyncRPCEngine> rpcEngine = std::make_shared<MockAsyncRPCEngine>();
|
|
std::shared_ptr<MockETLService> etl = std::make_shared<MockETLService>();
|
|
std::shared_ptr<util::TagDecoratorFactory> tagFactory = std::make_shared<util::TagDecoratorFactory>(cfg);
|
|
std::shared_ptr<RPCServerHandler<MockAsyncRPCEngine, MockETLService>> handler =
|
|
std::make_shared<RPCServerHandler<MockAsyncRPCEngine, MockETLService>>(cfg, backend, rpcEngine, etl);
|
|
std::shared_ptr<MockWsBase> session = std::make_shared<MockWsBase>(*tagFactory);
|
|
};
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPDefaultPath)
|
|
{
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr result = "{}";
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"status": "success"
|
|
},
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsNormalPath)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99,
|
|
"api_version": 2
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr result = "{}";
|
|
static auto constexpr response = R"({
|
|
"result":{},
|
|
"id": 99,
|
|
"status": "success",
|
|
"type": "response",
|
|
"api_version": 2,
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPForwardedPath)
|
|
{
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
// Note: forwarding always goes thru WS API
|
|
static auto constexpr result = R"({
|
|
"result": {
|
|
"index": 1
|
|
},
|
|
"forwarded": true
|
|
})";
|
|
static auto constexpr response = R"({
|
|
"result":{
|
|
"index": 1,
|
|
"status": "success"
|
|
},
|
|
"forwarded": true,
|
|
"warnings":[
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPForwardedErrorPath)
|
|
{
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
// Note: forwarding always goes thru WS API
|
|
static auto constexpr result = R"({
|
|
"error": "error",
|
|
"error_code": 123,
|
|
"error_message": "error message",
|
|
"status": "error",
|
|
"type": "response",
|
|
"forwarded": true
|
|
})";
|
|
static auto constexpr response = R"({
|
|
"result":{
|
|
"error": "error",
|
|
"error_code": 123,
|
|
"error_message": "error message",
|
|
"status": "error",
|
|
"type": "response"
|
|
},
|
|
"forwarded": true,
|
|
"warnings":[
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsForwardedPath)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
// Note: forwarding always goes thru WS API
|
|
static auto constexpr result = R"({
|
|
"result": {
|
|
"index": 1
|
|
},
|
|
"forwarded": true
|
|
})";
|
|
static auto constexpr response = R"({
|
|
"result":{
|
|
"index": 1
|
|
},
|
|
"forwarded": true,
|
|
"id": 99,
|
|
"status": "success",
|
|
"type": "response",
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsForwardedErrorPath)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
// Note: forwarding always goes thru WS API
|
|
static auto constexpr result = R"({
|
|
"error": "error",
|
|
"error_code": 123,
|
|
"error_message": "error message",
|
|
"status": "error",
|
|
"type": "response",
|
|
"forwarded": true
|
|
})";
|
|
// WS error responses, unlike their successful counterpart, contain everything on top level without "result"
|
|
static auto constexpr response = R"({
|
|
"error": "error",
|
|
"error_code": 123,
|
|
"error_message": "error message",
|
|
"status": "error",
|
|
"type": "response",
|
|
"forwarded": true,
|
|
"id": 99,
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
|
|
// Forwarded errors counted as successful:
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPErrorPath)
|
|
{
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"error": "invalidParams",
|
|
"error_code": 31,
|
|
"error_message": "ledgerIndexMalformed",
|
|
"status": "error",
|
|
"type": "response",
|
|
"request": {
|
|
"method": "ledger",
|
|
"params": [
|
|
{
|
|
"ledger_index": "xx"
|
|
}
|
|
]
|
|
}
|
|
},
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"method": "ledger",
|
|
"params": [
|
|
{
|
|
"ledger_index": "xx"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{rpc::Status{rpc::RippledError::rpcINVALID_PARAMS, "ledgerIndexMalformed"}}
|
|
));
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsErrorPath)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr response = R"({
|
|
"id": "123",
|
|
"error": "invalidParams",
|
|
"error_code": 31,
|
|
"error_message": "ledgerIndexMalformed",
|
|
"status": "error",
|
|
"type": "response",
|
|
"api_version": 2,
|
|
"request": {
|
|
"command": "ledger",
|
|
"ledger_index": "xx",
|
|
"id": "123",
|
|
"api_version": 2
|
|
},
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
}
|
|
]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"command": "ledger",
|
|
"ledger_index": "xx",
|
|
"id": "123",
|
|
"api_version": 2
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{rpc::Status{rpc::RippledError::rpcINVALID_PARAMS, "ledgerIndexMalformed"}}
|
|
));
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(45));
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPNotReady)
|
|
{
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"error": "notReady",
|
|
"error_code": 13,
|
|
"error_message": "Not ready to handle this request.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"request": {
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
}
|
|
}
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyNotReady).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsNotReady)
|
|
{
|
|
session->upgraded = true;
|
|
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
static auto constexpr response = R"({
|
|
"error": "notReady",
|
|
"error_code": 13,
|
|
"error_message": "Not ready to handle this request.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"id": 99,
|
|
"request": {
|
|
"command": "server_info",
|
|
"id": 99
|
|
}
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyNotReady).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPBadSyntaxWhenRequestSubscribe)
|
|
{
|
|
static auto constexpr request = R"({"method": "subscribe"})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"error": "badSyntax",
|
|
"error_code": 1,
|
|
"error_message": "Subscribe and unsubscribe are only allowed for websocket.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"request": {
|
|
"method": "subscribe",
|
|
"params": [{}]
|
|
}
|
|
}
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPMissingCommand)
|
|
{
|
|
static auto constexpr request = R"({"method2": "server_info"})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response = "Null method";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(session->message, response);
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPCommandNotString)
|
|
{
|
|
static auto constexpr request = R"({"method": 1})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response = "method is not string";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(session->message, response);
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPCommandIsEmpty)
|
|
{
|
|
static auto constexpr request = R"({"method": ""})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response = "method is empty";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(session->message, response);
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsMissingCommand)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr request = R"({
|
|
"command2": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response = R"({
|
|
"error": "missingCommand",
|
|
"error_code": 6001,
|
|
"error_message": "Method/Command is not specified or is not a string.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"id": 99,
|
|
"request":{
|
|
"command2": "server_info",
|
|
"id": 99
|
|
}
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableNotArray)
|
|
{
|
|
static auto constexpr response = "params unparseable";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"method": "ledger",
|
|
"params": "wrong"
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(session->message, response);
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPParamsUnparseableArrayWithDigit)
|
|
{
|
|
static auto constexpr response = "params unparseable";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"method": "ledger",
|
|
"params": [1]
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(session->message, response);
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPInternalError)
|
|
{
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"error": "internal",
|
|
"error_code": 73,
|
|
"error_message": "Internal error.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"request": {
|
|
"method": "ledger",
|
|
"params": [{}]
|
|
}
|
|
}
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"method": "ledger",
|
|
"params": [{}]
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyInternalError).Times(1);
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_)).Times(1).WillOnce(testing::Throw(std::runtime_error("MyError")));
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsInternalError)
|
|
{
|
|
session->upgraded = true;
|
|
|
|
static auto constexpr response = R"({
|
|
"error": "internal",
|
|
"error_code": 73,
|
|
"error_message": "Internal error.",
|
|
"status": "error",
|
|
"type": "response",
|
|
"id": "123",
|
|
"request": {
|
|
"command": "ledger",
|
|
"id": "123"
|
|
}
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr requestJSON = R"({
|
|
"command": "ledger",
|
|
"id": "123"
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyInternalError).Times(1);
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_)).Times(1).WillOnce(testing::Throw(std::runtime_error("MyError")));
|
|
|
|
(*handler)(requestJSON, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPOutDated)
|
|
{
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr result = "{}";
|
|
static auto constexpr response = R"({
|
|
"result": {
|
|
"status": "success"
|
|
},
|
|
"warnings": [
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
},
|
|
{
|
|
"id": 2002,
|
|
"message": "This server may be out of date"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(61));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsOutdated)
|
|
{
|
|
session->upgraded = true;
|
|
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr result = "{}";
|
|
static auto constexpr response = R"({
|
|
"result":{},
|
|
"id": 99,
|
|
"status": "success",
|
|
"type": "response",
|
|
"warnings":[
|
|
{
|
|
"id": 2001,
|
|
"message": "This is a clio server. clio only serves validated data. If you want to talk to rippled, include 'ledger_index':'current' in your request"
|
|
},
|
|
{
|
|
"id": 2002,
|
|
"message": "This server may be out of date"
|
|
}
|
|
]
|
|
})";
|
|
EXPECT_CALL(*rpcEngine, buildResponse(testing::_))
|
|
.WillOnce(testing::Return(rpc::Result{boost::json::parse(result).as_object()}));
|
|
EXPECT_CALL(*rpcEngine, notifyComplete("server_info", testing::_)).Times(1);
|
|
|
|
EXPECT_CALL(*etl, lastCloseAgeSeconds()).WillOnce(testing::Return(61));
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsTooBusy)
|
|
{
|
|
session->upgraded = true;
|
|
|
|
auto localRpcEngine = std::make_shared<MockRPCEngine>();
|
|
auto localHandler =
|
|
std::make_shared<RPCServerHandler<MockRPCEngine, MockETLService>>(cfg, backend, localRpcEngine, etl);
|
|
static auto constexpr request = R"({
|
|
"command": "server_info",
|
|
"id": 99
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response =
|
|
R"({
|
|
"error": "tooBusy",
|
|
"error_code": 9,
|
|
"error_message": "The server is too busy to help you now.",
|
|
"status": "error",
|
|
"type": "response"
|
|
})";
|
|
|
|
EXPECT_CALL(*localRpcEngine, notifyTooBusy).Times(1);
|
|
EXPECT_CALL(*localRpcEngine, post).WillOnce(testing::Return(false));
|
|
|
|
(*localHandler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPTooBusy)
|
|
{
|
|
auto localRpcEngine = std::make_shared<MockRPCEngine>();
|
|
auto localHandler =
|
|
std::make_shared<RPCServerHandler<MockRPCEngine, MockETLService>>(cfg, backend, localRpcEngine, etl);
|
|
static auto constexpr request = R"({
|
|
"method": "server_info",
|
|
"params": [{}]
|
|
})";
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
static auto constexpr response =
|
|
R"({
|
|
"error": "tooBusy",
|
|
"error_code": 9,
|
|
"error_message": "The server is too busy to help you now.",
|
|
"status": "error",
|
|
"type": "response"
|
|
})";
|
|
|
|
EXPECT_CALL(*localRpcEngine, notifyTooBusy).Times(1);
|
|
EXPECT_CALL(*localRpcEngine, post).WillOnce(testing::Return(false));
|
|
|
|
(*localHandler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, HTTPRequestNotJson)
|
|
{
|
|
static auto constexpr request = "not json";
|
|
static auto constexpr responsePrefix = "Unable to parse JSON from the request";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_THAT(session->message, testing::StartsWith(responsePrefix));
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_F(WebRPCServerHandlerTest, WsRequestNotJson)
|
|
{
|
|
session->upgraded = true;
|
|
static auto constexpr request = "not json";
|
|
static auto constexpr response =
|
|
R"({
|
|
"error": "badSyntax",
|
|
"error_code": 1,
|
|
"error_message": "Syntax error.",
|
|
"status": "error",
|
|
"type": "response"
|
|
})";
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(boost::json::parse(session->message), boost::json::parse(response));
|
|
}
|
|
|
|
struct InvalidAPIVersionTestBundle {
|
|
std::string testName;
|
|
std::string version;
|
|
std::string wsMessage;
|
|
};
|
|
|
|
// parameterized test cases for parameters check
|
|
struct WebRPCServerHandlerInvalidAPIVersionParamTest : public WebRPCServerHandlerTest,
|
|
public testing::WithParamInterface<InvalidAPIVersionTestBundle> {
|
|
};
|
|
|
|
auto
|
|
generateInvalidVersions()
|
|
{
|
|
return std::vector<InvalidAPIVersionTestBundle>{
|
|
{"v0", "0", fmt::format("Requested API version is lower than minimum supported ({})", rpc::API_VERSION_MIN)},
|
|
{"v4", "4", fmt::format("Requested API version is higher than maximum supported ({})", rpc::API_VERSION_MAX)},
|
|
{"null", "null", "API version must be an integer"},
|
|
{"str", "\"bogus\"", "API version must be an integer"},
|
|
{"bool", "false", "API version must be an integer"},
|
|
{"double", "12.34", "API version must be an integer"},
|
|
};
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(
|
|
WebRPCServerHandlerAPIVersionGroup,
|
|
WebRPCServerHandlerInvalidAPIVersionParamTest,
|
|
testing::ValuesIn(generateInvalidVersions()),
|
|
tests::util::NameGenerator
|
|
);
|
|
|
|
TEST_P(WebRPCServerHandlerInvalidAPIVersionParamTest, HTTPInvalidAPIVersion)
|
|
{
|
|
auto request = fmt::format(
|
|
R"({{
|
|
"method": "server_info",
|
|
"params": [{{
|
|
"api_version": {}
|
|
}}]
|
|
}})",
|
|
GetParam().version
|
|
);
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
EXPECT_EQ(session->message, "invalid_API_version");
|
|
EXPECT_EQ(session->lastStatus, boost::beast::http::status::bad_request);
|
|
}
|
|
|
|
TEST_P(WebRPCServerHandlerInvalidAPIVersionParamTest, WSInvalidAPIVersion)
|
|
{
|
|
session->upgraded = true;
|
|
auto request = fmt::format(
|
|
R"({{
|
|
"method": "server_info",
|
|
"api_version": {}
|
|
}})",
|
|
GetParam().version
|
|
);
|
|
|
|
backend->setRange(MINSEQ, MAXSEQ);
|
|
|
|
EXPECT_CALL(*rpcEngine, notifyBadSyntax).Times(1);
|
|
|
|
(*handler)(request, session);
|
|
|
|
auto response = boost::json::parse(session->message);
|
|
EXPECT_TRUE(response.is_object());
|
|
EXPECT_TRUE(response.as_object().contains("error"));
|
|
EXPECT_EQ(response.at("error").as_string(), "invalid_API_version");
|
|
EXPECT_TRUE(response.as_object().contains("error_message"));
|
|
EXPECT_EQ(response.at("error_message").as_string(), GetParam().wsMessage);
|
|
EXPECT_TRUE(response.as_object().contains("error_code"));
|
|
EXPECT_EQ(response.at("error_code").as_int64(), static_cast<int64_t>(rpc::ClioError::rpcINVALID_API_VERSION));
|
|
}
|
|
|
|
} // namespace
|