Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 1 | #include "extensions/openpower-pels/private_header.hpp" |
| 2 | #include "pel_utils.hpp" |
| 3 | |
| 4 | #include <gtest/gtest.h> |
| 5 | |
| 6 | using namespace openpower::pels; |
| 7 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 8 | class PrivateHeaderTest : public CleanLogID |
| 9 | { |
| 10 | }; |
| 11 | |
| 12 | TEST_F(PrivateHeaderTest, SizeTest) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 13 | { |
| 14 | EXPECT_EQ(PrivateHeader::flattenedSize(), 48); |
| 15 | } |
| 16 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 17 | TEST_F(PrivateHeaderTest, UnflattenFlattenTest) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 18 | { |
| 19 | auto data = pelDataFactory(TestPelType::privateHeaderSimple); |
| 20 | |
| 21 | Stream stream(*data); |
| 22 | 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 | |
| 31 | auto& ct = ph.createTimestamp(); |
| 32 | EXPECT_EQ(ct.yearMSB, 0x20); |
| 33 | EXPECT_EQ(ct.yearLSB, 0x30); |
| 34 | EXPECT_EQ(ct.month, 0x05); |
| 35 | EXPECT_EQ(ct.day, 0x09); |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 36 | EXPECT_EQ(ct.hour, 0x11); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 37 | EXPECT_EQ(ct.minutes, 0x1E); |
| 38 | EXPECT_EQ(ct.seconds, 0x01); |
| 39 | EXPECT_EQ(ct.hundredths, 0x63); |
| 40 | |
| 41 | auto& mt = ph.commitTimestamp(); |
| 42 | 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 Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 67 | ph.flatten(newStream); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 68 | EXPECT_EQ(*data, newData); |
| 69 | |
| 70 | // Change a field, then flatten and unflatten again |
| 71 | ph.creatorID() = 0x55; |
| 72 | |
| 73 | newStream.offset(0); |
| 74 | newData.clear(); |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 75 | ph.flatten(newStream); |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 76 | EXPECT_NE(*data, newData); |
| 77 | |
| 78 | newStream.offset(0); |
| 79 | PrivateHeader newPH(newStream); |
| 80 | |
| 81 | EXPECT_TRUE(newPH.valid()); |
| 82 | EXPECT_EQ(newPH.creatorID(), 0x55); |
| 83 | } |
| 84 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 85 | TEST_F(PrivateHeaderTest, ShortDataTest) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 86 | { |
| 87 | auto data = pelDataFactory(TestPelType::privateHeaderSimple); |
| 88 | data->resize(PrivateHeader::flattenedSize() - 1); |
| 89 | Stream stream(*data); |
| 90 | |
| 91 | PrivateHeader ph(stream); |
| 92 | |
| 93 | EXPECT_EQ(ph.valid(), false); |
| 94 | } |
| 95 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 96 | TEST_F(PrivateHeaderTest, CorruptDataTest1) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 97 | { |
| 98 | auto data = pelDataFactory(TestPelType::privateHeaderSimple); |
| 99 | Stream stream(*data); |
| 100 | |
| 101 | data->at(0) = 0; // corrupt the section ID |
| 102 | |
| 103 | PrivateHeader ph(stream); |
| 104 | |
| 105 | EXPECT_EQ(ph.valid(), false); |
| 106 | } |
| 107 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 108 | TEST_F(PrivateHeaderTest, CorruptDataTest2) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 109 | { |
| 110 | auto data = pelDataFactory(TestPelType::privateHeaderSimple); |
| 111 | Stream stream(*data); |
| 112 | |
| 113 | data->at(4) = 0x22; // corrupt the version |
| 114 | |
| 115 | PrivateHeader ph(stream); |
| 116 | |
| 117 | EXPECT_EQ(ph.valid(), false); |
| 118 | } |
| 119 | |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 120 | TEST_F(PrivateHeaderTest, CorruptDataTest3) |
Matt Spinler | d3335df | 2019-07-10 11:04:21 -0500 | [diff] [blame] | 121 | { |
| 122 | auto data = pelDataFactory(TestPelType::privateHeaderSimple); |
| 123 | Stream stream(*data); |
| 124 | |
| 125 | data->at(27) = 1; // corrupt the section count |
| 126 | |
| 127 | PrivateHeader ph(stream); |
| 128 | |
| 129 | EXPECT_EQ(ph.valid(), false); |
| 130 | } |
Matt Spinler | 289aa47 | 2019-09-20 12:33:29 -0500 | [diff] [blame] | 131 | |
| 132 | // Construct a PrivateHeader from scratch |
| 133 | TEST_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 | } |