1*a34a64bbSThu Nguyen #include "common/test/mocked_utils.hpp"
2*a34a64bbSThu Nguyen #include "common/utils.hpp"
3*a34a64bbSThu Nguyen #include "host-bmc/dbus_to_terminus_effecters.hpp"
4*a34a64bbSThu Nguyen
5*a34a64bbSThu Nguyen #include <nlohmann/json.hpp>
6*a34a64bbSThu Nguyen
7*a34a64bbSThu Nguyen #include <gtest/gtest.h>
8*a34a64bbSThu Nguyen
9*a34a64bbSThu Nguyen using namespace pldm::host_effecters;
10*a34a64bbSThu Nguyen using namespace pldm::utils;
11*a34a64bbSThu Nguyen
12*a34a64bbSThu Nguyen class MockHostEffecterParser : public HostEffecterParser
13*a34a64bbSThu Nguyen {
14*a34a64bbSThu Nguyen public:
MockHostEffecterParser(int fd,const pldm_pdr * repo,DBusHandler * const dbusHandler,const std::string & jsonPath)15*a34a64bbSThu Nguyen MockHostEffecterParser(int fd, const pldm_pdr* repo,
16*a34a64bbSThu Nguyen DBusHandler* const dbusHandler,
17*a34a64bbSThu Nguyen const std::string& jsonPath) :
18*a34a64bbSThu Nguyen HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr)
19*a34a64bbSThu Nguyen {}
20*a34a64bbSThu Nguyen
21*a34a64bbSThu Nguyen MOCK_METHOD(int, setHostStateEffecter,
22*a34a64bbSThu Nguyen (size_t, std::vector<set_effecter_state_field>&, uint16_t),
23*a34a64bbSThu Nguyen (override));
24*a34a64bbSThu Nguyen
25*a34a64bbSThu Nguyen MOCK_METHOD(void, createHostEffecterMatch,
26*a34a64bbSThu Nguyen (const std::string&, const std::string&, size_t, size_t,
27*a34a64bbSThu Nguyen uint16_t),
28*a34a64bbSThu Nguyen (override));
29*a34a64bbSThu Nguyen
gethostEffecterInfo()30*a34a64bbSThu Nguyen const std::vector<EffecterInfo>& gethostEffecterInfo()
31*a34a64bbSThu Nguyen {
32*a34a64bbSThu Nguyen return hostEffecterInfo;
33*a34a64bbSThu Nguyen }
34*a34a64bbSThu Nguyen };
35*a34a64bbSThu Nguyen
TEST(HostEffecterParser,parseEffecterJsonGoodPath)36*a34a64bbSThu Nguyen TEST(HostEffecterParser, parseEffecterJsonGoodPath)
37*a34a64bbSThu Nguyen {
38*a34a64bbSThu Nguyen MockdBusHandler dbusHandler;
39*a34a64bbSThu Nguyen int sockfd{};
40*a34a64bbSThu Nguyen MockHostEffecterParser hostEffecterParserGood(sockfd, nullptr, &dbusHandler,
41*a34a64bbSThu Nguyen "./host_effecter_jsons/good");
42*a34a64bbSThu Nguyen auto hostEffecterInfo = hostEffecterParserGood.gethostEffecterInfo();
43*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo.size(), 2);
44*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[0].effecterPdrType, PLDM_STATE_EFFECTER_PDR);
45*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[0].entityInstance, 0);
46*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[0].entityType, 33);
47*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[0].dbusInfo.size(), 1);
48*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[0].checkHostState, true);
49*a34a64bbSThu Nguyen DBusEffecterMapping dbusInfo{
50*a34a64bbSThu Nguyen {"/xyz/openbmc_project/control/host0/boot",
51*a34a64bbSThu Nguyen "xyz.openbmc_project.Control.Boot.Mode", "BootMode", "string"},
52*a34a64bbSThu Nguyen {"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"},
53*a34a64bbSThu Nguyen {196, {2}}};
54*a34a64bbSThu Nguyen auto& temp = hostEffecterInfo[0].dbusInfo[0];
55*a34a64bbSThu Nguyen ASSERT_EQ(temp.dbusMap.objectPath == dbusInfo.dbusMap.objectPath, true);
56*a34a64bbSThu Nguyen ASSERT_EQ(temp.dbusMap.interface == dbusInfo.dbusMap.interface, true);
57*a34a64bbSThu Nguyen ASSERT_EQ(temp.dbusMap.propertyName == dbusInfo.dbusMap.propertyName, true);
58*a34a64bbSThu Nguyen ASSERT_EQ(temp.dbusMap.propertyType == dbusInfo.dbusMap.propertyType, true);
59*a34a64bbSThu Nguyen
60*a34a64bbSThu Nguyen /* Check Numeric Effecter in Good Json file */
61*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].effecterPdrType, PLDM_NUMERIC_EFFECTER_PDR);
62*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].entityType, 32903);
63*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].entityInstance, 6);
64*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].containerId, 4);
65*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].dbusNumericEffecterInfo.size(), 1);
66*a34a64bbSThu Nguyen ASSERT_EQ(hostEffecterInfo[1].checkHostState, false);
67*a34a64bbSThu Nguyen DBusNumericEffecterMapping dbusInfoNumeric{
68*a34a64bbSThu Nguyen {"/xyz/openbmc_project/effecters/power/PLimit",
69*a34a64bbSThu Nguyen "xyz.openbmc_project.Effecter.Value", "Value", "double"},
70*a34a64bbSThu Nguyen 5,
71*a34a64bbSThu Nguyen 1,
72*a34a64bbSThu Nguyen 0,
73*a34a64bbSThu Nguyen -3,
74*a34a64bbSThu Nguyen 100};
75*a34a64bbSThu Nguyen auto& tempNumeric = hostEffecterInfo[1].dbusNumericEffecterInfo[0];
76*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.dbusMap.objectPath ==
77*a34a64bbSThu Nguyen dbusInfoNumeric.dbusMap.objectPath,
78*a34a64bbSThu Nguyen true);
79*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.dbusMap.interface ==
80*a34a64bbSThu Nguyen dbusInfoNumeric.dbusMap.interface,
81*a34a64bbSThu Nguyen true);
82*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.dbusMap.propertyName ==
83*a34a64bbSThu Nguyen dbusInfoNumeric.dbusMap.propertyName,
84*a34a64bbSThu Nguyen true);
85*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.dbusMap.propertyType ==
86*a34a64bbSThu Nguyen dbusInfoNumeric.dbusMap.propertyType,
87*a34a64bbSThu Nguyen true);
88*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.dataSize == dbusInfoNumeric.dataSize, true);
89*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.resolution == dbusInfoNumeric.resolution, true);
90*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.offset == dbusInfoNumeric.offset, true);
91*a34a64bbSThu Nguyen ASSERT_EQ(tempNumeric.unitModifier == dbusInfoNumeric.unitModifier, true);
92*a34a64bbSThu Nguyen }
93*a34a64bbSThu Nguyen
TEST(HostEffecterParser,parseEffecterJsonBadPath)94*a34a64bbSThu Nguyen TEST(HostEffecterParser, parseEffecterJsonBadPath)
95*a34a64bbSThu Nguyen {
96*a34a64bbSThu Nguyen MockdBusHandler dbusHandler;
97*a34a64bbSThu Nguyen int sockfd{};
98*a34a64bbSThu Nguyen MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
99*a34a64bbSThu Nguyen "./host_effecter_jsons/no_json");
100*a34a64bbSThu Nguyen ASSERT_THROW(
101*a34a64bbSThu Nguyen hostEffecterParser.parseEffecterJson("./host_effecter_jsons/no_json"),
102*a34a64bbSThu Nguyen std::exception);
103*a34a64bbSThu Nguyen ASSERT_THROW(
104*a34a64bbSThu Nguyen hostEffecterParser.parseEffecterJson("./host_effecter_jsons/malformed"),
105*a34a64bbSThu Nguyen std::exception);
106*a34a64bbSThu Nguyen }
107*a34a64bbSThu Nguyen
TEST(HostEffecterParser,findNewStateValue)108*a34a64bbSThu Nguyen TEST(HostEffecterParser, findNewStateValue)
109*a34a64bbSThu Nguyen {
110*a34a64bbSThu Nguyen MockdBusHandler dbusHandler;
111*a34a64bbSThu Nguyen int sockfd{};
112*a34a64bbSThu Nguyen MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
113*a34a64bbSThu Nguyen "./host_effecter_jsons/good");
114*a34a64bbSThu Nguyen
115*a34a64bbSThu Nguyen PropertyValue val1{std::in_place_type<std::string>,
116*a34a64bbSThu Nguyen "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"};
117*a34a64bbSThu Nguyen PropertyValue val2{std::in_place_type<std::string>,
118*a34a64bbSThu Nguyen "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup"};
119*a34a64bbSThu Nguyen auto newState = hostEffecterParser.findNewStateValue(0, 0, val1);
120*a34a64bbSThu Nguyen ASSERT_EQ(newState, 2);
121*a34a64bbSThu Nguyen
122*a34a64bbSThu Nguyen ASSERT_THROW(hostEffecterParser.findNewStateValue(0, 0, val2),
123*a34a64bbSThu Nguyen std::exception);
124*a34a64bbSThu Nguyen }
125*a34a64bbSThu Nguyen
TEST(HostEffecterParser,adjustValue)126*a34a64bbSThu Nguyen TEST(HostEffecterParser, adjustValue)
127*a34a64bbSThu Nguyen {
128*a34a64bbSThu Nguyen MockdBusHandler dbusHandler;
129*a34a64bbSThu Nguyen int sockfd{};
130*a34a64bbSThu Nguyen MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler,
131*a34a64bbSThu Nguyen "./host_effecter_jsons/good");
132*a34a64bbSThu Nguyen
133*a34a64bbSThu Nguyen auto realVal = hostEffecterParser.adjustValue(200, -50, 0.5, -2);
134*a34a64bbSThu Nguyen ASSERT_EQ(realVal, 12500);
135*a34a64bbSThu Nguyen realVal = hostEffecterParser.adjustValue(0, -50, 1, 0);
136*a34a64bbSThu Nguyen ASSERT_EQ(realVal, 50);
137*a34a64bbSThu Nguyen realVal = hostEffecterParser.adjustValue(0, 100, 1, -1);
138*a34a64bbSThu Nguyen ASSERT_EQ(realVal, -1000);
139*a34a64bbSThu Nguyen realVal = hostEffecterParser.adjustValue(2.34, 0, 1, -1);
140*a34a64bbSThu Nguyen ASSERT_EQ(realVal, 23);
141*a34a64bbSThu Nguyen realVal = hostEffecterParser.adjustValue(2.35, 0, 1, -1);
142*a34a64bbSThu Nguyen ASSERT_EQ(realVal, 24);
143*a34a64bbSThu Nguyen }
144