blob: bd1b51add727c972667cdfd8944bd8701129275a [file] [log] [blame]
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -06001/**
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
25using namespace phosphor::power::sequencer;
26
27TEST(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
44TEST(RailTests, Constructor)
45{
46 // Test where succeeds: No optional parameters have values
47 {
Shawn McCarney16e493a2024-01-29 14:20:32 -060048 std::string name{"12.0V"};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060049 std::optional<std::string> presence{};
50 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -060051 bool isPowerSupplyRail{true};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060052 bool checkStatusVout{false};
53 bool compareVoltageToLimits{false};
54 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -060055 Rail rail{name,
56 presence,
57 page,
58 isPowerSupplyRail,
59 checkStatusVout,
60 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060061 gpio};
62
Shawn McCarney16e493a2024-01-29 14:20:32 -060063 EXPECT_EQ(rail.getName(), "12.0V");
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060064 EXPECT_FALSE(rail.getPresence().has_value());
65 EXPECT_FALSE(rail.getPage().has_value());
Shawn McCarney16e493a2024-01-29 14:20:32 -060066 EXPECT_TRUE(rail.isPowerSupplyRail());
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060067 EXPECT_FALSE(rail.getCheckStatusVout());
68 EXPECT_FALSE(rail.getCompareVoltageToLimits());
69 EXPECT_FALSE(rail.getGPIO().has_value());
70 }
71
72 // Test where succeeds: All optional parameters have values
73 {
74 std::string name{"VCS_CPU1"};
75 std::optional<std::string> presence{
76 "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1"};
77 std::optional<uint8_t> page{11};
Shawn McCarney16e493a2024-01-29 14:20:32 -060078 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060079 bool checkStatusVout{true};
80 bool compareVoltageToLimits{true};
81 std::optional<GPIO> gpio{GPIO(60, true)};
Shawn McCarney16e493a2024-01-29 14:20:32 -060082 Rail rail{name,
83 presence,
84 page,
85 isPowerSupplyRail,
86 checkStatusVout,
87 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060088 gpio};
89
90 EXPECT_EQ(rail.getName(), "VCS_CPU1");
91 EXPECT_TRUE(rail.getPresence().has_value());
92 EXPECT_EQ(
93 rail.getPresence().value(),
94 "/xyz/openbmc_project/inventory/system/chassis/motherboard/cpu1");
95 EXPECT_TRUE(rail.getPage().has_value());
96 EXPECT_EQ(rail.getPage().value(), 11);
Shawn McCarney16e493a2024-01-29 14:20:32 -060097 EXPECT_FALSE(rail.isPowerSupplyRail());
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -060098 EXPECT_TRUE(rail.getCheckStatusVout());
99 EXPECT_TRUE(rail.getCompareVoltageToLimits());
100 EXPECT_TRUE(rail.getGPIO().has_value());
101 EXPECT_EQ(rail.getGPIO().value().line, 60);
102 EXPECT_TRUE(rail.getGPIO().value().activeLow);
103 }
104
105 // Test where fails: checkStatusVout is true and page has no value
106 {
107 std::string name{"VDD1"};
108 std::optional<std::string> presence{};
109 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600110 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600111 bool checkStatusVout{true};
112 bool compareVoltageToLimits{false};
113 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600114 EXPECT_THROW((Rail{name, presence, page, isPowerSupplyRail,
115 checkStatusVout, compareVoltageToLimits, gpio}),
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600116 std::invalid_argument);
117 }
118
119 // Test where fails: compareVoltageToLimits is true and page has no value
120 {
121 std::string name{"VDD1"};
122 std::optional<std::string> presence{};
123 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600124 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600125 bool checkStatusVout{false};
126 bool compareVoltageToLimits{true};
127 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600128 EXPECT_THROW((Rail{name, presence, page, isPowerSupplyRail,
129 checkStatusVout, compareVoltageToLimits, gpio}),
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600130 std::invalid_argument);
131 }
132}
133
134TEST(RailTests, GetName)
135{
136 std::string name{"VDD2"};
137 std::optional<std::string> presence{};
138 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600139 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600140 bool checkStatusVout{false};
141 bool compareVoltageToLimits{false};
142 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600143 Rail rail{name,
144 presence,
145 page,
146 isPowerSupplyRail,
147 checkStatusVout,
148 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600149 gpio};
150
151 EXPECT_EQ(rail.getName(), "VDD2");
152}
153
154TEST(RailTests, GetPresence)
155{
156 std::string name{"VDDR2"};
157 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600158 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600159 bool checkStatusVout{false};
160 bool compareVoltageToLimits{false};
161 std::optional<GPIO> gpio{};
162
163 // Test where presence has no value
164 {
165 std::optional<std::string> presence{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600166 Rail rail{name,
167 presence,
168 page,
169 isPowerSupplyRail,
170 checkStatusVout,
171 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600172 gpio};
173 EXPECT_FALSE(rail.getPresence().has_value());
174 }
175
176 // Test where presence has a value
177 {
178 std::optional<std::string> presence{
179 "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2"};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600180 Rail rail{name,
181 presence,
182 page,
183 isPowerSupplyRail,
184 checkStatusVout,
185 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600186 gpio};
187 EXPECT_TRUE(rail.getPresence().has_value());
188 EXPECT_EQ(
189 rail.getPresence().value(),
190 "/xyz/openbmc_project/inventory/system/chassis/motherboard/dimm2");
191 }
192}
193
194TEST(RailTests, GetPage)
195{
196 std::string name{"VDD2"};
197 std::optional<std::string> presence{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600198 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600199 bool checkStatusVout{false};
200 bool compareVoltageToLimits{false};
201 std::optional<GPIO> gpio{};
202
203 // Test where page has no value
204 {
205 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600206 Rail rail{name,
207 presence,
208 page,
209 isPowerSupplyRail,
210 checkStatusVout,
211 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600212 gpio};
213 EXPECT_FALSE(rail.getPage().has_value());
214 }
215
216 // Test where page has a value
217 {
218 std::optional<uint8_t> page{7};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600219 Rail rail{name,
220 presence,
221 page,
222 isPowerSupplyRail,
223 checkStatusVout,
224 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600225 gpio};
226 EXPECT_TRUE(rail.getPage().has_value());
227 EXPECT_EQ(rail.getPage().value(), 7);
228 }
229}
230
Shawn McCarney16e493a2024-01-29 14:20:32 -0600231TEST(RailTests, IsPowerSupplyRail)
232{
233 std::string name{"12.0V"};
234 std::optional<std::string> presence{};
235 std::optional<uint8_t> page{};
236 bool isPowerSupplyRail{true};
237 bool checkStatusVout{false};
238 bool compareVoltageToLimits{false};
239 std::optional<GPIO> gpio{};
240 Rail rail{name,
241 presence,
242 page,
243 isPowerSupplyRail,
244 checkStatusVout,
245 compareVoltageToLimits,
246 gpio};
247
248 EXPECT_TRUE(rail.isPowerSupplyRail());
249}
250
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600251TEST(RailTests, GetCheckStatusVout)
252{
253 std::string name{"VDD2"};
254 std::optional<std::string> presence{};
255 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600256 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600257 bool checkStatusVout{false};
258 bool compareVoltageToLimits{false};
259 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600260 Rail rail{name,
261 presence,
262 page,
263 isPowerSupplyRail,
264 checkStatusVout,
265 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600266 gpio};
267
268 EXPECT_FALSE(rail.getCheckStatusVout());
269}
270
271TEST(RailTests, GetCompareVoltageToLimits)
272{
273 std::string name{"VDD2"};
274 std::optional<std::string> presence{};
275 std::optional<uint8_t> page{13};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600276 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600277 bool checkStatusVout{false};
278 bool compareVoltageToLimits{true};
279 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600280 Rail rail{name,
281 presence,
282 page,
283 isPowerSupplyRail,
284 checkStatusVout,
285 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600286 gpio};
287
288 EXPECT_TRUE(rail.getCompareVoltageToLimits());
289}
290
291TEST(RailTests, GetGPIO)
292{
293 std::string name{"VDD2"};
294 std::optional<std::string> presence{};
295 std::optional<uint8_t> page{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600296 bool isPowerSupplyRail{false};
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600297 bool checkStatusVout{false};
298 bool compareVoltageToLimits{false};
299
300 // Test where gpio has no value
301 {
302 std::optional<GPIO> gpio{};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600303 Rail rail{name,
304 presence,
305 page,
306 isPowerSupplyRail,
307 checkStatusVout,
308 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600309 gpio};
310 EXPECT_FALSE(rail.getGPIO().has_value());
311 }
312
313 // Test where gpio has a value
314 {
315 std::optional<GPIO> gpio{GPIO(12, false)};
Shawn McCarney16e493a2024-01-29 14:20:32 -0600316 Rail rail{name,
317 presence,
318 page,
319 isPowerSupplyRail,
320 checkStatusVout,
321 compareVoltageToLimits,
Shawn McCarneyd6e9bfe2024-01-05 17:52:04 -0600322 gpio};
323 EXPECT_TRUE(rail.getGPIO().has_value());
324 EXPECT_EQ(rail.getGPIO().value().line, 12);
325 EXPECT_FALSE(rail.getGPIO().value().activeLow);
326 }
327}