blob: 88dd721d720a9849d3fd3a2e69f9295ea138c7da [file] [log] [blame]
Matt Spinler32f13c92019-10-09 12:48:25 -05001#include "extensions/openpower-pels/callouts.hpp"
2#include "pel_utils.hpp"
3
4#include <gtest/gtest.h>
5
6using namespace openpower::pels;
7using namespace openpower::pels::src;
8
9TEST(CalloutsTest, UnflattenFlattenTest)
10{
11 std::vector<uint8_t> data{0xC0, 0x00, 0x00,
12 0x00}; // ID, flags, length in words
13
14 // Add 2 callouts
15 auto callout = srcDataFactory(TestSRCType::calloutStructureA);
16 data.insert(data.end(), callout.begin(), callout.end());
17
18 callout = srcDataFactory(TestSRCType::calloutStructureB);
19 data.insert(data.end(), callout.begin(), callout.end());
20
21 Stream stream{data};
22
23 // Set the actual word length value at offset 2
24 uint16_t wordLength = data.size() / 4;
25 stream.offset(2);
26 stream << wordLength;
27 stream.offset(0);
28
29 Callouts callouts{stream};
30
31 EXPECT_EQ(callouts.flattenedSize(), data.size());
32 EXPECT_EQ(callouts.callouts().size(), 2);
33
34 // spot check that each callout has the right substructures
35 EXPECT_TRUE(callouts.callouts().front()->fruIdentity());
36 EXPECT_FALSE(callouts.callouts().front()->pceIdentity());
37 EXPECT_FALSE(callouts.callouts().front()->mru());
38
39 EXPECT_TRUE(callouts.callouts().back()->fruIdentity());
40 EXPECT_TRUE(callouts.callouts().back()->pceIdentity());
41 EXPECT_TRUE(callouts.callouts().back()->mru());
42
43 // Flatten
44 std::vector<uint8_t> newData;
45 Stream newStream{newData};
46
47 callouts.flatten(newStream);
48 EXPECT_EQ(data, newData);
49}
50
51TEST(CalloutsTest, BadDataTest)
52{
53 // Start out with a valid 2 callout object, then truncate it.
54 std::vector<uint8_t> data{0xC0, 0x00, 0x00,
55 0x00}; // ID, flags, length in words
56
57 // Add 2 callouts
58 auto callout = srcDataFactory(TestSRCType::calloutStructureA);
59 data.insert(data.end(), callout.begin(), callout.end());
60
61 callout = srcDataFactory(TestSRCType::calloutStructureB);
62 data.insert(data.end(), callout.begin(), callout.end());
63
64 Stream stream{data};
65
66 // Set the actual word length value at offset 2
67 uint16_t wordLength = data.size() / 4;
68 stream.offset(2);
69 stream << wordLength;
70 stream.offset(0);
71
72 // Shorten the data by an arbitrary amount so unflattening goes awry.
73 data.resize(data.size() - 37);
74
75 EXPECT_THROW(Callouts callouts{stream}, std::out_of_range);
76}