Unive Tien | 852160b | 2024-12-06 15:58:21 +0800 | [diff] [blame^] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "common/test/mocked_utils.hpp" |
| 4 | #include "common/types.hpp" |
| 5 | #include "common/utils.hpp" |
| 6 | #include "requester/configuration_discovery_handler.hpp" |
| 7 | |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using ::testing::_; |
| 12 | |
| 13 | TEST(ConfigurationDiscoveryHandlerTest, succesfullyDiscoverConfig) |
| 14 | { |
| 15 | constexpr uint8_t EID = 10; |
| 16 | constexpr auto mockedDbusPath = |
| 17 | "/xyz/openbmc_project/inventory/system/board/Mocked_Board_Slot_1/MockedDevice"; |
| 18 | constexpr auto mockedService = "xyz.openbmc_project.EntityManager"; |
| 19 | constexpr auto mockedInterface = |
| 20 | "xyz.openbmc_project.Configuration.MCTPEndpoint"; |
| 21 | MockdBusHandler mockedUtils; |
| 22 | auto handler = |
| 23 | std::make_unique<pldm::ConfigurationDiscoveryHandler>(&mockedUtils); |
| 24 | |
| 25 | pldm::utils::GetSubTreeResponse mockedGetSubtreeResponse{std::make_pair( |
| 26 | mockedDbusPath, |
| 27 | pldm::utils::MapperServiceMap{std::make_pair( |
| 28 | mockedService, pldm::utils::Interfaces{mockedInterface})})}; |
| 29 | |
| 30 | EXPECT_CALL(mockedUtils, getSubtree(_, _, _)) |
| 31 | .Times(1) |
| 32 | .WillOnce(testing::Return(mockedGetSubtreeResponse)); |
| 33 | |
| 34 | pldm::utils::PropertyMap mockGetAllResponse{ |
| 35 | {"Address", std::vector<uint64_t>{0x1}}, |
| 36 | {"Bus", uint64_t(0)}, |
| 37 | {"EndpointId", uint64_t(EID)}, |
| 38 | {"Name", std::string("MockedDevice")}}; |
| 39 | |
| 40 | EXPECT_CALL(mockedUtils, |
| 41 | getAll(mockedService, mockedDbusPath, mockedInterface)) |
| 42 | .Times(1) |
| 43 | .WillOnce(testing::Return(mockGetAllResponse)); |
| 44 | |
| 45 | pldm::MctpInfos mctpInfos; |
| 46 | mctpInfos.emplace_back(pldm::MctpInfo(EID, "emptyUUID", "", 1)); |
| 47 | |
| 48 | // Act |
| 49 | handler->handleMctpEndpoints(mctpInfos); |
| 50 | |
| 51 | ASSERT_EQ(1, handler->getConfigurations().size()); |
| 52 | } |
| 53 | |
| 54 | TEST(ConfigurationDiscoveryHandlerTest, BlockedByNoSuchElement) |
| 55 | { |
| 56 | constexpr uint8_t EID = 10; |
| 57 | constexpr auto mockedDbusPath = |
| 58 | "/xyz/openbmc_project/inventory/system/board/Mocked_Board_Slot_1/MockedDevice"; |
| 59 | constexpr auto mockedService = "xyz.openbmc_project.EntityManager"; |
| 60 | constexpr auto mockedInterface = |
| 61 | "xyz.openbmc_project.Configuration.MCTPEndpoint"; |
| 62 | MockdBusHandler mockedUtils; |
| 63 | auto handler = |
| 64 | std::make_unique<pldm::ConfigurationDiscoveryHandler>(&mockedUtils); |
| 65 | |
| 66 | pldm::utils::GetSubTreeResponse mockedGetSubtreeResponse{std::make_pair( |
| 67 | mockedDbusPath, |
| 68 | pldm::utils::MapperServiceMap{std::make_pair( |
| 69 | mockedService, pldm::utils::Interfaces{mockedInterface})})}; |
| 70 | |
| 71 | EXPECT_CALL(mockedUtils, getSubtree(_, _, _)) |
| 72 | .Times(1) |
| 73 | .WillOnce(testing::Return(mockedGetSubtreeResponse)); |
| 74 | |
| 75 | pldm::utils::PropertyMap mockGetAllResponse{ |
| 76 | {"Address", uint64_t(0x1)}, |
| 77 | {"Bus", uint64_t(0)}, |
| 78 | /* No EndpointId */ |
| 79 | {"Name", std::string("MockedDevice")}}; |
| 80 | |
| 81 | EXPECT_CALL(mockedUtils, |
| 82 | getAll(mockedService, mockedDbusPath, mockedInterface)) |
| 83 | .Times(1) |
| 84 | .WillOnce(testing::Return(mockGetAllResponse)); |
| 85 | |
| 86 | pldm::MctpInfos mctpInfos; |
| 87 | mctpInfos.emplace_back(pldm::MctpInfo(EID, "emptyUUID", "", 1)); |
| 88 | |
| 89 | // Act |
| 90 | EXPECT_NO_THROW(handler->handleMctpEndpoints(mctpInfos)); |
| 91 | |
| 92 | ASSERT_EQ(0, handler->getConfigurations().size()); |
| 93 | } |
| 94 | |
| 95 | TEST(ConfigurationDiscoveryHandlerTest, succesfullyRemoveConfig) |
| 96 | { |
| 97 | constexpr uint8_t EID = 10; |
| 98 | |
| 99 | MockdBusHandler mockedUtils; |
| 100 | auto handler = |
| 101 | std::make_unique<pldm::ConfigurationDiscoveryHandler>(&mockedUtils); |
| 102 | |
| 103 | pldm::MctpEndpoint mockedMctpEndpoint = { |
| 104 | uint64_t(0x1), uint64_t(EID), uint64_t(0), "MockedDevice", |
| 105 | std::nullopt}; |
| 106 | handler->getConfigurations().emplace( |
| 107 | "/xyz/openbmc_project/inventory/system/board/Mocked_Board_Slot_1/MockedDevice", |
| 108 | mockedMctpEndpoint); |
| 109 | |
| 110 | pldm::MctpInfos mctpInfos; |
| 111 | mctpInfos.emplace_back(pldm::MctpInfo(EID, "emptyUUID", "", 1)); |
| 112 | handler->handleRemovedMctpEndpoints(mctpInfos); |
| 113 | |
| 114 | ASSERT_EQ(0, handler->getConfigurations().size()); |
| 115 | } |