Matt Spinler | 97f7abc | 2019-11-06 09:40:23 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 16 | #include "elog_entry.hpp" |
| 17 | #include "extensions/openpower-pels/pel_types.hpp" |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 18 | #include "extensions/openpower-pels/private_header.hpp" |
| 19 | #include "extensions/openpower-pels/user_header.hpp" |
| 20 | #include "pel_utils.hpp" |
| 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
| 24 | using namespace openpower::pels; |
| 25 | |
| 26 | TEST(UserHeaderTest, SizeTest) |
| 27 | { |
| 28 | EXPECT_EQ(UserHeader::flattenedSize(), 24); |
| 29 | } |
| 30 | |
| 31 | TEST(UserHeaderTest, UnflattenFlattenTest) |
| 32 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 33 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 34 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 35 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 36 | UserHeader uh(stream); |
| 37 | EXPECT_EQ(uh.valid(), true); |
| 38 | |
| 39 | EXPECT_EQ(uh.header().id, 0x5548); |
| 40 | EXPECT_EQ(uh.header().size, UserHeader::flattenedSize()); |
| 41 | EXPECT_EQ(uh.header().version, 0x01); |
| 42 | EXPECT_EQ(uh.header().subType, 0x0A); |
| 43 | EXPECT_EQ(uh.header().componentID, 0x0B0C); |
| 44 | |
| 45 | EXPECT_EQ(uh.subsystem(), 0x10); |
| 46 | EXPECT_EQ(uh.scope(), 0x04); |
| 47 | EXPECT_EQ(uh.severity(), 0x20); |
| 48 | EXPECT_EQ(uh.eventType(), 0x00); |
| 49 | EXPECT_EQ(uh.problemDomain(), 0x03); |
| 50 | EXPECT_EQ(uh.problemVector(), 0x04); |
| 51 | EXPECT_EQ(uh.actionFlags(), 0x80C0); |
| 52 | |
| 53 | // Now flatten into a vector and check that this vector |
| 54 | // matches the original one. |
| 55 | std::vector<uint8_t> newData; |
| 56 | Stream newStream(newData); |
| 57 | |
Matt Spinler | cf5a8d0 | 2019-09-05 12:58:53 -0500 | [diff] [blame] | 58 | uh.flatten(newStream); |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 59 | EXPECT_EQ(data, newData); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TEST(UserHeaderTest, ShortDataTest) |
| 63 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 64 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
| 65 | data.resize(data.size() - 1); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 66 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 67 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 68 | UserHeader uh(stream); |
| 69 | |
| 70 | EXPECT_EQ(uh.valid(), false); |
| 71 | } |
| 72 | |
| 73 | TEST(UserHeaderTest, CorruptDataTest1) |
| 74 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 75 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
| 76 | data.resize(data.size() - 1); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 77 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 78 | data.at(0) = 0; // corrupt the section ID |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 79 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 80 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 81 | UserHeader uh(stream); |
| 82 | |
| 83 | EXPECT_EQ(uh.valid(), false); |
| 84 | } |
| 85 | |
| 86 | TEST(UserHeaderTest, CorruptDataTest2) |
| 87 | { |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 88 | auto data = pelDataFactory(TestPELType::userHeaderSection); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 89 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 90 | data.at(4) = 0x22; // corrupt the version |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 91 | |
Matt Spinler | 42828bd | 2019-10-11 10:39:30 -0500 | [diff] [blame] | 92 | Stream stream(data); |
Matt Spinler | 03c1d91 | 2019-07-10 14:12:15 -0500 | [diff] [blame] | 93 | UserHeader uh(stream); |
| 94 | |
| 95 | EXPECT_EQ(uh.valid(), false); |
| 96 | } |
Matt Spinler | fdb6a20 | 2019-09-20 14:09:20 -0500 | [diff] [blame] | 97 | |
| 98 | // Construct the User Header from the message registry |
| 99 | TEST(UserHeaderTest, ConstructionTest) |
| 100 | { |
| 101 | using namespace openpower::pels::message; |
| 102 | Entry regEntry; |
| 103 | |
| 104 | regEntry.name = "test"; |
| 105 | regEntry.subsystem = 5; |
| 106 | regEntry.severity = 0x40; |
| 107 | regEntry.actionFlags = 0xC000; |
| 108 | regEntry.eventType = 1; |
| 109 | regEntry.eventScope = 2; |
| 110 | |
| 111 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error); |
| 112 | |
| 113 | ASSERT_TRUE(uh.valid()); |
| 114 | EXPECT_EQ(uh.header().id, 0x5548); |
| 115 | EXPECT_EQ(uh.header().size, UserHeader::flattenedSize()); |
| 116 | EXPECT_EQ(uh.header().version, 0x01); |
| 117 | EXPECT_EQ(uh.header().subType, 0x00); |
| 118 | EXPECT_EQ(uh.header().componentID, |
| 119 | static_cast<uint16_t>(ComponentID::phosphorLogging)); |
| 120 | |
| 121 | ASSERT_EQ(uh.subsystem(), 5); |
| 122 | ASSERT_EQ(uh.severity(), 0x40); |
| 123 | ASSERT_EQ(uh.eventType(), 1); |
| 124 | ASSERT_EQ(uh.scope(), 2); |
| 125 | ASSERT_EQ(uh.problemDomain(), 0); |
| 126 | ASSERT_EQ(uh.problemVector(), 0); |
| 127 | ASSERT_EQ(uh.actionFlags(), 0xC000); |
| 128 | } |
| 129 | |
| 130 | // Test that the severity comes from the event log if not |
| 131 | // in the message registry |
| 132 | TEST(UserHeaderTest, UseEventLogSevTest) |
| 133 | { |
| 134 | using namespace openpower::pels::message; |
| 135 | Entry regEntry; |
| 136 | |
| 137 | regEntry.name = "test"; |
| 138 | regEntry.subsystem = 5; |
| 139 | regEntry.actionFlags = 0xC000; |
| 140 | regEntry.eventType = 1; |
| 141 | regEntry.eventScope = 2; |
| 142 | // Leave off severity |
| 143 | |
| 144 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error); |
| 145 | ASSERT_EQ(uh.severity(), 0x40); |
| 146 | } |
| 147 | |
| 148 | // Test that the optional event type & scope fields work |
| 149 | TEST(UserHeaderTest, DefaultEventTypeScopeTest) |
| 150 | { |
| 151 | using namespace openpower::pels::message; |
| 152 | Entry regEntry; |
| 153 | |
| 154 | regEntry.name = "test"; |
| 155 | regEntry.subsystem = 5; |
| 156 | regEntry.severity = 0x40; |
| 157 | regEntry.actionFlags = 0xC000; |
| 158 | |
| 159 | UserHeader uh(regEntry, phosphor::logging::Entry::Level::Error); |
| 160 | |
| 161 | ASSERT_EQ(uh.eventType(), 0); |
| 162 | ASSERT_EQ(uh.scope(), 0x03); |
| 163 | } |