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 | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 16 | #include "elog_entry.hpp" |
| 17 | #include "extensions/openpower-pels/pel_types.hpp" |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 18 | #include "extensions/openpower-pels/private_header.hpp" |
| 19 | #include "extensions/openpower-pels/user_header.hpp" |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 20 | #include "mocks.hpp" |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 21 | #include "pel_utils.hpp" |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | using namespace openpower::pels; |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 26 | using ::testing::Return; |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 27 | |
| 28 | TEST(UserHeaderTest, SizeTest) |
| 29 | { |
| 30 | EXPECT_EQ(UserHeader::flattenedSize(), 24); |
| 31 | } |
| 32 | |
| 33 | TEST(UserHeaderTest, UnflattenFlattenTest) |
| 34 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 35 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 36 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 37 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 38 | UserHeader uh(stream); |
| 39 | EXPECT_EQ(uh.valid(), true); |
| 40 | |
| 41 | EXPECT_EQ(uh.header().id, 0x5548); |
| 42 | EXPECT_EQ(uh.header().size, UserHeader::flattenedSize()); |
| 43 | EXPECT_EQ(uh.header().version, 0x01); |
| 44 | EXPECT_EQ(uh.header().subType, 0x0A); |
| 45 | EXPECT_EQ(uh.header().componentID, 0x0B0C); |
| 46 | |
| 47 | EXPECT_EQ(uh.subsystem(), 0x10); |
| 48 | EXPECT_EQ(uh.scope(), 0x04); |
| 49 | EXPECT_EQ(uh.severity(), 0x20); |
| 50 | EXPECT_EQ(uh.eventType(), 0x00); |
| 51 | EXPECT_EQ(uh.problemDomain(), 0x03); |
| 52 | EXPECT_EQ(uh.problemVector(), 0x04); |
| 53 | EXPECT_EQ(uh.actionFlags(), 0x80C0); |
| 54 | |
| 55 | // Now flatten into a vector and check that this vector |
| 56 | // matches the original one. |
| 57 | std::vector<uint8_t> newData; |
| 58 | Stream newStream(newData); |
| 59 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 60 | uh.flatten(newStream); |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 61 | EXPECT_EQ(data, newData); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | TEST(UserHeaderTest, ShortDataTest) |
| 65 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 66 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
| 67 | data.resize(data.size() - 1); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 68 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 69 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 70 | UserHeader uh(stream); |
| 71 | |
| 72 | EXPECT_EQ(uh.valid(), false); |
| 73 | } |
| 74 | |
| 75 | TEST(UserHeaderTest, CorruptDataTest1) |
| 76 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 77 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
| 78 | data.resize(data.size() - 1); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 79 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 80 | data.at(0) = 0; // corrupt the section ID |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 81 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 82 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 83 | UserHeader uh(stream); |
| 84 | |
| 85 | EXPECT_EQ(uh.valid(), false); |
| 86 | } |
| 87 | |
| 88 | TEST(UserHeaderTest, CorruptDataTest2) |
| 89 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 90 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 91 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 92 | data.at(4) = 0x22; // corrupt the version |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 93 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 94 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 95 | UserHeader uh(stream); |
| 96 | |
| 97 | EXPECT_EQ(uh.valid(), false); |
| 98 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 99 | |
| 100 | // Construct the User Header from the message registry |
| 101 | TEST(UserHeaderTest, ConstructionTest) |
| 102 | { |
| 103 | using namespace openpower::pels::message; |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 104 | { |
| 105 | Entry regEntry; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 106 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 107 | regEntry.name = "test"; |
| 108 | regEntry.subsystem = 5; |
| 109 | regEntry.severity = {{"", 0x40}}; |
| 110 | regEntry.actionFlags = 0xC000; |
| 111 | regEntry.eventType = 1; |
| 112 | regEntry.eventScope = 2; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 113 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 114 | MockDataInterface dataIface; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 115 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 116 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, |
| 117 | dataIface); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 118 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 119 | ASSERT_TRUE(uh.valid()); |
| 120 | EXPECT_EQ(uh.header().id, 0x5548); |
| 121 | EXPECT_EQ(uh.header().size, UserHeader::flattenedSize()); |
| 122 | EXPECT_EQ(uh.header().version, 0x01); |
| 123 | EXPECT_EQ(uh.header().subType, 0x00); |
| 124 | EXPECT_EQ(uh.header().componentID, |
| 125 | static_cast<uint16_t>(ComponentID::phosphorLogging)); |
| 126 | |
Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 127 | EXPECT_EQ(uh.subsystem(), 5); |
| 128 | EXPECT_EQ(uh.severity(), 0x40); |
| 129 | EXPECT_EQ(uh.eventType(), 1); |
| 130 | EXPECT_EQ(uh.scope(), 2); |
| 131 | EXPECT_EQ(uh.problemDomain(), 0); |
| 132 | EXPECT_EQ(uh.problemVector(), 0); |
| 133 | EXPECT_EQ(uh.actionFlags(), 0xC000); |
| 134 | |
| 135 | { |
| 136 | // The same thing, but as if the action flags weren't specified |
| 137 | // in the registry so they are a nullopt. The object should |
| 138 | // then set them to 0xFFFF. |
Matt Spinler | 1f93c59 | 2020-09-10 10:43:08 -0500 | [diff] [blame] | 139 | regEntry.actionFlags = std::nullopt; |
| 140 | |
| 141 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, |
| 142 | dataIface); |
| 143 | EXPECT_EQ(uh.actionFlags(), 0xFFFF); |
| 144 | } |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Test the system type based severity lookups |
| 148 | { |
| 149 | Entry regEntry; |
| 150 | |
| 151 | regEntry.name = "test"; |
| 152 | regEntry.subsystem = 5; |
| 153 | regEntry.severity = {{"", 0x20}, {"systemB", 0x10}, {"systemA", 0x00}}; |
| 154 | |
| 155 | MockDataInterface dataIface; |
Matt Spinler | 6ea4d5f | 2020-05-20 13:31:07 -0500 | [diff] [blame] | 156 | std::vector<std::string> names1{"systemA"}; |
| 157 | std::vector<std::string> names2{"systemB"}; |
| 158 | std::vector<std::string> names3{"systemC"}; |
| 159 | |
| 160 | EXPECT_CALL(dataIface, getSystemNames) |
Matt Spinler | 1ab6696 | 2020-10-29 13:21:44 -0500 | [diff] [blame] | 161 | .WillOnce(Return(names1)) |
| 162 | .WillOnce(Return(names2)) |
| 163 | .WillOnce(Return(names3)); |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 164 | |
| 165 | { |
| 166 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, |
| 167 | dataIface); |
| 168 | |
| 169 | EXPECT_EQ(uh.severity(), 0x00); |
| 170 | } |
| 171 | |
| 172 | { |
| 173 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, |
| 174 | dataIface); |
| 175 | |
| 176 | EXPECT_EQ(uh.severity(), 0x10); |
| 177 | } |
| 178 | |
| 179 | { |
| 180 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, |
| 181 | dataIface); |
| 182 | |
| 183 | EXPECT_EQ(uh.severity(), 0x20); |
| 184 | } |
| 185 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // Test that the severity comes from the event log if not |
| 189 | // in the message registry |
| 190 | TEST(UserHeaderTest, UseEventLogSevTest) |
| 191 | { |
| 192 | using namespace openpower::pels::message; |
| 193 | Entry regEntry; |
| 194 | |
| 195 | regEntry.name = "test"; |
| 196 | regEntry.subsystem = 5; |
| 197 | regEntry.actionFlags = 0xC000; |
| 198 | regEntry.eventType = 1; |
| 199 | regEntry.eventScope = 2; |
| 200 | // Leave off severity |
| 201 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 202 | MockDataInterface dataIface; |
| 203 | |
| 204 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 205 | ASSERT_EQ(uh.severity(), 0x40); |
| 206 | } |
| 207 | |
| 208 | // Test that the optional event type & scope fields work |
| 209 | TEST(UserHeaderTest, DefaultEventTypeScopeTest) |
| 210 | { |
| 211 | using namespace openpower::pels::message; |
| 212 | Entry regEntry; |
| 213 | |
| 214 | regEntry.name = "test"; |
| 215 | regEntry.subsystem = 5; |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 216 | regEntry.severity = {{"", 0x40}}; |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 217 | regEntry.actionFlags = 0xC000; |
| 218 | |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 219 | MockDataInterface dataIface; |
| 220 | |
| 221 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface); |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 222 | |
| 223 | ASSERT_EQ(uh.eventType(), 0); |
| 224 | ASSERT_EQ(uh.scope(), 0x03); |
| 225 | } |