blob: 31a1b616bff19ca6755d783f43e1da22798cdf47 [file] [log] [blame]
Matt Spinler97f7abc2019-11-06 09:40:23 -06001/**
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 */
Matt Spinlerd3335df2019-07-10 11:04:21 -050016#include "extensions/openpower-pels/private_header.hpp"
17#include "pel_utils.hpp"
18
19#include <gtest/gtest.h>
20
21using namespace openpower::pels;
22
Matt Spinler289aa472019-09-20 12:33:29 -050023class PrivateHeaderTest : public CleanLogID
24{
25};
26
27TEST_F(PrivateHeaderTest, SizeTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050028{
29 EXPECT_EQ(PrivateHeader::flattenedSize(), 48);
30}
31
Matt Spinler289aa472019-09-20 12:33:29 -050032TEST_F(PrivateHeaderTest, UnflattenFlattenTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050033{
Matt Spinler42828bd2019-10-11 10:39:30 -050034 auto data = pelDataFactory(TestPELType::privateHeaderSection);
Matt Spinlerd3335df2019-07-10 11:04:21 -050035
Matt Spinler42828bd2019-10-11 10:39:30 -050036 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -050037 PrivateHeader ph(stream);
38 EXPECT_EQ(ph.valid(), true);
39
40 EXPECT_EQ(ph.header().id, 0x5048);
41 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
42 EXPECT_EQ(ph.header().version, 0x01);
43 EXPECT_EQ(ph.header().subType, 0x02);
44 EXPECT_EQ(ph.header().componentID, 0x0304);
45
Matt Spinler97d19b42019-10-29 11:34:03 -050046 auto ct = ph.createTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050047 EXPECT_EQ(ct.yearMSB, 0x20);
48 EXPECT_EQ(ct.yearLSB, 0x30);
49 EXPECT_EQ(ct.month, 0x05);
50 EXPECT_EQ(ct.day, 0x09);
Matt Spinler289aa472019-09-20 12:33:29 -050051 EXPECT_EQ(ct.hour, 0x11);
Matt Spinlerd3335df2019-07-10 11:04:21 -050052 EXPECT_EQ(ct.minutes, 0x1E);
53 EXPECT_EQ(ct.seconds, 0x01);
54 EXPECT_EQ(ct.hundredths, 0x63);
55
Matt Spinler97d19b42019-10-29 11:34:03 -050056 auto mt = ph.commitTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050057 EXPECT_EQ(mt.yearMSB, 0x20);
58 EXPECT_EQ(mt.yearLSB, 0x31);
59 EXPECT_EQ(mt.month, 0x06);
60 EXPECT_EQ(mt.day, 0x0F);
61 EXPECT_EQ(mt.hour, 0x09);
62 EXPECT_EQ(mt.minutes, 0x22);
63 EXPECT_EQ(mt.seconds, 0x3A);
64 EXPECT_EQ(mt.hundredths, 0x00);
65
Matt Spinler48c44db2020-08-25 12:47:13 -050066 EXPECT_EQ(ph.creatorID(), 0x4F);
Matt Spinlerd3335df2019-07-10 11:04:21 -050067 EXPECT_EQ(ph.logType(), 0x00);
68 EXPECT_EQ(ph.sectionCount(), 0x02);
69 EXPECT_EQ(ph.obmcLogID(), 0x90919293);
70
71 char expected[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00};
72 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
73
74 EXPECT_EQ(ph.plid(), 0x50515253);
75 EXPECT_EQ(ph.id(), 0x80818283);
76
77 // Now flatten into a vector and check that this vector
78 // matches the original one.
79 std::vector<uint8_t> newData;
80 Stream newStream(newData);
81
Matt Spinlercf5a8d02019-09-05 12:58:53 -050082 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050083 EXPECT_EQ(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050084
85 // Change a field, then flatten and unflatten again
Matt Spinler97d19b42019-10-29 11:34:03 -050086 ph.setID(0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050087
88 newStream.offset(0);
89 newData.clear();
Matt Spinlercf5a8d02019-09-05 12:58:53 -050090 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050091 EXPECT_NE(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050092
93 newStream.offset(0);
94 PrivateHeader newPH(newStream);
95
96 EXPECT_TRUE(newPH.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -050097 EXPECT_EQ(newPH.id(), 0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050098}
99
Matt Spinler289aa472019-09-20 12:33:29 -0500100TEST_F(PrivateHeaderTest, ShortDataTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500101{
Matt Spinler42828bd2019-10-11 10:39:30 -0500102 auto data = pelDataFactory(TestPELType::privateHeaderSection);
103 data.resize(PrivateHeader::flattenedSize() - 1);
104 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500105
106 PrivateHeader ph(stream);
107
108 EXPECT_EQ(ph.valid(), false);
109}
110
Matt Spinler289aa472019-09-20 12:33:29 -0500111TEST_F(PrivateHeaderTest, CorruptDataTest1)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500112{
Matt Spinler42828bd2019-10-11 10:39:30 -0500113 auto data = pelDataFactory(TestPELType::privateHeaderSection);
114 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500115
Matt Spinler42828bd2019-10-11 10:39:30 -0500116 data.at(0) = 0; // corrupt the section ID
Matt Spinlerd3335df2019-07-10 11:04:21 -0500117
118 PrivateHeader ph(stream);
119
120 EXPECT_EQ(ph.valid(), false);
121}
122
Matt Spinler289aa472019-09-20 12:33:29 -0500123TEST_F(PrivateHeaderTest, CorruptDataTest2)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500124{
Matt Spinler42828bd2019-10-11 10:39:30 -0500125 auto data = pelDataFactory(TestPELType::privateHeaderSection);
126 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500127
Matt Spinler42828bd2019-10-11 10:39:30 -0500128 data.at(4) = 0x22; // corrupt the version
Matt Spinlerd3335df2019-07-10 11:04:21 -0500129
130 PrivateHeader ph(stream);
131
132 EXPECT_EQ(ph.valid(), false);
133}
134
Matt Spinler289aa472019-09-20 12:33:29 -0500135TEST_F(PrivateHeaderTest, CorruptDataTest3)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500136{
Matt Spinler42828bd2019-10-11 10:39:30 -0500137 auto data = pelDataFactory(TestPELType::privateHeaderSection);
138 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500139
Matt Spinler42828bd2019-10-11 10:39:30 -0500140 data.at(27) = 1; // corrupt the section count
Matt Spinlerd3335df2019-07-10 11:04:21 -0500141
142 PrivateHeader ph(stream);
143
144 EXPECT_EQ(ph.valid(), false);
145}
Matt Spinler289aa472019-09-20 12:33:29 -0500146
147// Construct a PrivateHeader from scratch
148TEST_F(PrivateHeaderTest, ConstructionTest)
149{
150 tm time_tm;
151 time_tm.tm_year = 125;
152 time_tm.tm_mon = 11;
153 time_tm.tm_mday = 31;
154 time_tm.tm_hour = 15;
155 time_tm.tm_min = 23;
156 time_tm.tm_sec = 42;
157 time_tm.tm_isdst = 0;
158
159 // Convert the above time into a uint64_t in ms since the epoch time
160 auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
161 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
162 timepoint.time_since_epoch())
163 .count();
164
165 PrivateHeader ph(0x3300, 42, timestamp);
166
167 EXPECT_TRUE(ph.valid());
168 EXPECT_EQ(ph.header().id, 0x5048);
169 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
170 EXPECT_EQ(ph.header().version, 0x01);
171 EXPECT_EQ(ph.header().subType, 0x00);
172 EXPECT_EQ(ph.header().componentID, 0x3300);
173
174 auto& ct = ph.createTimestamp();
175 EXPECT_EQ(ct.yearMSB, 0x20);
176 EXPECT_EQ(ct.yearLSB, 0x25);
177 EXPECT_EQ(ct.month, 0x12);
178 EXPECT_EQ(ct.day, 0x31);
179 EXPECT_EQ(ct.hour, 0x15);
180 EXPECT_EQ(ct.minutes, 0x23);
181 EXPECT_EQ(ct.seconds, 0x42);
182 EXPECT_EQ(ct.hundredths, 0x00);
183
184 EXPECT_EQ(ph.creatorID(), 'O');
185 EXPECT_EQ(ph.logType(), 0x00);
186 EXPECT_EQ(ph.sectionCount(), 0x01);
187 EXPECT_EQ(ph.obmcLogID(), 42);
188
189 char expected[] = {0, 0, 0, 0, 0, 0, 0, 0};
190 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
191
192 EXPECT_EQ(ph.id(), 0x50000001);
193 EXPECT_EQ(ph.id(), ph.plid());
194}