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" |
| 4 | #include "pel_utils.hpp" |
| 5 | |
| 6 | #include <filesystem> |
| 7 | #include <fstream> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | namespace fs = std::filesystem; |
| 12 | using namespace openpower::pels; |
| 13 | |
| 14 | class PELTest : public CleanLogID |
| 15 | { |
| 16 | }; |
| 17 | |
| 18 | TEST_F(PELTest, FlattenTest) |
| 19 | { |
| 20 | auto data = pelDataFactory(TestPelType::pelSimple); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 21 | auto origData = *data; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 22 | 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 Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 33 | ASSERT_EQ(origData, flattenedData); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | TEST_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 | |
| 53 | TEST_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 | |
| 70 | TEST_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 | |
| 79 | TEST_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 Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 92 | // 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 | |
| 102 | TEST_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 Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 111 | |
| 112 | TEST_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 Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 129 | |
| 130 | // Test that we'll create Generic optional sections for sections that |
| 131 | // there aren't explicit classes for. |
| 132 | TEST_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 Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 162 | auto origData = *data; |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 163 | |
| 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 Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 188 | |
| 189 | // Now flatten and check |
| 190 | auto newData = pel.data(); |
| 191 | |
| 192 | EXPECT_EQ(origData, newData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Test that an invalid section will still get a Generic object |
| 196 | TEST_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 | } |