blob: 3cba35dd18e471a218a000075e3c14df0cb9e30e [file] [log] [blame]
kasunath37bc0df2022-06-07 12:40:26 -07001#include "rde/external_storer_file.hpp"
2
kasunath3d0cd552022-08-25 20:22:58 -07003#include <boost/asio/io_context.hpp>
kasunatha3b64fb2022-06-15 18:47:18 -07004
kasunath37bc0df2022-06-07 12:40:26 -07005#include <string_view>
6
7#include <gmock/gmock-matchers.h>
8#include <gmock/gmock.h>
9#include <gtest/gtest.h>
10
11namespace bios_bmc_smm_error_logger
12{
13namespace rde
14{
15
16using ::testing::_;
17using ::testing::DoAll;
18using ::testing::Return;
19using ::testing::SaveArg;
20
21class MockFileWriter : public FileHandlerInterface
22{
23 public:
24 MOCK_METHOD(bool, createFolder, (const std::string& path),
25 (const, override));
26 MOCK_METHOD(bool, createFile,
27 (const std::string& path, const nlohmann::json& jsonPdr),
28 (const, override));
29};
30
31class ExternalStorerFileTest : public ::testing::Test
32{
33 public:
34 ExternalStorerFileTest() :
kasunath3d0cd552022-08-25 20:22:58 -070035 conn(std::make_shared<sdbusplus::asio::connection>(io)),
kasunath37bc0df2022-06-07 12:40:26 -070036 mockFileWriter(std::make_unique<MockFileWriter>())
37 {
38 mockFileWriterPtr = dynamic_cast<MockFileWriter*>(mockFileWriter.get());
39 exStorer = std::make_unique<ExternalStorerFileInterface>(
kasunath3d0cd552022-08-25 20:22:58 -070040 conn, rootPath, std::move(mockFileWriter));
kasunath37bc0df2022-06-07 12:40:26 -070041 }
42
43 protected:
kasunath3d0cd552022-08-25 20:22:58 -070044 boost::asio::io_context io;
45 std::shared_ptr<sdbusplus::asio::connection> conn;
46
kasunath37bc0df2022-06-07 12:40:26 -070047 std::unique_ptr<FileHandlerInterface> mockFileWriter;
48 std::unique_ptr<ExternalStorerFileInterface> exStorer;
49 MockFileWriter* mockFileWriterPtr;
50 const std::string rootPath = "/some/path";
51};
52
53TEST_F(ExternalStorerFileTest, InvalidJsonTest)
54{
55 // Try an invalid JSON.
56 std::string jsonStr = "Invalid JSON";
57 EXPECT_THAT(exStorer->publishJson(jsonStr), false);
58}
59
60TEST_F(ExternalStorerFileTest, NoOdataTypeFailureTest)
61{
62 // Try a JSON without @odata.type.
63 std::string jsonStr = R"(
64 {
65 "@odata.id": "/redfish/v1/Systems/system/Memory/dimm0/MemoryMetrics",
66 "Id":"Metrics"
67 }
68 )";
69 EXPECT_THAT(exStorer->publishJson(jsonStr), false);
70}
71
72TEST_F(ExternalStorerFileTest, LogServiceNoOdataIdTest)
73{
74 // Try a LogService without @odata.id.
75 std::string jsonStr = R"(
76 {
77 "@odata.type": "#LogService.v1_1_0.LogService","Id":"6F7-C1A7C"
78 }
79 )";
80 EXPECT_THAT(exStorer->publishJson(jsonStr), false);
81}
82
83TEST_F(ExternalStorerFileTest, LogServiceNoIdTest)
84{
85 // Try a LogService without Id.
86 std::string jsonStr = R"(
87 {
88 "@odata.id": "/redfish/v1/Systems/system/LogServices/6F7-C1A7C",
89 "@odata.type": "#LogService.v1_1_0.LogService"
90 }
91 )";
92 EXPECT_THAT(exStorer->publishJson(jsonStr), false);
93}
94
95TEST_F(ExternalStorerFileTest, LogServiceTest)
96{
97 // A valid LogService test.
98 std::string jsonStr = R"(
99 {
100 "@odata.id": "/redfish/v1/Systems/system/LogServices/6F7-C1A7C",
101 "@odata.type": "#LogService.v1_1_0.LogService","Id":"6F7-C1A7C"
102 }
103 )";
104 std::string exServiceFolder =
105 "/some/path/redfish/v1/Systems/system/LogServices/6F7-C1A7C";
106 std::string exEntriesFolder =
107 "/some/path/redfish/v1/Systems/system/LogServices/6F7-C1A7C/Entries";
108 nlohmann::json exEntriesJson = "{}"_json;
109 nlohmann::json exServiceJson = nlohmann::json::parse(jsonStr);
110 EXPECT_CALL(*mockFileWriterPtr, createFile(exServiceFolder, exServiceJson))
111 .WillOnce(Return(true));
112 EXPECT_CALL(*mockFileWriterPtr, createFile(exEntriesFolder, exEntriesJson))
113 .WillOnce(Return(true));
114 EXPECT_THAT(exStorer->publishJson(jsonStr), true);
115}
116
117TEST_F(ExternalStorerFileTest, LogEntryWithoutLogServiceTest)
118{
119 // Try a LogEntry without sending a LogService first.
120 std::string jsonLogEntry = R"(
121 {
122 "@odata.type": "#LogEntry.v1_13_0.LogEntry"
123 }
124 )";
125 EXPECT_THAT(exStorer->publishJson(jsonLogEntry), false);
126}
127
128TEST_F(ExternalStorerFileTest, LogEntryTest)
129{
130 // Before sending a LogEntry, first we need to push a LogService.
131 std::string jsonLogSerivce = R"(
132 {
133 "@odata.id": "/redfish/v1/Systems/system/LogServices/6F7-C1A7C",
134 "@odata.type": "#LogService.v1_1_0.LogService","Id":"6F7-C1A7C"
135 }
136 )";
137 std::string exServiceFolder =
138 "/some/path/redfish/v1/Systems/system/LogServices/6F7-C1A7C";
139 std::string exEntriesFolder =
140 "/some/path/redfish/v1/Systems/system/LogServices/6F7-C1A7C/Entries";
141 nlohmann::json exEntriesJson = "{}"_json;
142 nlohmann::json exServiceJson = nlohmann::json::parse(jsonLogSerivce);
143 EXPECT_CALL(*mockFileWriterPtr, createFile(exServiceFolder, exServiceJson))
144 .WillOnce(Return(true));
145 EXPECT_CALL(*mockFileWriterPtr, createFile(exEntriesFolder, exEntriesJson))
146 .WillOnce(Return(true));
147 EXPECT_THAT(exStorer->publishJson(jsonLogSerivce), true);
148
149 // Now send a LogEntry
150 std::string jsonLogEntry = R"(
151 {
152 "@odata.id": "/some/odata/id",
153 "@odata.type": "#LogEntry.v1_13_0.LogEntry"
154 }
155 )";
kasunatha3b64fb2022-06-15 18:47:18 -0700156
kasunath37bc0df2022-06-07 12:40:26 -0700157 nlohmann::json logEntryOut;
158 EXPECT_CALL(*mockFileWriterPtr, createFile(_, _))
159 .WillOnce(DoAll(SaveArg<1>(&logEntryOut), Return(true)));
kasunatha3b64fb2022-06-15 18:47:18 -0700160
kasunath37bc0df2022-06-07 12:40:26 -0700161 EXPECT_THAT(exStorer->publishJson(jsonLogEntry), true);
162 EXPECT_NE(logEntryOut["Id"], nullptr);
163 EXPECT_EQ(logEntryOut["@odata.id"], nullptr);
kasunath37bc0df2022-06-07 12:40:26 -0700164}
165
166TEST_F(ExternalStorerFileTest, OtherSchemaNoOdataIdTest)
167{
168 // Try a another PDRs without @odata.id.
169 std::string jsonStr = R"(
170 {
171 "@odata.type": "#MemoryMetrics.v1_4_1.MemoryMetrics",
172 "Id":"Metrics"
173 }
174 )";
175 EXPECT_THAT(exStorer->publishJson(jsonStr), false);
176}
177
178TEST_F(ExternalStorerFileTest, OtherSchemaTypeTest)
179{
180 // A valid MemoryMetrics PDR.
181 std::string jsonStr = R"(
182 {
183 "@odata.id": "/redfish/v1/Systems/system/Memory/dimm0/MemoryMetrics",
184 "@odata.type": "#MemoryMetrics.v1_4_1.MemoryMetrics",
185 "Id": "Metrics"
186 }
187 )";
188 std::string exFolder =
189 "/some/path/redfish/v1/Systems/system/Memory/dimm0/MemoryMetrics";
190 nlohmann::json exJson = nlohmann::json::parse(jsonStr);
191 EXPECT_CALL(*mockFileWriterPtr, createFile(exFolder, exJson))
192 .WillOnce(Return(true));
193 EXPECT_THAT(exStorer->publishJson(jsonStr), true);
194}
195
196} // namespace rde
197} // namespace bios_bmc_smm_error_logger