Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 3 | #include "common/types.hpp" |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 4 | #include "common/utils.hpp" |
| 5 | #include "requester/test/mock_mctp_discovery_handler_intf.hpp" |
| 6 | |
| 7 | #include <gmock/gmock.h> |
| 8 | #include <gtest/gtest.h> |
| 9 | |
| 10 | using ::testing::_; |
| 11 | |
| 12 | TEST(MctpEndpointDiscoveryTest, SingleHandleMctpEndpoint) |
| 13 | { |
| 14 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 15 | pldm::MockManager manager; |
| 16 | |
| 17 | EXPECT_CALL(manager, handleMctpEndpoints(_)).Times(1); |
| 18 | |
| 19 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 20 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 21 | mctpDiscoveryHandler = nullptr; |
| 22 | } |
| 23 | |
| 24 | TEST(MctpEndpointDiscoveryTest, MultipleHandleMctpEndpoints) |
| 25 | { |
| 26 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 27 | pldm::MockManager manager1; |
| 28 | pldm::MockManager manager2; |
| 29 | |
| 30 | EXPECT_CALL(manager1, handleMctpEndpoints(_)).Times(1); |
| 31 | EXPECT_CALL(manager2, handleMctpEndpoints(_)).Times(1); |
| 32 | |
| 33 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
Patrick Williams | 16c2a0a | 2024-08-16 15:20:59 -0400 | [diff] [blame] | 34 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{ |
| 35 | &manager1, &manager2}); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 36 | mctpDiscoveryHandler = nullptr; |
| 37 | } |
| 38 | |
| 39 | TEST(MctpEndpointDiscoveryTest, goodGetMctpInfos) |
| 40 | { |
| 41 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 42 | pldm::MockManager manager; |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 43 | std::map<pldm::MctpInfo, pldm::Availability> currentMctpInfoMap; |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 44 | |
| 45 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 46 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
Chau Ly | 75e0042 | 2024-03-19 12:33:08 +0000 | [diff] [blame] | 47 | mctpDiscoveryHandler->getMctpInfos(currentMctpInfoMap); |
| 48 | EXPECT_EQ(currentMctpInfoMap.size(), 0); |
Gilbert Chen | 44524a5 | 2022-02-14 12:12:25 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TEST(MctpEndpointDiscoveryTest, goodAddToExistingMctpInfos) |
| 52 | { |
| 53 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 54 | pldm::MockManager manager; |
| 55 | const pldm::MctpInfos& mctpInfos = { |
| 56 | pldm::MctpInfo(11, pldm::emptyUUID, "", 1), |
| 57 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)}; |
| 58 | |
| 59 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 60 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 61 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 62 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 63 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 64 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 65 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 66 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 67 | } |
| 68 | |
| 69 | TEST(MctpEndpointDiscoveryTest, badAddToExistingMctpInfos) |
| 70 | { |
| 71 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 72 | pldm::MockManager manager; |
| 73 | const pldm::MctpInfos& mctpInfos = { |
| 74 | pldm::MctpInfo(11, pldm::emptyUUID, "", 1)}; |
| 75 | |
| 76 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 77 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 78 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 79 | EXPECT_NE(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 80 | } |
| 81 | |
| 82 | TEST(MctpEndpointDiscoveryTest, goodRemoveFromExistingMctpInfos) |
| 83 | { |
| 84 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 85 | pldm::MockManager manager; |
| 86 | const pldm::MctpInfos& mctpInfos = { |
| 87 | pldm::MctpInfo(11, pldm::emptyUUID, "def", 2), |
| 88 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)}; |
| 89 | |
| 90 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 91 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 92 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 93 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 94 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 95 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 96 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 97 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 98 | pldm::MctpInfos removedInfos; |
| 99 | pldm::MctpInfos remainMctpInfos; |
| 100 | remainMctpInfos.emplace_back(pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)); |
| 101 | |
| 102 | mctpDiscoveryHandler->removeFromExistingMctpInfos(remainMctpInfos, |
| 103 | removedInfos); |
| 104 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 1); |
| 105 | mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 106 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 107 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 108 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 109 | EXPECT_EQ(removedInfos.size(), 1); |
| 110 | mctpInfo = removedInfos.back(); |
| 111 | EXPECT_EQ(std::get<0>(mctpInfo), 11); |
| 112 | EXPECT_EQ(std::get<2>(mctpInfo), "def"); |
| 113 | EXPECT_EQ(std::get<3>(mctpInfo), 2); |
| 114 | } |
| 115 | |
| 116 | TEST(MctpEndpointDiscoveryTest, goodRemoveEndpoints) |
| 117 | { |
| 118 | auto& bus = pldm::utils::DBusHandler::getBus(); |
| 119 | pldm::MockManager manager; |
| 120 | const pldm::MctpInfos& mctpInfos = { |
| 121 | pldm::MctpInfo(11, pldm::emptyUUID, "def", 2), |
| 122 | pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1)}; |
| 123 | |
| 124 | auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>( |
| 125 | bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager}); |
| 126 | mctpDiscoveryHandler->addToExistingMctpInfos(mctpInfos); |
| 127 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 2); |
| 128 | pldm::MctpInfo mctpInfo = mctpDiscoveryHandler->existingMctpInfos.back(); |
| 129 | EXPECT_EQ(std::get<0>(mctpInfo), 12); |
| 130 | EXPECT_EQ(std::get<2>(mctpInfo), "abc"); |
| 131 | EXPECT_EQ(std::get<3>(mctpInfo), 1); |
| 132 | sdbusplus::message_t msg = sdbusplus::bus::new_default().new_method_call( |
| 133 | "xyz.openbmc_project.sdbusplus.test.Object", |
| 134 | "/xyz/openbmc_project/sdbusplus/test/object", |
| 135 | "xyz.openbmc_project.sdbusplus.test.Object", "Unused"); |
| 136 | mctpDiscoveryHandler->removeEndpoints(msg); |
| 137 | EXPECT_EQ(mctpDiscoveryHandler->existingMctpInfos.size(), 0); |
| 138 | } |