Fixes #617
This commit is contained in:
cyan317
2023-05-02 13:24:23 +01:00
committed by GitHub
parent 4b2d53fc2f
commit 7776a5ffb6
6 changed files with 601 additions and 0 deletions

View File

@@ -0,0 +1,213 @@
//------------------------------------------------------------------------------
/*
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/common/AnyHandler.h>
#include <rpc/ngHandlers/BookChanges.h>
#include <util/Fixtures.h>
#include <util/TestObject.h>
#include <fmt/core.h>
using namespace RPCng;
namespace json = boost::json;
using namespace testing;
constexpr static auto CURRENCY = "0158415500000000C1F76FF6ECB0BAC600000000";
constexpr static auto ISSUER = "rK9DrarGKnVEo2nYp5MfVRXRYf5yRX3mwD";
constexpr static auto ACCOUNT1 = "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn";
constexpr static auto ACCOUNT2 = "rLEsXccBGNR3UPuPu2hUXPjziKC3qKSBun";
constexpr static auto LEDGERHASH = "4BC50C9B0D8515D3EAAE1E74B29A95804346C491EE1A95BF25E4AAB854A6A652";
constexpr static auto MAXSEQ = 30;
constexpr static auto MINSEQ = 10;
class RPCBookChangesHandlerTest : public HandlerBaseTest
{
};
struct BookChangesParamTestCaseBundle
{
std::string testName;
std::string testJson;
std::string expectedError;
std::string expectedErrorMessage;
};
// parameterized test cases for parameters check
struct BookChangesParameterTest : public RPCBookChangesHandlerTest,
public WithParamInterface<BookChangesParamTestCaseBundle>
{
struct NameGenerator
{
template <class ParamType>
std::string
operator()(const testing::TestParamInfo<ParamType>& info) const
{
auto bundle = static_cast<BookChangesParamTestCaseBundle>(info.param);
return bundle.testName;
}
};
};
static auto
generateTestValuesForParametersTest()
{
return std::vector<BookChangesParamTestCaseBundle>{
BookChangesParamTestCaseBundle{
"LedgerHashInvalid", R"({"ledger_hash":"1"})", "invalidParams", "ledger_hashMalformed"},
BookChangesParamTestCaseBundle{
"LedgerHashNotString", R"({"ledger_hash":1})", "invalidParams", "ledger_hashNotString"},
BookChangesParamTestCaseBundle{
"LedgerIndexInvalid", R"({"ledger_index":"a"})", "invalidParams", "ledgerIndexMalformed"},
};
}
INSTANTIATE_TEST_CASE_P(
RPCBookChangesGroup1,
BookChangesParameterTest,
ValuesIn(generateTestValuesForParametersTest()),
BookChangesParameterTest::NameGenerator{});
TEST_P(BookChangesParameterTest, InvalidParams)
{
auto const testBundle = GetParam();
runSpawn([&, this](auto& yield) {
auto const handler = AnyHandler{BookChangesHandler{mockBackendPtr}};
auto const req = json::parse(testBundle.testJson);
auto const output = handler.process(req, Context{std::ref(yield)});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), testBundle.expectedError);
EXPECT_EQ(err.at("error_message").as_string(), testBundle.expectedErrorMessage);
});
}
TEST_F(RPCBookChangesHandlerTest, LedgerNonExistViaIntSequence)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(MINSEQ); // min
mockBackendPtr->updateRange(MAXSEQ); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(MAXSEQ, _))
.WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
auto const static input = boost::json::parse(R"({"ledger_index":30})");
auto const handler = AnyHandler{BookChangesHandler{mockBackendPtr}};
runSpawn([&](auto& yield) {
auto const output = handler.process(input, Context{std::ref(yield)});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCBookChangesHandlerTest, LedgerNonExistViaStringSequence)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(MINSEQ); // min
mockBackendPtr->updateRange(MAXSEQ); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(MAXSEQ, _)).WillByDefault(Return(std::nullopt));
auto const static input = boost::json::parse(R"({"ledger_index":"30"})");
auto const handler = AnyHandler{BookChangesHandler{mockBackendPtr}};
runSpawn([&](auto& yield) {
auto const output = handler.process(input, Context{std::ref(yield)});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCBookChangesHandlerTest, LedgerNonExistViaHash)
{
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(MINSEQ); // min
mockBackendPtr->updateRange(MAXSEQ); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerByHash).Times(1);
// return empty ledgerinfo
ON_CALL(*rawBackendPtr, fetchLedgerByHash(ripple::uint256{LEDGERHASH}, _))
.WillByDefault(Return(std::optional<ripple::LedgerInfo>{}));
auto const static input = boost::json::parse(fmt::format(
R"({{
"ledger_hash":"{}"
}})",
LEDGERHASH));
auto const handler = AnyHandler{BookChangesHandler{mockBackendPtr}};
runSpawn([&](auto& yield) {
auto const output = handler.process(input, Context{std::ref(yield)});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
EXPECT_EQ(err.at("error_message").as_string(), "ledgerNotFound");
});
}
TEST_F(RPCBookChangesHandlerTest, NormalPath)
{
static auto constexpr expectedOut =
R"({
"type":"bookChanges",
"ledger_hash":"4BC50C9B0D8515D3EAAE1E74B29A95804346C491EE1A95BF25E4AAB854A6A652",
"ledger_index":30,
"ledger_time":0,
"validated":true,
"changes":[
{
"currency_a":"XRP_drops",
"currency_b":"rK9DrarGKnVEo2nYp5MfVRXRYf5yRX3mwD/0158415500000000C1F76FF6ECB0BAC600000000",
"volume_a":"2",
"volume_b":"2",
"high":"-1",
"low":"-1",
"open":"-1",
"close":"-1"
}
]
})";
auto const rawBackendPtr = static_cast<MockBackend*>(mockBackendPtr.get());
mockBackendPtr->updateRange(MINSEQ); // min
mockBackendPtr->updateRange(MAXSEQ); // max
EXPECT_CALL(*rawBackendPtr, fetchLedgerBySequence).Times(1);
ON_CALL(*rawBackendPtr, fetchLedgerBySequence(MAXSEQ, _))
.WillByDefault(Return(CreateLedgerInfo(LEDGERHASH, MAXSEQ)));
auto transactions = std::vector<TransactionAndMetadata>{};
auto trans1 = TransactionAndMetadata();
ripple::STObject obj = CreatePaymentTransactionObject(ACCOUNT1, ACCOUNT2, 1, 1, 32);
trans1.transaction = obj.getSerializer().peekData();
trans1.ledgerSequence = 32;
ripple::STObject metaObj = CreateMetaDataForBookChange(CURRENCY, ISSUER, 22, 1, 3, 3, 1);
trans1.metadata = metaObj.getSerializer().peekData();
transactions.push_back(trans1);
EXPECT_CALL(*rawBackendPtr, fetchAllTransactionsInLedger).Times(1);
ON_CALL(*rawBackendPtr, fetchAllTransactionsInLedger(MAXSEQ, _)).WillByDefault(Return(transactions));
auto const handler = AnyHandler{BookChangesHandler{mockBackendPtr}};
runSpawn([&](auto& yield) {
auto const output = handler.process(json::parse("{}"), Context{std::ref(yield)});
ASSERT_TRUE(output);
EXPECT_EQ(*output, json::parse(expectedOut));
});
}