1 /** 2 * Copyright © 2019 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "extensions/openpower-pels/manager.hpp" 17 #include "log_manager.hpp" 18 #include "mocks.hpp" 19 #include "pel_utils.hpp" 20 21 #include <fstream> 22 #include <regex> 23 #include <sdbusplus/test/sdbus_mock.hpp> 24 #include <xyz/openbmc_project/Common/error.hpp> 25 26 #include <gtest/gtest.h> 27 28 using namespace openpower::pels; 29 namespace fs = std::filesystem; 30 31 using ::testing::NiceMock; 32 using ::testing::Return; 33 34 class TestLogger 35 { 36 public: 37 void log(const std::string& name, phosphor::logging::Entry::Level level, 38 const EventLogger::ADMap& additionalData) 39 { 40 errName = name; 41 errLevel = level; 42 ad = additionalData; 43 } 44 45 std::string errName; 46 phosphor::logging::Entry::Level errLevel; 47 EventLogger::ADMap ad; 48 }; 49 50 class ManagerTest : public CleanPELFiles 51 { 52 public: 53 ManagerTest() : 54 bus(sdbusplus::get_mocked_new(&sdbusInterface)), 55 logManager(bus, "logging_path") 56 { 57 sd_event_default(&sdEvent); 58 } 59 60 ~ManagerTest() 61 { 62 sd_event_unref(sdEvent); 63 } 64 65 NiceMock<sdbusplus::SdBusMock> sdbusInterface; 66 sdbusplus::bus::bus bus; 67 phosphor::logging::internal::Manager logManager; 68 sd_event* sdEvent; 69 TestLogger logger; 70 }; 71 72 fs::path makeTempDir() 73 { 74 char path[] = "/tmp/tempnameXXXXXX"; 75 std::filesystem::path dir = mkdtemp(path); 76 return dir; 77 } 78 79 std::optional<fs::path> findAnyPELInRepo() 80 { 81 // PELs are named <timestamp>_<ID> 82 std::regex expr{"\\d+_\\d+"}; 83 84 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) 85 { 86 if (std::regex_search(f.path().string(), expr)) 87 { 88 return f.path(); 89 } 90 } 91 return std::nullopt; 92 } 93 94 size_t countPELsInRepo() 95 { 96 size_t count = 0; 97 std::regex expr{"\\d+_\\d+"}; 98 99 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) 100 { 101 if (std::regex_search(f.path().string(), expr)) 102 { 103 count++; 104 } 105 } 106 return count; 107 } 108 109 void deletePELFile(uint32_t id) 110 { 111 char search[20]; 112 113 sprintf(search, "\\d+_%.8X", id); 114 std::regex expr{search}; 115 116 for (auto& f : fs::directory_iterator(getPELRepoPath() / "logs")) 117 { 118 if (std::regex_search(f.path().string(), expr)) 119 { 120 fs::remove(f.path()); 121 break; 122 } 123 } 124 } 125 126 // Test that using the RAWPEL=<file> with the Manager::create() call gets 127 // a PEL saved in the repository. 128 TEST_F(ManagerTest, TestCreateWithPEL) 129 { 130 std::unique_ptr<DataInterfaceBase> dataIface = 131 std::make_unique<MockDataInterface>(); 132 133 openpower::pels::Manager manager{ 134 logManager, std::move(dataIface), 135 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 136 std::placeholders::_2, std::placeholders::_3)}; 137 138 // Create a PEL, write it to a file, and pass that filename into 139 // the create function. 140 auto data = pelDataFactory(TestPELType::pelSimple); 141 142 fs::path pelFilename = makeTempDir() / "rawpel"; 143 std::ofstream pelFile{pelFilename}; 144 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 145 pelFile.close(); 146 147 std::string adItem = "RAWPEL=" + pelFilename.string(); 148 std::vector<std::string> additionalData{adItem}; 149 std::vector<std::string> associations; 150 151 manager.create("error message", 42, 0, 152 phosphor::logging::Entry::Level::Error, additionalData, 153 associations); 154 155 // Find the file in the PEL repository directory 156 auto pelPathInRepo = findAnyPELInRepo(); 157 158 EXPECT_TRUE(pelPathInRepo); 159 160 // Now remove it based on its OpenBMC event log ID 161 manager.erase(42); 162 163 pelPathInRepo = findAnyPELInRepo(); 164 165 EXPECT_FALSE(pelPathInRepo); 166 167 fs::remove_all(pelFilename.parent_path()); 168 } 169 170 TEST_F(ManagerTest, TestCreateWithInvalidPEL) 171 { 172 std::unique_ptr<DataInterfaceBase> dataIface = 173 std::make_unique<MockDataInterface>(); 174 175 openpower::pels::Manager manager{ 176 logManager, std::move(dataIface), 177 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 178 std::placeholders::_2, std::placeholders::_3)}; 179 180 // Create a PEL, write it to a file, and pass that filename into 181 // the create function. 182 auto data = pelDataFactory(TestPELType::pelSimple); 183 184 // Truncate it to make it invalid. 185 data.resize(200); 186 187 fs::path pelFilename = makeTempDir() / "rawpel"; 188 std::ofstream pelFile{pelFilename}; 189 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 190 pelFile.close(); 191 192 std::string adItem = "RAWPEL=" + pelFilename.string(); 193 std::vector<std::string> additionalData{adItem}; 194 std::vector<std::string> associations; 195 196 manager.create("error message", 42, 0, 197 phosphor::logging::Entry::Level::Error, additionalData, 198 associations); 199 200 // Run the event loop to log the bad PEL event 201 sdeventplus::Event e{sdEvent}; 202 e.run(std::chrono::milliseconds(1)); 203 204 PEL invalidPEL{data}; 205 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL"); 206 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error); 207 EXPECT_EQ(std::stoi(logger.ad["PLID"], nullptr, 16), invalidPEL.plid()); 208 EXPECT_EQ(logger.ad["OBMC_LOG_ID"], "42"); 209 EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString()); 210 EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size())); 211 212 // Check that the bad PEL data was saved to a file. 213 auto badPELData = readPELFile(getPELRepoPath() / "badPEL"); 214 EXPECT_EQ(*badPELData, data); 215 216 fs::remove_all(pelFilename.parent_path()); 217 } 218 219 // Test that the message registry can be used to build a PEL. 220 TEST_F(ManagerTest, TestCreateWithMessageRegistry) 221 { 222 const auto registry = R"( 223 { 224 "PELs": 225 [ 226 { 227 "Name": "xyz.openbmc_project.Error.Test", 228 "Subsystem": "power_supply", 229 "ActionFlags": ["service_action", "report"], 230 "SRC": 231 { 232 "ReasonCode": "0x2030" 233 }, 234 "Documentation": 235 { 236 "Description": "A PGOOD Fault", 237 "Message": "PS had a PGOOD Fault" 238 } 239 }, 240 { 241 "Name": "xyz.openbmc_project.Logging.Error.Default", 242 "Subsystem": "bmc_firmware", 243 "SRC": 244 { 245 "ReasonCode": "0x2031" 246 }, 247 "Documentation": 248 { 249 "Description": "The entry used when no match found", 250 "Message": "This is a generic SRC" 251 } 252 } 253 ] 254 } 255 )"; 256 257 auto path = getPELReadOnlyDataPath(); 258 fs::create_directories(path); 259 path /= "message_registry.json"; 260 261 std::ofstream registryFile{path}; 262 registryFile << registry; 263 registryFile.close(); 264 265 std::unique_ptr<DataInterfaceBase> dataIface = 266 std::make_unique<MockDataInterface>(); 267 268 openpower::pels::Manager manager{ 269 logManager, std::move(dataIface), 270 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 271 std::placeholders::_2, std::placeholders::_3)}; 272 273 std::vector<std::string> additionalData{"FOO=BAR"}; 274 std::vector<std::string> associations; 275 276 // Create the event log to create the PEL from. 277 manager.create("xyz.openbmc_project.Error.Test", 33, 0, 278 phosphor::logging::Entry::Level::Error, additionalData, 279 associations); 280 281 // Ensure a PEL was created in the repository 282 auto pelFile = findAnyPELInRepo(); 283 ASSERT_TRUE(pelFile); 284 285 auto data = readPELFile(*pelFile); 286 PEL pel(*data); 287 288 // Spot check it. Other testcases cover the details. 289 EXPECT_TRUE(pel.valid()); 290 EXPECT_EQ(pel.obmcLogID(), 33); 291 EXPECT_EQ(pel.primarySRC().value()->asciiString(), 292 "BD612030 "); 293 // Check if the eventId creation is good 294 EXPECT_EQ(manager.getEventId(pel), 295 "BD612030 00000055 00000010 00000000 00000000 00000000 00000000 " 296 "00000000 00000000"); 297 298 // Remove it 299 manager.erase(33); 300 pelFile = findAnyPELInRepo(); 301 EXPECT_FALSE(pelFile); 302 303 // Create an event log that can't be found in the registry. 304 // In this case, xyz.openbmc_project.Logging.Error.Default will 305 // be used as the key instead to find a registry match. 306 manager.create("xyz.openbmc_project.Error.Foo", 42, 0, 307 phosphor::logging::Entry::Level::Error, additionalData, 308 associations); 309 310 // Ensure a PEL was still created in the repository 311 pelFile = findAnyPELInRepo(); 312 ASSERT_TRUE(pelFile); 313 314 data = readPELFile(*pelFile); 315 PEL newPEL(*data); 316 317 EXPECT_TRUE(newPEL.valid()); 318 EXPECT_EQ(newPEL.obmcLogID(), 42); 319 EXPECT_EQ(newPEL.primarySRC().value()->asciiString(), 320 "BD8D2031 "); 321 322 // Check for both the original AdditionalData item as well as 323 // the ERROR_NAME item that should contain the error message 324 // property that wasn't found. 325 std::string errorName; 326 std::string adItem; 327 328 for (const auto& section : newPEL.optionalSections()) 329 { 330 if (SectionID::userData == static_cast<SectionID>(section->header().id)) 331 { 332 if (UserDataFormat::json == 333 static_cast<UserDataFormat>(section->header().subType)) 334 { 335 auto ud = static_cast<UserData*>(section.get()); 336 337 // Check that there was a UserData section added that 338 // contains debug details about the device. 339 const auto& d = ud->data(); 340 std::string jsonString{d.begin(), d.end()}; 341 auto json = nlohmann::json::parse(jsonString); 342 343 if (json.contains("ERROR_NAME")) 344 { 345 errorName = json["ERROR_NAME"].get<std::string>(); 346 } 347 348 if (json.contains("FOO")) 349 { 350 adItem = json["FOO"].get<std::string>(); 351 } 352 } 353 } 354 if (!errorName.empty()) 355 { 356 break; 357 } 358 } 359 360 EXPECT_EQ(errorName, "xyz.openbmc_project.Error.Foo"); 361 EXPECT_EQ(adItem, "BAR"); 362 } 363 364 TEST_F(ManagerTest, TestDBusMethods) 365 { 366 std::unique_ptr<DataInterfaceBase> dataIface = 367 std::make_unique<MockDataInterface>(); 368 369 Manager manager{logManager, std::move(dataIface), 370 std::bind(std::mem_fn(&TestLogger::log), &logger, 371 std::placeholders::_1, std::placeholders::_2, 372 std::placeholders::_3)}; 373 374 // Create a PEL, write it to a file, and pass that filename into 375 // the create function so there's one in the repo. 376 auto data = pelDataFactory(TestPELType::pelSimple); 377 378 fs::path pelFilename = makeTempDir() / "rawpel"; 379 std::ofstream pelFile{pelFilename}; 380 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 381 pelFile.close(); 382 383 std::string adItem = "RAWPEL=" + pelFilename.string(); 384 std::vector<std::string> additionalData{adItem}; 385 std::vector<std::string> associations; 386 387 manager.create("error message", 42, 0, 388 phosphor::logging::Entry::Level::Error, additionalData, 389 associations); 390 391 // getPELFromOBMCID 392 auto newData = manager.getPELFromOBMCID(42); 393 EXPECT_EQ(newData.size(), data.size()); 394 395 // Read the PEL to get the ID for later 396 PEL pel{newData}; 397 auto id = pel.id(); 398 399 EXPECT_THROW( 400 manager.getPELFromOBMCID(id + 1), 401 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 402 403 // getPEL 404 auto unixfd = manager.getPEL(id); 405 406 // Get the size 407 struct stat s; 408 int r = fstat(unixfd, &s); 409 ASSERT_EQ(r, 0); 410 auto size = s.st_size; 411 412 // Open the FD and check the contents 413 FILE* fp = fdopen(unixfd, "r"); 414 ASSERT_NE(fp, nullptr); 415 416 std::vector<uint8_t> fdData; 417 fdData.resize(size); 418 r = fread(fdData.data(), 1, size, fp); 419 EXPECT_EQ(r, size); 420 421 EXPECT_EQ(newData, fdData); 422 423 fclose(fp); 424 425 // Run the event loop to close the FD 426 sdeventplus::Event e{sdEvent}; 427 e.run(std::chrono::milliseconds(1)); 428 429 EXPECT_THROW( 430 manager.getPEL(id + 1), 431 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 432 433 // hostAck 434 manager.hostAck(id); 435 436 EXPECT_THROW( 437 manager.hostAck(id + 1), 438 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 439 440 // hostReject 441 manager.hostReject(id, Manager::RejectionReason::BadPEL); 442 443 // Run the event loop to log the bad PEL event 444 e.run(std::chrono::milliseconds(1)); 445 446 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.SentBadPELToHost"); 447 EXPECT_EQ(id, std::stoi(logger.ad["BAD_ID"], nullptr, 16)); 448 449 manager.hostReject(id, Manager::RejectionReason::HostFull); 450 451 EXPECT_THROW( 452 manager.hostReject(id + 1, Manager::RejectionReason::BadPEL), 453 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 454 455 fs::remove_all(pelFilename.parent_path()); 456 } 457 458 // An ESEL from the wild 459 const std::string esel{ 460 "00 00 df 00 00 00 00 20 00 04 12 01 6f aa 00 00 " 461 "50 48 00 30 01 00 33 00 00 00 00 07 5c 69 cc 0d 00 00 00 07 5c d5 50 db " 462 "42 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 90 00 00 4e 90 00 00 4e " 463 "55 48 00 18 01 00 09 00 8a 03 40 00 00 00 00 00 ff ff 00 00 00 00 00 00 " 464 "50 53 00 50 01 01 00 00 02 00 00 09 33 2d 00 48 00 00 00 e0 00 00 10 00 " 465 "00 00 00 00 00 20 00 00 00 0c 00 02 00 00 00 fa 00 00 0c e4 00 00 00 12 " 466 "42 43 38 41 33 33 32 44 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 " 467 "20 20 20 20 20 20 20 20 55 44 00 1c 01 06 01 00 02 54 41 4b 00 00 00 06 " 468 "00 00 00 55 00 01 f9 20 00 00 00 00 55 44 00 24 01 06 01 00 01 54 41 4b " 469 "00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00 23 01 00 02 00 05 00 00 " 470 "55 44 00 0c 01 0b 01 00 0f 01 00 00 55 44 00 10 01 04 01 00 0f 9f de 6a " 471 "00 01 00 00 55 44 00 7c 00 0c 01 00 00 13 0c 02 00 fa 0c e4 16 00 01 2c " 472 "0c 1c 16 00 00 fa 0a f0 14 00 00 fa 0b b8 14 00 00 be 09 60 12 00 01 2c " 473 "0d 7a 12 00 00 fa 0c 4e 10 00 00 fa 0c e4 10 00 00 be 0a 8c 16 00 01 2c " 474 "0c 1c 16 00 01 09 09 f6 16 00 00 fa 09 f6 14 00 00 fa 0b b8 14 00 00 fa " 475 "0a f0 14 00 00 be 08 ca 12 00 01 2c 0c e4 12 00 00 fa 0b 54 10 00 00 fa " 476 "0c 2d 10 00 00 be 08 ca 55 44 00 58 01 03 01 00 00 00 00 00 00 05 31 64 " 477 "00 00 00 00 00 05 0d d4 00 00 00 00 40 5f 06 e0 00 00 00 00 40 5d d2 00 " 478 "00 00 00 00 40 57 d3 d0 00 00 00 00 40 58 f6 a0 00 00 00 00 40 54 c9 34 " 479 "00 00 00 00 40 55 9a 10 00 00 00 00 40 4c 0a 80 00 00 00 00 00 00 27 14 " 480 "55 44 01 84 01 01 01 00 48 6f 73 74 62 6f 6f 74 20 42 75 69 6c 64 20 49 " 481 "44 3a 20 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 34 64 66 2d 70 30 61 38 " 482 "37 64 63 34 2f 68 62 69 63 6f 72 65 2e 62 69 6e 00 49 42 4d 2d 77 69 74 " 483 "68 65 72 73 70 6f 6f 6e 2d 4f 50 39 2d 76 32 2e 34 2d 39 2e 32 33 34 0a " 484 "09 6f 70 2d 62 75 69 6c 64 2d 38 32 66 34 63 66 30 0a 09 62 75 69 6c 64 " 485 "72 6f 6f 74 2d 32 30 31 39 2e 30 35 2e 32 2d 31 30 2d 67 38 39 35 39 31 " 486 "31 34 0a 09 73 6b 69 62 6f 6f 74 2d 76 36 2e 35 2d 31 38 2d 67 34 37 30 " 487 "66 66 62 35 66 32 39 64 37 0a 09 68 6f 73 74 62 6f 6f 74 2d 66 65 63 37 " 488 "34 64 66 2d 70 30 61 38 37 64 63 34 0a 09 6f 63 63 2d 65 34 35 39 37 61 " 489 "62 0a 09 6c 69 6e 75 78 2d 35 2e 32 2e 31 37 2d 6f 70 65 6e 70 6f 77 65 " 490 "72 31 2d 70 64 64 63 63 30 33 33 0a 09 70 65 74 69 74 62 6f 6f 74 2d 76 " 491 "31 2e 31 30 2e 34 0a 09 6d 61 63 68 69 6e 65 2d 78 6d 6c 2d 63 36 32 32 " 492 "63 62 35 2d 70 37 65 63 61 62 33 64 0a 09 68 6f 73 74 62 6f 6f 74 2d 62 " 493 "69 6e 61 72 69 65 73 2d 36 36 65 39 61 36 30 0a 09 63 61 70 70 2d 75 63 " 494 "6f 64 65 2d 70 39 2d 64 64 32 2d 76 34 0a 09 73 62 65 2d 36 30 33 33 30 " 495 "65 30 0a 09 68 63 6f 64 65 2d 68 77 30 39 32 31 31 39 61 2e 6f 70 6d 73 " 496 "74 0a 00 00 55 44 00 70 01 04 01 00 0f 9f de 6a 00 05 00 00 07 5f 1d f4 " 497 "30 32 43 59 34 37 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " 498 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " 499 "0b ac 54 02 59 41 31 39 33 34 36 39 37 30 35 38 00 00 00 00 00 00 05 22 " 500 "a1 58 01 8a 00 58 40 20 17 18 4d 2c 00 00 00 fc 01 a1 00 00 55 44 00 14 " 501 "01 08 01 00 00 00 00 01 00 00 00 5a 00 00 00 05 55 44 03 fc 01 15 31 00 " 502 "01 28 00 42 46 41 50 49 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 f4 " 503 "00 00 00 00 00 00 03 f4 00 00 00 0b 00 00 00 00 00 00 00 3d 2c 9b c2 84 " 504 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 09 " 505 "00 00 00 00 00 11 bd 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 " 506 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 00 00 01 2c " 507 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c 00 00 00 64 00 00 00 3d " 508 "2c 9b d1 11 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 " 509 "00 00 00 0a 00 00 00 00 00 13 b5 a0 00 00 00 00 00 01 f8 80 00 00 00 00 " 510 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 " 511 "00 00 00 be 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a 8c 00 00 00 64 " 512 "00 00 00 3d 2c 9b df 98 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 " 513 "00 00 00 00 00 00 00 0b 00 00 00 00 00 15 ae 20 00 00 00 00 00 01 f8 80 " 514 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 " 515 "00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c e4 " 516 "00 00 00 64 00 00 00 3d 2c 9b ea b7 00 00 01 e4 00 48 43 4f fb ed 70 b1 " 517 "00 00 02 01 00 00 00 00 00 00 00 0c 00 00 00 00 00 17 a6 a0 00 00 00 00 " 518 "00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 " 519 "00 00 00 12 00 00 00 00 00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 " 520 "00 00 0c 4e 00 00 00 64 00 00 00 3d 2c 9b f6 27 00 00 01 e4 00 48 43 4f " 521 "fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0d 00 00 00 00 00 19 9f 20 " 522 "00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 " 523 "00 00 00 00 00 00 00 12 00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 " 524 "00 00 00 00 00 00 0d 7a 00 00 00 64 00 00 00 3d 2c 9c 05 75 00 00 01 e4 " 525 "00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0e 00 00 00 00 " 526 "00 1b 97 a0 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 00 00 00 00 " 527 "00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 be 00 00 00 00 " 528 "00 00 07 d0 00 00 00 00 00 00 09 60 00 00 00 64 00 00 00 3d 2c 9c 11 29 " 529 "00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 00 00 00 0f " 530 "00 00 00 00 00 1d 90 20 00 00 00 00 00 01 f8 80 00 00 00 00 00 00 00 01 " 531 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 00 00 00 00 fa " 532 "00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0b b8 00 00 00 64 00 00 00 3d " 533 "2c 9c 1c 45 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 00 00 00 00 " 534 "00 00 00 10 00 00 00 00 00 1f 88 a0 00 00 00 00 00 01 f8 80 00 00 00 00 " 535 "00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 00 00 00 00 " 536 "00 00 00 fa 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0a f0 00 00 00 64 " 537 "00 00 00 3d 2c 9c 2b 14 00 00 01 e4 00 48 43 4f fb ed 70 b1 00 00 02 01 " 538 "00 00 00 00 00 00 00 11 00 00 00 00 00 21 81 20 00 00 00 00 00 01 f8 80 " 539 "00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 16 " 540 "00 00 00 00 00 00 01 2c 00 00 00 00 00 00 07 d0 00 00 00 00 00 00 0c 1c " 541 "00 00 00 64 00 00 00 3d 2d 6d 8f 9e 00 00 01 e4 00 00 43 4f 52 d7 9c 36 " 542 "00 00 04 73 00 00 00 1c 00 00 00 3d 2d 6d 99 ac 00 00 01 e4 00 10 43 4f " 543 "3f f2 02 3d 00 00 05 58 00 00 00 00 02 00 00 01 00 00 00 00 00 00 00 40 " 544 "00 00 00 2c 55 44 00 30 01 15 31 00 01 28 00 42 46 41 50 49 5f 44 42 47 " 545 "00 00 00 00 00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 28 00 00 00 00 " 546 "00 00 00 00 55 44 01 74 01 15 31 00 01 28 00 42 46 41 50 49 5f 49 00 00 " 547 "00 00 00 00 00 00 00 00 00 00 01 6c 00 00 00 00 00 00 01 6c 00 00 00 0b " 548 "00 00 00 00 00 00 00 3c 0d 52 18 5e 00 00 01 e4 00 08 43 4f 46 79 94 13 " 549 "00 00 0a 5b 00 00 00 00 00 00 2c 00 00 00 00 24 00 00 00 3c 0d 6b 26 6c " 550 "00 00 01 e4 00 00 43 4f 4e 9b 18 74 00 00 01 03 00 00 00 1c 00 00 00 3c " 551 "12 b9 2d 13 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c " 552 "00 00 00 3c 13 02 73 53 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 " 553 "00 00 00 1c 00 00 00 3c 13 04 7c 94 00 00 01 e4 00 00 43 4f ea 31 ed d4 " 554 "00 00 05 c4 00 00 00 1c 00 00 00 3c 13 06 ad e1 00 00 01 e4 00 00 43 4f " 555 "ea 31 ed d4 00 00 05 c4 00 00 00 1c 00 00 00 3c 13 07 3f 77 00 00 01 e4 " 556 "00 00 43 4f 5e 4a 55 32 00 00 10 f2 00 00 00 1c 00 00 00 3c 13 07 4e e4 " 557 "00 00 01 e4 00 00 43 4f 5e 4a 55 32 00 00 0d 68 00 00 00 1c 00 00 00 3c " 558 "13 36 79 18 00 00 01 e4 00 00 43 4f ea 31 ed d4 00 00 05 c4 00 00 00 1c " 559 "00 00 00 3d 2c 9c 36 70 00 00 01 e4 00 00 43 4f 23 45 90 97 00 00 02 47 " 560 "00 00 00 1c 00 00 00 3d 2d 6d a3 ed 00 00 01 e4 00 08 43 4f 74 3a 5b 1a " 561 "00 00 04 cc 00 00 00 00 02 00 00 01 00 00 00 24 55 44 00 30 01 15 31 00 " 562 "01 28 00 42 53 43 41 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 28 " 563 "00 00 00 00 00 00 00 28 00 00 00 00 00 00 00 00"}; 564 565 TEST_F(ManagerTest, TestESELToRawData) 566 { 567 auto data = Manager::eselToRawData(esel); 568 569 EXPECT_EQ(data.size(), 2464); 570 571 PEL pel{data}; 572 EXPECT_TRUE(pel.valid()); 573 } 574 575 TEST_F(ManagerTest, TestCreateWithESEL) 576 { 577 std::unique_ptr<DataInterfaceBase> dataIface = 578 std::make_unique<MockDataInterface>(); 579 580 openpower::pels::Manager manager{ 581 logManager, std::move(dataIface), 582 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 583 std::placeholders::_2, std::placeholders::_3)}; 584 585 { 586 std::string adItem = "ESEL=" + esel; 587 std::vector<std::string> additionalData{adItem}; 588 std::vector<std::string> associations; 589 590 manager.create("error message", 37, 0, 591 phosphor::logging::Entry::Level::Error, additionalData, 592 associations); 593 594 auto data = manager.getPELFromOBMCID(37); 595 PEL pel{data}; 596 EXPECT_TRUE(pel.valid()); 597 } 598 599 // Now an invalid one 600 { 601 std::string adItem = "ESEL=" + esel; 602 603 // Crop it 604 adItem.resize(adItem.size() - 300); 605 606 std::vector<std::string> additionalData{adItem}; 607 std::vector<std::string> associations; 608 609 manager.create("error message", 38, 0, 610 phosphor::logging::Entry::Level::Error, additionalData, 611 associations); 612 613 EXPECT_THROW( 614 manager.getPELFromOBMCID(38), 615 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 616 617 // Run the event loop to log the bad PEL event 618 sdeventplus::Event e{sdEvent}; 619 e.run(std::chrono::milliseconds(1)); 620 621 EXPECT_EQ(logger.errName, "org.open_power.Logging.Error.BadHostPEL"); 622 EXPECT_EQ(logger.errLevel, phosphor::logging::Entry::Level::Error); 623 } 624 } 625 626 // Test that PELs will be pruned when necessary 627 TEST_F(ManagerTest, TestPruning) 628 { 629 sdeventplus::Event e{sdEvent}; 630 631 std::unique_ptr<DataInterfaceBase> dataIface = 632 std::make_unique<MockDataInterface>(); 633 634 openpower::pels::Manager manager{ 635 logManager, std::move(dataIface), 636 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 637 std::placeholders::_2, std::placeholders::_3)}; 638 639 // Create 25 1000B (4096B on disk each, which is what is used for pruning) 640 // BMC non-informational PELs in the 100KB repository. After the 24th one, 641 // the repo will be 96% full and a prune should be triggered to remove all 642 // but 7 to get under 30% full. Then when the 25th is added there will be 643 // 8 left. 644 645 auto dir = makeTempDir(); 646 for (int i = 1; i <= 25; i++) 647 { 648 auto data = pelFactory(42, 'O', 0x40, 0x8800, 1000); 649 650 fs::path pelFilename = dir / "rawpel"; 651 std::ofstream pelFile{pelFilename}; 652 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 653 pelFile.close(); 654 655 std::string adItem = "RAWPEL=" + pelFilename.string(); 656 std::vector<std::string> additionalData{adItem}; 657 std::vector<std::string> associations; 658 659 manager.create("error message", 42, 0, 660 phosphor::logging::Entry::Level::Error, additionalData, 661 associations); 662 663 // Simulate the code getting back to the event loop 664 // after each create. 665 e.run(std::chrono::milliseconds(1)); 666 667 if (i < 24) 668 { 669 EXPECT_EQ(countPELsInRepo(), i); 670 } 671 else if (i == 24) 672 { 673 // Prune occured 674 EXPECT_EQ(countPELsInRepo(), 7); 675 } 676 else // i == 25 677 { 678 EXPECT_EQ(countPELsInRepo(), 8); 679 } 680 } 681 682 try 683 { 684 // Make sure the 8 newest ones are still found. 685 for (uint32_t i = 0; i < 8; i++) 686 { 687 manager.getPEL(0x50000012 + i); 688 } 689 } 690 catch (sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument& e) 691 { 692 ADD_FAILURE() << "PELs should have all been found"; 693 } 694 695 fs::remove_all(dir); 696 } 697 698 // Test that manually deleting a PEL file will be recognized by the code. 699 TEST_F(ManagerTest, TestPELManualDelete) 700 { 701 sdeventplus::Event e{sdEvent}; 702 703 std::unique_ptr<DataInterfaceBase> dataIface = 704 std::make_unique<MockDataInterface>(); 705 706 openpower::pels::Manager manager{ 707 logManager, std::move(dataIface), 708 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 709 std::placeholders::_2, std::placeholders::_3)}; 710 711 auto data = pelDataFactory(TestPELType::pelSimple); 712 auto dir = makeTempDir(); 713 fs::path pelFilename = dir / "rawpel"; 714 715 std::string adItem = "RAWPEL=" + pelFilename.string(); 716 std::vector<std::string> additionalData{adItem}; 717 std::vector<std::string> associations; 718 719 // Add 20 PELs, they will get incrementing IDs like 720 // 0x50000001, 0x50000002, etc. 721 for (int i = 1; i <= 20; i++) 722 { 723 std::ofstream pelFile{pelFilename}; 724 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 725 pelFile.close(); 726 727 manager.create("error message", 42, 0, 728 phosphor::logging::Entry::Level::Error, additionalData, 729 associations); 730 731 // Sanity check this ID is really there so we can test 732 // it was deleted later. This will throw an exception if 733 // not present. 734 manager.getPEL(0x50000000 + i); 735 736 // Run an event loop pass where the internal FD is deleted 737 // after the getPEL function call. 738 e.run(std::chrono::milliseconds(1)); 739 } 740 741 EXPECT_EQ(countPELsInRepo(), 20); 742 743 deletePELFile(0x50000001); 744 745 // Run a single event loop pass so the inotify event can run 746 e.run(std::chrono::milliseconds(1)); 747 748 EXPECT_EQ(countPELsInRepo(), 19); 749 750 EXPECT_THROW( 751 manager.getPEL(0x50000001), 752 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 753 754 // Delete a few more, they should all get handled in the same 755 // event loop pass 756 std::vector<uint32_t> toDelete{0x50000002, 0x50000003, 0x50000004, 757 0x50000005, 0x50000006}; 758 std::for_each(toDelete.begin(), toDelete.end(), 759 [](auto i) { deletePELFile(i); }); 760 761 e.run(std::chrono::milliseconds(1)); 762 763 EXPECT_EQ(countPELsInRepo(), 14); 764 765 std::for_each(toDelete.begin(), toDelete.end(), [&manager](const auto i) { 766 EXPECT_THROW( 767 manager.getPEL(i), 768 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 769 }); 770 771 fs::remove_all(dir); 772 } 773 774 // Test that deleting all PELs at once is handled OK. 775 TEST_F(ManagerTest, TestPELManualDeleteAll) 776 { 777 sdeventplus::Event e{sdEvent}; 778 779 std::unique_ptr<DataInterfaceBase> dataIface = 780 std::make_unique<MockDataInterface>(); 781 782 openpower::pels::Manager manager{ 783 logManager, std::move(dataIface), 784 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 785 std::placeholders::_2, std::placeholders::_3)}; 786 787 auto data = pelDataFactory(TestPELType::pelSimple); 788 auto dir = makeTempDir(); 789 fs::path pelFilename = dir / "rawpel"; 790 791 std::string adItem = "RAWPEL=" + pelFilename.string(); 792 std::vector<std::string> additionalData{adItem}; 793 std::vector<std::string> associations; 794 795 // Add 200 PELs, they will get incrementing IDs like 796 // 0x50000001, 0x50000002, etc. 797 for (int i = 1; i <= 200; i++) 798 { 799 std::ofstream pelFile{pelFilename}; 800 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 801 pelFile.close(); 802 803 manager.create("error message", 42, 0, 804 phosphor::logging::Entry::Level::Error, additionalData, 805 associations); 806 807 // Sanity check this ID is really there so we can test 808 // it was deleted later. This will throw an exception if 809 // not present. 810 manager.getPEL(0x50000000 + i); 811 812 // Run an event loop pass where the internal FD is deleted 813 // after the getPEL function call. 814 e.run(std::chrono::milliseconds(1)); 815 } 816 817 // Delete them all at once 818 auto logPath = getPELRepoPath() / "logs"; 819 std::string cmd = "rm " + logPath.string() + "/*"; 820 821 { 822 auto rc = system(cmd.c_str()); 823 EXPECT_EQ(rc, 0); 824 } 825 826 EXPECT_EQ(countPELsInRepo(), 0); 827 828 // It will take 5 event loop passes to process them all 829 for (int i = 0; i < 5; i++) 830 { 831 e.run(std::chrono::milliseconds(1)); 832 } 833 834 for (int i = 1; i <= 200; i++) 835 { 836 EXPECT_THROW( 837 manager.getPEL(0x50000000 + i), 838 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument); 839 } 840 841 fs::remove_all(dir); 842 } 843 844 // Test that fault LEDs are turned on when PELs are created 845 TEST_F(ManagerTest, TestServiceIndicators) 846 { 847 std::unique_ptr<DataInterfaceBase> dataIface = 848 std::make_unique<MockDataInterface>(); 849 850 MockDataInterface* mockIface = 851 reinterpret_cast<MockDataInterface*>(dataIface.get()); 852 853 openpower::pels::Manager manager{ 854 logManager, std::move(dataIface), 855 std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1, 856 std::placeholders::_2, std::placeholders::_3)}; 857 858 // Add a PEL with a callout as if hostboot added it 859 { 860 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42", 0, true)) 861 .WillOnce(Return("/system/chassis/processor")); 862 863 EXPECT_CALL(*mockIface, 864 setFunctional("/system/chassis/processor", false)) 865 .Times(1); 866 867 // This hostboot PEL has a single hardware callout in it. 868 auto data = pelFactory(1, 'B', 0x20, 0xA400, 500); 869 870 fs::path pelFilename = makeTempDir() / "rawpel"; 871 std::ofstream pelFile{pelFilename}; 872 pelFile.write(reinterpret_cast<const char*>(data.data()), data.size()); 873 pelFile.close(); 874 875 std::string adItem = "RAWPEL=" + pelFilename.string(); 876 std::vector<std::string> additionalData{adItem}; 877 std::vector<std::string> associations; 878 879 manager.create("error message", 42, 0, 880 phosphor::logging::Entry::Level::Error, additionalData, 881 associations); 882 883 fs::remove_all(pelFilename.parent_path()); 884 } 885 886 // Add a BMC PEL with a callout that uses the message registry 887 { 888 std::vector<std::string> names{"systemA"}; 889 EXPECT_CALL(*mockIface, getSystemNames) 890 .Times(1) 891 .WillOnce(Return(names)); 892 893 EXPECT_CALL(*mockIface, expandLocationCode("P42-C23", 0)) 894 .WillOnce(Return("U42-P42-C23")); 895 896 // First call to this is when building the Callout section 897 EXPECT_CALL(*mockIface, getInventoryFromLocCode("P42-C23", 0, false)) 898 .WillOnce(Return("/system/chassis/processor")); 899 900 // Second call to this is finding the associated LED group 901 EXPECT_CALL(*mockIface, getInventoryFromLocCode("U42-P42-C23", 0, true)) 902 .WillOnce(Return("/system/chassis/processor")); 903 904 EXPECT_CALL(*mockIface, 905 setFunctional("/system/chassis/processor", false)) 906 .Times(1); 907 908 const auto registry = R"( 909 { 910 "PELs": 911 [ 912 { 913 "Name": "xyz.openbmc_project.Error.Test", 914 "Subsystem": "power_supply", 915 "ActionFlags": ["service_action", "report"], 916 "SRC": 917 { 918 "ReasonCode": "0x2030" 919 }, 920 "Callouts": [ 921 { 922 "CalloutList": [ 923 {"Priority": "high", "LocCode": "P42-C23"} 924 ] 925 } 926 ], 927 "Documentation": 928 { 929 "Description": "Test Error", 930 "Message": "Test Error" 931 } 932 } 933 ] 934 })"; 935 936 auto path = getPELReadOnlyDataPath(); 937 fs::create_directories(path); 938 path /= "message_registry.json"; 939 940 std::ofstream registryFile{path}; 941 registryFile << registry; 942 registryFile.close(); 943 944 std::vector<std::string> additionalData; 945 std::vector<std::string> associations; 946 947 manager.create("xyz.openbmc_project.Error.Test", 42, 0, 948 phosphor::logging::Entry::Level::Error, additionalData, 949 associations); 950 } 951 } 952