blob: 40a1ad28a9e26ab658888ee39a36e4c0ff0e960a [file] [log] [blame]
Matt Spinlerd3335df2019-07-10 11:04:21 -05001#include "extensions/openpower-pels/private_header.hpp"
2#include "pel_utils.hpp"
3
4#include <gtest/gtest.h>
5
6using namespace openpower::pels;
7
8TEST(PrivateHeaderTest, SizeTest)
9{
10 EXPECT_EQ(PrivateHeader::flattenedSize(), 48);
11}
12
13TEST(PrivateHeaderTest, UnflattenFlattenTest)
14{
15 auto data = pelDataFactory(TestPelType::privateHeaderSimple);
16
17 Stream stream(*data);
18 PrivateHeader ph(stream);
19 EXPECT_EQ(ph.valid(), true);
20
21 EXPECT_EQ(ph.header().id, 0x5048);
22 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
23 EXPECT_EQ(ph.header().version, 0x01);
24 EXPECT_EQ(ph.header().subType, 0x02);
25 EXPECT_EQ(ph.header().componentID, 0x0304);
26
27 auto& ct = ph.createTimestamp();
28 EXPECT_EQ(ct.yearMSB, 0x20);
29 EXPECT_EQ(ct.yearLSB, 0x30);
30 EXPECT_EQ(ct.month, 0x05);
31 EXPECT_EQ(ct.day, 0x09);
32 EXPECT_EQ(ct.hour, 0x011);
33 EXPECT_EQ(ct.minutes, 0x1E);
34 EXPECT_EQ(ct.seconds, 0x01);
35 EXPECT_EQ(ct.hundredths, 0x63);
36
37 auto& mt = ph.commitTimestamp();
38 EXPECT_EQ(mt.yearMSB, 0x20);
39 EXPECT_EQ(mt.yearLSB, 0x31);
40 EXPECT_EQ(mt.month, 0x06);
41 EXPECT_EQ(mt.day, 0x0F);
42 EXPECT_EQ(mt.hour, 0x09);
43 EXPECT_EQ(mt.minutes, 0x22);
44 EXPECT_EQ(mt.seconds, 0x3A);
45 EXPECT_EQ(mt.hundredths, 0x00);
46
47 EXPECT_EQ(ph.creatorID(), 0xAA);
48 EXPECT_EQ(ph.logType(), 0x00);
49 EXPECT_EQ(ph.sectionCount(), 0x02);
50 EXPECT_EQ(ph.obmcLogID(), 0x90919293);
51
52 char expected[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00};
53 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
54
55 EXPECT_EQ(ph.plid(), 0x50515253);
56 EXPECT_EQ(ph.id(), 0x80818283);
57
58 // Now flatten into a vector and check that this vector
59 // matches the original one.
60 std::vector<uint8_t> newData;
61 Stream newStream(newData);
62
63 newStream << ph;
64 EXPECT_EQ(*data, newData);
65
66 // Change a field, then flatten and unflatten again
67 ph.creatorID() = 0x55;
68
69 newStream.offset(0);
70 newData.clear();
71 newStream << ph;
72 EXPECT_NE(*data, newData);
73
74 newStream.offset(0);
75 PrivateHeader newPH(newStream);
76
77 EXPECT_TRUE(newPH.valid());
78 EXPECT_EQ(newPH.creatorID(), 0x55);
79}
80
81TEST(PrivateHeaderTest, ShortDataTest)
82{
83 auto data = pelDataFactory(TestPelType::privateHeaderSimple);
84 data->resize(PrivateHeader::flattenedSize() - 1);
85 Stream stream(*data);
86
87 PrivateHeader ph(stream);
88
89 EXPECT_EQ(ph.valid(), false);
90}
91
92TEST(PrivateHeaderTest, CorruptDataTest1)
93{
94 auto data = pelDataFactory(TestPelType::privateHeaderSimple);
95 Stream stream(*data);
96
97 data->at(0) = 0; // corrupt the section ID
98
99 PrivateHeader ph(stream);
100
101 EXPECT_EQ(ph.valid(), false);
102}
103
104TEST(PrivateHeaderTest, CorruptDataTest2)
105{
106 auto data = pelDataFactory(TestPelType::privateHeaderSimple);
107 Stream stream(*data);
108
109 data->at(4) = 0x22; // corrupt the version
110
111 PrivateHeader ph(stream);
112
113 EXPECT_EQ(ph.valid(), false);
114}
115
116TEST(PrivateHeaderTest, CorruptDataTest3)
117{
118 auto data = pelDataFactory(TestPelType::privateHeaderSimple);
119 Stream stream(*data);
120
121 data->at(27) = 1; // corrupt the section count
122
123 PrivateHeader ph(stream);
124
125 EXPECT_EQ(ph.valid(), false);
126}