13f1242f3SBrandon Wyman #include "../power_supply.hpp"
23f1242f3SBrandon Wyman #include "mock.hpp"
33f1242f3SBrandon Wyman 
43f1242f3SBrandon Wyman #include <xyz/openbmc_project/Common/Device/error.hpp>
53f1242f3SBrandon Wyman #include <xyz/openbmc_project/Common/error.hpp>
63f1242f3SBrandon Wyman 
73f1242f3SBrandon Wyman #include <gmock/gmock.h>
83f1242f3SBrandon Wyman #include <gtest/gtest.h>
93f1242f3SBrandon Wyman 
103f1242f3SBrandon Wyman using namespace phosphor::power::psu;
113f1242f3SBrandon Wyman using namespace phosphor::pmbus;
123f1242f3SBrandon Wyman 
133f1242f3SBrandon Wyman using ::testing::_;
1459a35793SBrandon Wyman using ::testing::Args;
153f1242f3SBrandon Wyman using ::testing::Assign;
163f1242f3SBrandon Wyman using ::testing::DoAll;
1759a35793SBrandon Wyman using ::testing::ElementsAre;
1859a35793SBrandon Wyman using ::testing::NotNull;
193f1242f3SBrandon Wyman using ::testing::Return;
203f1242f3SBrandon Wyman using ::testing::StrEq;
213f1242f3SBrandon Wyman 
223f1242f3SBrandon Wyman static auto PSUInventoryPath = "/xyz/bmc/inv/sys/chassis/board/powersupply0";
233f1242f3SBrandon Wyman 
243f1242f3SBrandon Wyman class PowerSupplyTests : public ::testing::Test
253f1242f3SBrandon Wyman {
263f1242f3SBrandon Wyman   public:
273f1242f3SBrandon Wyman     PowerSupplyTests() :
283f1242f3SBrandon Wyman         mockedUtil(reinterpret_cast<const MockedUtil&>(getUtils()))
293f1242f3SBrandon Wyman     {
303f1242f3SBrandon Wyman         ON_CALL(mockedUtil, getPresence(_, _)).WillByDefault(Return(false));
313f1242f3SBrandon Wyman     }
323f1242f3SBrandon Wyman 
333f1242f3SBrandon Wyman     ~PowerSupplyTests() override
343f1242f3SBrandon Wyman     {
353f1242f3SBrandon Wyman         freeUtils();
363f1242f3SBrandon Wyman     }
373f1242f3SBrandon Wyman 
383f1242f3SBrandon Wyman     const MockedUtil& mockedUtil;
393f1242f3SBrandon Wyman };
403f1242f3SBrandon Wyman 
413f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, Constructor)
423f1242f3SBrandon Wyman {
433f1242f3SBrandon Wyman     /**
443f1242f3SBrandon Wyman      * @param[in] invpath - String for inventory path to use
453f1242f3SBrandon Wyman      * @param[in] i2cbus - The bus number this power supply is on
463f1242f3SBrandon Wyman      * @param[in] i2caddr - The 16-bit I2C address of the power supply
473f1242f3SBrandon Wyman      */
483f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
491d7a7df8SBrandon Wyman 
501d7a7df8SBrandon Wyman     // Try where inventory path is empty, constructor should fail.
511d7a7df8SBrandon Wyman     try
521d7a7df8SBrandon Wyman     {
53510acaabSBrandon Wyman         auto psu = std::make_unique<PowerSupply>(bus, "", 3, 0x68);
541d7a7df8SBrandon Wyman         ADD_FAILURE() << "Should not have reached this line.";
551d7a7df8SBrandon Wyman     }
561d7a7df8SBrandon Wyman     catch (const std::invalid_argument& e)
571d7a7df8SBrandon Wyman     {
581d7a7df8SBrandon Wyman         EXPECT_STREQ(e.what(), "Invalid empty inventoryPath");
591d7a7df8SBrandon Wyman     }
601d7a7df8SBrandon Wyman     catch (...)
611d7a7df8SBrandon Wyman     {
621d7a7df8SBrandon Wyman         ADD_FAILURE() << "Should not have caught exception.";
631d7a7df8SBrandon Wyman     }
641d7a7df8SBrandon Wyman 
651d7a7df8SBrandon Wyman     // Test with valid arguments
661d7a7df8SBrandon Wyman     try
671d7a7df8SBrandon Wyman     {
681d7a7df8SBrandon Wyman         EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
691d7a7df8SBrandon Wyman             .Times(1);
701d7a7df8SBrandon Wyman         auto psu =
71510acaabSBrandon Wyman             std::make_unique<PowerSupply>(bus, PSUInventoryPath, 3, 0x68);
723f1242f3SBrandon Wyman 
733f1242f3SBrandon Wyman         EXPECT_EQ(psu->isPresent(), false);
743f1242f3SBrandon Wyman         EXPECT_EQ(psu->isFaulted(), false);
753f1242f3SBrandon Wyman         EXPECT_EQ(psu->hasInputFault(), false);
763f1242f3SBrandon Wyman         EXPECT_EQ(psu->hasMFRFault(), false);
773f1242f3SBrandon Wyman         EXPECT_EQ(psu->hasVINUVFault(), false);
783f1242f3SBrandon Wyman     }
791d7a7df8SBrandon Wyman     catch (...)
801d7a7df8SBrandon Wyman     {
811d7a7df8SBrandon Wyman         ADD_FAILURE() << "Should not have caught exception.";
821d7a7df8SBrandon Wyman     }
831d7a7df8SBrandon Wyman }
843f1242f3SBrandon Wyman 
853f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, Analyze)
863f1242f3SBrandon Wyman {
873f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
883f1242f3SBrandon Wyman 
893f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath))).Times(1);
90510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 4, 0x69};
913f1242f3SBrandon Wyman     psu.analyze();
923f1242f3SBrandon Wyman     // By default, nothing should change.
933f1242f3SBrandon Wyman     EXPECT_EQ(psu.isPresent(), false);
943f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), false);
953f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
963f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
973f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
983f1242f3SBrandon Wyman 
993f1242f3SBrandon Wyman     // In order to get the various faults tested, the power supply needs to be
1003f1242f3SBrandon Wyman     // present in order to read from the PMBus device(s).
1013f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
1023f1242f3SBrandon Wyman         .Times(1)
1033f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
104510acaabSBrandon Wyman     PowerSupply psu2{bus, PSUInventoryPath, 5, 0x6a};
1053f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1063f1242f3SBrandon Wyman 
1073f1242f3SBrandon Wyman     // STATUS_WORD 0x0000 is powered on, no faults.
1083f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu2.getPMBus());
1093f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
1103f1242f3SBrandon Wyman     psu2.analyze();
1113f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1123f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), false);
1133f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), false);
1143f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), false);
1153f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), false);
1163f1242f3SBrandon Wyman 
1173f1242f3SBrandon Wyman     // STATUS_WORD input fault/warn
1183f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
11910d94055SJay Meyer         .Times(2)
12010d94055SJay Meyer         .WillOnce(Return(status_word::INPUT_FAULT_WARN))
12110d94055SJay Meyer         .WillOnce(Return(0x0000));
1223f1242f3SBrandon Wyman     psu2.analyze();
1233f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1243f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), true);
1253f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), true);
1263f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), false);
1273f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), false);
1283f1242f3SBrandon Wyman 
1293f1242f3SBrandon Wyman     // STATUS_WORD INPUT/UV fault.
1303f1242f3SBrandon Wyman     // First need it to return good status, then the fault
1313f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
1323f1242f3SBrandon Wyman         .WillOnce(Return(0x0000))
13310d94055SJay Meyer         .WillOnce(Return(status_word::VIN_UV_FAULT))
13410d94055SJay Meyer         .WillOnce(Return(0x0000));
1353f1242f3SBrandon Wyman     psu2.analyze();
1363f1242f3SBrandon Wyman     psu2.analyze();
1373f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1383f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), true);
1393f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), false);
1403f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), false);
1413f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), true);
1423f1242f3SBrandon Wyman 
1433f1242f3SBrandon Wyman     // STATUS_WORD MFR fault.
1443f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
1453f1242f3SBrandon Wyman         .WillOnce(Return(0x0000))
14610d94055SJay Meyer         .WillOnce(Return(status_word::MFR_SPECIFIC_FAULT))
14710d94055SJay Meyer         .WillOnce(Return(1)); // mock return for read(STATUS_MFR... )
1483f1242f3SBrandon Wyman     psu2.analyze();
1493f1242f3SBrandon Wyman     psu2.analyze();
1503f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1513f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), true);
1523f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), false);
1533f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), true);
1543f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), false);
1553f1242f3SBrandon Wyman 
1563f1242f3SBrandon Wyman     // Ignore Temperature fault.
1573f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
1583f1242f3SBrandon Wyman         .WillOnce(Return(0x0000))
15910d94055SJay Meyer         .WillOnce(Return(status_word::TEMPERATURE_FAULT_WARN))
16010d94055SJay Meyer         .WillOnce(Return(0x0000));
1613f1242f3SBrandon Wyman     psu2.analyze();
1623f1242f3SBrandon Wyman     psu2.analyze();
1633f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1643f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), false);
1653f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), false);
1663f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), false);
1673f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), false);
1683f1242f3SBrandon Wyman 
1693f1242f3SBrandon Wyman     // Ignore fan fault
1703f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
1713f1242f3SBrandon Wyman         .WillOnce(Return(0x0000))
17210d94055SJay Meyer         .WillOnce(Return(status_word::FAN_FAULT))
17310d94055SJay Meyer         .WillOnce(Return(0x0000));
1743f1242f3SBrandon Wyman     psu2.analyze();
1753f1242f3SBrandon Wyman     psu2.analyze();
1763f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
1773f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isFaulted(), false);
1783f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasInputFault(), false);
1793f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasMFRFault(), false);
1803f1242f3SBrandon Wyman     EXPECT_EQ(psu2.hasVINUVFault(), false);
1813f1242f3SBrandon Wyman 
1823f1242f3SBrandon Wyman     // TODO: ReadFailure
1833f1242f3SBrandon Wyman }
1843f1242f3SBrandon Wyman 
18559a35793SBrandon Wyman TEST_F(PowerSupplyTests, OnOffConfig)
18659a35793SBrandon Wyman {
18759a35793SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
18859a35793SBrandon Wyman     uint8_t data = 0x15;
18959a35793SBrandon Wyman 
19059a35793SBrandon Wyman     // Test where PSU is NOT present
19159a35793SBrandon Wyman     try
19259a35793SBrandon Wyman     {
19359a35793SBrandon Wyman         EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
19459a35793SBrandon Wyman             .Times(1)
19559a35793SBrandon Wyman             .WillOnce(Return(false));
196510acaabSBrandon Wyman         PowerSupply psu{bus, PSUInventoryPath, 4, 0x69};
19759a35793SBrandon Wyman         MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
19859a35793SBrandon Wyman         // If it is not present, I should not be trying to write to it.
19959a35793SBrandon Wyman         EXPECT_CALL(mockPMBus, writeBinary(_, _, _)).Times(0);
20059a35793SBrandon Wyman         psu.onOffConfig(data);
20159a35793SBrandon Wyman     }
20259a35793SBrandon Wyman     catch (...)
20359a35793SBrandon Wyman     {
20459a35793SBrandon Wyman     }
20559a35793SBrandon Wyman 
20659a35793SBrandon Wyman     // Test where PSU is present
20759a35793SBrandon Wyman     try
20859a35793SBrandon Wyman     {
20959a35793SBrandon Wyman         EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
21059a35793SBrandon Wyman             .Times(1)
21159a35793SBrandon Wyman             .WillOnce(Return(true)); // present
212510acaabSBrandon Wyman         PowerSupply psu{bus, PSUInventoryPath, 5, 0x6a};
21359a35793SBrandon Wyman         MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
21459a35793SBrandon Wyman         // TODO: ???should I check the filename?
21559a35793SBrandon Wyman         EXPECT_CALL(mockPMBus,
21659a35793SBrandon Wyman                     writeBinary(_, ElementsAre(0x15), Type::HwmonDeviceDebug))
21759a35793SBrandon Wyman             .Times(1);
21859a35793SBrandon Wyman         psu.onOffConfig(data);
21959a35793SBrandon Wyman     }
22059a35793SBrandon Wyman     catch (...)
22159a35793SBrandon Wyman     {
22259a35793SBrandon Wyman     }
22359a35793SBrandon Wyman }
22459a35793SBrandon Wyman 
2253f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, ClearFaults)
2263f1242f3SBrandon Wyman {
2273f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
2283f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
2293f1242f3SBrandon Wyman         .Times(1)
2303f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
231510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 13, 0x68};
2323f1242f3SBrandon Wyman     EXPECT_EQ(psu.isPresent(), true);
2333f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), false);
2343f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
2353f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
2363f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
2373f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
23810d94055SJay Meyer     EXPECT_CALL(mockPMBus, read(_, _))
23910d94055SJay Meyer         .Times(2)
24010d94055SJay Meyer         .WillOnce(Return(0xFFFF))
24110d94055SJay Meyer         .WillOnce(Return(1)); // mock return for read(STATUS_MFR... )
2423f1242f3SBrandon Wyman     psu.analyze();
2433f1242f3SBrandon Wyman     EXPECT_EQ(psu.isPresent(), true);
2443f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), true);
2453f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), true);
2463f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), true);
2473f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), true);
2483c208464SBrandon Wyman     EXPECT_CALL(mockPMBus, read("in1_input", _))
2493c208464SBrandon Wyman         .Times(1)
2503c208464SBrandon Wyman         .WillOnce(Return(209000));
2513f1242f3SBrandon Wyman     psu.clearFaults();
2523f1242f3SBrandon Wyman     EXPECT_EQ(psu.isPresent(), true);
2533f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), false);
2543f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
2553f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
2563f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
2573f1242f3SBrandon Wyman }
2583f1242f3SBrandon Wyman 
2593f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, UpdateInventory)
2603f1242f3SBrandon Wyman {
2613f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
2621d7a7df8SBrandon Wyman 
2631d7a7df8SBrandon Wyman     try
2641d7a7df8SBrandon Wyman     {
2651d7a7df8SBrandon Wyman         EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
2661d7a7df8SBrandon Wyman             .Times(1)
2671d7a7df8SBrandon Wyman             .WillOnce(Return(false)); // missing
268510acaabSBrandon Wyman         PowerSupply psu{bus, PSUInventoryPath, 3, 0x68};
2691d7a7df8SBrandon Wyman         MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
2701d7a7df8SBrandon Wyman         // If it is not present, I should not be trying to read a string
2711d7a7df8SBrandon Wyman         EXPECT_CALL(mockPMBus, readString(_, _)).Times(0);
2721d7a7df8SBrandon Wyman         psu.updateInventory();
2731d7a7df8SBrandon Wyman     }
2741d7a7df8SBrandon Wyman     catch (...)
2751d7a7df8SBrandon Wyman     {
2761d7a7df8SBrandon Wyman         ADD_FAILURE() << "Should not have caught exception.";
2771d7a7df8SBrandon Wyman     }
2781d7a7df8SBrandon Wyman 
2791d7a7df8SBrandon Wyman     try
2801d7a7df8SBrandon Wyman     {
2813f1242f3SBrandon Wyman         EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath)))
2823f1242f3SBrandon Wyman             .Times(1)
2833f1242f3SBrandon Wyman             .WillOnce(Return(true)); // present
284510acaabSBrandon Wyman         PowerSupply psu{bus, PSUInventoryPath, 13, 0x69};
2851d7a7df8SBrandon Wyman         MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
2861d7a7df8SBrandon Wyman         EXPECT_CALL(mockPMBus, readString(_, _)).WillRepeatedly(Return(""));
2873f1242f3SBrandon Wyman         psu.updateInventory();
2881d7a7df8SBrandon Wyman 
289*3c530fbdSBrandon Wyman #if IBM_VPD
2901d7a7df8SBrandon Wyman         EXPECT_CALL(mockPMBus, readString(_, _))
2911d7a7df8SBrandon Wyman             .WillOnce(Return("CCIN"))
2921d7a7df8SBrandon Wyman             .WillOnce(Return("PN3456"))
2931d7a7df8SBrandon Wyman             .WillOnce(Return("FN3456"))
2941d7a7df8SBrandon Wyman             .WillOnce(Return("HEADER"))
2951d7a7df8SBrandon Wyman             .WillOnce(Return("SN3456"))
2961d7a7df8SBrandon Wyman             .WillOnce(Return("FW3456"));
297*3c530fbdSBrandon Wyman #endif
2981d7a7df8SBrandon Wyman         psu.updateInventory();
2991d7a7df8SBrandon Wyman         // TODO: D-Bus mocking to verify values stored on D-Bus (???)
3001d7a7df8SBrandon Wyman     }
3011d7a7df8SBrandon Wyman     catch (...)
3021d7a7df8SBrandon Wyman     {
3031d7a7df8SBrandon Wyman         ADD_FAILURE() << "Should not have caught exception.";
3041d7a7df8SBrandon Wyman     }
3053f1242f3SBrandon Wyman }
3063f1242f3SBrandon Wyman 
3073f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, IsPresent)
3083f1242f3SBrandon Wyman {
3093f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
3103f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, StrEq(PSUInventoryPath))).Times(1);
311510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 3, 0x68};
3123f1242f3SBrandon Wyman     EXPECT_EQ(psu.isPresent(), false);
3133f1242f3SBrandon Wyman 
3143f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, _))
3153f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
316510acaabSBrandon Wyman     PowerSupply psu2{bus, PSUInventoryPath, 10, 0x6b};
3173f1242f3SBrandon Wyman     EXPECT_EQ(psu2.isPresent(), true);
3183f1242f3SBrandon Wyman }
3193f1242f3SBrandon Wyman 
3203f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, IsFaulted)
3213f1242f3SBrandon Wyman {
3223f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
3233f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, _))
3243f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
325510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 11, 0x6f};
3263f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), false);
3273f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
32810d94055SJay Meyer     EXPECT_CALL(mockPMBus, read(_, _))
32910d94055SJay Meyer         .Times(2)
33010d94055SJay Meyer         .WillOnce(Return(0xFFFF))
33110d94055SJay Meyer         .WillOnce(Return(1)); // mock return for read(STATUS_MFR... )
3323f1242f3SBrandon Wyman     psu.analyze();
3333f1242f3SBrandon Wyman     EXPECT_EQ(psu.isFaulted(), true);
3343f1242f3SBrandon Wyman }
3353f1242f3SBrandon Wyman 
3363f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, HasInputFault)
3373f1242f3SBrandon Wyman {
3383f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
3393f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, _))
3403f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
341510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 3, 0x68};
3423f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
3433f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
3443f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3453f1242f3SBrandon Wyman     psu.analyze();
3463f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
3473f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
34810d94055SJay Meyer         .Times(2)
34910d94055SJay Meyer         .WillOnce(Return(status_word::INPUT_FAULT_WARN))
35010d94055SJay Meyer         .WillOnce(Return(0));
3513f1242f3SBrandon Wyman     psu.analyze();
3523f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), true);
3533f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3543f1242f3SBrandon Wyman     psu.analyze();
3553f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasInputFault(), false);
3563f1242f3SBrandon Wyman }
3573f1242f3SBrandon Wyman 
3583f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, HasMFRFault)
3593f1242f3SBrandon Wyman {
3603f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
3613f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, _))
3623f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
363510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 3, 0x68};
3643f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
3653f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
3663f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3673f1242f3SBrandon Wyman     psu.analyze();
3683f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
3693f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
37010d94055SJay Meyer         .Times(2)
37110d94055SJay Meyer         .WillOnce(Return(status_word::MFR_SPECIFIC_FAULT))
37210d94055SJay Meyer         .WillOnce(Return(1)); // mock return for read(STATUS_MFR... )
3733f1242f3SBrandon Wyman     psu.analyze();
3743f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), true);
3753f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3763f1242f3SBrandon Wyman     psu.analyze();
3773f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasMFRFault(), false);
3783f1242f3SBrandon Wyman }
3793f1242f3SBrandon Wyman 
3803f1242f3SBrandon Wyman TEST_F(PowerSupplyTests, HasVINUVFault)
3813f1242f3SBrandon Wyman {
3823f1242f3SBrandon Wyman     auto bus = sdbusplus::bus::new_default();
3833f1242f3SBrandon Wyman     EXPECT_CALL(mockedUtil, getPresence(_, _))
3843f1242f3SBrandon Wyman         .WillOnce(Return(true)); // present
385510acaabSBrandon Wyman     PowerSupply psu{bus, PSUInventoryPath, 3, 0x68};
3863f1242f3SBrandon Wyman     MockedPMBus& mockPMBus = static_cast<MockedPMBus&>(psu.getPMBus());
3873f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
3883f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3893f1242f3SBrandon Wyman     psu.analyze();
3903f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
3913f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _))
39210d94055SJay Meyer         .Times(2)
39310d94055SJay Meyer         .WillOnce(Return(status_word::VIN_UV_FAULT))
39410d94055SJay Meyer         .WillOnce(Return(0));
3953f1242f3SBrandon Wyman     psu.analyze();
3963f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), true);
3973f1242f3SBrandon Wyman     EXPECT_CALL(mockPMBus, read(_, _)).Times(1).WillOnce(Return(0x0000));
3983f1242f3SBrandon Wyman     psu.analyze();
3993f1242f3SBrandon Wyman     EXPECT_EQ(psu.hasVINUVFault(), false);
4003f1242f3SBrandon Wyman }
401