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