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 | |
| 543 | TEST(MultipartReceive, testDecodeRequestPass) |
| 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; |
| 551 | uint8_t pldm_type = 0x0; |
| 552 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 553 | uint32_t transfer_ctx; |
| 554 | uint32_t transfer_handle; |
| 555 | uint32_t section_offset; |
| 556 | uint32_t section_length; |
| 557 | |
| 558 | // Header values don't matter for this test. |
| 559 | pldm_msg_hdr hdr{}; |
| 560 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 561 | // ordering. |
| 562 | pldm_multipart_receive_req req_pkt = { |
| 563 | .pldm_type = kPldmType, |
| 564 | .transfer_opflag = kFlag, |
| 565 | .transfer_ctx = kTransferCtx, |
| 566 | .transfer_handle = kTransferHandle, |
| 567 | .section_offset = kSectionOffset, |
| 568 | .section_length = kSectionLength, |
| 569 | }; |
| 570 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 571 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 572 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 573 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 574 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 575 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 576 | int rc = decode_multipart_receive_req( |
| 577 | pldm_request, req.size() - hdrSize, &pldm_type, &flag, &transfer_ctx, |
| 578 | &transfer_handle, §ion_offset, §ion_length); |
| 579 | |
| 580 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 581 | EXPECT_EQ(pldm_type, kPldmType); |
| 582 | EXPECT_EQ(flag, kFlag); |
| 583 | EXPECT_EQ(transfer_ctx, kTransferCtx); |
| 584 | EXPECT_EQ(transfer_handle, kTransferHandle); |
| 585 | EXPECT_EQ(section_offset, kSectionOffset); |
| 586 | EXPECT_EQ(section_length, kSectionLength); |
| 587 | } |
| 588 | |
| 589 | TEST(MultipartReceive, testDecodeRequestFailNullData) |
| 590 | { |
| 591 | EXPECT_EQ(decode_multipart_receive_req(NULL, 0, NULL, NULL, NULL, NULL, |
| 592 | NULL, NULL), |
| 593 | PLDM_ERROR_INVALID_DATA); |
| 594 | } |
| 595 | |
| 596 | TEST(MultipartReceive, testDecodeRequestFailBadLength) |
| 597 | { |
| 598 | constexpr uint8_t kPldmType = PLDM_BASE; |
| 599 | constexpr uint8_t kFlag = PLDM_XFER_FIRST_PART; |
| 600 | uint8_t pldm_type; |
| 601 | uint8_t flag; |
| 602 | uint32_t transfer_ctx; |
| 603 | uint32_t transfer_handle; |
| 604 | uint32_t section_offset; |
| 605 | uint32_t section_length; |
| 606 | |
| 607 | // Header values don't matter for this test. |
| 608 | pldm_msg_hdr hdr{}; |
| 609 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 610 | // ordering. |
| 611 | pldm_multipart_receive_req req_pkt{}; |
| 612 | req_pkt.pldm_type = kPldmType; |
| 613 | req_pkt.transfer_opflag = kFlag; |
| 614 | |
| 615 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 616 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 617 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 618 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 619 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 620 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 621 | EXPECT_EQ(decode_multipart_receive_req( |
| 622 | pldm_request, (req.size() - hdrSize) + 1, &pldm_type, &flag, |
| 623 | &transfer_ctx, &transfer_handle, §ion_offset, |
| 624 | §ion_length), |
| 625 | PLDM_ERROR_INVALID_LENGTH); |
| 626 | } |
| 627 | |
| 628 | TEST(MultipartReceive, testDecodeRequestFailBadPldmType) |
| 629 | { |
| 630 | constexpr uint8_t kPldmType = 0xff; |
| 631 | constexpr uint8_t kFlag = PLDM_XFER_FIRST_PART; |
| 632 | uint8_t pldm_type; |
| 633 | uint8_t flag; |
| 634 | uint32_t transfer_ctx; |
| 635 | uint32_t transfer_handle; |
| 636 | uint32_t section_offset; |
| 637 | uint32_t section_length; |
| 638 | |
| 639 | // Header values don't matter for this test. |
| 640 | pldm_msg_hdr hdr{}; |
| 641 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 642 | // ordering. |
| 643 | pldm_multipart_receive_req req_pkt{}; |
| 644 | req_pkt.pldm_type = kPldmType; |
| 645 | req_pkt.transfer_opflag = kFlag; |
| 646 | |
| 647 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 648 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 649 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 650 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 651 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 652 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 653 | EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize, |
| 654 | &pldm_type, &flag, &transfer_ctx, |
| 655 | &transfer_handle, §ion_offset, |
| 656 | §ion_length), |
| 657 | PLDM_ERROR_INVALID_PLDM_TYPE); |
| 658 | } |
| 659 | |
| 660 | TEST(MultipartReceive, testDecodeRequestFailBadTransferFlag) |
| 661 | { |
| 662 | constexpr uint8_t kPldmType = PLDM_BASE; |
| 663 | constexpr uint8_t kFlag = PLDM_XFER_CURRENT_PART + 0x10; |
| 664 | uint8_t pldm_type; |
| 665 | uint8_t flag; |
| 666 | uint32_t transfer_ctx; |
| 667 | uint32_t transfer_handle; |
| 668 | uint32_t section_offset; |
| 669 | uint32_t section_length; |
| 670 | |
| 671 | // Header values don't matter for this test. |
| 672 | pldm_msg_hdr hdr{}; |
| 673 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 674 | // ordering. |
| 675 | pldm_multipart_receive_req req_pkt{}; |
| 676 | req_pkt.pldm_type = kPldmType; |
| 677 | req_pkt.transfer_opflag = kFlag; |
| 678 | |
| 679 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 680 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 681 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 682 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 683 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 684 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 685 | EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize, |
| 686 | &pldm_type, &flag, &transfer_ctx, |
| 687 | &transfer_handle, §ion_offset, |
| 688 | §ion_length), |
| 689 | PLDM_INVALID_TRANSFER_OPERATION_FLAG); |
| 690 | } |
| 691 | |
| 692 | TEST(MultipartReceive, testDecodeRequestFailBadOffset) |
| 693 | { |
| 694 | constexpr uint8_t kPldmType = PLDM_BASE; |
| 695 | constexpr uint8_t kFlag = PLDM_XFER_NEXT_PART; |
| 696 | constexpr uint32_t kTransferHandle = 0x01; |
| 697 | constexpr uint32_t kSectionOffset = 0x0; |
| 698 | uint8_t pldm_type; |
| 699 | uint8_t flag; |
| 700 | uint32_t transfer_ctx; |
| 701 | uint32_t transfer_handle; |
| 702 | uint32_t section_offset; |
| 703 | uint32_t section_length; |
| 704 | |
| 705 | // Header values don't matter for this test. |
| 706 | pldm_msg_hdr hdr{}; |
| 707 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 708 | // ordering. |
| 709 | pldm_multipart_receive_req req_pkt{}; |
| 710 | req_pkt.pldm_type = kPldmType; |
| 711 | req_pkt.transfer_opflag = kFlag; |
| 712 | req_pkt.transfer_handle = kTransferHandle; |
| 713 | req_pkt.section_offset = kSectionOffset; |
| 714 | |
| 715 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 716 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 717 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 718 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 719 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 720 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 721 | EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize, |
| 722 | &pldm_type, &flag, &transfer_ctx, |
| 723 | &transfer_handle, §ion_offset, |
| 724 | §ion_length), |
| 725 | PLDM_ERROR_INVALID_DATA); |
| 726 | } |
| 727 | |
| 728 | TEST(MultipartReceive, testDecodeRequestFailBadHandle) |
| 729 | { |
| 730 | constexpr uint8_t kPldmType = PLDM_BASE; |
| 731 | constexpr uint8_t kFlag = PLDM_XFER_NEXT_PART; |
| 732 | constexpr uint32_t kSectionOffset = 0x100; |
| 733 | constexpr uint32_t kTransferHandle = 0x0; |
| 734 | uint8_t pldm_type; |
| 735 | uint8_t flag; |
| 736 | uint32_t transfer_ctx; |
| 737 | uint32_t transfer_handle; |
| 738 | uint32_t section_offset; |
| 739 | uint32_t section_length; |
| 740 | |
| 741 | // Header values don't matter for this test. |
| 742 | pldm_msg_hdr hdr{}; |
| 743 | // Assign values to the packet struct and memcpy to ensure correct byte |
| 744 | // ordering. |
| 745 | pldm_multipart_receive_req req_pkt{}; |
| 746 | req_pkt.pldm_type = kPldmType; |
| 747 | req_pkt.transfer_opflag = kFlag; |
| 748 | req_pkt.transfer_handle = kTransferHandle; |
| 749 | req_pkt.section_offset = kSectionOffset; |
| 750 | |
| 751 | std::vector<uint8_t> req(sizeof(hdr) + PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 752 | std::memcpy(req.data(), &hdr, sizeof(hdr)); |
| 753 | std::memcpy(req.data() + sizeof(hdr), &req_pkt, sizeof(req_pkt)); |
| 754 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 755 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 756 | pldm_msg* pldm_request = reinterpret_cast<pldm_msg*>(req.data()); |
| 757 | EXPECT_EQ(decode_multipart_receive_req(pldm_request, req.size() - hdrSize, |
| 758 | &pldm_type, &flag, &transfer_ctx, |
| 759 | &transfer_handle, §ion_offset, |
| 760 | §ion_length), |
| 761 | PLDM_ERROR_INVALID_DATA); |
| 762 | } |
| 763 | |
Dung Cao | 8836784 | 2024-10-16 06:32:49 +0000 | [diff] [blame] | 764 | #ifdef LIBPLDM_API_TESTING |
| 765 | TEST(EncodeMultipartReceiveRequest, GoodTest) |
| 766 | { |
| 767 | uint8_t instance_id = 0; |
| 768 | |
| 769 | const struct pldm_multipart_receive_req req_data = { |
| 770 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 771 | |
| 772 | std::array<uint8_t, PLDM_MULTIPART_RECEIVE_REQ_BYTES> requestMsg = { |
| 773 | PLDM_BASE, PLDM_XFER_FIRST_PART, |
| 774 | 0x01, 0x00, |
| 775 | 0x00, 0x00, |
| 776 | 0x10, 0x00, |
| 777 | 0x00, 0x00, |
| 778 | 0x00, 0x00, |
| 779 | 0x00, 0x00, |
| 780 | 0x10, 0x00, |
| 781 | 0x00, 0x00}; |
| 782 | |
| 783 | PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 784 | auto rc = encode_base_multipart_receive_req( |
| 785 | instance_id, &req_data, requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 786 | |
| 787 | ASSERT_EQ(rc, 0); |
| 788 | EXPECT_EQ( |
| 789 | 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg))); |
| 790 | } |
| 791 | #endif |
| 792 | |
| 793 | #ifdef LIBPLDM_API_TESTING |
| 794 | TEST(EncodeMultipartReceiveRequest, BadTestUnAllocatedPtrParams) |
| 795 | { |
| 796 | uint8_t instance_id = 0; |
| 797 | int rc; |
| 798 | |
| 799 | const struct pldm_multipart_receive_req req_data = { |
| 800 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 801 | |
| 802 | PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 803 | rc = encode_base_multipart_receive_req(instance_id, nullptr, requestPtr, |
| 804 | PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 805 | EXPECT_EQ(rc, -EINVAL); |
| 806 | |
| 807 | rc = encode_base_multipart_receive_req(instance_id, &req_data, nullptr, |
| 808 | PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 809 | EXPECT_EQ(rc, -EINVAL); |
| 810 | } |
| 811 | #endif |
| 812 | |
| 813 | #ifdef LIBPLDM_API_TESTING |
| 814 | TEST(EncodeMultipartReceiveRequest, BadTestInvalidExpectedOutputMsgLength) |
| 815 | { |
| 816 | uint8_t instance_id = 0; |
| 817 | int rc; |
| 818 | |
| 819 | const struct pldm_multipart_receive_req req_data = { |
| 820 | PLDM_BASE, PLDM_XFER_FIRST_PART, 0x01, 0x10, 0x00, 0x10}; |
| 821 | |
| 822 | PLDM_MSG_DEFINE_P(requestPtr, PLDM_MULTIPART_RECEIVE_REQ_BYTES); |
| 823 | |
| 824 | rc = encode_base_multipart_receive_req(instance_id, &req_data, requestPtr, |
| 825 | 1); |
| 826 | EXPECT_EQ(rc, -EOVERFLOW); |
| 827 | } |
| 828 | #endif |
| 829 | |
| 830 | #ifdef LIBPLDM_API_TESTING |
| 831 | TEST(DecodeMultipartReceiveResponse, GoodTest) |
| 832 | { |
| 833 | uint8_t completionCode = PLDM_SUCCESS; |
| 834 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 835 | uint32_t nextDataTransferHandle = 0x15; |
| 836 | static constexpr const uint32_t dataLength = 9; |
| 837 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 838 | uint32_t dataIntegrityChecksum = 0x3C; |
| 839 | |
| 840 | struct pldm_multipart_receive_resp resp_data = {}; |
| 841 | |
| 842 | PLDM_MSGBUF_DEFINE_P(buf); |
| 843 | int rc; |
| 844 | |
| 845 | static constexpr const size_t payload_length = |
| 846 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 847 | sizeof(dataIntegrityChecksum); |
| 848 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 849 | |
| 850 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 851 | ASSERT_EQ(rc, 0); |
| 852 | |
| 853 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 854 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 855 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 856 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 857 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 858 | dataLength); |
| 859 | EXPECT_EQ(rc, 0); |
| 860 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 861 | |
| 862 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 863 | |
| 864 | uint32_t respDataIntegrityChecksum = 0; |
| 865 | |
| 866 | rc = decode_base_multipart_receive_resp( |
| 867 | responseMsg, payload_length, &resp_data, &respDataIntegrityChecksum); |
| 868 | |
| 869 | ASSERT_EQ(rc, 0); |
| 870 | EXPECT_EQ(resp_data.completion_code, completionCode); |
| 871 | EXPECT_EQ(resp_data.transfer_flag, transferFlag); |
| 872 | EXPECT_EQ(resp_data.next_transfer_handle, nextDataTransferHandle); |
| 873 | EXPECT_EQ(resp_data.data.length, dataLength); |
| 874 | EXPECT_EQ(0, |
| 875 | memcmp(data.data(), resp_data.data.ptr, resp_data.data.length)); |
| 876 | EXPECT_EQ(respDataIntegrityChecksum, dataIntegrityChecksum); |
| 877 | } |
| 878 | #endif |
| 879 | |
| 880 | #ifdef LIBPLDM_API_TESTING |
| 881 | TEST(DecodeMultipartReceiveResponse, BadTestUnAllocatedPtrParams) |
| 882 | { |
| 883 | uint8_t completionCode = PLDM_SUCCESS; |
| 884 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 885 | uint32_t nextDataTransferHandle = 0x15; |
| 886 | static constexpr const uint32_t dataLength = 9; |
| 887 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 888 | uint32_t dataIntegrityChecksum = 0x3C; |
| 889 | |
| 890 | struct pldm_multipart_receive_resp resp_data = {}; |
| 891 | |
| 892 | PLDM_MSGBUF_DEFINE_P(buf); |
| 893 | int rc; |
| 894 | |
| 895 | static constexpr const size_t payload_length = |
| 896 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 897 | sizeof(dataIntegrityChecksum); |
| 898 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 899 | |
| 900 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 901 | ASSERT_EQ(rc, 0); |
| 902 | |
| 903 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 904 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 905 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 906 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 907 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 908 | dataLength); |
| 909 | EXPECT_EQ(rc, 0); |
| 910 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 911 | |
| 912 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 913 | |
| 914 | uint32_t respDataIntegrityChecksum = 0; |
| 915 | |
| 916 | rc = decode_base_multipart_receive_resp(nullptr, payload_length, &resp_data, |
| 917 | &respDataIntegrityChecksum); |
| 918 | |
| 919 | EXPECT_EQ(rc, -EINVAL); |
| 920 | |
| 921 | rc = decode_base_multipart_receive_resp( |
| 922 | responseMsg, payload_length, nullptr, &respDataIntegrityChecksum); |
| 923 | |
| 924 | EXPECT_EQ(rc, -EINVAL); |
| 925 | } |
| 926 | #endif |
| 927 | |
| 928 | #ifdef LIBPLDM_API_TESTING |
| 929 | TEST(DecodeMultipartReceiveResponse, BadTestInvalidExpectedInputMsgLength) |
| 930 | { |
| 931 | uint8_t completionCode = PLDM_SUCCESS; |
| 932 | uint8_t transferFlag = PLDM_BASE_MULTIPART_RECEIVE_TRANSFER_FLAG_END; |
| 933 | uint32_t nextDataTransferHandle = 0x15; |
| 934 | static constexpr const uint32_t dataLength = 9; |
| 935 | std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 936 | uint32_t dataIntegrityChecksum = 0x3C; |
| 937 | |
| 938 | struct pldm_multipart_receive_resp resp_data = {}; |
| 939 | |
| 940 | PLDM_MSGBUF_DEFINE_P(buf); |
| 941 | int rc; |
| 942 | |
| 943 | static constexpr const size_t payload_length = |
| 944 | PLDM_BASE_MULTIPART_RECEIVE_RESP_MIN_BYTES + dataLength + |
| 945 | sizeof(dataIntegrityChecksum); |
| 946 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 947 | |
| 948 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 949 | ASSERT_EQ(rc, 0); |
| 950 | |
| 951 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 952 | pldm_msgbuf_insert_uint8(buf, transferFlag); |
| 953 | pldm_msgbuf_insert_uint32(buf, nextDataTransferHandle); |
| 954 | pldm_msgbuf_insert_uint32(buf, dataLength); |
| 955 | rc = pldm_msgbuf_insert_array_uint8(buf, dataLength, data.data(), |
| 956 | dataLength); |
| 957 | EXPECT_EQ(rc, 0); |
| 958 | pldm_msgbuf_insert_uint32(buf, dataIntegrityChecksum); |
| 959 | |
| 960 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 961 | |
| 962 | uint32_t respDataIntegrityChecksum = 0; |
| 963 | |
| 964 | rc = decode_base_multipart_receive_resp(responseMsg, 0, &resp_data, |
| 965 | &respDataIntegrityChecksum); |
| 966 | |
| 967 | EXPECT_EQ(rc, -EOVERFLOW); |
| 968 | } |
| 969 | #endif |
| 970 | |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 971 | TEST(CcOnlyResponse, testEncode) |
| 972 | { |
| 973 | struct pldm_msg responseMsg; |
| 974 | |
| 975 | auto rc = |
| 976 | encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/, |
Manojkiran Eda | 9e3a5d4 | 2024-06-17 16:06:42 +0530 | [diff] [blame] | 977 | 3 /*completion code*/, &responseMsg); |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 978 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 979 | |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 980 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 981 | auto p = reinterpret_cast<uint8_t*>(&responseMsg); |
| 982 | EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)), |
| 983 | ElementsAreArray({0, 1, 2, 3})); |
| 984 | |
| 985 | rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg); |
| 986 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 987 | |
| 988 | rc = encode_cc_only_resp(0, 1, 2, 3, nullptr); |
| 989 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 990 | } |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 991 | |
| 992 | TEST(SetTID, testGoodEncodeRequest) |
| 993 | { |
| 994 | uint8_t instanceId = 0; |
| 995 | uint8_t tid = 0x01; |
| 996 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 997 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 998 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 999 | |
| 1000 | auto rc = encode_set_tid_req(instanceId, tid, request); |
| 1001 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1002 | |
| 1003 | EXPECT_EQ(request->hdr.command, PLDM_SET_TID); |
| 1004 | EXPECT_EQ(request->hdr.type, PLDM_BASE); |
| 1005 | EXPECT_EQ(request->hdr.request, 1); |
| 1006 | EXPECT_EQ(request->hdr.datagram, 0); |
| 1007 | EXPECT_EQ(request->hdr.instance_id, instanceId); |
| 1008 | EXPECT_EQ(0, memcmp(request->payload, &tid, sizeof(tid))); |
| 1009 | } |
| 1010 | |
| 1011 | TEST(SetTID, testBadEncodeRequest) |
| 1012 | { |
| 1013 | uint8_t tid = 0x01; |
| 1014 | std::array<uint8_t, sizeof(pldm_msg_hdr) + sizeof(tid)> requestMsg{}; |
Andrew Jeffery | d0ba43a | 2024-09-09 15:50:17 +0930 | [diff] [blame] | 1015 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Gilbert Chen | 6c9c917 | 2022-10-18 17:07:29 +0800 | [diff] [blame] | 1016 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1017 | |
| 1018 | auto rc = encode_set_tid_req(0, tid, nullptr); |
| 1019 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1020 | |
| 1021 | rc = encode_set_tid_req(0, 0, request); |
| 1022 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1023 | |
| 1024 | rc = encode_set_tid_req(0, 0xff, request); |
| 1025 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1026 | } |
Andrew Jeffery | 91c06e9 | 2023-09-22 15:22:54 +0930 | [diff] [blame] | 1027 | |
| 1028 | #ifdef LIBPLDM_API_TESTING |
| 1029 | TEST(PldmMsgHdr, correlateSuccess) |
| 1030 | { |
| 1031 | static const struct pldm_msg_hdr req = { |
| 1032 | .instance_id = 0, |
| 1033 | .reserved = 0, |
| 1034 | .datagram = 0, |
| 1035 | .request = 1, |
| 1036 | .type = 0, |
| 1037 | .header_ver = 1, |
| 1038 | .command = 0x01, |
| 1039 | }; |
| 1040 | static const struct pldm_msg_hdr resp = { |
| 1041 | .instance_id = 0, |
| 1042 | .reserved = 0, |
| 1043 | .datagram = 0, |
| 1044 | .request = 0, |
| 1045 | .type = 0, |
| 1046 | .header_ver = 1, |
| 1047 | .command = 0x01, |
| 1048 | }; |
| 1049 | |
| 1050 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), true); |
| 1051 | } |
| 1052 | #endif |
| 1053 | |
| 1054 | #ifdef LIBPLDM_API_TESTING |
| 1055 | TEST(PldmMsgHdr, correlateFailInstanceID) |
| 1056 | { |
| 1057 | static const struct pldm_msg_hdr req = { |
| 1058 | .instance_id = 0, |
| 1059 | .reserved = 0, |
| 1060 | .datagram = 0, |
| 1061 | .request = 1, |
| 1062 | .type = 0, |
| 1063 | .header_ver = 1, |
| 1064 | .command = 0x01, |
| 1065 | }; |
| 1066 | static const struct pldm_msg_hdr resp = { |
| 1067 | .instance_id = 1, |
| 1068 | .reserved = 0, |
| 1069 | .datagram = 0, |
| 1070 | .request = 0, |
| 1071 | .type = 0, |
| 1072 | .header_ver = 1, |
| 1073 | .command = 0x01, |
| 1074 | }; |
| 1075 | |
| 1076 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1077 | } |
| 1078 | #endif |
| 1079 | |
| 1080 | #ifdef LIBPLDM_API_TESTING |
| 1081 | TEST(PldmMsgHdr, correlateFailRequest) |
| 1082 | { |
| 1083 | static const struct pldm_msg_hdr req = { |
| 1084 | .instance_id = 0, |
| 1085 | .reserved = 0, |
| 1086 | .datagram = 0, |
| 1087 | .request = 1, |
| 1088 | .type = 0, |
| 1089 | .header_ver = 1, |
| 1090 | .command = 0x01, |
| 1091 | }; |
| 1092 | static const struct pldm_msg_hdr resp = { |
| 1093 | .instance_id = 0, |
| 1094 | .reserved = 0, |
| 1095 | .datagram = 0, |
| 1096 | .request = 1, |
| 1097 | .type = 0, |
| 1098 | .header_ver = 1, |
| 1099 | .command = 0x01, |
| 1100 | }; |
| 1101 | |
| 1102 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1103 | } |
| 1104 | #endif |
| 1105 | |
| 1106 | #ifdef LIBPLDM_API_TESTING |
| 1107 | TEST(PldmMsgHdr, correlateFailType) |
| 1108 | { |
| 1109 | static const struct pldm_msg_hdr req = { |
| 1110 | .instance_id = 0, |
| 1111 | .reserved = 0, |
| 1112 | .datagram = 0, |
| 1113 | .request = 1, |
| 1114 | .type = 0, |
| 1115 | .header_ver = 1, |
| 1116 | .command = 0x01, |
| 1117 | }; |
| 1118 | static const struct pldm_msg_hdr resp = { |
| 1119 | .instance_id = 0, |
| 1120 | .reserved = 0, |
| 1121 | .datagram = 0, |
| 1122 | .request = 0, |
| 1123 | .type = 1, |
| 1124 | .header_ver = 1, |
| 1125 | .command = 0x01, |
| 1126 | }; |
| 1127 | |
| 1128 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1129 | } |
| 1130 | #endif |
| 1131 | |
| 1132 | #ifdef LIBPLDM_API_TESTING |
| 1133 | TEST(PldmMsgHdr, correlateFailCommand) |
| 1134 | { |
| 1135 | static const struct pldm_msg_hdr req = { |
| 1136 | .instance_id = 0, |
| 1137 | .reserved = 0, |
| 1138 | .datagram = 0, |
| 1139 | .request = 1, |
| 1140 | .type = 0, |
| 1141 | .header_ver = 1, |
| 1142 | .command = 0x01, |
| 1143 | }; |
| 1144 | static const struct pldm_msg_hdr resp = { |
| 1145 | .instance_id = 0, |
| 1146 | .reserved = 0, |
| 1147 | .datagram = 0, |
| 1148 | .request = 0, |
| 1149 | .type = 0, |
| 1150 | .header_ver = 1, |
| 1151 | .command = 0x02, |
| 1152 | }; |
| 1153 | |
| 1154 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
| 1158 | #ifdef LIBPLDM_API_TESTING |
| 1159 | TEST(PldmMsgHdr, correlateFailRequestIsResponse) |
| 1160 | { |
| 1161 | static const struct pldm_msg_hdr req = { |
| 1162 | .instance_id = 0, |
| 1163 | .reserved = 0, |
| 1164 | .datagram = 0, |
| 1165 | .request = 0, |
| 1166 | .type = 0, |
| 1167 | .header_ver = 1, |
| 1168 | .command = 0x01, |
| 1169 | }; |
| 1170 | static const struct pldm_msg_hdr resp = { |
| 1171 | .instance_id = 0, |
| 1172 | .reserved = 0, |
| 1173 | .datagram = 0, |
| 1174 | .request = 0, |
| 1175 | .type = 0, |
| 1176 | .header_ver = 1, |
| 1177 | .command = 0x02, |
| 1178 | }; |
| 1179 | |
| 1180 | ASSERT_EQ(pldm_msg_hdr_correlate_response(&req, &resp), false); |
| 1181 | } |
| 1182 | #endif |
Chau Ly | 59dce78 | 2025-04-01 08:22:07 +0000 | [diff] [blame] | 1183 | |
| 1184 | #ifdef LIBPLDM_API_TESTING |
| 1185 | TEST(EncodeNegotiateTransferParamsRequest, GoodTest) |
| 1186 | { |
| 1187 | uint8_t instance_id = 0; |
| 1188 | |
| 1189 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1190 | 0x0001, // BE 256 |
| 1191 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1192 | |
| 1193 | std::array<uint8_t, PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES> |
| 1194 | requestMsg = {0x01, 0x00, // requester_part_size = 256 |
| 1195 | 0x00, 0x00, 0x00, 0x00, |
| 1196 | 0x00, 0x00, 0x00, 0x81}; // requester_protocol_support = |
| 1197 | // PLDM_BASE & PLDM_FILE |
| 1198 | |
| 1199 | PLDM_MSG_DEFINE_P(requestPtr, |
| 1200 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1201 | auto rc = encode_pldm_base_negotiate_transfer_params_req( |
| 1202 | instance_id, &req_data, requestPtr, |
| 1203 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1204 | |
| 1205 | ASSERT_EQ(rc, 0); |
| 1206 | EXPECT_EQ( |
| 1207 | 0, memcmp(requestPtr->payload, requestMsg.data(), sizeof(requestMsg))); |
| 1208 | } |
| 1209 | #endif |
| 1210 | |
| 1211 | #ifdef LIBPLDM_API_TESTING |
| 1212 | TEST(EncodeNegotiateTransferParamsRequest, BadTestUnAllocatedPtrParams) |
| 1213 | { |
| 1214 | int rc; |
| 1215 | uint8_t instance_id = 0; |
| 1216 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1217 | 0x0001, // BE 256 |
| 1218 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1219 | |
| 1220 | PLDM_MSG_DEFINE_P(requestPtr, |
| 1221 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1222 | rc = encode_pldm_base_negotiate_transfer_params_req( |
| 1223 | instance_id, nullptr, requestPtr, |
| 1224 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1225 | EXPECT_EQ(rc, -EINVAL); |
| 1226 | |
| 1227 | rc = encode_pldm_base_negotiate_transfer_params_req( |
| 1228 | instance_id, &req_data, nullptr, |
| 1229 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1230 | EXPECT_EQ(rc, -EINVAL); |
| 1231 | } |
| 1232 | #endif |
| 1233 | |
| 1234 | #ifdef LIBPLDM_API_TESTING |
| 1235 | TEST(EncodeNegotiateTransferParamsRequest, |
| 1236 | BadTestInvalidExpectedOutputMsgLength) |
| 1237 | { |
| 1238 | int rc; |
| 1239 | uint8_t instance_id = 0; |
| 1240 | const struct pldm_base_negotiate_transfer_params_req req_data = { |
| 1241 | 0x0001, // BE 256 |
| 1242 | {{0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x00}, {0x81}}}; |
| 1243 | |
| 1244 | PLDM_MSG_DEFINE_P(requestPtr, |
| 1245 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_REQ_BYTES); |
| 1246 | |
| 1247 | rc = encode_pldm_base_negotiate_transfer_params_req(instance_id, &req_data, |
| 1248 | requestPtr, 1); |
| 1249 | EXPECT_EQ(rc, -EOVERFLOW); |
| 1250 | } |
| 1251 | #endif |
| 1252 | |
| 1253 | #ifdef LIBPLDM_API_TESTING |
| 1254 | TEST(DecodeNegotiateTransferParamsResponse, GoodTest) |
| 1255 | { |
| 1256 | uint8_t completionCode = PLDM_SUCCESS; |
| 1257 | uint16_t responderPartSize = 128; |
| 1258 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1259 | 0x00, 0x00, 0x00, 0x81}; |
| 1260 | |
| 1261 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1262 | |
| 1263 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1264 | int rc; |
| 1265 | |
| 1266 | static constexpr const size_t payload_length = |
| 1267 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1268 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1269 | |
| 1270 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1271 | ASSERT_EQ(rc, 0); |
| 1272 | |
| 1273 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1274 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1275 | rc = pldm_msgbuf_insert_array_uint8( |
| 1276 | buf, sizeof(resp_data.responder_protocol_support), |
| 1277 | responderProtocolSupport.data(), |
| 1278 | sizeof(resp_data.responder_protocol_support)); |
| 1279 | EXPECT_EQ(rc, 0); |
| 1280 | |
| 1281 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1282 | |
| 1283 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1284 | responseMsg, payload_length, &resp_data); |
| 1285 | |
| 1286 | ASSERT_EQ(rc, 0); |
| 1287 | EXPECT_EQ(resp_data.completion_code, completionCode); |
| 1288 | EXPECT_EQ(resp_data.responder_part_size, responderPartSize); |
| 1289 | EXPECT_EQ(0, memcmp(responderProtocolSupport.data(), |
| 1290 | resp_data.responder_protocol_support, |
| 1291 | sizeof(resp_data.responder_protocol_support))); |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
| 1295 | #ifdef LIBPLDM_API_TESTING |
| 1296 | TEST(DecodeNegotiateTransferParamsResponse, BadTestUnAllocatedPtrParams) |
| 1297 | { |
| 1298 | uint8_t completionCode = PLDM_SUCCESS; |
| 1299 | uint16_t responderPartSize = 128; |
| 1300 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1301 | 0x00, 0x00, 0x00, 0x81}; |
| 1302 | |
| 1303 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1304 | |
| 1305 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1306 | int rc; |
| 1307 | |
| 1308 | static constexpr const size_t payload_length = |
| 1309 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1310 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1311 | |
| 1312 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1313 | ASSERT_EQ(rc, 0); |
| 1314 | |
| 1315 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1316 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1317 | rc = pldm_msgbuf_insert_array_uint8( |
| 1318 | buf, sizeof(resp_data.responder_protocol_support), |
| 1319 | responderProtocolSupport.data(), |
| 1320 | sizeof(resp_data.responder_protocol_support)); |
| 1321 | EXPECT_EQ(rc, 0); |
| 1322 | |
| 1323 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1324 | |
| 1325 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1326 | nullptr, payload_length, &resp_data); |
| 1327 | |
| 1328 | EXPECT_EQ(rc, -EINVAL); |
| 1329 | |
| 1330 | rc = decode_pldm_base_negotiate_transfer_params_resp( |
| 1331 | responseMsg, payload_length, nullptr); |
| 1332 | |
| 1333 | EXPECT_EQ(rc, -EINVAL); |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
| 1337 | #ifdef LIBPLDM_API_TESTING |
| 1338 | TEST(DecodeNegotiateTransferParamsResponse, |
| 1339 | BadTestInvalidExpectedInputMsgLength) |
| 1340 | { |
| 1341 | uint8_t completionCode = PLDM_SUCCESS; |
| 1342 | uint16_t responderPartSize = 128; |
| 1343 | std::array<uint8_t, 8> responderProtocolSupport = {0x00, 0x00, 0x00, 0x00, |
| 1344 | 0x00, 0x00, 0x00, 0x81}; |
| 1345 | |
| 1346 | struct pldm_base_negotiate_transfer_params_resp resp_data = {}; |
| 1347 | |
| 1348 | PLDM_MSGBUF_DEFINE_P(buf); |
| 1349 | int rc; |
| 1350 | |
| 1351 | static constexpr const size_t payload_length = |
| 1352 | PLDM_BASE_NEGOTIATE_TRANSFER_PARAMETERS_RESP_BYTES; |
| 1353 | PLDM_MSG_DEFINE_P(responseMsg, payload_length); |
| 1354 | |
| 1355 | rc = pldm_msgbuf_init_errno(buf, 0, responseMsg->payload, payload_length); |
| 1356 | ASSERT_EQ(rc, 0); |
| 1357 | |
| 1358 | pldm_msgbuf_insert_uint8(buf, completionCode); |
| 1359 | pldm_msgbuf_insert_uint16(buf, responderPartSize); |
| 1360 | rc = pldm_msgbuf_insert_array_uint8( |
| 1361 | buf, sizeof(resp_data.responder_protocol_support), |
| 1362 | responderProtocolSupport.data(), |
| 1363 | sizeof(resp_data.responder_protocol_support)); |
| 1364 | EXPECT_EQ(rc, 0); |
| 1365 | |
| 1366 | ASSERT_EQ(pldm_msgbuf_complete_consumed(buf), 0); |
| 1367 | |
| 1368 | rc = decode_pldm_base_negotiate_transfer_params_resp(responseMsg, 0, |
| 1369 | &resp_data); |
| 1370 | |
| 1371 | EXPECT_EQ(rc, -EOVERFLOW); |
| 1372 | } |
| 1373 | #endif |