blob: e7f4253435c498b01843241f2448d7461bac6d66 [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09302
3#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05304#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +09305#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05306#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +09307
8#include "libpldm/base.h"
Andrew Jeffery7992eb82023-04-06 16:13:53 +09309#include "libpldm/entity.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +093010#include "libpldm/platform.h"
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053011#include "pldm_types.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +093012
13#include <gtest/gtest.h>
14
15constexpr auto hdrSize = sizeof(pldm_msg_hdr);
16
17TEST(SetStateEffecterStates, testEncodeResponse)
18{
19 std::array<uint8_t,
20 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
21 responseMsg{};
22 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
23 uint8_t completionCode = 0;
24
25 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response);
26
27 EXPECT_EQ(rc, PLDM_SUCCESS);
28 EXPECT_EQ(completionCode, response->payload[0]);
29}
30
31TEST(SetStateEffecterStates, testEncodeRequest)
32{
33 std::array<uint8_t,
34 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
35 requestMsg{};
36 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
37
38 uint16_t effecterId = 0x0A;
39 uint8_t compEffecterCnt = 0x2;
40 std::array<set_effecter_state_field, 8> stateField{};
41 stateField[0] = {PLDM_REQUEST_SET, 2};
42 stateField[1] = {PLDM_REQUEST_SET, 3};
43
44 auto rc = encode_set_state_effecter_states_req(
45 0, effecterId, compEffecterCnt, stateField.data(), request);
46
47 EXPECT_EQ(rc, PLDM_SUCCESS);
48 EXPECT_EQ(effecterId, request->payload[0]);
49 EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
50 EXPECT_EQ(stateField[0].set_request,
51 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
52 EXPECT_EQ(stateField[0].effecter_state,
53 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
54 sizeof(stateField[0].set_request)]);
55 EXPECT_EQ(stateField[1].set_request,
56 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
57 sizeof(stateField[0])]);
58 EXPECT_EQ(stateField[1].effecter_state,
59 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
60 sizeof(stateField[0]) +
61 sizeof(stateField[1].set_request)]);
62}
63
64TEST(SetStateEffecterStates, testGoodDecodeResponse)
65{
66 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
67 responseMsg{};
68
69 uint8_t retcompletion_code = 0;
70
71 responseMsg[hdrSize] = PLDM_SUCCESS;
72
73 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
74
75 auto rc = decode_set_state_effecter_states_resp(
76 response, responseMsg.size() - hdrSize, &retcompletion_code);
77
78 EXPECT_EQ(rc, PLDM_SUCCESS);
79 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
80}
81
82TEST(SetStateEffecterStates, testGoodDecodeRequest)
83{
84 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
85 requestMsg{};
86
87 uint16_t effecterId = 0x32;
88 uint16_t effecterIdLE = htole16(effecterId);
89 uint8_t compEffecterCnt = 0x2;
90
91 std::array<set_effecter_state_field, 8> stateField{};
92 stateField[0] = {PLDM_REQUEST_SET, 3};
93 stateField[1] = {PLDM_REQUEST_SET, 4};
94
95 uint16_t retEffecterId = 0;
96 uint8_t retCompEffecterCnt = 0;
97
98 std::array<set_effecter_state_field, 8> retStateField{};
99
100 memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE));
101 memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt,
102 sizeof(compEffecterCnt));
103 memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) +
104 hdrSize,
105 &stateField, sizeof(stateField));
106
107 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
108
109 auto rc = decode_set_state_effecter_states_req(
110 request, requestMsg.size() - hdrSize, &retEffecterId,
111 &retCompEffecterCnt, retStateField.data());
112
113 EXPECT_EQ(rc, PLDM_SUCCESS);
114 EXPECT_EQ(effecterId, retEffecterId);
115 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt);
116 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request);
117 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
118 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request);
119 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
120}
121
122TEST(SetStateEffecterStates, testBadDecodeRequest)
123{
124 const struct pldm_msg* msg = NULL;
125
126 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
127 NULL, NULL);
128
129 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
130}
131
132TEST(SetStateEffecterStates, testBadDecodeResponse)
133{
134 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
135 responseMsg{};
136
137 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
138
139 auto rc = decode_set_state_effecter_states_resp(response,
140 responseMsg.size(), NULL);
141
142 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
143}
144
145TEST(GetPDR, testGoodEncodeResponse)
146{
147 uint8_t completionCode = 0;
148 uint32_t nextRecordHndl = 0x12;
149 uint32_t nextDataTransferHndl = 0x13;
150 uint8_t transferFlag = PLDM_END;
151 uint16_t respCnt = 0x5;
152 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
153 uint8_t transferCRC = 6;
154
155 // + size of record data and transfer CRC
156 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
157 recordData.size() + 1);
158 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
159
160 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
161 nextDataTransferHndl, transferFlag, respCnt,
162 recordData.data(), transferCRC, response);
163
164 EXPECT_EQ(rc, PLDM_SUCCESS);
165 struct pldm_get_pdr_resp* resp =
166 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
167
168 EXPECT_EQ(completionCode, resp->completion_code);
169 EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle));
170 EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle));
171 EXPECT_EQ(transferFlag, resp->transfer_flag);
172 EXPECT_EQ(respCnt, le16toh(resp->response_count));
173 EXPECT_EQ(0,
174 memcmp(recordData.data(), resp->record_data, recordData.size()));
175 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
176 recordData.size()),
177 transferCRC);
178
179 transferFlag = PLDM_START_AND_END; // No CRC in this case
180 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
181 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
182 nextDataTransferHndl, transferFlag, respCnt,
183 recordData.data(), transferCRC, response);
184 EXPECT_EQ(rc, PLDM_SUCCESS);
185}
186
187TEST(GetPDR, testBadEncodeResponse)
188{
189 uint32_t nextRecordHndl = 0x12;
190 uint32_t nextDataTransferHndl = 0x13;
191 uint8_t transferFlag = PLDM_START_AND_END;
192 uint16_t respCnt = 0x5;
193 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
194 uint8_t transferCRC = 0;
195
196 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
197 nextDataTransferHndl, transferFlag, respCnt,
198 recordData.data(), transferCRC, nullptr);
199
200 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
201}
202
203TEST(GetPDR, testGoodDecodeRequest)
204{
205 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
206
207 uint32_t recordHndl = 0x32;
208 uint32_t dataTransferHndl = 0x11;
209 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
210 uint16_t requestCnt = 0x5;
211 uint16_t recordChangeNum = 0x01;
212
213 uint32_t retRecordHndl = 0;
214 uint32_t retDataTransferHndl = 0;
215 uint8_t retTransferOpFlag = 0;
216 uint16_t retRequestCnt = 0;
217 uint16_t retRecordChangeNum = 0;
218
219 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
220 struct pldm_get_pdr_req* request =
221 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
222
223 request->record_handle = htole32(recordHndl);
224 request->data_transfer_handle = htole32(dataTransferHndl);
225 request->transfer_op_flag = transferOpFlag;
226 request->request_count = htole16(requestCnt);
227 request->record_change_number = htole16(recordChangeNum);
228
229 auto rc = decode_get_pdr_req(
230 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
231 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
232
233 EXPECT_EQ(rc, PLDM_SUCCESS);
234 EXPECT_EQ(retRecordHndl, recordHndl);
235 EXPECT_EQ(retDataTransferHndl, dataTransferHndl);
236 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
237 EXPECT_EQ(retRequestCnt, requestCnt);
238 EXPECT_EQ(retRecordChangeNum, recordChangeNum);
239}
240
241TEST(GetPDR, testBadDecodeRequest)
242{
243 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
244 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
245
246 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
247 NULL);
248
249 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
250}
251
252TEST(GetPDR, testGoodEncodeRequest)
253{
254 uint32_t record_hndl = 0;
255 uint32_t data_transfer_hndl = 0;
256 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
257 uint16_t request_cnt = 20;
258 uint16_t record_chg_num = 0;
259
260 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
261 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
262
263 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
264 transfer_op_flag, request_cnt, record_chg_num,
265 request, PLDM_GET_PDR_REQ_BYTES);
266 EXPECT_EQ(rc, PLDM_SUCCESS);
267 struct pldm_get_pdr_req* req =
268 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
269 EXPECT_EQ(record_hndl, le32toh(req->record_handle));
270 EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle));
271 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
272 EXPECT_EQ(request_cnt, le16toh(req->request_count));
273 EXPECT_EQ(record_chg_num, le16toh(req->record_change_number));
274}
275
276TEST(GetPDR, testBadEncodeRequest)
277{
278 uint32_t record_hndl = 0;
279 uint32_t data_transfer_hndl = 0;
280 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
281 uint16_t request_cnt = 32;
282 uint16_t record_chg_num = 0;
283
284 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
285 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
286
287 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
288 transfer_op_flag, request_cnt, record_chg_num,
289 nullptr, PLDM_GET_PDR_REQ_BYTES);
290 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
291
292 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
293 transfer_op_flag, request_cnt, record_chg_num,
294 request, PLDM_GET_PDR_REQ_BYTES + 1);
295 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
296}
297
298TEST(GetPDR, testGoodDecodeResponse)
299{
300 const char* recordData = "123456789";
301 uint8_t completionCode = PLDM_SUCCESS;
302 uint32_t nextRecordHndl = 0;
303 uint32_t nextDataTransferHndl = 0;
304 uint8_t transferFlag = PLDM_END;
305 constexpr uint16_t respCnt = 9;
306 uint8_t transferCRC = 96;
307 size_t recordDataLength = 32;
308 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
309 sizeof(transferCRC)>
310 responseMsg{};
311
312 uint8_t retCompletionCode = 0;
313 uint8_t retRecordData[32] = {0};
314 uint32_t retNextRecordHndl = 0;
315 uint32_t retNextDataTransferHndl = 0;
316 uint8_t retTransferFlag = 0;
317 uint16_t retRespCnt = 0;
318 uint8_t retTransferCRC = 0;
319
320 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
321 struct pldm_get_pdr_resp* resp =
322 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
323 resp->completion_code = completionCode;
324 resp->next_record_handle = htole32(nextRecordHndl);
325 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
326 resp->transfer_flag = transferFlag;
327 resp->response_count = htole16(respCnt);
328 memcpy(resp->record_data, recordData, respCnt);
329 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
330
331 auto rc = decode_get_pdr_resp(
332 response, responseMsg.size() - hdrSize, &retCompletionCode,
333 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
334 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
335 EXPECT_EQ(rc, PLDM_SUCCESS);
336 EXPECT_EQ(retCompletionCode, completionCode);
337 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
338 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
339 EXPECT_EQ(retTransferFlag, transferFlag);
340 EXPECT_EQ(retRespCnt, respCnt);
341 EXPECT_EQ(retTransferCRC, transferCRC);
342 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
343}
344
345TEST(GetPDR, testBadDecodeResponse)
346{
347 const char* recordData = "123456789";
348 uint8_t completionCode = PLDM_SUCCESS;
349 uint32_t nextRecordHndl = 0;
350 uint32_t nextDataTransferHndl = 0;
351 uint8_t transferFlag = PLDM_END;
352 constexpr uint16_t respCnt = 9;
353 uint8_t transferCRC = 96;
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930354 size_t recordDataLength = respCnt - 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930355 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
356 sizeof(transferCRC)>
357 responseMsg{};
358
359 uint8_t retCompletionCode = 0;
360 uint8_t retRecordData[32] = {0};
361 uint32_t retNextRecordHndl = 0;
362 uint32_t retNextDataTransferHndl = 0;
363 uint8_t retTransferFlag = 0;
364 uint16_t retRespCnt = 0;
365 uint8_t retTransferCRC = 0;
366
367 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
368 struct pldm_get_pdr_resp* resp =
369 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
370 resp->completion_code = completionCode;
371 resp->next_record_handle = htole32(nextRecordHndl);
372 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
373 resp->transfer_flag = transferFlag;
374 resp->response_count = htole16(respCnt);
375 memcpy(resp->record_data, recordData, respCnt);
376 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
377
378 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
379 NULL, NULL, NULL, NULL, NULL, 0, NULL);
380 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
381
382 rc = decode_get_pdr_resp(
383 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
384 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
385 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
386 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
387}
388
389TEST(GetPDRRepositoryInfo, testGoodEncodeResponse)
390{
391 uint8_t completionCode = 0;
392 uint8_t repositoryState = PLDM_AVAILABLE;
393 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
394 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
395 uint32_t recordCount = 100;
396 uint32_t repositorySize = 100;
397 uint32_t largestRecordSize = UINT32_MAX;
398 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
399
400 std::vector<uint8_t> responseMsg(hdrSize +
401 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES);
402 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
403
404 auto rc = encode_get_pdr_repository_info_resp(
405 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
406 recordCount, repositorySize, largestRecordSize,
407 dataTransferHandleTimeout, response);
408
409 EXPECT_EQ(rc, PLDM_SUCCESS);
410 struct pldm_pdr_repository_info_resp* resp =
411 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
412 response->payload);
413
414 EXPECT_EQ(completionCode, resp->completion_code);
415 EXPECT_EQ(repositoryState, resp->repository_state);
416 EXPECT_EQ(0, memcmp(updateTime, resp->update_time, PLDM_TIMESTAMP104_SIZE));
417 EXPECT_EQ(0, memcmp(oemUpdateTime, resp->oem_update_time,
418 PLDM_TIMESTAMP104_SIZE));
419 EXPECT_EQ(recordCount, le32toh(resp->record_count));
420 EXPECT_EQ(repositorySize, le32toh(resp->repository_size));
421 EXPECT_EQ(largestRecordSize, le32toh(resp->largest_record_size));
422 EXPECT_EQ(dataTransferHandleTimeout, resp->data_transfer_handle_timeout);
423}
424
425TEST(GetPDRRepositoryInfo, testBadEncodeResponse)
426{
427 uint8_t repositoryState = PLDM_AVAILABLE;
428 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
429 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
430 uint32_t recordCount = 100;
431 uint32_t repositorySize = 100;
432 uint32_t largestRecordSize = UINT32_MAX;
433 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
434
435 auto rc = encode_get_pdr_repository_info_resp(
436 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
437 recordCount, repositorySize, largestRecordSize,
438 dataTransferHandleTimeout, nullptr);
439 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
440}
441
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800442TEST(GetPDRRepositoryInfo, testGoodDecodeResponse)
443{
444 uint8_t completionCode = PLDM_SUCCESS;
445 uint8_t repositoryState = PLDM_AVAILABLE;
446 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
447 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
448 uint32_t recordCount = 100;
449 uint32_t repositorySize = 100;
450 uint32_t largestRecordSize = UINT32_MAX;
451 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
452
453 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
454 responseMsg{};
455 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
456 struct pldm_pdr_repository_info_resp* resp =
457 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
458 response->payload);
459 resp->completion_code = completionCode;
460 resp->repository_state = repositoryState;
461 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
462 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
463 resp->record_count = htole32(recordCount);
464 resp->repository_size = htole32(repositorySize);
465 resp->largest_record_size = htole32(largestRecordSize);
466 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
467
468 uint8_t retCompletionCode = 0;
469 uint8_t retRepositoryState = 0;
470 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
471 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
472 uint32_t retRecordCount = 0;
473 uint32_t retRepositorySize = 0;
474 uint32_t retLargestRecordSize = 0;
475 uint8_t retDataTransferHandleTimeout = 0;
476
477 auto rc = decode_get_pdr_repository_info_resp(
478 response, responseMsg.size() - hdrSize, &retCompletionCode,
479 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
480 &retRepositorySize, &retLargestRecordSize,
481 &retDataTransferHandleTimeout);
482
483 EXPECT_EQ(rc, PLDM_SUCCESS);
484 EXPECT_EQ(completionCode, retCompletionCode);
485 EXPECT_EQ(repositoryState, retRepositoryState);
486 EXPECT_EQ(0, memcmp(updateTime, retUpdateTime, PLDM_TIMESTAMP104_SIZE));
487 EXPECT_EQ(0,
488 memcmp(oemUpdateTime, retOemUpdateTime, PLDM_TIMESTAMP104_SIZE));
489 EXPECT_EQ(recordCount, recordCount);
490 EXPECT_EQ(repositorySize, repositorySize);
491 EXPECT_EQ(largestRecordSize, largestRecordSize);
492 EXPECT_EQ(dataTransferHandleTimeout, dataTransferHandleTimeout);
493}
494
495TEST(GetPDRRepositoryInfo, testBadDecodeResponse)
496{
497 uint8_t completionCode = PLDM_SUCCESS;
498 uint8_t repositoryState = PLDM_AVAILABLE;
499 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
500 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
501 uint32_t recordCount = htole32(100);
502 uint32_t repositorySize = htole32(100);
503 uint32_t largestRecordSize = htole32(UINT32_MAX);
504 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
505
506 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
507 responseMsg{};
508 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
509 struct pldm_pdr_repository_info_resp* resp =
510 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
511 response->payload);
512 resp->completion_code = completionCode;
513 resp->repository_state = repositoryState;
514 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
515 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
516 resp->record_count = recordCount;
517 resp->repository_size = repositorySize;
518 resp->largest_record_size = largestRecordSize;
519 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
520
521 uint8_t retCompletionCode = 0;
522 uint8_t retRepositoryState = 0;
523 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
524 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
525 uint32_t retRecordCount = 0;
526 uint32_t retRepositorySize = 0;
527 uint32_t retLargestRecordSize = 0;
528 uint8_t retDataTransferHandleTimeout = 0;
529
530 auto rc = decode_get_pdr_repository_info_resp(
531 response, responseMsg.size() - hdrSize, NULL, NULL, NULL, NULL, NULL,
532 NULL, NULL, NULL);
533 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
534
535 rc = decode_get_pdr_repository_info_resp(
536 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
537 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
538 &retRepositorySize, &retLargestRecordSize,
539 &retDataTransferHandleTimeout);
540 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
541
542 resp->repository_state = PLDM_FAILED + 1;
543 rc = decode_get_pdr_repository_info_resp(
544 response, responseMsg.size() - hdrSize, &retCompletionCode,
545 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
546 &retRepositorySize, &retLargestRecordSize,
547 &retDataTransferHandleTimeout);
548 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
549}
550
Andrew Jeffery9c766792022-08-10 23:12:49 +0930551TEST(SetNumericEffecterValue, testGoodDecodeRequest)
552{
553 std::array<uint8_t,
554 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3>
555 requestMsg{};
556
557 uint16_t effecter_id = 32768;
558 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32;
559 uint32_t effecter_value = 123456789;
560
561 uint16_t reteffecter_id;
562 uint8_t reteffecter_data_size;
563 uint8_t reteffecter_value[4];
564
565 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
566 struct pldm_set_numeric_effecter_value_req* request =
567 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
568 req->payload);
569
570 request->effecter_id = htole16(effecter_id);
571 request->effecter_data_size = effecter_data_size;
572 uint32_t effecter_value_le = htole32(effecter_value);
573 memcpy(request->effecter_value, &effecter_value_le,
574 sizeof(effecter_value_le));
575
576 auto rc = decode_set_numeric_effecter_value_req(
577 req, requestMsg.size() - hdrSize, &reteffecter_id,
578 &reteffecter_data_size, reteffecter_value);
579
580 uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value));
581 EXPECT_EQ(rc, PLDM_SUCCESS);
582 EXPECT_EQ(reteffecter_id, effecter_id);
583 EXPECT_EQ(reteffecter_data_size, effecter_data_size);
584 EXPECT_EQ(value, effecter_value);
585}
586
587TEST(SetNumericEffecterValue, testBadDecodeRequest)
588{
589 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES>
590 requestMsg{};
591
592 auto rc = decode_set_numeric_effecter_value_req(
593 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL);
594 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
595
596 uint16_t effecter_id = 0x10;
597 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
598 uint8_t effecter_value = 1;
599
600 uint16_t reteffecter_id;
601 uint8_t reteffecter_data_size;
602 uint8_t reteffecter_value[4];
603
604 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
605 struct pldm_set_numeric_effecter_value_req* request =
606 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
607 req->payload);
608
609 request->effecter_id = effecter_id;
610 request->effecter_data_size = effecter_data_size;
611 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
612
613 rc = decode_set_numeric_effecter_value_req(
614 req, requestMsg.size() - hdrSize - 1, &reteffecter_id,
615 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
616 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
617}
618
619TEST(SetNumericEffecterValue, testGoodEncodeRequest)
620{
621 uint16_t effecter_id = 0;
622 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16;
623 uint16_t effecter_value = 65534;
624
625 std::vector<uint8_t> requestMsg(
626 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
627 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
628
629 auto rc = encode_set_numeric_effecter_value_req(
630 0, effecter_id, effecter_data_size,
631 reinterpret_cast<uint8_t*>(&effecter_value), request,
632 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
633 EXPECT_EQ(rc, PLDM_SUCCESS);
634
635 struct pldm_set_numeric_effecter_value_req* req =
636 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
637 request->payload);
638 EXPECT_EQ(effecter_id, req->effecter_id);
639 EXPECT_EQ(effecter_data_size, req->effecter_data_size);
640 uint16_t* val = (uint16_t*)req->effecter_value;
641 *val = le16toh(*val);
642 EXPECT_EQ(effecter_value, *val);
643}
644
645TEST(SetNumericEffecterValue, testBadEncodeRequest)
646{
647 std::vector<uint8_t> requestMsg(
648 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
649 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
650
651 auto rc = encode_set_numeric_effecter_value_req(
652 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
653 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
654
655 uint16_t effecter_value;
656 rc = encode_set_numeric_effecter_value_req(
657 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
658 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
659 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
660}
661
662TEST(SetNumericEffecterValue, testGoodDecodeResponse)
663{
664 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
665 responseMsg{};
666
667 uint8_t completion_code = 0xA0;
668
669 uint8_t retcompletion_code;
670
671 memcpy(responseMsg.data() + hdrSize, &completion_code,
672 sizeof(completion_code));
673
674 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
675
676 auto rc = decode_set_numeric_effecter_value_resp(
677 response, responseMsg.size() - hdrSize, &retcompletion_code);
678
679 EXPECT_EQ(rc, PLDM_SUCCESS);
680 EXPECT_EQ(completion_code, retcompletion_code);
681}
682
683TEST(SetNumericEffecterValue, testBadDecodeResponse)
684{
685 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
686 responseMsg{};
687
688 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
689
690 auto rc = decode_set_numeric_effecter_value_resp(response,
691 responseMsg.size(), NULL);
692
693 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
694}
695
696TEST(SetNumericEffecterValue, testGoodEncodeResponse)
697{
698 std::array<uint8_t, sizeof(pldm_msg_hdr) +
699 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
700 responseMsg{};
701 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
702 uint8_t completionCode = 0;
703
704 auto rc = encode_set_numeric_effecter_value_resp(
705 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
706
707 EXPECT_EQ(rc, PLDM_SUCCESS);
708 EXPECT_EQ(completionCode, response->payload[0]);
709}
710
711TEST(SetNumericEffecterValue, testBadEncodeResponse)
712{
713 auto rc = encode_set_numeric_effecter_value_resp(
714 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
715 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
716}
717
718TEST(GetStateSensorReadings, testGoodEncodeResponse)
719{
720 std::array<uint8_t, hdrSize +
721 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
722 sizeof(get_sensor_state_field) * 2>
723 responseMsg{};
724
725 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
726 uint8_t completionCode = 0;
727 uint8_t comp_sensorCnt = 0x2;
728
729 std::array<get_sensor_state_field, 2> stateField{};
730 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_NORMAL,
731 PLDM_SENSOR_WARNING, PLDM_SENSOR_UNKNOWN};
732 stateField[1] = {PLDM_SENSOR_FAILED, PLDM_SENSOR_UPPERFATAL,
733 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_FATAL};
734
735 auto rc = encode_get_state_sensor_readings_resp(
736 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response);
737
738 struct pldm_get_state_sensor_readings_resp* resp =
739 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
740 response->payload);
741
742 EXPECT_EQ(rc, PLDM_SUCCESS);
743 EXPECT_EQ(completionCode, resp->completion_code);
744 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count);
745 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state);
746 EXPECT_EQ(stateField[0].present_state, resp->field->present_state);
747 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state);
748 EXPECT_EQ(stateField[0].event_state, resp->field->event_state);
749 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state);
750 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state);
751 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state);
752 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state);
753}
754
755TEST(GetStateSensorReadings, testBadEncodeResponse)
756{
757 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr,
758 nullptr);
759
760 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
761}
762
763TEST(GetStateSensorReadings, testGoodDecodeResponse)
764{
765 std::array<uint8_t, hdrSize +
766 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
767 sizeof(get_sensor_state_field) * 2>
768 responseMsg{};
769
770 uint8_t completionCode = 0;
771 uint8_t comp_sensorCnt = 2;
772
773 std::array<get_sensor_state_field, 2> stateField{};
774 stateField[0] = {PLDM_SENSOR_DISABLED, PLDM_SENSOR_UNKNOWN,
775 PLDM_SENSOR_UNKNOWN, PLDM_SENSOR_UNKNOWN};
776 stateField[1] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_LOWERFATAL,
777 PLDM_SENSOR_LOWERCRITICAL, PLDM_SENSOR_WARNING};
778
779 uint8_t retcompletion_code = 0;
780 uint8_t retcomp_sensorCnt = 0;
781 std::array<get_sensor_state_field, 2> retstateField{};
782
783 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
784 struct pldm_get_state_sensor_readings_resp* resp =
785 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
786 response->payload);
787
788 resp->completion_code = completionCode;
789 resp->comp_sensor_count = comp_sensorCnt;
790 memcpy(resp->field, &stateField,
791 (sizeof(get_sensor_state_field) * comp_sensorCnt));
792
793 auto rc = decode_get_state_sensor_readings_resp(
794 response, responseMsg.size() - hdrSize, &retcompletion_code,
795 &retcomp_sensorCnt, retstateField.data());
796
797 EXPECT_EQ(rc, PLDM_SUCCESS);
798 EXPECT_EQ(completionCode, retcompletion_code);
799 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt);
800 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state);
801 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state);
802 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state);
803 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state);
804 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state);
805 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state);
806 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state);
807 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state);
808}
809
810TEST(GetStateSensorReadings, testBadDecodeResponse)
811{
812 std::array<uint8_t, hdrSize +
813 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
814 sizeof(get_sensor_state_field) * 2>
815 responseMsg{};
816
817 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
818
819 auto rc = decode_get_state_sensor_readings_resp(
820 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr);
821
822 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
823
824 uint8_t completionCode = 0;
825 uint8_t comp_sensorCnt = 1;
826
827 std::array<get_sensor_state_field, 1> stateField{};
828 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_UPPERFATAL,
829 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_WARNING};
830
831 uint8_t retcompletion_code = 0;
832 uint8_t retcomp_sensorCnt = 0;
833 std::array<get_sensor_state_field, 1> retstateField{};
834
835 struct pldm_get_state_sensor_readings_resp* resp =
836 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
837 response->payload);
838
839 resp->completion_code = completionCode;
840 resp->comp_sensor_count = comp_sensorCnt;
841 memcpy(resp->field, &stateField,
842 (sizeof(get_sensor_state_field) * comp_sensorCnt));
843
844 rc = decode_get_state_sensor_readings_resp(
Andrew Jeffery6ad4dc02023-04-12 15:56:45 +0930845 response, responseMsg.size() - hdrSize, &retcompletion_code,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930846 &retcomp_sensorCnt, retstateField.data());
847
848 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
849}
850
851TEST(GetStateSensorReadings, testGoodEncodeRequest)
852{
853 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
854 requestMsg{};
855
856 uint16_t sensorId = 0xAB;
857 bitfield8_t sensorRearm;
858 sensorRearm.byte = 0x03;
859
860 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
861 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0,
862 request);
863
864 struct pldm_get_state_sensor_readings_req* req =
865 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
866 request->payload);
867
868 EXPECT_EQ(rc, PLDM_SUCCESS);
869 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
870 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte);
871}
872
873TEST(GetStateSensorReadings, testBadEncodeRequest)
874{
875 bitfield8_t sensorRearm;
876 sensorRearm.byte = 0x0;
877
878 auto rc =
879 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr);
880
881 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
882}
883
884TEST(GetStateSensorReadings, testGoodDecodeRequest)
885{
886 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
887 requestMsg{};
888
889 uint16_t sensorId = 0xCD;
890 bitfield8_t sensorRearm;
891 sensorRearm.byte = 0x10;
892
893 uint16_t retsensorId;
894 bitfield8_t retsensorRearm;
895 uint8_t retreserved;
896
897 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
898
899 struct pldm_get_state_sensor_readings_req* req =
900 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
901 request->payload);
902
903 req->sensor_id = htole16(sensorId);
904 req->sensor_rearm.byte = sensorRearm.byte;
905
906 auto rc = decode_get_state_sensor_readings_req(
907 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm,
908 &retreserved);
909
910 EXPECT_EQ(rc, PLDM_SUCCESS);
911 EXPECT_EQ(sensorId, retsensorId);
912 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte);
913 EXPECT_EQ(0, retreserved);
914}
915
916TEST(GetStateSensorReadings, testBadDecodeRequest)
917{
918 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
919 requestMsg{};
920
921 auto rc = decode_get_state_sensor_readings_req(
922 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr);
923
924 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
925 uint16_t sensorId = 0x11;
926 bitfield8_t sensorRearm;
927 sensorRearm.byte = 0x04;
928
929 uint16_t retsensorId;
930 bitfield8_t retsensorRearm;
931 uint8_t retreserved;
932
933 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
934
935 struct pldm_get_state_sensor_readings_req* req =
936 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
937 request->payload);
938
939 req->sensor_id = htole16(sensorId);
940 req->sensor_rearm.byte = sensorRearm.byte;
941
942 rc = decode_get_state_sensor_readings_req(
943 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm,
944 &retreserved);
945
946 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
947}
948
Dung Caod6ae8982022-11-02 10:00:10 +0700949TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeRequest)
950{
951 uint8_t eventBufferSize = 32;
952
953 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES>
954 requestMsg{};
955 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
956
957 auto rc = encode_event_message_buffer_size_req(0, eventBufferSize, request);
958
959 EXPECT_EQ(rc, PLDM_SUCCESS);
960}
961
962TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeResponse)
963{
964 uint8_t completionCode = PLDM_SUCCESS;
965 uint16_t terminusMaxBufferSize = 256;
966
967 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
968 responseMsg{};
969
970 uint8_t retCompletionCode;
971 uint16_t retMaxBufferSize = 0;
972
973 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
974 struct pldm_event_message_buffer_size_resp* resp =
975 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
976 response->payload);
977
978 resp->completion_code = completionCode;
979 resp->terminus_max_buffer_size = terminusMaxBufferSize;
980
981 auto rc = decode_event_message_buffer_size_resp(
982 response, responseMsg.size() - hdrSize, &retCompletionCode,
983 &retMaxBufferSize);
984
985 EXPECT_EQ(rc, PLDM_SUCCESS);
986 EXPECT_EQ(retCompletionCode, completionCode);
987 EXPECT_EQ(terminusMaxBufferSize, retMaxBufferSize);
988}
989
990TEST(EventMessageBufferSize, testBadEventMessageBufferSizeResponse)
991{
992 uint8_t completionCode = PLDM_SUCCESS;
993 uint16_t terminusMaxBufferSize = 256;
994
995 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
996 responseMsg{};
997
998 uint8_t retCompletionCode;
999 uint16_t retMaxBufferSize = 0;
1000
1001 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1002 struct pldm_event_message_buffer_size_resp* resp =
1003 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
1004 response->payload);
1005 resp->completion_code = completionCode;
1006 resp->terminus_max_buffer_size = terminusMaxBufferSize;
1007
1008 auto rc =
1009 decode_event_message_buffer_size_resp(response, 0, nullptr, nullptr);
1010 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1011
1012 rc = decode_event_message_buffer_size_resp(
1013 response, responseMsg.size(), &retCompletionCode, &retMaxBufferSize);
1014 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1015}
1016
Dung Cao1bf8c872022-11-29 05:32:58 +07001017TEST(PlatformEventMessageSupported, testGoodEncodeRequest)
1018{
1019 uint8_t formatVersion = 0x01;
1020
1021 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES>
1022 requestMsg{};
1023
1024 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1025
1026 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1027
1028 struct pldm_event_message_supported_req* req =
1029 reinterpret_cast<struct pldm_event_message_supported_req*>(
1030 request->payload);
1031
1032 EXPECT_EQ(rc, PLDM_SUCCESS);
1033 EXPECT_EQ(formatVersion, req->format_version);
1034}
1035
1036TEST(PlatformEventMessageSupported, testBadEncodeRequest)
1037{
1038 uint8_t eventData = 34;
1039 uint8_t formatVersion = 0x0;
1040
1041 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES +
1042 sizeof(eventData)>
1043 requestMsg{};
1044 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1045
1046 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1047 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1048
1049 rc = encode_event_message_supported_req(0, formatVersion, nullptr);
1050 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1051}
1052
1053TEST(PlatformEventMessageSupported, testGoodDecodeRespond)
1054{
1055 uint8_t completionCode = PLDM_SUCCESS;
1056 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1057 bitfield8_t synchConfigSupported;
1058 synchConfigSupported.byte = 0xe;
1059 uint8_t numberEventClassReturned = 0x3;
1060 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1061 constexpr uint8_t eventClassCount = 3;
1062
1063 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1064 eventClassCount>
1065 responseMsg{};
1066
1067 uint8_t retCompletionCode;
1068 uint8_t retSynchConfig = 0;
1069 uint8_t retNumberEventClass = 0;
1070 bitfield8_t retSynchConfigSupport;
1071 uint8_t retEventClass[eventClassCount] = {0};
1072
1073 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1074 struct pldm_event_message_supported_resp* resp =
1075 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1076 response->payload);
1077
1078 resp->completion_code = completionCode;
1079 resp->synchrony_configuration = synchConfiguration;
1080 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1081 resp->number_event_class_returned = numberEventClassReturned;
1082 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1083
1084 auto rc = decode_event_message_supported_resp(
1085 response, responseMsg.size() - hdrSize, &retCompletionCode,
1086 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1087 retEventClass, eventClassCount);
1088
1089 EXPECT_EQ(rc, PLDM_SUCCESS);
1090 EXPECT_EQ(retCompletionCode, completionCode);
1091 EXPECT_EQ(retSynchConfig, synchConfiguration);
1092 EXPECT_EQ(retNumberEventClass, numberEventClassReturned);
1093 EXPECT_EQ(retSynchConfigSupport.byte, synchConfigSupported.byte);
1094 EXPECT_EQ(0, memcmp(eventClass.data(), resp->event_class,
1095 numberEventClassReturned));
1096}
1097
1098TEST(PlatformEventMessageSupported, testBadSynchConfiguration)
1099{
1100 uint8_t completionCode = PLDM_SUCCESS;
1101 uint8_t synchConfiguration = 0x4;
1102 bitfield8_t synchConfigSupported;
1103 synchConfigSupported.byte = 0xe;
1104 uint8_t numberEventClassReturned = 0x3;
1105 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1106 constexpr uint8_t eventClassCount = 3;
1107
1108 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1109 eventClassCount>
1110 responseMsg{};
1111
1112 uint8_t retCompletionCode;
1113 uint8_t retSynchConfig = 0;
1114 uint8_t retNumberEventClass = 0;
1115 bitfield8_t retSynchConfigSupport;
1116 uint8_t retEventClass[eventClassCount] = {0};
1117
1118 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1119 struct pldm_event_message_supported_resp* resp =
1120 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1121 response->payload);
1122
1123 resp->completion_code = completionCode;
1124 resp->synchrony_configuration = synchConfiguration;
1125 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1126 resp->number_event_class_returned = numberEventClassReturned;
1127 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1128
1129 auto rc = decode_event_message_supported_resp(
1130 response, responseMsg.size() - hdrSize, &retCompletionCode,
1131 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1132 retEventClass, eventClassCount);
1133
1134 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1135}
1136
1137TEST(PlatformEventMessageSupported, testBadDecodeRespond)
1138{
1139 uint8_t completionCode = PLDM_SUCCESS;
1140 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1141 bitfield8_t synchConfigSupported;
1142 synchConfigSupported.byte = 0xe;
1143 uint8_t numberEventClassReturned = 0x3;
1144 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1145 constexpr uint8_t eventClassCount = 3;
1146
1147 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1148 eventClassCount>
1149 responseMsg{};
1150
1151 uint8_t retCompletionCode;
1152 uint8_t retSynchConfig = 0;
1153 uint8_t retNumberEventClass = 0;
1154 bitfield8_t retSynchConfigSupport;
1155 uint8_t retEventClass[eventClassCount] = {0};
1156
1157 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1158 struct pldm_event_message_supported_resp* resp =
1159 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1160 response->payload);
1161 resp->completion_code = completionCode;
1162 resp->synchrony_configuration = synchConfiguration;
1163 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1164 resp->number_event_class_returned = numberEventClassReturned;
1165 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1166
1167 auto rc = decode_event_message_supported_resp(response, 0, nullptr, nullptr,
1168 nullptr, nullptr, nullptr, 0);
1169 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1170
1171 rc = decode_event_message_supported_resp(
1172 response, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES - 1,
1173 &retCompletionCode, &retSynchConfig, &retSynchConfigSupport,
1174 &retNumberEventClass, retEventClass, eventClassCount);
1175 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1176
1177 rc = decode_event_message_supported_resp(
1178 response, responseMsg.size() - hdrSize, &retCompletionCode,
1179 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1180 retEventClass, 1);
1181 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1182}
1183
Andrew Jeffery9c766792022-08-10 23:12:49 +09301184TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
1185{
1186 std::array<uint8_t,
1187 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
1188 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES>
1189 requestMsg{};
1190
1191 uint8_t retFormatVersion = 0;
1192 uint8_t retTid = 0;
1193 uint8_t retEventClass = 0;
1194 size_t retEventDataOffset = 0;
1195
1196 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
1197 struct pldm_platform_event_message_req* request =
1198 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload);
1199
1200 uint8_t formatVersion = 0x01;
1201 uint8_t tid = 0x02;
1202 // Sensor Event
1203 uint8_t eventClass = 0x00;
1204
1205 request->format_version = formatVersion;
1206 request->tid = tid;
1207 request->event_class = eventClass;
1208 size_t eventDataOffset =
1209 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass);
1210
1211 auto rc = decode_platform_event_message_req(
1212 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid,
1213 &retEventClass, &retEventDataOffset);
1214
1215 EXPECT_EQ(rc, PLDM_SUCCESS);
1216 EXPECT_EQ(retFormatVersion, formatVersion);
1217 EXPECT_EQ(retTid, tid);
1218 EXPECT_EQ(retEventClass, eventClass);
1219 EXPECT_EQ(retEventDataOffset, eventDataOffset);
1220}
1221
1222TEST(PlatformEventMessage, testBadDecodeRequest)
1223{
1224 const struct pldm_msg* msg = NULL;
1225 std::array<uint8_t,
1226 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
1227 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
1228 requestMsg{};
1229 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
1230 uint8_t retFormatVersion;
1231 uint8_t retTid = 0;
1232 uint8_t retEventClass = 0;
1233 size_t retEventDataOffset;
1234
1235 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL,
1236 NULL, NULL);
1237 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1238
1239 rc = decode_platform_event_message_req(
1240 req,
1241 requestMsg.size() - hdrSize -
1242 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES,
1243 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset);
1244 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1245}
1246
1247TEST(PlatformEventMessage, testGoodEncodeResponse)
1248{
1249 std::array<uint8_t,
1250 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
1251 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
1252 responseMsg{};
1253 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1254 uint8_t completionCode = 0;
1255 uint8_t instanceId = 0x01;
1256 uint8_t platformEventStatus = 0x01;
1257
1258 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS,
1259 platformEventStatus, response);
1260
1261 EXPECT_EQ(rc, PLDM_SUCCESS);
1262 EXPECT_EQ(completionCode, response->payload[0]);
1263 EXPECT_EQ(platformEventStatus, response->payload[1]);
1264}
1265
1266TEST(PlatformEventMessage, testBadEncodeResponse)
1267{
1268 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL);
1269 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1270}
1271
1272TEST(PlatformEventMessage, testGoodEncodeRequest)
1273{
1274 uint8_t formatVersion = 0x01;
1275 uint8_t Tid = 0x03;
1276 uint8_t eventClass = 0x00;
1277 uint8_t eventData = 34;
1278
1279 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
1280 sizeof(eventData)>
1281 requestMsg{};
1282
1283 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1284 auto rc = encode_platform_event_message_req(
1285 0, formatVersion, Tid, eventClass,
1286 reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request,
1287 sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
1288
1289 struct pldm_platform_event_message_req* req =
1290 reinterpret_cast<struct pldm_platform_event_message_req*>(
1291 request->payload);
1292
1293 EXPECT_EQ(rc, PLDM_SUCCESS);
1294 EXPECT_EQ(formatVersion, req->format_version);
1295 EXPECT_EQ(Tid, req->tid);
1296 EXPECT_EQ(eventClass, req->event_class);
1297 EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData)));
1298}
1299
1300TEST(PlatformEventMessage, testBadEncodeRequest)
1301{
1302 uint8_t Tid = 0x03;
1303 uint8_t eventClass = 0x00;
1304 uint8_t eventData = 34;
1305 size_t sz_eventData = sizeof(eventData);
1306 size_t payloadLen =
1307 sz_eventData + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
1308 uint8_t formatVersion = 0x01;
1309
1310 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
1311 sizeof(eventData)>
1312 requestMsg{};
1313 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1314
1315 auto rc = encode_platform_event_message_req(
1316 0, formatVersion, Tid, eventClass,
1317 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, nullptr,
1318 payloadLen);
1319 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1320 rc = encode_platform_event_message_req(
1321 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
1322 sz_eventData, request, payloadLen);
1323 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1324 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
1325 nullptr, 0, request, payloadLen);
1326 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1327 rc = encode_platform_event_message_req(
1328 0, formatVersion, Tid, eventClass,
1329 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, request, 0);
1330 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1331}
1332
1333TEST(PlatformEventMessage, testGoodDecodeResponse)
1334{
1335 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
1336 responseMsg{};
1337
1338 uint8_t completionCode = PLDM_SUCCESS;
1339 uint8_t platformEventStatus = 0x01;
1340
1341 uint8_t retcompletionCode;
1342 uint8_t retplatformEventStatus;
1343
1344 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1345 struct pldm_platform_event_message_resp* resp =
1346 reinterpret_cast<struct pldm_platform_event_message_resp*>(
1347 response->payload);
1348
1349 resp->completion_code = completionCode;
1350 resp->platform_event_status = platformEventStatus;
1351
1352 auto rc = decode_platform_event_message_resp(
1353 response, responseMsg.size() - hdrSize, &retcompletionCode,
1354 &retplatformEventStatus);
1355
1356 EXPECT_EQ(rc, PLDM_SUCCESS);
1357 EXPECT_EQ(completionCode, retcompletionCode);
1358 EXPECT_EQ(platformEventStatus, retplatformEventStatus);
1359}
1360
1361TEST(PlatformEventMessage, testBadDecodeResponse)
1362{
1363 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
1364 responseMsg{};
1365
1366 uint8_t completionCode = PLDM_SUCCESS;
1367 uint8_t platformEventStatus = 0x01;
1368
1369 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1370 struct pldm_platform_event_message_resp* resp =
1371 reinterpret_cast<struct pldm_platform_event_message_resp*>(
1372 response->payload);
1373 resp->completion_code = completionCode;
1374 resp->platform_event_status = platformEventStatus;
1375
1376 auto rc = decode_platform_event_message_resp(
1377 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
1378
1379 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1380
1381 rc = decode_platform_event_message_resp(
1382 response, responseMsg.size() - hdrSize - 1, &completionCode,
1383 &platformEventStatus);
1384
1385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1386}
1387
1388TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
1389{
1390 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +
1391 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
1392 eventDataArr{};
1393 uint16_t sensorId = 0x1234;
1394 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE;
1395
1396 struct pldm_sensor_event_data* eventData =
1397 (struct pldm_sensor_event_data*)eventDataArr.data();
1398 eventData->sensor_id = sensorId;
1399 eventData->sensor_event_class_type = sensorEventClassType;
1400
1401 size_t retSensorOpDataOffset;
1402 uint16_t retSensorId = 0;
1403 uint8_t retSensorEventClassType;
1404 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType);
1405 auto rc = decode_sensor_event_data(
1406 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(),
1407 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1408 EXPECT_EQ(rc, PLDM_SUCCESS);
1409 EXPECT_EQ(retSensorId, sensorId);
1410 EXPECT_EQ(retSensorEventClassType, sensorEventClassType);
1411 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset);
1412}
1413
1414TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest)
1415{
1416
1417 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH +
1418 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
1419 eventDataArr{};
1420
1421 struct pldm_sensor_event_data* eventData =
1422 (struct pldm_sensor_event_data*)eventDataArr.data();
1423
1424 size_t retSensorOpDataOffset;
1425 uint16_t retSensorId = 0;
1426 uint8_t retSensorEventClassType;
1427 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId,
1428 &retSensorEventClassType,
1429 &retSensorOpDataOffset);
1430 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1431
1432 rc = decode_sensor_event_data(
1433 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1434 eventDataArr.size() -
1435 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH,
1436 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1437 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1438
1439 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
1440
1441 rc = decode_sensor_event_data(
1442 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1443 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1444 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1445
1446 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
1447 rc = decode_sensor_event_data(
1448 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
1449 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
1450 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1451
1452 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE;
1453 rc = decode_sensor_event_data(
1454 reinterpret_cast<uint8_t*>(eventDataArr.data()),
1455 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType,
1456 &retSensorOpDataOffset);
1457 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1458}
1459
1460TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
1461{
1462 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1463 eventDataArr{};
1464
1465 struct pldm_sensor_event_sensor_op_state* sensorData =
1466 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
1467 uint8_t presentState = PLDM_SENSOR_ENABLED;
1468 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
1469 sensorData->present_op_state = presentState;
1470 sensorData->previous_op_state = previousState;
1471
1472 uint8_t retPresentState;
1473 uint8_t retPreviousState;
1474 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
1475 eventDataArr.size(), &retPresentState,
1476 &retPreviousState);
1477 EXPECT_EQ(rc, PLDM_SUCCESS);
1478 EXPECT_EQ(retPresentState, presentState);
1479 EXPECT_EQ(retPreviousState, previousState);
1480}
1481
1482TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
1483{
1484 uint8_t presentOpState;
1485 uint8_t previousOpState;
1486 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
1487 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
1488 &previousOpState);
1489 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1490
1491 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
1492 sensorData{};
1493 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1494 sensorDataLength + 1, &presentOpState,
1495 &previousOpState);
1496 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1497
1498 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1499 sensorDataLength, nullptr, &previousOpState);
1500 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1501}
1502
1503TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
1504{
1505 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1506 eventDataArr{};
1507
1508 struct pldm_sensor_event_state_sensor_state* sensorData =
1509 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
1510 uint8_t sensorOffset = 0x02;
1511 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1512 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1513 sensorData->sensor_offset = sensorOffset;
1514 sensorData->event_state = eventState;
1515 sensorData->previous_event_state = previousEventState;
1516
1517 uint8_t retSensorOffset;
1518 uint8_t retEventState;
1519 uint8_t retPreviousState;
1520 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1521 eventDataArr.size(), &retSensorOffset,
1522 &retEventState, &retPreviousState);
1523 EXPECT_EQ(rc, PLDM_SUCCESS);
1524 EXPECT_EQ(retSensorOffset, sensorOffset);
1525 EXPECT_EQ(retEventState, eventState);
1526 EXPECT_EQ(retPreviousState, previousEventState);
1527}
1528
1529TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
1530{
1531 uint8_t sensorOffset;
1532 uint8_t eventState;
1533 uint8_t previousEventState;
1534 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
1535 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
1536 &eventState, &previousEventState);
1537 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1538
1539 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
1540 sensorData{};
1541 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1542 sensorDataLength - 1, &sensorOffset,
1543 &eventState, &previousEventState);
1544 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1545
1546 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
1547 sensorDataLength, &sensorOffset, nullptr,
1548 &previousEventState);
1549 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1550}
1551
1552TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
1553{
1554 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1555 eventDataArr{};
1556 struct pldm_sensor_event_numeric_sensor_state* sensorData =
1557 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
1558
1559 size_t sensorDataLength =
1560 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
1561 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
1562 uint8_t previousEventState = PLDM_SENSOR_INTEST;
1563 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
1564 uint32_t presentReading = 305441741;
1565 sensorData->event_state = eventState;
1566 sensorData->previous_event_state = previousEventState;
1567 sensorData->sensor_data_size = sensorDataSize;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301568 {
1569 uint32_t presentReadingLE = htole32(presentReading);
1570 memcpy(&sensorData->present_reading, &presentReadingLE,
1571 sizeof(presentReadingLE));
1572 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301573
1574 uint8_t retEventState;
1575 uint8_t retPreviousEventState;
1576 uint8_t retSensorDataSize;
1577 uint32_t retPresentReading;
1578
1579 auto rc = decode_numeric_sensor_data(
1580 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
1581 &retEventState, &retPreviousEventState, &retSensorDataSize,
1582 &retPresentReading);
1583 EXPECT_EQ(rc, PLDM_SUCCESS);
1584 EXPECT_EQ(retEventState, eventState);
1585 EXPECT_EQ(retPreviousEventState, previousEventState);
1586 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1587 EXPECT_EQ(retPresentReading, presentReading);
1588
1589 int16_t presentReadingNew = -31432;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09301590 {
1591 int16_t presentReadingNewLE = htole16(presentReadingNew);
1592 memcpy(&sensorData->present_reading, &presentReadingNewLE,
1593 sizeof(presentReadingNewLE));
1594 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09301595 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
1596 sensorData->sensor_data_size = sensorDataSize;
1597 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
1598
1599 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
1600 sensorDataLength, &retEventState,
1601 &retPreviousEventState, &retSensorDataSize,
1602 &retPresentReading);
1603 EXPECT_EQ(rc, PLDM_SUCCESS);
1604 EXPECT_EQ(retEventState, eventState);
1605 EXPECT_EQ(retPreviousEventState, previousEventState);
1606 EXPECT_EQ(retSensorDataSize, sensorDataSize);
1607 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
1608}
1609
1610TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
1611{
1612 uint8_t eventState;
1613 uint8_t previousEventState;
1614 uint8_t sensorDataSize;
1615 uint32_t presentReading;
1616 size_t sensorDataLength =
1617 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
1618 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
1619 &previousEventState, &sensorDataSize,
1620 &presentReading);
1621 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1622
1623 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
1624 sensorData{};
1625 rc = decode_numeric_sensor_data(
1626 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
1627 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1628 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1629
1630 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
1631 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
1632 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
1633 rc = decode_numeric_sensor_data(
1634 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1635 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1636 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1637
1638 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
1639 rc = decode_numeric_sensor_data(
1640 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
1641 &eventState, &previousEventState, &sensorDataSize, &presentReading);
1642 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301643}
1644
1645TEST(GetNumericEffecterValue, testGoodEncodeRequest)
1646{
1647 std::vector<uint8_t> requestMsg(hdrSize +
1648 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
1649
1650 uint16_t effecter_id = 0xAB01;
1651
1652 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1653
1654 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
1655
1656 struct pldm_get_numeric_effecter_value_req* req =
1657 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1658 request->payload);
1659
1660 EXPECT_EQ(rc, PLDM_SUCCESS);
1661 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
1662}
1663
1664TEST(GetNumericEffecterValue, testBadEncodeRequest)
1665{
1666 std::vector<uint8_t> requestMsg(
1667 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
1668
1669 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
1670 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1671}
1672
1673TEST(GetNumericEffecterValue, testGoodDecodeRequest)
1674{
1675 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1676 requestMsg{};
1677
1678 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1679 struct pldm_get_numeric_effecter_value_req* req =
1680 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
1681 request->payload);
1682
1683 uint16_t effecter_id = 0x12AB;
1684 req->effecter_id = htole16(effecter_id);
1685
1686 uint16_t reteffecter_id;
1687
1688 auto rc = decode_get_numeric_effecter_value_req(
1689 request, requestMsg.size() - hdrSize, &reteffecter_id);
1690
1691 EXPECT_EQ(rc, PLDM_SUCCESS);
1692 EXPECT_EQ(effecter_id, reteffecter_id);
1693}
1694
1695TEST(GetNumericEffecterValue, testBadDecodeRequest)
1696{
1697 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
1698 requestMsg{};
1699
1700 auto rc = decode_get_numeric_effecter_value_req(
1701 nullptr, requestMsg.size() - hdrSize, nullptr);
1702
1703 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1704
1705 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1706 struct pldm_set_numeric_effecter_value_req* req =
1707 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
1708 request->payload);
1709
1710 uint16_t effecter_id = 0x1A;
1711 req->effecter_id = htole16(effecter_id);
1712 uint16_t reteffecter_id;
1713
1714 rc = decode_get_numeric_effecter_value_req(
1715 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
1716
1717 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1718}
1719
1720TEST(GetNumericEffecterValue, testGoodEncodeResponse)
1721{
1722 uint8_t completionCode = 0;
1723 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
1724 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1725 uint32_t pendingValue = 0x12345678;
1726 uint32_t presentValue = 0xABCDEF11;
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09301727 uint32_t val_pending;
1728 uint32_t val_present;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301729
1730 std::array<uint8_t,
1731 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1732 responseMsg{};
1733 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1734
1735 auto rc = encode_get_numeric_effecter_value_resp(
1736 0, completionCode, effecter_dataSize, effecter_operState,
1737 reinterpret_cast<uint8_t*>(&pendingValue),
1738 reinterpret_cast<uint8_t*>(&presentValue), response,
1739 responseMsg.size() - hdrSize);
1740
1741 struct pldm_get_numeric_effecter_value_resp* resp =
1742 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1743 response->payload);
1744
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09301745 memcpy(&val_pending, &resp->pending_and_present_values[0],
1746 sizeof(val_pending));
1747 val_pending = le32toh(val_pending);
1748 memcpy(&val_present, &resp->pending_and_present_values[4],
1749 sizeof(val_present));
1750 val_present = le32toh(val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301751
1752 EXPECT_EQ(rc, PLDM_SUCCESS);
1753 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
1754 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09301755 EXPECT_EQ(pendingValue, val_pending);
1756 EXPECT_EQ(presentValue, val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301757}
1758
1759TEST(GetNumericEffecterValue, testBadEncodeResponse)
1760{
1761 std::array<uint8_t,
1762 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1763 responseMsg{};
1764 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1765
1766 uint8_t pendingValue = 0x01;
1767 uint8_t presentValue = 0x02;
1768
1769 auto rc = encode_get_numeric_effecter_value_resp(
1770 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
1771 responseMsg.size() - hdrSize);
1772 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1773
1774 rc = encode_get_numeric_effecter_value_resp(
1775 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
1776 reinterpret_cast<uint8_t*>(&presentValue), response,
1777 responseMsg.size() - hdrSize);
1778 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1779
1780 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
1781 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
1782
1783 rc = encode_get_numeric_effecter_value_resp(
1784 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
1785 reinterpret_cast<uint8_t*>(&pendingValue),
1786 reinterpret_cast<uint8_t*>(&presentValue), response,
1787 responseMsg.size() - hdrSize);
1788 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1789}
1790
1791TEST(GetNumericEffecterValue, testGoodDecodeResponse)
1792{
1793 std::array<uint8_t,
1794 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
1795 responseMsg{};
1796
1797 uint8_t completionCode = 0;
1798 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
1799 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
1800 uint16_t pendingValue = 0x4321;
1801 uint16_t presentValue = 0xDCBA;
1802
1803 uint8_t retcompletionCode;
1804 uint8_t reteffecter_dataSize;
1805 uint8_t reteffecter_operState;
1806 uint8_t retpendingValue[2];
1807 uint8_t retpresentValue[2];
1808
1809 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1810 struct pldm_get_numeric_effecter_value_resp* resp =
1811 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1812 response->payload);
1813
1814 resp->completion_code = completionCode;
1815 resp->effecter_data_size = effecter_dataSize;
1816 resp->effecter_oper_state = effecter_operState;
1817
1818 uint16_t pendingValue_le = htole16(pendingValue);
1819 memcpy(resp->pending_and_present_values, &pendingValue_le,
1820 sizeof(pendingValue_le));
1821 uint16_t presentValue_le = htole16(presentValue);
1822 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1823 sizeof(presentValue_le));
1824
1825 auto rc = decode_get_numeric_effecter_value_resp(
1826 response, responseMsg.size() - hdrSize, &retcompletionCode,
1827 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1828 retpresentValue);
1829
1830 EXPECT_EQ(rc, PLDM_SUCCESS);
1831 EXPECT_EQ(completionCode, retcompletionCode);
1832 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
1833 EXPECT_EQ(effecter_operState, reteffecter_operState);
1834 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
1835 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
1836}
1837
1838TEST(GetNumericEffecterValue, testBadDecodeResponse)
1839{
1840 std::array<uint8_t,
1841 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
1842 responseMsg{};
1843
1844 auto rc = decode_get_numeric_effecter_value_resp(
1845 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
1846 nullptr, nullptr);
1847
1848 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1849
1850 uint8_t completionCode = 0;
1851 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
1852 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
1853 uint16_t pendingValue = 0x5678;
1854 uint16_t presentValue = 0xCDEF;
1855
1856 uint8_t retcompletionCode;
1857 uint8_t reteffecter_dataSize;
1858 uint8_t reteffecter_operState;
1859 uint8_t retpendingValue[2];
1860 uint8_t retpresentValue[2];
1861
1862 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1863 struct pldm_get_numeric_effecter_value_resp* resp =
1864 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
1865 response->payload);
1866
1867 resp->completion_code = completionCode;
1868 resp->effecter_data_size = effecter_dataSize;
1869 resp->effecter_oper_state = effecter_operState;
1870
1871 uint16_t pendingValue_le = htole16(pendingValue);
1872 memcpy(resp->pending_and_present_values, &pendingValue_le,
1873 sizeof(pendingValue_le));
1874 uint16_t presentValue_le = htole16(presentValue);
1875 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
1876 sizeof(presentValue_le));
1877
1878 rc = decode_get_numeric_effecter_value_resp(
1879 response, responseMsg.size() - hdrSize, &retcompletionCode,
1880 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
1881 retpresentValue);
1882
1883 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1884}
1885
1886TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
1887{
1888 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
1889 const uint8_t numberOfChangeRecords = 2;
1890 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
1891 const uint8_t numberOfChangeEntries1 = 2;
1892 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
1893 {0x00000000, 0x12345678}};
1894 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
1895 const uint8_t numberOfChangeEntries2 = 5;
1896 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
1897 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
1898 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
1899 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
1900 numberOfChangeRecords +
1901 (numberOfChangeEntries1 + numberOfChangeEntries2) *
1902 sizeof(uint32_t)>
1903 eventDataArr{};
1904
1905 struct pldm_pdr_repository_chg_event_data* eventData =
1906 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
1907 eventDataArr.data());
1908 eventData->event_data_format = eventDataFormat;
1909 eventData->number_of_change_records = numberOfChangeRecords;
1910 struct pldm_pdr_repository_change_record_data* changeRecord1 =
1911 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1912 eventData->change_records);
1913 changeRecord1->event_data_operation = eventDataOperation1;
1914 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
1915 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
1916 changeRecordArr1.size() * sizeof(uint32_t));
1917 struct pldm_pdr_repository_change_record_data* changeRecord2 =
1918 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
1919 eventData->change_records +
1920 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
1921 (changeRecordArr1.size() * sizeof(uint32_t)));
1922 changeRecord2->event_data_operation = eventDataOperation2;
1923 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
1924 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
1925 changeRecordArr2.size() * sizeof(uint32_t));
1926
1927 uint8_t retEventDataFormat{};
1928 uint8_t retNumberOfChangeRecords{};
1929 size_t retChangeRecordDataOffset{0};
1930 auto rc = decode_pldm_pdr_repository_chg_event_data(
1931 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
1932 &retEventDataFormat, &retNumberOfChangeRecords,
1933 &retChangeRecordDataOffset);
1934 EXPECT_EQ(rc, PLDM_SUCCESS);
1935 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
1936 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
1937
1938 const uint8_t* changeRecordData =
1939 reinterpret_cast<const uint8_t*>(changeRecord1);
1940 size_t changeRecordDataSize =
1941 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
1942 uint8_t retEventDataOperation;
1943 uint8_t retNumberOfChangeEntries;
1944 size_t retChangeEntryDataOffset;
1945
1946 rc = decode_pldm_pdr_repository_change_record_data(
1947 reinterpret_cast<const uint8_t*>(changeRecordData),
1948 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1949 &retChangeEntryDataOffset);
1950 EXPECT_EQ(rc, PLDM_SUCCESS);
1951 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
1952 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
1953 changeRecordData += retChangeEntryDataOffset;
1954 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
1955 sizeof(uint32_t) * retNumberOfChangeEntries));
1956
1957 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
1958 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
1959 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
1960 rc = decode_pldm_pdr_repository_change_record_data(
1961 reinterpret_cast<const uint8_t*>(changeRecordData),
1962 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
1963 &retChangeEntryDataOffset);
1964 EXPECT_EQ(rc, PLDM_SUCCESS);
1965 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
1966 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
1967 changeRecordData += retChangeEntryDataOffset;
1968 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
1969 sizeof(uint32_t) * retNumberOfChangeEntries));
1970}
1971
1972TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
1973{
1974 uint8_t eventDataFormat{};
1975 uint8_t numberOfChangeRecords{};
1976 size_t changeRecordDataOffset{};
1977 auto rc = decode_pldm_pdr_repository_chg_event_data(
1978 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
1979 &changeRecordDataOffset);
1980 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1981
1982 std::array<uint8_t, 2> eventData{};
1983 rc = decode_pldm_pdr_repository_chg_event_data(
1984 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
1985 &numberOfChangeRecords, &changeRecordDataOffset);
1986 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1987
1988 uint8_t eventDataOperation{};
1989 uint8_t numberOfChangeEntries{};
1990 size_t changeEntryDataOffset{};
1991 rc = decode_pldm_pdr_repository_change_record_data(
1992 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
1993 &changeEntryDataOffset);
1994 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1995
1996 std::array<uint8_t, 2> changeRecord{};
1997 rc = decode_pldm_pdr_repository_change_record_data(
1998 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
1999 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
2000 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2001}
2002
2003TEST(GetSensorReading, testGoodEncodeRequest)
2004{
2005 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2006 requestMsg{};
2007
2008 uint16_t sensorId = 0x1234;
2009 bool8_t rearmEventState = 0x01;
2010
2011 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2012 auto rc =
2013 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request);
2014
2015 struct pldm_get_sensor_reading_req* req =
2016 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2017
2018 EXPECT_EQ(rc, PLDM_SUCCESS);
2019 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
2020 EXPECT_EQ(rearmEventState, req->rearm_event_state);
2021}
2022
2023TEST(GetSensorReading, testBadEncodeRequest)
2024{
2025 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr);
2026
2027 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2028}
2029
2030TEST(GetSensorReading, testGoodDecodeRequest)
2031{
2032 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2033 requestMsg{};
2034
2035 uint16_t sensorId = 0xABCD;
2036 bool8_t rearmEventState = 0xA;
2037
2038 uint16_t retsensorId;
2039 bool8_t retrearmEventState;
2040
2041 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2042
2043 struct pldm_get_sensor_reading_req* req =
2044 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2045
2046 req->sensor_id = htole16(sensorId);
2047 req->rearm_event_state = rearmEventState;
2048
2049 auto rc =
2050 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize,
2051 &retsensorId, &retrearmEventState);
2052
2053 EXPECT_EQ(rc, PLDM_SUCCESS);
2054 EXPECT_EQ(sensorId, retsensorId);
2055 EXPECT_EQ(rearmEventState, retrearmEventState);
2056}
2057
2058TEST(GetSensorReading, testBadDecodeRequest)
2059{
2060 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
2061 requestMsg{};
2062
2063 auto rc = decode_get_sensor_reading_req(
2064 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr);
2065 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2066
2067 uint16_t sensorId = 0xABCD;
2068 bool8_t rearmEventState = 0xA;
2069
2070 uint16_t retsensorId;
2071 bool8_t retrearmEventState;
2072
2073 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2074
2075 struct pldm_get_sensor_reading_req* req =
2076 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
2077
2078 req->sensor_id = htole16(sensorId);
2079 req->rearm_event_state = rearmEventState;
2080
2081 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1,
2082 &retsensorId, &retrearmEventState);
2083
2084 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2085}
2086
2087TEST(GetSensorReading, testGoodEncodeResponse)
2088{
2089 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES>
2090 responseMsg{};
2091
2092 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2093
2094 uint8_t completionCode = 0;
2095 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2096 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED;
2097 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION;
2098 uint8_t presentState = PLDM_SENSOR_NORMAL;
2099 uint8_t previousState = PLDM_SENSOR_WARNING;
2100 uint8_t eventState = PLDM_SENSOR_UPPERWARNING;
2101 uint8_t presentReading = 0x21;
2102
2103 auto rc = encode_get_sensor_reading_resp(
2104 0, completionCode, sensor_dataSize, sensor_operationalState,
2105 sensor_event_messageEnable, presentState, previousState, eventState,
2106 reinterpret_cast<uint8_t*>(&presentReading), response,
2107 responseMsg.size() - hdrSize);
2108
2109 struct pldm_get_sensor_reading_resp* resp =
2110 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
2111 response->payload);
2112
2113 EXPECT_EQ(rc, PLDM_SUCCESS);
2114 EXPECT_EQ(completionCode, resp->completion_code);
2115 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size);
2116 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state);
2117 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable);
2118 EXPECT_EQ(presentState, resp->present_state);
2119 EXPECT_EQ(previousState, resp->previous_state);
2120 EXPECT_EQ(eventState, resp->event_state);
2121 EXPECT_EQ(presentReading,
2122 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0])));
2123}
2124
2125TEST(GetSensorReading, testBadEncodeResponse)
2126{
2127 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
2128 responseMsg{};
2129
2130 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2131
2132 uint8_t presentReading = 0x1;
2133
2134 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0,
2135 nullptr, nullptr,
2136 responseMsg.size() - hdrSize);
2137 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2138
2139 rc = encode_get_sensor_reading_resp(
2140 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1,
2141 reinterpret_cast<uint8_t*>(&presentReading), response,
2142 responseMsg.size() - hdrSize);
2143 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2144
2145 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2146
2147 rc = encode_get_sensor_reading_resp(
2148 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1,
2149 reinterpret_cast<uint8_t*>(&presentReading), response,
2150 responseMsg.size() - hdrSize);
2151 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2152}
2153
2154TEST(GetSensorReading, testGoodDecodeResponse)
2155{
2156 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
2157 responseMsg{};
2158
2159 uint8_t completionCode = 0;
2160 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
2161 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN;
2162 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED;
2163 uint8_t presentState = PLDM_SENSOR_CRITICAL;
2164 uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL;
2165 uint8_t eventState = PLDM_SENSOR_WARNING;
2166 uint32_t presentReading = 0xABCDEF11;
2167
2168 uint8_t retcompletionCode;
2169 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
2170 uint8_t retsensor_operationalState;
2171 uint8_t retsensor_event_messageEnable;
2172 uint8_t retpresentState;
2173 uint8_t retpreviousState;
2174 uint8_t reteventState;
2175 uint8_t retpresentReading[4];
2176
2177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2178 struct pldm_get_sensor_reading_resp* resp =
2179 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
2180 response->payload);
2181
2182 resp->completion_code = completionCode;
2183 resp->sensor_data_size = sensor_dataSize;
2184 resp->sensor_operational_state = sensor_operationalState;
2185 resp->sensor_event_message_enable = sensor_event_messageEnable;
2186 resp->present_state = presentState;
2187 resp->previous_state = previousState;
2188 resp->event_state = eventState;
2189
2190 uint32_t presentReading_le = htole32(presentReading);
2191 memcpy(resp->present_reading, &presentReading_le,
2192 sizeof(presentReading_le));
2193
2194 auto rc = decode_get_sensor_reading_resp(
2195 response, responseMsg.size() - hdrSize, &retcompletionCode,
2196 &retsensor_dataSize, &retsensor_operationalState,
2197 &retsensor_event_messageEnable, &retpresentState, &retpreviousState,
2198 &reteventState, retpresentReading);
2199
2200 EXPECT_EQ(rc, PLDM_SUCCESS);
2201 EXPECT_EQ(completionCode, retcompletionCode);
2202 EXPECT_EQ(sensor_dataSize, retsensor_dataSize);
2203 EXPECT_EQ(sensor_operationalState, retsensor_operationalState);
2204 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable);
2205 EXPECT_EQ(presentState, retpresentState);
2206 EXPECT_EQ(previousState, retpreviousState);
2207 EXPECT_EQ(eventState, reteventState);
2208 EXPECT_EQ(presentReading,
2209 *(reinterpret_cast<uint32_t*>(retpresentReading)));
2210}
2211
2212TEST(GetSensorReading, testBadDecodeResponse)
2213{
2214 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1>
2215 responseMsg{};
2216
2217 auto rc = decode_get_sensor_reading_resp(
2218 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
2219 nullptr, nullptr, nullptr, nullptr, nullptr);
2220
2221 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2222
2223 uint8_t completionCode = 0;
2224 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2225 uint8_t sensor_operationalState = PLDM_SENSOR_INTEST;
2226 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED;
2227 uint8_t presentState = PLDM_SENSOR_FATAL;
2228 uint8_t previousState = PLDM_SENSOR_UPPERFATAL;
2229 uint8_t eventState = PLDM_SENSOR_WARNING;
2230 uint8_t presentReading = 0xA;
2231
2232 uint8_t retcompletionCode;
2233 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
2234 uint8_t retsensor_operationalState;
2235 uint8_t retsensor_event_messageEnable;
2236 uint8_t retpresent_state;
2237 uint8_t retprevious_state;
2238 uint8_t retevent_state;
2239 uint8_t retpresentReading;
2240
2241 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2242 struct pldm_get_sensor_reading_resp* resp =
2243 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
2244 response->payload);
2245
2246 resp->completion_code = completionCode;
2247 resp->sensor_data_size = sensor_dataSize;
2248 resp->sensor_operational_state = sensor_operationalState;
2249 resp->sensor_event_message_enable = sensor_event_messageEnable;
2250 resp->present_state = presentState;
2251 resp->previous_state = previousState;
2252 resp->event_state = eventState;
2253 resp->present_reading[0] = presentReading;
2254
2255 rc = decode_get_sensor_reading_resp(
2256 response, responseMsg.size() - hdrSize, &retcompletionCode,
2257 &retsensor_dataSize, &retsensor_operationalState,
2258 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state,
2259 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading));
2260
2261 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2262}
2263
2264TEST(SetEventReceiver, testGoodEncodeRequest)
2265{
2266 uint8_t eventMessageGlobalEnable =
2267 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
2268 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
2269 uint8_t eventReceiverAddressInfo = 0x08;
2270 uint16_t heartbeatTimer = 0x78;
2271
2272 std::vector<uint8_t> requestMsg(hdrSize +
2273 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
2274 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2275
2276 auto rc = encode_set_event_receiver_req(
2277 0, eventMessageGlobalEnable, transportProtocolType,
2278 eventReceiverAddressInfo, heartbeatTimer, request);
2279
2280 EXPECT_EQ(rc, PLDM_SUCCESS);
2281 struct pldm_set_event_receiver_req* req =
2282 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
2283 EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable);
2284 EXPECT_EQ(transportProtocolType, req->transport_protocol_type);
2285 EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info);
2286 EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer));
2287}
2288
2289TEST(SetEventReceiver, testBadEncodeRequest)
2290{
2291 uint8_t eventMessageGlobalEnable =
2292 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
2293 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
2294 uint8_t eventReceiverAddressInfo = 0x08;
2295 uint16_t heartbeatTimer = 0;
2296
2297 std::vector<uint8_t> requestMsg(hdrSize +
2298 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
2299 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2300
2301 auto rc = encode_set_event_receiver_req(
2302 0, eventMessageGlobalEnable, transportProtocolType,
2303 eventReceiverAddressInfo, heartbeatTimer, request);
2304
2305 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2306}
2307
2308TEST(SetEventReceiver, testGoodDecodeResponse)
2309{
2310 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
2311 responseMsg{};
2312
2313 uint8_t retcompletion_code = 0;
2314 responseMsg[hdrSize] = PLDM_SUCCESS;
2315
2316 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2317 auto rc = decode_set_event_receiver_resp(
2318 response, responseMsg.size() - sizeof(pldm_msg_hdr),
2319 &retcompletion_code);
2320
2321 EXPECT_EQ(rc, PLDM_SUCCESS);
2322 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
2323}
2324
2325TEST(SetEventReceiver, testBadDecodeResponse)
2326{
2327 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
2328 responseMsg{};
2329 uint8_t retcompletion_code = 0;
2330 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2331
2332 auto rc = decode_set_event_receiver_resp(
2333 response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL);
2334
2335 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2336
2337 rc = decode_set_event_receiver_resp(
2338 nullptr, responseMsg.size() - sizeof(pldm_msg_hdr),
2339 &retcompletion_code);
2340
2341 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2342}
2343
2344TEST(SetEventReceiver, testGoodEncodeResponse)
2345{
2346 std::array<uint8_t,
2347 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
2348 responseMsg{};
2349 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2350 uint8_t completionCode = 0;
2351
2352 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response);
2353
2354 EXPECT_EQ(rc, PLDM_SUCCESS);
2355 EXPECT_EQ(completionCode, response->payload[0]);
2356}
2357
2358TEST(SetEventReceiver, testBadEncodeResponse)
2359{
2360 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL);
2361 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2362}
2363
2364TEST(SetEventReceiver, testGoodDecodeRequest)
2365{
2366
2367 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
2368 requestMsg{};
2369
2370 uint8_t eventMessageGlobalEnable =
2371 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
2372 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
2373 uint8_t eventReceiverAddressInfo = 0x08;
2374 uint16_t heartbeatTimer = 0x78;
2375
2376 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2377 struct pldm_set_event_receiver_req* req =
2378 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
2379
2380 req->event_message_global_enable = eventMessageGlobalEnable;
2381 req->transport_protocol_type = transportProtocolType;
2382 req->event_receiver_address_info = eventReceiverAddressInfo;
2383 req->heartbeat_timer = htole16(heartbeatTimer);
2384
2385 uint8_t reteventMessageGlobalEnable;
2386 uint8_t rettransportProtocolType;
2387 uint8_t reteventReceiverAddressInfo;
2388 uint16_t retheartbeatTimer;
2389 auto rc = decode_set_event_receiver_req(
2390 request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable,
2391 &rettransportProtocolType, &reteventReceiverAddressInfo,
2392 &retheartbeatTimer);
2393
2394 EXPECT_EQ(rc, PLDM_SUCCESS);
2395 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
2396 EXPECT_EQ(transportProtocolType, rettransportProtocolType);
2397 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
2398 EXPECT_EQ(heartbeatTimer, retheartbeatTimer);
2399}
2400
2401TEST(SetEventReceiver, testBadDecodeRequest)
2402{
2403 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
2404 requestMsg{};
2405
2406 auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize,
2407 NULL, NULL, NULL, NULL);
2408 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2409
2410 uint8_t eventMessageGlobalEnable =
2411 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
2412 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
2413 uint8_t eventReceiverAddressInfo = 0x08;
2414 uint16_t heartbeatTimer = 0x78;
2415
2416 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2417 struct pldm_set_event_receiver_req* req =
2418 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
2419
2420 req->event_message_global_enable = eventMessageGlobalEnable;
2421 req->transport_protocol_type = transportProtocolType;
2422 req->event_receiver_address_info = eventReceiverAddressInfo;
2423 req->heartbeat_timer = htole16(heartbeatTimer);
2424
2425 uint8_t reteventMessageGlobalEnable;
2426 uint8_t rettransportProtocolType;
2427 uint8_t reteventReceiverAddressInfo;
2428 uint16_t retheartbeatTimer;
2429 rc = decode_set_event_receiver_req(
2430 request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable,
2431 &rettransportProtocolType, &reteventReceiverAddressInfo,
2432 &retheartbeatTimer);
2433 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2434}
Andrew Jeffery7992eb82023-04-06 16:13:53 +09302435
2436TEST(decodeNumericSensorPdrData, Uint8Test)
2437{
2438 std::vector<uint8_t> pdr1{
2439 0x1,
2440 0x0,
2441 0x0,
2442 0x0, // record handle
2443 0x1, // PDRHeaderVersion
2444 PLDM_NUMERIC_SENSOR_PDR, // PDRType
2445 0x0,
2446 0x0, // recordChangeNumber
2447 PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
2448 0, // dataLength
2449 0,
2450 0, // PLDMTerminusHandle
2451 0x1,
2452 0x0, // sensorID=1
2453 PLDM_ENTITY_POWER_SUPPLY,
2454 0, // entityType=Power Supply(120)
2455 1,
2456 0, // entityInstanceNumber
2457 1,
2458 0, // containerID=1
2459 PLDM_NO_INIT, // sensorInit
2460 false, // sensorAuxiliaryNamesPDR
2461 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
2462 0, // unitModifier
2463 0, // rateUnit
2464 0, // baseOEMUnitHandle
2465 0, // auxUnit
2466 0, // auxUnitModifier
2467 0, // auxRateUnit
2468 0, // rel
2469 0, // auxOEMUnitHandle
2470 true, // isLinear
2471 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
2472 0,
2473 0,
2474 0xc0,
2475 0x3f, // resolution=1.5
2476 0,
2477 0,
2478 0x80,
2479 0x3f, // offset=1.0
2480 0,
2481 0, // accuracy
2482 0, // plusTolerance
2483 0, // minusTolerance
2484 3, // hysteresis = 3
2485 0, // supportedThresholds
2486 0, // thresholdAndHysteresisVolatility
2487 0,
2488 0,
2489 0x80,
2490 0x3f, // stateTransistionInterval=1.0
2491 0,
2492 0,
2493 0x80,
2494 0x3f, // updateInverval=1.0
2495 255, // maxReadable
2496 0, // minReadable
2497 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
2498 0, // rangeFieldsupport
2499 50, // nominalValue = 50
2500 60, // normalMax = 60
2501 40, // normalMin = 40
2502 70, // warningHigh = 70
2503 30, // warningLow = 30
2504 80, // criticalHigh = 80
2505 20, // criticalLow = 20
2506 90, // fatalHigh = 90
2507 10 // fatalLow = 10
2508 };
2509
2510 struct pldm_numeric_sensor_value_pdr decodedPdr;
2511 auto rc =
2512 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
2513 EXPECT_EQ(PLDM_SUCCESS, rc);
2514 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
2515 EXPECT_EQ(1, decodedPdr.hdr.version);
2516 EXPECT_EQ(PLDM_NUMERIC_SENSOR_PDR, decodedPdr.hdr.type);
2517 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
2518 EXPECT_EQ(PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, decodedPdr.hdr.length);
2519 EXPECT_EQ(1, decodedPdr.sensor_id);
2520 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
2521 EXPECT_EQ(1, decodedPdr.entity_instance_num);
2522 EXPECT_EQ(1, decodedPdr.container_id);
2523 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.sensor_init);
2524 EXPECT_EQ(false, decodedPdr.sensor_auxiliary_names_pdr);
2525 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
2526 EXPECT_EQ(0, decodedPdr.unit_modifier);
2527 EXPECT_EQ(0, decodedPdr.rate_unit);
2528 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
2529 EXPECT_EQ(0, decodedPdr.aux_unit);
2530 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
2531 EXPECT_EQ(0, decodedPdr.aux_rate_unit);
2532 EXPECT_EQ(0, decodedPdr.rel);
2533 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
2534 EXPECT_EQ(true, decodedPdr.is_linear);
2535 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT8, decodedPdr.sensor_data_size);
2536 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
2537 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
2538 EXPECT_EQ(0, decodedPdr.accuracy);
2539 EXPECT_EQ(0, decodedPdr.plus_tolerance);
2540 EXPECT_EQ(0, decodedPdr.minus_tolerance);
2541 EXPECT_EQ(3, decodedPdr.hysteresis.value_u8);
2542 EXPECT_EQ(0, decodedPdr.supported_thresholds.byte);
2543 EXPECT_EQ(0, decodedPdr.threshold_and_hysteresis_volatility.byte);
2544 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
2545 EXPECT_FLOAT_EQ(1.0f, decodedPdr.update_interval);
2546 EXPECT_EQ(255, decodedPdr.max_readable.value_u8);
2547 EXPECT_EQ(0, decodedPdr.min_readable.value_u8);
2548 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
2549 EXPECT_EQ(0, decodedPdr.range_field_support.byte);
2550 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
2551 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
2552 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
2553 EXPECT_EQ(70, decodedPdr.warning_high.value_u8);
2554 EXPECT_EQ(30, decodedPdr.warning_low.value_u8);
2555 EXPECT_EQ(80, decodedPdr.critical_high.value_u8);
2556 EXPECT_EQ(20, decodedPdr.critical_low.value_u8);
2557 EXPECT_EQ(90, decodedPdr.fatal_high.value_u8);
2558 EXPECT_EQ(10, decodedPdr.fatal_low.value_u8);
2559}
2560
2561TEST(decodeNumericSensorPdrData, Sint8Test)
2562{
2563 std::vector<uint8_t> pdr1{
2564 0x1,
2565 0x0,
2566 0x0,
2567 0x0, // record handle
2568 0x1, // PDRHeaderVersion
2569 PLDM_NUMERIC_SENSOR_PDR, // PDRType
2570 0x0,
2571 0x0, // recordChangeNumber
2572 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
2573 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
2574 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
2575 0, // dataLength
2576 0,
2577 0, // PLDMTerminusHandle
2578 0x1,
2579 0x0, // sensorID=1
2580 PLDM_ENTITY_POWER_SUPPLY,
2581 0, // entityType=Power Supply(120)
2582 1,
2583 0, // entityInstanceNumber
2584 0x1,
2585 0x0, // containerID=1
2586 PLDM_NO_INIT, // sensorInit
2587 false, // sensorAuxiliaryNamesPDR
2588 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
2589 0, // unitModifier
2590 0, // rateUnit
2591 0, // baseOEMUnitHandle
2592 0, // auxUnit
2593 0, // auxUnitModifier
2594 0, // auxRateUnit
2595 0, // rel
2596 0, // auxOEMUnitHandle
2597 true, // isLinear
2598 PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
2599 0,
2600 0,
2601 0,
2602 0, // resolution
2603 0,
2604 0,
2605 0,
2606 0, // offset
2607 0,
2608 0, // accuracy
2609 0, // plusTolerance
2610 0, // minusTolerance
2611 3, // hysteresis = 3
2612 0, // supportedThresholds
2613 0, // thresholdAndHysteresisVolatility
2614 0,
2615 0,
2616 0x80,
2617 0x3f, // stateTransistionInterval=1.0
2618 0,
2619 0,
2620 0x80,
2621 0x3f, // updateInverval=1.0
2622 0x64, // maxReadable = 100
2623 0x9c, // minReadable = -100
2624 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
2625 0, // rangeFieldsupport
2626 0, // nominalValue = 0
2627 5, // normalMax = 5
2628 0xfb, // normalMin = -5
2629 10, // warningHigh = 10
2630 0xf6, // warningLow = -10
2631 20, // criticalHigh = 20
2632 0xec, // criticalLow = -20
2633 30, // fatalHigh = 30
2634 0xe2 // fatalLow = -30
2635 };
2636
2637 struct pldm_numeric_sensor_value_pdr decodedPdr;
2638 auto rc =
2639 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
2640 EXPECT_EQ(PLDM_SUCCESS, rc);
2641
2642 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT8, decodedPdr.sensor_data_size);
2643 EXPECT_EQ(100, decodedPdr.max_readable.value_s8);
2644 EXPECT_EQ(-100, decodedPdr.min_readable.value_s8);
2645 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
2646 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
2647 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
2648 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
2649 EXPECT_EQ(10, decodedPdr.warning_high.value_s8);
2650 EXPECT_EQ(-10, decodedPdr.warning_low.value_s8);
2651 EXPECT_EQ(20, decodedPdr.critical_high.value_s8);
2652 EXPECT_EQ(-20, decodedPdr.critical_low.value_s8);
2653 EXPECT_EQ(30, decodedPdr.fatal_high.value_s8);
2654 EXPECT_EQ(-30, decodedPdr.fatal_low.value_s8);
2655}
2656
2657TEST(decodeNumericSensorPdrData, Uint16Test)
2658{
2659 std::vector<uint8_t> pdr1{
2660 0x1,
2661 0x0,
2662 0x0,
2663 0x0, // record handle
2664 0x1, // PDRHeaderVersion
2665 PLDM_NUMERIC_SENSOR_PDR, // PDRType
2666 0x0,
2667 0x0, // recordChangeNumber
2668 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
2669 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
2670 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
2671 0, // dataLength
2672 0,
2673 0, // PLDMTerminusHandle
2674 0x1,
2675 0x0, // sensorID=1
2676 PLDM_ENTITY_POWER_SUPPLY,
2677 0, // entityType=Power Supply(120)
2678 1,
2679 0, // entityInstanceNumber
2680 0x1,
2681 0x0, // containerID=1
2682 PLDM_NO_INIT, // sensorInit
2683 false, // sensorAuxiliaryNamesPDR
2684 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
2685 0, // unitModifier
2686 0, // rateUnit
2687 0, // baseOEMUnitHandle
2688 0, // auxUnit
2689 0, // auxUnitModifier
2690 0, // auxRateUnit
2691 0, // rel
2692 0, // auxOEMUnitHandle
2693 true, // isLinear
2694 PLDM_SENSOR_DATA_SIZE_UINT16, // sensorDataSize
2695 0,
2696 0,
2697 0,
2698 0, // resolution
2699 0,
2700 0,
2701 0,
2702 0, // offset
2703 0,
2704 0, // accuracy
2705 0, // plusTolerance
2706 0, // minusTolerance
2707 3,
2708 0, // hysteresis = 3
2709 0, // supportedThresholds
2710 0, // thresholdAndHysteresisVolatility
2711 0,
2712 0,
2713 0x80,
2714 0x3f, // stateTransistionInterval=1.0
2715 0,
2716 0,
2717 0x80,
2718 0x3f, // updateInverval=1.0
2719 0,
2720 0x10, // maxReadable = 4096
2721 0,
2722 0, // minReadable = 0
2723 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
2724 0, // rangeFieldsupport
2725 0x88,
2726 0x13, // nominalValue = 5,000
2727 0x70,
2728 0x17, // normalMax = 6,000
2729 0xa0,
2730 0x0f, // normalMin = 4,000
2731 0x58,
2732 0x1b, // warningHigh = 7,000
2733 0xb8,
2734 0x0b, // warningLow = 3,000
2735 0x40,
2736 0x1f, // criticalHigh = 8,000
2737 0xd0,
2738 0x07, // criticalLow = 2,000
2739 0x28,
2740 0x23, // fatalHigh = 9,000
2741 0xe8,
2742 0x03 // fatalLow = 1,000
2743 };
2744
2745 struct pldm_numeric_sensor_value_pdr decodedPdr;
2746 auto rc =
2747 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
2748 EXPECT_EQ(PLDM_SUCCESS, rc);
2749
2750 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT16, decodedPdr.sensor_data_size);
2751 EXPECT_EQ(4096, decodedPdr.max_readable.value_u16);
2752 EXPECT_EQ(0, decodedPdr.min_readable.value_u16);
2753 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
2754 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
2755 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
2756 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
2757 EXPECT_EQ(7000, decodedPdr.warning_high.value_u16);
2758 EXPECT_EQ(3000, decodedPdr.warning_low.value_u16);
2759 EXPECT_EQ(8000, decodedPdr.critical_high.value_u16);
2760 EXPECT_EQ(2000, decodedPdr.critical_low.value_u16);
2761 EXPECT_EQ(9000, decodedPdr.fatal_high.value_u16);
2762 EXPECT_EQ(1000, decodedPdr.fatal_low.value_u16);
2763}
2764
2765TEST(decodeNumericSensorPdrData, Sint16Test)
2766{
2767 std::vector<uint8_t> pdr1{
2768 0x1,
2769 0x0,
2770 0x0,
2771 0x0, // record handle
2772 0x1, // PDRHeaderVersion
2773 PLDM_NUMERIC_SENSOR_PDR, // PDRType
2774 0x0,
2775 0x0, // recordChangeNumber
2776 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
2777 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
2778 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
2779 0, // dataLength
2780 0,
2781 0, // PLDMTerminusHandle
2782 0x1,
2783 0x0, // sensorID=1
2784 PLDM_ENTITY_POWER_SUPPLY,
2785 0, // entityType=Power Supply(120)
2786 1,
2787 0, // entityInstanceNumber
2788 0x1,
2789 0x0, // containerID=1
2790 PLDM_NO_INIT, // sensorInit
2791 false, // sensorAuxiliaryNamesPDR
2792 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
2793 0, // unitModifier
2794 0, // rateUnit
2795 0, // baseOEMUnitHandle
2796 0, // auxUnit
2797 0, // auxUnitModifier
2798 0, // auxRateUnit
2799 0, // rel
2800 0, // auxOEMUnitHandle
2801 true, // isLinear
2802 PLDM_SENSOR_DATA_SIZE_SINT16, // sensorDataSize
2803 0,
2804 0,
2805 0,
2806 0, // resolution
2807 0,
2808 0,
2809 0,
2810 0, // offset
2811 0,
2812 0, // accuracy
2813 0, // plusTolerance
2814 0, // minusTolerance
2815 3,
2816 0, // hysteresis
2817 0, // supportedThresholds
2818 0, // thresholdAndHysteresisVolatility
2819 0,
2820 0,
2821 0x80,
2822 0x3f, // stateTransistionInterval=1.0
2823 0,
2824 0,
2825 0x80,
2826 0x3f, // updateInverval=1.0
2827 0xe8,
2828 0x03, // maxReadable = 1000
2829 0x18,
2830 0xfc, // minReadable = -1000
2831 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
2832 0, // rangeFieldsupport
2833 0,
2834 0, // nominalValue = 0
2835 0xf4,
2836 0x01, // normalMax = 500
2837 0x0c,
2838 0xfe, // normalMin = -500
2839 0xe8,
2840 0x03, // warningHigh = 1,000
2841 0x18,
2842 0xfc, // warningLow = -1,000
2843 0xd0,
2844 0x07, // criticalHigh = 2,000
2845 0x30,
2846 0xf8, // criticalLow = -2,000
2847 0xb8,
2848 0x0b, // fatalHigh = 3,000
2849 0x48,
2850 0xf4 // fatalLow = -3,000
2851 };
2852
2853 struct pldm_numeric_sensor_value_pdr decodedPdr;
2854 auto rc =
2855 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
2856 EXPECT_EQ(PLDM_SUCCESS, rc);
2857
2858 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT16, decodedPdr.sensor_data_size);
2859 EXPECT_EQ(1000, decodedPdr.max_readable.value_s16);
2860 EXPECT_EQ(-1000, decodedPdr.min_readable.value_s16);
2861 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
2862 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
2863 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
2864 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
2865 EXPECT_EQ(1000, decodedPdr.warning_high.value_s16);
2866 EXPECT_EQ(-1000, decodedPdr.warning_low.value_s16);
2867 EXPECT_EQ(2000, decodedPdr.critical_high.value_s16);
2868 EXPECT_EQ(-2000, decodedPdr.critical_low.value_s16);
2869 EXPECT_EQ(3000, decodedPdr.fatal_high.value_s16);
2870 EXPECT_EQ(-3000, decodedPdr.fatal_low.value_s16);
2871}
2872
2873TEST(decodeNumericSensorPdrData, Uint32Test)
2874{
2875 std::vector<uint8_t> pdr1{
2876 0x1,
2877 0x0,
2878 0x0,
2879 0x0, // record handle
2880 0x1, // PDRHeaderVersion
2881 PLDM_NUMERIC_SENSOR_PDR, // PDRType
2882 0x0,
2883 0x0, // recordChangeNumber
2884 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
2885 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
2886 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
2887 0, // dataLength
2888 0,
2889 0, // PLDMTerminusHandle
2890 0x1,
2891 0x0, // sensorID=1
2892 PLDM_ENTITY_POWER_SUPPLY,
2893 0, // entityType=Power Supply(120)
2894 1,
2895 0, // entityInstanceNumber
2896 0x1,
2897 0x0, // containerID=1
2898 PLDM_NO_INIT, // sensorInit
2899 false, // sensorAuxiliaryNamesPDR
2900 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
2901 0, // unitModifier
2902 0, // rateUnit
2903 0, // baseOEMUnitHandle
2904 0, // auxUnit
2905 0, // auxUnitModifier
2906 0, // auxRateUnit
2907 0, // rel
2908 0, // auxOEMUnitHandle
2909 true, // isLinear
2910 PLDM_SENSOR_DATA_SIZE_UINT32, // sensorDataSize
2911 0,
2912 0,
2913 0,
2914 0, // resolution
2915 0,
2916 0,
2917 0,
2918 0, // offset
2919 0,
2920 0, // accuracy
2921 0, // plusTolerance
2922 0, // minusTolerance
2923 3,
2924 0,
2925 0,
2926 0, // hysteresis
2927 0, // supportedThresholds
2928 0, // thresholdAndHysteresisVolatility
2929 0,
2930 0,
2931 0x80,
2932 0x3f, // stateTransistionInterval=1.0
2933 0,
2934 0,
2935 0x80,
2936 0x3f, // updateInverval=1.0
2937 0,
2938 0x10,
2939 0,
2940 0, // maxReadable = 4096
2941 0,
2942 0,
2943 0,
2944 0, // minReadable = 0
2945 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
2946 0, // rangeFieldsupport
2947 0x40,
2948 0x4b,
2949 0x4c,
2950 0x00, // nominalValue = 5,000,000
2951 0x80,
2952 0x8d,
2953 0x5b,
2954 0x00, // normalMax = 6,000,000
2955 0x00,
2956 0x09,
2957 0x3d,
2958 0x00, // normalMin = 4,000,000
2959 0xc0,
2960 0xcf,
2961 0x6a,
2962 0x00, // warningHigh = 7,000,000
2963 0xc0,
2964 0xc6,
2965 0x2d,
2966 0x00, // warningLow = 3,000,000
2967 0x00,
2968 0x12,
2969 0x7a,
2970 0x00, // criticalHigh = 8,000,000
2971 0x80,
2972 0x84,
2973 0x1e,
2974 0x00, // criticalLow = 2,000,000
2975 0x40,
2976 0x54,
2977 0x89,
2978 0x00, // fatalHigh = 9,000,000
2979 0x40,
2980 0x42,
2981 0x0f,
2982 0x00 // fatalLow = 1,000,000
2983 };
2984
2985 struct pldm_numeric_sensor_value_pdr decodedPdr;
2986 auto rc =
2987 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
2988 EXPECT_EQ(PLDM_SUCCESS, rc);
2989
2990 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT32, decodedPdr.sensor_data_size);
2991 EXPECT_EQ(4096, decodedPdr.max_readable.value_u32);
2992 EXPECT_EQ(0, decodedPdr.min_readable.value_u32);
2993 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
2994 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
2995 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
2996 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
2997 EXPECT_EQ(7000000, decodedPdr.warning_high.value_u32);
2998 EXPECT_EQ(3000000, decodedPdr.warning_low.value_u32);
2999 EXPECT_EQ(8000000, decodedPdr.critical_high.value_u32);
3000 EXPECT_EQ(2000000, decodedPdr.critical_low.value_u32);
3001 EXPECT_EQ(9000000, decodedPdr.fatal_high.value_u32);
3002 EXPECT_EQ(1000000, decodedPdr.fatal_low.value_u32);
3003}
3004
3005TEST(decodeNumericSensorPdrData, Sint32Test)
3006{
3007 std::vector<uint8_t> pdr1{
3008 0x1,
3009 0x0,
3010 0x0,
3011 0x0, // record handle
3012 0x1, // PDRHeaderVersion
3013 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3014 0x0,
3015 0x0, // recordChangeNumber
3016 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3017 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
3018 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
3019 0, // dataLength
3020 0,
3021 0, // PLDMTerminusHandle
3022 0x1,
3023 0x0, // sensorID=1
3024 PLDM_ENTITY_POWER_SUPPLY,
3025 0, // entityType=Power Supply(120)
3026 1,
3027 0, // entityInstanceNumber
3028 0x1,
3029 0x0, // containerID=1
3030 PLDM_NO_INIT, // sensorInit
3031 false, // sensorAuxiliaryNamesPDR
3032 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3033 0, // unitModifier
3034 0, // rateUnit
3035 0, // baseOEMUnitHandle
3036 0, // auxUnit
3037 0, // auxUnitModifier
3038 0, // auxRateUnit
3039 0, // rel
3040 0, // auxOEMUnitHandle
3041 true, // isLinear
3042 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
3043 0,
3044 0,
3045 0,
3046 0, // resolution
3047 0,
3048 0,
3049 0,
3050 0, // offset
3051 0,
3052 0, // accuracy
3053 0, // plusTolerance
3054 0, // minusTolerance
3055 3,
3056 0,
3057 0,
3058 0, // hysteresis
3059 0, // supportedThresholds
3060 0, // thresholdAndHysteresisVolatility
3061 0,
3062 0,
3063 0x80,
3064 0x3f, // stateTransistionInterval=1.0
3065 0,
3066 0,
3067 0x80,
3068 0x3f, // updateInverval=1.0
3069 0xa0,
3070 0x86,
3071 0x01,
3072 0x00, // maxReadable = 100000
3073 0x60,
3074 0x79,
3075 0xfe,
3076 0xff, // minReadable = -10000
3077 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
3078 0, // rangeFieldsupport
3079 0,
3080 0,
3081 0,
3082 0, // nominalValue = 0
3083 0x20,
3084 0xa1,
3085 0x07,
3086 0x00, // normalMax = 500,000
3087 0xe0,
3088 0x5e,
3089 0xf8,
3090 0xff, // normalMin = -500,000
3091 0x40,
3092 0x42,
3093 0x0f,
3094 0x00, // warningHigh = 1,000,000
3095 0xc0,
3096 0xbd,
3097 0xf0,
3098 0xff, // warningLow = -1,000,000
3099 0x80,
3100 0x84,
3101 0x1e,
3102 0x00, // criticalHigh = 2,000,000
3103 0x80,
3104 0x7b,
3105 0xe1,
3106 0xff, // criticalLow = -2,000,000
3107 0xc0,
3108 0xc6,
3109 0x2d,
3110 0x00, // fatalHigh = 3,000,000
3111 0x40,
3112 0x39,
3113 0xd2,
3114 0xff // fatalLow = -3,000,000
3115 };
3116
3117 struct pldm_numeric_sensor_value_pdr decodedPdr;
3118 auto rc =
3119 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3120 EXPECT_EQ(PLDM_SUCCESS, rc);
3121
3122 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
3123 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
3124 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
3125 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
3126 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
3127 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
3128 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
3129 EXPECT_EQ(1000000, decodedPdr.warning_high.value_s32);
3130 EXPECT_EQ(-1000000, decodedPdr.warning_low.value_s32);
3131 EXPECT_EQ(2000000, decodedPdr.critical_high.value_s32);
3132 EXPECT_EQ(-2000000, decodedPdr.critical_low.value_s32);
3133 EXPECT_EQ(3000000, decodedPdr.fatal_high.value_s32);
3134 EXPECT_EQ(-3000000, decodedPdr.fatal_low.value_s32);
3135}
3136
3137TEST(decodeNumericSensorPdrData, Real32Test)
3138{
3139 std::vector<uint8_t> pdr1{
3140 0x1,
3141 0x0,
3142 0x0,
3143 0x0, // record handle
3144 0x1, // PDRHeaderVersion
3145 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3146 0x0,
3147 0x0, // recordChangeNumber
3148 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3149 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
3150 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
3151 0, // dataLength
3152 0,
3153 0, // PLDMTerminusHandle
3154 0x1,
3155 0x0, // sensorID=1
3156 PLDM_ENTITY_POWER_SUPPLY,
3157 0, // entityType=Power Supply(120)
3158 1,
3159 0, // entityInstanceNumber
3160 0x1,
3161 0x0, // containerID=1
3162 PLDM_NO_INIT, // sensorInit
3163 false, // sensorAuxiliaryNamesPDR
3164 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3165 0, // unitModifier
3166 0, // rateUnit
3167 0, // baseOEMUnitHandle
3168 0, // auxUnit
3169 0, // auxUnitModifier
3170 0, // auxRateUnit
3171 0, // rel
3172 0, // auxOEMUnitHandle
3173 true, // isLinear
3174 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
3175 0,
3176 0,
3177 0,
3178 0, // resolution
3179 0,
3180 0,
3181 0,
3182 0, // offset
3183 0,
3184 0, // accuracy
3185 0, // plusTolerance
3186 0, // minusTolerance
3187 3,
3188 0,
3189 0,
3190 0, // hysteresis
3191 0, // supportedThresholds
3192 0, // thresholdAndHysteresisVolatility
3193 0,
3194 0,
3195 0x80,
3196 0x3f, // stateTransistionInterval=1.0
3197 0,
3198 0,
3199 0x80,
3200 0x3f, // updateInverval=1.0
3201 0xa0,
3202 0x86,
3203 0x01,
3204 0x00, // maxReadable = 100000
3205 0x60,
3206 0x79,
3207 0xfe,
3208 0xff, // minReadable = -10000
3209 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
3210 0, // rangeFieldsupport
3211 0,
3212 0,
3213 0,
3214 0, // nominalValue = 0.0
3215 0x33,
3216 0x33,
3217 0x48,
3218 0x42, // normalMax = 50.05
3219 0x33,
3220 0x33,
3221 0x48,
3222 0xc2, // normalMin = -50.05
3223 0x83,
3224 0x00,
3225 0xc8,
3226 0x42, // warningHigh = 100.001
3227 0x83,
3228 0x00,
3229 0xc8,
3230 0xc2, // warningLow = -100.001
3231 0x83,
3232 0x00,
3233 0x48,
3234 0x43, // criticalHigh = 200.002
3235 0x83,
3236 0x00,
3237 0x48,
3238 0xc3, // criticalLow = -200.002
3239 0x62,
3240 0x00,
3241 0x96,
3242 0x43, // fatalHigh = 300.003
3243 0x62,
3244 0x00,
3245 0x96,
3246 0xc3 // fatalLow = -300.003
3247 };
3248
3249 struct pldm_numeric_sensor_value_pdr decodedPdr;
3250 auto rc =
3251 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3252 EXPECT_EQ(PLDM_SUCCESS, rc);
3253
3254 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
3255 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
3256 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
3257 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
3258 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
3259 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
3260 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
3261 EXPECT_FLOAT_EQ(100.001f, decodedPdr.warning_high.value_f32);
3262 EXPECT_FLOAT_EQ(-100.001f, decodedPdr.warning_low.value_f32);
3263 EXPECT_FLOAT_EQ(200.002f, decodedPdr.critical_high.value_f32);
3264 EXPECT_FLOAT_EQ(-200.002f, decodedPdr.critical_low.value_f32);
3265 EXPECT_FLOAT_EQ(300.003f, decodedPdr.fatal_high.value_f32);
3266 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.fatal_low.value_f32);
3267}
3268
3269TEST(decodeNumericSensorPdrDataDeathTest, InvalidSizeTest)
3270{
3271 // A corrupted PDR. The data after plusTolerance missed.
3272 std::vector<uint8_t> pdr1{
3273 0x1,
3274 0x0,
3275 0x0,
3276 0x0, // record handle
3277 0x1, // PDRHeaderVersion
3278 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3279 0x0,
3280 0x0, // recordChangeNumber
3281 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH,
3282 0, // dataLength
3283 0,
3284 0, // PLDMTerminusHandle
3285 0x1,
3286 0x0, // sensorID=1
3287 PLDM_ENTITY_POWER_SUPPLY,
3288 0, // entityType=Power Supply(120)
3289 1,
3290 0, // entityInstanceNumber
3291 0x1,
3292 0x0, // containerID=1
3293 PLDM_NO_INIT, // sensorInit
3294 false, // sensorAuxiliaryNamesPDR
3295 2, // baseUint(2)=degrees C
3296 0, // unitModifier
3297 0, // rateUnit
3298 0, // baseOEMUnitHandle
3299 0, // auxUnit
3300 0, // auxUnitModifier
3301 0, // auxRateUnit
3302 0, // rel
3303 0, // auxOEMUnitHandle
3304 true, // isLinear
3305 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
3306 0,
3307 0,
3308 0,
3309 0, // resolution
3310 0,
3311 0,
3312 0,
3313 0, // offset
3314 0,
3315 0, // accuracy
3316 0 // plusTolerance
3317 };
3318
3319 struct pldm_numeric_sensor_value_pdr decodedPdr;
3320 int rc =
3321 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3322 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3323}