blob: 01e5eb093c8666c2bc7a1032b59a5a6d46a8e2c6 [file] [log] [blame]
Gilbert Chen44524a52022-02-14 12:12:25 +00001#include "config.h"
2
Chau Ly75e00422024-03-19 12:33:08 +00003#include "common/types.hpp"
Gilbert Chen44524a52022-02-14 12:12:25 +00004#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
10using ::testing::_;
11
12TEST(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
24TEST(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 Williams16c2a0a2024-08-16 15:20:59 -040034 bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{
35 &manager1, &manager2});
Gilbert Chen44524a52022-02-14 12:12:25 +000036 mctpDiscoveryHandler = nullptr;
37}
38
39TEST(MctpEndpointDiscoveryTest, goodGetMctpInfos)
40{
41 auto& bus = pldm::utils::DBusHandler::getBus();
42 pldm::MockManager manager;
Chau Ly75e00422024-03-19 12:33:08 +000043 std::map<pldm::MctpInfo, pldm::Availability> currentMctpInfoMap;
Gilbert Chen44524a52022-02-14 12:12:25 +000044
45 auto mctpDiscoveryHandler = std::make_unique<pldm::MctpDiscovery>(
46 bus, std::initializer_list<pldm::MctpDiscoveryHandlerIntf*>{&manager});
Chau Ly75e00422024-03-19 12:33:08 +000047 mctpDiscoveryHandler->getMctpInfos(currentMctpInfoMap);
48 EXPECT_EQ(currentMctpInfoMap.size(), 0);
Gilbert Chen44524a52022-02-14 12:12:25 +000049}
50
51TEST(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
69TEST(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
82TEST(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
116TEST(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}