blob: 986ca2e6f04078c1f3a002c96b4f1d4a92bb6aa8 [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 // Create IDMap, MockServices, and ActionEnvironment
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600136 IDMap idMap{};
Bob King73eacee2020-10-23 13:58:02 +0800137 MockServices services{};
138 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600139
140 I2CWriteByteAction action{0x7C, 0x0A};
141 action.execute(env);
142 ADD_FAILURE() << "Should not have reached this line.";
143 }
144 catch (const std::invalid_argument& e)
145 {
146 EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\"");
147 }
148 catch (...)
149 {
150 ADD_FAILURE() << "Should not have caught exception.";
151 }
152
153 // Test where fails: Reading byte fails
154 try
155 {
156 // Create mock I2CInterface: read() throws an I2CException
157 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
158 std::make_unique<i2c::MockedI2CInterface>();
159 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
160 EXPECT_CALL(*i2cInterface, read(0xA0, A<uint8_t&>()))
161 .Times(1)
162 .WillOnce(Throw(
163 i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70}));
164 EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint8_t>())).Times(0);
165
Bob King73eacee2020-10-23 13:58:02 +0800166 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800167 Device device{
168 "reg1", true,
169 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
170 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600171 IDMap idMap{};
172 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800173 MockServices services{};
174 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600175
176 I2CWriteByteAction action{0xA0, 0xD6, 0xC3};
177 action.execute(env);
178 ADD_FAILURE() << "Should not have reached this line.";
179 }
180 catch (const ActionError& e)
181 {
182 EXPECT_STREQ(e.what(), "ActionError: i2c_write_byte: { register: "
183 "0xA0, value: 0xD6, mask: 0xC3 }");
184 try
185 {
186 // Re-throw inner I2CException
187 std::rethrow_if_nested(e);
188 ADD_FAILURE() << "Should not have reached this line.";
189 }
190 catch (const i2c::I2CException& ie)
191 {
192 EXPECT_STREQ(
193 ie.what(),
194 "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70");
195 }
196 catch (...)
197 {
198 ADD_FAILURE() << "Should not have caught exception.";
199 }
200 }
201 catch (...)
202 {
203 ADD_FAILURE() << "Should not have caught exception.";
204 }
205
206 // Test where fails: Writing byte fails
207 try
208 {
209 // Create mock I2CInterface: write() throws an I2CException
210 std::unique_ptr<i2c::MockedI2CInterface> i2cInterface =
211 std::make_unique<i2c::MockedI2CInterface>();
212 EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true));
213 EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0);
214 EXPECT_CALL(*i2cInterface,
215 write(TypedEq<uint8_t>(0x7C), TypedEq<uint8_t>(0x1A)))
216 .Times(1)
217 .WillOnce(Throw(
218 i2c::I2CException{"Failed to write byte", "/dev/i2c-1", 0x70}));
219
Bob King73eacee2020-10-23 13:58:02 +0800220 // Create Device, IDMap, MockServices, and ActionEnvironment
Bob Kinga76898f2020-10-13 15:08:33 +0800221 Device device{
222 "reg1", true,
223 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
224 std::move(i2cInterface)};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600225 IDMap idMap{};
226 idMap.addDevice(device);
Bob King73eacee2020-10-23 13:58:02 +0800227 MockServices services{};
228 ActionEnvironment env{idMap, "reg1", services};
Shawn McCarneyf1c90612020-02-24 09:56:53 -0600229
230 I2CWriteByteAction action{0x7C, 0x1A};
231 action.execute(env);
232 ADD_FAILURE() << "Should not have reached this line.";
233 }
234 catch (const ActionError& e)
235 {
236 EXPECT_STREQ(e.what(), "ActionError: i2c_write_byte: { register: "
237 "0x7C, value: 0x1A, mask: 0xFF }");
238 try
239 {
240 // Re-throw inner I2CException
241 std::rethrow_if_nested(e);
242 ADD_FAILURE() << "Should not have reached this line.";
243 }
244 catch (const i2c::I2CException& ie)
245 {
246 EXPECT_STREQ(ie.what(), "I2CException: Failed to write byte: bus "
247 "/dev/i2c-1, addr 0x70");
248 }
249 catch (...)
250 {
251 ADD_FAILURE() << "Should not have caught exception.";
252 }
253 }
254 catch (...)
255 {
256 ADD_FAILURE() << "Should not have caught exception.";
257 }
258}
259
260TEST(I2CWriteByteActionTests, GetRegister)
261{
262 I2CWriteByteAction action{0x7C, 0xDE};
263 EXPECT_EQ(action.getRegister(), 0x7C);
264}
265
266TEST(I2CWriteByteActionTests, GetValue)
267{
268 I2CWriteByteAction action{0xA0, 0x03, 0x47};
269 EXPECT_EQ(action.getValue(), 0x03);
270}
271
272TEST(I2CWriteByteActionTests, GetMask)
273{
274 // Test where mask is not specified
275 {
276 I2CWriteByteAction action{0x7C, 0xDE};
277 EXPECT_EQ(action.getMask(), 0xFF);
278 }
279
280 // Test where mask is specified
281 {
282 I2CWriteByteAction action{0xA0, 0x03, 0x47};
283 EXPECT_EQ(action.getMask(), 0x47);
284 }
285}
286
287TEST(I2CWriteByteActionTests, ToString)
288{
289 I2CWriteByteAction action{0x7C, 0xDE, 0xFB};
290 EXPECT_EQ(action.toString(),
291 "i2c_write_byte: { register: 0x7C, value: 0xDE, mask: 0xFB }");
292}