blob: 322d0cafb9d83a5cb6dc9d292f218e09739664f7 [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"));
Sumit Kumar2c36fdd2021-09-21 03:12:11 -0500416 EXPECT_CALL(dataIface, getBootState())
417 .WillOnce(Return("State.SystemInitComplete"));
Ben Tynere32b7e72021-05-18 12:38:40 -0500418 EXPECT_CALL(dataIface, getSystemIMKeyword())
419 .WillOnce(Return(std::vector<uint8_t>{0, 1, 0x55, 0xAA}));
Matt Spinler677381b2020-01-23 10:04:29 -0600420
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600421 std::string pid = "_PID=" + std::to_string(getpid());
422 std::vector<std::string> ad{pid};
423 AdditionalData additionalData{ad};
424
425 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
426
427 EXPECT_TRUE(ud->valid());
428 EXPECT_EQ(ud->header().id, 0x5544);
429 EXPECT_EQ(ud->header().version, 0x01);
430 EXPECT_EQ(ud->header().subType, 0x01);
431 EXPECT_EQ(ud->header().componentID, 0x2000);
432
433 // Pull out the JSON data and check it.
434 const auto& d = ud->data();
435 std::string jsonString{d.begin(), d.end()};
436 auto json = nlohmann::json::parse(jsonString);
437
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500438 // Ensure the 'Process Name' entry contains the name of this test
439 // executable.
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600440 auto name = json["Process Name"].get<std::string>();
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500441 auto found = (name.find("pel_test") != std::string::npos) ||
442 (name.find("test-openpower-pels-pel") != std::string::npos);
443 EXPECT_TRUE(found);
444 // @TODO(stwcx): remove 'pel_test' when removing autotools.
Matt Spinler677381b2020-01-23 10:04:29 -0600445
Matt Spinlerc2b8a512021-05-21 12:44:42 -0600446 auto version = json["FW Version ID"].get<std::string>();
Matt Spinler677381b2020-01-23 10:04:29 -0600447 EXPECT_EQ(version, "ABCD1234");
Matt Spinler4aa23a12020-02-03 15:05:09 -0600448
449 auto state = json["BMCState"].get<std::string>();
450 EXPECT_EQ(state, "Ready");
451
452 state = json["ChassisState"].get<std::string>();
453 EXPECT_EQ(state, "On");
454
455 state = json["HostState"].get<std::string>();
456 EXPECT_EQ(state, "Off");
Ben Tynere32b7e72021-05-18 12:38:40 -0500457
Sumit Kumar2c36fdd2021-09-21 03:12:11 -0500458 state = json["BootState"].get<std::string>();
459 EXPECT_EQ(state, "SystemInitComplete");
460
Ben Tynere32b7e72021-05-18 12:38:40 -0500461 auto keyword = json["System IM"].get<std::string>();
462 EXPECT_EQ(keyword, "000155AA");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600463}
Matt Spinlerce3f4502020-01-22 15:44:35 -0600464
465// Test that the sections that override
466// virtual std::optional<std::string> Section::getJSON() const
467// return valid JSON.
468TEST_F(PELTest, SectionJSONTest)
469{
470 auto data = pelDataFactory(TestPELType::pelSimple);
471 PEL pel{data};
472
473 // Check that all JSON returned from the sections is
474 // parseable by nlohmann::json, which will throw an
475 // exception and fail the test if there is a problem.
476
477 // The getJSON() response needs to be wrapped in a { } to make
478 // actual valid JSON (PEL::toJSON() usually handles that).
479
480 auto jsonString = pel.privateHeader().getJSON();
481
482 // PrivateHeader always prints JSON
483 ASSERT_TRUE(jsonString);
484 *jsonString = '{' + *jsonString + '}';
485 auto json = nlohmann::json::parse(*jsonString);
486
487 jsonString = pel.userHeader().getJSON();
488
489 // UserHeader always prints JSON
490 ASSERT_TRUE(jsonString);
491 *jsonString = '{' + *jsonString + '}';
492 json = nlohmann::json::parse(*jsonString);
493
494 for (const auto& section : pel.optionalSections())
495 {
496 // The optional sections may or may not have implemented getJSON().
497 jsonString = section->getJSON();
498 if (jsonString)
499 {
500 *jsonString = '{' + *jsonString + '}';
501 auto json = nlohmann::json::parse(*jsonString);
502 }
503 }
504}
Matt Spinler5b289b22020-03-26 14:27:19 -0500505
506PelFFDCfile getJSONFFDC(const fs::path& dir)
507{
508 PelFFDCfile ffdc;
509 ffdc.format = UserDataFormat::json;
510 ffdc.subType = 5;
511 ffdc.version = 42;
512
513 auto inputJSON = R"({
514 "key1": "value1",
515 "key2": 42,
516 "key3" : [1, 2, 3, 4, 5],
517 "key4": {"key5": "value5"}
518 })"_json;
519
520 // Write the JSON to a file and get its descriptor.
521 auto s = inputJSON.dump();
522 std::vector<uint8_t> data{s.begin(), s.end()};
523 ffdc.fd = writeFileAndGetFD(dir, data);
524
525 return ffdc;
526}
527
528TEST_F(PELTest, MakeJSONFileUDSectionTest)
529{
530 auto dir = makeTempDir();
531
532 {
533 auto ffdc = getJSONFFDC(dir);
534
535 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
536 close(ffdc.fd);
537 ASSERT_TRUE(ud);
538 ASSERT_TRUE(ud->valid());
539 EXPECT_EQ(ud->header().id, 0x5544);
540
541 EXPECT_EQ(ud->header().version,
542 static_cast<uint8_t>(UserDataFormatVersion::json));
543 EXPECT_EQ(ud->header().subType,
544 static_cast<uint8_t>(UserDataFormat::json));
545 EXPECT_EQ(ud->header().componentID,
546 static_cast<uint16_t>(ComponentID::phosphorLogging));
547
548 // Pull the JSON back out of the the UserData section
549 const auto& d = ud->data();
550 std::string js{d.begin(), d.end()};
551 auto json = nlohmann::json::parse(js);
552
553 EXPECT_EQ("value1", json["key1"].get<std::string>());
554 EXPECT_EQ(42, json["key2"].get<int>());
555
556 std::vector<int> key3Values{1, 2, 3, 4, 5};
557 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
558
559 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
560 auto actual = json["key4"].get<std::map<std::string, std::string>>();
561 EXPECT_EQ(key4Values, actual);
562 }
563
564 {
565 // A bad FD
566 PelFFDCfile ffdc;
567 ffdc.format = UserDataFormat::json;
568 ffdc.subType = 5;
569 ffdc.version = 42;
570 ffdc.fd = 10000;
571
572 // The section shouldn't get made
573 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
574 ASSERT_FALSE(ud);
575 }
576
577 fs::remove_all(dir);
578}
579
580PelFFDCfile getCBORFFDC(const fs::path& dir)
581{
582 PelFFDCfile ffdc;
583 ffdc.format = UserDataFormat::cbor;
584 ffdc.subType = 5;
585 ffdc.version = 42;
586
587 auto inputJSON = R"({
588 "key1": "value1",
589 "key2": 42,
590 "key3" : [1, 2, 3, 4, 5],
591 "key4": {"key5": "value5"}
592 })"_json;
593
594 // Convert the JSON to CBOR and write it to a file
595 auto data = nlohmann::json::to_cbor(inputJSON);
596 ffdc.fd = writeFileAndGetFD(dir, data);
597
598 return ffdc;
599}
600
601TEST_F(PELTest, MakeCBORFileUDSectionTest)
602{
603 auto dir = makeTempDir();
604
605 auto ffdc = getCBORFFDC(dir);
606 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
607 close(ffdc.fd);
608 ASSERT_TRUE(ud);
609 ASSERT_TRUE(ud->valid());
610 EXPECT_EQ(ud->header().id, 0x5544);
611
612 EXPECT_EQ(ud->header().version,
613 static_cast<uint8_t>(UserDataFormatVersion::cbor));
614 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::cbor));
615 EXPECT_EQ(ud->header().componentID,
616 static_cast<uint16_t>(ComponentID::phosphorLogging));
617
618 // Pull the CBOR back out of the PEL section
619 // The number of pad bytes to make the section be 4B aligned
620 // was added at the end, read it and then remove it and the
621 // padding before parsing it.
622 auto data = ud->data();
623 Stream stream{data};
624 stream.offset(data.size() - 4);
625 uint32_t pad;
626 stream >> pad;
627
628 data.resize(data.size() - 4 - pad);
629
630 auto json = nlohmann::json::from_cbor(data);
631
632 EXPECT_EQ("value1", json["key1"].get<std::string>());
633 EXPECT_EQ(42, json["key2"].get<int>());
634
635 std::vector<int> key3Values{1, 2, 3, 4, 5};
636 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
637
638 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
639 auto actual = json["key4"].get<std::map<std::string, std::string>>();
640 EXPECT_EQ(key4Values, actual);
641
642 fs::remove_all(dir);
643}
644
645PelFFDCfile getTextFFDC(const fs::path& dir)
646{
647 PelFFDCfile ffdc;
648 ffdc.format = UserDataFormat::text;
649 ffdc.subType = 5;
650 ffdc.version = 42;
651
652 std::string text{"this is some text that will be used for FFDC"};
653 std::vector<uint8_t> data{text.begin(), text.end()};
654
655 ffdc.fd = writeFileAndGetFD(dir, data);
656
657 return ffdc;
658}
659
660TEST_F(PELTest, MakeTextFileUDSectionTest)
661{
662 auto dir = makeTempDir();
663
664 auto ffdc = getTextFFDC(dir);
665 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
666 close(ffdc.fd);
667 ASSERT_TRUE(ud);
668 ASSERT_TRUE(ud->valid());
669 EXPECT_EQ(ud->header().id, 0x5544);
670
671 EXPECT_EQ(ud->header().version,
672 static_cast<uint8_t>(UserDataFormatVersion::text));
673 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::text));
674 EXPECT_EQ(ud->header().componentID,
675 static_cast<uint16_t>(ComponentID::phosphorLogging));
676
677 // Get the text back out
678 std::string text{ud->data().begin(), ud->data().end()};
679 EXPECT_EQ(text, "this is some text that will be used for FFDC");
680
681 fs::remove_all(dir);
682}
683
684PelFFDCfile getCustomFFDC(const fs::path& dir, const std::vector<uint8_t>& data)
685{
686 PelFFDCfile ffdc;
687 ffdc.format = UserDataFormat::custom;
688 ffdc.subType = 5;
689 ffdc.version = 42;
690
691 ffdc.fd = writeFileAndGetFD(dir, data);
692
693 return ffdc;
694}
695
696TEST_F(PELTest, MakeCustomFileUDSectionTest)
697{
698 auto dir = makeTempDir();
699
700 {
701 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8};
702
703 auto ffdc = getCustomFFDC(dir, data);
704 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
705 close(ffdc.fd);
706 ASSERT_TRUE(ud);
707 ASSERT_TRUE(ud->valid());
708 EXPECT_EQ(ud->header().size, 8 + 8); // data size + header size
709 EXPECT_EQ(ud->header().id, 0x5544);
710
711 EXPECT_EQ(ud->header().version, 42);
712 EXPECT_EQ(ud->header().subType, 5);
713 EXPECT_EQ(ud->header().componentID, 0x2002);
714
715 // Get the data back out
716 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
717 EXPECT_EQ(data, newData);
718 }
719
720 // Do the same thing again, but make it be non 4B aligned
721 // so the data gets padded.
722 {
723 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8, 9};
724
725 auto ffdc = getCustomFFDC(dir, data);
726 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
727 close(ffdc.fd);
728 ASSERT_TRUE(ud);
729 ASSERT_TRUE(ud->valid());
730 EXPECT_EQ(ud->header().size, 12 + 8); // data size + header size
731 EXPECT_EQ(ud->header().id, 0x5544);
732
733 EXPECT_EQ(ud->header().version, 42);
734 EXPECT_EQ(ud->header().subType, 5);
735 EXPECT_EQ(ud->header().componentID, 0x2002);
736
737 // Get the data back out
738 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
739
740 // pad the original to 12B so we can compare
741 data.push_back(0);
742 data.push_back(0);
743 data.push_back(0);
744
745 EXPECT_EQ(data, newData);
746 }
747
748 fs::remove_all(dir);
749}
750
751// Test Adding FFDC from files to a PEL
752TEST_F(PELTest, CreateWithFFDCTest)
753{
754 auto dir = makeTempDir();
755 message::Entry regEntry;
756 uint64_t timestamp = 5;
757
758 regEntry.name = "test";
759 regEntry.subsystem = 5;
760 regEntry.actionFlags = 0xC000;
761 regEntry.src.type = 0xBD;
762 regEntry.src.reasonCode = 0x1234;
763
764 std::vector<std::string> additionalData{"KEY1=VALUE1"};
765 AdditionalData ad{additionalData};
766 NiceMock<MockDataInterface> dataIface;
767 PelFFDC ffdc;
768
769 std::vector<uint8_t> customData{1, 2, 3, 4, 5, 6, 7, 8};
770
771 // This will be trimmed when added
772 std::vector<uint8_t> hugeCustomData(17000, 0x42);
773
774 ffdc.emplace_back(std::move(getJSONFFDC(dir)));
775 ffdc.emplace_back(std::move(getCBORFFDC(dir)));
776 ffdc.emplace_back(std::move(getTextFFDC(dir)));
777 ffdc.emplace_back(std::move(getCustomFFDC(dir, customData)));
778 ffdc.emplace_back(std::move(getCustomFFDC(dir, hugeCustomData)));
779
Sumit Kumar9d43a722021-08-24 09:46:19 -0500780 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
781 "system/entry"};
782 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
783 .WillOnce(Return(std::vector<bool>{false, false, false}));
784
Matt Spinler5b289b22020-03-26 14:27:19 -0500785 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
786 ad, ffdc, dataIface};
787
788 EXPECT_TRUE(pel.valid());
789
790 // Clipped to the max
791 EXPECT_EQ(pel.size(), 16384);
792
793 // Check for the FFDC sections
794 size_t udCount = 0;
795 Section* ud = nullptr;
796
797 for (const auto& section : pel.optionalSections())
798 {
799 if (section->header().id == static_cast<uint16_t>(SectionID::userData))
800 {
801 udCount++;
802 ud = section.get();
803 }
804 }
805
806 EXPECT_EQ(udCount, 7); // AD section, sysInfo, 5 ffdc sections
807
808 // Check the last section was trimmed to
809 // something a bit less that 17000.
810 EXPECT_GT(ud->header().size, 14000);
811 EXPECT_LT(ud->header().size, 16000);
812
813 fs::remove_all(dir);
814}
Matt Spinler0a90a852020-06-04 13:18:27 -0500815
816// Create a PEL with device callouts
817TEST_F(PELTest, CreateWithDevCalloutsTest)
818{
819 message::Entry regEntry;
820 uint64_t timestamp = 5;
821
822 regEntry.name = "test";
823 regEntry.subsystem = 5;
824 regEntry.actionFlags = 0xC000;
825 regEntry.src.type = 0xBD;
826 regEntry.src.reasonCode = 0x1234;
827
828 NiceMock<MockDataInterface> dataIface;
829 PelFFDC ffdc;
830
831 const auto calloutJSON = R"(
832 {
833 "I2C":
834 {
835 "14":
836 {
837 "114":
838 {
839 "Callouts":[
840 {
841 "Name": "/chassis/motherboard/cpu0",
842 "LocationCode": "P1",
843 "Priority": "H"
844 }
845 ],
846 "Dest": "proc 0 target"
847 }
848 }
849 }
850 })";
851
852 std::vector<std::string> names{"systemA"};
853 EXPECT_CALL(dataIface, getSystemNames)
854 .Times(2)
Matt Spinler1ab66962020-10-29 13:21:44 -0500855 .WillRepeatedly(Return(names));
Matt Spinler0a90a852020-06-04 13:18:27 -0500856
Matt Spinler0d92b522021-06-16 13:28:17 -0600857 EXPECT_CALL(dataIface, expandLocationCode("P1", 0))
858 .Times(1)
Matt Spinler0a90a852020-06-04 13:18:27 -0500859 .WillOnce(Return("UXXX-P1"));
860
Matt Spinler2f9225a2020-08-05 12:58:49 -0500861 EXPECT_CALL(dataIface, getInventoryFromLocCode("P1", 0, false))
Matt Spinler0a90a852020-06-04 13:18:27 -0500862 .WillOnce(
863 Return("/xyz/openbmc_project/inventory/chassis/motherboard/cpu0"));
864
865 EXPECT_CALL(
866 dataIface,
867 getHWCalloutFields(
868 "/xyz/openbmc_project/inventory/chassis/motherboard/cpu0", _, _, _))
869 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
870 SetArgReferee<3>("123456789ABC")));
871
Sumit Kumar9d43a722021-08-24 09:46:19 -0500872 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
873 "system/entry"};
874 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
875 .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
876
Matt Spinler0a90a852020-06-04 13:18:27 -0500877 auto dataPath = getPELReadOnlyDataPath();
878 std::ofstream file{dataPath / "systemA_dev_callouts.json"};
879 file << calloutJSON;
880 file.close();
881
882 {
883 std::vector<std::string> data{
884 "CALLOUT_ERRNO=5",
885 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
886 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0072"};
887
888 AdditionalData ad{data};
889
890 PEL pel{
891 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
892 ad, ffdc, dataIface};
893
894 ASSERT_TRUE(pel.primarySRC().value()->callouts());
895 auto& callouts = pel.primarySRC().value()->callouts()->callouts();
896 ASSERT_EQ(callouts.size(), 1);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500897 ASSERT_TRUE(pel.isCalloutPresent());
Matt Spinler0a90a852020-06-04 13:18:27 -0500898
899 EXPECT_EQ(callouts[0]->priority(), 'H');
900 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P1");
901
902 auto& fru = callouts[0]->fruIdentity();
903 EXPECT_EQ(fru->getPN().value(), "1234567");
904 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
905 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
906
907 const auto& section = pel.optionalSections().back();
908
909 ASSERT_EQ(section->header().id, 0x5544); // UD
910 auto ud = static_cast<UserData*>(section.get());
911
912 // Check that there was a UserData section added that
913 // contains debug details about the device.
914 const auto& d = ud->data();
915 std::string jsonString{d.begin(), d.end()};
916 auto actualJSON = nlohmann::json::parse(jsonString);
917
918 auto expectedJSON = R"(
919 {
920 "PEL Internal Debug Data": {
921 "SRC": [
922 "I2C: bus: 14 address: 114 dest: proc 0 target"
923 ]
924 }
925 }
926 )"_json;
927
928 EXPECT_EQ(actualJSON, expectedJSON);
929 }
930
931 {
932 // Device path not found (wrong i2c addr), so no callouts
933 std::vector<std::string> data{
934 "CALLOUT_ERRNO=5",
935 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
936 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0099"};
937
938 AdditionalData ad{data};
939
940 PEL pel{
941 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
942 ad, ffdc, dataIface};
943
944 // no callouts
945 EXPECT_FALSE(pel.primarySRC().value()->callouts());
946
947 // Now check that there was a UserData section
948 // that contains the lookup error.
949 const auto& section = pel.optionalSections().back();
950
951 ASSERT_EQ(section->header().id, 0x5544); // UD
952 auto ud = static_cast<UserData*>(section.get());
953
954 const auto& d = ud->data();
955
956 std::string jsonString{d.begin(), d.end()};
957
958 auto actualJSON = nlohmann::json::parse(jsonString);
959
960 auto expectedJSON =
961 "{\"PEL Internal Debug Data\":{\"SRC\":"
962 "[\"Problem looking up I2C callouts on 14 153: "
963 "[json.exception.out_of_range.403] key '153' not found\"]}}"_json;
964
965 EXPECT_EQ(actualJSON, expectedJSON);
966 }
967
968 fs::remove_all(dataPath);
969}
Matt Spinlere513dbc2020-08-27 11:14:17 -0500970
971// Test PELs when the callouts are passed in using a JSON file.
972TEST_F(PELTest, CreateWithJSONCalloutsTest)
973{
974 PelFFDCfile ffdcFile;
975 ffdcFile.format = UserDataFormat::json;
976 ffdcFile.subType = 0xCA; // Callout JSON
977 ffdcFile.version = 1;
978
979 // Write these callouts to a JSON file and pass it into
980 // the PEL as an FFDC file.
981 auto inputJSON = R"([
982 {
983 "Priority": "H",
984 "LocationCode": "P0-C1"
985 },
986 {
987 "Priority": "M",
988 "Procedure": "PROCEDURE"
989 }
990 ])"_json;
991
992 auto s = inputJSON.dump();
993 std::vector<uint8_t> data{s.begin(), s.end()};
994 auto dir = makeTempDir();
995 ffdcFile.fd = writeFileAndGetFD(dir, data);
996
997 PelFFDC ffdc;
998 ffdc.push_back(std::move(ffdcFile));
999
1000 AdditionalData ad;
1001 NiceMock<MockDataInterface> dataIface;
1002
1003 EXPECT_CALL(dataIface, expandLocationCode("P0-C1", 0))
1004 .Times(1)
1005 .WillOnce(Return("UXXX-P0-C1"));
1006 EXPECT_CALL(dataIface, getInventoryFromLocCode("P0-C1", 0, false))
1007 .Times(1)
1008 .WillOnce(Return("/inv/system/chassis/motherboard/bmc"));
1009 EXPECT_CALL(dataIface, getHWCalloutFields(
1010 "/inv/system/chassis/motherboard/bmc", _, _, _))
1011 .Times(1)
1012 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
1013 SetArgReferee<3>("123456789ABC")));
1014
Sumit Kumar9d43a722021-08-24 09:46:19 -05001015 std::vector<std::string> dumpType{"bmc/entry", "resource/entry",
1016 "system/entry"};
1017 EXPECT_CALL(dataIface, checkDumpStatus(dumpType))
1018 .WillOnce(Return(std::vector<bool>{false, false, false}));
1019
Matt Spinlere513dbc2020-08-27 11:14:17 -05001020 message::Entry regEntry;
1021 regEntry.name = "test";
1022 regEntry.subsystem = 5;
1023 regEntry.actionFlags = 0xC000;
1024 regEntry.src.type = 0xBD;
1025 regEntry.src.reasonCode = 0x1234;
1026
1027 PEL pel{regEntry, 42, 5, phosphor::logging::Entry::Level::Error,
1028 ad, ffdc, dataIface};
1029
1030 ASSERT_TRUE(pel.valid());
1031 ASSERT_TRUE(pel.primarySRC().value()->callouts());
1032 const auto& callouts = pel.primarySRC().value()->callouts()->callouts();
1033 ASSERT_EQ(callouts.size(), 2);
1034
1035 {
1036 EXPECT_EQ(callouts[0]->priority(), 'H');
1037 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P0-C1");
1038
1039 auto& fru = callouts[0]->fruIdentity();
1040 EXPECT_EQ(fru->getPN().value(), "1234567");
1041 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
1042 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
1043 EXPECT_EQ(fru->failingComponentType(), src::FRUIdentity::hardwareFRU);
1044 }
1045 {
1046 EXPECT_EQ(callouts[1]->priority(), 'M');
1047 EXPECT_EQ(callouts[1]->locationCode(), "");
1048
1049 auto& fru = callouts[1]->fruIdentity();
1050 EXPECT_EQ(fru->getMaintProc().value(), "PROCEDU");
1051 EXPECT_EQ(fru->failingComponentType(),
1052 src::FRUIdentity::maintenanceProc);
1053 }
1054 fs::remove_all(dir);
1055}