blob: 60df7bf031ece282d56f527a56ee5f40fed5726d [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
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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +053031 pdrRepo, nullptr, 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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +053063 pdrRepo, nullptr, 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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +053089 pdrRepo, nullptr, 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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530113 pdrRepo, nullptr, 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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530139 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
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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530191 inPDRRepo, nullptr, 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",
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530227 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, 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 Joseph33e9c7e2020-06-11 22:09:52 +0530266 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr,
267 nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800268 Repo inRepo(inPDRRepo);
269 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
270
271 pdr_utils::PdrEntry e;
272 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
273 ASSERT_NE(record3, nullptr);
274
275 pldm_numeric_effecter_value_pdr* pdr =
276 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
277 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
278
279 uint16_t effecterId = 3;
280 uint32_t effecterValue = 2100000000; // 2036-07-18 21:20:00
281 PropertyValue propertyValue = static_cast<uint32_t>(effecterValue);
282
283 MockdBusHandler handlerObj;
284 DBusMapping dbusMapping{"/foo/bar", "xyz.openbmc_project.Foo.Bar",
285 "propertyName", "uint64_t"};
286 EXPECT_CALL(handlerObj, setDbusProperty(dbusMapping, propertyValue))
287 .Times(1);
288
289 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
290 MockdBusHandler, Handler>(
291 handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_UINT32,
292 reinterpret_cast<uint8_t*>(&effecterValue), 4);
293 ASSERT_EQ(rc, 0);
294
295 pldm_pdr_destroy(inPDRRepo);
296 pldm_pdr_destroy(numericEffecterPdrRepo);
297}
298
299TEST(setNumericEffecterValueHandler, testBadRequest)
300{
301 auto inPDRRepo = pldm_pdr_init();
302 auto numericEffecterPdrRepo = pldm_pdr_init();
303 Repo numericEffecterPDRs(numericEffecterPdrRepo);
Tom Joseph33e9c7e2020-06-11 22:09:52 +0530304 Handler handler("./pdr_jsons/state_effecter/good", "", inPDRRepo, nullptr,
305 nullptr);
George Liu64d26be2020-01-15 10:58:10 +0800306 Repo inRepo(inPDRRepo);
307 getRepoByType(inRepo, numericEffecterPDRs, PLDM_NUMERIC_EFFECTER_PDR);
308
309 pdr_utils::PdrEntry e;
310 auto record3 = pdr::getRecordByHandle(numericEffecterPDRs, 3, e);
311 ASSERT_NE(record3, nullptr);
312
313 pldm_numeric_effecter_value_pdr* pdr =
314 reinterpret_cast<pldm_numeric_effecter_value_pdr*>(e.data);
315 EXPECT_EQ(pdr->hdr.type, PLDM_NUMERIC_EFFECTER_PDR);
316
317 uint16_t effecterId = 3;
318 uint64_t effecterValue = 9876543210;
319 MockdBusHandler handlerObj;
320 auto rc = platform_numeric_effecter::setNumericEffecterValueHandler<
321 MockdBusHandler, Handler>(
322 handlerObj, handler, effecterId, PLDM_EFFECTER_DATA_SIZE_SINT32,
323 reinterpret_cast<uint8_t*>(&effecterValue), 3);
324 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
325
326 pldm_pdr_destroy(inPDRRepo);
327 pldm_pdr_destroy(numericEffecterPdrRepo);
328}
Tom Josephb4268602020-04-17 17:20:45 +0530329
330TEST(parseStateSensor, allScenarios)
331{
332 // Sample state sensor with SensorID - 1, EntityType - Processor Module(67)
333 // State Set ID - Operational Running Status(11), Supported States - 3,4
334 std::vector<uint8_t> sample1PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
335 0x00, 0x17, 0x00, 0x00, 0x00, 0x01, 0x00,
336 0x43, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
337 0x00, 0x01, 0x0b, 0x00, 0x01, 0x18};
338
339 const auto& [terminusHandle1, sensorID1, sensorInfo1] =
340 parseStateSensorPDR(sample1PDR);
341 const auto& [containerID1, entityType1, entityInstance1] =
342 std::get<0>(sensorInfo1);
343 const auto& states1 = std::get<1>(sensorInfo1);
344 CompositeSensorStates statesCmp1{{3u, 4u}};
345
346 ASSERT_EQ(le16toh(terminusHandle1), 0u);
347 ASSERT_EQ(le16toh(sensorID1), 1u);
348 ASSERT_EQ(le16toh(containerID1), 0u);
349 ASSERT_EQ(le16toh(entityType1), 67u);
350 ASSERT_EQ(le16toh(entityInstance1), 1u);
351 ASSERT_EQ(states1, statesCmp1);
352
353 // Sample state sensor with SensorID - 2, EntityType - System Firmware(31)
354 // State Set ID - Availability(2), Supported States - 3,4,9,10,11,13
355 std::vector<uint8_t> sample2PDR{0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
356 0x00, 0x17, 0x00, 0x00, 0x00, 0x02, 0x00,
357 0x1F, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
358 0x00, 0x01, 0x02, 0x00, 0x02, 0x18, 0x2E};
359
360 const auto& [terminusHandle2, sensorID2, sensorInfo2] =
361 parseStateSensorPDR(sample2PDR);
362 const auto& [containerID2, entityType2, entityInstance2] =
363 std::get<0>(sensorInfo2);
364 const auto& states2 = std::get<1>(sensorInfo2);
365 CompositeSensorStates statesCmp2{{3u, 4u, 9u, 10u, 11u, 13u}};
366
367 ASSERT_EQ(le16toh(terminusHandle2), 0u);
368 ASSERT_EQ(le16toh(sensorID2), 2u);
369 ASSERT_EQ(le16toh(containerID2), 0u);
370 ASSERT_EQ(le16toh(entityType2), 31u);
371 ASSERT_EQ(le16toh(entityInstance2), 1u);
372 ASSERT_EQ(states2, statesCmp2);
373
374 // Sample state sensor with SensorID - 3, EntityType - Virtual Machine
375 // Manager(33), Composite State Sensor -2 , State Set ID - Link State(33),
376 // Supported States - 1,2, State Set ID - Configuration State(15),
377 // Supported States - 1,2,3,4
378 std::vector<uint8_t> sample3PDR{
379 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
380 0x00, 0x03, 0x00, 0x21, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x00,
381 0x02, 0x21, 0x00, 0x01, 0x06, 0x0F, 0x00, 0x01, 0x1E};
382
383 const auto& [terminusHandle3, sensorID3, sensorInfo3] =
384 parseStateSensorPDR(sample3PDR);
385 const auto& [containerID3, entityType3, entityInstance3] =
386 std::get<0>(sensorInfo3);
387 const auto& states3 = std::get<1>(sensorInfo3);
388 CompositeSensorStates statesCmp3{{1u, 2u}, {1u, 2u, 3u, 4u}};
389
390 ASSERT_EQ(le16toh(terminusHandle3), 0u);
391 ASSERT_EQ(le16toh(sensorID3), 3u);
392 ASSERT_EQ(le16toh(containerID3), 1u);
393 ASSERT_EQ(le16toh(entityType3), 33u);
394 ASSERT_EQ(le16toh(entityInstance3), 2u);
395 ASSERT_EQ(states3, statesCmp3);
396}
TOM JOSEPHd4d97a52020-03-23 14:36:34 +0530397
398TEST(StateSensorHandler, allScenarios)
399{
400 using namespace pldm::responder::events;
401
402 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed1"),
403 std::exception);
404 ASSERT_THROW(StateSensorHandler("./event_jsons/malformed2"),
405 std::exception);
406
407 StateSensorHandler handler{"./event_jsons/good"};
408 constexpr uint8_t eventState0 = 0;
409 constexpr uint8_t eventState1 = 1;
410 constexpr uint8_t eventState2 = 2;
411 constexpr uint8_t eventState3 = 3;
412
413 // Event Entry 1
414 {
415 StateSensorEntry entry{1, 64, 1, 0};
416 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
417 DBusMapping mapping{"/xyz/abc/def",
418 "xyz.openbmc_project.example1.value", "value1",
419 "string"};
420 ASSERT_EQ(mapping == dbusMapping, true);
421
422 const auto& propValue0 = eventStateMap.at(eventState0);
423 const auto& propValue1 = eventStateMap.at(eventState1);
424 const auto& propValue2 = eventStateMap.at(eventState2);
425 PropertyValue value0{std::in_place_type<std::string>,
426 "xyz.openbmc_project.State.Normal"};
427 PropertyValue value1{std::in_place_type<std::string>,
428 "xyz.openbmc_project.State.Critical"};
429 PropertyValue value2{std::in_place_type<std::string>,
430 "xyz.openbmc_project.State.Fatal"};
431 ASSERT_EQ(value0 == propValue0, true);
432 ASSERT_EQ(value1 == propValue1, true);
433 ASSERT_EQ(value2 == propValue2, true);
434 }
435
436 // Event Entry 2
437 {
438 StateSensorEntry entry{1, 64, 1, 1};
439 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
440 DBusMapping mapping{"/xyz/abc/def",
441 "xyz.openbmc_project.example2.value", "value2",
442 "uint8_t"};
443 ASSERT_EQ(mapping == dbusMapping, true);
444
445 const auto& propValue0 = eventStateMap.at(eventState2);
446 const auto& propValue1 = eventStateMap.at(eventState3);
447 PropertyValue value0{std::in_place_type<uint8_t>, 9};
448 PropertyValue value1{std::in_place_type<uint8_t>, 10};
449 ASSERT_EQ(value0 == propValue0, true);
450 ASSERT_EQ(value1 == propValue1, true);
451 }
452
453 // Event Entry 3
454 {
455 StateSensorEntry entry{2, 67, 2, 0};
456 const auto& [dbusMapping, eventStateMap] = handler.getEventInfo(entry);
457 DBusMapping mapping{"/xyz/abc/ghi",
458 "xyz.openbmc_project.example3.value", "value3",
459 "bool"};
460 ASSERT_EQ(mapping == dbusMapping, true);
461
462 const auto& propValue0 = eventStateMap.at(eventState0);
463 const auto& propValue1 = eventStateMap.at(eventState1);
464 PropertyValue value0{std::in_place_type<bool>, false};
465 PropertyValue value1{std::in_place_type<bool>, true};
466 ASSERT_EQ(value0 == propValue0, true);
467 ASSERT_EQ(value1 == propValue1, true);
468 }
469
470 // Invalid Entry
471 {
472 StateSensorEntry entry{0, 0, 0, 0};
473 ASSERT_THROW(handler.getEventInfo(entry), std::out_of_range);
474 }
475}