blob: 56e356f49da19e03abd128375dfb23c8ff50d3b4 [file] [log] [blame]
Matt Spinlerb8323632019-09-20 15:11:04 -05001#include "elog_entry.hpp"
Matt Spinler131870c2019-09-25 13:29:04 -05002#include "extensions/openpower-pels/generic.hpp"
Matt Spinlercb6b0592019-07-16 15:58:51 -05003#include "extensions/openpower-pels/pel.hpp"
4#include "pel_utils.hpp"
5
6#include <filesystem>
7#include <fstream>
8
9#include <gtest/gtest.h>
10
11namespace fs = std::filesystem;
12using namespace openpower::pels;
13
14class PELTest : public CleanLogID
15{
16};
17
18TEST_F(PELTest, FlattenTest)
19{
20 auto data = pelDataFactory(TestPelType::pelSimple);
Matt Spinler07eefc52019-09-26 11:18:26 -050021 auto origData = *data;
Matt Spinlercb6b0592019-07-16 15:58:51 -050022 auto pel = std::make_unique<PEL>(*data);
23
24 // Check a few fields
25 EXPECT_TRUE(pel->valid());
26 EXPECT_EQ(pel->id(), 0x80818283);
27 EXPECT_EQ(pel->plid(), 0x50515253);
28 EXPECT_EQ(pel->userHeader()->subsystem(), 0x10);
29 EXPECT_EQ(pel->userHeader()->actionFlags(), 0x80C0);
30
31 // Test that data in == data out
32 auto flattenedData = pel->data();
Matt Spinler07eefc52019-09-26 11:18:26 -050033 ASSERT_EQ(origData, flattenedData);
Matt Spinlercb6b0592019-07-16 15:58:51 -050034}
35
36TEST_F(PELTest, CommitTimeTest)
37{
38 auto data = pelDataFactory(TestPelType::pelSimple);
39 auto pel = std::make_unique<PEL>(*data);
40
41 auto origTime = pel->commitTime();
42 pel->setCommitTime();
43 auto newTime = pel->commitTime();
44
45 ASSERT_NE(origTime, newTime);
46
47 // Make a new PEL and check new value is still there
48 auto newData = pel->data();
49 auto newPel = std::make_unique<PEL>(newData);
50 ASSERT_EQ(newTime, newPel->commitTime());
51}
52
53TEST_F(PELTest, AssignIDTest)
54{
55 auto data = pelDataFactory(TestPelType::pelSimple);
56 auto pel = std::make_unique<PEL>(*data);
57
58 auto origID = pel->id();
59 pel->assignID();
60 auto newID = pel->id();
61
62 ASSERT_NE(origID, newID);
63
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);
67 ASSERT_EQ(newID, newPel->id());
68}
69
70TEST_F(PELTest, WithLogIDTest)
71{
72 auto data = pelDataFactory(TestPelType::pelSimple);
73 auto pel = std::make_unique<PEL>(*data, 0x42);
74
75 EXPECT_TRUE(pel->valid());
76 EXPECT_EQ(pel->obmcLogID(), 0x42);
77}
78
79TEST_F(PELTest, InvalidPELTest)
80{
81 auto data = pelDataFactory(TestPelType::pelSimple);
82
83 // Too small
84 data->resize(PrivateHeader::flattenedSize());
85
86 auto pel = std::make_unique<PEL>(*data);
87
88 EXPECT_TRUE(pel->privateHeader()->valid());
89 EXPECT_FALSE(pel->userHeader()->valid());
90 EXPECT_FALSE(pel->valid());
91
Matt Spinlercb6b0592019-07-16 15:58:51 -050092 // Now corrupt the private header
93 data = pelDataFactory(TestPelType::pelSimple);
94 data->at(0) = 0;
95 pel = std::make_unique<PEL>(*data);
96
97 EXPECT_FALSE(pel->privateHeader()->valid());
98 EXPECT_TRUE(pel->userHeader()->valid());
99 EXPECT_FALSE(pel->valid());
100}
101
102TEST_F(PELTest, EmptyDataTest)
103{
104 std::vector<uint8_t> data;
105 auto pel = std::make_unique<PEL>(data);
106
107 EXPECT_FALSE(pel->privateHeader()->valid());
108 EXPECT_FALSE(pel->userHeader()->valid());
109 EXPECT_FALSE(pel->valid());
110}
Matt Spinlerb8323632019-09-20 15:11:04 -0500111
112TEST_F(PELTest, CreateFromRegistryTest)
113{
114 message::Entry regEntry;
115 uint64_t timestamp = 5;
116
117 regEntry.name = "test";
118 regEntry.subsystem = 5;
119 regEntry.actionFlags = 0xC000;
120
121 PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error};
122
123 EXPECT_TRUE(pel.valid());
124 EXPECT_EQ(pel.privateHeader()->obmcLogID(), 42);
125 EXPECT_EQ(pel.userHeader()->severity(), 0x40);
126
127 // Add more checks as more sections are added
128}
Matt Spinler131870c2019-09-25 13:29:04 -0500129
130// Test that we'll create Generic optional sections for sections that
131// there aren't explicit classes for.
132TEST_F(PELTest, GenericSectionTest)
133{
134 auto data = pelDataFactory(TestPelType::pelSimple);
135
136 std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX'
137 0x00, 0x18, // Size
138 0x01, 0x02, // version, subtype
139 0x03, 0x04, // comp ID
140
141 // some data
142 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63,
143 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A,
144 0x00};
145
146 std::vector<uint8_t> section2{
147 0x59, 0x59, // ID 'YY'
148 0x00, 0x20, // Size
149 0x01, 0x02, // version, subtype
150 0x03, 0x04, // comp ID
151
152 // some data
153 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F,
154 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
155
156 // Add the new sections at the end
157 data->insert(data->end(), section1.begin(), section1.end());
158 data->insert(data->end(), section2.begin(), section2.end());
159
160 // Increment the section count
161 data->at(27) += 2;
Matt Spinler07eefc52019-09-26 11:18:26 -0500162 auto origData = *data;
Matt Spinler131870c2019-09-25 13:29:04 -0500163
164 PEL pel{*data};
165
166 const auto& sections = pel.optionalSections();
167
168 bool foundXX = false;
169 bool foundYY = false;
170
171 // Check that we can find these 2 Generic sections
172 for (const auto& section : sections)
173 {
174 if (section->header().id == 0x5858)
175 {
176 foundXX = true;
177 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
178 }
179 else if (section->header().id == 0x5959)
180 {
181 foundYY = true;
182 EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr);
183 }
184 }
185
186 EXPECT_TRUE(foundXX);
187 EXPECT_TRUE(foundYY);
Matt Spinler07eefc52019-09-26 11:18:26 -0500188
189 // Now flatten and check
190 auto newData = pel.data();
191
192 EXPECT_EQ(origData, newData);
Matt Spinler131870c2019-09-25 13:29:04 -0500193}
194
195// Test that an invalid section will still get a Generic object
196TEST_F(PELTest, InvalidGenericTest)
197{
198 auto data = pelDataFactory(TestPelType::pelSimple);
199
200 // Not a valid section
201 std::vector<uint8_t> section1{0x01, 0x02, 0x03};
202
203 data->insert(data->end(), section1.begin(), section1.end());
204
205 // Increment the section count
206 data->at(27) += 1;
207
208 PEL pel{*data};
209 EXPECT_FALSE(pel.valid());
210
211 const auto& sections = pel.optionalSections();
212
213 bool foundGeneric = false;
214 for (const auto& section : sections)
215 {
216 if (dynamic_cast<Generic*>(section.get()) != nullptr)
217 {
218 foundGeneric = true;
219 EXPECT_EQ(section->valid(), false);
220 break;
221 }
222 }
223
224 EXPECT_TRUE(foundGeneric);
225}