blob: b09e3faf55dc9ce18993802d03eed6d84108378b [file] [log] [blame]
Shawn McCarneyf1c90612020-02-24 09:56:53 -06001/**
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"
17#include "action_error.hpp"
18#include "device.hpp"
19#include "i2c_interface.hpp"
20#include "i2c_write_byte_action.hpp"
21#include "id_map.hpp"
Bob King73eacee2020-10-23 13:58:02 +080022#include "mock_services.hpp"
Shawn McCarneyf1c90612020-02-24 09:56:53 -060023#include "mocked_i2c_interface.hpp"
24
25#include <cstdint>
26#include <memory>
27#include <stdexcept>
28#include <string>
29#include <utility>
30
31#include <gmock/gmock.h>
32#include <gtest/gtest.h>
33
34using namespace phosphor::power::regulators;
35
36using ::testing::A;
37using ::testing::Return;
38using ::testing::SetArgReferee;
39using ::testing::Throw;
40using ::testing::TypedEq;
41
42TEST(I2CWriteByteActionTests, Constructor)
43{
44 // Test where mask is not specified
45 {
46 I2CWriteByteAction action{0x7C, 0x0A};
47 EXPECT_EQ(action.getRegister(), 0x7C);
48 EXPECT_EQ(action.getValue(), 0x0A);
49 EXPECT_EQ(action.getMask(), 0xFF);
50 }
51
52 // Test where mask is specified
53 {
54 I2CWriteByteAction action{0xA0, 0xD6, 0xC3};
55 EXPECT_EQ(action.getRegister(), 0xA0);
56 EXPECT_EQ(action.getValue(), 0xD6);
57 EXPECT_EQ(action.getMask(), 0xC3);
58 }
59}
60
61TEST(I2CWriteByteActionTests, Execute)
62{
63 // Test where works: Mask not specified
64 try
65 {
66 // Create mock I2CInterface
67 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
68 std::make_unique<i2c::MockedI2CInterface>();
69 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
70 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
71 EXPECT_CALL(*i2cInterface,
72 write(TypedEq<uint8_t>(0x7C), TypedEq<uint8_t>(0x0A)))
73 .Times(1);
74
Bob King73eacee2020-10-23 13:58:02 +080075 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +080076 Device device{
77 "reg1", true,
78 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
79 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -060080 IDMap idMap{};
81 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +080082 MockServices services{};
83 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -060084
85 I2CWriteByteAction action{0x7C, 0x0A};
86 EXPECT_EQ(action.execute(env), true);
87 }
88 catch (...)
89 {
90 ADD_FAILURE() << "Should not have caught exception.";
91 }
92
93 // Test where works: Mask specified
94 try
95 {
96 // Create mock I2CInterface: read() returns value 0x69
97 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
98 std::make_unique<i2c::MockedI2CInterface>();
99 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
100 EXPECT_CALL(*i2cInterface, read(0xA0, A<uint8_t&>()))
101 .Times(1)
102 .WillOnce(SetArgReferee<1>(0x69));
103 EXPECT_CALL(*i2cInterface,
104 write(TypedEq<uint8_t>(0xA0), TypedEq<uint8_t>(0xEA)))
105 .Times(1);
106
Bob King73eacee2020-10-23 13:58:02 +0800107 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800108 Device device{
109 "reg1", true,
110 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
111 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600112 IDMap idMap{};
113 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800114 MockServices services{};
115 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600116
117 // Value to write : 0xD6 = 1101 0110
118 // Mask : 0xC3 = 1100 0011
119 // Current value : 0x69 = 0110 1001
120 // Value to write & mask: 0xC2 = 1100 0010
121 // ~Mask : 0x3C = 0011 1100
122 // Current value & ~mask: 0x28 = 0010 1000
123 // Final value to write : 0xEA = 1110 1010
124 I2CWriteByteAction action{0xA0, 0xD6, 0xC3};
125 EXPECT_EQ(action.execute(env), true);
126 }
127 catch (...)
128 {
129 ADD_FAILURE() << "Should not have caught exception.";
130 }
131
132 // Test where fails: Getting I2CInterface fails
133 try
134 {
Bob King73eacee2020-10-23 13:58:02 +0800135
136 // Create IDMap, MockServices, and ActionEnvironment
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600137 IDMap idMap{};
Bob King73eacee2020-10-23 13:58:02 +0800138 MockServices services{};
139 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600140
141 I2CWriteByteAction action{0x7C, 0x0A};
142 action.execute(env);
143 ADD_FAILURE() << "Should not have reached this line.";
144 }
145 catch (const std::invalid_argument& e)
146 {
147 EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
148 }
149 catch (...)
150 {
151 ADD_FAILURE() << "Should not have caught exception.";
152 }
153
154 // Test where fails: Reading byte fails
155 try
156 {
157 // Create mock I2CInterface: read() throws an I2CException
158 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
159 std::make_unique<i2c::MockedI2CInterface>();
160 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
161 EXPECT_CALL(*i2cInterface, read(0xA0, A<uint8_t&>()))
162 .Times(1)
163 .WillOnce(Throw(
164 i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
165 EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint8_t>())).Times(0);
166
Bob King73eacee2020-10-23 13:58:02 +0800167 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800168 Device device{
169 "reg1", true,
170 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
171 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600172 IDMap idMap{};
173 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800174 MockServices services{};
175 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600176
177 I2CWriteByteAction action{0xA0, 0xD6, 0xC3};
178 action.execute(env);
179 ADD_FAILURE() << "Should not have reached this line.";
180 }
181 catch (const ActionError& e)
182 {
183 EXPECT_STREQ(e.what(), "ActionError: i2c_write_byte: { register: "
184 "0xA0, value: 0xD6, mask: 0xC3 }");
185 try
186 {
187 // Re-throw inner I2CException
188 std::rethrow_if_nested(e);
189 ADD_FAILURE() << "Should not have reached this line.";
190 }
191 catch (const i2c::I2CException& ie)
192 {
193 EXPECT_STREQ(
194 ie.what(),
195 "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
196 }
197 catch (...)
198 {
199 ADD_FAILURE() << "Should not have caught exception.";
200 }
201 }
202 catch (...)
203 {
204 ADD_FAILURE() << "Should not have caught exception.";
205 }
206
207 // Test where fails: Writing byte fails
208 try
209 {
210 // Create mock I2CInterface: write() throws an I2CException
211 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
212 std::make_unique<i2c::MockedI2CInterface>();
213 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
214 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
215 EXPECT_CALL(*i2cInterface,
216 write(TypedEq<uint8_t>(0x7C), TypedEq<uint8_t>(0x1A)))
217 .Times(1)
218 .WillOnce(Throw(
219 i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
220
Bob King73eacee2020-10-23 13:58:02 +0800221 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800222 Device device{
223 "reg1", true,
224 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
225 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600226 IDMap idMap{};
227 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800228 MockServices services{};
229 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600230
231 I2CWriteByteAction action{0x7C, 0x1A};
232 action.execute(env);
233 ADD_FAILURE() << "Should not have reached this line.";
234 }
235 catch (const ActionError& e)
236 {
237 EXPECT_STREQ(e.what(), "ActionError: i2c_write_byte: { register: "
238 "0x7C, value: 0x1A, mask: 0xFF }");
239 try
240 {
241 // Re-throw inner I2CException
242 std::rethrow_if_nested(e);
243 ADD_FAILURE() << "Should not have reached this line.";
244 }
245 catch (const i2c::I2CException& ie)
246 {
247 EXPECT_STREQ(ie.what(), "I2CException: Failed to write byte: bus "
248 "/dev/i2c-1, addr 0x70");
249 }
250 catch (...)
251 {
252 ADD_FAILURE() << "Should not have caught exception.";
253 }
254 }
255 catch (...)
256 {
257 ADD_FAILURE() << "Should not have caught exception.";
258 }
259}
260
261TEST(I2CWriteByteActionTests, GetRegister)
262{
263 I2CWriteByteAction action{0x7C, 0xDE};
264 EXPECT_EQ(action.getRegister(), 0x7C);
265}
266
267TEST(I2CWriteByteActionTests, GetValue)
268{
269 I2CWriteByteAction action{0xA0, 0x03, 0x47};
270 EXPECT_EQ(action.getValue(), 0x03);
271}
272
273TEST(I2CWriteByteActionTests, GetMask)
274{
275 // Test where mask is not specified
276 {
277 I2CWriteByteAction action{0x7C, 0xDE};
278 EXPECT_EQ(action.getMask(), 0xFF);
279 }
280
281 // Test where mask is specified
282 {
283 I2CWriteByteAction action{0xA0, 0x03, 0x47};
284 EXPECT_EQ(action.getMask(), 0x47);
285 }
286}
287
288TEST(I2CWriteByteActionTests, ToString)
289{
290 I2CWriteByteAction action{0x7C, 0xDE, 0xFB};
291 EXPECT_EQ(action.toString(),
292 "i2c_write_byte: { register: 0x7C, value: 0xDE, mask: 0xFB }");
293}