1 #include "common/utils.hpp"
2 #include "fw-update/inventory_manager.hpp"
3 #include "requester/test/mock_request.hpp"
4 #include "test/test_instance_id.hpp"
5
6 #include <libpldm/firmware_update.h>
7
8 #include <gtest/gtest.h>
9
10 using namespace pldm;
11 using namespace std::chrono;
12 using namespace pldm::fw_update;
13
14 class InventoryManagerTest : public testing::Test
15 {
16 protected:
InventoryManagerTest()17 InventoryManagerTest() :
18 event(sdeventplus::Event::get_default()), instanceIdDb(),
19 reqHandler(nullptr, event, instanceIdDb, false, seconds(1), 2,
20 milliseconds(100)),
21 inventoryManager(&dBusHandler, reqHandler, instanceIdDb,
22 outDescriptorMap, outDownstreamDescriptorMap,
23 outComponentInfoMap, configurations)
24 {}
25
26 int fd = -1;
27 const pldm::utils::DBusHandler dBusHandler;
28 sdeventplus::Event event;
29 TestInstanceIdDb instanceIdDb;
30 requester::Handler<requester::Request> reqHandler;
31 InventoryManager inventoryManager;
32 DescriptorMap outDescriptorMap{};
33 DownstreamDescriptorMap outDownstreamDescriptorMap{};
34 ComponentInfoMap outComponentInfoMap{};
35 Configurations configurations;
36 };
37
TEST_F(InventoryManagerTest,handleQueryDeviceIdentifiersResponse)38 TEST_F(InventoryManagerTest, handleQueryDeviceIdentifiersResponse)
39 {
40 constexpr size_t respPayloadLength1 = 49;
41 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength1>
42 queryDeviceIdentifiersResp1{
43 0x00, 0x00, 0x00, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00,
44 0x04, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x02, 0x00, 0x10, 0x00, 0x12,
45 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18, 0xa0, 0x30, 0xfc, 0x8a,
46 0x56, 0x58, 0x7d, 0x5b, 0xFF, 0xFF, 0x0B, 0x00, 0x01, 0x07, 0x4f,
47 0x70, 0x65, 0x6e, 0x42, 0x4d, 0x43, 0x01, 0x02};
48 auto responseMsg1 =
49 reinterpret_cast<const pldm_msg*>(queryDeviceIdentifiersResp1.data());
50 inventoryManager.queryDeviceIdentifiers(1, responseMsg1,
51 respPayloadLength1);
52
53 DescriptorMap descriptorMap1{
54 {0x01,
55 {{PLDM_FWUP_IANA_ENTERPRISE_ID,
56 std::vector<uint8_t>{0x0a, 0x0b, 0x0c, 0xd}},
57 {PLDM_FWUP_UUID,
58 std::vector<uint8_t>{0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
59 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d,
60 0x5b}},
61 {PLDM_FWUP_VENDOR_DEFINED,
62 std::make_tuple("OpenBMC", std::vector<uint8_t>{0x01, 0x02})}}}};
63
64 EXPECT_EQ(outDescriptorMap.size(), descriptorMap1.size());
65 EXPECT_EQ(outDescriptorMap, descriptorMap1);
66
67 constexpr size_t respPayloadLength2 = 26;
68 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength2>
69 queryDeviceIdentifiersResp2{
70 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x01, 0x02,
71 0x00, 0x10, 0x00, 0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49,
72 0x43, 0x98, 0x00, 0xA0, 0x2F, 0x59, 0x9A, 0xCA, 0x02};
73 auto responseMsg2 =
74 reinterpret_cast<const pldm_msg*>(queryDeviceIdentifiersResp2.data());
75 inventoryManager.queryDeviceIdentifiers(2, responseMsg2,
76 respPayloadLength2);
77 DescriptorMap descriptorMap2{
78 {0x01,
79 {{PLDM_FWUP_IANA_ENTERPRISE_ID,
80 std::vector<uint8_t>{0x0a, 0x0b, 0x0c, 0xd}},
81 {PLDM_FWUP_UUID,
82 std::vector<uint8_t>{0x12, 0x44, 0xd2, 0x64, 0x8d, 0x7d, 0x47, 0x18,
83 0xa0, 0x30, 0xfc, 0x8a, 0x56, 0x58, 0x7d,
84 0x5b}},
85 {PLDM_FWUP_VENDOR_DEFINED,
86 std::make_tuple("OpenBMC", std::vector<uint8_t>{0x01, 0x02})}}},
87 {0x02,
88 {{PLDM_FWUP_UUID,
89 std::vector<uint8_t>{0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43,
90 0x98, 0x00, 0xA0, 0x2F, 0x59, 0x9A, 0xCA,
91 0x02}}}}};
92 EXPECT_EQ(outDescriptorMap.size(), descriptorMap2.size());
93 EXPECT_EQ(outDescriptorMap, descriptorMap2);
94 }
95
TEST_F(InventoryManagerTest,handleQueryDeviceIdentifiersResponseErrorCC)96 TEST_F(InventoryManagerTest, handleQueryDeviceIdentifiersResponseErrorCC)
97 {
98 constexpr size_t respPayloadLength = 1;
99 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength>
100 queryDeviceIdentifiersResp{0x00, 0x00, 0x00, 0x01};
101 auto responseMsg =
102 reinterpret_cast<const pldm_msg*>(queryDeviceIdentifiersResp.data());
103 inventoryManager.queryDeviceIdentifiers(1, responseMsg, respPayloadLength);
104 EXPECT_EQ(outDescriptorMap.size(), 0);
105 }
106
TEST_F(InventoryManagerTest,handleQueryDownstreamIdentifierResponse)107 TEST_F(InventoryManagerTest, handleQueryDownstreamIdentifierResponse)
108 {
109 constexpr uint8_t eid = 1;
110 constexpr uint8_t downstreamDeviceCount = 1;
111 constexpr uint32_t downstreamDeviceLen = 11;
112 constexpr size_t respPayloadLength =
113 PLDM_QUERY_DOWNSTREAM_IDENTIFIERS_RESP_MIN_LEN + downstreamDeviceLen;
114
115 std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength>
116 queryDownstreamIdentifiersResp{
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
118 0x0B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
119 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0xa0, 0x15};
120 auto responseMsg = new (queryDownstreamIdentifiersResp.data()) pldm_msg;
121
122 inventoryManager.queryDownstreamIdentifiers(eid, responseMsg,
123 respPayloadLength);
124
125 DownstreamDeviceInfo downstreamDevices = {
126 {0,
127 {{PLDM_FWUP_IANA_ENTERPRISE_ID,
128 std::vector<uint8_t>{0x00, 0x00, 0xa0, 0x15}}}}};
129 DownstreamDescriptorMap refDownstreamDescriptorMap{
130 {eid, downstreamDevices}};
131
132 ASSERT_EQ(outDownstreamDescriptorMap.size(), downstreamDeviceCount);
133 ASSERT_EQ(outDownstreamDescriptorMap.size(),
134 refDownstreamDescriptorMap.size());
135 ASSERT_EQ(outDownstreamDescriptorMap, refDownstreamDescriptorMap);
136 }
137
TEST_F(InventoryManagerTest,handleQueryDownstreamIdentifierResponseErrorCC)138 TEST_F(InventoryManagerTest, handleQueryDownstreamIdentifierResponseErrorCC)
139 {
140 constexpr size_t respPayloadLength = 1;
141 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength>
142 queryDownstreamIdentifiersResp{0x00, 0x00, 0x00, 0x01};
143 const auto responseMsg =
144 new (const_cast<unsigned char*>(queryDownstreamIdentifiersResp.data()))
145 pldm_msg;
146 inventoryManager.queryDownstreamIdentifiers(1, responseMsg,
147 respPayloadLength);
148
149 ASSERT_EQ(outDownstreamDescriptorMap.size(), 0);
150 }
151
TEST_F(InventoryManagerTest,getFirmwareParametersResponse)152 TEST_F(InventoryManagerTest, getFirmwareParametersResponse)
153 {
154 // constexpr uint16_t compCount = 2;
155 // constexpr std::string_view activeCompImageSetVersion{"DeviceVer1.0"};
156 // constexpr std::string_view activeCompVersion1{"Comp1v2.0"};
157 // constexpr std::string_view activeCompVersion2{"Comp2v3.0"};
158 constexpr uint16_t compClassification1 = 10;
159 constexpr uint16_t compIdentifier1 = 300;
160 constexpr uint8_t compClassificationIndex1 = 20;
161 constexpr uint16_t compClassification2 = 16;
162 constexpr uint16_t compIdentifier2 = 301;
163 constexpr uint8_t compClassificationIndex2 = 30;
164
165 constexpr size_t respPayloadLength1 = 119;
166 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength1>
167 getFirmwareParametersResp1{
168 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,
169 0x0c, 0x00, 0x00, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x56, 0x65,
170 0x72, 0x31, 0x2e, 0x30, 0x0a, 0x00, 0x2c, 0x01, 0x14, 0x00, 0x00,
171 0x00, 0x00, 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
172 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
173 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43,
174 0x6f, 0x6d, 0x70, 0x31, 0x76, 0x32, 0x2e, 0x30, 0x10, 0x00, 0x2d,
175 0x01, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x01, 0x09, 0x00, 0x00, 0x00,
176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
178 0x00, 0x00, 0x00, 0x43, 0x6f, 0x6d, 0x70, 0x32, 0x76, 0x33, 0x2e,
179 0x30};
180 auto responseMsg1 =
181 reinterpret_cast<const pldm_msg*>(getFirmwareParametersResp1.data());
182 inventoryManager.getFirmwareParameters(1, responseMsg1, respPayloadLength1);
183
184 ComponentInfoMap componentInfoMap1{
185 {1,
186 {{std::make_pair(compClassification1, compIdentifier1),
187 compClassificationIndex1},
188 {std::make_pair(compClassification2, compIdentifier2),
189 compClassificationIndex2}}}};
190 EXPECT_EQ(outComponentInfoMap.size(), componentInfoMap1.size());
191 EXPECT_EQ(outComponentInfoMap, componentInfoMap1);
192
193 // constexpr uint16_t compCount = 1;
194 // constexpr std::string_view activeCompImageSetVersion{"DeviceVer2.0"};
195 // constexpr std::string_view activeCompVersion1{"Comp3v4.0"};
196 constexpr uint16_t compClassification3 = 2;
197 constexpr uint16_t compIdentifier3 = 302;
198 constexpr uint8_t compClassificationIndex3 = 40;
199
200 constexpr size_t respPayloadLength2 = 119;
201 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength2>
202 getFirmwareParametersResp2{
203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
204 0x0c, 0x00, 0x00, 0x44, 0x65, 0x76, 0x69, 0x63, 0x65, 0x56, 0x65,
205 0x72, 0x32, 0x2e, 0x30, 0x02, 0x00, 0x2e, 0x01, 0x28, 0x00, 0x00,
206 0x00, 0x00, 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x43,
209 0x6f, 0x6d, 0x70, 0x33, 0x76, 0x34, 0x2e, 0x30};
210 auto responseMsg2 =
211 reinterpret_cast<const pldm_msg*>(getFirmwareParametersResp2.data());
212 inventoryManager.getFirmwareParameters(2, responseMsg2, respPayloadLength2);
213
214 ComponentInfoMap componentInfoMap2{
215 {1,
216 {{std::make_pair(compClassification1, compIdentifier1),
217 compClassificationIndex1},
218 {std::make_pair(compClassification2, compIdentifier2),
219 compClassificationIndex2}}},
220 {2,
221 {{std::make_pair(compClassification3, compIdentifier3),
222 compClassificationIndex3}}}};
223 EXPECT_EQ(outComponentInfoMap.size(), componentInfoMap2.size());
224 EXPECT_EQ(outComponentInfoMap, componentInfoMap2);
225 }
226
TEST_F(InventoryManagerTest,getFirmwareParametersResponseErrorCC)227 TEST_F(InventoryManagerTest, getFirmwareParametersResponseErrorCC)
228 {
229 constexpr size_t respPayloadLength = 1;
230 constexpr std::array<uint8_t, sizeof(pldm_msg_hdr) + respPayloadLength>
231 getFirmwareParametersResp{0x00, 0x00, 0x00, 0x01};
232 auto responseMsg =
233 reinterpret_cast<const pldm_msg*>(getFirmwareParametersResp.data());
234 inventoryManager.getFirmwareParameters(1, responseMsg, respPayloadLength);
235 EXPECT_EQ(outComponentInfoMap.size(), 0);
236 }
237