175e8e218SMyung Bae #include "async_resp.hpp"
275e8e218SMyung Bae #include "log_services.hpp"
3b0983db2SEd Tanous #include "manager_logservices_journal.hpp"
475e8e218SMyung Bae 
575e8e218SMyung Bae #include <systemd/sd-id128.h>
675e8e218SMyung Bae 
7f0b59af4SEd Tanous #include <cstdint>
875e8e218SMyung Bae #include <format>
9f0b59af4SEd Tanous #include <memory>
1075e8e218SMyung Bae #include <string>
1175e8e218SMyung Bae 
1275e8e218SMyung Bae #include <gtest/gtest.h>
1375e8e218SMyung Bae 
1475e8e218SMyung Bae namespace redfish
1575e8e218SMyung Bae {
1675e8e218SMyung Bae namespace
1775e8e218SMyung Bae {
1875e8e218SMyung Bae 
1975e8e218SMyung Bae TEST(LogServicesBMCJouralTest, LogServicesBMCJouralGetReturnsError)
2075e8e218SMyung Bae {
2175e8e218SMyung Bae     auto shareAsyncResp = std::make_shared<bmcweb::AsyncResp>();
2275e8e218SMyung Bae     sd_id128_t bootIDOut{};
2375e8e218SMyung Bae     uint64_t timestampOut = 0;
2475e8e218SMyung Bae     uint64_t indexOut = 0;
2575e8e218SMyung Bae     uint64_t timestampIn = 1740970301UL;
2675e8e218SMyung Bae     std::string badBootIDStr = "78770392794344a29f81507f3ce5e";
2775e8e218SMyung Bae     std::string goodBootIDStr = "78770392794344a29f81507f3ce5e78c";
2875e8e218SMyung Bae     sd_id128_t goodBootID{};
2975e8e218SMyung Bae 
3075e8e218SMyung Bae     // invalid test cases
3175e8e218SMyung Bae     EXPECT_FALSE(getTimestampFromID(shareAsyncResp, "", bootIDOut, timestampOut,
3275e8e218SMyung Bae                                     indexOut));
3375e8e218SMyung Bae     EXPECT_FALSE(getTimestampFromID(shareAsyncResp, badBootIDStr, bootIDOut,
3475e8e218SMyung Bae                                     timestampOut, indexOut));
3575e8e218SMyung Bae     EXPECT_FALSE(getTimestampFromID(
3675e8e218SMyung Bae         shareAsyncResp, std::format("{}_{}", badBootIDStr, timestampIn),
3775e8e218SMyung Bae         bootIDOut, timestampOut, indexOut));
3875e8e218SMyung Bae     EXPECT_FALSE(getTimestampFromID(
3975e8e218SMyung Bae         shareAsyncResp, std::format("{}_{}", badBootIDStr, timestampIn),
4075e8e218SMyung Bae         bootIDOut, timestampOut, indexOut));
4175e8e218SMyung Bae 
4275e8e218SMyung Bae     // obtain a goodBootID
4375e8e218SMyung Bae     EXPECT_GE(sd_id128_from_string(goodBootIDStr.c_str(), &goodBootID), 0);
4475e8e218SMyung Bae 
4575e8e218SMyung Bae     EXPECT_FALSE(getTimestampFromID(
4675e8e218SMyung Bae         shareAsyncResp, std::format("{}_{}", goodBootIDStr, "InvalidNum"),
4775e8e218SMyung Bae         bootIDOut, timestampOut, indexOut));
4875e8e218SMyung Bae 
4975e8e218SMyung Bae     // Success cases
5075e8e218SMyung Bae     EXPECT_TRUE(getTimestampFromID(
5175e8e218SMyung Bae         shareAsyncResp, std::format("{}_{}", goodBootIDStr, timestampIn),
5275e8e218SMyung Bae         bootIDOut, timestampOut, indexOut));
5375e8e218SMyung Bae     EXPECT_NE(sd_id128_equal(goodBootID, bootIDOut), 0);
5475e8e218SMyung Bae     EXPECT_EQ(timestampIn, timestampOut);
5575e8e218SMyung Bae     EXPECT_EQ(indexOut, 0);
5675e8e218SMyung Bae 
57*055713e4SEd Tanous     // Index of _1 is invalid. First index is omitted
58*055713e4SEd Tanous     EXPECT_FALSE(getTimestampFromID(
59*055713e4SEd Tanous         shareAsyncResp, std::format("{}_{}_1", goodBootIDStr, timestampIn),
60*055713e4SEd Tanous         bootIDOut, timestampOut, indexOut));
61*055713e4SEd Tanous 
62*055713e4SEd Tanous     // Index of _2 is valid, and should return a zero index (1)
6375e8e218SMyung Bae     EXPECT_TRUE(getTimestampFromID(
64*055713e4SEd Tanous         shareAsyncResp, std::format("{}_{}_2", goodBootIDStr, timestampIn),
65*055713e4SEd Tanous         bootIDOut, timestampOut, indexOut));
6675e8e218SMyung Bae     EXPECT_NE(sd_id128_equal(goodBootID, bootIDOut), 0);
6775e8e218SMyung Bae     EXPECT_EQ(timestampIn, timestampOut);
68*055713e4SEd Tanous     EXPECT_EQ(indexOut, 1);
6975e8e218SMyung Bae }
7075e8e218SMyung Bae 
716f056f24SEd Tanous TEST(LogServicesPostCodeParse, PostCodeParse)
726f056f24SEd Tanous {
736f056f24SEd Tanous     uint64_t currentValue = 0;
746f056f24SEd Tanous     uint16_t index = 0;
756f056f24SEd Tanous     EXPECT_TRUE(parsePostCode("B1-2", currentValue, index));
766f056f24SEd Tanous     EXPECT_EQ(currentValue, 2);
776f056f24SEd Tanous     EXPECT_EQ(index, 1);
786f056f24SEd Tanous     EXPECT_TRUE(parsePostCode("B200-300", currentValue, index));
796f056f24SEd Tanous     EXPECT_EQ(currentValue, 300);
806f056f24SEd Tanous     EXPECT_EQ(index, 200);
816f056f24SEd Tanous 
826f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("", currentValue, index));
836f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B", currentValue, index));
846f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1", currentValue, index));
856f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1-", currentValue, index));
866f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1A-2", currentValue, index));
876f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1A-2", currentValue, index));
886f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1A-2z", currentValue, index));
896f056f24SEd Tanous     // Uint16_t max + 1
906f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B65536-1", currentValue, index));
916f056f24SEd Tanous 
926f056f24SEd Tanous     // Uint64_t max + 1
936f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B1-18446744073709551616", currentValue, index));
946f056f24SEd Tanous 
956f056f24SEd Tanous     // Negative numbers
966f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B-1-2", currentValue, index));
976f056f24SEd Tanous     EXPECT_FALSE(parsePostCode("B-1--2", currentValue, index));
986f056f24SEd Tanous }
996f056f24SEd Tanous 
10075e8e218SMyung Bae } // namespace
10175e8e218SMyung Bae } // namespace redfish
102