Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 16 | #include "elog_entry.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 17 | #include "extensions/openpower-pels/generic.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 18 | #include "extensions/openpower-pels/pel.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 19 | #include "mocks.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 20 | #include "pel_utils.hpp" |
| 21 | |
| 22 | #include <filesystem> |
| 23 | #include <fstream> |
| 24 | |
| 25 | #include <gtest/gtest.h> |
| 26 | |
| 27 | namespace fs = std::filesystem; |
| 28 | using namespace openpower::pels; |
| 29 | |
| 30 | class PELTest : public CleanLogID |
| 31 | { |
| 32 | }; |
| 33 | |
| 34 | TEST_F(PELTest, FlattenTest) |
| 35 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 36 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 37 | auto origData = data; |
| 38 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 39 | |
| 40 | // Check a few fields |
| 41 | EXPECT_TRUE(pel->valid()); |
| 42 | EXPECT_EQ(pel->id(), 0x80818283); |
| 43 | EXPECT_EQ(pel->plid(), 0x50515253); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 44 | EXPECT_EQ(pel->userHeader().subsystem(), 0x10); |
| 45 | EXPECT_EQ(pel->userHeader().actionFlags(), 0x80C0); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 46 | |
| 47 | // Test that data in == data out |
| 48 | auto flattenedData = pel->data(); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 49 | ASSERT_EQ(origData, flattenedData); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST_F(PELTest, CommitTimeTest) |
| 53 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 54 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 55 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 56 | |
| 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 | |
| 69 | TEST_F(PELTest, AssignIDTest) |
| 70 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 71 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 72 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 73 | |
| 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 | |
| 86 | TEST_F(PELTest, WithLogIDTest) |
| 87 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 88 | auto data = pelDataFactory(TestPELType::pelSimple); |
| 89 | auto pel = std::make_unique<PEL>(data, 0x42); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 90 | |
| 91 | EXPECT_TRUE(pel->valid()); |
| 92 | EXPECT_EQ(pel->obmcLogID(), 0x42); |
| 93 | } |
| 94 | |
| 95 | TEST_F(PELTest, InvalidPELTest) |
| 96 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 97 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 98 | |
| 99 | // Too small |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 100 | data.resize(PrivateHeader::flattenedSize()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 101 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 102 | auto pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 103 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 104 | EXPECT_TRUE(pel->privateHeader().valid()); |
| 105 | EXPECT_FALSE(pel->userHeader().valid()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 106 | EXPECT_FALSE(pel->valid()); |
| 107 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 108 | // Now corrupt the private header |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 109 | data = pelDataFactory(TestPELType::pelSimple); |
| 110 | data.at(0) = 0; |
| 111 | pel = std::make_unique<PEL>(data); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 112 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 113 | EXPECT_FALSE(pel->privateHeader().valid()); |
| 114 | EXPECT_TRUE(pel->userHeader().valid()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 115 | EXPECT_FALSE(pel->valid()); |
| 116 | } |
| 117 | |
| 118 | TEST_F(PELTest, EmptyDataTest) |
| 119 | { |
| 120 | std::vector<uint8_t> data; |
| 121 | auto pel = std::make_unique<PEL>(data); |
| 122 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 123 | EXPECT_FALSE(pel->privateHeader().valid()); |
| 124 | EXPECT_FALSE(pel->userHeader().valid()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 125 | EXPECT_FALSE(pel->valid()); |
| 126 | } |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 127 | |
| 128 | TEST_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 Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 136 | regEntry.src.type = 0xBD; |
| 137 | regEntry.src.reasonCode = 0x1234; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 138 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 139 | AdditionalData ad; |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 140 | MockDataInterface dataIface; |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 141 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 142 | PEL pel{regEntry, 42, timestamp, phosphor::logging::Entry::Level::Error, ad, |
| 143 | dataIface}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 144 | |
| 145 | EXPECT_TRUE(pel.valid()); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 146 | EXPECT_EQ(pel.privateHeader().obmcLogID(), 42); |
| 147 | EXPECT_EQ(pel.userHeader().severity(), 0x40); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 148 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 149 | EXPECT_EQ(pel.primarySRC().value()->asciiString(), |
| 150 | "BD051234 "); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 151 | } |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 152 | |
| 153 | // Test that we'll create Generic optional sections for sections that |
| 154 | // there aren't explicit classes for. |
| 155 | TEST_F(PELTest, GenericSectionTest) |
| 156 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 157 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 158 | |
| 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 Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 180 | data.insert(data.end(), section1.begin(), section1.end()); |
| 181 | data.insert(data.end(), section2.begin(), section2.end()); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 182 | |
| 183 | // Increment the section count |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 184 | data.at(27) += 2; |
| 185 | auto origData = data; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 186 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 187 | PEL pel{data}; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 188 | |
| 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 Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 211 | |
| 212 | // Now flatten and check |
| 213 | auto newData = pel.data(); |
| 214 | |
| 215 | EXPECT_EQ(origData, newData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Test that an invalid section will still get a Generic object |
| 219 | TEST_F(PELTest, InvalidGenericTest) |
| 220 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 221 | auto data = pelDataFactory(TestPELType::pelSimple); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 222 | |
| 223 | // Not a valid section |
| 224 | std::vector<uint8_t> section1{0x01, 0x02, 0x03}; |
| 225 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 226 | data.insert(data.end(), section1.begin(), section1.end()); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 227 | |
| 228 | // Increment the section count |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 229 | data.at(27) += 1; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 230 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 231 | PEL pel{data}; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 232 | 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 Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 249 | |
| 250 | // Create a UserData section out of AdditionalData |
| 251 | TEST_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 Spinler | 53407be | 2019-11-18 09:16:31 -0600 | [diff] [blame] | 268 | |
| 269 | std::string expectedJSON = |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 270 | R"({"KEY1":"VALUE1","KEY2":"VALUE2","KEY3":"VALUE3"})"; |
Matt Spinler | 53407be | 2019-11-18 09:16:31 -0600 | [diff] [blame] | 271 | |
| 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 Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 278 | |
| 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 Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 284 | } |