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 "mocks.hpp"
21 #include "pel_utils.hpp"
22 
23 #include <gtest/gtest.h>
24 
25 using namespace openpower::pels;
26 using ::testing::Return;
27 
28 TEST(UserHeaderTest, SizeTest)
29 {
30     EXPECT_EQ(UserHeader::flattenedSize(), 24);
31 }
32 
33 TEST(UserHeaderTest, UnflattenFlattenTest)
34 {
35     auto data = pelDataFactory(TestPELType::userHeaderSection);
36 
37     Stream stream(data);
38     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 
60     uh.flatten(newStream);
61     EXPECT_EQ(data, newData);
62 }
63 
64 TEST(UserHeaderTest, ShortDataTest)
65 {
66     auto data = pelDataFactory(TestPELType::userHeaderSection);
67     data.resize(data.size() - 1);
68 
69     Stream stream(data);
70     UserHeader uh(stream);
71 
72     EXPECT_EQ(uh.valid(), false);
73 }
74 
75 TEST(UserHeaderTest, CorruptDataTest1)
76 {
77     auto data = pelDataFactory(TestPELType::userHeaderSection);
78     data.resize(data.size() - 1);
79 
80     data.at(0) = 0; // corrupt the section ID
81 
82     Stream stream(data);
83     UserHeader uh(stream);
84 
85     EXPECT_EQ(uh.valid(), false);
86 }
87 
88 TEST(UserHeaderTest, CorruptDataTest2)
89 {
90     auto data = pelDataFactory(TestPELType::userHeaderSection);
91 
92     data.at(4) = 0x22; // corrupt the version
93 
94     Stream stream(data);
95     UserHeader uh(stream);
96 
97     EXPECT_EQ(uh.valid(), false);
98 }
99 
100 // Construct the User Header from the message registry
101 TEST(UserHeaderTest, ConstructionTest)
102 {
103     using namespace openpower::pels::message;
104     {
105         Entry regEntry;
106 
107         regEntry.name = "test";
108         regEntry.subsystem = 5;
109         regEntry.severity = {{"", 0x40}};
110         regEntry.actionFlags = 0xC000;
111         regEntry.eventType = 1;
112         regEntry.eventScope = 2;
113 
114         MockDataInterface dataIface;
115 
116         UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error,
117                       dataIface);
118 
119         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 
127         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.
139             regEntry.actionFlags = std::nullopt;
140 
141             UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error,
142                           dataIface);
143             EXPECT_EQ(uh.actionFlags(), 0xFFFF);
144         }
145     }
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;
156         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)
161             .WillOnce(Return(names1))
162             .WillOnce(Return(names2))
163             .WillOnce(Return(names3));
164 
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     }
186 }
187 
188 // Test that the severity comes from the event log if not
189 // in the message registry
190 TEST(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 
202     MockDataInterface dataIface;
203 
204     UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
205     ASSERT_EQ(uh.severity(), 0x40);
206 }
207 
208 // Test that the optional event type & scope fields work
209 TEST(UserHeaderTest, DefaultEventTypeScopeTest)
210 {
211     using namespace openpower::pels::message;
212     Entry regEntry;
213 
214     regEntry.name = "test";
215     regEntry.subsystem = 5;
216     regEntry.severity = {{"", 0x40}};
217     regEntry.actionFlags = 0xC000;
218 
219     MockDataInterface dataIface;
220 
221     UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error, dataIface);
222 
223     ASSERT_EQ(uh.eventType(), 0);
224     ASSERT_EQ(uh.scope(), 0x03);
225 }
226