blob: 3fa3d55ba9dd03d9b640b0427d5c0e04a7910ab2 [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::_;
Matt Spinler56ad2a02020-03-26 14:00:52 -050030using ::testing::NiceMock;
Matt Spinler677381b2020-01-23 10:04:29 -060031using ::testing::Return;
Matt Spinler0a90a852020-06-04 13:18:27 -050032using ::testing::SetArgReferee;
Matt Spinlercb6b0592019-07-16 15:58:51 -050033
34class PELTest : public CleanLogID
35{
36};
37
Matt Spinler5b289b22020-03-26 14:27:19 -050038fs::path makeTempDir()
39{
40 char path[] = "/tmp/tempdirXXXXXX";
41 std::filesystem::path dir = mkdtemp(path);
42 return dir;
43}
44
45int writeFileAndGetFD(const fs::path& dir, const std::vector<uint8_t>& data)
46{
47 static size_t count = 0;
48 fs::path path = dir / (std::string{"file"} + std::to_string(count));
49 std::ofstream stream{path};
50 count++;
51
52 stream.write(reinterpret_cast<const char*>(data.data()), data.size());
53 stream.close();
54
55 FILE* fp = fopen(path.c_str(), "r");
56 return fileno(fp);
57}
58
Matt Spinlercb6b0592019-07-16 15:58:51 -050059TEST_F(PELTest, FlattenTest)
60{
Matt Spinler42828bd2019-10-11 10:39:30 -050061 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler42828bd2019-10-11 10:39:30 -050062 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050063
64 // Check a few fields
65 EXPECT_TRUE(pel->valid());
66 EXPECT_EQ(pel->id(), 0x80818283);
67 EXPECT_EQ(pel->plid(), 0x50515253);
Matt Spinler97d19b42019-10-29 11:34:03 -050068 EXPECT_EQ(pel->userHeader().subsystem(), 0x10);
69 EXPECT_EQ(pel->userHeader().actionFlags(), 0x80C0);
Matt Spinlercb6b0592019-07-16 15:58:51 -050070
71 // Test that data in == data out
72 auto flattenedData = pel->data();
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060073 EXPECT_EQ(data, flattenedData);
74 EXPECT_EQ(flattenedData.size(), pel->size());
Matt Spinlercb6b0592019-07-16 15:58:51 -050075}
76
77TEST_F(PELTest, CommitTimeTest)
78{
Matt Spinler42828bd2019-10-11 10:39:30 -050079 auto data = pelDataFactory(TestPELType::pelSimple);
80 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050081
82 auto origTime = pel->commitTime();
83 pel->setCommitTime();
84 auto newTime = pel->commitTime();
85
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060086 EXPECT_NE(origTime, newTime);
Matt Spinlercb6b0592019-07-16 15:58:51 -050087
88 // Make a new PEL and check new value is still there
89 auto newData = pel->data();
90 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060091 EXPECT_EQ(newTime, newPel->commitTime());
Matt Spinlercb6b0592019-07-16 15:58:51 -050092}
93
94TEST_F(PELTest, AssignIDTest)
95{
Matt Spinler42828bd2019-10-11 10:39:30 -050096 auto data = pelDataFactory(TestPELType::pelSimple);
97 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050098
99 auto origID = pel->id();
100 pel->assignID();
101 auto newID = pel->id();
102
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600103 EXPECT_NE(origID, newID);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500104
105 // Make a new PEL and check new value is still there
106 auto newData = pel->data();
107 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -0600108 EXPECT_EQ(newID, newPel->id());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500109}
110
111TEST_F(PELTest, WithLogIDTest)
112{
Matt Spinler42828bd2019-10-11 10:39:30 -0500113 auto data = pelDataFactory(TestPELType::pelSimple);
114 auto pel = std::make_unique<PEL>(data, 0x42);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500115
116 EXPECT_TRUE(pel->valid());
117 EXPECT_EQ(pel->obmcLogID(), 0x42);
118}
119
120TEST_F(PELTest, InvalidPELTest)
121{
Matt Spinler42828bd2019-10-11 10:39:30 -0500122 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500123
124 // Too small
Matt Spinler42828bd2019-10-11 10:39:30 -0500125 data.resize(PrivateHeader::flattenedSize());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500126
Matt Spinler42828bd2019-10-11 10:39:30 -0500127 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500128
Matt Spinler97d19b42019-10-29 11:34:03 -0500129 EXPECT_TRUE(pel->privateHeader().valid());
130 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500131 EXPECT_FALSE(pel->valid());
132
Matt Spinlercb6b0592019-07-16 15:58:51 -0500133 // Now corrupt the private header
Matt Spinler42828bd2019-10-11 10:39:30 -0500134 data = pelDataFactory(TestPELType::pelSimple);
135 data.at(0) = 0;
136 pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500137
Matt Spinler97d19b42019-10-29 11:34:03 -0500138 EXPECT_FALSE(pel->privateHeader().valid());
139 EXPECT_TRUE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500140 EXPECT_FALSE(pel->valid());
141}
142
143TEST_F(PELTest, EmptyDataTest)
144{
145 std::vector<uint8_t> data;
146 auto pel = std::make_unique<PEL>(data);
147
Matt Spinler97d19b42019-10-29 11:34:03 -0500148 EXPECT_FALSE(pel->privateHeader().valid());
149 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500150 EXPECT_FALSE(pel->valid());
151}
Matt Spinlerb8323632019-09-20 15:11:04 -0500152
153TEST_F(PELTest, CreateFromRegistryTest)
154{
155 message::Entry regEntry;
156 uint64_t timestamp = 5;
157
158 regEntry.name = "test";
159 regEntry.subsystem = 5;
160 regEntry.actionFlags = 0xC000;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500161 regEntry.src.type = 0xBD;
162 regEntry.src.reasonCode = 0x1234;
Matt Spinlerb8323632019-09-20 15:11:04 -0500163
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600164 std::vector<std::string> data{"KEY1=VALUE1"};
165 AdditionalData ad{data};
Matt Spinler56ad2a02020-03-26 14:00:52 -0500166 NiceMock<MockDataInterface> dataIface;
167 PelFFDC ffdc;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500168
Matt Spinler56ad2a02020-03-26 14:00:52 -0500169 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
170 ad, ffdc, dataIface};
Matt Spinlerb8323632019-09-20 15:11:04 -0500171
172 EXPECT_TRUE(pel.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -0500173 EXPECT_EQ(pel.privateHeader().obmcLogID(), 42);
174 EXPECT_EQ(pel.userHeader().severity(), 0x40);
Matt Spinlerb8323632019-09-20 15:11:04 -0500175
Matt Spinlerbd716f02019-10-15 10:54:11 -0500176 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
177 "BD051234 ");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600178
179 // Check that certain optional sections have been created
180 size_t mtmsCount = 0;
181 size_t euhCount = 0;
182 size_t udCount = 0;
183
184 for (const auto& section : pel.optionalSections())
185 {
186 if (section->header().id ==
187 static_cast<uint16_t>(SectionID::failingMTMS))
188 {
189 mtmsCount++;
190 }
191 else if (section->header().id ==
192 static_cast<uint16_t>(SectionID::extendedUserHeader))
193 {
194 euhCount++;
195 }
196 else if (section->header().id ==
197 static_cast<uint16_t>(SectionID::userData))
198 {
199 udCount++;
200 }
201 }
202
203 EXPECT_EQ(mtmsCount, 1);
204 EXPECT_EQ(euhCount, 1);
205 EXPECT_EQ(udCount, 2); // AD section and sysInfo section
Andrew Geissler44fc3162020-07-09 09:21:31 -0500206 ASSERT_FALSE(pel.isCalloutPresent());
Matt Spinler1f93c592020-09-10 10:43:08 -0500207
208 {
209 // The same thing, but without the action flags specified
210 // in the registry, so the constructor should set them.
211 regEntry.actionFlags = std::nullopt;
212
213 PEL pel2{
214 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
215 ad, ffdc, dataIface};
216
217 EXPECT_EQ(pel2.userHeader().actionFlags(), 0xA800);
218 }
Matt Spinlerb8323632019-09-20 15:11:04 -0500219}
Matt Spinler131870c2019-09-25 13:29:04 -0500220
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500221// Test that when the AdditionalData size is over 16KB that
222// the PEL that's created is exactly 16KB since the UserData
223// section that contains all that data was pruned.
224TEST_F(PELTest, CreateTooBigADTest)
225{
226 message::Entry regEntry;
227 uint64_t timestamp = 5;
228
229 regEntry.name = "test";
230 regEntry.subsystem = 5;
231 regEntry.actionFlags = 0xC000;
232 regEntry.src.type = 0xBD;
233 regEntry.src.reasonCode = 0x1234;
Matt Spinler56ad2a02020-03-26 14:00:52 -0500234 PelFFDC ffdc;
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500235
236 // Over the 16KB max PEL size
237 std::string bigAD{"KEY1="};
238 bigAD += std::string(17000, 'G');
239
240 std::vector<std::string> data{bigAD};
241 AdditionalData ad{data};
Matt Spinler56ad2a02020-03-26 14:00:52 -0500242 NiceMock<MockDataInterface> dataIface;
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500243
Matt Spinler56ad2a02020-03-26 14:00:52 -0500244 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
245 ad, ffdc, dataIface};
Matt Spinler9b7e94f2020-03-24 15:44:41 -0500246
247 EXPECT_TRUE(pel.valid());
248 EXPECT_EQ(pel.size(), 16384);
249
250 // Make sure that there are still 2 UD sections.
251 size_t udCount = 0;
252 for (const auto& section : pel.optionalSections())
253 {
254 if (section->header().id == static_cast<uint16_t>(SectionID::userData))
255 {
256 udCount++;
257 }
258 }
259
260 EXPECT_EQ(udCount, 2); // AD section and sysInfo section
261}
262
Matt Spinler131870c2019-09-25 13:29:04 -0500263// Test that we'll create Generic optional sections for sections that
264// there aren't explicit classes for.
265TEST_F(PELTest, GenericSectionTest)
266{
Matt Spinler42828bd2019-10-11 10:39:30 -0500267 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500268
269 std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
270 0x00, 0x18, // Size
271 0x01, 0x02, // version, subtype
272 0x03, 0x04, // comp ID
273
274 // some data
275 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
276 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
277 0x00};
278
279 std::vector<uint8_t> section2{
280 0x59, 0x59, // ID 'YY'
281 0x00, 0x20, // Size
282 0x01, 0x02, // version, subtype
283 0x03, 0x04, // comp ID
284
285 // some data
286 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
287 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
288
289 // Add the new sections at the end
Matt Spinler42828bd2019-10-11 10:39:30 -0500290 data.insert(data.end(), section1.begin(), section1.end());
291 data.insert(data.end(), section2.begin(), section2.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500292
293 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500294 data.at(27) += 2;
295 auto origData = data;
Matt Spinler131870c2019-09-25 13:29:04 -0500296
Matt Spinler42828bd2019-10-11 10:39:30 -0500297 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500298
299 const auto& sections = pel.optionalSections();
300
301 bool foundXX = false;
302 bool foundYY = false;
303
304 // Check that we can find these 2 Generic sections
305 for (const auto& section : sections)
306 {
307 if (section->header().id == 0x5858)
308 {
309 foundXX = true;
310 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
311 }
312 else if (section->header().id == 0x5959)
313 {
314 foundYY = true;
315 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
316 }
317 }
318
319 EXPECT_TRUE(foundXX);
320 EXPECT_TRUE(foundYY);
Matt Spinler07eefc52019-09-26 11:18:26 -0500321
322 // Now flatten and check
323 auto newData = pel.data();
324
325 EXPECT_EQ(origData, newData);
Matt Spinler131870c2019-09-25 13:29:04 -0500326}
327
328// Test that an invalid section will still get a Generic object
329TEST_F(PELTest, InvalidGenericTest)
330{
Matt Spinler42828bd2019-10-11 10:39:30 -0500331 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500332
333 // Not a valid section
334 std::vector<uint8_t> section1{0x01, 0x02, 0x03};
335
Matt Spinler42828bd2019-10-11 10:39:30 -0500336 data.insert(data.end(), section1.begin(), section1.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500337
338 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500339 data.at(27) += 1;
Matt Spinler131870c2019-09-25 13:29:04 -0500340
Matt Spinler42828bd2019-10-11 10:39:30 -0500341 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500342 EXPECT_FALSE(pel.valid());
343
344 const auto& sections = pel.optionalSections();
345
346 bool foundGeneric = false;
347 for (const auto& section : sections)
348 {
349 if (dynamic_cast<Generic*>(section.get()) != nullptr)
350 {
351 foundGeneric = true;
352 EXPECT_EQ(section->valid(), false);
353 break;
354 }
355 }
356
357 EXPECT_TRUE(foundGeneric);
358}
Matt Spinlerafa857c2019-10-24 13:03:46 -0500359
360// Create a UserData section out of AdditionalData
361TEST_F(PELTest, MakeUDSectionTest)
362{
363 std::vector<std::string> ad{"KEY1=VALUE1", "KEY2=VALUE2", "KEY3=VALUE3",
364 "ESEL=TEST"};
365 AdditionalData additionalData{ad};
366
367 auto ud = util::makeADUserDataSection(additionalData);
368
369 EXPECT_TRUE(ud->valid());
370 EXPECT_EQ(ud->header().id, 0x5544);
371 EXPECT_EQ(ud->header().version, 0x01);
372 EXPECT_EQ(ud->header().subType, 0x01);
373 EXPECT_EQ(ud->header().componentID, 0x2000);
374
375 const auto& d = ud->data();
376
377 std::string jsonString{d.begin(), d.end()};
Matt Spinler53407be2019-11-18 09:16:31 -0600378
379 std::string expectedJSON =
Matt Spinlerafa857c2019-10-24 13:03:46 -0500380 R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})";
Matt Spinler53407be2019-11-18 09:16:31 -0600381
382 // The actual data is null padded to a 4B boundary.
383 std::vector<uint8_t> expectedData;
384 expectedData.resize(52, '\0');
385 memcpy(expectedData.data(), expectedJSON.data(), expectedJSON.size());
386
387 EXPECT_EQ(d, expectedData);
Matt Spinlerafa857c2019-10-24 13:03:46 -0500388
389 // Ensure we can read this as JSON
390 auto newJSON = nlohmann::json::parse(jsonString);
391 EXPECT_EQ(newJSON["KEY1"], "VALUE1");
392 EXPECT_EQ(newJSON["KEY2"], "VALUE2");
393 EXPECT_EQ(newJSON["KEY3"], "VALUE3");
Matt Spinler97d19b42019-10-29 11:34:03 -0500394}
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600395
396// Create the UserData section that contains system info
Matt Spinler677381b2020-01-23 10:04:29 -0600397TEST_F(PELTest, SysInfoSectionTest)
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600398{
399 MockDataInterface dataIface;
400
Matt Spinler677381b2020-01-23 10:04:29 -0600401 EXPECT_CALL(dataIface, getBMCFWVersionID()).WillOnce(Return("ABCD1234"));
Matt Spinler4aa23a12020-02-03 15:05:09 -0600402 EXPECT_CALL(dataIface, getBMCState()).WillOnce(Return("State.Ready"));
403 EXPECT_CALL(dataIface, getChassisState()).WillOnce(Return("State.On"));
404 EXPECT_CALL(dataIface, getHostState()).WillOnce(Return("State.Off"));
Ben Tynere32b7e72021-05-18 12:38:40 -0500405 EXPECT_CALL(dataIface, getSystemIMKeyword())
406 .WillOnce(Return(std::vector<uint8_t>{0, 1, 0x55, 0xAA}));
Matt Spinler677381b2020-01-23 10:04:29 -0600407
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600408 std::string pid = "_PID=" + std::to_string(getpid());
409 std::vector<std::string> ad{pid};
410 AdditionalData additionalData{ad};
411
412 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
413
414 EXPECT_TRUE(ud->valid());
415 EXPECT_EQ(ud->header().id, 0x5544);
416 EXPECT_EQ(ud->header().version, 0x01);
417 EXPECT_EQ(ud->header().subType, 0x01);
418 EXPECT_EQ(ud->header().componentID, 0x2000);
419
420 // Pull out the JSON data and check it.
421 const auto& d = ud->data();
422 std::string jsonString{d.begin(), d.end()};
423 auto json = nlohmann::json::parse(jsonString);
424
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500425 // Ensure the 'Process Name' entry contains the name of this test
426 // executable.
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600427 auto name = json["Process Name"].get<std::string>();
Patrick Williamsd9f0d642021-04-21 15:43:21 -0500428 auto found = (name.find("pel_test") != std::string::npos) ||
429 (name.find("test-openpower-pels-pel") != std::string::npos);
430 EXPECT_TRUE(found);
431 // @TODO(stwcx): remove 'pel_test' when removing autotools.
Matt Spinler677381b2020-01-23 10:04:29 -0600432
Matt Spinlerc2b8a512021-05-21 12:44:42 -0600433 auto version = json["FW Version ID"].get<std::string>();
Matt Spinler677381b2020-01-23 10:04:29 -0600434 EXPECT_EQ(version, "ABCD1234");
Matt Spinler4aa23a12020-02-03 15:05:09 -0600435
436 auto state = json["BMCState"].get<std::string>();
437 EXPECT_EQ(state, "Ready");
438
439 state = json["ChassisState"].get<std::string>();
440 EXPECT_EQ(state, "On");
441
442 state = json["HostState"].get<std::string>();
443 EXPECT_EQ(state, "Off");
Ben Tynere32b7e72021-05-18 12:38:40 -0500444
445 auto keyword = json["System IM"].get<std::string>();
446 EXPECT_EQ(keyword, "000155AA");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600447}
Matt Spinlerce3f4502020-01-22 15:44:35 -0600448
449// Test that the sections that override
450// virtual std::optional<std::string> Section::getJSON() const
451// return valid JSON.
452TEST_F(PELTest, SectionJSONTest)
453{
454 auto data = pelDataFactory(TestPELType::pelSimple);
455 PEL pel{data};
456
457 // Check that all JSON returned from the sections is
458 // parseable by nlohmann::json, which will throw an
459 // exception and fail the test if there is a problem.
460
461 // The getJSON() response needs to be wrapped in a { } to make
462 // actual valid JSON (PEL::toJSON() usually handles that).
463
464 auto jsonString = pel.privateHeader().getJSON();
465
466 // PrivateHeader always prints JSON
467 ASSERT_TRUE(jsonString);
468 *jsonString = '{' + *jsonString + '}';
469 auto json = nlohmann::json::parse(*jsonString);
470
471 jsonString = pel.userHeader().getJSON();
472
473 // UserHeader always prints JSON
474 ASSERT_TRUE(jsonString);
475 *jsonString = '{' + *jsonString + '}';
476 json = nlohmann::json::parse(*jsonString);
477
478 for (const auto& section : pel.optionalSections())
479 {
480 // The optional sections may or may not have implemented getJSON().
481 jsonString = section->getJSON();
482 if (jsonString)
483 {
484 *jsonString = '{' + *jsonString + '}';
485 auto json = nlohmann::json::parse(*jsonString);
486 }
487 }
488}
Matt Spinler5b289b22020-03-26 14:27:19 -0500489
490PelFFDCfile getJSONFFDC(const fs::path& dir)
491{
492 PelFFDCfile ffdc;
493 ffdc.format = UserDataFormat::json;
494 ffdc.subType = 5;
495 ffdc.version = 42;
496
497 auto inputJSON = R"({
498 "key1": "value1",
499 "key2": 42,
500 "key3" : [1, 2, 3, 4, 5],
501 "key4": {"key5": "value5"}
502 })"_json;
503
504 // Write the JSON to a file and get its descriptor.
505 auto s = inputJSON.dump();
506 std::vector<uint8_t> data{s.begin(), s.end()};
507 ffdc.fd = writeFileAndGetFD(dir, data);
508
509 return ffdc;
510}
511
512TEST_F(PELTest, MakeJSONFileUDSectionTest)
513{
514 auto dir = makeTempDir();
515
516 {
517 auto ffdc = getJSONFFDC(dir);
518
519 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
520 close(ffdc.fd);
521 ASSERT_TRUE(ud);
522 ASSERT_TRUE(ud->valid());
523 EXPECT_EQ(ud->header().id, 0x5544);
524
525 EXPECT_EQ(ud->header().version,
526 static_cast<uint8_t>(UserDataFormatVersion::json));
527 EXPECT_EQ(ud->header().subType,
528 static_cast<uint8_t>(UserDataFormat::json));
529 EXPECT_EQ(ud->header().componentID,
530 static_cast<uint16_t>(ComponentID::phosphorLogging));
531
532 // Pull the JSON back out of the the UserData section
533 const auto& d = ud->data();
534 std::string js{d.begin(), d.end()};
535 auto json = nlohmann::json::parse(js);
536
537 EXPECT_EQ("value1", json["key1"].get<std::string>());
538 EXPECT_EQ(42, json["key2"].get<int>());
539
540 std::vector<int> key3Values{1, 2, 3, 4, 5};
541 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
542
543 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
544 auto actual = json["key4"].get<std::map<std::string, std::string>>();
545 EXPECT_EQ(key4Values, actual);
546 }
547
548 {
549 // A bad FD
550 PelFFDCfile ffdc;
551 ffdc.format = UserDataFormat::json;
552 ffdc.subType = 5;
553 ffdc.version = 42;
554 ffdc.fd = 10000;
555
556 // The section shouldn't get made
557 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
558 ASSERT_FALSE(ud);
559 }
560
561 fs::remove_all(dir);
562}
563
564PelFFDCfile getCBORFFDC(const fs::path& dir)
565{
566 PelFFDCfile ffdc;
567 ffdc.format = UserDataFormat::cbor;
568 ffdc.subType = 5;
569 ffdc.version = 42;
570
571 auto inputJSON = R"({
572 "key1": "value1",
573 "key2": 42,
574 "key3" : [1, 2, 3, 4, 5],
575 "key4": {"key5": "value5"}
576 })"_json;
577
578 // Convert the JSON to CBOR and write it to a file
579 auto data = nlohmann::json::to_cbor(inputJSON);
580 ffdc.fd = writeFileAndGetFD(dir, data);
581
582 return ffdc;
583}
584
585TEST_F(PELTest, MakeCBORFileUDSectionTest)
586{
587 auto dir = makeTempDir();
588
589 auto ffdc = getCBORFFDC(dir);
590 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
591 close(ffdc.fd);
592 ASSERT_TRUE(ud);
593 ASSERT_TRUE(ud->valid());
594 EXPECT_EQ(ud->header().id, 0x5544);
595
596 EXPECT_EQ(ud->header().version,
597 static_cast<uint8_t>(UserDataFormatVersion::cbor));
598 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::cbor));
599 EXPECT_EQ(ud->header().componentID,
600 static_cast<uint16_t>(ComponentID::phosphorLogging));
601
602 // Pull the CBOR back out of the PEL section
603 // The number of pad bytes to make the section be 4B aligned
604 // was added at the end, read it and then remove it and the
605 // padding before parsing it.
606 auto data = ud->data();
607 Stream stream{data};
608 stream.offset(data.size() - 4);
609 uint32_t pad;
610 stream >> pad;
611
612 data.resize(data.size() - 4 - pad);
613
614 auto json = nlohmann::json::from_cbor(data);
615
616 EXPECT_EQ("value1", json["key1"].get<std::string>());
617 EXPECT_EQ(42, json["key2"].get<int>());
618
619 std::vector<int> key3Values{1, 2, 3, 4, 5};
620 EXPECT_EQ(key3Values, json["key3"].get<std::vector<int>>());
621
622 std::map<std::string, std::string> key4Values{{"key5", "value5"}};
623 auto actual = json["key4"].get<std::map<std::string, std::string>>();
624 EXPECT_EQ(key4Values, actual);
625
626 fs::remove_all(dir);
627}
628
629PelFFDCfile getTextFFDC(const fs::path& dir)
630{
631 PelFFDCfile ffdc;
632 ffdc.format = UserDataFormat::text;
633 ffdc.subType = 5;
634 ffdc.version = 42;
635
636 std::string text{"this is some text that will be used for FFDC"};
637 std::vector<uint8_t> data{text.begin(), text.end()};
638
639 ffdc.fd = writeFileAndGetFD(dir, data);
640
641 return ffdc;
642}
643
644TEST_F(PELTest, MakeTextFileUDSectionTest)
645{
646 auto dir = makeTempDir();
647
648 auto ffdc = getTextFFDC(dir);
649 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
650 close(ffdc.fd);
651 ASSERT_TRUE(ud);
652 ASSERT_TRUE(ud->valid());
653 EXPECT_EQ(ud->header().id, 0x5544);
654
655 EXPECT_EQ(ud->header().version,
656 static_cast<uint8_t>(UserDataFormatVersion::text));
657 EXPECT_EQ(ud->header().subType, static_cast<uint8_t>(UserDataFormat::text));
658 EXPECT_EQ(ud->header().componentID,
659 static_cast<uint16_t>(ComponentID::phosphorLogging));
660
661 // Get the text back out
662 std::string text{ud->data().begin(), ud->data().end()};
663 EXPECT_EQ(text, "this is some text that will be used for FFDC");
664
665 fs::remove_all(dir);
666}
667
668PelFFDCfile getCustomFFDC(const fs::path& dir, const std::vector<uint8_t>& data)
669{
670 PelFFDCfile ffdc;
671 ffdc.format = UserDataFormat::custom;
672 ffdc.subType = 5;
673 ffdc.version = 42;
674
675 ffdc.fd = writeFileAndGetFD(dir, data);
676
677 return ffdc;
678}
679
680TEST_F(PELTest, MakeCustomFileUDSectionTest)
681{
682 auto dir = makeTempDir();
683
684 {
685 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8};
686
687 auto ffdc = getCustomFFDC(dir, data);
688 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
689 close(ffdc.fd);
690 ASSERT_TRUE(ud);
691 ASSERT_TRUE(ud->valid());
692 EXPECT_EQ(ud->header().size, 8 + 8); // data size + header size
693 EXPECT_EQ(ud->header().id, 0x5544);
694
695 EXPECT_EQ(ud->header().version, 42);
696 EXPECT_EQ(ud->header().subType, 5);
697 EXPECT_EQ(ud->header().componentID, 0x2002);
698
699 // Get the data back out
700 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
701 EXPECT_EQ(data, newData);
702 }
703
704 // Do the same thing again, but make it be non 4B aligned
705 // so the data gets padded.
706 {
707 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8, 9};
708
709 auto ffdc = getCustomFFDC(dir, data);
710 auto ud = util::makeFFDCuserDataSection(0x2002, ffdc);
711 close(ffdc.fd);
712 ASSERT_TRUE(ud);
713 ASSERT_TRUE(ud->valid());
714 EXPECT_EQ(ud->header().size, 12 + 8); // data size + header size
715 EXPECT_EQ(ud->header().id, 0x5544);
716
717 EXPECT_EQ(ud->header().version, 42);
718 EXPECT_EQ(ud->header().subType, 5);
719 EXPECT_EQ(ud->header().componentID, 0x2002);
720
721 // Get the data back out
722 std::vector<uint8_t> newData{ud->data().begin(), ud->data().end()};
723
724 // pad the original to 12B so we can compare
725 data.push_back(0);
726 data.push_back(0);
727 data.push_back(0);
728
729 EXPECT_EQ(data, newData);
730 }
731
732 fs::remove_all(dir);
733}
734
735// Test Adding FFDC from files to a PEL
736TEST_F(PELTest, CreateWithFFDCTest)
737{
738 auto dir = makeTempDir();
739 message::Entry regEntry;
740 uint64_t timestamp = 5;
741
742 regEntry.name = "test";
743 regEntry.subsystem = 5;
744 regEntry.actionFlags = 0xC000;
745 regEntry.src.type = 0xBD;
746 regEntry.src.reasonCode = 0x1234;
747
748 std::vector<std::string> additionalData{"KEY1=VALUE1"};
749 AdditionalData ad{additionalData};
750 NiceMock<MockDataInterface> dataIface;
751 PelFFDC ffdc;
752
753 std::vector<uint8_t> customData{1, 2, 3, 4, 5, 6, 7, 8};
754
755 // This will be trimmed when added
756 std::vector<uint8_t> hugeCustomData(17000, 0x42);
757
758 ffdc.emplace_back(std::move(getJSONFFDC(dir)));
759 ffdc.emplace_back(std::move(getCBORFFDC(dir)));
760 ffdc.emplace_back(std::move(getTextFFDC(dir)));
761 ffdc.emplace_back(std::move(getCustomFFDC(dir, customData)));
762 ffdc.emplace_back(std::move(getCustomFFDC(dir, hugeCustomData)));
763
764 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
765 ad, ffdc, dataIface};
766
767 EXPECT_TRUE(pel.valid());
768
769 // Clipped to the max
770 EXPECT_EQ(pel.size(), 16384);
771
772 // Check for the FFDC sections
773 size_t udCount = 0;
774 Section* ud = nullptr;
775
776 for (const auto& section : pel.optionalSections())
777 {
778 if (section->header().id == static_cast<uint16_t>(SectionID::userData))
779 {
780 udCount++;
781 ud = section.get();
782 }
783 }
784
785 EXPECT_EQ(udCount, 7); // AD section, sysInfo, 5 ffdc sections
786
787 // Check the last section was trimmed to
788 // something a bit less that 17000.
789 EXPECT_GT(ud->header().size, 14000);
790 EXPECT_LT(ud->header().size, 16000);
791
792 fs::remove_all(dir);
793}
Matt Spinler0a90a852020-06-04 13:18:27 -0500794
795// Create a PEL with device callouts
796TEST_F(PELTest, CreateWithDevCalloutsTest)
797{
798 message::Entry regEntry;
799 uint64_t timestamp = 5;
800
801 regEntry.name = "test";
802 regEntry.subsystem = 5;
803 regEntry.actionFlags = 0xC000;
804 regEntry.src.type = 0xBD;
805 regEntry.src.reasonCode = 0x1234;
806
807 NiceMock<MockDataInterface> dataIface;
808 PelFFDC ffdc;
809
810 const auto calloutJSON = R"(
811 {
812 "I2C":
813 {
814 "14":
815 {
816 "114":
817 {
818 "Callouts":[
819 {
820 "Name": "/chassis/motherboard/cpu0",
821 "LocationCode": "P1",
822 "Priority": "H"
823 }
824 ],
825 "Dest": "proc 0 target"
826 }
827 }
828 }
829 })";
830
831 std::vector<std::string> names{"systemA"};
832 EXPECT_CALL(dataIface, getSystemNames)
833 .Times(2)
Matt Spinler1ab66962020-10-29 13:21:44 -0500834 .WillRepeatedly(Return(names));
Matt Spinler0a90a852020-06-04 13:18:27 -0500835
836 EXPECT_CALL(dataIface,
837 getLocationCode(
838 "/xyz/openbmc_project/inventory/chassis/motherboard/cpu0"))
839 .WillOnce(Return("UXXX-P1"));
840
Matt Spinler2f9225a2020-08-05 12:58:49 -0500841 EXPECT_CALL(dataIface, getInventoryFromLocCode("P1", 0, false))
Matt Spinler0a90a852020-06-04 13:18:27 -0500842 .WillOnce(
843 Return("/xyz/openbmc_project/inventory/chassis/motherboard/cpu0"));
844
845 EXPECT_CALL(
846 dataIface,
847 getHWCalloutFields(
848 "/xyz/openbmc_project/inventory/chassis/motherboard/cpu0", _, _, _))
849 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
850 SetArgReferee<3>("123456789ABC")));
851
852 auto dataPath = getPELReadOnlyDataPath();
853 std::ofstream file{dataPath / "systemA_dev_callouts.json"};
854 file << calloutJSON;
855 file.close();
856
857 {
858 std::vector<std::string> data{
859 "CALLOUT_ERRNO=5",
860 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
861 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0072"};
862
863 AdditionalData ad{data};
864
865 PEL pel{
866 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
867 ad, ffdc, dataIface};
868
869 ASSERT_TRUE(pel.primarySRC().value()->callouts());
870 auto& callouts = pel.primarySRC().value()->callouts()->callouts();
871 ASSERT_EQ(callouts.size(), 1);
Andrew Geissler44fc3162020-07-09 09:21:31 -0500872 ASSERT_TRUE(pel.isCalloutPresent());
Matt Spinler0a90a852020-06-04 13:18:27 -0500873
874 EXPECT_EQ(callouts[0]->priority(), 'H');
875 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P1");
876
877 auto& fru = callouts[0]->fruIdentity();
878 EXPECT_EQ(fru->getPN().value(), "1234567");
879 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
880 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
881
882 const auto& section = pel.optionalSections().back();
883
884 ASSERT_EQ(section->header().id, 0x5544); // UD
885 auto ud = static_cast<UserData*>(section.get());
886
887 // Check that there was a UserData section added that
888 // contains debug details about the device.
889 const auto& d = ud->data();
890 std::string jsonString{d.begin(), d.end()};
891 auto actualJSON = nlohmann::json::parse(jsonString);
892
893 auto expectedJSON = R"(
894 {
895 "PEL Internal Debug Data": {
896 "SRC": [
897 "I2C: bus: 14 address: 114 dest: proc 0 target"
898 ]
899 }
900 }
901 )"_json;
902
903 EXPECT_EQ(actualJSON, expectedJSON);
904 }
905
906 {
907 // Device path not found (wrong i2c addr), so no callouts
908 std::vector<std::string> data{
909 "CALLOUT_ERRNO=5",
910 "CALLOUT_DEVICE_PATH=/sys/devices/platform/ahb/ahb:apb/"
911 "ahb:apb:bus@1e78a000/1e78a340.i2c-bus/i2c-14/14-0099"};
912
913 AdditionalData ad{data};
914
915 PEL pel{
916 regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error,
917 ad, ffdc, dataIface};
918
919 // no callouts
920 EXPECT_FALSE(pel.primarySRC().value()->callouts());
921
922 // Now check that there was a UserData section
923 // that contains the lookup error.
924 const auto& section = pel.optionalSections().back();
925
926 ASSERT_EQ(section->header().id, 0x5544); // UD
927 auto ud = static_cast<UserData*>(section.get());
928
929 const auto& d = ud->data();
930
931 std::string jsonString{d.begin(), d.end()};
932
933 auto actualJSON = nlohmann::json::parse(jsonString);
934
935 auto expectedJSON =
936 "{\"PEL Internal Debug Data\":{\"SRC\":"
937 "[\"Problem looking up I2C callouts on 14 153: "
938 "[json.exception.out_of_range.403] key '153' not found\"]}}"_json;
939
940 EXPECT_EQ(actualJSON, expectedJSON);
941 }
942
943 fs::remove_all(dataPath);
944}
Matt Spinlere513dbc2020-08-27 11:14:17 -0500945
946// Test PELs when the callouts are passed in using a JSON file.
947TEST_F(PELTest, CreateWithJSONCalloutsTest)
948{
949 PelFFDCfile ffdcFile;
950 ffdcFile.format = UserDataFormat::json;
951 ffdcFile.subType = 0xCA; // Callout JSON
952 ffdcFile.version = 1;
953
954 // Write these callouts to a JSON file and pass it into
955 // the PEL as an FFDC file.
956 auto inputJSON = R"([
957 {
958 "Priority": "H",
959 "LocationCode": "P0-C1"
960 },
961 {
962 "Priority": "M",
963 "Procedure": "PROCEDURE"
964 }
965 ])"_json;
966
967 auto s = inputJSON.dump();
968 std::vector<uint8_t> data{s.begin(), s.end()};
969 auto dir = makeTempDir();
970 ffdcFile.fd = writeFileAndGetFD(dir, data);
971
972 PelFFDC ffdc;
973 ffdc.push_back(std::move(ffdcFile));
974
975 AdditionalData ad;
976 NiceMock<MockDataInterface> dataIface;
977
978 EXPECT_CALL(dataIface, expandLocationCode("P0-C1", 0))
979 .Times(1)
980 .WillOnce(Return("UXXX-P0-C1"));
981 EXPECT_CALL(dataIface, getInventoryFromLocCode("P0-C1", 0, false))
982 .Times(1)
983 .WillOnce(Return("/inv/system/chassis/motherboard/bmc"));
984 EXPECT_CALL(dataIface, getHWCalloutFields(
985 "/inv/system/chassis/motherboard/bmc", _, _, _))
986 .Times(1)
987 .WillOnce(DoAll(SetArgReferee<1>("1234567"), SetArgReferee<2>("CCCC"),
988 SetArgReferee<3>("123456789ABC")));
989
990 message::Entry regEntry;
991 regEntry.name = "test";
992 regEntry.subsystem = 5;
993 regEntry.actionFlags = 0xC000;
994 regEntry.src.type = 0xBD;
995 regEntry.src.reasonCode = 0x1234;
996
997 PEL pel{regEntry, 42, 5, phosphor::logging::Entry::Level::Error,
998 ad, ffdc, dataIface};
999
1000 ASSERT_TRUE(pel.valid());
1001 ASSERT_TRUE(pel.primarySRC().value()->callouts());
1002 const auto& callouts = pel.primarySRC().value()->callouts()->callouts();
1003 ASSERT_EQ(callouts.size(), 2);
1004
1005 {
1006 EXPECT_EQ(callouts[0]->priority(), 'H');
1007 EXPECT_EQ(callouts[0]->locationCode(), "UXXX-P0-C1");
1008
1009 auto& fru = callouts[0]->fruIdentity();
1010 EXPECT_EQ(fru->getPN().value(), "1234567");
1011 EXPECT_EQ(fru->getCCIN().value(), "CCCC");
1012 EXPECT_EQ(fru->getSN().value(), "123456789ABC");
1013 EXPECT_EQ(fru->failingComponentType(), src::FRUIdentity::hardwareFRU);
1014 }
1015 {
1016 EXPECT_EQ(callouts[1]->priority(), 'M');
1017 EXPECT_EQ(callouts[1]->locationCode(), "");
1018
1019 auto& fru = callouts[1]->fruIdentity();
1020 EXPECT_EQ(fru->getMaintProc().value(), "PROCEDU");
1021 EXPECT_EQ(fru->failingComponentType(),
1022 src::FRUIdentity::maintenanceProc);
1023 }
1024 fs::remove_all(dir);
1025}