Shawn McCarney | d6e9bfe | 2024-01-05 17:52:04 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2024 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 | |
| 17 | #include "rail.hpp" |
| 18 | |
| 19 | #include <cstdint> |
| 20 | #include <optional> |
| 21 | #include <string> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | using namespace phosphor::power::sequencer; |
| 26 | |
| 27 | TEST(GPIOTests, Initialization) |
| 28 | { |
| 29 | // Default initialization |
| 30 | { |
| 31 | GPIO gpio; |
| 32 | EXPECT_EQ(gpio.line, 0); |
| 33 | EXPECT_FALSE(gpio.activeLow); |
| 34 | } |
| 35 | |
| 36 | // Explicit initialization |
| 37 | { |
| 38 | GPIO gpio{48, true}; |
| 39 | EXPECT_EQ(gpio.line, 48); |
| 40 | EXPECT_TRUE(gpio.activeLow); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | TEST(RailTests, Constructor) |
| 45 | { |
| 46 | // Test where succeeds: No optional parameters have values |
| 47 | { |
| 48 | std::string name{"VDD1"}; |
| 49 | std::optional<std::string> presence{}; |
| 50 | std::optional<uint8_t> page{}; |
| 51 | bool checkStatusVout{false}; |
| 52 | bool compareVoltageToLimits{false}; |
| 53 | std::optional<GPIO> gpio{}; |
| 54 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 55 | gpio}; |
| 56 | |
| 57 | EXPECT_EQ(rail.getName(), "VDD1"); |
| 58 | EXPECT_FALSE(rail.getPresence().has_value()); |
| 59 | EXPECT_FALSE(rail.getPage().has_value()); |
| 60 | EXPECT_FALSE(rail.getCheckStatusVout()); |
| 61 | EXPECT_FALSE(rail.getCompareVoltageToLimits()); |
| 62 | EXPECT_FALSE(rail.getGPIO().has_value()); |
| 63 | } |
| 64 | |
| 65 | // Test where succeeds: All optional parameters have values |
| 66 | { |
| 67 | std::string name{"VCS_CPU1"}; |
| 68 | std::optional<std::string> presence{ |
| 69 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1"}; |
| 70 | std::optional<uint8_t> page{11}; |
| 71 | bool checkStatusVout{true}; |
| 72 | bool compareVoltageToLimits{true}; |
| 73 | std::optional<GPIO> gpio{GPIO(60, true)}; |
| 74 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 75 | gpio}; |
| 76 | |
| 77 | EXPECT_EQ(rail.getName(), "VCS_CPU1"); |
| 78 | EXPECT_TRUE(rail.getPresence().has_value()); |
| 79 | EXPECT_EQ( |
| 80 | rail.getPresence().value(), |
| 81 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1"); |
| 82 | EXPECT_TRUE(rail.getPage().has_value()); |
| 83 | EXPECT_EQ(rail.getPage().value(), 11); |
| 84 | EXPECT_TRUE(rail.getCheckStatusVout()); |
| 85 | EXPECT_TRUE(rail.getCompareVoltageToLimits()); |
| 86 | EXPECT_TRUE(rail.getGPIO().has_value()); |
| 87 | EXPECT_EQ(rail.getGPIO().value().line, 60); |
| 88 | EXPECT_TRUE(rail.getGPIO().value().activeLow); |
| 89 | } |
| 90 | |
| 91 | // Test where fails: checkStatusVout is true and page has no value |
| 92 | { |
| 93 | std::string name{"VDD1"}; |
| 94 | std::optional<std::string> presence{}; |
| 95 | std::optional<uint8_t> page{}; |
| 96 | bool checkStatusVout{true}; |
| 97 | bool compareVoltageToLimits{false}; |
| 98 | std::optional<GPIO> gpio{}; |
| 99 | EXPECT_THROW((Rail{name, presence, page, checkStatusVout, |
| 100 | compareVoltageToLimits, gpio}), |
| 101 | std::invalid_argument); |
| 102 | } |
| 103 | |
| 104 | // Test where fails: compareVoltageToLimits is true and page has no value |
| 105 | { |
| 106 | std::string name{"VDD1"}; |
| 107 | std::optional<std::string> presence{}; |
| 108 | std::optional<uint8_t> page{}; |
| 109 | bool checkStatusVout{false}; |
| 110 | bool compareVoltageToLimits{true}; |
| 111 | std::optional<GPIO> gpio{}; |
| 112 | EXPECT_THROW((Rail{name, presence, page, checkStatusVout, |
| 113 | compareVoltageToLimits, gpio}), |
| 114 | std::invalid_argument); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | TEST(RailTests, GetName) |
| 119 | { |
| 120 | std::string name{"VDD2"}; |
| 121 | std::optional<std::string> presence{}; |
| 122 | std::optional<uint8_t> page{}; |
| 123 | bool checkStatusVout{false}; |
| 124 | bool compareVoltageToLimits{false}; |
| 125 | std::optional<GPIO> gpio{}; |
| 126 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 127 | gpio}; |
| 128 | |
| 129 | EXPECT_EQ(rail.getName(), "VDD2"); |
| 130 | } |
| 131 | |
| 132 | TEST(RailTests, GetPresence) |
| 133 | { |
| 134 | std::string name{"VDDR2"}; |
| 135 | std::optional<uint8_t> page{}; |
| 136 | bool checkStatusVout{false}; |
| 137 | bool compareVoltageToLimits{false}; |
| 138 | std::optional<GPIO> gpio{}; |
| 139 | |
| 140 | // Test where presence has no value |
| 141 | { |
| 142 | std::optional<std::string> presence{}; |
| 143 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 144 | gpio}; |
| 145 | EXPECT_FALSE(rail.getPresence().has_value()); |
| 146 | } |
| 147 | |
| 148 | // Test where presence has a value |
| 149 | { |
| 150 | std::optional<std::string> presence{ |
| 151 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2"}; |
| 152 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 153 | gpio}; |
| 154 | EXPECT_TRUE(rail.getPresence().has_value()); |
| 155 | EXPECT_EQ( |
| 156 | rail.getPresence().value(), |
| 157 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2"); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | TEST(RailTests, GetPage) |
| 162 | { |
| 163 | std::string name{"VDD2"}; |
| 164 | std::optional<std::string> presence{}; |
| 165 | bool checkStatusVout{false}; |
| 166 | bool compareVoltageToLimits{false}; |
| 167 | std::optional<GPIO> gpio{}; |
| 168 | |
| 169 | // Test where page has no value |
| 170 | { |
| 171 | std::optional<uint8_t> page{}; |
| 172 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 173 | gpio}; |
| 174 | EXPECT_FALSE(rail.getPage().has_value()); |
| 175 | } |
| 176 | |
| 177 | // Test where page has a value |
| 178 | { |
| 179 | std::optional<uint8_t> page{7}; |
| 180 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 181 | gpio}; |
| 182 | EXPECT_TRUE(rail.getPage().has_value()); |
| 183 | EXPECT_EQ(rail.getPage().value(), 7); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | TEST(RailTests, GetCheckStatusVout) |
| 188 | { |
| 189 | std::string name{"VDD2"}; |
| 190 | std::optional<std::string> presence{}; |
| 191 | std::optional<uint8_t> page{}; |
| 192 | bool checkStatusVout{false}; |
| 193 | bool compareVoltageToLimits{false}; |
| 194 | std::optional<GPIO> gpio{}; |
| 195 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 196 | gpio}; |
| 197 | |
| 198 | EXPECT_FALSE(rail.getCheckStatusVout()); |
| 199 | } |
| 200 | |
| 201 | TEST(RailTests, GetCompareVoltageToLimits) |
| 202 | { |
| 203 | std::string name{"VDD2"}; |
| 204 | std::optional<std::string> presence{}; |
| 205 | std::optional<uint8_t> page{13}; |
| 206 | bool checkStatusVout{false}; |
| 207 | bool compareVoltageToLimits{true}; |
| 208 | std::optional<GPIO> gpio{}; |
| 209 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 210 | gpio}; |
| 211 | |
| 212 | EXPECT_TRUE(rail.getCompareVoltageToLimits()); |
| 213 | } |
| 214 | |
| 215 | TEST(RailTests, GetGPIO) |
| 216 | { |
| 217 | std::string name{"VDD2"}; |
| 218 | std::optional<std::string> presence{}; |
| 219 | std::optional<uint8_t> page{}; |
| 220 | bool checkStatusVout{false}; |
| 221 | bool compareVoltageToLimits{false}; |
| 222 | |
| 223 | // Test where gpio has no value |
| 224 | { |
| 225 | std::optional<GPIO> gpio{}; |
| 226 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 227 | gpio}; |
| 228 | EXPECT_FALSE(rail.getGPIO().has_value()); |
| 229 | } |
| 230 | |
| 231 | // Test where gpio has a value |
| 232 | { |
| 233 | std::optional<GPIO> gpio{GPIO(12, false)}; |
| 234 | Rail rail{name, presence, page, checkStatusVout, compareVoltageToLimits, |
| 235 | gpio}; |
| 236 | EXPECT_TRUE(rail.getGPIO().has_value()); |
| 237 | EXPECT_EQ(rail.getGPIO().value().line, 12); |
| 238 | EXPECT_FALSE(rail.getGPIO().value().activeLow); |
| 239 | } |
| 240 | } |