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