xref: /openbmc/pldm/host-bmc/test/utils_test.cpp (revision 963c0fdf)
1 #include "common/utils.hpp"
2 
3 #include <libpldm/pdr.h>
4 
5 #include <filesystem>
6 
7 #include <gtest/gtest.h>
8 
9 namespace fs = std::filesystem;
10 using namespace pldm;
11 using namespace pldm::utils;
12 
13 TEST(EntityAssociation, addObjectPathEntityAssociations1)
14 {
15     pldm_entity entities[8]{};
16 
17     entities[0].entity_type = 45;
18     entities[0].entity_container_id = 0;
19 
20     entities[1].entity_type = 64;
21     entities[1].entity_container_id = 1;
22 
23     entities[2].entity_type = 67;
24     entities[2].entity_container_id = 2;
25     entities[3].entity_type = 67;
26     entities[3].entity_container_id = 2;
27 
28     entities[4].entity_type = 135;
29     entities[4].entity_container_id = 3;
30     entities[5].entity_type = 135;
31     entities[5].entity_container_id = 3;
32     entities[6].entity_type = 135;
33     entities[6].entity_container_id = 3;
34     entities[7].entity_type = 135;
35     entities[7].entity_container_id = 3;
36 
37     auto tree = pldm_entity_association_tree_init();
38 
39     auto l1 = pldm_entity_association_tree_add_entity(
40         tree, &entities[0], 1, nullptr, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true,
41         true, 0xFFFF);
42 
43     auto l2 = pldm_entity_association_tree_add_entity(
44         tree, &entities[1], 1, l1, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
45         0xFFFF);
46 
47     auto l3a = pldm_entity_association_tree_add_entity(
48         tree, &entities[2], 0, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
49         0xFFFF);
50     auto l3b = pldm_entity_association_tree_add_entity(
51         tree, &entities[3], 1, l2, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
52         0xFFFF);
53 
54     auto l4a = pldm_entity_association_tree_add_entity(
55         tree, &entities[4], 0, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
56         0xFFFF);
57     auto l4b = pldm_entity_association_tree_add_entity(
58         tree, &entities[5], 1, l3a, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
59         0xFFFF);
60 
61     auto l5a = pldm_entity_association_tree_add_entity(
62         tree, &entities[6], 0, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
63         0xFFFF);
64     auto l5b = pldm_entity_association_tree_add_entity(
65         tree, &entities[7], 1, l3b, PLDM_ENTITY_ASSOCIAION_PHYSICAL, true, true,
66         0xFFFF);
67 
68     EntityAssociations entityAssociations = {
69         {l1, l2}, {l2, l3a, l3b}, {l3a, l4a, l4b}, {l3b, l5a, l5b}};
70 
71     ObjectPathMaps retObjectMaps = {
72         {"/xyz/openbmc_project/inventory/chassis1", l1},
73         {"/xyz/openbmc_project/inventory/chassis1/motherboard1", l2},
74         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0", l3a},
75         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0/cpu0", l4a},
76         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm0/cpu1", l4b},
77         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1", l3b},
78         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1/cpu0", l5a},
79         {"/xyz/openbmc_project/inventory/chassis1/motherboard1/dcm1/cpu1",
80          l5b}};
81 
82     ObjectPathMaps objPathMap;
83     updateEntityAssociation(entityAssociations, tree, objPathMap);
84 
85     EXPECT_EQ(objPathMap.size(), retObjectMaps.size());
86 
87     int index = 0;
88     for (auto& obj : objPathMap)
89     {
90         if (retObjectMaps.contains(obj.first))
91         {
92             index++;
93             pldm_entity entity = pldm_entity_extract(obj.second);
94             pldm_entity retEntity =
95                 pldm_entity_extract(retObjectMaps[obj.first]);
96             EXPECT_EQ(entity.entity_type, retEntity.entity_type);
97             EXPECT_EQ(entity.entity_instance_num,
98                       retEntity.entity_instance_num);
99             EXPECT_EQ(pldm_entity_node_get_remote_container_id(obj.second),
100                       pldm_entity_node_get_remote_container_id(
101                           retObjectMaps[obj.first]));
102         }
103     }
104     EXPECT_EQ(index, retObjectMaps.size());
105     pldm_entity_association_tree_destroy(tree);
106 }
107