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 | |
| 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 | { |
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); |
| 102 | |
| 103 | const auto& d = ud.data(); |
| 104 | |
| 105 | EXPECT_EQ(d, data); |
| 106 | } |