blob: c90c09ec825dc0bc45c50a49752a5d60cfe8e3fb [file] [log] [blame]
Bob King8e4cb642020-04-24 13:18:53 +08001/**
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 McCarney45907cc2021-02-05 17:33:11 -060017#include "action_error.hpp"
Bob King8e4cb642020-04-24 13:18:53 +080018#include "compare_vpd_action.hpp"
19#include "id_map.hpp"
Shawn McCarney45907cc2021-02-05 17:33:11 -060020#include "mock_services.hpp"
21#include "mock_vpd.hpp"
Bob King8e4cb642020-04-24 13:18:53 +080022
23#include <exception>
24#include <memory>
25#include <stdexcept>
Shawn McCarney45907cc2021-02-05 17:33:11 -060026#include <string>
Bob King8e4cb642020-04-24 13:18:53 +080027#include <utility>
28
Shawn McCarney45907cc2021-02-05 17:33:11 -060029#include <gmock/gmock.h>
Bob King8e4cb642020-04-24 13:18:53 +080030#include <gtest/gtest.h>
31
Shawn McCarney45907cc2021-02-05 17:33:11 -060032using ::testing::Return;
33using ::testing::Throw;
34
Bob King8e4cb642020-04-24 13:18:53 +080035using namespace phosphor::power::regulators;
36
37TEST(CompareVPDActionTests, Constructor)
38{
Shawn McCarneya2a830b2021-10-30 14:24:31 -050039 // Value vector is not empty
40 {
41 std::vector<uint8_t> value{0x32, 0x44, 0x33, 0x35}; // "2D35"
42 CompareVPDAction action{
43 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane",
44 "CCIN", value};
45 EXPECT_EQ(
46 action.getFRU(),
47 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
48 EXPECT_EQ(action.getKeyword(), "CCIN");
49 EXPECT_EQ(action.getValue(), value);
50 }
51
52 // Value vector is empty
53 {
54 std::vector<uint8_t> value{};
55 CompareVPDAction action{
56 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane",
57 "CCIN", value};
58 EXPECT_EQ(
59 action.getFRU(),
60 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
61 EXPECT_EQ(action.getKeyword(), "CCIN");
62 EXPECT_EQ(action.getValue(), value);
63 }
Bob King8e4cb642020-04-24 13:18:53 +080064}
65
66TEST(CompareVPDActionTests, Execute)
67{
Shawn McCarneya2a830b2021-10-30 14:24:31 -050068 // Test where works: Actual VPD value is not empty
Shawn McCarney45907cc2021-02-05 17:33:11 -060069 {
70 std::string fru{"/xyz/openbmc_project/inventory/system"};
71 std::string keyword{"Model"};
Matt Spinleraacc2aa2021-05-25 09:31:35 -060072 std::vector<uint8_t> abcdValue{0x41, 0x42, 0x43, 0x44};
Shawn McCarney45907cc2021-02-05 17:33:11 -060073
74 // Create MockServices object. VPD service will return "ABCD" as VPD
Matt Spinleraacc2aa2021-05-25 09:31:35 -060075 // value 3 times.
Shawn McCarney45907cc2021-02-05 17:33:11 -060076 MockServices services{};
77 MockVPD& vpd = services.getMockVPD();
78 EXPECT_CALL(vpd, getValue(fru, keyword))
Matt Spinleraacc2aa2021-05-25 09:31:35 -060079 .Times(3)
80 .WillRepeatedly(Return(abcdValue));
Shawn McCarney45907cc2021-02-05 17:33:11 -060081
82 IDMap idMap{};
83 ActionEnvironment environment{idMap, "", services};
84
85 // Test where returns true: actual value == expected value
86 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -060087 CompareVPDAction action{fru, keyword, abcdValue};
Shawn McCarney45907cc2021-02-05 17:33:11 -060088 EXPECT_TRUE(action.execute(environment));
89 }
90
91 // Test where returns false: actual value != expected value
92 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -060093 CompareVPDAction action{fru, keyword,
94 std::vector<uint8_t>{1, 2, 3, 4}};
Shawn McCarney45907cc2021-02-05 17:33:11 -060095 EXPECT_FALSE(action.execute(environment));
96 }
97
Matt Spinleraacc2aa2021-05-25 09:31:35 -060098 // Test where returns false: expected value is empty
Shawn McCarney45907cc2021-02-05 17:33:11 -060099 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600100 CompareVPDAction action{fru, keyword, std::vector<uint8_t>{}};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600101 EXPECT_FALSE(action.execute(environment));
102 }
103 }
104
Shawn McCarneya2a830b2021-10-30 14:24:31 -0500105 // Test where works: Actual VPD value is empty
Shawn McCarney45907cc2021-02-05 17:33:11 -0600106 {
107 std::string fru{"/xyz/openbmc_project/inventory/system"};
108 std::string keyword{"Model"};
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600109 std::vector<uint8_t> emptyValue{};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600110
Shawn McCarneya2a830b2021-10-30 14:24:31 -0500111 // Create MockServices object. VPD service will return empty VPD value
Shawn McCarney45907cc2021-02-05 17:33:11 -0600112 // 2 times.
113 MockServices services{};
114 MockVPD& vpd = services.getMockVPD();
115 EXPECT_CALL(vpd, getValue(fru, keyword))
116 .Times(2)
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600117 .WillRepeatedly(Return(emptyValue));
Shawn McCarney45907cc2021-02-05 17:33:11 -0600118
119 IDMap idMap{};
120 ActionEnvironment environment{idMap, "", services};
121
122 // Test where returns true: actual value == expected value
123 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600124 CompareVPDAction action{fru, keyword, emptyValue};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600125 EXPECT_TRUE(action.execute(environment));
126 }
127
128 // Test where returns false: actual value != expected value
129 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600130 CompareVPDAction action{fru, keyword,
131 std::vector<uint8_t>{1, 2, 3}};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600132 EXPECT_FALSE(action.execute(environment));
133 }
134 }
135
136 // Test where fails: Exception thrown when trying to get actual VPD value
137 {
138 std::string fru{"/xyz/openbmc_project/inventory/system"};
139 std::string keyword{"Model"};
140
141 // Create MockServices object. VPD service will throw an exception.
142 MockServices services{};
143 MockVPD& vpd = services.getMockVPD();
144 EXPECT_CALL(vpd, getValue(fru, keyword))
145 .Times(1)
146 .WillOnce(
147 Throw(std::runtime_error{"D-Bus error: Invalid object path"}));
148
149 IDMap idMap{};
150 ActionEnvironment environment{idMap, "", services};
151
152 try
153 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600154 CompareVPDAction action{fru, keyword,
155 std::vector<uint8_t>{1, 2, 3}};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600156 action.execute(environment);
157 ADD_FAILURE() << "Should not have reached this line.";
158 }
159 catch (const ActionError& e)
160 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600161 EXPECT_STREQ(e.what(),
162 "ActionError: compare_vpd: { fru: "
163 "/xyz/openbmc_project/inventory/system, "
164 "keyword: Model, value: [ 0x1, 0x2, 0x3 ] }");
Shawn McCarney45907cc2021-02-05 17:33:11 -0600165 try
166 {
167 // Re-throw inner exception
168 std::rethrow_if_nested(e);
169 ADD_FAILURE() << "Should not have reached this line.";
170 }
171 catch (const std::runtime_error& re)
172 {
173 EXPECT_STREQ(re.what(), "D-Bus error: Invalid object path");
174 }
175 catch (...)
176 {
177 ADD_FAILURE() << "Should not have caught exception.";
178 }
179 }
180 catch (...)
181 {
182 ADD_FAILURE() << "Should not have caught exception.";
183 }
184 }
Bob King8e4cb642020-04-24 13:18:53 +0800185}
186
187TEST(CompareVPDActionTests, GetFRU)
188{
Bob Kinga76898f2020-10-13 15:08:33 +0800189 CompareVPDAction action{
190 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600191 std::vector<uint8_t>{1, 2, 3, 4}};
Bob Kinga76898f2020-10-13 15:08:33 +0800192 EXPECT_EQ(action.getFRU(),
193 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
Bob King8e4cb642020-04-24 13:18:53 +0800194}
195
196TEST(CompareVPDActionTests, GetKeyword)
197{
Bob Kinga76898f2020-10-13 15:08:33 +0800198 CompareVPDAction action{
199 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600200 std::vector<uint8_t>{1, 2, 3, 4}};
Bob King8e4cb642020-04-24 13:18:53 +0800201 EXPECT_EQ(action.getKeyword(), "CCIN");
202}
203
204TEST(CompareVPDActionTests, GetValue)
205{
Bob Kinga76898f2020-10-13 15:08:33 +0800206 CompareVPDAction action{
207 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600208 std::vector<uint8_t>{1, 2, 3, 4}};
209 EXPECT_EQ(action.getValue(), (std::vector<uint8_t>{0x1, 0x2, 0x3, 0x4}));
Bob King8e4cb642020-04-24 13:18:53 +0800210}
211
212TEST(CompareVPDActionTests, ToString)
213{
Shawn McCarneya2a830b2021-10-30 14:24:31 -0500214 // Test where value vector is not empty
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600215 {
216 CompareVPDAction action{
217 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane",
218 "CCIN", std::vector<uint8_t>{0x01, 0xA3, 0x0, 0xFF}};
219 EXPECT_EQ(action.toString(), "compare_vpd: { fru: "
220 "/xyz/openbmc_project/inventory/system/"
221 "chassis/disk_backplane, keyword: "
222 "CCIN, value: [ 0x1, 0xA3, 0x0, 0xFF ] }");
223 }
224
Shawn McCarneya2a830b2021-10-30 14:24:31 -0500225 // Test where value vector is empty
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600226 {
227 CompareVPDAction action{
228 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane",
229 "CCIN", std::vector<uint8_t>{}};
230 EXPECT_EQ(action.toString(), "compare_vpd: { fru: "
231 "/xyz/openbmc_project/inventory/system/"
232 "chassis/disk_backplane, keyword: "
233 "CCIN, value: [ ] }");
234 }
Bob King8e4cb642020-04-24 13:18:53 +0800235}