blob: 546b480892d0f19afcd9cbec612ee1296066b4a9 [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 Spinler6ea4d5f2020-05-20 13:31:07 -050027using ::testing::ReturnRef;
Matt Spinler03c1d912019-07-10 14:12:15 -050028
29TEST(UserHeaderTest, SizeTest)
30{
31 EXPECT_EQ(UserHeader::flattenedSize(), 24);
32}
33
34TEST(UserHeaderTest, UnflattenFlattenTest)
35{
Matt Spinler42828bd2019-10-11 10:39:30 -050036 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050037
Matt Spinler42828bd2019-10-11 10:39:30 -050038 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050039 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 Spinlercf5a8d02019-09-05 12:58:53 -050061 uh.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050062 EXPECT_EQ(data, newData);
Matt Spinler03c1d912019-07-10 14:12:15 -050063}
64
65TEST(UserHeaderTest, ShortDataTest)
66{
Matt Spinler42828bd2019-10-11 10:39:30 -050067 auto data = pelDataFactory(TestPELType::userHeaderSection);
68 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050069
Matt Spinler42828bd2019-10-11 10:39:30 -050070 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050071 UserHeader uh(stream);
72
73 EXPECT_EQ(uh.valid(), false);
74}
75
76TEST(UserHeaderTest, CorruptDataTest1)
77{
Matt Spinler42828bd2019-10-11 10:39:30 -050078 auto data = pelDataFactory(TestPELType::userHeaderSection);
79 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050080
Matt Spinler42828bd2019-10-11 10:39:30 -050081 data.at(0) = 0; // corrupt the section ID
Matt Spinler03c1d912019-07-10 14:12:15 -050082
Matt Spinler42828bd2019-10-11 10:39:30 -050083 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050084 UserHeader uh(stream);
85
86 EXPECT_EQ(uh.valid(), false);
87}
88
89TEST(UserHeaderTest, CorruptDataTest2)
90{
Matt Spinler42828bd2019-10-11 10:39:30 -050091 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050092
Matt Spinler42828bd2019-10-11 10:39:30 -050093 data.at(4) = 0x22; // corrupt the version
Matt Spinler03c1d912019-07-10 14:12:15 -050094
Matt Spinler42828bd2019-10-11 10:39:30 -050095 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050096 UserHeader uh(stream);
97
98 EXPECT_EQ(uh.valid(), false);
99}
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500100
101// Construct the User Header from the message registry
102TEST(UserHeaderTest, ConstructionTest)
103{
104 using namespace openpower::pels::message;
Matt Spinleraadccc82020-04-10 14:33:42 -0500105 {
106 Entry regEntry;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500107
Matt Spinleraadccc82020-04-10 14:33:42 -0500108 regEntry.name = "test";
109 regEntry.subsystem = 5;
110 regEntry.severity = {{"", 0x40}};
111 regEntry.actionFlags = 0xC000;
112 regEntry.eventType = 1;
113 regEntry.eventScope = 2;
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500114
Matt Spinleraadccc82020-04-10 14:33:42 -0500115 MockDataInterface dataIface;
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500116 std::vector<std::string> names{"systemA"};
117
118 EXPECT_CALL(dataIface, getSystemNames).WillOnce(ReturnRef(names));
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500119
Matt Spinleraadccc82020-04-10 14:33:42 -0500120 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error,
121 dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500122
Matt Spinleraadccc82020-04-10 14:33:42 -0500123 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 Spinler6ea4d5f2020-05-20 13:31:07 -0500149 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 Spinleraadccc82020-04-10 14:33:42 -0500157
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 Spinlerfdb6a202019-09-20 14:09:20 -0500179}
180
181// Test that the severity comes from the event log if not
182// in the message registry
183TEST(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 Spinleraadccc82020-04-10 14:33:42 -0500195 MockDataInterface dataIface;
196
197 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500198 ASSERT_EQ(uh.severity(), 0x40);
199}
200
201// Test that the optional event type & scope fields work
202TEST(UserHeaderTest, DefaultEventTypeScopeTest)
203{
204 using namespace openpower::pels::message;
205 Entry regEntry;
206
207 regEntry.name = "test";
208 regEntry.subsystem = 5;
Matt Spinleraadccc82020-04-10 14:33:42 -0500209 regEntry.severity = {{"", 0x40}};
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500210 regEntry.actionFlags = 0xC000;
211
Matt Spinleraadccc82020-04-10 14:33:42 -0500212 MockDataInterface dataIface;
213
Matt Spinler6ea4d5f2020-05-20 13:31:07 -0500214 std::vector<std::string> names{"systemA"};
215 EXPECT_CALL(dataIface, getSystemNames).WillOnce(ReturnRef(names));
216
Matt Spinleraadccc82020-04-10 14:33:42 -0500217 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
Matt Spinlerfdb6a202019-09-20 14:09:20 -0500218
219 ASSERT_EQ(uh.eventType(), 0);
220 ASSERT_EQ(uh.scope(), 0x03);
221}