xref: /openbmc/phosphor-power/phosphor-power-sequencer/test/gpios_only_device_tests.cpp (revision 7b7a5632c594d60f4620ca14379a766a56faf846)
1 /**
2  * Copyright © 2025 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 "gpios_only_device.hpp"
18 #include "mock_services.hpp"
19 #include "rail.hpp"
20 #include "services.hpp"
21 
22 #include <cstdint>
23 #include <map>
24 #include <memory>
25 #include <string>
26 #include <vector>
27 
28 #include <gmock/gmock.h>
29 #include <gtest/gtest.h>
30 
31 using namespace phosphor::power::sequencer;
32 
TEST(GPIOsOnlyDeviceTests,Constructor)33 TEST(GPIOsOnlyDeviceTests, Constructor)
34 {
35     std::string powerControlGPIOName{"power-chassis-control"};
36     std::string powerGoodGPIOName{"power-chassis-good"};
37     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
38 
39     EXPECT_EQ(device.getPowerControlGPIOName(), powerControlGPIOName);
40     EXPECT_EQ(device.getPowerGoodGPIOName(), powerGoodGPIOName);
41 }
42 
TEST(GPIOsOnlyDeviceTests,GetName)43 TEST(GPIOsOnlyDeviceTests, GetName)
44 {
45     std::string powerControlGPIOName{"power-chassis-control"};
46     std::string powerGoodGPIOName{"power-chassis-good"};
47     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
48 
49     EXPECT_EQ(device.getName(), GPIOsOnlyDevice::deviceName);
50 }
51 
TEST(GPIOsOnlyDeviceTests,GetBus)52 TEST(GPIOsOnlyDeviceTests, GetBus)
53 {
54     std::string powerControlGPIOName{"power-chassis-control"};
55     std::string powerGoodGPIOName{"power-chassis-good"};
56     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
57 
58     EXPECT_EQ(device.getBus(), 0);
59 }
60 
TEST(GPIOsOnlyDeviceTests,GetAddress)61 TEST(GPIOsOnlyDeviceTests, GetAddress)
62 {
63     std::string powerControlGPIOName{"power-chassis-control"};
64     std::string powerGoodGPIOName{"power-chassis-good"};
65     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
66 
67     EXPECT_EQ(device.getAddress(), 0);
68 }
69 
TEST(GPIOsOnlyDeviceTests,GetPowerControlGPIOName)70 TEST(GPIOsOnlyDeviceTests, GetPowerControlGPIOName)
71 {
72     std::string powerControlGPIOName{"power-on"};
73     std::string powerGoodGPIOName{"chassis-pgood"};
74     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
75 
76     EXPECT_EQ(device.getPowerControlGPIOName(), powerControlGPIOName);
77 }
78 
TEST(GPIOsOnlyDeviceTests,GetPowerGoodGPIOName)79 TEST(GPIOsOnlyDeviceTests, GetPowerGoodGPIOName)
80 {
81     std::string powerControlGPIOName{"power-on"};
82     std::string powerGoodGPIOName{"chassis-pgood"};
83     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
84 
85     EXPECT_EQ(device.getPowerGoodGPIOName(), powerGoodGPIOName);
86 }
87 
TEST(GPIOsOnlyDeviceTests,GetRails)88 TEST(GPIOsOnlyDeviceTests, GetRails)
89 {
90     std::string powerControlGPIOName{"power-on"};
91     std::string powerGoodGPIOName{"chassis-pgood"};
92     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
93 
94     EXPECT_TRUE(device.getRails().empty());
95 }
96 
TEST(GPIOsOnlyDeviceTests,GetGPIOValues)97 TEST(GPIOsOnlyDeviceTests, GetGPIOValues)
98 {
99     try
100     {
101         std::string powerControlGPIOName{"power-on"};
102         std::string powerGoodGPIOName{"chassis-pgood"};
103         GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
104 
105         MockServices services{};
106         device.getGPIOValues(services);
107         ADD_FAILURE() << "Should not have reached this line.";
108     }
109     catch (const std::exception& e)
110     {
111         EXPECT_STREQ(e.what(), "getGPIOValues() is not supported");
112     }
113 }
114 
TEST(GPIOsOnlyDeviceTests,GetStatusWord)115 TEST(GPIOsOnlyDeviceTests, GetStatusWord)
116 {
117     try
118     {
119         std::string powerControlGPIOName{"power-on"};
120         std::string powerGoodGPIOName{"chassis-pgood"};
121         GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
122 
123         device.getStatusWord(0);
124         ADD_FAILURE() << "Should not have reached this line.";
125     }
126     catch (const std::exception& e)
127     {
128         EXPECT_STREQ(e.what(), "getStatusWord() is not supported");
129     }
130 }
131 
TEST(GPIOsOnlyDeviceTests,GetStatusVout)132 TEST(GPIOsOnlyDeviceTests, GetStatusVout)
133 {
134     try
135     {
136         std::string powerControlGPIOName{"power-on"};
137         std::string powerGoodGPIOName{"chassis-pgood"};
138         GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
139 
140         device.getStatusVout(0);
141         ADD_FAILURE() << "Should not have reached this line.";
142     }
143     catch (const std::exception& e)
144     {
145         EXPECT_STREQ(e.what(), "getStatusVout() is not supported");
146     }
147 }
148 
TEST(GPIOsOnlyDeviceTests,GetReadVout)149 TEST(GPIOsOnlyDeviceTests, GetReadVout)
150 {
151     try
152     {
153         std::string powerControlGPIOName{"power-on"};
154         std::string powerGoodGPIOName{"chassis-pgood"};
155         GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
156 
157         device.getReadVout(0);
158         ADD_FAILURE() << "Should not have reached this line.";
159     }
160     catch (const std::exception& e)
161     {
162         EXPECT_STREQ(e.what(), "getReadVout() is not supported");
163     }
164 }
165 
TEST(GPIOsOnlyDeviceTests,GetVoutUVFaultLimit)166 TEST(GPIOsOnlyDeviceTests, GetVoutUVFaultLimit)
167 {
168     try
169     {
170         std::string powerControlGPIOName{"power-on"};
171         std::string powerGoodGPIOName{"chassis-pgood"};
172         GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
173 
174         device.getVoutUVFaultLimit(0);
175         ADD_FAILURE() << "Should not have reached this line.";
176     }
177     catch (const std::exception& e)
178     {
179         EXPECT_STREQ(e.what(), "getVoutUVFaultLimit() is not supported");
180     }
181 }
182 
TEST(GPIOsOnlyDeviceTests,FindPgoodFault)183 TEST(GPIOsOnlyDeviceTests, FindPgoodFault)
184 {
185     std::string powerControlGPIOName{"power-on"};
186     std::string powerGoodGPIOName{"chassis-pgood"};
187     GPIOsOnlyDevice device{powerControlGPIOName, powerGoodGPIOName};
188 
189     MockServices services{};
190     std::string powerSupplyError{};
191     std::map<std::string, std::string> additionalData{};
192     std::string error =
193         device.findPgoodFault(services, powerSupplyError, additionalData);
194     EXPECT_TRUE(error.empty());
195     EXPECT_EQ(additionalData.size(), 0);
196 }
197