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