blob: f980c9d5eaba746b2f6c47e4e2397691e6b8d5f7 [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 Spinlerb8323632019-09-20 15:11:04 -050016#include "elog_entry.hpp"
Matt Spinler131870c2019-09-25 13:29:04 -050017#include "extensions/openpower-pels/generic.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050018#include "extensions/openpower-pels/pel.hpp"
Matt Spinleraa659472019-10-23 09:26:48 -050019#include "mocks.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -050020#include "pel_utils.hpp"
21
22#include <filesystem>
23#include <fstream>
24
25#include <gtest/gtest.h>
26
27namespace fs = std::filesystem;
28using namespace openpower::pels;
Matt Spinler0a90a852020-06-04 13:18:27 -050029using ::testing::_;
William A. Kennington IIIb41fa542021-05-29 14:45:16 -070030using ::testing::DoAll;
Matt Spinler56ad2a02020-03-26 14:00:52 -050031using ::testing::NiceMock;
Matt Spinler677381b2020-01-23 10:04:29 -060032using ::testing::Return;
Matt Spinler0a90a852020-06-04 13:18:27 -050033using ::testing::SetArgReferee;
Matt Spinlercb6b0592019-07-16 15:58:51 -050034
35class PELTest : public CleanLogID
36{
37};
38
Matt Spinler5b289b22020-03-26 14:27:19 -050039fs::path makeTempDir()
40{
41 char path[] = "/tmp/tempdirXXXXXX";
42 std::filesystem::path dir = mkdtemp(path);
43 return dir;
44}
45
46int writeFileAndGetFD(const fs::path& dir, const std::vector<uint8_t>& data)
47{
48 static size_t count = 0;
49 fs::path path = dir / (std::string{"file"} + std::to_string(count));
50 std::ofstream stream{path};
51 count++;
52
53 stream.write(reinterpret_cast<const char*>(data.data()), data.size());
54 stream.close();
55
56 FILE* fp = fopen(path.c_str(), "r");
57 return fileno(fp);
58}
59
Matt Spinlercb6b0592019-07-16 15:58:51 -050060TEST_F(PELTest, FlattenTest)
61{
Matt Spinler42828bd2019-10-11 10:39:30 -050062 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler42828bd2019-10-11 10:39:30 -050063 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050064
65 // Check a few fields
66 EXPECT_TRUE(pel->valid());
67 EXPECT_EQ(pel->id(), 0x80818283);
68 EXPECT_EQ(pel->plid(), 0x50515253);
Matt Spinler97d19b42019-10-29 11:34:03 -050069 EXPECT_EQ(pel->userHeader().subsystem(), 0x10);
70 EXPECT_EQ(pel->userHeader().actionFlags(), 0x80C0);
Matt Spinlercb6b0592019-07-16 15:58:51 -050071
72 // Test that data in == data out
73 auto flattenedData = pel->data();
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060074 EXPECT_EQ(data, flattenedData);
75 EXPECT_EQ(flattenedData.size(), pel->size());
Matt Spinlercb6b0592019-07-16 15:58:51 -050076}
77
78TEST_F(PELTest, CommitTimeTest)
79{
Matt Spinler42828bd2019-10-11 10:39:30 -050080 auto data = pelDataFactory(TestPELType::pelSimple);
81 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050082
83 auto origTime = pel->commitTime();
84 pel->setCommitTime();
85 auto newTime = pel->commitTime();
86
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060087 EXPECT_NE(origTime, newTime);
Matt Spinlercb6b0592019-07-16 15:58:51 -050088
89 // Make a new PEL and check new value is still there
90 auto newData = pel->data();
91 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060092 EXPECT_EQ(newTime, newPel->commitTime());
Matt Spinlercb6b0592019-07-16 15:58:51 -050093}
94
95TEST_F(PELTest, AssignIDTest)
96{
Matt Spinler42828bd2019-10-11 10:39:30 -050097 auto data = pelDataFactory(TestPELType::pelSimple);
98 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050099
100 auto origID = pel->id();
101 pel->assignID();
102 auto newID = pel->id();
103
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600104 EXPECT_NE(origID, newID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500105
106 // Make a new PEL and check new value is still there
107 auto newData = pel->data();
108 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600109 EXPECT_EQ(newID, newPel->id());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500110}
111
112TEST_F(PELTest, WithLogIDTest)
113{
Matt Spinler42828bd2019-10-11 10:39:30 -0500114 auto data = pelDataFactory(TestPELType::pelSimple);
115 auto pel = std::make_unique<PEL>(data, 0x42);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500116
117 EXPECT_TRUE(pel->valid());
118 EXPECT_EQ(pel->obmcLogID(), 0x42);
119}
120
121TEST_F(PELTest, InvalidPELTest)
122{
Matt Spinler42828bd2019-10-11 10:39:30 -0500123 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500124
125 // Too small
Matt Spinler42828bd2019-10-11 10:39:30 -0500126 data.resize(PrivateHeader::flattenedSize());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500127
Matt Spinler42828bd2019-10-11 10:39:30 -0500128 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500129
Matt Spinler97d19b42019-10-29 11:34:03 -0500130 EXPECT_TRUE(pel->privateHeader().valid());
131 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500132 EXPECT_FALSE(pel->valid());
133
Matt Spinlercb6b0592019-07-16 15:58:51 -0500134 // Now corrupt the private header
Matt Spinler42828bd2019-10-11 10:39:30 -0500135 data = pelDataFactory(TestPELType::pelSimple);
136 data.at(0) = 0;
137 pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500138
Matt Spinler97d19b42019-10-29 11:34:03 -0500139 EXPECT_FALSE(pel->privateHeader().valid());
140 EXPECT_TRUE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500141 EXPECT_FALSE(pel->valid());
142}
143
144TEST_F(PELTest, EmptyDataTest)
145{
146 std::vector<uint8_t> data;
147 auto pel = std::make_unique<PEL>(data);
148
Matt Spinler97d19b42019-10-29 11:34:03 -0500149 EXPECT_FALSE(pel->privateHeader().valid());
150 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500151 EXPECT_FALSE(pel->valid());
152}
Matt Spinlerb8323632019-09-20 15:11:04 -0500153
154TEST_F(PELTest, CreateFromRegistryTest)
155{
156 message::Entry regEntry;
157 uint64_t timestamp = 5;
158
159 regEntry.name = "test";
160 regEntry.subsystem = 5;
161 regEntry.actionFlags = 0xC000;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500162 regEntry.src.type = 0xBD;
163 regEntry.src.reasonCode = 0x1234;
Matt Spinlerb8323632019-09-20 15:11:04 -0500164
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600165 std::vector<std::string> data{"KEY1=VALUE1"};
166 AdditionalData ad{data};
Matt Spinler56ad2a02020-03-26 14:00:52 -0500167 NiceMock<MockDataInterface> dataIface;
168 PelFFDC ffdc;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500169
Sumit Kumar9d43a722021-08-24 09:46:19 -0500170 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
171 "system/entry"};
172 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
173 .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
174
Matt Spinler56ad2a02020-03-26 14:00:52 -0500175 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
176 ad, ffdc, dataIface};
Matt Spinlerb8323632019-09-20 15:11:04 -0500177
178 EXPECT_TRUE(pel.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -0500179 EXPECT_EQ(pel.privateHeader().obmcLogID(), 42);
180 EXPECT_EQ(pel.userHeader().severity(), 0x40);
Matt Spinlerb8323632019-09-20 15:11:04 -0500181
Matt Spinlerbd716f02019-10-15 10:54:11 -0500182 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
183 "BD051234 ");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600184
185 // Check that certain optional sections have been created
186 size_t mtmsCount = 0;
187 size_t euhCount = 0;
188 size_t udCount = 0;
189
190 for (const auto& section : pel.optionalSections())
191 {
192 if (section->header().id ==
193 static_cast<uint16_t>(SectionID::failingMTMS))
194 {
195 mtmsCount++;
196 }
197 else if (section->header().id ==
198 static_cast<uint16_t>(SectionID::extendedUserHeader))
199 {
200 euhCount++;
201 }
202 else if (section->header().id ==
203 static_cast<uint16_t>(SectionID::userData))
204 {
205 udCount++;
206 }
207 }
208
209 EXPECT_EQ(mtmsCount, 1);
210 EXPECT_EQ(euhCount, 1);
211 EXPECT_EQ(udCount, 2); // AD section and sysInfo section
Andrew Geissler44fc3162020-07-09 09:21:31 -0500212 ASSERT_FALSE(pel.isCalloutPresent());
Matt Spinler1f93c592020-09-10 10:43:08 -0500213
214 {
215 // The same thing, but without the action flags specified
216 // in the registry, so the constructor should set them.
217 regEntry.actionFlags = std::nullopt;
218
219 PEL pel2{
220 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
221 ad, ffdc, dataIface};
222
223 EXPECT_EQ(pel2.userHeader().actionFlags(), 0xA800);
224 }
Matt Spinlerb8323632019-09-20 15:11:04 -0500225}
Matt Spinler131870c2019-09-25 13:29:04 -0500226
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500227// Test that when the AdditionalData size is over 16KB that
228// the PEL that's created is exactly 16KB since the UserData
229// section that contains all that data was pruned.
230TEST_F(PELTest, CreateTooBigADTest)
231{
232 message::Entry regEntry;
233 uint64_t timestamp = 5;
234
235 regEntry.name = "test";
236 regEntry.subsystem = 5;
237 regEntry.actionFlags = 0xC000;
238 regEntry.src.type = 0xBD;
239 regEntry.src.reasonCode = 0x1234;
Matt Spinler56ad2a02020-03-26 14:00:52 -0500240 PelFFDC ffdc;
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500241
242 // Over the 16KB max PEL size
243 std::string bigAD{"KEY1="};
244 bigAD += std::string(17000, 'G');
245
246 std::vector<std::string> data{bigAD};
247 AdditionalData ad{data};
Matt Spinler56ad2a02020-03-26 14:00:52 -0500248 NiceMock<MockDataInterface> dataIface;
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500249
Sumit Kumar9d43a722021-08-24 09:46:19 -0500250 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
251 "system/entry"};
252 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
253 .WillOnce(Return(std::vector<bool>{false, false, false}));
254
Matt Spinler56ad2a02020-03-26 14:00:52 -0500255 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
256 ad, ffdc, dataIface};
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500257
258 EXPECT_TRUE(pel.valid());
259 EXPECT_EQ(pel.size(), 16384);
260
261 // Make sure that there are still 2 UD sections.
262 size_t udCount = 0;
263 for (const auto& section : pel.optionalSections())
264 {
265 if (section->header().id == static_cast<uint16_t>(SectionID::userData))
266 {
267 udCount++;
268 }
269 }
270
271 EXPECT_EQ(udCount, 2); // AD section and sysInfo section
272}
273
Matt Spinler131870c2019-09-25 13:29:04 -0500274// Test that we'll create Generic optional sections for sections that
275// there aren't explicit classes for.
276TEST_F(PELTest, GenericSectionTest)
277{
Matt Spinler42828bd2019-10-11 10:39:30 -0500278 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500279
280 std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
281 0x00, 0x18, // Size
282 0x01, 0x02, // version, subtype
283 0x03, 0x04, // comp ID
284
285 // some data
286 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
287 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
288 0x00};
289
290 std::vector<uint8_t> section2{
291 0x59, 0x59, // ID 'YY'
292 0x00, 0x20, // Size
293 0x01, 0x02, // version, subtype
294 0x03, 0x04, // comp ID
295
296 // some data
297 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
298 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
299
300 // Add the new sections at the end
Matt Spinler42828bd2019-10-11 10:39:30 -0500301 data.insert(data.end(), section1.begin(), section1.end());
302 data.insert(data.end(), section2.begin(), section2.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500303
304 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500305 data.at(27) += 2;
306 auto origData = data;
Matt Spinler131870c2019-09-25 13:29:04 -0500307
Matt Spinler42828bd2019-10-11 10:39:30 -0500308 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500309
310 const auto& sections = pel.optionalSections();
311
312 bool foundXX = false;
313 bool foundYY = false;
314
315 // Check that we can find these 2 Generic sections
316 for (const auto& section : sections)
317 {
318 if (section->header().id == 0x5858)
319 {
320 foundXX = true;
321 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
322 }
323 else if (section->header().id == 0x5959)
324 {
325 foundYY = true;
326 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
327 }
328 }
329
330 EXPECT_TRUE(foundXX);
331 EXPECT_TRUE(foundYY);
Matt Spinler07eefc52019-09-26 11:18:26 -0500332
333 // Now flatten and check
334 auto newData = pel.data();
335
336 EXPECT_EQ(origData, newData);
Matt Spinler131870c2019-09-25 13:29:04 -0500337}
338
339// Test that an invalid section will still get a Generic object
340TEST_F(PELTest, InvalidGenericTest)
341{
Matt Spinler42828bd2019-10-11 10:39:30 -0500342 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500343
344 // Not a valid section
345 std::vector<uint8_t> section1{0x01, 0x02, 0x03};
346
Matt Spinler42828bd2019-10-11 10:39:30 -0500347 data.insert(data.end(), section1.begin(), section1.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500348
349 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500350 data.at(27) += 1;
Matt Spinler131870c2019-09-25 13:29:04 -0500351
Matt Spinler42828bd2019-10-11 10:39:30 -0500352 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500353 EXPECT_FALSE(pel.valid());
354
355 const auto& sections = pel.optionalSections();
356
357 bool foundGeneric = false;
358 for (const auto& section : sections)
359 {
360 if (dynamic_cast<Generic*>(section.get()) != nullptr)
361 {
362 foundGeneric = true;
363 EXPECT_EQ(section->valid(), false);
364 break;
365 }
366 }
367
368 EXPECT_TRUE(foundGeneric);
369}
Matt Spinlerafa857c2019-10-24 13:03:46 -0500370
371// Create a UserData section out of AdditionalData
372TEST_F(PELTest, MakeUDSectionTest)
373{
374 std::vector<std::string> ad{"KEY1=VALUE1", "KEY2=VALUE2", "KEY3=VALUE3",
375 "ESEL=TEST"};
376 AdditionalData additionalData{ad};
377
378 auto ud = util::makeADUserDataSection(additionalData);
379
380 EXPECT_TRUE(ud->valid());
381 EXPECT_EQ(ud->header().id, 0x5544);
382 EXPECT_EQ(ud->header().version, 0x01);
383 EXPECT_EQ(ud->header().subType, 0x01);
384 EXPECT_EQ(ud->header().componentID, 0x2000);
385
386 const auto& d = ud->data();
387
388 std::string jsonString{d.begin(), d.end()};
Matt Spinler53407be2019-11-18 09:16:31 -0600389
390 std::string expectedJSON =
Matt Spinlerafa857c2019-10-24 13:03:46 -0500391 R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})";
Matt Spinler53407be2019-11-18 09:16:31 -0600392
393 // The actual data is null padded to a 4B boundary.
394 std::vector<uint8_t> expectedData;
395 expectedData.resize(52, '\0');
396 memcpy(expectedData.data(), expectedJSON.data(), expectedJSON.size());
397
398 EXPECT_EQ(d, expectedData);
Matt Spinlerafa857c2019-10-24 13:03:46 -0500399
400 // Ensure we can read this as JSON
401 auto newJSON = nlohmann::json::parse(jsonString);
402 EXPECT_EQ(newJSON["KEY1"], "VALUE1");
403 EXPECT_EQ(newJSON["KEY2"], "VALUE2");
404 EXPECT_EQ(newJSON["KEY3"], "VALUE3");
Matt Spinler97d19b42019-10-29 11:34:03 -0500405}
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600406
407// Create the UserData section that contains system info
Matt Spinler677381b2020-01-23 10:04:29 -0600408TEST_F(PELTest, SysInfoSectionTest)
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600409{
410 MockDataInterface dataIface;
411
Matt Spinler677381b2020-01-23 10:04:29 -0600412 EXPECT_CALL(dataIface, getBMCFWVersionID()).WillOnce(Return("ABCD1234"));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600413 EXPECT_CALL(dataIface, getBMCState()).WillOnce(Return("State.Ready"));
414 EXPECT_CALL(dataIface, getChassisState()).WillOnce(Return("State.On"));
415 EXPECT_CALL(dataIface, getHostState()).WillOnce(Return("State.Off"));
Ben Tynere32b7e72021-05-18 12:38:40 -0500416 EXPECT_CALL(dataIface, getSystemIMKeyword())
417 .WillOnce(Return(std::vector<uint8_t>{0, 1, 0x55, 0xAA}));
Matt Spinler677381b2020-01-23 10:04:29 -0600418
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600419 std::string pid = "_PID=" + std::to_string(getpid());
420 std::vector<std::string> ad{pid};
421 AdditionalData additionalData{ad};
422
423 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
424
425 EXPECT_TRUE(ud->valid());
426 EXPECT_EQ(ud->header().id, 0x5544);
427 EXPECT_EQ(ud->header().version, 0x01);
428 EXPECT_EQ(ud->header().subType, 0x01);
429 EXPECT_EQ(ud->header().componentID, 0x2000);
430
431 // Pull out the JSON data and check it.
432 const auto& d = ud->data();
433 std::string jsonString{d.begin(), d.end()};
434 auto json = nlohmann::json::parse(jsonString);
435
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500436 // Ensure the 'Process Name' entry contains the name of this test
437 // executable.
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600438 auto name = json["Process Name"].get<std::string>();
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500439 auto found = (name.find("pel_test") != std::string::npos) ||
440 (name.find("test-openpower-pels-pel") != std::string::npos);
441 EXPECT_TRUE(found);
442 // @TODO(stwcx): remove 'pel_test' when removing autotools.
Matt Spinler677381b2020-01-23 10:04:29 -0600443
Matt Spinlerc2b8a512021-05-21 12:44:42 -0600444 auto version = json["FW Version ID"].get<std::string>();
Matt Spinler677381b2020-01-23 10:04:29 -0600445 EXPECT_EQ(version, "ABCD1234");
Matt Spinler4aa23a12020-02-03 15:05:09 -0600446
447 auto state = json["BMCState"].get<std::string>();
448 EXPECT_EQ(state, "Ready");
449
450 state = json["ChassisState"].get<std::string>();
451 EXPECT_EQ(state, "On");
452
453 state = json["HostState"].get<std::string>();
454 EXPECT_EQ(state, "Off");
Ben Tynere32b7e72021-05-18 12:38:40 -0500455
456 auto keyword = json["System IM"].get<std::string>();
457 EXPECT_EQ(keyword, "000155AA");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600458}
Matt Spinlerce3f4502020-01-22 15:44:35 -0600459
460// Test that the sections that override
461// virtual std::optional<std::string> Section::getJSON() const
462// return valid JSON.
463TEST_F(PELTest, SectionJSONTest)
464{
465 auto data = pelDataFactory(TestPELType::pelSimple);
466 PEL pel{data};
467
468 // Check that all JSON returned from the sections is
469 // parseable by nlohmann::json, which will throw an
470 // exception and fail the test if there is a problem.
471
472 // The getJSON() response needs to be wrapped in a { } to make
473 // actual valid JSON (PEL::toJSON() usually handles that).
474
475 auto jsonString = pel.privateHeader().getJSON();
476
477 // PrivateHeader always prints JSON
478 ASSERT_TRUE(jsonString);
479 *jsonString = '{' + *jsonString + '}';
480 auto json = nlohmann::json::parse(*jsonString);
481
482 jsonString = pel.userHeader().getJSON();
483
484 // UserHeader always prints JSON
485 ASSERT_TRUE(jsonString);
486 *jsonString = '{' + *jsonString + '}';
487 json = nlohmann::json::parse(*jsonString);
488
489 for (const auto& section : pel.optionalSections())
490 {
491 // The optional sections may or may not have implemented getJSON().
492 jsonString = section->getJSON();
493 if (jsonString)
494 {
495 *jsonString = '{' + *jsonString + '}';
496 auto json = nlohmann::json::parse(*jsonString);
497 }
498 }
499}
Matt Spinler5b289b22020-03-26 14:27:19 -0500500
501PelFFDCfile getJSONFFDC(const fs::path& dir)
502{
503 PelFFDCfile ffdc;
504 ffdc.format = UserDataFormat::json;
505 ffdc.subType = 5;
506 ffdc.version = 42;
507
508 auto inputJSON = R"({
509 "key1": "value1",
510 "key2": 42,
511 "key3" : [1, 2, 3, 4, 5],
512 "key4": {"key5": "value5"}
513 })"_json;
514
515 // Write the JSON to a file and get its descriptor.
516 auto s = inputJSON.dump();
517 std::vector<uint8_t> data{s.begin(), s.end()};
518 ffdc.fd = writeFileAndGetFD(dir, data);
519
520 return ffdc;
521}
522
523TEST_F(PELTest, MakeJSONFileUDSectionTest)
524{
525 auto dir = makeTempDir();
526
527 {
528 auto ffdc = getJSONFFDC(dir);
529
530 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
531 close(ffdc.fd);
532 ASSERT_TRUE(ud);
533 ASSERT_TRUE(ud->valid());
534 EXPECT_EQ(ud->header().id, 0x5544);
535
536 EXPECT_EQ(ud->header().version,
537 static_cast<uint8_t>(UserDataFormatVersion::json));
538 EXPECT_EQ(ud->header().subType,
539 static_cast<uint8_t>(UserDataFormat::json));
540 EXPECT_EQ(ud->header().componentID,
541 static_cast<uint16_t>(ComponentID::phosphorLogging));
542
543 // Pull the JSON back out of the the UserData section
544 const auto& d = ud->data();
545 std::string js{d.begin(), d.end()};
546 auto json = nlohmann::json::parse(js);
547
548 EXPECT_EQ("value1", json["key1"].get<std::string>());
549 EXPECT_EQ(42, json["key2"].get<int>());
550
551 std::vector<int> key3Values{1, 2, 3, 4, 5};
552 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
553
554 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
555 auto actual = json["key4"].get<std::map<std::string, std::string>>();
556 EXPECT_EQ(key4Values, actual);
557 }
558
559 {
560 // A bad FD
561 PelFFDCfile ffdc;
562 ffdc.format = UserDataFormat::json;
563 ffdc.subType = 5;
564 ffdc.version = 42;
565 ffdc.fd = 10000;
566
567 // The section shouldn't get made
568 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
569 ASSERT_FALSE(ud);
570 }
571
572 fs::remove_all(dir);
573}
574
575PelFFDCfile getCBORFFDC(const fs::path& dir)
576{
577 PelFFDCfile ffdc;
578 ffdc.format = UserDataFormat::cbor;
579 ffdc.subType = 5;
580 ffdc.version = 42;
581
582 auto inputJSON = R"({
583 "key1": "value1",
584 "key2": 42,
585 "key3" : [1, 2, 3, 4, 5],
586 "key4": {"key5": "value5"}
587 })"_json;
588
589 // Convert the JSON to CBOR and write it to a file
590 auto data = nlohmann::json::to_cbor(inputJSON);
591 ffdc.fd = writeFileAndGetFD(dir, data);
592
593 return ffdc;
594}
595
596TEST_F(PELTest, MakeCBORFileUDSectionTest)
597{
598 auto dir = makeTempDir();
599
600 auto ffdc = getCBORFFDC(dir);
601 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
602 close(ffdc.fd);
603 ASSERT_TRUE(ud);
604 ASSERT_TRUE(ud->valid());
605 EXPECT_EQ(ud->header().id, 0x5544);
606
607 EXPECT_EQ(ud->header().version,
608 static_cast<uint8_t>(UserDataFormatVersion::cbor));
609 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::cbor));
610 EXPECT_EQ(ud->header().componentID,
611 static_cast<uint16_t>(ComponentID::phosphorLogging));
612
613 // Pull the CBOR back out of the PEL section
614 // The number of pad bytes to make the section be 4B aligned
615 // was added at the end, read it and then remove it and the
616 // padding before parsing it.
617 auto data = ud->data();
618 Stream stream{data};
619 stream.offset(data.size() - 4);
620 uint32_t pad;
621 stream >> pad;
622
623 data.resize(data.size() - 4 - pad);
624
625 auto json = nlohmann::json::from_cbor(data);
626
627 EXPECT_EQ("value1", json["key1"].get<std::string>());
628 EXPECT_EQ(42, json["key2"].get<int>());
629
630 std::vector<int> key3Values{1, 2, 3, 4, 5};
631 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
632
633 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
634 auto actual = json["key4"].get<std::map<std::string, std::string>>();
635 EXPECT_EQ(key4Values, actual);
636
637 fs::remove_all(dir);
638}
639
640PelFFDCfile getTextFFDC(const fs::path& dir)
641{
642 PelFFDCfile ffdc;
643 ffdc.format = UserDataFormat::text;
644 ffdc.subType = 5;
645 ffdc.version = 42;
646
647 std::string text{"this is some text that will be used for FFDC"};
648 std::vector<uint8_t> data{text.begin(), text.end()};
649
650 ffdc.fd = writeFileAndGetFD(dir, data);
651
652 return ffdc;
653}
654
655TEST_F(PELTest, MakeTextFileUDSectionTest)
656{
657 auto dir = makeTempDir();
658
659 auto ffdc = getTextFFDC(dir);
660 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
661 close(ffdc.fd);
662 ASSERT_TRUE(ud);
663 ASSERT_TRUE(ud->valid());
664 EXPECT_EQ(ud->header().id, 0x5544);
665
666 EXPECT_EQ(ud->header().version,
667 static_cast<uint8_t>(UserDataFormatVersion::text));
668 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::text));
669 EXPECT_EQ(ud->header().componentID,
670 static_cast<uint16_t>(ComponentID::phosphorLogging));
671
672 // Get the text back out
673 std::string text{ud->data().begin(), ud->data().end()};
674 EXPECT_EQ(text, "this is some text that will be used for FFDC");
675
676 fs::remove_all(dir);
677}
678
679PelFFDCfile getCustomFFDC(const fs::path& dir, const std::vector<uint8_t>& data)
680{
681 PelFFDCfile ffdc;
682 ffdc.format = UserDataFormat::custom;
683 ffdc.subType = 5;
684 ffdc.version = 42;
685
686 ffdc.fd = writeFileAndGetFD(dir, data);
687
688 return ffdc;
689}
690
691TEST_F(PELTest, MakeCustomFileUDSectionTest)
692{
693 auto dir = makeTempDir();
694
695 {
696 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8};
697
698 auto ffdc = getCustomFFDC(dir, data);
699 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
700 close(ffdc.fd);
701 ASSERT_TRUE(ud);
702 ASSERT_TRUE(ud->valid());
703 EXPECT_EQ(ud->header().size, 8 + 8); // data size + header size
704 EXPECT_EQ(ud->header().id, 0x5544);
705
706 EXPECT_EQ(ud->header().version, 42);
707 EXPECT_EQ(ud->header().subType, 5);
708 EXPECT_EQ(ud->header().componentID, 0x2002);
709
710 // Get the data back out
711 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
712 EXPECT_EQ(data, newData);
713 }
714
715 // Do the same thing again, but make it be non 4B aligned
716 // so the data gets padded.
717 {
718 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8, 9};
719
720 auto ffdc = getCustomFFDC(dir, data);
721 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
722 close(ffdc.fd);
723 ASSERT_TRUE(ud);
724 ASSERT_TRUE(ud->valid());
725 EXPECT_EQ(ud->header().size, 12 + 8); // data size + header size
726 EXPECT_EQ(ud->header().id, 0x5544);
727
728 EXPECT_EQ(ud->header().version, 42);
729 EXPECT_EQ(ud->header().subType, 5);
730 EXPECT_EQ(ud->header().componentID, 0x2002);
731
732 // Get the data back out
733 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
734
735 // pad the original to 12B so we can compare
736 data.push_back(0);
737 data.push_back(0);
738 data.push_back(0);
739
740 EXPECT_EQ(data, newData);
741 }
742
743 fs::remove_all(dir);
744}
745
746// Test Adding FFDC from files to a PEL
747TEST_F(PELTest, CreateWithFFDCTest)
748{
749 auto dir = makeTempDir();
750 message::Entry regEntry;
751 uint64_t timestamp = 5;
752
753 regEntry.name = "test";
754 regEntry.subsystem = 5;
755 regEntry.actionFlags = 0xC000;
756 regEntry.src.type = 0xBD;
757 regEntry.src.reasonCode = 0x1234;
758
759 std::vector<std::string> additionalData{"KEY1=VALUE1"};
760 AdditionalData ad{additionalData};
761 NiceMock<MockDataInterface> dataIface;
762 PelFFDC ffdc;
763
764 std::vector<uint8_t> customData{1, 2, 3, 4, 5, 6, 7, 8};
765
766 // This will be trimmed when added
767 std::vector<uint8_t> hugeCustomData(17000, 0x42);
768
769 ffdc.emplace_back(std::move(getJSONFFDC(dir)));
770 ffdc.emplace_back(std::move(getCBORFFDC(dir)));
771 ffdc.emplace_back(std::move(getTextFFDC(dir)));
772 ffdc.emplace_back(std::move(getCustomFFDC(dir, customData)));
773 ffdc.emplace_back(std::move(getCustomFFDC(dir, hugeCustomData)));
774
Sumit Kumar9d43a722021-08-24 09:46:19 -0500775 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
776 "system/entry"};
777 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
778 .WillOnce(Return(std::vector<bool>{false, false, false}));
779
Matt Spinler5b289b22020-03-26 14:27:19 -0500780 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
781 ad, ffdc, dataIface};
782
783 EXPECT_TRUE(pel.valid());
784
785 // Clipped to the max
786 EXPECT_EQ(pel.size(), 16384);
787
788 // Check for the FFDC sections
789 size_t udCount = 0;
790 Section* ud = nullptr;
791
792 for (const auto& section : pel.optionalSections())
793 {
794 if (section->header().id == static_cast<uint16_t>(SectionID::userData))
795 {
796 udCount++;
797 ud = section.get();
798 }
799 }
800
801 EXPECT_EQ(udCount, 7); // AD section, sysInfo, 5 ffdc sections
802
803 // Check the last section was trimmed to
804 // something a bit less that 17000.
805 EXPECT_GT(ud->header().size, 14000);
806 EXPECT_LT(ud->header().size, 16000);
807
808 fs::remove_all(dir);
809}
Matt Spinler0a90a852020-06-04 13:18:27 -0500810
811// Create a PEL with device callouts
812TEST_F(PELTest, CreateWithDevCalloutsTest)
813{
814 message::Entry regEntry;
815 uint64_t timestamp = 5;
816
817 regEntry.name = "test";
818 regEntry.subsystem = 5;
819 regEntry.actionFlags = 0xC000;
820 regEntry.src.type = 0xBD;
821 regEntry.src.reasonCode = 0x1234;
822
823 NiceMock<MockDataInterface> dataIface;
824 PelFFDC ffdc;
825
826 const auto calloutJSON = R"(
827 {
828 "I2C":
829 {
830 "14":
831 {
832 "114":
833 {
834 "Callouts":[
835 {
836 "Name": "/chassis/motherboard/cpu0",
837 "LocationCode": "P1",
838 "Priority": "H"
839 }
840 ],
841 "Dest": "proc 0 target"
842 }
843 }
844 }
845 })";
846
847 std::vector<std::string> names{"systemA"};
848 EXPECT_CALL(dataIface, getSystemNames)
849 .Times(2)
Matt Spinler1ab66962020-10-29 13:21:44 -0500850 .WillRepeatedly(Return(names));
Matt Spinler0a90a852020-06-04 13:18:27 -0500851
Matt Spinler0d92b522021-06-16 13:28:17 -0600852 EXPECT_CALL(dataIface, expandLocationCode("P1", 0))
853 .Times(1)
Matt Spinler0a90a852020-06-04 13:18:27 -0500854 .WillOnce(Return("UXXX-P1"));
855
Matt Spinler2f9225a2020-08-05 12:58:49 -0500856 EXPECT_CALL(dataIface, getInventoryFromLocCode("P1", 0, false))
Matt Spinler0a90a852020-06-04 13:18:27 -0500857 .WillOnce(
858 Return("/xyz/openbmc_project/inventory/chassis/motherboard/cpu0"));
859
860 EXPECT_CALL(
861 dataIface,
862 getHWCalloutFields(
863 "/xyz/openbmc_project/inventory/chassis/motherboard/cpu0", _, _, _))
864 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
865 SetArgReferee<3>("123456789ABC")));
866
Sumit Kumar9d43a722021-08-24 09:46:19 -0500867 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
868 "system/entry"};
869 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
870 .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
871
Matt Spinler0a90a852020-06-04 13:18:27 -0500872 auto dataPath = getPELReadOnlyDataPath();
873 std::ofstream file{dataPath / "systemA_dev_callouts.json"};
874 file << calloutJSON;
875 file.close();
876
877 {
878 std::vector<std::string> data{
879 "CALLOUT_ERRNO=5",
880 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
881 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0072"};
882
883 AdditionalData ad{data};
884
885 PEL pel{
886 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
887 ad, ffdc, dataIface};
888
889 ASSERT_TRUE(pel.primarySRC().value()->callouts());
890 auto& callouts = pel.primarySRC().value()->callouts()->callouts();
891 ASSERT_EQ(callouts.size(), 1);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500892 ASSERT_TRUE(pel.isCalloutPresent());
Matt Spinler0a90a852020-06-04 13:18:27 -0500893
894 EXPECT_EQ(callouts[0]->priority(), 'H');
895 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P1");
896
897 auto& fru = callouts[0]->fruIdentity();
898 EXPECT_EQ(fru->getPN().value(), "1234567");
899 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
900 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
901
902 const auto& section = pel.optionalSections().back();
903
904 ASSERT_EQ(section->header().id, 0x5544); // UD
905 auto ud = static_cast<UserData*>(section.get());
906
907 // Check that there was a UserData section added that
908 // contains debug details about the device.
909 const auto& d = ud->data();
910 std::string jsonString{d.begin(), d.end()};
911 auto actualJSON = nlohmann::json::parse(jsonString);
912
913 auto expectedJSON = R"(
914 {
915 "PEL Internal Debug Data": {
916 "SRC": [
917 "I2C: bus: 14 address: 114 dest: proc 0 target"
918 ]
919 }
920 }
921 )"_json;
922
923 EXPECT_EQ(actualJSON, expectedJSON);
924 }
925
926 {
927 // Device path not found (wrong i2c addr), so no callouts
928 std::vector<std::string> data{
929 "CALLOUT_ERRNO=5",
930 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
931 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0099"};
932
933 AdditionalData ad{data};
934
935 PEL pel{
936 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
937 ad, ffdc, dataIface};
938
939 // no callouts
940 EXPECT_FALSE(pel.primarySRC().value()->callouts());
941
942 // Now check that there was a UserData section
943 // that contains the lookup error.
944 const auto& section = pel.optionalSections().back();
945
946 ASSERT_EQ(section->header().id, 0x5544); // UD
947 auto ud = static_cast<UserData*>(section.get());
948
949 const auto& d = ud->data();
950
951 std::string jsonString{d.begin(), d.end()};
952
953 auto actualJSON = nlohmann::json::parse(jsonString);
954
955 auto expectedJSON =
956 "{\"PEL Internal Debug Data\":{\"SRC\":"
957 "[\"Problem looking up I2C callouts on 14 153: "
958 "[json.exception.out_of_range.403] key '153' not found\"]}}"_json;
959
960 EXPECT_EQ(actualJSON, expectedJSON);
961 }
962
963 fs::remove_all(dataPath);
964}
Matt Spinlere513dbc2020-08-27 11:14:17 -0500965
966// Test PELs when the callouts are passed in using a JSON file.
967TEST_F(PELTest, CreateWithJSONCalloutsTest)
968{
969 PelFFDCfile ffdcFile;
970 ffdcFile.format = UserDataFormat::json;
971 ffdcFile.subType = 0xCA; // Callout JSON
972 ffdcFile.version = 1;
973
974 // Write these callouts to a JSON file and pass it into
975 // the PEL as an FFDC file.
976 auto inputJSON = R"([
977 {
978 "Priority": "H",
979 "LocationCode": "P0-C1"
980 },
981 {
982 "Priority": "M",
983 "Procedure": "PROCEDURE"
984 }
985 ])"_json;
986
987 auto s = inputJSON.dump();
988 std::vector<uint8_t> data{s.begin(), s.end()};
989 auto dir = makeTempDir();
990 ffdcFile.fd = writeFileAndGetFD(dir, data);
991
992 PelFFDC ffdc;
993 ffdc.push_back(std::move(ffdcFile));
994
995 AdditionalData ad;
996 NiceMock<MockDataInterface> dataIface;
997
998 EXPECT_CALL(dataIface, expandLocationCode("P0-C1", 0))
999 .Times(1)
1000 .WillOnce(Return("UXXX-P0-C1"));
1001 EXPECT_CALL(dataIface, getInventoryFromLocCode("P0-C1", 0, false))
1002 .Times(1)
1003 .WillOnce(Return("/inv/system/chassis/motherboard/bmc"));
1004 EXPECT_CALL(dataIface, getHWCalloutFields(
1005 "/inv/system/chassis/motherboard/bmc", _, _, _))
1006 .Times(1)
1007 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
1008 SetArgReferee<3>("123456789ABC")));
1009
Sumit Kumar9d43a722021-08-24 09:46:19 -05001010 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
1011 "system/entry"};
1012 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
1013 .WillOnce(Return(std::vector<bool>{false, false, false}));
1014
Matt Spinlere513dbc2020-08-27 11:14:17 -05001015 message::Entry regEntry;
1016 regEntry.name = "test";
1017 regEntry.subsystem = 5;
1018 regEntry.actionFlags = 0xC000;
1019 regEntry.src.type = 0xBD;
1020 regEntry.src.reasonCode = 0x1234;
1021
1022 PEL pel{regEntry, 42, 5, phosphor::logging::Entry::Level::Error,
1023 ad, ffdc, dataIface};
1024
1025 ASSERT_TRUE(pel.valid());
1026 ASSERT_TRUE(pel.primarySRC().value()->callouts());
1027 const auto& callouts = pel.primarySRC().value()->callouts()->callouts();
1028 ASSERT_EQ(callouts.size(), 2);
1029
1030 {
1031 EXPECT_EQ(callouts[0]->priority(), 'H');
1032 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P0-C1");
1033
1034 auto& fru = callouts[0]->fruIdentity();
1035 EXPECT_EQ(fru->getPN().value(), "1234567");
1036 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
1037 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
1038 EXPECT_EQ(fru->failingComponentType(), src::FRUIdentity::hardwareFRU);
1039 }
1040 {
1041 EXPECT_EQ(callouts[1]->priority(), 'M');
1042 EXPECT_EQ(callouts[1]->locationCode(), "");
1043
1044 auto& fru = callouts[1]->fruIdentity();
1045 EXPECT_EQ(fru->getMaintProc().value(), "PROCEDU");
1046 EXPECT_EQ(fru->failingComponentType(),
1047 src::FRUIdentity::maintenanceProc);
1048 }
1049 fs::remove_all(dir);
1050}