blob: f7c1446a847932a3c0cb4e227be5a1c4efec38ef [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
Patrick Williams2544b412022-10-04 08:41:06 -050024{};
Matt Spinler289aa472019-09-20 12:33:29 -050025
26TEST_F(PrivateHeaderTest, SizeTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050027{
28 EXPECT_EQ(PrivateHeader::flattenedSize(), 48);
29}
30
Matt Spinler289aa472019-09-20 12:33:29 -050031TEST_F(PrivateHeaderTest, UnflattenFlattenTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -050032{
Matt Spinler42828bd2019-10-11 10:39:30 -050033 auto data = pelDataFactory(TestPELType::privateHeaderSection);
Matt Spinlerd3335df2019-07-10 11:04:21 -050034
Matt Spinler42828bd2019-10-11 10:39:30 -050035 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -050036 PrivateHeader ph(stream);
37 EXPECT_EQ(ph.valid(), true);
38
39 EXPECT_EQ(ph.header().id, 0x5048);
40 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
41 EXPECT_EQ(ph.header().version, 0x01);
42 EXPECT_EQ(ph.header().subType, 0x02);
43 EXPECT_EQ(ph.header().componentID, 0x0304);
44
Matt Spinler97d19b42019-10-29 11:34:03 -050045 auto ct = ph.createTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050046 EXPECT_EQ(ct.yearMSB, 0x20);
47 EXPECT_EQ(ct.yearLSB, 0x30);
48 EXPECT_EQ(ct.month, 0x05);
49 EXPECT_EQ(ct.day, 0x09);
Matt Spinler289aa472019-09-20 12:33:29 -050050 EXPECT_EQ(ct.hour, 0x11);
Matt Spinlerd3335df2019-07-10 11:04:21 -050051 EXPECT_EQ(ct.minutes, 0x1E);
52 EXPECT_EQ(ct.seconds, 0x01);
53 EXPECT_EQ(ct.hundredths, 0x63);
54
Matt Spinler97d19b42019-10-29 11:34:03 -050055 auto mt = ph.commitTimestamp();
Matt Spinlerd3335df2019-07-10 11:04:21 -050056 EXPECT_EQ(mt.yearMSB, 0x20);
57 EXPECT_EQ(mt.yearLSB, 0x31);
58 EXPECT_EQ(mt.month, 0x06);
59 EXPECT_EQ(mt.day, 0x0F);
60 EXPECT_EQ(mt.hour, 0x09);
61 EXPECT_EQ(mt.minutes, 0x22);
62 EXPECT_EQ(mt.seconds, 0x3A);
63 EXPECT_EQ(mt.hundredths, 0x00);
64
Matt Spinler48c44db2020-08-25 12:47:13 -050065 EXPECT_EQ(ph.creatorID(), 0x4F);
Matt Spinlerd3335df2019-07-10 11:04:21 -050066 EXPECT_EQ(ph.logType(), 0x00);
67 EXPECT_EQ(ph.sectionCount(), 0x02);
68 EXPECT_EQ(ph.obmcLogID(), 0x90919293);
69
70 char expected[] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x00};
71 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
72
73 EXPECT_EQ(ph.plid(), 0x50515253);
74 EXPECT_EQ(ph.id(), 0x80818283);
75
76 // Now flatten into a vector and check that this vector
77 // matches the original one.
78 std::vector<uint8_t> newData;
79 Stream newStream(newData);
80
Matt Spinlercf5a8d02019-09-05 12:58:53 -050081 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050082 EXPECT_EQ(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050083
84 // Change a field, then flatten and unflatten again
Matt Spinler97d19b42019-10-29 11:34:03 -050085 ph.setID(0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050086
87 newStream.offset(0);
88 newData.clear();
Matt Spinlercf5a8d02019-09-05 12:58:53 -050089 ph.flatten(newStream);
Matt Spinler42828bd2019-10-11 10:39:30 -050090 EXPECT_NE(data, newData);
Matt Spinlerd3335df2019-07-10 11:04:21 -050091
92 newStream.offset(0);
93 PrivateHeader newPH(newStream);
94
95 EXPECT_TRUE(newPH.valid());
Matt Spinler97d19b42019-10-29 11:34:03 -050096 EXPECT_EQ(newPH.id(), 0x55);
Matt Spinlerd3335df2019-07-10 11:04:21 -050097}
98
Matt Spinler289aa472019-09-20 12:33:29 -050099TEST_F(PrivateHeaderTest, ShortDataTest)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500100{
Matt Spinler42828bd2019-10-11 10:39:30 -0500101 auto data = pelDataFactory(TestPELType::privateHeaderSection);
102 data.resize(PrivateHeader::flattenedSize() - 1);
103 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500104
105 PrivateHeader ph(stream);
106
107 EXPECT_EQ(ph.valid(), false);
108}
109
Matt Spinler289aa472019-09-20 12:33:29 -0500110TEST_F(PrivateHeaderTest, CorruptDataTest1)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500111{
Matt Spinler42828bd2019-10-11 10:39:30 -0500112 auto data = pelDataFactory(TestPELType::privateHeaderSection);
113 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500114
Matt Spinler42828bd2019-10-11 10:39:30 -0500115 data.at(0) = 0; // corrupt the section ID
Matt Spinlerd3335df2019-07-10 11:04:21 -0500116
117 PrivateHeader ph(stream);
118
119 EXPECT_EQ(ph.valid(), false);
120}
121
Matt Spinler289aa472019-09-20 12:33:29 -0500122TEST_F(PrivateHeaderTest, CorruptDataTest2)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500123{
Matt Spinler42828bd2019-10-11 10:39:30 -0500124 auto data = pelDataFactory(TestPELType::privateHeaderSection);
125 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500126
Matt Spinler42828bd2019-10-11 10:39:30 -0500127 data.at(4) = 0x22; // corrupt the version
Matt Spinlerd3335df2019-07-10 11:04:21 -0500128
129 PrivateHeader ph(stream);
130
131 EXPECT_EQ(ph.valid(), false);
132}
133
Matt Spinler289aa472019-09-20 12:33:29 -0500134TEST_F(PrivateHeaderTest, CorruptDataTest3)
Matt Spinlerd3335df2019-07-10 11:04:21 -0500135{
Matt Spinler42828bd2019-10-11 10:39:30 -0500136 auto data = pelDataFactory(TestPELType::privateHeaderSection);
137 Stream stream(data);
Matt Spinlerd3335df2019-07-10 11:04:21 -0500138
Matt Spinler42828bd2019-10-11 10:39:30 -0500139 data.at(27) = 1; // corrupt the section count
Matt Spinlerd3335df2019-07-10 11:04:21 -0500140
141 PrivateHeader ph(stream);
142
143 EXPECT_EQ(ph.valid(), false);
144}
Matt Spinler289aa472019-09-20 12:33:29 -0500145
146// Construct a PrivateHeader from scratch
147TEST_F(PrivateHeaderTest, ConstructionTest)
148{
149 tm time_tm;
150 time_tm.tm_year = 125;
151 time_tm.tm_mon = 11;
152 time_tm.tm_mday = 31;
153 time_tm.tm_hour = 15;
154 time_tm.tm_min = 23;
155 time_tm.tm_sec = 42;
156 time_tm.tm_isdst = 0;
157
158 // Convert the above time into a uint64_t in ms since the epoch time
159 auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
160 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
161 timepoint.time_since_epoch())
162 .count();
163
164 PrivateHeader ph(0x3300, 42, timestamp);
165
166 EXPECT_TRUE(ph.valid());
167 EXPECT_EQ(ph.header().id, 0x5048);
168 EXPECT_EQ(ph.header().size, PrivateHeader::flattenedSize());
169 EXPECT_EQ(ph.header().version, 0x01);
170 EXPECT_EQ(ph.header().subType, 0x00);
171 EXPECT_EQ(ph.header().componentID, 0x3300);
172
173 auto& ct = ph.createTimestamp();
174 EXPECT_EQ(ct.yearMSB, 0x20);
175 EXPECT_EQ(ct.yearLSB, 0x25);
176 EXPECT_EQ(ct.month, 0x12);
177 EXPECT_EQ(ct.day, 0x31);
178 EXPECT_EQ(ct.hour, 0x15);
179 EXPECT_EQ(ct.minutes, 0x23);
180 EXPECT_EQ(ct.seconds, 0x42);
181 EXPECT_EQ(ct.hundredths, 0x00);
182
183 EXPECT_EQ(ph.creatorID(), 'O');
184 EXPECT_EQ(ph.logType(), 0x00);
185 EXPECT_EQ(ph.sectionCount(), 0x01);
186 EXPECT_EQ(ph.obmcLogID(), 42);
187
188 char expected[] = {0, 0, 0, 0, 0, 0, 0, 0};
189 EXPECT_TRUE(memcmp(ph.creatorVersion().version, expected, 8) == 0);
190
191 EXPECT_EQ(ph.id(), 0x50000001);
192 EXPECT_EQ(ph.id(), ph.plid());
193}