blob: 0cf7c397f2fc50ef273db9c0378dae69914500e0 [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
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302128#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002129TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataDecodeRequest)
2130{
2131 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2132 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2133 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2134 eventData{
2135 0x1, // version
2136 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302137 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002138 };
2139
2140 uint8_t formatVersion = 0x01;
2141 uint16_t eventID = 0x7788;
2142 uint32_t dataTransferHandle = 0x11223344;
2143
2144 uint8_t retFormatVersion;
2145 uint16_t reteventID;
2146 uint32_t retDataTransferHandle;
2147
2148 auto rc = decode_pldm_message_poll_event_data(
2149 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
2150 &retFormatVersion, &reteventID, &retDataTransferHandle);
2151
2152 EXPECT_EQ(rc, PLDM_SUCCESS);
2153 EXPECT_EQ(retFormatVersion, formatVersion);
2154 EXPECT_EQ(reteventID, eventID);
2155 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
2156}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302157#endif
Dung Cao7c250342022-11-16 22:40:37 +07002158
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302159#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002160TEST(PlatformEventMessage, testBadPldmMsgPollEventDataDecodeRequest)
2161{
2162
2163 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2164 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2165 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2166 eventData{
2167 0x1, // version
2168 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302169 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002170 };
2171
2172 uint8_t retFormatVersion;
2173 uint16_t reteventID;
2174 uint32_t retDataTransferHandle;
2175
2176 auto rc = decode_pldm_message_poll_event_data(
2177 NULL, eventData.size(), &retFormatVersion, &reteventID,
2178 &retDataTransferHandle);
2179 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2180
2181 rc = decode_pldm_message_poll_event_data(
2182 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
2183 &retFormatVersion, &reteventID, &retDataTransferHandle);
2184 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2185
2186 // Event id is 0x0000
2187 eventData[1] = 0x00;
2188 eventData[2] = 0x00;
2189 rc = decode_pldm_message_poll_event_data(
2190 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
2191 &retFormatVersion, &reteventID, &retDataTransferHandle);
2192
2193 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2194
2195 // Event id is 0xffff
2196 eventData[1] = 0xff;
2197 eventData[2] = 0xff;
2198 rc = decode_pldm_message_poll_event_data(
2199 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
2200 &retFormatVersion, &reteventID, &retDataTransferHandle);
2201
2202 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2203}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302204#endif
Dung Cao7c250342022-11-16 22:40:37 +07002205
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302206#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002207TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataEncode)
2208{
2209 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2210 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2211 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2212 eventData{};
2213
2214 uint8_t formatVersion = 0x01;
2215 uint16_t eventID = 0x7788;
2216 uint32_t dataTransferHandle = 0x11223344;
2217
2218 int rc = encode_pldm_message_poll_event_data(
2219 formatVersion, eventID, dataTransferHandle,
2220 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
2221
2222 EXPECT_EQ(rc, PLDM_SUCCESS);
2223
2224 struct pldm_msgbuf _buf;
2225 struct pldm_msgbuf* buf = &_buf;
2226
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302227 rc = pldm_msgbuf_init_cc(buf, PLDM_MSG_POLL_EVENT_LENGTH,
2228 reinterpret_cast<uint8_t*>(eventData.data()),
2229 eventData.size());
Dung Cao7c250342022-11-16 22:40:37 +07002230 EXPECT_EQ(rc, PLDM_SUCCESS);
2231
2232 uint8_t retFormatVersion;
2233 uint16_t reteventID;
2234 uint32_t retDataTransferHandle;
2235
2236 EXPECT_EQ(pldm_msgbuf_extract_uint8(buf, &retFormatVersion), PLDM_SUCCESS);
2237 EXPECT_EQ(pldm_msgbuf_extract_uint16(buf, &reteventID), PLDM_SUCCESS);
2238 EXPECT_EQ(pldm_msgbuf_extract_uint32(buf, &retDataTransferHandle),
2239 PLDM_SUCCESS);
2240 EXPECT_EQ(retFormatVersion, formatVersion);
2241 EXPECT_EQ(reteventID, eventID);
2242 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
2243 EXPECT_EQ(pldm_msgbuf_destroy_consumed(buf), PLDM_SUCCESS);
2244}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302245#endif
Dung Cao7c250342022-11-16 22:40:37 +07002246
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302247#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002248TEST(PlatformEventMessage, testBadPldmMsgPollEventDataEncode)
2249{
2250 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2251 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2252 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2253 eventData{};
2254
2255 uint8_t formatVersion = 0x01;
2256 uint16_t eventID = 0x7788;
2257 uint32_t dataTransferHandle = 0x11223344;
2258
2259 int rc = encode_pldm_message_poll_event_data(
2260 formatVersion, eventID, dataTransferHandle, NULL, eventData.size());
2261 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2262
2263 eventID = 0x0000;
2264 rc = encode_pldm_message_poll_event_data(
2265 formatVersion, eventID, dataTransferHandle,
2266 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
2267 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2268
2269 eventID = 0xffff;
2270 rc = encode_pldm_message_poll_event_data(
2271 formatVersion, eventID, dataTransferHandle,
2272 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
2273 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2274}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302275#endif
Dung Cao7c250342022-11-16 22:40:37 +07002276
Andrew Jeffery9c766792022-08-10 23:12:49 +09302277TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
2278{
2279 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2280 eventDataArr{};
2281
2282 struct pldm_sensor_event_sensor_op_state* sensorData =
2283 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
2284 uint8_t presentState = PLDM_SENSOR_ENABLED;
2285 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
2286 sensorData->present_op_state = presentState;
2287 sensorData->previous_op_state = previousState;
2288
2289 uint8_t retPresentState;
2290 uint8_t retPreviousState;
2291 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
2292 eventDataArr.size(), &retPresentState,
2293 &retPreviousState);
2294 EXPECT_EQ(rc, PLDM_SUCCESS);
2295 EXPECT_EQ(retPresentState, presentState);
2296 EXPECT_EQ(retPreviousState, previousState);
2297}
2298
2299TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
2300{
2301 uint8_t presentOpState;
2302 uint8_t previousOpState;
2303 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
2304 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
2305 &previousOpState);
2306 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2307
2308 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2309 sensorData{};
2310 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2311 sensorDataLength + 1, &presentOpState,
2312 &previousOpState);
2313 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2314
2315 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2316 sensorDataLength, nullptr, &previousOpState);
2317 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2318}
2319
2320TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
2321{
2322 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2323 eventDataArr{};
2324
2325 struct pldm_sensor_event_state_sensor_state* sensorData =
2326 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
2327 uint8_t sensorOffset = 0x02;
2328 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2329 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2330 sensorData->sensor_offset = sensorOffset;
2331 sensorData->event_state = eventState;
2332 sensorData->previous_event_state = previousEventState;
2333
2334 uint8_t retSensorOffset;
2335 uint8_t retEventState;
2336 uint8_t retPreviousState;
2337 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2338 eventDataArr.size(), &retSensorOffset,
2339 &retEventState, &retPreviousState);
2340 EXPECT_EQ(rc, PLDM_SUCCESS);
2341 EXPECT_EQ(retSensorOffset, sensorOffset);
2342 EXPECT_EQ(retEventState, eventState);
2343 EXPECT_EQ(retPreviousState, previousEventState);
2344}
2345
2346TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
2347{
2348 uint8_t sensorOffset;
2349 uint8_t eventState;
2350 uint8_t previousEventState;
2351 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
2352 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
2353 &eventState, &previousEventState);
2354 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2355
2356 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2357 sensorData{};
2358 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2359 sensorDataLength - 1, &sensorOffset,
2360 &eventState, &previousEventState);
2361 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2362
2363 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2364 sensorDataLength, &sensorOffset, nullptr,
2365 &previousEventState);
2366 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2367}
2368
2369TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
2370{
2371 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2372 eventDataArr{};
2373 struct pldm_sensor_event_numeric_sensor_state* sensorData =
2374 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
2375
2376 size_t sensorDataLength =
2377 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
2378 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2379 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2380 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
2381 uint32_t presentReading = 305441741;
2382 sensorData->event_state = eventState;
2383 sensorData->previous_event_state = previousEventState;
2384 sensorData->sensor_data_size = sensorDataSize;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302385 {
2386 uint32_t presentReadingLE = htole32(presentReading);
2387 memcpy(&sensorData->present_reading, &presentReadingLE,
2388 sizeof(presentReadingLE));
2389 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302390
2391 uint8_t retEventState;
2392 uint8_t retPreviousEventState;
2393 uint8_t retSensorDataSize;
2394 uint32_t retPresentReading;
2395
2396 auto rc = decode_numeric_sensor_data(
2397 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
2398 &retEventState, &retPreviousEventState, &retSensorDataSize,
2399 &retPresentReading);
2400 EXPECT_EQ(rc, PLDM_SUCCESS);
2401 EXPECT_EQ(retEventState, eventState);
2402 EXPECT_EQ(retPreviousEventState, previousEventState);
2403 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2404 EXPECT_EQ(retPresentReading, presentReading);
2405
2406 int16_t presentReadingNew = -31432;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302407 {
2408 int16_t presentReadingNewLE = htole16(presentReadingNew);
2409 memcpy(&sensorData->present_reading, &presentReadingNewLE,
2410 sizeof(presentReadingNewLE));
2411 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302412 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
2413 sensorData->sensor_data_size = sensorDataSize;
2414 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
2415
2416 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2417 sensorDataLength, &retEventState,
2418 &retPreviousEventState, &retSensorDataSize,
2419 &retPresentReading);
2420 EXPECT_EQ(rc, PLDM_SUCCESS);
2421 EXPECT_EQ(retEventState, eventState);
2422 EXPECT_EQ(retPreviousEventState, previousEventState);
2423 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2424 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
2425}
2426
2427TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
2428{
2429 uint8_t eventState;
2430 uint8_t previousEventState;
2431 uint8_t sensorDataSize;
2432 uint32_t presentReading;
2433 size_t sensorDataLength =
2434 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
2435 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
2436 &previousEventState, &sensorDataSize,
2437 &presentReading);
2438 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2439
2440 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2441 sensorData{};
2442 rc = decode_numeric_sensor_data(
2443 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
2444 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2445 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2446
2447 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
2448 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
2449 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
2450 rc = decode_numeric_sensor_data(
2451 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2452 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2453 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2454
2455 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
2456 rc = decode_numeric_sensor_data(
2457 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2458 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2459 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302460}
2461
2462TEST(GetNumericEffecterValue, testGoodEncodeRequest)
2463{
2464 std::vector<uint8_t> requestMsg(hdrSize +
2465 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
2466
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002467 uint16_t effecter_id = 0xab01;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302468
2469 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2470
2471 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
2472
2473 struct pldm_get_numeric_effecter_value_req* req =
2474 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2475 request->payload);
2476
2477 EXPECT_EQ(rc, PLDM_SUCCESS);
2478 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
2479}
2480
2481TEST(GetNumericEffecterValue, testBadEncodeRequest)
2482{
2483 std::vector<uint8_t> requestMsg(
2484 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
2485
2486 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
2487 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2488}
2489
2490TEST(GetNumericEffecterValue, testGoodDecodeRequest)
2491{
2492 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2493 requestMsg{};
2494
2495 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2496 struct pldm_get_numeric_effecter_value_req* req =
2497 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2498 request->payload);
2499
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002500 uint16_t effecter_id = 0x12ab;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302501 req->effecter_id = htole16(effecter_id);
2502
2503 uint16_t reteffecter_id;
2504
2505 auto rc = decode_get_numeric_effecter_value_req(
2506 request, requestMsg.size() - hdrSize, &reteffecter_id);
2507
2508 EXPECT_EQ(rc, PLDM_SUCCESS);
2509 EXPECT_EQ(effecter_id, reteffecter_id);
2510}
2511
2512TEST(GetNumericEffecterValue, testBadDecodeRequest)
2513{
2514 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2515 requestMsg{};
2516
2517 auto rc = decode_get_numeric_effecter_value_req(
2518 nullptr, requestMsg.size() - hdrSize, nullptr);
2519
2520 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2521
2522 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2523 struct pldm_set_numeric_effecter_value_req* req =
2524 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
2525 request->payload);
2526
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002527 uint16_t effecter_id = 0x1a;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302528 req->effecter_id = htole16(effecter_id);
2529 uint16_t reteffecter_id;
2530
2531 rc = decode_get_numeric_effecter_value_req(
2532 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
2533
2534 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2535}
2536
2537TEST(GetNumericEffecterValue, testGoodEncodeResponse)
2538{
2539 uint8_t completionCode = 0;
2540 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
2541 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
2542 uint32_t pendingValue = 0x12345678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002543 uint32_t presentValue = 0xabcdef11;
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302544 uint32_t val_pending;
2545 uint32_t val_present;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302546
2547 std::array<uint8_t,
2548 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
2549 responseMsg{};
2550 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2551
2552 auto rc = encode_get_numeric_effecter_value_resp(
2553 0, completionCode, effecter_dataSize, effecter_operState,
2554 reinterpret_cast<uint8_t*>(&pendingValue),
2555 reinterpret_cast<uint8_t*>(&presentValue), response,
2556 responseMsg.size() - hdrSize);
2557
2558 struct pldm_get_numeric_effecter_value_resp* resp =
2559 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2560 response->payload);
2561
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302562 memcpy(&val_pending, &resp->pending_and_present_values[0],
2563 sizeof(val_pending));
2564 val_pending = le32toh(val_pending);
2565 memcpy(&val_present, &resp->pending_and_present_values[4],
2566 sizeof(val_present));
2567 val_present = le32toh(val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302568
2569 EXPECT_EQ(rc, PLDM_SUCCESS);
2570 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
2571 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302572 EXPECT_EQ(pendingValue, val_pending);
2573 EXPECT_EQ(presentValue, val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302574}
2575
2576TEST(GetNumericEffecterValue, testBadEncodeResponse)
2577{
2578 std::array<uint8_t,
2579 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
2580 responseMsg{};
2581 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2582
2583 uint8_t pendingValue = 0x01;
2584 uint8_t presentValue = 0x02;
2585
2586 auto rc = encode_get_numeric_effecter_value_resp(
2587 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
2588 responseMsg.size() - hdrSize);
2589 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2590
2591 rc = encode_get_numeric_effecter_value_resp(
2592 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
2593 reinterpret_cast<uint8_t*>(&presentValue), response,
2594 responseMsg.size() - hdrSize);
2595 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2596
2597 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2598 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
2599
2600 rc = encode_get_numeric_effecter_value_resp(
2601 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
2602 reinterpret_cast<uint8_t*>(&pendingValue),
2603 reinterpret_cast<uint8_t*>(&presentValue), response,
2604 responseMsg.size() - hdrSize);
2605 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2606}
2607
2608TEST(GetNumericEffecterValue, testGoodDecodeResponse)
2609{
2610 std::array<uint8_t,
2611 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
2612 responseMsg{};
2613
2614 uint8_t completionCode = 0;
2615 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
2616 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
2617 uint16_t pendingValue = 0x4321;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002618 uint16_t presentValue = 0xdcba;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302619
2620 uint8_t retcompletionCode;
2621 uint8_t reteffecter_dataSize;
2622 uint8_t reteffecter_operState;
2623 uint8_t retpendingValue[2];
2624 uint8_t retpresentValue[2];
2625
2626 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2627 struct pldm_get_numeric_effecter_value_resp* resp =
2628 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2629 response->payload);
2630
2631 resp->completion_code = completionCode;
2632 resp->effecter_data_size = effecter_dataSize;
2633 resp->effecter_oper_state = effecter_operState;
2634
2635 uint16_t pendingValue_le = htole16(pendingValue);
2636 memcpy(resp->pending_and_present_values, &pendingValue_le,
2637 sizeof(pendingValue_le));
2638 uint16_t presentValue_le = htole16(presentValue);
2639 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
2640 sizeof(presentValue_le));
2641
2642 auto rc = decode_get_numeric_effecter_value_resp(
2643 response, responseMsg.size() - hdrSize, &retcompletionCode,
2644 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
2645 retpresentValue);
2646
2647 EXPECT_EQ(rc, PLDM_SUCCESS);
2648 EXPECT_EQ(completionCode, retcompletionCode);
2649 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
2650 EXPECT_EQ(effecter_operState, reteffecter_operState);
2651 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
2652 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
2653}
2654
2655TEST(GetNumericEffecterValue, testBadDecodeResponse)
2656{
2657 std::array<uint8_t,
2658 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
2659 responseMsg{};
2660
2661 auto rc = decode_get_numeric_effecter_value_resp(
2662 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
2663 nullptr, nullptr);
2664
2665 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2666
2667 uint8_t completionCode = 0;
2668 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
2669 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
2670 uint16_t pendingValue = 0x5678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002671 uint16_t presentValue = 0xcdef;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302672
2673 uint8_t retcompletionCode;
2674 uint8_t reteffecter_dataSize;
2675 uint8_t reteffecter_operState;
2676 uint8_t retpendingValue[2];
2677 uint8_t retpresentValue[2];
2678
2679 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2680 struct pldm_get_numeric_effecter_value_resp* resp =
2681 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2682 response->payload);
2683
2684 resp->completion_code = completionCode;
2685 resp->effecter_data_size = effecter_dataSize;
2686 resp->effecter_oper_state = effecter_operState;
2687
2688 uint16_t pendingValue_le = htole16(pendingValue);
2689 memcpy(resp->pending_and_present_values, &pendingValue_le,
2690 sizeof(pendingValue_le));
2691 uint16_t presentValue_le = htole16(presentValue);
2692 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
2693 sizeof(presentValue_le));
2694
2695 rc = decode_get_numeric_effecter_value_resp(
2696 response, responseMsg.size() - hdrSize, &retcompletionCode,
2697 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
2698 retpresentValue);
2699
2700 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2701}
2702
2703TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
2704{
2705 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
2706 const uint8_t numberOfChangeRecords = 2;
2707 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
2708 const uint8_t numberOfChangeEntries1 = 2;
2709 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
2710 {0x00000000, 0x12345678}};
2711 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
2712 const uint8_t numberOfChangeEntries2 = 5;
2713 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
2714 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
2715 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
2716 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
2717 numberOfChangeRecords +
2718 (numberOfChangeEntries1 + numberOfChangeEntries2) *
2719 sizeof(uint32_t)>
2720 eventDataArr{};
2721
2722 struct pldm_pdr_repository_chg_event_data* eventData =
2723 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
2724 eventDataArr.data());
2725 eventData->event_data_format = eventDataFormat;
2726 eventData->number_of_change_records = numberOfChangeRecords;
2727 struct pldm_pdr_repository_change_record_data* changeRecord1 =
2728 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
2729 eventData->change_records);
2730 changeRecord1->event_data_operation = eventDataOperation1;
2731 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
2732 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
2733 changeRecordArr1.size() * sizeof(uint32_t));
2734 struct pldm_pdr_repository_change_record_data* changeRecord2 =
2735 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
2736 eventData->change_records +
2737 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
2738 (changeRecordArr1.size() * sizeof(uint32_t)));
2739 changeRecord2->event_data_operation = eventDataOperation2;
2740 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
2741 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
2742 changeRecordArr2.size() * sizeof(uint32_t));
2743
2744 uint8_t retEventDataFormat{};
2745 uint8_t retNumberOfChangeRecords{};
2746 size_t retChangeRecordDataOffset{0};
2747 auto rc = decode_pldm_pdr_repository_chg_event_data(
2748 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
2749 &retEventDataFormat, &retNumberOfChangeRecords,
2750 &retChangeRecordDataOffset);
2751 EXPECT_EQ(rc, PLDM_SUCCESS);
2752 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
2753 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
2754
2755 const uint8_t* changeRecordData =
2756 reinterpret_cast<const uint8_t*>(changeRecord1);
2757 size_t changeRecordDataSize =
2758 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
2759 uint8_t retEventDataOperation;
2760 uint8_t retNumberOfChangeEntries;
2761 size_t retChangeEntryDataOffset;
2762
2763 rc = decode_pldm_pdr_repository_change_record_data(
2764 reinterpret_cast<const uint8_t*>(changeRecordData),
2765 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
2766 &retChangeEntryDataOffset);
2767 EXPECT_EQ(rc, PLDM_SUCCESS);
2768 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
2769 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
2770 changeRecordData += retChangeEntryDataOffset;
2771 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
2772 sizeof(uint32_t) * retNumberOfChangeEntries));
2773
2774 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
2775 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
2776 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
2777 rc = decode_pldm_pdr_repository_change_record_data(
2778 reinterpret_cast<const uint8_t*>(changeRecordData),
2779 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
2780 &retChangeEntryDataOffset);
2781 EXPECT_EQ(rc, PLDM_SUCCESS);
2782 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
2783 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
2784 changeRecordData += retChangeEntryDataOffset;
2785 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
2786 sizeof(uint32_t) * retNumberOfChangeEntries));
2787}
2788
2789TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
2790{
2791 uint8_t eventDataFormat{};
2792 uint8_t numberOfChangeRecords{};
2793 size_t changeRecordDataOffset{};
2794 auto rc = decode_pldm_pdr_repository_chg_event_data(
2795 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
2796 &changeRecordDataOffset);
2797 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2798
2799 std::array<uint8_t, 2> eventData{};
2800 rc = decode_pldm_pdr_repository_chg_event_data(
2801 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
2802 &numberOfChangeRecords, &changeRecordDataOffset);
2803 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2804
2805 uint8_t eventDataOperation{};
2806 uint8_t numberOfChangeEntries{};
2807 size_t changeEntryDataOffset{};
2808 rc = decode_pldm_pdr_repository_change_record_data(
2809 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
2810 &changeEntryDataOffset);
2811 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2812
2813 std::array<uint8_t, 2> changeRecord{};
2814 rc = decode_pldm_pdr_repository_change_record_data(
2815 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
2816 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
2817 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2818}
2819
2820TEST(GetSensorReading, testGoodEncodeRequest)
2821{
2822 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2823 requestMsg{};
2824
2825 uint16_t sensorId = 0x1234;
2826 bool8_t rearmEventState = 0x01;
2827
2828 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2829 auto rc =
2830 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request);
2831
2832 struct pldm_get_sensor_reading_req* req =
2833 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2834
2835 EXPECT_EQ(rc, PLDM_SUCCESS);
2836 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
2837 EXPECT_EQ(rearmEventState, req->rearm_event_state);
2838}
2839
2840TEST(GetSensorReading, testBadEncodeRequest)
2841{
2842 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr);
2843
2844 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2845}
2846
2847TEST(GetSensorReading, testGoodDecodeRequest)
2848{
2849 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2850 requestMsg{};
2851
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002852 uint16_t sensorId = 0xabcd;
2853 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302854
2855 uint16_t retsensorId;
2856 bool8_t retrearmEventState;
2857
2858 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2859
2860 struct pldm_get_sensor_reading_req* req =
2861 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2862
2863 req->sensor_id = htole16(sensorId);
2864 req->rearm_event_state = rearmEventState;
2865
2866 auto rc =
2867 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize,
2868 &retsensorId, &retrearmEventState);
2869
2870 EXPECT_EQ(rc, PLDM_SUCCESS);
2871 EXPECT_EQ(sensorId, retsensorId);
2872 EXPECT_EQ(rearmEventState, retrearmEventState);
2873}
2874
2875TEST(GetSensorReading, testBadDecodeRequest)
2876{
2877 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2878 requestMsg{};
2879
2880 auto rc = decode_get_sensor_reading_req(
2881 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr);
2882 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2883
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002884 uint16_t sensorId = 0xabcd;
2885 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302886
2887 uint16_t retsensorId;
2888 bool8_t retrearmEventState;
2889
2890 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2891
2892 struct pldm_get_sensor_reading_req* req =
2893 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2894
2895 req->sensor_id = htole16(sensorId);
2896 req->rearm_event_state = rearmEventState;
2897
2898 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1,
2899 &retsensorId, &retrearmEventState);
2900
2901 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2902}
2903
2904TEST(GetSensorReading, testGoodEncodeResponse)
2905{
2906 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES>
2907 responseMsg{};
2908
2909 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2910
2911 uint8_t completionCode = 0;
2912 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2913 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED;
2914 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION;
2915 uint8_t presentState = PLDM_SENSOR_NORMAL;
2916 uint8_t previousState = PLDM_SENSOR_WARNING;
2917 uint8_t eventState = PLDM_SENSOR_UPPERWARNING;
2918 uint8_t presentReading = 0x21;
2919
2920 auto rc = encode_get_sensor_reading_resp(
2921 0, completionCode, sensor_dataSize, sensor_operationalState,
2922 sensor_event_messageEnable, presentState, previousState, eventState,
2923 reinterpret_cast<uint8_t*>(&presentReading), response,
2924 responseMsg.size() - hdrSize);
2925
2926 struct pldm_get_sensor_reading_resp* resp =
2927 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
2928 response->payload);
2929
2930 EXPECT_EQ(rc, PLDM_SUCCESS);
2931 EXPECT_EQ(completionCode, resp->completion_code);
2932 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size);
2933 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state);
2934 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable);
2935 EXPECT_EQ(presentState, resp->present_state);
2936 EXPECT_EQ(previousState, resp->previous_state);
2937 EXPECT_EQ(eventState, resp->event_state);
2938 EXPECT_EQ(presentReading,
2939 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0])));
2940}
2941
2942TEST(GetSensorReading, testBadEncodeResponse)
2943{
2944 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
2945 responseMsg{};
2946
2947 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2948
2949 uint8_t presentReading = 0x1;
2950
2951 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0,
2952 nullptr, nullptr,
2953 responseMsg.size() - hdrSize);
2954 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2955
2956 rc = encode_get_sensor_reading_resp(
2957 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1,
2958 reinterpret_cast<uint8_t*>(&presentReading), response,
2959 responseMsg.size() - hdrSize);
2960 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2961
2962 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2963
2964 rc = encode_get_sensor_reading_resp(
2965 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1,
2966 reinterpret_cast<uint8_t*>(&presentReading), response,
2967 responseMsg.size() - hdrSize);
2968 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2969}
2970
2971TEST(GetSensorReading, testGoodDecodeResponse)
2972{
2973 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
2974 responseMsg{};
2975
2976 uint8_t completionCode = 0;
2977 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
2978 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN;
2979 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED;
2980 uint8_t presentState = PLDM_SENSOR_CRITICAL;
2981 uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL;
2982 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002983 uint32_t presentReading = 0xabcdef11;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302984
2985 uint8_t retcompletionCode;
2986 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
2987 uint8_t retsensor_operationalState;
2988 uint8_t retsensor_event_messageEnable;
2989 uint8_t retpresentState;
2990 uint8_t retpreviousState;
2991 uint8_t reteventState;
2992 uint8_t retpresentReading[4];
2993
2994 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2995 struct pldm_get_sensor_reading_resp* resp =
2996 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
2997 response->payload);
2998
2999 resp->completion_code = completionCode;
3000 resp->sensor_data_size = sensor_dataSize;
3001 resp->sensor_operational_state = sensor_operationalState;
3002 resp->sensor_event_message_enable = sensor_event_messageEnable;
3003 resp->present_state = presentState;
3004 resp->previous_state = previousState;
3005 resp->event_state = eventState;
3006
3007 uint32_t presentReading_le = htole32(presentReading);
3008 memcpy(resp->present_reading, &presentReading_le,
3009 sizeof(presentReading_le));
3010
3011 auto rc = decode_get_sensor_reading_resp(
3012 response, responseMsg.size() - hdrSize, &retcompletionCode,
3013 &retsensor_dataSize, &retsensor_operationalState,
3014 &retsensor_event_messageEnable, &retpresentState, &retpreviousState,
3015 &reteventState, retpresentReading);
3016
3017 EXPECT_EQ(rc, PLDM_SUCCESS);
3018 EXPECT_EQ(completionCode, retcompletionCode);
3019 EXPECT_EQ(sensor_dataSize, retsensor_dataSize);
3020 EXPECT_EQ(sensor_operationalState, retsensor_operationalState);
3021 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable);
3022 EXPECT_EQ(presentState, retpresentState);
3023 EXPECT_EQ(previousState, retpreviousState);
3024 EXPECT_EQ(eventState, reteventState);
3025 EXPECT_EQ(presentReading,
3026 *(reinterpret_cast<uint32_t*>(retpresentReading)));
3027}
3028
3029TEST(GetSensorReading, testBadDecodeResponse)
3030{
3031 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1>
3032 responseMsg{};
3033
3034 auto rc = decode_get_sensor_reading_resp(
3035 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
3036 nullptr, nullptr, nullptr, nullptr, nullptr);
3037
3038 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3039
3040 uint8_t completionCode = 0;
3041 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3042 uint8_t sensor_operationalState = PLDM_SENSOR_INTEST;
3043 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED;
3044 uint8_t presentState = PLDM_SENSOR_FATAL;
3045 uint8_t previousState = PLDM_SENSOR_UPPERFATAL;
3046 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003047 uint8_t presentReading = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303048
3049 uint8_t retcompletionCode;
3050 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
3051 uint8_t retsensor_operationalState;
3052 uint8_t retsensor_event_messageEnable;
3053 uint8_t retpresent_state;
3054 uint8_t retprevious_state;
3055 uint8_t retevent_state;
3056 uint8_t retpresentReading;
3057
3058 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3059 struct pldm_get_sensor_reading_resp* resp =
3060 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3061 response->payload);
3062
3063 resp->completion_code = completionCode;
3064 resp->sensor_data_size = sensor_dataSize;
3065 resp->sensor_operational_state = sensor_operationalState;
3066 resp->sensor_event_message_enable = sensor_event_messageEnable;
3067 resp->present_state = presentState;
3068 resp->previous_state = previousState;
3069 resp->event_state = eventState;
3070 resp->present_reading[0] = presentReading;
3071
3072 rc = decode_get_sensor_reading_resp(
3073 response, responseMsg.size() - hdrSize, &retcompletionCode,
3074 &retsensor_dataSize, &retsensor_operationalState,
3075 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state,
3076 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading));
3077
3078 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3079}
3080
3081TEST(SetEventReceiver, testGoodEncodeRequest)
3082{
3083 uint8_t eventMessageGlobalEnable =
3084 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3085 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3086 uint8_t eventReceiverAddressInfo = 0x08;
3087 uint16_t heartbeatTimer = 0x78;
3088
3089 std::vector<uint8_t> requestMsg(hdrSize +
3090 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
3091 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3092
3093 auto rc = encode_set_event_receiver_req(
3094 0, eventMessageGlobalEnable, transportProtocolType,
3095 eventReceiverAddressInfo, heartbeatTimer, request);
3096
3097 EXPECT_EQ(rc, PLDM_SUCCESS);
3098 struct pldm_set_event_receiver_req* req =
3099 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3100 EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable);
3101 EXPECT_EQ(transportProtocolType, req->transport_protocol_type);
3102 EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info);
3103 EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer));
3104}
3105
3106TEST(SetEventReceiver, testBadEncodeRequest)
3107{
3108 uint8_t eventMessageGlobalEnable =
3109 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3110 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3111 uint8_t eventReceiverAddressInfo = 0x08;
3112 uint16_t heartbeatTimer = 0;
3113
3114 std::vector<uint8_t> requestMsg(hdrSize +
3115 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
3116 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3117
3118 auto rc = encode_set_event_receiver_req(
3119 0, eventMessageGlobalEnable, transportProtocolType,
3120 eventReceiverAddressInfo, heartbeatTimer, request);
3121
3122 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3123}
3124
3125TEST(SetEventReceiver, testGoodDecodeResponse)
3126{
3127 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3128 responseMsg{};
3129
3130 uint8_t retcompletion_code = 0;
3131 responseMsg[hdrSize] = PLDM_SUCCESS;
3132
3133 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3134 auto rc = decode_set_event_receiver_resp(
3135 response, responseMsg.size() - sizeof(pldm_msg_hdr),
3136 &retcompletion_code);
3137
3138 EXPECT_EQ(rc, PLDM_SUCCESS);
3139 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
3140}
3141
3142TEST(SetEventReceiver, testBadDecodeResponse)
3143{
3144 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3145 responseMsg{};
3146 uint8_t retcompletion_code = 0;
3147 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3148
3149 auto rc = decode_set_event_receiver_resp(
3150 response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL);
3151
3152 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3153
3154 rc = decode_set_event_receiver_resp(
3155 nullptr, responseMsg.size() - sizeof(pldm_msg_hdr),
3156 &retcompletion_code);
3157
3158 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3159}
3160
3161TEST(SetEventReceiver, testGoodEncodeResponse)
3162{
3163 std::array<uint8_t,
3164 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3165 responseMsg{};
3166 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3167 uint8_t completionCode = 0;
3168
3169 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response);
3170
3171 EXPECT_EQ(rc, PLDM_SUCCESS);
3172 EXPECT_EQ(completionCode, response->payload[0]);
3173}
3174
3175TEST(SetEventReceiver, testBadEncodeResponse)
3176{
3177 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL);
3178 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3179}
3180
3181TEST(SetEventReceiver, testGoodDecodeRequest)
3182{
3183
3184 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3185 requestMsg{};
3186
3187 uint8_t eventMessageGlobalEnable =
3188 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3189 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3190 uint8_t eventReceiverAddressInfo = 0x08;
3191 uint16_t heartbeatTimer = 0x78;
3192
3193 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3194 struct pldm_set_event_receiver_req* req =
3195 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3196
3197 req->event_message_global_enable = eventMessageGlobalEnable;
3198 req->transport_protocol_type = transportProtocolType;
3199 req->event_receiver_address_info = eventReceiverAddressInfo;
3200 req->heartbeat_timer = htole16(heartbeatTimer);
3201
3202 uint8_t reteventMessageGlobalEnable;
3203 uint8_t rettransportProtocolType;
3204 uint8_t reteventReceiverAddressInfo;
3205 uint16_t retheartbeatTimer;
3206 auto rc = decode_set_event_receiver_req(
3207 request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable,
3208 &rettransportProtocolType, &reteventReceiverAddressInfo,
3209 &retheartbeatTimer);
3210
3211 EXPECT_EQ(rc, PLDM_SUCCESS);
3212 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
3213 EXPECT_EQ(transportProtocolType, rettransportProtocolType);
3214 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
3215 EXPECT_EQ(heartbeatTimer, retheartbeatTimer);
3216}
3217
3218TEST(SetEventReceiver, testBadDecodeRequest)
3219{
3220 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3221 requestMsg{};
3222
3223 auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize,
3224 NULL, NULL, NULL, NULL);
3225 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3226
3227 uint8_t eventMessageGlobalEnable =
3228 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3229 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3230 uint8_t eventReceiverAddressInfo = 0x08;
3231 uint16_t heartbeatTimer = 0x78;
3232
3233 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3234 struct pldm_set_event_receiver_req* req =
3235 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3236
3237 req->event_message_global_enable = eventMessageGlobalEnable;
3238 req->transport_protocol_type = transportProtocolType;
3239 req->event_receiver_address_info = eventReceiverAddressInfo;
3240 req->heartbeat_timer = htole16(heartbeatTimer);
3241
3242 uint8_t reteventMessageGlobalEnable;
3243 uint8_t rettransportProtocolType;
3244 uint8_t reteventReceiverAddressInfo;
3245 uint16_t retheartbeatTimer;
3246 rc = decode_set_event_receiver_req(
3247 request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable,
3248 &rettransportProtocolType, &reteventReceiverAddressInfo,
3249 &retheartbeatTimer);
3250 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3251}
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303252
3253TEST(decodeNumericSensorPdrData, Uint8Test)
3254{
3255 std::vector<uint8_t> pdr1{
3256 0x1,
3257 0x0,
3258 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303259 0x0, // record handle
3260 0x1, // PDRHeaderVersion
3261 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303262 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303263 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303264 PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303265 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303266 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303267 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303268 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303269 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303270 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303271 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303272 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303273 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303274 1,
3275 0, // containerID=1
3276 PLDM_NO_INIT, // sensorInit
3277 false, // sensorAuxiliaryNamesPDR
3278 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3279 0, // unitModifier
3280 0, // rateUnit
3281 0, // baseOEMUnitHandle
3282 0, // auxUnit
3283 0, // auxUnitModifier
3284 0, // auxRateUnit
3285 0, // rel
3286 0, // auxOEMUnitHandle
3287 true, // isLinear
3288 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
3289 0,
3290 0,
3291 0xc0,
3292 0x3f, // resolution=1.5
3293 0,
3294 0,
3295 0x80,
3296 0x3f, // offset=1.0
3297 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303298 0, // accuracy
3299 0, // plusTolerance
3300 0, // minusTolerance
3301 3, // hysteresis = 3
3302 0, // supportedThresholds
3303 0, // thresholdAndHysteresisVolatility
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303304 0,
3305 0,
3306 0x80,
3307 0x3f, // stateTransistionInterval=1.0
3308 0,
3309 0,
3310 0x80,
3311 0x3f, // updateInverval=1.0
3312 255, // maxReadable
3313 0, // minReadable
3314 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
3315 0, // rangeFieldsupport
3316 50, // nominalValue = 50
3317 60, // normalMax = 60
3318 40, // normalMin = 40
3319 70, // warningHigh = 70
3320 30, // warningLow = 30
3321 80, // criticalHigh = 80
3322 20, // criticalLow = 20
3323 90, // fatalHigh = 90
3324 10 // fatalLow = 10
3325 };
3326
3327 struct pldm_numeric_sensor_value_pdr decodedPdr;
3328 auto rc =
3329 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3330 EXPECT_EQ(PLDM_SUCCESS, rc);
3331 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
3332 EXPECT_EQ(1, decodedPdr.hdr.version);
3333 EXPECT_EQ(PLDM_NUMERIC_SENSOR_PDR, decodedPdr.hdr.type);
3334 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
3335 EXPECT_EQ(PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, decodedPdr.hdr.length);
3336 EXPECT_EQ(1, decodedPdr.sensor_id);
3337 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
3338 EXPECT_EQ(1, decodedPdr.entity_instance_num);
3339 EXPECT_EQ(1, decodedPdr.container_id);
3340 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.sensor_init);
3341 EXPECT_EQ(false, decodedPdr.sensor_auxiliary_names_pdr);
3342 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
3343 EXPECT_EQ(0, decodedPdr.unit_modifier);
3344 EXPECT_EQ(0, decodedPdr.rate_unit);
3345 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
3346 EXPECT_EQ(0, decodedPdr.aux_unit);
3347 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
3348 EXPECT_EQ(0, decodedPdr.aux_rate_unit);
3349 EXPECT_EQ(0, decodedPdr.rel);
3350 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
3351 EXPECT_EQ(true, decodedPdr.is_linear);
3352 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT8, decodedPdr.sensor_data_size);
3353 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
3354 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
3355 EXPECT_EQ(0, decodedPdr.accuracy);
3356 EXPECT_EQ(0, decodedPdr.plus_tolerance);
3357 EXPECT_EQ(0, decodedPdr.minus_tolerance);
3358 EXPECT_EQ(3, decodedPdr.hysteresis.value_u8);
3359 EXPECT_EQ(0, decodedPdr.supported_thresholds.byte);
3360 EXPECT_EQ(0, decodedPdr.threshold_and_hysteresis_volatility.byte);
3361 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
3362 EXPECT_FLOAT_EQ(1.0f, decodedPdr.update_interval);
3363 EXPECT_EQ(255, decodedPdr.max_readable.value_u8);
3364 EXPECT_EQ(0, decodedPdr.min_readable.value_u8);
3365 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
3366 EXPECT_EQ(0, decodedPdr.range_field_support.byte);
3367 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
3368 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
3369 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
3370 EXPECT_EQ(70, decodedPdr.warning_high.value_u8);
3371 EXPECT_EQ(30, decodedPdr.warning_low.value_u8);
3372 EXPECT_EQ(80, decodedPdr.critical_high.value_u8);
3373 EXPECT_EQ(20, decodedPdr.critical_low.value_u8);
3374 EXPECT_EQ(90, decodedPdr.fatal_high.value_u8);
3375 EXPECT_EQ(10, decodedPdr.fatal_low.value_u8);
3376}
3377
3378TEST(decodeNumericSensorPdrData, Sint8Test)
3379{
3380 std::vector<uint8_t> pdr1{
3381 0x1,
3382 0x0,
3383 0x0,
3384 0x0, // record handle
3385 0x1, // PDRHeaderVersion
3386 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3387 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303388 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303389 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3390 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
3391 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303392 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303393 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303394 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303395 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303396 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303397 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303398 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303399 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303400 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303401 0x1,
3402 0x0, // containerID=1
3403 PLDM_NO_INIT, // sensorInit
3404 false, // sensorAuxiliaryNamesPDR
3405 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3406 0, // unitModifier
3407 0, // rateUnit
3408 0, // baseOEMUnitHandle
3409 0, // auxUnit
3410 0, // auxUnitModifier
3411 0, // auxRateUnit
3412 0, // rel
3413 0, // auxOEMUnitHandle
3414 true, // isLinear
3415 PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
3416 0,
3417 0,
3418 0,
3419 0, // resolution
3420 0,
3421 0,
3422 0,
3423 0, // offset
3424 0,
3425 0, // accuracy
3426 0, // plusTolerance
3427 0, // minusTolerance
3428 3, // hysteresis = 3
3429 0, // supportedThresholds
3430 0, // thresholdAndHysteresisVolatility
3431 0,
3432 0,
3433 0x80,
3434 0x3f, // stateTransistionInterval=1.0
3435 0,
3436 0,
3437 0x80,
3438 0x3f, // updateInverval=1.0
3439 0x64, // maxReadable = 100
3440 0x9c, // minReadable = -100
3441 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
3442 0, // rangeFieldsupport
3443 0, // nominalValue = 0
3444 5, // normalMax = 5
3445 0xfb, // normalMin = -5
3446 10, // warningHigh = 10
3447 0xf6, // warningLow = -10
3448 20, // criticalHigh = 20
3449 0xec, // criticalLow = -20
3450 30, // fatalHigh = 30
3451 0xe2 // fatalLow = -30
3452 };
3453
3454 struct pldm_numeric_sensor_value_pdr decodedPdr;
3455 auto rc =
3456 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3457 EXPECT_EQ(PLDM_SUCCESS, rc);
3458
3459 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT8, decodedPdr.sensor_data_size);
3460 EXPECT_EQ(100, decodedPdr.max_readable.value_s8);
3461 EXPECT_EQ(-100, decodedPdr.min_readable.value_s8);
3462 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
3463 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
3464 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
3465 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
3466 EXPECT_EQ(10, decodedPdr.warning_high.value_s8);
3467 EXPECT_EQ(-10, decodedPdr.warning_low.value_s8);
3468 EXPECT_EQ(20, decodedPdr.critical_high.value_s8);
3469 EXPECT_EQ(-20, decodedPdr.critical_low.value_s8);
3470 EXPECT_EQ(30, decodedPdr.fatal_high.value_s8);
3471 EXPECT_EQ(-30, decodedPdr.fatal_low.value_s8);
3472}
3473
3474TEST(decodeNumericSensorPdrData, Uint16Test)
3475{
3476 std::vector<uint8_t> pdr1{
3477 0x1,
3478 0x0,
3479 0x0,
3480 0x0, // record handle
3481 0x1, // PDRHeaderVersion
3482 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3483 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303484 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303485 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3486 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
3487 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09303488 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303489 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303490 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303491 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303492 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303493 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303494 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303495 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303496 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303497 0x1,
3498 0x0, // containerID=1
3499 PLDM_NO_INIT, // sensorInit
3500 false, // sensorAuxiliaryNamesPDR
3501 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3502 0, // unitModifier
3503 0, // rateUnit
3504 0, // baseOEMUnitHandle
3505 0, // auxUnit
3506 0, // auxUnitModifier
3507 0, // auxRateUnit
3508 0, // rel
3509 0, // auxOEMUnitHandle
3510 true, // isLinear
3511 PLDM_SENSOR_DATA_SIZE_UINT16, // sensorDataSize
3512 0,
3513 0,
3514 0,
3515 0, // resolution
3516 0,
3517 0,
3518 0,
3519 0, // offset
3520 0,
3521 0, // accuracy
3522 0, // plusTolerance
3523 0, // minusTolerance
3524 3,
3525 0, // hysteresis = 3
3526 0, // supportedThresholds
3527 0, // thresholdAndHysteresisVolatility
3528 0,
3529 0,
3530 0x80,
3531 0x3f, // stateTransistionInterval=1.0
3532 0,
3533 0,
3534 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09303535 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303536 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303537 0x10, // maxReadable = 4096
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303538 0,
3539 0, // minReadable = 0
3540 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
3541 0, // rangeFieldsupport
3542 0x88,
Andrew Jeffery01731472023-06-13 10:33:18 +09303543 0x13, // nominalValue = 5,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303544 0x70,
Andrew Jeffery01731472023-06-13 10:33:18 +09303545 0x17, // normalMax = 6,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303546 0xa0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303547 0x0f, // normalMin = 4,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303548 0x58,
Andrew Jeffery01731472023-06-13 10:33:18 +09303549 0x1b, // warningHigh = 7,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303550 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303551 0x0b, // warningLow = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303552 0x40,
Andrew Jeffery01731472023-06-13 10:33:18 +09303553 0x1f, // criticalHigh = 8,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303554 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303555 0x07, // criticalLow = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303556 0x28,
Andrew Jeffery01731472023-06-13 10:33:18 +09303557 0x23, // fatalHigh = 9,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303558 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303559 0x03 // fatalLow = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303560 };
3561
3562 struct pldm_numeric_sensor_value_pdr decodedPdr;
3563 auto rc =
3564 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3565 EXPECT_EQ(PLDM_SUCCESS, rc);
3566
3567 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT16, decodedPdr.sensor_data_size);
3568 EXPECT_EQ(4096, decodedPdr.max_readable.value_u16);
3569 EXPECT_EQ(0, decodedPdr.min_readable.value_u16);
3570 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
3571 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
3572 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
3573 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
3574 EXPECT_EQ(7000, decodedPdr.warning_high.value_u16);
3575 EXPECT_EQ(3000, decodedPdr.warning_low.value_u16);
3576 EXPECT_EQ(8000, decodedPdr.critical_high.value_u16);
3577 EXPECT_EQ(2000, decodedPdr.critical_low.value_u16);
3578 EXPECT_EQ(9000, decodedPdr.fatal_high.value_u16);
3579 EXPECT_EQ(1000, decodedPdr.fatal_low.value_u16);
3580}
3581
3582TEST(decodeNumericSensorPdrData, Sint16Test)
3583{
3584 std::vector<uint8_t> pdr1{
3585 0x1,
3586 0x0,
3587 0x0,
3588 0x0, // record handle
3589 0x1, // PDRHeaderVersion
3590 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3591 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303592 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303593 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3594 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
3595 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09303596 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303597 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303598 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303599 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303600 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303601 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303602 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303603 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303604 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303605 0x1,
3606 0x0, // containerID=1
3607 PLDM_NO_INIT, // sensorInit
3608 false, // sensorAuxiliaryNamesPDR
3609 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3610 0, // unitModifier
3611 0, // rateUnit
3612 0, // baseOEMUnitHandle
3613 0, // auxUnit
3614 0, // auxUnitModifier
3615 0, // auxRateUnit
3616 0, // rel
3617 0, // auxOEMUnitHandle
3618 true, // isLinear
3619 PLDM_SENSOR_DATA_SIZE_SINT16, // sensorDataSize
3620 0,
3621 0,
3622 0,
3623 0, // resolution
3624 0,
3625 0,
3626 0,
3627 0, // offset
3628 0,
3629 0, // accuracy
3630 0, // plusTolerance
3631 0, // minusTolerance
3632 3,
3633 0, // hysteresis
3634 0, // supportedThresholds
3635 0, // thresholdAndHysteresisVolatility
3636 0,
3637 0,
3638 0x80,
3639 0x3f, // stateTransistionInterval=1.0
3640 0,
3641 0,
3642 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09303643 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303644 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303645 0x03, // maxReadable = 1000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303646 0x18,
3647 0xfc, // minReadable = -1000
3648 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
3649 0, // rangeFieldsupport
3650 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303651 0, // nominalValue = 0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303652 0xf4,
Andrew Jeffery01731472023-06-13 10:33:18 +09303653 0x01, // normalMax = 500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303654 0x0c,
Andrew Jeffery01731472023-06-13 10:33:18 +09303655 0xfe, // normalMin = -500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303656 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303657 0x03, // warningHigh = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303658 0x18,
Andrew Jeffery01731472023-06-13 10:33:18 +09303659 0xfc, // warningLow = -1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303660 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303661 0x07, // criticalHigh = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303662 0x30,
Andrew Jeffery01731472023-06-13 10:33:18 +09303663 0xf8, // criticalLow = -2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303664 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303665 0x0b, // fatalHigh = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303666 0x48,
Andrew Jeffery01731472023-06-13 10:33:18 +09303667 0xf4 // fatalLow = -3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303668 };
3669
3670 struct pldm_numeric_sensor_value_pdr decodedPdr;
3671 auto rc =
3672 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3673 EXPECT_EQ(PLDM_SUCCESS, rc);
3674
3675 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT16, decodedPdr.sensor_data_size);
3676 EXPECT_EQ(1000, decodedPdr.max_readable.value_s16);
3677 EXPECT_EQ(-1000, decodedPdr.min_readable.value_s16);
3678 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
3679 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
3680 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
3681 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
3682 EXPECT_EQ(1000, decodedPdr.warning_high.value_s16);
3683 EXPECT_EQ(-1000, decodedPdr.warning_low.value_s16);
3684 EXPECT_EQ(2000, decodedPdr.critical_high.value_s16);
3685 EXPECT_EQ(-2000, decodedPdr.critical_low.value_s16);
3686 EXPECT_EQ(3000, decodedPdr.fatal_high.value_s16);
3687 EXPECT_EQ(-3000, decodedPdr.fatal_low.value_s16);
3688}
3689
3690TEST(decodeNumericSensorPdrData, Uint32Test)
3691{
3692 std::vector<uint8_t> pdr1{
3693 0x1,
3694 0x0,
3695 0x0,
3696 0x0, // record handle
3697 0x1, // PDRHeaderVersion
3698 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3699 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303700 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303701 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3702 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
3703 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09303704 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303705 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303706 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303707 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303708 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303709 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303710 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303711 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303712 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303713 0x1,
3714 0x0, // containerID=1
3715 PLDM_NO_INIT, // sensorInit
3716 false, // sensorAuxiliaryNamesPDR
3717 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3718 0, // unitModifier
3719 0, // rateUnit
3720 0, // baseOEMUnitHandle
3721 0, // auxUnit
3722 0, // auxUnitModifier
3723 0, // auxRateUnit
3724 0, // rel
3725 0, // auxOEMUnitHandle
3726 true, // isLinear
3727 PLDM_SENSOR_DATA_SIZE_UINT32, // sensorDataSize
3728 0,
3729 0,
3730 0,
3731 0, // resolution
3732 0,
3733 0,
3734 0,
3735 0, // offset
3736 0,
3737 0, // accuracy
3738 0, // plusTolerance
3739 0, // minusTolerance
3740 3,
3741 0,
3742 0,
3743 0, // hysteresis
3744 0, // supportedThresholds
3745 0, // thresholdAndHysteresisVolatility
3746 0,
3747 0,
3748 0x80,
3749 0x3f, // stateTransistionInterval=1.0
3750 0,
3751 0,
3752 0x80,
3753 0x3f, // updateInverval=1.0
3754 0,
3755 0x10,
3756 0,
3757 0, // maxReadable = 4096
3758 0,
3759 0,
3760 0,
3761 0, // minReadable = 0
3762 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
3763 0, // rangeFieldsupport
3764 0x40,
3765 0x4b,
3766 0x4c,
3767 0x00, // nominalValue = 5,000,000
3768 0x80,
3769 0x8d,
3770 0x5b,
3771 0x00, // normalMax = 6,000,000
3772 0x00,
3773 0x09,
3774 0x3d,
3775 0x00, // normalMin = 4,000,000
3776 0xc0,
3777 0xcf,
3778 0x6a,
3779 0x00, // warningHigh = 7,000,000
3780 0xc0,
3781 0xc6,
3782 0x2d,
3783 0x00, // warningLow = 3,000,000
3784 0x00,
3785 0x12,
3786 0x7a,
3787 0x00, // criticalHigh = 8,000,000
3788 0x80,
3789 0x84,
3790 0x1e,
3791 0x00, // criticalLow = 2,000,000
3792 0x40,
3793 0x54,
3794 0x89,
3795 0x00, // fatalHigh = 9,000,000
3796 0x40,
3797 0x42,
3798 0x0f,
3799 0x00 // fatalLow = 1,000,000
3800 };
3801
3802 struct pldm_numeric_sensor_value_pdr decodedPdr;
3803 auto rc =
3804 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3805 EXPECT_EQ(PLDM_SUCCESS, rc);
3806
3807 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT32, decodedPdr.sensor_data_size);
3808 EXPECT_EQ(4096, decodedPdr.max_readable.value_u32);
3809 EXPECT_EQ(0, decodedPdr.min_readable.value_u32);
3810 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
3811 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
3812 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
3813 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
3814 EXPECT_EQ(7000000, decodedPdr.warning_high.value_u32);
3815 EXPECT_EQ(3000000, decodedPdr.warning_low.value_u32);
3816 EXPECT_EQ(8000000, decodedPdr.critical_high.value_u32);
3817 EXPECT_EQ(2000000, decodedPdr.critical_low.value_u32);
3818 EXPECT_EQ(9000000, decodedPdr.fatal_high.value_u32);
3819 EXPECT_EQ(1000000, decodedPdr.fatal_low.value_u32);
3820}
3821
3822TEST(decodeNumericSensorPdrData, Sint32Test)
3823{
3824 std::vector<uint8_t> pdr1{
3825 0x1,
3826 0x0,
3827 0x0,
3828 0x0, // record handle
3829 0x1, // PDRHeaderVersion
3830 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3831 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303832 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303833 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3834 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
3835 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09303836 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303837 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303838 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303839 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303840 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303841 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303842 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303843 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303844 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303845 0x1,
3846 0x0, // containerID=1
3847 PLDM_NO_INIT, // sensorInit
3848 false, // sensorAuxiliaryNamesPDR
3849 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3850 0, // unitModifier
3851 0, // rateUnit
3852 0, // baseOEMUnitHandle
3853 0, // auxUnit
3854 0, // auxUnitModifier
3855 0, // auxRateUnit
3856 0, // rel
3857 0, // auxOEMUnitHandle
3858 true, // isLinear
3859 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
3860 0,
3861 0,
3862 0,
3863 0, // resolution
3864 0,
3865 0,
3866 0,
3867 0, // offset
3868 0,
3869 0, // accuracy
3870 0, // plusTolerance
3871 0, // minusTolerance
3872 3,
3873 0,
3874 0,
3875 0, // hysteresis
3876 0, // supportedThresholds
3877 0, // thresholdAndHysteresisVolatility
3878 0,
3879 0,
3880 0x80,
3881 0x3f, // stateTransistionInterval=1.0
3882 0,
3883 0,
3884 0x80,
3885 0x3f, // updateInverval=1.0
3886 0xa0,
3887 0x86,
3888 0x01,
3889 0x00, // maxReadable = 100000
3890 0x60,
3891 0x79,
3892 0xfe,
3893 0xff, // minReadable = -10000
3894 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
3895 0, // rangeFieldsupport
3896 0,
3897 0,
3898 0,
3899 0, // nominalValue = 0
3900 0x20,
3901 0xa1,
3902 0x07,
3903 0x00, // normalMax = 500,000
3904 0xe0,
3905 0x5e,
3906 0xf8,
3907 0xff, // normalMin = -500,000
3908 0x40,
3909 0x42,
3910 0x0f,
3911 0x00, // warningHigh = 1,000,000
3912 0xc0,
3913 0xbd,
3914 0xf0,
3915 0xff, // warningLow = -1,000,000
3916 0x80,
3917 0x84,
3918 0x1e,
3919 0x00, // criticalHigh = 2,000,000
3920 0x80,
3921 0x7b,
3922 0xe1,
3923 0xff, // criticalLow = -2,000,000
3924 0xc0,
3925 0xc6,
3926 0x2d,
3927 0x00, // fatalHigh = 3,000,000
3928 0x40,
3929 0x39,
3930 0xd2,
3931 0xff // fatalLow = -3,000,000
3932 };
3933
3934 struct pldm_numeric_sensor_value_pdr decodedPdr;
3935 auto rc =
3936 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3937 EXPECT_EQ(PLDM_SUCCESS, rc);
3938
3939 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
3940 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
3941 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
3942 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
3943 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
3944 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
3945 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
3946 EXPECT_EQ(1000000, decodedPdr.warning_high.value_s32);
3947 EXPECT_EQ(-1000000, decodedPdr.warning_low.value_s32);
3948 EXPECT_EQ(2000000, decodedPdr.critical_high.value_s32);
3949 EXPECT_EQ(-2000000, decodedPdr.critical_low.value_s32);
3950 EXPECT_EQ(3000000, decodedPdr.fatal_high.value_s32);
3951 EXPECT_EQ(-3000000, decodedPdr.fatal_low.value_s32);
3952}
3953
3954TEST(decodeNumericSensorPdrData, Real32Test)
3955{
3956 std::vector<uint8_t> pdr1{
3957 0x1,
3958 0x0,
3959 0x0,
3960 0x0, // record handle
3961 0x1, // PDRHeaderVersion
3962 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3963 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303964 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303965 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3966 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
3967 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09303968 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303969 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303970 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303971 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303972 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303973 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303974 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303975 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303976 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303977 0x1,
3978 0x0, // containerID=1
3979 PLDM_NO_INIT, // sensorInit
3980 false, // sensorAuxiliaryNamesPDR
3981 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3982 0, // unitModifier
3983 0, // rateUnit
3984 0, // baseOEMUnitHandle
3985 0, // auxUnit
3986 0, // auxUnitModifier
3987 0, // auxRateUnit
3988 0, // rel
3989 0, // auxOEMUnitHandle
3990 true, // isLinear
3991 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
3992 0,
3993 0,
3994 0,
3995 0, // resolution
3996 0,
3997 0,
3998 0,
3999 0, // offset
4000 0,
4001 0, // accuracy
4002 0, // plusTolerance
4003 0, // minusTolerance
4004 3,
4005 0,
4006 0,
4007 0, // hysteresis
4008 0, // supportedThresholds
4009 0, // thresholdAndHysteresisVolatility
4010 0,
4011 0,
4012 0x80,
4013 0x3f, // stateTransistionInterval=1.0
4014 0,
4015 0,
4016 0x80,
4017 0x3f, // updateInverval=1.0
4018 0xa0,
4019 0x86,
4020 0x01,
4021 0x00, // maxReadable = 100000
4022 0x60,
4023 0x79,
4024 0xfe,
4025 0xff, // minReadable = -10000
4026 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
4027 0, // rangeFieldsupport
4028 0,
4029 0,
4030 0,
4031 0, // nominalValue = 0.0
4032 0x33,
4033 0x33,
4034 0x48,
4035 0x42, // normalMax = 50.05
4036 0x33,
4037 0x33,
4038 0x48,
4039 0xc2, // normalMin = -50.05
4040 0x83,
4041 0x00,
4042 0xc8,
4043 0x42, // warningHigh = 100.001
4044 0x83,
4045 0x00,
4046 0xc8,
4047 0xc2, // warningLow = -100.001
4048 0x83,
4049 0x00,
4050 0x48,
4051 0x43, // criticalHigh = 200.002
4052 0x83,
4053 0x00,
4054 0x48,
4055 0xc3, // criticalLow = -200.002
4056 0x62,
4057 0x00,
4058 0x96,
4059 0x43, // fatalHigh = 300.003
4060 0x62,
4061 0x00,
4062 0x96,
4063 0xc3 // fatalLow = -300.003
4064 };
4065
4066 struct pldm_numeric_sensor_value_pdr decodedPdr;
4067 auto rc =
4068 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4069 EXPECT_EQ(PLDM_SUCCESS, rc);
4070
4071 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
4072 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
4073 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
4074 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
4075 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
4076 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
4077 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
4078 EXPECT_FLOAT_EQ(100.001f, decodedPdr.warning_high.value_f32);
4079 EXPECT_FLOAT_EQ(-100.001f, decodedPdr.warning_low.value_f32);
4080 EXPECT_FLOAT_EQ(200.002f, decodedPdr.critical_high.value_f32);
4081 EXPECT_FLOAT_EQ(-200.002f, decodedPdr.critical_low.value_f32);
4082 EXPECT_FLOAT_EQ(300.003f, decodedPdr.fatal_high.value_f32);
4083 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.fatal_low.value_f32);
4084}
4085
4086TEST(decodeNumericSensorPdrDataDeathTest, InvalidSizeTest)
4087{
4088 // A corrupted PDR. The data after plusTolerance missed.
4089 std::vector<uint8_t> pdr1{
4090 0x1,
4091 0x0,
4092 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304093 0x0, // record handle
4094 0x1, // PDRHeaderVersion
4095 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304096 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304097 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304098 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09304099 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304100 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304101 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304102 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304103 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304104 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304105 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304106 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304107 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304108 0x1,
4109 0x0, // containerID=1
4110 PLDM_NO_INIT, // sensorInit
4111 false, // sensorAuxiliaryNamesPDR
4112 2, // baseUint(2)=degrees C
4113 0, // unitModifier
4114 0, // rateUnit
4115 0, // baseOEMUnitHandle
4116 0, // auxUnit
4117 0, // auxUnitModifier
4118 0, // auxRateUnit
4119 0, // rel
4120 0, // auxOEMUnitHandle
4121 true, // isLinear
4122 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
4123 0,
4124 0,
4125 0,
4126 0, // resolution
4127 0,
4128 0,
4129 0,
4130 0, // offset
4131 0,
4132 0, // accuracy
4133 0 // plusTolerance
4134 };
4135
4136 struct pldm_numeric_sensor_value_pdr decodedPdr;
4137 int rc =
4138 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4139 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
4140}
Thu Nguyend4878cd2023-11-09 10:18:33 +07004141
Andrew Jeffery688be622024-05-23 11:22:51 +09304142#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004143TEST(decodeNumericEffecterPdrData, Uint8Test)
4144{
4145 std::vector<uint8_t> pdr1{
4146 0x1,
4147 0x0,
4148 0x0,
4149 0x0, // record handle
4150 0x1, // PDRHeaderVersion
4151 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4152 0x0,
4153 0x0, // recordChangeNumber
4154 PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
4155 0, // dataLength
4156 0,
4157 0, // PLDMTerminusHandle
4158 0x1,
4159 0x0, // effecterID=1
4160 PLDM_ENTITY_POWER_SUPPLY,
4161 0, // entityType=Power Supply(120)
4162 1,
4163 0, // entityInstanceNumber
4164 1,
4165 0, // containerID=1
4166 0x2,
4167 0x0, // effecter_semantic_id=2
4168 PLDM_NO_INIT, // effecterInit
4169 false, // effecterAuxiliaryNames
4170 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4171 0, // unitModifier
4172 0, // rateUnit
4173 0, // baseOEMUnitHandle
4174 0, // auxUnit
4175 0, // auxUnitModifier
4176 4, // auxRateUnit
4177 0, // auxOEMUnitHandle
4178 true, // isLinear
4179 PLDM_EFFECTER_DATA_SIZE_UINT8, // effecterDataSize
4180 0,
4181 0,
4182 0xc0,
4183 0x3f, // resolution=1.5
4184 0,
4185 0,
4186 0x80,
4187 0x3f, // offset=1.0
4188 0,
4189 0, // accuracy
4190 0, // plusTolerance
4191 0, // minusTolerance
4192 0,
4193 0,
4194 0x80,
4195 0x3f, // stateTransistionInterval=1.0
4196 0,
4197 0,
4198 0x80,
4199 0x3f, // transition_interval=1.0
4200 255, // maxSetdable
4201 0, // minSetable
4202 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
4203 0x1f, // rangeFieldsupport
4204 50, // nominalValue = 50
4205 60, // normalMax = 60
4206 40, // normalMin = 40
4207 90, // rated_max = 90
4208 10 // rated_min = 10
4209 };
4210
4211 struct pldm_numeric_effecter_value_pdr decodedPdr;
4212 auto rc =
4213 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4214 EXPECT_EQ(PLDM_SUCCESS, rc);
4215 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
4216 EXPECT_EQ(1, decodedPdr.hdr.version);
4217 EXPECT_EQ(PLDM_NUMERIC_EFFECTER_PDR, decodedPdr.hdr.type);
4218 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
4219 EXPECT_EQ(PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH, decodedPdr.hdr.length);
4220 EXPECT_EQ(1, decodedPdr.effecter_id);
4221 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
4222 EXPECT_EQ(1, decodedPdr.entity_instance);
4223 EXPECT_EQ(1, decodedPdr.container_id);
4224 EXPECT_EQ(2, decodedPdr.effecter_semantic_id);
4225 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.effecter_init);
4226 EXPECT_EQ(false, decodedPdr.effecter_auxiliary_names);
4227 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
4228 EXPECT_EQ(0, decodedPdr.unit_modifier);
4229 EXPECT_EQ(0, decodedPdr.rate_unit);
4230 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
4231 EXPECT_EQ(0, decodedPdr.aux_unit);
4232 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
4233 EXPECT_EQ(4, decodedPdr.aux_rate_unit);
4234 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
4235 EXPECT_EQ(true, decodedPdr.is_linear);
4236 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT8, decodedPdr.effecter_data_size);
4237 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
4238 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
4239 EXPECT_EQ(0, decodedPdr.accuracy);
4240 EXPECT_EQ(0, decodedPdr.plus_tolerance);
4241 EXPECT_EQ(0, decodedPdr.minus_tolerance);
4242 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
4243 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
4244 EXPECT_EQ(255, decodedPdr.max_settable.value_u8);
4245 EXPECT_EQ(0, decodedPdr.min_settable.value_u8);
4246 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
4247 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4248 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
4249 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
4250 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
4251 EXPECT_EQ(90, decodedPdr.rated_max.value_u8);
4252 EXPECT_EQ(10, decodedPdr.rated_min.value_u8);
4253}
Andrew Jeffery688be622024-05-23 11:22:51 +09304254#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004255
Andrew Jeffery688be622024-05-23 11:22:51 +09304256#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004257TEST(decodeNumericEffecterPdrData, Sint8Test)
4258{
4259 std::vector<uint8_t> pdr1{
4260 0x1,
4261 0x0,
4262 0x0,
4263 0x0, // record handle
4264 0x1, // PDRHeaderVersion
4265 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4266 0x0,
4267 0x0, // recordChangeNumber
4268 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4269 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH +
4270 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
4271 0, // dataLength
4272 0,
4273 0, // PLDMTerminusHandle
4274 0x1,
4275 0x0, // effecterID=1
4276 PLDM_ENTITY_POWER_SUPPLY,
4277 0, // entityType=Power Supply(120)
4278 1,
4279 0, // entityInstanceNumber
4280 0x1,
4281 0x0, // containerID=1
4282 0x2,
4283 0x0, // effecter_semantic_id=2
4284 PLDM_NO_INIT, // effecterInit
4285 false, // effecterAuxiliaryNames
4286 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4287 0, // unitModifier
4288 0, // rateUnit
4289 0, // baseOEMUnitHandle
4290 0, // auxUnit
4291 0, // auxUnitModifier
4292 0, // auxRateUnit
4293 0, // auxOEMUnitHandle
4294 true, // isLinear
4295 PLDM_RANGE_FIELD_FORMAT_SINT8, // effecterDataSize
4296 0,
4297 0,
4298 0,
4299 0, // resolution
4300 0,
4301 0,
4302 0,
4303 0, // offset
4304 0,
4305 0, // accuracy
4306 0, // plusTolerance
4307 0, // minusTolerance
4308 0,
4309 0,
4310 0x80,
4311 0x3f, // stateTransistionInterval=1.0
4312 0,
4313 0,
4314 0x80,
4315 0x3f, // transition_interval=1.0
4316 0x64, // maxSetdable = 100
4317 0x9c, // minSetable = -100
4318 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
4319 0x1f, // rangeFieldsupport
4320 0, // nominalValue = 0
4321 5, // normalMax = 5
4322 0xfb, // normalMin = -5
4323 30, // rated_max = 30
4324 0xe2 // rated_min = -30
4325 };
4326
4327 struct pldm_numeric_effecter_value_pdr decodedPdr;
4328 auto rc =
4329 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4330 EXPECT_EQ(PLDM_SUCCESS, rc);
4331
4332 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT8, decodedPdr.effecter_data_size);
4333 EXPECT_EQ(100, decodedPdr.max_settable.value_s8);
4334 EXPECT_EQ(-100, decodedPdr.min_settable.value_s8);
4335 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
4336 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4337 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
4338 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
4339 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
4340 EXPECT_EQ(30, decodedPdr.rated_max.value_s8);
4341 EXPECT_EQ(-30, decodedPdr.rated_min.value_s8);
4342}
Andrew Jeffery688be622024-05-23 11:22:51 +09304343#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004344
Andrew Jeffery688be622024-05-23 11:22:51 +09304345#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004346TEST(decodeNumericEffecterPdrData, Uint16Test)
4347{
4348 std::vector<uint8_t> pdr1{
4349 0x1,
4350 0x0,
4351 0x0,
4352 0x0, // record handle
4353 0x1, // PDRHeaderVersion
4354 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4355 0x0,
4356 0x0, // recordChangeNumber
4357 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4358 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4359 2 +
4360 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4361 0, // dataLength
4362 0,
4363 0, // PLDMTerminusHandle
4364 0x1,
4365 0x0, // effecterID=1
4366 PLDM_ENTITY_POWER_SUPPLY,
4367 0, // entityType=Power Supply(120)
4368 1,
4369 0, // entityInstanceNumber
4370 0x1,
4371 0x0, // containerID=1
4372 0x2,
4373 0x0, // effecter_semantic_id=2
4374 PLDM_NO_INIT, // effecterInit
4375 false, // effecterAuxiliaryNames
4376 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4377 0, // unitModifier
4378 0, // rateUnit
4379 0, // baseOEMUnitHandle
4380 0, // auxUnit
4381 0, // auxUnitModifier
4382 0, // auxRateUnit
4383 0, // auxOEMUnitHandle
4384 true, // isLinear
4385 PLDM_EFFECTER_DATA_SIZE_UINT16, // effecterDataSize
4386 0,
4387 0,
4388 0,
4389 0, // resolution
4390 0,
4391 0,
4392 0,
4393 0, // offset
4394 0,
4395 0, // accuracy
4396 0, // plusTolerance
4397 0, // minusTolerance
4398 0,
4399 0,
4400 0x80,
4401 0x3f, // stateTransistionInterval=1.0
4402 0,
4403 0,
4404 0x80,
4405 0x3f, // transition_interval=1.0
4406 0,
4407 0x10, // maxSetdable = 4096
4408 0,
4409 0, // minSetable = 0
4410 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
4411 0x1f, // rangeFieldsupport
4412 0x88,
4413 0x13, // nominalValue = 5,000
4414 0x70,
4415 0x17, // normalMax = 6,000
4416 0xa0,
4417 0x0f, // normalMin = 4,000
4418 0x28,
4419 0x23, // rated_max = 9,000
4420 0xe8,
4421 0x03 // rated_min = 1,000
4422 };
4423
4424 struct pldm_numeric_effecter_value_pdr decodedPdr;
4425 auto rc =
4426 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4427 EXPECT_EQ(PLDM_SUCCESS, rc);
4428
4429 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT16, decodedPdr.effecter_data_size);
4430 EXPECT_EQ(4096, decodedPdr.max_settable.value_u16);
4431 EXPECT_EQ(0, decodedPdr.min_settable.value_u16);
4432 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
4433 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4434 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
4435 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
4436 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
4437 EXPECT_EQ(9000, decodedPdr.rated_max.value_u16);
4438 EXPECT_EQ(1000, decodedPdr.rated_min.value_u16);
4439}
Andrew Jeffery688be622024-05-23 11:22:51 +09304440#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004441
Andrew Jeffery688be622024-05-23 11:22:51 +09304442#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004443TEST(decodeNumericEffecterPdrData, Sint16Test)
4444{
4445 std::vector<uint8_t> pdr1{
4446 0x1,
4447 0x0,
4448 0x0,
4449 0x0, // record handle
4450 0x1, // PDRHeaderVersion
4451 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4452 0x0,
4453 0x0, // recordChangeNumber
4454 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4455 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4456 2 +
4457 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4458 0, // dataLength
4459 0,
4460 0, // PLDMTerminusHandle
4461 0x1,
4462 0x0, // effecterID=1
4463 PLDM_ENTITY_POWER_SUPPLY,
4464 0, // entityType=Power Supply(120)
4465 1,
4466 0, // entityInstanceNumber
4467 0x1,
4468 0x0, // containerID=1
4469 0x2,
4470 0x0, // effecter_semantic_id=2
4471 PLDM_NO_INIT, // effecterInit
4472 false, // effecterAuxiliaryNames
4473 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4474 0, // unitModifier
4475 0, // rateUnit
4476 0, // baseOEMUnitHandle
4477 0, // auxUnit
4478 0, // auxUnitModifier
4479 0, // auxRateUnit
4480 0, // auxOEMUnitHandle
4481 true, // isLinear
4482 PLDM_EFFECTER_DATA_SIZE_SINT16, // effecterDataSize
4483 0,
4484 0,
4485 0,
4486 0, // resolution
4487 0,
4488 0,
4489 0,
4490 0, // offset
4491 0,
4492 0, // accuracy
4493 0, // plusTolerance
4494 0, // minusTolerance
4495 0,
4496 0,
4497 0x80,
4498 0x3f, // stateTransistionInterval=1.0
4499 0,
4500 0,
4501 0x80,
4502 0x3f, // transition_interval=1.0
4503 0xe8,
4504 0x03, // maxSetdable = 1000
4505 0x18,
4506 0xfc, // minSetable = -1000
4507 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
4508 0x1f, // rangeFieldsupport
4509 0,
4510 0, // nominalValue = 0
4511 0xf4,
4512 0x01, // normalMax = 500
4513 0x0c,
4514 0xfe, // normalMin = -500
4515 0xb8,
4516 0x0b, // rated_max = 3,000
4517 0x48,
4518 0xf4 // rated_min = -3,000
4519 };
4520
4521 struct pldm_numeric_effecter_value_pdr decodedPdr;
4522 auto rc =
4523 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4524 EXPECT_EQ(PLDM_SUCCESS, rc);
4525
4526 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT16, decodedPdr.effecter_data_size);
4527 EXPECT_EQ(1000, decodedPdr.max_settable.value_s16);
4528 EXPECT_EQ(-1000, decodedPdr.min_settable.value_s16);
4529 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
4530 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4531 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
4532 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
4533 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
4534 EXPECT_EQ(3000, decodedPdr.rated_max.value_s16);
4535 EXPECT_EQ(-3000, decodedPdr.rated_min.value_s16);
4536}
Andrew Jeffery688be622024-05-23 11:22:51 +09304537#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004538
Andrew Jeffery688be622024-05-23 11:22:51 +09304539#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004540TEST(decodeNumericEffecterPdrData, Uint32Test)
4541{
4542 std::vector<uint8_t> pdr1{
4543 0x1,
4544 0x0,
4545 0x0,
4546 0x0, // record handle
4547 0x1, // PDRHeaderVersion
4548 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4549 0x0,
4550 0x0, // recordChangeNumber
4551 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4552 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4553 4 +
4554 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
4555 0, // dataLength
4556 0,
4557 0, // PLDMTerminusHandle
4558 0x1,
4559 0x0, // effecterID=1
4560 PLDM_ENTITY_POWER_SUPPLY,
4561 0, // entityType=Power Supply(120)
4562 1,
4563 0, // entityInstanceNumber
4564 0x1,
4565 0x0, // containerID=1
4566 0x2,
4567 0x0, // effecter_semantic_id=2
4568 PLDM_NO_INIT, // effecterInit
4569 false, // effecterAuxiliaryNames
4570 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4571 0, // unitModifier
4572 0, // rateUnit
4573 0, // baseOEMUnitHandle
4574 0, // auxUnit
4575 0, // auxUnitModifier
4576 0, // auxRateUnit
4577 0, // auxOEMUnitHandle
4578 true, // isLinear
4579 PLDM_EFFECTER_DATA_SIZE_UINT32, // effecterDataSize
4580 0,
4581 0,
4582 0,
4583 0, // resolution
4584 0,
4585 0,
4586 0,
4587 0, // offset
4588 0,
4589 0, // accuracy
4590 0, // plusTolerance
4591 0, // minusTolerance
4592 0,
4593 0,
4594 0x80,
4595 0x3f, // stateTransistionInterval=1.0
4596 0,
4597 0,
4598 0x80,
4599 0x3f, // transition_interval=1.0
4600 0,
4601 0x10,
4602 0,
4603 0, // maxSetdable = 4096
4604 0,
4605 0,
4606 0,
4607 0, // minSetable = 0
4608 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
4609 0x1f, // rangeFieldsupport
4610 0x40,
4611 0x4b,
4612 0x4c,
4613 0x00, // nominalValue = 5,000,000
4614 0x80,
4615 0x8d,
4616 0x5b,
4617 0x00, // normalMax = 6,000,000
4618 0x00,
4619 0x09,
4620 0x3d,
4621 0x00, // normalMin = 4,000,000
4622 0x40,
4623 0x54,
4624 0x89,
4625 0x00, // rated_max = 9,000,000
4626 0x40,
4627 0x42,
4628 0x0f,
4629 0x00 // rated_min = 1,000,000
4630 };
4631
4632 struct pldm_numeric_effecter_value_pdr decodedPdr;
4633 auto rc =
4634 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4635 EXPECT_EQ(PLDM_SUCCESS, rc);
4636
4637 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT32, decodedPdr.effecter_data_size);
4638 EXPECT_EQ(4096, decodedPdr.max_settable.value_u32);
4639 EXPECT_EQ(0, decodedPdr.min_settable.value_u32);
4640 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
4641 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4642 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
4643 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
4644 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
4645 EXPECT_EQ(9000000, decodedPdr.rated_max.value_u32);
4646 EXPECT_EQ(1000000, decodedPdr.rated_min.value_u32);
4647}
Andrew Jeffery688be622024-05-23 11:22:51 +09304648#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004649
Andrew Jeffery688be622024-05-23 11:22:51 +09304650#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004651TEST(decodeNumericEffecterPdrData, Sint32Test)
4652{
4653 std::vector<uint8_t> pdr1{
4654 0x1,
4655 0x0,
4656 0x0,
4657 0x0, // record handle
4658 0x1, // PDRHeaderVersion
4659 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4660 0x0,
4661 0x0, // recordChangeNumber
4662 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4663 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4664 4 +
4665 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
4666 0, // dataLength
4667 0,
4668 0, // PLDMTerminusHandle
4669 0x1,
4670 0x0, // effecterID=1
4671 PLDM_ENTITY_POWER_SUPPLY,
4672 0, // entityType=Power Supply(120)
4673 1,
4674 0, // entityInstanceNumber
4675 0x1,
4676 0x0, // containerID=1
4677 0x2,
4678 0x0, // effecter_semantic_id=2
4679 PLDM_NO_INIT, // effecterInit
4680 false, // effecterAuxiliaryNames
4681 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4682 0, // unitModifier
4683 0, // rateUnit
4684 0, // baseOEMUnitHandle
4685 0, // auxUnit
4686 0, // auxUnitModifier
4687 0, // auxRateUnit
4688 0, // auxOEMUnitHandle
4689 true, // isLinear
4690 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
4691 0,
4692 0,
4693 0,
4694 0, // resolution
4695 0,
4696 0,
4697 0,
4698 0, // offset
4699 0,
4700 0, // accuracy
4701 0, // plusTolerance
4702 0, // minusTolerance
4703 0,
4704 0,
4705 0x80,
4706 0x3f, // stateTransistionInterval=1.0
4707 0,
4708 0,
4709 0x80,
4710 0x3f, // transition_interval=1.0
4711 0xa0,
4712 0x86,
4713 0x01,
4714 0x00, // maxSetdable = 100000
4715 0x60,
4716 0x79,
4717 0xfe,
4718 0xff, // minSetable = -10000
4719 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
4720 0x1f, // rangeFieldsupport
4721 0,
4722 0,
4723 0,
4724 0, // nominalValue = 0
4725 0x20,
4726 0xa1,
4727 0x07,
4728 0x00, // normalMax = 500,000
4729 0xe0,
4730 0x5e,
4731 0xf8,
4732 0xff, // normalMin = -500,000
4733 0xc0,
4734 0xc6,
4735 0x2d,
4736 0x00, // rated_max = 3,000,000
4737 0x40,
4738 0x39,
4739 0xd2,
4740 0xff // rated_min = -3,000,000
4741 };
4742
4743 struct pldm_numeric_effecter_value_pdr decodedPdr;
4744 auto rc =
4745 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4746 EXPECT_EQ(PLDM_SUCCESS, rc);
4747 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
4748 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
4749 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
4750 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
4751 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4752 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
4753 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
4754 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
4755 EXPECT_EQ(3000000, decodedPdr.rated_max.value_s32);
4756 EXPECT_EQ(-3000000, decodedPdr.rated_min.value_s32);
4757}
Andrew Jeffery688be622024-05-23 11:22:51 +09304758#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004759
Andrew Jeffery688be622024-05-23 11:22:51 +09304760#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004761TEST(decodeNumericEffecterPdrData, Real32Test)
4762{
4763 std::vector<uint8_t> pdr1{
4764 0x1,
4765 0x0,
4766 0x0,
4767 0x0, // record handle
4768 0x1, // PDRHeaderVersion
4769 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4770 0x0,
4771 0x0, // recordChangeNumber
4772 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4773 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4774 4 +
4775 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
4776 0, // dataLength
4777 0,
4778
4779 0, // PLDMTerminusHandle
4780 0x1,
4781 0x0, // effecterID=1
4782 PLDM_ENTITY_POWER_SUPPLY,
4783 0, // entityType=Power Supply(120)
4784 1,
4785 0, // entityInstanceNumber
4786 0x1,
4787 0x0, // containerID=1
4788 0x2,
4789 0x0, // effecter_semantic_id=2
4790 PLDM_NO_INIT, // effecterInit
4791 false, // effecterAuxiliaryNames
4792 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4793 0, // unitModifier
4794 0, // rateUnit
4795 0, // baseOEMUnitHandle
4796 0, // auxUnit
4797 0, // auxUnitModifier
4798 0, // auxRateUnit
4799 0, // auxOEMUnitHandle
4800 true, // isLinear
4801 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
4802 0,
4803 0,
4804 0,
4805 0, // resolution
4806 0,
4807 0,
4808 0,
4809 0, // offset
4810 0,
4811 0, // accuracy
4812 0, // plusTolerance
4813 0, // minusTolerance
4814 0,
4815 0,
4816 0x80,
4817 0x3f, // stateTransistionInterval=1.0
4818 0,
4819 0,
4820 0x80,
4821 0x3f, // transition_interval=1.0
4822 0xa0,
4823 0x86,
4824 0x01,
4825 0x00, // maxSetdable = 100000
4826 0x60,
4827 0x79,
4828 0xfe,
4829 0xff, // minSetable = -10000
4830 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
4831 0x1f, // rangeFieldsupport
4832 0,
4833 0,
4834 0,
4835 0, // nominalValue = 0.0
4836 0x33,
4837 0x33,
4838 0x48,
4839 0x42, // normalMax = 50.05
4840 0x33,
4841 0x33,
4842 0x48,
4843 0xc2, // normalMin = -50.05
4844 0x62,
4845 0x00,
4846 0x96,
4847 0x43, // rated_max = 300.003
4848 0x62,
4849 0x00,
4850 0x96,
4851 0xc3 // rated_min = -300.003
4852 };
4853
4854 struct pldm_numeric_effecter_value_pdr decodedPdr;
4855 auto rc =
4856 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4857 EXPECT_EQ(PLDM_SUCCESS, rc);
4858
4859 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
4860 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
4861 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
4862 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
4863 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
4864 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
4865 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4866 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
4867 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
4868 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
4869 EXPECT_FLOAT_EQ(300.003f, decodedPdr.rated_max.value_f32);
4870 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.rated_min.value_f32);
4871}
Andrew Jeffery688be622024-05-23 11:22:51 +09304872#endif
Tal Yacobia6fa5552024-05-05 16:57:38 +03004873
Tal Yacobia6fa5552024-05-05 16:57:38 +03004874TEST(GetStateEffecterStates, testEncodeAndDecodeRequest)
4875{
4876 std::array<uint8_t, hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
4877 requestMsg{};
4878
4879 constexpr std::array<uint8_t,
4880 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
4881 expectedRequestMsg{
4882 {0x80, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES, 1, 0xab}};
4883
4884 constexpr uint16_t effecter_id = 0xab01;
4885
4886 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
4887
4888 auto rc = encode_get_state_effecter_states_req(
4889 0, effecter_id, request, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
4890
4891 EXPECT_EQ(rc, PLDM_SUCCESS);
4892 EXPECT_EQ(requestMsg, expectedRequestMsg);
4893
4894 uint16_t ret_effecter_id;
4895
4896 rc = decode_get_state_effecter_states_req(
4897 request, requestMsg.size() - hdrSize, &ret_effecter_id);
4898
4899 EXPECT_EQ(rc, PLDM_SUCCESS);
4900 EXPECT_EQ(effecter_id, ret_effecter_id);
4901
4902 // Test invalid length decode request
4903
4904 rc = decode_get_state_effecter_states_req(
4905 request, requestMsg.size() - hdrSize - 1, &ret_effecter_id);
4906
Tal Yacobif490a382024-05-31 09:57:36 +03004907 EXPECT_EQ(rc, -EOVERFLOW);
Tal Yacobia6fa5552024-05-05 16:57:38 +03004908}
Tal Yacobia6fa5552024-05-05 16:57:38 +03004909
Tal Yacobia6fa5552024-05-05 16:57:38 +03004910TEST(GetStateEffecterStates, testBadEncodeRequest)
4911{
4912 std::vector<uint8_t> requestMsg(hdrSize +
4913 PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
4914
4915 auto rc = encode_get_state_effecter_states_req(
4916 0, 0, nullptr, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
Tal Yacobif490a382024-05-31 09:57:36 +03004917 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03004918}
Tal Yacobia6fa5552024-05-05 16:57:38 +03004919
Tal Yacobia6fa5552024-05-05 16:57:38 +03004920TEST(GetStateEffecterStates, testBadDecodeRequest)
4921{
4922 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
4923 requestMsg{};
4924
4925 auto rc = decode_get_state_effecter_states_req(
4926 nullptr, requestMsg.size() - hdrSize, nullptr);
4927
Tal Yacobif490a382024-05-31 09:57:36 +03004928 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03004929}
Tal Yacobia6fa5552024-05-05 16:57:38 +03004930
Tal Yacobia6fa5552024-05-05 16:57:38 +03004931TEST(GetStateEffecterStates, testEncodeAndDecodeResponse)
4932{
4933 constexpr uint8_t comp_effecterCnt = 0x2;
4934 constexpr uint8_t completionCode = 0;
4935 std::array<uint8_t,
4936 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
4937 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * comp_effecterCnt>
4938 expectedResponseMsg{{0, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES,
4939 completionCode, comp_effecterCnt,
4940 EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2,
4941 EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}};
4942
4943 decltype(expectedResponseMsg) responseMsg{};
4944
4945 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
4946
4947 std::array<get_effecter_state_field, comp_effecterCnt> stateField{
4948 {{EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2},
4949 {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}}};
4950
4951 struct pldm_get_state_effecter_states_resp resp_fields
4952 {
4953 PLDM_SUCCESS, comp_effecterCnt,
4954 {
4955 stateField[0], stateField[1]
4956 }
4957 };
4958
4959 auto rc = encode_get_state_effecter_states_resp(
4960 0, &resp_fields, response, responseMsg.size() - hdrSize);
4961
4962 EXPECT_EQ(rc, PLDM_SUCCESS);
4963 EXPECT_EQ(expectedResponseMsg, responseMsg);
4964
4965 struct pldm_get_state_effecter_states_resp ret_resp_fields;
4966
4967 rc = decode_get_state_effecter_states_resp(
4968 response, responseMsg.size() - hdrSize, &ret_resp_fields);
4969
4970 EXPECT_EQ(rc, PLDM_SUCCESS);
4971 EXPECT_EQ(completionCode, ret_resp_fields.completion_code);
4972 EXPECT_EQ(comp_effecterCnt, ret_resp_fields.comp_effecter_count);
4973 EXPECT_EQ(stateField[0].effecter_op_state,
4974 ret_resp_fields.field[0].effecter_op_state);
4975 EXPECT_EQ(stateField[0].pending_state,
4976 ret_resp_fields.field[0].pending_state);
4977 EXPECT_EQ(stateField[0].present_state,
4978 ret_resp_fields.field[0].present_state);
4979 EXPECT_EQ(stateField[1].effecter_op_state,
4980 ret_resp_fields.field[1].effecter_op_state);
4981 EXPECT_EQ(stateField[1].pending_state,
4982 ret_resp_fields.field[1].pending_state);
4983 EXPECT_EQ(stateField[1].present_state,
4984 ret_resp_fields.field[1].present_state);
4985
4986 // Test invalid length decode
4987
4988 rc = decode_get_state_effecter_states_resp(
4989 response,
4990 responseMsg.size() - hdrSize + PLDM_GET_EFFECTER_STATE_FIELD_SIZE,
4991 &ret_resp_fields);
4992
Tal Yacobif490a382024-05-31 09:57:36 +03004993 EXPECT_EQ(rc, -EBADMSG);
Tal Yacobia6fa5552024-05-05 16:57:38 +03004994}
Tal Yacobia6fa5552024-05-05 16:57:38 +03004995
Tal Yacobia6fa5552024-05-05 16:57:38 +03004996TEST(GetStateEffecterStates, testBadEncodeResponse)
4997{
4998 struct pldm_get_state_effecter_states_resp resp
4999 {
5000 PLDM_SUCCESS, 0,
5001 {
5002 }
5003 };
5004 auto rc = decode_get_state_effecter_states_resp(nullptr, 0, &resp);
5005
Tal Yacobif490a382024-05-31 09:57:36 +03005006 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005007}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005008
Tal Yacobia6fa5552024-05-05 16:57:38 +03005009TEST(GetStateEffecterStates, testBadDecodeResponse)
5010{
5011 std::array<uint8_t, hdrSize +
5012 PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
5013 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * 2>
5014 responseMsg{};
5015
5016 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
5017
5018 auto rc = decode_get_state_effecter_states_resp(
5019 response, responseMsg.size() - hdrSize, nullptr);
5020
Tal Yacobif490a382024-05-31 09:57:36 +03005021 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005022}
Thu Nguyendacfa352024-06-22 09:53:15 +00005023
5024[[maybe_unused]] static size_t str16len(char16_t* startptr)
5025{
5026 char16_t* endptr = startptr;
5027 while (*endptr)
5028 {
5029 endptr++;
5030 }
5031 return endptr - startptr;
5032}
5033
5034#ifdef LIBPLDM_API_TESTING
5035TEST(decodeEntityAuxNamePdrData, GoodTest)
5036{
5037 std::vector<uint8_t> pdr1{
5038 // Common PDR Header
5039 0x1, 0x0, 0x0, 0x0, // record handle
5040 0x1, // PDRHeaderVersion
5041 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5042 0x1,
5043 0x0, // recordChangeNumber
5044 0x27,
5045 0, // dataLength
5046 /* Entity Auxiliary Names PDR Data*/
5047 3,
5048 0, // entityType system software
5049 0x1,
5050 0x0, // Entity instance number =1
5051 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5052 0, // Overal system
5053 0, // shared Name Count one name only
5054 03, // nameStringCount
5055 0x65, 0x6e, 0x00, // Language Tag "en"
5056 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5057 0x66, 0x6e, 0x00, // Language Tag "en"
5058 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5059 0x67, 0x6e, 0x00, // Language Tag "en"
5060 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5061 };
5062
5063 const char expectTag0[] = {0x65, 0x6e, 0x00};
5064 const char expectTag1[] = {0x66, 0x6e, 0x00};
5065 const char expectTag2[] = {0x67, 0x6e, 0x00};
5066 const char expectName0[] = {0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00};
5067 const char expectName1[] = {0x00, 0x53, 0x00, 0x31, 0x00, 0x00};
5068 const char expectName2[] = {0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00};
5069 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5070 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5071 auto names_size = pdr1.size() - names_offset;
5072 size_t length = 0;
5073
5074 size_t decodedPdrSize =
5075 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5076 auto decodedPdr =
5077 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5078 EXPECT_NE(nullptr, decodedPdr);
5079
5080 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5081 decodedPdr, decodedPdrSize);
5082
5083 EXPECT_EQ(0, rc);
5084 EXPECT_EQ(1, decodedPdr->hdr.record_handle);
5085 EXPECT_EQ(1, decodedPdr->hdr.version);
5086 EXPECT_EQ(PLDM_ENTITY_AUXILIARY_NAMES_PDR, decodedPdr->hdr.type);
5087 EXPECT_EQ(1, decodedPdr->hdr.record_change_num);
5088 EXPECT_EQ(pdr1.size() - sizeof(struct pldm_pdr_hdr),
5089 decodedPdr->hdr.length);
5090 EXPECT_EQ(3, decodedPdr->container.entity_type);
5091 EXPECT_EQ(1, decodedPdr->container.entity_instance_num);
5092 EXPECT_EQ(PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5093 decodedPdr->container.entity_container_id);
5094 EXPECT_EQ(0, decodedPdr->shared_name_count);
5095 EXPECT_EQ(3, decodedPdr->name_string_count);
5096
5097 decodedPdr->names = (struct pldm_entity_auxiliary_name*)calloc(
5098 decodedPdr->name_string_count,
5099 sizeof(struct pldm_entity_auxiliary_name));
5100 EXPECT_NE(nullptr, decodedPdr->names);
5101
5102 rc = decode_pldm_entity_auxiliary_names_pdr_index(decodedPdr);
5103 EXPECT_EQ(0, rc);
5104
5105 length = strlen(decodedPdr->names[0].tag);
5106 EXPECT_EQ(strlen(expectTag0), length);
5107 EXPECT_EQ(strncmp(expectTag0, decodedPdr->names[0].tag, length + 1), 0);
5108
5109 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5110 ASSERT_EQ(0,
5111 (uintptr_t)decodedPdr->names[0].name & (alignof(char16_t) - 1));
5112 // NOLINTEND(clang-analyzer-unix.Malloc)
5113 length = str16len((char16_t*)decodedPdr->names[0].name);
5114 EXPECT_EQ(str16len((char16_t*)expectName0), length);
5115 EXPECT_EQ(3, str16len((char16_t*)expectName0));
5116 EXPECT_EQ(memcmp(expectName0, decodedPdr->names[0].name,
5117 sizeof(char16_t) * (length + 1)),
5118 0);
5119
5120 length = strlen(decodedPdr->names[1].tag);
5121 EXPECT_EQ(strlen(expectTag1), length);
5122 EXPECT_EQ(strncmp(expectTag1, decodedPdr->names[1].tag, length + 1), 0);
5123
5124 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5125 ASSERT_EQ(0,
5126 (uintptr_t)decodedPdr->names[1].name & (alignof(char16_t) - 1));
5127 // NOLINTEND(clang-analyzer-unix.Malloc)
5128 length = str16len((char16_t*)decodedPdr->names[1].name);
5129 EXPECT_EQ(str16len((char16_t*)expectName1), length);
5130 EXPECT_EQ(2, str16len((char16_t*)expectName1));
5131 EXPECT_EQ(memcmp(expectName1, decodedPdr->names[1].name,
5132 sizeof(char16_t) * (length + 1)),
5133 0);
5134
5135 length = strlen(decodedPdr->names[2].tag);
5136 EXPECT_EQ(strlen(expectTag2), length);
5137 EXPECT_EQ(strncmp(expectTag2, decodedPdr->names[2].tag, length + 1), 0);
5138
5139 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5140 ASSERT_EQ(0,
5141 (uintptr_t)decodedPdr->names[2].name & (alignof(char16_t) - 1));
5142 // NOLINTEND(clang-analyzer-unix.Malloc)
5143 length = str16len((char16_t*)decodedPdr->names[2].name);
5144 EXPECT_EQ(str16len((char16_t*)expectName2), length);
5145 EXPECT_EQ(3, str16len((char16_t*)expectName2));
5146 EXPECT_EQ(memcmp(expectName2, decodedPdr->names[2].name,
5147 sizeof(char16_t) * (length + 1)),
5148 0);
5149
5150 free(decodedPdr->names);
5151 free(decodedPdr);
5152}
5153#endif
5154
5155#ifdef LIBPLDM_API_TESTING
5156TEST(decodeEntityAuxNamePdrData, BadTest)
5157{
5158 std::vector<uint8_t> pdr1{
5159 // Common PDR Header
5160 0x1, 0x0, 0x0, 0x0, // record handle
5161 0x1, // PDRHeaderVersion
5162 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5163 0x1,
5164 0x0, // recordChangeNumber
5165 0x25, // correct size is 0x27, input invalid size
5166 0, // dataLength
5167 /* Entity Auxiliary Names PDR Data*/
5168 3,
5169 0, // entityType system software
5170 0x1,
5171 0x0, // Entity instance number =1
5172 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5173 0, // Overal system
5174 0, // shared Name Count one name only
5175 0, // Invalid nameStringCount
5176 0x65, 0x6e, 0x00, // Language Tag "en"
5177 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5178 0x66, 0x6e, 0x00, // Language Tag "en"
5179 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5180 0x67, 0x6e, 0x00, // Language Tag "en"
5181 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5182 };
5183
5184 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5185 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5186 auto names_size = pdr1.size() - names_offset;
5187
5188 size_t decodedPdrSize =
5189 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5190 auto decodedPdr =
5191 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5192
5193 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5194 decodedPdr, decodedPdrSize);
5195
5196 EXPECT_EQ(-EBADMSG, rc);
5197 free(decodedPdr);
5198}
5199#endif