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