fix: Fix the ledger_index timezone issue (#1522)

Fix unittest failures
This commit is contained in:
cyan317
2024-07-09 13:14:22 +01:00
committed by GitHub
parent d536433d64
commit 094ed8f299
8 changed files with 167 additions and 14 deletions

View File

@@ -119,6 +119,7 @@ target_sources(
util/RandomTests.cpp
util/RetryTests.cpp
util/SignalsHandlerTests.cpp
util/TimeUtilsTests.cpp
util/TxUtilTests.cpp
# Webserver
web/AdminVerificationTests.cpp

View File

@@ -90,6 +90,24 @@ TEST_F(RPCLedgerIndexTest, EarlierThanMinLedger)
});
}
TEST_F(RPCLedgerIndexTest, ChangeTimeZone)
{
setenv("TZ", "EST+5", 1);
backend->setRange(RANGEMIN, RANGEMAX);
auto const handler = AnyHandler{LedgerIndexHandler{backend}};
auto const req = json::parse(R"({"date": "2024-06-25T12:23:05Z"})");
auto const ledgerHeader =
CreateLedgerHeaderWithUnixTime(LEDGERHASH, RANGEMIN, 1719318190); //"2024-06-25T12:23:10Z"
EXPECT_CALL(*backend, fetchLedgerBySequence(RANGEMIN, _)).WillOnce(Return(ledgerHeader));
runSpawn([&](auto yield) {
auto const output = handler.process(req, Context{yield});
ASSERT_FALSE(output);
auto const err = rpc::makeError(output.result.error());
EXPECT_EQ(err.at("error").as_string(), "lgrNotFound");
});
unsetenv("TZ");
}
struct LedgerIndexTestsCaseBundle {
std::string testName;
std::string json;

View File

@@ -0,0 +1,51 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, 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 "util/TimeUtils.hpp"
#include <gtest/gtest.h>
TEST(TimeUtilTests, SystemTpFromUTCStrSuccess)
{
auto const tp = util::SystemTpFromUTCStr("2024-01-01T10:50:40Z", "%Y-%m-%dT%H:%M:%SZ");
ASSERT_TRUE(tp.has_value());
auto const time = std::chrono::system_clock::to_time_t(tp.value());
std::tm timeStruct;
gmtime_r(&time, &timeStruct);
EXPECT_EQ(timeStruct.tm_year + 1900, 2024);
EXPECT_EQ(timeStruct.tm_mon, 0);
EXPECT_EQ(timeStruct.tm_mday, 1);
EXPECT_EQ(timeStruct.tm_hour, 10);
EXPECT_EQ(timeStruct.tm_min, 50);
EXPECT_EQ(timeStruct.tm_sec, 40);
}
TEST(TimeUtilTests, SystemTpFromUTCStrFail)
{
auto const tp = util::SystemTpFromUTCStr("2024-01-01T", "%Y-%m-%dT%H:%M:%SZ");
ASSERT_FALSE(tp.has_value());
}
TEST(TimeUtilTests, SystemTpFromLedgerCloseTime)
{
using namespace std::chrono;
auto const tp = util::SystemTpFromLedgerCloseTime(ripple::NetClock::time_point{seconds{0}});
EXPECT_EQ(tp.time_since_epoch(), ripple::epoch_offset);
}