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