Shawn McCarney | 8215be3 | 2020-02-19 10:00:57 -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_compare_bit_action.hpp" |
| 20 | #include "i2c_interface.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 | |
| 33 | using namespace phosphor::power::regulators; |
| 34 | |
| 35 | using ::testing::A; |
| 36 | using ::testing::Return; |
| 37 | using ::testing::SetArgReferee; |
| 38 | using ::testing::Throw; |
| 39 | |
| 40 | TEST(I2CCompareBitActionTests, Constructor) |
| 41 | { |
| 42 | // Test where works |
| 43 | try |
| 44 | { |
| 45 | I2CCompareBitAction action{0x7C, 2, 0}; |
| 46 | EXPECT_EQ(action.getRegister(), 0x7C); |
| 47 | EXPECT_EQ(action.getPosition(), 2); |
| 48 | EXPECT_EQ(action.getValue(), 0); |
| 49 | } |
| 50 | catch (...) |
| 51 | { |
| 52 | ADD_FAILURE() << "Should not have caught exception."; |
| 53 | } |
| 54 | |
| 55 | // Test where fails: Invalid bit position > 7 |
| 56 | try |
| 57 | { |
| 58 | I2CCompareBitAction action{0x7C, 8, 0}; |
| 59 | ADD_FAILURE() << "Should not have reached this line."; |
| 60 | } |
| 61 | catch (const std::invalid_argument& e) |
| 62 | { |
| 63 | EXPECT_STREQ(e.what(), "Invalid bit position: 8"); |
| 64 | } |
| 65 | catch (...) |
| 66 | { |
| 67 | ADD_FAILURE() << "Should not have caught exception."; |
| 68 | } |
| 69 | |
| 70 | // Test where fails: Invalid bit value > 1 |
| 71 | try |
| 72 | { |
| 73 | I2CCompareBitAction action{0x7C, 2, 2}; |
| 74 | ADD_FAILURE() << "Should not have reached this line."; |
| 75 | } |
| 76 | catch (const std::invalid_argument& e) |
| 77 | { |
| 78 | EXPECT_STREQ(e.what(), "Invalid bit value: 2"); |
| 79 | } |
| 80 | catch (...) |
| 81 | { |
| 82 | ADD_FAILURE() << "Should not have caught exception."; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST(I2CCompareBitActionTests, Execute) |
| 87 | { |
| 88 | // Test where works |
| 89 | try |
| 90 | { |
| 91 | // Create mock I2CInterface: read() returns value 0x96 (1001 0110) |
| 92 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 93 | std::make_unique<i2c::MockedI2CInterface>(); |
| 94 | EXPECT_CALL(*i2cInterface, isOpen).WillRepeatedly(Return(true)); |
Shawn McCarney | 2c65700 | 2020-02-20 11:00:37 -0600 | [diff] [blame] | 95 | EXPECT_CALL(*i2cInterface, read(0x7C, A<uint8_t&>())) |
Shawn McCarney | 8215be3 | 2020-02-19 10:00:57 -0600 | [diff] [blame] | 96 | .WillRepeatedly(SetArgReferee<1>(0x96)); |
| 97 | |
| 98 | // Create Device, IDMap, and ActionEnvironment |
| 99 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 100 | std::move(i2cInterface)}; |
| 101 | IDMap idMap{}; |
| 102 | idMap.addDevice(device); |
| 103 | ActionEnvironment env{idMap, "reg1"}; |
| 104 | |
| 105 | // Test where actual bit value is equal to expected bit value. |
| 106 | // Test all bits in register value 0x96 == 1001 0110). |
| 107 | { |
| 108 | I2CCompareBitAction actions[] = {I2CCompareBitAction{0x7C, 7, 1}, |
| 109 | I2CCompareBitAction{0x7C, 6, 0}, |
| 110 | I2CCompareBitAction{0x7C, 5, 0}, |
| 111 | I2CCompareBitAction{0x7C, 4, 1}, |
| 112 | I2CCompareBitAction{0x7C, 3, 0}, |
| 113 | I2CCompareBitAction{0x7C, 2, 1}, |
| 114 | I2CCompareBitAction{0x7C, 1, 1}, |
| 115 | I2CCompareBitAction{0x7C, 0, 0}}; |
| 116 | for (I2CCompareBitAction& action : actions) |
| 117 | { |
| 118 | EXPECT_EQ(action.execute(env), true); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Test where actual bit value is not equal to expected bit value. |
| 123 | // Test all bits in register value 0x96 == 1001 0110). |
| 124 | { |
| 125 | I2CCompareBitAction actions[] = {I2CCompareBitAction{0x7C, 7, 0}, |
| 126 | I2CCompareBitAction{0x7C, 6, 1}, |
| 127 | I2CCompareBitAction{0x7C, 5, 1}, |
| 128 | I2CCompareBitAction{0x7C, 4, 0}, |
| 129 | I2CCompareBitAction{0x7C, 3, 1}, |
| 130 | I2CCompareBitAction{0x7C, 2, 0}, |
| 131 | I2CCompareBitAction{0x7C, 1, 0}, |
| 132 | I2CCompareBitAction{0x7C, 0, 1}}; |
| 133 | for (I2CCompareBitAction& action : actions) |
| 134 | { |
| 135 | EXPECT_EQ(action.execute(env), false); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | catch (...) |
| 140 | { |
| 141 | ADD_FAILURE() << "Should not have caught exception."; |
| 142 | } |
| 143 | |
| 144 | // Test where fails: Getting I2CInterface fails |
| 145 | try |
| 146 | { |
| 147 | // Create IDMap and ActionEnvironment |
| 148 | IDMap idMap{}; |
| 149 | ActionEnvironment env{idMap, "reg1"}; |
| 150 | |
| 151 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 152 | action.execute(env); |
| 153 | ADD_FAILURE() << "Should not have reached this line."; |
| 154 | } |
| 155 | catch (const std::invalid_argument& e) |
| 156 | { |
| 157 | EXPECT_STREQ(e.what(), "Unable to find device with ID \"reg1\""); |
| 158 | } |
| 159 | catch (...) |
| 160 | { |
| 161 | ADD_FAILURE() << "Should not have caught exception."; |
| 162 | } |
| 163 | |
| 164 | // Test where fails: Reading byte fails |
| 165 | try |
| 166 | { |
| 167 | // Create mock I2CInterface: read() throws an I2CException |
| 168 | std::unique_ptr<i2c::MockedI2CInterface> i2cInterface = |
| 169 | std::make_unique<i2c::MockedI2CInterface>(); |
| 170 | EXPECT_CALL(*i2cInterface, isOpen).Times(1).WillOnce(Return(true)); |
Shawn McCarney | 2c65700 | 2020-02-20 11:00:37 -0600 | [diff] [blame] | 171 | EXPECT_CALL(*i2cInterface, read(0x7C, A<uint8_t&>())) |
Shawn McCarney | 8215be3 | 2020-02-19 10:00:57 -0600 | [diff] [blame] | 172 | .Times(1) |
| 173 | .WillOnce(Throw( |
| 174 | i2c::I2CException{"Failed to read byte", "/dev/i2c-1", 0x70})); |
| 175 | |
| 176 | // Create Device, IDMap, and ActionEnvironment |
| 177 | Device device{"reg1", true, "/system/chassis/motherboard/reg1", |
| 178 | std::move(i2cInterface)}; |
| 179 | IDMap idMap{}; |
| 180 | idMap.addDevice(device); |
| 181 | ActionEnvironment env{idMap, "reg1"}; |
| 182 | |
| 183 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 184 | action.execute(env); |
| 185 | ADD_FAILURE() << "Should not have reached this line."; |
| 186 | } |
| 187 | catch (const ActionError& e) |
| 188 | { |
| 189 | EXPECT_STREQ(e.what(), "ActionError: i2c_compare_bit: { register: " |
| 190 | "0x7C, position: 5, value: 1 }"); |
| 191 | try |
| 192 | { |
| 193 | // Re-throw inner I2CException |
| 194 | std::rethrow_if_nested(e); |
| 195 | ADD_FAILURE() << "Should not have reached this line."; |
| 196 | } |
| 197 | catch (const i2c::I2CException& ie) |
| 198 | { |
| 199 | EXPECT_STREQ( |
| 200 | ie.what(), |
| 201 | "I2CException: Failed to read byte: bus /dev/i2c-1, addr 0x70"); |
| 202 | } |
| 203 | catch (...) |
| 204 | { |
| 205 | ADD_FAILURE() << "Should not have caught exception."; |
| 206 | } |
| 207 | } |
| 208 | catch (...) |
| 209 | { |
| 210 | ADD_FAILURE() << "Should not have caught exception."; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | TEST(I2CCompareBitActionTests, GetRegister) |
| 215 | { |
| 216 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 217 | EXPECT_EQ(action.getRegister(), 0x7C); |
| 218 | } |
| 219 | |
| 220 | TEST(I2CCompareBitActionTests, GetPosition) |
| 221 | { |
| 222 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 223 | EXPECT_EQ(action.getPosition(), 5); |
| 224 | } |
| 225 | |
| 226 | TEST(I2CCompareBitActionTests, GetValue) |
| 227 | { |
| 228 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 229 | EXPECT_EQ(action.getValue(), 1); |
| 230 | } |
| 231 | |
| 232 | TEST(I2CCompareBitActionTests, ToString) |
| 233 | { |
| 234 | I2CCompareBitAction action{0x7C, 5, 1}; |
| 235 | EXPECT_EQ(action.toString(), |
| 236 | "i2c_compare_bit: { register: 0x7C, position: 5, value: 1 }"); |
| 237 | } |