blob: e0a54bf20f2e14011e1e22d59b38e3263b5d51e2 [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"
George Liu36e81352020-07-01 14:40:30 +08005#include "mocked_utils.hpp"
6
7#include <sdbusplus/test/sdbus_mock.hpp>
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05008
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -05009#include <gtest/gtest.h>
10
11using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060012using namespace pldm::responder::platform;
13using namespace pldm::responder::pdr;
14using namespace pldm::responder::pdr_utils;
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050015
George Liu36e81352020-07-01 14:40:30 +080016using ::testing::_;
17using ::testing::Return;
18using ::testing::StrEq;
19
George Liu456c9a22020-01-13 11:36:22 +080020TEST(GeneratePDRByStateEffecter, testGoodJson)
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050021{
George Liu36e81352020-07-01 14:40:30 +080022 MockdBusHandler mockedUtils;
23 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
24 .Times(5)
25 .WillRepeatedly(Return("foo.bar"));
26
Deepak Kodihallic682fe22020-03-04 00:42:54 -060027 auto inPDRRepo = pldm_pdr_init();
28 auto outPDRRepo = pldm_pdr_init();
29 Repo outRepo(outPDRRepo);
George Liu36e81352020-07-01 14:40:30 +080030 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
31 "./event_jsons/good", inPDRRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060032 Repo inRepo(inPDRRepo);
33 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050034
35 // 2 entries
Deepak Kodihallic682fe22020-03-04 00:42:54 -060036 ASSERT_EQ(outRepo.getRecordCount(), 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050037
38 // Check first PDR
George Liue53193f2020-02-24 09:23:26 +080039 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060040 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +080041 ASSERT_NE(record1, nullptr);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050042 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +080043 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050044
45 ASSERT_EQ(pdr->hdr.record_handle, 1);
46 ASSERT_EQ(pdr->hdr.version, 1);
47 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
48 ASSERT_EQ(pdr->hdr.record_change_num, 0);
Sampa Misraa2fa0702019-05-31 01:28:55 -050049 ASSERT_EQ(pdr->hdr.length, 23);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050050
51 ASSERT_EQ(pdr->terminus_handle, 0);
52 ASSERT_EQ(pdr->effecter_id, 1);
53 ASSERT_EQ(pdr->entity_type, 33);
54 ASSERT_EQ(pdr->entity_instance, 0);
55 ASSERT_EQ(pdr->container_id, 0);
56 ASSERT_EQ(pdr->effecter_semantic_id, 0);
57 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
58 ASSERT_EQ(pdr->has_description_pdr, false);
Sampa Misraa2fa0702019-05-31 01:28:55 -050059 ASSERT_EQ(pdr->composite_effecter_count, 2);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050060 state_effecter_possible_states* states =
61 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
62 ASSERT_EQ(states->state_set_id, 196);
63 ASSERT_EQ(states->possible_states_size, 1);
64 bitfield8_t bf1{};
65 bf1.byte = 2;
66 ASSERT_EQ(states->states[0].byte, bf1.byte);
67
George Liu1ec85d42020-02-12 16:05:32 +080068 const auto& [dbusMappings1, dbusValMaps1] =
69 handler.getDbusObjMaps(pdr->effecter_id);
70 ASSERT_EQ(dbusMappings1[0].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +053071
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050072 // Check second PDR
Deepak Kodihallic682fe22020-03-04 00:42:54 -060073 auto record2 = pdr::getRecordByHandle(outRepo, 2, e);
George Liue53193f2020-02-24 09:23:26 +080074 ASSERT_NE(record2, nullptr);
75 pdr = reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -050076
77 ASSERT_EQ(pdr->hdr.record_handle, 2);
78 ASSERT_EQ(pdr->hdr.version, 1);
79 ASSERT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
80 ASSERT_EQ(pdr->hdr.record_change_num, 0);
81 ASSERT_EQ(pdr->hdr.length, 24);
82
83 ASSERT_EQ(pdr->terminus_handle, 0);
84 ASSERT_EQ(pdr->effecter_id, 2);
85 ASSERT_EQ(pdr->entity_type, 100);
86 ASSERT_EQ(pdr->entity_instance, 0);
87 ASSERT_EQ(pdr->container_id, 0);
88 ASSERT_EQ(pdr->effecter_semantic_id, 0);
89 ASSERT_EQ(pdr->effecter_init, PLDM_NO_INIT);
90 ASSERT_EQ(pdr->has_description_pdr, false);
91 ASSERT_EQ(pdr->composite_effecter_count, 2);
92 states =
93 reinterpret_cast<state_effecter_possible_states*>(pdr->possible_states);
94 ASSERT_EQ(states->state_set_id, 197);
95 ASSERT_EQ(states->possible_states_size, 1);
96 bf1.byte = 2;
97 ASSERT_EQ(states->states[0].byte, bf1.byte);
98 states = reinterpret_cast<state_effecter_possible_states*>(
99 pdr->possible_states + sizeof(state_effecter_possible_states));
100 ASSERT_EQ(states->state_set_id, 198);
101 ASSERT_EQ(states->possible_states_size, 2);
102 bitfield8_t bf2[2];
103 bf2[0].byte = 38;
104 bf2[1].byte = 128;
105 ASSERT_EQ(states->states[0].byte, bf2[0].byte);
106 ASSERT_EQ(states->states[1].byte, bf2[1].byte);
Deepak Kodihallid0782962019-05-07 16:36:47 +0530107
George Liu1ec85d42020-02-12 16:05:32 +0800108 const auto& [dbusMappings2, dbusValMaps2] =
109 handler.getDbusObjMaps(pdr->effecter_id);
110 ASSERT_EQ(dbusMappings2[0].objectPath, "/foo/bar");
George Liu36e81352020-07-01 14:40:30 +0800111 ASSERT_EQ(dbusMappings2[1].objectPath, "/foo/bar");
Deepak Kodihallid0782962019-05-07 16:36:47 +0530112
George Liu1ec85d42020-02-12 16:05:32 +0800113 ASSERT_THROW(handler.getDbusObjMaps(0xDEAD), std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600114
115 pldm_pdr_destroy(inPDRRepo);
116 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500117}
118
George Liu456c9a22020-01-13 11:36:22 +0800119TEST(GeneratePDRByNumericEffecter, testGoodJson)
120{
George Liu36e81352020-07-01 14:40:30 +0800121 MockdBusHandler mockedUtils;
122 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
123 .Times(5)
124 .WillRepeatedly(Return("foo.bar"));
125
George Liu456c9a22020-01-13 11:36:22 +0800126 auto inPDRRepo = pldm_pdr_init();
127 auto outPDRRepo = pldm_pdr_init();
128 Repo outRepo(outPDRRepo);
George Liu36e81352020-07-01 14:40:30 +0800129 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
130 inPDRRepo, nullptr, nullptr);
George Liu456c9a22020-01-13 11:36:22 +0800131 Repo inRepo(inPDRRepo);
132 getRepoByType(inRepo, outRepo, PLDM_NUMERIC_EFFECTER_PDR);
133
134 // 1 entries
135 ASSERT_EQ(outRepo.getRecordCount(), 1);
136
137 // Check first PDR
138 pdr_utils::PdrEntry e;
139 auto record = pdr::getRecordByHandle(outRepo, 3, e);
140 ASSERT_NE(record, nullptr);
141
142 pldm_numeric_effecter_value_pdr* pdr =
143 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
144 EXPECT_EQ(pdr->hdr.record_handle, 3);
145 EXPECT_EQ(pdr->hdr.version, 1);
146 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
147 EXPECT_EQ(pdr->hdr.record_change_num, 0);
148 EXPECT_EQ(pdr->hdr.length,
149 sizeof(pldm_numeric_effecter_value_pdr) - sizeof(pldm_pdr_hdr));
150
151 EXPECT_EQ(pdr->effecter_id, 3);
152 EXPECT_EQ(pdr->effecter_data_size, 4);
153
154 const auto& [dbusMappings, dbusValMaps] =
155 handler.getDbusObjMaps(pdr->effecter_id);
156 EXPECT_EQ(dbusMappings[0].objectPath, "/foo/bar");
157 EXPECT_EQ(dbusMappings[0].interface, "xyz.openbmc_project.Foo.Bar");
158 EXPECT_EQ(dbusMappings[0].propertyName, "propertyName");
159 EXPECT_EQ(dbusMappings[0].propertyType, "uint64_t");
160
161 pldm_pdr_destroy(inPDRRepo);
162 pldm_pdr_destroy(outPDRRepo);
163}
164
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500165TEST(GeneratePDR, testMalformedJson)
166{
George Liu36e81352020-07-01 14:40:30 +0800167 MockdBusHandler mockedUtils;
168 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
169 .Times(5)
170 .WillRepeatedly(Return("foo.bar"));
171
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600172 auto inPDRRepo = pldm_pdr_init();
173 auto outPDRRepo = pldm_pdr_init();
174 Repo outRepo(outPDRRepo);
George Liu36e81352020-07-01 14:40:30 +0800175 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
176 "./event_jsons/good", inPDRRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600177 Repo inRepo(inPDRRepo);
178 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
179
180 ASSERT_EQ(outRepo.getRecordCount(), 2);
George Liue53193f2020-02-24 09:23:26 +0800181 ASSERT_THROW(pdr_utils::readJson("./pdr_jsons/state_effecter/malformed"),
Sampa Misraaa8ae722019-12-12 03:20:40 -0600182 std::exception);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600183
184 pldm_pdr_destroy(inPDRRepo);
185 pldm_pdr_destroy(outPDRRepo);
Deepak Kodihallic6e8fb52019-05-02 08:35:31 -0500186}
Tom Joseph250c4752020-04-15 10:32:45 +0530187
188TEST(findStateEffecterId, goodJson)
189{
George Liu36e81352020-07-01 14:40:30 +0800190 MockdBusHandler mockedUtils;
191 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
192 .Times(5)
193 .WillRepeatedly(Return("foo.bar"));
194
Tom Joseph250c4752020-04-15 10:32:45 +0530195 auto inPDRRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +0800196 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
197 inPDRRepo, nullptr, nullptr);
Tom Joseph250c4752020-04-15 10:32:45 +0530198 uint16_t entityType = 33;
199 uint16_t entityInstance = 0;
200 uint16_t containerId = 0;
201 uint16_t stateSetId = 196;
202 auto effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
203 containerId, stateSetId);
204 ASSERT_EQ(effecterId, 1);
205 stateSetId = 300;
206 effecterId = findStateEffecterId(inPDRRepo, entityType, entityInstance,
207 containerId, stateSetId);
208 ASSERT_EQ(effecterId, PLDM_INVALID_EFFECTER_ID);
209 pldm_pdr_destroy(inPDRRepo);
210}