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 "extensions/openpower-pels/extended_user_data.hpp"
17 #include "pel_utils.hpp"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace openpower::pels;
22 
23 TEST(ExtUserDataTest, UnflattenFlattenTest)
24 {
25     auto eudSectionData = pelDataFactory(TestPELType::extendedUserDataSection);
26     Stream stream(eudSectionData);
27     ExtendedUserData eud(stream);
28 
29     EXPECT_TRUE(eud.valid());
30     EXPECT_EQ(eud.header().id, 0x4544);
31     EXPECT_EQ(eud.header().size, eudSectionData.size());
32     EXPECT_EQ(eud.header().version, 0x01);
33     EXPECT_EQ(eud.header().subType, 0x02);
34     EXPECT_EQ(eud.header().componentID, 0x2000);
35     EXPECT_EQ(eud.creatorID(), 'O');
36 
37     const auto& data = eud.data();
38 
39     // The eudSectionData itself starts 4B after the 8B header
40     EXPECT_EQ(data.size(), eudSectionData.size() - 12);
41 
42     for (size_t i = 0; i < data.size(); i++)
43     {
44         EXPECT_EQ(data[i], eudSectionData[i + 12]);
45     }
46 
47     // Now flatten
48     std::vector<uint8_t> newData;
49     Stream newStream(newData);
50     eud.flatten(newStream);
51 
52     EXPECT_EQ(eudSectionData, newData);
53 }
54 
55 TEST(ExtUserDataTest, BadDataTest)
56 {
57     auto data = pelDataFactory(TestPELType::extendedUserDataSection);
58     data.resize(8); // Too small
59 
60     Stream stream(data);
61     ExtendedUserData eud(stream);
62     EXPECT_FALSE(eud.valid());
63 }
64 
65 TEST(ExtUserDataTest, BadSizeFieldTest)
66 {
67     auto data = pelDataFactory(TestPELType::extendedUserDataSection);
68 
69     {
70         data[3] = 0xFF; // Set the size field too large
71         Stream stream(data);
72         ExtendedUserData eud(stream);
73         EXPECT_FALSE(eud.valid());
74     }
75     {
76         data[3] = 0x7; // Set the size field too small
77         Stream stream(data);
78         ExtendedUserData eud(stream);
79         EXPECT_FALSE(eud.valid());
80     }
81 }
82 
83 TEST(ExtUserDataTest, ConstructorTest)
84 {
85     std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
86 
87     ExtendedUserData eud{0x1112, 0x42, 0x01, 'B', data};
88 
89     EXPECT_TRUE(eud.valid());
90     EXPECT_EQ(eud.header().id, 0x4544);
91     EXPECT_EQ(eud.header().size, 20);
92     EXPECT_EQ(eud.header().subType, 0x42);
93     EXPECT_EQ(eud.header().version, 0x01);
94     EXPECT_EQ(eud.header().componentID, 0x1112);
95     EXPECT_EQ(eud.flattenedSize(), 20);
96     EXPECT_EQ(eud.creatorID(), 'B');
97 
98     const auto& d = eud.data();
99 
100     EXPECT_EQ(d, data);
101 }
102 
103 TEST(ExtUserDataTest, ShrinkTest)
104 {
105     std::vector<uint8_t> data(100, 0xFF);
106 
107     ExtendedUserData eud(0x1112, 0x42, 0x01, 'O', data);
108     EXPECT_TRUE(eud.valid());
109 
110     // 4B aligned
111     EXPECT_TRUE(eud.shrink(88));
112     EXPECT_EQ(eud.flattenedSize(), 88);
113     EXPECT_EQ(eud.header().size, 88);
114 
115     // rounded off
116     EXPECT_TRUE(eud.shrink(87));
117     EXPECT_EQ(eud.flattenedSize(), 84);
118     EXPECT_EQ(eud.header().size, 84);
119 
120     // too big
121     EXPECT_FALSE(eud.shrink(200));
122     EXPECT_EQ(eud.flattenedSize(), 84);
123     EXPECT_EQ(eud.header().size, 84);
124 
125     // way too small
126     EXPECT_FALSE(eud.shrink(3));
127     EXPECT_EQ(eud.flattenedSize(), 84);
128     EXPECT_EQ(eud.header().size, 84);
129 
130     // the smallest it can go
131     EXPECT_TRUE(eud.shrink(16));
132     EXPECT_EQ(eud.flattenedSize(), 16);
133     EXPECT_EQ(eud.header().size, 16);
134 
135     // one too small
136     EXPECT_FALSE(eud.shrink(15));
137     EXPECT_EQ(eud.flattenedSize(), 16);
138     EXPECT_EQ(eud.header().size, 16);
139 }
140