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"
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"
22 #include "mock_services.hpp"
23 #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
34 using namespace phosphor::power::regulators;
35
36 using ::testing::A;
37 using ::testing::Return;
38 using ::testing::SetArgReferee;
39 using ::testing::Throw;
40 using ::testing::TypedEq;
41
TEST(I2CWriteByteActionTests,Constructor)42 TEST(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
TEST(I2CWriteByteActionTests,Execute)61 TEST(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
75 // Create Device, IDMap, MockServices, and ActionEnvironment
76 Device device{
77 "reg1", true,
78 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
79 std::move(i2cInterface)};
80 IDMap idMap{};
81 idMap.addDevice(device);
82 MockServices services{};
83 ActionEnvironment env{idMap, "reg1", services};
84
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
107 // Create Device, IDMap, MockServices, and ActionEnvironment
108 Device device{
109 "reg1", true,
110 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
111 std::move(i2cInterface)};
112 IDMap idMap{};
113 idMap.addDevice(device);
114 MockServices services{};
115 ActionEnvironment env{idMap, "reg1", services};
116
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 {
135 // Create IDMap, MockServices, and ActionEnvironment
136 IDMap idMap{};
137 MockServices services{};
138 ActionEnvironment env{idMap, "reg1", services};
139
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
166 // Create Device, IDMap, MockServices, and ActionEnvironment
167 Device device{
168 "reg1", true,
169 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
170 std::move(i2cInterface)};
171 IDMap idMap{};
172 idMap.addDevice(device);
173 MockServices services{};
174 ActionEnvironment env{idMap, "reg1", services};
175
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
220 // Create Device, IDMap, MockServices, and ActionEnvironment
221 Device device{
222 "reg1", true,
223 "/xyz/openbmc_project/inventory/system/chassis/motherboard/reg1",
224 std::move(i2cInterface)};
225 IDMap idMap{};
226 idMap.addDevice(device);
227 MockServices services{};
228 ActionEnvironment env{idMap, "reg1", services};
229
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
TEST(I2CWriteByteActionTests,GetRegister)260 TEST(I2CWriteByteActionTests, GetRegister)
261 {
262 I2CWriteByteAction action{0x7C, 0xDE};
263 EXPECT_EQ(action.getRegister(), 0x7C);
264 }
265
TEST(I2CWriteByteActionTests,GetValue)266 TEST(I2CWriteByteActionTests, GetValue)
267 {
268 I2CWriteByteAction action{0xA0, 0x03, 0x47};
269 EXPECT_EQ(action.getValue(), 0x03);
270 }
271
TEST(I2CWriteByteActionTests,GetMask)272 TEST(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
TEST(I2CWriteByteActionTests,ToString)287 TEST(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 }
293