blob: fbecd60a0160554416a4323166d4d45d74d31c8a [file] [log] [blame]
Andrew Jefferyb0c1d202023-11-07 22:08:44 +10301#include <libpldm/base.h>
2#include <libpldm/pldm_types.h>
3
Andrew Jeffery9c766792022-08-10 23:12:49 +09304#include <array>
Manojkiran Eda9a8e4972022-11-28 16:38:21 +05305#include <cstdint>
Andrew Jeffery9c766792022-08-10 23:12:49 +09306#include <cstring>
7#include <vector>
8
Dung Cao88367842024-10-16 06:32:49 +00009#include "msgbuf.h"
10
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
14using testing::ElementsAreArray;
15
16constexpr auto hdrSize = sizeof(pldm_msg_hdr);
17
18TEST(PackPLDMMessage, BadPathTest)
19{
20 struct pldm_header_info hdr;
21 struct pldm_header_info* hdr_ptr = NULL;
22 pldm_msg_hdr msg{};
23
24 // PLDM header information pointer is NULL
25 auto rc = pack_pldm_header(hdr_ptr, &msg);
26 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
27
28 // PLDM message pointer is NULL
29 rc = pack_pldm_header(&hdr, nullptr);
30 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
31
32 // PLDM header information pointer and PLDM message pointer is NULL
33 rc = pack_pldm_header(hdr_ptr, nullptr);
34 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
35
36 // RESERVED message type
37 hdr.msg_type = PLDM_RESERVED;
38 rc = pack_pldm_header(&hdr, &msg);
39 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
40
41 // Instance ID out of range
42 hdr.msg_type = PLDM_REQUEST;
43 hdr.instance = 32;
44 rc = pack_pldm_header(&hdr, &msg);
45 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
46
47 // PLDM type out of range
48 hdr.msg_type = PLDM_REQUEST;
49 hdr.instance = 31;
50 hdr.pldm_type = 64;
51 rc = pack_pldm_header(&hdr, &msg);
52 EXPECT_EQ(rc, PLDM_ERROR_INVALID_PLDM_TYPE);
53}
54
55TEST(PackPLDMMessage, RequestMessageGoodPath)
56{
57 struct pldm_header_info hdr;
58 pldm_msg_hdr msg{};
59
60 // Message type is REQUEST and lower range of the field values
61 hdr.msg_type = PLDM_REQUEST;
62 hdr.instance = 0;
63 hdr.pldm_type = 0;
64 hdr.command = 0;
65
66 auto rc = pack_pldm_header(&hdr, &msg);
67 EXPECT_EQ(rc, PLDM_SUCCESS);
68 EXPECT_EQ(msg.request, 1);
69 EXPECT_EQ(msg.datagram, 0);
70 EXPECT_EQ(msg.instance_id, 0);
71 EXPECT_EQ(msg.type, 0);
72 EXPECT_EQ(msg.command, 0);
73
74 // Message type is REQUEST and upper range of the field values
75 hdr.instance = 31;
76 hdr.pldm_type = 63;
77 hdr.command = 255;
78
79 rc = pack_pldm_header(&hdr, &msg);
80 EXPECT_EQ(rc, PLDM_SUCCESS);
81 EXPECT_EQ(msg.request, 1);
82 EXPECT_EQ(msg.datagram, 0);
83 EXPECT_EQ(msg.instance_id, 31);
84 EXPECT_EQ(msg.type, 63);
85 EXPECT_EQ(msg.command, 255);
86
87 // Message type is PLDM_ASYNC_REQUEST_NOTIFY
88 hdr.msg_type = PLDM_ASYNC_REQUEST_NOTIFY;
89
90 rc = pack_pldm_header(&hdr, &msg);
91 EXPECT_EQ(rc, PLDM_SUCCESS);
92 EXPECT_EQ(msg.request, 1);
93 EXPECT_EQ(msg.datagram, 1);
94 EXPECT_EQ(msg.instance_id, 31);
95 EXPECT_EQ(msg.type, 63);
96 EXPECT_EQ(msg.command, 255);
97}
98
99TEST(PackPLDMMessage, ResponseMessageGoodPath)
100{
101 struct pldm_header_info hdr;
102 pldm_msg_hdr msg{};
103
104 // Message type is PLDM_RESPONSE and lower range of the field values
105 hdr.msg_type = PLDM_RESPONSE;
106 hdr.instance = 0;
107 hdr.pldm_type = 0;
108 hdr.command = 0;
109
110 auto rc = pack_pldm_header(&hdr, &msg);
111 EXPECT_EQ(rc, PLDM_SUCCESS);
112 EXPECT_EQ(msg.request, 0);
113 EXPECT_EQ(msg.datagram, 0);
114 EXPECT_EQ(msg.instance_id, 0);
115 EXPECT_EQ(msg.type, 0);
116 EXPECT_EQ(msg.command, 0);
117
118 // Message type is PLDM_RESPONSE and upper range of the field values
119 hdr.instance = 31;
120 hdr.pldm_type = 63;
121 hdr.command = 255;
122
123 rc = pack_pldm_header(&hdr, &msg);
124 EXPECT_EQ(rc, PLDM_SUCCESS);
125 EXPECT_EQ(msg.request, 0);
126 EXPECT_EQ(msg.datagram, 0);
127 EXPECT_EQ(msg.instance_id, 31);
128 EXPECT_EQ(msg.type, 63);
129 EXPECT_EQ(msg.command, 255);
130}
131
132TEST(UnpackPLDMMessage, BadPathTest)
133{
134 struct pldm_header_info hdr;
135
136 // PLDM message pointer is NULL
137 auto rc = unpack_pldm_header(nullptr, &hdr);
138 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
139}
140
141TEST(UnpackPLDMMessage, RequestMessageGoodPath)
142{
143 struct pldm_header_info hdr;
144 pldm_msg_hdr msg{};
145
146 // Unpack PLDM request message and lower range of field values
147 msg.request = 1;
148 auto rc = unpack_pldm_header(&msg, &hdr);
149 EXPECT_EQ(rc, PLDM_SUCCESS);
150 EXPECT_EQ(hdr.msg_type, PLDM_REQUEST);
151 EXPECT_EQ(hdr.instance, 0);
152 EXPECT_EQ(hdr.pldm_type, 0);
153 EXPECT_EQ(hdr.command, 0);
154
155 // Unpack PLDM async request message and lower range of field values
156 msg.datagram = 1;
157 rc = unpack_pldm_header(&msg, &hdr);
158 EXPECT_EQ(rc, PLDM_SUCCESS);
159 EXPECT_EQ(hdr.msg_type, PLDM_ASYNC_REQUEST_NOTIFY);
160
161 // Unpack PLDM request message and upper range of field values
162 msg.datagram = 0;
163 msg.instance_id = 31;
164 msg.type = 63;
165 msg.command = 255;
166 rc = unpack_pldm_header(&msg, &hdr);
167 EXPECT_EQ(rc, PLDM_SUCCESS);
168 EXPECT_EQ(hdr.msg_type, PLDM_REQUEST);
169 EXPECT_EQ(hdr.instance, 31);
170 EXPECT_EQ(hdr.pldm_type, 63);
171 EXPECT_EQ(hdr.command, 255);
172}
173
174TEST(UnpackPLDMMessage, ResponseMessageGoodPath)
175{
176 struct pldm_header_info hdr;
177 pldm_msg_hdr msg{};
178
179 // Unpack PLDM response message and lower range of field values
180 auto rc = unpack_pldm_header(&msg, &hdr);
181 EXPECT_EQ(rc, PLDM_SUCCESS);
182 EXPECT_EQ(hdr.msg_type, PLDM_RESPONSE);
183 EXPECT_EQ(hdr.instance, 0);
184 EXPECT_EQ(hdr.pldm_type, 0);
185 EXPECT_EQ(hdr.command, 0);
186
187 // Unpack PLDM response message and upper range of field values
188 msg.instance_id = 31;
189 msg.type = 63;
190 msg.command = 255;
191 rc = unpack_pldm_header(&msg, &hdr);
192 EXPECT_EQ(rc, PLDM_SUCCESS);
193 EXPECT_EQ(hdr.msg_type, PLDM_RESPONSE);
194 EXPECT_EQ(hdr.instance, 31);
195 EXPECT_EQ(hdr.pldm_type, 63);
196 EXPECT_EQ(hdr.command, 255);
197}
198
199TEST(GetPLDMCommands, testEncodeRequest)
200{
201 uint8_t pldmType = 0x05;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600202 ver32_t version{0xff, 0xff, 0xff, 0xff};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930203 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES>
204 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930205 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930206 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
207
208 auto rc = encode_get_commands_req(0, pldmType, version, request);
209 EXPECT_EQ(rc, PLDM_SUCCESS);
210 EXPECT_EQ(0, memcmp(request->payload, &pldmType, sizeof(pldmType)));
211 EXPECT_EQ(0, memcmp(request->payload + sizeof(pldmType), &version,
212 sizeof(version)));
213}
214
215TEST(GetPLDMCommands, testDecodeRequest)
216{
217 uint8_t pldmType = 0x05;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600218 ver32_t version{0xff, 0xff, 0xff, 0xff};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930219 uint8_t pldmTypeOut{};
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600220 ver32_t versionOut{0xff, 0xff, 0xff, 0xff};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930221 std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{};
222
223 memcpy(requestMsg.data() + hdrSize, &pldmType, sizeof(pldmType));
224 memcpy(requestMsg.data() + sizeof(pldmType) + hdrSize, &version,
225 sizeof(version));
226
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930227 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930228 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
229 auto rc = decode_get_commands_req(request, requestMsg.size() - hdrSize,
230 &pldmTypeOut, &versionOut);
231
232 EXPECT_EQ(rc, PLDM_SUCCESS);
233 EXPECT_EQ(pldmTypeOut, pldmType);
234 EXPECT_EQ(0, memcmp(&versionOut, &version, sizeof(version)));
235}
236
237TEST(GetPLDMCommands, testEncodeResponse)
238{
239 uint8_t completionCode = 0;
240 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES>
241 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930242 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930243 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
244 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> commands{};
245 commands[0].byte = 1;
246 commands[1].byte = 2;
247 commands[2].byte = 3;
248
249 auto rc =
250 encode_get_commands_resp(0, PLDM_SUCCESS, commands.data(), response);
251 EXPECT_EQ(rc, PLDM_SUCCESS);
252 uint8_t* payload_ptr = response->payload;
253 EXPECT_EQ(completionCode, payload_ptr[0]);
254 EXPECT_EQ(1, payload_ptr[sizeof(completionCode)]);
255 EXPECT_EQ(2,
256 payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte)]);
257 EXPECT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte) +
258 sizeof(commands[1].byte)]);
259}
260
261TEST(GetPLDMTypes, testEncodeResponse)
262{
263 uint8_t completionCode = 0;
264 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES>
265 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930266 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930267 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
268 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> types{};
269 types[0].byte = 1;
270 types[1].byte = 2;
271 types[2].byte = 3;
272
273 auto rc = encode_get_types_resp(0, PLDM_SUCCESS, types.data(), response);
274 EXPECT_EQ(rc, PLDM_SUCCESS);
275 uint8_t* payload_ptr = response->payload;
276 EXPECT_EQ(completionCode, payload_ptr[0]);
277 EXPECT_EQ(1, payload_ptr[sizeof(completionCode)]);
278 EXPECT_EQ(2, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte)]);
279 EXPECT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte) +
280 sizeof(types[1].byte)]);
281}
282
283TEST(GetPLDMTypes, testGoodDecodeResponse)
284{
285 std::array<uint8_t, hdrSize + PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
286 responseMsg[1 + hdrSize] = 1;
287 responseMsg[2 + hdrSize] = 2;
288 responseMsg[3 + hdrSize] = 3;
289 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{};
290
291 uint8_t completion_code;
292 responseMsg[hdrSize] = PLDM_SUCCESS;
293
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930295 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
296
297 auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize,
298 &completion_code, outTypes.data());
299
300 EXPECT_EQ(rc, PLDM_SUCCESS);
301 EXPECT_EQ(completion_code, PLDM_SUCCESS);
302 EXPECT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
303 EXPECT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
304 EXPECT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
305}
306
307TEST(GetPLDMTypes, testBadDecodeResponse)
308{
309 std::array<uint8_t, hdrSize + PLDM_GET_TYPES_RESP_BYTES> responseMsg{};
310 responseMsg[1 + hdrSize] = 1;
311 responseMsg[2 + hdrSize] = 2;
312 responseMsg[3 + hdrSize] = 3;
313 std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{};
314
315 uint8_t retcompletion_code = 0;
316 responseMsg[hdrSize] = PLDM_SUCCESS;
317
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930318 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930319 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
320
321 auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize - 1,
322 &retcompletion_code, outTypes.data());
323
324 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
325}
326
327TEST(GetPLDMCommands, testGoodDecodeResponse)
328{
329 std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
330 responseMsg[1 + hdrSize] = 1;
331 responseMsg[2 + hdrSize] = 2;
332 responseMsg[3 + hdrSize] = 3;
333 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{};
334
335 uint8_t completion_code;
336 responseMsg[hdrSize] = PLDM_SUCCESS;
337
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930338 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930339 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
340
341 auto rc = decode_get_commands_resp(response, responseMsg.size() - hdrSize,
342 &completion_code, outTypes.data());
343
344 EXPECT_EQ(rc, PLDM_SUCCESS);
345 EXPECT_EQ(completion_code, PLDM_SUCCESS);
346 EXPECT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte);
347 EXPECT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte);
348 EXPECT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte);
349}
350
351TEST(GetPLDMCommands, testBadDecodeResponse)
352{
353 std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{};
354 responseMsg[1 + hdrSize] = 1;
355 responseMsg[2 + hdrSize] = 2;
356 responseMsg[3 + hdrSize] = 3;
357 std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{};
358
359 uint8_t retcompletion_code = 0;
360 responseMsg[hdrSize] = PLDM_SUCCESS;
361
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930362 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930363 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
364
365 auto rc =
366 decode_get_commands_resp(response, responseMsg.size() - hdrSize - 1,
367 &retcompletion_code, outTypes.data());
368
369 EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
370}
371
372TEST(GetPLDMVersion, testGoodEncodeRequest)
373{
374 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES>
375 requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930376 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930377 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
378 uint8_t pldmType = 0x03;
379 uint32_t transferHandle = 0x0;
380 uint8_t opFlag = 0x01;
381
382 auto rc =
383 encode_get_version_req(0, transferHandle, opFlag, pldmType, request);
384 EXPECT_EQ(rc, PLDM_SUCCESS);
385 EXPECT_EQ(
386 0, memcmp(request->payload, &transferHandle, sizeof(transferHandle)));
387 EXPECT_EQ(0, memcmp(request->payload + sizeof(transferHandle), &opFlag,
388 sizeof(opFlag)));
389 EXPECT_EQ(0,
390 memcmp(request->payload + sizeof(transferHandle) + sizeof(opFlag),
391 &pldmType, sizeof(pldmType)));
392}
393
394TEST(GetPLDMVersion, testBadEncodeRequest)
395{
396 uint8_t pldmType = 0x03;
397 uint32_t transferHandle = 0x0;
398 uint8_t opFlag = 0x01;
399
400 auto rc =
401 encode_get_version_req(0, transferHandle, opFlag, pldmType, nullptr);
402
403 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
404}
405
406TEST(GetPLDMVersion, testEncodeResponse)
407{
408 uint8_t completionCode = 0;
409 uint32_t transferHandle = 0;
410 uint8_t flag = PLDM_START_AND_END;
411 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
412 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930413 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930414 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600415 ver32_t version = {0xff, 0xff, 0xff, 0xff};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930416
417 auto rc = encode_get_version_resp(0, PLDM_SUCCESS, 0, PLDM_START_AND_END,
418 &version, sizeof(ver32_t), response);
419
420 EXPECT_EQ(rc, PLDM_SUCCESS);
421 EXPECT_EQ(completionCode, response->payload[0]);
422 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
423 &transferHandle, sizeof(transferHandle)));
424 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
425 sizeof(transferHandle),
426 &flag, sizeof(flag)));
427 EXPECT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
428 sizeof(transferHandle) + sizeof(flag),
429 &version, sizeof(version)));
430}
431
432TEST(GetPLDMVersion, testDecodeRequest)
433{
434 std::array<uint8_t, hdrSize + PLDM_GET_VERSION_REQ_BYTES> requestMsg{};
435 uint32_t transferHandle = 0x0;
436 uint32_t retTransferHandle = 0x0;
437 uint8_t flag = PLDM_GET_FIRSTPART;
438 uint8_t retFlag = PLDM_GET_FIRSTPART;
439 uint8_t pldmType = PLDM_BASE;
440 uint8_t retType = PLDM_BASE;
441
442 memcpy(requestMsg.data() + hdrSize, &transferHandle,
443 sizeof(transferHandle));
444 memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize, &flag,
445 sizeof(flag));
446 memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag) + hdrSize,
447 &pldmType, sizeof(pldmType));
448
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930449 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930450 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
451
452 auto rc = decode_get_version_req(request, requestMsg.size() - hdrSize,
453 &retTransferHandle, &retFlag, &retType);
454
455 EXPECT_EQ(rc, PLDM_SUCCESS);
456 EXPECT_EQ(transferHandle, retTransferHandle);
457 EXPECT_EQ(flag, retFlag);
458 EXPECT_EQ(pldmType, retType);
459}
460
461TEST(GetPLDMVersion, testDecodeResponse)
462{
463 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES>
464 responseMsg{};
465 uint32_t transferHandle = 0x0;
466 uint32_t retTransferHandle = 0x0;
467 uint8_t flag = PLDM_START_AND_END;
468 uint8_t retFlag = PLDM_START_AND_END;
469 uint8_t completionCode = 0;
Pavithra Barithayadc7d3b52024-02-06 23:46:49 -0600470 ver32_t version = {0xff, 0xff, 0xff, 0xff};
Andrew Jeffery9c766792022-08-10 23:12:49 +0930471 ver32_t versionOut;
472 uint8_t completion_code;
473
474 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize,
475 &transferHandle, sizeof(transferHandle));
476 memcpy(responseMsg.data() + sizeof(completionCode) +
477 sizeof(transferHandle) + hdrSize,
478 &flag, sizeof(flag));
479 memcpy(responseMsg.data() + sizeof(completionCode) +
480 sizeof(transferHandle) + sizeof(flag) + hdrSize,
481 &version, sizeof(version));
482
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930483 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930484 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
485
486 auto rc = decode_get_version_resp(response, responseMsg.size() - hdrSize,
487 &completion_code, &retTransferHandle,
488 &retFlag, &versionOut);
489 EXPECT_EQ(rc, PLDM_SUCCESS);
490 EXPECT_EQ(transferHandle, retTransferHandle);
491 EXPECT_EQ(flag, retFlag);
492
493 EXPECT_EQ(versionOut.major, version.major);
494 EXPECT_EQ(versionOut.minor, version.minor);
495 EXPECT_EQ(versionOut.update, version.update);
496 EXPECT_EQ(versionOut.alpha, version.alpha);
497}
498
499TEST(GetTID, testEncodeRequest)
500{
501 pldm_msg request{};
502
503 auto rc = encode_get_tid_req(0, &request);
504 ASSERT_EQ(rc, PLDM_SUCCESS);
505}
506
507TEST(GetTID, testEncodeResponse)
508{
509 uint8_t completionCode = 0;
510 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES>
511 responseMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930512 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930513 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
514 uint8_t tid = 1;
515
516 auto rc = encode_get_tid_resp(0, PLDM_SUCCESS, tid, response);
517 EXPECT_EQ(rc, PLDM_SUCCESS);
518 uint8_t* payload = response->payload;
519 EXPECT_EQ(completionCode, payload[0]);
520 EXPECT_EQ(1, payload[sizeof(completionCode)]);
521}
522
523TEST(GetTID, testDecodeResponse)
524{
525 std::array<uint8_t, hdrSize + PLDM_GET_TID_RESP_BYTES> responseMsg{};
526 responseMsg[1 + hdrSize] = 1;
527
528 uint8_t tid;
529 uint8_t completion_code;
530 responseMsg[hdrSize] = PLDM_SUCCESS;
531
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +0930532 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930533 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
534
535 auto rc = decode_get_tid_resp(response, responseMsg.size() - hdrSize,
536 &completion_code, &tid);
537
538 EXPECT_EQ(rc, PLDM_SUCCESS);
539 EXPECT_EQ(completion_code, PLDM_SUCCESS);
540 EXPECT_EQ(tid, 1);
541}
542
Andrew Jefferyedb88262025-08-14 05:02:21 +0000543TEST(DecodeMultipartReceiveResponse, testDecodeRequestPass)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930544{
545 constexpr uint8_t kPldmType = PLDM_BASE;
546 constexpr uint8_t kFlag = PLDM_XFER_FIRST_PART;
547 constexpr uint32_t kTransferCtx = 0x01;
548 constexpr uint32_t kTransferHandle = 0x10;
549 constexpr uint32_t kSectionOffset = 0x0;
550 constexpr uint32_t kSectionLength = 0x10;
Andrew Jefferyedb88262025-08-14 05:02:21 +0000551
552 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
553 PLDM_MSGBUF_DEFINE_P(buf);
554 int rc;
555
556 // Header values don't matter for this test.
557 rc = pldm_msgbuf_init_errno(buf, PLDM_MULTIPART_RECEIVE_REQ_BYTES,
558 msg->payload, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
559 ASSERT_EQ(rc, 0);
560 pldm_msgbuf_insert_uint8(buf, kPldmType);
561 pldm_msgbuf_insert_uint8(buf, kFlag);
562 pldm_msgbuf_insert_uint32(buf, kTransferCtx);
563 pldm_msgbuf_insert_uint32(buf, kTransferHandle);
564 pldm_msgbuf_insert_uint32(buf, kSectionOffset);
565 pldm_msgbuf_insert_uint32(buf, kSectionLength);
566 rc = pldm_msgbuf_complete(buf);
567 ASSERT_EQ(rc, 0);
568
569 uint8_t pldm_type;
570 uint8_t flag;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930571 uint32_t transfer_ctx;
572 uint32_t transfer_handle;
573 uint32_t section_offset;
574 uint32_t section_length;
Andrew Jefferyedb88262025-08-14 05:02:21 +0000575 rc = decode_multipart_receive_req(
576 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag, &transfer_ctx,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930577 &transfer_handle, &section_offset, &section_length);
578
Andrew Jefferyedb88262025-08-14 05:02:21 +0000579 ASSERT_EQ(rc, PLDM_SUCCESS);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930580 EXPECT_EQ(pldm_type, kPldmType);
581 EXPECT_EQ(flag, kFlag);
582 EXPECT_EQ(transfer_ctx, kTransferCtx);
583 EXPECT_EQ(transfer_handle, kTransferHandle);
584 EXPECT_EQ(section_offset, kSectionOffset);
585 EXPECT_EQ(section_length, kSectionLength);
586}
587
Andrew Jefferyedb88262025-08-14 05:02:21 +0000588TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailNullData)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930589{
590 EXPECT_EQ(decode_multipart_receive_req(NULL, 0, NULL, NULL, NULL, NULL,
591 NULL, NULL),
592 PLDM_ERROR_INVALID_DATA);
593}
594
Andrew Jefferyedb88262025-08-14 05:02:21 +0000595TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadLength)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930596{
Andrew Jefferyedb88262025-08-14 05:02:21 +0000597 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930598 uint8_t pldm_type;
599 uint8_t flag;
600 uint32_t transfer_ctx;
601 uint32_t transfer_handle;
602 uint32_t section_offset;
603 uint32_t section_length;
604
Andrew Jefferyedb88262025-08-14 05:02:21 +0000605 memset(msg, 0, PLDM_MSG_SIZE(PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1));
Andrew Jeffery9c766792022-08-10 23:12:49 +0930606 EXPECT_EQ(decode_multipart_receive_req(
Andrew Jefferyedb88262025-08-14 05:02:21 +0000607 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1, &pldm_type, &flag,
Andrew Jeffery9c766792022-08-10 23:12:49 +0930608 &transfer_ctx, &transfer_handle, &section_offset,
609 &section_length),
Andrew Jeffery9c766792022-08-10 23:12:49 +0930610 PLDM_ERROR_INVALID_DATA);
611}
612
Andrew Jefferyedb88262025-08-14 05:02:21 +0000613TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadPldmType)
Andrew Jeffery9c766792022-08-10 23:12:49 +0930614{
Andrew Jefferyedb88262025-08-14 05:02:21 +0000615 constexpr uint8_t kPldmType = 0xff;
616 constexpr uint8_t kFlag = PLDM_XFER_FIRST_PART;
617
618 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
619 PLDM_MSGBUF_DEFINE_P(buf);
620 int rc;
621
622 // Header values don't matter for this test.
623 rc = pldm_msgbuf_init_errno(buf, PLDM_MULTIPART_RECEIVE_REQ_BYTES,
624 msg->payload, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
625 ASSERT_EQ(rc, 0);
626 pldm_msgbuf_insert_uint8(buf, kPldmType);
627 pldm_msgbuf_insert_uint8(buf, kFlag);
628 rc = pldm_msgbuf_complete(buf);
629 ASSERT_EQ(rc, 0);
630
Andrew Jeffery9c766792022-08-10 23:12:49 +0930631 uint8_t pldm_type;
632 uint8_t flag;
633 uint32_t transfer_ctx;
634 uint32_t transfer_handle;
635 uint32_t section_offset;
636 uint32_t section_length;
637
Andrew Jefferyedb88262025-08-14 05:02:21 +0000638 EXPECT_EQ(decode_multipart_receive_req(
639 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag,
640 &transfer_ctx, &transfer_handle, &section_offset,
641 &section_length),
642 PLDM_ERROR_INVALID_PLDM_TYPE);
643}
644
645TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadTransferFlag)
646{
647 constexpr uint8_t kPldmType = PLDM_BASE;
648 constexpr uint8_t kFlag = PLDM_XFER_CURRENT_PART + 0x10;
649
650 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
651 PLDM_MSGBUF_DEFINE_P(buf);
652 int rc;
653
Andrew Jeffery9c766792022-08-10 23:12:49 +0930654 // Header values don't matter for this test.
Andrew Jefferyedb88262025-08-14 05:02:21 +0000655 rc = pldm_msgbuf_init_errno(buf, PLDM_MULTIPART_RECEIVE_REQ_BYTES,
656 msg->payload, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
657 ASSERT_EQ(rc, 0);
658 pldm_msgbuf_insert_uint8(buf, kPldmType);
659 pldm_msgbuf_insert_uint8(buf, kFlag);
660 rc = pldm_msgbuf_complete(buf);
661 ASSERT_EQ(rc, 0);
Andrew Jeffery9c766792022-08-10 23:12:49 +0930662
Andrew Jefferyedb88262025-08-14 05:02:21 +0000663 uint8_t pldm_type;
664 uint8_t flag;
665 uint32_t transfer_ctx;
666 uint32_t transfer_handle;
667 uint32_t section_offset;
668 uint32_t section_length;
Andrew Jeffery9c766792022-08-10 23:12:49 +0930669
Andrew Jefferyedb88262025-08-14 05:02:21 +0000670 EXPECT_EQ(decode_multipart_receive_req(
671 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag,
672 &transfer_ctx, &transfer_handle, &section_offset,
673 &section_length),
674 PLDM_ERROR_UNEXPECTED_TRANSFER_FLAG_OPERATION);
675}
676
677TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadOffset)
678{
679 constexpr uint8_t kPldmType = PLDM_BASE;
680 constexpr uint8_t kFlag = PLDM_XFER_NEXT_PART;
681 constexpr uint32_t kTransferCtx = 0x01;
682 constexpr uint32_t kTransferHandle = 0x01;
683 constexpr uint32_t kSectionOffset = 0x0;
684
685 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
686 PLDM_MSGBUF_DEFINE_P(buf);
687 int rc;
688
689 // Header values don't matter for this test.
690 rc = pldm_msgbuf_init_errno(buf, PLDM_MULTIPART_RECEIVE_REQ_BYTES,
691 msg->payload, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
692 ASSERT_EQ(rc, 0);
693 pldm_msgbuf_insert_uint8(buf, kPldmType);
694 pldm_msgbuf_insert_uint8(buf, kFlag);
695 pldm_msgbuf_insert_uint32(buf, kTransferCtx);
696 pldm_msgbuf_insert_uint32(buf, kTransferHandle);
697 pldm_msgbuf_insert_uint32(buf, kSectionOffset);
698 rc = pldm_msgbuf_complete(buf);
699 ASSERT_EQ(rc, 0);
700
701 uint8_t pldm_type;
702 uint8_t flag;
703 uint32_t transfer_ctx;
704 uint32_t transfer_handle;
705 uint32_t section_offset;
706 uint32_t section_length;
707 EXPECT_EQ(decode_multipart_receive_req(
708 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag,
709 &transfer_ctx, &transfer_handle, &section_offset,
710 &section_length),
711 PLDM_ERROR_INVALID_DATA);
712}
713
714TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadHandle)
715{
716 constexpr uint8_t kPldmType = PLDM_BASE;
717 constexpr uint8_t kFlag = PLDM_XFER_NEXT_PART;
718 constexpr uint32_t kTransferCtx = 0x01;
719 constexpr uint32_t kTransferHandle = 0x0;
720 constexpr uint32_t kSectionOffset = 0x100;
721
722 PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
723 PLDM_MSGBUF_DEFINE_P(buf);
724 int rc;
725
726 // Header values don't matter for this test.
727 rc = pldm_msgbuf_init_errno(buf, PLDM_MULTIPART_RECEIVE_REQ_BYTES,
728 msg->payload, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
729 ASSERT_EQ(rc, 0);
730 pldm_msgbuf_insert_uint8(buf, kPldmType);
731 pldm_msgbuf_insert_uint8(buf, kFlag);
732 pldm_msgbuf_insert_uint32(buf, kTransferCtx);
733 pldm_msgbuf_insert_uint32(buf, kTransferHandle);
734 pldm_msgbuf_insert_uint32(buf, kSectionOffset);
735 rc = pldm_msgbuf_complete(buf);
736 ASSERT_EQ(rc, 0);
737
738 uint8_t pldm_type;
739 uint8_t flag;
740 uint32_t transfer_ctx;
741 uint32_t transfer_handle;
742 uint32_t section_offset;
743 uint32_t section_length;
744 EXPECT_EQ(decode_multipart_receive_req(
745 msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag,
746 &transfer_ctx, &transfer_handle, &section_offset,
747 &section_length),
Andrew Jeffery9c766792022-08-10 23:12:49 +0930748 PLDM_ERROR_INVALID_DATA);
749}
750
Dung Cao88367842024-10-16 06:32:49 +0000751#ifdef LIBPLDM_API_TESTING
752TEST(EncodeMultipartReceiveRequest, GoodTest)
753{
754 uint8_t instance_id = 0;
755
Chau Ly41846232025-08-13 04:43:27 +0000756 const struct pldm_base_multipart_receive_req req_data = {
Dung Cao88367842024-10-16 06:32:49 +0000757 PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10};
758
759 std::array<uint8_t, PLDM_MULTIPART_RECEIVE_REQ_BYTES> requestMsg = {
760 PLDM_BASE, PLDM_XFER_FIRST_PART,
761 0x01, 0x00,
762 0x00, 0x00,
763 0x10, 0x00,
764 0x00, 0x00,
765 0x00, 0x00,
766 0x00, 0x00,
767 0x10, 0x00,
768 0x00, 0x00};
769
770 PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
Chau Ly41846232025-08-13 04:43:27 +0000771 auto rc = encode_pldm_base_multipart_receive_req(
Dung Cao88367842024-10-16 06:32:49 +0000772 instance_id, &req_data, requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
773
774 ASSERT_EQ(rc, 0);
775 EXPECT_EQ(
776 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
777}
778#endif
779
780#ifdef LIBPLDM_API_TESTING
781TEST(EncodeMultipartReceiveRequest, BadTestUnAllocatedPtrParams)
782{
783 uint8_t instance_id = 0;
784 int rc;
785
Chau Ly41846232025-08-13 04:43:27 +0000786 const struct pldm_base_multipart_receive_req req_data = {
Dung Cao88367842024-10-16 06:32:49 +0000787 PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10};
788
789 PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
Chau Ly41846232025-08-13 04:43:27 +0000790 rc = encode_pldm_base_multipart_receive_req(
791 instance_id, nullptr, requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
Dung Cao88367842024-10-16 06:32:49 +0000792 EXPECT_EQ(rc, -EINVAL);
793
Chau Ly41846232025-08-13 04:43:27 +0000794 rc = encode_pldm_base_multipart_receive_req(
795 instance_id, &req_data, nullptr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
Dung Cao88367842024-10-16 06:32:49 +0000796 EXPECT_EQ(rc, -EINVAL);
797}
798#endif
799
800#ifdef LIBPLDM_API_TESTING
801TEST(EncodeMultipartReceiveRequest, BadTestInvalidExpectedOutputMsgLength)
802{
803 uint8_t instance_id = 0;
804 int rc;
805
Chau Ly41846232025-08-13 04:43:27 +0000806 const struct pldm_base_multipart_receive_req req_data = {
Dung Cao88367842024-10-16 06:32:49 +0000807 PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10};
808
809 PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES);
810
Chau Ly41846232025-08-13 04:43:27 +0000811 rc = encode_pldm_base_multipart_receive_req(instance_id, &req_data,
812 requestPtr, 1);
Dung Cao88367842024-10-16 06:32:49 +0000813 EXPECT_EQ(rc, -EOVERFLOW);
814}
815#endif
816
817#ifdef LIBPLDM_API_TESTING
818TEST(DecodeMultipartReceiveResponse, GoodTest)
819{
820 uint8_t completionCode = PLDM_SUCCESS;
821 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END;
822 uint32_t nextDataTransferHandle = 0x15;
823 static constexpr const uint32_t dataLength = 9;
824 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
825 uint32_t dataIntegrityChecksum = 0x3C;
826
Chau Ly41846232025-08-13 04:43:27 +0000827 struct pldm_base_multipart_receive_resp resp_data = {};
Dung Cao88367842024-10-16 06:32:49 +0000828
829 PLDM_MSGBUF_DEFINE_P(buf);
830 int rc;
831
832 static constexpr const size_t payload_length =
833 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength +
834 sizeof(dataIntegrityChecksum);
835 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
836
837 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
838 ASSERT_EQ(rc, 0);
839
840 pldm_msgbuf_insert_uint8(buf, completionCode);
841 pldm_msgbuf_insert_uint8(buf, transferFlag);
842 pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle);
843 pldm_msgbuf_insert_uint32(buf, dataLength);
844 rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(),
845 dataLength);
846 EXPECT_EQ(rc, 0);
847 pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum);
848
849 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
850
851 uint32_t respDataIntegrityChecksum = 0;
852
Chau Ly41846232025-08-13 04:43:27 +0000853 rc = decode_pldm_base_multipart_receive_resp(
Dung Cao88367842024-10-16 06:32:49 +0000854 responseMsg, payload_length, &resp_data, &respDataIntegrityChecksum);
855
856 ASSERT_EQ(rc, 0);
857 EXPECT_EQ(resp_data.completion_code, completionCode);
858 EXPECT_EQ(resp_data.transfer_flag, transferFlag);
859 EXPECT_EQ(resp_data.next_transfer_handle, nextDataTransferHandle);
860 EXPECT_EQ(resp_data.data.length, dataLength);
861 EXPECT_EQ(0,
862 memcmp(data.data(), resp_data.data.ptr, resp_data.data.length));
863 EXPECT_EQ(respDataIntegrityChecksum, dataIntegrityChecksum);
864}
865#endif
866
867#ifdef LIBPLDM_API_TESTING
868TEST(DecodeMultipartReceiveResponse, BadTestUnAllocatedPtrParams)
869{
870 uint8_t completionCode = PLDM_SUCCESS;
871 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END;
872 uint32_t nextDataTransferHandle = 0x15;
873 static constexpr const uint32_t dataLength = 9;
874 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
875 uint32_t dataIntegrityChecksum = 0x3C;
876
Chau Ly41846232025-08-13 04:43:27 +0000877 struct pldm_base_multipart_receive_resp resp_data = {};
Dung Cao88367842024-10-16 06:32:49 +0000878
879 PLDM_MSGBUF_DEFINE_P(buf);
880 int rc;
881
882 static constexpr const size_t payload_length =
883 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength +
884 sizeof(dataIntegrityChecksum);
885 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
886
887 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
888 ASSERT_EQ(rc, 0);
889
890 pldm_msgbuf_insert_uint8(buf, completionCode);
891 pldm_msgbuf_insert_uint8(buf, transferFlag);
892 pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle);
893 pldm_msgbuf_insert_uint32(buf, dataLength);
894 rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(),
895 dataLength);
896 EXPECT_EQ(rc, 0);
897 pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum);
898
899 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
900
901 uint32_t respDataIntegrityChecksum = 0;
902
Chau Ly41846232025-08-13 04:43:27 +0000903 rc = decode_pldm_base_multipart_receive_resp(
904 nullptr, payload_length, &resp_data, &respDataIntegrityChecksum);
Dung Cao88367842024-10-16 06:32:49 +0000905
906 EXPECT_EQ(rc, -EINVAL);
907
Chau Ly41846232025-08-13 04:43:27 +0000908 rc = decode_pldm_base_multipart_receive_resp(
Dung Cao88367842024-10-16 06:32:49 +0000909 responseMsg, payload_length, nullptr, &respDataIntegrityChecksum);
910
911 EXPECT_EQ(rc, -EINVAL);
912}
913#endif
914
915#ifdef LIBPLDM_API_TESTING
916TEST(DecodeMultipartReceiveResponse, BadTestInvalidExpectedInputMsgLength)
917{
918 uint8_t completionCode = PLDM_SUCCESS;
919 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END;
920 uint32_t nextDataTransferHandle = 0x15;
921 static constexpr const uint32_t dataLength = 9;
922 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
923 uint32_t dataIntegrityChecksum = 0x3C;
924
Chau Ly41846232025-08-13 04:43:27 +0000925 struct pldm_base_multipart_receive_resp resp_data = {};
Dung Cao88367842024-10-16 06:32:49 +0000926
927 PLDM_MSGBUF_DEFINE_P(buf);
928 int rc;
929
930 static constexpr const size_t payload_length =
931 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength +
932 sizeof(dataIntegrityChecksum);
933 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
934
935 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
936 ASSERT_EQ(rc, 0);
937
938 pldm_msgbuf_insert_uint8(buf, completionCode);
939 pldm_msgbuf_insert_uint8(buf, transferFlag);
940 pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle);
941 pldm_msgbuf_insert_uint32(buf, dataLength);
942 rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(),
943 dataLength);
944 EXPECT_EQ(rc, 0);
945 pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum);
946
947 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
948
949 uint32_t respDataIntegrityChecksum = 0;
950
Chau Ly41846232025-08-13 04:43:27 +0000951 rc = decode_pldm_base_multipart_receive_resp(responseMsg, 0, &resp_data,
952 &respDataIntegrityChecksum);
Dung Cao88367842024-10-16 06:32:49 +0000953
954 EXPECT_EQ(rc, -EOVERFLOW);
955}
956#endif
957
John Chung21a639d2025-07-17 18:48:19 -0500958#ifdef LIBPLDM_API_TESTING
959TEST(EncodeMultipartReceiveResponse, GoodTestWithChecksum)
960{
961 uint8_t instance_id = 0;
962 uint8_t completionCode = PLDM_SUCCESS;
963 uint8_t transferFlag =
964 PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START_AND_END;
965 uint32_t nextDataTransferHandle = 0x15;
966 static constexpr const uint32_t dataLength = 9;
967 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
968 uint32_t dataIntegrityChecksum = 0x3C;
969 static constexpr const size_t responseMsgLength =
970 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength +
971 sizeof(dataIntegrityChecksum);
972 size_t payload_length = responseMsgLength;
973
974 struct variable_field payload = {data.data(), dataLength};
Chau Ly41846232025-08-13 04:43:27 +0000975 struct pldm_base_multipart_receive_resp resp_data = {
John Chung21a639d2025-07-17 18:48:19 -0500976 completionCode, transferFlag, nextDataTransferHandle, payload};
977 std::array<uint8_t, responseMsgLength> responseMsg = {
978 completionCode,
979 transferFlag,
980 0x15, // nextDataTransferHandle
981 0x00,
982 0x00,
983 0x00,
984 0x09, // dataLength
985 0x00,
986 0x00,
987 0x00,
988 0x1, // data
989 0x2,
990 0x3,
991 0x4,
992 0x5,
993 0x6,
994 0x7,
995 0x8,
996 0x9,
997 0x3c, // dataIntegrityChecksum
998 0x00,
999 0x00,
1000 0x00};
1001
1002 PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength);
1003 int rc;
1004
1005 rc = encode_base_multipart_receive_resp(instance_id, &resp_data,
1006 dataIntegrityChecksum, responsePtr,
1007 &payload_length);
1008
1009 ASSERT_EQ(0, rc);
1010 EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(),
1011 sizeof(responseMsg)));
1012 EXPECT_EQ(payload_length, responseMsgLength);
1013}
1014#endif
1015
1016#ifdef LIBPLDM_API_TESTING
1017TEST(EncodeMultipartReceiveResponse, GoodTestWithoutChecksum)
1018{
1019 uint8_t instance_id = 0;
1020 uint8_t completionCode = PLDM_SUCCESS;
1021 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START;
1022 uint32_t nextDataTransferHandle = 0x16;
1023 static constexpr const uint32_t dataLength = 9;
1024 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1025 static constexpr const size_t responseMsgLength =
1026 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength;
1027 size_t payload_length = responseMsgLength;
1028
1029 struct variable_field payload = {data.data(), dataLength};
Chau Ly41846232025-08-13 04:43:27 +00001030 struct pldm_base_multipart_receive_resp resp_data = {
John Chung21a639d2025-07-17 18:48:19 -05001031 completionCode, transferFlag, nextDataTransferHandle, payload};
1032 std::array<uint8_t, responseMsgLength> responseMsg = {
1033 completionCode,
1034 transferFlag,
1035 0x16, // nextDataTransferHandle
1036 0x00,
1037 0x00,
1038 0x00,
1039 0x09, // dataLength
1040 0x00,
1041 0x00,
1042 0x00,
1043 0x1, // data
1044 0x2,
1045 0x3,
1046 0x4,
1047 0x5,
1048 0x6,
1049 0x7,
1050 0x8,
1051 0x9};
1052
1053 PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength);
1054 int rc;
1055
1056 rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0,
1057 responsePtr, &payload_length);
1058
1059 ASSERT_EQ(0, rc);
1060 EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(),
1061 sizeof(responseMsg)));
1062 EXPECT_EQ(payload_length, responseMsgLength);
1063}
1064#endif
1065
1066#ifdef LIBPLDM_API_TESTING
1067TEST(EncodeMultipartReceiveResponse, GoodTestCompletionCode)
1068{
1069 uint8_t instance_id = 0;
1070 uint8_t completionCode = PLDM_MULTIPART_RECEIVE_NEGOTIATION_INCOMPLETE;
1071 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START;
1072 uint32_t nextDataTransferHandle = 0x16;
1073 static constexpr const uint32_t dataLength = 9;
1074 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1075 static constexpr const size_t responseMsgLength =
1076 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength;
1077 size_t payload_length = responseMsgLength;
1078
1079 struct variable_field payload = {data.data(), dataLength};
Chau Ly41846232025-08-13 04:43:27 +00001080 struct pldm_base_multipart_receive_resp resp_data = {
John Chung21a639d2025-07-17 18:48:19 -05001081 completionCode, transferFlag, nextDataTransferHandle, payload};
1082 std::array<uint8_t, 1> responseMsg = {completionCode};
1083
1084 PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength);
1085 int rc;
1086
1087 rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0,
1088 responsePtr, &payload_length);
1089
1090 ASSERT_EQ(0, rc);
1091 EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(),
1092 sizeof(responseMsg)));
1093 EXPECT_EQ(payload_length, 1);
1094}
1095#endif
1096
1097#ifdef LIBPLDM_API_TESTING
1098TEST(EncodeMultipartReceiveResponse, BadTestUnAllocatedParams)
1099{
1100 uint8_t instance_id = 0;
1101 uint8_t completionCode = PLDM_SUCCESS;
1102 uint8_t transferFlag =
1103 PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START_AND_END;
1104 uint32_t nextDataTransferHandle = 0x15;
1105 static constexpr const uint32_t dataLength = 9;
1106 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1107 uint32_t dataIntegrityChecksum = 0x3C;
1108 static constexpr const size_t responseMsgLength =
1109 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength +
1110 sizeof(dataIntegrityChecksum);
1111 size_t payload_length = responseMsgLength;
1112
1113 struct variable_field payload = {data.data(), dataLength};
Chau Ly41846232025-08-13 04:43:27 +00001114 struct pldm_base_multipart_receive_resp resp_data = {
John Chung21a639d2025-07-17 18:48:19 -05001115 completionCode, transferFlag, nextDataTransferHandle, payload};
1116
1117 PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength);
1118 int rc;
1119
1120 rc = encode_base_multipart_receive_resp(instance_id, nullptr,
1121 dataIntegrityChecksum, responsePtr,
1122 &payload_length);
1123 EXPECT_EQ(rc, -EINVAL);
1124
1125 rc = encode_base_multipart_receive_resp(instance_id, &resp_data,
1126 dataIntegrityChecksum, nullptr,
1127 &payload_length);
1128 EXPECT_EQ(rc, -EINVAL);
1129
1130 rc = encode_base_multipart_receive_resp(
1131 instance_id, &resp_data, dataIntegrityChecksum, responsePtr, nullptr);
1132 EXPECT_EQ(rc, -EINVAL);
1133
1134 resp_data.data.ptr = nullptr;
1135 rc = encode_base_multipart_receive_resp(instance_id, &resp_data,
1136 dataIntegrityChecksum, responsePtr,
1137 &payload_length);
1138 EXPECT_EQ(rc, -EINVAL);
1139}
1140#endif
1141
1142#ifdef LIBPLDM_API_TESTING
1143TEST(EncodeMultipartReceiveResponse, BadTestInvalidExpectedOutputMsgLength)
1144{
1145 uint8_t instance_id = 0;
1146 uint8_t completionCode = PLDM_SUCCESS;
1147 uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START;
1148 uint32_t nextDataTransferHandle = 0x16;
1149 static constexpr const uint32_t dataLength = 9;
1150 std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9};
1151 static constexpr const size_t responseMsgLength =
1152 PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES;
1153 size_t payload_length = responseMsgLength;
1154
1155 struct variable_field payload = {data.data(), dataLength};
Chau Ly41846232025-08-13 04:43:27 +00001156 struct pldm_base_multipart_receive_resp resp_data = {
John Chung21a639d2025-07-17 18:48:19 -05001157 completionCode, transferFlag, nextDataTransferHandle, payload};
1158
1159 PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength);
1160 int rc;
1161
1162 rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0,
1163 responsePtr, &payload_length);
1164 EXPECT_EQ(rc, -EOVERFLOW);
1165}
1166#endif
1167
Andrew Jeffery9c766792022-08-10 23:12:49 +09301168TEST(CcOnlyResponse, testEncode)
1169{
1170 struct pldm_msg responseMsg;
1171
1172 auto rc =
1173 encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/,
Manojkiran Eda9e3a5d42024-06-17 16:06:42 +05301174 3 /*completion code*/, &responseMsg);
Andrew Jeffery9c766792022-08-10 23:12:49 +09301175 EXPECT_EQ(rc, PLDM_SUCCESS);
1176
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301177 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Andrew Jeffery9c766792022-08-10 23:12:49 +09301178 auto p = reinterpret_cast<uint8_t*>(&responseMsg);
1179 EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)),
1180 ElementsAreArray({0, 1, 2, 3}));
1181
1182 rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg);
1183 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1184
1185 rc = encode_cc_only_resp(0, 1, 2, 3, nullptr);
1186 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1187}
Gilbert Chen6c9c9172022-10-18 17:07:29 +08001188
1189TEST(SetTID, testGoodEncodeRequest)
1190{
1191 uint8_t instanceId = 0;
1192 uint8_t tid = 0x01;
1193 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301194 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chen6c9c9172022-10-18 17:07:29 +08001195 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1196
1197 auto rc = encode_set_tid_req(instanceId, tid, request);
1198 ASSERT_EQ(rc, PLDM_SUCCESS);
1199
1200 EXPECT_EQ(request->hdr.command, PLDM_SET_TID);
1201 EXPECT_EQ(request->hdr.type, PLDM_BASE);
1202 EXPECT_EQ(request->hdr.request, 1);
1203 EXPECT_EQ(request->hdr.datagram, 0);
1204 EXPECT_EQ(request->hdr.instance_id, instanceId);
1205 EXPECT_EQ(0, memcmp(request->payload, &tid, sizeof(tid)));
1206}
1207
1208TEST(SetTID, testBadEncodeRequest)
1209{
1210 uint8_t tid = 0x01;
1211 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
Andrew Jefferyd0ba43a2024-09-09 15:50:17 +09301212 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Gilbert Chen6c9c9172022-10-18 17:07:29 +08001213 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1214
1215 auto rc = encode_set_tid_req(0, tid, nullptr);
1216 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1217
1218 rc = encode_set_tid_req(0, 0, request);
1219 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1220
1221 rc = encode_set_tid_req(0, 0xff, request);
1222 EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1223}
Andrew Jeffery91c06e92023-09-22 15:22:54 +09301224
1225#ifdef LIBPLDM_API_TESTING
Vishnu Santhoshec19cbc2025-05-30 16:34:28 +05301226TEST(SetTID, testGoodDecodeRequest)
1227{
1228 uint8_t tid = 0x01;
1229 uint8_t tidOut = 0x00;
1230 std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{};
1231
1232 requestMsg[sizeof(pldm_msg_hdr)] = tid;
1233
1234 pldm_msg* request = new (requestMsg.data()) pldm_msg;
1235 auto rc = decode_set_tid_req(
1236 request, requestMsg.size() - sizeof(pldm_msg_hdr), &tidOut);
1237
1238 EXPECT_EQ(rc, PLDM_SUCCESS);
1239 EXPECT_EQ(tid, tidOut);
1240}
1241#endif
1242
1243#ifdef LIBPLDM_API_TESTING
1244TEST(SetTID, testBadDecodeRequestMsg)
1245{
1246 uint8_t tid = 0x01;
1247 std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{};
1248
1249 auto rc = decode_set_tid_req(
1250 nullptr, requestMsg.size() - sizeof(pldm_msg_hdr), &tid);
1251
1252 EXPECT_EQ(rc, -EINVAL);
1253}
1254#endif
1255
1256#ifdef LIBPLDM_API_TESTING
1257TEST(SetTID, testBadDecodeRequestTid)
1258{
1259 std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{};
1260 pldm_msg* request = new (requestMsg.data()) pldm_msg;
1261
1262 auto rc = decode_set_tid_req(
1263 request, requestMsg.size() - sizeof(pldm_msg_hdr), nullptr);
1264
1265 EXPECT_EQ(rc, -EINVAL);
1266}
1267#endif
1268
1269#ifdef LIBPLDM_API_TESTING
1270TEST(SetTID, testBadDecodeRequestMsgSize)
1271{
1272 std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{};
1273 pldm_msg* request = new (requestMsg.data()) pldm_msg;
1274
1275 auto rc = decode_set_tid_req(request, -1, nullptr);
1276
1277 EXPECT_EQ(rc, -EINVAL);
1278}
1279#endif
1280
1281#ifdef LIBPLDM_API_TESTING
Andrew Jeffery91c06e92023-09-22 15:22:54 +09301282TEST(PldmMsgHdr, correlateSuccess)
1283{
1284 static const struct pldm_msg_hdr req = {
1285 .instance_id = 0,
1286 .reserved = 0,
1287 .datagram = 0,
1288 .request = 1,
1289 .type = 0,
1290 .header_ver = 1,
1291 .command = 0x01,
1292 };
1293 static const struct pldm_msg_hdr resp = {
1294 .instance_id = 0,
1295 .reserved = 0,
1296 .datagram = 0,
1297 .request = 0,
1298 .type = 0,
1299 .header_ver = 1,
1300 .command = 0x01,
1301 };
1302
1303 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), true);
1304}
1305#endif
1306
1307#ifdef LIBPLDM_API_TESTING
1308TEST(PldmMsgHdr, correlateFailInstanceID)
1309{
1310 static const struct pldm_msg_hdr req = {
1311 .instance_id = 0,
1312 .reserved = 0,
1313 .datagram = 0,
1314 .request = 1,
1315 .type = 0,
1316 .header_ver = 1,
1317 .command = 0x01,
1318 };
1319 static const struct pldm_msg_hdr resp = {
1320 .instance_id = 1,
1321 .reserved = 0,
1322 .datagram = 0,
1323 .request = 0,
1324 .type = 0,
1325 .header_ver = 1,
1326 .command = 0x01,
1327 };
1328
1329 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false);
1330}
1331#endif
1332
1333#ifdef LIBPLDM_API_TESTING
1334TEST(PldmMsgHdr, correlateFailRequest)
1335{
1336 static const struct pldm_msg_hdr req = {
1337 .instance_id = 0,
1338 .reserved = 0,
1339 .datagram = 0,
1340 .request = 1,
1341 .type = 0,
1342 .header_ver = 1,
1343 .command = 0x01,
1344 };
1345 static const struct pldm_msg_hdr resp = {
1346 .instance_id = 0,
1347 .reserved = 0,
1348 .datagram = 0,
1349 .request = 1,
1350 .type = 0,
1351 .header_ver = 1,
1352 .command = 0x01,
1353 };
1354
1355 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false);
1356}
1357#endif
1358
1359#ifdef LIBPLDM_API_TESTING
1360TEST(PldmMsgHdr, correlateFailType)
1361{
1362 static const struct pldm_msg_hdr req = {
1363 .instance_id = 0,
1364 .reserved = 0,
1365 .datagram = 0,
1366 .request = 1,
1367 .type = 0,
1368 .header_ver = 1,
1369 .command = 0x01,
1370 };
1371 static const struct pldm_msg_hdr resp = {
1372 .instance_id = 0,
1373 .reserved = 0,
1374 .datagram = 0,
1375 .request = 0,
1376 .type = 1,
1377 .header_ver = 1,
1378 .command = 0x01,
1379 };
1380
1381 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false);
1382}
1383#endif
1384
1385#ifdef LIBPLDM_API_TESTING
1386TEST(PldmMsgHdr, correlateFailCommand)
1387{
1388 static const struct pldm_msg_hdr req = {
1389 .instance_id = 0,
1390 .reserved = 0,
1391 .datagram = 0,
1392 .request = 1,
1393 .type = 0,
1394 .header_ver = 1,
1395 .command = 0x01,
1396 };
1397 static const struct pldm_msg_hdr resp = {
1398 .instance_id = 0,
1399 .reserved = 0,
1400 .datagram = 0,
1401 .request = 0,
1402 .type = 0,
1403 .header_ver = 1,
1404 .command = 0x02,
1405 };
1406
1407 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false);
1408}
1409#endif
1410
1411#ifdef LIBPLDM_API_TESTING
1412TEST(PldmMsgHdr, correlateFailRequestIsResponse)
1413{
1414 static const struct pldm_msg_hdr req = {
1415 .instance_id = 0,
1416 .reserved = 0,
1417 .datagram = 0,
1418 .request = 0,
1419 .type = 0,
1420 .header_ver = 1,
1421 .command = 0x01,
1422 };
1423 static const struct pldm_msg_hdr resp = {
1424 .instance_id = 0,
1425 .reserved = 0,
1426 .datagram = 0,
1427 .request = 0,
1428 .type = 0,
1429 .header_ver = 1,
1430 .command = 0x02,
1431 };
1432
1433 ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false);
1434}
1435#endif
Chau Ly59dce782025-04-01 08:22:07 +00001436
1437#ifdef LIBPLDM_API_TESTING
1438TEST(EncodeNegotiateTransferParamsRequest, GoodTest)
1439{
1440 uint8_t instance_id = 0;
1441
1442 const struct pldm_base_negotiate_transfer_params_req req_data = {
1443 0x0001, // BE 256
1444 {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}};
1445
1446 std::array<uint8_t, PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES>
1447 requestMsg = {0x01, 0x00, // requester_part_size = 256
1448 0x00, 0x00, 0x00, 0x00,
1449 0x00, 0x00, 0x00, 0x81}; // requester_protocol_support =
1450 // PLDM_BASE & PLDM_FILE
1451
1452 PLDM_MSG_DEFINE_P(requestPtr,
1453 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1454 auto rc = encode_pldm_base_negotiate_transfer_params_req(
1455 instance_id, &req_data, requestPtr,
1456 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1457
1458 ASSERT_EQ(rc, 0);
1459 EXPECT_EQ(
1460 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg)));
1461}
1462#endif
1463
1464#ifdef LIBPLDM_API_TESTING
1465TEST(EncodeNegotiateTransferParamsRequest, BadTestUnAllocatedPtrParams)
1466{
1467 int rc;
1468 uint8_t instance_id = 0;
1469 const struct pldm_base_negotiate_transfer_params_req req_data = {
1470 0x0001, // BE 256
1471 {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}};
1472
1473 PLDM_MSG_DEFINE_P(requestPtr,
1474 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1475 rc = encode_pldm_base_negotiate_transfer_params_req(
1476 instance_id, nullptr, requestPtr,
1477 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1478 EXPECT_EQ(rc, -EINVAL);
1479
1480 rc = encode_pldm_base_negotiate_transfer_params_req(
1481 instance_id, &req_data, nullptr,
1482 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1483 EXPECT_EQ(rc, -EINVAL);
1484}
1485#endif
1486
1487#ifdef LIBPLDM_API_TESTING
1488TEST(EncodeNegotiateTransferParamsRequest,
1489 BadTestInvalidExpectedOutputMsgLength)
1490{
1491 int rc;
1492 uint8_t instance_id = 0;
1493 const struct pldm_base_negotiate_transfer_params_req req_data = {
1494 0x0001, // BE 256
1495 {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}};
1496
1497 PLDM_MSG_DEFINE_P(requestPtr,
1498 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES);
1499
1500 rc = encode_pldm_base_negotiate_transfer_params_req(instance_id, &req_data,
1501 requestPtr, 1);
1502 EXPECT_EQ(rc, -EOVERFLOW);
1503}
1504#endif
1505
1506#ifdef LIBPLDM_API_TESTING
1507TEST(DecodeNegotiateTransferParamsResponse, GoodTest)
1508{
1509 uint8_t completionCode = PLDM_SUCCESS;
1510 uint16_t responderPartSize = 128;
1511 std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00,
1512 0x00, 0x00, 0x00, 0x81};
1513
1514 struct pldm_base_negotiate_transfer_params_resp resp_data = {};
1515
1516 PLDM_MSGBUF_DEFINE_P(buf);
1517 int rc;
1518
1519 static constexpr const size_t payload_length =
1520 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES;
1521 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
1522
1523 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
1524 ASSERT_EQ(rc, 0);
1525
1526 pldm_msgbuf_insert_uint8(buf, completionCode);
1527 pldm_msgbuf_insert_uint16(buf, responderPartSize);
1528 rc = pldm_msgbuf_insert_array_uint8(
1529 buf, sizeof(resp_data.responder_protocol_support),
1530 responderProtocolSupport.data(),
1531 sizeof(resp_data.responder_protocol_support));
1532 EXPECT_EQ(rc, 0);
1533
1534 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
1535
1536 rc = decode_pldm_base_negotiate_transfer_params_resp(
1537 responseMsg, payload_length, &resp_data);
1538
1539 ASSERT_EQ(rc, 0);
1540 EXPECT_EQ(resp_data.completion_code, completionCode);
1541 EXPECT_EQ(resp_data.responder_part_size, responderPartSize);
1542 EXPECT_EQ(0, memcmp(responderProtocolSupport.data(),
1543 resp_data.responder_protocol_support,
1544 sizeof(resp_data.responder_protocol_support)));
1545}
1546#endif
1547
1548#ifdef LIBPLDM_API_TESTING
1549TEST(DecodeNegotiateTransferParamsResponse, BadTestUnAllocatedPtrParams)
1550{
1551 uint8_t completionCode = PLDM_SUCCESS;
1552 uint16_t responderPartSize = 128;
1553 std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00,
1554 0x00, 0x00, 0x00, 0x81};
1555
1556 struct pldm_base_negotiate_transfer_params_resp resp_data = {};
1557
1558 PLDM_MSGBUF_DEFINE_P(buf);
1559 int rc;
1560
1561 static constexpr const size_t payload_length =
1562 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES;
1563 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
1564
1565 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
1566 ASSERT_EQ(rc, 0);
1567
1568 pldm_msgbuf_insert_uint8(buf, completionCode);
1569 pldm_msgbuf_insert_uint16(buf, responderPartSize);
1570 rc = pldm_msgbuf_insert_array_uint8(
1571 buf, sizeof(resp_data.responder_protocol_support),
1572 responderProtocolSupport.data(),
1573 sizeof(resp_data.responder_protocol_support));
1574 EXPECT_EQ(rc, 0);
1575
1576 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
1577
1578 rc = decode_pldm_base_negotiate_transfer_params_resp(
1579 nullptr, payload_length, &resp_data);
1580
1581 EXPECT_EQ(rc, -EINVAL);
1582
1583 rc = decode_pldm_base_negotiate_transfer_params_resp(
1584 responseMsg, payload_length, nullptr);
1585
1586 EXPECT_EQ(rc, -EINVAL);
1587}
1588#endif
1589
1590#ifdef LIBPLDM_API_TESTING
1591TEST(DecodeNegotiateTransferParamsResponse,
1592 BadTestInvalidExpectedInputMsgLength)
1593{
1594 uint8_t completionCode = PLDM_SUCCESS;
1595 uint16_t responderPartSize = 128;
1596 std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00,
1597 0x00, 0x00, 0x00, 0x81};
1598
1599 struct pldm_base_negotiate_transfer_params_resp resp_data = {};
1600
1601 PLDM_MSGBUF_DEFINE_P(buf);
1602 int rc;
1603
1604 static constexpr const size_t payload_length =
1605 PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES;
1606 PLDM_MSG_DEFINE_P(responseMsg, payload_length);
1607
1608 rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length);
1609 ASSERT_EQ(rc, 0);
1610
1611 pldm_msgbuf_insert_uint8(buf, completionCode);
1612 pldm_msgbuf_insert_uint16(buf, responderPartSize);
1613 rc = pldm_msgbuf_insert_array_uint8(
1614 buf, sizeof(resp_data.responder_protocol_support),
1615 responderProtocolSupport.data(),
1616 sizeof(resp_data.responder_protocol_support));
1617 EXPECT_EQ(rc, 0);
1618
1619 ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0);
1620
1621 rc = decode_pldm_base_negotiate_transfer_params_resp(responseMsg, 0,
1622 &resp_data);
1623
1624 EXPECT_EQ(rc, -EOVERFLOW);
1625}
Andrew Jefferyedb88262025-08-14 05:02:21 +00001626#endif