diff --git a/src/rpc/handlers/LedgerEntry.cpp b/src/rpc/handlers/LedgerEntry.cpp index 14be58305..98827ad99 100644 --- a/src/rpc/handlers/LedgerEntry.cpp +++ b/src/rpc/handlers/LedgerEntry.cpp @@ -24,6 +24,7 @@ #include "rpc/RPCHelpers.hpp" #include "rpc/common/Types.hpp" #include "util/AccountUtils.hpp" +#include "util/Assert.hpp" #include #include @@ -33,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -153,6 +155,7 @@ LedgerEntryHandler::process(LedgerEntryHandler::Input input, Context const& ctx) // check ledger exists auto const range = sharedPtrBackend_->fetchLedgerRange(); + ASSERT(range.has_value(), "Ledger range must be available"); auto const lgrInfoOrStatus = getLedgerHeaderFromHashOrSeq( *sharedPtrBackend_, ctx.yield, input.ledgerHash, input.ledgerIndex, range->maxSequence ); diff --git a/tests/unit/rpc/handlers/LedgerEntryTests.cpp b/tests/unit/rpc/handlers/LedgerEntryTests.cpp index 1b43c0d17..aec5bc02f 100644 --- a/tests/unit/rpc/handlers/LedgerEntryTests.cpp +++ b/tests/unit/rpc/handlers/LedgerEntryTests.cpp @@ -26,6 +26,9 @@ #include "util/NameGenerator.hpp" #include "util/TestObject.hpp" +#include +#include +#include #include #include #include @@ -2726,6 +2729,7 @@ TEST_F(RPCLedgerEntryTest, LedgerEntryDeleted) // Expected Result: return entryNotFound error TEST_F(RPCLedgerEntryTest, LedgerEntryNotExist) { + backend->setRange(RANGEMIN, RANGEMAX); auto const ledgerinfo = CreateLedgerHeader(LEDGERHASH, RANGEMAX); EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, _)).WillRepeatedly(Return(ledgerinfo)); EXPECT_CALL(*backend, doFetchLedgerObject(ripple::uint256{INDEX1}, RANGEMAX, _)) @@ -2916,6 +2920,7 @@ TEST_F(RPCLedgerEntryTest, ObjectDeletedPreviously) // Expected Result: return entryNotFound error TEST_F(RPCLedgerEntryTest, ObjectSeqNotExist) { + backend->setRange(RANGEMIN, RANGEMAX); auto const ledgerinfo = CreateLedgerHeader(LEDGERHASH, RANGEMAX); EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMAX, _)).WillRepeatedly(Return(ledgerinfo)); EXPECT_CALL(*backend, doFetchLedgerObject(ripple::uint256{INDEX1}, RANGEMAX, _)) @@ -2938,3 +2943,27 @@ TEST_F(RPCLedgerEntryTest, ObjectSeqNotExist) EXPECT_EQ(myerr, "entryNotFound"); }); } + +using RPCLedgerEntryDeathTest = RPCLedgerEntryTest; + +TEST_F(RPCLedgerEntryDeathTest, RangeNotAvailable) +{ + boost::asio::io_context ctx; + bool checkCalled = false; + spawn(ctx, [&, _ = make_work_guard(ctx)](boost::asio::yield_context yield) { + auto const handler = AnyHandler{LedgerEntryHandler{backend}}; + auto const req = json::parse(fmt::format( + R"({{ + "index": "{}" + }})", + INDEX1 + )); + checkCalled = true; + EXPECT_DEATH( + { [[maybe_unused]] auto __ = handler.process(req, Context{yield}); }, "Ledger range must be available" + ); + }); + + ctx.run(); + ASSERT_TRUE(checkCalled); +}