Andrew Jeffery | b0c1d20 | 2023-11-07 22:08:44 +1030 | [diff] [blame] | 1 | #include <libpldm/base.h> |
| 2 | #include <libpldm/pldm_types.h> |
| 3 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 4 | #include <array> |
Manojkiran Eda | 9a8e497 | 2022-11-28 16:38:21 +0530 | [diff] [blame] | 5 | #include <cstdint> |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 6 | #include <cstring> |
| 7 | #include <vector> |
| 8 | |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 9 | #include "msgbuf.h" |
| 10 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | |
| 14 | using testing::ElementsAreArray; |
| 15 | |
| 16 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 17 | |
| 18 | TEST(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 | |
| 55 | TEST(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 | |
| 99 | TEST(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 | |
| 132 | TEST(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 | |
| 141 | TEST(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 | |
| 174 | TEST(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 | |
| 199 | TEST(GetPLDMCommands, testEncodeRequest) |
| 200 | { |
| 201 | uint8_t pldmType = 0x05; |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 202 | ver32_t version{0xff, 0xff, 0xff, 0xff}; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 203 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 204 | requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 205 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 206 | 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 | |
| 215 | TEST(GetPLDMCommands, testDecodeRequest) |
| 216 | { |
| 217 | uint8_t pldmType = 0x05; |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 218 | ver32_t version{0xff, 0xff, 0xff, 0xff}; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 219 | uint8_t pldmTypeOut{}; |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 220 | ver32_t versionOut{0xff, 0xff, 0xff, 0xff}; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 221 | 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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 227 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 228 | 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 | |
| 237 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 242 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 243 | 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 | |
| 261 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 266 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 267 | 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 | |
| 283 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 294 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 295 | 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 | |
| 307 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 318 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 319 | 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 | |
| 327 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 338 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 339 | 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 | |
| 351 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 362 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 363 | 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 | |
| 372 | TEST(GetPLDMVersion, testGoodEncodeRequest) |
| 373 | { |
| 374 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 375 | requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 376 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 377 | 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 | |
| 394 | TEST(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 | |
| 406 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 413 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 414 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
Pavithra Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 415 | ver32_t version = {0xff, 0xff, 0xff, 0xff}; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 416 | |
| 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 | |
| 432 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 449 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 450 | 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 | |
| 461 | TEST(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 Barithaya | dc7d3b5 | 2024-02-06 23:46:49 -0600 | [diff] [blame] | 470 | ver32_t version = {0xff, 0xff, 0xff, 0xff}; |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 471 | 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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 483 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 484 | 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 | |
| 499 | TEST(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 | |
| 507 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 512 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 513 | 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 | |
| 523 | TEST(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 Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 532 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 533 | 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 Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 543 | TEST(DecodeMultipartReceiveResponse, testDecodeRequestPass) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 544 | { |
| 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 Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 551 | |
| 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 571 | uint32_t transfer_ctx; |
| 572 | uint32_t transfer_handle; |
| 573 | uint32_t section_offset; |
| 574 | uint32_t section_length; |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 575 | rc = decode_multipart_receive_req( |
| 576 | msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag, &transfer_ctx, |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 577 | &transfer_handle, §ion_offset, §ion_length); |
| 578 | |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 579 | ASSERT_EQ(rc, PLDM_SUCCESS); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 580 | 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 Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 588 | TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailNullData) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 589 | { |
| 590 | EXPECT_EQ(decode_multipart_receive_req(NULL, 0, NULL, NULL, NULL, NULL, |
| 591 | NULL, NULL), |
| 592 | PLDM_ERROR_INVALID_DATA); |
| 593 | } |
| 594 | |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 595 | TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadLength) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 596 | { |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 597 | PLDM_MSG_DEFINE_P(msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 598 | 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 Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 605 | memset(msg, 0, PLDM_MSG_SIZE(PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1)); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 606 | EXPECT_EQ(decode_multipart_receive_req( |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 607 | msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES + 1, &pldm_type, &flag, |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 608 | &transfer_ctx, &transfer_handle, §ion_offset, |
| 609 | §ion_length), |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 610 | PLDM_ERROR_INVALID_DATA); |
| 611 | } |
| 612 | |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 613 | TEST(DecodeMultipartReceiveResponse, testDecodeRequestFailBadPldmType) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 614 | { |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 615 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 631 | 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 Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 638 | EXPECT_EQ(decode_multipart_receive_req( |
| 639 | msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag, |
| 640 | &transfer_ctx, &transfer_handle, §ion_offset, |
| 641 | §ion_length), |
| 642 | PLDM_ERROR_INVALID_PLDM_TYPE); |
| 643 | } |
| 644 | |
| 645 | TEST(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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 654 | // Header values don't matter for this test. |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 655 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 662 | |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 663 | 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 Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 669 | |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 670 | EXPECT_EQ(decode_multipart_receive_req( |
| 671 | msg, PLDM_MULTIPART_RECEIVE_REQ_BYTES, &pldm_type, &flag, |
| 672 | &transfer_ctx, &transfer_handle, §ion_offset, |
| 673 | §ion_length), |
| 674 | PLDM_ERROR_UNEXPECTED_TRANSFER_FLAG_OPERATION); |
| 675 | } |
| 676 | |
| 677 | TEST(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, §ion_offset, |
| 710 | §ion_length), |
| 711 | PLDM_ERROR_INVALID_DATA); |
| 712 | } |
| 713 | |
| 714 | TEST(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, §ion_offset, |
| 747 | §ion_length), |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 748 | PLDM_ERROR_INVALID_DATA); |
| 749 | } |
| 750 | |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 751 | #ifdef LIBPLDM_API_TESTING |
| 752 | TEST(EncodeMultipartReceiveRequest, GoodTest) |
| 753 | { |
| 754 | uint8_t instance_id = 0; |
| 755 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 756 | const struct pldm_base_multipart_receive_req req_data = { |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 757 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 758 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 759 | static constexpr const size_t requestMsgLength = |
| 760 | PLDM_MULTIPART_RECEIVE_REQ_BYTES; |
| 761 | |
| 762 | std::array<uint8_t, requestMsgLength> requestMsg = { |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 763 | PLDM_BASE, PLDM_XFER_FIRST_PART, |
| 764 | 0x01, 0x00, |
| 765 | 0x00, 0x00, |
| 766 | 0x10, 0x00, |
| 767 | 0x00, 0x00, |
| 768 | 0x00, 0x00, |
| 769 | 0x00, 0x00, |
| 770 | 0x10, 0x00, |
| 771 | 0x00, 0x00}; |
| 772 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 773 | PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength); |
| 774 | size_t payload_length = requestMsgLength; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 775 | auto rc = encode_pldm_base_multipart_receive_req( |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 776 | instance_id, &req_data, requestPtr, &payload_length); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 777 | |
| 778 | ASSERT_EQ(rc, 0); |
| 779 | EXPECT_EQ( |
| 780 | 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg))); |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 781 | EXPECT_EQ(payload_length, requestMsgLength); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 782 | } |
| 783 | #endif |
| 784 | |
| 785 | #ifdef LIBPLDM_API_TESTING |
| 786 | TEST(EncodeMultipartReceiveRequest, BadTestUnAllocatedPtrParams) |
| 787 | { |
| 788 | uint8_t instance_id = 0; |
| 789 | int rc; |
| 790 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 791 | const struct pldm_base_multipart_receive_req req_data = { |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 792 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 793 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 794 | static constexpr const size_t requestMsgLength = |
| 795 | PLDM_MULTIPART_RECEIVE_REQ_BYTES; |
| 796 | |
| 797 | PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength); |
| 798 | size_t payload_length = requestMsgLength; |
| 799 | rc = encode_pldm_base_multipart_receive_req(instance_id, nullptr, |
| 800 | requestPtr, &payload_length); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 801 | EXPECT_EQ(rc, -EINVAL); |
| 802 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 803 | rc = encode_pldm_base_multipart_receive_req(instance_id, &req_data, nullptr, |
| 804 | &payload_length); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 805 | EXPECT_EQ(rc, -EINVAL); |
| 806 | } |
| 807 | #endif |
| 808 | |
| 809 | #ifdef LIBPLDM_API_TESTING |
| 810 | TEST(EncodeMultipartReceiveRequest, BadTestInvalidExpectedOutputMsgLength) |
| 811 | { |
| 812 | uint8_t instance_id = 0; |
| 813 | int rc; |
| 814 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 815 | const struct pldm_base_multipart_receive_req req_data = { |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 816 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 817 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 818 | static constexpr const size_t requestMsgLength = |
| 819 | PLDM_MULTIPART_RECEIVE_REQ_BYTES; |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 820 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 821 | PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength); |
| 822 | size_t payload_length = 1; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 823 | rc = encode_pldm_base_multipart_receive_req(instance_id, &req_data, |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 824 | requestPtr, &payload_length); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 825 | EXPECT_EQ(rc, -EOVERFLOW); |
| 826 | } |
| 827 | #endif |
| 828 | |
| 829 | #ifdef LIBPLDM_API_TESTING |
| 830 | TEST(DecodeMultipartReceiveResponse, GoodTest) |
| 831 | { |
| 832 | uint8_t completionCode = PLDM_SUCCESS; |
| 833 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 834 | uint32_t nextDataTransferHandle = 0x15; |
| 835 | static constexpr const uint32_t dataLength = 9; |
| 836 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 837 | uint32_t dataIntegrityChecksum = 0x3C; |
| 838 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 839 | struct pldm_base_multipart_receive_resp resp_data = {}; |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 840 | |
| 841 | PLDM_MSGBUF_DEFINE_P(buf); |
| 842 | int rc; |
| 843 | |
| 844 | static constexpr const size_t payload_length = |
| 845 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 846 | sizeof(dataIntegrityChecksum); |
| 847 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 848 | |
| 849 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 850 | ASSERT_EQ(rc, 0); |
| 851 | |
| 852 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 853 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 854 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 855 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 856 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 857 | dataLength); |
| 858 | EXPECT_EQ(rc, 0); |
| 859 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 860 | |
| 861 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 862 | |
| 863 | uint32_t respDataIntegrityChecksum = 0; |
| 864 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 865 | rc = decode_pldm_base_multipart_receive_resp( |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 866 | responseMsg, payload_length, &resp_data, &respDataIntegrityChecksum); |
| 867 | |
| 868 | ASSERT_EQ(rc, 0); |
| 869 | EXPECT_EQ(resp_data.completion_code, completionCode); |
| 870 | EXPECT_EQ(resp_data.transfer_flag, transferFlag); |
| 871 | EXPECT_EQ(resp_data.next_transfer_handle, nextDataTransferHandle); |
| 872 | EXPECT_EQ(resp_data.data.length, dataLength); |
| 873 | EXPECT_EQ(0, |
| 874 | memcmp(data.data(), resp_data.data.ptr, resp_data.data.length)); |
| 875 | EXPECT_EQ(respDataIntegrityChecksum, dataIntegrityChecksum); |
| 876 | } |
| 877 | #endif |
| 878 | |
| 879 | #ifdef LIBPLDM_API_TESTING |
| 880 | TEST(DecodeMultipartReceiveResponse, BadTestUnAllocatedPtrParams) |
| 881 | { |
| 882 | uint8_t completionCode = PLDM_SUCCESS; |
| 883 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 884 | uint32_t nextDataTransferHandle = 0x15; |
| 885 | static constexpr const uint32_t dataLength = 9; |
| 886 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 887 | uint32_t dataIntegrityChecksum = 0x3C; |
| 888 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 889 | struct pldm_base_multipart_receive_resp resp_data = {}; |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 890 | |
| 891 | PLDM_MSGBUF_DEFINE_P(buf); |
| 892 | int rc; |
| 893 | |
| 894 | static constexpr const size_t payload_length = |
| 895 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 896 | sizeof(dataIntegrityChecksum); |
| 897 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 898 | |
| 899 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 900 | ASSERT_EQ(rc, 0); |
| 901 | |
| 902 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 903 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 904 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 905 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 906 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 907 | dataLength); |
| 908 | EXPECT_EQ(rc, 0); |
| 909 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 910 | |
| 911 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 912 | |
| 913 | uint32_t respDataIntegrityChecksum = 0; |
| 914 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 915 | rc = decode_pldm_base_multipart_receive_resp( |
| 916 | nullptr, payload_length, &resp_data, &respDataIntegrityChecksum); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 917 | |
| 918 | EXPECT_EQ(rc, -EINVAL); |
| 919 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 920 | rc = decode_pldm_base_multipart_receive_resp( |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 921 | responseMsg, payload_length, nullptr, &respDataIntegrityChecksum); |
| 922 | |
| 923 | EXPECT_EQ(rc, -EINVAL); |
| 924 | } |
| 925 | #endif |
| 926 | |
| 927 | #ifdef LIBPLDM_API_TESTING |
| 928 | TEST(DecodeMultipartReceiveResponse, BadTestInvalidExpectedInputMsgLength) |
| 929 | { |
| 930 | uint8_t completionCode = PLDM_SUCCESS; |
| 931 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 932 | uint32_t nextDataTransferHandle = 0x15; |
| 933 | static constexpr const uint32_t dataLength = 9; |
| 934 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 935 | uint32_t dataIntegrityChecksum = 0x3C; |
| 936 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 937 | struct pldm_base_multipart_receive_resp resp_data = {}; |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 938 | |
| 939 | PLDM_MSGBUF_DEFINE_P(buf); |
| 940 | int rc; |
| 941 | |
| 942 | static constexpr const size_t payload_length = |
| 943 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 944 | sizeof(dataIntegrityChecksum); |
| 945 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 946 | |
| 947 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 948 | ASSERT_EQ(rc, 0); |
| 949 | |
| 950 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 951 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 952 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 953 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 954 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 955 | dataLength); |
| 956 | EXPECT_EQ(rc, 0); |
| 957 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 958 | |
| 959 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 960 | |
| 961 | uint32_t respDataIntegrityChecksum = 0; |
| 962 | |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 963 | rc = decode_pldm_base_multipart_receive_resp(responseMsg, 0, &resp_data, |
| 964 | &respDataIntegrityChecksum); |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 965 | |
| 966 | EXPECT_EQ(rc, -EOVERFLOW); |
| 967 | } |
| 968 | #endif |
| 969 | |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 970 | #ifdef LIBPLDM_API_TESTING |
| 971 | TEST(EncodeMultipartReceiveResponse, GoodTestWithChecksum) |
| 972 | { |
| 973 | uint8_t instance_id = 0; |
| 974 | uint8_t completionCode = PLDM_SUCCESS; |
| 975 | uint8_t transferFlag = |
| 976 | PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START_AND_END; |
| 977 | uint32_t nextDataTransferHandle = 0x15; |
| 978 | static constexpr const uint32_t dataLength = 9; |
| 979 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 980 | uint32_t dataIntegrityChecksum = 0x3C; |
| 981 | static constexpr const size_t responseMsgLength = |
| 982 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 983 | sizeof(dataIntegrityChecksum); |
| 984 | size_t payload_length = responseMsgLength; |
| 985 | |
| 986 | struct variable_field payload = {data.data(), dataLength}; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 987 | struct pldm_base_multipart_receive_resp resp_data = { |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 988 | completionCode, transferFlag, nextDataTransferHandle, payload}; |
| 989 | std::array<uint8_t, responseMsgLength> responseMsg = { |
| 990 | completionCode, |
| 991 | transferFlag, |
| 992 | 0x15, // nextDataTransferHandle |
| 993 | 0x00, |
| 994 | 0x00, |
| 995 | 0x00, |
| 996 | 0x09, // dataLength |
| 997 | 0x00, |
| 998 | 0x00, |
| 999 | 0x00, |
| 1000 | 0x1, // data |
| 1001 | 0x2, |
| 1002 | 0x3, |
| 1003 | 0x4, |
| 1004 | 0x5, |
| 1005 | 0x6, |
| 1006 | 0x7, |
| 1007 | 0x8, |
| 1008 | 0x9, |
| 1009 | 0x3c, // dataIntegrityChecksum |
| 1010 | 0x00, |
| 1011 | 0x00, |
| 1012 | 0x00}; |
| 1013 | |
| 1014 | PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength); |
| 1015 | int rc; |
| 1016 | |
| 1017 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, |
| 1018 | dataIntegrityChecksum, responsePtr, |
| 1019 | &payload_length); |
| 1020 | |
| 1021 | ASSERT_EQ(0, rc); |
| 1022 | EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(), |
| 1023 | sizeof(responseMsg))); |
| 1024 | EXPECT_EQ(payload_length, responseMsgLength); |
| 1025 | } |
| 1026 | #endif |
| 1027 | |
| 1028 | #ifdef LIBPLDM_API_TESTING |
| 1029 | TEST(EncodeMultipartReceiveResponse, GoodTestWithoutChecksum) |
| 1030 | { |
| 1031 | uint8_t instance_id = 0; |
| 1032 | uint8_t completionCode = PLDM_SUCCESS; |
| 1033 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START; |
| 1034 | uint32_t nextDataTransferHandle = 0x16; |
| 1035 | static constexpr const uint32_t dataLength = 9; |
| 1036 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 1037 | static constexpr const size_t responseMsgLength = |
| 1038 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength; |
| 1039 | size_t payload_length = responseMsgLength; |
| 1040 | |
| 1041 | struct variable_field payload = {data.data(), dataLength}; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 1042 | struct pldm_base_multipart_receive_resp resp_data = { |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 1043 | completionCode, transferFlag, nextDataTransferHandle, payload}; |
| 1044 | std::array<uint8_t, responseMsgLength> responseMsg = { |
| 1045 | completionCode, |
| 1046 | transferFlag, |
| 1047 | 0x16, // nextDataTransferHandle |
| 1048 | 0x00, |
| 1049 | 0x00, |
| 1050 | 0x00, |
| 1051 | 0x09, // dataLength |
| 1052 | 0x00, |
| 1053 | 0x00, |
| 1054 | 0x00, |
| 1055 | 0x1, // data |
| 1056 | 0x2, |
| 1057 | 0x3, |
| 1058 | 0x4, |
| 1059 | 0x5, |
| 1060 | 0x6, |
| 1061 | 0x7, |
| 1062 | 0x8, |
| 1063 | 0x9}; |
| 1064 | |
| 1065 | PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength); |
| 1066 | int rc; |
| 1067 | |
| 1068 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0, |
| 1069 | responsePtr, &payload_length); |
| 1070 | |
| 1071 | ASSERT_EQ(0, rc); |
| 1072 | EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(), |
| 1073 | sizeof(responseMsg))); |
| 1074 | EXPECT_EQ(payload_length, responseMsgLength); |
| 1075 | } |
| 1076 | #endif |
| 1077 | |
| 1078 | #ifdef LIBPLDM_API_TESTING |
| 1079 | TEST(EncodeMultipartReceiveResponse, GoodTestCompletionCode) |
| 1080 | { |
| 1081 | uint8_t instance_id = 0; |
| 1082 | uint8_t completionCode = PLDM_MULTIPART_RECEIVE_NEGOTIATION_INCOMPLETE; |
| 1083 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START; |
| 1084 | uint32_t nextDataTransferHandle = 0x16; |
| 1085 | static constexpr const uint32_t dataLength = 9; |
| 1086 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 1087 | static constexpr const size_t responseMsgLength = |
| 1088 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength; |
| 1089 | size_t payload_length = responseMsgLength; |
| 1090 | |
| 1091 | struct variable_field payload = {data.data(), dataLength}; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 1092 | struct pldm_base_multipart_receive_resp resp_data = { |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 1093 | completionCode, transferFlag, nextDataTransferHandle, payload}; |
| 1094 | std::array<uint8_t, 1> responseMsg = {completionCode}; |
| 1095 | |
| 1096 | PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength); |
| 1097 | int rc; |
| 1098 | |
| 1099 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0, |
| 1100 | responsePtr, &payload_length); |
| 1101 | |
| 1102 | ASSERT_EQ(0, rc); |
| 1103 | EXPECT_EQ(0, memcmp(responsePtr->payload, responseMsg.data(), |
| 1104 | sizeof(responseMsg))); |
| 1105 | EXPECT_EQ(payload_length, 1); |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
| 1109 | #ifdef LIBPLDM_API_TESTING |
| 1110 | TEST(EncodeMultipartReceiveResponse, BadTestUnAllocatedParams) |
| 1111 | { |
| 1112 | uint8_t instance_id = 0; |
| 1113 | uint8_t completionCode = PLDM_SUCCESS; |
| 1114 | uint8_t transferFlag = |
| 1115 | PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START_AND_END; |
| 1116 | uint32_t nextDataTransferHandle = 0x15; |
| 1117 | static constexpr const uint32_t dataLength = 9; |
| 1118 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 1119 | uint32_t dataIntegrityChecksum = 0x3C; |
| 1120 | static constexpr const size_t responseMsgLength = |
| 1121 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 1122 | sizeof(dataIntegrityChecksum); |
| 1123 | size_t payload_length = responseMsgLength; |
| 1124 | |
| 1125 | struct variable_field payload = {data.data(), dataLength}; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 1126 | struct pldm_base_multipart_receive_resp resp_data = { |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 1127 | completionCode, transferFlag, nextDataTransferHandle, payload}; |
| 1128 | |
| 1129 | PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength); |
| 1130 | int rc; |
| 1131 | |
| 1132 | rc = encode_base_multipart_receive_resp(instance_id, nullptr, |
| 1133 | dataIntegrityChecksum, responsePtr, |
| 1134 | &payload_length); |
| 1135 | EXPECT_EQ(rc, -EINVAL); |
| 1136 | |
| 1137 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, |
| 1138 | dataIntegrityChecksum, nullptr, |
| 1139 | &payload_length); |
| 1140 | EXPECT_EQ(rc, -EINVAL); |
| 1141 | |
| 1142 | rc = encode_base_multipart_receive_resp( |
| 1143 | instance_id, &resp_data, dataIntegrityChecksum, responsePtr, nullptr); |
| 1144 | EXPECT_EQ(rc, -EINVAL); |
| 1145 | |
| 1146 | resp_data.data.ptr = nullptr; |
| 1147 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, |
| 1148 | dataIntegrityChecksum, responsePtr, |
| 1149 | &payload_length); |
| 1150 | EXPECT_EQ(rc, -EINVAL); |
| 1151 | } |
| 1152 | #endif |
| 1153 | |
| 1154 | #ifdef LIBPLDM_API_TESTING |
| 1155 | TEST(EncodeMultipartReceiveResponse, BadTestInvalidExpectedOutputMsgLength) |
| 1156 | { |
| 1157 | uint8_t instance_id = 0; |
| 1158 | uint8_t completionCode = PLDM_SUCCESS; |
| 1159 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_START; |
| 1160 | uint32_t nextDataTransferHandle = 0x16; |
| 1161 | static constexpr const uint32_t dataLength = 9; |
| 1162 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 1163 | static constexpr const size_t responseMsgLength = |
| 1164 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES; |
| 1165 | size_t payload_length = responseMsgLength; |
| 1166 | |
| 1167 | struct variable_field payload = {data.data(), dataLength}; |
Chau Ly | 4184623 | 2025-08-13 04:43:27 +0000 | [diff] [blame] | 1168 | struct pldm_base_multipart_receive_resp resp_data = { |
John Chung | 21a639d | 2025-07-17 18:48:19 -0500 | [diff] [blame] | 1169 | completionCode, transferFlag, nextDataTransferHandle, payload}; |
| 1170 | |
| 1171 | PLDM_MSG_DEFINE_P(responsePtr, responseMsgLength); |
| 1172 | int rc; |
| 1173 | |
| 1174 | rc = encode_base_multipart_receive_resp(instance_id, &resp_data, 0, |
| 1175 | responsePtr, &payload_length); |
| 1176 | EXPECT_EQ(rc, -EOVERFLOW); |
| 1177 | } |
| 1178 | #endif |
| 1179 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1180 | TEST(CcOnlyResponse, testEncode) |
| 1181 | { |
| 1182 | struct pldm_msg responseMsg; |
| 1183 | |
| 1184 | auto rc = |
| 1185 | encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/, |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 1186 | 3 /*completion code*/, &responseMsg); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1187 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1188 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 1189 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 1190 | auto p = reinterpret_cast<uint8_t*>(&responseMsg); |
| 1191 | EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)), |
| 1192 | ElementsAreArray({0, 1, 2, 3})); |
| 1193 | |
| 1194 | rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg); |
| 1195 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1196 | |
| 1197 | rc = encode_cc_only_resp(0, 1, 2, 3, nullptr); |
| 1198 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1199 | } |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 1200 | |
| 1201 | TEST(SetTID, testGoodEncodeRequest) |
| 1202 | { |
| 1203 | uint8_t instanceId = 0; |
| 1204 | uint8_t tid = 0x01; |
| 1205 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 1206 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 1207 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1208 | |
| 1209 | auto rc = encode_set_tid_req(instanceId, tid, request); |
| 1210 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1211 | |
| 1212 | EXPECT_EQ(request->hdr.command, PLDM_SET_TID); |
| 1213 | EXPECT_EQ(request->hdr.type, PLDM_BASE); |
| 1214 | EXPECT_EQ(request->hdr.request, 1); |
| 1215 | EXPECT_EQ(request->hdr.datagram, 0); |
| 1216 | EXPECT_EQ(request->hdr.instance_id, instanceId); |
| 1217 | EXPECT_EQ(0, memcmp(request->payload, &tid, sizeof(tid))); |
| 1218 | } |
| 1219 | |
| 1220 | TEST(SetTID, testBadEncodeRequest) |
| 1221 | { |
| 1222 | uint8_t tid = 0x01; |
| 1223 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 1224 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 1225 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1226 | |
| 1227 | auto rc = encode_set_tid_req(0, tid, nullptr); |
| 1228 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1229 | |
| 1230 | rc = encode_set_tid_req(0, 0, request); |
| 1231 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1232 | |
| 1233 | rc = encode_set_tid_req(0, 0xff, request); |
| 1234 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1235 | } |
Andrew Jeffery | 91c06e9 | 2023-09-22 15:22:54 +0930 | [diff] [blame] | 1236 | |
| 1237 | #ifdef LIBPLDM_API_TESTING |
Vishnu Santhosh | ec19cbc | 2025-05-30 16:34:28 +0530 | [diff] [blame] | 1238 | TEST(SetTID, testGoodDecodeRequest) |
| 1239 | { |
| 1240 | uint8_t tid = 0x01; |
| 1241 | uint8_t tidOut = 0x00; |
| 1242 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{}; |
| 1243 | |
| 1244 | requestMsg[sizeof(pldm_msg_hdr)] = tid; |
| 1245 | |
| 1246 | pldm_msg* request = new (requestMsg.data()) pldm_msg; |
| 1247 | auto rc = decode_set_tid_req( |
| 1248 | request, requestMsg.size() - sizeof(pldm_msg_hdr), &tidOut); |
| 1249 | |
| 1250 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 1251 | EXPECT_EQ(tid, tidOut); |
| 1252 | } |
| 1253 | #endif |
| 1254 | |
| 1255 | #ifdef LIBPLDM_API_TESTING |
| 1256 | TEST(SetTID, testBadDecodeRequestMsg) |
| 1257 | { |
| 1258 | uint8_t tid = 0x01; |
| 1259 | std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{}; |
| 1260 | |
| 1261 | auto rc = decode_set_tid_req( |
| 1262 | nullptr, requestMsg.size() - sizeof(pldm_msg_hdr), &tid); |
| 1263 | |
| 1264 | EXPECT_EQ(rc, -EINVAL); |
| 1265 | } |
| 1266 | #endif |
| 1267 | |
| 1268 | #ifdef LIBPLDM_API_TESTING |
| 1269 | TEST(SetTID, testBadDecodeRequestTid) |
| 1270 | { |
| 1271 | std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{}; |
| 1272 | pldm_msg* request = new (requestMsg.data()) pldm_msg; |
| 1273 | |
| 1274 | auto rc = decode_set_tid_req( |
| 1275 | request, requestMsg.size() - sizeof(pldm_msg_hdr), nullptr); |
| 1276 | |
| 1277 | EXPECT_EQ(rc, -EINVAL); |
| 1278 | } |
| 1279 | #endif |
| 1280 | |
| 1281 | #ifdef LIBPLDM_API_TESTING |
| 1282 | TEST(SetTID, testBadDecodeRequestMsgSize) |
| 1283 | { |
| 1284 | std::array<uint8_t, hdrSize + PLDM_SET_TID_REQ_BYTES> requestMsg{}; |
| 1285 | pldm_msg* request = new (requestMsg.data()) pldm_msg; |
| 1286 | |
| 1287 | auto rc = decode_set_tid_req(request, -1, nullptr); |
| 1288 | |
| 1289 | EXPECT_EQ(rc, -EINVAL); |
| 1290 | } |
| 1291 | #endif |
| 1292 | |
| 1293 | #ifdef LIBPLDM_API_TESTING |
Andrew Jeffery | 91c06e9 | 2023-09-22 15:22:54 +0930 | [diff] [blame] | 1294 | TEST(PldmMsgHdr, correlateSuccess) |
| 1295 | { |
| 1296 | static const struct pldm_msg_hdr req = { |
| 1297 | .instance_id = 0, |
| 1298 | .reserved = 0, |
| 1299 | .datagram = 0, |
| 1300 | .request = 1, |
| 1301 | .type = 0, |
| 1302 | .header_ver = 1, |
| 1303 | .command = 0x01, |
| 1304 | }; |
| 1305 | static const struct pldm_msg_hdr resp = { |
| 1306 | .instance_id = 0, |
| 1307 | .reserved = 0, |
| 1308 | .datagram = 0, |
| 1309 | .request = 0, |
| 1310 | .type = 0, |
| 1311 | .header_ver = 1, |
| 1312 | .command = 0x01, |
| 1313 | }; |
| 1314 | |
| 1315 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), true); |
| 1316 | } |
| 1317 | #endif |
| 1318 | |
| 1319 | #ifdef LIBPLDM_API_TESTING |
| 1320 | TEST(PldmMsgHdr, correlateFailInstanceID) |
| 1321 | { |
| 1322 | static const struct pldm_msg_hdr req = { |
| 1323 | .instance_id = 0, |
| 1324 | .reserved = 0, |
| 1325 | .datagram = 0, |
| 1326 | .request = 1, |
| 1327 | .type = 0, |
| 1328 | .header_ver = 1, |
| 1329 | .command = 0x01, |
| 1330 | }; |
| 1331 | static const struct pldm_msg_hdr resp = { |
| 1332 | .instance_id = 1, |
| 1333 | .reserved = 0, |
| 1334 | .datagram = 0, |
| 1335 | .request = 0, |
| 1336 | .type = 0, |
| 1337 | .header_ver = 1, |
| 1338 | .command = 0x01, |
| 1339 | }; |
| 1340 | |
| 1341 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1342 | } |
| 1343 | #endif |
| 1344 | |
| 1345 | #ifdef LIBPLDM_API_TESTING |
| 1346 | TEST(PldmMsgHdr, correlateFailRequest) |
| 1347 | { |
| 1348 | static const struct pldm_msg_hdr req = { |
| 1349 | .instance_id = 0, |
| 1350 | .reserved = 0, |
| 1351 | .datagram = 0, |
| 1352 | .request = 1, |
| 1353 | .type = 0, |
| 1354 | .header_ver = 1, |
| 1355 | .command = 0x01, |
| 1356 | }; |
| 1357 | static const struct pldm_msg_hdr resp = { |
| 1358 | .instance_id = 0, |
| 1359 | .reserved = 0, |
| 1360 | .datagram = 0, |
| 1361 | .request = 1, |
| 1362 | .type = 0, |
| 1363 | .header_ver = 1, |
| 1364 | .command = 0x01, |
| 1365 | }; |
| 1366 | |
| 1367 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1368 | } |
| 1369 | #endif |
| 1370 | |
| 1371 | #ifdef LIBPLDM_API_TESTING |
| 1372 | TEST(PldmMsgHdr, correlateFailType) |
| 1373 | { |
| 1374 | static const struct pldm_msg_hdr req = { |
| 1375 | .instance_id = 0, |
| 1376 | .reserved = 0, |
| 1377 | .datagram = 0, |
| 1378 | .request = 1, |
| 1379 | .type = 0, |
| 1380 | .header_ver = 1, |
| 1381 | .command = 0x01, |
| 1382 | }; |
| 1383 | static const struct pldm_msg_hdr resp = { |
| 1384 | .instance_id = 0, |
| 1385 | .reserved = 0, |
| 1386 | .datagram = 0, |
| 1387 | .request = 0, |
| 1388 | .type = 1, |
| 1389 | .header_ver = 1, |
| 1390 | .command = 0x01, |
| 1391 | }; |
| 1392 | |
| 1393 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1394 | } |
| 1395 | #endif |
| 1396 | |
| 1397 | #ifdef LIBPLDM_API_TESTING |
| 1398 | TEST(PldmMsgHdr, correlateFailCommand) |
| 1399 | { |
| 1400 | static const struct pldm_msg_hdr req = { |
| 1401 | .instance_id = 0, |
| 1402 | .reserved = 0, |
| 1403 | .datagram = 0, |
| 1404 | .request = 1, |
| 1405 | .type = 0, |
| 1406 | .header_ver = 1, |
| 1407 | .command = 0x01, |
| 1408 | }; |
| 1409 | static const struct pldm_msg_hdr resp = { |
| 1410 | .instance_id = 0, |
| 1411 | .reserved = 0, |
| 1412 | .datagram = 0, |
| 1413 | .request = 0, |
| 1414 | .type = 0, |
| 1415 | .header_ver = 1, |
| 1416 | .command = 0x02, |
| 1417 | }; |
| 1418 | |
| 1419 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1420 | } |
| 1421 | #endif |
| 1422 | |
| 1423 | #ifdef LIBPLDM_API_TESTING |
| 1424 | TEST(PldmMsgHdr, correlateFailRequestIsResponse) |
| 1425 | { |
| 1426 | static const struct pldm_msg_hdr req = { |
| 1427 | .instance_id = 0, |
| 1428 | .reserved = 0, |
| 1429 | .datagram = 0, |
| 1430 | .request = 0, |
| 1431 | .type = 0, |
| 1432 | .header_ver = 1, |
| 1433 | .command = 0x01, |
| 1434 | }; |
| 1435 | static const struct pldm_msg_hdr resp = { |
| 1436 | .instance_id = 0, |
| 1437 | .reserved = 0, |
| 1438 | .datagram = 0, |
| 1439 | .request = 0, |
| 1440 | .type = 0, |
| 1441 | .header_ver = 1, |
| 1442 | .command = 0x02, |
| 1443 | }; |
| 1444 | |
| 1445 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1446 | } |
| 1447 | #endif |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1448 | |
| 1449 | #ifdef LIBPLDM_API_TESTING |
| 1450 | TEST(EncodeNegotiateTransferParamsRequest, GoodTest) |
| 1451 | { |
| 1452 | uint8_t instance_id = 0; |
| 1453 | |
| 1454 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1455 | 0x0001, // BE 256 |
| 1456 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1457 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1458 | static constexpr const size_t requestMsgLength = |
| 1459 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES; |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1460 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1461 | std::array<uint8_t, requestMsgLength> requestMsg = { |
| 1462 | 0x01, 0x00, // requester_part_size = 256 |
| 1463 | 0x00, 0x00, 0x00, 0x00, |
| 1464 | 0x00, 0x00, 0x00, 0x81}; // requester_protocol_support = |
| 1465 | // PLDM_BASE & PLDM_FILE |
| 1466 | |
| 1467 | PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength); |
| 1468 | size_t payload_length = requestMsgLength; |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1469 | auto rc = encode_pldm_base_negotiate_transfer_params_req( |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1470 | instance_id, &req_data, requestPtr, &payload_length); |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1471 | |
| 1472 | ASSERT_EQ(rc, 0); |
| 1473 | EXPECT_EQ( |
| 1474 | 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg))); |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1475 | EXPECT_EQ(payload_length, requestMsgLength); |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1476 | } |
| 1477 | #endif |
| 1478 | |
| 1479 | #ifdef LIBPLDM_API_TESTING |
| 1480 | TEST(EncodeNegotiateTransferParamsRequest, BadTestUnAllocatedPtrParams) |
| 1481 | { |
| 1482 | int rc; |
| 1483 | uint8_t instance_id = 0; |
| 1484 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1485 | 0x0001, // BE 256 |
| 1486 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1487 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1488 | static constexpr const size_t requestMsgLength = |
| 1489 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES; |
| 1490 | |
| 1491 | PLDM_MSG_DEFINE_P(requestPtr, requestMsgLength); |
| 1492 | size_t payload_length = requestMsgLength; |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1493 | rc = encode_pldm_base_negotiate_transfer_params_req( |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1494 | instance_id, nullptr, requestPtr, &payload_length); |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1495 | EXPECT_EQ(rc, -EINVAL); |
| 1496 | |
| 1497 | rc = encode_pldm_base_negotiate_transfer_params_req( |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1498 | instance_id, &req_data, nullptr, &payload_length); |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1499 | EXPECT_EQ(rc, -EINVAL); |
| 1500 | } |
| 1501 | #endif |
| 1502 | |
| 1503 | #ifdef LIBPLDM_API_TESTING |
| 1504 | TEST(EncodeNegotiateTransferParamsRequest, |
| 1505 | BadTestInvalidExpectedOutputMsgLength) |
| 1506 | { |
| 1507 | int rc; |
| 1508 | uint8_t instance_id = 0; |
| 1509 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1510 | 0x0001, // BE 256 |
| 1511 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1512 | |
| 1513 | PLDM_MSG_DEFINE_P(requestPtr, |
| 1514 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1515 | |
Chau Ly | e98a72f | 2025-08-15 03:38:50 +0000 | [diff] [blame] | 1516 | size_t payload_length = 1; |
| 1517 | rc = encode_pldm_base_negotiate_transfer_params_req( |
| 1518 | instance_id, &req_data, requestPtr, &payload_length); |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1519 | EXPECT_EQ(rc, -EOVERFLOW); |
| 1520 | } |
| 1521 | #endif |
| 1522 | |
| 1523 | #ifdef LIBPLDM_API_TESTING |
| 1524 | TEST(DecodeNegotiateTransferParamsResponse, GoodTest) |
| 1525 | { |
| 1526 | uint8_t completionCode = PLDM_SUCCESS; |
| 1527 | uint16_t responderPartSize = 128; |
| 1528 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1529 | 0x00, 0x00, 0x00, 0x81}; |
| 1530 | |
| 1531 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1532 | |
| 1533 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1534 | int rc; |
| 1535 | |
| 1536 | static constexpr const size_t payload_length = |
| 1537 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1538 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1539 | |
| 1540 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1541 | ASSERT_EQ(rc, 0); |
| 1542 | |
| 1543 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1544 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1545 | rc = pldm_msgbuf_insert_array_uint8( |
| 1546 | buf, sizeof(resp_data.responder_protocol_support), |
| 1547 | responderProtocolSupport.data(), |
| 1548 | sizeof(resp_data.responder_protocol_support)); |
| 1549 | EXPECT_EQ(rc, 0); |
| 1550 | |
| 1551 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1552 | |
| 1553 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1554 | responseMsg, payload_length, &resp_data); |
| 1555 | |
| 1556 | ASSERT_EQ(rc, 0); |
| 1557 | EXPECT_EQ(resp_data.completion_code, completionCode); |
| 1558 | EXPECT_EQ(resp_data.responder_part_size, responderPartSize); |
| 1559 | EXPECT_EQ(0, memcmp(responderProtocolSupport.data(), |
| 1560 | resp_data.responder_protocol_support, |
| 1561 | sizeof(resp_data.responder_protocol_support))); |
| 1562 | } |
| 1563 | #endif |
| 1564 | |
| 1565 | #ifdef LIBPLDM_API_TESTING |
| 1566 | TEST(DecodeNegotiateTransferParamsResponse, BadTestUnAllocatedPtrParams) |
| 1567 | { |
| 1568 | uint8_t completionCode = PLDM_SUCCESS; |
| 1569 | uint16_t responderPartSize = 128; |
| 1570 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1571 | 0x00, 0x00, 0x00, 0x81}; |
| 1572 | |
| 1573 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1574 | |
| 1575 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1576 | int rc; |
| 1577 | |
| 1578 | static constexpr const size_t payload_length = |
| 1579 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1580 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1581 | |
| 1582 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1583 | ASSERT_EQ(rc, 0); |
| 1584 | |
| 1585 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1586 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1587 | rc = pldm_msgbuf_insert_array_uint8( |
| 1588 | buf, sizeof(resp_data.responder_protocol_support), |
| 1589 | responderProtocolSupport.data(), |
| 1590 | sizeof(resp_data.responder_protocol_support)); |
| 1591 | EXPECT_EQ(rc, 0); |
| 1592 | |
| 1593 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1594 | |
| 1595 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1596 | nullptr, payload_length, &resp_data); |
| 1597 | |
| 1598 | EXPECT_EQ(rc, -EINVAL); |
| 1599 | |
| 1600 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1601 | responseMsg, payload_length, nullptr); |
| 1602 | |
| 1603 | EXPECT_EQ(rc, -EINVAL); |
| 1604 | } |
| 1605 | #endif |
| 1606 | |
| 1607 | #ifdef LIBPLDM_API_TESTING |
| 1608 | TEST(DecodeNegotiateTransferParamsResponse, |
| 1609 | BadTestInvalidExpectedInputMsgLength) |
| 1610 | { |
| 1611 | uint8_t completionCode = PLDM_SUCCESS; |
| 1612 | uint16_t responderPartSize = 128; |
| 1613 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1614 | 0x00, 0x00, 0x00, 0x81}; |
| 1615 | |
| 1616 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1617 | |
| 1618 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1619 | int rc; |
| 1620 | |
| 1621 | static constexpr const size_t payload_length = |
| 1622 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1623 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1624 | |
| 1625 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1626 | ASSERT_EQ(rc, 0); |
| 1627 | |
| 1628 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1629 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1630 | rc = pldm_msgbuf_insert_array_uint8( |
| 1631 | buf, sizeof(resp_data.responder_protocol_support), |
| 1632 | responderProtocolSupport.data(), |
| 1633 | sizeof(resp_data.responder_protocol_support)); |
| 1634 | EXPECT_EQ(rc, 0); |
| 1635 | |
| 1636 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1637 | |
| 1638 | rc = decode_pldm_base_negotiate_transfer_params_resp(responseMsg, 0, |
| 1639 | &resp_data); |
| 1640 | |
| 1641 | EXPECT_EQ(rc, -EOVERFLOW); |
| 1642 | } |
Andrew Jeffery | edb8826 | 2025-08-14 05:02:21 +0000 | [diff] [blame] | 1643 | #endif |