1 #include <endian.h> 2 #include <libpldm/base.h> 3 #include <libpldm/entity.h> 4 #include <libpldm/platform.h> 5 #include <libpldm/pldm_types.h> 6 7 #include <array> 8 #include <cerrno> 9 #include <cstdint> 10 #include <cstring> 11 #include <vector> 12 13 #include "msgbuf.h" 14 15 #include <gtest/gtest.h> 16 17 constexpr auto hdrSize = sizeof(pldm_msg_hdr); 18 19 TEST(SetStateEffecterStates, testEncodeResponse) 20 { 21 std::array<uint8_t, 22 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> 23 responseMsg{}; 24 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 25 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 26 uint8_t completionCode = 0; 27 28 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response); 29 30 EXPECT_EQ(rc, PLDM_SUCCESS); 31 EXPECT_EQ(completionCode, response->payload[0]); 32 } 33 34 TEST(SetStateEffecterStates, testEncodeRequest) 35 { 36 std::array<uint8_t, 37 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES> 38 requestMsg{}; 39 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 40 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 41 42 uint16_t effecterId = 0x0a; 43 uint8_t compEffecterCnt = 0x2; 44 std::array<set_effecter_state_field, 8> stateField{}; 45 stateField[0] = {PLDM_REQUEST_SET, 2}; 46 stateField[1] = {PLDM_REQUEST_SET, 3}; 47 48 auto rc = encode_set_state_effecter_states_req( 49 0, effecterId, compEffecterCnt, stateField.data(), request); 50 51 EXPECT_EQ(rc, PLDM_SUCCESS); 52 EXPECT_EQ(effecterId, request->payload[0]); 53 EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]); 54 EXPECT_EQ(stateField[0].set_request, 55 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]); 56 EXPECT_EQ(stateField[0].effecter_state, 57 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + 58 sizeof(stateField[0].set_request)]); 59 EXPECT_EQ(stateField[1].set_request, 60 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + 61 sizeof(stateField[0])]); 62 EXPECT_EQ(stateField[1].effecter_state, 63 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) + 64 sizeof(stateField[0]) + 65 sizeof(stateField[1].set_request)]); 66 } 67 68 TEST(SetStateEffecterStates, testGoodDecodeResponse) 69 { 70 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> 71 responseMsg{}; 72 73 uint8_t retcompletion_code = 0; 74 75 responseMsg[hdrSize] = PLDM_SUCCESS; 76 77 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 78 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 79 80 auto rc = decode_set_state_effecter_states_resp( 81 response, responseMsg.size() - hdrSize, &retcompletion_code); 82 83 EXPECT_EQ(rc, PLDM_SUCCESS); 84 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code); 85 } 86 87 TEST(SetStateEffecterStates, testGoodDecodeRequest) 88 { 89 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES> 90 requestMsg{}; 91 92 uint16_t effecterId = 0x32; 93 uint16_t effecterIdLE = htole16(effecterId); 94 uint8_t compEffecterCnt = 0x2; 95 96 std::array<set_effecter_state_field, 8> stateField{}; 97 stateField[0] = {PLDM_REQUEST_SET, 3}; 98 stateField[1] = {PLDM_REQUEST_SET, 4}; 99 100 uint16_t retEffecterId = 0; 101 uint8_t retCompEffecterCnt = 0; 102 103 std::array<set_effecter_state_field, 8> retStateField{}; 104 105 memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE)); 106 memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt, 107 sizeof(compEffecterCnt)); 108 memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) + 109 hdrSize, 110 &stateField, sizeof(stateField)); 111 112 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 113 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 114 115 auto rc = decode_set_state_effecter_states_req( 116 request, requestMsg.size() - hdrSize, &retEffecterId, 117 &retCompEffecterCnt, retStateField.data()); 118 119 EXPECT_EQ(rc, PLDM_SUCCESS); 120 EXPECT_EQ(effecterId, retEffecterId); 121 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt); 122 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request); 123 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state); 124 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request); 125 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state); 126 } 127 128 TEST(SetStateEffecterStates, testBadDecodeRequest) 129 { 130 const struct pldm_msg* msg = NULL; 131 132 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL, 133 NULL, NULL); 134 135 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 136 } 137 138 TEST(SetStateEffecterStates, testBadDecodeResponse) 139 { 140 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES> 141 responseMsg{}; 142 143 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 144 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 145 146 auto rc = decode_set_state_effecter_states_resp(response, 147 responseMsg.size(), NULL); 148 149 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 150 } 151 152 TEST(GetPDR, testGoodEncodeResponse) 153 { 154 uint8_t completionCode = 0; 155 uint32_t nextRecordHndl = 0x12; 156 uint32_t nextDataTransferHndl = 0x13; 157 uint8_t transferFlag = PLDM_END; 158 uint16_t respCnt = 0x5; 159 std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; 160 uint8_t transferCRC = 6; 161 162 // + size of record data and transfer CRC 163 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + 164 recordData.size() + 1); 165 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 166 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 167 168 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, 169 nextDataTransferHndl, transferFlag, respCnt, 170 recordData.data(), transferCRC, response); 171 172 EXPECT_EQ(rc, PLDM_SUCCESS); 173 struct pldm_get_pdr_resp* resp = 174 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 175 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); 176 177 EXPECT_EQ(completionCode, resp->completion_code); 178 EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle)); 179 EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle)); 180 EXPECT_EQ(transferFlag, resp->transfer_flag); 181 EXPECT_EQ(respCnt, le16toh(resp->response_count)); 182 EXPECT_EQ(0, 183 memcmp(recordData.data(), resp->record_data, recordData.size())); 184 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 + 185 recordData.size()), 186 transferCRC); 187 188 transferFlag = PLDM_START_AND_END; // No CRC in this case 189 responseMsg.resize(responseMsg.size() - sizeof(transferCRC)); 190 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, 191 nextDataTransferHndl, transferFlag, respCnt, 192 recordData.data(), transferCRC, response); 193 EXPECT_EQ(rc, PLDM_SUCCESS); 194 } 195 196 TEST(GetPDR, testBadEncodeResponse) 197 { 198 uint32_t nextRecordHndl = 0x12; 199 uint32_t nextDataTransferHndl = 0x13; 200 uint8_t transferFlag = PLDM_START_AND_END; 201 uint16_t respCnt = 0x5; 202 std::vector<uint8_t> recordData{1, 2, 3, 4, 5}; 203 uint8_t transferCRC = 0; 204 205 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl, 206 nextDataTransferHndl, transferFlag, respCnt, 207 recordData.data(), transferCRC, nullptr); 208 209 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 210 } 211 212 TEST(GetPDR, testGoodDecodeRequest) 213 { 214 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{}; 215 216 uint32_t recordHndl = 0x32; 217 uint32_t dataTransferHndl = 0x11; 218 uint8_t transferOpFlag = PLDM_GET_FIRSTPART; 219 uint16_t requestCnt = 0x5; 220 uint16_t recordChangeNum = 0x01; 221 222 uint32_t retRecordHndl = 0; 223 uint32_t retDataTransferHndl = 0; 224 uint8_t retTransferOpFlag = 0; 225 uint16_t retRequestCnt = 0; 226 uint16_t retRecordChangeNum = 0; 227 228 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 229 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 230 struct pldm_get_pdr_req* request = 231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 232 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload); 233 234 request->record_handle = htole32(recordHndl); 235 request->data_transfer_handle = htole32(dataTransferHndl); 236 request->transfer_op_flag = transferOpFlag; 237 request->request_count = htole16(requestCnt); 238 request->record_change_number = htole16(recordChangeNum); 239 240 auto rc = decode_get_pdr_req( 241 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl, 242 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum); 243 244 EXPECT_EQ(rc, PLDM_SUCCESS); 245 EXPECT_EQ(retRecordHndl, recordHndl); 246 EXPECT_EQ(retDataTransferHndl, dataTransferHndl); 247 EXPECT_EQ(retTransferOpFlag, transferOpFlag); 248 EXPECT_EQ(retRequestCnt, requestCnt); 249 EXPECT_EQ(retRecordChangeNum, recordChangeNum); 250 } 251 252 TEST(GetPDR, testBadDecodeRequest) 253 { 254 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{}; 255 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 256 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 257 258 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL, 259 NULL); 260 261 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 262 } 263 264 TEST(GetPDR, testGoodEncodeRequest) 265 { 266 uint32_t record_hndl = 0; 267 uint32_t data_transfer_hndl = 0; 268 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; 269 uint16_t request_cnt = 20; 270 uint16_t record_chg_num = 0; 271 272 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); 273 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 274 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 275 276 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, 277 transfer_op_flag, request_cnt, record_chg_num, 278 request, PLDM_GET_PDR_REQ_BYTES); 279 EXPECT_EQ(rc, PLDM_SUCCESS); 280 struct pldm_get_pdr_req* req = 281 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 282 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload); 283 EXPECT_EQ(record_hndl, le32toh(req->record_handle)); 284 EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle)); 285 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag); 286 EXPECT_EQ(request_cnt, le16toh(req->request_count)); 287 EXPECT_EQ(record_chg_num, le16toh(req->record_change_number)); 288 } 289 290 TEST(GetPDR, testBadEncodeRequest) 291 { 292 uint32_t record_hndl = 0; 293 uint32_t data_transfer_hndl = 0; 294 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART; 295 uint16_t request_cnt = 32; 296 uint16_t record_chg_num = 0; 297 298 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES); 299 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 300 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 301 302 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, 303 transfer_op_flag, request_cnt, record_chg_num, 304 nullptr, PLDM_GET_PDR_REQ_BYTES); 305 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 306 307 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl, 308 transfer_op_flag, request_cnt, record_chg_num, 309 request, PLDM_GET_PDR_REQ_BYTES + 1); 310 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 311 } 312 313 TEST(GetPDR, testGoodDecodeResponse) 314 { 315 const char* recordData = "123456789"; 316 uint8_t completionCode = PLDM_SUCCESS; 317 uint32_t nextRecordHndl = 0; 318 uint32_t nextDataTransferHndl = 0; 319 uint8_t transferFlag = PLDM_END; 320 constexpr uint16_t respCnt = 9; 321 uint8_t transferCRC = 96; 322 size_t recordDataLength = 32; 323 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + 324 sizeof(transferCRC)> 325 responseMsg{}; 326 327 uint8_t retCompletionCode = 0; 328 uint8_t retRecordData[32] = {0}; 329 uint32_t retNextRecordHndl = 0; 330 uint32_t retNextDataTransferHndl = 0; 331 uint8_t retTransferFlag = 0; 332 uint16_t retRespCnt = 0; 333 uint8_t retTransferCRC = 0; 334 335 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 336 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 337 struct pldm_get_pdr_resp* resp = 338 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 339 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); 340 resp->completion_code = completionCode; 341 resp->next_record_handle = htole32(nextRecordHndl); 342 resp->next_data_transfer_handle = htole32(nextDataTransferHndl); 343 resp->transfer_flag = transferFlag; 344 resp->response_count = htole16(respCnt); 345 memcpy(resp->record_data, recordData, respCnt); 346 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; 347 348 auto rc = decode_get_pdr_resp( 349 response, responseMsg.size() - hdrSize, &retCompletionCode, 350 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, 351 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); 352 EXPECT_EQ(rc, PLDM_SUCCESS); 353 EXPECT_EQ(retCompletionCode, completionCode); 354 EXPECT_EQ(retNextRecordHndl, nextRecordHndl); 355 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl); 356 EXPECT_EQ(retTransferFlag, transferFlag); 357 EXPECT_EQ(retRespCnt, respCnt); 358 EXPECT_EQ(retTransferCRC, transferCRC); 359 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt)); 360 } 361 362 TEST(GetPDR, testBadDecodeResponse) 363 { 364 const char* recordData = "123456789"; 365 uint8_t completionCode = PLDM_SUCCESS; 366 uint32_t nextRecordHndl = 0; 367 uint32_t nextDataTransferHndl = 0; 368 uint8_t transferFlag = PLDM_END; 369 constexpr uint16_t respCnt = 9; 370 uint8_t transferCRC = 96; 371 size_t recordDataLength = respCnt - 1; 372 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt + 373 sizeof(transferCRC)> 374 responseMsg{}; 375 376 uint8_t retCompletionCode = 0; 377 uint8_t retRecordData[32] = {0}; 378 uint32_t retNextRecordHndl = 0; 379 uint32_t retNextDataTransferHndl = 0; 380 uint8_t retTransferFlag = 0; 381 uint16_t retRespCnt = 0; 382 uint8_t retTransferCRC = 0; 383 384 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 385 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 386 struct pldm_get_pdr_resp* resp = 387 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 388 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload); 389 resp->completion_code = completionCode; 390 resp->next_record_handle = htole32(nextRecordHndl); 391 resp->next_data_transfer_handle = htole32(nextDataTransferHndl); 392 resp->transfer_flag = transferFlag; 393 resp->response_count = htole16(respCnt); 394 memcpy(resp->record_data, recordData, respCnt); 395 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC; 396 397 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL, 398 NULL, NULL, NULL, NULL, NULL, 0, NULL); 399 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 400 401 rc = decode_get_pdr_resp( 402 response, responseMsg.size() - hdrSize - 1, &retCompletionCode, 403 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag, 404 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC); 405 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 406 } 407 408 #ifdef LIBPLDM_API_TESTING 409 TEST(GetPDR, testGoodDecodeResponseSafe) 410 { 411 static const char recordData[] = "123456789"; 412 413 alignas(pldm_msg) unsigned char data[sizeof(pldm_msg_hdr) + 414 PLDM_GET_PDR_MIN_RESP_BYTES + 415 sizeof(recordData) - 1 + 1]; 416 struct pldm_msgbuf _buf; 417 struct pldm_msgbuf* buf = &_buf; 418 int rc; 419 420 pldm_msg* msg = new (data) pldm_msg; 421 422 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload, 423 sizeof(data) - sizeof(msg->hdr)); 424 ASSERT_EQ(rc, 0); 425 426 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS); 427 pldm_msgbuf_insert_uint32(buf, 0); 428 pldm_msgbuf_insert_uint32(buf, 0); 429 pldm_msgbuf_insert_uint8(buf, PLDM_END); 430 pldm_msgbuf_insert_uint16(buf, sizeof(recordData) - 1); 431 rc = pldm_msgbuf_insert_array_char(buf, sizeof(recordData) - 1, recordData, 432 sizeof(recordData) - 1); 433 ASSERT_EQ(rc, 0); 434 pldm_msgbuf_insert_uint8(buf, 96); 435 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 436 437 alignas(pldm_get_pdr_resp) unsigned char 438 resp_data[sizeof(pldm_get_pdr_resp) + sizeof(recordData) - 1]; 439 pldm_get_pdr_resp* resp = new (resp_data) pldm_get_pdr_resp; 440 uint8_t crc; 441 rc = decode_get_pdr_resp_safe(msg, sizeof(data) - sizeof(msg->hdr), resp, 442 sizeof(resp_data) - sizeof(*resp), &crc); 443 ASSERT_EQ(rc, 0); 444 EXPECT_EQ(resp->completion_code, PLDM_SUCCESS); 445 EXPECT_EQ(resp->next_record_handle, 0); 446 EXPECT_EQ(resp->next_data_transfer_handle, 0); 447 EXPECT_EQ(resp->transfer_flag, PLDM_END); 448 ASSERT_EQ(resp->response_count, sizeof(recordData) - 1); 449 EXPECT_EQ(crc, 96); 450 EXPECT_EQ(0, memcmp(recordData, resp->record_data, resp->response_count)); 451 } 452 #endif 453 454 #ifdef LIBPLDM_API_TESTING 455 TEST(GetPDR, testBadDecodeResponseSafeTrivial) 456 { 457 pldm_get_pdr_resp resp; 458 uint8_t crc; 459 int rc; 460 461 rc = decode_get_pdr_resp_safe(nullptr, PLDM_GET_PDR_MIN_RESP_BYTES, &resp, 462 sizeof(resp), &crc); 463 EXPECT_EQ(rc, -EINVAL); 464 465 alignas(pldm_msg) unsigned char 466 msg_data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES]; 467 pldm_msg* msg = new (msg_data) pldm_msg; 468 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, nullptr, 469 sizeof(resp), &crc); 470 EXPECT_EQ(rc, -EINVAL); 471 472 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, &resp, 473 sizeof(resp), nullptr); 474 EXPECT_EQ(rc, -EINVAL); 475 476 msg->payload[0] = PLDM_ERROR_INVALID_DATA; 477 rc = decode_get_pdr_resp_safe(msg, 1, &resp, sizeof(resp), &crc); 478 EXPECT_EQ(rc, 0); 479 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA); 480 } 481 #endif 482 483 TEST(GetPDRRepositoryInfo, testGoodEncodeResponse) 484 { 485 uint8_t completionCode = 0; 486 uint8_t repositoryState = PLDM_AVAILABLE; 487 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 488 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 489 uint32_t recordCount = 100; 490 uint32_t repositorySize = 100; 491 uint32_t largestRecordSize = UINT32_MAX; 492 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT; 493 494 std::vector<uint8_t> responseMsg(hdrSize + 495 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES); 496 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 497 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 498 499 auto rc = encode_get_pdr_repository_info_resp( 500 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime, 501 recordCount, repositorySize, largestRecordSize, 502 dataTransferHandleTimeout, response); 503 504 EXPECT_EQ(rc, PLDM_SUCCESS); 505 struct pldm_pdr_repository_info_resp* resp = 506 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 507 reinterpret_cast<struct pldm_pdr_repository_info_resp*>( 508 response->payload); 509 510 EXPECT_EQ(completionCode, resp->completion_code); 511 EXPECT_EQ(repositoryState, resp->repository_state); 512 EXPECT_EQ(0, memcmp(updateTime, resp->update_time, PLDM_TIMESTAMP104_SIZE)); 513 EXPECT_EQ(0, memcmp(oemUpdateTime, resp->oem_update_time, 514 PLDM_TIMESTAMP104_SIZE)); 515 EXPECT_EQ(recordCount, le32toh(resp->record_count)); 516 EXPECT_EQ(repositorySize, le32toh(resp->repository_size)); 517 EXPECT_EQ(largestRecordSize, le32toh(resp->largest_record_size)); 518 EXPECT_EQ(dataTransferHandleTimeout, resp->data_transfer_handle_timeout); 519 } 520 521 TEST(GetPDRRepositoryInfo, testBadEncodeResponse) 522 { 523 uint8_t repositoryState = PLDM_AVAILABLE; 524 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 525 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 526 uint32_t recordCount = 100; 527 uint32_t repositorySize = 100; 528 uint32_t largestRecordSize = UINT32_MAX; 529 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT; 530 531 auto rc = encode_get_pdr_repository_info_resp( 532 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime, 533 recordCount, repositorySize, largestRecordSize, 534 dataTransferHandleTimeout, nullptr); 535 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 536 } 537 538 TEST(GetPDRRepositoryInfo, testGoodDecodeResponse) 539 { 540 uint8_t completionCode = PLDM_SUCCESS; 541 uint8_t repositoryState = PLDM_AVAILABLE; 542 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 543 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 544 uint32_t recordCount = 100; 545 uint32_t repositorySize = 100; 546 uint32_t largestRecordSize = UINT32_MAX; 547 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT; 548 549 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES> 550 responseMsg{}; 551 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 552 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 553 struct pldm_pdr_repository_info_resp* resp = 554 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 555 reinterpret_cast<struct pldm_pdr_repository_info_resp*>( 556 response->payload); 557 resp->completion_code = completionCode; 558 resp->repository_state = repositoryState; 559 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE); 560 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE); 561 resp->record_count = htole32(recordCount); 562 resp->repository_size = htole32(repositorySize); 563 resp->largest_record_size = htole32(largestRecordSize); 564 resp->data_transfer_handle_timeout = dataTransferHandleTimeout; 565 566 uint8_t retCompletionCode = 0; 567 uint8_t retRepositoryState = 0; 568 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 569 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 570 uint32_t retRecordCount = 0; 571 uint32_t retRepositorySize = 0; 572 uint32_t retLargestRecordSize = 0; 573 uint8_t retDataTransferHandleTimeout = 0; 574 575 auto rc = decode_get_pdr_repository_info_resp( 576 response, responseMsg.size() - hdrSize, &retCompletionCode, 577 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount, 578 &retRepositorySize, &retLargestRecordSize, 579 &retDataTransferHandleTimeout); 580 581 EXPECT_EQ(rc, PLDM_SUCCESS); 582 EXPECT_EQ(completionCode, retCompletionCode); 583 EXPECT_EQ(repositoryState, retRepositoryState); 584 EXPECT_EQ(0, memcmp(updateTime, retUpdateTime, PLDM_TIMESTAMP104_SIZE)); 585 EXPECT_EQ(0, 586 memcmp(oemUpdateTime, retOemUpdateTime, PLDM_TIMESTAMP104_SIZE)); 587 EXPECT_EQ(recordCount, recordCount); 588 EXPECT_EQ(repositorySize, repositorySize); 589 EXPECT_EQ(largestRecordSize, largestRecordSize); 590 EXPECT_EQ(dataTransferHandleTimeout, dataTransferHandleTimeout); 591 } 592 593 TEST(GetPDRRepositoryInfo, testBadDecodeResponse) 594 { 595 uint8_t completionCode = PLDM_SUCCESS; 596 uint8_t repositoryState = PLDM_AVAILABLE; 597 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 598 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 599 uint32_t recordCount = htole32(100); 600 uint32_t repositorySize = htole32(100); 601 uint32_t largestRecordSize = htole32(UINT32_MAX); 602 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT; 603 604 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES> 605 responseMsg{}; 606 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 607 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 608 struct pldm_pdr_repository_info_resp* resp = 609 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 610 reinterpret_cast<struct pldm_pdr_repository_info_resp*>( 611 response->payload); 612 resp->completion_code = completionCode; 613 resp->repository_state = repositoryState; 614 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE); 615 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE); 616 resp->record_count = recordCount; 617 resp->repository_size = repositorySize; 618 resp->largest_record_size = largestRecordSize; 619 resp->data_transfer_handle_timeout = dataTransferHandleTimeout; 620 621 uint8_t retCompletionCode = 0; 622 uint8_t retRepositoryState = 0; 623 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 624 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 625 uint32_t retRecordCount = 0; 626 uint32_t retRepositorySize = 0; 627 uint32_t retLargestRecordSize = 0; 628 uint8_t retDataTransferHandleTimeout = 0; 629 630 auto rc = decode_get_pdr_repository_info_resp( 631 response, responseMsg.size() - hdrSize, NULL, NULL, NULL, NULL, NULL, 632 NULL, NULL, NULL); 633 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 634 635 rc = decode_get_pdr_repository_info_resp( 636 response, responseMsg.size() - hdrSize - 1, &retCompletionCode, 637 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount, 638 &retRepositorySize, &retLargestRecordSize, 639 &retDataTransferHandleTimeout); 640 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 641 642 resp->repository_state = PLDM_FAILED + 1; 643 rc = decode_get_pdr_repository_info_resp( 644 response, responseMsg.size() - hdrSize, &retCompletionCode, 645 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount, 646 &retRepositorySize, &retLargestRecordSize, 647 &retDataTransferHandleTimeout); 648 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 649 } 650 651 #ifdef LIBPLDM_API_TESTING 652 TEST(GetPDRRepositoryInfo, testGoodDecodeResponseSafe) 653 { 654 alignas(pldm_msg) unsigned char 655 data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES]; 656 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 657 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0}; 658 struct pldm_msgbuf _buf; 659 struct pldm_msgbuf* buf = &_buf; 660 int rc; 661 662 pldm_msg* msg = new (data) pldm_msg; 663 664 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, 665 msg->payload, sizeof(data) - sizeof(msg->hdr)); 666 ASSERT_EQ(rc, 0); 667 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS); 668 pldm_msgbuf_insert_uint8(buf, PLDM_AVAILABLE); 669 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE, updateTime, 670 sizeof(updateTime)); 671 ASSERT_EQ(rc, 0); 672 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE, 673 oemUpdateTime, sizeof(oemUpdateTime)); 674 ASSERT_EQ(rc, 0); 675 pldm_msgbuf_insert_uint32(buf, 100); 676 pldm_msgbuf_insert_uint32(buf, 100); 677 pldm_msgbuf_insert_uint32(buf, UINT32_MAX); 678 pldm_msgbuf_insert_uint8(buf, PLDM_NO_TIMEOUT); 679 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 680 681 struct pldm_pdr_repository_info_resp resp; 682 rc = decode_get_pdr_repository_info_resp_safe( 683 msg, sizeof(data) - sizeof(msg->hdr), &resp); 684 685 EXPECT_EQ(rc, 0); 686 EXPECT_EQ(PLDM_SUCCESS, resp.completion_code); 687 EXPECT_EQ(PLDM_AVAILABLE, resp.repository_state); 688 EXPECT_EQ(0, 689 memcmp(updateTime, resp.update_time, sizeof(resp.update_time))); 690 EXPECT_EQ(0, memcmp(oemUpdateTime, resp.oem_update_time, 691 sizeof(resp.oem_update_time))); 692 EXPECT_EQ(100, resp.record_count); 693 EXPECT_EQ(100, resp.repository_size); 694 EXPECT_EQ(UINT32_MAX, resp.largest_record_size); 695 EXPECT_EQ(PLDM_NO_TIMEOUT, resp.data_transfer_handle_timeout); 696 } 697 #endif 698 699 #ifdef LIBPLDM_API_TESTING 700 TEST(GetPDRRepositoryInfo, testBadDecodeResponseSafeTrivial) 701 { 702 struct pldm_pdr_repository_info_resp resp; 703 int rc; 704 705 rc = decode_get_pdr_repository_info_resp_safe( 706 nullptr, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, &resp); 707 EXPECT_EQ(rc, -EINVAL); 708 709 alignas(pldm_msg) unsigned char 710 msg_data[sizeof(pldm_msg) - 1 + 711 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES]; 712 pldm_msg* msg = new (msg_data) pldm_msg; 713 rc = decode_get_pdr_repository_info_resp_safe(msg, 0, &resp); 714 EXPECT_EQ(rc, -EOVERFLOW); 715 716 rc = decode_get_pdr_repository_info_resp_safe( 717 msg, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, nullptr); 718 EXPECT_EQ(rc, -EINVAL); 719 720 msg->payload[0] = PLDM_ERROR_INVALID_DATA; 721 rc = decode_get_pdr_repository_info_resp_safe(msg, 1, &resp); 722 EXPECT_EQ(rc, 0); 723 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA); 724 } 725 #endif 726 727 TEST(SetNumericEffecterValue, testGoodDecodeRequest) 728 { 729 std::array<uint8_t, 730 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3> 731 requestMsg{}; 732 733 uint16_t effecter_id = 32768; 734 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32; 735 uint32_t effecter_value = 123456789; 736 737 uint16_t reteffecter_id; 738 uint8_t reteffecter_data_size; 739 uint8_t reteffecter_value[4]; 740 741 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 742 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 743 struct pldm_set_numeric_effecter_value_req* request = 744 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 745 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( 746 req->payload); 747 748 request->effecter_id = htole16(effecter_id); 749 request->effecter_data_size = effecter_data_size; 750 uint32_t effecter_value_le = htole32(effecter_value); 751 memcpy(request->effecter_value, &effecter_value_le, 752 sizeof(effecter_value_le)); 753 754 auto rc = decode_set_numeric_effecter_value_req( 755 req, requestMsg.size() - hdrSize, &reteffecter_id, 756 &reteffecter_data_size, reteffecter_value); 757 758 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 759 uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value)); 760 EXPECT_EQ(rc, PLDM_SUCCESS); 761 EXPECT_EQ(reteffecter_id, effecter_id); 762 EXPECT_EQ(reteffecter_data_size, effecter_data_size); 763 EXPECT_EQ(value, effecter_value); 764 } 765 766 TEST(SetNumericEffecterValue, testBadDecodeRequest) 767 { 768 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES> 769 requestMsg{}; 770 771 auto rc = decode_set_numeric_effecter_value_req( 772 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL); 773 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 774 775 uint16_t effecter_id = 0x10; 776 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8; 777 uint8_t effecter_value = 1; 778 779 uint16_t reteffecter_id; 780 uint8_t reteffecter_data_size; 781 uint8_t reteffecter_value[4]; 782 783 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 784 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 785 struct pldm_set_numeric_effecter_value_req* request = 786 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 787 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( 788 req->payload); 789 790 request->effecter_id = effecter_id; 791 request->effecter_data_size = effecter_data_size; 792 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value)); 793 794 rc = decode_set_numeric_effecter_value_req( 795 req, requestMsg.size() - hdrSize - 1, &reteffecter_id, 796 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 797 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value)); 798 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 799 } 800 801 TEST(SetNumericEffecterValue, testGoodEncodeRequest) 802 { 803 uint16_t effecter_id = 0; 804 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16; 805 uint16_t effecter_value = 65534; 806 807 std::vector<uint8_t> requestMsg( 808 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1); 809 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 810 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 811 812 auto rc = encode_set_numeric_effecter_value_req( 813 0, effecter_id, effecter_data_size, 814 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 815 reinterpret_cast<uint8_t*>(&effecter_value), request, 816 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1); 817 EXPECT_EQ(rc, PLDM_SUCCESS); 818 819 struct pldm_set_numeric_effecter_value_req* req = 820 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 821 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( 822 request->payload); 823 EXPECT_EQ(effecter_id, req->effecter_id); 824 EXPECT_EQ(effecter_data_size, req->effecter_data_size); 825 uint16_t* val = (uint16_t*)req->effecter_value; 826 *val = le16toh(*val); 827 EXPECT_EQ(effecter_value, *val); 828 } 829 830 TEST(SetNumericEffecterValue, testBadEncodeRequest) 831 { 832 std::vector<uint8_t> requestMsg( 833 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); 834 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 835 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 836 837 auto rc = encode_set_numeric_effecter_value_req( 838 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); 839 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 840 841 uint16_t effecter_value; 842 rc = encode_set_numeric_effecter_value_req( 843 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 844 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request, 845 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); 846 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 847 } 848 849 TEST(SetNumericEffecterValue, testGoodDecodeResponse) 850 { 851 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> 852 responseMsg{}; 853 854 uint8_t completion_code = 0xa0; 855 856 uint8_t retcompletion_code; 857 858 memcpy(responseMsg.data() + hdrSize, &completion_code, 859 sizeof(completion_code)); 860 861 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 862 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 863 864 auto rc = decode_set_numeric_effecter_value_resp( 865 response, responseMsg.size() - hdrSize, &retcompletion_code); 866 867 EXPECT_EQ(rc, PLDM_SUCCESS); 868 EXPECT_EQ(completion_code, retcompletion_code); 869 } 870 871 TEST(SetNumericEffecterValue, testBadDecodeResponse) 872 { 873 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> 874 responseMsg{}; 875 876 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 877 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 878 879 auto rc = decode_set_numeric_effecter_value_resp(response, 880 responseMsg.size(), NULL); 881 882 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 883 } 884 885 TEST(SetNumericEffecterValue, testGoodEncodeResponse) 886 { 887 std::array<uint8_t, sizeof(pldm_msg_hdr) + 888 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES> 889 responseMsg{}; 890 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 891 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 892 uint8_t completionCode = 0; 893 894 auto rc = encode_set_numeric_effecter_value_resp( 895 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); 896 897 EXPECT_EQ(rc, PLDM_SUCCESS); 898 EXPECT_EQ(completionCode, response->payload[0]); 899 } 900 901 TEST(SetNumericEffecterValue, testBadEncodeResponse) 902 { 903 auto rc = encode_set_numeric_effecter_value_resp( 904 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES); 905 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 906 } 907 908 TEST(GetStateSensorReadings, testGoodEncodeResponse) 909 { 910 std::array<uint8_t, hdrSize + 911 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + 912 sizeof(get_sensor_state_field) * 2> 913 responseMsg{}; 914 915 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 916 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 917 uint8_t completionCode = 0; 918 uint8_t comp_sensorCnt = 0x2; 919 920 std::array<get_sensor_state_field, 2> stateField{}; 921 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_NORMAL, 922 PLDM_SENSOR_WARNING, PLDM_SENSOR_UNKNOWN}; 923 stateField[1] = {PLDM_SENSOR_FAILED, PLDM_SENSOR_UPPERFATAL, 924 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_FATAL}; 925 926 auto rc = encode_get_state_sensor_readings_resp( 927 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response); 928 929 struct pldm_get_state_sensor_readings_resp* resp = 930 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 931 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( 932 response->payload); 933 934 EXPECT_EQ(rc, PLDM_SUCCESS); 935 EXPECT_EQ(completionCode, resp->completion_code); 936 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count); 937 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state); 938 EXPECT_EQ(stateField[0].present_state, resp->field->present_state); 939 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state); 940 EXPECT_EQ(stateField[0].event_state, resp->field->event_state); 941 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state); 942 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state); 943 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state); 944 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state); 945 } 946 947 TEST(GetStateSensorReadings, testBadEncodeResponse) 948 { 949 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr, 950 nullptr); 951 952 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 953 } 954 955 TEST(GetStateSensorReadings, testGoodDecodeResponse) 956 { 957 std::array<uint8_t, hdrSize + 958 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + 959 sizeof(get_sensor_state_field) * 2> 960 responseMsg{}; 961 962 uint8_t completionCode = 0; 963 uint8_t comp_sensorCnt = 2; 964 965 std::array<get_sensor_state_field, 2> stateField{}; 966 stateField[0] = {PLDM_SENSOR_DISABLED, PLDM_SENSOR_UNKNOWN, 967 PLDM_SENSOR_UNKNOWN, PLDM_SENSOR_UNKNOWN}; 968 stateField[1] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_LOWERFATAL, 969 PLDM_SENSOR_LOWERCRITICAL, PLDM_SENSOR_WARNING}; 970 971 uint8_t retcompletion_code = 0; 972 uint8_t retcomp_sensorCnt = 0; 973 std::array<get_sensor_state_field, 2> retstateField{}; 974 975 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 976 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 977 struct pldm_get_state_sensor_readings_resp* resp = 978 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 979 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( 980 response->payload); 981 982 resp->completion_code = completionCode; 983 resp->comp_sensor_count = comp_sensorCnt; 984 memcpy(resp->field, &stateField, 985 (sizeof(get_sensor_state_field) * comp_sensorCnt)); 986 987 auto rc = decode_get_state_sensor_readings_resp( 988 response, responseMsg.size() - hdrSize, &retcompletion_code, 989 &retcomp_sensorCnt, retstateField.data()); 990 991 EXPECT_EQ(rc, PLDM_SUCCESS); 992 EXPECT_EQ(completionCode, retcompletion_code); 993 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt); 994 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state); 995 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state); 996 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state); 997 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state); 998 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state); 999 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state); 1000 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state); 1001 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state); 1002 } 1003 1004 TEST(GetStateSensorReadings, testBadDecodeResponse) 1005 { 1006 std::array<uint8_t, hdrSize + 1007 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES + 1008 sizeof(get_sensor_state_field) * 2> 1009 responseMsg{}; 1010 1011 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1012 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1013 1014 auto rc = decode_get_state_sensor_readings_resp( 1015 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr); 1016 1017 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1018 1019 uint8_t completionCode = 0; 1020 uint8_t comp_sensorCnt = 1; 1021 1022 std::array<get_sensor_state_field, 1> stateField{}; 1023 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_UPPERFATAL, 1024 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_WARNING}; 1025 1026 uint8_t retcompletion_code = 0; 1027 uint8_t retcomp_sensorCnt = 0; 1028 std::array<get_sensor_state_field, 1> retstateField{}; 1029 1030 struct pldm_get_state_sensor_readings_resp* resp = 1031 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1032 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>( 1033 response->payload); 1034 1035 resp->completion_code = completionCode; 1036 resp->comp_sensor_count = comp_sensorCnt; 1037 memcpy(resp->field, &stateField, 1038 (sizeof(get_sensor_state_field) * comp_sensorCnt)); 1039 1040 rc = decode_get_state_sensor_readings_resp( 1041 response, responseMsg.size() - hdrSize, &retcompletion_code, 1042 &retcomp_sensorCnt, retstateField.data()); 1043 1044 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1045 } 1046 1047 TEST(GetStateSensorReadings, testGoodEncodeRequest) 1048 { 1049 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> 1050 requestMsg{}; 1051 1052 uint16_t sensorId = 0xab; 1053 bitfield8_t sensorRearm; 1054 sensorRearm.byte = 0x03; 1055 1056 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1057 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1058 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0, 1059 request); 1060 1061 struct pldm_get_state_sensor_readings_req* req = 1062 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1063 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( 1064 request->payload); 1065 1066 EXPECT_EQ(rc, PLDM_SUCCESS); 1067 EXPECT_EQ(sensorId, le16toh(req->sensor_id)); 1068 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte); 1069 } 1070 1071 TEST(GetStateSensorReadings, testBadEncodeRequest) 1072 { 1073 bitfield8_t sensorRearm; 1074 sensorRearm.byte = 0x0; 1075 1076 auto rc = 1077 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr); 1078 1079 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1080 } 1081 1082 TEST(GetStateSensorReadings, testGoodDecodeRequest) 1083 { 1084 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> 1085 requestMsg{}; 1086 1087 uint16_t sensorId = 0xcd; 1088 bitfield8_t sensorRearm; 1089 sensorRearm.byte = 0x10; 1090 1091 uint16_t retsensorId; 1092 bitfield8_t retsensorRearm; 1093 uint8_t retreserved; 1094 1095 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1096 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1097 1098 struct pldm_get_state_sensor_readings_req* req = 1099 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1100 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( 1101 request->payload); 1102 1103 req->sensor_id = htole16(sensorId); 1104 req->sensor_rearm.byte = sensorRearm.byte; 1105 1106 auto rc = decode_get_state_sensor_readings_req( 1107 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm, 1108 &retreserved); 1109 1110 EXPECT_EQ(rc, PLDM_SUCCESS); 1111 EXPECT_EQ(sensorId, retsensorId); 1112 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte); 1113 EXPECT_EQ(0, retreserved); 1114 } 1115 1116 TEST(GetStateSensorReadings, testBadDecodeRequest) 1117 { 1118 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES> 1119 requestMsg{}; 1120 1121 auto rc = decode_get_state_sensor_readings_req( 1122 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr); 1123 1124 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1125 uint16_t sensorId = 0x11; 1126 bitfield8_t sensorRearm; 1127 sensorRearm.byte = 0x04; 1128 1129 uint16_t retsensorId; 1130 bitfield8_t retsensorRearm; 1131 uint8_t retreserved; 1132 1133 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1134 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1135 1136 struct pldm_get_state_sensor_readings_req* req = 1137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1138 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>( 1139 request->payload); 1140 1141 req->sensor_id = htole16(sensorId); 1142 req->sensor_rearm.byte = sensorRearm.byte; 1143 1144 rc = decode_get_state_sensor_readings_req( 1145 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm, 1146 &retreserved); 1147 1148 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1149 } 1150 1151 TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeRequest) 1152 { 1153 uint8_t eventBufferSize = 32; 1154 1155 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES> 1156 requestMsg{}; 1157 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1158 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1159 1160 auto rc = encode_event_message_buffer_size_req(0, eventBufferSize, request); 1161 1162 EXPECT_EQ(rc, PLDM_SUCCESS); 1163 } 1164 1165 TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeResponse) 1166 { 1167 uint8_t completionCode = PLDM_SUCCESS; 1168 uint16_t terminusMaxBufferSize = 256; 1169 1170 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES> 1171 responseMsg{}; 1172 1173 uint8_t retCompletionCode; 1174 uint16_t retMaxBufferSize = 0; 1175 1176 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1178 struct pldm_event_message_buffer_size_resp* resp = 1179 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1180 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>( 1181 response->payload); 1182 1183 resp->completion_code = completionCode; 1184 resp->terminus_max_buffer_size = terminusMaxBufferSize; 1185 1186 auto rc = decode_event_message_buffer_size_resp( 1187 response, responseMsg.size() - hdrSize, &retCompletionCode, 1188 &retMaxBufferSize); 1189 1190 EXPECT_EQ(rc, PLDM_SUCCESS); 1191 EXPECT_EQ(retCompletionCode, completionCode); 1192 EXPECT_EQ(terminusMaxBufferSize, retMaxBufferSize); 1193 } 1194 1195 TEST(EventMessageBufferSize, testBadEventMessageBufferSizeResponse) 1196 { 1197 uint8_t completionCode = PLDM_SUCCESS; 1198 uint16_t terminusMaxBufferSize = 256; 1199 1200 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES> 1201 responseMsg{}; 1202 1203 uint8_t retCompletionCode; 1204 uint16_t retMaxBufferSize = 0; 1205 1206 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1207 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1208 struct pldm_event_message_buffer_size_resp* resp = 1209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1210 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>( 1211 response->payload); 1212 resp->completion_code = completionCode; 1213 resp->terminus_max_buffer_size = terminusMaxBufferSize; 1214 1215 auto rc = 1216 decode_event_message_buffer_size_resp(response, 0, nullptr, nullptr); 1217 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1218 1219 rc = decode_event_message_buffer_size_resp( 1220 response, responseMsg.size(), &retCompletionCode, &retMaxBufferSize); 1221 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1222 } 1223 1224 TEST(PlatformEventMessageSupported, testGoodEncodeRequest) 1225 { 1226 uint8_t formatVersion = 0x01; 1227 1228 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES> 1229 requestMsg{}; 1230 1231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1232 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1233 1234 auto rc = encode_event_message_supported_req(0, formatVersion, request); 1235 1236 struct pldm_event_message_supported_req* req = 1237 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1238 reinterpret_cast<struct pldm_event_message_supported_req*>( 1239 request->payload); 1240 1241 EXPECT_EQ(rc, PLDM_SUCCESS); 1242 EXPECT_EQ(formatVersion, req->format_version); 1243 } 1244 1245 TEST(PlatformEventMessageSupported, testBadEncodeRequest) 1246 { 1247 uint8_t eventData = 34; 1248 uint8_t formatVersion = 0x0; 1249 1250 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES + 1251 sizeof(eventData)> 1252 requestMsg{}; 1253 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1254 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1255 1256 auto rc = encode_event_message_supported_req(0, formatVersion, request); 1257 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1258 1259 rc = encode_event_message_supported_req(0, formatVersion, nullptr); 1260 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1261 } 1262 1263 TEST(PlatformEventMessageSupported, testGoodDecodeRespond) 1264 { 1265 uint8_t completionCode = PLDM_SUCCESS; 1266 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS; 1267 bitfield8_t synchConfigSupported; 1268 synchConfigSupported.byte = 0xe; 1269 uint8_t numberEventClassReturned = 0x3; 1270 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa}; 1271 constexpr uint8_t eventClassCount = 3; 1272 1273 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES + 1274 eventClassCount> 1275 responseMsg{}; 1276 1277 uint8_t retCompletionCode; 1278 uint8_t retSynchConfig = 0; 1279 uint8_t retNumberEventClass = 0; 1280 bitfield8_t retSynchConfigSupport; 1281 uint8_t retEventClass[eventClassCount] = {0}; 1282 1283 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1284 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1285 struct pldm_event_message_supported_resp* resp = 1286 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1287 reinterpret_cast<struct pldm_event_message_supported_resp*>( 1288 response->payload); 1289 1290 resp->completion_code = completionCode; 1291 resp->synchrony_configuration = synchConfiguration; 1292 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte; 1293 resp->number_event_class_returned = numberEventClassReturned; 1294 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned); 1295 1296 auto rc = decode_event_message_supported_resp( 1297 response, responseMsg.size() - hdrSize, &retCompletionCode, 1298 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass, 1299 retEventClass, eventClassCount); 1300 1301 EXPECT_EQ(rc, PLDM_SUCCESS); 1302 EXPECT_EQ(retCompletionCode, completionCode); 1303 EXPECT_EQ(retSynchConfig, synchConfiguration); 1304 EXPECT_EQ(retNumberEventClass, numberEventClassReturned); 1305 EXPECT_EQ(retSynchConfigSupport.byte, synchConfigSupported.byte); 1306 EXPECT_EQ(0, memcmp(eventClass.data(), resp->event_class, 1307 numberEventClassReturned)); 1308 } 1309 1310 TEST(PlatformEventMessageSupported, testBadSynchConfiguration) 1311 { 1312 uint8_t completionCode = PLDM_SUCCESS; 1313 uint8_t synchConfiguration = 0x4; 1314 bitfield8_t synchConfigSupported; 1315 synchConfigSupported.byte = 0xe; 1316 uint8_t numberEventClassReturned = 0x3; 1317 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa}; 1318 constexpr uint8_t eventClassCount = 3; 1319 1320 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES + 1321 eventClassCount> 1322 responseMsg{}; 1323 1324 uint8_t retCompletionCode; 1325 uint8_t retSynchConfig = 0; 1326 uint8_t retNumberEventClass = 0; 1327 bitfield8_t retSynchConfigSupport; 1328 uint8_t retEventClass[eventClassCount] = {0}; 1329 1330 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1331 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1332 struct pldm_event_message_supported_resp* resp = 1333 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1334 reinterpret_cast<struct pldm_event_message_supported_resp*>( 1335 response->payload); 1336 1337 resp->completion_code = completionCode; 1338 resp->synchrony_configuration = synchConfiguration; 1339 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte; 1340 resp->number_event_class_returned = numberEventClassReturned; 1341 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned); 1342 1343 auto rc = decode_event_message_supported_resp( 1344 response, responseMsg.size() - hdrSize, &retCompletionCode, 1345 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass, 1346 retEventClass, eventClassCount); 1347 1348 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1349 } 1350 1351 TEST(PlatformEventMessageSupported, testBadDecodeRespond) 1352 { 1353 uint8_t completionCode = PLDM_SUCCESS; 1354 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS; 1355 bitfield8_t synchConfigSupported; 1356 synchConfigSupported.byte = 0xe; 1357 uint8_t numberEventClassReturned = 0x3; 1358 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa}; 1359 constexpr uint8_t eventClassCount = 3; 1360 1361 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES + 1362 eventClassCount> 1363 responseMsg{}; 1364 1365 uint8_t retCompletionCode; 1366 uint8_t retSynchConfig = 0; 1367 uint8_t retNumberEventClass = 0; 1368 bitfield8_t retSynchConfigSupport; 1369 uint8_t retEventClass[eventClassCount] = {0}; 1370 1371 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1372 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1373 struct pldm_event_message_supported_resp* resp = 1374 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1375 reinterpret_cast<struct pldm_event_message_supported_resp*>( 1376 response->payload); 1377 resp->completion_code = completionCode; 1378 resp->synchrony_configuration = synchConfiguration; 1379 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte; 1380 resp->number_event_class_returned = numberEventClassReturned; 1381 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned); 1382 1383 auto rc = decode_event_message_supported_resp(response, 0, nullptr, nullptr, 1384 nullptr, nullptr, nullptr, 0); 1385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1386 1387 rc = decode_event_message_supported_resp( 1388 response, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES - 1, 1389 &retCompletionCode, &retSynchConfig, &retSynchConfigSupport, 1390 &retNumberEventClass, retEventClass, eventClassCount); 1391 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1392 1393 rc = decode_event_message_supported_resp( 1394 response, responseMsg.size() - hdrSize, &retCompletionCode, 1395 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass, 1396 retEventClass, 1); 1397 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1398 } 1399 1400 TEST(PollForPlatformEventMessage, testGoodEncodeRequestFirstPart) 1401 { 1402 uint8_t formatVersion = 0x01; 1403 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART; 1404 uint32_t dataTransferHandle = 0xaabbccdd; 1405 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL; 1406 1407 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1408 1409 auto rc = encode_poll_for_platform_event_message_req( 1410 0, formatVersion, transferOperationFlag, dataTransferHandle, 1411 eventIdToAcknowledge, request, 1412 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1413 EXPECT_EQ(rc, PLDM_SUCCESS); 1414 1415 struct pldm_msgbuf _buf; 1416 struct pldm_msgbuf* buf = &_buf; 1417 rc = pldm_msgbuf_init_errno( 1418 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload, 1419 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1420 EXPECT_EQ(rc, 0); 1421 1422 uint8_t retFormatVersion; 1423 uint8_t retTransferOperationFlag; 1424 uint32_t retDataTransferHandle; 1425 uint16_t retEventIdToAcknowledge; 1426 1427 pldm_msgbuf_extract_uint8(buf, retFormatVersion); 1428 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag); 1429 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle); 1430 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge); 1431 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 1432 1433 EXPECT_EQ(retFormatVersion, formatVersion); 1434 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1435 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1436 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1437 } 1438 1439 TEST(PollForPlatformEventMessage, testGoodEncodeRequestNextPart) 1440 { 1441 uint8_t formatVersion = 0x01; 1442 uint8_t transferOperationFlag = PLDM_GET_NEXTPART; 1443 uint32_t dataTransferHandle = 0xaabbccdd; 1444 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT; 1445 1446 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1447 1448 auto rc = encode_poll_for_platform_event_message_req( 1449 0, formatVersion, transferOperationFlag, dataTransferHandle, 1450 eventIdToAcknowledge, request, 1451 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1452 EXPECT_EQ(rc, PLDM_SUCCESS); 1453 1454 struct pldm_msgbuf _buf; 1455 struct pldm_msgbuf* buf = &_buf; 1456 rc = pldm_msgbuf_init_errno( 1457 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload, 1458 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1459 EXPECT_EQ(rc, 0); 1460 1461 uint8_t retFormatVersion; 1462 uint8_t retTransferOperationFlag; 1463 uint32_t retDataTransferHandle; 1464 uint16_t retEventIdToAcknowledge; 1465 1466 pldm_msgbuf_extract_uint8(buf, retFormatVersion); 1467 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag); 1468 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle); 1469 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge); 1470 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 1471 1472 EXPECT_EQ(retFormatVersion, formatVersion); 1473 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1474 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1475 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1476 } 1477 1478 TEST(PollForPlatformEventMessage, testGoodEncodeRequestAckOnly) 1479 { 1480 uint8_t formatVersion = 0x01; 1481 uint8_t transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY; 1482 uint32_t dataTransferHandle = 0xaabbccdd; 1483 uint16_t eventIdToAcknowledge = 0x1234; 1484 1485 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1486 1487 auto rc = encode_poll_for_platform_event_message_req( 1488 0, formatVersion, transferOperationFlag, dataTransferHandle, 1489 eventIdToAcknowledge, request, 1490 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1491 EXPECT_EQ(rc, PLDM_SUCCESS); 1492 1493 struct pldm_msgbuf _buf; 1494 struct pldm_msgbuf* buf = &_buf; 1495 rc = pldm_msgbuf_init_errno( 1496 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload, 1497 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1498 EXPECT_EQ(rc, 0); 1499 1500 uint8_t retFormatVersion; 1501 uint8_t retTransferOperationFlag; 1502 uint32_t retDataTransferHandle; 1503 uint16_t retEventIdToAcknowledge; 1504 1505 pldm_msgbuf_extract_uint8(buf, retFormatVersion); 1506 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag); 1507 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle); 1508 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge); 1509 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 1510 1511 EXPECT_EQ(retFormatVersion, formatVersion); 1512 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1513 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1514 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1515 } 1516 1517 TEST(PollForPlatformEventMessage, testBadEncodeRequest) 1518 { 1519 uint8_t formatVersion = 0x01; 1520 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART; 1521 uint32_t dataTransferHandle = 0xaabbccdd; 1522 uint16_t eventIdToAcknowledge = 0x1234; 1523 1524 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1525 1526 auto rc = encode_poll_for_platform_event_message_req( 1527 0, formatVersion, transferOperationFlag, dataTransferHandle, 1528 eventIdToAcknowledge, nullptr, 1529 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1530 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1531 1532 transferOperationFlag = PLDM_GET_FIRSTPART; 1533 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT; 1534 rc = encode_poll_for_platform_event_message_req( 1535 0, formatVersion, transferOperationFlag, dataTransferHandle, 1536 eventIdToAcknowledge, request, 1537 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1538 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1539 1540 transferOperationFlag = PLDM_GET_NEXTPART; 1541 eventIdToAcknowledge = 0x1234; 1542 rc = encode_poll_for_platform_event_message_req( 1543 0, formatVersion, transferOperationFlag, dataTransferHandle, 1544 eventIdToAcknowledge, request, 1545 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1546 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1547 1548 transferOperationFlag = PLDM_GET_NEXTPART; 1549 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL; 1550 rc = encode_poll_for_platform_event_message_req( 1551 0, formatVersion, transferOperationFlag, dataTransferHandle, 1552 eventIdToAcknowledge, request, 1553 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1554 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1555 1556 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY; 1557 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL; 1558 rc = encode_poll_for_platform_event_message_req( 1559 0, formatVersion, transferOperationFlag, dataTransferHandle, 1560 eventIdToAcknowledge, request, 1561 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1562 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1563 1564 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY; 1565 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT; 1566 rc = encode_poll_for_platform_event_message_req( 1567 0, formatVersion, transferOperationFlag, dataTransferHandle, 1568 eventIdToAcknowledge, request, 1569 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1570 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1571 1572 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY + 1; 1573 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT; 1574 rc = encode_poll_for_platform_event_message_req( 1575 0, formatVersion, transferOperationFlag, dataTransferHandle, 1576 eventIdToAcknowledge, request, 1577 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES); 1578 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1579 } 1580 1581 TEST(PollForPlatformEventMessage, testGoodDecodeRespond) 1582 { 1583 uint8_t completionCode = PLDM_SUCCESS; 1584 uint8_t tId = 0x9; 1585 uint16_t eventId = 159; 1586 uint32_t nextDataTransferHandle = 0x11223344; 1587 uint8_t transferFlag = PLDM_START_AND_END; 1588 uint8_t eventClass = 0x5; 1589 uint8_t eventData[5] = {0x55, 0x44, 0x33, 0x22, 0x11}; 1590 constexpr uint32_t eventDataSize = 0x00000005; 1591 uint32_t eventDataIntegrityChecksum = 0x66778899; 1592 1593 std::vector<uint8_t> responseMsg{ 1594 0x1, 1595 0x0, 1596 0x0, 1597 PLDM_SUCCESS, 1598 0x9, // tid 1599 159, 1600 0x0, // event id 1601 0x44, 1602 0x33, 1603 0x22, 1604 0x11, // next_data_transfer_handle 1605 PLDM_START_AND_END, // transfer_flag 1606 0x05, // event class 1607 0x05, 1608 0x00, 1609 0x00, 1610 0x00, // event_data_size 1611 0x55, 1612 0x44, 1613 0x33, 1614 0x22, 1615 0x11, // event_data[5] 1616 0x99, 1617 0x88, 1618 0x77, 1619 0x66 // event_data_integrity_checksum 1620 }; 1621 const uint32_t respMsgLen = 23; 1622 1623 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1624 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1625 1626 uint8_t retCompletionCode; 1627 uint8_t retTid = 0; 1628 uint16_t retEventId = 0; 1629 uint32_t retNextDataTransferHandle = 0; 1630 uint8_t retTransferFlag = 0; 1631 uint8_t retEventClass = 0; 1632 uint32_t retEventDataSize = 0; 1633 uint8_t* retEventData = nullptr; 1634 uint32_t retEventDataIntegrityChecksum = 0; 1635 1636 auto rc = decode_poll_for_platform_event_message_resp( 1637 response, respMsgLen, &retCompletionCode, &retTid, &retEventId, 1638 &retNextDataTransferHandle, &retTransferFlag, &retEventClass, 1639 &retEventDataSize, (void**)&retEventData, 1640 &retEventDataIntegrityChecksum); 1641 1642 EXPECT_EQ(rc, PLDM_SUCCESS); 1643 EXPECT_EQ(retCompletionCode, completionCode); 1644 EXPECT_EQ(retTid, tId); 1645 EXPECT_EQ(retEventId, eventId); 1646 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle); 1647 EXPECT_EQ(retTransferFlag, transferFlag); 1648 EXPECT_EQ(retEventClass, eventClass); 1649 EXPECT_EQ(retEventDataSize, eventDataSize); 1650 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum); 1651 EXPECT_EQ(0, memcmp(eventData, retEventData, eventDataSize)); 1652 } 1653 1654 TEST(PollForPlatformEventMessage, testGoodDecodeAckOnlyRespond) 1655 { 1656 uint8_t completionCode = PLDM_SUCCESS; 1657 uint8_t tId = 0x9; 1658 uint16_t eventId = 0xffff; 1659 1660 std::vector<uint8_t> responseMsg{ 1661 0x1, 0x0, 0x0, PLDM_SUCCESS, 1662 0x9, // tid 1663 0xff, 1664 0xff // event id 1665 }; 1666 const uint32_t respMsgLen = 4; 1667 1668 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1669 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1670 1671 uint8_t retCompletionCode; 1672 uint8_t retTid = 0; 1673 uint16_t retEventId = 0; 1674 uint32_t retNextDataTransferHandle = 0; 1675 uint8_t retTransferFlag = 0; 1676 uint8_t retEventClass = 0; 1677 uint32_t retEventDataSize = 0; 1678 uint8_t* retEventData = nullptr; 1679 uint32_t retEventDataIntegrityChecksum = 0; 1680 1681 auto rc = decode_poll_for_platform_event_message_resp( 1682 response, respMsgLen, &retCompletionCode, &retTid, &retEventId, 1683 &retNextDataTransferHandle, &retTransferFlag, &retEventClass, 1684 &retEventDataSize, (void**)&retEventData, 1685 &retEventDataIntegrityChecksum); 1686 1687 EXPECT_EQ(rc, PLDM_SUCCESS); 1688 EXPECT_EQ(retCompletionCode, completionCode); 1689 EXPECT_EQ(retTid, tId); 1690 EXPECT_EQ(retEventId, eventId); 1691 1692 eventId = 0x0000; 1693 responseMsg[5] = 0x00; 1694 responseMsg[6] = 0x00; 1695 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1696 response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1697 1698 rc = decode_poll_for_platform_event_message_resp( 1699 response, respMsgLen, &retCompletionCode, &retTid, &retEventId, 1700 &retNextDataTransferHandle, &retTransferFlag, &retEventClass, 1701 &retEventDataSize, (void**)&retEventData, 1702 &retEventDataIntegrityChecksum); 1703 1704 EXPECT_EQ(rc, PLDM_SUCCESS); 1705 EXPECT_EQ(retCompletionCode, completionCode); 1706 EXPECT_EQ(retTid, tId); 1707 EXPECT_EQ(retEventId, eventId); 1708 } 1709 1710 TEST(PollForPlatformEventMessage, testBadDecodeRespond) 1711 { 1712 std::vector<uint8_t> responseMsg{ 1713 0x1, 1714 0x0, 1715 0x0, 1716 PLDM_SUCCESS, 1717 0x9, // tid 1718 159, 1719 0x0, // event id 1720 0x44, 1721 0x33, 1722 0x22, 1723 0x11, // next_data_transfer_handle 1724 PLDM_START_AND_END, // transfer_flag 1725 0x05, // event class 1726 0x05, 1727 0x00, 1728 0x00, 1729 0x00, // event_data_size 1730 0x55, 1731 0x44, 1732 0x33, 1733 0x22, 1734 0x11, // event_data[5] 1735 0x99, 1736 0x88, 1737 0x77, 1738 0x66 // event_data_integrity_checksum 1739 }; 1740 // const uint32_t respMsgLen = 23; 1741 1742 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1743 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1744 1745 auto rc = decode_poll_for_platform_event_message_resp( 1746 nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, 1747 nullptr, nullptr, nullptr); 1748 1749 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1750 1751 uint8_t retCompletionCode; 1752 uint8_t retTid = 0; 1753 uint16_t retEventId = 0; 1754 uint32_t retNextDataTransferHandle = 0; 1755 uint8_t retTransferFlag = 0; 1756 uint8_t retEventClass = 0; 1757 uint32_t retEventDataSize = 0; 1758 uint8_t* retEventData = nullptr; 1759 uint32_t retEventDataIntegrityChecksum = 0; 1760 1761 rc = decode_poll_for_platform_event_message_resp( 1762 response, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES - 1, 1763 &retCompletionCode, &retTid, &retEventId, &retNextDataTransferHandle, 1764 &retTransferFlag, &retEventClass, &retEventDataSize, 1765 (void**)&retEventData, &retEventDataIntegrityChecksum); 1766 1767 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 1768 } 1769 1770 TEST(PollForPlatformEventMessage, testGoodDecodeRequestFirstPart) 1771 { 1772 uint8_t formatVersion = 0x1; 1773 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART; 1774 uint32_t dataTransferHandle = 0x11223344; 1775 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL; 1776 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART, 1777 0x44, 0x33, 0x22, 0x11, 0x00, 1778 0x00}; 1779 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1780 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1781 1782 uint8_t retFormatVersion; 1783 uint8_t retTransferOperationFlag; 1784 uint32_t retDataTransferHandle; 1785 uint16_t retEventIdToAcknowledge; 1786 1787 auto rc = decode_poll_for_platform_event_message_req( 1788 request, requestMsg.size() - hdrSize, &retFormatVersion, 1789 &retTransferOperationFlag, &retDataTransferHandle, 1790 &retEventIdToAcknowledge); 1791 1792 EXPECT_EQ(rc, PLDM_SUCCESS); 1793 EXPECT_EQ(retFormatVersion, formatVersion); 1794 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1795 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1796 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1797 } 1798 1799 TEST(PollForPlatformEventMessage, testGoodDecodeRequestNextPart) 1800 { 1801 uint8_t formatVersion = 0x1; 1802 uint8_t transferOperationFlag = PLDM_GET_NEXTPART; 1803 uint32_t dataTransferHandle = 0x11223344; 1804 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT; 1805 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_NEXTPART, 1806 0x44, 0x33, 0x22, 0x11, 0xff, 1807 0xff}; 1808 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1809 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1810 1811 uint8_t retFormatVersion; 1812 uint8_t retTransferOperationFlag; 1813 uint32_t retDataTransferHandle; 1814 uint16_t retEventIdToAcknowledge; 1815 1816 auto rc = decode_poll_for_platform_event_message_req( 1817 request, requestMsg.size() - hdrSize, &retFormatVersion, 1818 &retTransferOperationFlag, &retDataTransferHandle, 1819 &retEventIdToAcknowledge); 1820 1821 EXPECT_EQ(rc, PLDM_SUCCESS); 1822 EXPECT_EQ(retFormatVersion, formatVersion); 1823 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1824 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1825 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1826 } 1827 1828 TEST(PollForPlatformEventMessage, testGoodDecodeRequestAck) 1829 { 1830 uint8_t formatVersion = 0x1; 1831 uint8_t transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY; 1832 uint32_t dataTransferHandle = 0x11223344; 1833 uint16_t eventIdToAcknowledge = 0x1234; 1834 std::vector<uint8_t> requestMsg{ 1835 0x1, 0x0, 0x0, 0x1, PLDM_ACKNOWLEDGEMENT_ONLY, 0x44, 0x33, 1836 0x22, 0x11, 0x34, 0x12}; 1837 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1838 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1839 1840 uint8_t retFormatVersion; 1841 uint8_t retTransferOperationFlag; 1842 uint32_t retDataTransferHandle; 1843 uint16_t retEventIdToAcknowledge; 1844 1845 auto rc = decode_poll_for_platform_event_message_req( 1846 request, requestMsg.size() - hdrSize, &retFormatVersion, 1847 &retTransferOperationFlag, &retDataTransferHandle, 1848 &retEventIdToAcknowledge); 1849 1850 EXPECT_EQ(rc, PLDM_SUCCESS); 1851 EXPECT_EQ(retFormatVersion, formatVersion); 1852 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag); 1853 EXPECT_EQ(retDataTransferHandle, dataTransferHandle); 1854 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge); 1855 } 1856 1857 TEST(PollForPlatformEventMessage, testBadDecodeRequest) 1858 { 1859 /* 1860 * transfer_operation_flag is PLDM_GET_FIRSTPART and 1861 * event_id_to_acknowledge is not PLDM_PLATFORM_EVENT_ID_NULL 1862 */ 1863 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART, 1864 0x44, 0x33, 0x22, 0x11, 0x66, 1865 0x55}; 1866 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1867 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 1868 1869 uint8_t retFormatVersion; 1870 uint8_t retTransferOperationFlag; 1871 uint32_t retDataTransferHandle; 1872 uint16_t retEventIdToAcknowledge; 1873 1874 auto rc = decode_poll_for_platform_event_message_req( 1875 NULL, requestMsg.size() - hdrSize, &retFormatVersion, 1876 &retTransferOperationFlag, &retDataTransferHandle, 1877 &retEventIdToAcknowledge); 1878 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1879 1880 /* 1881 * transfer_operation_flag is not PLDM_GET_FIRSTPART or PLDM_GET_NEXTPART or 1882 * PLDM_ACKNOWLEDGEMENT_ONLY 1883 */ 1884 1885 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY + 1; 1886 1887 rc = decode_poll_for_platform_event_message_req( 1888 request, requestMsg.size() - hdrSize, &retFormatVersion, 1889 &retTransferOperationFlag, &retDataTransferHandle, 1890 &retEventIdToAcknowledge); 1891 1892 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1893 1894 /* 1895 * transfer_operation_flag is PLDM_GET_NEXTPART and 1896 * event_id_to_acknowledge is not PLDM_PLATFORM_EVENT_ID_FRAGMENT 1897 */ 1898 requestMsg[4] = PLDM_GET_NEXTPART; 1899 requestMsg[9] = 0x11; 1900 requestMsg[10] = 0x22; 1901 1902 rc = decode_poll_for_platform_event_message_req( 1903 request, requestMsg.size() - hdrSize, &retFormatVersion, 1904 &retTransferOperationFlag, &retDataTransferHandle, 1905 &retEventIdToAcknowledge); 1906 1907 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1908 1909 /* 1910 * transfer_operation_flag is PLDM_ACKNOWLEDGEMENT_ONLY and 1911 * event_id_to_acknowledge is PLDM_PLATFORM_EVENT_ID_FRAGMENT 1912 */ 1913 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY; 1914 requestMsg[9] = 0xff; 1915 requestMsg[10] = 0xff; 1916 1917 rc = decode_poll_for_platform_event_message_req( 1918 request, requestMsg.size() - hdrSize, &retFormatVersion, 1919 &retTransferOperationFlag, &retDataTransferHandle, 1920 &retEventIdToAcknowledge); 1921 1922 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1923 1924 /* 1925 * transfer_operation_flag is PLDM_ACKNOWLEDGEMENT_ONLY and 1926 * event_id_to_acknowledge is PLDM_PLATFORM_EVENT_ID_NULL 1927 */ 1928 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY; 1929 requestMsg[9] = 0x00; 1930 requestMsg[10] = 0x00; 1931 1932 rc = decode_poll_for_platform_event_message_req( 1933 request, requestMsg.size() - hdrSize, &retFormatVersion, 1934 &retTransferOperationFlag, &retDataTransferHandle, 1935 &retEventIdToAcknowledge); 1936 1937 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 1938 } 1939 1940 TEST(PollForPlatformEventMessage, testGoodEncodeResposeP1) 1941 { 1942 uint8_t completionCode = PLDM_SUCCESS; 1943 uint8_t instance_id = 0; 1944 uint8_t tId = 0x9; 1945 uint16_t eventId = 0x1; 1946 uint32_t nextDataTransferHandle = 0xffff; 1947 uint8_t transferFlag = PLDM_END; 1948 uint8_t eventClass = 0x5; 1949 constexpr uint32_t eventDataSize = 9; 1950 uint8_t pEventData[eventDataSize] = {0x31, 0x32, 0x33, 0x34, 0x35, 1951 0x36, 0x37, 0x38, 0x39}; 1952 uint32_t eventDataIntegrityChecksum = 0x11223344; 1953 constexpr size_t payloadLength = 1954 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4; 1955 1956 std::array<uint8_t, hdrSize + payloadLength> responseMsg{}; 1957 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1958 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 1959 1960 auto rc = encode_poll_for_platform_event_message_resp( 1961 instance_id, completionCode, tId, eventId, nextDataTransferHandle, 1962 transferFlag, eventClass, eventDataSize, pEventData, 1963 eventDataIntegrityChecksum, response, payloadLength); 1964 EXPECT_EQ(rc, PLDM_SUCCESS); 1965 1966 struct pldm_msgbuf _buf; 1967 struct pldm_msgbuf* buf = &_buf; 1968 rc = pldm_msgbuf_init_cc( 1969 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES, 1970 response->payload, payloadLength); 1971 EXPECT_EQ(rc, PLDM_SUCCESS); 1972 1973 uint8_t retCompletionCode; 1974 uint8_t retTid = 0; 1975 uint16_t retEventId = 0; 1976 uint32_t retNextDataTransferHandle = 0; 1977 uint8_t retTransferFlag = 0; 1978 uint8_t retEventClass = 0; 1979 uint32_t retEventDataSize = 0; 1980 uint8_t retEventData[payloadLength] = {0}; 1981 uint32_t retEventDataIntegrityChecksum = 0; 1982 1983 pldm_msgbuf_extract_uint8(buf, retCompletionCode); 1984 pldm_msgbuf_extract_uint8(buf, retTid); 1985 pldm_msgbuf_extract_uint16(buf, retEventId); 1986 pldm_msgbuf_extract_uint32(buf, retNextDataTransferHandle); 1987 pldm_msgbuf_extract_uint8(buf, retTransferFlag); 1988 pldm_msgbuf_extract_uint8(buf, retEventClass); 1989 pldm_msgbuf_extract_uint32(buf, retEventDataSize); 1990 rc = pldm_msgbuf_extract_array_uint8(buf, retEventDataSize, retEventData, 1991 sizeof(retEventData)); 1992 ASSERT_EQ(rc, 0); 1993 pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum); 1994 1995 EXPECT_EQ(rc, PLDM_SUCCESS); 1996 EXPECT_EQ(retCompletionCode, completionCode); 1997 EXPECT_EQ(retTid, tId); 1998 EXPECT_EQ(retEventId, eventId); 1999 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle); 2000 EXPECT_EQ(retTransferFlag, transferFlag); 2001 EXPECT_EQ(retEventClass, eventClass); 2002 EXPECT_EQ(retEventDataSize, eventDataSize); 2003 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum); 2004 EXPECT_EQ(0, memcmp(pEventData, retEventData, eventDataSize)); 2005 2006 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS); 2007 } 2008 2009 TEST(PollForPlatformEventMessage, testGoodEncodeResposeP2) 2010 { 2011 uint8_t completionCode = PLDM_SUCCESS; 2012 uint8_t instance_id = 0; 2013 uint8_t tId = 0x9; 2014 uint16_t eventId = 0x0000; 2015 constexpr size_t payloadLength = 2016 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES; 2017 2018 std::array<uint8_t, hdrSize + payloadLength> responseMsg{}; 2019 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2020 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2021 2022 auto rc = encode_poll_for_platform_event_message_resp( 2023 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0, 2024 response, payloadLength); 2025 EXPECT_EQ(rc, PLDM_SUCCESS); 2026 2027 struct pldm_msgbuf _buf; 2028 struct pldm_msgbuf* buf = &_buf; 2029 rc = pldm_msgbuf_init_cc( 2030 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES, 2031 response->payload, payloadLength); 2032 EXPECT_EQ(rc, PLDM_SUCCESS); 2033 2034 uint8_t retCompletionCode; 2035 uint8_t retTid = 0; 2036 uint16_t retEventId = 0; 2037 2038 pldm_msgbuf_extract_uint8(buf, retCompletionCode); 2039 pldm_msgbuf_extract_uint8(buf, retTid); 2040 pldm_msgbuf_extract_uint16(buf, retEventId); 2041 2042 EXPECT_EQ(rc, PLDM_SUCCESS); 2043 EXPECT_EQ(retCompletionCode, completionCode); 2044 EXPECT_EQ(retTid, tId); 2045 EXPECT_EQ(retEventId, eventId); 2046 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS); 2047 } 2048 2049 TEST(PollForPlatformEventMessage, testGoodEncodeResposeP3) 2050 { 2051 uint8_t completionCode = PLDM_SUCCESS; 2052 uint8_t instance_id = 0; 2053 uint8_t tId = 0x9; 2054 uint16_t eventId = 0xffff; 2055 constexpr size_t payloadLength = 2056 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES; 2057 2058 std::array<uint8_t, hdrSize + payloadLength> responseMsg{}; 2059 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2060 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2061 2062 auto rc = encode_poll_for_platform_event_message_resp( 2063 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0, 2064 response, payloadLength); 2065 EXPECT_EQ(rc, PLDM_SUCCESS); 2066 2067 struct pldm_msgbuf _buf; 2068 struct pldm_msgbuf* buf = &_buf; 2069 rc = pldm_msgbuf_init_cc( 2070 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES, 2071 response->payload, payloadLength); 2072 EXPECT_EQ(rc, PLDM_SUCCESS); 2073 2074 uint8_t retCompletionCode; 2075 uint8_t retTid = 0; 2076 uint16_t retEventId = 0; 2077 2078 pldm_msgbuf_extract_uint8(buf, retCompletionCode); 2079 pldm_msgbuf_extract_uint8(buf, retTid); 2080 pldm_msgbuf_extract_uint16(buf, retEventId); 2081 2082 EXPECT_EQ(rc, PLDM_SUCCESS); 2083 EXPECT_EQ(retCompletionCode, completionCode); 2084 EXPECT_EQ(retTid, tId); 2085 EXPECT_EQ(retEventId, eventId); 2086 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS); 2087 } 2088 2089 TEST(PollForPlatformEventMessage, testGoodEncodeResposeP4) 2090 { 2091 uint8_t completionCode = PLDM_SUCCESS; 2092 uint8_t instance_id = 0; 2093 uint8_t tId = 0x9; 2094 uint16_t eventId = 0x1; 2095 uint32_t nextDataTransferHandle = 0xffff; 2096 uint8_t transferFlag = PLDM_END; 2097 uint8_t eventClass = 0x5; 2098 constexpr uint32_t eventDataSize = 0; 2099 uint32_t eventDataIntegrityChecksum = 0x11223344; 2100 size_t payloadLength = 2101 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4; 2102 2103 std::array<uint8_t, hdrSize + 2104 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + 2105 eventDataSize + 4> 2106 responseMsg{}; 2107 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2108 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2109 2110 auto rc = encode_poll_for_platform_event_message_resp( 2111 instance_id, completionCode, tId, eventId, nextDataTransferHandle, 2112 transferFlag, eventClass, eventDataSize, NULL, 2113 eventDataIntegrityChecksum, response, payloadLength); 2114 EXPECT_EQ(rc, PLDM_SUCCESS); 2115 2116 struct pldm_msgbuf _buf; 2117 struct pldm_msgbuf* buf = &_buf; 2118 rc = pldm_msgbuf_init_cc( 2119 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES, 2120 response->payload, payloadLength); 2121 EXPECT_EQ(rc, PLDM_SUCCESS); 2122 2123 uint8_t retCompletionCode; 2124 uint8_t retTid = 0; 2125 uint16_t retEventId = 0; 2126 uint32_t retNextDataTransferHandle = 0; 2127 uint8_t retTransferFlag = 0; 2128 uint8_t retEventClass = 0; 2129 uint32_t retEventDataSize = 0; 2130 uint32_t retEventDataIntegrityChecksum = 0; 2131 2132 pldm_msgbuf_extract_uint8(buf, retCompletionCode); 2133 pldm_msgbuf_extract_uint8(buf, retTid); 2134 pldm_msgbuf_extract_uint16(buf, retEventId); 2135 pldm_msgbuf_extract_uint32(buf, retNextDataTransferHandle); 2136 pldm_msgbuf_extract_uint8(buf, retTransferFlag); 2137 pldm_msgbuf_extract_uint8(buf, retEventClass); 2138 pldm_msgbuf_extract_uint32(buf, retEventDataSize); 2139 pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum); 2140 2141 EXPECT_EQ(rc, PLDM_SUCCESS); 2142 EXPECT_EQ(retCompletionCode, completionCode); 2143 EXPECT_EQ(retTid, tId); 2144 EXPECT_EQ(retEventId, eventId); 2145 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle); 2146 EXPECT_EQ(retTransferFlag, transferFlag); 2147 EXPECT_EQ(retEventClass, eventClass); 2148 EXPECT_EQ(retEventDataSize, eventDataSize); 2149 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum); 2150 2151 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS); 2152 } 2153 2154 TEST(PollForPlatformEventMessage, testBadEncodeResponse) 2155 { 2156 uint8_t completionCode = PLDM_SUCCESS; 2157 uint8_t instance_id = 0; 2158 uint8_t tId = 0x9; 2159 uint16_t eventId = 0x1; 2160 uint32_t nextDataTransferHandle = 0xffff; 2161 uint8_t transferFlag = 0x0; 2162 uint8_t eventClass = 0x5; 2163 const uint32_t eventDataSize = 0; 2164 uint32_t eventDataIntegrityChecksum = 0x11223344; 2165 constexpr size_t payloadLength = 2166 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4; 2167 2168 std::array<uint8_t, hdrSize + payloadLength> responseMsg{}; 2169 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2170 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2171 2172 auto rc = encode_poll_for_platform_event_message_resp( 2173 instance_id, completionCode, tId, eventId, nextDataTransferHandle, 2174 transferFlag, eventClass, eventDataSize, NULL, 2175 eventDataIntegrityChecksum, NULL, payloadLength); 2176 2177 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2178 2179 rc = encode_poll_for_platform_event_message_resp( 2180 instance_id, completionCode, tId, eventId, nextDataTransferHandle, 2181 transferFlag, eventClass, 1, NULL, eventDataIntegrityChecksum, response, 2182 payloadLength); 2183 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2184 } 2185 2186 TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest) 2187 { 2188 std::array<uint8_t, 2189 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 2190 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES> 2191 requestMsg{}; 2192 2193 uint8_t retFormatVersion = 0; 2194 uint8_t retTid = 0; 2195 uint8_t retEventClass = 0; 2196 size_t retEventDataOffset = 0; 2197 2198 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2199 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 2200 struct pldm_platform_event_message_req* request = 2201 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2202 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload); 2203 2204 uint8_t formatVersion = 0x01; 2205 uint8_t tid = 0x02; 2206 // Sensor Event 2207 uint8_t eventClass = 0x00; 2208 2209 request->format_version = formatVersion; 2210 request->tid = tid; 2211 request->event_class = eventClass; 2212 size_t eventDataOffset = 2213 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass); 2214 2215 auto rc = decode_platform_event_message_req( 2216 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid, 2217 &retEventClass, &retEventDataOffset); 2218 2219 EXPECT_EQ(rc, PLDM_SUCCESS); 2220 EXPECT_EQ(retFormatVersion, formatVersion); 2221 EXPECT_EQ(retTid, tid); 2222 EXPECT_EQ(retEventClass, eventClass); 2223 EXPECT_EQ(retEventDataOffset, eventDataOffset); 2224 } 2225 2226 TEST(PlatformEventMessage, testBadDecodeRequest) 2227 { 2228 const struct pldm_msg* msg = NULL; 2229 std::array<uint8_t, 2230 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 2231 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1> 2232 requestMsg{}; 2233 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2234 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); 2235 uint8_t retFormatVersion; 2236 uint8_t retTid = 0; 2237 uint8_t retEventClass = 0; 2238 size_t retEventDataOffset; 2239 2240 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL, 2241 NULL, NULL); 2242 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2243 2244 rc = decode_platform_event_message_req( 2245 req, 2246 requestMsg.size() - hdrSize - 2247 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES, 2248 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset); 2249 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2250 } 2251 2252 TEST(PlatformEventMessage, testGoodEncodeResponse) 2253 { 2254 std::array<uint8_t, 2255 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 2256 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1> 2257 responseMsg{}; 2258 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2259 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2260 uint8_t completionCode = 0; 2261 uint8_t instanceId = 0x01; 2262 uint8_t platformEventStatus = 0x01; 2263 2264 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS, 2265 platformEventStatus, response); 2266 2267 EXPECT_EQ(rc, PLDM_SUCCESS); 2268 EXPECT_EQ(completionCode, response->payload[0]); 2269 EXPECT_EQ(platformEventStatus, response->payload[1]); 2270 } 2271 2272 TEST(PlatformEventMessage, testBadEncodeResponse) 2273 { 2274 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL); 2275 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2276 } 2277 2278 TEST(PlatformEventMessage, testGoodEncodeRequest) 2279 { 2280 static constexpr const uint8_t formatVersion = 0x01; 2281 static constexpr const uint8_t eventClass = 0x00; 2282 static constexpr const uint8_t eventData = 34; 2283 static constexpr const uint8_t Tid = 0x03; 2284 struct pldm_platform_event_message_req req; 2285 struct pldm_msgbuf _buf; 2286 struct pldm_msgbuf* buf = &_buf; 2287 size_t len; 2288 void* data; 2289 2290 PLDM_MSG_DEFINE_P(request, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + 2291 sizeof(eventData)); 2292 2293 /* Test with the minimum event type value */ 2294 auto rc = encode_platform_event_message_req( 2295 0, formatVersion, Tid, eventClass, &eventData, sizeof(eventData), 2296 request, sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); 2297 ASSERT_EQ(rc, PLDM_SUCCESS); 2298 2299 rc = pldm_msgbuf_init_errno( 2300 buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES, request->payload, 2301 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData)); 2302 ASSERT_EQ(rc, 0); 2303 2304 pldm_msgbuf_extract_uint8(buf, req.format_version); 2305 pldm_msgbuf_extract_uint8(buf, req.tid); 2306 pldm_msgbuf_extract_uint8(buf, req.event_class); 2307 data = nullptr; 2308 pldm_msgbuf_span_remaining(buf, &data, &len); 2309 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 2310 2311 EXPECT_EQ(formatVersion, req.format_version); 2312 EXPECT_EQ(Tid, req.tid); 2313 EXPECT_EQ(eventClass, req.event_class); 2314 ASSERT_EQ(sizeof(eventData), len); 2315 EXPECT_EQ(0, memcmp(&eventData, data, len)); 2316 2317 /* Test with the maximum event type value */ 2318 rc = encode_platform_event_message_req( 2319 0, formatVersion, Tid, PLDM_CPER_EVENT, &eventData, sizeof(eventData), 2320 request, sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES); 2321 ASSERT_EQ(rc, PLDM_SUCCESS); 2322 2323 rc = pldm_msgbuf_init_errno( 2324 buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES, request->payload, 2325 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData)); 2326 ASSERT_EQ(rc, 0); 2327 2328 pldm_msgbuf_extract_uint8(buf, req.format_version); 2329 pldm_msgbuf_extract_uint8(buf, req.tid); 2330 pldm_msgbuf_extract_uint8(buf, req.event_class); 2331 2332 data = nullptr; 2333 pldm_msgbuf_span_remaining(buf, &data, &len); 2334 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0); 2335 2336 EXPECT_EQ(formatVersion, req.format_version); 2337 EXPECT_EQ(Tid, req.tid); 2338 EXPECT_EQ(PLDM_CPER_EVENT, req.event_class); 2339 ASSERT_EQ(sizeof(eventData), len); 2340 EXPECT_EQ(0, memcmp(&eventData, data, len)); 2341 } 2342 2343 TEST(PlatformEventMessage, testBadEncodeRequest) 2344 { 2345 uint8_t Tid = 0x03; 2346 uint8_t eventClass = 0x00; 2347 uint8_t eventData = 34; 2348 uint8_t formatVersion = 0x01; 2349 static constexpr const size_t payloadLen = 2350 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData); 2351 2352 PLDM_MSG_DEFINE_P(request, payloadLen); 2353 2354 auto rc = encode_platform_event_message_req( 2355 0, formatVersion, Tid, eventClass, &eventData, sizeof(eventData), 2356 nullptr, payloadLen); 2357 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2358 2359 rc = encode_platform_event_message_req(0, 0, Tid, eventClass, &eventData, 2360 sizeof(eventData), request, 2361 payloadLen); 2362 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2363 2364 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass, 2365 nullptr, 0, request, payloadLen); 2366 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2367 2368 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass, 2369 &eventData, sizeof(eventData), 2370 request, 0); 2371 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2372 2373 rc = encode_platform_event_message_req( 2374 0, formatVersion, Tid, PLDM_CPER_EVENT + 1, &eventData, 2375 sizeof(eventData), request, payloadLen); 2376 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2377 } 2378 2379 TEST(PlatformEventMessage, testGoodDecodeResponse) 2380 { 2381 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES> 2382 responseMsg{}; 2383 2384 uint8_t completionCode = PLDM_SUCCESS; 2385 uint8_t platformEventStatus = 0x01; 2386 2387 uint8_t retcompletionCode; 2388 uint8_t retplatformEventStatus; 2389 2390 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2391 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2392 struct pldm_platform_event_message_resp* resp = 2393 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2394 reinterpret_cast<struct pldm_platform_event_message_resp*>( 2395 response->payload); 2396 2397 resp->completion_code = completionCode; 2398 resp->platform_event_status = platformEventStatus; 2399 2400 auto rc = decode_platform_event_message_resp( 2401 response, responseMsg.size() - hdrSize, &retcompletionCode, 2402 &retplatformEventStatus); 2403 2404 EXPECT_EQ(rc, PLDM_SUCCESS); 2405 EXPECT_EQ(completionCode, retcompletionCode); 2406 EXPECT_EQ(platformEventStatus, retplatformEventStatus); 2407 } 2408 2409 TEST(PlatformEventMessage, testBadDecodeResponse) 2410 { 2411 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES> 2412 responseMsg{}; 2413 2414 uint8_t completionCode = PLDM_SUCCESS; 2415 uint8_t platformEventStatus = 0x01; 2416 2417 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2418 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2419 struct pldm_platform_event_message_resp* resp = 2420 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2421 reinterpret_cast<struct pldm_platform_event_message_resp*>( 2422 response->payload); 2423 resp->completion_code = completionCode; 2424 resp->platform_event_status = platformEventStatus; 2425 2426 auto rc = decode_platform_event_message_resp( 2427 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr); 2428 2429 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2430 2431 rc = decode_platform_event_message_resp( 2432 response, responseMsg.size() - hdrSize - 1, &completionCode, 2433 &platformEventStatus); 2434 2435 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2436 } 2437 2438 TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest) 2439 { 2440 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH + 2441 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES> 2442 eventDataArr{}; 2443 uint16_t sensorId = 0x1234; 2444 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE; 2445 2446 struct pldm_sensor_event_data* eventData = 2447 (struct pldm_sensor_event_data*)eventDataArr.data(); 2448 eventData->sensor_id = sensorId; 2449 eventData->sensor_event_class_type = sensorEventClassType; 2450 2451 size_t retSensorOpDataOffset; 2452 uint16_t retSensorId = 0; 2453 uint8_t retSensorEventClassType; 2454 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType); 2455 auto rc = decode_sensor_event_data( 2456 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2457 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(), 2458 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); 2459 EXPECT_EQ(rc, PLDM_SUCCESS); 2460 EXPECT_EQ(retSensorId, sensorId); 2461 EXPECT_EQ(retSensorEventClassType, sensorEventClassType); 2462 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset); 2463 } 2464 2465 TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest) 2466 { 2467 2468 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH + 2469 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES> 2470 eventDataArr{}; 2471 2472 struct pldm_sensor_event_data* eventData = 2473 (struct pldm_sensor_event_data*)eventDataArr.data(); 2474 2475 size_t retSensorOpDataOffset; 2476 uint16_t retSensorId = 0; 2477 uint8_t retSensorEventClassType; 2478 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId, 2479 &retSensorEventClassType, 2480 &retSensorOpDataOffset); 2481 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2482 2483 rc = decode_sensor_event_data( 2484 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2485 reinterpret_cast<uint8_t*>(eventDataArr.data()), 2486 eventDataArr.size() - 2487 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH, 2488 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); 2489 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2490 2491 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE; 2492 2493 rc = decode_sensor_event_data( 2494 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2495 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(), 2496 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); 2497 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2498 2499 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE; 2500 rc = decode_sensor_event_data( 2501 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2502 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(), 2503 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset); 2504 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2505 2506 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE; 2507 rc = decode_sensor_event_data( 2508 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2509 reinterpret_cast<uint8_t*>(eventDataArr.data()), 2510 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType, 2511 &retSensorOpDataOffset); 2512 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2513 } 2514 2515 TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataDecodeRequest) 2516 { 2517 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION + 2518 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID + 2519 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE> 2520 eventData{ 2521 0x1, // version 2522 0x88, 0x77, // Event Id 2523 0x44, 0x33, 0x22, 0x11 // Transfer Handle 2524 }; 2525 2526 uint8_t formatVersion = 0x01; 2527 uint16_t eventID = 0x7788; 2528 uint32_t dataTransferHandle = 0x11223344; 2529 2530 struct pldm_message_poll_event poll_event = {}; 2531 2532 auto rc = decode_pldm_message_poll_event_data( 2533 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2534 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), 2535 &poll_event); 2536 2537 EXPECT_EQ(rc, PLDM_SUCCESS); 2538 EXPECT_EQ(poll_event.format_version, formatVersion); 2539 EXPECT_EQ(poll_event.event_id, eventID); 2540 EXPECT_EQ(poll_event.data_transfer_handle, dataTransferHandle); 2541 } 2542 2543 TEST(PlatformEventMessage, testBadPldmMsgPollEventDataDecodeRequest) 2544 { 2545 2546 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION + 2547 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID + 2548 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE> 2549 eventData{ 2550 0x1, // version 2551 0x88, 0x77, // Event Id 2552 0x44, 0x33, 0x22, 0x11 // Transfer Handle 2553 }; 2554 2555 struct pldm_message_poll_event poll_event = {}; 2556 2557 auto rc = decode_pldm_message_poll_event_data(NULL, eventData.size(), 2558 &poll_event); 2559 EXPECT_EQ(rc, -EINVAL); 2560 2561 rc = decode_pldm_message_poll_event_data( 2562 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2563 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), NULL); 2564 EXPECT_EQ(rc, -EINVAL); 2565 2566 rc = decode_pldm_message_poll_event_data( 2567 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2568 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1, 2569 &poll_event); 2570 EXPECT_EQ(rc, -EOVERFLOW); 2571 2572 // Event id is 0x0000 2573 eventData[1] = 0x00; 2574 eventData[2] = 0x00; 2575 rc = decode_pldm_message_poll_event_data( 2576 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2577 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), 2578 &poll_event); 2579 2580 EXPECT_EQ(rc, -EPROTO); 2581 2582 // Event id is 0xffff 2583 eventData[1] = 0xff; 2584 eventData[2] = 0xff; 2585 rc = decode_pldm_message_poll_event_data( 2586 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2587 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), 2588 &poll_event); 2589 2590 EXPECT_EQ(rc, -EPROTO); 2591 } 2592 2593 #ifdef LIBPLDM_API_TESTING 2594 TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataEncode) 2595 { 2596 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION + 2597 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID + 2598 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE> 2599 eventData{}; 2600 2601 struct pldm_message_poll_event poll_event = {}; 2602 poll_event.format_version = 0x01; 2603 poll_event.event_id = 0x7788; 2604 poll_event.data_transfer_handle = 0x11223344; 2605 2606 int rc = encode_pldm_message_poll_event_data( 2607 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2608 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()), 2609 eventData.size()); 2610 2611 EXPECT_EQ(rc, PLDM_SUCCESS); 2612 2613 struct pldm_msgbuf _buf; 2614 struct pldm_msgbuf* buf = &_buf; 2615 2616 rc = pldm_msgbuf_init_cc( 2617 buf, PLDM_MSG_POLL_EVENT_LENGTH, 2618 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2619 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size()); 2620 EXPECT_EQ(rc, PLDM_SUCCESS); 2621 2622 uint8_t retFormatVersion; 2623 uint16_t reteventID; 2624 uint32_t retDataTransferHandle; 2625 2626 EXPECT_EQ(pldm_msgbuf_extract_uint8(buf, retFormatVersion), PLDM_SUCCESS); 2627 EXPECT_EQ(pldm_msgbuf_extract_uint16(buf, reteventID), PLDM_SUCCESS); 2628 EXPECT_EQ(pldm_msgbuf_extract_uint32(buf, retDataTransferHandle), 2629 PLDM_SUCCESS); 2630 EXPECT_EQ(retFormatVersion, poll_event.format_version); 2631 EXPECT_EQ(reteventID, poll_event.event_id); 2632 EXPECT_EQ(retDataTransferHandle, poll_event.data_transfer_handle); 2633 EXPECT_EQ(pldm_msgbuf_destroy_consumed(buf), PLDM_SUCCESS); 2634 } 2635 #endif 2636 2637 #ifdef LIBPLDM_API_TESTING 2638 TEST(PlatformEventMessage, testBadPldmMsgPollEventDataEncode) 2639 { 2640 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION + 2641 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID + 2642 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE> 2643 eventData{}; 2644 2645 struct pldm_message_poll_event poll_event = {}; 2646 poll_event.format_version = 0x01; 2647 poll_event.event_id = 0x7788; 2648 poll_event.data_transfer_handle = 0x11223344; 2649 2650 int rc = encode_pldm_message_poll_event_data(&poll_event, NULL, 2651 eventData.size()); 2652 EXPECT_EQ(rc, -EINVAL); 2653 2654 poll_event.event_id = 0x0000; 2655 rc = encode_pldm_message_poll_event_data( 2656 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2657 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()), 2658 eventData.size()); 2659 EXPECT_EQ(rc, -EPROTO); 2660 2661 poll_event.event_id = 0xffff; 2662 rc = encode_pldm_message_poll_event_data( 2663 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2664 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()), 2665 eventData.size()); 2666 EXPECT_EQ(rc, -EPROTO); 2667 } 2668 #endif 2669 2670 TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest) 2671 { 2672 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH> 2673 eventDataArr{}; 2674 2675 struct pldm_sensor_event_sensor_op_state* sensorData = 2676 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data(); 2677 uint8_t presentState = PLDM_SENSOR_ENABLED; 2678 uint8_t previousState = PLDM_SENSOR_INITIALIZING; 2679 sensorData->present_op_state = presentState; 2680 sensorData->previous_op_state = previousState; 2681 2682 uint8_t retPresentState; 2683 uint8_t retPreviousState; 2684 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2685 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData), 2686 eventDataArr.size(), &retPresentState, 2687 &retPreviousState); 2688 EXPECT_EQ(rc, PLDM_SUCCESS); 2689 EXPECT_EQ(retPresentState, presentState); 2690 EXPECT_EQ(retPreviousState, previousState); 2691 } 2692 2693 TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest) 2694 { 2695 uint8_t presentOpState; 2696 uint8_t previousOpState; 2697 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH; 2698 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState, 2699 &previousOpState); 2700 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2701 2702 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH> 2703 sensorData{}; 2704 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2705 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()), 2706 sensorDataLength + 1, &presentOpState, 2707 &previousOpState); 2708 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2709 2710 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2711 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()), 2712 sensorDataLength, nullptr, &previousOpState); 2713 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2714 } 2715 2716 TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest) 2717 { 2718 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH> 2719 eventDataArr{}; 2720 2721 struct pldm_sensor_event_state_sensor_state* sensorData = 2722 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data(); 2723 uint8_t sensorOffset = 0x02; 2724 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN; 2725 uint8_t previousEventState = PLDM_SENSOR_INTEST; 2726 sensorData->sensor_offset = sensorOffset; 2727 sensorData->event_state = eventState; 2728 sensorData->previous_event_state = previousEventState; 2729 2730 uint8_t retSensorOffset; 2731 uint8_t retEventState; 2732 uint8_t retPreviousState; 2733 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2734 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData), 2735 eventDataArr.size(), &retSensorOffset, 2736 &retEventState, &retPreviousState); 2737 EXPECT_EQ(rc, PLDM_SUCCESS); 2738 EXPECT_EQ(retSensorOffset, sensorOffset); 2739 EXPECT_EQ(retEventState, eventState); 2740 EXPECT_EQ(retPreviousState, previousEventState); 2741 } 2742 2743 TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest) 2744 { 2745 uint8_t sensorOffset; 2746 uint8_t eventState; 2747 uint8_t previousEventState; 2748 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH; 2749 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset, 2750 &eventState, &previousEventState); 2751 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2752 2753 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH> 2754 sensorData{}; 2755 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2756 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()), 2757 sensorDataLength - 1, &sensorOffset, 2758 &eventState, &previousEventState); 2759 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2760 2761 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2762 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()), 2763 sensorDataLength, &sensorOffset, nullptr, 2764 &previousEventState); 2765 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2766 } 2767 2768 TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest) 2769 { 2770 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH> 2771 eventDataArr{}; 2772 struct pldm_sensor_event_numeric_sensor_state* sensorData = 2773 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data(); 2774 2775 size_t sensorDataLength = 2776 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH; 2777 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN; 2778 uint8_t previousEventState = PLDM_SENSOR_INTEST; 2779 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32; 2780 uint32_t presentReading = 305441741; 2781 sensorData->event_state = eventState; 2782 sensorData->previous_event_state = previousEventState; 2783 sensorData->sensor_data_size = sensorDataSize; 2784 { 2785 uint32_t presentReadingLE = htole32(presentReading); 2786 memcpy(&sensorData->present_reading, &presentReadingLE, 2787 sizeof(presentReadingLE)); 2788 } 2789 2790 uint8_t retEventState; 2791 uint8_t retPreviousEventState; 2792 uint8_t retSensorDataSize; 2793 uint32_t retPresentReading; 2794 2795 auto rc = decode_numeric_sensor_data( 2796 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2797 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength, 2798 &retEventState, &retPreviousEventState, &retSensorDataSize, 2799 &retPresentReading); 2800 EXPECT_EQ(rc, PLDM_SUCCESS); 2801 EXPECT_EQ(retEventState, eventState); 2802 EXPECT_EQ(retPreviousEventState, previousEventState); 2803 EXPECT_EQ(retSensorDataSize, sensorDataSize); 2804 EXPECT_EQ(retPresentReading, presentReading); 2805 2806 int16_t presentReadingNew = -31432; 2807 { 2808 int16_t presentReadingNewLE = htole16(presentReadingNew); 2809 memcpy(&sensorData->present_reading, &presentReadingNewLE, 2810 sizeof(presentReadingNewLE)); 2811 } 2812 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16; 2813 sensorData->sensor_data_size = sensorDataSize; 2814 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH; 2815 2816 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2817 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData), 2818 sensorDataLength, &retEventState, 2819 &retPreviousEventState, &retSensorDataSize, 2820 &retPresentReading); 2821 EXPECT_EQ(rc, PLDM_SUCCESS); 2822 EXPECT_EQ(retEventState, eventState); 2823 EXPECT_EQ(retPreviousEventState, previousEventState); 2824 EXPECT_EQ(retSensorDataSize, sensorDataSize); 2825 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew); 2826 } 2827 2828 TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest) 2829 { 2830 uint8_t eventState; 2831 uint8_t previousEventState; 2832 uint8_t sensorDataSize; 2833 uint32_t presentReading; 2834 size_t sensorDataLength = 2835 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH; 2836 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState, 2837 &previousEventState, &sensorDataSize, 2838 &presentReading); 2839 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2840 2841 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH> 2842 sensorData{}; 2843 rc = decode_numeric_sensor_data( 2844 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2845 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1, 2846 &eventState, &previousEventState, &sensorDataSize, &presentReading); 2847 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2848 2849 struct pldm_sensor_event_numeric_sensor_state* numericSensorData = 2850 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data(); 2851 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8; 2852 rc = decode_numeric_sensor_data( 2853 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2854 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength, 2855 &eventState, &previousEventState, &sensorDataSize, &presentReading); 2856 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2857 2858 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16; 2859 rc = decode_numeric_sensor_data( 2860 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2861 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength, 2862 &eventState, &previousEventState, &sensorDataSize, &presentReading); 2863 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2864 } 2865 2866 TEST(GetNumericEffecterValue, testGoodEncodeRequest) 2867 { 2868 std::vector<uint8_t> requestMsg(hdrSize + 2869 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES); 2870 2871 uint16_t effecter_id = 0xab01; 2872 2873 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2874 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 2875 2876 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request); 2877 2878 struct pldm_get_numeric_effecter_value_req* req = 2879 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2880 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>( 2881 request->payload); 2882 2883 EXPECT_EQ(rc, PLDM_SUCCESS); 2884 EXPECT_EQ(effecter_id, le16toh(req->effecter_id)); 2885 } 2886 2887 TEST(GetNumericEffecterValue, testBadEncodeRequest) 2888 { 2889 std::vector<uint8_t> requestMsg( 2890 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES); 2891 2892 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr); 2893 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2894 } 2895 2896 TEST(GetNumericEffecterValue, testGoodDecodeRequest) 2897 { 2898 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES> 2899 requestMsg{}; 2900 2901 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2902 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 2903 struct pldm_get_numeric_effecter_value_req* req = 2904 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2905 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>( 2906 request->payload); 2907 2908 uint16_t effecter_id = 0x12ab; 2909 req->effecter_id = htole16(effecter_id); 2910 2911 uint16_t reteffecter_id; 2912 2913 auto rc = decode_get_numeric_effecter_value_req( 2914 request, requestMsg.size() - hdrSize, &reteffecter_id); 2915 2916 EXPECT_EQ(rc, PLDM_SUCCESS); 2917 EXPECT_EQ(effecter_id, reteffecter_id); 2918 } 2919 2920 TEST(GetNumericEffecterValue, testBadDecodeRequest) 2921 { 2922 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES> 2923 requestMsg{}; 2924 2925 auto rc = decode_get_numeric_effecter_value_req( 2926 nullptr, requestMsg.size() - hdrSize, nullptr); 2927 2928 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 2929 2930 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2931 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 2932 struct pldm_set_numeric_effecter_value_req* req = 2933 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2934 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>( 2935 request->payload); 2936 2937 uint16_t effecter_id = 0x1a; 2938 req->effecter_id = htole16(effecter_id); 2939 uint16_t reteffecter_id; 2940 2941 rc = decode_get_numeric_effecter_value_req( 2942 request, requestMsg.size() - hdrSize - 1, &reteffecter_id); 2943 2944 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 2945 } 2946 2947 TEST(GetNumericEffecterValue, testGoodEncodeResponse) 2948 { 2949 uint8_t completionCode = 0; 2950 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32; 2951 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING; 2952 uint32_t pendingValue = 0x12345678; 2953 uint32_t presentValue = 0xabcdef11; 2954 uint32_t val_pending; 2955 uint32_t val_present; 2956 2957 std::array<uint8_t, 2958 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6> 2959 responseMsg{}; 2960 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2961 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2962 2963 auto rc = encode_get_numeric_effecter_value_resp( 2964 0, completionCode, effecter_dataSize, effecter_operState, 2965 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2966 reinterpret_cast<uint8_t*>(&pendingValue), 2967 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2968 reinterpret_cast<uint8_t*>(&presentValue), response, 2969 responseMsg.size() - hdrSize); 2970 2971 struct pldm_get_numeric_effecter_value_resp* resp = 2972 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2973 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( 2974 response->payload); 2975 2976 memcpy(&val_pending, &resp->pending_and_present_values[0], 2977 sizeof(val_pending)); 2978 val_pending = le32toh(val_pending); 2979 memcpy(&val_present, &resp->pending_and_present_values[4], 2980 sizeof(val_present)); 2981 val_present = le32toh(val_present); 2982 2983 EXPECT_EQ(rc, PLDM_SUCCESS); 2984 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size); 2985 EXPECT_EQ(effecter_operState, resp->effecter_oper_state); 2986 EXPECT_EQ(pendingValue, val_pending); 2987 EXPECT_EQ(presentValue, val_present); 2988 } 2989 2990 TEST(GetNumericEffecterValue, testBadEncodeResponse) 2991 { 2992 std::array<uint8_t, 2993 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2> 2994 responseMsg{}; 2995 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 2996 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 2997 2998 uint8_t pendingValue = 0x01; 2999 uint8_t presentValue = 0x02; 3000 3001 auto rc = encode_get_numeric_effecter_value_resp( 3002 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr, 3003 responseMsg.size() - hdrSize); 3004 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3005 3006 rc = encode_get_numeric_effecter_value_resp( 3007 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3008 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue), 3009 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3010 reinterpret_cast<uint8_t*>(&presentValue), response, 3011 responseMsg.size() - hdrSize); 3012 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3013 3014 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; 3015 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED; 3016 3017 rc = encode_get_numeric_effecter_value_resp( 3018 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState, 3019 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3020 reinterpret_cast<uint8_t*>(&pendingValue), 3021 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3022 reinterpret_cast<uint8_t*>(&presentValue), response, 3023 responseMsg.size() - hdrSize); 3024 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3025 } 3026 3027 TEST(GetNumericEffecterValue, testGoodDecodeResponse) 3028 { 3029 std::array<uint8_t, 3030 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2> 3031 responseMsg{}; 3032 3033 uint8_t completionCode = 0; 3034 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16; 3035 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING; 3036 uint16_t pendingValue = 0x4321; 3037 uint16_t presentValue = 0xdcba; 3038 3039 uint8_t retcompletionCode; 3040 uint8_t reteffecter_dataSize; 3041 uint8_t reteffecter_operState; 3042 uint8_t retpendingValue[2]; 3043 uint8_t retpresentValue[2]; 3044 3045 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3046 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3047 struct pldm_get_numeric_effecter_value_resp* resp = 3048 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3049 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( 3050 response->payload); 3051 3052 resp->completion_code = completionCode; 3053 resp->effecter_data_size = effecter_dataSize; 3054 resp->effecter_oper_state = effecter_operState; 3055 3056 uint16_t pendingValue_le = htole16(pendingValue); 3057 memcpy(resp->pending_and_present_values, &pendingValue_le, 3058 sizeof(pendingValue_le)); 3059 uint16_t presentValue_le = htole16(presentValue); 3060 memcpy(&resp->pending_and_present_values[2], &presentValue_le, 3061 sizeof(presentValue_le)); 3062 3063 auto rc = decode_get_numeric_effecter_value_resp( 3064 response, responseMsg.size() - hdrSize, &retcompletionCode, 3065 &reteffecter_dataSize, &reteffecter_operState, retpendingValue, 3066 retpresentValue); 3067 3068 EXPECT_EQ(rc, PLDM_SUCCESS); 3069 EXPECT_EQ(completionCode, retcompletionCode); 3070 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize); 3071 EXPECT_EQ(effecter_operState, reteffecter_operState); 3072 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3073 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue))); 3074 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3075 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue))); 3076 } 3077 3078 TEST(GetNumericEffecterValue, testBadDecodeResponse) 3079 { 3080 std::array<uint8_t, 3081 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6> 3082 responseMsg{}; 3083 3084 auto rc = decode_get_numeric_effecter_value_resp( 3085 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr, 3086 nullptr, nullptr); 3087 3088 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3089 3090 uint8_t completionCode = 0; 3091 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16; 3092 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED; 3093 uint16_t pendingValue = 0x5678; 3094 uint16_t presentValue = 0xcdef; 3095 3096 uint8_t retcompletionCode; 3097 uint8_t reteffecter_dataSize; 3098 uint8_t reteffecter_operState; 3099 uint8_t retpendingValue[2]; 3100 uint8_t retpresentValue[2]; 3101 3102 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3103 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3104 struct pldm_get_numeric_effecter_value_resp* resp = 3105 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3106 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>( 3107 response->payload); 3108 3109 resp->completion_code = completionCode; 3110 resp->effecter_data_size = effecter_dataSize; 3111 resp->effecter_oper_state = effecter_operState; 3112 3113 uint16_t pendingValue_le = htole16(pendingValue); 3114 memcpy(resp->pending_and_present_values, &pendingValue_le, 3115 sizeof(pendingValue_le)); 3116 uint16_t presentValue_le = htole16(presentValue); 3117 memcpy(&resp->pending_and_present_values[2], &presentValue_le, 3118 sizeof(presentValue_le)); 3119 3120 rc = decode_get_numeric_effecter_value_resp( 3121 response, responseMsg.size() - hdrSize, &retcompletionCode, 3122 &reteffecter_dataSize, &reteffecter_operState, retpendingValue, 3123 retpresentValue); 3124 3125 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3126 } 3127 3128 TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest) 3129 { 3130 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES; 3131 const uint8_t numberOfChangeRecords = 2; 3132 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED; 3133 const uint8_t numberOfChangeEntries1 = 2; 3134 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{ 3135 {0x00000000, 0x12345678}}; 3136 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED; 3137 const uint8_t numberOfChangeEntries2 = 5; 3138 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{ 3139 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}}; 3140 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH + 3141 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH * 3142 numberOfChangeRecords + 3143 (numberOfChangeEntries1 + numberOfChangeEntries2) * 3144 sizeof(uint32_t)> 3145 eventDataArr{}; 3146 3147 struct pldm_pdr_repository_chg_event_data* eventData = 3148 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3149 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>( 3150 eventDataArr.data()); 3151 eventData->event_data_format = eventDataFormat; 3152 eventData->number_of_change_records = numberOfChangeRecords; 3153 struct pldm_pdr_repository_change_record_data* changeRecord1 = 3154 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3155 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>( 3156 eventData->change_records); 3157 changeRecord1->event_data_operation = eventDataOperation1; 3158 changeRecord1->number_of_change_entries = numberOfChangeEntries1; 3159 memcpy(changeRecord1->change_entry, &changeRecordArr1[0], 3160 changeRecordArr1.size() * sizeof(uint32_t)); 3161 struct pldm_pdr_repository_change_record_data* changeRecord2 = 3162 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3163 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>( 3164 eventData->change_records + 3165 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH + 3166 (changeRecordArr1.size() * sizeof(uint32_t))); 3167 changeRecord2->event_data_operation = eventDataOperation2; 3168 changeRecord2->number_of_change_entries = numberOfChangeEntries2; 3169 memcpy(changeRecord2->change_entry, &changeRecordArr2[0], 3170 changeRecordArr2.size() * sizeof(uint32_t)); 3171 3172 uint8_t retEventDataFormat{}; 3173 uint8_t retNumberOfChangeRecords{}; 3174 size_t retChangeRecordDataOffset{0}; 3175 auto rc = decode_pldm_pdr_repository_chg_event_data( 3176 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3177 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(), 3178 &retEventDataFormat, &retNumberOfChangeRecords, 3179 &retChangeRecordDataOffset); 3180 EXPECT_EQ(rc, PLDM_SUCCESS); 3181 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES); 3182 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords); 3183 3184 const uint8_t* changeRecordData = 3185 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3186 reinterpret_cast<const uint8_t*>(changeRecord1); 3187 size_t changeRecordDataSize = 3188 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH; 3189 uint8_t retEventDataOperation; 3190 uint8_t retNumberOfChangeEntries; 3191 size_t retChangeEntryDataOffset; 3192 3193 rc = decode_pldm_pdr_repository_change_record_data( 3194 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3195 reinterpret_cast<const uint8_t*>(changeRecordData), 3196 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries, 3197 &retChangeEntryDataOffset); 3198 EXPECT_EQ(rc, PLDM_SUCCESS); 3199 EXPECT_EQ(retEventDataOperation, eventDataOperation1); 3200 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1); 3201 changeRecordData += retChangeEntryDataOffset; 3202 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0], 3203 sizeof(uint32_t) * retNumberOfChangeEntries)); 3204 3205 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries; 3206 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries - 3207 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH; 3208 rc = decode_pldm_pdr_repository_change_record_data( 3209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3210 reinterpret_cast<const uint8_t*>(changeRecordData), 3211 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries, 3212 &retChangeEntryDataOffset); 3213 EXPECT_EQ(rc, PLDM_SUCCESS); 3214 EXPECT_EQ(retEventDataOperation, eventDataOperation2); 3215 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2); 3216 changeRecordData += retChangeEntryDataOffset; 3217 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0], 3218 sizeof(uint32_t) * retNumberOfChangeEntries)); 3219 } 3220 3221 TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest) 3222 { 3223 uint8_t eventDataFormat{}; 3224 uint8_t numberOfChangeRecords{}; 3225 size_t changeRecordDataOffset{}; 3226 auto rc = decode_pldm_pdr_repository_chg_event_data( 3227 NULL, 0, &eventDataFormat, &numberOfChangeRecords, 3228 &changeRecordDataOffset); 3229 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3230 3231 std::array<uint8_t, 2> eventData{}; 3232 rc = decode_pldm_pdr_repository_chg_event_data( 3233 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3234 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat, 3235 &numberOfChangeRecords, &changeRecordDataOffset); 3236 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3237 3238 uint8_t eventDataOperation{}; 3239 uint8_t numberOfChangeEntries{}; 3240 size_t changeEntryDataOffset{}; 3241 rc = decode_pldm_pdr_repository_change_record_data( 3242 NULL, 0, &eventDataOperation, &numberOfChangeEntries, 3243 &changeEntryDataOffset); 3244 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3245 3246 std::array<uint8_t, 2> changeRecord{}; 3247 rc = decode_pldm_pdr_repository_change_record_data( 3248 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3249 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0, 3250 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset); 3251 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3252 } 3253 3254 TEST(GetSensorReading, testGoodEncodeRequest) 3255 { 3256 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> 3257 requestMsg{}; 3258 3259 uint16_t sensorId = 0x1234; 3260 bool8_t rearmEventState = 0x01; 3261 3262 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3263 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 3264 auto rc = 3265 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request); 3266 3267 struct pldm_get_sensor_reading_req* req = 3268 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3269 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); 3270 3271 EXPECT_EQ(rc, PLDM_SUCCESS); 3272 EXPECT_EQ(sensorId, le16toh(req->sensor_id)); 3273 EXPECT_EQ(rearmEventState, req->rearm_event_state); 3274 } 3275 3276 TEST(GetSensorReading, testBadEncodeRequest) 3277 { 3278 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr); 3279 3280 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3281 } 3282 3283 TEST(GetSensorReading, testGoodDecodeRequest) 3284 { 3285 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> 3286 requestMsg{}; 3287 3288 uint16_t sensorId = 0xabcd; 3289 bool8_t rearmEventState = 0xa; 3290 3291 uint16_t retsensorId; 3292 bool8_t retrearmEventState; 3293 3294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3295 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 3296 3297 struct pldm_get_sensor_reading_req* req = 3298 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3299 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); 3300 3301 req->sensor_id = htole16(sensorId); 3302 req->rearm_event_state = rearmEventState; 3303 3304 auto rc = 3305 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize, 3306 &retsensorId, &retrearmEventState); 3307 3308 EXPECT_EQ(rc, PLDM_SUCCESS); 3309 EXPECT_EQ(sensorId, retsensorId); 3310 EXPECT_EQ(rearmEventState, retrearmEventState); 3311 } 3312 3313 TEST(GetSensorReading, testBadDecodeRequest) 3314 { 3315 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES> 3316 requestMsg{}; 3317 3318 auto rc = decode_get_sensor_reading_req( 3319 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr); 3320 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3321 3322 uint16_t sensorId = 0xabcd; 3323 bool8_t rearmEventState = 0xa; 3324 3325 uint16_t retsensorId; 3326 bool8_t retrearmEventState; 3327 3328 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3329 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 3330 3331 struct pldm_get_sensor_reading_req* req = 3332 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3333 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload); 3334 3335 req->sensor_id = htole16(sensorId); 3336 req->rearm_event_state = rearmEventState; 3337 3338 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1, 3339 &retsensorId, &retrearmEventState); 3340 3341 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3342 } 3343 3344 TEST(GetSensorReading, testGoodEncodeResponse) 3345 { 3346 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES> 3347 responseMsg{}; 3348 3349 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3350 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3351 3352 uint8_t completionCode = 0; 3353 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; 3354 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED; 3355 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION; 3356 uint8_t presentState = PLDM_SENSOR_NORMAL; 3357 uint8_t previousState = PLDM_SENSOR_WARNING; 3358 uint8_t eventState = PLDM_SENSOR_UPPERWARNING; 3359 uint8_t presentReading = 0x21; 3360 3361 auto rc = encode_get_sensor_reading_resp( 3362 0, completionCode, sensor_dataSize, sensor_operationalState, 3363 sensor_event_messageEnable, presentState, previousState, eventState, 3364 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3365 reinterpret_cast<uint8_t*>(&presentReading), response, 3366 responseMsg.size() - hdrSize); 3367 3368 struct pldm_get_sensor_reading_resp* resp = 3369 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3370 reinterpret_cast<struct pldm_get_sensor_reading_resp*>( 3371 response->payload); 3372 3373 EXPECT_EQ(rc, PLDM_SUCCESS); 3374 EXPECT_EQ(completionCode, resp->completion_code); 3375 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size); 3376 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state); 3377 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable); 3378 EXPECT_EQ(presentState, resp->present_state); 3379 EXPECT_EQ(previousState, resp->previous_state); 3380 EXPECT_EQ(eventState, resp->event_state); 3381 EXPECT_EQ(presentReading, 3382 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3383 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0]))); 3384 } 3385 3386 TEST(GetSensorReading, testBadEncodeResponse) 3387 { 3388 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3> 3389 responseMsg{}; 3390 3391 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3392 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3393 3394 uint8_t presentReading = 0x1; 3395 3396 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0, 3397 nullptr, nullptr, 3398 responseMsg.size() - hdrSize); 3399 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3400 3401 rc = encode_get_sensor_reading_resp( 3402 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1, 3403 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3404 reinterpret_cast<uint8_t*>(&presentReading), response, 3405 responseMsg.size() - hdrSize); 3406 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3407 3408 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; 3409 3410 rc = encode_get_sensor_reading_resp( 3411 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1, 3412 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3413 reinterpret_cast<uint8_t*>(&presentReading), response, 3414 responseMsg.size() - hdrSize); 3415 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3416 } 3417 3418 TEST(GetSensorReading, testGoodDecodeResponse) 3419 { 3420 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3> 3421 responseMsg{}; 3422 3423 uint8_t completionCode = 0; 3424 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32; 3425 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN; 3426 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED; 3427 uint8_t presentState = PLDM_SENSOR_CRITICAL; 3428 uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL; 3429 uint8_t eventState = PLDM_SENSOR_WARNING; 3430 uint32_t presentReading = 0xabcdef11; 3431 3432 uint8_t retcompletionCode; 3433 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32; 3434 uint8_t retsensor_operationalState; 3435 uint8_t retsensor_event_messageEnable; 3436 uint8_t retpresentState; 3437 uint8_t retpreviousState; 3438 uint8_t reteventState; 3439 uint8_t retpresentReading[4]; 3440 3441 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3442 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3443 struct pldm_get_sensor_reading_resp* resp = 3444 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3445 reinterpret_cast<struct pldm_get_sensor_reading_resp*>( 3446 response->payload); 3447 3448 resp->completion_code = completionCode; 3449 resp->sensor_data_size = sensor_dataSize; 3450 resp->sensor_operational_state = sensor_operationalState; 3451 resp->sensor_event_message_enable = sensor_event_messageEnable; 3452 resp->present_state = presentState; 3453 resp->previous_state = previousState; 3454 resp->event_state = eventState; 3455 3456 uint32_t presentReading_le = htole32(presentReading); 3457 memcpy(resp->present_reading, &presentReading_le, 3458 sizeof(presentReading_le)); 3459 3460 auto rc = decode_get_sensor_reading_resp( 3461 response, responseMsg.size() - hdrSize, &retcompletionCode, 3462 &retsensor_dataSize, &retsensor_operationalState, 3463 &retsensor_event_messageEnable, &retpresentState, &retpreviousState, 3464 &reteventState, retpresentReading); 3465 3466 EXPECT_EQ(rc, PLDM_SUCCESS); 3467 EXPECT_EQ(completionCode, retcompletionCode); 3468 EXPECT_EQ(sensor_dataSize, retsensor_dataSize); 3469 EXPECT_EQ(sensor_operationalState, retsensor_operationalState); 3470 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable); 3471 EXPECT_EQ(presentState, retpresentState); 3472 EXPECT_EQ(previousState, retpreviousState); 3473 EXPECT_EQ(eventState, reteventState); 3474 EXPECT_EQ(presentReading, 3475 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3476 *(reinterpret_cast<uint32_t*>(retpresentReading))); 3477 } 3478 3479 TEST(GetSensorReading, testBadDecodeResponse) 3480 { 3481 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1> 3482 responseMsg{}; 3483 3484 auto rc = decode_get_sensor_reading_resp( 3485 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr, 3486 nullptr, nullptr, nullptr, nullptr, nullptr); 3487 3488 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3489 3490 uint8_t completionCode = 0; 3491 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8; 3492 uint8_t sensor_operationalState = PLDM_SENSOR_INTEST; 3493 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED; 3494 uint8_t presentState = PLDM_SENSOR_FATAL; 3495 uint8_t previousState = PLDM_SENSOR_UPPERFATAL; 3496 uint8_t eventState = PLDM_SENSOR_WARNING; 3497 uint8_t presentReading = 0xa; 3498 3499 uint8_t retcompletionCode; 3500 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16; 3501 uint8_t retsensor_operationalState; 3502 uint8_t retsensor_event_messageEnable; 3503 uint8_t retpresent_state; 3504 uint8_t retprevious_state; 3505 uint8_t retevent_state; 3506 uint8_t retpresentReading; 3507 3508 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3509 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3510 struct pldm_get_sensor_reading_resp* resp = 3511 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3512 reinterpret_cast<struct pldm_get_sensor_reading_resp*>( 3513 response->payload); 3514 3515 resp->completion_code = completionCode; 3516 resp->sensor_data_size = sensor_dataSize; 3517 resp->sensor_operational_state = sensor_operationalState; 3518 resp->sensor_event_message_enable = sensor_event_messageEnable; 3519 resp->present_state = presentState; 3520 resp->previous_state = previousState; 3521 resp->event_state = eventState; 3522 resp->present_reading[0] = presentReading; 3523 3524 rc = decode_get_sensor_reading_resp( 3525 response, responseMsg.size() - hdrSize, &retcompletionCode, 3526 &retsensor_dataSize, &retsensor_operationalState, 3527 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state, 3528 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3529 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading)); 3530 3531 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3532 } 3533 3534 TEST(SetEventReceiver, testGoodEncodeRequest) 3535 { 3536 uint8_t eventMessageGlobalEnable = 3537 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; 3538 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; 3539 uint8_t eventReceiverAddressInfo = 0x08; 3540 uint16_t heartbeatTimer = 0x78; 3541 3542 PLDM_MSG_DEFINE_P(request, PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3543 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES); 3544 3545 auto rc = encode_set_event_receiver_req( 3546 0, eventMessageGlobalEnable, transportProtocolType, 3547 eventReceiverAddressInfo, heartbeatTimer, request); 3548 3549 EXPECT_EQ(rc, PLDM_SUCCESS); 3550 struct pldm_set_event_receiver_req* req = 3551 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3552 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); 3553 EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable); 3554 EXPECT_EQ(transportProtocolType, req->transport_protocol_type); 3555 EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info); 3556 EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer)); 3557 } 3558 3559 TEST(SetEventReceiver, testBadEncodeRequest) 3560 { 3561 uint8_t eventMessageGlobalEnable = 3562 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; 3563 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; 3564 uint8_t eventReceiverAddressInfo = 0x08; 3565 uint16_t heartbeatTimer = 0; 3566 3567 PLDM_MSG_DEFINE_P(request, PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3568 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES); 3569 3570 auto rc = encode_set_event_receiver_req( 3571 0, eventMessageGlobalEnable, transportProtocolType, 3572 eventReceiverAddressInfo, heartbeatTimer, request); 3573 3574 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3575 } 3576 3577 TEST(SetEventReceiver, testGoodDecodeResponse) 3578 { 3579 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> 3580 responseMsg{}; 3581 3582 uint8_t retcompletion_code = 0; 3583 responseMsg[hdrSize] = PLDM_SUCCESS; 3584 3585 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3586 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3587 auto rc = decode_set_event_receiver_resp( 3588 response, responseMsg.size() - sizeof(pldm_msg_hdr), 3589 &retcompletion_code); 3590 3591 EXPECT_EQ(rc, PLDM_SUCCESS); 3592 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code); 3593 } 3594 3595 TEST(SetEventReceiver, testBadDecodeResponse) 3596 { 3597 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> 3598 responseMsg{}; 3599 uint8_t retcompletion_code = 0; 3600 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3601 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3602 3603 auto rc = decode_set_event_receiver_resp( 3604 response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL); 3605 3606 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3607 3608 rc = decode_set_event_receiver_resp( 3609 nullptr, responseMsg.size() - sizeof(pldm_msg_hdr), 3610 &retcompletion_code); 3611 3612 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3613 } 3614 3615 TEST(SetEventReceiver, testGoodEncodeResponse) 3616 { 3617 std::array<uint8_t, 3618 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES> 3619 responseMsg{}; 3620 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3621 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 3622 uint8_t completionCode = 0; 3623 3624 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response); 3625 3626 EXPECT_EQ(rc, PLDM_SUCCESS); 3627 EXPECT_EQ(completionCode, response->payload[0]); 3628 } 3629 3630 TEST(SetEventReceiver, testBadEncodeResponse) 3631 { 3632 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL); 3633 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3634 } 3635 3636 TEST(SetEventReceiver, testGoodDecodeRequest) 3637 { 3638 PLDM_MSG_DEFINE_P(request, PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3639 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES); 3640 3641 uint8_t eventMessageGlobalEnable = 3642 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; 3643 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; 3644 uint8_t eventReceiverAddressInfo = 0x08; 3645 uint16_t heartbeatTimer = 0x78; 3646 3647 struct pldm_set_event_receiver_req* req = 3648 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3649 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); 3650 3651 req->event_message_global_enable = eventMessageGlobalEnable; 3652 req->transport_protocol_type = transportProtocolType; 3653 req->event_receiver_address_info = eventReceiverAddressInfo; 3654 req->heartbeat_timer = htole16(heartbeatTimer); 3655 3656 uint8_t reteventMessageGlobalEnable; 3657 uint8_t rettransportProtocolType; 3658 uint8_t reteventReceiverAddressInfo; 3659 uint16_t retheartbeatTimer; 3660 auto rc = decode_set_event_receiver_req( 3661 request, 3662 PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3663 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES, 3664 &reteventMessageGlobalEnable, &rettransportProtocolType, 3665 &reteventReceiverAddressInfo, &retheartbeatTimer); 3666 3667 EXPECT_EQ(rc, PLDM_SUCCESS); 3668 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable); 3669 EXPECT_EQ(transportProtocolType, rettransportProtocolType); 3670 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo); 3671 EXPECT_EQ(heartbeatTimer, retheartbeatTimer); 3672 } 3673 3674 TEST(SetEventReceiver, testBadDecodeRequest) 3675 { 3676 PLDM_MSG_DEFINE_P(request, PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3677 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES); 3678 3679 auto rc = decode_set_event_receiver_req( 3680 NULL, 3681 PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3682 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES, 3683 NULL, NULL, NULL, NULL); 3684 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); 3685 3686 uint8_t eventMessageGlobalEnable = 3687 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE; 3688 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP; 3689 uint8_t eventReceiverAddressInfo = 0x08; 3690 uint16_t heartbeatTimer = 0x78; 3691 3692 struct pldm_set_event_receiver_req* req = 3693 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 3694 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload); 3695 3696 req->event_message_global_enable = eventMessageGlobalEnable; 3697 req->transport_protocol_type = transportProtocolType; 3698 req->event_receiver_address_info = eventReceiverAddressInfo; 3699 req->heartbeat_timer = htole16(heartbeatTimer); 3700 3701 uint8_t reteventMessageGlobalEnable; 3702 uint8_t rettransportProtocolType; 3703 uint8_t reteventReceiverAddressInfo; 3704 uint16_t retheartbeatTimer; 3705 3706 #ifdef NDEBUG 3707 rc = decode_set_event_receiver_req( 3708 request, 3709 PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3710 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES - 1, 3711 &reteventMessageGlobalEnable, &rettransportProtocolType, 3712 &reteventReceiverAddressInfo, &retheartbeatTimer); 3713 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 3714 #else 3715 EXPECT_DEATH(decode_set_event_receiver_req( 3716 request, 3717 PLDM_SET_EVENT_RECEIVER_REQ_BYTES + 3718 PLDM_SET_EVENT_RECEIVER_REQ_HEARTBEAT_BYTES - 1, 3719 &reteventMessageGlobalEnable, &rettransportProtocolType, 3720 &reteventReceiverAddressInfo, &retheartbeatTimer), 3721 "ctx->remaining >= 0"); 3722 #endif 3723 } 3724 3725 TEST(decodeNumericSensorPdrData, Uint8Test) 3726 { 3727 std::vector<uint8_t> pdr1{ 3728 0x1, 3729 0x0, 3730 0x0, 3731 0x0, // record handle 3732 0x1, // PDRHeaderVersion 3733 PLDM_NUMERIC_SENSOR_PDR, // PDRType 3734 0x0, 3735 0x0, // recordChangeNumber 3736 PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, 3737 0, // dataLength 3738 0, 3739 0, // PLDMTerminusHandle 3740 0x1, 3741 0x0, // sensorID=1 3742 PLDM_ENTITY_POWER_SUPPLY, 3743 0, // entityType=Power Supply(120) 3744 1, 3745 0, // entityInstanceNumber 3746 1, 3747 0, // containerID=1 3748 PLDM_NO_INIT, // sensorInit 3749 false, // sensorAuxiliaryNamesPDR 3750 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 3751 0, // unitModifier 3752 0, // rateUnit 3753 0, // baseOEMUnitHandle 3754 0, // auxUnit 3755 0, // auxUnitModifier 3756 0, // auxRateUnit 3757 0, // rel 3758 0, // auxOEMUnitHandle 3759 true, // isLinear 3760 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize 3761 0, 3762 0, 3763 0xc0, 3764 0x3f, // resolution=1.5 3765 0, 3766 0, 3767 0x80, 3768 0x3f, // offset=1.0 3769 0, 3770 0, // accuracy 3771 0, // plusTolerance 3772 0, // minusTolerance 3773 3, // hysteresis = 3 3774 0, // supportedThresholds 3775 0, // thresholdAndHysteresisVolatility 3776 0, 3777 0, 3778 0x80, 3779 0x3f, // stateTransistionInterval=1.0 3780 0, 3781 0, 3782 0x80, 3783 0x3f, // updateInverval=1.0 3784 255, // maxReadable 3785 0, // minReadable 3786 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat 3787 0, // rangeFieldsupport 3788 50, // nominalValue = 50 3789 60, // normalMax = 60 3790 40, // normalMin = 40 3791 70, // warningHigh = 70 3792 30, // warningLow = 30 3793 80, // criticalHigh = 80 3794 20, // criticalLow = 20 3795 90, // fatalHigh = 90 3796 10 // fatalLow = 10 3797 }; 3798 3799 struct pldm_numeric_sensor_value_pdr decodedPdr; 3800 auto rc = 3801 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 3802 EXPECT_EQ(PLDM_SUCCESS, rc); 3803 EXPECT_EQ(1, decodedPdr.hdr.record_handle); 3804 EXPECT_EQ(1, decodedPdr.hdr.version); 3805 EXPECT_EQ(PLDM_NUMERIC_SENSOR_PDR, decodedPdr.hdr.type); 3806 EXPECT_EQ(0, decodedPdr.hdr.record_change_num); 3807 EXPECT_EQ(PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, decodedPdr.hdr.length); 3808 EXPECT_EQ(1, decodedPdr.sensor_id); 3809 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type); 3810 EXPECT_EQ(1, decodedPdr.entity_instance_num); 3811 EXPECT_EQ(1, decodedPdr.container_id); 3812 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.sensor_init); 3813 EXPECT_EQ(false, decodedPdr.sensor_auxiliary_names_pdr); 3814 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit); 3815 EXPECT_EQ(0, decodedPdr.unit_modifier); 3816 EXPECT_EQ(0, decodedPdr.rate_unit); 3817 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle); 3818 EXPECT_EQ(0, decodedPdr.aux_unit); 3819 EXPECT_EQ(0, decodedPdr.aux_unit_modifier); 3820 EXPECT_EQ(0, decodedPdr.aux_rate_unit); 3821 EXPECT_EQ(0, decodedPdr.rel); 3822 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle); 3823 EXPECT_EQ(true, decodedPdr.is_linear); 3824 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT8, decodedPdr.sensor_data_size); 3825 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution); 3826 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset); 3827 EXPECT_EQ(0, decodedPdr.accuracy); 3828 EXPECT_EQ(0, decodedPdr.plus_tolerance); 3829 EXPECT_EQ(0, decodedPdr.minus_tolerance); 3830 EXPECT_EQ(3, decodedPdr.hysteresis.value_u8); 3831 EXPECT_EQ(0, decodedPdr.supported_thresholds.byte); 3832 EXPECT_EQ(0, decodedPdr.threshold_and_hysteresis_volatility.byte); 3833 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval); 3834 EXPECT_FLOAT_EQ(1.0f, decodedPdr.update_interval); 3835 EXPECT_EQ(255, decodedPdr.max_readable.value_u8); 3836 EXPECT_EQ(0, decodedPdr.min_readable.value_u8); 3837 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format); 3838 EXPECT_EQ(0, decodedPdr.range_field_support.byte); 3839 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8); 3840 EXPECT_EQ(60, decodedPdr.normal_max.value_u8); 3841 EXPECT_EQ(40, decodedPdr.normal_min.value_u8); 3842 EXPECT_EQ(70, decodedPdr.warning_high.value_u8); 3843 EXPECT_EQ(30, decodedPdr.warning_low.value_u8); 3844 EXPECT_EQ(80, decodedPdr.critical_high.value_u8); 3845 EXPECT_EQ(20, decodedPdr.critical_low.value_u8); 3846 EXPECT_EQ(90, decodedPdr.fatal_high.value_u8); 3847 EXPECT_EQ(10, decodedPdr.fatal_low.value_u8); 3848 } 3849 3850 TEST(decodeNumericSensorPdrData, Sint8Test) 3851 { 3852 std::vector<uint8_t> pdr1{ 3853 0x1, 3854 0x0, 3855 0x0, 3856 0x0, // record handle 3857 0x1, // PDRHeaderVersion 3858 PLDM_NUMERIC_SENSOR_PDR, // PDRType 3859 0x0, 3860 0x0, // recordChangeNumber 3861 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 3862 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH + 3863 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH, 3864 0, // dataLength 3865 0, 3866 0, // PLDMTerminusHandle 3867 0x1, 3868 0x0, // sensorID=1 3869 PLDM_ENTITY_POWER_SUPPLY, 3870 0, // entityType=Power Supply(120) 3871 1, 3872 0, // entityInstanceNumber 3873 0x1, 3874 0x0, // containerID=1 3875 PLDM_NO_INIT, // sensorInit 3876 false, // sensorAuxiliaryNamesPDR 3877 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 3878 0, // unitModifier 3879 0, // rateUnit 3880 0, // baseOEMUnitHandle 3881 0, // auxUnit 3882 0, // auxUnitModifier 3883 0, // auxRateUnit 3884 0, // rel 3885 0, // auxOEMUnitHandle 3886 true, // isLinear 3887 PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize 3888 0, 3889 0, 3890 0, 3891 0, // resolution 3892 0, 3893 0, 3894 0, 3895 0, // offset 3896 0, 3897 0, // accuracy 3898 0, // plusTolerance 3899 0, // minusTolerance 3900 3, // hysteresis = 3 3901 0, // supportedThresholds 3902 0, // thresholdAndHysteresisVolatility 3903 0, 3904 0, 3905 0x80, 3906 0x3f, // stateTransistionInterval=1.0 3907 0, 3908 0, 3909 0x80, 3910 0x3f, // updateInverval=1.0 3911 0x64, // maxReadable = 100 3912 0x9c, // minReadable = -100 3913 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat 3914 0, // rangeFieldsupport 3915 0, // nominalValue = 0 3916 5, // normalMax = 5 3917 0xfb, // normalMin = -5 3918 10, // warningHigh = 10 3919 0xf6, // warningLow = -10 3920 20, // criticalHigh = 20 3921 0xec, // criticalLow = -20 3922 30, // fatalHigh = 30 3923 0xe2 // fatalLow = -30 3924 }; 3925 3926 struct pldm_numeric_sensor_value_pdr decodedPdr; 3927 auto rc = 3928 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 3929 EXPECT_EQ(PLDM_SUCCESS, rc); 3930 3931 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT8, decodedPdr.sensor_data_size); 3932 EXPECT_EQ(100, decodedPdr.max_readable.value_s8); 3933 EXPECT_EQ(-100, decodedPdr.min_readable.value_s8); 3934 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format); 3935 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8); 3936 EXPECT_EQ(5, decodedPdr.normal_max.value_s8); 3937 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8); 3938 EXPECT_EQ(10, decodedPdr.warning_high.value_s8); 3939 EXPECT_EQ(-10, decodedPdr.warning_low.value_s8); 3940 EXPECT_EQ(20, decodedPdr.critical_high.value_s8); 3941 EXPECT_EQ(-20, decodedPdr.critical_low.value_s8); 3942 EXPECT_EQ(30, decodedPdr.fatal_high.value_s8); 3943 EXPECT_EQ(-30, decodedPdr.fatal_low.value_s8); 3944 } 3945 3946 TEST(decodeNumericSensorPdrData, Uint16Test) 3947 { 3948 std::vector<uint8_t> pdr1{ 3949 0x1, 3950 0x0, 3951 0x0, 3952 0x0, // record handle 3953 0x1, // PDRHeaderVersion 3954 PLDM_NUMERIC_SENSOR_PDR, // PDRType 3955 0x0, 3956 0x0, // recordChangeNumber 3957 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 3958 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 + 3959 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2, 3960 0, // dataLength 3961 0, 3962 0, // PLDMTerminusHandle 3963 0x1, 3964 0x0, // sensorID=1 3965 PLDM_ENTITY_POWER_SUPPLY, 3966 0, // entityType=Power Supply(120) 3967 1, 3968 0, // entityInstanceNumber 3969 0x1, 3970 0x0, // containerID=1 3971 PLDM_NO_INIT, // sensorInit 3972 false, // sensorAuxiliaryNamesPDR 3973 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 3974 0, // unitModifier 3975 0, // rateUnit 3976 0, // baseOEMUnitHandle 3977 0, // auxUnit 3978 0, // auxUnitModifier 3979 0, // auxRateUnit 3980 0, // rel 3981 0, // auxOEMUnitHandle 3982 true, // isLinear 3983 PLDM_SENSOR_DATA_SIZE_UINT16, // sensorDataSize 3984 0, 3985 0, 3986 0, 3987 0, // resolution 3988 0, 3989 0, 3990 0, 3991 0, // offset 3992 0, 3993 0, // accuracy 3994 0, // plusTolerance 3995 0, // minusTolerance 3996 3, 3997 0, // hysteresis = 3 3998 0, // supportedThresholds 3999 0, // thresholdAndHysteresisVolatility 4000 0, 4001 0, 4002 0x80, 4003 0x3f, // stateTransistionInterval=1.0 4004 0, 4005 0, 4006 0x80, 4007 0x3f, // updateInverval=1.0 4008 0, 4009 0x10, // maxReadable = 4096 4010 0, 4011 0, // minReadable = 0 4012 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat 4013 0, // rangeFieldsupport 4014 0x88, 4015 0x13, // nominalValue = 5,000 4016 0x70, 4017 0x17, // normalMax = 6,000 4018 0xa0, 4019 0x0f, // normalMin = 4,000 4020 0x58, 4021 0x1b, // warningHigh = 7,000 4022 0xb8, 4023 0x0b, // warningLow = 3,000 4024 0x40, 4025 0x1f, // criticalHigh = 8,000 4026 0xd0, 4027 0x07, // criticalLow = 2,000 4028 0x28, 4029 0x23, // fatalHigh = 9,000 4030 0xe8, 4031 0x03 // fatalLow = 1,000 4032 }; 4033 4034 struct pldm_numeric_sensor_value_pdr decodedPdr; 4035 auto rc = 4036 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4037 EXPECT_EQ(PLDM_SUCCESS, rc); 4038 4039 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT16, decodedPdr.sensor_data_size); 4040 EXPECT_EQ(4096, decodedPdr.max_readable.value_u16); 4041 EXPECT_EQ(0, decodedPdr.min_readable.value_u16); 4042 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format); 4043 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16); 4044 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16); 4045 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16); 4046 EXPECT_EQ(7000, decodedPdr.warning_high.value_u16); 4047 EXPECT_EQ(3000, decodedPdr.warning_low.value_u16); 4048 EXPECT_EQ(8000, decodedPdr.critical_high.value_u16); 4049 EXPECT_EQ(2000, decodedPdr.critical_low.value_u16); 4050 EXPECT_EQ(9000, decodedPdr.fatal_high.value_u16); 4051 EXPECT_EQ(1000, decodedPdr.fatal_low.value_u16); 4052 } 4053 4054 TEST(decodeNumericSensorPdrData, Sint16Test) 4055 { 4056 std::vector<uint8_t> pdr1{ 4057 0x1, 4058 0x0, 4059 0x0, 4060 0x0, // record handle 4061 0x1, // PDRHeaderVersion 4062 PLDM_NUMERIC_SENSOR_PDR, // PDRType 4063 0x0, 4064 0x0, // recordChangeNumber 4065 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 4066 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 + 4067 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2, 4068 0, // dataLength 4069 0, 4070 0, // PLDMTerminusHandle 4071 0x1, 4072 0x0, // sensorID=1 4073 PLDM_ENTITY_POWER_SUPPLY, 4074 0, // entityType=Power Supply(120) 4075 1, 4076 0, // entityInstanceNumber 4077 0x1, 4078 0x0, // containerID=1 4079 PLDM_NO_INIT, // sensorInit 4080 false, // sensorAuxiliaryNamesPDR 4081 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4082 0, // unitModifier 4083 0, // rateUnit 4084 0, // baseOEMUnitHandle 4085 0, // auxUnit 4086 0, // auxUnitModifier 4087 0, // auxRateUnit 4088 0, // rel 4089 0, // auxOEMUnitHandle 4090 true, // isLinear 4091 PLDM_SENSOR_DATA_SIZE_SINT16, // sensorDataSize 4092 0, 4093 0, 4094 0, 4095 0, // resolution 4096 0, 4097 0, 4098 0, 4099 0, // offset 4100 0, 4101 0, // accuracy 4102 0, // plusTolerance 4103 0, // minusTolerance 4104 3, 4105 0, // hysteresis 4106 0, // supportedThresholds 4107 0, // thresholdAndHysteresisVolatility 4108 0, 4109 0, 4110 0x80, 4111 0x3f, // stateTransistionInterval=1.0 4112 0, 4113 0, 4114 0x80, 4115 0x3f, // updateInverval=1.0 4116 0xe8, 4117 0x03, // maxReadable = 1000 4118 0x18, 4119 0xfc, // minReadable = -1000 4120 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat 4121 0, // rangeFieldsupport 4122 0, 4123 0, // nominalValue = 0 4124 0xf4, 4125 0x01, // normalMax = 500 4126 0x0c, 4127 0xfe, // normalMin = -500 4128 0xe8, 4129 0x03, // warningHigh = 1,000 4130 0x18, 4131 0xfc, // warningLow = -1,000 4132 0xd0, 4133 0x07, // criticalHigh = 2,000 4134 0x30, 4135 0xf8, // criticalLow = -2,000 4136 0xb8, 4137 0x0b, // fatalHigh = 3,000 4138 0x48, 4139 0xf4 // fatalLow = -3,000 4140 }; 4141 4142 struct pldm_numeric_sensor_value_pdr decodedPdr; 4143 auto rc = 4144 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4145 EXPECT_EQ(PLDM_SUCCESS, rc); 4146 4147 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT16, decodedPdr.sensor_data_size); 4148 EXPECT_EQ(1000, decodedPdr.max_readable.value_s16); 4149 EXPECT_EQ(-1000, decodedPdr.min_readable.value_s16); 4150 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format); 4151 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16); 4152 EXPECT_EQ(500, decodedPdr.normal_max.value_s16); 4153 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16); 4154 EXPECT_EQ(1000, decodedPdr.warning_high.value_s16); 4155 EXPECT_EQ(-1000, decodedPdr.warning_low.value_s16); 4156 EXPECT_EQ(2000, decodedPdr.critical_high.value_s16); 4157 EXPECT_EQ(-2000, decodedPdr.critical_low.value_s16); 4158 EXPECT_EQ(3000, decodedPdr.fatal_high.value_s16); 4159 EXPECT_EQ(-3000, decodedPdr.fatal_low.value_s16); 4160 } 4161 4162 TEST(decodeNumericSensorPdrData, Uint32Test) 4163 { 4164 std::vector<uint8_t> pdr1{ 4165 0x1, 4166 0x0, 4167 0x0, 4168 0x0, // record handle 4169 0x1, // PDRHeaderVersion 4170 PLDM_NUMERIC_SENSOR_PDR, // PDRType 4171 0x0, 4172 0x0, // recordChangeNumber 4173 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 4174 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 + 4175 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 4176 0, // dataLength 4177 0, 4178 0, // PLDMTerminusHandle 4179 0x1, 4180 0x0, // sensorID=1 4181 PLDM_ENTITY_POWER_SUPPLY, 4182 0, // entityType=Power Supply(120) 4183 1, 4184 0, // entityInstanceNumber 4185 0x1, 4186 0x0, // containerID=1 4187 PLDM_NO_INIT, // sensorInit 4188 false, // sensorAuxiliaryNamesPDR 4189 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4190 0, // unitModifier 4191 0, // rateUnit 4192 0, // baseOEMUnitHandle 4193 0, // auxUnit 4194 0, // auxUnitModifier 4195 0, // auxRateUnit 4196 0, // rel 4197 0, // auxOEMUnitHandle 4198 true, // isLinear 4199 PLDM_SENSOR_DATA_SIZE_UINT32, // sensorDataSize 4200 0, 4201 0, 4202 0, 4203 0, // resolution 4204 0, 4205 0, 4206 0, 4207 0, // offset 4208 0, 4209 0, // accuracy 4210 0, // plusTolerance 4211 0, // minusTolerance 4212 3, 4213 0, 4214 0, 4215 0, // hysteresis 4216 0, // supportedThresholds 4217 0, // thresholdAndHysteresisVolatility 4218 0, 4219 0, 4220 0x80, 4221 0x3f, // stateTransistionInterval=1.0 4222 0, 4223 0, 4224 0x80, 4225 0x3f, // updateInverval=1.0 4226 0, 4227 0x10, 4228 0, 4229 0, // maxReadable = 4096 4230 0, 4231 0, 4232 0, 4233 0, // minReadable = 0 4234 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat 4235 0, // rangeFieldsupport 4236 0x40, 4237 0x4b, 4238 0x4c, 4239 0x00, // nominalValue = 5,000,000 4240 0x80, 4241 0x8d, 4242 0x5b, 4243 0x00, // normalMax = 6,000,000 4244 0x00, 4245 0x09, 4246 0x3d, 4247 0x00, // normalMin = 4,000,000 4248 0xc0, 4249 0xcf, 4250 0x6a, 4251 0x00, // warningHigh = 7,000,000 4252 0xc0, 4253 0xc6, 4254 0x2d, 4255 0x00, // warningLow = 3,000,000 4256 0x00, 4257 0x12, 4258 0x7a, 4259 0x00, // criticalHigh = 8,000,000 4260 0x80, 4261 0x84, 4262 0x1e, 4263 0x00, // criticalLow = 2,000,000 4264 0x40, 4265 0x54, 4266 0x89, 4267 0x00, // fatalHigh = 9,000,000 4268 0x40, 4269 0x42, 4270 0x0f, 4271 0x00 // fatalLow = 1,000,000 4272 }; 4273 4274 struct pldm_numeric_sensor_value_pdr decodedPdr; 4275 auto rc = 4276 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4277 EXPECT_EQ(PLDM_SUCCESS, rc); 4278 4279 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT32, decodedPdr.sensor_data_size); 4280 EXPECT_EQ(4096, decodedPdr.max_readable.value_u32); 4281 EXPECT_EQ(0, decodedPdr.min_readable.value_u32); 4282 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format); 4283 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32); 4284 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32); 4285 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32); 4286 EXPECT_EQ(7000000, decodedPdr.warning_high.value_u32); 4287 EXPECT_EQ(3000000, decodedPdr.warning_low.value_u32); 4288 EXPECT_EQ(8000000, decodedPdr.critical_high.value_u32); 4289 EXPECT_EQ(2000000, decodedPdr.critical_low.value_u32); 4290 EXPECT_EQ(9000000, decodedPdr.fatal_high.value_u32); 4291 EXPECT_EQ(1000000, decodedPdr.fatal_low.value_u32); 4292 } 4293 4294 TEST(decodeNumericSensorPdrData, Sint32Test) 4295 { 4296 std::vector<uint8_t> pdr1{ 4297 0x1, 4298 0x0, 4299 0x0, 4300 0x0, // record handle 4301 0x1, // PDRHeaderVersion 4302 PLDM_NUMERIC_SENSOR_PDR, // PDRType 4303 0x0, 4304 0x0, // recordChangeNumber 4305 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 4306 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 + 4307 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 4308 0, // dataLength 4309 0, 4310 0, // PLDMTerminusHandle 4311 0x1, 4312 0x0, // sensorID=1 4313 PLDM_ENTITY_POWER_SUPPLY, 4314 0, // entityType=Power Supply(120) 4315 1, 4316 0, // entityInstanceNumber 4317 0x1, 4318 0x0, // containerID=1 4319 PLDM_NO_INIT, // sensorInit 4320 false, // sensorAuxiliaryNamesPDR 4321 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4322 0, // unitModifier 4323 0, // rateUnit 4324 0, // baseOEMUnitHandle 4325 0, // auxUnit 4326 0, // auxUnitModifier 4327 0, // auxRateUnit 4328 0, // rel 4329 0, // auxOEMUnitHandle 4330 true, // isLinear 4331 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize 4332 0, 4333 0, 4334 0, 4335 0, // resolution 4336 0, 4337 0, 4338 0, 4339 0, // offset 4340 0, 4341 0, // accuracy 4342 0, // plusTolerance 4343 0, // minusTolerance 4344 3, 4345 0, 4346 0, 4347 0, // hysteresis 4348 0, // supportedThresholds 4349 0, // thresholdAndHysteresisVolatility 4350 0, 4351 0, 4352 0x80, 4353 0x3f, // stateTransistionInterval=1.0 4354 0, 4355 0, 4356 0x80, 4357 0x3f, // updateInverval=1.0 4358 0xa0, 4359 0x86, 4360 0x01, 4361 0x00, // maxReadable = 100000 4362 0x60, 4363 0x79, 4364 0xfe, 4365 0xff, // minReadable = -10000 4366 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat 4367 0, // rangeFieldsupport 4368 0, 4369 0, 4370 0, 4371 0, // nominalValue = 0 4372 0x20, 4373 0xa1, 4374 0x07, 4375 0x00, // normalMax = 500,000 4376 0xe0, 4377 0x5e, 4378 0xf8, 4379 0xff, // normalMin = -500,000 4380 0x40, 4381 0x42, 4382 0x0f, 4383 0x00, // warningHigh = 1,000,000 4384 0xc0, 4385 0xbd, 4386 0xf0, 4387 0xff, // warningLow = -1,000,000 4388 0x80, 4389 0x84, 4390 0x1e, 4391 0x00, // criticalHigh = 2,000,000 4392 0x80, 4393 0x7b, 4394 0xe1, 4395 0xff, // criticalLow = -2,000,000 4396 0xc0, 4397 0xc6, 4398 0x2d, 4399 0x00, // fatalHigh = 3,000,000 4400 0x40, 4401 0x39, 4402 0xd2, 4403 0xff // fatalLow = -3,000,000 4404 }; 4405 4406 struct pldm_numeric_sensor_value_pdr decodedPdr; 4407 auto rc = 4408 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4409 EXPECT_EQ(PLDM_SUCCESS, rc); 4410 4411 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size); 4412 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32); 4413 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32); 4414 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format); 4415 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32); 4416 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32); 4417 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32); 4418 EXPECT_EQ(1000000, decodedPdr.warning_high.value_s32); 4419 EXPECT_EQ(-1000000, decodedPdr.warning_low.value_s32); 4420 EXPECT_EQ(2000000, decodedPdr.critical_high.value_s32); 4421 EXPECT_EQ(-2000000, decodedPdr.critical_low.value_s32); 4422 EXPECT_EQ(3000000, decodedPdr.fatal_high.value_s32); 4423 EXPECT_EQ(-3000000, decodedPdr.fatal_low.value_s32); 4424 } 4425 4426 TEST(decodeNumericSensorPdrData, Real32Test) 4427 { 4428 std::vector<uint8_t> pdr1{ 4429 0x1, 4430 0x0, 4431 0x0, 4432 0x0, // record handle 4433 0x1, // PDRHeaderVersion 4434 PLDM_NUMERIC_SENSOR_PDR, // PDRType 4435 0x0, 4436 0x0, // recordChangeNumber 4437 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH + 4438 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 + 4439 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 4440 0, // dataLength 4441 0, 4442 0, // PLDMTerminusHandle 4443 0x1, 4444 0x0, // sensorID=1 4445 PLDM_ENTITY_POWER_SUPPLY, 4446 0, // entityType=Power Supply(120) 4447 1, 4448 0, // entityInstanceNumber 4449 0x1, 4450 0x0, // containerID=1 4451 PLDM_NO_INIT, // sensorInit 4452 false, // sensorAuxiliaryNamesPDR 4453 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4454 0, // unitModifier 4455 0, // rateUnit 4456 0, // baseOEMUnitHandle 4457 0, // auxUnit 4458 0, // auxUnitModifier 4459 0, // auxRateUnit 4460 0, // rel 4461 0, // auxOEMUnitHandle 4462 true, // isLinear 4463 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize 4464 0, 4465 0, 4466 0, 4467 0, // resolution 4468 0, 4469 0, 4470 0, 4471 0, // offset 4472 0, 4473 0, // accuracy 4474 0, // plusTolerance 4475 0, // minusTolerance 4476 3, 4477 0, 4478 0, 4479 0, // hysteresis 4480 0, // supportedThresholds 4481 0, // thresholdAndHysteresisVolatility 4482 0, 4483 0, 4484 0x80, 4485 0x3f, // stateTransistionInterval=1.0 4486 0, 4487 0, 4488 0x80, 4489 0x3f, // updateInverval=1.0 4490 0xa0, 4491 0x86, 4492 0x01, 4493 0x00, // maxReadable = 100000 4494 0x60, 4495 0x79, 4496 0xfe, 4497 0xff, // minReadable = -10000 4498 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat 4499 0, // rangeFieldsupport 4500 0, 4501 0, 4502 0, 4503 0, // nominalValue = 0.0 4504 0x33, 4505 0x33, 4506 0x48, 4507 0x42, // normalMax = 50.05 4508 0x33, 4509 0x33, 4510 0x48, 4511 0xc2, // normalMin = -50.05 4512 0x83, 4513 0x00, 4514 0xc8, 4515 0x42, // warningHigh = 100.001 4516 0x83, 4517 0x00, 4518 0xc8, 4519 0xc2, // warningLow = -100.001 4520 0x83, 4521 0x00, 4522 0x48, 4523 0x43, // criticalHigh = 200.002 4524 0x83, 4525 0x00, 4526 0x48, 4527 0xc3, // criticalLow = -200.002 4528 0x62, 4529 0x00, 4530 0x96, 4531 0x43, // fatalHigh = 300.003 4532 0x62, 4533 0x00, 4534 0x96, 4535 0xc3 // fatalLow = -300.003 4536 }; 4537 4538 struct pldm_numeric_sensor_value_pdr decodedPdr; 4539 auto rc = 4540 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4541 EXPECT_EQ(PLDM_SUCCESS, rc); 4542 4543 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size); 4544 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32); 4545 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32); 4546 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format); 4547 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32); 4548 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32); 4549 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32); 4550 EXPECT_FLOAT_EQ(100.001f, decodedPdr.warning_high.value_f32); 4551 EXPECT_FLOAT_EQ(-100.001f, decodedPdr.warning_low.value_f32); 4552 EXPECT_FLOAT_EQ(200.002f, decodedPdr.critical_high.value_f32); 4553 EXPECT_FLOAT_EQ(-200.002f, decodedPdr.critical_low.value_f32); 4554 EXPECT_FLOAT_EQ(300.003f, decodedPdr.fatal_high.value_f32); 4555 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.fatal_low.value_f32); 4556 } 4557 4558 TEST(decodeNumericSensorPdrDataDeathTest, InvalidSizeTest) 4559 { 4560 // A corrupted PDR. The data after plusTolerance missed. 4561 std::vector<uint8_t> pdr1{ 4562 0x1, 4563 0x0, 4564 0x0, 4565 0x0, // record handle 4566 0x1, // PDRHeaderVersion 4567 PLDM_NUMERIC_SENSOR_PDR, // PDRType 4568 0x0, 4569 0x0, // recordChangeNumber 4570 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH, 4571 0, // dataLength 4572 0, 4573 0, // PLDMTerminusHandle 4574 0x1, 4575 0x0, // sensorID=1 4576 PLDM_ENTITY_POWER_SUPPLY, 4577 0, // entityType=Power Supply(120) 4578 1, 4579 0, // entityInstanceNumber 4580 0x1, 4581 0x0, // containerID=1 4582 PLDM_NO_INIT, // sensorInit 4583 false, // sensorAuxiliaryNamesPDR 4584 2, // baseUint(2)=degrees C 4585 0, // unitModifier 4586 0, // rateUnit 4587 0, // baseOEMUnitHandle 4588 0, // auxUnit 4589 0, // auxUnitModifier 4590 0, // auxRateUnit 4591 0, // rel 4592 0, // auxOEMUnitHandle 4593 true, // isLinear 4594 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize 4595 0, 4596 0, 4597 0, 4598 0, // resolution 4599 0, 4600 0, 4601 0, 4602 0, // offset 4603 0, 4604 0, // accuracy 4605 0 // plusTolerance 4606 }; 4607 4608 struct pldm_numeric_sensor_value_pdr decodedPdr; 4609 int rc = 4610 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4611 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); 4612 } 4613 4614 #ifdef LIBPLDM_API_TESTING 4615 TEST(decodeNumericEffecterPdrData, Uint8Test) 4616 { 4617 std::vector<uint8_t> pdr1{ 4618 0x1, 4619 0x0, 4620 0x0, 4621 0x0, // record handle 4622 0x1, // PDRHeaderVersion 4623 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 4624 0x0, 4625 0x0, // recordChangeNumber 4626 PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH, 4627 0, // dataLength 4628 0, 4629 0, // PLDMTerminusHandle 4630 0x1, 4631 0x0, // effecterID=1 4632 PLDM_ENTITY_POWER_SUPPLY, 4633 0, // entityType=Power Supply(120) 4634 1, 4635 0, // entityInstanceNumber 4636 1, 4637 0, // containerID=1 4638 0x2, 4639 0x0, // effecter_semantic_id=2 4640 PLDM_NO_INIT, // effecterInit 4641 false, // effecterAuxiliaryNames 4642 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4643 0, // unitModifier 4644 0, // rateUnit 4645 0, // baseOEMUnitHandle 4646 0, // auxUnit 4647 0, // auxUnitModifier 4648 4, // auxRateUnit 4649 0, // auxOEMUnitHandle 4650 true, // isLinear 4651 PLDM_EFFECTER_DATA_SIZE_UINT8, // effecterDataSize 4652 0, 4653 0, 4654 0xc0, 4655 0x3f, // resolution=1.5 4656 0, 4657 0, 4658 0x80, 4659 0x3f, // offset=1.0 4660 0, 4661 0, // accuracy 4662 0, // plusTolerance 4663 0, // minusTolerance 4664 0, 4665 0, 4666 0x80, 4667 0x3f, // stateTransistionInterval=1.0 4668 0, 4669 0, 4670 0x80, 4671 0x3f, // transition_interval=1.0 4672 255, // maxSetdable 4673 0, // minSetable 4674 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat 4675 0x1f, // rangeFieldsupport 4676 50, // nominalValue = 50 4677 60, // normalMax = 60 4678 40, // normalMin = 40 4679 90, // rated_max = 90 4680 10 // rated_min = 10 4681 }; 4682 4683 struct pldm_numeric_effecter_value_pdr decodedPdr; 4684 auto rc = 4685 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4686 EXPECT_EQ(PLDM_SUCCESS, rc); 4687 EXPECT_EQ(1, decodedPdr.hdr.record_handle); 4688 EXPECT_EQ(1, decodedPdr.hdr.version); 4689 EXPECT_EQ(PLDM_NUMERIC_EFFECTER_PDR, decodedPdr.hdr.type); 4690 EXPECT_EQ(0, decodedPdr.hdr.record_change_num); 4691 EXPECT_EQ(PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH, decodedPdr.hdr.length); 4692 EXPECT_EQ(1, decodedPdr.effecter_id); 4693 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type); 4694 EXPECT_EQ(1, decodedPdr.entity_instance); 4695 EXPECT_EQ(1, decodedPdr.container_id); 4696 EXPECT_EQ(2, decodedPdr.effecter_semantic_id); 4697 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.effecter_init); 4698 EXPECT_EQ(false, decodedPdr.effecter_auxiliary_names); 4699 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit); 4700 EXPECT_EQ(0, decodedPdr.unit_modifier); 4701 EXPECT_EQ(0, decodedPdr.rate_unit); 4702 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle); 4703 EXPECT_EQ(0, decodedPdr.aux_unit); 4704 EXPECT_EQ(0, decodedPdr.aux_unit_modifier); 4705 EXPECT_EQ(4, decodedPdr.aux_rate_unit); 4706 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle); 4707 EXPECT_EQ(true, decodedPdr.is_linear); 4708 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT8, decodedPdr.effecter_data_size); 4709 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution); 4710 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset); 4711 EXPECT_EQ(0, decodedPdr.accuracy); 4712 EXPECT_EQ(0, decodedPdr.plus_tolerance); 4713 EXPECT_EQ(0, decodedPdr.minus_tolerance); 4714 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval); 4715 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval); 4716 EXPECT_EQ(255, decodedPdr.max_settable.value_u8); 4717 EXPECT_EQ(0, decodedPdr.min_settable.value_u8); 4718 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format); 4719 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 4720 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8); 4721 EXPECT_EQ(60, decodedPdr.normal_max.value_u8); 4722 EXPECT_EQ(40, decodedPdr.normal_min.value_u8); 4723 EXPECT_EQ(90, decodedPdr.rated_max.value_u8); 4724 EXPECT_EQ(10, decodedPdr.rated_min.value_u8); 4725 } 4726 #endif 4727 4728 #ifdef LIBPLDM_API_TESTING 4729 TEST(decodeNumericEffecterPdrData, Sint8Test) 4730 { 4731 std::vector<uint8_t> pdr1{ 4732 0x1, 4733 0x0, 4734 0x0, 4735 0x0, // record handle 4736 0x1, // PDRHeaderVersion 4737 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 4738 0x0, 4739 0x0, // recordChangeNumber 4740 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 4741 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH + 4742 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH, 4743 0, // dataLength 4744 0, 4745 0, // PLDMTerminusHandle 4746 0x1, 4747 0x0, // effecterID=1 4748 PLDM_ENTITY_POWER_SUPPLY, 4749 0, // entityType=Power Supply(120) 4750 1, 4751 0, // entityInstanceNumber 4752 0x1, 4753 0x0, // containerID=1 4754 0x2, 4755 0x0, // effecter_semantic_id=2 4756 PLDM_NO_INIT, // effecterInit 4757 false, // effecterAuxiliaryNames 4758 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4759 0, // unitModifier 4760 0, // rateUnit 4761 0, // baseOEMUnitHandle 4762 0, // auxUnit 4763 0, // auxUnitModifier 4764 0, // auxRateUnit 4765 0, // auxOEMUnitHandle 4766 true, // isLinear 4767 PLDM_RANGE_FIELD_FORMAT_SINT8, // effecterDataSize 4768 0, 4769 0, 4770 0, 4771 0, // resolution 4772 0, 4773 0, 4774 0, 4775 0, // offset 4776 0, 4777 0, // accuracy 4778 0, // plusTolerance 4779 0, // minusTolerance 4780 0, 4781 0, 4782 0x80, 4783 0x3f, // stateTransistionInterval=1.0 4784 0, 4785 0, 4786 0x80, 4787 0x3f, // transition_interval=1.0 4788 0x64, // maxSetdable = 100 4789 0x9c, // minSetable = -100 4790 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat 4791 0x1f, // rangeFieldsupport 4792 0, // nominalValue = 0 4793 5, // normalMax = 5 4794 0xfb, // normalMin = -5 4795 30, // rated_max = 30 4796 0xe2 // rated_min = -30 4797 }; 4798 4799 struct pldm_numeric_effecter_value_pdr decodedPdr; 4800 auto rc = 4801 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4802 EXPECT_EQ(PLDM_SUCCESS, rc); 4803 4804 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT8, decodedPdr.effecter_data_size); 4805 EXPECT_EQ(100, decodedPdr.max_settable.value_s8); 4806 EXPECT_EQ(-100, decodedPdr.min_settable.value_s8); 4807 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format); 4808 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 4809 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8); 4810 EXPECT_EQ(5, decodedPdr.normal_max.value_s8); 4811 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8); 4812 EXPECT_EQ(30, decodedPdr.rated_max.value_s8); 4813 EXPECT_EQ(-30, decodedPdr.rated_min.value_s8); 4814 } 4815 #endif 4816 4817 #ifdef LIBPLDM_API_TESTING 4818 TEST(decodeNumericEffecterPdrData, Uint16Test) 4819 { 4820 std::vector<uint8_t> pdr1{ 4821 0x1, 4822 0x0, 4823 0x0, 4824 0x0, // record handle 4825 0x1, // PDRHeaderVersion 4826 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 4827 0x0, 4828 0x0, // recordChangeNumber 4829 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 4830 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH * 4831 2 + 4832 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2, 4833 0, // dataLength 4834 0, 4835 0, // PLDMTerminusHandle 4836 0x1, 4837 0x0, // effecterID=1 4838 PLDM_ENTITY_POWER_SUPPLY, 4839 0, // entityType=Power Supply(120) 4840 1, 4841 0, // entityInstanceNumber 4842 0x1, 4843 0x0, // containerID=1 4844 0x2, 4845 0x0, // effecter_semantic_id=2 4846 PLDM_NO_INIT, // effecterInit 4847 false, // effecterAuxiliaryNames 4848 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4849 0, // unitModifier 4850 0, // rateUnit 4851 0, // baseOEMUnitHandle 4852 0, // auxUnit 4853 0, // auxUnitModifier 4854 0, // auxRateUnit 4855 0, // auxOEMUnitHandle 4856 true, // isLinear 4857 PLDM_EFFECTER_DATA_SIZE_UINT16, // effecterDataSize 4858 0, 4859 0, 4860 0, 4861 0, // resolution 4862 0, 4863 0, 4864 0, 4865 0, // offset 4866 0, 4867 0, // accuracy 4868 0, // plusTolerance 4869 0, // minusTolerance 4870 0, 4871 0, 4872 0x80, 4873 0x3f, // stateTransistionInterval=1.0 4874 0, 4875 0, 4876 0x80, 4877 0x3f, // transition_interval=1.0 4878 0, 4879 0x10, // maxSetdable = 4096 4880 0, 4881 0, // minSetable = 0 4882 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat 4883 0x1f, // rangeFieldsupport 4884 0x88, 4885 0x13, // nominalValue = 5,000 4886 0x70, 4887 0x17, // normalMax = 6,000 4888 0xa0, 4889 0x0f, // normalMin = 4,000 4890 0x28, 4891 0x23, // rated_max = 9,000 4892 0xe8, 4893 0x03 // rated_min = 1,000 4894 }; 4895 4896 struct pldm_numeric_effecter_value_pdr decodedPdr; 4897 auto rc = 4898 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4899 EXPECT_EQ(PLDM_SUCCESS, rc); 4900 4901 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT16, decodedPdr.effecter_data_size); 4902 EXPECT_EQ(4096, decodedPdr.max_settable.value_u16); 4903 EXPECT_EQ(0, decodedPdr.min_settable.value_u16); 4904 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format); 4905 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 4906 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16); 4907 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16); 4908 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16); 4909 EXPECT_EQ(9000, decodedPdr.rated_max.value_u16); 4910 EXPECT_EQ(1000, decodedPdr.rated_min.value_u16); 4911 } 4912 #endif 4913 4914 #ifdef LIBPLDM_API_TESTING 4915 TEST(decodeNumericEffecterPdrData, Sint16Test) 4916 { 4917 std::vector<uint8_t> pdr1{ 4918 0x1, 4919 0x0, 4920 0x0, 4921 0x0, // record handle 4922 0x1, // PDRHeaderVersion 4923 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 4924 0x0, 4925 0x0, // recordChangeNumber 4926 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 4927 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH * 4928 2 + 4929 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2, 4930 0, // dataLength 4931 0, 4932 0, // PLDMTerminusHandle 4933 0x1, 4934 0x0, // effecterID=1 4935 PLDM_ENTITY_POWER_SUPPLY, 4936 0, // entityType=Power Supply(120) 4937 1, 4938 0, // entityInstanceNumber 4939 0x1, 4940 0x0, // containerID=1 4941 0x2, 4942 0x0, // effecter_semantic_id=2 4943 PLDM_NO_INIT, // effecterInit 4944 false, // effecterAuxiliaryNames 4945 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 4946 0, // unitModifier 4947 0, // rateUnit 4948 0, // baseOEMUnitHandle 4949 0, // auxUnit 4950 0, // auxUnitModifier 4951 0, // auxRateUnit 4952 0, // auxOEMUnitHandle 4953 true, // isLinear 4954 PLDM_EFFECTER_DATA_SIZE_SINT16, // effecterDataSize 4955 0, 4956 0, 4957 0, 4958 0, // resolution 4959 0, 4960 0, 4961 0, 4962 0, // offset 4963 0, 4964 0, // accuracy 4965 0, // plusTolerance 4966 0, // minusTolerance 4967 0, 4968 0, 4969 0x80, 4970 0x3f, // stateTransistionInterval=1.0 4971 0, 4972 0, 4973 0x80, 4974 0x3f, // transition_interval=1.0 4975 0xe8, 4976 0x03, // maxSetdable = 1000 4977 0x18, 4978 0xfc, // minSetable = -1000 4979 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat 4980 0x1f, // rangeFieldsupport 4981 0, 4982 0, // nominalValue = 0 4983 0xf4, 4984 0x01, // normalMax = 500 4985 0x0c, 4986 0xfe, // normalMin = -500 4987 0xb8, 4988 0x0b, // rated_max = 3,000 4989 0x48, 4990 0xf4 // rated_min = -3,000 4991 }; 4992 4993 struct pldm_numeric_effecter_value_pdr decodedPdr; 4994 auto rc = 4995 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 4996 EXPECT_EQ(PLDM_SUCCESS, rc); 4997 4998 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT16, decodedPdr.effecter_data_size); 4999 EXPECT_EQ(1000, decodedPdr.max_settable.value_s16); 5000 EXPECT_EQ(-1000, decodedPdr.min_settable.value_s16); 5001 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format); 5002 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 5003 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16); 5004 EXPECT_EQ(500, decodedPdr.normal_max.value_s16); 5005 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16); 5006 EXPECT_EQ(3000, decodedPdr.rated_max.value_s16); 5007 EXPECT_EQ(-3000, decodedPdr.rated_min.value_s16); 5008 } 5009 #endif 5010 5011 #ifdef LIBPLDM_API_TESTING 5012 TEST(decodeNumericEffecterPdrData, Uint32Test) 5013 { 5014 std::vector<uint8_t> pdr1{ 5015 0x1, 5016 0x0, 5017 0x0, 5018 0x0, // record handle 5019 0x1, // PDRHeaderVersion 5020 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 5021 0x0, 5022 0x0, // recordChangeNumber 5023 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 5024 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH * 5025 4 + 5026 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 5027 0, // dataLength 5028 0, 5029 0, // PLDMTerminusHandle 5030 0x1, 5031 0x0, // effecterID=1 5032 PLDM_ENTITY_POWER_SUPPLY, 5033 0, // entityType=Power Supply(120) 5034 1, 5035 0, // entityInstanceNumber 5036 0x1, 5037 0x0, // containerID=1 5038 0x2, 5039 0x0, // effecter_semantic_id=2 5040 PLDM_NO_INIT, // effecterInit 5041 false, // effecterAuxiliaryNames 5042 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 5043 0, // unitModifier 5044 0, // rateUnit 5045 0, // baseOEMUnitHandle 5046 0, // auxUnit 5047 0, // auxUnitModifier 5048 0, // auxRateUnit 5049 0, // auxOEMUnitHandle 5050 true, // isLinear 5051 PLDM_EFFECTER_DATA_SIZE_UINT32, // effecterDataSize 5052 0, 5053 0, 5054 0, 5055 0, // resolution 5056 0, 5057 0, 5058 0, 5059 0, // offset 5060 0, 5061 0, // accuracy 5062 0, // plusTolerance 5063 0, // minusTolerance 5064 0, 5065 0, 5066 0x80, 5067 0x3f, // stateTransistionInterval=1.0 5068 0, 5069 0, 5070 0x80, 5071 0x3f, // transition_interval=1.0 5072 0, 5073 0x10, 5074 0, 5075 0, // maxSetdable = 4096 5076 0, 5077 0, 5078 0, 5079 0, // minSetable = 0 5080 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat 5081 0x1f, // rangeFieldsupport 5082 0x40, 5083 0x4b, 5084 0x4c, 5085 0x00, // nominalValue = 5,000,000 5086 0x80, 5087 0x8d, 5088 0x5b, 5089 0x00, // normalMax = 6,000,000 5090 0x00, 5091 0x09, 5092 0x3d, 5093 0x00, // normalMin = 4,000,000 5094 0x40, 5095 0x54, 5096 0x89, 5097 0x00, // rated_max = 9,000,000 5098 0x40, 5099 0x42, 5100 0x0f, 5101 0x00 // rated_min = 1,000,000 5102 }; 5103 5104 struct pldm_numeric_effecter_value_pdr decodedPdr; 5105 auto rc = 5106 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 5107 EXPECT_EQ(PLDM_SUCCESS, rc); 5108 5109 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT32, decodedPdr.effecter_data_size); 5110 EXPECT_EQ(4096, decodedPdr.max_settable.value_u32); 5111 EXPECT_EQ(0, decodedPdr.min_settable.value_u32); 5112 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format); 5113 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 5114 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32); 5115 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32); 5116 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32); 5117 EXPECT_EQ(9000000, decodedPdr.rated_max.value_u32); 5118 EXPECT_EQ(1000000, decodedPdr.rated_min.value_u32); 5119 } 5120 #endif 5121 5122 #ifdef LIBPLDM_API_TESTING 5123 TEST(decodeNumericEffecterPdrData, Sint32Test) 5124 { 5125 std::vector<uint8_t> pdr1{ 5126 0x1, 5127 0x0, 5128 0x0, 5129 0x0, // record handle 5130 0x1, // PDRHeaderVersion 5131 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 5132 0x0, 5133 0x0, // recordChangeNumber 5134 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 5135 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH * 5136 4 + 5137 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 5138 0, // dataLength 5139 0, 5140 0, // PLDMTerminusHandle 5141 0x1, 5142 0x0, // effecterID=1 5143 PLDM_ENTITY_POWER_SUPPLY, 5144 0, // entityType=Power Supply(120) 5145 1, 5146 0, // entityInstanceNumber 5147 0x1, 5148 0x0, // containerID=1 5149 0x2, 5150 0x0, // effecter_semantic_id=2 5151 PLDM_NO_INIT, // effecterInit 5152 false, // effecterAuxiliaryNames 5153 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 5154 0, // unitModifier 5155 0, // rateUnit 5156 0, // baseOEMUnitHandle 5157 0, // auxUnit 5158 0, // auxUnitModifier 5159 0, // auxRateUnit 5160 0, // auxOEMUnitHandle 5161 true, // isLinear 5162 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize 5163 0, 5164 0, 5165 0, 5166 0, // resolution 5167 0, 5168 0, 5169 0, 5170 0, // offset 5171 0, 5172 0, // accuracy 5173 0, // plusTolerance 5174 0, // minusTolerance 5175 0, 5176 0, 5177 0x80, 5178 0x3f, // stateTransistionInterval=1.0 5179 0, 5180 0, 5181 0x80, 5182 0x3f, // transition_interval=1.0 5183 0xa0, 5184 0x86, 5185 0x01, 5186 0x00, // maxSetdable = 100000 5187 0x60, 5188 0x79, 5189 0xfe, 5190 0xff, // minSetable = -10000 5191 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat 5192 0x1f, // rangeFieldsupport 5193 0, 5194 0, 5195 0, 5196 0, // nominalValue = 0 5197 0x20, 5198 0xa1, 5199 0x07, 5200 0x00, // normalMax = 500,000 5201 0xe0, 5202 0x5e, 5203 0xf8, 5204 0xff, // normalMin = -500,000 5205 0xc0, 5206 0xc6, 5207 0x2d, 5208 0x00, // rated_max = 3,000,000 5209 0x40, 5210 0x39, 5211 0xd2, 5212 0xff // rated_min = -3,000,000 5213 }; 5214 5215 struct pldm_numeric_effecter_value_pdr decodedPdr; 5216 auto rc = 5217 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 5218 EXPECT_EQ(PLDM_SUCCESS, rc); 5219 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size); 5220 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32); 5221 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32); 5222 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format); 5223 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 5224 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32); 5225 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32); 5226 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32); 5227 EXPECT_EQ(3000000, decodedPdr.rated_max.value_s32); 5228 EXPECT_EQ(-3000000, decodedPdr.rated_min.value_s32); 5229 } 5230 #endif 5231 5232 #ifdef LIBPLDM_API_TESTING 5233 TEST(decodeNumericEffecterPdrData, Real32Test) 5234 { 5235 std::vector<uint8_t> pdr1{ 5236 0x1, 5237 0x0, 5238 0x0, 5239 0x0, // record handle 5240 0x1, // PDRHeaderVersion 5241 PLDM_NUMERIC_EFFECTER_PDR, // PDRType 5242 0x0, 5243 0x0, // recordChangeNumber 5244 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH + 5245 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH * 5246 4 + 5247 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4, 5248 0, // dataLength 5249 0, 5250 5251 0, // PLDMTerminusHandle 5252 0x1, 5253 0x0, // effecterID=1 5254 PLDM_ENTITY_POWER_SUPPLY, 5255 0, // entityType=Power Supply(120) 5256 1, 5257 0, // entityInstanceNumber 5258 0x1, 5259 0x0, // containerID=1 5260 0x2, 5261 0x0, // effecter_semantic_id=2 5262 PLDM_NO_INIT, // effecterInit 5263 false, // effecterAuxiliaryNames 5264 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C 5265 0, // unitModifier 5266 0, // rateUnit 5267 0, // baseOEMUnitHandle 5268 0, // auxUnit 5269 0, // auxUnitModifier 5270 0, // auxRateUnit 5271 0, // auxOEMUnitHandle 5272 true, // isLinear 5273 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize 5274 0, 5275 0, 5276 0, 5277 0, // resolution 5278 0, 5279 0, 5280 0, 5281 0, // offset 5282 0, 5283 0, // accuracy 5284 0, // plusTolerance 5285 0, // minusTolerance 5286 0, 5287 0, 5288 0x80, 5289 0x3f, // stateTransistionInterval=1.0 5290 0, 5291 0, 5292 0x80, 5293 0x3f, // transition_interval=1.0 5294 0xa0, 5295 0x86, 5296 0x01, 5297 0x00, // maxSetdable = 100000 5298 0x60, 5299 0x79, 5300 0xfe, 5301 0xff, // minSetable = -10000 5302 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat 5303 0x1f, // rangeFieldsupport 5304 0, 5305 0, 5306 0, 5307 0, // nominalValue = 0.0 5308 0x33, 5309 0x33, 5310 0x48, 5311 0x42, // normalMax = 50.05 5312 0x33, 5313 0x33, 5314 0x48, 5315 0xc2, // normalMin = -50.05 5316 0x62, 5317 0x00, 5318 0x96, 5319 0x43, // rated_max = 300.003 5320 0x62, 5321 0x00, 5322 0x96, 5323 0xc3 // rated_min = -300.003 5324 }; 5325 5326 struct pldm_numeric_effecter_value_pdr decodedPdr; 5327 auto rc = 5328 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr); 5329 EXPECT_EQ(PLDM_SUCCESS, rc); 5330 5331 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size); 5332 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval); 5333 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval); 5334 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32); 5335 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32); 5336 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format); 5337 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte); 5338 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32); 5339 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32); 5340 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32); 5341 EXPECT_FLOAT_EQ(300.003f, decodedPdr.rated_max.value_f32); 5342 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.rated_min.value_f32); 5343 } 5344 #endif 5345 5346 TEST(GetStateEffecterStates, testEncodeAndDecodeRequest) 5347 { 5348 std::array<uint8_t, hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES> 5349 requestMsg{}; 5350 5351 constexpr std::array<uint8_t, 5352 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES> 5353 expectedRequestMsg{ 5354 {0x80, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES, 1, 0xab}}; 5355 5356 constexpr uint16_t effecter_id = 0xab01; 5357 5358 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5359 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); 5360 5361 auto rc = encode_get_state_effecter_states_req( 5362 0, effecter_id, request, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES); 5363 5364 EXPECT_EQ(rc, PLDM_SUCCESS); 5365 EXPECT_EQ(requestMsg, expectedRequestMsg); 5366 5367 uint16_t ret_effecter_id; 5368 5369 rc = decode_get_state_effecter_states_req( 5370 request, requestMsg.size() - hdrSize, &ret_effecter_id); 5371 5372 EXPECT_EQ(rc, PLDM_SUCCESS); 5373 EXPECT_EQ(effecter_id, ret_effecter_id); 5374 5375 // Test invalid length decode request 5376 5377 rc = decode_get_state_effecter_states_req( 5378 request, requestMsg.size() - hdrSize - 1, &ret_effecter_id); 5379 5380 EXPECT_EQ(rc, -EOVERFLOW); 5381 } 5382 5383 TEST(GetStateEffecterStates, testBadEncodeRequest) 5384 { 5385 std::vector<uint8_t> requestMsg(hdrSize + 5386 PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES); 5387 5388 auto rc = encode_get_state_effecter_states_req( 5389 0, 0, nullptr, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES); 5390 EXPECT_EQ(rc, -EINVAL); 5391 } 5392 5393 TEST(GetStateEffecterStates, testBadDecodeRequest) 5394 { 5395 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES> 5396 requestMsg{}; 5397 5398 auto rc = decode_get_state_effecter_states_req( 5399 nullptr, requestMsg.size() - hdrSize, nullptr); 5400 5401 EXPECT_EQ(rc, -EINVAL); 5402 } 5403 5404 TEST(GetStateEffecterStates, testEncodeAndDecodeResponse) 5405 { 5406 constexpr uint8_t comp_effecterCnt = 0x2; 5407 constexpr uint8_t completionCode = 0; 5408 std::array<uint8_t, 5409 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES + 5410 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * comp_effecterCnt> 5411 expectedResponseMsg{{0, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES, 5412 completionCode, comp_effecterCnt, 5413 EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2, 5414 EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}}; 5415 5416 decltype(expectedResponseMsg) responseMsg{}; 5417 5418 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5419 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 5420 5421 std::array<get_effecter_state_field, comp_effecterCnt> stateField{ 5422 {{EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2}, 5423 {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}}}; 5424 5425 struct pldm_get_state_effecter_states_resp resp_fields 5426 { 5427 PLDM_SUCCESS, comp_effecterCnt, 5428 { 5429 stateField[0], stateField[1] 5430 } 5431 }; 5432 5433 auto rc = encode_get_state_effecter_states_resp( 5434 0, &resp_fields, response, responseMsg.size() - hdrSize); 5435 5436 EXPECT_EQ(rc, PLDM_SUCCESS); 5437 EXPECT_EQ(expectedResponseMsg, responseMsg); 5438 5439 struct pldm_get_state_effecter_states_resp ret_resp_fields; 5440 5441 rc = decode_get_state_effecter_states_resp( 5442 response, responseMsg.size() - hdrSize, &ret_resp_fields); 5443 5444 EXPECT_EQ(rc, PLDM_SUCCESS); 5445 EXPECT_EQ(completionCode, ret_resp_fields.completion_code); 5446 EXPECT_EQ(comp_effecterCnt, ret_resp_fields.comp_effecter_count); 5447 EXPECT_EQ(stateField[0].effecter_op_state, 5448 ret_resp_fields.field[0].effecter_op_state); 5449 EXPECT_EQ(stateField[0].pending_state, 5450 ret_resp_fields.field[0].pending_state); 5451 EXPECT_EQ(stateField[0].present_state, 5452 ret_resp_fields.field[0].present_state); 5453 EXPECT_EQ(stateField[1].effecter_op_state, 5454 ret_resp_fields.field[1].effecter_op_state); 5455 EXPECT_EQ(stateField[1].pending_state, 5456 ret_resp_fields.field[1].pending_state); 5457 EXPECT_EQ(stateField[1].present_state, 5458 ret_resp_fields.field[1].present_state); 5459 5460 // Test invalid length decode 5461 5462 rc = decode_get_state_effecter_states_resp( 5463 response, 5464 responseMsg.size() - hdrSize + PLDM_GET_EFFECTER_STATE_FIELD_SIZE, 5465 &ret_resp_fields); 5466 5467 EXPECT_EQ(rc, -EBADMSG); 5468 } 5469 5470 TEST(GetStateEffecterStates, testBadEncodeResponse) 5471 { 5472 struct pldm_get_state_effecter_states_resp resp 5473 { 5474 PLDM_SUCCESS, 0, 5475 { 5476 } 5477 }; 5478 auto rc = decode_get_state_effecter_states_resp(nullptr, 0, &resp); 5479 5480 EXPECT_EQ(rc, -EINVAL); 5481 } 5482 5483 TEST(GetStateEffecterStates, testBadDecodeResponse) 5484 { 5485 std::array<uint8_t, hdrSize + 5486 PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES + 5487 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * 2> 5488 responseMsg{}; 5489 5490 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5491 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); 5492 5493 auto rc = decode_get_state_effecter_states_resp( 5494 response, responseMsg.size() - hdrSize, nullptr); 5495 5496 EXPECT_EQ(rc, -EINVAL); 5497 } 5498 5499 [[maybe_unused]] static size_t str16len(char16_t* startptr) 5500 { 5501 char16_t* endptr = startptr; 5502 while (*endptr) 5503 { 5504 endptr++; 5505 } 5506 return endptr - startptr; 5507 } 5508 5509 TEST(decodeEntityAuxNamePdrData, GoodTest) 5510 { 5511 std::vector<uint8_t> pdr1{ 5512 // Common PDR Header 5513 0x1, 0x0, 0x0, 0x0, // record handle 5514 0x1, // PDRHeaderVersion 5515 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType 5516 0x1, 5517 0x0, // recordChangeNumber 5518 0x27, 5519 0, // dataLength 5520 /* Entity Auxiliary Names PDR Data*/ 5521 3, 5522 0, // entityType system software 5523 0x1, 5524 0x0, // Entity instance number =1 5525 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID, 5526 0, // Overal system 5527 0, // shared Name Count one name only 5528 03, // nameStringCount 5529 0x65, 0x6e, 0x00, // Language Tag "en" 5530 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S" 5531 0x66, 0x6e, 0x00, // Language Tag "en" 5532 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1" 5533 0x67, 0x6e, 0x00, // Language Tag "en" 5534 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3" 5535 }; 5536 5537 const char expectTag0[] = {0x65, 0x6e, 0x00}; 5538 const char expectTag1[] = {0x66, 0x6e, 0x00}; 5539 const char expectTag2[] = {0x67, 0x6e, 0x00}; 5540 const char expectName0[] = {0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00}; 5541 const char expectName1[] = {0x00, 0x53, 0x00, 0x31, 0x00, 0x00}; 5542 const char expectName2[] = {0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00}; 5543 auto names_offset = sizeof(struct pldm_pdr_hdr) + 5544 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH; 5545 auto names_size = pdr1.size() - names_offset; 5546 size_t length = 0; 5547 5548 size_t decodedPdrSize = 5549 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size; 5550 auto decodedPdr = 5551 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize); 5552 EXPECT_NE(nullptr, decodedPdr); 5553 5554 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(), 5555 decodedPdr, decodedPdrSize); 5556 5557 EXPECT_EQ(0, rc); 5558 EXPECT_EQ(1, decodedPdr->hdr.record_handle); 5559 EXPECT_EQ(1, decodedPdr->hdr.version); 5560 EXPECT_EQ(PLDM_ENTITY_AUXILIARY_NAMES_PDR, decodedPdr->hdr.type); 5561 EXPECT_EQ(1, decodedPdr->hdr.record_change_num); 5562 EXPECT_EQ(pdr1.size() - sizeof(struct pldm_pdr_hdr), 5563 decodedPdr->hdr.length); 5564 EXPECT_EQ(3, decodedPdr->container.entity_type); 5565 EXPECT_EQ(1, decodedPdr->container.entity_instance_num); 5566 EXPECT_EQ(PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID, 5567 decodedPdr->container.entity_container_id); 5568 EXPECT_EQ(0, decodedPdr->shared_name_count); 5569 EXPECT_EQ(3, decodedPdr->name_string_count); 5570 5571 decodedPdr->names = (struct pldm_entity_auxiliary_name*)calloc( 5572 decodedPdr->name_string_count, 5573 sizeof(struct pldm_entity_auxiliary_name)); 5574 EXPECT_NE(nullptr, decodedPdr->names); 5575 5576 rc = decode_pldm_entity_auxiliary_names_pdr_index(decodedPdr); 5577 EXPECT_EQ(0, rc); 5578 5579 length = strlen(decodedPdr->names[0].tag); 5580 EXPECT_EQ(strlen(expectTag0), length); 5581 EXPECT_EQ(strncmp(expectTag0, decodedPdr->names[0].tag, length + 1), 0); 5582 5583 // NOLINTBEGIN(clang-analyzer-unix.Malloc) 5584 ASSERT_EQ(0, 5585 (uintptr_t)decodedPdr->names[0].name & (alignof(char16_t) - 1)); 5586 // NOLINTEND(clang-analyzer-unix.Malloc) 5587 length = str16len((char16_t*)decodedPdr->names[0].name); 5588 EXPECT_EQ(str16len((char16_t*)expectName0), length); 5589 EXPECT_EQ(3, str16len((char16_t*)expectName0)); 5590 EXPECT_EQ(memcmp(expectName0, decodedPdr->names[0].name, 5591 sizeof(char16_t) * (length + 1)), 5592 0); 5593 5594 length = strlen(decodedPdr->names[1].tag); 5595 EXPECT_EQ(strlen(expectTag1), length); 5596 EXPECT_EQ(strncmp(expectTag1, decodedPdr->names[1].tag, length + 1), 0); 5597 5598 // NOLINTBEGIN(clang-analyzer-unix.Malloc) 5599 ASSERT_EQ(0, 5600 (uintptr_t)decodedPdr->names[1].name & (alignof(char16_t) - 1)); 5601 // NOLINTEND(clang-analyzer-unix.Malloc) 5602 length = str16len((char16_t*)decodedPdr->names[1].name); 5603 EXPECT_EQ(str16len((char16_t*)expectName1), length); 5604 EXPECT_EQ(2, str16len((char16_t*)expectName1)); 5605 EXPECT_EQ(memcmp(expectName1, decodedPdr->names[1].name, 5606 sizeof(char16_t) * (length + 1)), 5607 0); 5608 5609 length = strlen(decodedPdr->names[2].tag); 5610 EXPECT_EQ(strlen(expectTag2), length); 5611 EXPECT_EQ(strncmp(expectTag2, decodedPdr->names[2].tag, length + 1), 0); 5612 5613 // NOLINTBEGIN(clang-analyzer-unix.Malloc) 5614 ASSERT_EQ(0, 5615 (uintptr_t)decodedPdr->names[2].name & (alignof(char16_t) - 1)); 5616 // NOLINTEND(clang-analyzer-unix.Malloc) 5617 length = str16len((char16_t*)decodedPdr->names[2].name); 5618 EXPECT_EQ(str16len((char16_t*)expectName2), length); 5619 EXPECT_EQ(3, str16len((char16_t*)expectName2)); 5620 EXPECT_EQ(memcmp(expectName2, decodedPdr->names[2].name, 5621 sizeof(char16_t) * (length + 1)), 5622 0); 5623 5624 free(decodedPdr->names); 5625 free(decodedPdr); 5626 } 5627 5628 TEST(decodeEntityAuxNamePdrData, BadTest) 5629 { 5630 std::vector<uint8_t> pdr1{ 5631 // Common PDR Header 5632 0x1, 0x0, 0x0, 0x0, // record handle 5633 0x1, // PDRHeaderVersion 5634 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType 5635 0x1, 5636 0x0, // recordChangeNumber 5637 0x25, // correct size is 0x27, input invalid size 5638 0, // dataLength 5639 /* Entity Auxiliary Names PDR Data*/ 5640 3, 5641 0, // entityType system software 5642 0x1, 5643 0x0, // Entity instance number =1 5644 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID, 5645 0, // Overal system 5646 0, // shared Name Count one name only 5647 0, // Invalid nameStringCount 5648 0x65, 0x6e, 0x00, // Language Tag "en" 5649 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S" 5650 0x66, 0x6e, 0x00, // Language Tag "en" 5651 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1" 5652 0x67, 0x6e, 0x00, // Language Tag "en" 5653 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3" 5654 }; 5655 5656 auto names_offset = sizeof(struct pldm_pdr_hdr) + 5657 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH; 5658 auto names_size = pdr1.size() - names_offset; 5659 5660 size_t decodedPdrSize = 5661 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size; 5662 auto decodedPdr = 5663 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize); 5664 5665 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(), 5666 decodedPdr, decodedPdrSize); 5667 5668 EXPECT_EQ(-EBADMSG, rc); 5669 free(decodedPdr); 5670 } 5671 5672 TEST(PlatformEventMessage, testGoodCperEventDataDecodeRequest) 5673 { 5674 constexpr const size_t eventDataSize = 4; 5675 constexpr const size_t eventSize = 5676 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize; 5677 std::array<uint8_t, eventSize> eventData{ 5678 0x1, // format version 5679 0x0, // format type 5680 0x4, 0x0, // event data length 5681 0x44, 0x33, 0x22, 0x11 // data 5682 }; 5683 5684 uint8_t expectedFormatVersion = 1; 5685 uint8_t expectedFormatType = 0; 5686 uint16_t expectedEventDataLength = 4; 5687 uint8_t expectCperEventData[] = {0x44, 0x33, 0x22, 0x11}; 5688 5689 size_t cperEventSize = 5690 sizeof(struct pldm_platform_cper_event) + eventDataSize; 5691 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5692 auto cper_event = reinterpret_cast<struct pldm_platform_cper_event*>( 5693 malloc(cperEventSize)); 5694 5695 auto rc = decode_pldm_platform_cper_event( 5696 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5697 reinterpret_cast<const void*>(eventData.data()), eventData.size(), 5698 cper_event, cperEventSize); 5699 5700 EXPECT_EQ(rc, 0); 5701 EXPECT_EQ(cper_event->format_version, expectedFormatVersion); 5702 EXPECT_EQ(cper_event->format_type, expectedFormatType); 5703 EXPECT_EQ(cper_event->event_data_length, expectedEventDataLength); 5704 5705 auto cperEventData = pldm_platform_cper_event_event_data(cper_event); 5706 EXPECT_NE(cperEventData, nullptr); 5707 if (cperEventData) 5708 { 5709 EXPECT_EQ(0, memcmp(expectCperEventData, cperEventData, 5710 expectedEventDataLength)); 5711 } 5712 5713 free(cper_event); 5714 } 5715 5716 TEST(PlatformEventMessage, testBadCperEventDataDecodeRequest) 5717 { 5718 5719 constexpr const size_t eventDataSize = 4; 5720 constexpr const size_t eventSize = 5721 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize; 5722 std::array<uint8_t, eventSize> eventData{ 5723 0x1, // format version 5724 0x0, // format type 5725 0x4, 0x0, // event data length 5726 0x44, 0x33, 0x22, 0x11 // data 5727 }; 5728 5729 size_t cperEventSize = 5730 sizeof(struct pldm_platform_cper_event) + eventDataSize; 5731 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5732 auto cperEvent = reinterpret_cast<struct pldm_platform_cper_event*>( 5733 malloc(cperEventSize)); 5734 5735 auto rc = decode_pldm_platform_cper_event(NULL, eventData.size(), cperEvent, 5736 cperEventSize); 5737 EXPECT_EQ(rc, -EINVAL); 5738 5739 rc = decode_pldm_platform_cper_event( 5740 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5741 reinterpret_cast<const void*>(eventData.data()), eventData.size(), NULL, 5742 cperEventSize); 5743 EXPECT_EQ(rc, -EINVAL); 5744 5745 #ifdef NDEBUG 5746 rc = decode_pldm_platform_cper_event( 5747 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5748 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1, 5749 cperEvent, cperEventSize); 5750 EXPECT_EQ(rc, -EOVERFLOW); 5751 #else 5752 EXPECT_DEATH( 5753 decode_pldm_platform_cper_event( 5754 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5755 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1, 5756 cperEvent, cperEventSize), 5757 "ctx->remaining >= 0"); 5758 #endif 5759 5760 rc = decode_pldm_platform_cper_event( 5761 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5762 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), 5763 cperEvent, cperEventSize - 1); 5764 EXPECT_EQ(rc, -EOVERFLOW); 5765 5766 rc = decode_pldm_platform_cper_event( 5767 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5768 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), 5769 cperEvent, cperEventSize + 1); 5770 EXPECT_EQ(rc, 0); 5771 5772 // Invalid CPER Event Format Type 5773 eventData[1] = 0x2; 5774 rc = decode_pldm_platform_cper_event( 5775 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5776 reinterpret_cast<const void*>(eventData.data()), eventData.size(), 5777 cperEvent, cperEventSize); 5778 5779 EXPECT_EQ(rc, -EPROTO); 5780 5781 // Invalid cper event data size 5782 eventData[1] = 0x1; 5783 eventData[2] = 3; 5784 rc = decode_pldm_platform_cper_event( 5785 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5786 reinterpret_cast<const void*>(eventData.data()), eventData.size(), 5787 cperEvent, cperEventSize); 5788 5789 EXPECT_EQ(rc, -EBADMSG); 5790 5791 eventData[2] = 5; 5792 rc = decode_pldm_platform_cper_event( 5793 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 5794 reinterpret_cast<const void*>(eventData.data()), eventData.size(), 5795 cperEvent, cperEventSize); 5796 5797 EXPECT_EQ(rc, -EOVERFLOW); 5798 5799 free(cperEvent); 5800 } 5801