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