Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 1 | #include "elog_entry.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 2 | #include "extensions/openpower-pels/generic.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 3 | #include "extensions/openpower-pels/pel.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 4 | #include "mocks.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 5 | #include "pel_utils.hpp" |
| 6 | |
| 7 | #include <filesystem> |
| 8 | #include <fstream> |
| 9 | |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace fs = std::filesystem; |
| 13 | using namespace openpower::pels; |
| 14 | |
| 15 | class PELTest : public CleanLogID |
| 16 | { |
| 17 | }; |
| 18 | |
| 19 | TEST_F(PELTest, FlattenTest) |
| 20 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 21 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 22 | auto origData = data; |
| 23 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 24 | |
| 25 | // Check a few fields |
| 26 | EXPECT_TRUE(pel->valid()); |
| 27 | EXPECT_EQ(pel->id(), 0x80818283); |
| 28 | EXPECT_EQ(pel->plid(), 0x50515253); |
| 29 | EXPECT_EQ(pel->userHeader()->subsystem(), 0x10); |
| 30 | EXPECT_EQ(pel->userHeader()->actionFlags(), 0x80C0); |
| 31 | |
| 32 | // Test that data in == data out |
| 33 | auto flattenedData = pel->data(); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 34 | ASSERT_EQ(origData, flattenedData); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | TEST_F(PELTest, CommitTimeTest) |
| 38 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 39 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 40 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 41 | |
| 42 | auto origTime = pel->commitTime(); |
| 43 | pel->setCommitTime(); |
| 44 | auto newTime = pel->commitTime(); |
| 45 | |
| 46 | ASSERT_NE(origTime, newTime); |
| 47 | |
| 48 | // Make a new PEL and check new value is still there |
| 49 | auto newData = pel->data(); |
| 50 | auto newPel = std::make_unique<PEL>(newData); |
| 51 | ASSERT_EQ(newTime, newPel->commitTime()); |
| 52 | } |
| 53 | |
| 54 | TEST_F(PELTest, AssignIDTest) |
| 55 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 56 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 57 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 58 | |
| 59 | auto origID = pel->id(); |
| 60 | pel->assignID(); |
| 61 | auto newID = pel->id(); |
| 62 | |
| 63 | ASSERT_NE(origID, newID); |
| 64 | |
| 65 | // Make a new PEL and check new value is still there |
| 66 | auto newData = pel->data(); |
| 67 | auto newPel = std::make_unique<PEL>(newData); |
| 68 | ASSERT_EQ(newID, newPel->id()); |
| 69 | } |
| 70 | |
| 71 | TEST_F(PELTest, WithLogIDTest) |
| 72 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 73 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 74 | auto pel = std::make_unique<PEL>(data, 0x42); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 75 | |
| 76 | EXPECT_TRUE(pel->valid()); |
| 77 | EXPECT_EQ(pel->obmcLogID(), 0x42); |
| 78 | } |
| 79 | |
| 80 | TEST_F(PELTest, InvalidPELTest) |
| 81 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 82 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 83 | |
| 84 | // Too small |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 85 | data.resize(PrivateHeader::flattenedSize()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 86 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 87 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 88 | |
| 89 | EXPECT_TRUE(pel->privateHeader()->valid()); |
| 90 | EXPECT_FALSE(pel->userHeader()->valid()); |
| 91 | EXPECT_FALSE(pel->valid()); |
| 92 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 93 | // Now corrupt the private header |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 94 | data = pelDataFactory(TestPELType::pelSimple); |
| 95 | data.at(0) = 0; |
| 96 | pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 97 | |
| 98 | EXPECT_FALSE(pel->privateHeader()->valid()); |
| 99 | EXPECT_TRUE(pel->userHeader()->valid()); |
| 100 | EXPECT_FALSE(pel->valid()); |
| 101 | } |
| 102 | |
| 103 | TEST_F(PELTest, EmptyDataTest) |
| 104 | { |
| 105 | std::vector<uint8_t> data; |
| 106 | auto pel = std::make_unique<PEL>(data); |
| 107 | |
| 108 | EXPECT_FALSE(pel->privateHeader()->valid()); |
| 109 | EXPECT_FALSE(pel->userHeader()->valid()); |
| 110 | EXPECT_FALSE(pel->valid()); |
| 111 | } |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 112 | |
| 113 | TEST_F(PELTest, CreateFromRegistryTest) |
| 114 | { |
| 115 | message::Entry regEntry; |
| 116 | uint64_t timestamp = 5; |
| 117 | |
| 118 | regEntry.name = "test"; |
| 119 | regEntry.subsystem = 5; |
| 120 | regEntry.actionFlags = 0xC000; |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 121 | regEntry.src.type = 0xBD; |
| 122 | regEntry.src.reasonCode = 0x1234; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 123 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 124 | AdditionalData ad; |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 125 | MockDataInterface dataIface; |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 126 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 127 | PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error, ad, |
| 128 | dataIface}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 129 | |
| 130 | EXPECT_TRUE(pel.valid()); |
| 131 | EXPECT_EQ(pel.privateHeader()->obmcLogID(), 42); |
| 132 | EXPECT_EQ(pel.userHeader()->severity(), 0x40); |
| 133 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 134 | EXPECT_EQ(pel.primarySRC().value()->asciiString(), |
| 135 | "BD051234 "); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 136 | } |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 137 | |
| 138 | // Test that we'll create Generic optional sections for sections that |
| 139 | // there aren't explicit classes for. |
| 140 | TEST_F(PELTest, GenericSectionTest) |
| 141 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 142 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 143 | |
| 144 | std::vector<uint8_t> section1{0x58, 0x58, // ID 'XX' |
| 145 | 0x00, 0x18, // Size |
| 146 | 0x01, 0x02, // version, subtype |
| 147 | 0x03, 0x04, // comp ID |
| 148 | |
| 149 | // some data |
| 150 | 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, |
| 151 | 0x20, 0x31, 0x06, 0x0F, 0x09, 0x22, 0x3A, |
| 152 | 0x00}; |
| 153 | |
| 154 | std::vector<uint8_t> section2{ |
| 155 | 0x59, 0x59, // ID 'YY' |
| 156 | 0x00, 0x20, // Size |
| 157 | 0x01, 0x02, // version, subtype |
| 158 | 0x03, 0x04, // comp ID |
| 159 | |
| 160 | // some data |
| 161 | 0x20, 0x30, 0x05, 0x09, 0x11, 0x1E, 0x1, 0x63, 0x20, 0x31, 0x06, 0x0F, |
| 162 | 0x09, 0x22, 0x3A, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}; |
| 163 | |
| 164 | // Add the new sections at the end |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 165 | data.insert(data.end(), section1.begin(), section1.end()); |
| 166 | data.insert(data.end(), section2.begin(), section2.end()); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 167 | |
| 168 | // Increment the section count |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 169 | data.at(27) += 2; |
| 170 | auto origData = data; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 171 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 172 | PEL pel{data}; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 173 | |
| 174 | const auto& sections = pel.optionalSections(); |
| 175 | |
| 176 | bool foundXX = false; |
| 177 | bool foundYY = false; |
| 178 | |
| 179 | // Check that we can find these 2 Generic sections |
| 180 | for (const auto& section : sections) |
| 181 | { |
| 182 | if (section->header().id == 0x5858) |
| 183 | { |
| 184 | foundXX = true; |
| 185 | EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr); |
| 186 | } |
| 187 | else if (section->header().id == 0x5959) |
| 188 | { |
| 189 | foundYY = true; |
| 190 | EXPECT_NE(dynamic_cast<Generic*>(section.get()), nullptr); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | EXPECT_TRUE(foundXX); |
| 195 | EXPECT_TRUE(foundYY); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 196 | |
| 197 | // Now flatten and check |
| 198 | auto newData = pel.data(); |
| 199 | |
| 200 | EXPECT_EQ(origData, newData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Test that an invalid section will still get a Generic object |
| 204 | TEST_F(PELTest, InvalidGenericTest) |
| 205 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 206 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 207 | |
| 208 | // Not a valid section |
| 209 | std::vector<uint8_t> section1{0x01, 0x02, 0x03}; |
| 210 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 211 | data.insert(data.end(), section1.begin(), section1.end()); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 212 | |
| 213 | // Increment the section count |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 214 | data.at(27) += 1; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 215 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 216 | PEL pel{data}; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 217 | EXPECT_FALSE(pel.valid()); |
| 218 | |
| 219 | const auto& sections = pel.optionalSections(); |
| 220 | |
| 221 | bool foundGeneric = false; |
| 222 | for (const auto& section : sections) |
| 223 | { |
| 224 | if (dynamic_cast<Generic*>(section.get()) != nullptr) |
| 225 | { |
| 226 | foundGeneric = true; |
| 227 | EXPECT_EQ(section->valid(), false); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | EXPECT_TRUE(foundGeneric); |
| 233 | } |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 234 | |
| 235 | // Create a UserData section out of AdditionalData |
| 236 | TEST_F(PELTest, MakeUDSectionTest) |
| 237 | { |
| 238 | std::vector<std::string> ad{"KEY1=VALUE1", "KEY2=VALUE2", "KEY3=VALUE3", |
| 239 | "ESEL=TEST"}; |
| 240 | AdditionalData additionalData{ad}; |
| 241 | |
| 242 | auto ud = util::makeADUserDataSection(additionalData); |
| 243 | |
| 244 | EXPECT_TRUE(ud->valid()); |
| 245 | EXPECT_EQ(ud->header().id, 0x5544); |
| 246 | EXPECT_EQ(ud->header().version, 0x01); |
| 247 | EXPECT_EQ(ud->header().subType, 0x01); |
| 248 | EXPECT_EQ(ud->header().componentID, 0x2000); |
| 249 | |
| 250 | const auto& d = ud->data(); |
| 251 | |
| 252 | std::string jsonString{d.begin(), d.end()}; |
| 253 | std::string expected = |
| 254 | R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})"; |
| 255 | EXPECT_EQ(jsonString, expected); |
| 256 | |
| 257 | // Ensure we can read this as JSON |
| 258 | auto newJSON = nlohmann::json::parse(jsonString); |
| 259 | EXPECT_EQ(newJSON["KEY1"], "VALUE1"); |
| 260 | EXPECT_EQ(newJSON["KEY2"], "VALUE2"); |
| 261 | EXPECT_EQ(newJSON["KEY3"], "VALUE3"); |
| 262 | } |