blob: f487d3b38b641b4b2b004bab1b88f9ce58f48842 [file] [log] [blame]
TOM JOSEPHd4d97a52020-03-23 14:36:34 +05301#include "libpldmresponder/event_parser.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05302#include "libpldmresponder/pdr.hpp"
George Liue53193f2020-02-24 09:23:26 +08003#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05304#include "libpldmresponder/platform.hpp"
George Liu64d26be2020-01-15 10:58:10 +08005#include "libpldmresponder/platform_numeric_effecter.hpp"
George Liu0d7aca82020-03-30 15:01:36 +08006#include "libpldmresponder/platform_state_effecter.hpp"
George Liu1e44c732020-02-28 20:20:06 +08007#include "mocked_utils.hpp"
8#include "utils.hpp"
Deepak Kodihalli557dfb02019-05-12 13:11:17 +05309
10#include <iostream>
11
George Liu1e44c732020-02-28 20:20:06 +080012using namespace pldm::utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053013using namespace pldm::responder;
Deepak Kodihallic682fe22020-03-04 00:42:54 -060014using namespace pldm::responder::platform;
15using namespace pldm::responder::pdr;
16using namespace pldm::responder::pdr_utils;
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053017
18TEST(getPDR, testGoodPath)
19{
20 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
21 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080022 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053023 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
24
George Liue53193f2020-02-24 09:23:26 +080025 struct pldm_get_pdr_req* request =
26 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
27 request->request_count = 100;
28
Deepak Kodihallic682fe22020-03-04 00:42:54 -060029 auto pdrRepo = pldm_pdr_init();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053030 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
31 pdrRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060032 Repo repo(pdrRepo);
33 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080034 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053035 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
36
George Liue53193f2020-02-24 09:23:26 +080037 struct pldm_get_pdr_resp* resp =
38 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
39 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
40 ASSERT_EQ(2, resp->next_record_handle);
41 ASSERT_EQ(true, resp->response_count != 0);
42
43 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
44 ASSERT_EQ(hdr->record_handle, 1);
45 ASSERT_EQ(hdr->version, 1);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060046
47 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053048}
49
50TEST(getPDR, testShortRead)
51{
52 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
53 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080054 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053055 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
56
George Liue53193f2020-02-24 09:23:26 +080057 struct pldm_get_pdr_req* request =
58 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
59 request->request_count = 1;
60
Deepak Kodihallic682fe22020-03-04 00:42:54 -060061 auto pdrRepo = pldm_pdr_init();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053062 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
63 pdrRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060064 Repo repo(pdrRepo);
65 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080066 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053067 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +080068 struct pldm_get_pdr_resp* resp =
69 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
70 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
71 ASSERT_EQ(1, resp->response_count);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060072 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053073}
74
75TEST(getPDR, testBadRecordHandle)
76{
77 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
78 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +080079 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053080 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
81
George Liue53193f2020-02-24 09:23:26 +080082 struct pldm_get_pdr_req* request =
83 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
84 request->record_handle = 100000;
85 request->request_count = 1;
86
Deepak Kodihallic682fe22020-03-04 00:42:54 -060087 auto pdrRepo = pldm_pdr_init();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +053088 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
89 pdrRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060090 Repo repo(pdrRepo);
91 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +080092 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053093 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
94
95 ASSERT_EQ(responsePtr->payload[0], PLDM_PLATFORM_INVALID_RECORD_HANDLE);
Deepak Kodihallic682fe22020-03-04 00:42:54 -060096
97 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +053098}
99
100TEST(getPDR, testNoNextRecord)
101{
102 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
103 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800104 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530105 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
106
George Liue53193f2020-02-24 09:23:26 +0800107 struct pldm_get_pdr_req* request =
108 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
109 request->record_handle = 1;
110
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600111 auto pdrRepo = pldm_pdr_init();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530112 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
113 pdrRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600114 Repo repo(pdrRepo);
115 ASSERT_EQ(repo.empty(), false);
George Liue53193f2020-02-24 09:23:26 +0800116 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530117 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800118 struct pldm_get_pdr_resp* resp =
119 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
120 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
121 ASSERT_EQ(2, resp->next_record_handle);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600122
123 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530124}
125
126TEST(getPDR, testFindPDR)
127{
128 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REQ_BYTES>
129 requestPayload{};
George Liue53193f2020-02-24 09:23:26 +0800130 auto req = reinterpret_cast<pldm_msg*>(requestPayload.data());
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530131 size_t requestPayloadLength = requestPayload.size() - sizeof(pldm_msg_hdr);
132
George Liue53193f2020-02-24 09:23:26 +0800133 struct pldm_get_pdr_req* request =
134 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
135 request->request_count = 100;
136
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600137 auto pdrRepo = pldm_pdr_init();
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530138 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
139 pdrRepo, 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
144 // Let's try to find a PDR of type stateEffecter (= 11) and entity type =
145 // 100
146 bool found = false;
147 uint32_t handle = 0; // start asking for PDRs from recordHandle 0
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530148 while (!found)
149 {
George Liue53193f2020-02-24 09:23:26 +0800150 request->record_handle = handle;
George Liue53193f2020-02-24 09:23:26 +0800151 auto response = handler.getPDR(req, requestPayloadLength);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530152 auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
George Liue53193f2020-02-24 09:23:26 +0800153 struct pldm_get_pdr_resp* resp =
154 reinterpret_cast<struct pldm_get_pdr_resp*>(responsePtr->payload);
155 ASSERT_EQ(PLDM_SUCCESS, resp->completion_code);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530156
George Liue53193f2020-02-24 09:23:26 +0800157 handle = resp->next_record_handle; // point to the next pdr in case
158 // current is not what we want
159
160 pldm_pdr_hdr* hdr = reinterpret_cast<pldm_pdr_hdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600161 std::cerr << "PDR next record handle " << handle << "\n";
George Liue53193f2020-02-24 09:23:26 +0800162 std::cerr << "PDR type " << hdr->type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530163 if (hdr->type == PLDM_STATE_EFFECTER_PDR)
164 {
165 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800166 reinterpret_cast<pldm_state_effecter_pdr*>(resp->record_data);
Sampa Misraaa8ae722019-12-12 03:20:40 -0600167 std::cerr << "PDR entity type " << pdr->entity_type << "\n";
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530168 if (pdr->entity_type == 100)
169 {
170 found = true;
171 // Rest of the PDR can be accessed as need be
172 break;
173 }
174 }
George Liue53193f2020-02-24 09:23:26 +0800175 if (!resp->next_record_handle) // no more records
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530176 {
177 break;
178 }
179 }
180 ASSERT_EQ(found, true);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600181
182 pldm_pdr_destroy(pdrRepo);
Deepak Kodihalli557dfb02019-05-12 13:11:17 +0530183}
Sampa Misraa2fa0702019-05-31 01:28:55 -0500184
Sampa Misraa2fa0702019-05-31 01:28:55 -0500185TEST(setStateEffecterStatesHandler, testGoodRequest)
186{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600187 auto inPDRRepo = pldm_pdr_init();
188 auto outPDRRepo = pldm_pdr_init();
189 Repo outRepo(outPDRRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530190 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
191 inPDRRepo, nullptr);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600192 Repo inRepo(inPDRRepo);
193 getRepoByType(inRepo, outRepo, PLDM_STATE_EFFECTER_PDR);
George Liue53193f2020-02-24 09:23:26 +0800194 pdr_utils::PdrEntry e;
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600195 auto record1 = pdr::getRecordByHandle(outRepo, 1, e);
George Liue53193f2020-02-24 09:23:26 +0800196 ASSERT_NE(record1, nullptr);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500197 pldm_state_effecter_pdr* pdr =
George Liue53193f2020-02-24 09:23:26 +0800198 reinterpret_cast<pldm_state_effecter_pdr*>(e.data);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500199 EXPECT_EQ(pdr->hdr.type, PLDM_STATE_EFFECTER_PDR);
200
201 std::vector<set_effecter_state_field> stateField;
202 stateField.push_back({PLDM_REQUEST_SET, 1});
203 stateField.push_back({PLDM_REQUEST_SET, 1});
George Liu1ec85d42020-02-12 16:05:32 +0800204 std::string value = "xyz.openbmc_project.Foo.Bar.V1";
George Liu1e44c732020-02-28 20:20:06 +0800205 PropertyValue propertyValue = value;
Sampa Misraa2fa0702019-05-31 01:28:55 -0500206
207 MockdBusHandler handlerObj;
George Liu1ec85d42020-02-12 16:05:32 +0800208 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
209 "propertyName", "string"};
George Liu1e44c732020-02-28 20:20:06 +0800210
211 EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
Sampa Misraa2fa0702019-05-31 01:28:55 -0500212 .Times(2);
George Liu0d7aca82020-03-30 15:01:36 +0800213 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
214 MockdBusHandler, Handler>(handlerObj, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500215 ASSERT_EQ(rc, 0);
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600216
217 pldm_pdr_destroy(inPDRRepo);
218 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500219}
220
221TEST(setStateEffecterStatesHandler, testBadRequest)
222{
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600223 auto inPDRRepo = pldm_pdr_init();
224 auto outPDRRepo = pldm_pdr_init();
225 Repo outRepo(outPDRRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530226 Handler handler("./pdr_jsons/state_effecter/good", "./event_jsons/good",
227 inPDRRepo, 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, 3});
239 stateField.push_back({PLDM_REQUEST_SET, 4});
240
241 MockdBusHandler handlerObj;
George Liu0d7aca82020-03-30 15:01:36 +0800242 auto rc = platform_state_effecter::setStateEffecterStatesHandler<
243 MockdBusHandler, Handler>(handlerObj, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500244 ASSERT_EQ(rc, PLDM_PLATFORM_SET_EFFECTER_UNSUPPORTED_SENSORSTATE);
245
George Liu0d7aca82020-03-30 15:01:36 +0800246 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
247 Handler>(
248 handlerObj, handler, 0x9, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500249 ASSERT_EQ(rc, PLDM_PLATFORM_INVALID_EFFECTER_ID);
250
251 stateField.push_back({PLDM_REQUEST_SET, 4});
George Liu0d7aca82020-03-30 15:01:36 +0800252 rc = platform_state_effecter::setStateEffecterStatesHandler<MockdBusHandler,
253 Handler>(
254 handlerObj, handler, 0x1, stateField);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500255 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
256
Deepak Kodihallic682fe22020-03-04 00:42:54 -0600257 pldm_pdr_destroy(inPDRRepo);
258 pldm_pdr_destroy(outPDRRepo);
Sampa Misraa2fa0702019-05-31 01:28:55 -0500259}
George Liu64d26be2020-01-15 10:58:10 +0800260
261TEST(setNumericEffecterValueHandler, testGoodRequest)
262{
263 auto inPDRRepo = pldm_pdr_init();
264 auto numericEffecterPdrRepo = pldm_pdr_init();
265 Repo numericEffecterPDRs(numericEffecterPdrRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530266 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800267 Repo inRepo(inPDRRepo);
268 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
269
270 pdr_utils::PdrEntry e;
271 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
272 ASSERT_NE(record3, nullptr);
273
274 pldm_numeric_effecter_value_pdr* pdr =
275 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
276 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
277
278 uint16_t effecterId = 3;
279 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
280 PropertyValue propertyValue = static_cast<uint32_t>(effecterValue);
281
282 MockdBusHandler handlerObj;
283 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
284 "propertyName", "uint64_t"};
285 EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
286 .Times(1);
287
288 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
289 MockdBusHandler, Handler>(
290 handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
291 reinterpret_cast<uint8_t*>(&effecterValue), 4);
292 ASSERT_EQ(rc, 0);
293
294 pldm_pdr_destroy(inPDRRepo);
295 pldm_pdr_destroy(numericEffecterPdrRepo);
296}
297
298TEST(setNumericEffecterValueHandler, testBadRequest)
299{
300 auto inPDRRepo = pldm_pdr_init();
301 auto numericEffecterPdrRepo = pldm_pdr_init();
302 Repo numericEffecterPDRs(numericEffecterPdrRepo);
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530303 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800304 Repo inRepo(inPDRRepo);
305 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
306
307 pdr_utils::PdrEntry e;
308 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
309 ASSERT_NE(record3, nullptr);
310
311 pldm_numeric_effecter_value_pdr* pdr =
312 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
313 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
314
315 uint16_t effecterId = 3;
316 uint64_t effecterValue = 9876543210;
317 MockdBusHandler handlerObj;
318 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
319 MockdBusHandler, Handler>(
320 handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
321 reinterpret_cast<uint8_t*>(&effecterValue), 3);
322 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
323
324 pldm_pdr_destroy(inPDRRepo);
325 pldm_pdr_destroy(numericEffecterPdrRepo);
326}
Tom Josephb4268602020-04-17 17:20:45 +0530327
328TEST(parseStateSensor, allScenarios)
329{
330 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
331 // State Set ID - Operational Running Status(11), Supported States - 3,4
332 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
333 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
334 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
335 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
336
337 const auto& [terminusHandle1, sensorID1, sensorInfo1] =
338 parseStateSensorPDR(sample1PDR);
339 const auto& [containerID1, entityType1, entityInstance1] =
340 std::get<0>(sensorInfo1);
341 const auto& states1 = std::get<1>(sensorInfo1);
342 CompositeSensorStates statesCmp1{{3u, 4u}};
343
344 ASSERT_EQ(le16toh(terminusHandle1), 0u);
345 ASSERT_EQ(le16toh(sensorID1), 1u);
346 ASSERT_EQ(le16toh(containerID1), 0u);
347 ASSERT_EQ(le16toh(entityType1), 67u);
348 ASSERT_EQ(le16toh(entityInstance1), 1u);
349 ASSERT_EQ(states1, statesCmp1);
350
351 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
352 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
353 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
354 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
355 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
356 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
357
358 const auto& [terminusHandle2, sensorID2, sensorInfo2] =
359 parseStateSensorPDR(sample2PDR);
360 const auto& [containerID2, entityType2, entityInstance2] =
361 std::get<0>(sensorInfo2);
362 const auto& states2 = std::get<1>(sensorInfo2);
363 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
364
365 ASSERT_EQ(le16toh(terminusHandle2), 0u);
366 ASSERT_EQ(le16toh(sensorID2), 2u);
367 ASSERT_EQ(le16toh(containerID2), 0u);
368 ASSERT_EQ(le16toh(entityType2), 31u);
369 ASSERT_EQ(le16toh(entityInstance2), 1u);
370 ASSERT_EQ(states2, statesCmp2);
371
372 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
373 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
374 // Supported States - 1,2, State Set ID - Configuration State(15),
375 // Supported States - 1,2,3,4
376 std::vector<uint8_t> sample3PDR{
377 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
378 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
379 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
380
381 const auto& [terminusHandle3, sensorID3, sensorInfo3] =
382 parseStateSensorPDR(sample3PDR);
383 const auto& [containerID3, entityType3, entityInstance3] =
384 std::get<0>(sensorInfo3);
385 const auto& states3 = std::get<1>(sensorInfo3);
386 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
387
388 ASSERT_EQ(le16toh(terminusHandle3), 0u);
389 ASSERT_EQ(le16toh(sensorID3), 3u);
390 ASSERT_EQ(le16toh(containerID3), 1u);
391 ASSERT_EQ(le16toh(entityType3), 33u);
392 ASSERT_EQ(le16toh(entityInstance3), 2u);
393 ASSERT_EQ(states3, statesCmp3);
394}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530395
396TEST(StateSensorHandler, allScenarios)
397{
398 using namespace pldm::responder::events;
399
400 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed1"),
401 std::exception);
402 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed2"),
403 std::exception);
404
405 StateSensorHandler handler{"./event_jsons/good"};
406 constexpr uint8_t eventState0 = 0;
407 constexpr uint8_t eventState1 = 1;
408 constexpr uint8_t eventState2 = 2;
409 constexpr uint8_t eventState3 = 3;
410
411 // Event Entry 1
412 {
413 StateSensorEntry entry{1, 64, 1, 0};
414 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
415 DBusMapping mapping{"/xyz/abc/def",
416 "xyz.openbmc_project.example1.value", "value1",
417 "string"};
418 ASSERT_EQ(mapping == dbusMapping, true);
419
420 const auto& propValue0 = eventStateMap.at(eventState0);
421 const auto& propValue1 = eventStateMap.at(eventState1);
422 const auto& propValue2 = eventStateMap.at(eventState2);
423 PropertyValue value0{std::in_place_type<std::string>,
424 "xyz.openbmc_project.State.Normal"};
425 PropertyValue value1{std::in_place_type<std::string>,
426 "xyz.openbmc_project.State.Critical"};
427 PropertyValue value2{std::in_place_type<std::string>,
428 "xyz.openbmc_project.State.Fatal"};
429 ASSERT_EQ(value0 == propValue0, true);
430 ASSERT_EQ(value1 == propValue1, true);
431 ASSERT_EQ(value2 == propValue2, true);
432 }
433
434 // Event Entry 2
435 {
436 StateSensorEntry entry{1, 64, 1, 1};
437 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
438 DBusMapping mapping{"/xyz/abc/def",
439 "xyz.openbmc_project.example2.value", "value2",
440 "uint8_t"};
441 ASSERT_EQ(mapping == dbusMapping, true);
442
443 const auto& propValue0 = eventStateMap.at(eventState2);
444 const auto& propValue1 = eventStateMap.at(eventState3);
445 PropertyValue value0{std::in_place_type<uint8_t>, 9};
446 PropertyValue value1{std::in_place_type<uint8_t>, 10};
447 ASSERT_EQ(value0 == propValue0, true);
448 ASSERT_EQ(value1 == propValue1, true);
449 }
450
451 // Event Entry 3
452 {
453 StateSensorEntry entry{2, 67, 2, 0};
454 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
455 DBusMapping mapping{"/xyz/abc/ghi",
456 "xyz.openbmc_project.example3.value", "value3",
457 "bool"};
458 ASSERT_EQ(mapping == dbusMapping, true);
459
460 const auto& propValue0 = eventStateMap.at(eventState0);
461 const auto& propValue1 = eventStateMap.at(eventState1);
462 PropertyValue value0{std::in_place_type<bool>, false};
463 PropertyValue value1{std::in_place_type<bool>, true};
464 ASSERT_EQ(value0 == propValue0, true);
465 ASSERT_EQ(value1 == propValue1, true);
466 }
467
468 // Invalid Entry
469 {
470 StateSensorEntry entry{0, 0, 0, 0};
471 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
472 }
473}