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 | |
| 7 | #include <iostream> |
| 8 | |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using namespace pldm::host_effecters; |
| 12 | using namespace pldm::utils; |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 13 | |
| 14 | class MockHostEffecterParser : public HostEffecterParser |
| 15 | { |
| 16 | public: |
| 17 | MockHostEffecterParser(int fd, const pldm_pdr* repo, |
| 18 | DBusHandler* const dbusHandler, |
| 19 | const std::string& jsonPath) : |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 20 | HostEffecterParser(nullptr, fd, repo, dbusHandler, jsonPath, nullptr) |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 21 | {} |
| 22 | |
Pavithra Barithaya | 2af90d3 | 2023-07-10 04:47:32 -0500 | [diff] [blame^] | 23 | MOCK_METHOD(int, setHostStateEffecter, |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 24 | (size_t, std::vector<set_effecter_state_field>&, uint16_t), |
Pavithra Barithaya | 2af90d3 | 2023-07-10 04:47:32 -0500 | [diff] [blame^] | 25 | (override)); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 26 | |
| 27 | MOCK_METHOD(void, createHostEffecterMatch, |
| 28 | (const std::string&, const std::string&, size_t, size_t, |
| 29 | uint16_t), |
Andrew Jeffery | c3d27e0 | 2023-04-28 12:44:29 +0930 | [diff] [blame] | 30 | (override)); |
Tom Joseph | 250c475 | 2020-04-15 10:32:45 +0530 | [diff] [blame] | 31 | |
| 32 | const std::vector<EffecterInfo>& gethostEffecterInfo() |
| 33 | { |
| 34 | return hostEffecterInfo; |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | TEST(HostEffecterParser, parseEffecterJsonGoodPath) |
| 39 | { |
| 40 | MockdBusHandler dbusHandler; |
| 41 | int sockfd{}; |
| 42 | MockHostEffecterParser hostEffecterParserGood(sockfd, nullptr, &dbusHandler, |
| 43 | "./host_effecter_jsons/good"); |
| 44 | auto hostEffecterInfo = hostEffecterParserGood.gethostEffecterInfo(); |
| 45 | ASSERT_EQ(hostEffecterInfo.size(), 1); |
| 46 | ASSERT_EQ(hostEffecterInfo[0].entityInstance, 0); |
| 47 | ASSERT_EQ(hostEffecterInfo[0].entityType, 33); |
| 48 | ASSERT_EQ(hostEffecterInfo[0].dbusInfo.size(), 1); |
| 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 | |
| 61 | TEST(HostEffecterParser, parseEffecterJsonBadPath) |
| 62 | { |
| 63 | MockdBusHandler dbusHandler; |
| 64 | int sockfd{}; |
| 65 | MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler, |
| 66 | "./host_effecter_jsons/no_json"); |
| 67 | ASSERT_THROW( |
| 68 | hostEffecterParser.parseEffecterJson("./host_effecter_jsons/no_json"), |
| 69 | std::exception); |
| 70 | ASSERT_THROW( |
| 71 | hostEffecterParser.parseEffecterJson("./host_effecter_jsons/malformed"), |
| 72 | std::exception); |
| 73 | } |
| 74 | |
| 75 | TEST(HostEffecterParser, findNewStateValue) |
| 76 | { |
| 77 | MockdBusHandler dbusHandler; |
| 78 | int sockfd{}; |
| 79 | MockHostEffecterParser hostEffecterParser(sockfd, nullptr, &dbusHandler, |
| 80 | "./host_effecter_jsons/good"); |
| 81 | |
| 82 | PropertyValue val1{std::in_place_type<std::string>, |
| 83 | "xyz.openbmc_project.Control.Boot.Mode.Modes.Regular"}; |
| 84 | PropertyValue val2{std::in_place_type<std::string>, |
| 85 | "xyz.openbmc_project.Control.Boot.Mode.Modes.Setup"}; |
| 86 | auto newState = hostEffecterParser.findNewStateValue(0, 0, val1); |
| 87 | ASSERT_EQ(newState, 2); |
| 88 | |
| 89 | ASSERT_THROW(hostEffecterParser.findNewStateValue(0, 0, val2), |
| 90 | std::exception); |
| 91 | } |