kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 1 | #include "rde/external_storer_file.hpp" |
| 2 | |
kasunath | 3d0cd55 | 2022-08-25 20:22:58 -0700 | [diff] [blame] | 3 | #include <boost/asio/io_context.hpp> |
kasunath | a3b64fb | 2022-06-15 18:47:18 -0700 | [diff] [blame] | 4 | |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
| 7 | #include <gmock/gmock-matchers.h> |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace bios_bmc_smm_error_logger |
| 12 | { |
| 13 | namespace rde |
| 14 | { |
| 15 | |
| 16 | using ::testing::_; |
| 17 | using ::testing::DoAll; |
| 18 | using ::testing::Return; |
| 19 | using ::testing::SaveArg; |
| 20 | |
| 21 | class 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 | |
| 31 | class ExternalStorerFileTest : public ::testing::Test |
| 32 | { |
| 33 | public: |
| 34 | ExternalStorerFileTest() : |
kasunath | 3d0cd55 | 2022-08-25 20:22:58 -0700 | [diff] [blame] | 35 | conn(std::make_shared<sdbusplus::asio::connection>(io)), |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 36 | mockFileWriter(std::make_unique<MockFileWriter>()) |
| 37 | { |
| 38 | mockFileWriterPtr = dynamic_cast<MockFileWriter*>(mockFileWriter.get()); |
| 39 | exStorer = std::make_unique<ExternalStorerFileInterface>( |
kasunath | 3d0cd55 | 2022-08-25 20:22:58 -0700 | [diff] [blame] | 40 | conn, rootPath, std::move(mockFileWriter)); |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | protected: |
kasunath | 3d0cd55 | 2022-08-25 20:22:58 -0700 | [diff] [blame] | 44 | boost::asio::io_context io; |
| 45 | std::shared_ptr<sdbusplus::asio::connection> conn; |
| 46 | |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 47 | std::unique_ptr<FileHandlerInterface> mockFileWriter; |
| 48 | std::unique_ptr<ExternalStorerFileInterface> exStorer; |
| 49 | MockFileWriter* mockFileWriterPtr; |
| 50 | const std::string rootPath = "/some/path"; |
| 51 | }; |
| 52 | |
| 53 | TEST_F(ExternalStorerFileTest, InvalidJsonTest) |
| 54 | { |
| 55 | // Try an invalid JSON. |
| 56 | std::string jsonStr = "Invalid JSON"; |
| 57 | EXPECT_THAT(exStorer->publishJson(jsonStr), false); |
| 58 | } |
| 59 | |
| 60 | TEST_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 | |
| 72 | TEST_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 | |
| 83 | TEST_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 | |
| 95 | TEST_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 | |
| 117 | TEST_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 | |
| 128 | TEST_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 | )"; |
kasunath | a3b64fb | 2022-06-15 18:47:18 -0700 | [diff] [blame] | 156 | |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 157 | nlohmann::json logEntryOut; |
| 158 | EXPECT_CALL(*mockFileWriterPtr, createFile(_, _)) |
| 159 | .WillOnce(DoAll(SaveArg<1>(&logEntryOut), Return(true))); |
kasunath | a3b64fb | 2022-06-15 18:47:18 -0700 | [diff] [blame] | 160 | |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 161 | EXPECT_THAT(exStorer->publishJson(jsonLogEntry), true); |
| 162 | EXPECT_NE(logEntryOut["Id"], nullptr); |
| 163 | EXPECT_EQ(logEntryOut["@odata.id"], nullptr); |
kasunath | 37bc0df | 2022-06-07 12:40:26 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST_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 | |
| 178 | TEST_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 |