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