Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 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 | */ |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 23 | std::vector<uint8_t> udSectionData{ |
| 24 | 0x55, 0x44, // ID 'UD' |
| 25 | 0x00, 0x10, // Size |
| 26 | 0x01, 0x02, // version, subtype |
| 27 | 0x03, 0x04, // comp ID |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 28 | |
Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 29 | // Data |
| 30 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}; |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 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 | { |
Matt Spinler | 53407be | 2019-11-18 09:16:31 -0600 | [diff] [blame] | 92 | std::vector<uint8_t> data{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 93 | |
| 94 | UserData ud(0x1112, 0x42, 0x01, data); |
| 95 | EXPECT_TRUE(ud.valid()); |
| 96 | |
| 97 | EXPECT_EQ(ud.header().id, 0x5544); |
Matt Spinler | 53407be | 2019-11-18 09:16:31 -0600 | [diff] [blame] | 98 | EXPECT_EQ(ud.header().size, 16); |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 99 | EXPECT_EQ(ud.header().version, 0x01); |
| 100 | EXPECT_EQ(ud.header().subType, 0x42); |
| 101 | EXPECT_EQ(ud.header().componentID, 0x1112); |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 102 | EXPECT_EQ(ud.flattenedSize(), 16); |
Aatir Manzur | 51c9263 | 2019-09-06 13:30:48 -0500 | [diff] [blame] | 103 | |
| 104 | const auto& d = ud.data(); |
| 105 | |
| 106 | EXPECT_EQ(d, data); |
| 107 | } |
Matt Spinler | cbf3c4d | 2020-03-24 15:34:52 -0500 | [diff] [blame] | 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 | } |