blob: 9c441303e01a8fb95e81443065619c44a3e852aa [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10302#include <libpldm/base.h>
3#include <libpldm/entity.h>
4#include <libpldm/platform.h>
5#include <libpldm/pldm_types.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09306
7#include <array>
Tal Yacobif490a382024-05-31 09:57:36 +03008#include <cerrno>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +093010#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053011#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +093012
Thu Nguyen159a98b2022-11-02 10:00:10 +070013#include "msgbuf.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +093014
15#include <gtest/gtest.h>
16
17constexpr auto hdrSize = sizeof(pldm_msg_hdr);
18
19TEST(SetStateEffecterStates, testEncodeResponse)
20{
21 std::array<uint8_t,
22 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
23 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093024 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093025 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
26 uint8_t completionCode = 0;
27
28 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response);
29
30 EXPECT_EQ(rc, PLDM_SUCCESS);
31 EXPECT_EQ(completionCode, response->payload[0]);
32}
33
34TEST(SetStateEffecterStates, testEncodeRequest)
35{
36 std::array<uint8_t,
37 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
38 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093039 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093040 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
41
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -060042 uint16_t effecterId = 0x0a;
Andrew Jeffery9c766792022-08-10 23:12:49 +093043 uint8_t compEffecterCnt = 0x2;
44 std::array<set_effecter_state_field, 8> stateField{};
45 stateField[0] = {PLDM_REQUEST_SET, 2};
46 stateField[1] = {PLDM_REQUEST_SET, 3};
47
48 auto rc = encode_set_state_effecter_states_req(
49 0, effecterId, compEffecterCnt, stateField.data(), request);
50
51 EXPECT_EQ(rc, PLDM_SUCCESS);
52 EXPECT_EQ(effecterId, request->payload[0]);
53 EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
54 EXPECT_EQ(stateField[0].set_request,
55 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
56 EXPECT_EQ(stateField[0].effecter_state,
57 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
58 sizeof(stateField[0].set_request)]);
59 EXPECT_EQ(stateField[1].set_request,
60 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
61 sizeof(stateField[0])]);
62 EXPECT_EQ(stateField[1].effecter_state,
63 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
64 sizeof(stateField[0]) +
65 sizeof(stateField[1].set_request)]);
66}
67
68TEST(SetStateEffecterStates, testGoodDecodeResponse)
69{
70 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
71 responseMsg{};
72
73 uint8_t retcompletion_code = 0;
74
75 responseMsg[hdrSize] = PLDM_SUCCESS;
76
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093077 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093078 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
79
80 auto rc = decode_set_state_effecter_states_resp(
81 response, responseMsg.size() - hdrSize, &retcompletion_code);
82
83 EXPECT_EQ(rc, PLDM_SUCCESS);
84 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
85}
86
87TEST(SetStateEffecterStates, testGoodDecodeRequest)
88{
89 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
90 requestMsg{};
91
92 uint16_t effecterId = 0x32;
93 uint16_t effecterIdLE = htole16(effecterId);
94 uint8_t compEffecterCnt = 0x2;
95
96 std::array<set_effecter_state_field, 8> stateField{};
97 stateField[0] = {PLDM_REQUEST_SET, 3};
98 stateField[1] = {PLDM_REQUEST_SET, 4};
99
100 uint16_t retEffecterId = 0;
101 uint8_t retCompEffecterCnt = 0;
102
103 std::array<set_effecter_state_field, 8> retStateField{};
104
105 memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE));
106 memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt,
107 sizeof(compEffecterCnt));
108 memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) +
109 hdrSize,
110 &stateField, sizeof(stateField));
111
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930112 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930113 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
114
115 auto rc = decode_set_state_effecter_states_req(
116 request, requestMsg.size() - hdrSize, &retEffecterId,
117 &retCompEffecterCnt, retStateField.data());
118
119 EXPECT_EQ(rc, PLDM_SUCCESS);
120 EXPECT_EQ(effecterId, retEffecterId);
121 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt);
122 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request);
123 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
124 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request);
125 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
126}
127
128TEST(SetStateEffecterStates, testBadDecodeRequest)
129{
130 const struct pldm_msg* msg = NULL;
131
132 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
133 NULL, NULL);
134
135 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
136}
137
138TEST(SetStateEffecterStates, testBadDecodeResponse)
139{
140 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
141 responseMsg{};
142
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930143 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930144 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
145
146 auto rc = decode_set_state_effecter_states_resp(response,
147 responseMsg.size(), NULL);
148
149 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
150}
151
152TEST(GetPDR, testGoodEncodeResponse)
153{
154 uint8_t completionCode = 0;
155 uint32_t nextRecordHndl = 0x12;
156 uint32_t nextDataTransferHndl = 0x13;
157 uint8_t transferFlag = PLDM_END;
158 uint16_t respCnt = 0x5;
159 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
160 uint8_t transferCRC = 6;
161
162 // + size of record data and transfer CRC
163 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
164 recordData.size() + 1);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930165 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930166 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
167
168 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
169 nextDataTransferHndl, transferFlag, respCnt,
170 recordData.data(), transferCRC, response);
171
172 EXPECT_EQ(rc, PLDM_SUCCESS);
173 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930174 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930175 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
176
177 EXPECT_EQ(completionCode, resp->completion_code);
178 EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle));
179 EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle));
180 EXPECT_EQ(transferFlag, resp->transfer_flag);
181 EXPECT_EQ(respCnt, le16toh(resp->response_count));
182 EXPECT_EQ(0,
183 memcmp(recordData.data(), resp->record_data, recordData.size()));
184 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
185 recordData.size()),
186 transferCRC);
187
188 transferFlag = PLDM_START_AND_END; // No CRC in this case
189 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
190 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
191 nextDataTransferHndl, transferFlag, respCnt,
192 recordData.data(), transferCRC, response);
193 EXPECT_EQ(rc, PLDM_SUCCESS);
194}
195
196TEST(GetPDR, testBadEncodeResponse)
197{
198 uint32_t nextRecordHndl = 0x12;
199 uint32_t nextDataTransferHndl = 0x13;
200 uint8_t transferFlag = PLDM_START_AND_END;
201 uint16_t respCnt = 0x5;
202 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
203 uint8_t transferCRC = 0;
204
205 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
206 nextDataTransferHndl, transferFlag, respCnt,
207 recordData.data(), transferCRC, nullptr);
208
209 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
210}
211
212TEST(GetPDR, testGoodDecodeRequest)
213{
214 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
215
216 uint32_t recordHndl = 0x32;
217 uint32_t dataTransferHndl = 0x11;
218 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
219 uint16_t requestCnt = 0x5;
220 uint16_t recordChangeNum = 0x01;
221
222 uint32_t retRecordHndl = 0;
223 uint32_t retDataTransferHndl = 0;
224 uint8_t retTransferOpFlag = 0;
225 uint16_t retRequestCnt = 0;
226 uint16_t retRecordChangeNum = 0;
227
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930228 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930229 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
230 struct pldm_get_pdr_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930232 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
233
234 request->record_handle = htole32(recordHndl);
235 request->data_transfer_handle = htole32(dataTransferHndl);
236 request->transfer_op_flag = transferOpFlag;
237 request->request_count = htole16(requestCnt);
238 request->record_change_number = htole16(recordChangeNum);
239
240 auto rc = decode_get_pdr_req(
241 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
242 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
243
244 EXPECT_EQ(rc, PLDM_SUCCESS);
245 EXPECT_EQ(retRecordHndl, recordHndl);
246 EXPECT_EQ(retDataTransferHndl, dataTransferHndl);
247 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
248 EXPECT_EQ(retRequestCnt, requestCnt);
249 EXPECT_EQ(retRecordChangeNum, recordChangeNum);
250}
251
252TEST(GetPDR, testBadDecodeRequest)
253{
254 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930255 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930256 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
257
258 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
259 NULL);
260
261 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
262}
263
264TEST(GetPDR, testGoodEncodeRequest)
265{
266 uint32_t record_hndl = 0;
267 uint32_t data_transfer_hndl = 0;
268 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
269 uint16_t request_cnt = 20;
270 uint16_t record_chg_num = 0;
271
272 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930273 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930274 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
275
276 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
277 transfer_op_flag, request_cnt, record_chg_num,
278 request, PLDM_GET_PDR_REQ_BYTES);
279 EXPECT_EQ(rc, PLDM_SUCCESS);
280 struct pldm_get_pdr_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930281 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930282 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
283 EXPECT_EQ(record_hndl, le32toh(req->record_handle));
284 EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle));
285 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
286 EXPECT_EQ(request_cnt, le16toh(req->request_count));
287 EXPECT_EQ(record_chg_num, le16toh(req->record_change_number));
288}
289
290TEST(GetPDR, testBadEncodeRequest)
291{
292 uint32_t record_hndl = 0;
293 uint32_t data_transfer_hndl = 0;
294 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
295 uint16_t request_cnt = 32;
296 uint16_t record_chg_num = 0;
297
298 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930299 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930300 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
301
302 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
303 transfer_op_flag, request_cnt, record_chg_num,
304 nullptr, PLDM_GET_PDR_REQ_BYTES);
305 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
306
307 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
308 transfer_op_flag, request_cnt, record_chg_num,
309 request, PLDM_GET_PDR_REQ_BYTES + 1);
310 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
311}
312
313TEST(GetPDR, testGoodDecodeResponse)
314{
315 const char* recordData = "123456789";
316 uint8_t completionCode = PLDM_SUCCESS;
317 uint32_t nextRecordHndl = 0;
318 uint32_t nextDataTransferHndl = 0;
319 uint8_t transferFlag = PLDM_END;
320 constexpr uint16_t respCnt = 9;
321 uint8_t transferCRC = 96;
322 size_t recordDataLength = 32;
323 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
324 sizeof(transferCRC)>
325 responseMsg{};
326
327 uint8_t retCompletionCode = 0;
328 uint8_t retRecordData[32] = {0};
329 uint32_t retNextRecordHndl = 0;
330 uint32_t retNextDataTransferHndl = 0;
331 uint8_t retTransferFlag = 0;
332 uint16_t retRespCnt = 0;
333 uint8_t retTransferCRC = 0;
334
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930335 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930336 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
337 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930338 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
340 resp->completion_code = completionCode;
341 resp->next_record_handle = htole32(nextRecordHndl);
342 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
343 resp->transfer_flag = transferFlag;
344 resp->response_count = htole16(respCnt);
345 memcpy(resp->record_data, recordData, respCnt);
346 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
347
348 auto rc = decode_get_pdr_resp(
349 response, responseMsg.size() - hdrSize, &retCompletionCode,
350 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
351 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
352 EXPECT_EQ(rc, PLDM_SUCCESS);
353 EXPECT_EQ(retCompletionCode, completionCode);
354 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
355 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
356 EXPECT_EQ(retTransferFlag, transferFlag);
357 EXPECT_EQ(retRespCnt, respCnt);
358 EXPECT_EQ(retTransferCRC, transferCRC);
359 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
360}
361
362TEST(GetPDR, testBadDecodeResponse)
363{
364 const char* recordData = "123456789";
365 uint8_t completionCode = PLDM_SUCCESS;
366 uint32_t nextRecordHndl = 0;
367 uint32_t nextDataTransferHndl = 0;
368 uint8_t transferFlag = PLDM_END;
369 constexpr uint16_t respCnt = 9;
370 uint8_t transferCRC = 96;
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930371 size_t recordDataLength = respCnt - 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930372 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
373 sizeof(transferCRC)>
374 responseMsg{};
375
376 uint8_t retCompletionCode = 0;
377 uint8_t retRecordData[32] = {0};
378 uint32_t retNextRecordHndl = 0;
379 uint32_t retNextDataTransferHndl = 0;
380 uint8_t retTransferFlag = 0;
381 uint16_t retRespCnt = 0;
382 uint8_t retTransferCRC = 0;
383
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930384 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930385 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
386 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930387 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930388 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
389 resp->completion_code = completionCode;
390 resp->next_record_handle = htole32(nextRecordHndl);
391 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
392 resp->transfer_flag = transferFlag;
393 resp->response_count = htole16(respCnt);
394 memcpy(resp->record_data, recordData, respCnt);
395 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
396
397 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
398 NULL, NULL, NULL, NULL, NULL, 0, NULL);
399 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
400
401 rc = decode_get_pdr_resp(
402 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
403 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
404 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
405 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
406}
407
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000408#ifdef LIBPLDM_API_TESTING
409TEST(GetPDR, testGoodDecodeResponseSafe)
410{
411 static const char recordData[] = "123456789";
412
413 alignas(pldm_msg) unsigned char data[sizeof(pldm_msg_hdr) +
414 PLDM_GET_PDR_MIN_RESP_BYTES +
415 sizeof(recordData) - 1 + 1];
416 struct pldm_msgbuf _buf;
417 struct pldm_msgbuf* buf = &_buf;
418 int rc;
419
420 pldm_msg* msg = new (data) pldm_msg;
421
422 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload,
423 sizeof(data) - sizeof(msg->hdr));
424 ASSERT_EQ(rc, 0);
425
426 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS);
427 pldm_msgbuf_insert_uint32(buf, 0);
428 pldm_msgbuf_insert_uint32(buf, 0);
429 pldm_msgbuf_insert_uint8(buf, PLDM_END);
430 pldm_msgbuf_insert_uint16(buf, sizeof(recordData) - 1);
431 rc = pldm_msgbuf_insert_array_char(buf, sizeof(recordData) - 1, recordData,
432 sizeof(recordData) - 1);
433 ASSERT_EQ(rc, 0);
434 pldm_msgbuf_insert_uint8(buf, 96);
435 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
436
437 alignas(pldm_get_pdr_resp) unsigned char
438 resp_data[sizeof(pldm_get_pdr_resp) + sizeof(recordData) - 1];
439 pldm_get_pdr_resp* resp = new (resp_data) pldm_get_pdr_resp;
440 uint8_t crc;
441 rc = decode_get_pdr_resp_safe(msg, sizeof(data) - sizeof(msg->hdr), resp,
442 sizeof(resp_data) - sizeof(*resp), &crc);
443 ASSERT_EQ(rc, 0);
444 EXPECT_EQ(resp->completion_code, PLDM_SUCCESS);
445 EXPECT_EQ(resp->next_record_handle, 0);
446 EXPECT_EQ(resp->next_data_transfer_handle, 0);
447 EXPECT_EQ(resp->transfer_flag, PLDM_END);
448 ASSERT_EQ(resp->response_count, sizeof(recordData) - 1);
449 EXPECT_EQ(crc, 96);
450 EXPECT_EQ(0, memcmp(recordData, resp->record_data, resp->response_count));
451}
452#endif
453
454#ifdef LIBPLDM_API_TESTING
455TEST(GetPDR, testBadDecodeResponseSafeTrivial)
456{
457 pldm_get_pdr_resp resp;
458 uint8_t crc;
459 int rc;
460
461 rc = decode_get_pdr_resp_safe(nullptr, PLDM_GET_PDR_MIN_RESP_BYTES, &resp,
462 sizeof(resp), &crc);
463 EXPECT_EQ(rc, -EINVAL);
464
465 alignas(pldm_msg) unsigned char
466 msg_data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES];
467 pldm_msg* msg = new (msg_data) pldm_msg;
468 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, nullptr,
469 sizeof(resp), &crc);
470 EXPECT_EQ(rc, -EINVAL);
471
472 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, &resp,
473 sizeof(resp), nullptr);
474 EXPECT_EQ(rc, -EINVAL);
475
476 msg->payload[0] = PLDM_ERROR_INVALID_DATA;
477 rc = decode_get_pdr_resp_safe(msg, 1, &resp, sizeof(resp), &crc);
478 EXPECT_EQ(rc, 0);
479 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA);
480}
481#endif
482
Andrew Jeffery9c766792022-08-10 23:12:49 +0930483TEST(GetPDRRepositoryInfo, testGoodEncodeResponse)
484{
485 uint8_t completionCode = 0;
486 uint8_t repositoryState = PLDM_AVAILABLE;
487 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
488 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
489 uint32_t recordCount = 100;
490 uint32_t repositorySize = 100;
491 uint32_t largestRecordSize = UINT32_MAX;
492 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
493
494 std::vector<uint8_t> responseMsg(hdrSize +
495 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930496 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930497 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
498
499 auto rc = encode_get_pdr_repository_info_resp(
500 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
501 recordCount, repositorySize, largestRecordSize,
502 dataTransferHandleTimeout, response);
503
504 EXPECT_EQ(rc, PLDM_SUCCESS);
505 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930506 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930507 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
508 response->payload);
509
510 EXPECT_EQ(completionCode, resp->completion_code);
511 EXPECT_EQ(repositoryState, resp->repository_state);
512 EXPECT_EQ(0, memcmp(updateTime, resp->update_time, PLDM_TIMESTAMP104_SIZE));
513 EXPECT_EQ(0, memcmp(oemUpdateTime, resp->oem_update_time,
514 PLDM_TIMESTAMP104_SIZE));
515 EXPECT_EQ(recordCount, le32toh(resp->record_count));
516 EXPECT_EQ(repositorySize, le32toh(resp->repository_size));
517 EXPECT_EQ(largestRecordSize, le32toh(resp->largest_record_size));
518 EXPECT_EQ(dataTransferHandleTimeout, resp->data_transfer_handle_timeout);
519}
520
521TEST(GetPDRRepositoryInfo, testBadEncodeResponse)
522{
523 uint8_t repositoryState = PLDM_AVAILABLE;
524 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
525 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
526 uint32_t recordCount = 100;
527 uint32_t repositorySize = 100;
528 uint32_t largestRecordSize = UINT32_MAX;
529 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
530
531 auto rc = encode_get_pdr_repository_info_resp(
532 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
533 recordCount, repositorySize, largestRecordSize,
534 dataTransferHandleTimeout, nullptr);
535 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
536}
537
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800538TEST(GetPDRRepositoryInfo, testGoodDecodeResponse)
539{
540 uint8_t completionCode = PLDM_SUCCESS;
541 uint8_t repositoryState = PLDM_AVAILABLE;
542 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
543 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
544 uint32_t recordCount = 100;
545 uint32_t repositorySize = 100;
546 uint32_t largestRecordSize = UINT32_MAX;
547 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
548
549 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
550 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930551 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800552 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
553 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930554 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800555 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
556 response->payload);
557 resp->completion_code = completionCode;
558 resp->repository_state = repositoryState;
559 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
560 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
561 resp->record_count = htole32(recordCount);
562 resp->repository_size = htole32(repositorySize);
563 resp->largest_record_size = htole32(largestRecordSize);
564 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
565
566 uint8_t retCompletionCode = 0;
567 uint8_t retRepositoryState = 0;
568 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
569 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
570 uint32_t retRecordCount = 0;
571 uint32_t retRepositorySize = 0;
572 uint32_t retLargestRecordSize = 0;
573 uint8_t retDataTransferHandleTimeout = 0;
574
575 auto rc = decode_get_pdr_repository_info_resp(
576 response, responseMsg.size() - hdrSize, &retCompletionCode,
577 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
578 &retRepositorySize, &retLargestRecordSize,
579 &retDataTransferHandleTimeout);
580
581 EXPECT_EQ(rc, PLDM_SUCCESS);
582 EXPECT_EQ(completionCode, retCompletionCode);
583 EXPECT_EQ(repositoryState, retRepositoryState);
584 EXPECT_EQ(0, memcmp(updateTime, retUpdateTime, PLDM_TIMESTAMP104_SIZE));
585 EXPECT_EQ(0,
586 memcmp(oemUpdateTime, retOemUpdateTime, PLDM_TIMESTAMP104_SIZE));
587 EXPECT_EQ(recordCount, recordCount);
588 EXPECT_EQ(repositorySize, repositorySize);
589 EXPECT_EQ(largestRecordSize, largestRecordSize);
590 EXPECT_EQ(dataTransferHandleTimeout, dataTransferHandleTimeout);
591}
592
593TEST(GetPDRRepositoryInfo, testBadDecodeResponse)
594{
595 uint8_t completionCode = PLDM_SUCCESS;
596 uint8_t repositoryState = PLDM_AVAILABLE;
597 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
598 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
599 uint32_t recordCount = htole32(100);
600 uint32_t repositorySize = htole32(100);
601 uint32_t largestRecordSize = htole32(UINT32_MAX);
602 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
603
604 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
605 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930606 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800607 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
608 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930609 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800610 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
611 response->payload);
612 resp->completion_code = completionCode;
613 resp->repository_state = repositoryState;
614 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
615 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
616 resp->record_count = recordCount;
617 resp->repository_size = repositorySize;
618 resp->largest_record_size = largestRecordSize;
619 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
620
621 uint8_t retCompletionCode = 0;
622 uint8_t retRepositoryState = 0;
623 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
624 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
625 uint32_t retRecordCount = 0;
626 uint32_t retRepositorySize = 0;
627 uint32_t retLargestRecordSize = 0;
628 uint8_t retDataTransferHandleTimeout = 0;
629
630 auto rc = decode_get_pdr_repository_info_resp(
631 response, responseMsg.size() - hdrSize, NULL, NULL, NULL, NULL, NULL,
632 NULL, NULL, NULL);
633 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
634
635 rc = decode_get_pdr_repository_info_resp(
636 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
637 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
638 &retRepositorySize, &retLargestRecordSize,
639 &retDataTransferHandleTimeout);
640 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
641
642 resp->repository_state = PLDM_FAILED + 1;
643 rc = decode_get_pdr_repository_info_resp(
644 response, responseMsg.size() - hdrSize, &retCompletionCode,
645 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
646 &retRepositorySize, &retLargestRecordSize,
647 &retDataTransferHandleTimeout);
648 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
649}
650
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000651#ifdef LIBPLDM_API_TESTING
652TEST(GetPDRRepositoryInfo, testGoodDecodeResponseSafe)
653{
654 alignas(pldm_msg) unsigned char
655 data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES];
656 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
657 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
658 struct pldm_msgbuf _buf;
659 struct pldm_msgbuf* buf = &_buf;
660 int rc;
661
662 pldm_msg* msg = new (data) pldm_msg;
663
664 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
665 msg->payload, sizeof(data) - sizeof(msg->hdr));
666 ASSERT_EQ(rc, 0);
667 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS);
668 pldm_msgbuf_insert_uint8(buf, PLDM_AVAILABLE);
669 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE, updateTime,
670 sizeof(updateTime));
671 ASSERT_EQ(rc, 0);
672 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE,
673 oemUpdateTime, sizeof(oemUpdateTime));
674 ASSERT_EQ(rc, 0);
675 pldm_msgbuf_insert_uint32(buf, 100);
676 pldm_msgbuf_insert_uint32(buf, 100);
677 pldm_msgbuf_insert_uint32(buf, UINT32_MAX);
678 pldm_msgbuf_insert_uint8(buf, PLDM_NO_TIMEOUT);
679 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
680
681 struct pldm_pdr_repository_info_resp resp;
682 rc = decode_get_pdr_repository_info_resp_safe(
683 msg, sizeof(data) - sizeof(msg->hdr), &resp);
684
685 EXPECT_EQ(rc, 0);
686 EXPECT_EQ(PLDM_SUCCESS, resp.completion_code);
687 EXPECT_EQ(PLDM_AVAILABLE, resp.repository_state);
688 EXPECT_EQ(0,
689 memcmp(updateTime, resp.update_time, sizeof(resp.update_time)));
690 EXPECT_EQ(0, memcmp(oemUpdateTime, resp.oem_update_time,
691 sizeof(resp.oem_update_time)));
692 EXPECT_EQ(100, resp.record_count);
693 EXPECT_EQ(100, resp.repository_size);
694 EXPECT_EQ(UINT32_MAX, resp.largest_record_size);
695 EXPECT_EQ(PLDM_NO_TIMEOUT, resp.data_transfer_handle_timeout);
696}
697#endif
698
699#ifdef LIBPLDM_API_TESTING
700TEST(GetPDRRepositoryInfo, testBadDecodeResponseSafeTrivial)
701{
702 struct pldm_pdr_repository_info_resp resp;
703 int rc;
704
705 rc = decode_get_pdr_repository_info_resp_safe(
706 nullptr, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, &resp);
707 EXPECT_EQ(rc, -EINVAL);
708
709 alignas(pldm_msg) unsigned char
710 msg_data[sizeof(pldm_msg) - 1 +
711 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES];
712 pldm_msg* msg = new (msg_data) pldm_msg;
713 rc = decode_get_pdr_repository_info_resp_safe(msg, 0, &resp);
714 EXPECT_EQ(rc, -EOVERFLOW);
715
716 rc = decode_get_pdr_repository_info_resp_safe(
717 msg, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, nullptr);
718 EXPECT_EQ(rc, -EINVAL);
719
720 msg->payload[0] = PLDM_ERROR_INVALID_DATA;
721 rc = decode_get_pdr_repository_info_resp_safe(msg, 1, &resp);
722 EXPECT_EQ(rc, 0);
723 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA);
724}
725#endif
726
Andrew Jeffery9c766792022-08-10 23:12:49 +0930727TEST(SetNumericEffecterValue, testGoodDecodeRequest)
728{
729 std::array<uint8_t,
730 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3>
731 requestMsg{};
732
733 uint16_t effecter_id = 32768;
734 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32;
735 uint32_t effecter_value = 123456789;
736
737 uint16_t reteffecter_id;
738 uint8_t reteffecter_data_size;
739 uint8_t reteffecter_value[4];
740
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930741 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930742 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
743 struct pldm_set_numeric_effecter_value_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930744 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930745 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
746 req->payload);
747
748 request->effecter_id = htole16(effecter_id);
749 request->effecter_data_size = effecter_data_size;
750 uint32_t effecter_value_le = htole32(effecter_value);
751 memcpy(request->effecter_value, &effecter_value_le,
752 sizeof(effecter_value_le));
753
754 auto rc = decode_set_numeric_effecter_value_req(
755 req, requestMsg.size() - hdrSize, &reteffecter_id,
756 &reteffecter_data_size, reteffecter_value);
757
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930758 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930759 uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value));
760 EXPECT_EQ(rc, PLDM_SUCCESS);
761 EXPECT_EQ(reteffecter_id, effecter_id);
762 EXPECT_EQ(reteffecter_data_size, effecter_data_size);
763 EXPECT_EQ(value, effecter_value);
764}
765
766TEST(SetNumericEffecterValue, testBadDecodeRequest)
767{
768 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES>
769 requestMsg{};
770
771 auto rc = decode_set_numeric_effecter_value_req(
772 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL);
773 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
774
775 uint16_t effecter_id = 0x10;
776 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
777 uint8_t effecter_value = 1;
778
779 uint16_t reteffecter_id;
780 uint8_t reteffecter_data_size;
781 uint8_t reteffecter_value[4];
782
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930783 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930784 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
785 struct pldm_set_numeric_effecter_value_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930786 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930787 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
788 req->payload);
789
790 request->effecter_id = effecter_id;
791 request->effecter_data_size = effecter_data_size;
792 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
793
794 rc = decode_set_numeric_effecter_value_req(
795 req, requestMsg.size() - hdrSize - 1, &reteffecter_id,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930796 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930797 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
798 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
799}
800
801TEST(SetNumericEffecterValue, testGoodEncodeRequest)
802{
803 uint16_t effecter_id = 0;
804 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16;
805 uint16_t effecter_value = 65534;
806
807 std::vector<uint8_t> requestMsg(
808 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930809 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930810 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
811
812 auto rc = encode_set_numeric_effecter_value_req(
813 0, effecter_id, effecter_data_size,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930814 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930815 reinterpret_cast<uint8_t*>(&effecter_value), request,
816 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
817 EXPECT_EQ(rc, PLDM_SUCCESS);
818
819 struct pldm_set_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930820 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930821 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
822 request->payload);
823 EXPECT_EQ(effecter_id, req->effecter_id);
824 EXPECT_EQ(effecter_data_size, req->effecter_data_size);
825 uint16_t* val = (uint16_t*)req->effecter_value;
826 *val = le16toh(*val);
827 EXPECT_EQ(effecter_value, *val);
828}
829
830TEST(SetNumericEffecterValue, testBadEncodeRequest)
831{
832 std::vector<uint8_t> requestMsg(
833 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930834 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930835 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
836
837 auto rc = encode_set_numeric_effecter_value_req(
838 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
839 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
840
841 uint16_t effecter_value;
842 rc = encode_set_numeric_effecter_value_req(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930843 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930844 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
845 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
846 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
847}
848
849TEST(SetNumericEffecterValue, testGoodDecodeResponse)
850{
851 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
852 responseMsg{};
853
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600854 uint8_t completion_code = 0xa0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930855
856 uint8_t retcompletion_code;
857
858 memcpy(responseMsg.data() + hdrSize, &completion_code,
859 sizeof(completion_code));
860
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930861 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930862 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
863
864 auto rc = decode_set_numeric_effecter_value_resp(
865 response, responseMsg.size() - hdrSize, &retcompletion_code);
866
867 EXPECT_EQ(rc, PLDM_SUCCESS);
868 EXPECT_EQ(completion_code, retcompletion_code);
869}
870
871TEST(SetNumericEffecterValue, testBadDecodeResponse)
872{
873 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
874 responseMsg{};
875
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930876 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930877 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
878
879 auto rc = decode_set_numeric_effecter_value_resp(response,
880 responseMsg.size(), NULL);
881
882 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
883}
884
885TEST(SetNumericEffecterValue, testGoodEncodeResponse)
886{
887 std::array<uint8_t, sizeof(pldm_msg_hdr) +
888 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
889 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930890 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930891 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
892 uint8_t completionCode = 0;
893
894 auto rc = encode_set_numeric_effecter_value_resp(
895 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
896
897 EXPECT_EQ(rc, PLDM_SUCCESS);
898 EXPECT_EQ(completionCode, response->payload[0]);
899}
900
901TEST(SetNumericEffecterValue, testBadEncodeResponse)
902{
903 auto rc = encode_set_numeric_effecter_value_resp(
904 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
905 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
906}
907
908TEST(GetStateSensorReadings, testGoodEncodeResponse)
909{
910 std::array<uint8_t, hdrSize +
911 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
912 sizeof(get_sensor_state_field) * 2>
913 responseMsg{};
914
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930915 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930916 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
917 uint8_t completionCode = 0;
918 uint8_t comp_sensorCnt = 0x2;
919
920 std::array<get_sensor_state_field, 2> stateField{};
921 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_NORMAL,
922 PLDM_SENSOR_WARNING, PLDM_SENSOR_UNKNOWN};
923 stateField[1] = {PLDM_SENSOR_FAILED, PLDM_SENSOR_UPPERFATAL,
924 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_FATAL};
925
926 auto rc = encode_get_state_sensor_readings_resp(
927 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response);
928
929 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930930 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930931 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
932 response->payload);
933
934 EXPECT_EQ(rc, PLDM_SUCCESS);
935 EXPECT_EQ(completionCode, resp->completion_code);
936 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count);
937 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state);
938 EXPECT_EQ(stateField[0].present_state, resp->field->present_state);
939 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state);
940 EXPECT_EQ(stateField[0].event_state, resp->field->event_state);
941 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state);
942 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state);
943 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state);
944 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state);
945}
946
947TEST(GetStateSensorReadings, testBadEncodeResponse)
948{
949 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr,
950 nullptr);
951
952 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
953}
954
955TEST(GetStateSensorReadings, testGoodDecodeResponse)
956{
957 std::array<uint8_t, hdrSize +
958 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
959 sizeof(get_sensor_state_field) * 2>
960 responseMsg{};
961
962 uint8_t completionCode = 0;
963 uint8_t comp_sensorCnt = 2;
964
965 std::array<get_sensor_state_field, 2> stateField{};
966 stateField[0] = {PLDM_SENSOR_DISABLED, PLDM_SENSOR_UNKNOWN,
967 PLDM_SENSOR_UNKNOWN, PLDM_SENSOR_UNKNOWN};
968 stateField[1] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_LOWERFATAL,
969 PLDM_SENSOR_LOWERCRITICAL, PLDM_SENSOR_WARNING};
970
971 uint8_t retcompletion_code = 0;
972 uint8_t retcomp_sensorCnt = 0;
973 std::array<get_sensor_state_field, 2> retstateField{};
974
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930975 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930976 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
977 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930978 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930979 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
980 response->payload);
981
982 resp->completion_code = completionCode;
983 resp->comp_sensor_count = comp_sensorCnt;
984 memcpy(resp->field, &stateField,
985 (sizeof(get_sensor_state_field) * comp_sensorCnt));
986
987 auto rc = decode_get_state_sensor_readings_resp(
988 response, responseMsg.size() - hdrSize, &retcompletion_code,
989 &retcomp_sensorCnt, retstateField.data());
990
991 EXPECT_EQ(rc, PLDM_SUCCESS);
992 EXPECT_EQ(completionCode, retcompletion_code);
993 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt);
994 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state);
995 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state);
996 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state);
997 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state);
998 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state);
999 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state);
1000 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state);
1001 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state);
1002}
1003
1004TEST(GetStateSensorReadings, testBadDecodeResponse)
1005{
1006 std::array<uint8_t, hdrSize +
1007 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
1008 sizeof(get_sensor_state_field) * 2>
1009 responseMsg{};
1010
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301011 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301012 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1013
1014 auto rc = decode_get_state_sensor_readings_resp(
1015 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr);
1016
1017 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1018
1019 uint8_t completionCode = 0;
1020 uint8_t comp_sensorCnt = 1;
1021
1022 std::array<get_sensor_state_field, 1> stateField{};
1023 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_UPPERFATAL,
1024 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_WARNING};
1025
1026 uint8_t retcompletion_code = 0;
1027 uint8_t retcomp_sensorCnt = 0;
1028 std::array<get_sensor_state_field, 1> retstateField{};
1029
1030 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301031 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301032 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
1033 response->payload);
1034
1035 resp->completion_code = completionCode;
1036 resp->comp_sensor_count = comp_sensorCnt;
1037 memcpy(resp->field, &stateField,
1038 (sizeof(get_sensor_state_field) * comp_sensorCnt));
1039
1040 rc = decode_get_state_sensor_readings_resp(
Andrew Jeffery6ad4dc02023-04-12 15:56:45 +09301041 response, responseMsg.size() - hdrSize, &retcompletion_code,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301042 &retcomp_sensorCnt, retstateField.data());
1043
1044 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1045}
1046
1047TEST(GetStateSensorReadings, testGoodEncodeRequest)
1048{
1049 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1050 requestMsg{};
1051
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001052 uint16_t sensorId = 0xab;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301053 bitfield8_t sensorRearm;
1054 sensorRearm.byte = 0x03;
1055
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301056 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301057 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1058 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0,
1059 request);
1060
1061 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301062 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301063 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1064 request->payload);
1065
1066 EXPECT_EQ(rc, PLDM_SUCCESS);
1067 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
1068 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte);
1069}
1070
1071TEST(GetStateSensorReadings, testBadEncodeRequest)
1072{
1073 bitfield8_t sensorRearm;
1074 sensorRearm.byte = 0x0;
1075
1076 auto rc =
1077 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr);
1078
1079 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1080}
1081
1082TEST(GetStateSensorReadings, testGoodDecodeRequest)
1083{
1084 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1085 requestMsg{};
1086
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001087 uint16_t sensorId = 0xcd;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301088 bitfield8_t sensorRearm;
1089 sensorRearm.byte = 0x10;
1090
1091 uint16_t retsensorId;
1092 bitfield8_t retsensorRearm;
1093 uint8_t retreserved;
1094
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301095 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301096 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1097
1098 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301099 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301100 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1101 request->payload);
1102
1103 req->sensor_id = htole16(sensorId);
1104 req->sensor_rearm.byte = sensorRearm.byte;
1105
1106 auto rc = decode_get_state_sensor_readings_req(
1107 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm,
1108 &retreserved);
1109
1110 EXPECT_EQ(rc, PLDM_SUCCESS);
1111 EXPECT_EQ(sensorId, retsensorId);
1112 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte);
1113 EXPECT_EQ(0, retreserved);
1114}
1115
1116TEST(GetStateSensorReadings, testBadDecodeRequest)
1117{
1118 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1119 requestMsg{};
1120
1121 auto rc = decode_get_state_sensor_readings_req(
1122 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr);
1123
1124 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1125 uint16_t sensorId = 0x11;
1126 bitfield8_t sensorRearm;
1127 sensorRearm.byte = 0x04;
1128
1129 uint16_t retsensorId;
1130 bitfield8_t retsensorRearm;
1131 uint8_t retreserved;
1132
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301133 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301134 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1135
1136 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301138 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1139 request->payload);
1140
1141 req->sensor_id = htole16(sensorId);
1142 req->sensor_rearm.byte = sensorRearm.byte;
1143
1144 rc = decode_get_state_sensor_readings_req(
1145 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm,
1146 &retreserved);
1147
1148 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1149}
1150
Dung Caod6ae8982022-11-02 10:00:10 +07001151TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeRequest)
1152{
1153 uint8_t eventBufferSize = 32;
1154
1155 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES>
1156 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301157 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001158 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1159
1160 auto rc = encode_event_message_buffer_size_req(0, eventBufferSize, request);
1161
1162 EXPECT_EQ(rc, PLDM_SUCCESS);
1163}
1164
1165TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeResponse)
1166{
1167 uint8_t completionCode = PLDM_SUCCESS;
1168 uint16_t terminusMaxBufferSize = 256;
1169
1170 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
1171 responseMsg{};
1172
1173 uint8_t retCompletionCode;
1174 uint16_t retMaxBufferSize = 0;
1175
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301176 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1178 struct pldm_event_message_buffer_size_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301179 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001180 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
1181 response->payload);
1182
1183 resp->completion_code = completionCode;
1184 resp->terminus_max_buffer_size = terminusMaxBufferSize;
1185
1186 auto rc = decode_event_message_buffer_size_resp(
1187 response, responseMsg.size() - hdrSize, &retCompletionCode,
1188 &retMaxBufferSize);
1189
1190 EXPECT_EQ(rc, PLDM_SUCCESS);
1191 EXPECT_EQ(retCompletionCode, completionCode);
1192 EXPECT_EQ(terminusMaxBufferSize, retMaxBufferSize);
1193}
1194
1195TEST(EventMessageBufferSize, testBadEventMessageBufferSizeResponse)
1196{
1197 uint8_t completionCode = PLDM_SUCCESS;
1198 uint16_t terminusMaxBufferSize = 256;
1199
1200 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
1201 responseMsg{};
1202
1203 uint8_t retCompletionCode;
1204 uint16_t retMaxBufferSize = 0;
1205
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301206 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001207 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1208 struct pldm_event_message_buffer_size_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001210 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
1211 response->payload);
1212 resp->completion_code = completionCode;
1213 resp->terminus_max_buffer_size = terminusMaxBufferSize;
1214
1215 auto rc =
1216 decode_event_message_buffer_size_resp(response, 0, nullptr, nullptr);
1217 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1218
1219 rc = decode_event_message_buffer_size_resp(
1220 response, responseMsg.size(), &retCompletionCode, &retMaxBufferSize);
1221 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1222}
1223
Dung Cao1bf8c872022-11-29 05:32:58 +07001224TEST(PlatformEventMessageSupported, testGoodEncodeRequest)
1225{
1226 uint8_t formatVersion = 0x01;
1227
1228 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES>
1229 requestMsg{};
1230
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001232 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1233
1234 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1235
1236 struct pldm_event_message_supported_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301237 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001238 reinterpret_cast<struct pldm_event_message_supported_req*>(
1239 request->payload);
1240
1241 EXPECT_EQ(rc, PLDM_SUCCESS);
1242 EXPECT_EQ(formatVersion, req->format_version);
1243}
1244
1245TEST(PlatformEventMessageSupported, testBadEncodeRequest)
1246{
1247 uint8_t eventData = 34;
1248 uint8_t formatVersion = 0x0;
1249
1250 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES +
1251 sizeof(eventData)>
1252 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301253 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001254 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1255
1256 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1257 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1258
1259 rc = encode_event_message_supported_req(0, formatVersion, nullptr);
1260 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1261}
1262
1263TEST(PlatformEventMessageSupported, testGoodDecodeRespond)
1264{
1265 uint8_t completionCode = PLDM_SUCCESS;
1266 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1267 bitfield8_t synchConfigSupported;
1268 synchConfigSupported.byte = 0xe;
1269 uint8_t numberEventClassReturned = 0x3;
1270 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1271 constexpr uint8_t eventClassCount = 3;
1272
1273 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1274 eventClassCount>
1275 responseMsg{};
1276
1277 uint8_t retCompletionCode;
1278 uint8_t retSynchConfig = 0;
1279 uint8_t retNumberEventClass = 0;
1280 bitfield8_t retSynchConfigSupport;
1281 uint8_t retEventClass[eventClassCount] = {0};
1282
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301283 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001284 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1285 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301286 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001287 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1288 response->payload);
1289
1290 resp->completion_code = completionCode;
1291 resp->synchrony_configuration = synchConfiguration;
1292 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1293 resp->number_event_class_returned = numberEventClassReturned;
1294 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1295
1296 auto rc = decode_event_message_supported_resp(
1297 response, responseMsg.size() - hdrSize, &retCompletionCode,
1298 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1299 retEventClass, eventClassCount);
1300
1301 EXPECT_EQ(rc, PLDM_SUCCESS);
1302 EXPECT_EQ(retCompletionCode, completionCode);
1303 EXPECT_EQ(retSynchConfig, synchConfiguration);
1304 EXPECT_EQ(retNumberEventClass, numberEventClassReturned);
1305 EXPECT_EQ(retSynchConfigSupport.byte, synchConfigSupported.byte);
1306 EXPECT_EQ(0, memcmp(eventClass.data(), resp->event_class,
1307 numberEventClassReturned));
1308}
1309
1310TEST(PlatformEventMessageSupported, testBadSynchConfiguration)
1311{
1312 uint8_t completionCode = PLDM_SUCCESS;
1313 uint8_t synchConfiguration = 0x4;
1314 bitfield8_t synchConfigSupported;
1315 synchConfigSupported.byte = 0xe;
1316 uint8_t numberEventClassReturned = 0x3;
1317 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1318 constexpr uint8_t eventClassCount = 3;
1319
1320 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1321 eventClassCount>
1322 responseMsg{};
1323
1324 uint8_t retCompletionCode;
1325 uint8_t retSynchConfig = 0;
1326 uint8_t retNumberEventClass = 0;
1327 bitfield8_t retSynchConfigSupport;
1328 uint8_t retEventClass[eventClassCount] = {0};
1329
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301330 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001331 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1332 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301333 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001334 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1335 response->payload);
1336
1337 resp->completion_code = completionCode;
1338 resp->synchrony_configuration = synchConfiguration;
1339 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1340 resp->number_event_class_returned = numberEventClassReturned;
1341 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1342
1343 auto rc = decode_event_message_supported_resp(
1344 response, responseMsg.size() - hdrSize, &retCompletionCode,
1345 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1346 retEventClass, eventClassCount);
1347
1348 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1349}
1350
1351TEST(PlatformEventMessageSupported, testBadDecodeRespond)
1352{
1353 uint8_t completionCode = PLDM_SUCCESS;
1354 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1355 bitfield8_t synchConfigSupported;
1356 synchConfigSupported.byte = 0xe;
1357 uint8_t numberEventClassReturned = 0x3;
1358 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1359 constexpr uint8_t eventClassCount = 3;
1360
1361 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1362 eventClassCount>
1363 responseMsg{};
1364
1365 uint8_t retCompletionCode;
1366 uint8_t retSynchConfig = 0;
1367 uint8_t retNumberEventClass = 0;
1368 bitfield8_t retSynchConfigSupport;
1369 uint8_t retEventClass[eventClassCount] = {0};
1370
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301371 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001372 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1373 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301374 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001375 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1376 response->payload);
1377 resp->completion_code = completionCode;
1378 resp->synchrony_configuration = synchConfiguration;
1379 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1380 resp->number_event_class_returned = numberEventClassReturned;
1381 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1382
1383 auto rc = decode_event_message_supported_resp(response, 0, nullptr, nullptr,
1384 nullptr, nullptr, nullptr, 0);
1385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1386
1387 rc = decode_event_message_supported_resp(
1388 response, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES - 1,
1389 &retCompletionCode, &retSynchConfig, &retSynchConfigSupport,
1390 &retNumberEventClass, retEventClass, eventClassCount);
1391 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1392
1393 rc = decode_event_message_supported_resp(
1394 response, responseMsg.size() - hdrSize, &retCompletionCode,
1395 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1396 retEventClass, 1);
1397 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1398}
1399
Thu Nguyen387b10f2024-09-24 11:33:16 +00001400TEST(PollForPlatformEventMessage, testGoodEncodeRequestFirstPart)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001401{
1402 uint8_t formatVersion = 0x01;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001403 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART;
1404 uint32_t dataTransferHandle = 0xaabbccdd;
1405 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL;
Thu Nguyen159a98b2022-11-02 10:00:10 +07001406
Thu Nguyen387b10f2024-09-24 11:33:16 +00001407 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001408
1409 auto rc = encode_poll_for_platform_event_message_req(
1410 0, formatVersion, transferOperationFlag, dataTransferHandle,
1411 eventIdToAcknowledge, request,
1412 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1413 EXPECT_EQ(rc, PLDM_SUCCESS);
1414
1415 struct pldm_msgbuf _buf;
1416 struct pldm_msgbuf* buf = &_buf;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001417 rc = pldm_msgbuf_init_errno(
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301418 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
1419 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
Thu Nguyen387b10f2024-09-24 11:33:16 +00001420 EXPECT_EQ(rc, 0);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001421
1422 uint8_t retFormatVersion;
1423 uint8_t retTransferOperationFlag;
1424 uint32_t retDataTransferHandle;
1425 uint16_t retEventIdToAcknowledge;
1426
Andrew Jefferye5f12532024-10-01 12:18:49 +09301427 pldm_msgbuf_extract_uint8(buf, retFormatVersion);
1428 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag);
1429 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle);
1430 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge);
Thu Nguyen387b10f2024-09-24 11:33:16 +00001431 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001432
1433 EXPECT_EQ(retFormatVersion, formatVersion);
1434 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1435 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1436 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
Thu Nguyen387b10f2024-09-24 11:33:16 +00001437}
1438
1439TEST(PollForPlatformEventMessage, testGoodEncodeRequestNextPart)
1440{
1441 uint8_t formatVersion = 0x01;
1442 uint8_t transferOperationFlag = PLDM_GET_NEXTPART;
1443 uint32_t dataTransferHandle = 0xaabbccdd;
1444 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT;
1445
1446 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1447
1448 auto rc = encode_poll_for_platform_event_message_req(
1449 0, formatVersion, transferOperationFlag, dataTransferHandle,
1450 eventIdToAcknowledge, request,
1451 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1452 EXPECT_EQ(rc, PLDM_SUCCESS);
1453
1454 struct pldm_msgbuf _buf;
1455 struct pldm_msgbuf* buf = &_buf;
1456 rc = pldm_msgbuf_init_errno(
1457 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
1458 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1459 EXPECT_EQ(rc, 0);
1460
1461 uint8_t retFormatVersion;
1462 uint8_t retTransferOperationFlag;
1463 uint32_t retDataTransferHandle;
1464 uint16_t retEventIdToAcknowledge;
1465
Andrew Jefferye5f12532024-10-01 12:18:49 +09301466 pldm_msgbuf_extract_uint8(buf, retFormatVersion);
1467 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag);
1468 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle);
1469 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge);
Thu Nguyen387b10f2024-09-24 11:33:16 +00001470 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
1471
1472 EXPECT_EQ(retFormatVersion, formatVersion);
1473 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1474 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1475 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1476}
1477
1478TEST(PollForPlatformEventMessage, testGoodEncodeRequestAckOnly)
1479{
1480 uint8_t formatVersion = 0x01;
1481 uint8_t transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY;
1482 uint32_t dataTransferHandle = 0xaabbccdd;
Thu Nguyen9e16b182024-10-01 03:12:16 +00001483 uint16_t eventIdToAcknowledge = 0x1234;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001484
1485 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1486
1487 auto rc = encode_poll_for_platform_event_message_req(
1488 0, formatVersion, transferOperationFlag, dataTransferHandle,
1489 eventIdToAcknowledge, request,
1490 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1491 EXPECT_EQ(rc, PLDM_SUCCESS);
1492
1493 struct pldm_msgbuf _buf;
1494 struct pldm_msgbuf* buf = &_buf;
1495 rc = pldm_msgbuf_init_errno(
1496 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
1497 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1498 EXPECT_EQ(rc, 0);
1499
1500 uint8_t retFormatVersion;
1501 uint8_t retTransferOperationFlag;
1502 uint32_t retDataTransferHandle;
1503 uint16_t retEventIdToAcknowledge;
1504
Andrew Jefferye5f12532024-10-01 12:18:49 +09301505 pldm_msgbuf_extract_uint8(buf, retFormatVersion);
1506 pldm_msgbuf_extract_uint8(buf, retTransferOperationFlag);
1507 pldm_msgbuf_extract_uint32(buf, retDataTransferHandle);
1508 pldm_msgbuf_extract_uint16(buf, retEventIdToAcknowledge);
Thu Nguyen387b10f2024-09-24 11:33:16 +00001509 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
1510
1511 EXPECT_EQ(retFormatVersion, formatVersion);
1512 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1513 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1514 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001515}
1516
1517TEST(PollForPlatformEventMessage, testBadEncodeRequest)
1518{
1519 uint8_t formatVersion = 0x01;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001520 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART;
1521 uint32_t dataTransferHandle = 0xaabbccdd;
1522 uint16_t eventIdToAcknowledge = 0x1234;
Thu Nguyen159a98b2022-11-02 10:00:10 +07001523
Thu Nguyen387b10f2024-09-24 11:33:16 +00001524 PLDM_MSG_DEFINE_P(request, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001525
1526 auto rc = encode_poll_for_platform_event_message_req(
1527 0, formatVersion, transferOperationFlag, dataTransferHandle,
1528 eventIdToAcknowledge, nullptr,
1529 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001530 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1531
Thu Nguyen387b10f2024-09-24 11:33:16 +00001532 transferOperationFlag = PLDM_GET_FIRSTPART;
1533 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT;
1534 rc = encode_poll_for_platform_event_message_req(
Thu Nguyen159a98b2022-11-02 10:00:10 +07001535 0, formatVersion, transferOperationFlag, dataTransferHandle,
Thu Nguyen387b10f2024-09-24 11:33:16 +00001536 eventIdToAcknowledge, request,
1537 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1538 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1539
1540 transferOperationFlag = PLDM_GET_NEXTPART;
1541 eventIdToAcknowledge = 0x1234;
1542 rc = encode_poll_for_platform_event_message_req(
1543 0, formatVersion, transferOperationFlag, dataTransferHandle,
1544 eventIdToAcknowledge, request,
1545 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1546 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1547
1548 transferOperationFlag = PLDM_GET_NEXTPART;
1549 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL;
1550 rc = encode_poll_for_platform_event_message_req(
1551 0, formatVersion, transferOperationFlag, dataTransferHandle,
1552 eventIdToAcknowledge, request,
1553 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1554 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1555
1556 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY;
Thu Nguyen9e16b182024-10-01 03:12:16 +00001557 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001558 rc = encode_poll_for_platform_event_message_req(
1559 0, formatVersion, transferOperationFlag, dataTransferHandle,
1560 eventIdToAcknowledge, request,
1561 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1562 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1563
1564 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY;
Thu Nguyen9e16b182024-10-01 03:12:16 +00001565 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001566 rc = encode_poll_for_platform_event_message_req(
1567 0, formatVersion, transferOperationFlag, dataTransferHandle,
1568 eventIdToAcknowledge, request,
1569 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1570 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1571
1572 transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY + 1;
1573 eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT;
1574 rc = encode_poll_for_platform_event_message_req(
1575 0, formatVersion, transferOperationFlag, dataTransferHandle,
1576 eventIdToAcknowledge, request,
1577 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1578 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001579}
1580
1581TEST(PollForPlatformEventMessage, testGoodDecodeRespond)
1582{
1583 uint8_t completionCode = PLDM_SUCCESS;
1584 uint8_t tId = 0x9;
1585 uint16_t eventId = 159;
1586 uint32_t nextDataTransferHandle = 0x11223344;
1587 uint8_t transferFlag = PLDM_START_AND_END;
1588 uint8_t eventClass = 0x5;
1589 uint8_t eventData[5] = {0x55, 0x44, 0x33, 0x22, 0x11};
1590 constexpr uint32_t eventDataSize = 0x00000005;
1591 uint32_t eventDataIntegrityChecksum = 0x66778899;
1592
1593 std::vector<uint8_t> responseMsg{
1594 0x1,
1595 0x0,
1596 0x0,
1597 PLDM_SUCCESS,
1598 0x9, // tid
1599 159,
1600 0x0, // event id
1601 0x44,
1602 0x33,
1603 0x22,
1604 0x11, // next_data_transfer_handle
1605 PLDM_START_AND_END, // transfer_flag
1606 0x05, // event class
1607 0x05,
1608 0x00,
1609 0x00,
1610 0x00, // event_data_size
1611 0x55,
1612 0x44,
1613 0x33,
1614 0x22,
1615 0x11, // event_data[5]
1616 0x99,
1617 0x88,
1618 0x77,
1619 0x66 // event_data_integrity_checksum
1620 };
1621 const uint32_t respMsgLen = 23;
1622
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301623 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001624 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1625
1626 uint8_t retCompletionCode;
1627 uint8_t retTid = 0;
1628 uint16_t retEventId = 0;
1629 uint32_t retNextDataTransferHandle = 0;
1630 uint8_t retTransferFlag = 0;
1631 uint8_t retEventClass = 0;
1632 uint32_t retEventDataSize = 0;
1633 uint8_t* retEventData = nullptr;
1634 uint32_t retEventDataIntegrityChecksum = 0;
1635
1636 auto rc = decode_poll_for_platform_event_message_resp(
1637 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1638 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1639 &retEventDataSize, (void**)&retEventData,
1640 &retEventDataIntegrityChecksum);
1641
1642 EXPECT_EQ(rc, PLDM_SUCCESS);
1643 EXPECT_EQ(retCompletionCode, completionCode);
1644 EXPECT_EQ(retTid, tId);
1645 EXPECT_EQ(retEventId, eventId);
1646 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
1647 EXPECT_EQ(retTransferFlag, transferFlag);
1648 EXPECT_EQ(retEventClass, eventClass);
1649 EXPECT_EQ(retEventDataSize, eventDataSize);
1650 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
1651 EXPECT_EQ(0, memcmp(eventData, retEventData, eventDataSize));
1652}
1653
1654TEST(PollForPlatformEventMessage, testGoodDecodeAckOnlyRespond)
1655{
1656 uint8_t completionCode = PLDM_SUCCESS;
1657 uint8_t tId = 0x9;
1658 uint16_t eventId = 0xffff;
1659
1660 std::vector<uint8_t> responseMsg{
1661 0x1, 0x0, 0x0, PLDM_SUCCESS,
1662 0x9, // tid
1663 0xff,
1664 0xff // event id
1665 };
1666 const uint32_t respMsgLen = 4;
1667
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301668 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001669 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1670
1671 uint8_t retCompletionCode;
1672 uint8_t retTid = 0;
1673 uint16_t retEventId = 0;
1674 uint32_t retNextDataTransferHandle = 0;
1675 uint8_t retTransferFlag = 0;
1676 uint8_t retEventClass = 0;
1677 uint32_t retEventDataSize = 0;
1678 uint8_t* retEventData = nullptr;
1679 uint32_t retEventDataIntegrityChecksum = 0;
1680
1681 auto rc = decode_poll_for_platform_event_message_resp(
1682 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1683 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1684 &retEventDataSize, (void**)&retEventData,
1685 &retEventDataIntegrityChecksum);
1686
1687 EXPECT_EQ(rc, PLDM_SUCCESS);
1688 EXPECT_EQ(retCompletionCode, completionCode);
1689 EXPECT_EQ(retTid, tId);
1690 EXPECT_EQ(retEventId, eventId);
1691
1692 eventId = 0x0000;
1693 responseMsg[5] = 0x00;
1694 responseMsg[6] = 0x00;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301695 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001696 response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1697
1698 rc = decode_poll_for_platform_event_message_resp(
1699 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1700 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1701 &retEventDataSize, (void**)&retEventData,
1702 &retEventDataIntegrityChecksum);
1703
1704 EXPECT_EQ(rc, PLDM_SUCCESS);
1705 EXPECT_EQ(retCompletionCode, completionCode);
1706 EXPECT_EQ(retTid, tId);
1707 EXPECT_EQ(retEventId, eventId);
1708}
1709
1710TEST(PollForPlatformEventMessage, testBadDecodeRespond)
1711{
1712 std::vector<uint8_t> responseMsg{
1713 0x1,
1714 0x0,
1715 0x0,
1716 PLDM_SUCCESS,
1717 0x9, // tid
1718 159,
1719 0x0, // event id
1720 0x44,
1721 0x33,
1722 0x22,
1723 0x11, // next_data_transfer_handle
1724 PLDM_START_AND_END, // transfer_flag
1725 0x05, // event class
1726 0x05,
1727 0x00,
1728 0x00,
1729 0x00, // event_data_size
1730 0x55,
1731 0x44,
1732 0x33,
1733 0x22,
1734 0x11, // event_data[5]
1735 0x99,
1736 0x88,
1737 0x77,
1738 0x66 // event_data_integrity_checksum
1739 };
1740 // const uint32_t respMsgLen = 23;
1741
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301742 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001743 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1744
1745 auto rc = decode_poll_for_platform_event_message_resp(
1746 nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1747 nullptr, nullptr, nullptr);
1748
1749 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1750
1751 uint8_t retCompletionCode;
1752 uint8_t retTid = 0;
1753 uint16_t retEventId = 0;
1754 uint32_t retNextDataTransferHandle = 0;
1755 uint8_t retTransferFlag = 0;
1756 uint8_t retEventClass = 0;
1757 uint32_t retEventDataSize = 0;
1758 uint8_t* retEventData = nullptr;
1759 uint32_t retEventDataIntegrityChecksum = 0;
1760
1761 rc = decode_poll_for_platform_event_message_resp(
1762 response, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES - 1,
1763 &retCompletionCode, &retTid, &retEventId, &retNextDataTransferHandle,
1764 &retTransferFlag, &retEventClass, &retEventDataSize,
1765 (void**)&retEventData, &retEventDataIntegrityChecksum);
1766
1767 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1768}
1769
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001770TEST(PollForPlatformEventMessage, testGoodDecodeRequestFirstPart)
1771{
1772 uint8_t formatVersion = 0x1;
1773 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART;
1774 uint32_t dataTransferHandle = 0x11223344;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001775 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_NULL;
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001776 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART,
1777 0x44, 0x33, 0x22, 0x11, 0x00,
1778 0x00};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301779 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001780 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1781
1782 uint8_t retFormatVersion;
1783 uint8_t retTransferOperationFlag;
1784 uint32_t retDataTransferHandle;
1785 uint16_t retEventIdToAcknowledge;
1786
1787 auto rc = decode_poll_for_platform_event_message_req(
1788 request, requestMsg.size() - hdrSize, &retFormatVersion,
1789 &retTransferOperationFlag, &retDataTransferHandle,
1790 &retEventIdToAcknowledge);
1791
1792 EXPECT_EQ(rc, PLDM_SUCCESS);
1793 EXPECT_EQ(retFormatVersion, formatVersion);
1794 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1795 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1796 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1797}
1798
1799TEST(PollForPlatformEventMessage, testGoodDecodeRequestNextPart)
1800{
1801 uint8_t formatVersion = 0x1;
1802 uint8_t transferOperationFlag = PLDM_GET_NEXTPART;
1803 uint32_t dataTransferHandle = 0x11223344;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001804 uint16_t eventIdToAcknowledge = PLDM_PLATFORM_EVENT_ID_FRAGMENT;
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001805 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_NEXTPART,
1806 0x44, 0x33, 0x22, 0x11, 0xff,
1807 0xff};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301808 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001809 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1810
1811 uint8_t retFormatVersion;
1812 uint8_t retTransferOperationFlag;
1813 uint32_t retDataTransferHandle;
1814 uint16_t retEventIdToAcknowledge;
1815
1816 auto rc = decode_poll_for_platform_event_message_req(
1817 request, requestMsg.size() - hdrSize, &retFormatVersion,
1818 &retTransferOperationFlag, &retDataTransferHandle,
1819 &retEventIdToAcknowledge);
1820
1821 EXPECT_EQ(rc, PLDM_SUCCESS);
1822 EXPECT_EQ(retFormatVersion, formatVersion);
1823 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1824 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1825 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1826}
1827
1828TEST(PollForPlatformEventMessage, testGoodDecodeRequestAck)
1829{
1830 uint8_t formatVersion = 0x1;
1831 uint8_t transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY;
1832 uint32_t dataTransferHandle = 0x11223344;
Thu Nguyen9e16b182024-10-01 03:12:16 +00001833 uint16_t eventIdToAcknowledge = 0x1234;
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001834 std::vector<uint8_t> requestMsg{
1835 0x1, 0x0, 0x0, 0x1, PLDM_ACKNOWLEDGEMENT_ONLY, 0x44, 0x33,
Thu Nguyen9e16b182024-10-01 03:12:16 +00001836 0x22, 0x11, 0x34, 0x12};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301837 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001838 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1839
1840 uint8_t retFormatVersion;
1841 uint8_t retTransferOperationFlag;
1842 uint32_t retDataTransferHandle;
1843 uint16_t retEventIdToAcknowledge;
1844
1845 auto rc = decode_poll_for_platform_event_message_req(
1846 request, requestMsg.size() - hdrSize, &retFormatVersion,
1847 &retTransferOperationFlag, &retDataTransferHandle,
1848 &retEventIdToAcknowledge);
1849
1850 EXPECT_EQ(rc, PLDM_SUCCESS);
1851 EXPECT_EQ(retFormatVersion, formatVersion);
1852 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1853 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1854 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1855}
1856
1857TEST(PollForPlatformEventMessage, testBadDecodeRequest)
1858{
Thu Nguyen387b10f2024-09-24 11:33:16 +00001859 /*
1860 * transfer_operation_flag is PLDM_GET_FIRSTPART and
1861 * event_id_to_acknowledge is not PLDM_PLATFORM_EVENT_ID_NULL
1862 */
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001863 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART,
1864 0x44, 0x33, 0x22, 0x11, 0x66,
1865 0x55};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301866 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001867 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1868
1869 uint8_t retFormatVersion;
1870 uint8_t retTransferOperationFlag;
1871 uint32_t retDataTransferHandle;
1872 uint16_t retEventIdToAcknowledge;
1873
1874 auto rc = decode_poll_for_platform_event_message_req(
1875 NULL, requestMsg.size() - hdrSize, &retFormatVersion,
1876 &retTransferOperationFlag, &retDataTransferHandle,
1877 &retEventIdToAcknowledge);
1878 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1879
1880 /*
1881 * transfer_operation_flag is not PLDM_GET_FIRSTPART or PLDM_GET_NEXTPART or
1882 * PLDM_ACKNOWLEDGEMENT_ONLY
1883 */
1884
1885 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY + 1;
1886
1887 rc = decode_poll_for_platform_event_message_req(
1888 request, requestMsg.size() - hdrSize, &retFormatVersion,
1889 &retTransferOperationFlag, &retDataTransferHandle,
1890 &retEventIdToAcknowledge);
1891
1892 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001893
1894 /*
1895 * transfer_operation_flag is PLDM_GET_NEXTPART and
Thu Nguyen387b10f2024-09-24 11:33:16 +00001896 * event_id_to_acknowledge is not PLDM_PLATFORM_EVENT_ID_FRAGMENT
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001897 */
1898 requestMsg[4] = PLDM_GET_NEXTPART;
Thu Nguyen387b10f2024-09-24 11:33:16 +00001899 requestMsg[9] = 0x11;
1900 requestMsg[10] = 0x22;
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001901
1902 rc = decode_poll_for_platform_event_message_req(
1903 request, requestMsg.size() - hdrSize, &retFormatVersion,
1904 &retTransferOperationFlag, &retDataTransferHandle,
1905 &retEventIdToAcknowledge);
1906
1907 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1908
1909 /*
Thu Nguyen387b10f2024-09-24 11:33:16 +00001910 * transfer_operation_flag is PLDM_ACKNOWLEDGEMENT_ONLY and
Thu Nguyen9e16b182024-10-01 03:12:16 +00001911 * event_id_to_acknowledge is PLDM_PLATFORM_EVENT_ID_FRAGMENT
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001912 */
Thu Nguyen387b10f2024-09-24 11:33:16 +00001913 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY;
Thu Nguyen9e16b182024-10-01 03:12:16 +00001914 requestMsg[9] = 0xff;
1915 requestMsg[10] = 0xff;
1916
1917 rc = decode_poll_for_platform_event_message_req(
1918 request, requestMsg.size() - hdrSize, &retFormatVersion,
1919 &retTransferOperationFlag, &retDataTransferHandle,
1920 &retEventIdToAcknowledge);
1921
1922 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1923
1924 /*
1925 * transfer_operation_flag is PLDM_ACKNOWLEDGEMENT_ONLY and
1926 * event_id_to_acknowledge is PLDM_PLATFORM_EVENT_ID_NULL
1927 */
1928 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY;
1929 requestMsg[9] = 0x00;
1930 requestMsg[10] = 0x00;
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001931
1932 rc = decode_poll_for_platform_event_message_req(
1933 request, requestMsg.size() - hdrSize, &retFormatVersion,
1934 &retTransferOperationFlag, &retDataTransferHandle,
1935 &retEventIdToAcknowledge);
1936
1937 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1938}
1939
1940TEST(PollForPlatformEventMessage, testGoodEncodeResposeP1)
1941{
1942 uint8_t completionCode = PLDM_SUCCESS;
1943 uint8_t instance_id = 0;
1944 uint8_t tId = 0x9;
1945 uint16_t eventId = 0x1;
1946 uint32_t nextDataTransferHandle = 0xffff;
1947 uint8_t transferFlag = PLDM_END;
1948 uint8_t eventClass = 0x5;
1949 constexpr uint32_t eventDataSize = 9;
1950 uint8_t pEventData[eventDataSize] = {0x31, 0x32, 0x33, 0x34, 0x35,
1951 0x36, 0x37, 0x38, 0x39};
1952 uint32_t eventDataIntegrityChecksum = 0x11223344;
1953 constexpr size_t payloadLength =
1954 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
1955
1956 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301957 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001958 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1959
1960 auto rc = encode_poll_for_platform_event_message_resp(
1961 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
1962 transferFlag, eventClass, eventDataSize, pEventData,
1963 eventDataIntegrityChecksum, response, payloadLength);
1964 EXPECT_EQ(rc, PLDM_SUCCESS);
1965
1966 struct pldm_msgbuf _buf;
1967 struct pldm_msgbuf* buf = &_buf;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301968 rc = pldm_msgbuf_init_errno(
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301969 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1970 response->payload, payloadLength);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09301971 EXPECT_EQ(rc, 0);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001972
1973 uint8_t retCompletionCode;
1974 uint8_t retTid = 0;
1975 uint16_t retEventId = 0;
1976 uint32_t retNextDataTransferHandle = 0;
1977 uint8_t retTransferFlag = 0;
1978 uint8_t retEventClass = 0;
1979 uint32_t retEventDataSize = 0;
1980 uint8_t retEventData[payloadLength] = {0};
1981 uint32_t retEventDataIntegrityChecksum = 0;
1982
Andrew Jefferye5f12532024-10-01 12:18:49 +09301983 pldm_msgbuf_extract_uint8(buf, retCompletionCode);
1984 pldm_msgbuf_extract_uint8(buf, retTid);
1985 pldm_msgbuf_extract_uint16(buf, retEventId);
1986 pldm_msgbuf_extract_uint32(buf, retNextDataTransferHandle);
1987 pldm_msgbuf_extract_uint8(buf, retTransferFlag);
1988 pldm_msgbuf_extract_uint8(buf, retEventClass);
1989 pldm_msgbuf_extract_uint32(buf, retEventDataSize);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001990 rc = pldm_msgbuf_extract_array_uint8(buf, retEventDataSize, retEventData,
1991 sizeof(retEventData));
1992 ASSERT_EQ(rc, 0);
Andrew Jefferye5f12532024-10-01 12:18:49 +09301993 pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001994
1995 EXPECT_EQ(rc, PLDM_SUCCESS);
1996 EXPECT_EQ(retCompletionCode, completionCode);
1997 EXPECT_EQ(retTid, tId);
1998 EXPECT_EQ(retEventId, eventId);
1999 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
2000 EXPECT_EQ(retTransferFlag, transferFlag);
2001 EXPECT_EQ(retEventClass, eventClass);
2002 EXPECT_EQ(retEventDataSize, eventDataSize);
2003 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
2004 EXPECT_EQ(0, memcmp(pEventData, retEventData, eventDataSize));
2005
2006 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
2007}
2008
2009TEST(PollForPlatformEventMessage, testGoodEncodeResposeP2)
2010{
2011 uint8_t completionCode = PLDM_SUCCESS;
2012 uint8_t instance_id = 0;
2013 uint8_t tId = 0x9;
2014 uint16_t eventId = 0x0000;
2015 constexpr size_t payloadLength =
2016 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES;
2017
2018 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302019 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002020 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2021
2022 auto rc = encode_poll_for_platform_event_message_resp(
2023 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0,
2024 response, payloadLength);
2025 EXPECT_EQ(rc, PLDM_SUCCESS);
2026
2027 struct pldm_msgbuf _buf;
2028 struct pldm_msgbuf* buf = &_buf;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302029 rc = pldm_msgbuf_init_errno(
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302030 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2031 response->payload, payloadLength);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302032 EXPECT_EQ(rc, 0);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002033
2034 uint8_t retCompletionCode;
2035 uint8_t retTid = 0;
2036 uint16_t retEventId = 0;
2037
Andrew Jefferye5f12532024-10-01 12:18:49 +09302038 pldm_msgbuf_extract_uint8(buf, retCompletionCode);
2039 pldm_msgbuf_extract_uint8(buf, retTid);
2040 pldm_msgbuf_extract_uint16(buf, retEventId);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002041
2042 EXPECT_EQ(rc, PLDM_SUCCESS);
2043 EXPECT_EQ(retCompletionCode, completionCode);
2044 EXPECT_EQ(retTid, tId);
2045 EXPECT_EQ(retEventId, eventId);
2046 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
2047}
2048
2049TEST(PollForPlatformEventMessage, testGoodEncodeResposeP3)
2050{
2051 uint8_t completionCode = PLDM_SUCCESS;
2052 uint8_t instance_id = 0;
2053 uint8_t tId = 0x9;
2054 uint16_t eventId = 0xffff;
2055 constexpr size_t payloadLength =
2056 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES;
2057
2058 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302059 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002060 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2061
2062 auto rc = encode_poll_for_platform_event_message_resp(
2063 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0,
2064 response, payloadLength);
2065 EXPECT_EQ(rc, PLDM_SUCCESS);
2066
2067 struct pldm_msgbuf _buf;
2068 struct pldm_msgbuf* buf = &_buf;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302069 rc = pldm_msgbuf_init_errno(
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302070 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2071 response->payload, payloadLength);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302072 EXPECT_EQ(rc, 0);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002073
2074 uint8_t retCompletionCode;
2075 uint8_t retTid = 0;
2076 uint16_t retEventId = 0;
2077
Andrew Jefferye5f12532024-10-01 12:18:49 +09302078 pldm_msgbuf_extract_uint8(buf, retCompletionCode);
2079 pldm_msgbuf_extract_uint8(buf, retTid);
2080 pldm_msgbuf_extract_uint16(buf, retEventId);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002081
2082 EXPECT_EQ(rc, PLDM_SUCCESS);
2083 EXPECT_EQ(retCompletionCode, completionCode);
2084 EXPECT_EQ(retTid, tId);
2085 EXPECT_EQ(retEventId, eventId);
2086 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
2087}
2088
2089TEST(PollForPlatformEventMessage, testGoodEncodeResposeP4)
2090{
2091 uint8_t completionCode = PLDM_SUCCESS;
2092 uint8_t instance_id = 0;
2093 uint8_t tId = 0x9;
2094 uint16_t eventId = 0x1;
2095 uint32_t nextDataTransferHandle = 0xffff;
2096 uint8_t transferFlag = PLDM_END;
2097 uint8_t eventClass = 0x5;
2098 constexpr uint32_t eventDataSize = 0;
2099 uint32_t eventDataIntegrityChecksum = 0x11223344;
2100 size_t payloadLength =
2101 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
2102
2103 std::array<uint8_t, hdrSize +
2104 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES +
2105 eventDataSize + 4>
2106 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302107 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002108 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2109
2110 auto rc = encode_poll_for_platform_event_message_resp(
2111 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2112 transferFlag, eventClass, eventDataSize, NULL,
2113 eventDataIntegrityChecksum, response, payloadLength);
2114 EXPECT_EQ(rc, PLDM_SUCCESS);
2115
2116 struct pldm_msgbuf _buf;
2117 struct pldm_msgbuf* buf = &_buf;
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302118 rc = pldm_msgbuf_init_errno(
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302119 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2120 response->payload, payloadLength);
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302121 EXPECT_EQ(rc, 0);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002122
2123 uint8_t retCompletionCode;
2124 uint8_t retTid = 0;
2125 uint16_t retEventId = 0;
2126 uint32_t retNextDataTransferHandle = 0;
2127 uint8_t retTransferFlag = 0;
2128 uint8_t retEventClass = 0;
2129 uint32_t retEventDataSize = 0;
2130 uint32_t retEventDataIntegrityChecksum = 0;
2131
Andrew Jefferye5f12532024-10-01 12:18:49 +09302132 pldm_msgbuf_extract_uint8(buf, retCompletionCode);
2133 pldm_msgbuf_extract_uint8(buf, retTid);
2134 pldm_msgbuf_extract_uint16(buf, retEventId);
2135 pldm_msgbuf_extract_uint32(buf, retNextDataTransferHandle);
2136 pldm_msgbuf_extract_uint8(buf, retTransferFlag);
2137 pldm_msgbuf_extract_uint8(buf, retEventClass);
2138 pldm_msgbuf_extract_uint32(buf, retEventDataSize);
2139 pldm_msgbuf_extract_uint32(buf, retEventDataIntegrityChecksum);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002140
2141 EXPECT_EQ(rc, PLDM_SUCCESS);
2142 EXPECT_EQ(retCompletionCode, completionCode);
2143 EXPECT_EQ(retTid, tId);
2144 EXPECT_EQ(retEventId, eventId);
2145 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
2146 EXPECT_EQ(retTransferFlag, transferFlag);
2147 EXPECT_EQ(retEventClass, eventClass);
2148 EXPECT_EQ(retEventDataSize, eventDataSize);
2149 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
2150
2151 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
2152}
2153
2154TEST(PollForPlatformEventMessage, testBadEncodeResponse)
2155{
2156 uint8_t completionCode = PLDM_SUCCESS;
2157 uint8_t instance_id = 0;
2158 uint8_t tId = 0x9;
2159 uint16_t eventId = 0x1;
2160 uint32_t nextDataTransferHandle = 0xffff;
2161 uint8_t transferFlag = 0x0;
2162 uint8_t eventClass = 0x5;
2163 const uint32_t eventDataSize = 0;
2164 uint32_t eventDataIntegrityChecksum = 0x11223344;
2165 constexpr size_t payloadLength =
2166 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
2167
2168 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302169 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002170 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2171
2172 auto rc = encode_poll_for_platform_event_message_resp(
2173 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2174 transferFlag, eventClass, eventDataSize, NULL,
2175 eventDataIntegrityChecksum, NULL, payloadLength);
2176
2177 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2178
2179 rc = encode_poll_for_platform_event_message_resp(
2180 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2181 transferFlag, eventClass, 1, NULL, eventDataIntegrityChecksum, response,
2182 payloadLength);
2183 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2184}
2185
Andrew Jeffery9c766792022-08-10 23:12:49 +09302186TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
2187{
2188 std::array<uint8_t,
2189 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2190 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES>
2191 requestMsg{};
2192
2193 uint8_t retFormatVersion = 0;
2194 uint8_t retTid = 0;
2195 uint8_t retEventClass = 0;
2196 size_t retEventDataOffset = 0;
2197
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302198 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302199 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
2200 struct pldm_platform_event_message_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302201 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302202 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload);
2203
2204 uint8_t formatVersion = 0x01;
2205 uint8_t tid = 0x02;
2206 // Sensor Event
2207 uint8_t eventClass = 0x00;
2208
2209 request->format_version = formatVersion;
2210 request->tid = tid;
2211 request->event_class = eventClass;
2212 size_t eventDataOffset =
2213 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass);
2214
2215 auto rc = decode_platform_event_message_req(
2216 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid,
2217 &retEventClass, &retEventDataOffset);
2218
2219 EXPECT_EQ(rc, PLDM_SUCCESS);
2220 EXPECT_EQ(retFormatVersion, formatVersion);
2221 EXPECT_EQ(retTid, tid);
2222 EXPECT_EQ(retEventClass, eventClass);
2223 EXPECT_EQ(retEventDataOffset, eventDataOffset);
2224}
2225
2226TEST(PlatformEventMessage, testBadDecodeRequest)
2227{
2228 const struct pldm_msg* msg = NULL;
2229 std::array<uint8_t,
2230 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2231 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
2232 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302233 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302234 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
2235 uint8_t retFormatVersion;
2236 uint8_t retTid = 0;
2237 uint8_t retEventClass = 0;
2238 size_t retEventDataOffset;
2239
2240 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL,
2241 NULL, NULL);
2242 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2243
2244 rc = decode_platform_event_message_req(
2245 req,
2246 requestMsg.size() - hdrSize -
2247 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES,
2248 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset);
2249 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2250}
2251
2252TEST(PlatformEventMessage, testGoodEncodeResponse)
2253{
2254 std::array<uint8_t,
2255 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2256 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
2257 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302258 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302259 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2260 uint8_t completionCode = 0;
2261 uint8_t instanceId = 0x01;
2262 uint8_t platformEventStatus = 0x01;
2263
2264 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS,
2265 platformEventStatus, response);
2266
2267 EXPECT_EQ(rc, PLDM_SUCCESS);
2268 EXPECT_EQ(completionCode, response->payload[0]);
2269 EXPECT_EQ(platformEventStatus, response->payload[1]);
2270}
2271
2272TEST(PlatformEventMessage, testBadEncodeResponse)
2273{
2274 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL);
2275 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2276}
2277
2278TEST(PlatformEventMessage, testGoodEncodeRequest)
2279{
John Chungb43a7782024-09-26 22:04:27 +08002280 static constexpr const uint8_t formatVersion = 0x01;
2281 static constexpr const uint8_t eventClass = 0x00;
2282 static constexpr const uint8_t eventData = 34;
2283 static constexpr const uint8_t Tid = 0x03;
2284 struct pldm_platform_event_message_req req;
2285 struct pldm_msgbuf _buf;
2286 struct pldm_msgbuf* buf = &_buf;
2287 size_t len;
2288 void* data;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302289
John Chungb43a7782024-09-26 22:04:27 +08002290 PLDM_MSG_DEFINE_P(request, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2291 sizeof(eventData));
Andrew Jeffery9c766792022-08-10 23:12:49 +09302292
John Chungb43a7782024-09-26 22:04:27 +08002293 /* Test with the minimum event type value */
Andrew Jeffery9c766792022-08-10 23:12:49 +09302294 auto rc = encode_platform_event_message_req(
John Chungb43a7782024-09-26 22:04:27 +08002295 0, formatVersion, Tid, eventClass, &eventData, sizeof(eventData),
2296 request, sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
2297 ASSERT_EQ(rc, PLDM_SUCCESS);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302298
John Chungb43a7782024-09-26 22:04:27 +08002299 rc = pldm_msgbuf_init_errno(
2300 buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES, request->payload,
2301 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData));
2302 ASSERT_EQ(rc, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302303
John Chungb43a7782024-09-26 22:04:27 +08002304 pldm_msgbuf_extract_uint8(buf, req.format_version);
2305 pldm_msgbuf_extract_uint8(buf, req.tid);
2306 pldm_msgbuf_extract_uint8(buf, req.event_class);
2307 data = nullptr;
2308 pldm_msgbuf_span_remaining(buf, &data, &len);
2309 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
2310
2311 EXPECT_EQ(formatVersion, req.format_version);
2312 EXPECT_EQ(Tid, req.tid);
2313 EXPECT_EQ(eventClass, req.event_class);
2314 ASSERT_EQ(sizeof(eventData), len);
2315 EXPECT_EQ(0, memcmp(&eventData, data, len));
2316
2317 /* Test with the maximum event type value */
2318 rc = encode_platform_event_message_req(
2319 0, formatVersion, Tid, PLDM_CPER_EVENT, &eventData, sizeof(eventData),
2320 request, sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
2321 ASSERT_EQ(rc, PLDM_SUCCESS);
2322
2323 rc = pldm_msgbuf_init_errno(
2324 buf, PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES, request->payload,
2325 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData));
2326 ASSERT_EQ(rc, 0);
2327
2328 pldm_msgbuf_extract_uint8(buf, req.format_version);
2329 pldm_msgbuf_extract_uint8(buf, req.tid);
2330 pldm_msgbuf_extract_uint8(buf, req.event_class);
2331
2332 data = nullptr;
2333 pldm_msgbuf_span_remaining(buf, &data, &len);
2334 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
2335
2336 EXPECT_EQ(formatVersion, req.format_version);
2337 EXPECT_EQ(Tid, req.tid);
2338 EXPECT_EQ(PLDM_CPER_EVENT, req.event_class);
2339 ASSERT_EQ(sizeof(eventData), len);
2340 EXPECT_EQ(0, memcmp(&eventData, data, len));
Andrew Jeffery9c766792022-08-10 23:12:49 +09302341}
2342
2343TEST(PlatformEventMessage, testBadEncodeRequest)
2344{
2345 uint8_t Tid = 0x03;
2346 uint8_t eventClass = 0x00;
2347 uint8_t eventData = 34;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302348 uint8_t formatVersion = 0x01;
John Chungb43a7782024-09-26 22:04:27 +08002349 static constexpr const size_t payloadLen =
2350 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES + sizeof(eventData);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302351
John Chungb43a7782024-09-26 22:04:27 +08002352 PLDM_MSG_DEFINE_P(request, payloadLen);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302353
2354 auto rc = encode_platform_event_message_req(
John Chungb43a7782024-09-26 22:04:27 +08002355 0, formatVersion, Tid, eventClass, &eventData, sizeof(eventData),
2356 nullptr, payloadLen);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302357 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Chungb43a7782024-09-26 22:04:27 +08002358
2359 rc = encode_platform_event_message_req(0, 0, Tid, eventClass, &eventData,
2360 sizeof(eventData), request,
2361 payloadLen);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302362 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Chungb43a7782024-09-26 22:04:27 +08002363
Andrew Jeffery9c766792022-08-10 23:12:49 +09302364 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
2365 nullptr, 0, request, payloadLen);
2366 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
John Chungb43a7782024-09-26 22:04:27 +08002367
2368 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
2369 &eventData, sizeof(eventData),
2370 request, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302371 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
John Chungb43a7782024-09-26 22:04:27 +08002372
2373 rc = encode_platform_event_message_req(
2374 0, formatVersion, Tid, PLDM_CPER_EVENT + 1, &eventData,
2375 sizeof(eventData), request, payloadLen);
2376 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302377}
2378
2379TEST(PlatformEventMessage, testGoodDecodeResponse)
2380{
2381 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
2382 responseMsg{};
2383
2384 uint8_t completionCode = PLDM_SUCCESS;
2385 uint8_t platformEventStatus = 0x01;
2386
2387 uint8_t retcompletionCode;
2388 uint8_t retplatformEventStatus;
2389
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302390 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302391 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2392 struct pldm_platform_event_message_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302393 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302394 reinterpret_cast<struct pldm_platform_event_message_resp*>(
2395 response->payload);
2396
2397 resp->completion_code = completionCode;
2398 resp->platform_event_status = platformEventStatus;
2399
2400 auto rc = decode_platform_event_message_resp(
2401 response, responseMsg.size() - hdrSize, &retcompletionCode,
2402 &retplatformEventStatus);
2403
2404 EXPECT_EQ(rc, PLDM_SUCCESS);
2405 EXPECT_EQ(completionCode, retcompletionCode);
2406 EXPECT_EQ(platformEventStatus, retplatformEventStatus);
2407}
2408
2409TEST(PlatformEventMessage, testBadDecodeResponse)
2410{
2411 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
2412 responseMsg{};
2413
2414 uint8_t completionCode = PLDM_SUCCESS;
2415 uint8_t platformEventStatus = 0x01;
2416
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302417 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302418 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2419 struct pldm_platform_event_message_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302420 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302421 reinterpret_cast<struct pldm_platform_event_message_resp*>(
2422 response->payload);
2423 resp->completion_code = completionCode;
2424 resp->platform_event_status = platformEventStatus;
2425
2426 auto rc = decode_platform_event_message_resp(
2427 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
2428
2429 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2430
2431 rc = decode_platform_event_message_resp(
2432 response, responseMsg.size() - hdrSize - 1, &completionCode,
2433 &platformEventStatus);
2434
2435 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2436}
2437
2438TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
2439{
2440 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +
2441 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
2442 eventDataArr{};
2443 uint16_t sensorId = 0x1234;
2444 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE;
2445
2446 struct pldm_sensor_event_data* eventData =
2447 (struct pldm_sensor_event_data*)eventDataArr.data();
2448 eventData->sensor_id = sensorId;
2449 eventData->sensor_event_class_type = sensorEventClassType;
2450
2451 size_t retSensorOpDataOffset;
2452 uint16_t retSensorId = 0;
2453 uint8_t retSensorEventClassType;
2454 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType);
2455 auto rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302456 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302457 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(),
2458 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2459 EXPECT_EQ(rc, PLDM_SUCCESS);
2460 EXPECT_EQ(retSensorId, sensorId);
2461 EXPECT_EQ(retSensorEventClassType, sensorEventClassType);
2462 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset);
2463}
2464
2465TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest)
2466{
2467
2468 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH +
2469 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
2470 eventDataArr{};
2471
2472 struct pldm_sensor_event_data* eventData =
2473 (struct pldm_sensor_event_data*)eventDataArr.data();
2474
2475 size_t retSensorOpDataOffset;
2476 uint16_t retSensorId = 0;
2477 uint8_t retSensorEventClassType;
2478 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId,
2479 &retSensorEventClassType,
2480 &retSensorOpDataOffset);
2481 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2482
2483 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302484 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302485 reinterpret_cast<uint8_t*>(eventDataArr.data()),
2486 eventDataArr.size() -
2487 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH,
2488 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2489 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2490
2491 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
2492
2493 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302494 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302495 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
2496 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2497 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2498
2499 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
2500 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302501 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302502 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
2503 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2504 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2505
2506 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE;
2507 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302508 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302509 reinterpret_cast<uint8_t*>(eventDataArr.data()),
2510 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType,
2511 &retSensorOpDataOffset);
2512 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2513}
2514
Dung Cao7c250342022-11-16 22:40:37 +07002515TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataDecodeRequest)
2516{
2517 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2518 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2519 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2520 eventData{
2521 0x1, // version
2522 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302523 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002524 };
2525
2526 uint8_t formatVersion = 0x01;
2527 uint16_t eventID = 0x7788;
2528 uint32_t dataTransferHandle = 0x11223344;
2529
Thu Nguyen7739d122024-07-26 11:36:39 +00002530 struct pldm_message_poll_event poll_event = {};
Dung Cao7c250342022-11-16 22:40:37 +07002531
2532 auto rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302533 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002534 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002535 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002536
2537 EXPECT_EQ(rc, PLDM_SUCCESS);
Thu Nguyen7739d122024-07-26 11:36:39 +00002538 EXPECT_EQ(poll_event.format_version, formatVersion);
2539 EXPECT_EQ(poll_event.event_id, eventID);
2540 EXPECT_EQ(poll_event.data_transfer_handle, dataTransferHandle);
Dung Cao7c250342022-11-16 22:40:37 +07002541}
2542
2543TEST(PlatformEventMessage, testBadPldmMsgPollEventDataDecodeRequest)
2544{
2545
2546 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2547 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2548 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2549 eventData{
2550 0x1, // version
2551 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302552 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002553 };
2554
Thu Nguyen7739d122024-07-26 11:36:39 +00002555 struct pldm_message_poll_event poll_event = {};
Dung Cao7c250342022-11-16 22:40:37 +07002556
Thu Nguyen7739d122024-07-26 11:36:39 +00002557 auto rc = decode_pldm_message_poll_event_data(NULL, eventData.size(),
2558 &poll_event);
2559 EXPECT_EQ(rc, -EINVAL);
2560
2561 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302562 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002563 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), NULL);
2564 EXPECT_EQ(rc, -EINVAL);
Dung Cao7c250342022-11-16 22:40:37 +07002565
2566 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302567 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002568 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
Thu Nguyen7739d122024-07-26 11:36:39 +00002569 &poll_event);
2570 EXPECT_EQ(rc, -EOVERFLOW);
Dung Cao7c250342022-11-16 22:40:37 +07002571
2572 // Event id is 0x0000
2573 eventData[1] = 0x00;
2574 eventData[2] = 0x00;
2575 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302576 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002577 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002578 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002579
Thu Nguyen7739d122024-07-26 11:36:39 +00002580 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002581
2582 // Event id is 0xffff
2583 eventData[1] = 0xff;
2584 eventData[2] = 0xff;
2585 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302586 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002587 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002588 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002589
Thu Nguyen7739d122024-07-26 11:36:39 +00002590 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002591}
2592
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302593#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002594TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataEncode)
2595{
2596 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2597 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2598 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2599 eventData{};
2600
Thu Nguyen7739d122024-07-26 11:36:39 +00002601 struct pldm_message_poll_event poll_event = {};
2602 poll_event.format_version = 0x01;
2603 poll_event.event_id = 0x7788;
2604 poll_event.data_transfer_handle = 0x11223344;
Dung Cao7c250342022-11-16 22:40:37 +07002605
2606 int rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302607 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002608 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2609 eventData.size());
Dung Cao7c250342022-11-16 22:40:37 +07002610
2611 EXPECT_EQ(rc, PLDM_SUCCESS);
2612
2613 struct pldm_msgbuf _buf;
2614 struct pldm_msgbuf* buf = &_buf;
2615
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302616 rc = pldm_msgbuf_init_errno(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302617 buf, PLDM_MSG_POLL_EVENT_LENGTH,
2618 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2619 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
Andrew Jeffery830c1eb2024-10-04 10:48:10 +09302620 EXPECT_EQ(rc, 0);
Dung Cao7c250342022-11-16 22:40:37 +07002621
2622 uint8_t retFormatVersion;
2623 uint16_t reteventID;
2624 uint32_t retDataTransferHandle;
2625
Andrew Jefferye5f12532024-10-01 12:18:49 +09302626 EXPECT_EQ(pldm_msgbuf_extract_uint8(buf, retFormatVersion), PLDM_SUCCESS);
2627 EXPECT_EQ(pldm_msgbuf_extract_uint16(buf, reteventID), PLDM_SUCCESS);
2628 EXPECT_EQ(pldm_msgbuf_extract_uint32(buf, retDataTransferHandle),
Dung Cao7c250342022-11-16 22:40:37 +07002629 PLDM_SUCCESS);
Thu Nguyen7739d122024-07-26 11:36:39 +00002630 EXPECT_EQ(retFormatVersion, poll_event.format_version);
2631 EXPECT_EQ(reteventID, poll_event.event_id);
2632 EXPECT_EQ(retDataTransferHandle, poll_event.data_transfer_handle);
Dung Cao7c250342022-11-16 22:40:37 +07002633 EXPECT_EQ(pldm_msgbuf_destroy_consumed(buf), PLDM_SUCCESS);
2634}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302635#endif
Dung Cao7c250342022-11-16 22:40:37 +07002636
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302637#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002638TEST(PlatformEventMessage, testBadPldmMsgPollEventDataEncode)
2639{
2640 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2641 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2642 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2643 eventData{};
2644
Thu Nguyen7739d122024-07-26 11:36:39 +00002645 struct pldm_message_poll_event poll_event = {};
2646 poll_event.format_version = 0x01;
2647 poll_event.event_id = 0x7788;
2648 poll_event.data_transfer_handle = 0x11223344;
Dung Cao7c250342022-11-16 22:40:37 +07002649
Thu Nguyen7739d122024-07-26 11:36:39 +00002650 int rc = encode_pldm_message_poll_event_data(&poll_event, NULL,
2651 eventData.size());
2652 EXPECT_EQ(rc, -EINVAL);
Dung Cao7c250342022-11-16 22:40:37 +07002653
Thu Nguyen7739d122024-07-26 11:36:39 +00002654 poll_event.event_id = 0x0000;
Dung Cao7c250342022-11-16 22:40:37 +07002655 rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302656 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002657 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2658 eventData.size());
2659 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002660
Thu Nguyen7739d122024-07-26 11:36:39 +00002661 poll_event.event_id = 0xffff;
Dung Cao7c250342022-11-16 22:40:37 +07002662 rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302663 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002664 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2665 eventData.size());
2666 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002667}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302668#endif
Dung Cao7c250342022-11-16 22:40:37 +07002669
Andrew Jeffery9c766792022-08-10 23:12:49 +09302670TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
2671{
2672 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2673 eventDataArr{};
2674
2675 struct pldm_sensor_event_sensor_op_state* sensorData =
2676 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
2677 uint8_t presentState = PLDM_SENSOR_ENABLED;
2678 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
2679 sensorData->present_op_state = presentState;
2680 sensorData->previous_op_state = previousState;
2681
2682 uint8_t retPresentState;
2683 uint8_t retPreviousState;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302684 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302685 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
2686 eventDataArr.size(), &retPresentState,
2687 &retPreviousState);
2688 EXPECT_EQ(rc, PLDM_SUCCESS);
2689 EXPECT_EQ(retPresentState, presentState);
2690 EXPECT_EQ(retPreviousState, previousState);
2691}
2692
2693TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
2694{
2695 uint8_t presentOpState;
2696 uint8_t previousOpState;
2697 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
2698 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
2699 &previousOpState);
2700 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2701
2702 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2703 sensorData{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302704 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302705 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2706 sensorDataLength + 1, &presentOpState,
2707 &previousOpState);
2708 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2709
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302710 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302711 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2712 sensorDataLength, nullptr, &previousOpState);
2713 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2714}
2715
2716TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
2717{
2718 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2719 eventDataArr{};
2720
2721 struct pldm_sensor_event_state_sensor_state* sensorData =
2722 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
2723 uint8_t sensorOffset = 0x02;
2724 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2725 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2726 sensorData->sensor_offset = sensorOffset;
2727 sensorData->event_state = eventState;
2728 sensorData->previous_event_state = previousEventState;
2729
2730 uint8_t retSensorOffset;
2731 uint8_t retEventState;
2732 uint8_t retPreviousState;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302733 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302734 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2735 eventDataArr.size(), &retSensorOffset,
2736 &retEventState, &retPreviousState);
2737 EXPECT_EQ(rc, PLDM_SUCCESS);
2738 EXPECT_EQ(retSensorOffset, sensorOffset);
2739 EXPECT_EQ(retEventState, eventState);
2740 EXPECT_EQ(retPreviousState, previousEventState);
2741}
2742
2743TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
2744{
2745 uint8_t sensorOffset;
2746 uint8_t eventState;
2747 uint8_t previousEventState;
2748 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
2749 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
2750 &eventState, &previousEventState);
2751 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2752
2753 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2754 sensorData{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302755 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302756 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2757 sensorDataLength - 1, &sensorOffset,
2758 &eventState, &previousEventState);
2759 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2760
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302761 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302762 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2763 sensorDataLength, &sensorOffset, nullptr,
2764 &previousEventState);
2765 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2766}
2767
2768TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
2769{
2770 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2771 eventDataArr{};
2772 struct pldm_sensor_event_numeric_sensor_state* sensorData =
2773 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
2774
2775 size_t sensorDataLength =
2776 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
2777 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2778 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2779 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
2780 uint32_t presentReading = 305441741;
2781 sensorData->event_state = eventState;
2782 sensorData->previous_event_state = previousEventState;
2783 sensorData->sensor_data_size = sensorDataSize;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302784 {
2785 uint32_t presentReadingLE = htole32(presentReading);
2786 memcpy(&sensorData->present_reading, &presentReadingLE,
2787 sizeof(presentReadingLE));
2788 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302789
2790 uint8_t retEventState;
2791 uint8_t retPreviousEventState;
2792 uint8_t retSensorDataSize;
2793 uint32_t retPresentReading;
2794
2795 auto rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302796 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302797 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
2798 &retEventState, &retPreviousEventState, &retSensorDataSize,
2799 &retPresentReading);
2800 EXPECT_EQ(rc, PLDM_SUCCESS);
2801 EXPECT_EQ(retEventState, eventState);
2802 EXPECT_EQ(retPreviousEventState, previousEventState);
2803 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2804 EXPECT_EQ(retPresentReading, presentReading);
2805
2806 int16_t presentReadingNew = -31432;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302807 {
2808 int16_t presentReadingNewLE = htole16(presentReadingNew);
2809 memcpy(&sensorData->present_reading, &presentReadingNewLE,
2810 sizeof(presentReadingNewLE));
2811 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302812 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
2813 sensorData->sensor_data_size = sensorDataSize;
2814 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
2815
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302816 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302817 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2818 sensorDataLength, &retEventState,
2819 &retPreviousEventState, &retSensorDataSize,
2820 &retPresentReading);
2821 EXPECT_EQ(rc, PLDM_SUCCESS);
2822 EXPECT_EQ(retEventState, eventState);
2823 EXPECT_EQ(retPreviousEventState, previousEventState);
2824 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2825 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
2826}
2827
2828TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
2829{
2830 uint8_t eventState;
2831 uint8_t previousEventState;
2832 uint8_t sensorDataSize;
2833 uint32_t presentReading;
2834 size_t sensorDataLength =
2835 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
2836 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
2837 &previousEventState, &sensorDataSize,
2838 &presentReading);
2839 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2840
2841 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2842 sensorData{};
2843 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302844 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302845 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
2846 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2847 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2848
2849 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
2850 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
2851 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
2852 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302853 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302854 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2855 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2856 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2857
2858 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
2859 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302860 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302861 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2862 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2863 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302864}
2865
2866TEST(GetNumericEffecterValue, testGoodEncodeRequest)
2867{
2868 std::vector<uint8_t> requestMsg(hdrSize +
2869 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
2870
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002871 uint16_t effecter_id = 0xab01;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302872
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302873 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302874 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2875
2876 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
2877
2878 struct pldm_get_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302879 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302880 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2881 request->payload);
2882
2883 EXPECT_EQ(rc, PLDM_SUCCESS);
2884 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
2885}
2886
2887TEST(GetNumericEffecterValue, testBadEncodeRequest)
2888{
2889 std::vector<uint8_t> requestMsg(
2890 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
2891
2892 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
2893 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2894}
2895
2896TEST(GetNumericEffecterValue, testGoodDecodeRequest)
2897{
2898 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2899 requestMsg{};
2900
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302901 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302902 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2903 struct pldm_get_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302904 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302905 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2906 request->payload);
2907
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002908 uint16_t effecter_id = 0x12ab;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302909 req->effecter_id = htole16(effecter_id);
2910
2911 uint16_t reteffecter_id;
2912
2913 auto rc = decode_get_numeric_effecter_value_req(
2914 request, requestMsg.size() - hdrSize, &reteffecter_id);
2915
2916 EXPECT_EQ(rc, PLDM_SUCCESS);
2917 EXPECT_EQ(effecter_id, reteffecter_id);
2918}
2919
2920TEST(GetNumericEffecterValue, testBadDecodeRequest)
2921{
2922 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2923 requestMsg{};
2924
2925 auto rc = decode_get_numeric_effecter_value_req(
2926 nullptr, requestMsg.size() - hdrSize, nullptr);
2927
2928 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2929
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302930 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302931 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2932 struct pldm_set_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302933 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302934 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
2935 request->payload);
2936
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002937 uint16_t effecter_id = 0x1a;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302938 req->effecter_id = htole16(effecter_id);
2939 uint16_t reteffecter_id;
2940
2941 rc = decode_get_numeric_effecter_value_req(
2942 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
2943
2944 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2945}
2946
2947TEST(GetNumericEffecterValue, testGoodEncodeResponse)
2948{
2949 uint8_t completionCode = 0;
2950 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
2951 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
2952 uint32_t pendingValue = 0x12345678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002953 uint32_t presentValue = 0xabcdef11;
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302954 uint32_t val_pending;
2955 uint32_t val_present;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302956
2957 std::array<uint8_t,
2958 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
2959 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302960 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302961 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2962
2963 auto rc = encode_get_numeric_effecter_value_resp(
2964 0, completionCode, effecter_dataSize, effecter_operState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302965 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302966 reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302967 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302968 reinterpret_cast<uint8_t*>(&presentValue), response,
2969 responseMsg.size() - hdrSize);
2970
2971 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302972 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302973 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2974 response->payload);
2975
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302976 memcpy(&val_pending, &resp->pending_and_present_values[0],
2977 sizeof(val_pending));
2978 val_pending = le32toh(val_pending);
2979 memcpy(&val_present, &resp->pending_and_present_values[4],
2980 sizeof(val_present));
2981 val_present = le32toh(val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302982
2983 EXPECT_EQ(rc, PLDM_SUCCESS);
2984 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
2985 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302986 EXPECT_EQ(pendingValue, val_pending);
2987 EXPECT_EQ(presentValue, val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302988}
2989
2990TEST(GetNumericEffecterValue, testBadEncodeResponse)
2991{
2992 std::array<uint8_t,
2993 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
2994 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302995 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302996 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2997
2998 uint8_t pendingValue = 0x01;
2999 uint8_t presentValue = 0x02;
3000
3001 auto rc = encode_get_numeric_effecter_value_resp(
3002 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
3003 responseMsg.size() - hdrSize);
3004 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3005
3006 rc = encode_get_numeric_effecter_value_resp(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303007 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303008 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303009 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303010 reinterpret_cast<uint8_t*>(&presentValue), response,
3011 responseMsg.size() - hdrSize);
3012 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3013
3014 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3015 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
3016
3017 rc = encode_get_numeric_effecter_value_resp(
3018 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303019 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303020 reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303021 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303022 reinterpret_cast<uint8_t*>(&presentValue), response,
3023 responseMsg.size() - hdrSize);
3024 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3025}
3026
3027TEST(GetNumericEffecterValue, testGoodDecodeResponse)
3028{
3029 std::array<uint8_t,
3030 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
3031 responseMsg{};
3032
3033 uint8_t completionCode = 0;
3034 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
3035 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
3036 uint16_t pendingValue = 0x4321;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003037 uint16_t presentValue = 0xdcba;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303038
3039 uint8_t retcompletionCode;
3040 uint8_t reteffecter_dataSize;
3041 uint8_t reteffecter_operState;
3042 uint8_t retpendingValue[2];
3043 uint8_t retpresentValue[2];
3044
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303045 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303046 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3047 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303048 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303049 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
3050 response->payload);
3051
3052 resp->completion_code = completionCode;
3053 resp->effecter_data_size = effecter_dataSize;
3054 resp->effecter_oper_state = effecter_operState;
3055
3056 uint16_t pendingValue_le = htole16(pendingValue);
3057 memcpy(resp->pending_and_present_values, &pendingValue_le,
3058 sizeof(pendingValue_le));
3059 uint16_t presentValue_le = htole16(presentValue);
3060 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
3061 sizeof(presentValue_le));
3062
3063 auto rc = decode_get_numeric_effecter_value_resp(
3064 response, responseMsg.size() - hdrSize, &retcompletionCode,
3065 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
3066 retpresentValue);
3067
3068 EXPECT_EQ(rc, PLDM_SUCCESS);
3069 EXPECT_EQ(completionCode, retcompletionCode);
3070 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
3071 EXPECT_EQ(effecter_operState, reteffecter_operState);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303072 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303073 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303074 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303075 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
3076}
3077
3078TEST(GetNumericEffecterValue, testBadDecodeResponse)
3079{
3080 std::array<uint8_t,
3081 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
3082 responseMsg{};
3083
3084 auto rc = decode_get_numeric_effecter_value_resp(
3085 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
3086 nullptr, nullptr);
3087
3088 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3089
3090 uint8_t completionCode = 0;
3091 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
3092 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
3093 uint16_t pendingValue = 0x5678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003094 uint16_t presentValue = 0xcdef;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303095
3096 uint8_t retcompletionCode;
3097 uint8_t reteffecter_dataSize;
3098 uint8_t reteffecter_operState;
3099 uint8_t retpendingValue[2];
3100 uint8_t retpresentValue[2];
3101
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303102 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303103 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3104 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303105 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303106 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
3107 response->payload);
3108
3109 resp->completion_code = completionCode;
3110 resp->effecter_data_size = effecter_dataSize;
3111 resp->effecter_oper_state = effecter_operState;
3112
3113 uint16_t pendingValue_le = htole16(pendingValue);
3114 memcpy(resp->pending_and_present_values, &pendingValue_le,
3115 sizeof(pendingValue_le));
3116 uint16_t presentValue_le = htole16(presentValue);
3117 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
3118 sizeof(presentValue_le));
3119
3120 rc = decode_get_numeric_effecter_value_resp(
3121 response, responseMsg.size() - hdrSize, &retcompletionCode,
3122 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
3123 retpresentValue);
3124
3125 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3126}
3127
3128TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
3129{
3130 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
3131 const uint8_t numberOfChangeRecords = 2;
3132 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
3133 const uint8_t numberOfChangeEntries1 = 2;
3134 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
3135 {0x00000000, 0x12345678}};
3136 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
3137 const uint8_t numberOfChangeEntries2 = 5;
3138 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
3139 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
3140 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
3141 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
3142 numberOfChangeRecords +
3143 (numberOfChangeEntries1 + numberOfChangeEntries2) *
3144 sizeof(uint32_t)>
3145 eventDataArr{};
3146
3147 struct pldm_pdr_repository_chg_event_data* eventData =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303148 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303149 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
3150 eventDataArr.data());
3151 eventData->event_data_format = eventDataFormat;
3152 eventData->number_of_change_records = numberOfChangeRecords;
3153 struct pldm_pdr_repository_change_record_data* changeRecord1 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303154 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303155 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
3156 eventData->change_records);
3157 changeRecord1->event_data_operation = eventDataOperation1;
3158 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
3159 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
3160 changeRecordArr1.size() * sizeof(uint32_t));
3161 struct pldm_pdr_repository_change_record_data* changeRecord2 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303162 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303163 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
3164 eventData->change_records +
3165 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
3166 (changeRecordArr1.size() * sizeof(uint32_t)));
3167 changeRecord2->event_data_operation = eventDataOperation2;
3168 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
3169 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
3170 changeRecordArr2.size() * sizeof(uint32_t));
3171
3172 uint8_t retEventDataFormat{};
3173 uint8_t retNumberOfChangeRecords{};
3174 size_t retChangeRecordDataOffset{0};
3175 auto rc = decode_pldm_pdr_repository_chg_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303176 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303177 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
3178 &retEventDataFormat, &retNumberOfChangeRecords,
3179 &retChangeRecordDataOffset);
3180 EXPECT_EQ(rc, PLDM_SUCCESS);
3181 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
3182 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
3183
3184 const uint8_t* changeRecordData =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303185 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303186 reinterpret_cast<const uint8_t*>(changeRecord1);
3187 size_t changeRecordDataSize =
3188 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
3189 uint8_t retEventDataOperation;
3190 uint8_t retNumberOfChangeEntries;
3191 size_t retChangeEntryDataOffset;
3192
3193 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303194 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303195 reinterpret_cast<const uint8_t*>(changeRecordData),
3196 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
3197 &retChangeEntryDataOffset);
3198 EXPECT_EQ(rc, PLDM_SUCCESS);
3199 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
3200 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
3201 changeRecordData += retChangeEntryDataOffset;
3202 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
3203 sizeof(uint32_t) * retNumberOfChangeEntries));
3204
3205 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
3206 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
3207 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
3208 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303210 reinterpret_cast<const uint8_t*>(changeRecordData),
3211 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
3212 &retChangeEntryDataOffset);
3213 EXPECT_EQ(rc, PLDM_SUCCESS);
3214 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
3215 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
3216 changeRecordData += retChangeEntryDataOffset;
3217 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
3218 sizeof(uint32_t) * retNumberOfChangeEntries));
3219}
3220
3221TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
3222{
3223 uint8_t eventDataFormat{};
3224 uint8_t numberOfChangeRecords{};
3225 size_t changeRecordDataOffset{};
3226 auto rc = decode_pldm_pdr_repository_chg_event_data(
3227 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
3228 &changeRecordDataOffset);
3229 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3230
3231 std::array<uint8_t, 2> eventData{};
3232 rc = decode_pldm_pdr_repository_chg_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303233 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303234 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
3235 &numberOfChangeRecords, &changeRecordDataOffset);
3236 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3237
3238 uint8_t eventDataOperation{};
3239 uint8_t numberOfChangeEntries{};
3240 size_t changeEntryDataOffset{};
3241 rc = decode_pldm_pdr_repository_change_record_data(
3242 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
3243 &changeEntryDataOffset);
3244 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3245
3246 std::array<uint8_t, 2> changeRecord{};
3247 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303248 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303249 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
3250 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
3251 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3252}
3253
3254TEST(GetSensorReading, testGoodEncodeRequest)
3255{
3256 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3257 requestMsg{};
3258
3259 uint16_t sensorId = 0x1234;
3260 bool8_t rearmEventState = 0x01;
3261
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303262 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303263 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3264 auto rc =
3265 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request);
3266
3267 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303268 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303269 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3270
3271 EXPECT_EQ(rc, PLDM_SUCCESS);
3272 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
3273 EXPECT_EQ(rearmEventState, req->rearm_event_state);
3274}
3275
3276TEST(GetSensorReading, testBadEncodeRequest)
3277{
3278 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr);
3279
3280 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3281}
3282
3283TEST(GetSensorReading, testGoodDecodeRequest)
3284{
3285 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3286 requestMsg{};
3287
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003288 uint16_t sensorId = 0xabcd;
3289 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303290
3291 uint16_t retsensorId;
3292 bool8_t retrearmEventState;
3293
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303295 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3296
3297 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303298 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303299 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3300
3301 req->sensor_id = htole16(sensorId);
3302 req->rearm_event_state = rearmEventState;
3303
3304 auto rc =
3305 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize,
3306 &retsensorId, &retrearmEventState);
3307
3308 EXPECT_EQ(rc, PLDM_SUCCESS);
3309 EXPECT_EQ(sensorId, retsensorId);
3310 EXPECT_EQ(rearmEventState, retrearmEventState);
3311}
3312
3313TEST(GetSensorReading, testBadDecodeRequest)
3314{
3315 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3316 requestMsg{};
3317
3318 auto rc = decode_get_sensor_reading_req(
3319 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr);
3320 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3321
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003322 uint16_t sensorId = 0xabcd;
3323 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303324
3325 uint16_t retsensorId;
3326 bool8_t retrearmEventState;
3327
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303328 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303329 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3330
3331 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303332 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303333 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3334
3335 req->sensor_id = htole16(sensorId);
3336 req->rearm_event_state = rearmEventState;
3337
3338 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1,
3339 &retsensorId, &retrearmEventState);
3340
3341 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3342}
3343
3344TEST(GetSensorReading, testGoodEncodeResponse)
3345{
3346 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES>
3347 responseMsg{};
3348
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303349 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303350 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3351
3352 uint8_t completionCode = 0;
3353 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3354 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED;
3355 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION;
3356 uint8_t presentState = PLDM_SENSOR_NORMAL;
3357 uint8_t previousState = PLDM_SENSOR_WARNING;
3358 uint8_t eventState = PLDM_SENSOR_UPPERWARNING;
3359 uint8_t presentReading = 0x21;
3360
3361 auto rc = encode_get_sensor_reading_resp(
3362 0, completionCode, sensor_dataSize, sensor_operationalState,
3363 sensor_event_messageEnable, presentState, previousState, eventState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303364 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303365 reinterpret_cast<uint8_t*>(&presentReading), response,
3366 responseMsg.size() - hdrSize);
3367
3368 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303369 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303370 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3371 response->payload);
3372
3373 EXPECT_EQ(rc, PLDM_SUCCESS);
3374 EXPECT_EQ(completionCode, resp->completion_code);
3375 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size);
3376 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state);
3377 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable);
3378 EXPECT_EQ(presentState, resp->present_state);
3379 EXPECT_EQ(previousState, resp->previous_state);
3380 EXPECT_EQ(eventState, resp->event_state);
3381 EXPECT_EQ(presentReading,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303382 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303383 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0])));
3384}
3385
3386TEST(GetSensorReading, testBadEncodeResponse)
3387{
3388 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
3389 responseMsg{};
3390
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303391 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303392 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3393
3394 uint8_t presentReading = 0x1;
3395
3396 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0,
3397 nullptr, nullptr,
3398 responseMsg.size() - hdrSize);
3399 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3400
3401 rc = encode_get_sensor_reading_resp(
3402 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303403 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303404 reinterpret_cast<uint8_t*>(&presentReading), response,
3405 responseMsg.size() - hdrSize);
3406 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3407
3408 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3409
3410 rc = encode_get_sensor_reading_resp(
3411 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303412 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303413 reinterpret_cast<uint8_t*>(&presentReading), response,
3414 responseMsg.size() - hdrSize);
3415 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3416}
3417
3418TEST(GetSensorReading, testGoodDecodeResponse)
3419{
3420 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
3421 responseMsg{};
3422
3423 uint8_t completionCode = 0;
3424 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
3425 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN;
3426 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED;
3427 uint8_t presentState = PLDM_SENSOR_CRITICAL;
3428 uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL;
3429 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003430 uint32_t presentReading = 0xabcdef11;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303431
3432 uint8_t retcompletionCode;
3433 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
3434 uint8_t retsensor_operationalState;
3435 uint8_t retsensor_event_messageEnable;
3436 uint8_t retpresentState;
3437 uint8_t retpreviousState;
3438 uint8_t reteventState;
3439 uint8_t retpresentReading[4];
3440
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303441 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303442 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3443 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303444 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303445 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3446 response->payload);
3447
3448 resp->completion_code = completionCode;
3449 resp->sensor_data_size = sensor_dataSize;
3450 resp->sensor_operational_state = sensor_operationalState;
3451 resp->sensor_event_message_enable = sensor_event_messageEnable;
3452 resp->present_state = presentState;
3453 resp->previous_state = previousState;
3454 resp->event_state = eventState;
3455
3456 uint32_t presentReading_le = htole32(presentReading);
3457 memcpy(resp->present_reading, &presentReading_le,
3458 sizeof(presentReading_le));
3459
3460 auto rc = decode_get_sensor_reading_resp(
3461 response, responseMsg.size() - hdrSize, &retcompletionCode,
3462 &retsensor_dataSize, &retsensor_operationalState,
3463 &retsensor_event_messageEnable, &retpresentState, &retpreviousState,
3464 &reteventState, retpresentReading);
3465
3466 EXPECT_EQ(rc, PLDM_SUCCESS);
3467 EXPECT_EQ(completionCode, retcompletionCode);
3468 EXPECT_EQ(sensor_dataSize, retsensor_dataSize);
3469 EXPECT_EQ(sensor_operationalState, retsensor_operationalState);
3470 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable);
3471 EXPECT_EQ(presentState, retpresentState);
3472 EXPECT_EQ(previousState, retpreviousState);
3473 EXPECT_EQ(eventState, reteventState);
3474 EXPECT_EQ(presentReading,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303475 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303476 *(reinterpret_cast<uint32_t*>(retpresentReading)));
3477}
3478
3479TEST(GetSensorReading, testBadDecodeResponse)
3480{
3481 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1>
3482 responseMsg{};
3483
3484 auto rc = decode_get_sensor_reading_resp(
3485 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
3486 nullptr, nullptr, nullptr, nullptr, nullptr);
3487
3488 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3489
3490 uint8_t completionCode = 0;
3491 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3492 uint8_t sensor_operationalState = PLDM_SENSOR_INTEST;
3493 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED;
3494 uint8_t presentState = PLDM_SENSOR_FATAL;
3495 uint8_t previousState = PLDM_SENSOR_UPPERFATAL;
3496 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003497 uint8_t presentReading = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303498
3499 uint8_t retcompletionCode;
3500 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
3501 uint8_t retsensor_operationalState;
3502 uint8_t retsensor_event_messageEnable;
3503 uint8_t retpresent_state;
3504 uint8_t retprevious_state;
3505 uint8_t retevent_state;
3506 uint8_t retpresentReading;
3507
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303508 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303509 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3510 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303511 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303512 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3513 response->payload);
3514
3515 resp->completion_code = completionCode;
3516 resp->sensor_data_size = sensor_dataSize;
3517 resp->sensor_operational_state = sensor_operationalState;
3518 resp->sensor_event_message_enable = sensor_event_messageEnable;
3519 resp->present_state = presentState;
3520 resp->previous_state = previousState;
3521 resp->event_state = eventState;
3522 resp->present_reading[0] = presentReading;
3523
3524 rc = decode_get_sensor_reading_resp(
3525 response, responseMsg.size() - hdrSize, &retcompletionCode,
3526 &retsensor_dataSize, &retsensor_operationalState,
3527 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303528 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303529 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading));
3530
3531 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3532}
3533
3534TEST(SetEventReceiver, testGoodEncodeRequest)
3535{
3536 uint8_t eventMessageGlobalEnable =
3537 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3538 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3539 uint8_t eventReceiverAddressInfo = 0x08;
3540 uint16_t heartbeatTimer = 0x78;
3541
Andrew Jeffery2332e052024-10-08 13:52:34 +10303542 std::vector<uint8_t> requestMsg(hdrSize +
3543 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
3544 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3545 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jeffery9c766792022-08-10 23:12:49 +09303546
3547 auto rc = encode_set_event_receiver_req(
3548 0, eventMessageGlobalEnable, transportProtocolType,
3549 eventReceiverAddressInfo, heartbeatTimer, request);
3550
3551 EXPECT_EQ(rc, PLDM_SUCCESS);
3552 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303553 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303554 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3555 EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable);
3556 EXPECT_EQ(transportProtocolType, req->transport_protocol_type);
3557 EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info);
3558 EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer));
3559}
3560
3561TEST(SetEventReceiver, testBadEncodeRequest)
3562{
3563 uint8_t eventMessageGlobalEnable =
3564 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3565 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3566 uint8_t eventReceiverAddressInfo = 0x08;
3567 uint16_t heartbeatTimer = 0;
3568
Andrew Jeffery2332e052024-10-08 13:52:34 +10303569 std::vector<uint8_t> requestMsg(hdrSize +
3570 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
3571 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3572 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jeffery9c766792022-08-10 23:12:49 +09303573
3574 auto rc = encode_set_event_receiver_req(
3575 0, eventMessageGlobalEnable, transportProtocolType,
3576 eventReceiverAddressInfo, heartbeatTimer, request);
3577
3578 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3579}
3580
3581TEST(SetEventReceiver, testGoodDecodeResponse)
3582{
3583 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3584 responseMsg{};
3585
3586 uint8_t retcompletion_code = 0;
3587 responseMsg[hdrSize] = PLDM_SUCCESS;
3588
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303589 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303590 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3591 auto rc = decode_set_event_receiver_resp(
3592 response, responseMsg.size() - sizeof(pldm_msg_hdr),
3593 &retcompletion_code);
3594
3595 EXPECT_EQ(rc, PLDM_SUCCESS);
3596 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
3597}
3598
3599TEST(SetEventReceiver, testBadDecodeResponse)
3600{
3601 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3602 responseMsg{};
3603 uint8_t retcompletion_code = 0;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303604 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303605 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3606
3607 auto rc = decode_set_event_receiver_resp(
3608 response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL);
3609
3610 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3611
3612 rc = decode_set_event_receiver_resp(
3613 nullptr, responseMsg.size() - sizeof(pldm_msg_hdr),
3614 &retcompletion_code);
3615
3616 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3617}
3618
3619TEST(SetEventReceiver, testGoodEncodeResponse)
3620{
3621 std::array<uint8_t,
3622 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3623 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303624 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303625 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3626 uint8_t completionCode = 0;
3627
3628 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response);
3629
3630 EXPECT_EQ(rc, PLDM_SUCCESS);
3631 EXPECT_EQ(completionCode, response->payload[0]);
3632}
3633
3634TEST(SetEventReceiver, testBadEncodeResponse)
3635{
3636 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL);
3637 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3638}
3639
3640TEST(SetEventReceiver, testGoodDecodeRequest)
3641{
Andrew Jeffery2332e052024-10-08 13:52:34 +10303642
3643 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3644 requestMsg{};
Andrew Jeffery9c766792022-08-10 23:12:49 +09303645
3646 uint8_t eventMessageGlobalEnable =
3647 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3648 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3649 uint8_t eventReceiverAddressInfo = 0x08;
3650 uint16_t heartbeatTimer = 0x78;
3651
Andrew Jeffery2332e052024-10-08 13:52:34 +10303652 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3653 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jeffery9c766792022-08-10 23:12:49 +09303654 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303655 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303656 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3657
3658 req->event_message_global_enable = eventMessageGlobalEnable;
3659 req->transport_protocol_type = transportProtocolType;
3660 req->event_receiver_address_info = eventReceiverAddressInfo;
3661 req->heartbeat_timer = htole16(heartbeatTimer);
3662
3663 uint8_t reteventMessageGlobalEnable;
3664 uint8_t rettransportProtocolType;
3665 uint8_t reteventReceiverAddressInfo;
3666 uint16_t retheartbeatTimer;
3667 auto rc = decode_set_event_receiver_req(
Andrew Jeffery2332e052024-10-08 13:52:34 +10303668 request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable,
3669 &rettransportProtocolType, &reteventReceiverAddressInfo,
3670 &retheartbeatTimer);
Andrew Jeffery9c766792022-08-10 23:12:49 +09303671
3672 EXPECT_EQ(rc, PLDM_SUCCESS);
3673 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
3674 EXPECT_EQ(transportProtocolType, rettransportProtocolType);
3675 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
3676 EXPECT_EQ(heartbeatTimer, retheartbeatTimer);
Gilbert Chen98e137d2024-10-08 08:00:39 +00003677
3678 eventMessageGlobalEnable = PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC;
3679 req->event_message_global_enable = eventMessageGlobalEnable;
3680 rc = decode_set_event_receiver_req(
3681 request, PLDM_SET_EVENT_RECEIVER_MIN_REQ_BYTES,
3682 &reteventMessageGlobalEnable, &rettransportProtocolType,
3683 &reteventReceiverAddressInfo, &retheartbeatTimer);
3684 EXPECT_EQ(rc, PLDM_SUCCESS);
3685 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
3686 EXPECT_EQ(transportProtocolType, rettransportProtocolType);
3687 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
Andrew Jeffery9c766792022-08-10 23:12:49 +09303688}
3689
3690TEST(SetEventReceiver, testBadDecodeRequest)
3691{
Andrew Jeffery2332e052024-10-08 13:52:34 +10303692 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3693 requestMsg{};
Andrew Jeffery9c766792022-08-10 23:12:49 +09303694
Andrew Jeffery2332e052024-10-08 13:52:34 +10303695 auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize,
3696 NULL, NULL, NULL, NULL);
Andrew Jeffery9c766792022-08-10 23:12:49 +09303697 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3698
3699 uint8_t eventMessageGlobalEnable =
3700 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3701 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3702 uint8_t eventReceiverAddressInfo = 0x08;
3703 uint16_t heartbeatTimer = 0x78;
3704
Andrew Jeffery2332e052024-10-08 13:52:34 +10303705 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
3706 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Andrew Jeffery9c766792022-08-10 23:12:49 +09303707 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303708 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303709 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3710
3711 req->event_message_global_enable = eventMessageGlobalEnable;
3712 req->transport_protocol_type = transportProtocolType;
3713 req->event_receiver_address_info = eventReceiverAddressInfo;
3714 req->heartbeat_timer = htole16(heartbeatTimer);
3715
3716 uint8_t reteventMessageGlobalEnable;
3717 uint8_t rettransportProtocolType;
3718 uint8_t reteventReceiverAddressInfo;
3719 uint16_t retheartbeatTimer;
Andrew Jeffery4f60fb72024-09-23 13:56:44 +09303720
Andrew Jeffery9c766792022-08-10 23:12:49 +09303721 rc = decode_set_event_receiver_req(
Andrew Jeffery2332e052024-10-08 13:52:34 +10303722 request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable,
3723 &rettransportProtocolType, &reteventReceiverAddressInfo,
3724 &retheartbeatTimer);
Andrew Jeffery9c766792022-08-10 23:12:49 +09303725 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Gilbert Chen98e137d2024-10-08 08:00:39 +00003726
3727 req->event_message_global_enable = PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC;
3728 rc = decode_set_event_receiver_req(
3729 request, PLDM_SET_EVENT_RECEIVER_MIN_REQ_BYTES - 1,
3730 &reteventMessageGlobalEnable, &rettransportProtocolType,
3731 &reteventReceiverAddressInfo, &retheartbeatTimer);
3732 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3733
3734 req->event_message_global_enable =
3735 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3736 req->heartbeat_timer = 0;
3737 rc = decode_set_event_receiver_req(
3738 request, PLDM_SET_EVENT_RECEIVER_REQ_BYTES,
3739 &reteventMessageGlobalEnable, &rettransportProtocolType,
3740 &reteventReceiverAddressInfo, &retheartbeatTimer);
3741 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
Andrew Jeffery9c766792022-08-10 23:12:49 +09303742}
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303743
3744TEST(decodeNumericSensorPdrData, Uint8Test)
3745{
3746 std::vector<uint8_t> pdr1{
3747 0x1,
3748 0x0,
3749 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303750 0x0, // record handle
3751 0x1, // PDRHeaderVersion
3752 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303753 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303754 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303755 PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303756 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303757 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303758 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303759 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303760 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303761 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303762 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303763 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303764 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303765 1,
3766 0, // containerID=1
3767 PLDM_NO_INIT, // sensorInit
3768 false, // sensorAuxiliaryNamesPDR
3769 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3770 0, // unitModifier
3771 0, // rateUnit
3772 0, // baseOEMUnitHandle
3773 0, // auxUnit
3774 0, // auxUnitModifier
3775 0, // auxRateUnit
3776 0, // rel
3777 0, // auxOEMUnitHandle
3778 true, // isLinear
3779 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
3780 0,
3781 0,
3782 0xc0,
3783 0x3f, // resolution=1.5
3784 0,
3785 0,
3786 0x80,
3787 0x3f, // offset=1.0
3788 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303789 0, // accuracy
3790 0, // plusTolerance
3791 0, // minusTolerance
3792 3, // hysteresis = 3
3793 0, // supportedThresholds
3794 0, // thresholdAndHysteresisVolatility
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303795 0,
3796 0,
3797 0x80,
3798 0x3f, // stateTransistionInterval=1.0
3799 0,
3800 0,
3801 0x80,
3802 0x3f, // updateInverval=1.0
3803 255, // maxReadable
3804 0, // minReadable
3805 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
3806 0, // rangeFieldsupport
3807 50, // nominalValue = 50
3808 60, // normalMax = 60
3809 40, // normalMin = 40
3810 70, // warningHigh = 70
3811 30, // warningLow = 30
3812 80, // criticalHigh = 80
3813 20, // criticalLow = 20
3814 90, // fatalHigh = 90
3815 10 // fatalLow = 10
3816 };
3817
3818 struct pldm_numeric_sensor_value_pdr decodedPdr;
3819 auto rc =
3820 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3821 EXPECT_EQ(PLDM_SUCCESS, rc);
3822 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
3823 EXPECT_EQ(1, decodedPdr.hdr.version);
3824 EXPECT_EQ(PLDM_NUMERIC_SENSOR_PDR, decodedPdr.hdr.type);
3825 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
3826 EXPECT_EQ(PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, decodedPdr.hdr.length);
3827 EXPECT_EQ(1, decodedPdr.sensor_id);
3828 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
3829 EXPECT_EQ(1, decodedPdr.entity_instance_num);
3830 EXPECT_EQ(1, decodedPdr.container_id);
3831 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.sensor_init);
3832 EXPECT_EQ(false, decodedPdr.sensor_auxiliary_names_pdr);
3833 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
3834 EXPECT_EQ(0, decodedPdr.unit_modifier);
3835 EXPECT_EQ(0, decodedPdr.rate_unit);
3836 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
3837 EXPECT_EQ(0, decodedPdr.aux_unit);
3838 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
3839 EXPECT_EQ(0, decodedPdr.aux_rate_unit);
3840 EXPECT_EQ(0, decodedPdr.rel);
3841 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
3842 EXPECT_EQ(true, decodedPdr.is_linear);
3843 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT8, decodedPdr.sensor_data_size);
3844 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
3845 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
3846 EXPECT_EQ(0, decodedPdr.accuracy);
3847 EXPECT_EQ(0, decodedPdr.plus_tolerance);
3848 EXPECT_EQ(0, decodedPdr.minus_tolerance);
3849 EXPECT_EQ(3, decodedPdr.hysteresis.value_u8);
3850 EXPECT_EQ(0, decodedPdr.supported_thresholds.byte);
3851 EXPECT_EQ(0, decodedPdr.threshold_and_hysteresis_volatility.byte);
3852 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
3853 EXPECT_FLOAT_EQ(1.0f, decodedPdr.update_interval);
3854 EXPECT_EQ(255, decodedPdr.max_readable.value_u8);
3855 EXPECT_EQ(0, decodedPdr.min_readable.value_u8);
3856 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
3857 EXPECT_EQ(0, decodedPdr.range_field_support.byte);
3858 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
3859 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
3860 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
3861 EXPECT_EQ(70, decodedPdr.warning_high.value_u8);
3862 EXPECT_EQ(30, decodedPdr.warning_low.value_u8);
3863 EXPECT_EQ(80, decodedPdr.critical_high.value_u8);
3864 EXPECT_EQ(20, decodedPdr.critical_low.value_u8);
3865 EXPECT_EQ(90, decodedPdr.fatal_high.value_u8);
3866 EXPECT_EQ(10, decodedPdr.fatal_low.value_u8);
3867}
3868
3869TEST(decodeNumericSensorPdrData, Sint8Test)
3870{
3871 std::vector<uint8_t> pdr1{
3872 0x1,
3873 0x0,
3874 0x0,
3875 0x0, // record handle
3876 0x1, // PDRHeaderVersion
3877 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3878 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303879 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303880 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3881 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
3882 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303883 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303884 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303885 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303886 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303887 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303888 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303889 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303890 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303891 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303892 0x1,
3893 0x0, // containerID=1
3894 PLDM_NO_INIT, // sensorInit
3895 false, // sensorAuxiliaryNamesPDR
3896 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3897 0, // unitModifier
3898 0, // rateUnit
3899 0, // baseOEMUnitHandle
3900 0, // auxUnit
3901 0, // auxUnitModifier
3902 0, // auxRateUnit
3903 0, // rel
3904 0, // auxOEMUnitHandle
3905 true, // isLinear
3906 PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
3907 0,
3908 0,
3909 0,
3910 0, // resolution
3911 0,
3912 0,
3913 0,
3914 0, // offset
3915 0,
3916 0, // accuracy
3917 0, // plusTolerance
3918 0, // minusTolerance
3919 3, // hysteresis = 3
3920 0, // supportedThresholds
3921 0, // thresholdAndHysteresisVolatility
3922 0,
3923 0,
3924 0x80,
3925 0x3f, // stateTransistionInterval=1.0
3926 0,
3927 0,
3928 0x80,
3929 0x3f, // updateInverval=1.0
3930 0x64, // maxReadable = 100
3931 0x9c, // minReadable = -100
3932 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
3933 0, // rangeFieldsupport
3934 0, // nominalValue = 0
3935 5, // normalMax = 5
3936 0xfb, // normalMin = -5
3937 10, // warningHigh = 10
3938 0xf6, // warningLow = -10
3939 20, // criticalHigh = 20
3940 0xec, // criticalLow = -20
3941 30, // fatalHigh = 30
3942 0xe2 // fatalLow = -30
3943 };
3944
3945 struct pldm_numeric_sensor_value_pdr decodedPdr;
3946 auto rc =
3947 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3948 EXPECT_EQ(PLDM_SUCCESS, rc);
3949
3950 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT8, decodedPdr.sensor_data_size);
3951 EXPECT_EQ(100, decodedPdr.max_readable.value_s8);
3952 EXPECT_EQ(-100, decodedPdr.min_readable.value_s8);
3953 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
3954 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
3955 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
3956 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
3957 EXPECT_EQ(10, decodedPdr.warning_high.value_s8);
3958 EXPECT_EQ(-10, decodedPdr.warning_low.value_s8);
3959 EXPECT_EQ(20, decodedPdr.critical_high.value_s8);
3960 EXPECT_EQ(-20, decodedPdr.critical_low.value_s8);
3961 EXPECT_EQ(30, decodedPdr.fatal_high.value_s8);
3962 EXPECT_EQ(-30, decodedPdr.fatal_low.value_s8);
3963}
3964
3965TEST(decodeNumericSensorPdrData, Uint16Test)
3966{
3967 std::vector<uint8_t> pdr1{
3968 0x1,
3969 0x0,
3970 0x0,
3971 0x0, // record handle
3972 0x1, // PDRHeaderVersion
3973 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3974 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303975 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303976 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3977 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
3978 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09303979 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303980 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303981 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303982 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303983 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303984 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303985 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303986 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303987 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303988 0x1,
3989 0x0, // containerID=1
3990 PLDM_NO_INIT, // sensorInit
3991 false, // sensorAuxiliaryNamesPDR
3992 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3993 0, // unitModifier
3994 0, // rateUnit
3995 0, // baseOEMUnitHandle
3996 0, // auxUnit
3997 0, // auxUnitModifier
3998 0, // auxRateUnit
3999 0, // rel
4000 0, // auxOEMUnitHandle
4001 true, // isLinear
4002 PLDM_SENSOR_DATA_SIZE_UINT16, // sensorDataSize
4003 0,
4004 0,
4005 0,
4006 0, // resolution
4007 0,
4008 0,
4009 0,
4010 0, // offset
4011 0,
4012 0, // accuracy
4013 0, // plusTolerance
4014 0, // minusTolerance
4015 3,
4016 0, // hysteresis = 3
4017 0, // supportedThresholds
4018 0, // thresholdAndHysteresisVolatility
4019 0,
4020 0,
4021 0x80,
4022 0x3f, // stateTransistionInterval=1.0
4023 0,
4024 0,
4025 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09304026 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304027 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304028 0x10, // maxReadable = 4096
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304029 0,
4030 0, // minReadable = 0
4031 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
4032 0, // rangeFieldsupport
4033 0x88,
Andrew Jeffery01731472023-06-13 10:33:18 +09304034 0x13, // nominalValue = 5,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304035 0x70,
Andrew Jeffery01731472023-06-13 10:33:18 +09304036 0x17, // normalMax = 6,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304037 0xa0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304038 0x0f, // normalMin = 4,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304039 0x58,
Andrew Jeffery01731472023-06-13 10:33:18 +09304040 0x1b, // warningHigh = 7,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304041 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09304042 0x0b, // warningLow = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304043 0x40,
Andrew Jeffery01731472023-06-13 10:33:18 +09304044 0x1f, // criticalHigh = 8,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304045 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304046 0x07, // criticalLow = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304047 0x28,
Andrew Jeffery01731472023-06-13 10:33:18 +09304048 0x23, // fatalHigh = 9,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304049 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09304050 0x03 // fatalLow = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304051 };
4052
4053 struct pldm_numeric_sensor_value_pdr decodedPdr;
4054 auto rc =
4055 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4056 EXPECT_EQ(PLDM_SUCCESS, rc);
4057
4058 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT16, decodedPdr.sensor_data_size);
4059 EXPECT_EQ(4096, decodedPdr.max_readable.value_u16);
4060 EXPECT_EQ(0, decodedPdr.min_readable.value_u16);
4061 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
4062 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
4063 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
4064 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
4065 EXPECT_EQ(7000, decodedPdr.warning_high.value_u16);
4066 EXPECT_EQ(3000, decodedPdr.warning_low.value_u16);
4067 EXPECT_EQ(8000, decodedPdr.critical_high.value_u16);
4068 EXPECT_EQ(2000, decodedPdr.critical_low.value_u16);
4069 EXPECT_EQ(9000, decodedPdr.fatal_high.value_u16);
4070 EXPECT_EQ(1000, decodedPdr.fatal_low.value_u16);
4071}
4072
4073TEST(decodeNumericSensorPdrData, Sint16Test)
4074{
4075 std::vector<uint8_t> pdr1{
4076 0x1,
4077 0x0,
4078 0x0,
4079 0x0, // record handle
4080 0x1, // PDRHeaderVersion
4081 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4082 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304083 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304084 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4085 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
4086 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09304087 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304088 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304089 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304090 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304091 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304092 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304093 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304094 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304095 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304096 0x1,
4097 0x0, // containerID=1
4098 PLDM_NO_INIT, // sensorInit
4099 false, // sensorAuxiliaryNamesPDR
4100 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4101 0, // unitModifier
4102 0, // rateUnit
4103 0, // baseOEMUnitHandle
4104 0, // auxUnit
4105 0, // auxUnitModifier
4106 0, // auxRateUnit
4107 0, // rel
4108 0, // auxOEMUnitHandle
4109 true, // isLinear
4110 PLDM_SENSOR_DATA_SIZE_SINT16, // sensorDataSize
4111 0,
4112 0,
4113 0,
4114 0, // resolution
4115 0,
4116 0,
4117 0,
4118 0, // offset
4119 0,
4120 0, // accuracy
4121 0, // plusTolerance
4122 0, // minusTolerance
4123 3,
4124 0, // hysteresis
4125 0, // supportedThresholds
4126 0, // thresholdAndHysteresisVolatility
4127 0,
4128 0,
4129 0x80,
4130 0x3f, // stateTransistionInterval=1.0
4131 0,
4132 0,
4133 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09304134 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304135 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09304136 0x03, // maxReadable = 1000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304137 0x18,
4138 0xfc, // minReadable = -1000
4139 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
4140 0, // rangeFieldsupport
4141 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304142 0, // nominalValue = 0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304143 0xf4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304144 0x01, // normalMax = 500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304145 0x0c,
Andrew Jeffery01731472023-06-13 10:33:18 +09304146 0xfe, // normalMin = -500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304147 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09304148 0x03, // warningHigh = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304149 0x18,
Andrew Jeffery01731472023-06-13 10:33:18 +09304150 0xfc, // warningLow = -1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304151 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304152 0x07, // criticalHigh = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304153 0x30,
Andrew Jeffery01731472023-06-13 10:33:18 +09304154 0xf8, // criticalLow = -2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304155 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09304156 0x0b, // fatalHigh = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304157 0x48,
Andrew Jeffery01731472023-06-13 10:33:18 +09304158 0xf4 // fatalLow = -3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304159 };
4160
4161 struct pldm_numeric_sensor_value_pdr decodedPdr;
4162 auto rc =
4163 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4164 EXPECT_EQ(PLDM_SUCCESS, rc);
4165
4166 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT16, decodedPdr.sensor_data_size);
4167 EXPECT_EQ(1000, decodedPdr.max_readable.value_s16);
4168 EXPECT_EQ(-1000, decodedPdr.min_readable.value_s16);
4169 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
4170 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
4171 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
4172 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
4173 EXPECT_EQ(1000, decodedPdr.warning_high.value_s16);
4174 EXPECT_EQ(-1000, decodedPdr.warning_low.value_s16);
4175 EXPECT_EQ(2000, decodedPdr.critical_high.value_s16);
4176 EXPECT_EQ(-2000, decodedPdr.critical_low.value_s16);
4177 EXPECT_EQ(3000, decodedPdr.fatal_high.value_s16);
4178 EXPECT_EQ(-3000, decodedPdr.fatal_low.value_s16);
4179}
4180
4181TEST(decodeNumericSensorPdrData, Uint32Test)
4182{
4183 std::vector<uint8_t> pdr1{
4184 0x1,
4185 0x0,
4186 0x0,
4187 0x0, // record handle
4188 0x1, // PDRHeaderVersion
4189 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4190 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304191 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304192 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4193 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4194 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304195 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304196 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304197 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304198 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304199 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304200 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304201 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304202 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304203 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304204 0x1,
4205 0x0, // containerID=1
4206 PLDM_NO_INIT, // sensorInit
4207 false, // sensorAuxiliaryNamesPDR
4208 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4209 0, // unitModifier
4210 0, // rateUnit
4211 0, // baseOEMUnitHandle
4212 0, // auxUnit
4213 0, // auxUnitModifier
4214 0, // auxRateUnit
4215 0, // rel
4216 0, // auxOEMUnitHandle
4217 true, // isLinear
4218 PLDM_SENSOR_DATA_SIZE_UINT32, // sensorDataSize
4219 0,
4220 0,
4221 0,
4222 0, // resolution
4223 0,
4224 0,
4225 0,
4226 0, // offset
4227 0,
4228 0, // accuracy
4229 0, // plusTolerance
4230 0, // minusTolerance
4231 3,
4232 0,
4233 0,
4234 0, // hysteresis
4235 0, // supportedThresholds
4236 0, // thresholdAndHysteresisVolatility
4237 0,
4238 0,
4239 0x80,
4240 0x3f, // stateTransistionInterval=1.0
4241 0,
4242 0,
4243 0x80,
4244 0x3f, // updateInverval=1.0
4245 0,
4246 0x10,
4247 0,
4248 0, // maxReadable = 4096
4249 0,
4250 0,
4251 0,
4252 0, // minReadable = 0
4253 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
4254 0, // rangeFieldsupport
4255 0x40,
4256 0x4b,
4257 0x4c,
4258 0x00, // nominalValue = 5,000,000
4259 0x80,
4260 0x8d,
4261 0x5b,
4262 0x00, // normalMax = 6,000,000
4263 0x00,
4264 0x09,
4265 0x3d,
4266 0x00, // normalMin = 4,000,000
4267 0xc0,
4268 0xcf,
4269 0x6a,
4270 0x00, // warningHigh = 7,000,000
4271 0xc0,
4272 0xc6,
4273 0x2d,
4274 0x00, // warningLow = 3,000,000
4275 0x00,
4276 0x12,
4277 0x7a,
4278 0x00, // criticalHigh = 8,000,000
4279 0x80,
4280 0x84,
4281 0x1e,
4282 0x00, // criticalLow = 2,000,000
4283 0x40,
4284 0x54,
4285 0x89,
4286 0x00, // fatalHigh = 9,000,000
4287 0x40,
4288 0x42,
4289 0x0f,
4290 0x00 // fatalLow = 1,000,000
4291 };
4292
4293 struct pldm_numeric_sensor_value_pdr decodedPdr;
4294 auto rc =
4295 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4296 EXPECT_EQ(PLDM_SUCCESS, rc);
4297
4298 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT32, decodedPdr.sensor_data_size);
4299 EXPECT_EQ(4096, decodedPdr.max_readable.value_u32);
4300 EXPECT_EQ(0, decodedPdr.min_readable.value_u32);
4301 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
4302 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
4303 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
4304 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
4305 EXPECT_EQ(7000000, decodedPdr.warning_high.value_u32);
4306 EXPECT_EQ(3000000, decodedPdr.warning_low.value_u32);
4307 EXPECT_EQ(8000000, decodedPdr.critical_high.value_u32);
4308 EXPECT_EQ(2000000, decodedPdr.critical_low.value_u32);
4309 EXPECT_EQ(9000000, decodedPdr.fatal_high.value_u32);
4310 EXPECT_EQ(1000000, decodedPdr.fatal_low.value_u32);
4311}
4312
4313TEST(decodeNumericSensorPdrData, Sint32Test)
4314{
4315 std::vector<uint8_t> pdr1{
4316 0x1,
4317 0x0,
4318 0x0,
4319 0x0, // record handle
4320 0x1, // PDRHeaderVersion
4321 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4322 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304323 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304324 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4325 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4326 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304327 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304328 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304329 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304330 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304331 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304332 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304333 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304334 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304335 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304336 0x1,
4337 0x0, // containerID=1
4338 PLDM_NO_INIT, // sensorInit
4339 false, // sensorAuxiliaryNamesPDR
4340 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4341 0, // unitModifier
4342 0, // rateUnit
4343 0, // baseOEMUnitHandle
4344 0, // auxUnit
4345 0, // auxUnitModifier
4346 0, // auxRateUnit
4347 0, // rel
4348 0, // auxOEMUnitHandle
4349 true, // isLinear
4350 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
4351 0,
4352 0,
4353 0,
4354 0, // resolution
4355 0,
4356 0,
4357 0,
4358 0, // offset
4359 0,
4360 0, // accuracy
4361 0, // plusTolerance
4362 0, // minusTolerance
4363 3,
4364 0,
4365 0,
4366 0, // hysteresis
4367 0, // supportedThresholds
4368 0, // thresholdAndHysteresisVolatility
4369 0,
4370 0,
4371 0x80,
4372 0x3f, // stateTransistionInterval=1.0
4373 0,
4374 0,
4375 0x80,
4376 0x3f, // updateInverval=1.0
4377 0xa0,
4378 0x86,
4379 0x01,
4380 0x00, // maxReadable = 100000
4381 0x60,
4382 0x79,
4383 0xfe,
4384 0xff, // minReadable = -10000
4385 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
4386 0, // rangeFieldsupport
4387 0,
4388 0,
4389 0,
4390 0, // nominalValue = 0
4391 0x20,
4392 0xa1,
4393 0x07,
4394 0x00, // normalMax = 500,000
4395 0xe0,
4396 0x5e,
4397 0xf8,
4398 0xff, // normalMin = -500,000
4399 0x40,
4400 0x42,
4401 0x0f,
4402 0x00, // warningHigh = 1,000,000
4403 0xc0,
4404 0xbd,
4405 0xf0,
4406 0xff, // warningLow = -1,000,000
4407 0x80,
4408 0x84,
4409 0x1e,
4410 0x00, // criticalHigh = 2,000,000
4411 0x80,
4412 0x7b,
4413 0xe1,
4414 0xff, // criticalLow = -2,000,000
4415 0xc0,
4416 0xc6,
4417 0x2d,
4418 0x00, // fatalHigh = 3,000,000
4419 0x40,
4420 0x39,
4421 0xd2,
4422 0xff // fatalLow = -3,000,000
4423 };
4424
4425 struct pldm_numeric_sensor_value_pdr decodedPdr;
4426 auto rc =
4427 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4428 EXPECT_EQ(PLDM_SUCCESS, rc);
4429
4430 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
4431 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
4432 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
4433 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
4434 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
4435 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
4436 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
4437 EXPECT_EQ(1000000, decodedPdr.warning_high.value_s32);
4438 EXPECT_EQ(-1000000, decodedPdr.warning_low.value_s32);
4439 EXPECT_EQ(2000000, decodedPdr.critical_high.value_s32);
4440 EXPECT_EQ(-2000000, decodedPdr.critical_low.value_s32);
4441 EXPECT_EQ(3000000, decodedPdr.fatal_high.value_s32);
4442 EXPECT_EQ(-3000000, decodedPdr.fatal_low.value_s32);
4443}
4444
4445TEST(decodeNumericSensorPdrData, Real32Test)
4446{
4447 std::vector<uint8_t> pdr1{
4448 0x1,
4449 0x0,
4450 0x0,
4451 0x0, // record handle
4452 0x1, // PDRHeaderVersion
4453 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4454 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304455 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304456 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4457 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4458 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304459 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304460 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304461 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304462 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304463 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304464 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304465 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304466 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304467 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304468 0x1,
4469 0x0, // containerID=1
4470 PLDM_NO_INIT, // sensorInit
4471 false, // sensorAuxiliaryNamesPDR
4472 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4473 0, // unitModifier
4474 0, // rateUnit
4475 0, // baseOEMUnitHandle
4476 0, // auxUnit
4477 0, // auxUnitModifier
4478 0, // auxRateUnit
4479 0, // rel
4480 0, // auxOEMUnitHandle
4481 true, // isLinear
4482 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
4483 0,
4484 0,
4485 0,
4486 0, // resolution
4487 0,
4488 0,
4489 0,
4490 0, // offset
4491 0,
4492 0, // accuracy
4493 0, // plusTolerance
4494 0, // minusTolerance
4495 3,
4496 0,
4497 0,
4498 0, // hysteresis
4499 0, // supportedThresholds
4500 0, // thresholdAndHysteresisVolatility
4501 0,
4502 0,
4503 0x80,
4504 0x3f, // stateTransistionInterval=1.0
4505 0,
4506 0,
4507 0x80,
4508 0x3f, // updateInverval=1.0
4509 0xa0,
4510 0x86,
4511 0x01,
4512 0x00, // maxReadable = 100000
4513 0x60,
4514 0x79,
4515 0xfe,
4516 0xff, // minReadable = -10000
4517 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
4518 0, // rangeFieldsupport
4519 0,
4520 0,
4521 0,
4522 0, // nominalValue = 0.0
4523 0x33,
4524 0x33,
4525 0x48,
4526 0x42, // normalMax = 50.05
4527 0x33,
4528 0x33,
4529 0x48,
4530 0xc2, // normalMin = -50.05
4531 0x83,
4532 0x00,
4533 0xc8,
4534 0x42, // warningHigh = 100.001
4535 0x83,
4536 0x00,
4537 0xc8,
4538 0xc2, // warningLow = -100.001
4539 0x83,
4540 0x00,
4541 0x48,
4542 0x43, // criticalHigh = 200.002
4543 0x83,
4544 0x00,
4545 0x48,
4546 0xc3, // criticalLow = -200.002
4547 0x62,
4548 0x00,
4549 0x96,
4550 0x43, // fatalHigh = 300.003
4551 0x62,
4552 0x00,
4553 0x96,
4554 0xc3 // fatalLow = -300.003
4555 };
4556
4557 struct pldm_numeric_sensor_value_pdr decodedPdr;
4558 auto rc =
4559 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4560 EXPECT_EQ(PLDM_SUCCESS, rc);
4561
4562 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
4563 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
4564 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
4565 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
4566 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
4567 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
4568 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
4569 EXPECT_FLOAT_EQ(100.001f, decodedPdr.warning_high.value_f32);
4570 EXPECT_FLOAT_EQ(-100.001f, decodedPdr.warning_low.value_f32);
4571 EXPECT_FLOAT_EQ(200.002f, decodedPdr.critical_high.value_f32);
4572 EXPECT_FLOAT_EQ(-200.002f, decodedPdr.critical_low.value_f32);
4573 EXPECT_FLOAT_EQ(300.003f, decodedPdr.fatal_high.value_f32);
4574 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.fatal_low.value_f32);
4575}
4576
4577TEST(decodeNumericSensorPdrDataDeathTest, InvalidSizeTest)
4578{
4579 // A corrupted PDR. The data after plusTolerance missed.
4580 std::vector<uint8_t> pdr1{
4581 0x1,
4582 0x0,
4583 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304584 0x0, // record handle
4585 0x1, // PDRHeaderVersion
4586 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304587 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304588 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304589 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09304590 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304591 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304592 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304593 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304594 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304595 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304596 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304597 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304598 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304599 0x1,
4600 0x0, // containerID=1
4601 PLDM_NO_INIT, // sensorInit
4602 false, // sensorAuxiliaryNamesPDR
4603 2, // baseUint(2)=degrees C
4604 0, // unitModifier
4605 0, // rateUnit
4606 0, // baseOEMUnitHandle
4607 0, // auxUnit
4608 0, // auxUnitModifier
4609 0, // auxRateUnit
4610 0, // rel
4611 0, // auxOEMUnitHandle
4612 true, // isLinear
4613 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
4614 0,
4615 0,
4616 0,
4617 0, // resolution
4618 0,
4619 0,
4620 0,
4621 0, // offset
4622 0,
4623 0, // accuracy
4624 0 // plusTolerance
4625 };
4626
4627 struct pldm_numeric_sensor_value_pdr decodedPdr;
4628 int rc =
4629 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4630 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
4631}
Thu Nguyend4878cd2023-11-09 10:18:33 +07004632
Andrew Jeffery688be622024-05-23 11:22:51 +09304633#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004634TEST(decodeNumericEffecterPdrData, Uint8Test)
4635{
4636 std::vector<uint8_t> pdr1{
4637 0x1,
4638 0x0,
4639 0x0,
4640 0x0, // record handle
4641 0x1, // PDRHeaderVersion
4642 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4643 0x0,
4644 0x0, // recordChangeNumber
4645 PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
4646 0, // dataLength
4647 0,
4648 0, // PLDMTerminusHandle
4649 0x1,
4650 0x0, // effecterID=1
4651 PLDM_ENTITY_POWER_SUPPLY,
4652 0, // entityType=Power Supply(120)
4653 1,
4654 0, // entityInstanceNumber
4655 1,
4656 0, // containerID=1
4657 0x2,
4658 0x0, // effecter_semantic_id=2
4659 PLDM_NO_INIT, // effecterInit
4660 false, // effecterAuxiliaryNames
4661 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4662 0, // unitModifier
4663 0, // rateUnit
4664 0, // baseOEMUnitHandle
4665 0, // auxUnit
4666 0, // auxUnitModifier
4667 4, // auxRateUnit
4668 0, // auxOEMUnitHandle
4669 true, // isLinear
4670 PLDM_EFFECTER_DATA_SIZE_UINT8, // effecterDataSize
4671 0,
4672 0,
4673 0xc0,
4674 0x3f, // resolution=1.5
4675 0,
4676 0,
4677 0x80,
4678 0x3f, // offset=1.0
4679 0,
4680 0, // accuracy
4681 0, // plusTolerance
4682 0, // minusTolerance
4683 0,
4684 0,
4685 0x80,
4686 0x3f, // stateTransistionInterval=1.0
4687 0,
4688 0,
4689 0x80,
4690 0x3f, // transition_interval=1.0
4691 255, // maxSetdable
4692 0, // minSetable
4693 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
4694 0x1f, // rangeFieldsupport
4695 50, // nominalValue = 50
4696 60, // normalMax = 60
4697 40, // normalMin = 40
4698 90, // rated_max = 90
4699 10 // rated_min = 10
4700 };
4701
4702 struct pldm_numeric_effecter_value_pdr decodedPdr;
4703 auto rc =
4704 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4705 EXPECT_EQ(PLDM_SUCCESS, rc);
4706 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
4707 EXPECT_EQ(1, decodedPdr.hdr.version);
4708 EXPECT_EQ(PLDM_NUMERIC_EFFECTER_PDR, decodedPdr.hdr.type);
4709 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
4710 EXPECT_EQ(PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH, decodedPdr.hdr.length);
4711 EXPECT_EQ(1, decodedPdr.effecter_id);
4712 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
4713 EXPECT_EQ(1, decodedPdr.entity_instance);
4714 EXPECT_EQ(1, decodedPdr.container_id);
4715 EXPECT_EQ(2, decodedPdr.effecter_semantic_id);
4716 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.effecter_init);
4717 EXPECT_EQ(false, decodedPdr.effecter_auxiliary_names);
4718 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
4719 EXPECT_EQ(0, decodedPdr.unit_modifier);
4720 EXPECT_EQ(0, decodedPdr.rate_unit);
4721 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
4722 EXPECT_EQ(0, decodedPdr.aux_unit);
4723 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
4724 EXPECT_EQ(4, decodedPdr.aux_rate_unit);
4725 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
4726 EXPECT_EQ(true, decodedPdr.is_linear);
4727 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT8, decodedPdr.effecter_data_size);
4728 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
4729 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
4730 EXPECT_EQ(0, decodedPdr.accuracy);
4731 EXPECT_EQ(0, decodedPdr.plus_tolerance);
4732 EXPECT_EQ(0, decodedPdr.minus_tolerance);
4733 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
4734 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
4735 EXPECT_EQ(255, decodedPdr.max_settable.value_u8);
4736 EXPECT_EQ(0, decodedPdr.min_settable.value_u8);
4737 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
4738 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4739 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
4740 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
4741 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
4742 EXPECT_EQ(90, decodedPdr.rated_max.value_u8);
4743 EXPECT_EQ(10, decodedPdr.rated_min.value_u8);
4744}
Andrew Jeffery688be622024-05-23 11:22:51 +09304745#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004746
Andrew Jeffery688be622024-05-23 11:22:51 +09304747#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004748TEST(decodeNumericEffecterPdrData, Sint8Test)
4749{
4750 std::vector<uint8_t> pdr1{
4751 0x1,
4752 0x0,
4753 0x0,
4754 0x0, // record handle
4755 0x1, // PDRHeaderVersion
4756 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4757 0x0,
4758 0x0, // recordChangeNumber
4759 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4760 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH +
4761 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
4762 0, // dataLength
4763 0,
4764 0, // PLDMTerminusHandle
4765 0x1,
4766 0x0, // effecterID=1
4767 PLDM_ENTITY_POWER_SUPPLY,
4768 0, // entityType=Power Supply(120)
4769 1,
4770 0, // entityInstanceNumber
4771 0x1,
4772 0x0, // containerID=1
4773 0x2,
4774 0x0, // effecter_semantic_id=2
4775 PLDM_NO_INIT, // effecterInit
4776 false, // effecterAuxiliaryNames
4777 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4778 0, // unitModifier
4779 0, // rateUnit
4780 0, // baseOEMUnitHandle
4781 0, // auxUnit
4782 0, // auxUnitModifier
4783 0, // auxRateUnit
4784 0, // auxOEMUnitHandle
4785 true, // isLinear
4786 PLDM_RANGE_FIELD_FORMAT_SINT8, // effecterDataSize
4787 0,
4788 0,
4789 0,
4790 0, // resolution
4791 0,
4792 0,
4793 0,
4794 0, // offset
4795 0,
4796 0, // accuracy
4797 0, // plusTolerance
4798 0, // minusTolerance
4799 0,
4800 0,
4801 0x80,
4802 0x3f, // stateTransistionInterval=1.0
4803 0,
4804 0,
4805 0x80,
4806 0x3f, // transition_interval=1.0
4807 0x64, // maxSetdable = 100
4808 0x9c, // minSetable = -100
4809 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
4810 0x1f, // rangeFieldsupport
4811 0, // nominalValue = 0
4812 5, // normalMax = 5
4813 0xfb, // normalMin = -5
4814 30, // rated_max = 30
4815 0xe2 // rated_min = -30
4816 };
4817
4818 struct pldm_numeric_effecter_value_pdr decodedPdr;
4819 auto rc =
4820 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4821 EXPECT_EQ(PLDM_SUCCESS, rc);
4822
4823 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT8, decodedPdr.effecter_data_size);
4824 EXPECT_EQ(100, decodedPdr.max_settable.value_s8);
4825 EXPECT_EQ(-100, decodedPdr.min_settable.value_s8);
4826 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
4827 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4828 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
4829 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
4830 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
4831 EXPECT_EQ(30, decodedPdr.rated_max.value_s8);
4832 EXPECT_EQ(-30, decodedPdr.rated_min.value_s8);
4833}
Andrew Jeffery688be622024-05-23 11:22:51 +09304834#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004835
Andrew Jeffery688be622024-05-23 11:22:51 +09304836#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004837TEST(decodeNumericEffecterPdrData, Uint16Test)
4838{
4839 std::vector<uint8_t> pdr1{
4840 0x1,
4841 0x0,
4842 0x0,
4843 0x0, // record handle
4844 0x1, // PDRHeaderVersion
4845 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4846 0x0,
4847 0x0, // recordChangeNumber
4848 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4849 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4850 2 +
4851 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4852 0, // dataLength
4853 0,
4854 0, // PLDMTerminusHandle
4855 0x1,
4856 0x0, // effecterID=1
4857 PLDM_ENTITY_POWER_SUPPLY,
4858 0, // entityType=Power Supply(120)
4859 1,
4860 0, // entityInstanceNumber
4861 0x1,
4862 0x0, // containerID=1
4863 0x2,
4864 0x0, // effecter_semantic_id=2
4865 PLDM_NO_INIT, // effecterInit
4866 false, // effecterAuxiliaryNames
4867 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4868 0, // unitModifier
4869 0, // rateUnit
4870 0, // baseOEMUnitHandle
4871 0, // auxUnit
4872 0, // auxUnitModifier
4873 0, // auxRateUnit
4874 0, // auxOEMUnitHandle
4875 true, // isLinear
4876 PLDM_EFFECTER_DATA_SIZE_UINT16, // effecterDataSize
4877 0,
4878 0,
4879 0,
4880 0, // resolution
4881 0,
4882 0,
4883 0,
4884 0, // offset
4885 0,
4886 0, // accuracy
4887 0, // plusTolerance
4888 0, // minusTolerance
4889 0,
4890 0,
4891 0x80,
4892 0x3f, // stateTransistionInterval=1.0
4893 0,
4894 0,
4895 0x80,
4896 0x3f, // transition_interval=1.0
4897 0,
4898 0x10, // maxSetdable = 4096
4899 0,
4900 0, // minSetable = 0
4901 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
4902 0x1f, // rangeFieldsupport
4903 0x88,
4904 0x13, // nominalValue = 5,000
4905 0x70,
4906 0x17, // normalMax = 6,000
4907 0xa0,
4908 0x0f, // normalMin = 4,000
4909 0x28,
4910 0x23, // rated_max = 9,000
4911 0xe8,
4912 0x03 // rated_min = 1,000
4913 };
4914
4915 struct pldm_numeric_effecter_value_pdr decodedPdr;
4916 auto rc =
4917 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4918 EXPECT_EQ(PLDM_SUCCESS, rc);
4919
4920 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT16, decodedPdr.effecter_data_size);
4921 EXPECT_EQ(4096, decodedPdr.max_settable.value_u16);
4922 EXPECT_EQ(0, decodedPdr.min_settable.value_u16);
4923 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
4924 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4925 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
4926 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
4927 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
4928 EXPECT_EQ(9000, decodedPdr.rated_max.value_u16);
4929 EXPECT_EQ(1000, decodedPdr.rated_min.value_u16);
4930}
Andrew Jeffery688be622024-05-23 11:22:51 +09304931#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004932
Andrew Jeffery688be622024-05-23 11:22:51 +09304933#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004934TEST(decodeNumericEffecterPdrData, Sint16Test)
4935{
4936 std::vector<uint8_t> pdr1{
4937 0x1,
4938 0x0,
4939 0x0,
4940 0x0, // record handle
4941 0x1, // PDRHeaderVersion
4942 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4943 0x0,
4944 0x0, // recordChangeNumber
4945 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4946 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4947 2 +
4948 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4949 0, // dataLength
4950 0,
4951 0, // PLDMTerminusHandle
4952 0x1,
4953 0x0, // effecterID=1
4954 PLDM_ENTITY_POWER_SUPPLY,
4955 0, // entityType=Power Supply(120)
4956 1,
4957 0, // entityInstanceNumber
4958 0x1,
4959 0x0, // containerID=1
4960 0x2,
4961 0x0, // effecter_semantic_id=2
4962 PLDM_NO_INIT, // effecterInit
4963 false, // effecterAuxiliaryNames
4964 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4965 0, // unitModifier
4966 0, // rateUnit
4967 0, // baseOEMUnitHandle
4968 0, // auxUnit
4969 0, // auxUnitModifier
4970 0, // auxRateUnit
4971 0, // auxOEMUnitHandle
4972 true, // isLinear
4973 PLDM_EFFECTER_DATA_SIZE_SINT16, // effecterDataSize
4974 0,
4975 0,
4976 0,
4977 0, // resolution
4978 0,
4979 0,
4980 0,
4981 0, // offset
4982 0,
4983 0, // accuracy
4984 0, // plusTolerance
4985 0, // minusTolerance
4986 0,
4987 0,
4988 0x80,
4989 0x3f, // stateTransistionInterval=1.0
4990 0,
4991 0,
4992 0x80,
4993 0x3f, // transition_interval=1.0
4994 0xe8,
4995 0x03, // maxSetdable = 1000
4996 0x18,
4997 0xfc, // minSetable = -1000
4998 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
4999 0x1f, // rangeFieldsupport
5000 0,
5001 0, // nominalValue = 0
5002 0xf4,
5003 0x01, // normalMax = 500
5004 0x0c,
5005 0xfe, // normalMin = -500
5006 0xb8,
5007 0x0b, // rated_max = 3,000
5008 0x48,
5009 0xf4 // rated_min = -3,000
5010 };
5011
5012 struct pldm_numeric_effecter_value_pdr decodedPdr;
5013 auto rc =
5014 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5015 EXPECT_EQ(PLDM_SUCCESS, rc);
5016
5017 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT16, decodedPdr.effecter_data_size);
5018 EXPECT_EQ(1000, decodedPdr.max_settable.value_s16);
5019 EXPECT_EQ(-1000, decodedPdr.min_settable.value_s16);
5020 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
5021 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5022 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
5023 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
5024 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
5025 EXPECT_EQ(3000, decodedPdr.rated_max.value_s16);
5026 EXPECT_EQ(-3000, decodedPdr.rated_min.value_s16);
5027}
Andrew Jeffery688be622024-05-23 11:22:51 +09305028#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07005029
Andrew Jeffery688be622024-05-23 11:22:51 +09305030#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07005031TEST(decodeNumericEffecterPdrData, Uint32Test)
5032{
5033 std::vector<uint8_t> pdr1{
5034 0x1,
5035 0x0,
5036 0x0,
5037 0x0, // record handle
5038 0x1, // PDRHeaderVersion
5039 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
5040 0x0,
5041 0x0, // recordChangeNumber
5042 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
5043 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
5044 4 +
5045 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
5046 0, // dataLength
5047 0,
5048 0, // PLDMTerminusHandle
5049 0x1,
5050 0x0, // effecterID=1
5051 PLDM_ENTITY_POWER_SUPPLY,
5052 0, // entityType=Power Supply(120)
5053 1,
5054 0, // entityInstanceNumber
5055 0x1,
5056 0x0, // containerID=1
5057 0x2,
5058 0x0, // effecter_semantic_id=2
5059 PLDM_NO_INIT, // effecterInit
5060 false, // effecterAuxiliaryNames
5061 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
5062 0, // unitModifier
5063 0, // rateUnit
5064 0, // baseOEMUnitHandle
5065 0, // auxUnit
5066 0, // auxUnitModifier
5067 0, // auxRateUnit
5068 0, // auxOEMUnitHandle
5069 true, // isLinear
5070 PLDM_EFFECTER_DATA_SIZE_UINT32, // effecterDataSize
5071 0,
5072 0,
5073 0,
5074 0, // resolution
5075 0,
5076 0,
5077 0,
5078 0, // offset
5079 0,
5080 0, // accuracy
5081 0, // plusTolerance
5082 0, // minusTolerance
5083 0,
5084 0,
5085 0x80,
5086 0x3f, // stateTransistionInterval=1.0
5087 0,
5088 0,
5089 0x80,
5090 0x3f, // transition_interval=1.0
5091 0,
5092 0x10,
5093 0,
5094 0, // maxSetdable = 4096
5095 0,
5096 0,
5097 0,
5098 0, // minSetable = 0
5099 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
5100 0x1f, // rangeFieldsupport
5101 0x40,
5102 0x4b,
5103 0x4c,
5104 0x00, // nominalValue = 5,000,000
5105 0x80,
5106 0x8d,
5107 0x5b,
5108 0x00, // normalMax = 6,000,000
5109 0x00,
5110 0x09,
5111 0x3d,
5112 0x00, // normalMin = 4,000,000
5113 0x40,
5114 0x54,
5115 0x89,
5116 0x00, // rated_max = 9,000,000
5117 0x40,
5118 0x42,
5119 0x0f,
5120 0x00 // rated_min = 1,000,000
5121 };
5122
5123 struct pldm_numeric_effecter_value_pdr decodedPdr;
5124 auto rc =
5125 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5126 EXPECT_EQ(PLDM_SUCCESS, rc);
5127
5128 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT32, decodedPdr.effecter_data_size);
5129 EXPECT_EQ(4096, decodedPdr.max_settable.value_u32);
5130 EXPECT_EQ(0, decodedPdr.min_settable.value_u32);
5131 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
5132 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5133 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
5134 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
5135 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
5136 EXPECT_EQ(9000000, decodedPdr.rated_max.value_u32);
5137 EXPECT_EQ(1000000, decodedPdr.rated_min.value_u32);
5138}
Andrew Jeffery688be622024-05-23 11:22:51 +09305139#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07005140
Andrew Jeffery688be622024-05-23 11:22:51 +09305141#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07005142TEST(decodeNumericEffecterPdrData, Sint32Test)
5143{
5144 std::vector<uint8_t> pdr1{
5145 0x1,
5146 0x0,
5147 0x0,
5148 0x0, // record handle
5149 0x1, // PDRHeaderVersion
5150 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
5151 0x0,
5152 0x0, // recordChangeNumber
5153 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
5154 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
5155 4 +
5156 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
5157 0, // dataLength
5158 0,
5159 0, // PLDMTerminusHandle
5160 0x1,
5161 0x0, // effecterID=1
5162 PLDM_ENTITY_POWER_SUPPLY,
5163 0, // entityType=Power Supply(120)
5164 1,
5165 0, // entityInstanceNumber
5166 0x1,
5167 0x0, // containerID=1
5168 0x2,
5169 0x0, // effecter_semantic_id=2
5170 PLDM_NO_INIT, // effecterInit
5171 false, // effecterAuxiliaryNames
5172 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
5173 0, // unitModifier
5174 0, // rateUnit
5175 0, // baseOEMUnitHandle
5176 0, // auxUnit
5177 0, // auxUnitModifier
5178 0, // auxRateUnit
5179 0, // auxOEMUnitHandle
5180 true, // isLinear
5181 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
5182 0,
5183 0,
5184 0,
5185 0, // resolution
5186 0,
5187 0,
5188 0,
5189 0, // offset
5190 0,
5191 0, // accuracy
5192 0, // plusTolerance
5193 0, // minusTolerance
5194 0,
5195 0,
5196 0x80,
5197 0x3f, // stateTransistionInterval=1.0
5198 0,
5199 0,
5200 0x80,
5201 0x3f, // transition_interval=1.0
5202 0xa0,
5203 0x86,
5204 0x01,
5205 0x00, // maxSetdable = 100000
5206 0x60,
5207 0x79,
5208 0xfe,
5209 0xff, // minSetable = -10000
5210 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
5211 0x1f, // rangeFieldsupport
5212 0,
5213 0,
5214 0,
5215 0, // nominalValue = 0
5216 0x20,
5217 0xa1,
5218 0x07,
5219 0x00, // normalMax = 500,000
5220 0xe0,
5221 0x5e,
5222 0xf8,
5223 0xff, // normalMin = -500,000
5224 0xc0,
5225 0xc6,
5226 0x2d,
5227 0x00, // rated_max = 3,000,000
5228 0x40,
5229 0x39,
5230 0xd2,
5231 0xff // rated_min = -3,000,000
5232 };
5233
5234 struct pldm_numeric_effecter_value_pdr decodedPdr;
5235 auto rc =
5236 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5237 EXPECT_EQ(PLDM_SUCCESS, rc);
5238 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
5239 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
5240 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
5241 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
5242 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5243 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
5244 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
5245 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
5246 EXPECT_EQ(3000000, decodedPdr.rated_max.value_s32);
5247 EXPECT_EQ(-3000000, decodedPdr.rated_min.value_s32);
5248}
Andrew Jeffery688be622024-05-23 11:22:51 +09305249#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07005250
Andrew Jeffery688be622024-05-23 11:22:51 +09305251#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07005252TEST(decodeNumericEffecterPdrData, Real32Test)
5253{
5254 std::vector<uint8_t> pdr1{
5255 0x1,
5256 0x0,
5257 0x0,
5258 0x0, // record handle
5259 0x1, // PDRHeaderVersion
5260 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
5261 0x0,
5262 0x0, // recordChangeNumber
5263 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
5264 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
5265 4 +
5266 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
5267 0, // dataLength
5268 0,
5269
5270 0, // PLDMTerminusHandle
5271 0x1,
5272 0x0, // effecterID=1
5273 PLDM_ENTITY_POWER_SUPPLY,
5274 0, // entityType=Power Supply(120)
5275 1,
5276 0, // entityInstanceNumber
5277 0x1,
5278 0x0, // containerID=1
5279 0x2,
5280 0x0, // effecter_semantic_id=2
5281 PLDM_NO_INIT, // effecterInit
5282 false, // effecterAuxiliaryNames
5283 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
5284 0, // unitModifier
5285 0, // rateUnit
5286 0, // baseOEMUnitHandle
5287 0, // auxUnit
5288 0, // auxUnitModifier
5289 0, // auxRateUnit
5290 0, // auxOEMUnitHandle
5291 true, // isLinear
5292 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
5293 0,
5294 0,
5295 0,
5296 0, // resolution
5297 0,
5298 0,
5299 0,
5300 0, // offset
5301 0,
5302 0, // accuracy
5303 0, // plusTolerance
5304 0, // minusTolerance
5305 0,
5306 0,
5307 0x80,
5308 0x3f, // stateTransistionInterval=1.0
5309 0,
5310 0,
5311 0x80,
5312 0x3f, // transition_interval=1.0
5313 0xa0,
5314 0x86,
5315 0x01,
5316 0x00, // maxSetdable = 100000
5317 0x60,
5318 0x79,
5319 0xfe,
5320 0xff, // minSetable = -10000
5321 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
5322 0x1f, // rangeFieldsupport
5323 0,
5324 0,
5325 0,
5326 0, // nominalValue = 0.0
5327 0x33,
5328 0x33,
5329 0x48,
5330 0x42, // normalMax = 50.05
5331 0x33,
5332 0x33,
5333 0x48,
5334 0xc2, // normalMin = -50.05
5335 0x62,
5336 0x00,
5337 0x96,
5338 0x43, // rated_max = 300.003
5339 0x62,
5340 0x00,
5341 0x96,
5342 0xc3 // rated_min = -300.003
5343 };
5344
5345 struct pldm_numeric_effecter_value_pdr decodedPdr;
5346 auto rc =
5347 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5348 EXPECT_EQ(PLDM_SUCCESS, rc);
5349
5350 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
5351 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
5352 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
5353 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
5354 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
5355 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
5356 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5357 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
5358 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
5359 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
5360 EXPECT_FLOAT_EQ(300.003f, decodedPdr.rated_max.value_f32);
5361 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.rated_min.value_f32);
5362}
Andrew Jeffery688be622024-05-23 11:22:51 +09305363#endif
Tal Yacobia6fa5552024-05-05 16:57:38 +03005364
Tal Yacobia6fa5552024-05-05 16:57:38 +03005365TEST(GetStateEffecterStates, testEncodeAndDecodeRequest)
5366{
5367 std::array<uint8_t, hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
5368 requestMsg{};
5369
5370 constexpr std::array<uint8_t,
5371 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
5372 expectedRequestMsg{
5373 {0x80, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES, 1, 0xab}};
5374
5375 constexpr uint16_t effecter_id = 0xab01;
5376
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305377 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005378 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
5379
5380 auto rc = encode_get_state_effecter_states_req(
5381 0, effecter_id, request, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
5382
5383 EXPECT_EQ(rc, PLDM_SUCCESS);
5384 EXPECT_EQ(requestMsg, expectedRequestMsg);
5385
5386 uint16_t ret_effecter_id;
5387
5388 rc = decode_get_state_effecter_states_req(
5389 request, requestMsg.size() - hdrSize, &ret_effecter_id);
5390
5391 EXPECT_EQ(rc, PLDM_SUCCESS);
5392 EXPECT_EQ(effecter_id, ret_effecter_id);
5393
5394 // Test invalid length decode request
5395
5396 rc = decode_get_state_effecter_states_req(
5397 request, requestMsg.size() - hdrSize - 1, &ret_effecter_id);
5398
Tal Yacobif490a382024-05-31 09:57:36 +03005399 EXPECT_EQ(rc, -EOVERFLOW);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005400}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005401
Tal Yacobia6fa5552024-05-05 16:57:38 +03005402TEST(GetStateEffecterStates, testBadEncodeRequest)
5403{
5404 std::vector<uint8_t> requestMsg(hdrSize +
5405 PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
5406
5407 auto rc = encode_get_state_effecter_states_req(
5408 0, 0, nullptr, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
Tal Yacobif490a382024-05-31 09:57:36 +03005409 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005410}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005411
Tal Yacobia6fa5552024-05-05 16:57:38 +03005412TEST(GetStateEffecterStates, testBadDecodeRequest)
5413{
5414 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
5415 requestMsg{};
5416
5417 auto rc = decode_get_state_effecter_states_req(
5418 nullptr, requestMsg.size() - hdrSize, nullptr);
5419
Tal Yacobif490a382024-05-31 09:57:36 +03005420 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005421}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005422
Tal Yacobia6fa5552024-05-05 16:57:38 +03005423TEST(GetStateEffecterStates, testEncodeAndDecodeResponse)
5424{
5425 constexpr uint8_t comp_effecterCnt = 0x2;
5426 constexpr uint8_t completionCode = 0;
5427 std::array<uint8_t,
5428 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
5429 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * comp_effecterCnt>
5430 expectedResponseMsg{{0, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES,
5431 completionCode, comp_effecterCnt,
5432 EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2,
5433 EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}};
5434
5435 decltype(expectedResponseMsg) responseMsg{};
5436
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305437 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005438 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
5439
5440 std::array<get_effecter_state_field, comp_effecterCnt> stateField{
5441 {{EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2},
5442 {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}}};
5443
5444 struct pldm_get_state_effecter_states_resp resp_fields
5445 {
5446 PLDM_SUCCESS, comp_effecterCnt,
5447 {
5448 stateField[0], stateField[1]
5449 }
5450 };
5451
5452 auto rc = encode_get_state_effecter_states_resp(
5453 0, &resp_fields, response, responseMsg.size() - hdrSize);
5454
5455 EXPECT_EQ(rc, PLDM_SUCCESS);
5456 EXPECT_EQ(expectedResponseMsg, responseMsg);
5457
5458 struct pldm_get_state_effecter_states_resp ret_resp_fields;
5459
5460 rc = decode_get_state_effecter_states_resp(
5461 response, responseMsg.size() - hdrSize, &ret_resp_fields);
5462
5463 EXPECT_EQ(rc, PLDM_SUCCESS);
5464 EXPECT_EQ(completionCode, ret_resp_fields.completion_code);
5465 EXPECT_EQ(comp_effecterCnt, ret_resp_fields.comp_effecter_count);
5466 EXPECT_EQ(stateField[0].effecter_op_state,
5467 ret_resp_fields.field[0].effecter_op_state);
5468 EXPECT_EQ(stateField[0].pending_state,
5469 ret_resp_fields.field[0].pending_state);
5470 EXPECT_EQ(stateField[0].present_state,
5471 ret_resp_fields.field[0].present_state);
5472 EXPECT_EQ(stateField[1].effecter_op_state,
5473 ret_resp_fields.field[1].effecter_op_state);
5474 EXPECT_EQ(stateField[1].pending_state,
5475 ret_resp_fields.field[1].pending_state);
5476 EXPECT_EQ(stateField[1].present_state,
5477 ret_resp_fields.field[1].present_state);
5478
5479 // Test invalid length decode
5480
5481 rc = decode_get_state_effecter_states_resp(
5482 response,
5483 responseMsg.size() - hdrSize + PLDM_GET_EFFECTER_STATE_FIELD_SIZE,
5484 &ret_resp_fields);
5485
Tal Yacobif490a382024-05-31 09:57:36 +03005486 EXPECT_EQ(rc, -EBADMSG);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005487}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005488
Tal Yacobia6fa5552024-05-05 16:57:38 +03005489TEST(GetStateEffecterStates, testBadEncodeResponse)
5490{
5491 struct pldm_get_state_effecter_states_resp resp
5492 {
5493 PLDM_SUCCESS, 0,
5494 {
5495 }
5496 };
5497 auto rc = decode_get_state_effecter_states_resp(nullptr, 0, &resp);
5498
Tal Yacobif490a382024-05-31 09:57:36 +03005499 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005500}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005501
Tal Yacobia6fa5552024-05-05 16:57:38 +03005502TEST(GetStateEffecterStates, testBadDecodeResponse)
5503{
5504 std::array<uint8_t, hdrSize +
5505 PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
5506 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * 2>
5507 responseMsg{};
5508
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305509 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005510 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
5511
5512 auto rc = decode_get_state_effecter_states_resp(
5513 response, responseMsg.size() - hdrSize, nullptr);
5514
Tal Yacobif490a382024-05-31 09:57:36 +03005515 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005516}
Thu Nguyendacfa352024-06-22 09:53:15 +00005517
5518[[maybe_unused]] static size_t str16len(char16_t* startptr)
5519{
5520 char16_t* endptr = startptr;
5521 while (*endptr)
5522 {
5523 endptr++;
5524 }
5525 return endptr - startptr;
5526}
5527
Thu Nguyendacfa352024-06-22 09:53:15 +00005528TEST(decodeEntityAuxNamePdrData, GoodTest)
5529{
5530 std::vector<uint8_t> pdr1{
5531 // Common PDR Header
5532 0x1, 0x0, 0x0, 0x0, // record handle
5533 0x1, // PDRHeaderVersion
5534 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5535 0x1,
5536 0x0, // recordChangeNumber
5537 0x27,
5538 0, // dataLength
5539 /* Entity Auxiliary Names PDR Data*/
5540 3,
5541 0, // entityType system software
5542 0x1,
5543 0x0, // Entity instance number =1
5544 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5545 0, // Overal system
5546 0, // shared Name Count one name only
5547 03, // nameStringCount
5548 0x65, 0x6e, 0x00, // Language Tag "en"
5549 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5550 0x66, 0x6e, 0x00, // Language Tag "en"
5551 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5552 0x67, 0x6e, 0x00, // Language Tag "en"
5553 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5554 };
5555
5556 const char expectTag0[] = {0x65, 0x6e, 0x00};
5557 const char expectTag1[] = {0x66, 0x6e, 0x00};
5558 const char expectTag2[] = {0x67, 0x6e, 0x00};
5559 const char expectName0[] = {0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00};
5560 const char expectName1[] = {0x00, 0x53, 0x00, 0x31, 0x00, 0x00};
5561 const char expectName2[] = {0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00};
5562 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5563 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5564 auto names_size = pdr1.size() - names_offset;
5565 size_t length = 0;
5566
5567 size_t decodedPdrSize =
5568 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5569 auto decodedPdr =
5570 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5571 EXPECT_NE(nullptr, decodedPdr);
5572
5573 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5574 decodedPdr, decodedPdrSize);
5575
5576 EXPECT_EQ(0, rc);
5577 EXPECT_EQ(1, decodedPdr->hdr.record_handle);
5578 EXPECT_EQ(1, decodedPdr->hdr.version);
5579 EXPECT_EQ(PLDM_ENTITY_AUXILIARY_NAMES_PDR, decodedPdr->hdr.type);
5580 EXPECT_EQ(1, decodedPdr->hdr.record_change_num);
5581 EXPECT_EQ(pdr1.size() - sizeof(struct pldm_pdr_hdr),
5582 decodedPdr->hdr.length);
5583 EXPECT_EQ(3, decodedPdr->container.entity_type);
5584 EXPECT_EQ(1, decodedPdr->container.entity_instance_num);
5585 EXPECT_EQ(PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5586 decodedPdr->container.entity_container_id);
5587 EXPECT_EQ(0, decodedPdr->shared_name_count);
5588 EXPECT_EQ(3, decodedPdr->name_string_count);
5589
5590 decodedPdr->names = (struct pldm_entity_auxiliary_name*)calloc(
5591 decodedPdr->name_string_count,
5592 sizeof(struct pldm_entity_auxiliary_name));
5593 EXPECT_NE(nullptr, decodedPdr->names);
5594
5595 rc = decode_pldm_entity_auxiliary_names_pdr_index(decodedPdr);
5596 EXPECT_EQ(0, rc);
5597
5598 length = strlen(decodedPdr->names[0].tag);
5599 EXPECT_EQ(strlen(expectTag0), length);
5600 EXPECT_EQ(strncmp(expectTag0, decodedPdr->names[0].tag, length + 1), 0);
5601
5602 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5603 ASSERT_EQ(0,
5604 (uintptr_t)decodedPdr->names[0].name & (alignof(char16_t) - 1));
5605 // NOLINTEND(clang-analyzer-unix.Malloc)
5606 length = str16len((char16_t*)decodedPdr->names[0].name);
5607 EXPECT_EQ(str16len((char16_t*)expectName0), length);
5608 EXPECT_EQ(3, str16len((char16_t*)expectName0));
5609 EXPECT_EQ(memcmp(expectName0, decodedPdr->names[0].name,
5610 sizeof(char16_t) * (length + 1)),
5611 0);
5612
5613 length = strlen(decodedPdr->names[1].tag);
5614 EXPECT_EQ(strlen(expectTag1), length);
5615 EXPECT_EQ(strncmp(expectTag1, decodedPdr->names[1].tag, length + 1), 0);
5616
5617 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5618 ASSERT_EQ(0,
5619 (uintptr_t)decodedPdr->names[1].name & (alignof(char16_t) - 1));
5620 // NOLINTEND(clang-analyzer-unix.Malloc)
5621 length = str16len((char16_t*)decodedPdr->names[1].name);
5622 EXPECT_EQ(str16len((char16_t*)expectName1), length);
5623 EXPECT_EQ(2, str16len((char16_t*)expectName1));
5624 EXPECT_EQ(memcmp(expectName1, decodedPdr->names[1].name,
5625 sizeof(char16_t) * (length + 1)),
5626 0);
5627
5628 length = strlen(decodedPdr->names[2].tag);
5629 EXPECT_EQ(strlen(expectTag2), length);
5630 EXPECT_EQ(strncmp(expectTag2, decodedPdr->names[2].tag, length + 1), 0);
5631
5632 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5633 ASSERT_EQ(0,
5634 (uintptr_t)decodedPdr->names[2].name & (alignof(char16_t) - 1));
5635 // NOLINTEND(clang-analyzer-unix.Malloc)
5636 length = str16len((char16_t*)decodedPdr->names[2].name);
5637 EXPECT_EQ(str16len((char16_t*)expectName2), length);
5638 EXPECT_EQ(3, str16len((char16_t*)expectName2));
5639 EXPECT_EQ(memcmp(expectName2, decodedPdr->names[2].name,
5640 sizeof(char16_t) * (length + 1)),
5641 0);
5642
5643 free(decodedPdr->names);
5644 free(decodedPdr);
5645}
Thu Nguyendacfa352024-06-22 09:53:15 +00005646
Thu Nguyendacfa352024-06-22 09:53:15 +00005647TEST(decodeEntityAuxNamePdrData, BadTest)
5648{
5649 std::vector<uint8_t> pdr1{
5650 // Common PDR Header
5651 0x1, 0x0, 0x0, 0x0, // record handle
5652 0x1, // PDRHeaderVersion
5653 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5654 0x1,
5655 0x0, // recordChangeNumber
5656 0x25, // correct size is 0x27, input invalid size
5657 0, // dataLength
5658 /* Entity Auxiliary Names PDR Data*/
5659 3,
5660 0, // entityType system software
5661 0x1,
5662 0x0, // Entity instance number =1
5663 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5664 0, // Overal system
5665 0, // shared Name Count one name only
5666 0, // Invalid nameStringCount
5667 0x65, 0x6e, 0x00, // Language Tag "en"
5668 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5669 0x66, 0x6e, 0x00, // Language Tag "en"
5670 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5671 0x67, 0x6e, 0x00, // Language Tag "en"
5672 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5673 };
5674
5675 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5676 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5677 auto names_size = pdr1.size() - names_offset;
5678
5679 size_t decodedPdrSize =
5680 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5681 auto decodedPdr =
5682 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5683
5684 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5685 decodedPdr, decodedPdrSize);
5686
5687 EXPECT_EQ(-EBADMSG, rc);
5688 free(decodedPdr);
5689}
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005690
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005691TEST(PlatformEventMessage, testGoodCperEventDataDecodeRequest)
5692{
5693 constexpr const size_t eventDataSize = 4;
5694 constexpr const size_t eventSize =
5695 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize;
5696 std::array<uint8_t, eventSize> eventData{
5697 0x1, // format version
5698 0x0, // format type
5699 0x4, 0x0, // event data length
5700 0x44, 0x33, 0x22, 0x11 // data
5701 };
5702
5703 uint8_t expectedFormatVersion = 1;
5704 uint8_t expectedFormatType = 0;
5705 uint16_t expectedEventDataLength = 4;
5706 uint8_t expectCperEventData[] = {0x44, 0x33, 0x22, 0x11};
5707
5708 size_t cperEventSize =
5709 sizeof(struct pldm_platform_cper_event) + eventDataSize;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305710 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005711 auto cper_event = reinterpret_cast<struct pldm_platform_cper_event*>(
5712 malloc(cperEventSize));
5713
Thu Nguyen02903032024-09-03 06:39:50 +00005714 auto rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305715 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005716 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5717 cper_event, cperEventSize);
5718
5719 EXPECT_EQ(rc, 0);
5720 EXPECT_EQ(cper_event->format_version, expectedFormatVersion);
5721 EXPECT_EQ(cper_event->format_type, expectedFormatType);
5722 EXPECT_EQ(cper_event->event_data_length, expectedEventDataLength);
5723
5724 auto cperEventData = pldm_platform_cper_event_event_data(cper_event);
5725 EXPECT_NE(cperEventData, nullptr);
5726 if (cperEventData)
5727 {
5728 EXPECT_EQ(0, memcmp(expectCperEventData, cperEventData,
5729 expectedEventDataLength));
5730 }
5731
5732 free(cper_event);
5733}
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005734
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005735TEST(PlatformEventMessage, testBadCperEventDataDecodeRequest)
5736{
5737
5738 constexpr const size_t eventDataSize = 4;
5739 constexpr const size_t eventSize =
5740 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize;
5741 std::array<uint8_t, eventSize> eventData{
5742 0x1, // format version
5743 0x0, // format type
5744 0x4, 0x0, // event data length
5745 0x44, 0x33, 0x22, 0x11 // data
5746 };
5747
5748 size_t cperEventSize =
5749 sizeof(struct pldm_platform_cper_event) + eventDataSize;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305750 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005751 auto cperEvent = reinterpret_cast<struct pldm_platform_cper_event*>(
5752 malloc(cperEventSize));
5753
Thu Nguyen02903032024-09-03 06:39:50 +00005754 auto rc = decode_pldm_platform_cper_event(NULL, eventData.size(), cperEvent,
5755 cperEventSize);
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005756 EXPECT_EQ(rc, -EINVAL);
5757
Thu Nguyen02903032024-09-03 06:39:50 +00005758 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305759 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005760 reinterpret_cast<const void*>(eventData.data()), eventData.size(), NULL,
5761 cperEventSize);
5762 EXPECT_EQ(rc, -EINVAL);
5763
Thu Nguyen02903032024-09-03 06:39:50 +00005764 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305765 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005766 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
5767 cperEvent, cperEventSize);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00005768 EXPECT_EQ(rc, -EOVERFLOW);
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005769
Thu Nguyen02903032024-09-03 06:39:50 +00005770 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305771 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005772 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
5773 cperEvent, cperEventSize - 1);
5774 EXPECT_EQ(rc, -EOVERFLOW);
5775
Thu Nguyen02903032024-09-03 06:39:50 +00005776 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305777 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005778 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
5779 cperEvent, cperEventSize + 1);
5780 EXPECT_EQ(rc, 0);
5781
5782 // Invalid CPER Event Format Type
5783 eventData[1] = 0x2;
Thu Nguyen02903032024-09-03 06:39:50 +00005784 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305785 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005786 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5787 cperEvent, cperEventSize);
5788
5789 EXPECT_EQ(rc, -EPROTO);
5790
5791 // Invalid cper event data size
5792 eventData[1] = 0x1;
5793 eventData[2] = 3;
Thu Nguyen02903032024-09-03 06:39:50 +00005794 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305795 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005796 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5797 cperEvent, cperEventSize);
5798
5799 EXPECT_EQ(rc, -EBADMSG);
5800
5801 eventData[2] = 5;
Thu Nguyen02903032024-09-03 06:39:50 +00005802 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305803 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005804 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5805 cperEvent, cperEventSize);
5806
5807 EXPECT_EQ(rc, -EOVERFLOW);
5808
5809 free(cperEvent);
5810}