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