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