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