blob: 26361b9d82f966f694714065457f9595a01fa631 [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{
Matt Spinleraacc2aa2021-05-25 09:31:35 -060039 std::vector<uint8_t> value{0x32, 0x44, 0x33, 0x35}; // "2D35"
Bob Kinga76898f2020-10-13 15:08:33 +080040 CompareVPDAction action{
41 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -060042 value};
Bob Kinga76898f2020-10-13 15:08:33 +080043 EXPECT_EQ(action.getFRU(),
44 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
Bob King8e4cb642020-04-24 13:18:53 +080045 EXPECT_EQ(action.getKeyword(), "CCIN");
Matt Spinleraacc2aa2021-05-25 09:31:35 -060046 EXPECT_EQ(action.getValue(), value);
Bob King8e4cb642020-04-24 13:18:53 +080047}
48
49TEST(CompareVPDActionTests, Execute)
50{
Shawn McCarney45907cc2021-02-05 17:33:11 -060051 // 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 Spinleraacc2aa2021-05-25 09:31:35 -060055 std::vector<uint8_t> abcdValue{0x41, 0x42, 0x43, 0x44};
Shawn McCarney45907cc2021-02-05 17:33:11 -060056
57 // Create MockServices object. VPD service will return "ABCD" as VPD
Matt Spinleraacc2aa2021-05-25 09:31:35 -060058 // value 3 times.
Shawn McCarney45907cc2021-02-05 17:33:11 -060059 MockServices services{};
60 MockVPD& vpd = services.getMockVPD();
61 EXPECT_CALL(vpd, getValue(fru, keyword))
Matt Spinleraacc2aa2021-05-25 09:31:35 -060062 .Times(3)
63 .WillRepeatedly(Return(abcdValue));
Shawn McCarney45907cc2021-02-05 17:33:11 -060064
65 IDMap idMap{};
66 ActionEnvironment environment{idMap, "", services};
67
68 // Test where returns true: actual value == expected value
69 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -060070 CompareVPDAction action{fru, keyword, abcdValue};
Shawn McCarney45907cc2021-02-05 17:33:11 -060071 EXPECT_TRUE(action.execute(environment));
72 }
73
74 // Test where returns false: actual value != expected value
75 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -060076 CompareVPDAction action{fru, keyword,
77 std::vector<uint8_t>{1, 2, 3, 4}};
Shawn McCarney45907cc2021-02-05 17:33:11 -060078 EXPECT_FALSE(action.execute(environment));
79 }
80
Matt Spinleraacc2aa2021-05-25 09:31:35 -060081 // Test where returns false: expected value is empty
Shawn McCarney45907cc2021-02-05 17:33:11 -060082 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -060083 CompareVPDAction action{fru, keyword, std::vector<uint8_t>{}};
Shawn McCarney45907cc2021-02-05 17:33:11 -060084 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 Spinleraacc2aa2021-05-25 09:31:35 -060092 std::vector<uint8_t> emptyValue{};
Shawn McCarney45907cc2021-02-05 17:33:11 -060093
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 Spinleraacc2aa2021-05-25 09:31:35 -0600100 .WillRepeatedly(Return(emptyValue));
Shawn McCarney45907cc2021-02-05 17:33:11 -0600101
102 IDMap idMap{};
103 ActionEnvironment environment{idMap, "", services};
104
105 // Test where returns true: actual value == expected value
106 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600107 CompareVPDAction action{fru, keyword, emptyValue};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600108 EXPECT_TRUE(action.execute(environment));
109 }
110
111 // Test where returns false: actual value != expected value
112 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600113 CompareVPDAction action{fru, keyword,
114 std::vector<uint8_t>{1, 2, 3}};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600115 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 Spinleraacc2aa2021-05-25 09:31:35 -0600137 CompareVPDAction action{fru, keyword,
138 std::vector<uint8_t>{1, 2, 3}};
Shawn McCarney45907cc2021-02-05 17:33:11 -0600139 action.execute(environment);
140 ADD_FAILURE() << "Should not have reached this line.";
141 }
142 catch (const ActionError& e)
143 {
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600144 EXPECT_STREQ(e.what(),
145 "ActionError: compare_vpd: { fru: "
146 "/xyz/openbmc_project/inventory/system, "
147 "keyword: Model, value: [ 0x1, 0x2, 0x3 ] }");
Shawn McCarney45907cc2021-02-05 17:33:11 -0600148 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 King8e4cb642020-04-24 13:18:53 +0800168}
169
170TEST(CompareVPDActionTests, GetFRU)
171{
Bob Kinga76898f2020-10-13 15:08:33 +0800172 CompareVPDAction action{
173 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600174 std::vector<uint8_t>{1, 2, 3, 4}};
Bob Kinga76898f2020-10-13 15:08:33 +0800175 EXPECT_EQ(action.getFRU(),
176 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane");
Bob King8e4cb642020-04-24 13:18:53 +0800177}
178
179TEST(CompareVPDActionTests, GetKeyword)
180{
Bob Kinga76898f2020-10-13 15:08:33 +0800181 CompareVPDAction action{
182 "/xyz/openbmc_project/inventory/system/chassis/disk_backplane", "CCIN",
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600183 std::vector<uint8_t>{1, 2, 3, 4}};
Bob King8e4cb642020-04-24 13:18:53 +0800184 EXPECT_EQ(action.getKeyword(), "CCIN");
185}
186
187TEST(CompareVPDActionTests, GetValue)
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}};
192 EXPECT_EQ(action.getValue(), (std::vector<uint8_t>{0x1, 0x2, 0x3, 0x4}));
Bob King8e4cb642020-04-24 13:18:53 +0800193}
194
195TEST(CompareVPDActionTests, ToString)
196{
Matt Spinleraacc2aa2021-05-25 09:31:35 -0600197 {
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 King8e4cb642020-04-24 13:18:53 +0800216}