Shawn McCarney | a8119f2 | 2020-03-02 16:20:18 -0600 | [diff] [blame] | 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 "id_map.hpp" |
| 21 | #include "mocked_i2c_interface.hpp" |
| 22 | #include "pmbus_error.hpp" |
| 23 | #include "pmbus_utils.hpp" |
| 24 | #include "pmbus_write_vout_command_action.hpp" |
| 25 | #include "write_verification_error.hpp" |
| 26 | |
| 27 | #include <cstdint> |
| 28 | #include <memory> |
| 29 | #include <optional> |
| 30 | #include <stdexcept> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | |
| 34 | #include <gmock/gmock.h> |
| 35 | #include <gtest/gtest.h> |
| 36 | |
| 37 | using namespace phosphor::power::regulators; |
| 38 | |
| 39 | using ::testing::A; |
| 40 | using ::testing::Return; |
| 41 | using ::testing::SetArgReferee; |
| 42 | using ::testing::Throw; |
| 43 | using ::testing::TypedEq; |
| 44 | |
| 45 | TEST(PMBusWriteVoutCommandActionTests, Constructor) |
| 46 | { |
| 47 | // Test where works: Volts value and exponent value are specified |
| 48 | try |
| 49 | { |
| 50 | std::optional<double> volts{1.3}; |
| 51 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 52 | std::optional<int8_t> exponent{-8}; |
| 53 | bool isVerified{true}; |
| 54 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 55 | EXPECT_EQ(action.getVolts().has_value(), true); |
| 56 | EXPECT_EQ(action.getVolts().value(), 1.3); |
| 57 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 58 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 59 | EXPECT_EQ(action.getExponent().value(), -8); |
| 60 | EXPECT_EQ(action.isVerified(), true); |
| 61 | } |
| 62 | catch (...) |
| 63 | { |
| 64 | ADD_FAILURE() << "Should not have caught exception."; |
| 65 | } |
| 66 | |
| 67 | // Test where works: Volts value and exponent value are not specified |
| 68 | try |
| 69 | { |
| 70 | std::optional<double> volts{}; |
| 71 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 72 | std::optional<int8_t> exponent{}; |
| 73 | bool isVerified{false}; |
| 74 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 75 | EXPECT_EQ(action.getVolts().has_value(), false); |
| 76 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 77 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 78 | EXPECT_EQ(action.isVerified(), false); |
| 79 | } |
| 80 | catch (...) |
| 81 | { |
| 82 | ADD_FAILURE() << "Should not have caught exception."; |
| 83 | } |
| 84 | |
| 85 | // Test where fails: Data format is not linear |
| 86 | try |
| 87 | { |
| 88 | std::optional<double> volts{}; |
| 89 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::direct}; |
| 90 | std::optional<int8_t> exponent{}; |
| 91 | bool isVerified{false}; |
| 92 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 93 | ADD_FAILURE() << "Should not have reached this line."; |
| 94 | } |
| 95 | catch (const std::invalid_argument& e) |
| 96 | { |
| 97 | EXPECT_STREQ(e.what(), "Unsupported data format specified"); |
| 98 | } |
| 99 | catch (...) |
| 100 | { |
| 101 | ADD_FAILURE() << "Should not have caught exception."; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | TEST(PMBusWriteVoutCommandActionTests, Execute) |
| 106 | { |
| 107 | // Test where works: Volts value and exponent value defined in action; |
| 108 | // write is verified. |
| 109 | try |
| 110 | { |
| 111 | // Create mock I2CInterface. Expect action to do the following: |
| 112 | // * will not read from VOUT_MODE (command/register 0x20) |
| 113 | // * will write 0x014D to VOUT_COMMAND (command/register 0x21) |
| 114 | // * will read 0x014D from VOUT_COMMAND |
| 115 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 116 | std::make_unique<i2c::MockedI2CInterface>(); |
| 117 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 118 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 119 | EXPECT_CALL(*i2cInterface, |
| 120 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 121 | .Times(1); |
| 122 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 123 | .Times(1) |
| 124 | .WillOnce(SetArgReferee<1>(0x014D)); |
| 125 | |
| 126 | // Create Device, IDMap, and ActionEnvironment |
| 127 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 128 | std::move(i2cInterface)}; |
| 129 | IDMap idMap{}; |
| 130 | idMap.addDevice(device); |
| 131 | ActionEnvironment env{idMap, "reg1"}; |
| 132 | |
| 133 | // Create and execute action |
| 134 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 135 | std::optional<double> volts{1.3}; |
| 136 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 137 | std::optional<int8_t> exponent{-8}; |
| 138 | bool isVerified{true}; |
| 139 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 140 | EXPECT_EQ(action.execute(env), true); |
| 141 | } |
| 142 | catch (...) |
| 143 | { |
| 144 | ADD_FAILURE() << "Should not have caught exception."; |
| 145 | } |
| 146 | |
| 147 | // Test where works: Volts value defined in ActionEnvironment; exponent |
| 148 | // value defined in VOUT_MODE; write is not verified. |
| 149 | try |
| 150 | { |
| 151 | // Create mock I2CInterface. Expect action to do the following: |
| 152 | // * will read 0b0001'0111 (linear format, -9 exponent) from VOUT_MODE |
| 153 | // * will write 0x069A to VOUT_COMMAND |
| 154 | // * will not read from VOUT_COMMAND |
| 155 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 156 | std::make_unique<i2c::MockedI2CInterface>(); |
| 157 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 158 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 159 | .Times(1) |
| 160 | .WillOnce(SetArgReferee<1>(0b0001'0111)); |
| 161 | EXPECT_CALL(*i2cInterface, |
| 162 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x069A))) |
| 163 | .Times(1); |
| 164 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint16_t&>())).Times(0); |
| 165 | |
| 166 | // Create Device, IDMap, and ActionEnvironment. Set volts value to 3.3 |
| 167 | // in ActionEnvironment. |
| 168 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 169 | std::move(i2cInterface)}; |
| 170 | IDMap idMap{}; |
| 171 | idMap.addDevice(device); |
| 172 | ActionEnvironment env{idMap, "reg1"}; |
| 173 | env.setVolts(3.3); |
| 174 | |
| 175 | // Create and execute action |
| 176 | // Linear format volts value = (3.3 / 2^(-9)) = 1689.6 = 1690 = 0x069A |
| 177 | std::optional<double> volts{}; |
| 178 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 179 | std::optional<int8_t> exponent{}; |
| 180 | bool isVerified{false}; |
| 181 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 182 | EXPECT_EQ(action.execute(env), true); |
| 183 | } |
| 184 | catch (...) |
| 185 | { |
| 186 | ADD_FAILURE() << "Should not have caught exception."; |
| 187 | } |
| 188 | |
| 189 | // Test where fails: No volts value defined |
| 190 | try |
| 191 | { |
| 192 | // Create IDMap and ActionEnvironment |
| 193 | IDMap idMap{}; |
| 194 | ActionEnvironment env{idMap, "reg1"}; |
| 195 | |
| 196 | // Create and execute action |
| 197 | std::optional<double> volts{}; |
| 198 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 199 | std::optional<int8_t> exponent{-8}; |
| 200 | bool isVerified{false}; |
| 201 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 202 | action.execute(env); |
| 203 | ADD_FAILURE() << "Should not have reached this line."; |
| 204 | } |
| 205 | catch (const ActionError& e) |
| 206 | { |
| 207 | EXPECT_STREQ( |
| 208 | e.what(), |
| 209 | "ActionError: pmbus_write_vout_command: { format: linear, " |
| 210 | "exponent: -8, is_verified: false }: No volts value defined"); |
| 211 | } |
| 212 | catch (...) |
| 213 | { |
| 214 | ADD_FAILURE() << "Should not have caught exception."; |
| 215 | } |
| 216 | |
| 217 | // Test where fails: Unable to get I2C interface to current device |
| 218 | try |
| 219 | { |
| 220 | // Create IDMap and ActionEnvironment |
| 221 | IDMap idMap{}; |
| 222 | ActionEnvironment env{idMap, "reg1"}; |
| 223 | |
| 224 | // Create and execute action |
| 225 | std::optional<double> volts{1.3}; |
| 226 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 227 | std::optional<int8_t> exponent{-8}; |
| 228 | bool isVerified{false}; |
| 229 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 230 | action.execute(env); |
| 231 | ADD_FAILURE() << "Should not have reached this line."; |
| 232 | } |
| 233 | catch (const std::invalid_argument& e) |
| 234 | { |
| 235 | EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\""); |
| 236 | } |
| 237 | catch (...) |
| 238 | { |
| 239 | ADD_FAILURE() << "Should not have caught exception."; |
| 240 | } |
| 241 | |
| 242 | // Test where fails: Unable to read VOUT_MODE to get exponent |
| 243 | try |
| 244 | { |
| 245 | // Create mock I2CInterface. Expect action to do the following: |
| 246 | // * will try to read VOUT_MODE; exception will be thrown |
| 247 | // * will not write to VOUT_COMMAND due to exception |
| 248 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 249 | std::make_unique<i2c::MockedI2CInterface>(); |
| 250 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 251 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 252 | .Times(1) |
| 253 | .WillOnce(Throw( |
| 254 | i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70})); |
| 255 | EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0); |
| 256 | |
| 257 | // Create Device, IDMap, and ActionEnvironment |
| 258 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 259 | std::move(i2cInterface)}; |
| 260 | IDMap idMap{}; |
| 261 | idMap.addDevice(device); |
| 262 | ActionEnvironment env{idMap, "reg1"}; |
| 263 | |
| 264 | // Create and execute action |
| 265 | std::optional<double> volts{3.3}; |
| 266 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 267 | std::optional<int8_t> exponent{}; |
| 268 | bool isVerified{false}; |
| 269 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 270 | action.execute(env); |
| 271 | ADD_FAILURE() << "Should not have reached this line."; |
| 272 | } |
| 273 | catch (const ActionError& e) |
| 274 | { |
| 275 | EXPECT_STREQ(e.what(), |
| 276 | "ActionError: pmbus_write_vout_command: { volts: 3.3, " |
| 277 | "format: linear, is_verified: false }"); |
| 278 | try |
| 279 | { |
| 280 | // Re-throw inner I2CException |
| 281 | std::rethrow_if_nested(e); |
| 282 | ADD_FAILURE() << "Should not have reached this line."; |
| 283 | } |
| 284 | catch (const i2c::I2CException& ie) |
| 285 | { |
| 286 | EXPECT_STREQ( |
| 287 | ie.what(), |
| 288 | "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70"); |
| 289 | } |
| 290 | catch (...) |
| 291 | { |
| 292 | ADD_FAILURE() << "Should not have caught exception."; |
| 293 | } |
| 294 | } |
| 295 | catch (...) |
| 296 | { |
| 297 | ADD_FAILURE() << "Should not have caught exception."; |
| 298 | } |
| 299 | |
| 300 | // Test where fails: VOUT_MODE data format is not linear |
| 301 | try |
| 302 | { |
| 303 | // Create mock I2CInterface. Expect action to do the following: |
| 304 | // * will read 0b0010'0000 (vid data format) from VOUT_MODE |
| 305 | // * will not write to VOUT_COMMAND due to data format error |
| 306 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 307 | std::make_unique<i2c::MockedI2CInterface>(); |
| 308 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 309 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x20), A<uint8_t&>())) |
| 310 | .Times(1) |
| 311 | .WillOnce(SetArgReferee<1>(0b0010'0000)); |
| 312 | EXPECT_CALL(*i2cInterface, write(A<uint8_t>(), A<uint16_t>())).Times(0); |
| 313 | |
| 314 | // Create Device, IDMap, and ActionEnvironment |
| 315 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 316 | std::move(i2cInterface)}; |
| 317 | IDMap idMap{}; |
| 318 | idMap.addDevice(device); |
| 319 | ActionEnvironment env{idMap, "reg1"}; |
| 320 | |
| 321 | // Create and execute action |
| 322 | std::optional<double> volts{3.3}; |
| 323 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 324 | std::optional<int8_t> exponent{}; |
| 325 | bool isVerified{false}; |
| 326 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 327 | action.execute(env); |
| 328 | ADD_FAILURE() << "Should not have reached this line."; |
| 329 | } |
| 330 | catch (const ActionError& e) |
| 331 | { |
| 332 | EXPECT_STREQ(e.what(), |
| 333 | "ActionError: pmbus_write_vout_command: { volts: 3.3, " |
| 334 | "format: linear, is_verified: false }"); |
| 335 | try |
| 336 | { |
| 337 | // Re-throw inner PMBusError |
| 338 | std::rethrow_if_nested(e); |
| 339 | ADD_FAILURE() << "Should not have reached this line."; |
| 340 | } |
| 341 | catch (const PMBusError& pe) |
| 342 | { |
| 343 | EXPECT_STREQ( |
| 344 | pe.what(), |
| 345 | "PMBusError: VOUT_MODE contains unsupported data format"); |
| 346 | } |
| 347 | catch (...) |
| 348 | { |
| 349 | ADD_FAILURE() << "Should not have caught exception."; |
| 350 | } |
| 351 | } |
| 352 | catch (...) |
| 353 | { |
| 354 | ADD_FAILURE() << "Should not have caught exception."; |
| 355 | } |
| 356 | |
| 357 | // Test where fails: Unable to write VOUT_COMMAND |
| 358 | try |
| 359 | { |
| 360 | // Create mock I2CInterface. Expect action to do the following: |
| 361 | // * will not read from VOUT_MODE |
| 362 | // * will try to write 0x014D to VOUT_COMMAND; exception will be thrown |
| 363 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 364 | std::make_unique<i2c::MockedI2CInterface>(); |
| 365 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 366 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 367 | EXPECT_CALL(*i2cInterface, |
| 368 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 369 | .Times(1) |
| 370 | .WillOnce(Throw(i2c::I2CException{"Failed to write word data", |
| 371 | "/dev/i2c-1", 0x70})); |
| 372 | |
| 373 | // Create Device, IDMap, and ActionEnvironment |
| 374 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 375 | std::move(i2cInterface)}; |
| 376 | IDMap idMap{}; |
| 377 | idMap.addDevice(device); |
| 378 | ActionEnvironment env{idMap, "reg1"}; |
| 379 | |
| 380 | // Create and execute action |
| 381 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 382 | std::optional<double> volts{1.3}; |
| 383 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 384 | std::optional<int8_t> exponent{-8}; |
| 385 | bool isVerified{false}; |
| 386 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 387 | action.execute(env); |
| 388 | ADD_FAILURE() << "Should not have reached this line."; |
| 389 | } |
| 390 | catch (const ActionError& e) |
| 391 | { |
| 392 | EXPECT_STREQ(e.what(), |
| 393 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 394 | "format: linear, exponent: -8, is_verified: false }"); |
| 395 | try |
| 396 | { |
| 397 | // Re-throw inner I2CException |
| 398 | std::rethrow_if_nested(e); |
| 399 | ADD_FAILURE() << "Should not have reached this line."; |
| 400 | } |
| 401 | catch (const i2c::I2CException& ie) |
| 402 | { |
| 403 | EXPECT_STREQ(ie.what(), "I2CException: Failed to write word data: " |
| 404 | "bus /dev/i2c-1, addr 0x70"); |
| 405 | } |
| 406 | catch (...) |
| 407 | { |
| 408 | ADD_FAILURE() << "Should not have caught exception."; |
| 409 | } |
| 410 | } |
| 411 | catch (...) |
| 412 | { |
| 413 | ADD_FAILURE() << "Should not have caught exception."; |
| 414 | } |
| 415 | |
| 416 | // Test where fails: Unable to read VOUT_COMMAND |
| 417 | try |
| 418 | { |
| 419 | // Create mock I2CInterface. Expect action to do the following: |
| 420 | // * will not read from VOUT_MODE |
| 421 | // * will write 0x014D to VOUT_COMMAND |
| 422 | // * will try to read from VOUT_COMMAND; exception will be thrown |
| 423 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 424 | std::make_unique<i2c::MockedI2CInterface>(); |
| 425 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 426 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 427 | EXPECT_CALL(*i2cInterface, |
| 428 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 429 | .Times(1); |
| 430 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 431 | .Times(1) |
| 432 | .WillOnce(Throw(i2c::I2CException{"Failed to read word data", |
| 433 | "/dev/i2c-1", 0x70})); |
| 434 | |
| 435 | // Create Device, IDMap, and ActionEnvironment |
| 436 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 437 | std::move(i2cInterface)}; |
| 438 | IDMap idMap{}; |
| 439 | idMap.addDevice(device); |
| 440 | ActionEnvironment env{idMap, "reg1"}; |
| 441 | |
| 442 | // Create and execute action |
| 443 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 444 | std::optional<double> volts{1.3}; |
| 445 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 446 | std::optional<int8_t> exponent{-8}; |
| 447 | bool isVerified{true}; |
| 448 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 449 | action.execute(env); |
| 450 | ADD_FAILURE() << "Should not have reached this line."; |
| 451 | } |
| 452 | catch (const ActionError& e) |
| 453 | { |
| 454 | EXPECT_STREQ(e.what(), |
| 455 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 456 | "format: linear, exponent: -8, is_verified: true }"); |
| 457 | try |
| 458 | { |
| 459 | // Re-throw inner I2CException |
| 460 | std::rethrow_if_nested(e); |
| 461 | ADD_FAILURE() << "Should not have reached this line."; |
| 462 | } |
| 463 | catch (const i2c::I2CException& ie) |
| 464 | { |
| 465 | EXPECT_STREQ(ie.what(), "I2CException: Failed to read word data: " |
| 466 | "bus /dev/i2c-1, addr 0x70"); |
| 467 | } |
| 468 | catch (...) |
| 469 | { |
| 470 | ADD_FAILURE() << "Should not have caught exception."; |
| 471 | } |
| 472 | } |
| 473 | catch (...) |
| 474 | { |
| 475 | ADD_FAILURE() << "Should not have caught exception."; |
| 476 | } |
| 477 | |
| 478 | // Test where fails: Write verification error |
| 479 | try |
| 480 | { |
| 481 | // Create mock I2CInterface. Expect action to do the following: |
| 482 | // * will not read from VOUT_MODE |
| 483 | // * will write 0x014D to VOUT_COMMAND |
| 484 | // * will read 0x014C from VOUT_COMMAND (not equal to 0x014D) |
| 485 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 486 | std::make_unique<i2c::MockedI2CInterface>(); |
| 487 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
| 488 | EXPECT_CALL(*i2cInterface, read(A<uint8_t>(), A<uint8_t&>())).Times(0); |
| 489 | EXPECT_CALL(*i2cInterface, |
| 490 | write(TypedEq<uint8_t>(0x21), TypedEq<uint16_t>(0x014D))) |
| 491 | .Times(1); |
| 492 | EXPECT_CALL(*i2cInterface, read(TypedEq<uint8_t>(0x21), A<uint16_t&>())) |
| 493 | .Times(1) |
| 494 | .WillOnce(SetArgReferee<1>(0x014C)); |
| 495 | |
| 496 | // Create Device, IDMap, and ActionEnvironment |
| 497 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 498 | std::move(i2cInterface)}; |
| 499 | IDMap idMap{}; |
| 500 | idMap.addDevice(device); |
| 501 | ActionEnvironment env{idMap, "reg1"}; |
| 502 | |
| 503 | // Create and execute action |
| 504 | // Linear format volts value = (1.3 / 2^(-8)) = 332.8 = 333 = 0x014D |
| 505 | std::optional<double> volts{1.3}; |
| 506 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 507 | std::optional<int8_t> exponent{-8}; |
| 508 | bool isVerified{true}; |
| 509 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 510 | action.execute(env); |
| 511 | ADD_FAILURE() << "Should not have reached this line."; |
| 512 | } |
| 513 | catch (const ActionError& e) |
| 514 | { |
| 515 | EXPECT_STREQ(e.what(), |
| 516 | "ActionError: pmbus_write_vout_command: { volts: 1.3, " |
| 517 | "format: linear, exponent: -8, is_verified: true }"); |
| 518 | try |
| 519 | { |
| 520 | // Re-throw inner WriteVerificationError |
| 521 | std::rethrow_if_nested(e); |
| 522 | ADD_FAILURE() << "Should not have reached this line."; |
| 523 | } |
| 524 | catch (const WriteVerificationError& we) |
| 525 | { |
| 526 | EXPECT_STREQ( |
| 527 | we.what(), |
| 528 | "WriteVerificationError: device: reg1, register: VOUT_COMMAND, " |
| 529 | "value_written: 0x14D, value_read: 0x14C"); |
| 530 | } |
| 531 | catch (...) |
| 532 | { |
| 533 | ADD_FAILURE() << "Should not have caught exception."; |
| 534 | } |
| 535 | } |
| 536 | catch (...) |
| 537 | { |
| 538 | ADD_FAILURE() << "Should not have caught exception."; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | TEST(PMBusWriteVoutCommandActionTests, GetExponent) |
| 543 | { |
| 544 | std::optional<double> volts{1.3}; |
| 545 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 546 | bool isVerified{true}; |
| 547 | |
| 548 | // Exponent value was specified |
| 549 | { |
| 550 | std::optional<int8_t> exponent{-9}; |
| 551 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 552 | EXPECT_EQ(action.getExponent().has_value(), true); |
| 553 | EXPECT_EQ(action.getExponent().value(), -9); |
| 554 | } |
| 555 | |
| 556 | // Exponent value was not specified |
| 557 | { |
| 558 | std::optional<int8_t> exponent{}; |
| 559 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 560 | EXPECT_EQ(action.getExponent().has_value(), false); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | TEST(PMBusWriteVoutCommandActionTests, GetFormat) |
| 565 | { |
| 566 | std::optional<double> volts{}; |
| 567 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 568 | std::optional<int8_t> exponent{}; |
| 569 | bool isVerified{false}; |
| 570 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 571 | EXPECT_EQ(action.getFormat(), pmbus_utils::VoutDataFormat::linear); |
| 572 | } |
| 573 | |
| 574 | TEST(PMBusWriteVoutCommandActionTests, GetVolts) |
| 575 | { |
| 576 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 577 | std::optional<int8_t> exponent{-8}; |
| 578 | bool isVerified{true}; |
| 579 | |
| 580 | // Volts value was specified |
| 581 | { |
| 582 | std::optional<double> volts{1.3}; |
| 583 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 584 | EXPECT_EQ(action.getVolts().has_value(), true); |
| 585 | EXPECT_EQ(action.getVolts().value(), 1.3); |
| 586 | } |
| 587 | |
| 588 | // Volts value was not specified |
| 589 | { |
| 590 | std::optional<double> volts{}; |
| 591 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 592 | EXPECT_EQ(action.getVolts().has_value(), false); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | TEST(PMBusWriteVoutCommandActionTests, IsVerified) |
| 597 | { |
| 598 | std::optional<double> volts{1.3}; |
| 599 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 600 | std::optional<int8_t> exponent{-8}; |
| 601 | bool isVerified{true}; |
| 602 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 603 | EXPECT_EQ(action.isVerified(), true); |
| 604 | } |
| 605 | |
| 606 | TEST(PMBusWriteVoutCommandActionTests, ToString) |
| 607 | { |
| 608 | // Test where volts value and exponent value are specified |
| 609 | { |
| 610 | std::optional<double> volts{1.3}; |
| 611 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 612 | std::optional<int8_t> exponent{-8}; |
| 613 | bool isVerified{true}; |
| 614 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 615 | EXPECT_EQ(action.toString(), |
| 616 | "pmbus_write_vout_command: { volts: 1.3, format: linear, " |
| 617 | "exponent: -8, is_verified: true }"); |
| 618 | } |
| 619 | |
| 620 | // Test where volts value and exponent value are not specified |
| 621 | { |
| 622 | std::optional<double> volts{}; |
| 623 | pmbus_utils::VoutDataFormat format{pmbus_utils::VoutDataFormat::linear}; |
| 624 | std::optional<int8_t> exponent{}; |
| 625 | bool isVerified{false}; |
| 626 | PMBusWriteVoutCommandAction action{volts, format, exponent, isVerified}; |
| 627 | EXPECT_EQ( |
| 628 | action.toString(), |
| 629 | "pmbus_write_vout_command: { format: linear, is_verified: false }"); |
| 630 | } |
| 631 | } |