blob: d654fca3dfafc45d7fbd5b510b2999a3e854dbd5 [file] [log] [blame]
George Liu5bfb0dc2021-05-01 14:28:41 +08001#include "common/utils.hpp"
2#include "libpldmresponder/fru.hpp"
Tom Joseph151d5332019-11-17 22:21:45 +05303#include "libpldmresponder/fru_parser.hpp"
4
George Liu5bfb0dc2021-05-01 14:28:41 +08005#include <config.h>
6#include <libpldm/pdr.h>
7
8#include <sdbusplus/message.hpp>
9
Tom Joseph151d5332019-11-17 22:21:45 +053010#include <gtest/gtest.h>
George Liu5bfb0dc2021-05-01 14:28:41 +080011
Tom Joseph151d5332019-11-17 22:21:45 +053012TEST(FruParser, allScenarios)
13{
14 using namespace pldm::responder::fru_parser;
15
Manojkiran Eda03b01ca2021-06-29 08:55:09 +053016 FruParser parser{"./fru_jsons/good",
17 "./fru_jsons/fru_master/fru_master.json"};
Tom Joseph151d5332019-11-17 22:21:45 +053018
19 // Get an item with a single PLDM FRU record
Deepak Kodihallida079602020-08-05 03:23:03 -050020 FruRecordInfos cpu{
21 {1,
22 1,
23 {{"xyz.openbmc_project.Inventory.Decorator.Asset", "Model", "string",
24 2},
25 {"xyz.openbmc_project.Inventory.Decorator.Asset", "PartNumber",
26 "string", 3},
27 {"xyz.openbmc_project.Inventory.Decorator.Asset", "SerialNumber",
28 "string", 4},
29 {"xyz.openbmc_project.Inventory.Decorator.Asset", "Manufacturer",
30 "string", 5},
31 {"xyz.openbmc_project.Inventory.Item", "PrettyName", "string", 8},
32 {"xyz.openbmc_project.Inventory.Decorator.AssetTag", "AssetTag",
33 "string", 11},
34 {"xyz.openbmc_project.Inventory.Decorator.Revision", "Version",
35 "string", 10}}},
36 {1,
37 1,
38 {{"xyz.openbmc_project.Inventory.Decorator.Asset", "PartNumber",
39 "string", 3},
40 {"xyz.openbmc_project.Inventory.Decorator.Asset", "SerialNumber",
41 "string", 4}}}};
Tom Joseph151d5332019-11-17 22:21:45 +053042 auto cpuInfos =
43 parser.getRecordInfo("xyz.openbmc_project.Inventory.Item.Cpu");
Deepak Kodihallida079602020-08-05 03:23:03 -050044 ASSERT_EQ(cpuInfos.size(), 2);
Tom Joseph151d5332019-11-17 22:21:45 +053045 ASSERT_EQ(cpu == cpuInfos, true);
46
Deepak Kodihallida079602020-08-05 03:23:03 -050047 // Get an item type with 3 PLDM FRU records
Tom Joseph151d5332019-11-17 22:21:45 +053048 auto boardInfos =
49 parser.getRecordInfo("xyz.openbmc_project.Inventory.Item.Board");
Deepak Kodihallida079602020-08-05 03:23:03 -050050 ASSERT_EQ(boardInfos.size(), 3);
Tom Joseph151d5332019-11-17 22:21:45 +053051
52 // D-Bus lookup info for FRU information
Deepak Kodihallida079602020-08-05 03:23:03 -050053 DBusLookupInfo lookupInfo{
54 "xyz.openbmc_project.Inventory.Manager",
55 "/xyz/openbmc_project/inventory",
56 {"xyz.openbmc_project.Inventory.Item.Chassis",
57 "xyz.openbmc_project.Inventory.Item.Board",
Jayashankar Padatha137f902021-06-04 07:16:11 -050058 "xyz.openbmc_project.Inventory.Item.PCIeDevice",
Deepak Kodihallida079602020-08-05 03:23:03 -050059 "xyz.openbmc_project.Inventory.Item.Board.Motherboard",
Jayashankar Padatha137f902021-06-04 07:16:11 -050060 "xyz.openbmc_project.Inventory.Item.Dimm",
Deepak Kodihallida079602020-08-05 03:23:03 -050061 "xyz.openbmc_project.Inventory.Item.Panel",
Jayashankar Padatha137f902021-06-04 07:16:11 -050062 "xyz.openbmc_project.Inventory.Item.DiskBackplane",
63 "xyz.openbmc_project.Inventory.Item.Fan",
Deepak Kodihallida079602020-08-05 03:23:03 -050064 "xyz.openbmc_project.Inventory.Item.PowerSupply",
Jayashankar Padatha137f902021-06-04 07:16:11 -050065 "xyz.openbmc_project.Inventory.Item.Battery",
Deepak Kodihallida079602020-08-05 03:23:03 -050066 "xyz.openbmc_project.Inventory.Item.Vrm",
67 "xyz.openbmc_project.Inventory.Item.Cpu",
68 "xyz.openbmc_project.Inventory.Item.Bmc",
Jayashankar Padatha137f902021-06-04 07:16:11 -050069 "xyz.openbmc_project.Inventory.Item.Connector",
70 "xyz.openbmc_project.Inventory.Item.PCIeSlot",
71 "xyz.openbmc_project.Inventory.Item.System",
72 "xyz.openbmc_project.Inventory.Item.Tpm"}};
Tom Joseph151d5332019-11-17 22:21:45 +053073 auto dbusInfo = parser.inventoryLookup();
74 ASSERT_EQ(dbusInfo == lookupInfo, true);
75
Tom Joseph151d5332019-11-17 22:21:45 +053076 ASSERT_THROW(
77 parser.getRecordInfo("xyz.openbmc_project.Inventory.Item.DIMM"),
78 std::exception);
79}
George Liu5bfb0dc2021-05-01 14:28:41 +080080
81TEST(FruImpl, updateAssociationTreeTest)
82{
83 using namespace pldm::responder;
84 using namespace pldm::responder::fru_parser;
85 using namespace pldm::responder::dbus;
86 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
87 pldm_pdr_init(), pldm_pdr_destroy);
88 std::unique_ptr<pldm_entity_association_tree,
89 decltype(&pldm_entity_association_tree_destroy)>
90 entityTree(pldm_entity_association_tree_init(),
91 pldm_entity_association_tree_destroy);
92 std::unique_ptr<pldm_entity_association_tree,
93 decltype(&pldm_entity_association_tree_destroy)>
94 bmcEntityTree(pldm_entity_association_tree_init(),
95 pldm_entity_association_tree_destroy);
96
97 ObjectValueTree objects{
98 {sdbusplus::message::object_path(
99 "/xyz/openbmc_project/inventory/system"),
100 {{"xyz.openbmc_project.Inventory.Item.System", {}}}},
101 {sdbusplus::message::object_path(
102 "/xyz/openbmc_project/inventory/system/chassis"),
103 {{"xyz.openbmc_project.Inventory.Item.Chassis", {}}}},
104 {sdbusplus::message::object_path(
105 "/xyz/openbmc_project/inventory/system/chassis/motherboard"),
106 {{"xyz.openbmc_project.Inventory.Item.Board.Motherboard", {}}}}};
107
108 typedef struct test_pldm_entity_node
109 {
110 pldm_entity entity;
111 pldm_entity parent;
112 uint16_t host_container_id;
113 pldm_entity_node* first_child;
114 pldm_entity_node* next_sibling;
115 uint8_t association_type;
116 } test_pldm_entity_node;
117
Andrew Jeffery19fdb452023-08-24 14:10:26 +0930118 pldm::responder::FruImpl mockedFruHandler(
119 FRU_JSONS_DIR, "./fru_jsons/fru_master/fru_master.json", pdrRepo.get(),
120 entityTree.get(), bmcEntityTree.get());
George Liu5bfb0dc2021-05-01 14:28:41 +0800121
122 pldm_entity systemEntity{0x2d01, 1, 0};
123 pldm_entity chassisEntity{0x2d, 1, 1};
124 pldm_entity motherboardEntity{0x40, 1, 2};
125 pldm_entity panelEntity{0x45, 1, 3};
126
127 dbus::InterfaceMap systemIface{
128 {"xyz.openbmc_project.Inventory.Item.System", {}}};
129 dbus::InterfaceMap chassisIface{
130 {"xyz.openbmc_project.Inventory.Item.Chassis", {}}};
131 dbus::InterfaceMap motherboardIface{
132 {"xyz.openbmc_project.Inventory.Item.Board.Motherboard", {}}};
133
134 mockedFruHandler.updateAssociationTree(
135 objects, "/xyz/openbmc_project/inventory/system/chassis/motherboard");
136
137 pldm_entity_node* node = pldm_entity_association_tree_find(entityTree.get(),
138 &systemEntity);
139 EXPECT_TRUE(node != NULL);
140
141 node = pldm_entity_association_tree_find(entityTree.get(), &chassisEntity);
Andrew Jeffery32403432023-08-24 14:12:50 +0930142 ASSERT_TRUE(node != NULL);
George Liu5bfb0dc2021-05-01 14:28:41 +0800143 test_pldm_entity_node* test_node = (test_pldm_entity_node*)node;
144 EXPECT_TRUE((test_node->parent).entity_type == systemEntity.entity_type);
145
146 node = pldm_entity_association_tree_find(entityTree.get(),
147 &motherboardEntity);
Andrew Jeffery32403432023-08-24 14:12:50 +0930148 ASSERT_TRUE(node != NULL);
George Liu5bfb0dc2021-05-01 14:28:41 +0800149 test_node = (test_pldm_entity_node*)node;
150 EXPECT_TRUE((test_node->parent).entity_type == chassisEntity.entity_type);
151
152 node = pldm_entity_association_tree_find(entityTree.get(), &panelEntity);
153 EXPECT_TRUE(node == NULL);
154}
155
156TEST(FruImpl, entityByObjectPath)
157{
158 using namespace pldm::responder::dbus;
159 std::unique_ptr<pldm_pdr, decltype(&pldm_pdr_destroy)> pdrRepo(
160 pldm_pdr_init(), pldm_pdr_destroy);
161 std::unique_ptr<pldm_entity_association_tree,
162 decltype(&pldm_entity_association_tree_destroy)>
163 entityTree(pldm_entity_association_tree_init(),
164 pldm_entity_association_tree_destroy);
165 std::unique_ptr<pldm_entity_association_tree,
166 decltype(&pldm_entity_association_tree_destroy)>
167 bmcEntityTree(pldm_entity_association_tree_init(),
168 pldm_entity_association_tree_destroy);
169
170 InterfaceMap iface = {{"xyz.openbmc_project.Inventory.Item.Chassis", {}}};
Andrew Jeffery19fdb452023-08-24 14:10:26 +0930171 pldm::responder::FruImpl mockedFruHandler(
172 FRU_JSONS_DIR, "./fru_jsons/fru_master/fru_master.json", pdrRepo.get(),
173 entityTree.get(), bmcEntityTree.get());
George Liu5bfb0dc2021-05-01 14:28:41 +0800174
175 // Good path
176 auto entityPtr = mockedFruHandler.getEntityByObjectPath(iface);
177 pldm_entity entity = *entityPtr;
178 EXPECT_TRUE(entity.entity_type == 45);
179 // Bad Path
180 InterfaceMap invalidIface = {
181 {"xyz.openbmc_project.Inventory.Item.Motherboard", {}}};
182 entityPtr = mockedFruHandler.getEntityByObjectPath(invalidIface);
183 ASSERT_TRUE(!entityPtr);
184}