blob: 93420502f44ef7d2cb8ccead74f4b5c14f9be1bf [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
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 Spinlerf9bae182019-10-09 13:37:38 -050016#include "extensions/openpower-pels/src.hpp"
Matt Spinler075e5ba2020-02-21 15:46:00 -060017#include "mocks.hpp"
Matt Spinlerf9bae182019-10-09 13:37:38 -050018#include "pel_utils.hpp"
19
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +080020#include <fstream>
21
Matt Spinlerf9bae182019-10-09 13:37:38 -050022#include <gtest/gtest.h>
23
24using namespace openpower::pels;
Matt Spinlered046852020-03-13 13:58:15 -050025using ::testing::_;
26using ::testing::InvokeWithoutArgs;
Matt Spinler075e5ba2020-02-21 15:46:00 -060027using ::testing::NiceMock;
28using ::testing::Return;
Matt Spinlered046852020-03-13 13:58:15 -050029using ::testing::SetArgReferee;
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +080030namespace fs = std::filesystem;
Matt Spinlerf9bae182019-10-09 13:37:38 -050031
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +080032const 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
80class 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
105fs::path SRCTest::regDir{};
106
107TEST_F(SRCTest, UnflattenFlattenTestNoCallouts)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500108{
Matt Spinler42828bd2019-10-11 10:39:30 -0500109 auto data = pelDataFactory(TestPELType::primarySRCSection);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500110
111 Stream stream{data};
112 SRC src{stream};
113
114 EXPECT_TRUE(src.valid());
115
116 EXPECT_EQ(src.header().id, 0x5053);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600117 EXPECT_EQ(src.header().size, 0x50);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500118 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 Spinlerbd716f02019-10-15 10:54:11 -0500128 EXPECT_EQ(0x02020255, hexwords[0]);
129 EXPECT_EQ(0x03030310, hexwords[1]);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500130 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 Isa6fd0c1e2020-02-06 14:25:57 +0800148TEST_F(SRCTest, UnflattenFlattenTest2Callouts)
Matt Spinlerf9bae182019-10-09 13:37:38 -0500149{
Matt Spinler42828bd2019-10-11 10:39:30 -0500150 auto data = pelDataFactory(TestPELType::primarySRCSection2Callouts);
Matt Spinlerf9bae182019-10-09 13:37:38 -0500151
152 Stream stream{data};
153 SRC src{stream};
154
155 EXPECT_TRUE(src.valid());
Matt Spinlerbd716f02019-10-15 10:54:11 -0500156 EXPECT_EQ(src.flags(), 0x01); // Additional sections within the SRC.
Matt Spinlerf9bae182019-10-09 13:37:38 -0500157
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 Spinlerbd716f02019-10-15 10:54:11 -0500183
184// Create an SRC from the message registry
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +0800185TEST_F(SRCTest, CreateTestNoCallouts)
Matt Spinlerbd716f02019-10-15 10:54:11 -0500186{
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 Spinler075e5ba2020-02-21 15:46:00 -0600202 NiceMock<MockDataInterface> dataIface;
203
204 EXPECT_CALL(dataIface, getMotherboardCCIN).WillOnce(Return("ABCD"));
205
206 SRC src{entry, ad, dataIface};
Matt Spinlerbd716f02019-10-15 10:54:11 -0500207
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 Spinler075e5ba2020-02-21 15:46:00 -0600221 EXPECT_EQ(hexwords[3 - 2] & 0xFFFF0000, 0xABCD0000); // Motherboard CCIN
Matt Spinlerbd716f02019-10-15 10:54:11 -0500222
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 Isa6fd0c1e2020-02-06 14:25:57 +0800252
Matt Spinler075e5ba2020-02-21 15:46:00 -0600253// Test when the CCIN string isn't a 4 character number
254TEST_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 Isa6fd0c1e2020-02-06 14:25:57 +0800296// Test the getErrorDetails function
297TEST_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 Spinler075e5ba2020-02-21 15:46:00 -0600306 NiceMock<MockDataInterface> dataIface;
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +0800307
Matt Spinler075e5ba2020-02-21 15:46:00 -0600308 SRC src{*entry, ad, dataIface};
Harisuddin Mohamed Isa6fd0c1e2020-02-06 14:25:57 +0800309 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 Spinlered046852020-03-13 13:58:15 -0500317
318// Test that an inventory path callout string is
319// converted into the appropriate FRU callout.
320TEST_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.
369TEST_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}
Matt Spinler03984582020-04-09 13:17:58 -0500414
415TEST_F(SRCTest, RegistryCalloutTest)
416{
417 message::Entry entry;
418 entry.src.type = 0xBD;
419 entry.src.reasonCode = 0xABCD;
420 entry.subsystem = 0x42;
421 entry.src.powerFault = false;
422
423 entry.callouts = R"(
424 [
425 {
426 "System": "systemA",
427 "CalloutList":
428 [
429 {
430 "Priority": "high",
431 "SymbolicFRU": "service_docs"
432 },
433 {
434 "Priority": "medium",
435 "Procedure": "no_vpd_for_fru"
436 }
437 ]
438 },
439 {
440 "System": "systemB",
441 "CalloutList":
442 [
443 {
444 "Priority": "high",
445 "LocCode": "P0-C8",
446 "SymbolicFRUTrusted": "service_docs"
447 },
448 {
449 "Priority": "medium",
450 "SymbolicFRUTrusted": "service_docs"
451 }
452 ]
453
454 }
455 ])"_json;
456
457 {
458 // Call out a symbolic FRU and a procedure
459 AdditionalData ad;
460 NiceMock<MockDataInterface> dataIface;
461 EXPECT_CALL(dataIface, getSystemType).WillOnce(Return("systemA"));
462
463 SRC src{entry, ad, dataIface};
464
465 auto& callouts = src.callouts()->callouts();
466 ASSERT_EQ(callouts.size(), 2);
467
468 EXPECT_EQ(callouts[0]->locationCodeSize(), 0);
469 EXPECT_EQ(callouts[0]->priority(), 'H');
470
471 EXPECT_EQ(callouts[1]->locationCodeSize(), 0);
472 EXPECT_EQ(callouts[1]->priority(), 'M');
473
474 auto& fru1 = callouts[0]->fruIdentity();
475 EXPECT_EQ(fru1->getPN().value(), "SVCDOCS");
476 EXPECT_EQ(fru1->failingComponentType(), src::FRUIdentity::symbolicFRU);
477 EXPECT_FALSE(fru1->getMaintProc());
478 EXPECT_FALSE(fru1->getSN());
479 EXPECT_FALSE(fru1->getCCIN());
480
481 auto& fru2 = callouts[1]->fruIdentity();
482 EXPECT_EQ(fru2->getMaintProc().value(), "BMCSP01");
483 EXPECT_EQ(fru2->failingComponentType(),
484 src::FRUIdentity::maintenanceProc);
485 EXPECT_FALSE(fru2->getPN());
486 EXPECT_FALSE(fru2->getSN());
487 EXPECT_FALSE(fru2->getCCIN());
488 }
489
490 {
491 // Call out a trusted symbolic FRU with a location code, and
492 // another one without.
493 AdditionalData ad;
494 NiceMock<MockDataInterface> dataIface;
495 EXPECT_CALL(dataIface, getSystemType).WillOnce(Return("systemB"));
496
497 SRC src{entry, ad, dataIface};
498
499 auto& callouts = src.callouts()->callouts();
500 EXPECT_EQ(callouts.size(), 2);
501
502 EXPECT_EQ(callouts[0]->locationCode(), "P0-C8");
503 EXPECT_EQ(callouts[0]->priority(), 'H');
504
505 EXPECT_EQ(callouts[1]->locationCodeSize(), 0);
506 EXPECT_EQ(callouts[1]->priority(), 'M');
507
508 auto& fru1 = callouts[0]->fruIdentity();
509 EXPECT_EQ(fru1->getPN().value(), "SVCDOCS");
510 EXPECT_EQ(fru1->failingComponentType(),
511 src::FRUIdentity::symbolicFRUTrustedLocCode);
512 EXPECT_FALSE(fru1->getMaintProc());
513 EXPECT_FALSE(fru1->getSN());
514 EXPECT_FALSE(fru1->getCCIN());
515
516 // It asked for a trusted symbolic FRU, but no location code
517 // was provided so it is switched back to a normal one
518 auto& fru2 = callouts[1]->fruIdentity();
519 EXPECT_EQ(fru2->getPN().value(), "SVCDOCS");
520 EXPECT_EQ(fru2->failingComponentType(), src::FRUIdentity::symbolicFRU);
521 EXPECT_FALSE(fru2->getMaintProc());
522 EXPECT_FALSE(fru2->getSN());
523 EXPECT_FALSE(fru2->getCCIN());
524 }
525}