blob: 9d98c1f14e82c550978323e2caabf556b6372bf1 [file] [log] [blame]
George Liu6492f522020-06-16 10:34:05 +08001#include "libpldm/platform.h"
2
George Liue53193f2020-02-24 09:23:26 +08003#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihallic682fe22020-03-04 00:42:54 -06004#include "libpldmresponder/platform.hpp"
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05005
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05006#include <gtest/gtest.h>
7
8using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -06009using namespace pldm::responder::platform;
10using namespace pldm::responder::pdr;
11using namespace pldm::responder::pdr_utils;
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050012
George Liu456c9a22020-01-13 11:36:22 +080013TEST(GeneratePDRByStateEffecter, testGoodJson)
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050014{
Deepak Kodihallic682fe22020-03-04 00:42:54 -060015 auto inPDRRepo = pldm_pdr_init();
16 auto outPDRRepo = pldm_pdr_init();
17 Repo outRepo(outPDRRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053018 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
19 inPDRRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060020 Repo inRepo(inPDRRepo);
21 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050022
23 // 2 entries
Deepak Kodihallic682fe22020-03-04 00:42:54 -060024 ASSERT_EQ(outRepo.getRecordCount(), 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050025
26 // Check first PDR
George Liue53193f2020-02-24 09:23:26 +080027 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060028 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +080029 ASSERT_NE(record1, nullptr);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050030 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +080031 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050032
33 ASSERT_EQ(pdr->hdr.record_handle, 1);
34 ASSERT_EQ(pdr->hdr.version, 1);
35 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
36 ASSERT_EQ(pdr->hdr.record_change_num, 0);
Sampa Misraa2fa0702019-05-31 01:28:55 -050037 ASSERT_EQ(pdr->hdr.length, 23);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050038
39 ASSERT_EQ(pdr->terminus_handle, 0);
40 ASSERT_EQ(pdr->effecter_id, 1);
41 ASSERT_EQ(pdr->entity_type, 33);
42 ASSERT_EQ(pdr->entity_instance, 0);
43 ASSERT_EQ(pdr->container_id, 0);
44 ASSERT_EQ(pdr->effecter_semantic_id, 0);
45 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
46 ASSERT_EQ(pdr->has_description_pdr, false);
Sampa Misraa2fa0702019-05-31 01:28:55 -050047 ASSERT_EQ(pdr->composite_effecter_count, 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050048 state_effecter_possible_states* states =
49 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
50 ASSERT_EQ(states->state_set_id, 196);
51 ASSERT_EQ(states->possible_states_size, 1);
52 bitfield8_t bf1{};
53 bf1.byte = 2;
54 ASSERT_EQ(states->states[0].byte, bf1.byte);
55
George Liu1ec85d42020-02-12 16:05:32 +080056 const auto& [dbusMappings1, dbusValMaps1] =
57 handler.getDbusObjMaps(pdr->effecter_id);
58 ASSERT_EQ(dbusMappings1[0].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +053059
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050060 // Check second PDR
Deepak Kodihallic682fe22020-03-04 00:42:54 -060061 auto record2 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +080062 ASSERT_NE(record2, nullptr);
63 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050064
65 ASSERT_EQ(pdr->hdr.record_handle, 2);
66 ASSERT_EQ(pdr->hdr.version, 1);
67 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
68 ASSERT_EQ(pdr->hdr.record_change_num, 0);
69 ASSERT_EQ(pdr->hdr.length, 24);
70
71 ASSERT_EQ(pdr->terminus_handle, 0);
72 ASSERT_EQ(pdr->effecter_id, 2);
73 ASSERT_EQ(pdr->entity_type, 100);
74 ASSERT_EQ(pdr->entity_instance, 0);
75 ASSERT_EQ(pdr->container_id, 0);
76 ASSERT_EQ(pdr->effecter_semantic_id, 0);
77 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
78 ASSERT_EQ(pdr->has_description_pdr, false);
79 ASSERT_EQ(pdr->composite_effecter_count, 2);
80 states =
81 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
82 ASSERT_EQ(states->state_set_id, 197);
83 ASSERT_EQ(states->possible_states_size, 1);
84 bf1.byte = 2;
85 ASSERT_EQ(states->states[0].byte, bf1.byte);
86 states = reinterpret_cast<state_effecter_possible_states*>(
87 pdr->possible_states + sizeof(state_effecter_possible_states));
88 ASSERT_EQ(states->state_set_id, 198);
89 ASSERT_EQ(states->possible_states_size, 2);
90 bitfield8_t bf2[2];
91 bf2[0].byte = 38;
92 bf2[1].byte = 128;
93 ASSERT_EQ(states->states[0].byte, bf2[0].byte);
94 ASSERT_EQ(states->states[1].byte, bf2[1].byte);
Deepak Kodihallid0782962019-05-07 16:36:47 +053095
George Liu1ec85d42020-02-12 16:05:32 +080096 const auto& [dbusMappings2, dbusValMaps2] =
97 handler.getDbusObjMaps(pdr->effecter_id);
98 ASSERT_EQ(dbusMappings2[0].objectPath, "/foo/bar");
99 ASSERT_EQ(dbusMappings2[1].objectPath, "/foo/bar/baz");
Deepak Kodihallid0782962019-05-07 16:36:47 +0530100
George Liu1ec85d42020-02-12 16:05:32 +0800101 ASSERT_THROW(handler.getDbusObjMaps(0xDEAD), std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600102
103 pldm_pdr_destroy(inPDRRepo);
104 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500105}
106
George Liu456c9a22020-01-13 11:36:22 +0800107TEST(GeneratePDRByNumericEffecter, testGoodJson)
108{
109 auto inPDRRepo = pldm_pdr_init();
110 auto outPDRRepo = pldm_pdr_init();
111 Repo outRepo(outPDRRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530112 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr);
George Liu456c9a22020-01-13 11:36:22 +0800113 Repo inRepo(inPDRRepo);
114 getRepoByType(inRepo, outRepo, PLDM_NUMERIC_EFFECTER_PDR);
115
116 // 1 entries
117 ASSERT_EQ(outRepo.getRecordCount(), 1);
118
119 // Check first PDR
120 pdr_utils::PdrEntry e;
121 auto record = pdr::getRecordByHandle(outRepo, 3, e);
122 ASSERT_NE(record, nullptr);
123
124 pldm_numeric_effecter_value_pdr* pdr =
125 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
126 EXPECT_EQ(pdr->hdr.record_handle, 3);
127 EXPECT_EQ(pdr->hdr.version, 1);
128 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
129 EXPECT_EQ(pdr->hdr.record_change_num, 0);
130 EXPECT_EQ(pdr->hdr.length,
131 sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr));
132
133 EXPECT_EQ(pdr->effecter_id, 3);
134 EXPECT_EQ(pdr->effecter_data_size, 4);
135
136 const auto& [dbusMappings, dbusValMaps] =
137 handler.getDbusObjMaps(pdr->effecter_id);
138 EXPECT_EQ(dbusMappings[0].objectPath, "/foo/bar");
139 EXPECT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar");
140 EXPECT_EQ(dbusMappings[0].propertyName, "propertyName");
141 EXPECT_EQ(dbusMappings[0].propertyType, "uint64_t");
142
143 pldm_pdr_destroy(inPDRRepo);
144 pldm_pdr_destroy(outPDRRepo);
145}
146
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500147TEST(GeneratePDR, testNoJson)
148{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600149 auto pdrRepo = pldm_pdr_init();
150
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530151 ASSERT_THROW(Handler("./pdr_jsons/not_there", "./event_jsons/not_there",
152 pdrRepo, nullptr),
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500153 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600154
155 pldm_pdr_destroy(pdrRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500156}
157
158TEST(GeneratePDR, testMalformedJson)
159{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600160 auto inPDRRepo = pldm_pdr_init();
161 auto outPDRRepo = pldm_pdr_init();
162 Repo outRepo(outPDRRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530163 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
164 inPDRRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600165 Repo inRepo(inPDRRepo);
166 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
167
168 ASSERT_EQ(outRepo.getRecordCount(), 2);
George Liue53193f2020-02-24 09:23:26 +0800169 ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"),
Sampa Misraaa8ae722019-12-12 03:20:40 -0600170 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600171
172 pldm_pdr_destroy(inPDRRepo);
173 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500174}
Tom Joseph250c4752020-04-15 10:32:45 +0530175
176TEST(findStateEffecterId, goodJson)
177{
178 auto inPDRRepo = pldm_pdr_init();
179 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr);
180 uint16_t entityType = 33;
181 uint16_t entityInstance = 0;
182 uint16_t containerId = 0;
183 uint16_t stateSetId = 196;
184 auto effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
185 containerId, stateSetId);
186 ASSERT_EQ(effecterId, 1);
187 stateSetId = 300;
188 effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
189 containerId, stateSetId);
190 ASSERT_EQ(effecterId, PLDM_INVALID_EFFECTER_ID);
191 pldm_pdr_destroy(inPDRRepo);
192}