blob: 2cf58d7d49c11a0e9a941f6963a81147d36590b8 [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 Spinler677381b2020-01-23 10:04:29 -060029using ::testing::Return;
Matt Spinlercb6b0592019-07-16 15:58:51 -050030
31class PELTest : public CleanLogID
32{
33};
34
35TEST_F(PELTest, FlattenTest)
36{
Matt Spinler42828bd2019-10-11 10:39:30 -050037 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler42828bd2019-10-11 10:39:30 -050038 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050039
40 // Check a few fields
41 EXPECT_TRUE(pel->valid());
42 EXPECT_EQ(pel->id(), 0x80818283);
43 EXPECT_EQ(pel->plid(), 0x50515253);
Matt Spinler97d19b42019-10-29 11:34:03 -050044 EXPECT_EQ(pel->userHeader().subsystem(), 0x10);
45 EXPECT_EQ(pel->userHeader().actionFlags(), 0x80C0);
Matt Spinlercb6b0592019-07-16 15:58:51 -050046
47 // Test that data in == data out
48 auto flattenedData = pel->data();
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060049 EXPECT_EQ(data, flattenedData);
50 EXPECT_EQ(flattenedData.size(), pel->size());
Matt Spinlercb6b0592019-07-16 15:58:51 -050051}
52
53TEST_F(PELTest, CommitTimeTest)
54{
Matt Spinler42828bd2019-10-11 10:39:30 -050055 auto data = pelDataFactory(TestPELType::pelSimple);
56 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050057
58 auto origTime = pel->commitTime();
59 pel->setCommitTime();
60 auto newTime = pel->commitTime();
61
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060062 EXPECT_NE(origTime, newTime);
Matt Spinlercb6b0592019-07-16 15:58:51 -050063
64 // Make a new PEL and check new value is still there
65 auto newData = pel->data();
66 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060067 EXPECT_EQ(newTime, newPel->commitTime());
Matt Spinlercb6b0592019-07-16 15:58:51 -050068}
69
70TEST_F(PELTest, AssignIDTest)
71{
Matt Spinler42828bd2019-10-11 10:39:30 -050072 auto data = pelDataFactory(TestPELType::pelSimple);
73 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050074
75 auto origID = pel->id();
76 pel->assignID();
77 auto newID = pel->id();
78
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060079 EXPECT_NE(origID, newID);
Matt Spinlercb6b0592019-07-16 15:58:51 -050080
81 // Make a new PEL and check new value is still there
82 auto newData = pel->data();
83 auto newPel = std::make_unique<PEL>(newData);
Matt Spinlerf1b46ff2020-01-22 14:10:04 -060084 EXPECT_EQ(newID, newPel->id());
Matt Spinlercb6b0592019-07-16 15:58:51 -050085}
86
87TEST_F(PELTest, WithLogIDTest)
88{
Matt Spinler42828bd2019-10-11 10:39:30 -050089 auto data = pelDataFactory(TestPELType::pelSimple);
90 auto pel = std::make_unique<PEL>(data, 0x42);
Matt Spinlercb6b0592019-07-16 15:58:51 -050091
92 EXPECT_TRUE(pel->valid());
93 EXPECT_EQ(pel->obmcLogID(), 0x42);
94}
95
96TEST_F(PELTest, InvalidPELTest)
97{
Matt Spinler42828bd2019-10-11 10:39:30 -050098 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinlercb6b0592019-07-16 15:58:51 -050099
100 // Too small
Matt Spinler42828bd2019-10-11 10:39:30 -0500101 data.resize(PrivateHeader::flattenedSize());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500102
Matt Spinler42828bd2019-10-11 10:39:30 -0500103 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500104
Matt Spinler97d19b42019-10-29 11:34:03 -0500105 EXPECT_TRUE(pel->privateHeader().valid());
106 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500107 EXPECT_FALSE(pel->valid());
108
Matt Spinlercb6b0592019-07-16 15:58:51 -0500109 // Now corrupt the private header
Matt Spinler42828bd2019-10-11 10:39:30 -0500110 data = pelDataFactory(TestPELType::pelSimple);
111 data.at(0) = 0;
112 pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500113
Matt Spinler97d19b42019-10-29 11:34:03 -0500114 EXPECT_FALSE(pel->privateHeader().valid());
115 EXPECT_TRUE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500116 EXPECT_FALSE(pel->valid());
117}
118
119TEST_F(PELTest, EmptyDataTest)
120{
121 std::vector<uint8_t> data;
122 auto pel = std::make_unique<PEL>(data);
123
Matt Spinler97d19b42019-10-29 11:34:03 -0500124 EXPECT_FALSE(pel->privateHeader().valid());
125 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500126 EXPECT_FALSE(pel->valid());
127}
Matt Spinlerb8323632019-09-20 15:11:04 -0500128
129TEST_F(PELTest, CreateFromRegistryTest)
130{
131 message::Entry regEntry;
132 uint64_t timestamp = 5;
133
134 regEntry.name = "test";
135 regEntry.subsystem = 5;
136 regEntry.actionFlags = 0xC000;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500137 regEntry.src.type = 0xBD;
138 regEntry.src.reasonCode = 0x1234;
Matt Spinlerb8323632019-09-20 15:11:04 -0500139
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600140 std::vector<std::string> data{"KEY1=VALUE1"};
141 AdditionalData ad{data};
Matt Spinleraa659472019-10-23 09:26:48 -0500142 MockDataInterface dataIface;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500143
Matt Spinleraa659472019-10-23 09:26:48 -0500144 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error, ad,
145 dataIface};
Matt Spinlerb8323632019-09-20 15:11:04 -0500146
147 EXPECT_TRUE(pel.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -0500148 EXPECT_EQ(pel.privateHeader().obmcLogID(), 42);
149 EXPECT_EQ(pel.userHeader().severity(), 0x40);
Matt Spinlerb8323632019-09-20 15:11:04 -0500150
Matt Spinlerbd716f02019-10-15 10:54:11 -0500151 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
152 "BD051234 ");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600153
154 // Check that certain optional sections have been created
155 size_t mtmsCount = 0;
156 size_t euhCount = 0;
157 size_t udCount = 0;
158
159 for (const auto& section : pel.optionalSections())
160 {
161 if (section->header().id ==
162 static_cast<uint16_t>(SectionID::failingMTMS))
163 {
164 mtmsCount++;
165 }
166 else if (section->header().id ==
167 static_cast<uint16_t>(SectionID::extendedUserHeader))
168 {
169 euhCount++;
170 }
171 else if (section->header().id ==
172 static_cast<uint16_t>(SectionID::userData))
173 {
174 udCount++;
175 }
176 }
177
178 EXPECT_EQ(mtmsCount, 1);
179 EXPECT_EQ(euhCount, 1);
180 EXPECT_EQ(udCount, 2); // AD section and sysInfo section
Matt Spinlerb8323632019-09-20 15:11:04 -0500181}
Matt Spinler131870c2019-09-25 13:29:04 -0500182
183// Test that we'll create Generic optional sections for sections that
184// there aren't explicit classes for.
185TEST_F(PELTest, GenericSectionTest)
186{
Matt Spinler42828bd2019-10-11 10:39:30 -0500187 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500188
189 std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
190 0x00, 0x18, // Size
191 0x01, 0x02, // version, subtype
192 0x03, 0x04, // comp ID
193
194 // some data
195 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
196 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
197 0x00};
198
199 std::vector<uint8_t> section2{
200 0x59, 0x59, // ID 'YY'
201 0x00, 0x20, // Size
202 0x01, 0x02, // version, subtype
203 0x03, 0x04, // comp ID
204
205 // some data
206 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
207 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
208
209 // Add the new sections at the end
Matt Spinler42828bd2019-10-11 10:39:30 -0500210 data.insert(data.end(), section1.begin(), section1.end());
211 data.insert(data.end(), section2.begin(), section2.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500212
213 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500214 data.at(27) += 2;
215 auto origData = data;
Matt Spinler131870c2019-09-25 13:29:04 -0500216
Matt Spinler42828bd2019-10-11 10:39:30 -0500217 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500218
219 const auto& sections = pel.optionalSections();
220
221 bool foundXX = false;
222 bool foundYY = false;
223
224 // Check that we can find these 2 Generic sections
225 for (const auto& section : sections)
226 {
227 if (section->header().id == 0x5858)
228 {
229 foundXX = true;
230 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
231 }
232 else if (section->header().id == 0x5959)
233 {
234 foundYY = true;
235 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
236 }
237 }
238
239 EXPECT_TRUE(foundXX);
240 EXPECT_TRUE(foundYY);
Matt Spinler07eefc52019-09-26 11:18:26 -0500241
242 // Now flatten and check
243 auto newData = pel.data();
244
245 EXPECT_EQ(origData, newData);
Matt Spinler131870c2019-09-25 13:29:04 -0500246}
247
248// Test that an invalid section will still get a Generic object
249TEST_F(PELTest, InvalidGenericTest)
250{
Matt Spinler42828bd2019-10-11 10:39:30 -0500251 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500252
253 // Not a valid section
254 std::vector<uint8_t> section1{0x01, 0x02, 0x03};
255
Matt Spinler42828bd2019-10-11 10:39:30 -0500256 data.insert(data.end(), section1.begin(), section1.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500257
258 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500259 data.at(27) += 1;
Matt Spinler131870c2019-09-25 13:29:04 -0500260
Matt Spinler42828bd2019-10-11 10:39:30 -0500261 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500262 EXPECT_FALSE(pel.valid());
263
264 const auto& sections = pel.optionalSections();
265
266 bool foundGeneric = false;
267 for (const auto& section : sections)
268 {
269 if (dynamic_cast<Generic*>(section.get()) != nullptr)
270 {
271 foundGeneric = true;
272 EXPECT_EQ(section->valid(), false);
273 break;
274 }
275 }
276
277 EXPECT_TRUE(foundGeneric);
278}
Matt Spinlerafa857c2019-10-24 13:03:46 -0500279
280// Create a UserData section out of AdditionalData
281TEST_F(PELTest, MakeUDSectionTest)
282{
283 std::vector<std::string> ad{"KEY1=VALUE1", "KEY2=VALUE2", "KEY3=VALUE3",
284 "ESEL=TEST"};
285 AdditionalData additionalData{ad};
286
287 auto ud = util::makeADUserDataSection(additionalData);
288
289 EXPECT_TRUE(ud->valid());
290 EXPECT_EQ(ud->header().id, 0x5544);
291 EXPECT_EQ(ud->header().version, 0x01);
292 EXPECT_EQ(ud->header().subType, 0x01);
293 EXPECT_EQ(ud->header().componentID, 0x2000);
294
295 const auto& d = ud->data();
296
297 std::string jsonString{d.begin(), d.end()};
Matt Spinler53407be2019-11-18 09:16:31 -0600298
299 std::string expectedJSON =
Matt Spinlerafa857c2019-10-24 13:03:46 -0500300 R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})";
Matt Spinler53407be2019-11-18 09:16:31 -0600301
302 // The actual data is null padded to a 4B boundary.
303 std::vector<uint8_t> expectedData;
304 expectedData.resize(52, '\0');
305 memcpy(expectedData.data(), expectedJSON.data(), expectedJSON.size());
306
307 EXPECT_EQ(d, expectedData);
Matt Spinlerafa857c2019-10-24 13:03:46 -0500308
309 // Ensure we can read this as JSON
310 auto newJSON = nlohmann::json::parse(jsonString);
311 EXPECT_EQ(newJSON["KEY1"], "VALUE1");
312 EXPECT_EQ(newJSON["KEY2"], "VALUE2");
313 EXPECT_EQ(newJSON["KEY3"], "VALUE3");
Matt Spinler97d19b42019-10-29 11:34:03 -0500314}
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600315
316// Create the UserData section that contains system info
Matt Spinler677381b2020-01-23 10:04:29 -0600317TEST_F(PELTest, SysInfoSectionTest)
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600318{
319 MockDataInterface dataIface;
320
Matt Spinler677381b2020-01-23 10:04:29 -0600321 EXPECT_CALL(dataIface, getBMCFWVersionID()).WillOnce(Return("ABCD1234"));
322
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600323 std::string pid = "_PID=" + std::to_string(getpid());
324 std::vector<std::string> ad{pid};
325 AdditionalData additionalData{ad};
326
327 auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
328
329 EXPECT_TRUE(ud->valid());
330 EXPECT_EQ(ud->header().id, 0x5544);
331 EXPECT_EQ(ud->header().version, 0x01);
332 EXPECT_EQ(ud->header().subType, 0x01);
333 EXPECT_EQ(ud->header().componentID, 0x2000);
334
335 // Pull out the JSON data and check it.
336 const auto& d = ud->data();
337 std::string jsonString{d.begin(), d.end()};
338 auto json = nlohmann::json::parse(jsonString);
339
340 // Ensure the 'Process Name' entry contains 'pel_test'
341 auto name = json["Process Name"].get<std::string>();
342 EXPECT_NE(name.find("pel_test"), std::string::npos);
Matt Spinler677381b2020-01-23 10:04:29 -0600343
344 auto version = json["BMC Version ID"].get<std::string>();
345 EXPECT_EQ(version, "ABCD1234");
Matt Spinler4dcd3f42020-01-22 14:55:07 -0600346}
Matt Spinlerce3f4502020-01-22 15:44:35 -0600347
348// Test that the sections that override
349// virtual std::optional<std::string> Section::getJSON() const
350// return valid JSON.
351TEST_F(PELTest, SectionJSONTest)
352{
353 auto data = pelDataFactory(TestPELType::pelSimple);
354 PEL pel{data};
355
356 // Check that all JSON returned from the sections is
357 // parseable by nlohmann::json, which will throw an
358 // exception and fail the test if there is a problem.
359
360 // The getJSON() response needs to be wrapped in a { } to make
361 // actual valid JSON (PEL::toJSON() usually handles that).
362
363 auto jsonString = pel.privateHeader().getJSON();
364
365 // PrivateHeader always prints JSON
366 ASSERT_TRUE(jsonString);
367 *jsonString = '{' + *jsonString + '}';
368 auto json = nlohmann::json::parse(*jsonString);
369
370 jsonString = pel.userHeader().getJSON();
371
372 // UserHeader always prints JSON
373 ASSERT_TRUE(jsonString);
374 *jsonString = '{' + *jsonString + '}';
375 json = nlohmann::json::parse(*jsonString);
376
377 for (const auto& section : pel.optionalSections())
378 {
379 // The optional sections may or may not have implemented getJSON().
380 jsonString = section->getJSON();
381 if (jsonString)
382 {
383 *jsonString = '{' + *jsonString + '}';
384 auto json = nlohmann::json::parse(*jsonString);
385 }
386 }
387}