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