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/callouts.hpp"
17 #include "pel_utils.hpp"
18 
19 #include <gtest/gtest.h>
20 
21 using namespace openpower::pels;
22 using namespace openpower::pels::src;
23 
24 TEST(CalloutsTest, UnflattenFlattenTest)
25 {
26     std::vector<uint8_t> data{0xC0, 0x00, 0x00,
27                               0x00}; // ID, flags, length in words
28 
29     // Add 2 callouts
30     auto callout = srcDataFactory(TestSRCType::calloutStructureA);
31     data.insert(data.end(), callout.begin(), callout.end());
32 
33     callout = srcDataFactory(TestSRCType::calloutStructureB);
34     data.insert(data.end(), callout.begin(), callout.end());
35 
36     Stream stream{data};
37 
38     // Set the actual word length value at offset 2
39     uint16_t wordLength = data.size() / 4;
40     stream.offset(2);
41     stream << wordLength;
42     stream.offset(0);
43 
44     Callouts callouts{stream};
45 
46     EXPECT_EQ(callouts.flattenedSize(), data.size());
47     EXPECT_EQ(callouts.callouts().size(), 2);
48 
49     // spot check that each callout has the right substructures
50     EXPECT_TRUE(callouts.callouts().front()->fruIdentity());
51     EXPECT_FALSE(callouts.callouts().front()->pceIdentity());
52     EXPECT_FALSE(callouts.callouts().front()->mru());
53 
54     EXPECT_TRUE(callouts.callouts().back()->fruIdentity());
55     EXPECT_TRUE(callouts.callouts().back()->pceIdentity());
56     EXPECT_TRUE(callouts.callouts().back()->mru());
57 
58     // Flatten
59     std::vector<uint8_t> newData;
60     Stream newStream{newData};
61 
62     callouts.flatten(newStream);
63     EXPECT_EQ(data, newData);
64 }
65 
66 TEST(CalloutsTest, BadDataTest)
67 {
68     // Start out with a valid 2 callout object, then truncate it.
69     std::vector<uint8_t> data{0xC0, 0x00, 0x00,
70                               0x00}; // ID, flags, length in words
71 
72     // Add 2 callouts
73     auto callout = srcDataFactory(TestSRCType::calloutStructureA);
74     data.insert(data.end(), callout.begin(), callout.end());
75 
76     callout = srcDataFactory(TestSRCType::calloutStructureB);
77     data.insert(data.end(), callout.begin(), callout.end());
78 
79     Stream stream{data};
80 
81     // Set the actual word length value at offset 2
82     uint16_t wordLength = data.size() / 4;
83     stream.offset(2);
84     stream << wordLength;
85     stream.offset(0);
86 
87     // Shorten the data by an arbitrary amount so unflattening goes awry.
88     data.resize(data.size() - 37);
89 
90     EXPECT_THROW(Callouts callouts{stream}, std::out_of_range);
91 }
92