Tom Joseph | 5327988 | 2021-04-28 06:29:13 -0700 | [diff] [blame] | 1 | #include "common/test/mocked_utils.hpp" |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 2 | #include "common/utils.hpp" |
Deepak Kodihalli | ac19bd6 | 2020-06-16 08:25:23 -0500 | [diff] [blame] | 3 | #include "host-bmc/dbus_to_host_effecters.hpp" |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 4 | |
| 5 | #include <nlohmann/json.hpp> |
| 6 | |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | using namespace pldm::host_effecters; |
| 10 | using namespace pldm::utils; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 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) : |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 18 | HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 19 | {} |
| 20 | |
Pavithra Barithaya | 2af90d3 | 2023-07-10 04:47:32 -0500 | [diff] [blame] | 21 | MOCK_METHOD(int, setHostStateEffecter, |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 22 | (size_t, std::vector<set_effecter_state_field>&, uint16_t), |
Pavithra Barithaya | 2af90d3 | 2023-07-10 04:47:32 -0500 | [diff] [blame] | 23 | (override)); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 24 | |
| 25 | MOCK_METHOD(void, createHostEffecterMatch, |
| 26 | (const std::string&, const std::string&, size_t, size_t, |
| 27 | uint16_t), |
Andrew Jeffery | c3d27e0 | 2023-04-28 12:44:29 +0930 | [diff] [blame] | 28 | (override)); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 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(), 1); |
| 44 | ASSERT_EQ(hostEffecterInfo[0].entityInstance, 0); |
| 45 | ASSERT_EQ(hostEffecterInfo[0].entityType, 33); |
| 46 | ASSERT_EQ(hostEffecterInfo[0].dbusInfo.size(), 1); |
| 47 | DBusEffecterMapping dbusInfo{ |
| 48 | {"/xyz/openbmc_project/control/host0/boot", |
| 49 | "xyz.openbmc_project.Control.Boot.Mode", "BootMode", "string"}, |
| 50 | {"xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"}, |
| 51 | {196, {2}}}; |
| 52 | auto& temp = hostEffecterInfo[0].dbusInfo[0]; |
| 53 | ASSERT_EQ(temp.dbusMap.objectPath == dbusInfo.dbusMap.objectPath, true); |
| 54 | ASSERT_EQ(temp.dbusMap.interface == dbusInfo.dbusMap.interface, true); |
| 55 | ASSERT_EQ(temp.dbusMap.propertyName == dbusInfo.dbusMap.propertyName, true); |
| 56 | ASSERT_EQ(temp.dbusMap.propertyType == dbusInfo.dbusMap.propertyType, true); |
| 57 | } |
| 58 | |
| 59 | TEST(HostEffecterParser, parseEffecterJsonBadPath) |
| 60 | { |
| 61 | MockdBusHandler dbusHandler; |
| 62 | int sockfd{}; |
| 63 | MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler, |
| 64 | "./host_effecter_jsons/no_json"); |
| 65 | ASSERT_THROW( |
| 66 | hostEffecterParser.parseEffecterJson("./host_effecter_jsons/no_json"), |
| 67 | std::exception); |
| 68 | ASSERT_THROW( |
| 69 | hostEffecterParser.parseEffecterJson("./host_effecter_jsons/malformed"), |
| 70 | std::exception); |
| 71 | } |
| 72 | |
| 73 | TEST(HostEffecterParser, findNewStateValue) |
| 74 | { |
| 75 | MockdBusHandler dbusHandler; |
| 76 | int sockfd{}; |
| 77 | MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler, |
| 78 | "./host_effecter_jsons/good"); |
| 79 | |
| 80 | PropertyValue val1{std::in_place_type<std::string>, |
| 81 | "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"}; |
| 82 | PropertyValue val2{std::in_place_type<std::string>, |
| 83 | "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup"}; |
| 84 | auto newState = hostEffecterParser.findNewStateValue(0, 0, val1); |
| 85 | ASSERT_EQ(newState, 2); |
| 86 | |
| 87 | ASSERT_THROW(hostEffecterParser.findNewStateValue(0, 0, val2), |
| 88 | std::exception); |
| 89 | } |