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