Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 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 | */ |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 16 | #include "extensions/openpower-pels/src.hpp" |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 17 | #include "mocks.hpp" |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 18 | #include "pel_utils.hpp" |
| 19 | |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 20 | #include <fstream> |
| 21 | |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | using namespace openpower::pels; |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 25 | using ::testing::_; |
| 26 | using ::testing::InvokeWithoutArgs; |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 27 | using ::testing::NiceMock; |
| 28 | using ::testing::Return; |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 29 | using ::testing::SetArgReferee; |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 30 | namespace fs = std::filesystem; |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 31 | |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 32 | const auto testRegistry = R"( |
| 33 | { |
| 34 | "PELs": |
| 35 | [ |
| 36 | { |
| 37 | "Name": "xyz.openbmc_project.Error.Test", |
| 38 | "Subsystem": "bmc_firmware", |
| 39 | "SRC": |
| 40 | { |
| 41 | "ReasonCode": "0xABCD", |
| 42 | "Words6To9": |
| 43 | { |
| 44 | "6": |
| 45 | { |
| 46 | "Description": "Component ID", |
| 47 | "AdditionalDataPropSource": "COMPID" |
| 48 | }, |
| 49 | "7": |
| 50 | { |
| 51 | "Description": "Failure count", |
| 52 | "AdditionalDataPropSource": "FREQUENCY" |
| 53 | }, |
| 54 | "8": |
| 55 | { |
| 56 | "Description": "Time period", |
| 57 | "AdditionalDataPropSource": "DURATION" |
| 58 | }, |
| 59 | "9": |
| 60 | { |
| 61 | "Description": "Error code", |
| 62 | "AdditionalDataPropSource": "ERRORCODE" |
| 63 | } |
| 64 | } |
| 65 | }, |
| 66 | "Documentation": |
| 67 | { |
| 68 | "Description": "A Component Fault", |
| 69 | "Message": "Comp %1 failed %2 times over %3 secs with ErrorCode %4", |
| 70 | "MessageArgSources": |
| 71 | [ |
| 72 | "SRCWord6", "SRCWord7", "SRCWord8", "SRCWord9" |
| 73 | ] |
| 74 | } |
| 75 | } |
| 76 | ] |
| 77 | } |
| 78 | )"; |
| 79 | |
| 80 | class SRCTest : public ::testing::Test |
| 81 | { |
| 82 | protected: |
| 83 | static void SetUpTestCase() |
| 84 | { |
| 85 | char path[] = "/tmp/srctestXXXXXX"; |
| 86 | regDir = mkdtemp(path); |
| 87 | } |
| 88 | |
| 89 | static void TearDownTestCase() |
| 90 | { |
| 91 | fs::remove_all(regDir); |
| 92 | } |
| 93 | |
| 94 | static std::string writeData(const char* data) |
| 95 | { |
| 96 | fs::path path = regDir / "registry.json"; |
| 97 | std::ofstream stream{path}; |
| 98 | stream << data; |
| 99 | return path; |
| 100 | } |
| 101 | |
| 102 | static fs::path regDir; |
| 103 | }; |
| 104 | |
| 105 | fs::path SRCTest::regDir{}; |
| 106 | |
| 107 | TEST_F(SRCTest, UnflattenFlattenTestNoCallouts) |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 108 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 109 | auto data = pelDataFactory(TestPELType::primarySRCSection); |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 110 | |
| 111 | Stream stream{data}; |
| 112 | SRC src{stream}; |
| 113 | |
| 114 | EXPECT_TRUE(src.valid()); |
| 115 | |
| 116 | EXPECT_EQ(src.header().id, 0x5053); |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 117 | EXPECT_EQ(src.header().size, 0x50); |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 118 | EXPECT_EQ(src.header().version, 0x01); |
| 119 | EXPECT_EQ(src.header().subType, 0x01); |
| 120 | EXPECT_EQ(src.header().componentID, 0x0202); |
| 121 | |
| 122 | EXPECT_EQ(src.version(), 0x02); |
| 123 | EXPECT_EQ(src.flags(), 0x00); |
| 124 | EXPECT_EQ(src.hexWordCount(), 9); |
| 125 | EXPECT_EQ(src.size(), 0x48); |
| 126 | |
| 127 | const auto& hexwords = src.hexwordData(); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 128 | EXPECT_EQ(0x02020255, hexwords[0]); |
| 129 | EXPECT_EQ(0x03030310, hexwords[1]); |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 130 | EXPECT_EQ(0x04040404, hexwords[2]); |
| 131 | EXPECT_EQ(0x05050505, hexwords[3]); |
| 132 | EXPECT_EQ(0x06060606, hexwords[4]); |
| 133 | EXPECT_EQ(0x07070707, hexwords[5]); |
| 134 | EXPECT_EQ(0x08080808, hexwords[6]); |
| 135 | EXPECT_EQ(0x09090909, hexwords[7]); |
| 136 | |
| 137 | EXPECT_EQ(src.asciiString(), "BD8D5678 "); |
| 138 | EXPECT_FALSE(src.callouts()); |
| 139 | |
| 140 | // Flatten |
| 141 | std::vector<uint8_t> newData; |
| 142 | Stream newStream{newData}; |
| 143 | |
| 144 | src.flatten(newStream); |
| 145 | EXPECT_EQ(data, newData); |
| 146 | } |
| 147 | |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 148 | TEST_F(SRCTest, UnflattenFlattenTest2Callouts) |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 149 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 150 | auto data = pelDataFactory(TestPELType::primarySRCSection2Callouts); |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 151 | |
| 152 | Stream stream{data}; |
| 153 | SRC src{stream}; |
| 154 | |
| 155 | EXPECT_TRUE(src.valid()); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 156 | EXPECT_EQ(src.flags(), 0x01); // Additional sections within the SRC. |
Matt Spinler | f9bae18 | 2019-10-09 13:37:38 -0500 | [diff] [blame] | 157 | |
| 158 | // Spot check the SRC fields, but they're the same as above |
| 159 | EXPECT_EQ(src.asciiString(), "BD8D5678 "); |
| 160 | |
| 161 | // There should be 2 callouts |
| 162 | const auto& calloutsSection = src.callouts(); |
| 163 | ASSERT_TRUE(calloutsSection); |
| 164 | const auto& callouts = calloutsSection->callouts(); |
| 165 | EXPECT_EQ(callouts.size(), 2); |
| 166 | |
| 167 | // spot check that each callout has the right substructures |
| 168 | EXPECT_TRUE(callouts.front()->fruIdentity()); |
| 169 | EXPECT_FALSE(callouts.front()->pceIdentity()); |
| 170 | EXPECT_FALSE(callouts.front()->mru()); |
| 171 | |
| 172 | EXPECT_TRUE(callouts.back()->fruIdentity()); |
| 173 | EXPECT_TRUE(callouts.back()->pceIdentity()); |
| 174 | EXPECT_TRUE(callouts.back()->mru()); |
| 175 | |
| 176 | // Flatten |
| 177 | std::vector<uint8_t> newData; |
| 178 | Stream newStream{newData}; |
| 179 | |
| 180 | src.flatten(newStream); |
| 181 | EXPECT_EQ(data, newData); |
| 182 | } |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 183 | |
| 184 | // Create an SRC from the message registry |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 185 | TEST_F(SRCTest, CreateTestNoCallouts) |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 186 | { |
| 187 | message::Entry entry; |
| 188 | entry.src.type = 0xBD; |
| 189 | entry.src.reasonCode = 0xABCD; |
| 190 | entry.subsystem = 0x42; |
| 191 | entry.src.powerFault = true; |
| 192 | entry.src.hexwordADFields = {{5, "TEST1"}, // Not a user defined word |
| 193 | {6, "TEST1"}, |
| 194 | {7, "TEST2"}, |
| 195 | {8, "TEST3"}, |
| 196 | {9, "TEST4"}}; |
| 197 | |
| 198 | // Values for the SRC words pointed to above |
| 199 | std::vector<std::string> adData{"TEST1=0x12345678", "TEST2=12345678", |
| 200 | "TEST3=0XDEF", "TEST4=Z"}; |
| 201 | AdditionalData ad{adData}; |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 202 | NiceMock<MockDataInterface> dataIface; |
| 203 | |
| 204 | EXPECT_CALL(dataIface, getMotherboardCCIN).WillOnce(Return("ABCD")); |
| 205 | |
| 206 | SRC src{entry, ad, dataIface}; |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 207 | |
| 208 | EXPECT_TRUE(src.valid()); |
| 209 | EXPECT_TRUE(src.isPowerFaultEvent()); |
| 210 | EXPECT_EQ(src.size(), baseSRCSize); |
| 211 | |
| 212 | const auto& hexwords = src.hexwordData(); |
| 213 | |
| 214 | // The spec always refers to SRC words 2 - 9, and as the hexwordData() |
| 215 | // array index starts at 0 use the math in the [] below to make it easier |
| 216 | // to tell what is being accessed. |
| 217 | EXPECT_EQ(hexwords[2 - 2] & 0xF0000000, 0); // Partition dump status |
| 218 | EXPECT_EQ(hexwords[2 - 2] & 0x00F00000, 0); // Partition boot type |
| 219 | EXPECT_EQ(hexwords[2 - 2] & 0x000000FF, 0x55); // SRC format |
| 220 | EXPECT_EQ(hexwords[3 - 2] & 0x000000FF, 0x10); // BMC position |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 221 | EXPECT_EQ(hexwords[3 - 2] & 0xFFFF0000, 0xABCD0000); // Motherboard CCIN |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 222 | |
| 223 | // Validate more fields here as the code starts filling them in. |
| 224 | |
| 225 | // Ensure hex word 5 wasn't allowed to be set to TEST1's contents |
| 226 | EXPECT_EQ(hexwords[5 - 2], 0); |
| 227 | |
| 228 | // The user defined hex word fields specifed in the additional data. |
| 229 | EXPECT_EQ(hexwords[6 - 2], 0x12345678); // TEST1 |
| 230 | EXPECT_EQ(hexwords[7 - 2], 12345678); // TEST2 |
| 231 | EXPECT_EQ(hexwords[8 - 2], 0xdef); // TEST3 |
| 232 | EXPECT_EQ(hexwords[9 - 2], 0); // TEST4, but can't convert a 'Z' |
| 233 | |
| 234 | EXPECT_EQ(src.asciiString(), "BD42ABCD "); |
| 235 | |
| 236 | // No callouts |
| 237 | EXPECT_FALSE(src.callouts()); |
| 238 | |
| 239 | // May as well spot check the flatten/unflatten |
| 240 | std::vector<uint8_t> data; |
| 241 | Stream stream{data}; |
| 242 | src.flatten(stream); |
| 243 | |
| 244 | stream.offset(0); |
| 245 | SRC newSRC{stream}; |
| 246 | |
| 247 | EXPECT_TRUE(newSRC.valid()); |
| 248 | EXPECT_EQ(newSRC.isPowerFaultEvent(), src.isPowerFaultEvent()); |
| 249 | EXPECT_EQ(newSRC.asciiString(), src.asciiString()); |
| 250 | EXPECT_FALSE(newSRC.callouts()); |
| 251 | } |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 252 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 253 | // Test when the CCIN string isn't a 4 character number |
| 254 | TEST_F(SRCTest, BadCCINTest) |
| 255 | { |
| 256 | message::Entry entry; |
| 257 | entry.src.type = 0xBD; |
| 258 | entry.src.reasonCode = 0xABCD; |
| 259 | entry.subsystem = 0x42; |
| 260 | entry.src.powerFault = false; |
| 261 | |
| 262 | std::vector<std::string> adData{}; |
| 263 | AdditionalData ad{adData}; |
| 264 | NiceMock<MockDataInterface> dataIface; |
| 265 | |
| 266 | // First it isn't a number, then it is too long, |
| 267 | // then it is empty. |
| 268 | EXPECT_CALL(dataIface, getMotherboardCCIN) |
| 269 | .WillOnce(Return("X")) |
| 270 | .WillOnce(Return("12345")) |
| 271 | .WillOnce(Return("")); |
| 272 | |
| 273 | // The CCIN in the first half should still be 0 each time. |
| 274 | { |
| 275 | SRC src{entry, ad, dataIface}; |
| 276 | EXPECT_TRUE(src.valid()); |
| 277 | const auto& hexwords = src.hexwordData(); |
| 278 | EXPECT_EQ(hexwords[3 - 2] & 0xFFFF0000, 0x00000000); |
| 279 | } |
| 280 | |
| 281 | { |
| 282 | SRC src{entry, ad, dataIface}; |
| 283 | EXPECT_TRUE(src.valid()); |
| 284 | const auto& hexwords = src.hexwordData(); |
| 285 | EXPECT_EQ(hexwords[3 - 2] & 0xFFFF0000, 0x00000000); |
| 286 | } |
| 287 | |
| 288 | { |
| 289 | SRC src{entry, ad, dataIface}; |
| 290 | EXPECT_TRUE(src.valid()); |
| 291 | const auto& hexwords = src.hexwordData(); |
| 292 | EXPECT_EQ(hexwords[3 - 2] & 0xFFFF0000, 0x00000000); |
| 293 | } |
| 294 | } |
| 295 | |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 296 | // Test the getErrorDetails function |
| 297 | TEST_F(SRCTest, MessageSubstitutionTest) |
| 298 | { |
| 299 | auto path = SRCTest::writeData(testRegistry); |
| 300 | message::Registry registry{path}; |
| 301 | auto entry = registry.lookup("0xABCD", message::LookupType::reasonCode); |
| 302 | |
| 303 | std::vector<std::string> adData{"COMPID=0x1", "FREQUENCY=0x4", |
| 304 | "DURATION=30", "ERRORCODE=0x01ABCDEF"}; |
| 305 | AdditionalData ad{adData}; |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 306 | NiceMock<MockDataInterface> dataIface; |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 307 | |
Matt Spinler | 075e5ba | 2020-02-21 15:46:00 -0600 | [diff] [blame] | 308 | SRC src{*entry, ad, dataIface}; |
Harisuddin Mohamed Isa | 6fd0c1e | 2020-02-06 14:25:57 +0800 | [diff] [blame] | 309 | EXPECT_TRUE(src.valid()); |
| 310 | |
| 311 | auto errorDetails = src.getErrorDetails(registry, DetailLevel::message); |
| 312 | ASSERT_TRUE(errorDetails); |
| 313 | EXPECT_EQ( |
| 314 | errorDetails.value(), |
| 315 | "Comp 0x1 failed 0x4 times over 0x1E secs with ErrorCode 0x1ABCDEF"); |
| 316 | } |
Matt Spinler | ed04685 | 2020-03-13 13:58:15 -0500 | [diff] [blame] | 317 | |
| 318 | // Test that an inventory path callout string is |
| 319 | // converted into the appropriate FRU callout. |
| 320 | TEST_F(SRCTest, InventoryCalloutTest) |
| 321 | { |
| 322 | message::Entry entry; |
| 323 | entry.src.type = 0xBD; |
| 324 | entry.src.reasonCode = 0xABCD; |
| 325 | entry.subsystem = 0x42; |
| 326 | entry.src.powerFault = false; |
| 327 | |
| 328 | std::vector<std::string> adData{"CALLOUT_INVENTORY_PATH=motherboard"}; |
| 329 | AdditionalData ad{adData}; |
| 330 | NiceMock<MockDataInterface> dataIface; |
| 331 | |
| 332 | EXPECT_CALL(dataIface, getHWCalloutFields("motherboard", _, _, _, _)) |
| 333 | .Times(1) |
| 334 | .WillOnce(DoAll(SetArgReferee<1>("UTMS-P1"), |
| 335 | SetArgReferee<2>("1234567"), SetArgReferee<3>("CCCC"), |
| 336 | SetArgReferee<4>("123456789ABC"))); |
| 337 | |
| 338 | SRC src{entry, ad, dataIface}; |
| 339 | EXPECT_TRUE(src.valid()); |
| 340 | |
| 341 | ASSERT_TRUE(src.callouts()); |
| 342 | |
| 343 | EXPECT_EQ(src.callouts()->callouts().size(), 1); |
| 344 | |
| 345 | auto& callout = src.callouts()->callouts().front(); |
| 346 | |
| 347 | EXPECT_EQ(callout->locationCode(), "UTMS-P1"); |
| 348 | |
| 349 | auto& fru = callout->fruIdentity(); |
| 350 | |
| 351 | EXPECT_EQ(fru->getPN().value(), "1234567"); |
| 352 | EXPECT_EQ(fru->getCCIN().value(), "CCCC"); |
| 353 | EXPECT_EQ(fru->getSN().value(), "123456789ABC"); |
| 354 | |
| 355 | // flatten and unflatten |
| 356 | std::vector<uint8_t> data; |
| 357 | Stream stream{data}; |
| 358 | src.flatten(stream); |
| 359 | |
| 360 | stream.offset(0); |
| 361 | SRC newSRC{stream}; |
| 362 | EXPECT_TRUE(newSRC.valid()); |
| 363 | ASSERT_TRUE(src.callouts()); |
| 364 | EXPECT_EQ(src.callouts()->callouts().size(), 1); |
| 365 | } |
| 366 | |
| 367 | // Test that when the FRU fields can't be obtained that |
| 368 | // a procedure callout is used. |
| 369 | TEST_F(SRCTest, InventoryCalloutNoVPDTest) |
| 370 | { |
| 371 | message::Entry entry; |
| 372 | entry.src.type = 0xBD; |
| 373 | entry.src.reasonCode = 0xABCD; |
| 374 | entry.subsystem = 0x42; |
| 375 | entry.src.powerFault = false; |
| 376 | |
| 377 | std::vector<std::string> adData{"CALLOUT_INVENTORY_PATH=motherboard"}; |
| 378 | AdditionalData ad{adData}; |
| 379 | NiceMock<MockDataInterface> dataIface; |
| 380 | |
| 381 | auto func = []() { throw sdbusplus::exception::SdBusError(5, "Error"); }; |
| 382 | |
| 383 | EXPECT_CALL(dataIface, getHWCalloutFields("motherboard", _, _, _, _)) |
| 384 | .Times(1) |
| 385 | .WillOnce(InvokeWithoutArgs(func)); |
| 386 | |
| 387 | SRC src{entry, ad, dataIface}; |
| 388 | EXPECT_TRUE(src.valid()); |
| 389 | |
| 390 | ASSERT_TRUE(src.callouts()); |
| 391 | |
| 392 | EXPECT_EQ(src.callouts()->callouts().size(), 1); |
| 393 | |
| 394 | auto& callout = src.callouts()->callouts().front(); |
| 395 | |
| 396 | auto& fru = callout->fruIdentity(); |
| 397 | |
| 398 | EXPECT_EQ(fru->getMaintProc().value(), "BMCSP01"); |
| 399 | EXPECT_FALSE(fru->getPN()); |
| 400 | EXPECT_FALSE(fru->getSN()); |
| 401 | EXPECT_FALSE(fru->getCCIN()); |
| 402 | // |
| 403 | // flatten and unflatten |
| 404 | std::vector<uint8_t> data; |
| 405 | Stream stream{data}; |
| 406 | src.flatten(stream); |
| 407 | |
| 408 | stream.offset(0); |
| 409 | SRC newSRC{stream}; |
| 410 | EXPECT_TRUE(newSRC.valid()); |
| 411 | ASSERT_TRUE(src.callouts()); |
| 412 | EXPECT_EQ(src.callouts()->callouts().size(), 1); |
| 413 | } |