Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 | */ |
| 16 | #include "action_environment.hpp" |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 17 | #include "action_error.hpp" |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 18 | #include "compare_vpd_action.hpp" |
| 19 | #include "id_map.hpp" |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 20 | #include "mock_services.hpp" |
| 21 | #include "mock_vpd.hpp" |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 22 | |
| 23 | #include <exception> |
| 24 | #include <memory> |
| 25 | #include <stdexcept> |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 26 | #include <string> |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 27 | #include <utility> |
| 28 | |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 29 | #include <gmock/gmock.h> |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 30 | #include <gtest/gtest.h> |
| 31 | |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 32 | using ::testing::Return; |
| 33 | using ::testing::Throw; |
| 34 | |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 35 | using namespace phosphor::power::regulators; |
| 36 | |
| 37 | TEST(CompareVPDActionTests, Constructor) |
| 38 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 39 | std::vector<uint8_t> value{0x32, 0x44, 0x33, 0x35}; // "2D35" |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 40 | CompareVPDAction action{ |
| 41 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 42 | value}; |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 43 | EXPECT_EQ(action.getFRU(), |
| 44 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane"); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 45 | EXPECT_EQ(action.getKeyword(), "CCIN"); |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 46 | EXPECT_EQ(action.getValue(), value); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | TEST(CompareVPDActionTests, Execute) |
| 50 | { |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 51 | // Test where works: Actual VPD value is not an empty string |
| 52 | { |
| 53 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 54 | std::string keyword{"Model"}; |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 55 | std::vector<uint8_t> abcdValue{0x41, 0x42, 0x43, 0x44}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 56 | |
| 57 | // Create MockServices object. VPD service will return "ABCD" as VPD |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 58 | // value 3 times. |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 59 | MockServices services{}; |
| 60 | MockVPD& vpd = services.getMockVPD(); |
| 61 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 62 | .Times(3) |
| 63 | .WillRepeatedly(Return(abcdValue)); |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 64 | |
| 65 | IDMap idMap{}; |
| 66 | ActionEnvironment environment{idMap, "", services}; |
| 67 | |
| 68 | // Test where returns true: actual value == expected value |
| 69 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 70 | CompareVPDAction action{fru, keyword, abcdValue}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 71 | EXPECT_TRUE(action.execute(environment)); |
| 72 | } |
| 73 | |
| 74 | // Test where returns false: actual value != expected value |
| 75 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 76 | CompareVPDAction action{fru, keyword, |
| 77 | std::vector<uint8_t>{1, 2, 3, 4}}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 78 | EXPECT_FALSE(action.execute(environment)); |
| 79 | } |
| 80 | |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 81 | // Test where returns false: expected value is empty |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 82 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 83 | CompareVPDAction action{fru, keyword, std::vector<uint8_t>{}}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 84 | EXPECT_FALSE(action.execute(environment)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Test where works: Actual VPD value is an empty string |
| 89 | { |
| 90 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 91 | std::string keyword{"Model"}; |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 92 | std::vector<uint8_t> emptyValue{}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 93 | |
| 94 | // Create MockServices object. VPD service will return "" as VPD value |
| 95 | // 2 times. |
| 96 | MockServices services{}; |
| 97 | MockVPD& vpd = services.getMockVPD(); |
| 98 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
| 99 | .Times(2) |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 100 | .WillRepeatedly(Return(emptyValue)); |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 101 | |
| 102 | IDMap idMap{}; |
| 103 | ActionEnvironment environment{idMap, "", services}; |
| 104 | |
| 105 | // Test where returns true: actual value == expected value |
| 106 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 107 | CompareVPDAction action{fru, keyword, emptyValue}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 108 | EXPECT_TRUE(action.execute(environment)); |
| 109 | } |
| 110 | |
| 111 | // Test where returns false: actual value != expected value |
| 112 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 113 | CompareVPDAction action{fru, keyword, |
| 114 | std::vector<uint8_t>{1, 2, 3}}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 115 | EXPECT_FALSE(action.execute(environment)); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Test where fails: Exception thrown when trying to get actual VPD value |
| 120 | { |
| 121 | std::string fru{"/xyz/openbmc_project/inventory/system"}; |
| 122 | std::string keyword{"Model"}; |
| 123 | |
| 124 | // Create MockServices object. VPD service will throw an exception. |
| 125 | MockServices services{}; |
| 126 | MockVPD& vpd = services.getMockVPD(); |
| 127 | EXPECT_CALL(vpd, getValue(fru, keyword)) |
| 128 | .Times(1) |
| 129 | .WillOnce( |
| 130 | Throw(std::runtime_error{"D-Bus error: Invalid object path"})); |
| 131 | |
| 132 | IDMap idMap{}; |
| 133 | ActionEnvironment environment{idMap, "", services}; |
| 134 | |
| 135 | try |
| 136 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 137 | CompareVPDAction action{fru, keyword, |
| 138 | std::vector<uint8_t>{1, 2, 3}}; |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 139 | action.execute(environment); |
| 140 | ADD_FAILURE() << "Should not have reached this line."; |
| 141 | } |
| 142 | catch (const ActionError& e) |
| 143 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 144 | EXPECT_STREQ(e.what(), |
| 145 | "ActionError: compare_vpd: { fru: " |
| 146 | "/xyz/openbmc_project/inventory/system, " |
| 147 | "keyword: Model, value: [ 0x1, 0x2, 0x3 ] }"); |
Shawn McCarney | 45907cc | 2021-02-05 17:33:11 -0600 | [diff] [blame] | 148 | try |
| 149 | { |
| 150 | // Re-throw inner exception |
| 151 | std::rethrow_if_nested(e); |
| 152 | ADD_FAILURE() << "Should not have reached this line."; |
| 153 | } |
| 154 | catch (const std::runtime_error& re) |
| 155 | { |
| 156 | EXPECT_STREQ(re.what(), "D-Bus error: Invalid object path"); |
| 157 | } |
| 158 | catch (...) |
| 159 | { |
| 160 | ADD_FAILURE() << "Should not have caught exception."; |
| 161 | } |
| 162 | } |
| 163 | catch (...) |
| 164 | { |
| 165 | ADD_FAILURE() << "Should not have caught exception."; |
| 166 | } |
| 167 | } |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST(CompareVPDActionTests, GetFRU) |
| 171 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 172 | CompareVPDAction action{ |
| 173 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 174 | std::vector<uint8_t>{1, 2, 3, 4}}; |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 175 | EXPECT_EQ(action.getFRU(), |
| 176 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane"); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | TEST(CompareVPDActionTests, GetKeyword) |
| 180 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 181 | CompareVPDAction action{ |
| 182 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 183 | std::vector<uint8_t>{1, 2, 3, 4}}; |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 184 | EXPECT_EQ(action.getKeyword(), "CCIN"); |
| 185 | } |
| 186 | |
| 187 | TEST(CompareVPDActionTests, GetValue) |
| 188 | { |
Bob King | a76898f | 2020-10-13 15:08:33 +0800 | [diff] [blame] | 189 | CompareVPDAction action{ |
| 190 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN", |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 191 | std::vector<uint8_t>{1, 2, 3, 4}}; |
| 192 | EXPECT_EQ(action.getValue(), (std::vector<uint8_t>{0x1, 0x2, 0x3, 0x4})); |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | TEST(CompareVPDActionTests, ToString) |
| 196 | { |
Matt Spinler | aacc2aa | 2021-05-25 09:31:35 -0600 | [diff] [blame] | 197 | { |
| 198 | CompareVPDAction action{ |
| 199 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", |
| 200 | "CCIN", std::vector<uint8_t>{0x01, 0xA3, 0x0, 0xFF}}; |
| 201 | EXPECT_EQ(action.toString(), "compare_vpd: { fru: " |
| 202 | "/xyz/openbmc_project/inventory/system/" |
| 203 | "chassis/disk_backplane, keyword: " |
| 204 | "CCIN, value: [ 0x1, 0xA3, 0x0, 0xFF ] }"); |
| 205 | } |
| 206 | |
| 207 | { |
| 208 | CompareVPDAction action{ |
| 209 | "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", |
| 210 | "CCIN", std::vector<uint8_t>{}}; |
| 211 | EXPECT_EQ(action.toString(), "compare_vpd: { fru: " |
| 212 | "/xyz/openbmc_project/inventory/system/" |
| 213 | "chassis/disk_backplane, keyword: " |
| 214 | "CCIN, value: [ ] }"); |
| 215 | } |
Bob King | 8e4cb64 | 2020-04-24 13:18:53 +0800 | [diff] [blame] | 216 | } |