blob: a8a530c86aaf181e4b93d4ae5daa942f8768d424 [file] [log] [blame]
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05301#include <endian.h>
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10302#include <libpldm/base.h>
3#include <libpldm/entity.h>
4#include <libpldm/platform.h>
5#include <libpldm/pldm_types.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +09306
7#include <array>
Tal Yacobif490a382024-05-31 09:57:36 +03008#include <cerrno>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05309#include <cstdint>
Andrew Jeffery5a706072023-04-05 19:45:31 +093010#include <cstring>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +053011#include <vector>
Andrew Jeffery9c766792022-08-10 23:12:49 +093012
Thu Nguyen159a98b2022-11-02 10:00:10 +070013#include "msgbuf.h"
Andrew Jeffery9c766792022-08-10 23:12:49 +093014
15#include <gtest/gtest.h>
16
17constexpr auto hdrSize = sizeof(pldm_msg_hdr);
18
19TEST(SetStateEffecterStates, testEncodeResponse)
20{
21 std::array<uint8_t,
22 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
23 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093024 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093025 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
26 uint8_t completionCode = 0;
27
28 auto rc = encode_set_state_effecter_states_resp(0, PLDM_SUCCESS, response);
29
30 EXPECT_EQ(rc, PLDM_SUCCESS);
31 EXPECT_EQ(completionCode, response->payload[0]);
32}
33
34TEST(SetStateEffecterStates, testEncodeRequest)
35{
36 std::array<uint8_t,
37 sizeof(pldm_msg_hdr) + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
38 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093039 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093040 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
41
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -060042 uint16_t effecterId = 0x0a;
Andrew Jeffery9c766792022-08-10 23:12:49 +093043 uint8_t compEffecterCnt = 0x2;
44 std::array<set_effecter_state_field, 8> stateField{};
45 stateField[0] = {PLDM_REQUEST_SET, 2};
46 stateField[1] = {PLDM_REQUEST_SET, 3};
47
48 auto rc = encode_set_state_effecter_states_req(
49 0, effecterId, compEffecterCnt, stateField.data(), request);
50
51 EXPECT_EQ(rc, PLDM_SUCCESS);
52 EXPECT_EQ(effecterId, request->payload[0]);
53 EXPECT_EQ(compEffecterCnt, request->payload[sizeof(effecterId)]);
54 EXPECT_EQ(stateField[0].set_request,
55 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt)]);
56 EXPECT_EQ(stateField[0].effecter_state,
57 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
58 sizeof(stateField[0].set_request)]);
59 EXPECT_EQ(stateField[1].set_request,
60 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
61 sizeof(stateField[0])]);
62 EXPECT_EQ(stateField[1].effecter_state,
63 request->payload[sizeof(effecterId) + sizeof(compEffecterCnt) +
64 sizeof(stateField[0]) +
65 sizeof(stateField[1].set_request)]);
66}
67
68TEST(SetStateEffecterStates, testGoodDecodeResponse)
69{
70 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
71 responseMsg{};
72
73 uint8_t retcompletion_code = 0;
74
75 responseMsg[hdrSize] = PLDM_SUCCESS;
76
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +093077 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +093078 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
79
80 auto rc = decode_set_state_effecter_states_resp(
81 response, responseMsg.size() - hdrSize, &retcompletion_code);
82
83 EXPECT_EQ(rc, PLDM_SUCCESS);
84 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
85}
86
87TEST(SetStateEffecterStates, testGoodDecodeRequest)
88{
89 std::array<uint8_t, hdrSize + PLDM_SET_STATE_EFFECTER_STATES_REQ_BYTES>
90 requestMsg{};
91
92 uint16_t effecterId = 0x32;
93 uint16_t effecterIdLE = htole16(effecterId);
94 uint8_t compEffecterCnt = 0x2;
95
96 std::array<set_effecter_state_field, 8> stateField{};
97 stateField[0] = {PLDM_REQUEST_SET, 3};
98 stateField[1] = {PLDM_REQUEST_SET, 4};
99
100 uint16_t retEffecterId = 0;
101 uint8_t retCompEffecterCnt = 0;
102
103 std::array<set_effecter_state_field, 8> retStateField{};
104
105 memcpy(requestMsg.data() + hdrSize, &effecterIdLE, sizeof(effecterIdLE));
106 memcpy(requestMsg.data() + sizeof(effecterIdLE) + hdrSize, &compEffecterCnt,
107 sizeof(compEffecterCnt));
108 memcpy(requestMsg.data() + sizeof(effecterIdLE) + sizeof(compEffecterCnt) +
109 hdrSize,
110 &stateField, sizeof(stateField));
111
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930112 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930113 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
114
115 auto rc = decode_set_state_effecter_states_req(
116 request, requestMsg.size() - hdrSize, &retEffecterId,
117 &retCompEffecterCnt, retStateField.data());
118
119 EXPECT_EQ(rc, PLDM_SUCCESS);
120 EXPECT_EQ(effecterId, retEffecterId);
121 EXPECT_EQ(retCompEffecterCnt, compEffecterCnt);
122 EXPECT_EQ(retStateField[0].set_request, stateField[0].set_request);
123 EXPECT_EQ(retStateField[0].effecter_state, stateField[0].effecter_state);
124 EXPECT_EQ(retStateField[1].set_request, stateField[1].set_request);
125 EXPECT_EQ(retStateField[1].effecter_state, stateField[1].effecter_state);
126}
127
128TEST(SetStateEffecterStates, testBadDecodeRequest)
129{
130 const struct pldm_msg* msg = NULL;
131
132 auto rc = decode_set_state_effecter_states_req(msg, sizeof(*msg), NULL,
133 NULL, NULL);
134
135 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
136}
137
138TEST(SetStateEffecterStates, testBadDecodeResponse)
139{
140 std::array<uint8_t, PLDM_SET_STATE_EFFECTER_STATES_RESP_BYTES>
141 responseMsg{};
142
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930143 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930144 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
145
146 auto rc = decode_set_state_effecter_states_resp(response,
147 responseMsg.size(), NULL);
148
149 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
150}
151
152TEST(GetPDR, testGoodEncodeResponse)
153{
154 uint8_t completionCode = 0;
155 uint32_t nextRecordHndl = 0x12;
156 uint32_t nextDataTransferHndl = 0x13;
157 uint8_t transferFlag = PLDM_END;
158 uint16_t respCnt = 0x5;
159 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
160 uint8_t transferCRC = 6;
161
162 // + size of record data and transfer CRC
163 std::vector<uint8_t> responseMsg(hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES +
164 recordData.size() + 1);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930165 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930166 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
167
168 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
169 nextDataTransferHndl, transferFlag, respCnt,
170 recordData.data(), transferCRC, response);
171
172 EXPECT_EQ(rc, PLDM_SUCCESS);
173 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930174 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930175 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
176
177 EXPECT_EQ(completionCode, resp->completion_code);
178 EXPECT_EQ(nextRecordHndl, le32toh(resp->next_record_handle));
179 EXPECT_EQ(nextDataTransferHndl, le32toh(resp->next_data_transfer_handle));
180 EXPECT_EQ(transferFlag, resp->transfer_flag);
181 EXPECT_EQ(respCnt, le16toh(resp->response_count));
182 EXPECT_EQ(0,
183 memcmp(recordData.data(), resp->record_data, recordData.size()));
184 EXPECT_EQ(*(response->payload + sizeof(pldm_get_pdr_resp) - 1 +
185 recordData.size()),
186 transferCRC);
187
188 transferFlag = PLDM_START_AND_END; // No CRC in this case
189 responseMsg.resize(responseMsg.size() - sizeof(transferCRC));
190 rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
191 nextDataTransferHndl, transferFlag, respCnt,
192 recordData.data(), transferCRC, response);
193 EXPECT_EQ(rc, PLDM_SUCCESS);
194}
195
196TEST(GetPDR, testBadEncodeResponse)
197{
198 uint32_t nextRecordHndl = 0x12;
199 uint32_t nextDataTransferHndl = 0x13;
200 uint8_t transferFlag = PLDM_START_AND_END;
201 uint16_t respCnt = 0x5;
202 std::vector<uint8_t> recordData{1, 2, 3, 4, 5};
203 uint8_t transferCRC = 0;
204
205 auto rc = encode_get_pdr_resp(0, PLDM_SUCCESS, nextRecordHndl,
206 nextDataTransferHndl, transferFlag, respCnt,
207 recordData.data(), transferCRC, nullptr);
208
209 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
210}
211
212TEST(GetPDR, testGoodDecodeRequest)
213{
214 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REQ_BYTES> requestMsg{};
215
216 uint32_t recordHndl = 0x32;
217 uint32_t dataTransferHndl = 0x11;
218 uint8_t transferOpFlag = PLDM_GET_FIRSTPART;
219 uint16_t requestCnt = 0x5;
220 uint16_t recordChangeNum = 0x01;
221
222 uint32_t retRecordHndl = 0;
223 uint32_t retDataTransferHndl = 0;
224 uint8_t retTransferOpFlag = 0;
225 uint16_t retRequestCnt = 0;
226 uint16_t retRecordChangeNum = 0;
227
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930228 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930229 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
230 struct pldm_get_pdr_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930232 reinterpret_cast<struct pldm_get_pdr_req*>(req->payload);
233
234 request->record_handle = htole32(recordHndl);
235 request->data_transfer_handle = htole32(dataTransferHndl);
236 request->transfer_op_flag = transferOpFlag;
237 request->request_count = htole16(requestCnt);
238 request->record_change_number = htole16(recordChangeNum);
239
240 auto rc = decode_get_pdr_req(
241 req, requestMsg.size() - hdrSize, &retRecordHndl, &retDataTransferHndl,
242 &retTransferOpFlag, &retRequestCnt, &retRecordChangeNum);
243
244 EXPECT_EQ(rc, PLDM_SUCCESS);
245 EXPECT_EQ(retRecordHndl, recordHndl);
246 EXPECT_EQ(retDataTransferHndl, dataTransferHndl);
247 EXPECT_EQ(retTransferOpFlag, transferOpFlag);
248 EXPECT_EQ(retRequestCnt, requestCnt);
249 EXPECT_EQ(retRecordChangeNum, recordChangeNum);
250}
251
252TEST(GetPDR, testBadDecodeRequest)
253{
254 std::array<uint8_t, PLDM_GET_PDR_REQ_BYTES> requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930255 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930256 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
257
258 auto rc = decode_get_pdr_req(req, requestMsg.size(), NULL, NULL, NULL, NULL,
259 NULL);
260
261 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
262}
263
264TEST(GetPDR, testGoodEncodeRequest)
265{
266 uint32_t record_hndl = 0;
267 uint32_t data_transfer_hndl = 0;
268 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
269 uint16_t request_cnt = 20;
270 uint16_t record_chg_num = 0;
271
272 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930273 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930274 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
275
276 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
277 transfer_op_flag, request_cnt, record_chg_num,
278 request, PLDM_GET_PDR_REQ_BYTES);
279 EXPECT_EQ(rc, PLDM_SUCCESS);
280 struct pldm_get_pdr_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930281 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930282 reinterpret_cast<struct pldm_get_pdr_req*>(request->payload);
283 EXPECT_EQ(record_hndl, le32toh(req->record_handle));
284 EXPECT_EQ(data_transfer_hndl, le32toh(req->data_transfer_handle));
285 EXPECT_EQ(transfer_op_flag, req->transfer_op_flag);
286 EXPECT_EQ(request_cnt, le16toh(req->request_count));
287 EXPECT_EQ(record_chg_num, le16toh(req->record_change_number));
288}
289
290TEST(GetPDR, testBadEncodeRequest)
291{
292 uint32_t record_hndl = 0;
293 uint32_t data_transfer_hndl = 0;
294 uint8_t transfer_op_flag = PLDM_GET_FIRSTPART;
295 uint16_t request_cnt = 32;
296 uint16_t record_chg_num = 0;
297
298 std::vector<uint8_t> requestMsg(hdrSize + PLDM_GET_PDR_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930299 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930300 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
301
302 auto rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
303 transfer_op_flag, request_cnt, record_chg_num,
304 nullptr, PLDM_GET_PDR_REQ_BYTES);
305 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
306
307 rc = encode_get_pdr_req(0, record_hndl, data_transfer_hndl,
308 transfer_op_flag, request_cnt, record_chg_num,
309 request, PLDM_GET_PDR_REQ_BYTES + 1);
310 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
311}
312
313TEST(GetPDR, testGoodDecodeResponse)
314{
315 const char* recordData = "123456789";
316 uint8_t completionCode = PLDM_SUCCESS;
317 uint32_t nextRecordHndl = 0;
318 uint32_t nextDataTransferHndl = 0;
319 uint8_t transferFlag = PLDM_END;
320 constexpr uint16_t respCnt = 9;
321 uint8_t transferCRC = 96;
322 size_t recordDataLength = 32;
323 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
324 sizeof(transferCRC)>
325 responseMsg{};
326
327 uint8_t retCompletionCode = 0;
328 uint8_t retRecordData[32] = {0};
329 uint32_t retNextRecordHndl = 0;
330 uint32_t retNextDataTransferHndl = 0;
331 uint8_t retTransferFlag = 0;
332 uint16_t retRespCnt = 0;
333 uint8_t retTransferCRC = 0;
334
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930335 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930336 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
337 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930338 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
340 resp->completion_code = completionCode;
341 resp->next_record_handle = htole32(nextRecordHndl);
342 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
343 resp->transfer_flag = transferFlag;
344 resp->response_count = htole16(respCnt);
345 memcpy(resp->record_data, recordData, respCnt);
346 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
347
348 auto rc = decode_get_pdr_resp(
349 response, responseMsg.size() - hdrSize, &retCompletionCode,
350 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
351 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
352 EXPECT_EQ(rc, PLDM_SUCCESS);
353 EXPECT_EQ(retCompletionCode, completionCode);
354 EXPECT_EQ(retNextRecordHndl, nextRecordHndl);
355 EXPECT_EQ(retNextDataTransferHndl, nextDataTransferHndl);
356 EXPECT_EQ(retTransferFlag, transferFlag);
357 EXPECT_EQ(retRespCnt, respCnt);
358 EXPECT_EQ(retTransferCRC, transferCRC);
359 EXPECT_EQ(0, memcmp(recordData, resp->record_data, respCnt));
360}
361
362TEST(GetPDR, testBadDecodeResponse)
363{
364 const char* recordData = "123456789";
365 uint8_t completionCode = PLDM_SUCCESS;
366 uint32_t nextRecordHndl = 0;
367 uint32_t nextDataTransferHndl = 0;
368 uint8_t transferFlag = PLDM_END;
369 constexpr uint16_t respCnt = 9;
370 uint8_t transferCRC = 96;
Andrew Jeffery4e5e8a22023-04-04 11:58:45 +0930371 size_t recordDataLength = respCnt - 1;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930372 std::array<uint8_t, hdrSize + PLDM_GET_PDR_MIN_RESP_BYTES + respCnt +
373 sizeof(transferCRC)>
374 responseMsg{};
375
376 uint8_t retCompletionCode = 0;
377 uint8_t retRecordData[32] = {0};
378 uint32_t retNextRecordHndl = 0;
379 uint32_t retNextDataTransferHndl = 0;
380 uint8_t retTransferFlag = 0;
381 uint16_t retRespCnt = 0;
382 uint8_t retTransferCRC = 0;
383
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930384 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930385 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
386 struct pldm_get_pdr_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930387 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930388 reinterpret_cast<struct pldm_get_pdr_resp*>(response->payload);
389 resp->completion_code = completionCode;
390 resp->next_record_handle = htole32(nextRecordHndl);
391 resp->next_data_transfer_handle = htole32(nextDataTransferHndl);
392 resp->transfer_flag = transferFlag;
393 resp->response_count = htole16(respCnt);
394 memcpy(resp->record_data, recordData, respCnt);
395 response->payload[PLDM_GET_PDR_MIN_RESP_BYTES + respCnt] = transferCRC;
396
397 auto rc = decode_get_pdr_resp(response, responseMsg.size() - hdrSize, NULL,
398 NULL, NULL, NULL, NULL, NULL, 0, NULL);
399 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
400
401 rc = decode_get_pdr_resp(
402 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
403 &retNextRecordHndl, &retNextDataTransferHndl, &retTransferFlag,
404 &retRespCnt, retRecordData, recordDataLength, &retTransferCRC);
405 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
406}
407
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000408#ifdef LIBPLDM_API_TESTING
409TEST(GetPDR, testGoodDecodeResponseSafe)
410{
411 static const char recordData[] = "123456789";
412
413 alignas(pldm_msg) unsigned char data[sizeof(pldm_msg_hdr) +
414 PLDM_GET_PDR_MIN_RESP_BYTES +
415 sizeof(recordData) - 1 + 1];
416 struct pldm_msgbuf _buf;
417 struct pldm_msgbuf* buf = &_buf;
418 int rc;
419
420 pldm_msg* msg = new (data) pldm_msg;
421
422 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_MIN_RESP_BYTES, msg->payload,
423 sizeof(data) - sizeof(msg->hdr));
424 ASSERT_EQ(rc, 0);
425
426 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS);
427 pldm_msgbuf_insert_uint32(buf, 0);
428 pldm_msgbuf_insert_uint32(buf, 0);
429 pldm_msgbuf_insert_uint8(buf, PLDM_END);
430 pldm_msgbuf_insert_uint16(buf, sizeof(recordData) - 1);
431 rc = pldm_msgbuf_insert_array_char(buf, sizeof(recordData) - 1, recordData,
432 sizeof(recordData) - 1);
433 ASSERT_EQ(rc, 0);
434 pldm_msgbuf_insert_uint8(buf, 96);
435 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
436
437 alignas(pldm_get_pdr_resp) unsigned char
438 resp_data[sizeof(pldm_get_pdr_resp) + sizeof(recordData) - 1];
439 pldm_get_pdr_resp* resp = new (resp_data) pldm_get_pdr_resp;
440 uint8_t crc;
441 rc = decode_get_pdr_resp_safe(msg, sizeof(data) - sizeof(msg->hdr), resp,
442 sizeof(resp_data) - sizeof(*resp), &crc);
443 ASSERT_EQ(rc, 0);
444 EXPECT_EQ(resp->completion_code, PLDM_SUCCESS);
445 EXPECT_EQ(resp->next_record_handle, 0);
446 EXPECT_EQ(resp->next_data_transfer_handle, 0);
447 EXPECT_EQ(resp->transfer_flag, PLDM_END);
448 ASSERT_EQ(resp->response_count, sizeof(recordData) - 1);
449 EXPECT_EQ(crc, 96);
450 EXPECT_EQ(0, memcmp(recordData, resp->record_data, resp->response_count));
451}
452#endif
453
454#ifdef LIBPLDM_API_TESTING
455TEST(GetPDR, testBadDecodeResponseSafeTrivial)
456{
457 pldm_get_pdr_resp resp;
458 uint8_t crc;
459 int rc;
460
461 rc = decode_get_pdr_resp_safe(nullptr, PLDM_GET_PDR_MIN_RESP_BYTES, &resp,
462 sizeof(resp), &crc);
463 EXPECT_EQ(rc, -EINVAL);
464
465 alignas(pldm_msg) unsigned char
466 msg_data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_MIN_RESP_BYTES];
467 pldm_msg* msg = new (msg_data) pldm_msg;
468 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, nullptr,
469 sizeof(resp), &crc);
470 EXPECT_EQ(rc, -EINVAL);
471
472 rc = decode_get_pdr_resp_safe(msg, PLDM_GET_PDR_MIN_RESP_BYTES, &resp,
473 sizeof(resp), nullptr);
474 EXPECT_EQ(rc, -EINVAL);
475
476 msg->payload[0] = PLDM_ERROR_INVALID_DATA;
477 rc = decode_get_pdr_resp_safe(msg, 1, &resp, sizeof(resp), &crc);
478 EXPECT_EQ(rc, 0);
479 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA);
480}
481#endif
482
Andrew Jeffery9c766792022-08-10 23:12:49 +0930483TEST(GetPDRRepositoryInfo, testGoodEncodeResponse)
484{
485 uint8_t completionCode = 0;
486 uint8_t repositoryState = PLDM_AVAILABLE;
487 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
488 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
489 uint32_t recordCount = 100;
490 uint32_t repositorySize = 100;
491 uint32_t largestRecordSize = UINT32_MAX;
492 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
493
494 std::vector<uint8_t> responseMsg(hdrSize +
495 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930496 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930497 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
498
499 auto rc = encode_get_pdr_repository_info_resp(
500 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
501 recordCount, repositorySize, largestRecordSize,
502 dataTransferHandleTimeout, response);
503
504 EXPECT_EQ(rc, PLDM_SUCCESS);
505 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930506 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930507 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
508 response->payload);
509
510 EXPECT_EQ(completionCode, resp->completion_code);
511 EXPECT_EQ(repositoryState, resp->repository_state);
512 EXPECT_EQ(0, memcmp(updateTime, resp->update_time, PLDM_TIMESTAMP104_SIZE));
513 EXPECT_EQ(0, memcmp(oemUpdateTime, resp->oem_update_time,
514 PLDM_TIMESTAMP104_SIZE));
515 EXPECT_EQ(recordCount, le32toh(resp->record_count));
516 EXPECT_EQ(repositorySize, le32toh(resp->repository_size));
517 EXPECT_EQ(largestRecordSize, le32toh(resp->largest_record_size));
518 EXPECT_EQ(dataTransferHandleTimeout, resp->data_transfer_handle_timeout);
519}
520
521TEST(GetPDRRepositoryInfo, testBadEncodeResponse)
522{
523 uint8_t repositoryState = PLDM_AVAILABLE;
524 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
525 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
526 uint32_t recordCount = 100;
527 uint32_t repositorySize = 100;
528 uint32_t largestRecordSize = UINT32_MAX;
529 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
530
531 auto rc = encode_get_pdr_repository_info_resp(
532 0, PLDM_SUCCESS, repositoryState, updateTime, oemUpdateTime,
533 recordCount, repositorySize, largestRecordSize,
534 dataTransferHandleTimeout, nullptr);
535 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
536}
537
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800538TEST(GetPDRRepositoryInfo, testGoodDecodeResponse)
539{
540 uint8_t completionCode = PLDM_SUCCESS;
541 uint8_t repositoryState = PLDM_AVAILABLE;
542 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
543 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
544 uint32_t recordCount = 100;
545 uint32_t repositorySize = 100;
546 uint32_t largestRecordSize = UINT32_MAX;
547 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
548
549 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
550 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930551 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800552 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
553 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930554 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800555 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
556 response->payload);
557 resp->completion_code = completionCode;
558 resp->repository_state = repositoryState;
559 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
560 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
561 resp->record_count = htole32(recordCount);
562 resp->repository_size = htole32(repositorySize);
563 resp->largest_record_size = htole32(largestRecordSize);
564 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
565
566 uint8_t retCompletionCode = 0;
567 uint8_t retRepositoryState = 0;
568 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
569 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
570 uint32_t retRecordCount = 0;
571 uint32_t retRepositorySize = 0;
572 uint32_t retLargestRecordSize = 0;
573 uint8_t retDataTransferHandleTimeout = 0;
574
575 auto rc = decode_get_pdr_repository_info_resp(
576 response, responseMsg.size() - hdrSize, &retCompletionCode,
577 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
578 &retRepositorySize, &retLargestRecordSize,
579 &retDataTransferHandleTimeout);
580
581 EXPECT_EQ(rc, PLDM_SUCCESS);
582 EXPECT_EQ(completionCode, retCompletionCode);
583 EXPECT_EQ(repositoryState, retRepositoryState);
584 EXPECT_EQ(0, memcmp(updateTime, retUpdateTime, PLDM_TIMESTAMP104_SIZE));
585 EXPECT_EQ(0,
586 memcmp(oemUpdateTime, retOemUpdateTime, PLDM_TIMESTAMP104_SIZE));
587 EXPECT_EQ(recordCount, recordCount);
588 EXPECT_EQ(repositorySize, repositorySize);
589 EXPECT_EQ(largestRecordSize, largestRecordSize);
590 EXPECT_EQ(dataTransferHandleTimeout, dataTransferHandleTimeout);
591}
592
593TEST(GetPDRRepositoryInfo, testBadDecodeResponse)
594{
595 uint8_t completionCode = PLDM_SUCCESS;
596 uint8_t repositoryState = PLDM_AVAILABLE;
597 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
598 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
599 uint32_t recordCount = htole32(100);
600 uint32_t repositorySize = htole32(100);
601 uint32_t largestRecordSize = htole32(UINT32_MAX);
602 uint8_t dataTransferHandleTimeout = PLDM_NO_TIMEOUT;
603
604 std::array<uint8_t, hdrSize + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES>
605 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930606 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800607 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
608 struct pldm_pdr_repository_info_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930609 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chenb7c73e52022-11-10 11:29:52 +0800610 reinterpret_cast<struct pldm_pdr_repository_info_resp*>(
611 response->payload);
612 resp->completion_code = completionCode;
613 resp->repository_state = repositoryState;
614 memcpy(resp->update_time, updateTime, PLDM_TIMESTAMP104_SIZE);
615 memcpy(resp->oem_update_time, oemUpdateTime, PLDM_TIMESTAMP104_SIZE);
616 resp->record_count = recordCount;
617 resp->repository_size = repositorySize;
618 resp->largest_record_size = largestRecordSize;
619 resp->data_transfer_handle_timeout = dataTransferHandleTimeout;
620
621 uint8_t retCompletionCode = 0;
622 uint8_t retRepositoryState = 0;
623 uint8_t retUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
624 uint8_t retOemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
625 uint32_t retRecordCount = 0;
626 uint32_t retRepositorySize = 0;
627 uint32_t retLargestRecordSize = 0;
628 uint8_t retDataTransferHandleTimeout = 0;
629
630 auto rc = decode_get_pdr_repository_info_resp(
631 response, responseMsg.size() - hdrSize, NULL, NULL, NULL, NULL, NULL,
632 NULL, NULL, NULL);
633 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
634
635 rc = decode_get_pdr_repository_info_resp(
636 response, responseMsg.size() - hdrSize - 1, &retCompletionCode,
637 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
638 &retRepositorySize, &retLargestRecordSize,
639 &retDataTransferHandleTimeout);
640 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
641
642 resp->repository_state = PLDM_FAILED + 1;
643 rc = decode_get_pdr_repository_info_resp(
644 response, responseMsg.size() - hdrSize, &retCompletionCode,
645 &retRepositoryState, retUpdateTime, retOemUpdateTime, &retRecordCount,
646 &retRepositorySize, &retLargestRecordSize,
647 &retDataTransferHandleTimeout);
648 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
649}
650
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +0000651#ifdef LIBPLDM_API_TESTING
652TEST(GetPDRRepositoryInfo, testGoodDecodeResponseSafe)
653{
654 alignas(pldm_msg) unsigned char
655 data[sizeof(pldm_msg_hdr) + PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES];
656 uint8_t updateTime[PLDM_TIMESTAMP104_SIZE] = {0};
657 uint8_t oemUpdateTime[PLDM_TIMESTAMP104_SIZE] = {0};
658 struct pldm_msgbuf _buf;
659 struct pldm_msgbuf* buf = &_buf;
660 int rc;
661
662 pldm_msg* msg = new (data) pldm_msg;
663
664 rc = pldm_msgbuf_init_errno(buf, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES,
665 msg->payload, sizeof(data) - sizeof(msg->hdr));
666 ASSERT_EQ(rc, 0);
667 pldm_msgbuf_insert_uint8(buf, PLDM_SUCCESS);
668 pldm_msgbuf_insert_uint8(buf, PLDM_AVAILABLE);
669 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE, updateTime,
670 sizeof(updateTime));
671 ASSERT_EQ(rc, 0);
672 rc = pldm_msgbuf_insert_array_uint8(buf, PLDM_TIMESTAMP104_SIZE,
673 oemUpdateTime, sizeof(oemUpdateTime));
674 ASSERT_EQ(rc, 0);
675 pldm_msgbuf_insert_uint32(buf, 100);
676 pldm_msgbuf_insert_uint32(buf, 100);
677 pldm_msgbuf_insert_uint32(buf, UINT32_MAX);
678 pldm_msgbuf_insert_uint8(buf, PLDM_NO_TIMEOUT);
679 ASSERT_EQ(pldm_msgbuf_destroy_consumed(buf), 0);
680
681 struct pldm_pdr_repository_info_resp resp;
682 rc = decode_get_pdr_repository_info_resp_safe(
683 msg, sizeof(data) - sizeof(msg->hdr), &resp);
684
685 EXPECT_EQ(rc, 0);
686 EXPECT_EQ(PLDM_SUCCESS, resp.completion_code);
687 EXPECT_EQ(PLDM_AVAILABLE, resp.repository_state);
688 EXPECT_EQ(0,
689 memcmp(updateTime, resp.update_time, sizeof(resp.update_time)));
690 EXPECT_EQ(0, memcmp(oemUpdateTime, resp.oem_update_time,
691 sizeof(resp.oem_update_time)));
692 EXPECT_EQ(100, resp.record_count);
693 EXPECT_EQ(100, resp.repository_size);
694 EXPECT_EQ(UINT32_MAX, resp.largest_record_size);
695 EXPECT_EQ(PLDM_NO_TIMEOUT, resp.data_transfer_handle_timeout);
696}
697#endif
698
699#ifdef LIBPLDM_API_TESTING
700TEST(GetPDRRepositoryInfo, testBadDecodeResponseSafeTrivial)
701{
702 struct pldm_pdr_repository_info_resp resp;
703 int rc;
704
705 rc = decode_get_pdr_repository_info_resp_safe(
706 nullptr, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, &resp);
707 EXPECT_EQ(rc, -EINVAL);
708
709 alignas(pldm_msg) unsigned char
710 msg_data[sizeof(pldm_msg) - 1 +
711 PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES];
712 pldm_msg* msg = new (msg_data) pldm_msg;
713 rc = decode_get_pdr_repository_info_resp_safe(msg, 0, &resp);
714 EXPECT_EQ(rc, -EOVERFLOW);
715
716 rc = decode_get_pdr_repository_info_resp_safe(
717 msg, PLDM_GET_PDR_REPOSITORY_INFO_RESP_BYTES, nullptr);
718 EXPECT_EQ(rc, -EINVAL);
719
720 msg->payload[0] = PLDM_ERROR_INVALID_DATA;
721 rc = decode_get_pdr_repository_info_resp_safe(msg, 1, &resp);
722 EXPECT_EQ(rc, 0);
723 EXPECT_EQ(resp.completion_code, PLDM_ERROR_INVALID_DATA);
724}
725#endif
726
Andrew Jeffery9c766792022-08-10 23:12:49 +0930727TEST(SetNumericEffecterValue, testGoodDecodeRequest)
728{
729 std::array<uint8_t,
730 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 3>
731 requestMsg{};
732
733 uint16_t effecter_id = 32768;
734 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT32;
735 uint32_t effecter_value = 123456789;
736
737 uint16_t reteffecter_id;
738 uint8_t reteffecter_data_size;
739 uint8_t reteffecter_value[4];
740
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930741 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930742 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
743 struct pldm_set_numeric_effecter_value_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930744 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930745 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
746 req->payload);
747
748 request->effecter_id = htole16(effecter_id);
749 request->effecter_data_size = effecter_data_size;
750 uint32_t effecter_value_le = htole32(effecter_value);
751 memcpy(request->effecter_value, &effecter_value_le,
752 sizeof(effecter_value_le));
753
754 auto rc = decode_set_numeric_effecter_value_req(
755 req, requestMsg.size() - hdrSize, &reteffecter_id,
756 &reteffecter_data_size, reteffecter_value);
757
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930758 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930759 uint32_t value = *(reinterpret_cast<uint32_t*>(reteffecter_value));
760 EXPECT_EQ(rc, PLDM_SUCCESS);
761 EXPECT_EQ(reteffecter_id, effecter_id);
762 EXPECT_EQ(reteffecter_data_size, effecter_data_size);
763 EXPECT_EQ(value, effecter_value);
764}
765
766TEST(SetNumericEffecterValue, testBadDecodeRequest)
767{
768 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES>
769 requestMsg{};
770
771 auto rc = decode_set_numeric_effecter_value_req(
772 NULL, requestMsg.size() - hdrSize, NULL, NULL, NULL);
773 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
774
775 uint16_t effecter_id = 0x10;
776 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT8;
777 uint8_t effecter_value = 1;
778
779 uint16_t reteffecter_id;
780 uint8_t reteffecter_data_size;
781 uint8_t reteffecter_value[4];
782
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930783 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930784 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
785 struct pldm_set_numeric_effecter_value_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930786 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930787 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
788 req->payload);
789
790 request->effecter_id = effecter_id;
791 request->effecter_data_size = effecter_data_size;
792 memcpy(request->effecter_value, &effecter_value, sizeof(effecter_value));
793
794 rc = decode_set_numeric_effecter_value_req(
795 req, requestMsg.size() - hdrSize - 1, &reteffecter_id,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930796 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930797 &reteffecter_data_size, reinterpret_cast<uint8_t*>(&reteffecter_value));
798 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
799}
800
801TEST(SetNumericEffecterValue, testGoodEncodeRequest)
802{
803 uint16_t effecter_id = 0;
804 uint8_t effecter_data_size = PLDM_EFFECTER_DATA_SIZE_UINT16;
805 uint16_t effecter_value = 65534;
806
807 std::vector<uint8_t> requestMsg(
808 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930809 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930810 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
811
812 auto rc = encode_set_numeric_effecter_value_req(
813 0, effecter_id, effecter_data_size,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930814 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930815 reinterpret_cast<uint8_t*>(&effecter_value), request,
816 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES + 1);
817 EXPECT_EQ(rc, PLDM_SUCCESS);
818
819 struct pldm_set_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930820 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930821 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
822 request->payload);
823 EXPECT_EQ(effecter_id, req->effecter_id);
824 EXPECT_EQ(effecter_data_size, req->effecter_data_size);
825 uint16_t* val = (uint16_t*)req->effecter_value;
826 *val = le16toh(*val);
827 EXPECT_EQ(effecter_value, *val);
828}
829
830TEST(SetNumericEffecterValue, testBadEncodeRequest)
831{
832 std::vector<uint8_t> requestMsg(
833 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930834 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930835 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
836
837 auto rc = encode_set_numeric_effecter_value_req(
838 0, 0, 0, NULL, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
839 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
840
841 uint16_t effecter_value;
842 rc = encode_set_numeric_effecter_value_req(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930843 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930844 0, 0, 6, reinterpret_cast<uint8_t*>(&effecter_value), request,
845 PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
846 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
847}
848
849TEST(SetNumericEffecterValue, testGoodDecodeResponse)
850{
851 std::array<uint8_t, hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
852 responseMsg{};
853
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600854 uint8_t completion_code = 0xa0;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930855
856 uint8_t retcompletion_code;
857
858 memcpy(responseMsg.data() + hdrSize, &completion_code,
859 sizeof(completion_code));
860
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930861 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930862 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
863
864 auto rc = decode_set_numeric_effecter_value_resp(
865 response, responseMsg.size() - hdrSize, &retcompletion_code);
866
867 EXPECT_EQ(rc, PLDM_SUCCESS);
868 EXPECT_EQ(completion_code, retcompletion_code);
869}
870
871TEST(SetNumericEffecterValue, testBadDecodeResponse)
872{
873 std::array<uint8_t, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
874 responseMsg{};
875
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930876 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930877 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
878
879 auto rc = decode_set_numeric_effecter_value_resp(response,
880 responseMsg.size(), NULL);
881
882 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
883}
884
885TEST(SetNumericEffecterValue, testGoodEncodeResponse)
886{
887 std::array<uint8_t, sizeof(pldm_msg_hdr) +
888 PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES>
889 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930890 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930891 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
892 uint8_t completionCode = 0;
893
894 auto rc = encode_set_numeric_effecter_value_resp(
895 0, PLDM_SUCCESS, response, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
896
897 EXPECT_EQ(rc, PLDM_SUCCESS);
898 EXPECT_EQ(completionCode, response->payload[0]);
899}
900
901TEST(SetNumericEffecterValue, testBadEncodeResponse)
902{
903 auto rc = encode_set_numeric_effecter_value_resp(
904 0, PLDM_SUCCESS, NULL, PLDM_SET_NUMERIC_EFFECTER_VALUE_RESP_BYTES);
905 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
906}
907
908TEST(GetStateSensorReadings, testGoodEncodeResponse)
909{
910 std::array<uint8_t, hdrSize +
911 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
912 sizeof(get_sensor_state_field) * 2>
913 responseMsg{};
914
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930915 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930916 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
917 uint8_t completionCode = 0;
918 uint8_t comp_sensorCnt = 0x2;
919
920 std::array<get_sensor_state_field, 2> stateField{};
921 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_NORMAL,
922 PLDM_SENSOR_WARNING, PLDM_SENSOR_UNKNOWN};
923 stateField[1] = {PLDM_SENSOR_FAILED, PLDM_SENSOR_UPPERFATAL,
924 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_FATAL};
925
926 auto rc = encode_get_state_sensor_readings_resp(
927 0, PLDM_SUCCESS, comp_sensorCnt, stateField.data(), response);
928
929 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930930 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930931 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
932 response->payload);
933
934 EXPECT_EQ(rc, PLDM_SUCCESS);
935 EXPECT_EQ(completionCode, resp->completion_code);
936 EXPECT_EQ(comp_sensorCnt, resp->comp_sensor_count);
937 EXPECT_EQ(stateField[0].sensor_op_state, resp->field->sensor_op_state);
938 EXPECT_EQ(stateField[0].present_state, resp->field->present_state);
939 EXPECT_EQ(stateField[0].previous_state, resp->field->previous_state);
940 EXPECT_EQ(stateField[0].event_state, resp->field->event_state);
941 EXPECT_EQ(stateField[1].sensor_op_state, resp->field[1].sensor_op_state);
942 EXPECT_EQ(stateField[1].present_state, resp->field[1].present_state);
943 EXPECT_EQ(stateField[1].previous_state, resp->field[1].previous_state);
944 EXPECT_EQ(stateField[1].event_state, resp->field[1].event_state);
945}
946
947TEST(GetStateSensorReadings, testBadEncodeResponse)
948{
949 auto rc = encode_get_state_sensor_readings_resp(0, PLDM_SUCCESS, 0, nullptr,
950 nullptr);
951
952 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
953}
954
955TEST(GetStateSensorReadings, testGoodDecodeResponse)
956{
957 std::array<uint8_t, hdrSize +
958 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
959 sizeof(get_sensor_state_field) * 2>
960 responseMsg{};
961
962 uint8_t completionCode = 0;
963 uint8_t comp_sensorCnt = 2;
964
965 std::array<get_sensor_state_field, 2> stateField{};
966 stateField[0] = {PLDM_SENSOR_DISABLED, PLDM_SENSOR_UNKNOWN,
967 PLDM_SENSOR_UNKNOWN, PLDM_SENSOR_UNKNOWN};
968 stateField[1] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_LOWERFATAL,
969 PLDM_SENSOR_LOWERCRITICAL, PLDM_SENSOR_WARNING};
970
971 uint8_t retcompletion_code = 0;
972 uint8_t retcomp_sensorCnt = 0;
973 std::array<get_sensor_state_field, 2> retstateField{};
974
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930975 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930976 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
977 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930978 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930979 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
980 response->payload);
981
982 resp->completion_code = completionCode;
983 resp->comp_sensor_count = comp_sensorCnt;
984 memcpy(resp->field, &stateField,
985 (sizeof(get_sensor_state_field) * comp_sensorCnt));
986
987 auto rc = decode_get_state_sensor_readings_resp(
988 response, responseMsg.size() - hdrSize, &retcompletion_code,
989 &retcomp_sensorCnt, retstateField.data());
990
991 EXPECT_EQ(rc, PLDM_SUCCESS);
992 EXPECT_EQ(completionCode, retcompletion_code);
993 EXPECT_EQ(comp_sensorCnt, retcomp_sensorCnt);
994 EXPECT_EQ(stateField[0].sensor_op_state, retstateField[0].sensor_op_state);
995 EXPECT_EQ(stateField[0].present_state, retstateField[0].present_state);
996 EXPECT_EQ(stateField[0].previous_state, retstateField[0].previous_state);
997 EXPECT_EQ(stateField[0].event_state, retstateField[0].event_state);
998 EXPECT_EQ(stateField[1].sensor_op_state, retstateField[1].sensor_op_state);
999 EXPECT_EQ(stateField[1].present_state, retstateField[1].present_state);
1000 EXPECT_EQ(stateField[1].previous_state, retstateField[1].previous_state);
1001 EXPECT_EQ(stateField[1].event_state, retstateField[1].event_state);
1002}
1003
1004TEST(GetStateSensorReadings, testBadDecodeResponse)
1005{
1006 std::array<uint8_t, hdrSize +
1007 PLDM_GET_STATE_SENSOR_READINGS_MIN_RESP_BYTES +
1008 sizeof(get_sensor_state_field) * 2>
1009 responseMsg{};
1010
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301011 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301012 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1013
1014 auto rc = decode_get_state_sensor_readings_resp(
1015 response, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr);
1016
1017 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1018
1019 uint8_t completionCode = 0;
1020 uint8_t comp_sensorCnt = 1;
1021
1022 std::array<get_sensor_state_field, 1> stateField{};
1023 stateField[0] = {PLDM_SENSOR_ENABLED, PLDM_SENSOR_UPPERFATAL,
1024 PLDM_SENSOR_UPPERCRITICAL, PLDM_SENSOR_WARNING};
1025
1026 uint8_t retcompletion_code = 0;
1027 uint8_t retcomp_sensorCnt = 0;
1028 std::array<get_sensor_state_field, 1> retstateField{};
1029
1030 struct pldm_get_state_sensor_readings_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301031 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301032 reinterpret_cast<struct pldm_get_state_sensor_readings_resp*>(
1033 response->payload);
1034
1035 resp->completion_code = completionCode;
1036 resp->comp_sensor_count = comp_sensorCnt;
1037 memcpy(resp->field, &stateField,
1038 (sizeof(get_sensor_state_field) * comp_sensorCnt));
1039
1040 rc = decode_get_state_sensor_readings_resp(
Andrew Jeffery6ad4dc02023-04-12 15:56:45 +09301041 response, responseMsg.size() - hdrSize, &retcompletion_code,
Andrew Jeffery9c766792022-08-10 23:12:49 +09301042 &retcomp_sensorCnt, retstateField.data());
1043
1044 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1045}
1046
1047TEST(GetStateSensorReadings, testGoodEncodeRequest)
1048{
1049 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1050 requestMsg{};
1051
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001052 uint16_t sensorId = 0xab;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301053 bitfield8_t sensorRearm;
1054 sensorRearm.byte = 0x03;
1055
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301056 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301057 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1058 auto rc = encode_get_state_sensor_readings_req(0, sensorId, sensorRearm, 0,
1059 request);
1060
1061 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301062 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301063 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1064 request->payload);
1065
1066 EXPECT_EQ(rc, PLDM_SUCCESS);
1067 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
1068 EXPECT_EQ(sensorRearm.byte, req->sensor_rearm.byte);
1069}
1070
1071TEST(GetStateSensorReadings, testBadEncodeRequest)
1072{
1073 bitfield8_t sensorRearm;
1074 sensorRearm.byte = 0x0;
1075
1076 auto rc =
1077 encode_get_state_sensor_readings_req(0, 0, sensorRearm, 0, nullptr);
1078
1079 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1080}
1081
1082TEST(GetStateSensorReadings, testGoodDecodeRequest)
1083{
1084 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1085 requestMsg{};
1086
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06001087 uint16_t sensorId = 0xcd;
Andrew Jeffery9c766792022-08-10 23:12:49 +09301088 bitfield8_t sensorRearm;
1089 sensorRearm.byte = 0x10;
1090
1091 uint16_t retsensorId;
1092 bitfield8_t retsensorRearm;
1093 uint8_t retreserved;
1094
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301095 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301096 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1097
1098 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301099 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301100 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1101 request->payload);
1102
1103 req->sensor_id = htole16(sensorId);
1104 req->sensor_rearm.byte = sensorRearm.byte;
1105
1106 auto rc = decode_get_state_sensor_readings_req(
1107 request, requestMsg.size() - hdrSize, &retsensorId, &retsensorRearm,
1108 &retreserved);
1109
1110 EXPECT_EQ(rc, PLDM_SUCCESS);
1111 EXPECT_EQ(sensorId, retsensorId);
1112 EXPECT_EQ(sensorRearm.byte, retsensorRearm.byte);
1113 EXPECT_EQ(0, retreserved);
1114}
1115
1116TEST(GetStateSensorReadings, testBadDecodeRequest)
1117{
1118 std::array<uint8_t, hdrSize + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES>
1119 requestMsg{};
1120
1121 auto rc = decode_get_state_sensor_readings_req(
1122 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr, nullptr);
1123
1124 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1125 uint16_t sensorId = 0x11;
1126 bitfield8_t sensorRearm;
1127 sensorRearm.byte = 0x04;
1128
1129 uint16_t retsensorId;
1130 bitfield8_t retsensorRearm;
1131 uint8_t retreserved;
1132
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301133 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301134 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1135
1136 struct pldm_get_state_sensor_readings_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301137 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301138 reinterpret_cast<struct pldm_get_state_sensor_readings_req*>(
1139 request->payload);
1140
1141 req->sensor_id = htole16(sensorId);
1142 req->sensor_rearm.byte = sensorRearm.byte;
1143
1144 rc = decode_get_state_sensor_readings_req(
1145 request, requestMsg.size() - hdrSize - 1, &retsensorId, &retsensorRearm,
1146 &retreserved);
1147
1148 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1149}
1150
Dung Caod6ae8982022-11-02 10:00:10 +07001151TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeRequest)
1152{
1153 uint8_t eventBufferSize = 32;
1154
1155 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_REQ_BYTES>
1156 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301157 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001158 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1159
1160 auto rc = encode_event_message_buffer_size_req(0, eventBufferSize, request);
1161
1162 EXPECT_EQ(rc, PLDM_SUCCESS);
1163}
1164
1165TEST(EventMessageBufferSize, testGoodEventMessageBufferSizeResponse)
1166{
1167 uint8_t completionCode = PLDM_SUCCESS;
1168 uint16_t terminusMaxBufferSize = 256;
1169
1170 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
1171 responseMsg{};
1172
1173 uint8_t retCompletionCode;
1174 uint16_t retMaxBufferSize = 0;
1175
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301176 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1178 struct pldm_event_message_buffer_size_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301179 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001180 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
1181 response->payload);
1182
1183 resp->completion_code = completionCode;
1184 resp->terminus_max_buffer_size = terminusMaxBufferSize;
1185
1186 auto rc = decode_event_message_buffer_size_resp(
1187 response, responseMsg.size() - hdrSize, &retCompletionCode,
1188 &retMaxBufferSize);
1189
1190 EXPECT_EQ(rc, PLDM_SUCCESS);
1191 EXPECT_EQ(retCompletionCode, completionCode);
1192 EXPECT_EQ(terminusMaxBufferSize, retMaxBufferSize);
1193}
1194
1195TEST(EventMessageBufferSize, testBadEventMessageBufferSizeResponse)
1196{
1197 uint8_t completionCode = PLDM_SUCCESS;
1198 uint16_t terminusMaxBufferSize = 256;
1199
1200 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_BUFFER_SIZE_RESP_BYTES>
1201 responseMsg{};
1202
1203 uint8_t retCompletionCode;
1204 uint16_t retMaxBufferSize = 0;
1205
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301206 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001207 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1208 struct pldm_event_message_buffer_size_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301209 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Caod6ae8982022-11-02 10:00:10 +07001210 reinterpret_cast<struct pldm_event_message_buffer_size_resp*>(
1211 response->payload);
1212 resp->completion_code = completionCode;
1213 resp->terminus_max_buffer_size = terminusMaxBufferSize;
1214
1215 auto rc =
1216 decode_event_message_buffer_size_resp(response, 0, nullptr, nullptr);
1217 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1218
1219 rc = decode_event_message_buffer_size_resp(
1220 response, responseMsg.size(), &retCompletionCode, &retMaxBufferSize);
1221 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1222}
1223
Dung Cao1bf8c872022-11-29 05:32:58 +07001224TEST(PlatformEventMessageSupported, testGoodEncodeRequest)
1225{
1226 uint8_t formatVersion = 0x01;
1227
1228 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES>
1229 requestMsg{};
1230
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301231 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001232 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1233
1234 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1235
1236 struct pldm_event_message_supported_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301237 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001238 reinterpret_cast<struct pldm_event_message_supported_req*>(
1239 request->payload);
1240
1241 EXPECT_EQ(rc, PLDM_SUCCESS);
1242 EXPECT_EQ(formatVersion, req->format_version);
1243}
1244
1245TEST(PlatformEventMessageSupported, testBadEncodeRequest)
1246{
1247 uint8_t eventData = 34;
1248 uint8_t formatVersion = 0x0;
1249
1250 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_REQ_BYTES +
1251 sizeof(eventData)>
1252 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301253 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001254 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1255
1256 auto rc = encode_event_message_supported_req(0, formatVersion, request);
1257 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1258
1259 rc = encode_event_message_supported_req(0, formatVersion, nullptr);
1260 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1261}
1262
1263TEST(PlatformEventMessageSupported, testGoodDecodeRespond)
1264{
1265 uint8_t completionCode = PLDM_SUCCESS;
1266 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1267 bitfield8_t synchConfigSupported;
1268 synchConfigSupported.byte = 0xe;
1269 uint8_t numberEventClassReturned = 0x3;
1270 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1271 constexpr uint8_t eventClassCount = 3;
1272
1273 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1274 eventClassCount>
1275 responseMsg{};
1276
1277 uint8_t retCompletionCode;
1278 uint8_t retSynchConfig = 0;
1279 uint8_t retNumberEventClass = 0;
1280 bitfield8_t retSynchConfigSupport;
1281 uint8_t retEventClass[eventClassCount] = {0};
1282
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301283 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001284 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1285 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301286 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001287 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1288 response->payload);
1289
1290 resp->completion_code = completionCode;
1291 resp->synchrony_configuration = synchConfiguration;
1292 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1293 resp->number_event_class_returned = numberEventClassReturned;
1294 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1295
1296 auto rc = decode_event_message_supported_resp(
1297 response, responseMsg.size() - hdrSize, &retCompletionCode,
1298 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1299 retEventClass, eventClassCount);
1300
1301 EXPECT_EQ(rc, PLDM_SUCCESS);
1302 EXPECT_EQ(retCompletionCode, completionCode);
1303 EXPECT_EQ(retSynchConfig, synchConfiguration);
1304 EXPECT_EQ(retNumberEventClass, numberEventClassReturned);
1305 EXPECT_EQ(retSynchConfigSupport.byte, synchConfigSupported.byte);
1306 EXPECT_EQ(0, memcmp(eventClass.data(), resp->event_class,
1307 numberEventClassReturned));
1308}
1309
1310TEST(PlatformEventMessageSupported, testBadSynchConfiguration)
1311{
1312 uint8_t completionCode = PLDM_SUCCESS;
1313 uint8_t synchConfiguration = 0x4;
1314 bitfield8_t synchConfigSupported;
1315 synchConfigSupported.byte = 0xe;
1316 uint8_t numberEventClassReturned = 0x3;
1317 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1318 constexpr uint8_t eventClassCount = 3;
1319
1320 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1321 eventClassCount>
1322 responseMsg{};
1323
1324 uint8_t retCompletionCode;
1325 uint8_t retSynchConfig = 0;
1326 uint8_t retNumberEventClass = 0;
1327 bitfield8_t retSynchConfigSupport;
1328 uint8_t retEventClass[eventClassCount] = {0};
1329
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301330 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001331 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1332 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301333 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001334 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1335 response->payload);
1336
1337 resp->completion_code = completionCode;
1338 resp->synchrony_configuration = synchConfiguration;
1339 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1340 resp->number_event_class_returned = numberEventClassReturned;
1341 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1342
1343 auto rc = decode_event_message_supported_resp(
1344 response, responseMsg.size() - hdrSize, &retCompletionCode,
1345 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1346 retEventClass, eventClassCount);
1347
1348 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1349}
1350
1351TEST(PlatformEventMessageSupported, testBadDecodeRespond)
1352{
1353 uint8_t completionCode = PLDM_SUCCESS;
1354 uint8_t synchConfiguration = PLDM_MESSAGE_TYPE_SYNCHRONOUS;
1355 bitfield8_t synchConfigSupported;
1356 synchConfigSupported.byte = 0xe;
1357 uint8_t numberEventClassReturned = 0x3;
1358 std::vector<uint8_t> eventClass{0x0, 0x5, 0xfa};
1359 constexpr uint8_t eventClassCount = 3;
1360
1361 std::array<uint8_t, hdrSize + PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES +
1362 eventClassCount>
1363 responseMsg{};
1364
1365 uint8_t retCompletionCode;
1366 uint8_t retSynchConfig = 0;
1367 uint8_t retNumberEventClass = 0;
1368 bitfield8_t retSynchConfigSupport;
1369 uint8_t retEventClass[eventClassCount] = {0};
1370
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301371 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001372 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1373 struct pldm_event_message_supported_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301374 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao1bf8c872022-11-29 05:32:58 +07001375 reinterpret_cast<struct pldm_event_message_supported_resp*>(
1376 response->payload);
1377 resp->completion_code = completionCode;
1378 resp->synchrony_configuration = synchConfiguration;
1379 resp->synchrony_configuration_supported.byte = synchConfigSupported.byte;
1380 resp->number_event_class_returned = numberEventClassReturned;
1381 memcpy(resp->event_class, eventClass.data(), numberEventClassReturned);
1382
1383 auto rc = decode_event_message_supported_resp(response, 0, nullptr, nullptr,
1384 nullptr, nullptr, nullptr, 0);
1385 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1386
1387 rc = decode_event_message_supported_resp(
1388 response, PLDM_EVENT_MESSAGE_SUPPORTED_MIN_RESP_BYTES - 1,
1389 &retCompletionCode, &retSynchConfig, &retSynchConfigSupport,
1390 &retNumberEventClass, retEventClass, eventClassCount);
1391 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1392
1393 rc = decode_event_message_supported_resp(
1394 response, responseMsg.size() - hdrSize, &retCompletionCode,
1395 &retSynchConfig, &retSynchConfigSupport, &retNumberEventClass,
1396 retEventClass, 1);
1397 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1398}
1399
Thu Nguyen159a98b2022-11-02 10:00:10 +07001400TEST(PollForPlatformEventMessage, testGoodEncodeRequest)
1401{
1402 uint8_t formatVersion = 0x01;
1403 uint8_t transferOperationFlag = 0x1;
1404 uint32_t dataTransferHandle = 0xffffffff;
1405 uint16_t eventIdToAcknowledge = 0x0;
1406
1407 std::array<uint8_t,
1408 hdrSize + PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES>
1409 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301410 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001411 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1412
1413 auto rc = encode_poll_for_platform_event_message_req(
1414 0, formatVersion, transferOperationFlag, dataTransferHandle,
1415 eventIdToAcknowledge, request,
1416 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1417 EXPECT_EQ(rc, PLDM_SUCCESS);
1418
1419 struct pldm_msgbuf _buf;
1420 struct pldm_msgbuf* buf = &_buf;
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301421 rc = pldm_msgbuf_init_cc(
1422 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES, request->payload,
1423 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
Thu Nguyen159a98b2022-11-02 10:00:10 +07001424 EXPECT_EQ(rc, PLDM_SUCCESS);
1425
1426 uint8_t retFormatVersion;
1427 uint8_t retTransferOperationFlag;
1428 uint32_t retDataTransferHandle;
1429 uint16_t retEventIdToAcknowledge;
1430
1431 pldm_msgbuf_extract_uint8(buf, &retFormatVersion);
1432 pldm_msgbuf_extract_uint8(buf, &retTransferOperationFlag);
1433 pldm_msgbuf_extract_uint32(buf, &retDataTransferHandle);
1434 pldm_msgbuf_extract_uint16(buf, &retEventIdToAcknowledge);
1435
1436 EXPECT_EQ(retFormatVersion, formatVersion);
1437 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1438 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1439 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1440 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
1441}
1442
1443TEST(PollForPlatformEventMessage, testBadEncodeRequest)
1444{
1445 uint8_t formatVersion = 0x01;
1446 uint8_t transferOperationFlag = 0x1;
1447 uint32_t dataTransferHandle = 0xffffffff;
1448 uint16_t eventIdToAcknowledge = 0x0;
1449
1450 std::array<uint8_t,
1451 hdrSize + PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES>
1452 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301453 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001454 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1455
1456 auto rc = encode_poll_for_platform_event_message_req(
1457 0, formatVersion, transferOperationFlag, dataTransferHandle,
1458 eventIdToAcknowledge, nullptr,
1459 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_REQ_BYTES);
1460
1461 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1462
1463 encode_poll_for_platform_event_message_req(
1464 0, formatVersion, transferOperationFlag, dataTransferHandle,
1465 eventIdToAcknowledge, request, hdrSize);
1466}
1467
1468TEST(PollForPlatformEventMessage, testGoodDecodeRespond)
1469{
1470 uint8_t completionCode = PLDM_SUCCESS;
1471 uint8_t tId = 0x9;
1472 uint16_t eventId = 159;
1473 uint32_t nextDataTransferHandle = 0x11223344;
1474 uint8_t transferFlag = PLDM_START_AND_END;
1475 uint8_t eventClass = 0x5;
1476 uint8_t eventData[5] = {0x55, 0x44, 0x33, 0x22, 0x11};
1477 constexpr uint32_t eventDataSize = 0x00000005;
1478 uint32_t eventDataIntegrityChecksum = 0x66778899;
1479
1480 std::vector<uint8_t> responseMsg{
1481 0x1,
1482 0x0,
1483 0x0,
1484 PLDM_SUCCESS,
1485 0x9, // tid
1486 159,
1487 0x0, // event id
1488 0x44,
1489 0x33,
1490 0x22,
1491 0x11, // next_data_transfer_handle
1492 PLDM_START_AND_END, // transfer_flag
1493 0x05, // event class
1494 0x05,
1495 0x00,
1496 0x00,
1497 0x00, // event_data_size
1498 0x55,
1499 0x44,
1500 0x33,
1501 0x22,
1502 0x11, // event_data[5]
1503 0x99,
1504 0x88,
1505 0x77,
1506 0x66 // event_data_integrity_checksum
1507 };
1508 const uint32_t respMsgLen = 23;
1509
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301510 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001511 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1512
1513 uint8_t retCompletionCode;
1514 uint8_t retTid = 0;
1515 uint16_t retEventId = 0;
1516 uint32_t retNextDataTransferHandle = 0;
1517 uint8_t retTransferFlag = 0;
1518 uint8_t retEventClass = 0;
1519 uint32_t retEventDataSize = 0;
1520 uint8_t* retEventData = nullptr;
1521 uint32_t retEventDataIntegrityChecksum = 0;
1522
1523 auto rc = decode_poll_for_platform_event_message_resp(
1524 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1525 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1526 &retEventDataSize, (void**)&retEventData,
1527 &retEventDataIntegrityChecksum);
1528
1529 EXPECT_EQ(rc, PLDM_SUCCESS);
1530 EXPECT_EQ(retCompletionCode, completionCode);
1531 EXPECT_EQ(retTid, tId);
1532 EXPECT_EQ(retEventId, eventId);
1533 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
1534 EXPECT_EQ(retTransferFlag, transferFlag);
1535 EXPECT_EQ(retEventClass, eventClass);
1536 EXPECT_EQ(retEventDataSize, eventDataSize);
1537 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
1538 EXPECT_EQ(0, memcmp(eventData, retEventData, eventDataSize));
1539}
1540
1541TEST(PollForPlatformEventMessage, testGoodDecodeAckOnlyRespond)
1542{
1543 uint8_t completionCode = PLDM_SUCCESS;
1544 uint8_t tId = 0x9;
1545 uint16_t eventId = 0xffff;
1546
1547 std::vector<uint8_t> responseMsg{
1548 0x1, 0x0, 0x0, PLDM_SUCCESS,
1549 0x9, // tid
1550 0xff,
1551 0xff // event id
1552 };
1553 const uint32_t respMsgLen = 4;
1554
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301555 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001556 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1557
1558 uint8_t retCompletionCode;
1559 uint8_t retTid = 0;
1560 uint16_t retEventId = 0;
1561 uint32_t retNextDataTransferHandle = 0;
1562 uint8_t retTransferFlag = 0;
1563 uint8_t retEventClass = 0;
1564 uint32_t retEventDataSize = 0;
1565 uint8_t* retEventData = nullptr;
1566 uint32_t retEventDataIntegrityChecksum = 0;
1567
1568 auto rc = decode_poll_for_platform_event_message_resp(
1569 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1570 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1571 &retEventDataSize, (void**)&retEventData,
1572 &retEventDataIntegrityChecksum);
1573
1574 EXPECT_EQ(rc, PLDM_SUCCESS);
1575 EXPECT_EQ(retCompletionCode, completionCode);
1576 EXPECT_EQ(retTid, tId);
1577 EXPECT_EQ(retEventId, eventId);
1578
1579 eventId = 0x0000;
1580 responseMsg[5] = 0x00;
1581 responseMsg[6] = 0x00;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301582 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001583 response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1584
1585 rc = decode_poll_for_platform_event_message_resp(
1586 response, respMsgLen, &retCompletionCode, &retTid, &retEventId,
1587 &retNextDataTransferHandle, &retTransferFlag, &retEventClass,
1588 &retEventDataSize, (void**)&retEventData,
1589 &retEventDataIntegrityChecksum);
1590
1591 EXPECT_EQ(rc, PLDM_SUCCESS);
1592 EXPECT_EQ(retCompletionCode, completionCode);
1593 EXPECT_EQ(retTid, tId);
1594 EXPECT_EQ(retEventId, eventId);
1595}
1596
1597TEST(PollForPlatformEventMessage, testBadDecodeRespond)
1598{
1599 std::vector<uint8_t> responseMsg{
1600 0x1,
1601 0x0,
1602 0x0,
1603 PLDM_SUCCESS,
1604 0x9, // tid
1605 159,
1606 0x0, // event id
1607 0x44,
1608 0x33,
1609 0x22,
1610 0x11, // next_data_transfer_handle
1611 PLDM_START_AND_END, // transfer_flag
1612 0x05, // event class
1613 0x05,
1614 0x00,
1615 0x00,
1616 0x00, // event_data_size
1617 0x55,
1618 0x44,
1619 0x33,
1620 0x22,
1621 0x11, // event_data[5]
1622 0x99,
1623 0x88,
1624 0x77,
1625 0x66 // event_data_integrity_checksum
1626 };
1627 // const uint32_t respMsgLen = 23;
1628
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301629 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen159a98b2022-11-02 10:00:10 +07001630 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1631
1632 auto rc = decode_poll_for_platform_event_message_resp(
1633 nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
1634 nullptr, nullptr, nullptr);
1635
1636 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1637
1638 uint8_t retCompletionCode;
1639 uint8_t retTid = 0;
1640 uint16_t retEventId = 0;
1641 uint32_t retNextDataTransferHandle = 0;
1642 uint8_t retTransferFlag = 0;
1643 uint8_t retEventClass = 0;
1644 uint32_t retEventDataSize = 0;
1645 uint8_t* retEventData = nullptr;
1646 uint32_t retEventDataIntegrityChecksum = 0;
1647
1648 rc = decode_poll_for_platform_event_message_resp(
1649 response, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES - 1,
1650 &retCompletionCode, &retTid, &retEventId, &retNextDataTransferHandle,
1651 &retTransferFlag, &retEventClass, &retEventDataSize,
1652 (void**)&retEventData, &retEventDataIntegrityChecksum);
1653
1654 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1655}
1656
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001657TEST(PollForPlatformEventMessage, testGoodDecodeRequestFirstPart)
1658{
1659 uint8_t formatVersion = 0x1;
1660 uint8_t transferOperationFlag = PLDM_GET_FIRSTPART;
1661 uint32_t dataTransferHandle = 0x11223344;
1662 uint16_t eventIdToAcknowledge = 0x0;
1663 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART,
1664 0x44, 0x33, 0x22, 0x11, 0x00,
1665 0x00};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301666 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001667 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1668
1669 uint8_t retFormatVersion;
1670 uint8_t retTransferOperationFlag;
1671 uint32_t retDataTransferHandle;
1672 uint16_t retEventIdToAcknowledge;
1673
1674 auto rc = decode_poll_for_platform_event_message_req(
1675 request, requestMsg.size() - hdrSize, &retFormatVersion,
1676 &retTransferOperationFlag, &retDataTransferHandle,
1677 &retEventIdToAcknowledge);
1678
1679 EXPECT_EQ(rc, PLDM_SUCCESS);
1680 EXPECT_EQ(retFormatVersion, formatVersion);
1681 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1682 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1683 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1684}
1685
1686TEST(PollForPlatformEventMessage, testGoodDecodeRequestNextPart)
1687{
1688 uint8_t formatVersion = 0x1;
1689 uint8_t transferOperationFlag = PLDM_GET_NEXTPART;
1690 uint32_t dataTransferHandle = 0x11223344;
1691 uint16_t eventIdToAcknowledge = 0xffff;
1692 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_NEXTPART,
1693 0x44, 0x33, 0x22, 0x11, 0xff,
1694 0xff};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301695 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001696 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1697
1698 uint8_t retFormatVersion;
1699 uint8_t retTransferOperationFlag;
1700 uint32_t retDataTransferHandle;
1701 uint16_t retEventIdToAcknowledge;
1702
1703 auto rc = decode_poll_for_platform_event_message_req(
1704 request, requestMsg.size() - hdrSize, &retFormatVersion,
1705 &retTransferOperationFlag, &retDataTransferHandle,
1706 &retEventIdToAcknowledge);
1707
1708 EXPECT_EQ(rc, PLDM_SUCCESS);
1709 EXPECT_EQ(retFormatVersion, formatVersion);
1710 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1711 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1712 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1713}
1714
1715TEST(PollForPlatformEventMessage, testGoodDecodeRequestAck)
1716{
1717 uint8_t formatVersion = 0x1;
1718 uint8_t transferOperationFlag = PLDM_ACKNOWLEDGEMENT_ONLY;
1719 uint32_t dataTransferHandle = 0x11223344;
1720 uint16_t eventIdToAcknowledge = 0xffff;
1721 std::vector<uint8_t> requestMsg{
1722 0x1, 0x0, 0x0, 0x1, PLDM_ACKNOWLEDGEMENT_ONLY, 0x44, 0x33,
1723 0x22, 0x11, 0xff, 0xff};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301724 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001725 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1726
1727 uint8_t retFormatVersion;
1728 uint8_t retTransferOperationFlag;
1729 uint32_t retDataTransferHandle;
1730 uint16_t retEventIdToAcknowledge;
1731
1732 auto rc = decode_poll_for_platform_event_message_req(
1733 request, requestMsg.size() - hdrSize, &retFormatVersion,
1734 &retTransferOperationFlag, &retDataTransferHandle,
1735 &retEventIdToAcknowledge);
1736
1737 EXPECT_EQ(rc, PLDM_SUCCESS);
1738 EXPECT_EQ(retFormatVersion, formatVersion);
1739 EXPECT_EQ(retTransferOperationFlag, transferOperationFlag);
1740 EXPECT_EQ(retDataTransferHandle, dataTransferHandle);
1741 EXPECT_EQ(retEventIdToAcknowledge, eventIdToAcknowledge);
1742}
1743
1744TEST(PollForPlatformEventMessage, testBadDecodeRequest)
1745{
1746 std::vector<uint8_t> requestMsg{0x1, 0x0, 0x0, 0x1, PLDM_GET_FIRSTPART,
1747 0x44, 0x33, 0x22, 0x11, 0x66,
1748 0x55};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301749 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001750 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1751
1752 uint8_t retFormatVersion;
1753 uint8_t retTransferOperationFlag;
1754 uint32_t retDataTransferHandle;
1755 uint16_t retEventIdToAcknowledge;
1756
1757 auto rc = decode_poll_for_platform_event_message_req(
1758 NULL, requestMsg.size() - hdrSize, &retFormatVersion,
1759 &retTransferOperationFlag, &retDataTransferHandle,
1760 &retEventIdToAcknowledge);
1761 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1762
1763 /*
1764 * transfer_operation_flag is not PLDM_GET_FIRSTPART or PLDM_GET_NEXTPART or
1765 * PLDM_ACKNOWLEDGEMENT_ONLY
1766 */
1767
1768 requestMsg[4] = PLDM_ACKNOWLEDGEMENT_ONLY + 1;
1769
1770 rc = decode_poll_for_platform_event_message_req(
1771 request, requestMsg.size() - hdrSize, &retFormatVersion,
1772 &retTransferOperationFlag, &retDataTransferHandle,
1773 &retEventIdToAcknowledge);
1774
1775 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1776 /*
1777 * transfer_operation_flag is PLDM_GET_FIRSTPART and
1778 * event_id_to_acknowledge not 0x0000
1779 */
1780 requestMsg[4] = PLDM_GET_FIRSTPART;
1781 requestMsg[9] = 0x0;
1782 requestMsg[10] = 0x1;
1783
1784 rc = decode_poll_for_platform_event_message_req(
1785 request, requestMsg.size() - hdrSize, &retFormatVersion,
1786 &retTransferOperationFlag, &retDataTransferHandle,
1787 &retEventIdToAcknowledge);
1788
1789 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1790
1791 /*
1792 * transfer_operation_flag is not PLDM_GET_FIRSTPART and
1793 * event_id_to_acknowledge is 0x0000
1794 */
1795 requestMsg[4] = PLDM_GET_NEXTPART;
1796 requestMsg[9] = 0x0;
1797 requestMsg[10] = 0x0;
1798
1799 rc = decode_poll_for_platform_event_message_req(
1800 request, requestMsg.size() - hdrSize, &retFormatVersion,
1801 &retTransferOperationFlag, &retDataTransferHandle,
1802 &retEventIdToAcknowledge);
1803
1804 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1805
1806 /*
1807 * transfer_operation_flag is PLDM_GET_NEXTPART and
1808 * event_id_to_acknowledge not 0xffff
1809 */
1810 requestMsg[4] = PLDM_GET_NEXTPART;
1811 requestMsg[9] = 0x0;
1812 requestMsg[10] = 0x1;
1813
1814 rc = decode_poll_for_platform_event_message_req(
1815 request, requestMsg.size() - hdrSize, &retFormatVersion,
1816 &retTransferOperationFlag, &retDataTransferHandle,
1817 &retEventIdToAcknowledge);
1818
1819 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1820
1821 /*
1822 * transfer_operation_flag is not PLDM_GET_NEXTPART and
1823 * event_id_to_acknowledge is 0xffff
1824 */
1825 requestMsg[4] = PLDM_GET_FIRSTPART;
1826 requestMsg[9] = 0xff;
1827 requestMsg[10] = 0xff;
1828
1829 rc = decode_poll_for_platform_event_message_req(
1830 request, requestMsg.size() - hdrSize, &retFormatVersion,
1831 &retTransferOperationFlag, &retDataTransferHandle,
1832 &retEventIdToAcknowledge);
1833
1834 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1835}
1836
1837TEST(PollForPlatformEventMessage, testGoodEncodeResposeP1)
1838{
1839 uint8_t completionCode = PLDM_SUCCESS;
1840 uint8_t instance_id = 0;
1841 uint8_t tId = 0x9;
1842 uint16_t eventId = 0x1;
1843 uint32_t nextDataTransferHandle = 0xffff;
1844 uint8_t transferFlag = PLDM_END;
1845 uint8_t eventClass = 0x5;
1846 constexpr uint32_t eventDataSize = 9;
1847 uint8_t pEventData[eventDataSize] = {0x31, 0x32, 0x33, 0x34, 0x35,
1848 0x36, 0x37, 0x38, 0x39};
1849 uint32_t eventDataIntegrityChecksum = 0x11223344;
1850 constexpr size_t payloadLength =
1851 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
1852
1853 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301854 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001855 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1856
1857 auto rc = encode_poll_for_platform_event_message_resp(
1858 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
1859 transferFlag, eventClass, eventDataSize, pEventData,
1860 eventDataIntegrityChecksum, response, payloadLength);
1861 EXPECT_EQ(rc, PLDM_SUCCESS);
1862
1863 struct pldm_msgbuf _buf;
1864 struct pldm_msgbuf* buf = &_buf;
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301865 rc = pldm_msgbuf_init_cc(
1866 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1867 response->payload, payloadLength);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001868 EXPECT_EQ(rc, PLDM_SUCCESS);
1869
1870 uint8_t retCompletionCode;
1871 uint8_t retTid = 0;
1872 uint16_t retEventId = 0;
1873 uint32_t retNextDataTransferHandle = 0;
1874 uint8_t retTransferFlag = 0;
1875 uint8_t retEventClass = 0;
1876 uint32_t retEventDataSize = 0;
1877 uint8_t retEventData[payloadLength] = {0};
1878 uint32_t retEventDataIntegrityChecksum = 0;
1879
1880 pldm_msgbuf_extract_uint8(buf, &retCompletionCode);
1881 pldm_msgbuf_extract_uint8(buf, &retTid);
1882 pldm_msgbuf_extract_uint16(buf, &retEventId);
1883 pldm_msgbuf_extract_uint32(buf, &retNextDataTransferHandle);
1884 pldm_msgbuf_extract_uint8(buf, &retTransferFlag);
1885 pldm_msgbuf_extract_uint8(buf, &retEventClass);
1886 pldm_msgbuf_extract_uint32(buf, &retEventDataSize);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00001887 rc = pldm_msgbuf_extract_array_uint8(buf, retEventDataSize, retEventData,
1888 sizeof(retEventData));
1889 ASSERT_EQ(rc, 0);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001890 pldm_msgbuf_extract_uint32(buf, &retEventDataIntegrityChecksum);
1891
1892 EXPECT_EQ(rc, PLDM_SUCCESS);
1893 EXPECT_EQ(retCompletionCode, completionCode);
1894 EXPECT_EQ(retTid, tId);
1895 EXPECT_EQ(retEventId, eventId);
1896 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
1897 EXPECT_EQ(retTransferFlag, transferFlag);
1898 EXPECT_EQ(retEventClass, eventClass);
1899 EXPECT_EQ(retEventDataSize, eventDataSize);
1900 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
1901 EXPECT_EQ(0, memcmp(pEventData, retEventData, eventDataSize));
1902
1903 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
1904}
1905
1906TEST(PollForPlatformEventMessage, testGoodEncodeResposeP2)
1907{
1908 uint8_t completionCode = PLDM_SUCCESS;
1909 uint8_t instance_id = 0;
1910 uint8_t tId = 0x9;
1911 uint16_t eventId = 0x0000;
1912 constexpr size_t payloadLength =
1913 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES;
1914
1915 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301916 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001917 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1918
1919 auto rc = encode_poll_for_platform_event_message_resp(
1920 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0,
1921 response, payloadLength);
1922 EXPECT_EQ(rc, PLDM_SUCCESS);
1923
1924 struct pldm_msgbuf _buf;
1925 struct pldm_msgbuf* buf = &_buf;
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301926 rc = pldm_msgbuf_init_cc(
1927 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1928 response->payload, payloadLength);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001929 EXPECT_EQ(rc, PLDM_SUCCESS);
1930
1931 uint8_t retCompletionCode;
1932 uint8_t retTid = 0;
1933 uint16_t retEventId = 0;
1934
1935 pldm_msgbuf_extract_uint8(buf, &retCompletionCode);
1936 pldm_msgbuf_extract_uint8(buf, &retTid);
1937 pldm_msgbuf_extract_uint16(buf, &retEventId);
1938
1939 EXPECT_EQ(rc, PLDM_SUCCESS);
1940 EXPECT_EQ(retCompletionCode, completionCode);
1941 EXPECT_EQ(retTid, tId);
1942 EXPECT_EQ(retEventId, eventId);
1943 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
1944}
1945
1946TEST(PollForPlatformEventMessage, testGoodEncodeResposeP3)
1947{
1948 uint8_t completionCode = PLDM_SUCCESS;
1949 uint8_t instance_id = 0;
1950 uint8_t tId = 0x9;
1951 uint16_t eventId = 0xffff;
1952 constexpr size_t payloadLength =
1953 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES;
1954
1955 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301956 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001957 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1958
1959 auto rc = encode_poll_for_platform_event_message_resp(
1960 instance_id, completionCode, tId, eventId, 0, 0, 0, 0, NULL, 0,
1961 response, payloadLength);
1962 EXPECT_EQ(rc, PLDM_SUCCESS);
1963
1964 struct pldm_msgbuf _buf;
1965 struct pldm_msgbuf* buf = &_buf;
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09301966 rc = pldm_msgbuf_init_cc(
1967 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
1968 response->payload, payloadLength);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07001969 EXPECT_EQ(rc, PLDM_SUCCESS);
1970
1971 uint8_t retCompletionCode;
1972 uint8_t retTid = 0;
1973 uint16_t retEventId = 0;
1974
1975 pldm_msgbuf_extract_uint8(buf, &retCompletionCode);
1976 pldm_msgbuf_extract_uint8(buf, &retTid);
1977 pldm_msgbuf_extract_uint16(buf, &retEventId);
1978
1979 EXPECT_EQ(rc, PLDM_SUCCESS);
1980 EXPECT_EQ(retCompletionCode, completionCode);
1981 EXPECT_EQ(retTid, tId);
1982 EXPECT_EQ(retEventId, eventId);
1983 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
1984}
1985
1986TEST(PollForPlatformEventMessage, testGoodEncodeResposeP4)
1987{
1988 uint8_t completionCode = PLDM_SUCCESS;
1989 uint8_t instance_id = 0;
1990 uint8_t tId = 0x9;
1991 uint16_t eventId = 0x1;
1992 uint32_t nextDataTransferHandle = 0xffff;
1993 uint8_t transferFlag = PLDM_END;
1994 uint8_t eventClass = 0x5;
1995 constexpr uint32_t eventDataSize = 0;
1996 uint32_t eventDataIntegrityChecksum = 0x11223344;
1997 size_t payloadLength =
1998 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
1999
2000 std::array<uint8_t, hdrSize +
2001 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES +
2002 eventDataSize + 4>
2003 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302004 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002005 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2006
2007 auto rc = encode_poll_for_platform_event_message_resp(
2008 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2009 transferFlag, eventClass, eventDataSize, NULL,
2010 eventDataIntegrityChecksum, response, payloadLength);
2011 EXPECT_EQ(rc, PLDM_SUCCESS);
2012
2013 struct pldm_msgbuf _buf;
2014 struct pldm_msgbuf* buf = &_buf;
Andrew Jefferyc8df31c2024-05-21 16:47:43 +09302015 rc = pldm_msgbuf_init_cc(
2016 buf, PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_MIN_RESP_BYTES,
2017 response->payload, payloadLength);
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002018 EXPECT_EQ(rc, PLDM_SUCCESS);
2019
2020 uint8_t retCompletionCode;
2021 uint8_t retTid = 0;
2022 uint16_t retEventId = 0;
2023 uint32_t retNextDataTransferHandle = 0;
2024 uint8_t retTransferFlag = 0;
2025 uint8_t retEventClass = 0;
2026 uint32_t retEventDataSize = 0;
2027 uint32_t retEventDataIntegrityChecksum = 0;
2028
2029 pldm_msgbuf_extract_uint8(buf, &retCompletionCode);
2030 pldm_msgbuf_extract_uint8(buf, &retTid);
2031 pldm_msgbuf_extract_uint16(buf, &retEventId);
2032 pldm_msgbuf_extract_uint32(buf, &retNextDataTransferHandle);
2033 pldm_msgbuf_extract_uint8(buf, &retTransferFlag);
2034 pldm_msgbuf_extract_uint8(buf, &retEventClass);
2035 pldm_msgbuf_extract_uint32(buf, &retEventDataSize);
2036 pldm_msgbuf_extract_uint32(buf, &retEventDataIntegrityChecksum);
2037
2038 EXPECT_EQ(rc, PLDM_SUCCESS);
2039 EXPECT_EQ(retCompletionCode, completionCode);
2040 EXPECT_EQ(retTid, tId);
2041 EXPECT_EQ(retEventId, eventId);
2042 EXPECT_EQ(retNextDataTransferHandle, nextDataTransferHandle);
2043 EXPECT_EQ(retTransferFlag, transferFlag);
2044 EXPECT_EQ(retEventClass, eventClass);
2045 EXPECT_EQ(retEventDataSize, eventDataSize);
2046 EXPECT_EQ(retEventDataIntegrityChecksum, eventDataIntegrityChecksum);
2047
2048 EXPECT_EQ(pldm_msgbuf_destroy(buf), PLDM_SUCCESS);
2049}
2050
2051TEST(PollForPlatformEventMessage, testBadEncodeResponse)
2052{
2053 uint8_t completionCode = PLDM_SUCCESS;
2054 uint8_t instance_id = 0;
2055 uint8_t tId = 0x9;
2056 uint16_t eventId = 0x1;
2057 uint32_t nextDataTransferHandle = 0xffff;
2058 uint8_t transferFlag = 0x0;
2059 uint8_t eventClass = 0x5;
2060 const uint32_t eventDataSize = 0;
2061 uint32_t eventDataIntegrityChecksum = 0x11223344;
2062 constexpr size_t payloadLength =
2063 PLDM_POLL_FOR_PLATFORM_EVENT_MESSAGE_RESP_BYTES + eventDataSize + 4;
2064
2065 std::array<uint8_t, hdrSize + payloadLength> responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302066 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen8eb20f22022-11-16 22:34:55 +07002067 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2068
2069 auto rc = encode_poll_for_platform_event_message_resp(
2070 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2071 transferFlag, eventClass, eventDataSize, NULL,
2072 eventDataIntegrityChecksum, NULL, payloadLength);
2073
2074 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2075
2076 rc = encode_poll_for_platform_event_message_resp(
2077 instance_id, completionCode, tId, eventId, nextDataTransferHandle,
2078 transferFlag, eventClass, 1, NULL, eventDataIntegrityChecksum, response,
2079 payloadLength);
2080 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2081}
2082
Andrew Jeffery9c766792022-08-10 23:12:49 +09302083TEST(PlatformEventMessage, testGoodStateSensorDecodeRequest)
2084{
2085 std::array<uint8_t,
2086 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2087 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES>
2088 requestMsg{};
2089
2090 uint8_t retFormatVersion = 0;
2091 uint8_t retTid = 0;
2092 uint8_t retEventClass = 0;
2093 size_t retEventDataOffset = 0;
2094
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302095 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302096 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
2097 struct pldm_platform_event_message_req* request =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302098 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302099 reinterpret_cast<struct pldm_platform_event_message_req*>(req->payload);
2100
2101 uint8_t formatVersion = 0x01;
2102 uint8_t tid = 0x02;
2103 // Sensor Event
2104 uint8_t eventClass = 0x00;
2105
2106 request->format_version = formatVersion;
2107 request->tid = tid;
2108 request->event_class = eventClass;
2109 size_t eventDataOffset =
2110 sizeof(formatVersion) + sizeof(tid) + sizeof(eventClass);
2111
2112 auto rc = decode_platform_event_message_req(
2113 req, requestMsg.size() - hdrSize, &retFormatVersion, &retTid,
2114 &retEventClass, &retEventDataOffset);
2115
2116 EXPECT_EQ(rc, PLDM_SUCCESS);
2117 EXPECT_EQ(retFormatVersion, formatVersion);
2118 EXPECT_EQ(retTid, tid);
2119 EXPECT_EQ(retEventClass, eventClass);
2120 EXPECT_EQ(retEventDataOffset, eventDataOffset);
2121}
2122
2123TEST(PlatformEventMessage, testBadDecodeRequest)
2124{
2125 const struct pldm_msg* msg = NULL;
2126 std::array<uint8_t,
2127 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2128 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
2129 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302130 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302131 auto req = reinterpret_cast<pldm_msg*>(requestMsg.data());
2132 uint8_t retFormatVersion;
2133 uint8_t retTid = 0;
2134 uint8_t retEventClass = 0;
2135 size_t retEventDataOffset;
2136
2137 auto rc = decode_platform_event_message_req(msg, sizeof(*msg), NULL, NULL,
2138 NULL, NULL);
2139 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2140
2141 rc = decode_platform_event_message_req(
2142 req,
2143 requestMsg.size() - hdrSize -
2144 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES,
2145 &retFormatVersion, &retTid, &retEventClass, &retEventDataOffset);
2146 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2147}
2148
2149TEST(PlatformEventMessage, testGoodEncodeResponse)
2150{
2151 std::array<uint8_t,
2152 hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2153 PLDM_PLATFORM_EVENT_MESSAGE_STATE_SENSOR_STATE_REQ_BYTES - 1>
2154 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302155 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302156 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2157 uint8_t completionCode = 0;
2158 uint8_t instanceId = 0x01;
2159 uint8_t platformEventStatus = 0x01;
2160
2161 auto rc = encode_platform_event_message_resp(instanceId, PLDM_SUCCESS,
2162 platformEventStatus, response);
2163
2164 EXPECT_EQ(rc, PLDM_SUCCESS);
2165 EXPECT_EQ(completionCode, response->payload[0]);
2166 EXPECT_EQ(platformEventStatus, response->payload[1]);
2167}
2168
2169TEST(PlatformEventMessage, testBadEncodeResponse)
2170{
2171 auto rc = encode_platform_event_message_resp(0, PLDM_SUCCESS, 1, NULL);
2172 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2173}
2174
2175TEST(PlatformEventMessage, testGoodEncodeRequest)
2176{
2177 uint8_t formatVersion = 0x01;
2178 uint8_t Tid = 0x03;
2179 uint8_t eventClass = 0x00;
2180 uint8_t eventData = 34;
2181
2182 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2183 sizeof(eventData)>
2184 requestMsg{};
2185
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302186 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302187 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2188 auto rc = encode_platform_event_message_req(
2189 0, formatVersion, Tid, eventClass,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302190 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302191 reinterpret_cast<uint8_t*>(&eventData), sizeof(eventData), request,
2192 sizeof(eventData) + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
2193
2194 struct pldm_platform_event_message_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302195 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302196 reinterpret_cast<struct pldm_platform_event_message_req*>(
2197 request->payload);
2198
2199 EXPECT_EQ(rc, PLDM_SUCCESS);
2200 EXPECT_EQ(formatVersion, req->format_version);
2201 EXPECT_EQ(Tid, req->tid);
2202 EXPECT_EQ(eventClass, req->event_class);
2203 EXPECT_EQ(0, memcmp(&eventData, req->event_data, sizeof(eventData)));
2204}
2205
2206TEST(PlatformEventMessage, testBadEncodeRequest)
2207{
2208 uint8_t Tid = 0x03;
2209 uint8_t eventClass = 0x00;
2210 uint8_t eventData = 34;
2211 size_t sz_eventData = sizeof(eventData);
2212 size_t payloadLen =
2213 sz_eventData + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES;
2214 uint8_t formatVersion = 0x01;
2215
2216 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
2217 sizeof(eventData)>
2218 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302219 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302220 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2221
2222 auto rc = encode_platform_event_message_req(
2223 0, formatVersion, Tid, eventClass,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302224 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302225 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, nullptr,
2226 payloadLen);
2227 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2228 rc = encode_platform_event_message_req(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302229 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302230 0, 0, Tid, eventClass, reinterpret_cast<uint8_t*>(&eventData),
2231 sz_eventData, request, payloadLen);
2232 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2233 rc = encode_platform_event_message_req(0, formatVersion, Tid, eventClass,
2234 nullptr, 0, request, payloadLen);
2235 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2236 rc = encode_platform_event_message_req(
2237 0, formatVersion, Tid, eventClass,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302238 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302239 reinterpret_cast<uint8_t*>(&eventData), sz_eventData, request, 0);
2240 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2241}
2242
2243TEST(PlatformEventMessage, testGoodDecodeResponse)
2244{
2245 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
2246 responseMsg{};
2247
2248 uint8_t completionCode = PLDM_SUCCESS;
2249 uint8_t platformEventStatus = 0x01;
2250
2251 uint8_t retcompletionCode;
2252 uint8_t retplatformEventStatus;
2253
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302254 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302255 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2256 struct pldm_platform_event_message_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302257 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302258 reinterpret_cast<struct pldm_platform_event_message_resp*>(
2259 response->payload);
2260
2261 resp->completion_code = completionCode;
2262 resp->platform_event_status = platformEventStatus;
2263
2264 auto rc = decode_platform_event_message_resp(
2265 response, responseMsg.size() - hdrSize, &retcompletionCode,
2266 &retplatformEventStatus);
2267
2268 EXPECT_EQ(rc, PLDM_SUCCESS);
2269 EXPECT_EQ(completionCode, retcompletionCode);
2270 EXPECT_EQ(platformEventStatus, retplatformEventStatus);
2271}
2272
2273TEST(PlatformEventMessage, testBadDecodeResponse)
2274{
2275 std::array<uint8_t, hdrSize + PLDM_PLATFORM_EVENT_MESSAGE_RESP_BYTES>
2276 responseMsg{};
2277
2278 uint8_t completionCode = PLDM_SUCCESS;
2279 uint8_t platformEventStatus = 0x01;
2280
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302281 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302282 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2283 struct pldm_platform_event_message_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302284 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302285 reinterpret_cast<struct pldm_platform_event_message_resp*>(
2286 response->payload);
2287 resp->completion_code = completionCode;
2288 resp->platform_event_status = platformEventStatus;
2289
2290 auto rc = decode_platform_event_message_resp(
2291 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr);
2292
2293 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2294
2295 rc = decode_platform_event_message_resp(
2296 response, responseMsg.size() - hdrSize - 1, &completionCode,
2297 &platformEventStatus);
2298
2299 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2300}
2301
2302TEST(PlatformEventMessage, testGoodSensorEventDataDecodeRequest)
2303{
2304 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH +
2305 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
2306 eventDataArr{};
2307 uint16_t sensorId = 0x1234;
2308 uint8_t sensorEventClassType = PLDM_SENSOR_OP_STATE;
2309
2310 struct pldm_sensor_event_data* eventData =
2311 (struct pldm_sensor_event_data*)eventDataArr.data();
2312 eventData->sensor_id = sensorId;
2313 eventData->sensor_event_class_type = sensorEventClassType;
2314
2315 size_t retSensorOpDataOffset;
2316 uint16_t retSensorId = 0;
2317 uint8_t retSensorEventClassType;
2318 size_t sensorOpDataOffset = sizeof(sensorId) + sizeof(sensorEventClassType);
2319 auto rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302320 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302321 reinterpret_cast<uint8_t*>(eventData), eventDataArr.size(),
2322 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2323 EXPECT_EQ(rc, PLDM_SUCCESS);
2324 EXPECT_EQ(retSensorId, sensorId);
2325 EXPECT_EQ(retSensorEventClassType, sensorEventClassType);
2326 EXPECT_EQ(retSensorOpDataOffset, sensorOpDataOffset);
2327}
2328
2329TEST(PlatformEventMessage, testBadSensorEventDataDecodeRequest)
2330{
2331
2332 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH +
2333 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES>
2334 eventDataArr{};
2335
2336 struct pldm_sensor_event_data* eventData =
2337 (struct pldm_sensor_event_data*)eventDataArr.data();
2338
2339 size_t retSensorOpDataOffset;
2340 uint16_t retSensorId = 0;
2341 uint8_t retSensorEventClassType;
2342 auto rc = decode_sensor_event_data(NULL, eventDataArr.size(), &retSensorId,
2343 &retSensorEventClassType,
2344 &retSensorOpDataOffset);
2345 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2346
2347 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302348 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302349 reinterpret_cast<uint8_t*>(eventDataArr.data()),
2350 eventDataArr.size() -
2351 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH,
2352 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2353 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2354
2355 eventData->sensor_event_class_type = PLDM_SENSOR_OP_STATE;
2356
2357 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302358 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302359 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
2360 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2361 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2362
2363 eventData->sensor_event_class_type = PLDM_STATE_SENSOR_STATE;
2364 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302365 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302366 reinterpret_cast<uint8_t*>(eventDataArr.data()), eventDataArr.size(),
2367 &retSensorId, &retSensorEventClassType, &retSensorOpDataOffset);
2368 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2369
2370 eventData->sensor_event_class_type = PLDM_NUMERIC_SENSOR_STATE;
2371 rc = decode_sensor_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302372 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302373 reinterpret_cast<uint8_t*>(eventDataArr.data()),
2374 eventDataArr.size() + 1, &retSensorId, &retSensorEventClassType,
2375 &retSensorOpDataOffset);
2376 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2377}
2378
Dung Cao7c250342022-11-16 22:40:37 +07002379TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataDecodeRequest)
2380{
2381 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2382 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2383 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2384 eventData{
2385 0x1, // version
2386 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302387 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002388 };
2389
2390 uint8_t formatVersion = 0x01;
2391 uint16_t eventID = 0x7788;
2392 uint32_t dataTransferHandle = 0x11223344;
2393
Thu Nguyen7739d122024-07-26 11:36:39 +00002394 struct pldm_message_poll_event poll_event = {};
Dung Cao7c250342022-11-16 22:40:37 +07002395
2396 auto rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302397 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002398 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002399 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002400
2401 EXPECT_EQ(rc, PLDM_SUCCESS);
Thu Nguyen7739d122024-07-26 11:36:39 +00002402 EXPECT_EQ(poll_event.format_version, formatVersion);
2403 EXPECT_EQ(poll_event.event_id, eventID);
2404 EXPECT_EQ(poll_event.data_transfer_handle, dataTransferHandle);
Dung Cao7c250342022-11-16 22:40:37 +07002405}
2406
2407TEST(PlatformEventMessage, testBadPldmMsgPollEventDataDecodeRequest)
2408{
2409
2410 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2411 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2412 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2413 eventData{
2414 0x1, // version
2415 0x88, 0x77, // Event Id
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05302416 0x44, 0x33, 0x22, 0x11 // Transfer Handle
Dung Cao7c250342022-11-16 22:40:37 +07002417 };
2418
Thu Nguyen7739d122024-07-26 11:36:39 +00002419 struct pldm_message_poll_event poll_event = {};
Dung Cao7c250342022-11-16 22:40:37 +07002420
Thu Nguyen7739d122024-07-26 11:36:39 +00002421 auto rc = decode_pldm_message_poll_event_data(NULL, eventData.size(),
2422 &poll_event);
2423 EXPECT_EQ(rc, -EINVAL);
2424
2425 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302426 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002427 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(), NULL);
2428 EXPECT_EQ(rc, -EINVAL);
Dung Cao7c250342022-11-16 22:40:37 +07002429
2430 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302431 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002432 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
Thu Nguyen7739d122024-07-26 11:36:39 +00002433 &poll_event);
2434 EXPECT_EQ(rc, -EOVERFLOW);
Dung Cao7c250342022-11-16 22:40:37 +07002435
2436 // Event id is 0x0000
2437 eventData[1] = 0x00;
2438 eventData[2] = 0x00;
2439 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302440 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002441 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002442 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002443
Thu Nguyen7739d122024-07-26 11:36:39 +00002444 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002445
2446 // Event id is 0xffff
2447 eventData[1] = 0xff;
2448 eventData[2] = 0xff;
2449 rc = decode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302450 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Dung Cao7c250342022-11-16 22:40:37 +07002451 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
Thu Nguyen7739d122024-07-26 11:36:39 +00002452 &poll_event);
Dung Cao7c250342022-11-16 22:40:37 +07002453
Thu Nguyen7739d122024-07-26 11:36:39 +00002454 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002455}
2456
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302457#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002458TEST(PlatformEventMessage, testGoodPldmMsgPollEventDataEncode)
2459{
2460 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2461 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2462 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2463 eventData{};
2464
Thu Nguyen7739d122024-07-26 11:36:39 +00002465 struct pldm_message_poll_event poll_event = {};
2466 poll_event.format_version = 0x01;
2467 poll_event.event_id = 0x7788;
2468 poll_event.data_transfer_handle = 0x11223344;
Dung Cao7c250342022-11-16 22:40:37 +07002469
2470 int rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302471 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002472 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2473 eventData.size());
Dung Cao7c250342022-11-16 22:40:37 +07002474
2475 EXPECT_EQ(rc, PLDM_SUCCESS);
2476
2477 struct pldm_msgbuf _buf;
2478 struct pldm_msgbuf* buf = &_buf;
2479
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302480 rc = pldm_msgbuf_init_cc(
2481 buf, PLDM_MSG_POLL_EVENT_LENGTH,
2482 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2483 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size());
Dung Cao7c250342022-11-16 22:40:37 +07002484 EXPECT_EQ(rc, PLDM_SUCCESS);
2485
2486 uint8_t retFormatVersion;
2487 uint16_t reteventID;
2488 uint32_t retDataTransferHandle;
2489
2490 EXPECT_EQ(pldm_msgbuf_extract_uint8(buf, &retFormatVersion), PLDM_SUCCESS);
2491 EXPECT_EQ(pldm_msgbuf_extract_uint16(buf, &reteventID), PLDM_SUCCESS);
2492 EXPECT_EQ(pldm_msgbuf_extract_uint32(buf, &retDataTransferHandle),
2493 PLDM_SUCCESS);
Thu Nguyen7739d122024-07-26 11:36:39 +00002494 EXPECT_EQ(retFormatVersion, poll_event.format_version);
2495 EXPECT_EQ(reteventID, poll_event.event_id);
2496 EXPECT_EQ(retDataTransferHandle, poll_event.data_transfer_handle);
Dung Cao7c250342022-11-16 22:40:37 +07002497 EXPECT_EQ(pldm_msgbuf_destroy_consumed(buf), PLDM_SUCCESS);
2498}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302499#endif
Dung Cao7c250342022-11-16 22:40:37 +07002500
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302501#ifdef LIBPLDM_API_TESTING
Dung Cao7c250342022-11-16 22:40:37 +07002502TEST(PlatformEventMessage, testBadPldmMsgPollEventDataEncode)
2503{
2504 std::array<uint8_t, PLDM_PLATFORM_EVENT_MESSAGE_FORMAT_VERSION +
2505 PLDM_PLATFORM_EVENT_MESSAGE_EVENT_ID +
2506 PLDM_PLATFORM_EVENT_MESSAGE_TRANFER_HANDLE>
2507 eventData{};
2508
Thu Nguyen7739d122024-07-26 11:36:39 +00002509 struct pldm_message_poll_event poll_event = {};
2510 poll_event.format_version = 0x01;
2511 poll_event.event_id = 0x7788;
2512 poll_event.data_transfer_handle = 0x11223344;
Dung Cao7c250342022-11-16 22:40:37 +07002513
Thu Nguyen7739d122024-07-26 11:36:39 +00002514 int rc = encode_pldm_message_poll_event_data(&poll_event, NULL,
2515 eventData.size());
2516 EXPECT_EQ(rc, -EINVAL);
Dung Cao7c250342022-11-16 22:40:37 +07002517
Thu Nguyen7739d122024-07-26 11:36:39 +00002518 poll_event.event_id = 0x0000;
Dung Cao7c250342022-11-16 22:40:37 +07002519 rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302520 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002521 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2522 eventData.size());
2523 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002524
Thu Nguyen7739d122024-07-26 11:36:39 +00002525 poll_event.event_id = 0xffff;
Dung Cao7c250342022-11-16 22:40:37 +07002526 rc = encode_pldm_message_poll_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302527 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyen7739d122024-07-26 11:36:39 +00002528 &poll_event, reinterpret_cast<uint8_t*>(eventData.data()),
2529 eventData.size());
2530 EXPECT_EQ(rc, -EPROTO);
Dung Cao7c250342022-11-16 22:40:37 +07002531}
Andrew Jeffery9d2a1c62023-06-05 13:02:16 +09302532#endif
Dung Cao7c250342022-11-16 22:40:37 +07002533
Andrew Jeffery9c766792022-08-10 23:12:49 +09302534TEST(PlatformEventMessage, testGoodSensorOpEventDataDecodeRequest)
2535{
2536 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2537 eventDataArr{};
2538
2539 struct pldm_sensor_event_sensor_op_state* sensorData =
2540 (struct pldm_sensor_event_sensor_op_state*)eventDataArr.data();
2541 uint8_t presentState = PLDM_SENSOR_ENABLED;
2542 uint8_t previousState = PLDM_SENSOR_INITIALIZING;
2543 sensorData->present_op_state = presentState;
2544 sensorData->previous_op_state = previousState;
2545
2546 uint8_t retPresentState;
2547 uint8_t retPreviousState;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302548 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302549 auto rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData),
2550 eventDataArr.size(), &retPresentState,
2551 &retPreviousState);
2552 EXPECT_EQ(rc, PLDM_SUCCESS);
2553 EXPECT_EQ(retPresentState, presentState);
2554 EXPECT_EQ(retPreviousState, previousState);
2555}
2556
2557TEST(PlatformEventMessage, testBadSensorOpEventDataDecodeRequest)
2558{
2559 uint8_t presentOpState;
2560 uint8_t previousOpState;
2561 size_t sensorDataLength = PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH;
2562 auto rc = decode_sensor_op_data(NULL, sensorDataLength, &presentOpState,
2563 &previousOpState);
2564 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2565
2566 std::array<uint8_t, PLDM_SENSOR_EVENT_SENSOR_OP_STATE_DATA_LENGTH>
2567 sensorData{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302568 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302569 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2570 sensorDataLength + 1, &presentOpState,
2571 &previousOpState);
2572 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2573
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302574 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302575 rc = decode_sensor_op_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2576 sensorDataLength, nullptr, &previousOpState);
2577 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2578}
2579
2580TEST(PlatformEventMessage, testGoodSensorStateEventDataDecodeRequest)
2581{
2582 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2583 eventDataArr{};
2584
2585 struct pldm_sensor_event_state_sensor_state* sensorData =
2586 (struct pldm_sensor_event_state_sensor_state*)eventDataArr.data();
2587 uint8_t sensorOffset = 0x02;
2588 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2589 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2590 sensorData->sensor_offset = sensorOffset;
2591 sensorData->event_state = eventState;
2592 sensorData->previous_event_state = previousEventState;
2593
2594 uint8_t retSensorOffset;
2595 uint8_t retEventState;
2596 uint8_t retPreviousState;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302597 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302598 auto rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2599 eventDataArr.size(), &retSensorOffset,
2600 &retEventState, &retPreviousState);
2601 EXPECT_EQ(rc, PLDM_SUCCESS);
2602 EXPECT_EQ(retSensorOffset, sensorOffset);
2603 EXPECT_EQ(retEventState, eventState);
2604 EXPECT_EQ(retPreviousState, previousEventState);
2605}
2606
2607TEST(PlatformEventMessage, testBadStateSensorEventDataDecodeRequest)
2608{
2609 uint8_t sensorOffset;
2610 uint8_t eventState;
2611 uint8_t previousEventState;
2612 size_t sensorDataLength = PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH;
2613 auto rc = decode_state_sensor_data(NULL, sensorDataLength, &sensorOffset,
2614 &eventState, &previousEventState);
2615 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2616
2617 std::array<uint8_t, PLDM_SENSOR_EVENT_STATE_SENSOR_STATE_DATA_LENGTH>
2618 sensorData{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302619 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302620 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2621 sensorDataLength - 1, &sensorOffset,
2622 &eventState, &previousEventState);
2623 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2624
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302625 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302626 rc = decode_state_sensor_data(reinterpret_cast<uint8_t*>(sensorData.data()),
2627 sensorDataLength, &sensorOffset, nullptr,
2628 &previousEventState);
2629 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2630}
2631
2632TEST(PlatformEventMessage, testGoodNumericSensorEventDataDecodeRequest)
2633{
2634 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2635 eventDataArr{};
2636 struct pldm_sensor_event_numeric_sensor_state* sensorData =
2637 (struct pldm_sensor_event_numeric_sensor_state*)eventDataArr.data();
2638
2639 size_t sensorDataLength =
2640 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_32BIT_DATA_LENGTH;
2641 uint8_t eventState = PLDM_SENSOR_SHUTTINGDOWN;
2642 uint8_t previousEventState = PLDM_SENSOR_INTEST;
2643 uint8_t sensorDataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
2644 uint32_t presentReading = 305441741;
2645 sensorData->event_state = eventState;
2646 sensorData->previous_event_state = previousEventState;
2647 sensorData->sensor_data_size = sensorDataSize;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302648 {
2649 uint32_t presentReadingLE = htole32(presentReading);
2650 memcpy(&sensorData->present_reading, &presentReadingLE,
2651 sizeof(presentReadingLE));
2652 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302653
2654 uint8_t retEventState;
2655 uint8_t retPreviousEventState;
2656 uint8_t retSensorDataSize;
2657 uint32_t retPresentReading;
2658
2659 auto rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302660 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302661 reinterpret_cast<uint8_t*>(sensorData), sensorDataLength,
2662 &retEventState, &retPreviousEventState, &retSensorDataSize,
2663 &retPresentReading);
2664 EXPECT_EQ(rc, PLDM_SUCCESS);
2665 EXPECT_EQ(retEventState, eventState);
2666 EXPECT_EQ(retPreviousEventState, previousEventState);
2667 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2668 EXPECT_EQ(retPresentReading, presentReading);
2669
2670 int16_t presentReadingNew = -31432;
Andrew Jeffery92f6c3c2023-04-13 15:50:10 +09302671 {
2672 int16_t presentReadingNewLE = htole16(presentReadingNew);
2673 memcpy(&sensorData->present_reading, &presentReadingNewLE,
2674 sizeof(presentReadingNewLE));
2675 }
Andrew Jeffery9c766792022-08-10 23:12:49 +09302676 sensorDataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
2677 sensorData->sensor_data_size = sensorDataSize;
2678 sensorDataLength = PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_16BIT_DATA_LENGTH;
2679
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302680 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302681 rc = decode_numeric_sensor_data(reinterpret_cast<uint8_t*>(sensorData),
2682 sensorDataLength, &retEventState,
2683 &retPreviousEventState, &retSensorDataSize,
2684 &retPresentReading);
2685 EXPECT_EQ(rc, PLDM_SUCCESS);
2686 EXPECT_EQ(retEventState, eventState);
2687 EXPECT_EQ(retPreviousEventState, previousEventState);
2688 EXPECT_EQ(retSensorDataSize, sensorDataSize);
2689 EXPECT_EQ(static_cast<int16_t>(retPresentReading), presentReadingNew);
2690}
2691
2692TEST(PlatformEventMessage, testBadNumericSensorEventDataDecodeRequest)
2693{
2694 uint8_t eventState;
2695 uint8_t previousEventState;
2696 uint8_t sensorDataSize;
2697 uint32_t presentReading;
2698 size_t sensorDataLength =
2699 PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH;
2700 auto rc = decode_numeric_sensor_data(NULL, sensorDataLength, &eventState,
2701 &previousEventState, &sensorDataSize,
2702 &presentReading);
2703 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2704
2705 std::array<uint8_t, PLDM_SENSOR_EVENT_NUMERIC_SENSOR_STATE_MAX_DATA_LENGTH>
2706 sensorData{};
2707 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302708 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302709 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength - 1,
2710 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2711 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2712
2713 struct pldm_sensor_event_numeric_sensor_state* numericSensorData =
2714 (struct pldm_sensor_event_numeric_sensor_state*)sensorData.data();
2715 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT8;
2716 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302717 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302718 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2719 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2720 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2721
2722 numericSensorData->sensor_data_size = PLDM_SENSOR_DATA_SIZE_UINT16;
2723 rc = decode_numeric_sensor_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302724 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302725 reinterpret_cast<uint8_t*>(sensorData.data()), sensorDataLength,
2726 &eventState, &previousEventState, &sensorDataSize, &presentReading);
2727 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302728}
2729
2730TEST(GetNumericEffecterValue, testGoodEncodeRequest)
2731{
2732 std::vector<uint8_t> requestMsg(hdrSize +
2733 PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES);
2734
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002735 uint16_t effecter_id = 0xab01;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302736
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302737 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302738 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2739
2740 auto rc = encode_get_numeric_effecter_value_req(0, effecter_id, request);
2741
2742 struct pldm_get_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302743 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302744 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2745 request->payload);
2746
2747 EXPECT_EQ(rc, PLDM_SUCCESS);
2748 EXPECT_EQ(effecter_id, le16toh(req->effecter_id));
2749}
2750
2751TEST(GetNumericEffecterValue, testBadEncodeRequest)
2752{
2753 std::vector<uint8_t> requestMsg(
2754 hdrSize + PLDM_SET_NUMERIC_EFFECTER_VALUE_MIN_REQ_BYTES);
2755
2756 auto rc = encode_get_numeric_effecter_value_req(0, 0, nullptr);
2757 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2758}
2759
2760TEST(GetNumericEffecterValue, testGoodDecodeRequest)
2761{
2762 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2763 requestMsg{};
2764
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302765 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302766 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2767 struct pldm_get_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302768 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302769 reinterpret_cast<struct pldm_get_numeric_effecter_value_req*>(
2770 request->payload);
2771
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002772 uint16_t effecter_id = 0x12ab;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302773 req->effecter_id = htole16(effecter_id);
2774
2775 uint16_t reteffecter_id;
2776
2777 auto rc = decode_get_numeric_effecter_value_req(
2778 request, requestMsg.size() - hdrSize, &reteffecter_id);
2779
2780 EXPECT_EQ(rc, PLDM_SUCCESS);
2781 EXPECT_EQ(effecter_id, reteffecter_id);
2782}
2783
2784TEST(GetNumericEffecterValue, testBadDecodeRequest)
2785{
2786 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
2787 requestMsg{};
2788
2789 auto rc = decode_get_numeric_effecter_value_req(
2790 nullptr, requestMsg.size() - hdrSize, nullptr);
2791
2792 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2793
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302794 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302795 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2796 struct pldm_set_numeric_effecter_value_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302797 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302798 reinterpret_cast<struct pldm_set_numeric_effecter_value_req*>(
2799 request->payload);
2800
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002801 uint16_t effecter_id = 0x1a;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302802 req->effecter_id = htole16(effecter_id);
2803 uint16_t reteffecter_id;
2804
2805 rc = decode_get_numeric_effecter_value_req(
2806 request, requestMsg.size() - hdrSize - 1, &reteffecter_id);
2807
2808 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2809}
2810
2811TEST(GetNumericEffecterValue, testGoodEncodeResponse)
2812{
2813 uint8_t completionCode = 0;
2814 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
2815 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
2816 uint32_t pendingValue = 0x12345678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002817 uint32_t presentValue = 0xabcdef11;
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302818 uint32_t val_pending;
2819 uint32_t val_present;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302820
2821 std::array<uint8_t,
2822 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
2823 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302824 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302825 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2826
2827 auto rc = encode_get_numeric_effecter_value_resp(
2828 0, completionCode, effecter_dataSize, effecter_operState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302829 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302830 reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302831 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302832 reinterpret_cast<uint8_t*>(&presentValue), response,
2833 responseMsg.size() - hdrSize);
2834
2835 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302836 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302837 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2838 response->payload);
2839
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302840 memcpy(&val_pending, &resp->pending_and_present_values[0],
2841 sizeof(val_pending));
2842 val_pending = le32toh(val_pending);
2843 memcpy(&val_present, &resp->pending_and_present_values[4],
2844 sizeof(val_present));
2845 val_present = le32toh(val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302846
2847 EXPECT_EQ(rc, PLDM_SUCCESS);
2848 EXPECT_EQ(effecter_dataSize, resp->effecter_data_size);
2849 EXPECT_EQ(effecter_operState, resp->effecter_oper_state);
Andrew Jefferyf990dcf2023-04-12 20:54:48 +09302850 EXPECT_EQ(pendingValue, val_pending);
2851 EXPECT_EQ(presentValue, val_present);
Andrew Jeffery9c766792022-08-10 23:12:49 +09302852}
2853
2854TEST(GetNumericEffecterValue, testBadEncodeResponse)
2855{
2856 std::array<uint8_t,
2857 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
2858 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302859 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302860 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2861
2862 uint8_t pendingValue = 0x01;
2863 uint8_t presentValue = 0x02;
2864
2865 auto rc = encode_get_numeric_effecter_value_resp(
2866 0, PLDM_SUCCESS, 0, 0, nullptr, nullptr, nullptr,
2867 responseMsg.size() - hdrSize);
2868 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2869
2870 rc = encode_get_numeric_effecter_value_resp(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302871 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302872 0, PLDM_SUCCESS, 6, 9, reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302873 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302874 reinterpret_cast<uint8_t*>(&presentValue), response,
2875 responseMsg.size() - hdrSize);
2876 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2877
2878 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
2879 uint8_t effecter_operState = EFFECTER_OPER_STATE_FAILED;
2880
2881 rc = encode_get_numeric_effecter_value_resp(
2882 0, PLDM_SUCCESS, effecter_dataSize, effecter_operState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302883 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302884 reinterpret_cast<uint8_t*>(&pendingValue),
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302885 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302886 reinterpret_cast<uint8_t*>(&presentValue), response,
2887 responseMsg.size() - hdrSize);
2888 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2889}
2890
2891TEST(GetNumericEffecterValue, testGoodDecodeResponse)
2892{
2893 std::array<uint8_t,
2894 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 2>
2895 responseMsg{};
2896
2897 uint8_t completionCode = 0;
2898 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT16;
2899 uint8_t effecter_operState = EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING;
2900 uint16_t pendingValue = 0x4321;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002901 uint16_t presentValue = 0xdcba;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302902
2903 uint8_t retcompletionCode;
2904 uint8_t reteffecter_dataSize;
2905 uint8_t reteffecter_operState;
2906 uint8_t retpendingValue[2];
2907 uint8_t retpresentValue[2];
2908
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302909 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302910 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2911 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302912 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302913 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2914 response->payload);
2915
2916 resp->completion_code = completionCode;
2917 resp->effecter_data_size = effecter_dataSize;
2918 resp->effecter_oper_state = effecter_operState;
2919
2920 uint16_t pendingValue_le = htole16(pendingValue);
2921 memcpy(resp->pending_and_present_values, &pendingValue_le,
2922 sizeof(pendingValue_le));
2923 uint16_t presentValue_le = htole16(presentValue);
2924 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
2925 sizeof(presentValue_le));
2926
2927 auto rc = decode_get_numeric_effecter_value_resp(
2928 response, responseMsg.size() - hdrSize, &retcompletionCode,
2929 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
2930 retpresentValue);
2931
2932 EXPECT_EQ(rc, PLDM_SUCCESS);
2933 EXPECT_EQ(completionCode, retcompletionCode);
2934 EXPECT_EQ(effecter_dataSize, reteffecter_dataSize);
2935 EXPECT_EQ(effecter_operState, reteffecter_operState);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302936 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302937 EXPECT_EQ(pendingValue, *(reinterpret_cast<uint16_t*>(retpendingValue)));
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302938 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302939 EXPECT_EQ(presentValue, *(reinterpret_cast<uint16_t*>(retpresentValue)));
2940}
2941
2942TEST(GetNumericEffecterValue, testBadDecodeResponse)
2943{
2944 std::array<uint8_t,
2945 hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_MIN_RESP_BYTES + 6>
2946 responseMsg{};
2947
2948 auto rc = decode_get_numeric_effecter_value_resp(
2949 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
2950 nullptr, nullptr);
2951
2952 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2953
2954 uint8_t completionCode = 0;
2955 uint8_t effecter_dataSize = PLDM_EFFECTER_DATA_SIZE_SINT16;
2956 uint8_t effecter_operState = EFFECTER_OPER_STATE_DISABLED;
2957 uint16_t pendingValue = 0x5678;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06002958 uint16_t presentValue = 0xcdef;
Andrew Jeffery9c766792022-08-10 23:12:49 +09302959
2960 uint8_t retcompletionCode;
2961 uint8_t reteffecter_dataSize;
2962 uint8_t reteffecter_operState;
2963 uint8_t retpendingValue[2];
2964 uint8_t retpresentValue[2];
2965
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302966 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302967 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
2968 struct pldm_get_numeric_effecter_value_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09302969 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09302970 reinterpret_cast<struct pldm_get_numeric_effecter_value_resp*>(
2971 response->payload);
2972
2973 resp->completion_code = completionCode;
2974 resp->effecter_data_size = effecter_dataSize;
2975 resp->effecter_oper_state = effecter_operState;
2976
2977 uint16_t pendingValue_le = htole16(pendingValue);
2978 memcpy(resp->pending_and_present_values, &pendingValue_le,
2979 sizeof(pendingValue_le));
2980 uint16_t presentValue_le = htole16(presentValue);
2981 memcpy(&resp->pending_and_present_values[2], &presentValue_le,
2982 sizeof(presentValue_le));
2983
2984 rc = decode_get_numeric_effecter_value_resp(
2985 response, responseMsg.size() - hdrSize, &retcompletionCode,
2986 &reteffecter_dataSize, &reteffecter_operState, retpendingValue,
2987 retpresentValue);
2988
2989 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2990}
2991
2992TEST(PldmPDRRepositoryChgEventEvent, testGoodDecodeRequest)
2993{
2994 const uint8_t eventDataFormat = FORMAT_IS_PDR_HANDLES;
2995 const uint8_t numberOfChangeRecords = 2;
2996 uint8_t eventDataOperation1 = PLDM_RECORDS_DELETED;
2997 const uint8_t numberOfChangeEntries1 = 2;
2998 std::array<uint32_t, numberOfChangeEntries1> changeRecordArr1{
2999 {0x00000000, 0x12345678}};
3000 uint8_t eventDataOperation2 = PLDM_RECORDS_ADDED;
3001 const uint8_t numberOfChangeEntries2 = 5;
3002 std::array<uint32_t, numberOfChangeEntries2> changeRecordArr2{
3003 {0x01234567, 0x11223344, 0x45678901, 0x21222324, 0x98765432}};
3004 std::array<uint8_t, PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
3005 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH *
3006 numberOfChangeRecords +
3007 (numberOfChangeEntries1 + numberOfChangeEntries2) *
3008 sizeof(uint32_t)>
3009 eventDataArr{};
3010
3011 struct pldm_pdr_repository_chg_event_data* eventData =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303012 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303013 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
3014 eventDataArr.data());
3015 eventData->event_data_format = eventDataFormat;
3016 eventData->number_of_change_records = numberOfChangeRecords;
3017 struct pldm_pdr_repository_change_record_data* changeRecord1 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303018 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303019 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
3020 eventData->change_records);
3021 changeRecord1->event_data_operation = eventDataOperation1;
3022 changeRecord1->number_of_change_entries = numberOfChangeEntries1;
3023 memcpy(changeRecord1->change_entry, &changeRecordArr1[0],
3024 changeRecordArr1.size() * sizeof(uint32_t));
3025 struct pldm_pdr_repository_change_record_data* changeRecord2 =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303026 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303027 reinterpret_cast<struct pldm_pdr_repository_change_record_data*>(
3028 eventData->change_records +
3029 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
3030 (changeRecordArr1.size() * sizeof(uint32_t)));
3031 changeRecord2->event_data_operation = eventDataOperation2;
3032 changeRecord2->number_of_change_entries = numberOfChangeEntries2;
3033 memcpy(changeRecord2->change_entry, &changeRecordArr2[0],
3034 changeRecordArr2.size() * sizeof(uint32_t));
3035
3036 uint8_t retEventDataFormat{};
3037 uint8_t retNumberOfChangeRecords{};
3038 size_t retChangeRecordDataOffset{0};
3039 auto rc = decode_pldm_pdr_repository_chg_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303040 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303041 reinterpret_cast<const uint8_t*>(eventData), eventDataArr.size(),
3042 &retEventDataFormat, &retNumberOfChangeRecords,
3043 &retChangeRecordDataOffset);
3044 EXPECT_EQ(rc, PLDM_SUCCESS);
3045 EXPECT_EQ(retEventDataFormat, FORMAT_IS_PDR_HANDLES);
3046 EXPECT_EQ(retNumberOfChangeRecords, numberOfChangeRecords);
3047
3048 const uint8_t* changeRecordData =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303049 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303050 reinterpret_cast<const uint8_t*>(changeRecord1);
3051 size_t changeRecordDataSize =
3052 eventDataArr.size() - PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH;
3053 uint8_t retEventDataOperation;
3054 uint8_t retNumberOfChangeEntries;
3055 size_t retChangeEntryDataOffset;
3056
3057 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303058 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303059 reinterpret_cast<const uint8_t*>(changeRecordData),
3060 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
3061 &retChangeEntryDataOffset);
3062 EXPECT_EQ(rc, PLDM_SUCCESS);
3063 EXPECT_EQ(retEventDataOperation, eventDataOperation1);
3064 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries1);
3065 changeRecordData += retChangeEntryDataOffset;
3066 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr1[0],
3067 sizeof(uint32_t) * retNumberOfChangeEntries));
3068
3069 changeRecordData += sizeof(uint32_t) * retNumberOfChangeEntries;
3070 changeRecordDataSize -= sizeof(uint32_t) * retNumberOfChangeEntries -
3071 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH;
3072 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303073 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303074 reinterpret_cast<const uint8_t*>(changeRecordData),
3075 changeRecordDataSize, &retEventDataOperation, &retNumberOfChangeEntries,
3076 &retChangeEntryDataOffset);
3077 EXPECT_EQ(rc, PLDM_SUCCESS);
3078 EXPECT_EQ(retEventDataOperation, eventDataOperation2);
3079 EXPECT_EQ(retNumberOfChangeEntries, numberOfChangeEntries2);
3080 changeRecordData += retChangeEntryDataOffset;
3081 EXPECT_EQ(0, memcmp(changeRecordData, &changeRecordArr2[0],
3082 sizeof(uint32_t) * retNumberOfChangeEntries));
3083}
3084
3085TEST(PldmPDRRepositoryChgEventEvent, testBadDecodeRequest)
3086{
3087 uint8_t eventDataFormat{};
3088 uint8_t numberOfChangeRecords{};
3089 size_t changeRecordDataOffset{};
3090 auto rc = decode_pldm_pdr_repository_chg_event_data(
3091 NULL, 0, &eventDataFormat, &numberOfChangeRecords,
3092 &changeRecordDataOffset);
3093 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3094
3095 std::array<uint8_t, 2> eventData{};
3096 rc = decode_pldm_pdr_repository_chg_event_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303097 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303098 reinterpret_cast<const uint8_t*>(eventData.data()), 0, &eventDataFormat,
3099 &numberOfChangeRecords, &changeRecordDataOffset);
3100 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3101
3102 uint8_t eventDataOperation{};
3103 uint8_t numberOfChangeEntries{};
3104 size_t changeEntryDataOffset{};
3105 rc = decode_pldm_pdr_repository_change_record_data(
3106 NULL, 0, &eventDataOperation, &numberOfChangeEntries,
3107 &changeEntryDataOffset);
3108 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3109
3110 std::array<uint8_t, 2> changeRecord{};
3111 rc = decode_pldm_pdr_repository_change_record_data(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303112 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303113 reinterpret_cast<const uint8_t*>(changeRecord.data()), 0,
3114 &eventDataOperation, &numberOfChangeEntries, &changeEntryDataOffset);
3115 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3116}
3117
3118TEST(GetSensorReading, testGoodEncodeRequest)
3119{
3120 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3121 requestMsg{};
3122
3123 uint16_t sensorId = 0x1234;
3124 bool8_t rearmEventState = 0x01;
3125
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303126 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303127 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3128 auto rc =
3129 encode_get_sensor_reading_req(0, sensorId, rearmEventState, request);
3130
3131 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303132 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303133 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3134
3135 EXPECT_EQ(rc, PLDM_SUCCESS);
3136 EXPECT_EQ(sensorId, le16toh(req->sensor_id));
3137 EXPECT_EQ(rearmEventState, req->rearm_event_state);
3138}
3139
3140TEST(GetSensorReading, testBadEncodeRequest)
3141{
3142 auto rc = encode_get_sensor_reading_req(0, 0, 0, nullptr);
3143
3144 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3145}
3146
3147TEST(GetSensorReading, testGoodDecodeRequest)
3148{
3149 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3150 requestMsg{};
3151
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003152 uint16_t sensorId = 0xabcd;
3153 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303154
3155 uint16_t retsensorId;
3156 bool8_t retrearmEventState;
3157
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303158 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303159 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3160
3161 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303162 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303163 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3164
3165 req->sensor_id = htole16(sensorId);
3166 req->rearm_event_state = rearmEventState;
3167
3168 auto rc =
3169 decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize,
3170 &retsensorId, &retrearmEventState);
3171
3172 EXPECT_EQ(rc, PLDM_SUCCESS);
3173 EXPECT_EQ(sensorId, retsensorId);
3174 EXPECT_EQ(rearmEventState, retrearmEventState);
3175}
3176
3177TEST(GetSensorReading, testBadDecodeRequest)
3178{
3179 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_REQ_BYTES>
3180 requestMsg{};
3181
3182 auto rc = decode_get_sensor_reading_req(
3183 nullptr, requestMsg.size() - hdrSize, nullptr, nullptr);
3184 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3185
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003186 uint16_t sensorId = 0xabcd;
3187 bool8_t rearmEventState = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303188
3189 uint16_t retsensorId;
3190 bool8_t retrearmEventState;
3191
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303192 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303193 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3194
3195 struct pldm_get_sensor_reading_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303196 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303197 reinterpret_cast<struct pldm_get_sensor_reading_req*>(request->payload);
3198
3199 req->sensor_id = htole16(sensorId);
3200 req->rearm_event_state = rearmEventState;
3201
3202 rc = decode_get_sensor_reading_req(request, requestMsg.size() - hdrSize - 1,
3203 &retsensorId, &retrearmEventState);
3204
3205 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3206}
3207
3208TEST(GetSensorReading, testGoodEncodeResponse)
3209{
3210 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES>
3211 responseMsg{};
3212
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303213 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303214 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3215
3216 uint8_t completionCode = 0;
3217 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3218 uint8_t sensor_operationalState = PLDM_SENSOR_ENABLED;
3219 uint8_t sensor_event_messageEnable = PLDM_NO_EVENT_GENERATION;
3220 uint8_t presentState = PLDM_SENSOR_NORMAL;
3221 uint8_t previousState = PLDM_SENSOR_WARNING;
3222 uint8_t eventState = PLDM_SENSOR_UPPERWARNING;
3223 uint8_t presentReading = 0x21;
3224
3225 auto rc = encode_get_sensor_reading_resp(
3226 0, completionCode, sensor_dataSize, sensor_operationalState,
3227 sensor_event_messageEnable, presentState, previousState, eventState,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303228 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303229 reinterpret_cast<uint8_t*>(&presentReading), response,
3230 responseMsg.size() - hdrSize);
3231
3232 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303233 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303234 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3235 response->payload);
3236
3237 EXPECT_EQ(rc, PLDM_SUCCESS);
3238 EXPECT_EQ(completionCode, resp->completion_code);
3239 EXPECT_EQ(sensor_dataSize, resp->sensor_data_size);
3240 EXPECT_EQ(sensor_operationalState, resp->sensor_operational_state);
3241 EXPECT_EQ(sensor_event_messageEnable, resp->sensor_event_message_enable);
3242 EXPECT_EQ(presentState, resp->present_state);
3243 EXPECT_EQ(previousState, resp->previous_state);
3244 EXPECT_EQ(eventState, resp->event_state);
3245 EXPECT_EQ(presentReading,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303246 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303247 *(reinterpret_cast<uint8_t*>(&resp->present_reading[0])));
3248}
3249
3250TEST(GetSensorReading, testBadEncodeResponse)
3251{
3252 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
3253 responseMsg{};
3254
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303255 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303256 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3257
3258 uint8_t presentReading = 0x1;
3259
3260 auto rc = encode_get_sensor_reading_resp(0, PLDM_SUCCESS, 0, 0, 0, 0, 0, 0,
3261 nullptr, nullptr,
3262 responseMsg.size() - hdrSize);
3263 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3264
3265 rc = encode_get_sensor_reading_resp(
3266 0, PLDM_SUCCESS, 6, 1, 1, 1, 1, 1,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303267 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303268 reinterpret_cast<uint8_t*>(&presentReading), response,
3269 responseMsg.size() - hdrSize);
3270 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3271
3272 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3273
3274 rc = encode_get_sensor_reading_resp(
3275 0, PLDM_SUCCESS, sensor_dataSize, 1, 1, 1, 1, 1,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303276 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303277 reinterpret_cast<uint8_t*>(&presentReading), response,
3278 responseMsg.size() - hdrSize);
3279 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3280}
3281
3282TEST(GetSensorReading, testGoodDecodeResponse)
3283{
3284 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 3>
3285 responseMsg{};
3286
3287 uint8_t completionCode = 0;
3288 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT32;
3289 uint8_t sensor_operationalState = PLDM_SENSOR_STATUSUNKOWN;
3290 uint8_t sensor_event_messageEnable = PLDM_EVENTS_ENABLED;
3291 uint8_t presentState = PLDM_SENSOR_CRITICAL;
3292 uint8_t previousState = PLDM_SENSOR_UPPERCRITICAL;
3293 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003294 uint32_t presentReading = 0xabcdef11;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303295
3296 uint8_t retcompletionCode;
3297 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_UINT32;
3298 uint8_t retsensor_operationalState;
3299 uint8_t retsensor_event_messageEnable;
3300 uint8_t retpresentState;
3301 uint8_t retpreviousState;
3302 uint8_t reteventState;
3303 uint8_t retpresentReading[4];
3304
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303305 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303306 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3307 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303308 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303309 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3310 response->payload);
3311
3312 resp->completion_code = completionCode;
3313 resp->sensor_data_size = sensor_dataSize;
3314 resp->sensor_operational_state = sensor_operationalState;
3315 resp->sensor_event_message_enable = sensor_event_messageEnable;
3316 resp->present_state = presentState;
3317 resp->previous_state = previousState;
3318 resp->event_state = eventState;
3319
3320 uint32_t presentReading_le = htole32(presentReading);
3321 memcpy(resp->present_reading, &presentReading_le,
3322 sizeof(presentReading_le));
3323
3324 auto rc = decode_get_sensor_reading_resp(
3325 response, responseMsg.size() - hdrSize, &retcompletionCode,
3326 &retsensor_dataSize, &retsensor_operationalState,
3327 &retsensor_event_messageEnable, &retpresentState, &retpreviousState,
3328 &reteventState, retpresentReading);
3329
3330 EXPECT_EQ(rc, PLDM_SUCCESS);
3331 EXPECT_EQ(completionCode, retcompletionCode);
3332 EXPECT_EQ(sensor_dataSize, retsensor_dataSize);
3333 EXPECT_EQ(sensor_operationalState, retsensor_operationalState);
3334 EXPECT_EQ(sensor_event_messageEnable, retsensor_event_messageEnable);
3335 EXPECT_EQ(presentState, retpresentState);
3336 EXPECT_EQ(previousState, retpreviousState);
3337 EXPECT_EQ(eventState, reteventState);
3338 EXPECT_EQ(presentReading,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303339 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303340 *(reinterpret_cast<uint32_t*>(retpresentReading)));
3341}
3342
3343TEST(GetSensorReading, testBadDecodeResponse)
3344{
3345 std::array<uint8_t, hdrSize + PLDM_GET_SENSOR_READING_MIN_RESP_BYTES + 1>
3346 responseMsg{};
3347
3348 auto rc = decode_get_sensor_reading_resp(
3349 nullptr, responseMsg.size() - hdrSize, nullptr, nullptr, nullptr,
3350 nullptr, nullptr, nullptr, nullptr, nullptr);
3351
3352 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3353
3354 uint8_t completionCode = 0;
3355 uint8_t sensor_dataSize = PLDM_EFFECTER_DATA_SIZE_UINT8;
3356 uint8_t sensor_operationalState = PLDM_SENSOR_INTEST;
3357 uint8_t sensor_event_messageEnable = PLDM_EVENTS_DISABLED;
3358 uint8_t presentState = PLDM_SENSOR_FATAL;
3359 uint8_t previousState = PLDM_SENSOR_UPPERFATAL;
3360 uint8_t eventState = PLDM_SENSOR_WARNING;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -06003361 uint8_t presentReading = 0xa;
Andrew Jeffery9c766792022-08-10 23:12:49 +09303362
3363 uint8_t retcompletionCode;
3364 uint8_t retsensor_dataSize = PLDM_SENSOR_DATA_SIZE_SINT16;
3365 uint8_t retsensor_operationalState;
3366 uint8_t retsensor_event_messageEnable;
3367 uint8_t retpresent_state;
3368 uint8_t retprevious_state;
3369 uint8_t retevent_state;
3370 uint8_t retpresentReading;
3371
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303372 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303373 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3374 struct pldm_get_sensor_reading_resp* resp =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303375 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303376 reinterpret_cast<struct pldm_get_sensor_reading_resp*>(
3377 response->payload);
3378
3379 resp->completion_code = completionCode;
3380 resp->sensor_data_size = sensor_dataSize;
3381 resp->sensor_operational_state = sensor_operationalState;
3382 resp->sensor_event_message_enable = sensor_event_messageEnable;
3383 resp->present_state = presentState;
3384 resp->previous_state = previousState;
3385 resp->event_state = eventState;
3386 resp->present_reading[0] = presentReading;
3387
3388 rc = decode_get_sensor_reading_resp(
3389 response, responseMsg.size() - hdrSize, &retcompletionCode,
3390 &retsensor_dataSize, &retsensor_operationalState,
3391 &retsensor_event_messageEnable, &retpresent_state, &retprevious_state,
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303392 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303393 &retevent_state, reinterpret_cast<uint8_t*>(&retpresentReading));
3394
3395 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3396}
3397
3398TEST(SetEventReceiver, testGoodEncodeRequest)
3399{
3400 uint8_t eventMessageGlobalEnable =
3401 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3402 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3403 uint8_t eventReceiverAddressInfo = 0x08;
3404 uint16_t heartbeatTimer = 0x78;
3405
3406 std::vector<uint8_t> requestMsg(hdrSize +
3407 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303408 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303409 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3410
3411 auto rc = encode_set_event_receiver_req(
3412 0, eventMessageGlobalEnable, transportProtocolType,
3413 eventReceiverAddressInfo, heartbeatTimer, request);
3414
3415 EXPECT_EQ(rc, PLDM_SUCCESS);
3416 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303417 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303418 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3419 EXPECT_EQ(eventMessageGlobalEnable, req->event_message_global_enable);
3420 EXPECT_EQ(transportProtocolType, req->transport_protocol_type);
3421 EXPECT_EQ(eventReceiverAddressInfo, req->event_receiver_address_info);
3422 EXPECT_EQ(heartbeatTimer, le16toh(req->heartbeat_timer));
3423}
3424
3425TEST(SetEventReceiver, testBadEncodeRequest)
3426{
3427 uint8_t eventMessageGlobalEnable =
3428 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3429 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3430 uint8_t eventReceiverAddressInfo = 0x08;
3431 uint16_t heartbeatTimer = 0;
3432
3433 std::vector<uint8_t> requestMsg(hdrSize +
3434 PLDM_SET_EVENT_RECEIVER_REQ_BYTES);
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303435 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303436 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3437
3438 auto rc = encode_set_event_receiver_req(
3439 0, eventMessageGlobalEnable, transportProtocolType,
3440 eventReceiverAddressInfo, heartbeatTimer, request);
3441
3442 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3443}
3444
3445TEST(SetEventReceiver, testGoodDecodeResponse)
3446{
3447 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3448 responseMsg{};
3449
3450 uint8_t retcompletion_code = 0;
3451 responseMsg[hdrSize] = PLDM_SUCCESS;
3452
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303453 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303454 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3455 auto rc = decode_set_event_receiver_resp(
3456 response, responseMsg.size() - sizeof(pldm_msg_hdr),
3457 &retcompletion_code);
3458
3459 EXPECT_EQ(rc, PLDM_SUCCESS);
3460 EXPECT_EQ(PLDM_SUCCESS, retcompletion_code);
3461}
3462
3463TEST(SetEventReceiver, testBadDecodeResponse)
3464{
3465 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3466 responseMsg{};
3467 uint8_t retcompletion_code = 0;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303468 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303469 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3470
3471 auto rc = decode_set_event_receiver_resp(
3472 response, responseMsg.size() - sizeof(pldm_msg_hdr), NULL);
3473
3474 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3475
3476 rc = decode_set_event_receiver_resp(
3477 nullptr, responseMsg.size() - sizeof(pldm_msg_hdr),
3478 &retcompletion_code);
3479
3480 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3481}
3482
3483TEST(SetEventReceiver, testGoodEncodeResponse)
3484{
3485 std::array<uint8_t,
3486 sizeof(pldm_msg_hdr) + PLDM_SET_EVENT_RECEIVER_RESP_BYTES>
3487 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303488 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303489 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
3490 uint8_t completionCode = 0;
3491
3492 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, response);
3493
3494 EXPECT_EQ(rc, PLDM_SUCCESS);
3495 EXPECT_EQ(completionCode, response->payload[0]);
3496}
3497
3498TEST(SetEventReceiver, testBadEncodeResponse)
3499{
3500 auto rc = encode_set_event_receiver_resp(0, PLDM_SUCCESS, NULL);
3501 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3502}
3503
3504TEST(SetEventReceiver, testGoodDecodeRequest)
3505{
3506
3507 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3508 requestMsg{};
3509
3510 uint8_t eventMessageGlobalEnable =
3511 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3512 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3513 uint8_t eventReceiverAddressInfo = 0x08;
3514 uint16_t heartbeatTimer = 0x78;
3515
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303516 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303517 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3518 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303519 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303520 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3521
3522 req->event_message_global_enable = eventMessageGlobalEnable;
3523 req->transport_protocol_type = transportProtocolType;
3524 req->event_receiver_address_info = eventReceiverAddressInfo;
3525 req->heartbeat_timer = htole16(heartbeatTimer);
3526
3527 uint8_t reteventMessageGlobalEnable;
3528 uint8_t rettransportProtocolType;
3529 uint8_t reteventReceiverAddressInfo;
3530 uint16_t retheartbeatTimer;
3531 auto rc = decode_set_event_receiver_req(
3532 request, requestMsg.size() - hdrSize, &reteventMessageGlobalEnable,
3533 &rettransportProtocolType, &reteventReceiverAddressInfo,
3534 &retheartbeatTimer);
3535
3536 EXPECT_EQ(rc, PLDM_SUCCESS);
3537 EXPECT_EQ(eventMessageGlobalEnable, reteventMessageGlobalEnable);
3538 EXPECT_EQ(transportProtocolType, rettransportProtocolType);
3539 EXPECT_EQ(eventReceiverAddressInfo, reteventReceiverAddressInfo);
3540 EXPECT_EQ(heartbeatTimer, retheartbeatTimer);
3541}
3542
3543TEST(SetEventReceiver, testBadDecodeRequest)
3544{
3545 std::array<uint8_t, hdrSize + PLDM_SET_EVENT_RECEIVER_REQ_BYTES>
3546 requestMsg{};
3547
3548 auto rc = decode_set_event_receiver_req(NULL, requestMsg.size() - hdrSize,
3549 NULL, NULL, NULL, NULL);
3550 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
3551
3552 uint8_t eventMessageGlobalEnable =
3553 PLDM_EVENT_MESSAGE_GLOBAL_ENABLE_ASYNC_KEEP_ALIVE;
3554 uint8_t transportProtocolType = PLDM_TRANSPORT_PROTOCOL_TYPE_MCTP;
3555 uint8_t eventReceiverAddressInfo = 0x08;
3556 uint16_t heartbeatTimer = 0x78;
3557
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303558 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303559 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
3560 struct pldm_set_event_receiver_req* req =
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09303561 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09303562 reinterpret_cast<struct pldm_set_event_receiver_req*>(request->payload);
3563
3564 req->event_message_global_enable = eventMessageGlobalEnable;
3565 req->transport_protocol_type = transportProtocolType;
3566 req->event_receiver_address_info = eventReceiverAddressInfo;
3567 req->heartbeat_timer = htole16(heartbeatTimer);
3568
3569 uint8_t reteventMessageGlobalEnable;
3570 uint8_t rettransportProtocolType;
3571 uint8_t reteventReceiverAddressInfo;
3572 uint16_t retheartbeatTimer;
3573 rc = decode_set_event_receiver_req(
3574 request, requestMsg.size() - hdrSize - 1, &reteventMessageGlobalEnable,
3575 &rettransportProtocolType, &reteventReceiverAddressInfo,
3576 &retheartbeatTimer);
3577 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
3578}
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303579
3580TEST(decodeNumericSensorPdrData, Uint8Test)
3581{
3582 std::vector<uint8_t> pdr1{
3583 0x1,
3584 0x0,
3585 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303586 0x0, // record handle
3587 0x1, // PDRHeaderVersion
3588 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303589 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303590 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303591 PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303592 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303593 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303594 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303595 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303596 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303597 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303598 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303599 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303600 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303601 1,
3602 0, // containerID=1
3603 PLDM_NO_INIT, // sensorInit
3604 false, // sensorAuxiliaryNamesPDR
3605 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3606 0, // unitModifier
3607 0, // rateUnit
3608 0, // baseOEMUnitHandle
3609 0, // auxUnit
3610 0, // auxUnitModifier
3611 0, // auxRateUnit
3612 0, // rel
3613 0, // auxOEMUnitHandle
3614 true, // isLinear
3615 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
3616 0,
3617 0,
3618 0xc0,
3619 0x3f, // resolution=1.5
3620 0,
3621 0,
3622 0x80,
3623 0x3f, // offset=1.0
3624 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303625 0, // accuracy
3626 0, // plusTolerance
3627 0, // minusTolerance
3628 3, // hysteresis = 3
3629 0, // supportedThresholds
3630 0, // thresholdAndHysteresisVolatility
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303631 0,
3632 0,
3633 0x80,
3634 0x3f, // stateTransistionInterval=1.0
3635 0,
3636 0,
3637 0x80,
3638 0x3f, // updateInverval=1.0
3639 255, // maxReadable
3640 0, // minReadable
3641 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
3642 0, // rangeFieldsupport
3643 50, // nominalValue = 50
3644 60, // normalMax = 60
3645 40, // normalMin = 40
3646 70, // warningHigh = 70
3647 30, // warningLow = 30
3648 80, // criticalHigh = 80
3649 20, // criticalLow = 20
3650 90, // fatalHigh = 90
3651 10 // fatalLow = 10
3652 };
3653
3654 struct pldm_numeric_sensor_value_pdr decodedPdr;
3655 auto rc =
3656 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3657 EXPECT_EQ(PLDM_SUCCESS, rc);
3658 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
3659 EXPECT_EQ(1, decodedPdr.hdr.version);
3660 EXPECT_EQ(PLDM_NUMERIC_SENSOR_PDR, decodedPdr.hdr.type);
3661 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
3662 EXPECT_EQ(PLDM_PDR_NUMERIC_SENSOR_PDR_MIN_LENGTH, decodedPdr.hdr.length);
3663 EXPECT_EQ(1, decodedPdr.sensor_id);
3664 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
3665 EXPECT_EQ(1, decodedPdr.entity_instance_num);
3666 EXPECT_EQ(1, decodedPdr.container_id);
3667 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.sensor_init);
3668 EXPECT_EQ(false, decodedPdr.sensor_auxiliary_names_pdr);
3669 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
3670 EXPECT_EQ(0, decodedPdr.unit_modifier);
3671 EXPECT_EQ(0, decodedPdr.rate_unit);
3672 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
3673 EXPECT_EQ(0, decodedPdr.aux_unit);
3674 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
3675 EXPECT_EQ(0, decodedPdr.aux_rate_unit);
3676 EXPECT_EQ(0, decodedPdr.rel);
3677 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
3678 EXPECT_EQ(true, decodedPdr.is_linear);
3679 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT8, decodedPdr.sensor_data_size);
3680 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
3681 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
3682 EXPECT_EQ(0, decodedPdr.accuracy);
3683 EXPECT_EQ(0, decodedPdr.plus_tolerance);
3684 EXPECT_EQ(0, decodedPdr.minus_tolerance);
3685 EXPECT_EQ(3, decodedPdr.hysteresis.value_u8);
3686 EXPECT_EQ(0, decodedPdr.supported_thresholds.byte);
3687 EXPECT_EQ(0, decodedPdr.threshold_and_hysteresis_volatility.byte);
3688 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
3689 EXPECT_FLOAT_EQ(1.0f, decodedPdr.update_interval);
3690 EXPECT_EQ(255, decodedPdr.max_readable.value_u8);
3691 EXPECT_EQ(0, decodedPdr.min_readable.value_u8);
3692 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
3693 EXPECT_EQ(0, decodedPdr.range_field_support.byte);
3694 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
3695 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
3696 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
3697 EXPECT_EQ(70, decodedPdr.warning_high.value_u8);
3698 EXPECT_EQ(30, decodedPdr.warning_low.value_u8);
3699 EXPECT_EQ(80, decodedPdr.critical_high.value_u8);
3700 EXPECT_EQ(20, decodedPdr.critical_low.value_u8);
3701 EXPECT_EQ(90, decodedPdr.fatal_high.value_u8);
3702 EXPECT_EQ(10, decodedPdr.fatal_low.value_u8);
3703}
3704
3705TEST(decodeNumericSensorPdrData, Sint8Test)
3706{
3707 std::vector<uint8_t> pdr1{
3708 0x1,
3709 0x0,
3710 0x0,
3711 0x0, // record handle
3712 0x1, // PDRHeaderVersion
3713 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3714 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303715 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303716 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3717 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH +
3718 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09303719 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303720 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303721 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303722 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303723 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303724 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303725 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303726 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303727 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303728 0x1,
3729 0x0, // containerID=1
3730 PLDM_NO_INIT, // sensorInit
3731 false, // sensorAuxiliaryNamesPDR
3732 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3733 0, // unitModifier
3734 0, // rateUnit
3735 0, // baseOEMUnitHandle
3736 0, // auxUnit
3737 0, // auxUnitModifier
3738 0, // auxRateUnit
3739 0, // rel
3740 0, // auxOEMUnitHandle
3741 true, // isLinear
3742 PLDM_RANGE_FIELD_FORMAT_SINT8, // sensorDataSize
3743 0,
3744 0,
3745 0,
3746 0, // resolution
3747 0,
3748 0,
3749 0,
3750 0, // offset
3751 0,
3752 0, // accuracy
3753 0, // plusTolerance
3754 0, // minusTolerance
3755 3, // hysteresis = 3
3756 0, // supportedThresholds
3757 0, // thresholdAndHysteresisVolatility
3758 0,
3759 0,
3760 0x80,
3761 0x3f, // stateTransistionInterval=1.0
3762 0,
3763 0,
3764 0x80,
3765 0x3f, // updateInverval=1.0
3766 0x64, // maxReadable = 100
3767 0x9c, // minReadable = -100
3768 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
3769 0, // rangeFieldsupport
3770 0, // nominalValue = 0
3771 5, // normalMax = 5
3772 0xfb, // normalMin = -5
3773 10, // warningHigh = 10
3774 0xf6, // warningLow = -10
3775 20, // criticalHigh = 20
3776 0xec, // criticalLow = -20
3777 30, // fatalHigh = 30
3778 0xe2 // fatalLow = -30
3779 };
3780
3781 struct pldm_numeric_sensor_value_pdr decodedPdr;
3782 auto rc =
3783 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3784 EXPECT_EQ(PLDM_SUCCESS, rc);
3785
3786 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT8, decodedPdr.sensor_data_size);
3787 EXPECT_EQ(100, decodedPdr.max_readable.value_s8);
3788 EXPECT_EQ(-100, decodedPdr.min_readable.value_s8);
3789 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
3790 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
3791 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
3792 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
3793 EXPECT_EQ(10, decodedPdr.warning_high.value_s8);
3794 EXPECT_EQ(-10, decodedPdr.warning_low.value_s8);
3795 EXPECT_EQ(20, decodedPdr.critical_high.value_s8);
3796 EXPECT_EQ(-20, decodedPdr.critical_low.value_s8);
3797 EXPECT_EQ(30, decodedPdr.fatal_high.value_s8);
3798 EXPECT_EQ(-30, decodedPdr.fatal_low.value_s8);
3799}
3800
3801TEST(decodeNumericSensorPdrData, Uint16Test)
3802{
3803 std::vector<uint8_t> pdr1{
3804 0x1,
3805 0x0,
3806 0x0,
3807 0x0, // record handle
3808 0x1, // PDRHeaderVersion
3809 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3810 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303811 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303812 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3813 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
3814 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09303815 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303816 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303817 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303818 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303819 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303820 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303821 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303822 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303823 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303824 0x1,
3825 0x0, // containerID=1
3826 PLDM_NO_INIT, // sensorInit
3827 false, // sensorAuxiliaryNamesPDR
3828 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3829 0, // unitModifier
3830 0, // rateUnit
3831 0, // baseOEMUnitHandle
3832 0, // auxUnit
3833 0, // auxUnitModifier
3834 0, // auxRateUnit
3835 0, // rel
3836 0, // auxOEMUnitHandle
3837 true, // isLinear
3838 PLDM_SENSOR_DATA_SIZE_UINT16, // sensorDataSize
3839 0,
3840 0,
3841 0,
3842 0, // resolution
3843 0,
3844 0,
3845 0,
3846 0, // offset
3847 0,
3848 0, // accuracy
3849 0, // plusTolerance
3850 0, // minusTolerance
3851 3,
3852 0, // hysteresis = 3
3853 0, // supportedThresholds
3854 0, // thresholdAndHysteresisVolatility
3855 0,
3856 0,
3857 0x80,
3858 0x3f, // stateTransistionInterval=1.0
3859 0,
3860 0,
3861 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09303862 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303863 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303864 0x10, // maxReadable = 4096
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303865 0,
3866 0, // minReadable = 0
3867 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
3868 0, // rangeFieldsupport
3869 0x88,
Andrew Jeffery01731472023-06-13 10:33:18 +09303870 0x13, // nominalValue = 5,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303871 0x70,
Andrew Jeffery01731472023-06-13 10:33:18 +09303872 0x17, // normalMax = 6,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303873 0xa0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303874 0x0f, // normalMin = 4,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303875 0x58,
Andrew Jeffery01731472023-06-13 10:33:18 +09303876 0x1b, // warningHigh = 7,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303877 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303878 0x0b, // warningLow = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303879 0x40,
Andrew Jeffery01731472023-06-13 10:33:18 +09303880 0x1f, // criticalHigh = 8,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303881 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303882 0x07, // criticalLow = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303883 0x28,
Andrew Jeffery01731472023-06-13 10:33:18 +09303884 0x23, // fatalHigh = 9,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303885 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303886 0x03 // fatalLow = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303887 };
3888
3889 struct pldm_numeric_sensor_value_pdr decodedPdr;
3890 auto rc =
3891 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
3892 EXPECT_EQ(PLDM_SUCCESS, rc);
3893
3894 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT16, decodedPdr.sensor_data_size);
3895 EXPECT_EQ(4096, decodedPdr.max_readable.value_u16);
3896 EXPECT_EQ(0, decodedPdr.min_readable.value_u16);
3897 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
3898 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
3899 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
3900 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
3901 EXPECT_EQ(7000, decodedPdr.warning_high.value_u16);
3902 EXPECT_EQ(3000, decodedPdr.warning_low.value_u16);
3903 EXPECT_EQ(8000, decodedPdr.critical_high.value_u16);
3904 EXPECT_EQ(2000, decodedPdr.critical_low.value_u16);
3905 EXPECT_EQ(9000, decodedPdr.fatal_high.value_u16);
3906 EXPECT_EQ(1000, decodedPdr.fatal_low.value_u16);
3907}
3908
3909TEST(decodeNumericSensorPdrData, Sint16Test)
3910{
3911 std::vector<uint8_t> pdr1{
3912 0x1,
3913 0x0,
3914 0x0,
3915 0x0, // record handle
3916 0x1, // PDRHeaderVersion
3917 PLDM_NUMERIC_SENSOR_PDR, // PDRType
3918 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303919 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303920 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
3921 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 2 +
3922 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
Andrew Jeffery01731472023-06-13 10:33:18 +09303923 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303924 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303925 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303926 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303927 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303928 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09303929 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303930 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09303931 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303932 0x1,
3933 0x0, // containerID=1
3934 PLDM_NO_INIT, // sensorInit
3935 false, // sensorAuxiliaryNamesPDR
3936 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
3937 0, // unitModifier
3938 0, // rateUnit
3939 0, // baseOEMUnitHandle
3940 0, // auxUnit
3941 0, // auxUnitModifier
3942 0, // auxRateUnit
3943 0, // rel
3944 0, // auxOEMUnitHandle
3945 true, // isLinear
3946 PLDM_SENSOR_DATA_SIZE_SINT16, // sensorDataSize
3947 0,
3948 0,
3949 0,
3950 0, // resolution
3951 0,
3952 0,
3953 0,
3954 0, // offset
3955 0,
3956 0, // accuracy
3957 0, // plusTolerance
3958 0, // minusTolerance
3959 3,
3960 0, // hysteresis
3961 0, // supportedThresholds
3962 0, // thresholdAndHysteresisVolatility
3963 0,
3964 0,
3965 0x80,
3966 0x3f, // stateTransistionInterval=1.0
3967 0,
3968 0,
3969 0x80,
Andrew Jeffery01731472023-06-13 10:33:18 +09303970 0x3f, // updateInverval=1.0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303971 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303972 0x03, // maxReadable = 1000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303973 0x18,
3974 0xfc, // minReadable = -1000
3975 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
3976 0, // rangeFieldsupport
3977 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303978 0, // nominalValue = 0
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303979 0xf4,
Andrew Jeffery01731472023-06-13 10:33:18 +09303980 0x01, // normalMax = 500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303981 0x0c,
Andrew Jeffery01731472023-06-13 10:33:18 +09303982 0xfe, // normalMin = -500
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303983 0xe8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303984 0x03, // warningHigh = 1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303985 0x18,
Andrew Jeffery01731472023-06-13 10:33:18 +09303986 0xfc, // warningLow = -1,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303987 0xd0,
Andrew Jeffery01731472023-06-13 10:33:18 +09303988 0x07, // criticalHigh = 2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303989 0x30,
Andrew Jeffery01731472023-06-13 10:33:18 +09303990 0xf8, // criticalLow = -2,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303991 0xb8,
Andrew Jeffery01731472023-06-13 10:33:18 +09303992 0x0b, // fatalHigh = 3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303993 0x48,
Andrew Jeffery01731472023-06-13 10:33:18 +09303994 0xf4 // fatalLow = -3,000
Andrew Jeffery7992eb82023-04-06 16:13:53 +09303995 };
3996
3997 struct pldm_numeric_sensor_value_pdr decodedPdr;
3998 auto rc =
3999 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4000 EXPECT_EQ(PLDM_SUCCESS, rc);
4001
4002 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT16, decodedPdr.sensor_data_size);
4003 EXPECT_EQ(1000, decodedPdr.max_readable.value_s16);
4004 EXPECT_EQ(-1000, decodedPdr.min_readable.value_s16);
4005 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
4006 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
4007 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
4008 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
4009 EXPECT_EQ(1000, decodedPdr.warning_high.value_s16);
4010 EXPECT_EQ(-1000, decodedPdr.warning_low.value_s16);
4011 EXPECT_EQ(2000, decodedPdr.critical_high.value_s16);
4012 EXPECT_EQ(-2000, decodedPdr.critical_low.value_s16);
4013 EXPECT_EQ(3000, decodedPdr.fatal_high.value_s16);
4014 EXPECT_EQ(-3000, decodedPdr.fatal_low.value_s16);
4015}
4016
4017TEST(decodeNumericSensorPdrData, Uint32Test)
4018{
4019 std::vector<uint8_t> pdr1{
4020 0x1,
4021 0x0,
4022 0x0,
4023 0x0, // record handle
4024 0x1, // PDRHeaderVersion
4025 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4026 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304027 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304028 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4029 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4030 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304031 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304032 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304033 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304034 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304035 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304036 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304037 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304038 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304039 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304040 0x1,
4041 0x0, // containerID=1
4042 PLDM_NO_INIT, // sensorInit
4043 false, // sensorAuxiliaryNamesPDR
4044 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4045 0, // unitModifier
4046 0, // rateUnit
4047 0, // baseOEMUnitHandle
4048 0, // auxUnit
4049 0, // auxUnitModifier
4050 0, // auxRateUnit
4051 0, // rel
4052 0, // auxOEMUnitHandle
4053 true, // isLinear
4054 PLDM_SENSOR_DATA_SIZE_UINT32, // sensorDataSize
4055 0,
4056 0,
4057 0,
4058 0, // resolution
4059 0,
4060 0,
4061 0,
4062 0, // offset
4063 0,
4064 0, // accuracy
4065 0, // plusTolerance
4066 0, // minusTolerance
4067 3,
4068 0,
4069 0,
4070 0, // hysteresis
4071 0, // supportedThresholds
4072 0, // thresholdAndHysteresisVolatility
4073 0,
4074 0,
4075 0x80,
4076 0x3f, // stateTransistionInterval=1.0
4077 0,
4078 0,
4079 0x80,
4080 0x3f, // updateInverval=1.0
4081 0,
4082 0x10,
4083 0,
4084 0, // maxReadable = 4096
4085 0,
4086 0,
4087 0,
4088 0, // minReadable = 0
4089 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
4090 0, // rangeFieldsupport
4091 0x40,
4092 0x4b,
4093 0x4c,
4094 0x00, // nominalValue = 5,000,000
4095 0x80,
4096 0x8d,
4097 0x5b,
4098 0x00, // normalMax = 6,000,000
4099 0x00,
4100 0x09,
4101 0x3d,
4102 0x00, // normalMin = 4,000,000
4103 0xc0,
4104 0xcf,
4105 0x6a,
4106 0x00, // warningHigh = 7,000,000
4107 0xc0,
4108 0xc6,
4109 0x2d,
4110 0x00, // warningLow = 3,000,000
4111 0x00,
4112 0x12,
4113 0x7a,
4114 0x00, // criticalHigh = 8,000,000
4115 0x80,
4116 0x84,
4117 0x1e,
4118 0x00, // criticalLow = 2,000,000
4119 0x40,
4120 0x54,
4121 0x89,
4122 0x00, // fatalHigh = 9,000,000
4123 0x40,
4124 0x42,
4125 0x0f,
4126 0x00 // fatalLow = 1,000,000
4127 };
4128
4129 struct pldm_numeric_sensor_value_pdr decodedPdr;
4130 auto rc =
4131 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4132 EXPECT_EQ(PLDM_SUCCESS, rc);
4133
4134 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_UINT32, decodedPdr.sensor_data_size);
4135 EXPECT_EQ(4096, decodedPdr.max_readable.value_u32);
4136 EXPECT_EQ(0, decodedPdr.min_readable.value_u32);
4137 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
4138 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
4139 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
4140 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
4141 EXPECT_EQ(7000000, decodedPdr.warning_high.value_u32);
4142 EXPECT_EQ(3000000, decodedPdr.warning_low.value_u32);
4143 EXPECT_EQ(8000000, decodedPdr.critical_high.value_u32);
4144 EXPECT_EQ(2000000, decodedPdr.critical_low.value_u32);
4145 EXPECT_EQ(9000000, decodedPdr.fatal_high.value_u32);
4146 EXPECT_EQ(1000000, decodedPdr.fatal_low.value_u32);
4147}
4148
4149TEST(decodeNumericSensorPdrData, Sint32Test)
4150{
4151 std::vector<uint8_t> pdr1{
4152 0x1,
4153 0x0,
4154 0x0,
4155 0x0, // record handle
4156 0x1, // PDRHeaderVersion
4157 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4158 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304159 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304160 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4161 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4162 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304163 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304164 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304165 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304166 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304167 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304168 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304169 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304170 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304171 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304172 0x1,
4173 0x0, // containerID=1
4174 PLDM_NO_INIT, // sensorInit
4175 false, // sensorAuxiliaryNamesPDR
4176 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4177 0, // unitModifier
4178 0, // rateUnit
4179 0, // baseOEMUnitHandle
4180 0, // auxUnit
4181 0, // auxUnitModifier
4182 0, // auxRateUnit
4183 0, // rel
4184 0, // auxOEMUnitHandle
4185 true, // isLinear
4186 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
4187 0,
4188 0,
4189 0,
4190 0, // resolution
4191 0,
4192 0,
4193 0,
4194 0, // offset
4195 0,
4196 0, // accuracy
4197 0, // plusTolerance
4198 0, // minusTolerance
4199 3,
4200 0,
4201 0,
4202 0, // hysteresis
4203 0, // supportedThresholds
4204 0, // thresholdAndHysteresisVolatility
4205 0,
4206 0,
4207 0x80,
4208 0x3f, // stateTransistionInterval=1.0
4209 0,
4210 0,
4211 0x80,
4212 0x3f, // updateInverval=1.0
4213 0xa0,
4214 0x86,
4215 0x01,
4216 0x00, // maxReadable = 100000
4217 0x60,
4218 0x79,
4219 0xfe,
4220 0xff, // minReadable = -10000
4221 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
4222 0, // rangeFieldsupport
4223 0,
4224 0,
4225 0,
4226 0, // nominalValue = 0
4227 0x20,
4228 0xa1,
4229 0x07,
4230 0x00, // normalMax = 500,000
4231 0xe0,
4232 0x5e,
4233 0xf8,
4234 0xff, // normalMin = -500,000
4235 0x40,
4236 0x42,
4237 0x0f,
4238 0x00, // warningHigh = 1,000,000
4239 0xc0,
4240 0xbd,
4241 0xf0,
4242 0xff, // warningLow = -1,000,000
4243 0x80,
4244 0x84,
4245 0x1e,
4246 0x00, // criticalHigh = 2,000,000
4247 0x80,
4248 0x7b,
4249 0xe1,
4250 0xff, // criticalLow = -2,000,000
4251 0xc0,
4252 0xc6,
4253 0x2d,
4254 0x00, // fatalHigh = 3,000,000
4255 0x40,
4256 0x39,
4257 0xd2,
4258 0xff // fatalLow = -3,000,000
4259 };
4260
4261 struct pldm_numeric_sensor_value_pdr decodedPdr;
4262 auto rc =
4263 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4264 EXPECT_EQ(PLDM_SUCCESS, rc);
4265
4266 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
4267 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
4268 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
4269 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
4270 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
4271 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
4272 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
4273 EXPECT_EQ(1000000, decodedPdr.warning_high.value_s32);
4274 EXPECT_EQ(-1000000, decodedPdr.warning_low.value_s32);
4275 EXPECT_EQ(2000000, decodedPdr.critical_high.value_s32);
4276 EXPECT_EQ(-2000000, decodedPdr.critical_low.value_s32);
4277 EXPECT_EQ(3000000, decodedPdr.fatal_high.value_s32);
4278 EXPECT_EQ(-3000000, decodedPdr.fatal_low.value_s32);
4279}
4280
4281TEST(decodeNumericSensorPdrData, Real32Test)
4282{
4283 std::vector<uint8_t> pdr1{
4284 0x1,
4285 0x0,
4286 0x0,
4287 0x0, // record handle
4288 0x1, // PDRHeaderVersion
4289 PLDM_NUMERIC_SENSOR_PDR, // PDRType
4290 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304291 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304292 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH +
4293 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_SENSOR_DATA_SIZE_MIN_LENGTH * 4 +
4294 PLDM_PDR_NUMERIC_SENSOR_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
Andrew Jeffery01731472023-06-13 10:33:18 +09304295 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304296 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304297 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304298 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304299 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304300 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304301 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304302 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304303 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304304 0x1,
4305 0x0, // containerID=1
4306 PLDM_NO_INIT, // sensorInit
4307 false, // sensorAuxiliaryNamesPDR
4308 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4309 0, // unitModifier
4310 0, // rateUnit
4311 0, // baseOEMUnitHandle
4312 0, // auxUnit
4313 0, // auxUnitModifier
4314 0, // auxRateUnit
4315 0, // rel
4316 0, // auxOEMUnitHandle
4317 true, // isLinear
4318 PLDM_SENSOR_DATA_SIZE_SINT32, // sensorDataSize
4319 0,
4320 0,
4321 0,
4322 0, // resolution
4323 0,
4324 0,
4325 0,
4326 0, // offset
4327 0,
4328 0, // accuracy
4329 0, // plusTolerance
4330 0, // minusTolerance
4331 3,
4332 0,
4333 0,
4334 0, // hysteresis
4335 0, // supportedThresholds
4336 0, // thresholdAndHysteresisVolatility
4337 0,
4338 0,
4339 0x80,
4340 0x3f, // stateTransistionInterval=1.0
4341 0,
4342 0,
4343 0x80,
4344 0x3f, // updateInverval=1.0
4345 0xa0,
4346 0x86,
4347 0x01,
4348 0x00, // maxReadable = 100000
4349 0x60,
4350 0x79,
4351 0xfe,
4352 0xff, // minReadable = -10000
4353 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
4354 0, // rangeFieldsupport
4355 0,
4356 0,
4357 0,
4358 0, // nominalValue = 0.0
4359 0x33,
4360 0x33,
4361 0x48,
4362 0x42, // normalMax = 50.05
4363 0x33,
4364 0x33,
4365 0x48,
4366 0xc2, // normalMin = -50.05
4367 0x83,
4368 0x00,
4369 0xc8,
4370 0x42, // warningHigh = 100.001
4371 0x83,
4372 0x00,
4373 0xc8,
4374 0xc2, // warningLow = -100.001
4375 0x83,
4376 0x00,
4377 0x48,
4378 0x43, // criticalHigh = 200.002
4379 0x83,
4380 0x00,
4381 0x48,
4382 0xc3, // criticalLow = -200.002
4383 0x62,
4384 0x00,
4385 0x96,
4386 0x43, // fatalHigh = 300.003
4387 0x62,
4388 0x00,
4389 0x96,
4390 0xc3 // fatalLow = -300.003
4391 };
4392
4393 struct pldm_numeric_sensor_value_pdr decodedPdr;
4394 auto rc =
4395 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4396 EXPECT_EQ(PLDM_SUCCESS, rc);
4397
4398 EXPECT_EQ(PLDM_SENSOR_DATA_SIZE_SINT32, decodedPdr.sensor_data_size);
4399 EXPECT_EQ(100000, decodedPdr.max_readable.value_s32);
4400 EXPECT_EQ(-100000, decodedPdr.min_readable.value_s32);
4401 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
4402 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
4403 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
4404 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
4405 EXPECT_FLOAT_EQ(100.001f, decodedPdr.warning_high.value_f32);
4406 EXPECT_FLOAT_EQ(-100.001f, decodedPdr.warning_low.value_f32);
4407 EXPECT_FLOAT_EQ(200.002f, decodedPdr.critical_high.value_f32);
4408 EXPECT_FLOAT_EQ(-200.002f, decodedPdr.critical_low.value_f32);
4409 EXPECT_FLOAT_EQ(300.003f, decodedPdr.fatal_high.value_f32);
4410 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.fatal_low.value_f32);
4411}
4412
4413TEST(decodeNumericSensorPdrDataDeathTest, InvalidSizeTest)
4414{
4415 // A corrupted PDR. The data after plusTolerance missed.
4416 std::vector<uint8_t> pdr1{
4417 0x1,
4418 0x0,
4419 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304420 0x0, // record handle
4421 0x1, // PDRHeaderVersion
4422 PLDM_NUMERIC_SENSOR_PDR, // PDRType
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304423 0x0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304424 0x0, // recordChangeNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304425 PLDM_PDR_NUMERIC_SENSOR_PDR_FIXED_LENGTH,
Andrew Jeffery01731472023-06-13 10:33:18 +09304426 0, // dataLength
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304427 0,
Andrew Jeffery01731472023-06-13 10:33:18 +09304428 0, // PLDMTerminusHandle
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304429 0x1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304430 0x0, // sensorID=1
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304431 PLDM_ENTITY_POWER_SUPPLY,
Andrew Jeffery01731472023-06-13 10:33:18 +09304432 0, // entityType=Power Supply(120)
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304433 1,
Andrew Jeffery01731472023-06-13 10:33:18 +09304434 0, // entityInstanceNumber
Andrew Jeffery7992eb82023-04-06 16:13:53 +09304435 0x1,
4436 0x0, // containerID=1
4437 PLDM_NO_INIT, // sensorInit
4438 false, // sensorAuxiliaryNamesPDR
4439 2, // baseUint(2)=degrees C
4440 0, // unitModifier
4441 0, // rateUnit
4442 0, // baseOEMUnitHandle
4443 0, // auxUnit
4444 0, // auxUnitModifier
4445 0, // auxRateUnit
4446 0, // rel
4447 0, // auxOEMUnitHandle
4448 true, // isLinear
4449 PLDM_SENSOR_DATA_SIZE_UINT8, // sensorDataSize
4450 0,
4451 0,
4452 0,
4453 0, // resolution
4454 0,
4455 0,
4456 0,
4457 0, // offset
4458 0,
4459 0, // accuracy
4460 0 // plusTolerance
4461 };
4462
4463 struct pldm_numeric_sensor_value_pdr decodedPdr;
4464 int rc =
4465 decode_numeric_sensor_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4466 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
4467}
Thu Nguyend4878cd2023-11-09 10:18:33 +07004468
Andrew Jeffery688be622024-05-23 11:22:51 +09304469#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004470TEST(decodeNumericEffecterPdrData, Uint8Test)
4471{
4472 std::vector<uint8_t> pdr1{
4473 0x1,
4474 0x0,
4475 0x0,
4476 0x0, // record handle
4477 0x1, // PDRHeaderVersion
4478 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4479 0x0,
4480 0x0, // recordChangeNumber
4481 PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH,
4482 0, // dataLength
4483 0,
4484 0, // PLDMTerminusHandle
4485 0x1,
4486 0x0, // effecterID=1
4487 PLDM_ENTITY_POWER_SUPPLY,
4488 0, // entityType=Power Supply(120)
4489 1,
4490 0, // entityInstanceNumber
4491 1,
4492 0, // containerID=1
4493 0x2,
4494 0x0, // effecter_semantic_id=2
4495 PLDM_NO_INIT, // effecterInit
4496 false, // effecterAuxiliaryNames
4497 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4498 0, // unitModifier
4499 0, // rateUnit
4500 0, // baseOEMUnitHandle
4501 0, // auxUnit
4502 0, // auxUnitModifier
4503 4, // auxRateUnit
4504 0, // auxOEMUnitHandle
4505 true, // isLinear
4506 PLDM_EFFECTER_DATA_SIZE_UINT8, // effecterDataSize
4507 0,
4508 0,
4509 0xc0,
4510 0x3f, // resolution=1.5
4511 0,
4512 0,
4513 0x80,
4514 0x3f, // offset=1.0
4515 0,
4516 0, // accuracy
4517 0, // plusTolerance
4518 0, // minusTolerance
4519 0,
4520 0,
4521 0x80,
4522 0x3f, // stateTransistionInterval=1.0
4523 0,
4524 0,
4525 0x80,
4526 0x3f, // transition_interval=1.0
4527 255, // maxSetdable
4528 0, // minSetable
4529 PLDM_RANGE_FIELD_FORMAT_UINT8, // rangeFieldFormat
4530 0x1f, // rangeFieldsupport
4531 50, // nominalValue = 50
4532 60, // normalMax = 60
4533 40, // normalMin = 40
4534 90, // rated_max = 90
4535 10 // rated_min = 10
4536 };
4537
4538 struct pldm_numeric_effecter_value_pdr decodedPdr;
4539 auto rc =
4540 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4541 EXPECT_EQ(PLDM_SUCCESS, rc);
4542 EXPECT_EQ(1, decodedPdr.hdr.record_handle);
4543 EXPECT_EQ(1, decodedPdr.hdr.version);
4544 EXPECT_EQ(PLDM_NUMERIC_EFFECTER_PDR, decodedPdr.hdr.type);
4545 EXPECT_EQ(0, decodedPdr.hdr.record_change_num);
4546 EXPECT_EQ(PLDM_PDR_NUMERIC_EFFECTER_PDR_MIN_LENGTH, decodedPdr.hdr.length);
4547 EXPECT_EQ(1, decodedPdr.effecter_id);
4548 EXPECT_EQ(PLDM_ENTITY_POWER_SUPPLY, decodedPdr.entity_type);
4549 EXPECT_EQ(1, decodedPdr.entity_instance);
4550 EXPECT_EQ(1, decodedPdr.container_id);
4551 EXPECT_EQ(2, decodedPdr.effecter_semantic_id);
4552 EXPECT_EQ(PLDM_NO_INIT, decodedPdr.effecter_init);
4553 EXPECT_EQ(false, decodedPdr.effecter_auxiliary_names);
4554 EXPECT_EQ(PLDM_SENSOR_UNIT_DEGRESS_C, decodedPdr.base_unit);
4555 EXPECT_EQ(0, decodedPdr.unit_modifier);
4556 EXPECT_EQ(0, decodedPdr.rate_unit);
4557 EXPECT_EQ(0, decodedPdr.base_oem_unit_handle);
4558 EXPECT_EQ(0, decodedPdr.aux_unit);
4559 EXPECT_EQ(0, decodedPdr.aux_unit_modifier);
4560 EXPECT_EQ(4, decodedPdr.aux_rate_unit);
4561 EXPECT_EQ(0, decodedPdr.aux_oem_unit_handle);
4562 EXPECT_EQ(true, decodedPdr.is_linear);
4563 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT8, decodedPdr.effecter_data_size);
4564 EXPECT_FLOAT_EQ(1.5f, decodedPdr.resolution);
4565 EXPECT_FLOAT_EQ(1.0f, decodedPdr.offset);
4566 EXPECT_EQ(0, decodedPdr.accuracy);
4567 EXPECT_EQ(0, decodedPdr.plus_tolerance);
4568 EXPECT_EQ(0, decodedPdr.minus_tolerance);
4569 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
4570 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
4571 EXPECT_EQ(255, decodedPdr.max_settable.value_u8);
4572 EXPECT_EQ(0, decodedPdr.min_settable.value_u8);
4573 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT8, decodedPdr.range_field_format);
4574 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4575 EXPECT_EQ(50, decodedPdr.nominal_value.value_u8);
4576 EXPECT_EQ(60, decodedPdr.normal_max.value_u8);
4577 EXPECT_EQ(40, decodedPdr.normal_min.value_u8);
4578 EXPECT_EQ(90, decodedPdr.rated_max.value_u8);
4579 EXPECT_EQ(10, decodedPdr.rated_min.value_u8);
4580}
Andrew Jeffery688be622024-05-23 11:22:51 +09304581#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004582
Andrew Jeffery688be622024-05-23 11:22:51 +09304583#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004584TEST(decodeNumericEffecterPdrData, Sint8Test)
4585{
4586 std::vector<uint8_t> pdr1{
4587 0x1,
4588 0x0,
4589 0x0,
4590 0x0, // record handle
4591 0x1, // PDRHeaderVersion
4592 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4593 0x0,
4594 0x0, // recordChangeNumber
4595 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4596 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH +
4597 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH,
4598 0, // dataLength
4599 0,
4600 0, // PLDMTerminusHandle
4601 0x1,
4602 0x0, // effecterID=1
4603 PLDM_ENTITY_POWER_SUPPLY,
4604 0, // entityType=Power Supply(120)
4605 1,
4606 0, // entityInstanceNumber
4607 0x1,
4608 0x0, // containerID=1
4609 0x2,
4610 0x0, // effecter_semantic_id=2
4611 PLDM_NO_INIT, // effecterInit
4612 false, // effecterAuxiliaryNames
4613 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4614 0, // unitModifier
4615 0, // rateUnit
4616 0, // baseOEMUnitHandle
4617 0, // auxUnit
4618 0, // auxUnitModifier
4619 0, // auxRateUnit
4620 0, // auxOEMUnitHandle
4621 true, // isLinear
4622 PLDM_RANGE_FIELD_FORMAT_SINT8, // effecterDataSize
4623 0,
4624 0,
4625 0,
4626 0, // resolution
4627 0,
4628 0,
4629 0,
4630 0, // offset
4631 0,
4632 0, // accuracy
4633 0, // plusTolerance
4634 0, // minusTolerance
4635 0,
4636 0,
4637 0x80,
4638 0x3f, // stateTransistionInterval=1.0
4639 0,
4640 0,
4641 0x80,
4642 0x3f, // transition_interval=1.0
4643 0x64, // maxSetdable = 100
4644 0x9c, // minSetable = -100
4645 PLDM_RANGE_FIELD_FORMAT_SINT8, // rangeFieldFormat
4646 0x1f, // rangeFieldsupport
4647 0, // nominalValue = 0
4648 5, // normalMax = 5
4649 0xfb, // normalMin = -5
4650 30, // rated_max = 30
4651 0xe2 // rated_min = -30
4652 };
4653
4654 struct pldm_numeric_effecter_value_pdr decodedPdr;
4655 auto rc =
4656 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4657 EXPECT_EQ(PLDM_SUCCESS, rc);
4658
4659 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT8, decodedPdr.effecter_data_size);
4660 EXPECT_EQ(100, decodedPdr.max_settable.value_s8);
4661 EXPECT_EQ(-100, decodedPdr.min_settable.value_s8);
4662 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT8, decodedPdr.range_field_format);
4663 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4664 EXPECT_EQ(0, decodedPdr.nominal_value.value_s8);
4665 EXPECT_EQ(5, decodedPdr.normal_max.value_s8);
4666 EXPECT_EQ(-5, decodedPdr.normal_min.value_s8);
4667 EXPECT_EQ(30, decodedPdr.rated_max.value_s8);
4668 EXPECT_EQ(-30, decodedPdr.rated_min.value_s8);
4669}
Andrew Jeffery688be622024-05-23 11:22:51 +09304670#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004671
Andrew Jeffery688be622024-05-23 11:22:51 +09304672#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004673TEST(decodeNumericEffecterPdrData, Uint16Test)
4674{
4675 std::vector<uint8_t> pdr1{
4676 0x1,
4677 0x0,
4678 0x0,
4679 0x0, // record handle
4680 0x1, // PDRHeaderVersion
4681 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4682 0x0,
4683 0x0, // recordChangeNumber
4684 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4685 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4686 2 +
4687 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4688 0, // dataLength
4689 0,
4690 0, // PLDMTerminusHandle
4691 0x1,
4692 0x0, // effecterID=1
4693 PLDM_ENTITY_POWER_SUPPLY,
4694 0, // entityType=Power Supply(120)
4695 1,
4696 0, // entityInstanceNumber
4697 0x1,
4698 0x0, // containerID=1
4699 0x2,
4700 0x0, // effecter_semantic_id=2
4701 PLDM_NO_INIT, // effecterInit
4702 false, // effecterAuxiliaryNames
4703 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4704 0, // unitModifier
4705 0, // rateUnit
4706 0, // baseOEMUnitHandle
4707 0, // auxUnit
4708 0, // auxUnitModifier
4709 0, // auxRateUnit
4710 0, // auxOEMUnitHandle
4711 true, // isLinear
4712 PLDM_EFFECTER_DATA_SIZE_UINT16, // effecterDataSize
4713 0,
4714 0,
4715 0,
4716 0, // resolution
4717 0,
4718 0,
4719 0,
4720 0, // offset
4721 0,
4722 0, // accuracy
4723 0, // plusTolerance
4724 0, // minusTolerance
4725 0,
4726 0,
4727 0x80,
4728 0x3f, // stateTransistionInterval=1.0
4729 0,
4730 0,
4731 0x80,
4732 0x3f, // transition_interval=1.0
4733 0,
4734 0x10, // maxSetdable = 4096
4735 0,
4736 0, // minSetable = 0
4737 PLDM_RANGE_FIELD_FORMAT_UINT16, // rangeFieldFormat
4738 0x1f, // rangeFieldsupport
4739 0x88,
4740 0x13, // nominalValue = 5,000
4741 0x70,
4742 0x17, // normalMax = 6,000
4743 0xa0,
4744 0x0f, // normalMin = 4,000
4745 0x28,
4746 0x23, // rated_max = 9,000
4747 0xe8,
4748 0x03 // rated_min = 1,000
4749 };
4750
4751 struct pldm_numeric_effecter_value_pdr decodedPdr;
4752 auto rc =
4753 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4754 EXPECT_EQ(PLDM_SUCCESS, rc);
4755
4756 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT16, decodedPdr.effecter_data_size);
4757 EXPECT_EQ(4096, decodedPdr.max_settable.value_u16);
4758 EXPECT_EQ(0, decodedPdr.min_settable.value_u16);
4759 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT16, decodedPdr.range_field_format);
4760 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4761 EXPECT_EQ(5000, decodedPdr.nominal_value.value_u16);
4762 EXPECT_EQ(6000, decodedPdr.normal_max.value_u16);
4763 EXPECT_EQ(4000, decodedPdr.normal_min.value_u16);
4764 EXPECT_EQ(9000, decodedPdr.rated_max.value_u16);
4765 EXPECT_EQ(1000, decodedPdr.rated_min.value_u16);
4766}
Andrew Jeffery688be622024-05-23 11:22:51 +09304767#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004768
Andrew Jeffery688be622024-05-23 11:22:51 +09304769#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004770TEST(decodeNumericEffecterPdrData, Sint16Test)
4771{
4772 std::vector<uint8_t> pdr1{
4773 0x1,
4774 0x0,
4775 0x0,
4776 0x0, // record handle
4777 0x1, // PDRHeaderVersion
4778 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4779 0x0,
4780 0x0, // recordChangeNumber
4781 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4782 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4783 2 +
4784 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 2,
4785 0, // dataLength
4786 0,
4787 0, // PLDMTerminusHandle
4788 0x1,
4789 0x0, // effecterID=1
4790 PLDM_ENTITY_POWER_SUPPLY,
4791 0, // entityType=Power Supply(120)
4792 1,
4793 0, // entityInstanceNumber
4794 0x1,
4795 0x0, // containerID=1
4796 0x2,
4797 0x0, // effecter_semantic_id=2
4798 PLDM_NO_INIT, // effecterInit
4799 false, // effecterAuxiliaryNames
4800 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4801 0, // unitModifier
4802 0, // rateUnit
4803 0, // baseOEMUnitHandle
4804 0, // auxUnit
4805 0, // auxUnitModifier
4806 0, // auxRateUnit
4807 0, // auxOEMUnitHandle
4808 true, // isLinear
4809 PLDM_EFFECTER_DATA_SIZE_SINT16, // effecterDataSize
4810 0,
4811 0,
4812 0,
4813 0, // resolution
4814 0,
4815 0,
4816 0,
4817 0, // offset
4818 0,
4819 0, // accuracy
4820 0, // plusTolerance
4821 0, // minusTolerance
4822 0,
4823 0,
4824 0x80,
4825 0x3f, // stateTransistionInterval=1.0
4826 0,
4827 0,
4828 0x80,
4829 0x3f, // transition_interval=1.0
4830 0xe8,
4831 0x03, // maxSetdable = 1000
4832 0x18,
4833 0xfc, // minSetable = -1000
4834 PLDM_RANGE_FIELD_FORMAT_SINT16, // rangeFieldFormat
4835 0x1f, // rangeFieldsupport
4836 0,
4837 0, // nominalValue = 0
4838 0xf4,
4839 0x01, // normalMax = 500
4840 0x0c,
4841 0xfe, // normalMin = -500
4842 0xb8,
4843 0x0b, // rated_max = 3,000
4844 0x48,
4845 0xf4 // rated_min = -3,000
4846 };
4847
4848 struct pldm_numeric_effecter_value_pdr decodedPdr;
4849 auto rc =
4850 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4851 EXPECT_EQ(PLDM_SUCCESS, rc);
4852
4853 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT16, decodedPdr.effecter_data_size);
4854 EXPECT_EQ(1000, decodedPdr.max_settable.value_s16);
4855 EXPECT_EQ(-1000, decodedPdr.min_settable.value_s16);
4856 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT16, decodedPdr.range_field_format);
4857 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4858 EXPECT_EQ(0, decodedPdr.nominal_value.value_s16);
4859 EXPECT_EQ(500, decodedPdr.normal_max.value_s16);
4860 EXPECT_EQ(-500, decodedPdr.normal_min.value_s16);
4861 EXPECT_EQ(3000, decodedPdr.rated_max.value_s16);
4862 EXPECT_EQ(-3000, decodedPdr.rated_min.value_s16);
4863}
Andrew Jeffery688be622024-05-23 11:22:51 +09304864#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004865
Andrew Jeffery688be622024-05-23 11:22:51 +09304866#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004867TEST(decodeNumericEffecterPdrData, Uint32Test)
4868{
4869 std::vector<uint8_t> pdr1{
4870 0x1,
4871 0x0,
4872 0x0,
4873 0x0, // record handle
4874 0x1, // PDRHeaderVersion
4875 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4876 0x0,
4877 0x0, // recordChangeNumber
4878 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4879 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4880 4 +
4881 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
4882 0, // dataLength
4883 0,
4884 0, // PLDMTerminusHandle
4885 0x1,
4886 0x0, // effecterID=1
4887 PLDM_ENTITY_POWER_SUPPLY,
4888 0, // entityType=Power Supply(120)
4889 1,
4890 0, // entityInstanceNumber
4891 0x1,
4892 0x0, // containerID=1
4893 0x2,
4894 0x0, // effecter_semantic_id=2
4895 PLDM_NO_INIT, // effecterInit
4896 false, // effecterAuxiliaryNames
4897 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
4898 0, // unitModifier
4899 0, // rateUnit
4900 0, // baseOEMUnitHandle
4901 0, // auxUnit
4902 0, // auxUnitModifier
4903 0, // auxRateUnit
4904 0, // auxOEMUnitHandle
4905 true, // isLinear
4906 PLDM_EFFECTER_DATA_SIZE_UINT32, // effecterDataSize
4907 0,
4908 0,
4909 0,
4910 0, // resolution
4911 0,
4912 0,
4913 0,
4914 0, // offset
4915 0,
4916 0, // accuracy
4917 0, // plusTolerance
4918 0, // minusTolerance
4919 0,
4920 0,
4921 0x80,
4922 0x3f, // stateTransistionInterval=1.0
4923 0,
4924 0,
4925 0x80,
4926 0x3f, // transition_interval=1.0
4927 0,
4928 0x10,
4929 0,
4930 0, // maxSetdable = 4096
4931 0,
4932 0,
4933 0,
4934 0, // minSetable = 0
4935 PLDM_RANGE_FIELD_FORMAT_UINT32, // rangeFieldFormat
4936 0x1f, // rangeFieldsupport
4937 0x40,
4938 0x4b,
4939 0x4c,
4940 0x00, // nominalValue = 5,000,000
4941 0x80,
4942 0x8d,
4943 0x5b,
4944 0x00, // normalMax = 6,000,000
4945 0x00,
4946 0x09,
4947 0x3d,
4948 0x00, // normalMin = 4,000,000
4949 0x40,
4950 0x54,
4951 0x89,
4952 0x00, // rated_max = 9,000,000
4953 0x40,
4954 0x42,
4955 0x0f,
4956 0x00 // rated_min = 1,000,000
4957 };
4958
4959 struct pldm_numeric_effecter_value_pdr decodedPdr;
4960 auto rc =
4961 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
4962 EXPECT_EQ(PLDM_SUCCESS, rc);
4963
4964 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_UINT32, decodedPdr.effecter_data_size);
4965 EXPECT_EQ(4096, decodedPdr.max_settable.value_u32);
4966 EXPECT_EQ(0, decodedPdr.min_settable.value_u32);
4967 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_UINT32, decodedPdr.range_field_format);
4968 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
4969 EXPECT_EQ(5000000, decodedPdr.nominal_value.value_u32);
4970 EXPECT_EQ(6000000, decodedPdr.normal_max.value_u32);
4971 EXPECT_EQ(4000000, decodedPdr.normal_min.value_u32);
4972 EXPECT_EQ(9000000, decodedPdr.rated_max.value_u32);
4973 EXPECT_EQ(1000000, decodedPdr.rated_min.value_u32);
4974}
Andrew Jeffery688be622024-05-23 11:22:51 +09304975#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07004976
Andrew Jeffery688be622024-05-23 11:22:51 +09304977#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07004978TEST(decodeNumericEffecterPdrData, Sint32Test)
4979{
4980 std::vector<uint8_t> pdr1{
4981 0x1,
4982 0x0,
4983 0x0,
4984 0x0, // record handle
4985 0x1, // PDRHeaderVersion
4986 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
4987 0x0,
4988 0x0, // recordChangeNumber
4989 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
4990 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
4991 4 +
4992 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
4993 0, // dataLength
4994 0,
4995 0, // PLDMTerminusHandle
4996 0x1,
4997 0x0, // effecterID=1
4998 PLDM_ENTITY_POWER_SUPPLY,
4999 0, // entityType=Power Supply(120)
5000 1,
5001 0, // entityInstanceNumber
5002 0x1,
5003 0x0, // containerID=1
5004 0x2,
5005 0x0, // effecter_semantic_id=2
5006 PLDM_NO_INIT, // effecterInit
5007 false, // effecterAuxiliaryNames
5008 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
5009 0, // unitModifier
5010 0, // rateUnit
5011 0, // baseOEMUnitHandle
5012 0, // auxUnit
5013 0, // auxUnitModifier
5014 0, // auxRateUnit
5015 0, // auxOEMUnitHandle
5016 true, // isLinear
5017 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
5018 0,
5019 0,
5020 0,
5021 0, // resolution
5022 0,
5023 0,
5024 0,
5025 0, // offset
5026 0,
5027 0, // accuracy
5028 0, // plusTolerance
5029 0, // minusTolerance
5030 0,
5031 0,
5032 0x80,
5033 0x3f, // stateTransistionInterval=1.0
5034 0,
5035 0,
5036 0x80,
5037 0x3f, // transition_interval=1.0
5038 0xa0,
5039 0x86,
5040 0x01,
5041 0x00, // maxSetdable = 100000
5042 0x60,
5043 0x79,
5044 0xfe,
5045 0xff, // minSetable = -10000
5046 PLDM_RANGE_FIELD_FORMAT_SINT32, // rangeFieldFormat
5047 0x1f, // rangeFieldsupport
5048 0,
5049 0,
5050 0,
5051 0, // nominalValue = 0
5052 0x20,
5053 0xa1,
5054 0x07,
5055 0x00, // normalMax = 500,000
5056 0xe0,
5057 0x5e,
5058 0xf8,
5059 0xff, // normalMin = -500,000
5060 0xc0,
5061 0xc6,
5062 0x2d,
5063 0x00, // rated_max = 3,000,000
5064 0x40,
5065 0x39,
5066 0xd2,
5067 0xff // rated_min = -3,000,000
5068 };
5069
5070 struct pldm_numeric_effecter_value_pdr decodedPdr;
5071 auto rc =
5072 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5073 EXPECT_EQ(PLDM_SUCCESS, rc);
5074 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
5075 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
5076 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
5077 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_SINT32, decodedPdr.range_field_format);
5078 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5079 EXPECT_EQ(0, decodedPdr.nominal_value.value_s32);
5080 EXPECT_EQ(500000, decodedPdr.normal_max.value_s32);
5081 EXPECT_EQ(-500000, decodedPdr.normal_min.value_s32);
5082 EXPECT_EQ(3000000, decodedPdr.rated_max.value_s32);
5083 EXPECT_EQ(-3000000, decodedPdr.rated_min.value_s32);
5084}
Andrew Jeffery688be622024-05-23 11:22:51 +09305085#endif
Thu Nguyend4878cd2023-11-09 10:18:33 +07005086
Andrew Jeffery688be622024-05-23 11:22:51 +09305087#ifdef LIBPLDM_API_TESTING
Thu Nguyend4878cd2023-11-09 10:18:33 +07005088TEST(decodeNumericEffecterPdrData, Real32Test)
5089{
5090 std::vector<uint8_t> pdr1{
5091 0x1,
5092 0x0,
5093 0x0,
5094 0x0, // record handle
5095 0x1, // PDRHeaderVersion
5096 PLDM_NUMERIC_EFFECTER_PDR, // PDRType
5097 0x0,
5098 0x0, // recordChangeNumber
5099 PLDM_PDR_NUMERIC_EFFECTER_PDR_FIXED_LENGTH +
5100 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_EFFECTER_DATA_SIZE_MIN_LENGTH *
5101 4 +
5102 PLDM_PDR_NUMERIC_EFFECTER_PDR_VARIED_RANGE_FIELD_MIN_LENGTH * 4,
5103 0, // dataLength
5104 0,
5105
5106 0, // PLDMTerminusHandle
5107 0x1,
5108 0x0, // effecterID=1
5109 PLDM_ENTITY_POWER_SUPPLY,
5110 0, // entityType=Power Supply(120)
5111 1,
5112 0, // entityInstanceNumber
5113 0x1,
5114 0x0, // containerID=1
5115 0x2,
5116 0x0, // effecter_semantic_id=2
5117 PLDM_NO_INIT, // effecterInit
5118 false, // effecterAuxiliaryNames
5119 PLDM_SENSOR_UNIT_DEGRESS_C, // baseUint(2)=degrees C
5120 0, // unitModifier
5121 0, // rateUnit
5122 0, // baseOEMUnitHandle
5123 0, // auxUnit
5124 0, // auxUnitModifier
5125 0, // auxRateUnit
5126 0, // auxOEMUnitHandle
5127 true, // isLinear
5128 PLDM_EFFECTER_DATA_SIZE_SINT32, // effecterDataSize
5129 0,
5130 0,
5131 0,
5132 0, // resolution
5133 0,
5134 0,
5135 0,
5136 0, // offset
5137 0,
5138 0, // accuracy
5139 0, // plusTolerance
5140 0, // minusTolerance
5141 0,
5142 0,
5143 0x80,
5144 0x3f, // stateTransistionInterval=1.0
5145 0,
5146 0,
5147 0x80,
5148 0x3f, // transition_interval=1.0
5149 0xa0,
5150 0x86,
5151 0x01,
5152 0x00, // maxSetdable = 100000
5153 0x60,
5154 0x79,
5155 0xfe,
5156 0xff, // minSetable = -10000
5157 PLDM_RANGE_FIELD_FORMAT_REAL32, // rangeFieldFormat
5158 0x1f, // rangeFieldsupport
5159 0,
5160 0,
5161 0,
5162 0, // nominalValue = 0.0
5163 0x33,
5164 0x33,
5165 0x48,
5166 0x42, // normalMax = 50.05
5167 0x33,
5168 0x33,
5169 0x48,
5170 0xc2, // normalMin = -50.05
5171 0x62,
5172 0x00,
5173 0x96,
5174 0x43, // rated_max = 300.003
5175 0x62,
5176 0x00,
5177 0x96,
5178 0xc3 // rated_min = -300.003
5179 };
5180
5181 struct pldm_numeric_effecter_value_pdr decodedPdr;
5182 auto rc =
5183 decode_numeric_effecter_pdr_data(pdr1.data(), pdr1.size(), &decodedPdr);
5184 EXPECT_EQ(PLDM_SUCCESS, rc);
5185
5186 EXPECT_EQ(PLDM_EFFECTER_DATA_SIZE_SINT32, decodedPdr.effecter_data_size);
5187 EXPECT_FLOAT_EQ(1.0f, decodedPdr.state_transition_interval);
5188 EXPECT_FLOAT_EQ(1.0f, decodedPdr.transition_interval);
5189 EXPECT_EQ(100000, decodedPdr.max_settable.value_s32);
5190 EXPECT_EQ(-100000, decodedPdr.min_settable.value_s32);
5191 EXPECT_EQ(PLDM_RANGE_FIELD_FORMAT_REAL32, decodedPdr.range_field_format);
5192 EXPECT_EQ(0x1f, decodedPdr.range_field_support.byte);
5193 EXPECT_FLOAT_EQ(0, decodedPdr.nominal_value.value_f32);
5194 EXPECT_FLOAT_EQ(50.05f, decodedPdr.normal_max.value_f32);
5195 EXPECT_FLOAT_EQ(-50.05f, decodedPdr.normal_min.value_f32);
5196 EXPECT_FLOAT_EQ(300.003f, decodedPdr.rated_max.value_f32);
5197 EXPECT_FLOAT_EQ(-300.003f, decodedPdr.rated_min.value_f32);
5198}
Andrew Jeffery688be622024-05-23 11:22:51 +09305199#endif
Tal Yacobia6fa5552024-05-05 16:57:38 +03005200
Tal Yacobia6fa5552024-05-05 16:57:38 +03005201TEST(GetStateEffecterStates, testEncodeAndDecodeRequest)
5202{
5203 std::array<uint8_t, hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
5204 requestMsg{};
5205
5206 constexpr std::array<uint8_t,
5207 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES>
5208 expectedRequestMsg{
5209 {0x80, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES, 1, 0xab}};
5210
5211 constexpr uint16_t effecter_id = 0xab01;
5212
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305213 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005214 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
5215
5216 auto rc = encode_get_state_effecter_states_req(
5217 0, effecter_id, request, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
5218
5219 EXPECT_EQ(rc, PLDM_SUCCESS);
5220 EXPECT_EQ(requestMsg, expectedRequestMsg);
5221
5222 uint16_t ret_effecter_id;
5223
5224 rc = decode_get_state_effecter_states_req(
5225 request, requestMsg.size() - hdrSize, &ret_effecter_id);
5226
5227 EXPECT_EQ(rc, PLDM_SUCCESS);
5228 EXPECT_EQ(effecter_id, ret_effecter_id);
5229
5230 // Test invalid length decode request
5231
5232 rc = decode_get_state_effecter_states_req(
5233 request, requestMsg.size() - hdrSize - 1, &ret_effecter_id);
5234
Tal Yacobif490a382024-05-31 09:57:36 +03005235 EXPECT_EQ(rc, -EOVERFLOW);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005236}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005237
Tal Yacobia6fa5552024-05-05 16:57:38 +03005238TEST(GetStateEffecterStates, testBadEncodeRequest)
5239{
5240 std::vector<uint8_t> requestMsg(hdrSize +
5241 PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
5242
5243 auto rc = encode_get_state_effecter_states_req(
5244 0, 0, nullptr, PLDM_GET_STATE_EFFECTER_STATES_REQ_BYTES);
Tal Yacobif490a382024-05-31 09:57:36 +03005245 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005246}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005247
Tal Yacobia6fa5552024-05-05 16:57:38 +03005248TEST(GetStateEffecterStates, testBadDecodeRequest)
5249{
5250 std::array<uint8_t, hdrSize + PLDM_GET_NUMERIC_EFFECTER_VALUE_REQ_BYTES>
5251 requestMsg{};
5252
5253 auto rc = decode_get_state_effecter_states_req(
5254 nullptr, requestMsg.size() - hdrSize, nullptr);
5255
Tal Yacobif490a382024-05-31 09:57:36 +03005256 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005257}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005258
Tal Yacobia6fa5552024-05-05 16:57:38 +03005259TEST(GetStateEffecterStates, testEncodeAndDecodeResponse)
5260{
5261 constexpr uint8_t comp_effecterCnt = 0x2;
5262 constexpr uint8_t completionCode = 0;
5263 std::array<uint8_t,
5264 hdrSize + PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
5265 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * comp_effecterCnt>
5266 expectedResponseMsg{{0, PLDM_PLATFORM, PLDM_GET_STATE_EFFECTER_STATES,
5267 completionCode, comp_effecterCnt,
5268 EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2,
5269 EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}};
5270
5271 decltype(expectedResponseMsg) responseMsg{};
5272
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305273 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005274 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
5275
5276 std::array<get_effecter_state_field, comp_effecterCnt> stateField{
5277 {{EFFECTER_OPER_STATE_ENABLED_NOUPDATEPENDING, 2, 2},
5278 {EFFECTER_OPER_STATE_ENABLED_UPDATEPENDING, 2, 3}}};
5279
5280 struct pldm_get_state_effecter_states_resp resp_fields
5281 {
5282 PLDM_SUCCESS, comp_effecterCnt,
5283 {
5284 stateField[0], stateField[1]
5285 }
5286 };
5287
5288 auto rc = encode_get_state_effecter_states_resp(
5289 0, &resp_fields, response, responseMsg.size() - hdrSize);
5290
5291 EXPECT_EQ(rc, PLDM_SUCCESS);
5292 EXPECT_EQ(expectedResponseMsg, responseMsg);
5293
5294 struct pldm_get_state_effecter_states_resp ret_resp_fields;
5295
5296 rc = decode_get_state_effecter_states_resp(
5297 response, responseMsg.size() - hdrSize, &ret_resp_fields);
5298
5299 EXPECT_EQ(rc, PLDM_SUCCESS);
5300 EXPECT_EQ(completionCode, ret_resp_fields.completion_code);
5301 EXPECT_EQ(comp_effecterCnt, ret_resp_fields.comp_effecter_count);
5302 EXPECT_EQ(stateField[0].effecter_op_state,
5303 ret_resp_fields.field[0].effecter_op_state);
5304 EXPECT_EQ(stateField[0].pending_state,
5305 ret_resp_fields.field[0].pending_state);
5306 EXPECT_EQ(stateField[0].present_state,
5307 ret_resp_fields.field[0].present_state);
5308 EXPECT_EQ(stateField[1].effecter_op_state,
5309 ret_resp_fields.field[1].effecter_op_state);
5310 EXPECT_EQ(stateField[1].pending_state,
5311 ret_resp_fields.field[1].pending_state);
5312 EXPECT_EQ(stateField[1].present_state,
5313 ret_resp_fields.field[1].present_state);
5314
5315 // Test invalid length decode
5316
5317 rc = decode_get_state_effecter_states_resp(
5318 response,
5319 responseMsg.size() - hdrSize + PLDM_GET_EFFECTER_STATE_FIELD_SIZE,
5320 &ret_resp_fields);
5321
Tal Yacobif490a382024-05-31 09:57:36 +03005322 EXPECT_EQ(rc, -EBADMSG);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005323}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005324
Tal Yacobia6fa5552024-05-05 16:57:38 +03005325TEST(GetStateEffecterStates, testBadEncodeResponse)
5326{
5327 struct pldm_get_state_effecter_states_resp resp
5328 {
5329 PLDM_SUCCESS, 0,
5330 {
5331 }
5332 };
5333 auto rc = decode_get_state_effecter_states_resp(nullptr, 0, &resp);
5334
Tal Yacobif490a382024-05-31 09:57:36 +03005335 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005336}
Tal Yacobia6fa5552024-05-05 16:57:38 +03005337
Tal Yacobia6fa5552024-05-05 16:57:38 +03005338TEST(GetStateEffecterStates, testBadDecodeResponse)
5339{
5340 std::array<uint8_t, hdrSize +
5341 PLDM_GET_STATE_EFFECTER_STATES_MIN_RESP_BYTES +
5342 PLDM_GET_EFFECTER_STATE_FIELD_SIZE * 2>
5343 responseMsg{};
5344
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305345 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Tal Yacobia6fa5552024-05-05 16:57:38 +03005346 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
5347
5348 auto rc = decode_get_state_effecter_states_resp(
5349 response, responseMsg.size() - hdrSize, nullptr);
5350
Tal Yacobif490a382024-05-31 09:57:36 +03005351 EXPECT_EQ(rc, -EINVAL);
Tal Yacobia6fa5552024-05-05 16:57:38 +03005352}
Thu Nguyendacfa352024-06-22 09:53:15 +00005353
5354[[maybe_unused]] static size_t str16len(char16_t* startptr)
5355{
5356 char16_t* endptr = startptr;
5357 while (*endptr)
5358 {
5359 endptr++;
5360 }
5361 return endptr - startptr;
5362}
5363
Thu Nguyendacfa352024-06-22 09:53:15 +00005364TEST(decodeEntityAuxNamePdrData, GoodTest)
5365{
5366 std::vector<uint8_t> pdr1{
5367 // Common PDR Header
5368 0x1, 0x0, 0x0, 0x0, // record handle
5369 0x1, // PDRHeaderVersion
5370 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5371 0x1,
5372 0x0, // recordChangeNumber
5373 0x27,
5374 0, // dataLength
5375 /* Entity Auxiliary Names PDR Data*/
5376 3,
5377 0, // entityType system software
5378 0x1,
5379 0x0, // Entity instance number =1
5380 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5381 0, // Overal system
5382 0, // shared Name Count one name only
5383 03, // nameStringCount
5384 0x65, 0x6e, 0x00, // Language Tag "en"
5385 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5386 0x66, 0x6e, 0x00, // Language Tag "en"
5387 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5388 0x67, 0x6e, 0x00, // Language Tag "en"
5389 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5390 };
5391
5392 const char expectTag0[] = {0x65, 0x6e, 0x00};
5393 const char expectTag1[] = {0x66, 0x6e, 0x00};
5394 const char expectTag2[] = {0x67, 0x6e, 0x00};
5395 const char expectName0[] = {0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00};
5396 const char expectName1[] = {0x00, 0x53, 0x00, 0x31, 0x00, 0x00};
5397 const char expectName2[] = {0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00};
5398 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5399 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5400 auto names_size = pdr1.size() - names_offset;
5401 size_t length = 0;
5402
5403 size_t decodedPdrSize =
5404 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5405 auto decodedPdr =
5406 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5407 EXPECT_NE(nullptr, decodedPdr);
5408
5409 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5410 decodedPdr, decodedPdrSize);
5411
5412 EXPECT_EQ(0, rc);
5413 EXPECT_EQ(1, decodedPdr->hdr.record_handle);
5414 EXPECT_EQ(1, decodedPdr->hdr.version);
5415 EXPECT_EQ(PLDM_ENTITY_AUXILIARY_NAMES_PDR, decodedPdr->hdr.type);
5416 EXPECT_EQ(1, decodedPdr->hdr.record_change_num);
5417 EXPECT_EQ(pdr1.size() - sizeof(struct pldm_pdr_hdr),
5418 decodedPdr->hdr.length);
5419 EXPECT_EQ(3, decodedPdr->container.entity_type);
5420 EXPECT_EQ(1, decodedPdr->container.entity_instance_num);
5421 EXPECT_EQ(PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5422 decodedPdr->container.entity_container_id);
5423 EXPECT_EQ(0, decodedPdr->shared_name_count);
5424 EXPECT_EQ(3, decodedPdr->name_string_count);
5425
5426 decodedPdr->names = (struct pldm_entity_auxiliary_name*)calloc(
5427 decodedPdr->name_string_count,
5428 sizeof(struct pldm_entity_auxiliary_name));
5429 EXPECT_NE(nullptr, decodedPdr->names);
5430
5431 rc = decode_pldm_entity_auxiliary_names_pdr_index(decodedPdr);
5432 EXPECT_EQ(0, rc);
5433
5434 length = strlen(decodedPdr->names[0].tag);
5435 EXPECT_EQ(strlen(expectTag0), length);
5436 EXPECT_EQ(strncmp(expectTag0, decodedPdr->names[0].tag, length + 1), 0);
5437
5438 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5439 ASSERT_EQ(0,
5440 (uintptr_t)decodedPdr->names[0].name & (alignof(char16_t) - 1));
5441 // NOLINTEND(clang-analyzer-unix.Malloc)
5442 length = str16len((char16_t*)decodedPdr->names[0].name);
5443 EXPECT_EQ(str16len((char16_t*)expectName0), length);
5444 EXPECT_EQ(3, str16len((char16_t*)expectName0));
5445 EXPECT_EQ(memcmp(expectName0, decodedPdr->names[0].name,
5446 sizeof(char16_t) * (length + 1)),
5447 0);
5448
5449 length = strlen(decodedPdr->names[1].tag);
5450 EXPECT_EQ(strlen(expectTag1), length);
5451 EXPECT_EQ(strncmp(expectTag1, decodedPdr->names[1].tag, length + 1), 0);
5452
5453 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5454 ASSERT_EQ(0,
5455 (uintptr_t)decodedPdr->names[1].name & (alignof(char16_t) - 1));
5456 // NOLINTEND(clang-analyzer-unix.Malloc)
5457 length = str16len((char16_t*)decodedPdr->names[1].name);
5458 EXPECT_EQ(str16len((char16_t*)expectName1), length);
5459 EXPECT_EQ(2, str16len((char16_t*)expectName1));
5460 EXPECT_EQ(memcmp(expectName1, decodedPdr->names[1].name,
5461 sizeof(char16_t) * (length + 1)),
5462 0);
5463
5464 length = strlen(decodedPdr->names[2].tag);
5465 EXPECT_EQ(strlen(expectTag2), length);
5466 EXPECT_EQ(strncmp(expectTag2, decodedPdr->names[2].tag, length + 1), 0);
5467
5468 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
5469 ASSERT_EQ(0,
5470 (uintptr_t)decodedPdr->names[2].name & (alignof(char16_t) - 1));
5471 // NOLINTEND(clang-analyzer-unix.Malloc)
5472 length = str16len((char16_t*)decodedPdr->names[2].name);
5473 EXPECT_EQ(str16len((char16_t*)expectName2), length);
5474 EXPECT_EQ(3, str16len((char16_t*)expectName2));
5475 EXPECT_EQ(memcmp(expectName2, decodedPdr->names[2].name,
5476 sizeof(char16_t) * (length + 1)),
5477 0);
5478
5479 free(decodedPdr->names);
5480 free(decodedPdr);
5481}
Thu Nguyendacfa352024-06-22 09:53:15 +00005482
Thu Nguyendacfa352024-06-22 09:53:15 +00005483TEST(decodeEntityAuxNamePdrData, BadTest)
5484{
5485 std::vector<uint8_t> pdr1{
5486 // Common PDR Header
5487 0x1, 0x0, 0x0, 0x0, // record handle
5488 0x1, // PDRHeaderVersion
5489 PLDM_ENTITY_AUXILIARY_NAMES_PDR, // PDRType
5490 0x1,
5491 0x0, // recordChangeNumber
5492 0x25, // correct size is 0x27, input invalid size
5493 0, // dataLength
5494 /* Entity Auxiliary Names PDR Data*/
5495 3,
5496 0, // entityType system software
5497 0x1,
5498 0x0, // Entity instance number =1
5499 PLDM_PLATFORM_ENTITY_SYSTEM_CONTAINER_ID,
5500 0, // Overal system
5501 0, // shared Name Count one name only
5502 0, // Invalid nameStringCount
5503 0x65, 0x6e, 0x00, // Language Tag "en"
5504 0x00, 0x53, 0x00, 0x30, 0x00, 0x53, 0x00, 0x00, // Entity Name "S0S"
5505 0x66, 0x6e, 0x00, // Language Tag "en"
5506 0x00, 0x53, 0x00, 0x31, 0x00, 0x00, // Entity Name "S1"
5507 0x67, 0x6e, 0x00, // Language Tag "en"
5508 0x00, 0x52, 0x00, 0x52, 0x00, 0x33, 0x00, 0x00 // Entity Name "RR3"
5509 };
5510
5511 auto names_offset = sizeof(struct pldm_pdr_hdr) +
5512 PLDM_PDR_ENTITY_AUXILIARY_NAME_PDR_MIN_LENGTH;
5513 auto names_size = pdr1.size() - names_offset;
5514
5515 size_t decodedPdrSize =
5516 sizeof(struct pldm_entity_auxiliary_names_pdr) + names_size;
5517 auto decodedPdr =
5518 (struct pldm_entity_auxiliary_names_pdr*)malloc(decodedPdrSize);
5519
5520 auto rc = decode_entity_auxiliary_names_pdr(pdr1.data(), pdr1.size(),
5521 decodedPdr, decodedPdrSize);
5522
5523 EXPECT_EQ(-EBADMSG, rc);
5524 free(decodedPdr);
5525}
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005526
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005527TEST(PlatformEventMessage, testGoodCperEventDataDecodeRequest)
5528{
5529 constexpr const size_t eventDataSize = 4;
5530 constexpr const size_t eventSize =
5531 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize;
5532 std::array<uint8_t, eventSize> eventData{
5533 0x1, // format version
5534 0x0, // format type
5535 0x4, 0x0, // event data length
5536 0x44, 0x33, 0x22, 0x11 // data
5537 };
5538
5539 uint8_t expectedFormatVersion = 1;
5540 uint8_t expectedFormatType = 0;
5541 uint16_t expectedEventDataLength = 4;
5542 uint8_t expectCperEventData[] = {0x44, 0x33, 0x22, 0x11};
5543
5544 size_t cperEventSize =
5545 sizeof(struct pldm_platform_cper_event) + eventDataSize;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305546 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005547 auto cper_event = reinterpret_cast<struct pldm_platform_cper_event*>(
5548 malloc(cperEventSize));
5549
Thu Nguyen02903032024-09-03 06:39:50 +00005550 auto rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305551 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005552 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5553 cper_event, cperEventSize);
5554
5555 EXPECT_EQ(rc, 0);
5556 EXPECT_EQ(cper_event->format_version, expectedFormatVersion);
5557 EXPECT_EQ(cper_event->format_type, expectedFormatType);
5558 EXPECT_EQ(cper_event->event_data_length, expectedEventDataLength);
5559
5560 auto cperEventData = pldm_platform_cper_event_event_data(cper_event);
5561 EXPECT_NE(cperEventData, nullptr);
5562 if (cperEventData)
5563 {
5564 EXPECT_EQ(0, memcmp(expectCperEventData, cperEventData,
5565 expectedEventDataLength));
5566 }
5567
5568 free(cper_event);
5569}
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005570
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005571TEST(PlatformEventMessage, testBadCperEventDataDecodeRequest)
5572{
5573
5574 constexpr const size_t eventDataSize = 4;
5575 constexpr const size_t eventSize =
5576 PLDM_PLATFORM_CPER_EVENT_MIN_LENGTH + eventDataSize;
5577 std::array<uint8_t, eventSize> eventData{
5578 0x1, // format version
5579 0x0, // format type
5580 0x4, 0x0, // event data length
5581 0x44, 0x33, 0x22, 0x11 // data
5582 };
5583
5584 size_t cperEventSize =
5585 sizeof(struct pldm_platform_cper_event) + eventDataSize;
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305586 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005587 auto cperEvent = reinterpret_cast<struct pldm_platform_cper_event*>(
5588 malloc(cperEventSize));
5589
Thu Nguyen02903032024-09-03 06:39:50 +00005590 auto rc = decode_pldm_platform_cper_event(NULL, eventData.size(), cperEvent,
5591 cperEventSize);
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005592 EXPECT_EQ(rc, -EINVAL);
5593
Thu Nguyen02903032024-09-03 06:39:50 +00005594 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305595 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005596 reinterpret_cast<const void*>(eventData.data()), eventData.size(), NULL,
5597 cperEventSize);
5598 EXPECT_EQ(rc, -EINVAL);
5599
5600#ifdef NDEBUG
Thu Nguyen02903032024-09-03 06:39:50 +00005601 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305602 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005603 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
5604 cperEvent, cperEventSize);
Andrew Jeffery0a1be3c2024-08-11 08:34:10 +00005605 EXPECT_EQ(rc, -EOVERFLOW);
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005606#else
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305607 EXPECT_DEATH(
5608 decode_pldm_platform_cper_event(
5609 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
5610 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size() - 1,
5611 cperEvent, cperEventSize),
5612 "ctx->remaining >= 0");
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005613#endif
5614
Thu Nguyen02903032024-09-03 06:39:50 +00005615 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305616 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005617 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
5618 cperEvent, cperEventSize - 1);
5619 EXPECT_EQ(rc, -EOVERFLOW);
5620
Thu Nguyen02903032024-09-03 06:39:50 +00005621 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305622 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005623 reinterpret_cast<uint8_t*>(eventData.data()), eventData.size(),
5624 cperEvent, cperEventSize + 1);
5625 EXPECT_EQ(rc, 0);
5626
5627 // Invalid CPER Event Format Type
5628 eventData[1] = 0x2;
Thu Nguyen02903032024-09-03 06:39:50 +00005629 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305630 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005631 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5632 cperEvent, cperEventSize);
5633
5634 EXPECT_EQ(rc, -EPROTO);
5635
5636 // Invalid cper event data size
5637 eventData[1] = 0x1;
5638 eventData[2] = 3;
Thu Nguyen02903032024-09-03 06:39:50 +00005639 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305640 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005641 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5642 cperEvent, cperEventSize);
5643
5644 EXPECT_EQ(rc, -EBADMSG);
5645
5646 eventData[2] = 5;
Thu Nguyen02903032024-09-03 06:39:50 +00005647 rc = decode_pldm_platform_cper_event(
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09305648 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Thu Nguyena5d18dc2024-08-07 08:29:34 +00005649 reinterpret_cast<const void*>(eventData.data()), eventData.size(),
5650 cperEvent, cperEventSize);
5651
5652 EXPECT_EQ(rc, -EOVERFLOW);
5653
5654 free(cperEvent);
5655}