Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 3 | #include "common/test/mocked_utils.hpp" |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 4 | #include "common/types.hpp" |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 5 | #include "common/utils.hpp" |
| 6 | #include "requester/test/mock_mctp_discovery_handler_intf.hpp" |
| 7 | |
| 8 | #include <gmock/gmock.h> |
| 9 | #include <gtest/gtest.h> |
| 10 | |
| 11 | using ::testing::_; |
| 12 | |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 13 | class TestMctpDiscovery : public ::testing::Test |
| 14 | { |
| 15 | public: |
| 16 | static const pldm::Configurations& getConfigurations( |
| 17 | const pldm::MctpDiscovery& mctpDiscovery) |
| 18 | { |
| 19 | return mctpDiscovery.configurations; |
| 20 | } |
| 21 | static void searchConfigurationFor(pldm::MctpDiscovery& mctpDiscovery, |
| 22 | pldm::utils::DBusHandler& handler, |
| 23 | pldm::MctpInfo& mctpInfo) |
| 24 | { |
| 25 | mctpDiscovery.searchConfigurationFor(handler, mctpInfo); |
| 26 | } |
| 27 | }; |
| 28 | |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 29 | TEST(MctpEndpointDiscoveryTest, SingleHandleMctpEndpoint) |
| 30 | { |
| 31 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 32 | pldm::MockManager manager; |
| 33 | |
| 34 | EXPECT_CALL(manager, handleMctpEndpoints(_)).Times(1); |
| 35 | |
| 36 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 37 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 38 | mctpDiscoveryHandler = nullptr; |
| 39 | } |
| 40 | |
| 41 | TEST(MctpEndpointDiscoveryTest, MultipleHandleMctpEndpoints) |
| 42 | { |
| 43 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 44 | pldm::MockManager manager1; |
| 45 | pldm::MockManager manager2; |
| 46 | |
| 47 | EXPECT_CALL(manager1, handleMctpEndpoints(_)).Times(1); |
| 48 | EXPECT_CALL(manager2, handleMctpEndpoints(_)).Times(1); |
| 49 | |
| 50 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 51 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{ |
| 52 | &manager1, &manager2}); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 53 | mctpDiscoveryHandler = nullptr; |
| 54 | } |
| 55 | |
| 56 | TEST(MctpEndpointDiscoveryTest, goodGetMctpInfos) |
| 57 | { |
| 58 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 59 | pldm::MockManager manager; |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 60 | std::map<pldm::MctpInfo, pldm::Availability> currentMctpInfoMap; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 61 | |
| 62 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 63 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 64 | mctpDiscoveryHandler->getMctpInfos(currentMctpInfoMap); |
| 65 | EXPECT_EQ(currentMctpInfoMap.size(), 0); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | TEST(MctpEndpointDiscoveryTest, goodAddToExistingMctpInfos) |
| 69 | { |
| 70 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 71 | pldm::MockManager manager; |
| 72 | const pldm::MctpInfos& mctpInfos = { |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 73 | pldm::MctpInfo(11, pldm::emptyUUID, "", 1, std::nullopt), |
| 74 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)}; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 75 | |
| 76 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 77 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 78 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 79 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 80 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 81 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 82 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 83 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 84 | } |
| 85 | |
| 86 | TEST(MctpEndpointDiscoveryTest, badAddToExistingMctpInfos) |
| 87 | { |
| 88 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 89 | pldm::MockManager manager; |
| 90 | const pldm::MctpInfos& mctpInfos = { |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 91 | pldm::MctpInfo(11, pldm::emptyUUID, "", 1, std::nullopt)}; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 92 | |
| 93 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 94 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 95 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 96 | EXPECT_NE(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 97 | } |
| 98 | |
| 99 | TEST(MctpEndpointDiscoveryTest, goodRemoveFromExistingMctpInfos) |
| 100 | { |
| 101 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 102 | pldm::MockManager manager; |
| 103 | const pldm::MctpInfos& mctpInfos = { |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 104 | pldm::MctpInfo(11, pldm::emptyUUID, "def", 2, std::nullopt), |
| 105 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)}; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 106 | |
| 107 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 108 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 109 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 110 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 111 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 112 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 113 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 114 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 115 | pldm::MctpInfos removedInfos; |
| 116 | pldm::MctpInfos remainMctpInfos; |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 117 | remainMctpInfos.emplace_back( |
| 118 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 119 | |
| 120 | mctpDiscoveryHandler->removeFromExistingMctpInfos(remainMctpInfos, |
| 121 | removedInfos); |
| 122 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 1); |
| 123 | mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 124 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 125 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 126 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 127 | EXPECT_EQ(removedInfos.size(), 1); |
| 128 | mctpInfo = removedInfos.back(); |
| 129 | EXPECT_EQ(std::get<0>(mctpInfo), 11); |
| 130 | EXPECT_EQ(std::get<2>(mctpInfo), "def"); |
| 131 | EXPECT_EQ(std::get<3>(mctpInfo), 2); |
| 132 | } |
| 133 | |
| 134 | TEST(MctpEndpointDiscoveryTest, goodRemoveEndpoints) |
| 135 | { |
| 136 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 137 | pldm::MockManager manager; |
| 138 | const pldm::MctpInfos& mctpInfos = { |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 139 | pldm::MctpInfo(11, pldm::emptyUUID, "def", 2, std::nullopt), |
| 140 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)}; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 141 | |
| 142 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 143 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 144 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 145 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 146 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 147 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 148 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 149 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 150 | sdbusplus::message_t msg = sdbusplus::bus::new_default().new_method_call( |
| 151 | "xyz.openbmc_project.sdbusplus.test.Object", |
| 152 | "/xyz/openbmc_project/sdbusplus/test/object", |
| 153 | "xyz.openbmc_project.sdbusplus.test.Object", "Unused"); |
| 154 | mctpDiscoveryHandler->removeEndpoints(msg); |
| 155 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 0); |
| 156 | } |
Unive Tien | c40d4a6 | 2025-03-12 11:36:07 +0800 | [diff] [blame^] | 157 | |
| 158 | TEST(MctpEndpointDiscoveryTest, goodSearchConfigurationFor) |
| 159 | { |
| 160 | MockdBusHandler mockedDbusHandler; |
| 161 | auto& bus = mockedDbusHandler.getBus(); |
| 162 | pldm::MockManager manager; |
| 163 | const pldm::MctpInfos& mctpInfos = { |
| 164 | pldm::MctpInfo(10, pldm::emptyUUID, "abc", 1, std::nullopt)}; |
| 165 | |
| 166 | constexpr auto mockedDbusPath = |
| 167 | "/xyz/openbmc_project/inventory/system/board/Mocked_Board_Slot_1/MockedDevice"; |
| 168 | constexpr auto mockedService = "xyz.openbmc_project.EntityManager"; |
| 169 | std::vector<std::string> mockedInterfaces{ |
| 170 | "xyz.openbmc_project.Configuration.MCTPI2CTarget", |
| 171 | "xyz.openbmc_project.Configuration.MCTPI3CTarget"}; |
| 172 | |
| 173 | pldm::utils::GetAssociatedSubTreeResponse |
| 174 | mockedGetAssociatedSubTreeResponse{ |
| 175 | {mockedDbusPath, {{mockedService, mockedInterfaces}}}}; |
| 176 | |
| 177 | EXPECT_CALL(mockedDbusHandler, getAssociatedSubTree(_, _, _, _)) |
| 178 | .WillOnce(testing::Return(mockedGetAssociatedSubTreeResponse)); |
| 179 | |
| 180 | pldm::utils::PropertyMap mockGetI2CTargetPropertiesResponse{ |
| 181 | {"Address", uint64_t(0x1)}, |
| 182 | {"Bus", uint64_t(0)}, |
| 183 | {"Name", std::string("MockedDevice")}}; |
| 184 | |
| 185 | EXPECT_CALL(mockedDbusHandler, getDbusPropertiesVariant(_, _, _)) |
| 186 | .WillOnce(testing::Return(mockGetI2CTargetPropertiesResponse)); |
| 187 | |
| 188 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 189 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 190 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 191 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 1); |
| 192 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 193 | EXPECT_EQ(std::get<0>(mctpInfo), 10); |
| 194 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 195 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 196 | TestMctpDiscovery::searchConfigurationFor(*mctpDiscoveryHandler, |
| 197 | mockedDbusHandler, mctpInfo); |
| 198 | EXPECT_EQ(std::get<4>(mctpInfo), |
| 199 | std::optional<std::string>("MockedDevice")); |
| 200 | auto configuration = |
| 201 | TestMctpDiscovery::getConfigurations(*mctpDiscoveryHandler); |
| 202 | EXPECT_EQ(configuration.size(), 1); |
| 203 | } |
| 204 | |
| 205 | TEST(MctpEndpointDiscoveryTest, badSearchConfigurationFor) |
| 206 | { |
| 207 | MockdBusHandler mockedDbusHandler; |
| 208 | auto& bus = mockedDbusHandler.getBus(); |
| 209 | pldm::MockManager manager; |
| 210 | const pldm::MctpInfos& mctpInfos = { |
| 211 | pldm::MctpInfo(10, pldm::emptyUUID, "abc", 1, std::nullopt)}; |
| 212 | |
| 213 | constexpr auto mockedDbusPath = |
| 214 | "/xyz/openbmc_project/inventory/system/board/Mocked_Board_Slot_1/MockedDevice"; |
| 215 | constexpr auto mockedService = "xyz.openbmc_project.EntityManager"; |
| 216 | std::vector<std::string> mockedInterfaces{ |
| 217 | "xyz.openbmc_project.Configuration.MCTPPCIETarget", |
| 218 | "xyz.openbmc_project.Configuration.MCTPUSBTarget"}; |
| 219 | |
| 220 | pldm::utils::GetAssociatedSubTreeResponse |
| 221 | mockedGetAssociatedSubTreeResponse{ |
| 222 | {mockedDbusPath, {{mockedService, mockedInterfaces}}}}; |
| 223 | |
| 224 | EXPECT_CALL(mockedDbusHandler, getAssociatedSubTree(_, _, _, _)) |
| 225 | .WillOnce(testing::Return(mockedGetAssociatedSubTreeResponse)); |
| 226 | |
| 227 | pldm::utils::PropertyMap mockGetI2CTargetPropertiesResponse{ |
| 228 | {"Address", uint64_t(0x1)}, {"Bus", uint64_t(0)}}; |
| 229 | |
| 230 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 231 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 232 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 233 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 1); |
| 234 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 235 | EXPECT_EQ(std::get<0>(mctpInfo), 10); |
| 236 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 237 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 238 | TestMctpDiscovery::searchConfigurationFor(*mctpDiscoveryHandler, |
| 239 | mockedDbusHandler, mctpInfo); |
| 240 | auto configuration = |
| 241 | TestMctpDiscovery::getConfigurations(*mctpDiscoveryHandler); |
| 242 | EXPECT_EQ(configuration.size(), 0); |
| 243 | } |