Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <array> |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 4 | #include <cstring> |
| 5 | #include <vector> |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 6 | |
| 7 | #include "libpldm/base.h" |
| 8 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 9 | #include <gmock/gmock.h> |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 10 | #include <gtest/gtest.h> |
| 11 | |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 12 | using testing::ElementsAreArray; |
| 13 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 14 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 15 | |
Tom Joseph | 4125104 | 2019-02-07 16:17:07 +0530 | [diff] [blame] | 16 | TEST(PackPLDMMessage, BadPathTest) |
| 17 | { |
| 18 | struct pldm_header_info hdr; |
| 19 | struct pldm_header_info* hdr_ptr = NULL; |
| 20 | pldm_msg_hdr msg{}; |
| 21 | |
| 22 | // PLDM header information pointer is NULL |
| 23 | auto rc = pack_pldm_header(hdr_ptr, &msg); |
| 24 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 25 | |
| 26 | // PLDM message pointer is NULL |
| 27 | rc = pack_pldm_header(&hdr, nullptr); |
| 28 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 29 | |
| 30 | // PLDM header information pointer and PLDM message pointer is NULL |
| 31 | rc = pack_pldm_header(hdr_ptr, nullptr); |
| 32 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 33 | |
| 34 | // RESERVED message type |
| 35 | hdr.msg_type = PLDM_RESERVED; |
| 36 | rc = pack_pldm_header(&hdr, &msg); |
| 37 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 38 | |
| 39 | // Instance ID out of range |
| 40 | hdr.msg_type = PLDM_REQUEST; |
| 41 | hdr.instance = 32; |
| 42 | rc = pack_pldm_header(&hdr, &msg); |
| 43 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 44 | |
| 45 | // PLDM type out of range |
| 46 | hdr.msg_type = PLDM_REQUEST; |
| 47 | hdr.instance = 31; |
| 48 | hdr.pldm_type = 64; |
| 49 | rc = pack_pldm_header(&hdr, &msg); |
| 50 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_PLDM_TYPE); |
| 51 | } |
| 52 | |
| 53 | TEST(PackPLDMMessage, RequestMessageGoodPath) |
| 54 | { |
| 55 | struct pldm_header_info hdr; |
| 56 | pldm_msg_hdr msg{}; |
| 57 | |
| 58 | // Message type is REQUEST and lower range of the field values |
| 59 | hdr.msg_type = PLDM_REQUEST; |
| 60 | hdr.instance = 0; |
| 61 | hdr.pldm_type = 0; |
| 62 | hdr.command = 0; |
| 63 | |
| 64 | auto rc = pack_pldm_header(&hdr, &msg); |
| 65 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 66 | ASSERT_EQ(msg.request, 1); |
| 67 | ASSERT_EQ(msg.datagram, 0); |
| 68 | ASSERT_EQ(msg.instance_id, 0); |
| 69 | ASSERT_EQ(msg.type, 0); |
| 70 | ASSERT_EQ(msg.command, 0); |
| 71 | |
| 72 | // Message type is REQUEST and upper range of the field values |
| 73 | hdr.instance = 31; |
| 74 | hdr.pldm_type = 63; |
| 75 | hdr.command = 255; |
| 76 | |
| 77 | rc = pack_pldm_header(&hdr, &msg); |
| 78 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 79 | ASSERT_EQ(msg.request, 1); |
| 80 | ASSERT_EQ(msg.datagram, 0); |
| 81 | ASSERT_EQ(msg.instance_id, 31); |
| 82 | ASSERT_EQ(msg.type, 63); |
| 83 | ASSERT_EQ(msg.command, 255); |
| 84 | |
| 85 | // Message type is PLDM_ASYNC_REQUEST_NOTIFY |
| 86 | hdr.msg_type = PLDM_ASYNC_REQUEST_NOTIFY; |
| 87 | |
| 88 | rc = pack_pldm_header(&hdr, &msg); |
| 89 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 90 | ASSERT_EQ(msg.request, 1); |
| 91 | ASSERT_EQ(msg.datagram, 1); |
| 92 | ASSERT_EQ(msg.instance_id, 31); |
| 93 | ASSERT_EQ(msg.type, 63); |
| 94 | ASSERT_EQ(msg.command, 255); |
| 95 | } |
| 96 | |
| 97 | TEST(PackPLDMMessage, ResponseMessageGoodPath) |
| 98 | { |
| 99 | struct pldm_header_info hdr; |
| 100 | pldm_msg_hdr msg{}; |
| 101 | |
| 102 | // Message type is PLDM_RESPONSE and lower range of the field values |
| 103 | hdr.msg_type = PLDM_RESPONSE; |
| 104 | hdr.instance = 0; |
| 105 | hdr.pldm_type = 0; |
| 106 | hdr.command = 0; |
| 107 | |
| 108 | auto rc = pack_pldm_header(&hdr, &msg); |
| 109 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 110 | ASSERT_EQ(msg.request, 0); |
| 111 | ASSERT_EQ(msg.datagram, 0); |
| 112 | ASSERT_EQ(msg.instance_id, 0); |
| 113 | ASSERT_EQ(msg.type, 0); |
| 114 | ASSERT_EQ(msg.command, 0); |
| 115 | |
| 116 | // Message type is PLDM_RESPONSE and upper range of the field values |
| 117 | hdr.instance = 31; |
| 118 | hdr.pldm_type = 63; |
| 119 | hdr.command = 255; |
| 120 | |
| 121 | rc = pack_pldm_header(&hdr, &msg); |
| 122 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 123 | ASSERT_EQ(msg.request, 0); |
| 124 | ASSERT_EQ(msg.datagram, 0); |
| 125 | ASSERT_EQ(msg.instance_id, 31); |
| 126 | ASSERT_EQ(msg.type, 63); |
| 127 | ASSERT_EQ(msg.command, 255); |
| 128 | } |
| 129 | |
| 130 | TEST(UnpackPLDMMessage, BadPathTest) |
| 131 | { |
| 132 | struct pldm_header_info hdr; |
| 133 | |
| 134 | // PLDM message pointer is NULL |
| 135 | auto rc = unpack_pldm_header(nullptr, &hdr); |
| 136 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 137 | } |
| 138 | |
| 139 | TEST(UnpackPLDMMessage, RequestMessageGoodPath) |
| 140 | { |
| 141 | struct pldm_header_info hdr; |
| 142 | pldm_msg_hdr msg{}; |
| 143 | |
| 144 | // Unpack PLDM request message and lower range of field values |
| 145 | msg.request = 1; |
| 146 | auto rc = unpack_pldm_header(&msg, &hdr); |
| 147 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 148 | ASSERT_EQ(hdr.msg_type, PLDM_REQUEST); |
| 149 | ASSERT_EQ(hdr.instance, 0); |
| 150 | ASSERT_EQ(hdr.pldm_type, 0); |
| 151 | ASSERT_EQ(hdr.command, 0); |
| 152 | |
| 153 | // Unpack PLDM async request message and lower range of field values |
| 154 | msg.datagram = 1; |
| 155 | rc = unpack_pldm_header(&msg, &hdr); |
| 156 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 157 | ASSERT_EQ(hdr.msg_type, PLDM_ASYNC_REQUEST_NOTIFY); |
| 158 | |
| 159 | // Unpack PLDM request message and upper range of field values |
| 160 | msg.datagram = 0; |
| 161 | msg.instance_id = 31; |
| 162 | msg.type = 63; |
| 163 | msg.command = 255; |
| 164 | rc = unpack_pldm_header(&msg, &hdr); |
| 165 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 166 | ASSERT_EQ(hdr.msg_type, PLDM_REQUEST); |
| 167 | ASSERT_EQ(hdr.instance, 31); |
| 168 | ASSERT_EQ(hdr.pldm_type, 63); |
| 169 | ASSERT_EQ(hdr.command, 255); |
| 170 | } |
| 171 | |
| 172 | TEST(UnpackPLDMMessage, ResponseMessageGoodPath) |
| 173 | { |
| 174 | struct pldm_header_info hdr; |
| 175 | pldm_msg_hdr msg{}; |
| 176 | |
| 177 | // Unpack PLDM response message and lower range of field values |
| 178 | auto rc = unpack_pldm_header(&msg, &hdr); |
| 179 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 180 | ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE); |
| 181 | ASSERT_EQ(hdr.instance, 0); |
| 182 | ASSERT_EQ(hdr.pldm_type, 0); |
| 183 | ASSERT_EQ(hdr.command, 0); |
| 184 | |
| 185 | // Unpack PLDM response message and upper range of field values |
| 186 | msg.instance_id = 31; |
| 187 | msg.type = 63; |
| 188 | msg.command = 255; |
| 189 | rc = unpack_pldm_header(&msg, &hdr); |
| 190 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 191 | ASSERT_EQ(hdr.msg_type, PLDM_RESPONSE); |
| 192 | ASSERT_EQ(hdr.instance, 31); |
| 193 | ASSERT_EQ(hdr.pldm_type, 63); |
| 194 | ASSERT_EQ(hdr.command, 255); |
| 195 | } |
| 196 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 197 | TEST(GetPLDMCommands, testEncodeRequest) |
| 198 | { |
| 199 | uint8_t pldmType = 0x05; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 200 | ver32_t version{0xFF, 0xFF, 0xFF, 0xFF}; |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 201 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_REQ_BYTES> |
| 202 | requestMsg{}; |
| 203 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 204 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 205 | auto rc = encode_get_commands_req(0, pldmType, version, request); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 206 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 207 | ASSERT_EQ(0, memcmp(request->payload, &pldmType, sizeof(pldmType))); |
| 208 | ASSERT_EQ(0, memcmp(request->payload + sizeof(pldmType), &version, |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 209 | sizeof(version))); |
| 210 | } |
| 211 | |
| 212 | TEST(GetPLDMCommands, testDecodeRequest) |
| 213 | { |
| 214 | uint8_t pldmType = 0x05; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 215 | ver32_t version{0xFF, 0xFF, 0xFF, 0xFF}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 216 | uint8_t pldmTypeOut{}; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 217 | ver32_t versionOut{0xFF, 0xFF, 0xFF, 0xFF}; |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 218 | std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_REQ_BYTES> requestMsg{}; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 219 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 220 | memcpy(requestMsg.data() + hdrSize, &pldmType, sizeof(pldmType)); |
| 221 | memcpy(requestMsg.data() + sizeof(pldmType) + hdrSize, &version, |
| 222 | sizeof(version)); |
| 223 | |
| 224 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 225 | auto rc = decode_get_commands_req(request, requestMsg.size() - hdrSize, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 226 | &pldmTypeOut, &versionOut); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 227 | |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 228 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 229 | ASSERT_EQ(pldmTypeOut, pldmType); |
| 230 | ASSERT_EQ(0, memcmp(&versionOut, &version, sizeof(version))); |
| 231 | } |
| 232 | |
| 233 | TEST(GetPLDMCommands, testEncodeResponse) |
| 234 | { |
| 235 | uint8_t completionCode = 0; |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 236 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_COMMANDS_RESP_BYTES> |
| 237 | responseMsg{}; |
| 238 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 239 | std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> commands{}; |
| 240 | commands[0].byte = 1; |
| 241 | commands[1].byte = 2; |
| 242 | commands[2].byte = 3; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 243 | |
| 244 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 245 | encode_get_commands_resp(0, PLDM_SUCCESS, commands.data(), response); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 246 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 247 | uint8_t* payload_ptr = response->payload; |
| 248 | ASSERT_EQ(completionCode, payload_ptr[0]); |
| 249 | ASSERT_EQ(1, payload_ptr[sizeof(completionCode)]); |
| 250 | ASSERT_EQ(2, |
| 251 | payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte)]); |
| 252 | ASSERT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(commands[0].byte) + |
| 253 | sizeof(commands[1].byte)]); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | TEST(GetPLDMTypes, testEncodeResponse) |
| 257 | { |
| 258 | uint8_t completionCode = 0; |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 259 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TYPES_RESP_BYTES> |
| 260 | responseMsg{}; |
| 261 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 262 | std::array<bitfield8_t, PLDM_MAX_TYPES / 8> types{}; |
| 263 | types[0].byte = 1; |
| 264 | types[1].byte = 2; |
| 265 | types[2].byte = 3; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 266 | |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 267 | auto rc = encode_get_types_resp(0, PLDM_SUCCESS, types.data(), response); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 268 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 269 | uint8_t* payload_ptr = response->payload; |
| 270 | ASSERT_EQ(completionCode, payload_ptr[0]); |
| 271 | ASSERT_EQ(1, payload_ptr[sizeof(completionCode)]); |
| 272 | ASSERT_EQ(2, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte)]); |
| 273 | ASSERT_EQ(3, payload_ptr[sizeof(completionCode) + sizeof(types[0].byte) + |
| 274 | sizeof(types[1].byte)]); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST(GetPLDMTypes, testDecodeResponse) |
| 278 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 279 | std::array<uint8_t, hdrSize + PLDM_GET_TYPES_RESP_BYTES> responseMsg{}; |
| 280 | responseMsg[1 + hdrSize] = 1; |
| 281 | responseMsg[2 + hdrSize] = 2; |
| 282 | responseMsg[3 + hdrSize] = 3; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 283 | std::array<bitfield8_t, PLDM_MAX_TYPES / 8> outTypes{}; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 284 | uint8_t completion_code; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 285 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 286 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 287 | |
| 288 | auto rc = decode_get_types_resp(response, responseMsg.size() - hdrSize, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 289 | &completion_code, outTypes.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 290 | |
| 291 | ASSERT_EQ(rc, PLDM_SUCCESS); |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 292 | ASSERT_EQ(completion_code, PLDM_SUCCESS); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 293 | ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte); |
| 294 | ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte); |
| 295 | ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | TEST(GetPLDMCommands, testDecodeResponse) |
| 299 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 300 | std::array<uint8_t, hdrSize + PLDM_GET_COMMANDS_RESP_BYTES> responseMsg{}; |
| 301 | responseMsg[1 + hdrSize] = 1; |
| 302 | responseMsg[2 + hdrSize] = 2; |
| 303 | responseMsg[3 + hdrSize] = 3; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 304 | std::array<bitfield8_t, PLDM_MAX_CMDS_PER_TYPE / 8> outTypes{}; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 305 | uint8_t completion_code; |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 306 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 307 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 308 | |
| 309 | auto rc = decode_get_commands_resp(response, responseMsg.size() - hdrSize, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 310 | &completion_code, outTypes.data()); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 311 | |
| 312 | ASSERT_EQ(rc, PLDM_SUCCESS); |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 313 | ASSERT_EQ(completion_code, PLDM_SUCCESS); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 314 | ASSERT_EQ(responseMsg[1 + hdrSize], outTypes[0].byte); |
| 315 | ASSERT_EQ(responseMsg[2 + hdrSize], outTypes[1].byte); |
| 316 | ASSERT_EQ(responseMsg[3 + hdrSize], outTypes[2].byte); |
Deepak Kodihalli | 1b24f97 | 2019-02-01 04:09:13 -0600 | [diff] [blame] | 317 | } |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 318 | |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 319 | TEST(GetPLDMVersion, testGoodEncodeRequest) |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 320 | { |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 321 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_REQ_BYTES> |
| 322 | requestMsg{}; |
| 323 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 324 | uint8_t pldmType = 0x03; |
| 325 | uint32_t transferHandle = 0x0; |
| 326 | uint8_t opFlag = 0x01; |
| 327 | |
| 328 | auto rc = |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 329 | encode_get_version_req(0, transferHandle, opFlag, pldmType, request); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 330 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 331 | ASSERT_EQ( |
| 332 | 0, memcmp(request->payload, &transferHandle, sizeof(transferHandle))); |
| 333 | ASSERT_EQ(0, memcmp(request->payload + sizeof(transferHandle), &opFlag, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 334 | sizeof(opFlag))); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 335 | ASSERT_EQ(0, |
| 336 | memcmp(request->payload + sizeof(transferHandle) + sizeof(opFlag), |
| 337 | &pldmType, sizeof(pldmType))); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 338 | } |
| 339 | |
Sampa Misra | 94d704b | 2019-02-22 03:36:02 -0600 | [diff] [blame] | 340 | TEST(GetPLDMVersion, testBadEncodeRequest) |
| 341 | { |
| 342 | uint8_t pldmType = 0x03; |
| 343 | uint32_t transferHandle = 0x0; |
| 344 | uint8_t opFlag = 0x01; |
| 345 | |
| 346 | auto rc = |
| 347 | encode_get_version_req(0, transferHandle, opFlag, pldmType, nullptr); |
| 348 | |
| 349 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 350 | } |
| 351 | |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 352 | TEST(GetPLDMVersion, testEncodeResponse) |
| 353 | { |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 354 | uint8_t completionCode = 0; |
| 355 | uint32_t transferHandle = 0; |
| 356 | uint8_t flag = PLDM_START_AND_END; |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 357 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES> |
| 358 | responseMsg{}; |
| 359 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 360 | ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 361 | |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 362 | auto rc = encode_get_version_resp(0, PLDM_SUCCESS, 0, PLDM_START_AND_END, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 363 | &version, sizeof(ver32_t), response); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 364 | |
| 365 | ASSERT_EQ(rc, PLDM_SUCCESS); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 366 | ASSERT_EQ(completionCode, response->payload[0]); |
| 367 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
| 368 | &transferHandle, sizeof(transferHandle))); |
| 369 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 370 | sizeof(transferHandle), |
| 371 | &flag, sizeof(flag))); |
| 372 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 373 | sizeof(transferHandle) + sizeof(flag), |
| 374 | &version, sizeof(version))); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | TEST(GetPLDMVersion, testDecodeRequest) |
| 378 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 379 | std::array<uint8_t, hdrSize + PLDM_GET_VERSION_REQ_BYTES> requestMsg{}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 380 | uint32_t transferHandle = 0x0; |
| 381 | uint32_t retTransferHandle = 0x0; |
| 382 | uint8_t flag = PLDM_GET_FIRSTPART; |
| 383 | uint8_t retFlag = PLDM_GET_FIRSTPART; |
| 384 | uint8_t pldmType = PLDM_BASE; |
| 385 | uint8_t retType = PLDM_BASE; |
| 386 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 387 | memcpy(requestMsg.data() + hdrSize, &transferHandle, |
| 388 | sizeof(transferHandle)); |
| 389 | memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize, &flag, |
| 390 | sizeof(flag)); |
| 391 | memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(flag) + hdrSize, |
| 392 | &pldmType, sizeof(pldmType)); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 393 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 394 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 395 | |
| 396 | auto rc = decode_get_version_req(request, requestMsg.size() - hdrSize, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 397 | &retTransferHandle, &retFlag, &retType); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 398 | |
| 399 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 400 | ASSERT_EQ(transferHandle, retTransferHandle); |
| 401 | ASSERT_EQ(flag, retFlag); |
| 402 | ASSERT_EQ(pldmType, retType); |
| 403 | } |
| 404 | |
| 405 | TEST(GetPLDMVersion, testDecodeResponse) |
| 406 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 407 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_VERSION_RESP_BYTES> |
| 408 | responseMsg{}; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 409 | uint32_t transferHandle = 0x0; |
| 410 | uint32_t retTransferHandle = 0x0; |
| 411 | uint8_t flag = PLDM_START_AND_END; |
| 412 | uint8_t retFlag = PLDM_START_AND_END; |
| 413 | uint8_t completionCode = 0; |
Deepak Kodihalli | 97e0bd5 | 2019-02-21 03:54:22 -0600 | [diff] [blame] | 414 | ver32_t version = {0xFF, 0xFF, 0xFF, 0xFF}; |
| 415 | ver32_t versionOut; |
Deepak Kodihalli | 8c64346 | 2019-02-21 10:43:36 -0600 | [diff] [blame] | 416 | uint8_t completion_code; |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 417 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 418 | memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, |
| 419 | &transferHandle, sizeof(transferHandle)); |
| 420 | memcpy(responseMsg.data() + sizeof(completionCode) + |
| 421 | sizeof(transferHandle) + hdrSize, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 422 | &flag, sizeof(flag)); |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 423 | memcpy(responseMsg.data() + sizeof(completionCode) + |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 424 | sizeof(transferHandle) + sizeof(flag) + hdrSize, |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 425 | &version, sizeof(version)); |
| 426 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 427 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 428 | |
| 429 | auto rc = decode_get_version_resp(response, responseMsg.size() - hdrSize, |
vkaverap | a6575b8 | 2019-04-03 05:33:52 -0500 | [diff] [blame] | 430 | &completion_code, &retTransferHandle, |
| 431 | &retFlag, &versionOut); |
Sampa Misra | 432e187 | 2019-02-13 03:49:43 -0600 | [diff] [blame] | 432 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 433 | ASSERT_EQ(transferHandle, retTransferHandle); |
| 434 | ASSERT_EQ(flag, retFlag); |
| 435 | |
| 436 | ASSERT_EQ(versionOut.major, version.major); |
| 437 | ASSERT_EQ(versionOut.minor, version.minor); |
| 438 | ASSERT_EQ(versionOut.update, version.update); |
| 439 | ASSERT_EQ(versionOut.alpha, version.alpha); |
| 440 | } |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 441 | |
Sridevi Ramesh | bc6ff26 | 2019-12-12 04:58:35 -0600 | [diff] [blame^] | 442 | TEST(GetTID, testEncodeRequest) |
| 443 | { |
| 444 | pldm_msg request{}; |
| 445 | |
| 446 | auto rc = encode_get_tid_req(0, &request); |
| 447 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 448 | } |
| 449 | |
John Wang | 5c4f80d | 2019-07-29 11:12:18 +0800 | [diff] [blame] | 450 | TEST(GetTID, testEncodeResponse) |
| 451 | { |
| 452 | uint8_t completionCode = 0; |
| 453 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_TID_RESP_BYTES> |
| 454 | responseMsg{}; |
| 455 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 456 | uint8_t tid = 1; |
| 457 | |
| 458 | auto rc = encode_get_tid_resp(0, PLDM_SUCCESS, tid, response); |
| 459 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 460 | uint8_t* payload = response->payload; |
| 461 | ASSERT_EQ(completionCode, payload[0]); |
| 462 | ASSERT_EQ(1, payload[sizeof(completionCode)]); |
| 463 | } |
| 464 | |
| 465 | TEST(GetTID, testDecodeResponse) |
| 466 | { |
| 467 | std::array<uint8_t, hdrSize + PLDM_GET_TID_RESP_BYTES> responseMsg{}; |
| 468 | responseMsg[1 + hdrSize] = 1; |
| 469 | |
| 470 | uint8_t tid; |
| 471 | uint8_t completion_code; |
| 472 | |
| 473 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 474 | |
| 475 | auto rc = decode_get_tid_resp(response, responseMsg.size() - hdrSize, |
| 476 | &completion_code, &tid); |
| 477 | |
| 478 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 479 | ASSERT_EQ(completion_code, PLDM_SUCCESS); |
| 480 | ASSERT_EQ(tid, 1); |
| 481 | } |
John Wang | 7f02d70 | 2019-12-03 13:38:14 +0800 | [diff] [blame] | 482 | |
| 483 | TEST(CcOnlyResponse, testEncode) |
| 484 | { |
| 485 | struct pldm_msg responseMsg; |
| 486 | |
| 487 | auto rc = |
| 488 | encode_cc_only_resp(0 /*instance id*/, 1 /*pldm type*/, 2 /*command*/, |
| 489 | 3 /*complection code*/, &responseMsg); |
| 490 | EXPECT_EQ(rc, PLDM_SUCCESS); |
| 491 | |
| 492 | auto p = reinterpret_cast<uint8_t*>(&responseMsg); |
| 493 | EXPECT_THAT(std::vector<uint8_t>(p, p + sizeof(responseMsg)), |
| 494 | ElementsAreArray({0, 1, 2, 3})); |
| 495 | |
| 496 | rc = encode_cc_only_resp(PLDM_INSTANCE_MAX + 1, 1, 2, 3, &responseMsg); |
| 497 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 498 | |
| 499 | rc = encode_cc_only_resp(0, 1, 2, 3, nullptr); |
| 500 | EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 501 | } |