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