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