blob: 7b8842c6f4dcf7063bf2314d0447e1d2cd988b55 [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"
20#include "pel_utils.hpp"
21
22#include <gtest/gtest.h>
23
24using namespace openpower::pels;
25
26TEST(UserHeaderTest, SizeTest)
27{
28 EXPECT_EQ(UserHeader::flattenedSize(), 24);
29}
30
31TEST(UserHeaderTest, UnflattenFlattenTest)
32{
Matt Spinler42828bd2019-10-11 10:39:30 -050033 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050034
Matt Spinler42828bd2019-10-11 10:39:30 -050035 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050036 UserHeader uh(stream);
37 EXPECT_EQ(uh.valid(), true);
38
39 EXPECT_EQ(uh.header().id, 0x5548);
40 EXPECT_EQ(uh.header().size, UserHeader::flattenedSize());
41 EXPECT_EQ(uh.header().version, 0x01);
42 EXPECT_EQ(uh.header().subType, 0x0A);
43 EXPECT_EQ(uh.header().componentID, 0x0B0C);
44
45 EXPECT_EQ(uh.subsystem(), 0x10);
46 EXPECT_EQ(uh.scope(), 0x04);
47 EXPECT_EQ(uh.severity(), 0x20);
48 EXPECT_EQ(uh.eventType(), 0x00);
49 EXPECT_EQ(uh.problemDomain(), 0x03);
50 EXPECT_EQ(uh.problemVector(), 0x04);
51 EXPECT_EQ(uh.actionFlags(), 0x80C0);
52
53 // Now flatten into a vector and check that this vector
54 // matches the original one.
55 std::vector<uint8_t> newData;
56 Stream newStream(newData);
57
Matt Spinlercf5a8d02019-09-05 12:58:53 -050058 uh.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050059 EXPECT_EQ(data, newData);
Matt Spinler03c1d912019-07-10 14:12:15 -050060}
61
62TEST(UserHeaderTest, ShortDataTest)
63{
Matt Spinler42828bd2019-10-11 10:39:30 -050064 auto data = pelDataFactory(TestPELType::userHeaderSection);
65 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050066
Matt Spinler42828bd2019-10-11 10:39:30 -050067 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050068 UserHeader uh(stream);
69
70 EXPECT_EQ(uh.valid(), false);
71}
72
73TEST(UserHeaderTest, CorruptDataTest1)
74{
Matt Spinler42828bd2019-10-11 10:39:30 -050075 auto data = pelDataFactory(TestPELType::userHeaderSection);
76 data.resize(data.size() - 1);
Matt Spinler03c1d912019-07-10 14:12:15 -050077
Matt Spinler42828bd2019-10-11 10:39:30 -050078 data.at(0) = 0; // corrupt the section ID
Matt Spinler03c1d912019-07-10 14:12:15 -050079
Matt Spinler42828bd2019-10-11 10:39:30 -050080 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050081 UserHeader uh(stream);
82
83 EXPECT_EQ(uh.valid(), false);
84}
85
86TEST(UserHeaderTest, CorruptDataTest2)
87{
Matt Spinler42828bd2019-10-11 10:39:30 -050088 auto data = pelDataFactory(TestPELType::userHeaderSection);
Matt Spinler03c1d912019-07-10 14:12:15 -050089
Matt Spinler42828bd2019-10-11 10:39:30 -050090 data.at(4) = 0x22; // corrupt the version
Matt Spinler03c1d912019-07-10 14:12:15 -050091
Matt Spinler42828bd2019-10-11 10:39:30 -050092 Stream stream(data);
Matt Spinler03c1d912019-07-10 14:12:15 -050093 UserHeader uh(stream);
94
95 EXPECT_EQ(uh.valid(), false);
96}
Matt Spinlerfdb6a202019-09-20 14:09:20 -050097
98// Construct the User Header from the message registry
99TEST(UserHeaderTest, ConstructionTest)
100{
101 using namespace openpower::pels::message;
102 Entry regEntry;
103
104 regEntry.name = "test";
105 regEntry.subsystem = 5;
106 regEntry.severity = 0x40;
107 regEntry.actionFlags = 0xC000;
108 regEntry.eventType = 1;
109 regEntry.eventScope = 2;
110
111 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error);
112
113 ASSERT_TRUE(uh.valid());
114 EXPECT_EQ(uh.header().id, 0x5548);
115 EXPECT_EQ(uh.header().size, UserHeader::flattenedSize());
116 EXPECT_EQ(uh.header().version, 0x01);
117 EXPECT_EQ(uh.header().subType, 0x00);
118 EXPECT_EQ(uh.header().componentID,
119 static_cast<uint16_t>(ComponentID::phosphorLogging));
120
121 ASSERT_EQ(uh.subsystem(), 5);
122 ASSERT_EQ(uh.severity(), 0x40);
123 ASSERT_EQ(uh.eventType(), 1);
124 ASSERT_EQ(uh.scope(), 2);
125 ASSERT_EQ(uh.problemDomain(), 0);
126 ASSERT_EQ(uh.problemVector(), 0);
127 ASSERT_EQ(uh.actionFlags(), 0xC000);
128}
129
130// Test that the severity comes from the event log if not
131// in the message registry
132TEST(UserHeaderTest, UseEventLogSevTest)
133{
134 using namespace openpower::pels::message;
135 Entry regEntry;
136
137 regEntry.name = "test";
138 regEntry.subsystem = 5;
139 regEntry.actionFlags = 0xC000;
140 regEntry.eventType = 1;
141 regEntry.eventScope = 2;
142 // Leave off severity
143
144 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error);
145 ASSERT_EQ(uh.severity(), 0x40);
146}
147
148// Test that the optional event type & scope fields work
149TEST(UserHeaderTest, DefaultEventTypeScopeTest)
150{
151 using namespace openpower::pels::message;
152 Entry regEntry;
153
154 regEntry.name = "test";
155 regEntry.subsystem = 5;
156 regEntry.severity = 0x40;
157 regEntry.actionFlags = 0xC000;
158
159 UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error);
160
161 ASSERT_EQ(uh.eventType(), 0);
162 ASSERT_EQ(uh.scope(), 0x03);
163}