blob: b7b6583699d3a75d07bf747c01b887d550c81023 [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
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 Spinlerfdb6a202019-09-20 14:09:20 -050016#include "elog_entry.hpp"
17#include "extensions/openpower-pels/pel_types.hpp"
Matt Spinler03c1d912019-07-10 14:12:15 -050018#include "extensions/openpower-pels/private_header.hpp"
19#include "extensions/openpower-pels/user_header.hpp"
Matt Spinleraadccc82020-04-10 14:33:42 -050020#include "mocks.hpp"
Matt Spinler03c1d912019-07-10 14:12:15 -050021#include "pel_utils.hpp"
22
23#include <gtest/gtest.h>
24
25using namespace openpower::pels;
Matt Spinleraadccc82020-04-10 14:33:42 -050026using ::testing::Return;
Matt Spinler03c1d912019-07-10 14:12:15 -050027
28TEST(UserHeaderTest, SizeTest)
29{
30 EXPECT_EQ(UserHeader::flattenedSize(), 24);
31}
32
33TEST(UserHeaderTest, UnflattenFlattenTest)
34{
Matt Spinler42828bd2019-10-11 10:39:30 -050035 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050036
Matt Spinler42828bd2019-10-11 10:39:30 -050037 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050038 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 Spinlercf5a8d02019-09-05 12:58:53 -050060 uh.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050061 EXPECT_EQ(data, newData);
Matt Spinler03c1d912019-07-10 14:12:15 -050062}
63
64TEST(UserHeaderTest, ShortDataTest)
65{
Matt Spinler42828bd2019-10-11 10:39:30 -050066 auto data = pelDataFactory(TestPELType::userHeaderSection);
67 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050068
Matt Spinler42828bd2019-10-11 10:39:30 -050069 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050070 UserHeader uh(stream);
71
72 EXPECT_EQ(uh.valid(), false);
73}
74
75TEST(UserHeaderTest, CorruptDataTest1)
76{
Matt Spinler42828bd2019-10-11 10:39:30 -050077 auto data = pelDataFactory(TestPELType::userHeaderSection);
78 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050079
Matt Spinler42828bd2019-10-11 10:39:30 -050080 data.at(0) = 0; // corrupt the section ID
Matt Spinler03c1d912019-07-10 14:12:15 -050081
Matt Spinler42828bd2019-10-11 10:39:30 -050082 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050083 UserHeader uh(stream);
84
85 EXPECT_EQ(uh.valid(), false);
86}
87
88TEST(UserHeaderTest, CorruptDataTest2)
89{
Matt Spinler42828bd2019-10-11 10:39:30 -050090 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050091
Matt Spinler42828bd2019-10-11 10:39:30 -050092 data.at(4) = 0x22; // corrupt the version
Matt Spinler03c1d912019-07-10 14:12:15 -050093
Matt Spinler42828bd2019-10-11 10:39:30 -050094 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050095 UserHeader uh(stream);
96
97 EXPECT_EQ(uh.valid(), false);
98}
Matt Spinlerfdb6a202019-09-20 14:09:20 -050099
100// Construct the User Header from the message registry
101TEST(UserHeaderTest, ConstructionTest)
102{
103 using namespace openpower::pels::message;
Matt Spinleraadccc82020-04-10 14:33:42 -0500104 {
105 Entry regEntry;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500106
Matt Spinleraadccc82020-04-10 14:33:42 -0500107 regEntry.name = "test";
108 regEntry.subsystem = 5;
109 regEntry.severity = {{"", 0x40}};
110 regEntry.actionFlags = 0xC000;
111 regEntry.eventType = 1;
112 regEntry.eventScope = 2;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500113
Matt Spinleraadccc82020-04-10 14:33:42 -0500114 MockDataInterface dataIface;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500115
Matt Spinleraadccc82020-04-10 14:33:42 -0500116 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error,
117 dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500118
Matt Spinleraadccc82020-04-10 14:33:42 -0500119 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 Spinler1f93c592020-09-10 10:43:08 -0500127 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 Spinler1f93c592020-09-10 10:43:08 -0500139 regEntry.actionFlags = std::nullopt;
140
141 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error,
142 dataIface);
143 EXPECT_EQ(uh.actionFlags(), 0xFFFF);
144 }
Matt Spinleraadccc82020-04-10 14:33:42 -0500145 }
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 Spinler6ea4d5f2020-05-20 13:31:07 -0500156 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 Spinler1ab66962020-10-29 13:21:44 -0500161 .WillOnce(Return(names1))
162 .WillOnce(Return(names2))
163 .WillOnce(Return(names3));
Matt Spinleraadccc82020-04-10 14:33:42 -0500164
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 Spinlerfdb6a202019-09-20 14:09:20 -0500186}
187
188// Test that the severity comes from the event log if not
189// in the message registry
190TEST(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 Spinleraadccc82020-04-10 14:33:42 -0500202 MockDataInterface dataIface;
203
204 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500205 ASSERT_EQ(uh.severity(), 0x40);
206}
207
208// Test that the optional event type & scope fields work
209TEST(UserHeaderTest, DefaultEventTypeScopeTest)
210{
211 using namespace openpower::pels::message;
212 Entry regEntry;
213
214 regEntry.name = "test";
215 regEntry.subsystem = 5;
Matt Spinleraadccc82020-04-10 14:33:42 -0500216 regEntry.severity = {{"", 0x40}};
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500217 regEntry.actionFlags = 0xC000;
218
Matt Spinleraadccc82020-04-10 14:33:42 -0500219 MockDataInterface dataIface;
220
221 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500222
223 ASSERT_EQ(uh.eventType(), 0);
224 ASSERT_EQ(uh.scope(), 0x03);
225}