1 #include "async_resp.hpp"
2 #include "log_services.hpp"
3 
4 #include <systemd/sd-id128.h>
5 
6 #include <cstdint>
7 #include <format>
8 #include <memory>
9 #include <string>
10 
11 #include <gtest/gtest.h>
12 
13 namespace redfish
14 {
15 namespace
16 {
17 
18 TEST(LogServicesBMCJouralTest, LogServicesBMCJouralGetReturnsError)
19 {
20     auto shareAsyncResp = std::make_shared<bmcweb::AsyncResp>();
21     sd_id128_t bootIDOut{};
22     uint64_t timestampOut = 0;
23     uint64_t indexOut = 0;
24     uint64_t timestampIn = 1740970301UL;
25     std::string badBootIDStr = "78770392794344a29f81507f3ce5e";
26     std::string goodBootIDStr = "78770392794344a29f81507f3ce5e78c";
27     sd_id128_t goodBootID{};
28 
29     // invalid test cases
30     EXPECT_FALSE(getTimestampFromID(shareAsyncResp, "", bootIDOut, timestampOut,
31                                     indexOut));
32     EXPECT_FALSE(getTimestampFromID(shareAsyncResp, badBootIDStr, bootIDOut,
33                                     timestampOut, indexOut));
34     EXPECT_FALSE(getTimestampFromID(
35         shareAsyncResp, std::format("{}_{}", badBootIDStr, timestampIn),
36         bootIDOut, timestampOut, indexOut));
37     EXPECT_FALSE(getTimestampFromID(
38         shareAsyncResp, std::format("{}_{}", badBootIDStr, timestampIn),
39         bootIDOut, timestampOut, indexOut));
40 
41     // obtain a goodBootID
42     EXPECT_GE(sd_id128_from_string(goodBootIDStr.c_str(), &goodBootID), 0);
43 
44     EXPECT_FALSE(getTimestampFromID(
45         shareAsyncResp, std::format("{}_{}", goodBootIDStr, "InvalidNum"),
46         bootIDOut, timestampOut, indexOut));
47 
48     // Success cases
49     EXPECT_TRUE(getTimestampFromID(
50         shareAsyncResp, std::format("{}_{}", goodBootIDStr, timestampIn),
51         bootIDOut, timestampOut, indexOut));
52     EXPECT_NE(sd_id128_equal(goodBootID, bootIDOut), 0);
53     EXPECT_EQ(timestampIn, timestampOut);
54     EXPECT_EQ(indexOut, 0);
55 
56     uint64_t indexIn = 1;
57     EXPECT_TRUE(getTimestampFromID(
58         shareAsyncResp,
59         std::format("{}_{}_{}", goodBootIDStr, timestampIn, indexIn), bootIDOut,
60         timestampOut, indexOut));
61     EXPECT_NE(sd_id128_equal(goodBootID, bootIDOut), 0);
62     EXPECT_EQ(timestampIn, timestampOut);
63     EXPECT_EQ(indexOut, indexIn);
64 }
65 
66 TEST(LogServicesPostCodeParse, PostCodeParse)
67 {
68     uint64_t currentValue = 0;
69     uint16_t index = 0;
70     EXPECT_TRUE(parsePostCode("B1-2", currentValue, index));
71     EXPECT_EQ(currentValue, 2);
72     EXPECT_EQ(index, 1);
73     EXPECT_TRUE(parsePostCode("B200-300", currentValue, index));
74     EXPECT_EQ(currentValue, 300);
75     EXPECT_EQ(index, 200);
76 
77     EXPECT_FALSE(parsePostCode("", currentValue, index));
78     EXPECT_FALSE(parsePostCode("B", currentValue, index));
79     EXPECT_FALSE(parsePostCode("B1", currentValue, index));
80     EXPECT_FALSE(parsePostCode("B1-", currentValue, index));
81     EXPECT_FALSE(parsePostCode("B1A-2", currentValue, index));
82     EXPECT_FALSE(parsePostCode("B1A-2", currentValue, index));
83     EXPECT_FALSE(parsePostCode("B1A-2z", currentValue, index));
84     // Uint16_t max + 1
85     EXPECT_FALSE(parsePostCode("B65536-1", currentValue, index));
86 
87     // Uint64_t max + 1
88     EXPECT_FALSE(parsePostCode("B1-18446744073709551616", currentValue, index));
89 
90     // Negative numbers
91     EXPECT_FALSE(parsePostCode("B-1-2", currentValue, index));
92     EXPECT_FALSE(parsePostCode("B-1--2", currentValue, index));
93 }
94 
95 } // namespace
96 } // namespace redfish
97