xref: /openbmc/pldm/requester/test/mctp_endpoint_discovery_test.cpp (revision c40d4a68684a114b6a26adeca40b49146aa2c319)
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/test/mock_mctp_discovery_handler_intf.hpp"
7 
8 #include <gmock/gmock.h>
9 #include <gtest/gtest.h>
10 
11 using ::testing::_;
12 
13 class TestMctpDiscovery : public ::testing::Test
14 {
15   public:
getConfigurations(const pldm::MctpDiscovery & mctpDiscovery)16     static const pldm::Configurations& getConfigurations(
17         const pldm::MctpDiscovery& mctpDiscovery)
18     {
19         return mctpDiscovery.configurations;
20     }
searchConfigurationFor(pldm::MctpDiscovery & mctpDiscovery,pldm::utils::DBusHandler & handler,pldm::MctpInfo & mctpInfo)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 
TEST(MctpEndpointDiscoveryTest,SingleHandleMctpEndpoint)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 
TEST(MctpEndpointDiscoveryTest,MultipleHandleMctpEndpoints)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>(
51         bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{
52                  &manager1, &manager2});
53     mctpDiscoveryHandler = nullptr;
54 }
55 
TEST(MctpEndpointDiscoveryTest,goodGetMctpInfos)56 TEST(MctpEndpointDiscoveryTest, goodGetMctpInfos)
57 {
58     auto& bus = pldm::utils::DBusHandler::getBus();
59     pldm::MockManager manager;
60     std::map<pldm::MctpInfo, pldm::Availability> currentMctpInfoMap;
61 
62     auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
63         bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
64     mctpDiscoveryHandler->getMctpInfos(currentMctpInfoMap);
65     EXPECT_EQ(currentMctpInfoMap.size(), 0);
66 }
67 
TEST(MctpEndpointDiscoveryTest,goodAddToExistingMctpInfos)68 TEST(MctpEndpointDiscoveryTest, goodAddToExistingMctpInfos)
69 {
70     auto& bus = pldm::utils::DBusHandler::getBus();
71     pldm::MockManager manager;
72     const pldm::MctpInfos& mctpInfos = {
73         pldm::MctpInfo(11, pldm::emptyUUID, "", 1, std::nullopt),
74         pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)};
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 
TEST(MctpEndpointDiscoveryTest,badAddToExistingMctpInfos)86 TEST(MctpEndpointDiscoveryTest, badAddToExistingMctpInfos)
87 {
88     auto& bus = pldm::utils::DBusHandler::getBus();
89     pldm::MockManager manager;
90     const pldm::MctpInfos& mctpInfos = {
91         pldm::MctpInfo(11, pldm::emptyUUID, "", 1, std::nullopt)};
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 
TEST(MctpEndpointDiscoveryTest,goodRemoveFromExistingMctpInfos)99 TEST(MctpEndpointDiscoveryTest, goodRemoveFromExistingMctpInfos)
100 {
101     auto& bus = pldm::utils::DBusHandler::getBus();
102     pldm::MockManager manager;
103     const pldm::MctpInfos& mctpInfos = {
104         pldm::MctpInfo(11, pldm::emptyUUID, "def", 2, std::nullopt),
105         pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)};
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;
117     remainMctpInfos.emplace_back(
118         pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt));
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 
TEST(MctpEndpointDiscoveryTest,goodRemoveEndpoints)134 TEST(MctpEndpointDiscoveryTest, goodRemoveEndpoints)
135 {
136     auto& bus = pldm::utils::DBusHandler::getBus();
137     pldm::MockManager manager;
138     const pldm::MctpInfos& mctpInfos = {
139         pldm::MctpInfo(11, pldm::emptyUUID, "def", 2, std::nullopt),
140         pldm::MctpInfo(12, pldm::emptyUUID, "abc", 1, std::nullopt)};
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 }
157 
TEST(MctpEndpointDiscoveryTest,goodSearchConfigurationFor)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 
TEST(MctpEndpointDiscoveryTest,badSearchConfigurationFor)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 }
244