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  */
16 #include "elog_entry.hpp"
17 #include "extensions/openpower-pels/pel_types.hpp"
18 #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 
24 using namespace openpower::pels;
25 
26 TEST(UserHeaderTest, SizeTest)
27 {
28     EXPECT_EQ(UserHeader::flattenedSize(), 24);
29 }
30 
31 TEST(UserHeaderTest, UnflattenFlattenTest)
32 {
33     auto data = pelDataFactory(TestPELType::userHeaderSection);
34 
35     Stream stream(data);
36     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 
58     uh.flatten(newStream);
59     EXPECT_EQ(data, newData);
60 }
61 
62 TEST(UserHeaderTest, ShortDataTest)
63 {
64     auto data = pelDataFactory(TestPELType::userHeaderSection);
65     data.resize(data.size() - 1);
66 
67     Stream stream(data);
68     UserHeader uh(stream);
69 
70     EXPECT_EQ(uh.valid(), false);
71 }
72 
73 TEST(UserHeaderTest, CorruptDataTest1)
74 {
75     auto data = pelDataFactory(TestPELType::userHeaderSection);
76     data.resize(data.size() - 1);
77 
78     data.at(0) = 0; // corrupt the section ID
79 
80     Stream stream(data);
81     UserHeader uh(stream);
82 
83     EXPECT_EQ(uh.valid(), false);
84 }
85 
86 TEST(UserHeaderTest, CorruptDataTest2)
87 {
88     auto data = pelDataFactory(TestPELType::userHeaderSection);
89 
90     data.at(4) = 0x22; // corrupt the version
91 
92     Stream stream(data);
93     UserHeader uh(stream);
94 
95     EXPECT_EQ(uh.valid(), false);
96 }
97 
98 // Construct the User Header from the message registry
99 TEST(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
132 TEST(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
149 TEST(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 }
164