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{"12.0V"};
49         std::optional<std::string> presence{};
50         std::optional<uint8_t> page{};
51         bool isPowerSupplyRail{true};
52         bool checkStatusVout{false};
53         bool compareVoltageToLimit{false};
54         std::optional<GPIO> gpio{};
55         Rail rail{name,
56                   presence,
57                   page,
58                   isPowerSupplyRail,
59                   checkStatusVout,
60                   compareVoltageToLimit,
61                   gpio};
62 
63         EXPECT_EQ(rail.getName(), "12.0V");
64         EXPECT_FALSE(rail.getPresence().has_value());
65         EXPECT_FALSE(rail.getPage().has_value());
66         EXPECT_TRUE(rail.isPowerSupplyRail());
67         EXPECT_FALSE(rail.getCheckStatusVout());
68         EXPECT_FALSE(rail.getCompareVoltageToLimit());
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};
78         bool isPowerSupplyRail{false};
79         bool checkStatusVout{true};
80         bool compareVoltageToLimit{true};
81         std::optional<GPIO> gpio{GPIO(60, true)};
82         Rail rail{name,
83                   presence,
84                   page,
85                   isPowerSupplyRail,
86                   checkStatusVout,
87                   compareVoltageToLimit,
88                   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);
97         EXPECT_FALSE(rail.isPowerSupplyRail());
98         EXPECT_TRUE(rail.getCheckStatusVout());
99         EXPECT_TRUE(rail.getCompareVoltageToLimit());
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{};
110         bool isPowerSupplyRail{false};
111         bool checkStatusVout{true};
112         bool compareVoltageToLimit{false};
113         std::optional<GPIO> gpio{};
114         EXPECT_THROW((Rail{name, presence, page, isPowerSupplyRail,
115                            checkStatusVout, compareVoltageToLimit, gpio}),
116                      std::invalid_argument);
117     }
118 
119     // Test where fails: compareVoltageToLimit 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{};
124         bool isPowerSupplyRail{false};
125         bool checkStatusVout{false};
126         bool compareVoltageToLimit{true};
127         std::optional<GPIO> gpio{};
128         EXPECT_THROW((Rail{name, presence, page, isPowerSupplyRail,
129                            checkStatusVout, compareVoltageToLimit, gpio}),
130                      std::invalid_argument);
131     }
132 }
133 
134 TEST(RailTests, GetName)
135 {
136     std::string name{"VDD2"};
137     std::optional<std::string> presence{};
138     std::optional<uint8_t> page{};
139     bool isPowerSupplyRail{false};
140     bool checkStatusVout{false};
141     bool compareVoltageToLimit{false};
142     std::optional<GPIO> gpio{};
143     Rail rail{name,
144               presence,
145               page,
146               isPowerSupplyRail,
147               checkStatusVout,
148               compareVoltageToLimit,
149               gpio};
150 
151     EXPECT_EQ(rail.getName(), "VDD2");
152 }
153 
154 TEST(RailTests, GetPresence)
155 {
156     std::string name{"VDDR2"};
157     std::optional<uint8_t> page{};
158     bool isPowerSupplyRail{false};
159     bool checkStatusVout{false};
160     bool compareVoltageToLimit{false};
161     std::optional<GPIO> gpio{};
162 
163     // Test where presence has no value
164     {
165         std::optional<std::string> presence{};
166         Rail rail{name,
167                   presence,
168                   page,
169                   isPowerSupplyRail,
170                   checkStatusVout,
171                   compareVoltageToLimit,
172                   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"};
180         Rail rail{name,
181                   presence,
182                   page,
183                   isPowerSupplyRail,
184                   checkStatusVout,
185                   compareVoltageToLimit,
186                   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 
194 TEST(RailTests, GetPage)
195 {
196     std::string name{"VDD2"};
197     std::optional<std::string> presence{};
198     bool isPowerSupplyRail{false};
199     bool checkStatusVout{false};
200     bool compareVoltageToLimit{false};
201     std::optional<GPIO> gpio{};
202 
203     // Test where page has no value
204     {
205         std::optional<uint8_t> page{};
206         Rail rail{name,
207                   presence,
208                   page,
209                   isPowerSupplyRail,
210                   checkStatusVout,
211                   compareVoltageToLimit,
212                   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};
219         Rail rail{name,
220                   presence,
221                   page,
222                   isPowerSupplyRail,
223                   checkStatusVout,
224                   compareVoltageToLimit,
225                   gpio};
226         EXPECT_TRUE(rail.getPage().has_value());
227         EXPECT_EQ(rail.getPage().value(), 7);
228     }
229 }
230 
231 TEST(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 compareVoltageToLimit{false};
239     std::optional<GPIO> gpio{};
240     Rail rail{name,
241               presence,
242               page,
243               isPowerSupplyRail,
244               checkStatusVout,
245               compareVoltageToLimit,
246               gpio};
247 
248     EXPECT_TRUE(rail.isPowerSupplyRail());
249 }
250 
251 TEST(RailTests, GetCheckStatusVout)
252 {
253     std::string name{"VDD2"};
254     std::optional<std::string> presence{};
255     std::optional<uint8_t> page{};
256     bool isPowerSupplyRail{false};
257     bool checkStatusVout{false};
258     bool compareVoltageToLimit{false};
259     std::optional<GPIO> gpio{};
260     Rail rail{name,
261               presence,
262               page,
263               isPowerSupplyRail,
264               checkStatusVout,
265               compareVoltageToLimit,
266               gpio};
267 
268     EXPECT_FALSE(rail.getCheckStatusVout());
269 }
270 
271 TEST(RailTests, GetCompareVoltageToLimit)
272 {
273     std::string name{"VDD2"};
274     std::optional<std::string> presence{};
275     std::optional<uint8_t> page{13};
276     bool isPowerSupplyRail{false};
277     bool checkStatusVout{false};
278     bool compareVoltageToLimit{true};
279     std::optional<GPIO> gpio{};
280     Rail rail{name,
281               presence,
282               page,
283               isPowerSupplyRail,
284               checkStatusVout,
285               compareVoltageToLimit,
286               gpio};
287 
288     EXPECT_TRUE(rail.getCompareVoltageToLimit());
289 }
290 
291 TEST(RailTests, GetGPIO)
292 {
293     std::string name{"VDD2"};
294     std::optional<std::string> presence{};
295     std::optional<uint8_t> page{};
296     bool isPowerSupplyRail{false};
297     bool checkStatusVout{false};
298     bool compareVoltageToLimit{false};
299 
300     // Test where gpio has no value
301     {
302         std::optional<GPIO> gpio{};
303         Rail rail{name,
304                   presence,
305                   page,
306                   isPowerSupplyRail,
307                   checkStatusVout,
308                   compareVoltageToLimit,
309                   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)};
316         Rail rail{name,
317                   presence,
318                   page,
319                   isPowerSupplyRail,
320                   checkStatusVout,
321                   compareVoltageToLimit,
322                   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 }
328