Return error when limit<=0 (#804)

Fix #806
This commit is contained in:
cyan317
2023-08-02 15:34:42 +01:00
committed by GitHub
parent 1804e3e9c0
commit c90bc15959
45 changed files with 832 additions and 562 deletions

View File

@@ -56,7 +56,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonHexLedgerHash)
"ledger_hash": "xxx"
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -76,7 +76,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonStringLedgerHash)
"ledger_hash": 123
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -96,7 +96,7 @@ TEST_F(RPCAccountLinesHandlerTest, InvalidLedgerIndexString)
"ledger_index": "notvalidated"
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -115,7 +115,7 @@ TEST_F(RPCAccountLinesHandlerTest, MarkerNotString)
"marker": 9
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -137,7 +137,7 @@ TEST_F(RPCAccountLinesHandlerTest, InvalidMarker)
"marker": "123invalid"
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -152,7 +152,7 @@ TEST_F(RPCAccountLinesHandlerTest, InvalidMarker)
"marker": 401
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -169,7 +169,7 @@ TEST_F(RPCAccountLinesHandlerTest, AccountInvalidFormat)
R"({
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jp"
})");
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "actMalformed");
@@ -186,7 +186,7 @@ TEST_F(RPCAccountLinesHandlerTest, AccountNotString)
R"({
"account": 12
})");
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -204,7 +204,7 @@ TEST_F(RPCAccountLinesHandlerTest, PeerInvalidFormat)
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"peer": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jp"
})");
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "actMalformed");
@@ -221,7 +221,7 @@ TEST_F(RPCAccountLinesHandlerTest, PeerNotString)
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"peer": 12
})");
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -230,6 +230,57 @@ TEST_F(RPCAccountLinesHandlerTest, PeerNotString)
});
}
TEST_F(RPCAccountLinesHandlerTest, LimitNotInt)
{
runSpawn([this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const input = json::parse(
R"({
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"limit": "t"
})");
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "invalidParams");
});
}
TEST_F(RPCAccountLinesHandlerTest, LimitNagetive)
{
runSpawn([this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const input = json::parse(
R"({
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"limit": -1
})");
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "invalidParams");
});
}
TEST_F(RPCAccountLinesHandlerTest, LimitZero)
{
runSpawn([this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const input = json::parse(
R"({
"account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"limit": 0
})");
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "invalidParams");
});
}
// error case ledger non exist via hash
TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerHash)
{
@@ -248,7 +299,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerHash)
LEDGERHASH));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
@@ -274,7 +325,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerStringIndex)
ACCOUNT));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
@@ -298,7 +349,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerIntIndex)
ACCOUNT));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
@@ -326,7 +377,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerHash2)
LEDGERHASH));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
@@ -351,7 +402,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistLedgerViaLedgerIndex2)
ACCOUNT));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
@@ -380,7 +431,7 @@ TEST_F(RPCAccountLinesHandlerTest, NonExistAccount)
LEDGERHASH));
runSpawn([&, this](auto yield) {
auto const handler = AnyHandler{AccountLinesHandler{mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_FALSE(output);
auto const err = RPC::makeError(output.error());
EXPECT_EQ(err.at("error").as_string(), "actNotFound");
@@ -463,7 +514,7 @@ TEST_F(RPCAccountLinesHandlerTest, DefaultParameterTest)
})";
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(json::parse(correctOutput), *output);
});
@@ -516,7 +567,7 @@ TEST_F(RPCAccountLinesHandlerTest, UseLimit)
"limit": 20
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("lines").as_array().size(), 20);
@@ -531,7 +582,7 @@ TEST_F(RPCAccountLinesHandlerTest, UseLimit)
"limit": 9
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output); // todo: check limit somehow?
});
@@ -543,7 +594,7 @@ TEST_F(RPCAccountLinesHandlerTest, UseLimit)
"limit": 401
}})",
ACCOUNT));
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output); // todo: check limit somehow?
});
}
@@ -607,7 +658,7 @@ TEST_F(RPCAccountLinesHandlerTest, UseDestination)
ACCOUNT3));
runSpawn([&, this](auto yield) {
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("lines").as_array().size(), 20);
});
@@ -643,7 +694,7 @@ TEST_F(RPCAccountLinesHandlerTest, EmptyChannel)
ACCOUNT));
runSpawn([&, this](auto yield) {
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ((*output).as_object().at("lines").as_array().size(), 0);
});
@@ -731,7 +782,7 @@ TEST_F(RPCAccountLinesHandlerTest, OptionalResponseField)
ACCOUNT));
runSpawn([&, this](auto yield) {
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(json::parse(correctOutput), *output);
});
@@ -800,7 +851,7 @@ TEST_F(RPCAccountLinesHandlerTest, MarkerOutput)
limit));
runSpawn([&, this](auto yield) {
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
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((*output).as_object().at("lines").as_array().size(), 15);
@@ -859,7 +910,7 @@ TEST_F(RPCAccountLinesHandlerTest, MarkerInput)
nextPage));
runSpawn([&, this](auto yield) {
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_TRUE((*output).as_object().if_contains("marker") == nullptr);
// the first item is the marker itself, so the result will have limit-1
@@ -945,7 +996,7 @@ TEST_F(RPCAccountLinesHandlerTest, LimitLessThanMin)
AccountLinesHandler::LIMIT_MIN);
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(json::parse(correctOutput), *output);
});
@@ -1028,7 +1079,7 @@ TEST_F(RPCAccountLinesHandlerTest, LimitMoreThanMax)
AccountLinesHandler::LIMIT_MAX);
auto handler = AnyHandler{AccountLinesHandler{this->mockBackendPtr}};
auto const output = handler.process(input, Context{std::ref(yield)});
auto const output = handler.process(input, Context{yield});
ASSERT_TRUE(output);
EXPECT_EQ(json::parse(correctOutput), *output);
});