blob: afab5cadd4b442c0c63532af7aa1718f0cedb1ca [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;
29
30class PELTest : public CleanLogID
31{
32};
33
34TEST_F(PELTest, FlattenTest)
35{
Matt Spinler42828bd2019-10-11 10:39:30 -050036 auto data = pelDataFactory(TestPELType::pelSimple);
37 auto origData = data;
38 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 Spinler07eefc52019-09-26 11:18:26 -050049 ASSERT_EQ(origData, flattenedData);
Matt Spinlercb6b0592019-07-16 15:58:51 -050050}
51
52TEST_F(PELTest, CommitTimeTest)
53{
Matt Spinler42828bd2019-10-11 10:39:30 -050054 auto data = pelDataFactory(TestPELType::pelSimple);
55 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050056
57 auto origTime = pel->commitTime();
58 pel->setCommitTime();
59 auto newTime = pel->commitTime();
60
61 ASSERT_NE(origTime, newTime);
62
63 // Make a new PEL and check new value is still there
64 auto newData = pel->data();
65 auto newPel = std::make_unique<PEL>(newData);
66 ASSERT_EQ(newTime, newPel->commitTime());
67}
68
69TEST_F(PELTest, AssignIDTest)
70{
Matt Spinler42828bd2019-10-11 10:39:30 -050071 auto data = pelDataFactory(TestPELType::pelSimple);
72 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -050073
74 auto origID = pel->id();
75 pel->assignID();
76 auto newID = pel->id();
77
78 ASSERT_NE(origID, newID);
79
80 // Make a new PEL and check new value is still there
81 auto newData = pel->data();
82 auto newPel = std::make_unique<PEL>(newData);
83 ASSERT_EQ(newID, newPel->id());
84}
85
86TEST_F(PELTest, WithLogIDTest)
87{
Matt Spinler42828bd2019-10-11 10:39:30 -050088 auto data = pelDataFactory(TestPELType::pelSimple);
89 auto pel = std::make_unique<PEL>(data, 0x42);
Matt Spinlercb6b0592019-07-16 15:58:51 -050090
91 EXPECT_TRUE(pel->valid());
92 EXPECT_EQ(pel->obmcLogID(), 0x42);
93}
94
95TEST_F(PELTest, InvalidPELTest)
96{
Matt Spinler42828bd2019-10-11 10:39:30 -050097 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinlercb6b0592019-07-16 15:58:51 -050098
99 // Too small
Matt Spinler42828bd2019-10-11 10:39:30 -0500100 data.resize(PrivateHeader::flattenedSize());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500101
Matt Spinler42828bd2019-10-11 10:39:30 -0500102 auto pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500103
Matt Spinler97d19b42019-10-29 11:34:03 -0500104 EXPECT_TRUE(pel->privateHeader().valid());
105 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500106 EXPECT_FALSE(pel->valid());
107
Matt Spinlercb6b0592019-07-16 15:58:51 -0500108 // Now corrupt the private header
Matt Spinler42828bd2019-10-11 10:39:30 -0500109 data = pelDataFactory(TestPELType::pelSimple);
110 data.at(0) = 0;
111 pel = std::make_unique<PEL>(data);
Matt Spinlercb6b0592019-07-16 15:58:51 -0500112
Matt Spinler97d19b42019-10-29 11:34:03 -0500113 EXPECT_FALSE(pel->privateHeader().valid());
114 EXPECT_TRUE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500115 EXPECT_FALSE(pel->valid());
116}
117
118TEST_F(PELTest, EmptyDataTest)
119{
120 std::vector<uint8_t> data;
121 auto pel = std::make_unique<PEL>(data);
122
Matt Spinler97d19b42019-10-29 11:34:03 -0500123 EXPECT_FALSE(pel->privateHeader().valid());
124 EXPECT_FALSE(pel->userHeader().valid());
Matt Spinlercb6b0592019-07-16 15:58:51 -0500125 EXPECT_FALSE(pel->valid());
126}
Matt Spinlerb8323632019-09-20 15:11:04 -0500127
128TEST_F(PELTest, CreateFromRegistryTest)
129{
130 message::Entry regEntry;
131 uint64_t timestamp = 5;
132
133 regEntry.name = "test";
134 regEntry.subsystem = 5;
135 regEntry.actionFlags = 0xC000;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500136 regEntry.src.type = 0xBD;
137 regEntry.src.reasonCode = 0x1234;
Matt Spinlerb8323632019-09-20 15:11:04 -0500138
Matt Spinlerbd716f02019-10-15 10:54:11 -0500139 AdditionalData ad;
Matt Spinleraa659472019-10-23 09:26:48 -0500140 MockDataInterface dataIface;
Matt Spinlerbd716f02019-10-15 10:54:11 -0500141
Matt Spinleraa659472019-10-23 09:26:48 -0500142 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error, ad,
143 dataIface};
Matt Spinlerb8323632019-09-20 15:11:04 -0500144
145 EXPECT_TRUE(pel.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -0500146 EXPECT_EQ(pel.privateHeader().obmcLogID(), 42);
147 EXPECT_EQ(pel.userHeader().severity(), 0x40);
Matt Spinlerb8323632019-09-20 15:11:04 -0500148
Matt Spinlerbd716f02019-10-15 10:54:11 -0500149 EXPECT_EQ(pel.primarySRC().value()->asciiString(),
150 "BD051234 ");
Matt Spinlerb8323632019-09-20 15:11:04 -0500151}
Matt Spinler131870c2019-09-25 13:29:04 -0500152
153// Test that we'll create Generic optional sections for sections that
154// there aren't explicit classes for.
155TEST_F(PELTest, GenericSectionTest)
156{
Matt Spinler42828bd2019-10-11 10:39:30 -0500157 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500158
159 std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
160 0x00, 0x18, // Size
161 0x01, 0x02, // version, subtype
162 0x03, 0x04, // comp ID
163
164 // some data
165 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
166 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
167 0x00};
168
169 std::vector<uint8_t> section2{
170 0x59, 0x59, // ID 'YY'
171 0x00, 0x20, // Size
172 0x01, 0x02, // version, subtype
173 0x03, 0x04, // comp ID
174
175 // some data
176 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
177 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
178
179 // Add the new sections at the end
Matt Spinler42828bd2019-10-11 10:39:30 -0500180 data.insert(data.end(), section1.begin(), section1.end());
181 data.insert(data.end(), section2.begin(), section2.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500182
183 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500184 data.at(27) += 2;
185 auto origData = data;
Matt Spinler131870c2019-09-25 13:29:04 -0500186
Matt Spinler42828bd2019-10-11 10:39:30 -0500187 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500188
189 const auto& sections = pel.optionalSections();
190
191 bool foundXX = false;
192 bool foundYY = false;
193
194 // Check that we can find these 2 Generic sections
195 for (const auto& section : sections)
196 {
197 if (section->header().id == 0x5858)
198 {
199 foundXX = true;
200 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
201 }
202 else if (section->header().id == 0x5959)
203 {
204 foundYY = true;
205 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
206 }
207 }
208
209 EXPECT_TRUE(foundXX);
210 EXPECT_TRUE(foundYY);
Matt Spinler07eefc52019-09-26 11:18:26 -0500211
212 // Now flatten and check
213 auto newData = pel.data();
214
215 EXPECT_EQ(origData, newData);
Matt Spinler131870c2019-09-25 13:29:04 -0500216}
217
218// Test that an invalid section will still get a Generic object
219TEST_F(PELTest, InvalidGenericTest)
220{
Matt Spinler42828bd2019-10-11 10:39:30 -0500221 auto data = pelDataFactory(TestPELType::pelSimple);
Matt Spinler131870c2019-09-25 13:29:04 -0500222
223 // Not a valid section
224 std::vector<uint8_t> section1{0x01, 0x02, 0x03};
225
Matt Spinler42828bd2019-10-11 10:39:30 -0500226 data.insert(data.end(), section1.begin(), section1.end());
Matt Spinler131870c2019-09-25 13:29:04 -0500227
228 // Increment the section count
Matt Spinler42828bd2019-10-11 10:39:30 -0500229 data.at(27) += 1;
Matt Spinler131870c2019-09-25 13:29:04 -0500230
Matt Spinler42828bd2019-10-11 10:39:30 -0500231 PEL pel{data};
Matt Spinler131870c2019-09-25 13:29:04 -0500232 EXPECT_FALSE(pel.valid());
233
234 const auto& sections = pel.optionalSections();
235
236 bool foundGeneric = false;
237 for (const auto& section : sections)
238 {
239 if (dynamic_cast<Generic*>(section.get()) != nullptr)
240 {
241 foundGeneric = true;
242 EXPECT_EQ(section->valid(), false);
243 break;
244 }
245 }
246
247 EXPECT_TRUE(foundGeneric);
248}
Matt Spinlerafa857c2019-10-24 13:03:46 -0500249
250// Create a UserData section out of AdditionalData
251TEST_F(PELTest, MakeUDSectionTest)
252{
253 std::vector<std::string> ad{"KEY1=VALUE1", "KEY2=VALUE2", "KEY3=VALUE3",
254 "ESEL=TEST"};
255 AdditionalData additionalData{ad};
256
257 auto ud = util::makeADUserDataSection(additionalData);
258
259 EXPECT_TRUE(ud->valid());
260 EXPECT_EQ(ud->header().id, 0x5544);
261 EXPECT_EQ(ud->header().version, 0x01);
262 EXPECT_EQ(ud->header().subType, 0x01);
263 EXPECT_EQ(ud->header().componentID, 0x2000);
264
265 const auto& d = ud->data();
266
267 std::string jsonString{d.begin(), d.end()};
Matt Spinler53407be2019-11-18 09:16:31 -0600268
269 std::string expectedJSON =
Matt Spinlerafa857c2019-10-24 13:03:46 -0500270 R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})";
Matt Spinler53407be2019-11-18 09:16:31 -0600271
272 // The actual data is null padded to a 4B boundary.
273 std::vector<uint8_t> expectedData;
274 expectedData.resize(52, '\0');
275 memcpy(expectedData.data(), expectedJSON.data(), expectedJSON.size());
276
277 EXPECT_EQ(d, expectedData);
Matt Spinlerafa857c2019-10-24 13:03:46 -0500278
279 // Ensure we can read this as JSON
280 auto newJSON = nlohmann::json::parse(jsonString);
281 EXPECT_EQ(newJSON["KEY1"], "VALUE1");
282 EXPECT_EQ(newJSON["KEY2"], "VALUE2");
283 EXPECT_EQ(newJSON["KEY3"], "VALUE3");
Matt Spinler97d19b42019-10-29 11:34:03 -0500284}