blob: 481483897f79b5a9d3cef7fa4424b6cd5abf1b3e [file] [log] [blame]
Unive Tien7ad45b42025-08-18 06:04:53 +00001#include "common/test/mocked_utils.hpp"
2#include "fw-update/firmware_inventory.hpp"
3#include "fw-update/firmware_inventory_manager.hpp"
4
5#include <gmock/gmock.h>
6#include <gtest/gtest.h>
7
8using namespace pldm;
9using namespace std::chrono;
10using namespace pldm::fw_update;
11
12// Helper class for testing: inherits FirmwareInventory and exposes protected
13class FirmwareInventoryTest : public pldm::fw_update::FirmwareInventory
14{
15 public:
16 using FirmwareInventory::FirmwareInventory;
17
18 const std::string& getSoftwarePath() const
19 {
20 return this->softwarePath;
21 }
22 const SoftwareAssociationDefinitions& getAssociation() const
23 {
24 return this->association;
25 }
26 const SoftwareVersion& getVersion() const
27 {
28 return this->version;
29 }
30};
31
32class FirmwareInventoryManagerTest : public FirmwareInventoryManager
33{
34 public:
35 FirmwareInventoryManagerTest(const pldm::utils::DBusHandler* handler,
36 const Configurations& config) :
37 FirmwareInventoryManager(handler, config)
38 {}
39
40 SoftwareMap& getSoftwareMap()
41 {
42 return softwareMap;
43 }
44};
45
46TEST(GetBoardPath_WithMockHandler, ReturnsExpectedBoardPath)
47{
48 MockdBusHandler mockHandler;
49 InventoryPath inventoryPath =
50 "/xyz/openbmc_project/inventory/system/board/PLDM_Device";
51 pldm::utils::GetAncestorsResponse fakeResponse = {{inventoryPath, {}}};
52 EXPECT_CALL(mockHandler, getAncestors)
53 .WillOnce(::testing::Return(fakeResponse));
54
55 Configurations configurations;
56 std::string boardInventoryPath =
57 "/xyz/openbmc_project/inventory/system/board/PLDM_Device";
58 pldm::eid endpointId = 1;
59 pldm::UUID endpointUuid = "uuid";
60 pldm::MctpMedium endpointMedium = "medium";
61 pldm::NetworkId endpointNetId = 0;
62 pldm::MctpInfoName endpointName = "BMC";
63 pldm::MctpInfo endpointInfo = std::make_tuple(
64 endpointId, endpointUuid, endpointMedium, endpointNetId, endpointName);
65 configurations[boardInventoryPath] = endpointInfo;
66
67 FirmwareInventoryManagerTest inventoryManager(&mockHandler, configurations);
68
69 SoftwareIdentifier softwareIdentifier{endpointId, 100};
70 SoftwareName softwareName{"TestDevice"};
71 std::string firmwareVersion{"1.0.0"};
72 Descriptors firmwareDescriptors;
73 ComponentInfo firmwareComponentInfo;
74
75 inventoryManager.createFirmwareEntry(
76 softwareIdentifier, softwareName, firmwareVersion, firmwareDescriptors,
77 firmwareComponentInfo);
78 ASSERT_TRUE(inventoryManager.getSoftwareMap().contains(softwareIdentifier));
79
80 auto inventoryIt =
81 inventoryManager.getSoftwareMap().find(softwareIdentifier);
82 ASSERT_NE(inventoryIt, inventoryManager.getSoftwareMap().end());
83 const auto* inventory =
84 static_cast<FirmwareInventoryTest*>(inventoryIt->second.get());
85 ASSERT_NE(inventory, nullptr);
86 EXPECT_NE(inventory->getSoftwarePath().find(
87 "/xyz/openbmc_project/software/PLDM_Device_TestDevice_"),
88 std::string::npos);
89 EXPECT_EQ(inventory->getVersion().version(), firmwareVersion);
90}