blob: 4eb052ba8f0f77231ebf71dcf3e80492e6eab2ac [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
Matt Spinler289aa472019-09-20 12:33:29 -05008class PrivateHeaderTest : public CleanLogID
9{
10};
11
12TEST_F(PrivateHeaderTest, SizeTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050013{
14 EXPECT_EQ(PrivateHeader::flattenedSize(), 48);
15}
16
Matt Spinler289aa472019-09-20 12:33:29 -050017TEST_F(PrivateHeaderTest, UnflattenFlattenTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050018{
Matt Spinler42828bd2019-10-11 10:39:30 -050019 auto data = pelDataFactory(TestPELType::privateHeaderSection);
Matt Spinlerd3335df2019-07-10 11:04:21 -050020
Matt Spinler42828bd2019-10-11 10:39:30 -050021 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -050022 PrivateHeader ph(stream);
23 EXPECT_EQ(ph.valid(), true);
24
25 EXPECT_EQ(ph.header().id, 0x5048);
26 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
27 EXPECT_EQ(ph.header().version, 0x01);
28 EXPECT_EQ(ph.header().subType, 0x02);
29 EXPECT_EQ(ph.header().componentID, 0x0304);
30
Matt Spinler97d19b42019-10-29 11:34:03 -050031 auto ct = ph.createTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050032 EXPECT_EQ(ct.yearMSB, 0x20);
33 EXPECT_EQ(ct.yearLSB, 0x30);
34 EXPECT_EQ(ct.month, 0x05);
35 EXPECT_EQ(ct.day, 0x09);
Matt Spinler289aa472019-09-20 12:33:29 -050036 EXPECT_EQ(ct.hour, 0x11);
Matt Spinlerd3335df2019-07-10 11:04:21 -050037 EXPECT_EQ(ct.minutes, 0x1E);
38 EXPECT_EQ(ct.seconds, 0x01);
39 EXPECT_EQ(ct.hundredths, 0x63);
40
Matt Spinler97d19b42019-10-29 11:34:03 -050041 auto mt = ph.commitTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050042 EXPECT_EQ(mt.yearMSB, 0x20);
43 EXPECT_EQ(mt.yearLSB, 0x31);
44 EXPECT_EQ(mt.month, 0x06);
45 EXPECT_EQ(mt.day, 0x0F);
46 EXPECT_EQ(mt.hour, 0x09);
47 EXPECT_EQ(mt.minutes, 0x22);
48 EXPECT_EQ(mt.seconds, 0x3A);
49 EXPECT_EQ(mt.hundredths, 0x00);
50
51 EXPECT_EQ(ph.creatorID(), 0xAA);
52 EXPECT_EQ(ph.logType(), 0x00);
53 EXPECT_EQ(ph.sectionCount(), 0x02);
54 EXPECT_EQ(ph.obmcLogID(), 0x90919293);
55
56 char expected[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00};
57 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
58
59 EXPECT_EQ(ph.plid(), 0x50515253);
60 EXPECT_EQ(ph.id(), 0x80818283);
61
62 // Now flatten into a vector and check that this vector
63 // matches the original one.
64 std::vector<uint8_t> newData;
65 Stream newStream(newData);
66
Matt Spinlercf5a8d02019-09-05 12:58:53 -050067 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050068 EXPECT_EQ(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050069
70 // Change a field, then flatten and unflatten again
Matt Spinler97d19b42019-10-29 11:34:03 -050071 ph.setID(0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050072
73 newStream.offset(0);
74 newData.clear();
Matt Spinlercf5a8d02019-09-05 12:58:53 -050075 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050076 EXPECT_NE(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050077
78 newStream.offset(0);
79 PrivateHeader newPH(newStream);
80
81 EXPECT_TRUE(newPH.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -050082 EXPECT_EQ(newPH.id(), 0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050083}
84
Matt Spinler289aa472019-09-20 12:33:29 -050085TEST_F(PrivateHeaderTest, ShortDataTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050086{
Matt Spinler42828bd2019-10-11 10:39:30 -050087 auto data = pelDataFactory(TestPELType::privateHeaderSection);
88 data.resize(PrivateHeader::flattenedSize() - 1);
89 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -050090
91 PrivateHeader ph(stream);
92
93 EXPECT_EQ(ph.valid(), false);
94}
95
Matt Spinler289aa472019-09-20 12:33:29 -050096TEST_F(PrivateHeaderTest, CorruptDataTest1)
Matt Spinlerd3335df2019-07-10 11:04:21 -050097{
Matt Spinler42828bd2019-10-11 10:39:30 -050098 auto data = pelDataFactory(TestPELType::privateHeaderSection);
99 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500100
Matt Spinler42828bd2019-10-11 10:39:30 -0500101 data.at(0) = 0; // corrupt the section ID
Matt Spinlerd3335df2019-07-10 11:04:21 -0500102
103 PrivateHeader ph(stream);
104
105 EXPECT_EQ(ph.valid(), false);
106}
107
Matt Spinler289aa472019-09-20 12:33:29 -0500108TEST_F(PrivateHeaderTest, CorruptDataTest2)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500109{
Matt Spinler42828bd2019-10-11 10:39:30 -0500110 auto data = pelDataFactory(TestPELType::privateHeaderSection);
111 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500112
Matt Spinler42828bd2019-10-11 10:39:30 -0500113 data.at(4) = 0x22; // corrupt the version
Matt Spinlerd3335df2019-07-10 11:04:21 -0500114
115 PrivateHeader ph(stream);
116
117 EXPECT_EQ(ph.valid(), false);
118}
119
Matt Spinler289aa472019-09-20 12:33:29 -0500120TEST_F(PrivateHeaderTest, CorruptDataTest3)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500121{
Matt Spinler42828bd2019-10-11 10:39:30 -0500122 auto data = pelDataFactory(TestPELType::privateHeaderSection);
123 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500124
Matt Spinler42828bd2019-10-11 10:39:30 -0500125 data.at(27) = 1; // corrupt the section count
Matt Spinlerd3335df2019-07-10 11:04:21 -0500126
127 PrivateHeader ph(stream);
128
129 EXPECT_EQ(ph.valid(), false);
130}
Matt Spinler289aa472019-09-20 12:33:29 -0500131
132// Construct a PrivateHeader from scratch
133TEST_F(PrivateHeaderTest, ConstructionTest)
134{
135 tm time_tm;
136 time_tm.tm_year = 125;
137 time_tm.tm_mon = 11;
138 time_tm.tm_mday = 31;
139 time_tm.tm_hour = 15;
140 time_tm.tm_min = 23;
141 time_tm.tm_sec = 42;
142 time_tm.tm_isdst = 0;
143
144 // Convert the above time into a uint64_t in ms since the epoch time
145 auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
146 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
147 timepoint.time_since_epoch())
148 .count();
149
150 PrivateHeader ph(0x3300, 42, timestamp);
151
152 EXPECT_TRUE(ph.valid());
153 EXPECT_EQ(ph.header().id, 0x5048);
154 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
155 EXPECT_EQ(ph.header().version, 0x01);
156 EXPECT_EQ(ph.header().subType, 0x00);
157 EXPECT_EQ(ph.header().componentID, 0x3300);
158
159 auto& ct = ph.createTimestamp();
160 EXPECT_EQ(ct.yearMSB, 0x20);
161 EXPECT_EQ(ct.yearLSB, 0x25);
162 EXPECT_EQ(ct.month, 0x12);
163 EXPECT_EQ(ct.day, 0x31);
164 EXPECT_EQ(ct.hour, 0x15);
165 EXPECT_EQ(ct.minutes, 0x23);
166 EXPECT_EQ(ct.seconds, 0x42);
167 EXPECT_EQ(ct.hundredths, 0x00);
168
169 EXPECT_EQ(ph.creatorID(), 'O');
170 EXPECT_EQ(ph.logType(), 0x00);
171 EXPECT_EQ(ph.sectionCount(), 0x01);
172 EXPECT_EQ(ph.obmcLogID(), 42);
173
174 char expected[] = {0, 0, 0, 0, 0, 0, 0, 0};
175 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
176
177 EXPECT_EQ(ph.id(), 0x50000001);
178 EXPECT_EQ(ph.id(), ph.plid());
179}