blob: 22ededa8e6290a67ce0061800eb2e41711229b42 [file] [log] [blame]
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05001#include "common/utils.hpp"
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05302#include "libpldmresponder/event_parser.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05303#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08004#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05305#include "libpldmresponder/platform.hpp"
George Liu64d26be2020-01-15 10:58:10 +08006#include "libpldmresponder/platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +08007#include "libpldmresponder/platform_state_effecter.hpp"
George Liu1e44c732020-02-28 20:20:06 +08008#include "mocked_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05309
George Liu36e81352020-07-01 14:40:30 +080010#include <sdbusplus/test/sdbus_mock.hpp>
11
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053012#include <iostream>
13
George Liu1e44c732020-02-28 20:20:06 +080014using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053015using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060016using namespace pldm::responder::platform;
17using namespace pldm::responder::pdr;
18using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053019
George Liu36e81352020-07-01 14:40:30 +080020using ::testing::_;
21using ::testing::Return;
22using ::testing::StrEq;
23
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053024TEST(getPDR, testGoodPath)
25{
26 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
27 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080028 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053029 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
30
George Liue53193f2020-02-24 09:23:26 +080031 struct pldm_get_pdr_req* request =
32 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
33 request->request_count = 100;
34
George Liu36e81352020-07-01 14:40:30 +080035 MockdBusHandler mockedUtils;
36 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
37 .Times(5)
38 .WillRepeatedly(Return("foo.bar"));
39
Deepak Kodihallic682fe22020-03-04 00:42:54 -060040 auto pdrRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +080041 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
42 "./event_jsons/good", pdrRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060043 Repo repo(pdrRepo);
44 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080045 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053046 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
47
George Liue53193f2020-02-24 09:23:26 +080048 struct pldm_get_pdr_resp* resp =
49 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
50 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
51 ASSERT_EQ(2, resp->next_record_handle);
52 ASSERT_EQ(true, resp->response_count != 0);
53
54 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
55 ASSERT_EQ(hdr->record_handle, 1);
56 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060057
58 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053059}
60
61TEST(getPDR, testShortRead)
62{
63 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
64 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080065 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053066 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
67
George Liue53193f2020-02-24 09:23:26 +080068 struct pldm_get_pdr_req* request =
69 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
70 request->request_count = 1;
71
George Liu36e81352020-07-01 14:40:30 +080072 MockdBusHandler mockedUtils;
73 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
74 .Times(5)
75 .WillRepeatedly(Return("foo.bar"));
76
Deepak Kodihallic682fe22020-03-04 00:42:54 -060077 auto pdrRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +080078 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
79 "./event_jsons/good", pdrRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060080 Repo repo(pdrRepo);
81 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080082 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053083 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080084 struct pldm_get_pdr_resp* resp =
85 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
86 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
87 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060088 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053089}
90
91TEST(getPDR, testBadRecordHandle)
92{
93 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
94 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080095 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053096 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
97
George Liue53193f2020-02-24 09:23:26 +080098 struct pldm_get_pdr_req* request =
99 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
100 request->record_handle = 100000;
101 request->request_count = 1;
102
George Liu36e81352020-07-01 14:40:30 +0800103 MockdBusHandler mockedUtils;
104 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
105 .Times(5)
106 .WillRepeatedly(Return("foo.bar"));
107
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600108 auto pdrRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +0800109 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
110 "./event_jsons/good", pdrRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600111 Repo repo(pdrRepo);
112 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800113 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530114 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
115
116 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600117
118 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530119}
120
121TEST(getPDR, testNoNextRecord)
122{
123 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
124 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800125 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530126 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
127
George Liue53193f2020-02-24 09:23:26 +0800128 struct pldm_get_pdr_req* request =
129 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
130 request->record_handle = 1;
131
George Liu36e81352020-07-01 14:40:30 +0800132 MockdBusHandler mockedUtils;
133 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
134 .Times(5)
135 .WillRepeatedly(Return("foo.bar"));
136
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600137 auto pdrRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +0800138 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
139 "./event_jsons/good", pdrRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600140 Repo repo(pdrRepo);
141 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800142 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530143 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800144 struct pldm_get_pdr_resp* resp =
145 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
146 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
147 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600148
149 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530150}
151
152TEST(getPDR, testFindPDR)
153{
154 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
155 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800156 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530157 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
158
George Liue53193f2020-02-24 09:23:26 +0800159 struct pldm_get_pdr_req* request =
160 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
161 request->request_count = 100;
162
George Liu36e81352020-07-01 14:40:30 +0800163 MockdBusHandler mockedUtils;
164 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
165 .Times(5)
166 .WillRepeatedly(Return("foo.bar"));
167
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600168 auto pdrRepo = pldm_pdr_init();
George Liu36e81352020-07-01 14:40:30 +0800169 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
170 "./event_jsons/good", pdrRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600171 Repo repo(pdrRepo);
172 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800173 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530174
175 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
176 // 100
177 bool found = false;
178 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530179 while (!found)
180 {
George Liue53193f2020-02-24 09:23:26 +0800181 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800182 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530183 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800184 struct pldm_get_pdr_resp* resp =
185 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
186 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530187
George Liue53193f2020-02-24 09:23:26 +0800188 handle = resp->next_record_handle; // point to the next pdr in case
189 // current is not what we want
190
191 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600192 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800193 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530194 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
195 {
196 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800197 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600198 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530199 if (pdr->entity_type == 100)
200 {
201 found = true;
202 // Rest of the PDR can be accessed as need be
203 break;
204 }
205 }
George Liue53193f2020-02-24 09:23:26 +0800206 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530207 {
208 break;
209 }
210 }
211 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600212
213 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530214}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500215
Sampa Misraa2fa0702019-05-31 01:28:55 -0500216TEST(setStateEffecterStatesHandler, testGoodRequest)
217{
George Liu36e81352020-07-01 14:40:30 +0800218 MockdBusHandler mockedUtils;
219 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
220 .Times(5)
221 .WillRepeatedly(Return("foo.bar"));
222
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600223 auto inPDRRepo = pldm_pdr_init();
224 auto outPDRRepo = pldm_pdr_init();
225 Repo outRepo(outPDRRepo);
George Liu36e81352020-07-01 14:40:30 +0800226 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
227 "./event_jsons/good", inPDRRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600228 Repo inRepo(inPDRRepo);
229 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800230 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600231 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +0800232 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500233 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800234 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500235 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
236
237 std::vector<set_effecter_state_field> stateField;
238 stateField.push_back({PLDM_REQUEST_SET, 1});
239 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800240 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800241 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500242
George Liu1ec85d42020-02-12 16:05:32 +0800243 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
244 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800245
George Liu36e81352020-07-01 14:40:30 +0800246 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500247 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800248 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800249 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500250 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600251
252 pldm_pdr_destroy(inPDRRepo);
253 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500254}
255
256TEST(setStateEffecterStatesHandler, testBadRequest)
257{
George Liu36e81352020-07-01 14:40:30 +0800258 MockdBusHandler mockedUtils;
259 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
260 .Times(5)
261 .WillRepeatedly(Return("foo.bar"));
262
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600263 auto inPDRRepo = pldm_pdr_init();
264 auto outPDRRepo = pldm_pdr_init();
265 Repo outRepo(outPDRRepo);
George Liu36e81352020-07-01 14:40:30 +0800266 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good",
267 "./event_jsons/good", inPDRRepo, nullptr, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600268 Repo inRepo(inPDRRepo);
269 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800270 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600271 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +0800272 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500273 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800274 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500275 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
276
277 std::vector<set_effecter_state_field> stateField;
278 stateField.push_back({PLDM_REQUEST_SET, 3});
279 stateField.push_back({PLDM_REQUEST_SET, 4});
280
George Liu0d7aca82020-03-30 15:01:36 +0800281 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
George Liu36e81352020-07-01 14:40:30 +0800282 MockdBusHandler, Handler>(mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500283 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
284
George Liu0d7aca82020-03-30 15:01:36 +0800285 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
286 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800287 mockedUtils, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500288 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
289
290 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800291 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
292 Handler>(
George Liu36e81352020-07-01 14:40:30 +0800293 mockedUtils, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500294 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
295
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600296 pldm_pdr_destroy(inPDRRepo);
297 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500298}
George Liu64d26be2020-01-15 10:58:10 +0800299
300TEST(setNumericEffecterValueHandler, testGoodRequest)
301{
George Liu36e81352020-07-01 14:40:30 +0800302 MockdBusHandler mockedUtils;
303 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
304 .Times(5)
305 .WillRepeatedly(Return("foo.bar"));
306
George Liu64d26be2020-01-15 10:58:10 +0800307 auto inPDRRepo = pldm_pdr_init();
308 auto numericEffecterPdrRepo = pldm_pdr_init();
309 Repo numericEffecterPDRs(numericEffecterPdrRepo);
George Liu36e81352020-07-01 14:40:30 +0800310 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
311 inPDRRepo, nullptr, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800312 Repo inRepo(inPDRRepo);
313 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
314
315 pdr_utils::PdrEntry e;
316 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
317 ASSERT_NE(record3, nullptr);
318
319 pldm_numeric_effecter_value_pdr* pdr =
320 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
321 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
322
323 uint16_t effecterId = 3;
324 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
325 PropertyValue propertyValue = static_cast<uint32_t>(effecterValue);
326
George Liu64d26be2020-01-15 10:58:10 +0800327 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
328 "propertyName", "uint64_t"};
George Liu36e81352020-07-01 14:40:30 +0800329 EXPECT_CALL(mockedUtils, setDbusProperty(dbusMapping, propertyValue))
George Liu64d26be2020-01-15 10:58:10 +0800330 .Times(1);
331
332 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
333 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800334 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
George Liu64d26be2020-01-15 10:58:10 +0800335 reinterpret_cast<uint8_t*>(&effecterValue), 4);
336 ASSERT_EQ(rc, 0);
337
338 pldm_pdr_destroy(inPDRRepo);
339 pldm_pdr_destroy(numericEffecterPdrRepo);
340}
341
342TEST(setNumericEffecterValueHandler, testBadRequest)
343{
George Liu36e81352020-07-01 14:40:30 +0800344 MockdBusHandler mockedUtils;
345 EXPECT_CALL(mockedUtils, getService(StrEq("/foo/bar"), _))
346 .Times(5)
347 .WillRepeatedly(Return("foo.bar"));
348
George Liu64d26be2020-01-15 10:58:10 +0800349 auto inPDRRepo = pldm_pdr_init();
350 auto numericEffecterPdrRepo = pldm_pdr_init();
351 Repo numericEffecterPDRs(numericEffecterPdrRepo);
George Liu36e81352020-07-01 14:40:30 +0800352 Handler handler(mockedUtils, "./pdr_jsons/state_effecter/good", "",
353 inPDRRepo, nullptr, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800354 Repo inRepo(inPDRRepo);
355 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
356
357 pdr_utils::PdrEntry e;
358 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
359 ASSERT_NE(record3, nullptr);
360
361 pldm_numeric_effecter_value_pdr* pdr =
362 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
363 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
364
365 uint16_t effecterId = 3;
366 uint64_t effecterValue = 9876543210;
George Liu64d26be2020-01-15 10:58:10 +0800367 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
368 MockdBusHandler, Handler>(
George Liu36e81352020-07-01 14:40:30 +0800369 mockedUtils, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
George Liu64d26be2020-01-15 10:58:10 +0800370 reinterpret_cast<uint8_t*>(&effecterValue), 3);
371 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
372
373 pldm_pdr_destroy(inPDRRepo);
374 pldm_pdr_destroy(numericEffecterPdrRepo);
375}
Tom Josephb4268602020-04-17 17:20:45 +0530376
377TEST(parseStateSensor, allScenarios)
378{
379 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
380 // State Set ID - Operational Running Status(11), Supported States - 3,4
381 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
382 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
383 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
384 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
385
386 const auto& [terminusHandle1, sensorID1, sensorInfo1] =
387 parseStateSensorPDR(sample1PDR);
388 const auto& [containerID1, entityType1, entityInstance1] =
389 std::get<0>(sensorInfo1);
390 const auto& states1 = std::get<1>(sensorInfo1);
391 CompositeSensorStates statesCmp1{{3u, 4u}};
392
393 ASSERT_EQ(le16toh(terminusHandle1), 0u);
394 ASSERT_EQ(le16toh(sensorID1), 1u);
395 ASSERT_EQ(le16toh(containerID1), 0u);
396 ASSERT_EQ(le16toh(entityType1), 67u);
397 ASSERT_EQ(le16toh(entityInstance1), 1u);
398 ASSERT_EQ(states1, statesCmp1);
399
400 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
401 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
402 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
403 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
404 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
405 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
406
407 const auto& [terminusHandle2, sensorID2, sensorInfo2] =
408 parseStateSensorPDR(sample2PDR);
409 const auto& [containerID2, entityType2, entityInstance2] =
410 std::get<0>(sensorInfo2);
411 const auto& states2 = std::get<1>(sensorInfo2);
412 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
413
414 ASSERT_EQ(le16toh(terminusHandle2), 0u);
415 ASSERT_EQ(le16toh(sensorID2), 2u);
416 ASSERT_EQ(le16toh(containerID2), 0u);
417 ASSERT_EQ(le16toh(entityType2), 31u);
418 ASSERT_EQ(le16toh(entityInstance2), 1u);
419 ASSERT_EQ(states2, statesCmp2);
420
421 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
422 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
423 // Supported States - 1,2, State Set ID - Configuration State(15),
424 // Supported States - 1,2,3,4
425 std::vector<uint8_t> sample3PDR{
426 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
427 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
428 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
429
430 const auto& [terminusHandle3, sensorID3, sensorInfo3] =
431 parseStateSensorPDR(sample3PDR);
432 const auto& [containerID3, entityType3, entityInstance3] =
433 std::get<0>(sensorInfo3);
434 const auto& states3 = std::get<1>(sensorInfo3);
435 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
436
437 ASSERT_EQ(le16toh(terminusHandle3), 0u);
438 ASSERT_EQ(le16toh(sensorID3), 3u);
439 ASSERT_EQ(le16toh(containerID3), 1u);
440 ASSERT_EQ(le16toh(entityType3), 33u);
441 ASSERT_EQ(le16toh(entityInstance3), 2u);
442 ASSERT_EQ(states3, statesCmp3);
443}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530444
445TEST(StateSensorHandler, allScenarios)
446{
447 using namespace pldm::responder::events;
448
449 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed1"),
450 std::exception);
451 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed2"),
452 std::exception);
453
454 StateSensorHandler handler{"./event_jsons/good"};
455 constexpr uint8_t eventState0 = 0;
456 constexpr uint8_t eventState1 = 1;
457 constexpr uint8_t eventState2 = 2;
458 constexpr uint8_t eventState3 = 3;
459
460 // Event Entry 1
461 {
462 StateSensorEntry entry{1, 64, 1, 0};
463 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
464 DBusMapping mapping{"/xyz/abc/def",
465 "xyz.openbmc_project.example1.value", "value1",
466 "string"};
467 ASSERT_EQ(mapping == dbusMapping, true);
468
469 const auto& propValue0 = eventStateMap.at(eventState0);
470 const auto& propValue1 = eventStateMap.at(eventState1);
471 const auto& propValue2 = eventStateMap.at(eventState2);
472 PropertyValue value0{std::in_place_type<std::string>,
473 "xyz.openbmc_project.State.Normal"};
474 PropertyValue value1{std::in_place_type<std::string>,
475 "xyz.openbmc_project.State.Critical"};
476 PropertyValue value2{std::in_place_type<std::string>,
477 "xyz.openbmc_project.State.Fatal"};
478 ASSERT_EQ(value0 == propValue0, true);
479 ASSERT_EQ(value1 == propValue1, true);
480 ASSERT_EQ(value2 == propValue2, true);
481 }
482
483 // Event Entry 2
484 {
485 StateSensorEntry entry{1, 64, 1, 1};
486 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
487 DBusMapping mapping{"/xyz/abc/def",
488 "xyz.openbmc_project.example2.value", "value2",
489 "uint8_t"};
490 ASSERT_EQ(mapping == dbusMapping, true);
491
492 const auto& propValue0 = eventStateMap.at(eventState2);
493 const auto& propValue1 = eventStateMap.at(eventState3);
494 PropertyValue value0{std::in_place_type<uint8_t>, 9};
495 PropertyValue value1{std::in_place_type<uint8_t>, 10};
496 ASSERT_EQ(value0 == propValue0, true);
497 ASSERT_EQ(value1 == propValue1, true);
498 }
499
500 // Event Entry 3
501 {
502 StateSensorEntry entry{2, 67, 2, 0};
503 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
504 DBusMapping mapping{"/xyz/abc/ghi",
505 "xyz.openbmc_project.example3.value", "value3",
506 "bool"};
507 ASSERT_EQ(mapping == dbusMapping, true);
508
509 const auto& propValue0 = eventStateMap.at(eventState0);
510 const auto& propValue1 = eventStateMap.at(eventState1);
511 PropertyValue value0{std::in_place_type<bool>, false};
512 PropertyValue value1{std::in_place_type<bool>, true};
513 ASSERT_EQ(value0 == propValue0, true);
514 ASSERT_EQ(value1 == propValue1, true);
515 }
516
517 // Invalid Entry
518 {
519 StateSensorEntry entry{0, 0, 0, 0};
520 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
521 }
522}