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