George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | #include "libpldm/base.h" |
| 2 | #include "libpldm/file_io.h" |
| 3 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 4 | #include <string.h> |
| 5 | |
| 6 | #include <array> |
| 7 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 10 | constexpr auto hdrSize = sizeof(pldm_msg_hdr); |
| 11 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 12 | TEST(ReadWriteFileMemory, testGoodDecodeRequest) |
| 13 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 14 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES + hdrSize> requestMsg{}; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 15 | |
| 16 | // Random value for fileHandle, offset, length, address |
| 17 | uint32_t fileHandle = 0x12345678; |
| 18 | uint32_t offset = 0x87654321; |
| 19 | uint32_t length = 0x13245768; |
| 20 | uint64_t address = 0x124356879ACBDE0F; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 21 | uint32_t fileHandleLe = htole32(fileHandle); |
| 22 | uint32_t offsetLe = htole32(offset); |
| 23 | uint32_t lengthLe = htole32(length); |
| 24 | uint64_t addressLe = htole64(address); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 25 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 26 | memcpy(requestMsg.data() + hdrSize, &fileHandleLe, sizeof(fileHandleLe)); |
| 27 | memcpy(requestMsg.data() + sizeof(fileHandleLe) + hdrSize, &offsetLe, |
| 28 | sizeof(offsetLe)); |
| 29 | memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) + |
| 30 | hdrSize, |
| 31 | &lengthLe, sizeof(lengthLe)); |
| 32 | memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) + |
| 33 | sizeof(lengthLe) + hdrSize, |
| 34 | &addressLe, sizeof(addressLe)); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 35 | |
| 36 | uint32_t retFileHandle = 0; |
| 37 | uint32_t retOffset = 0; |
| 38 | uint32_t retLength = 0; |
| 39 | uint64_t retAddress = 0; |
| 40 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 41 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 42 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 43 | // Invoke decode the read file memory request |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 44 | auto rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 45 | &retFileHandle, &retOffset, &retLength, |
| 46 | &retAddress); |
| 47 | |
| 48 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 49 | ASSERT_EQ(fileHandle, retFileHandle); |
| 50 | ASSERT_EQ(offset, retOffset); |
| 51 | ASSERT_EQ(length, retLength); |
| 52 | ASSERT_EQ(address, retAddress); |
| 53 | } |
| 54 | |
| 55 | TEST(ReadWriteFileMemory, testBadDecodeRequest) |
| 56 | { |
| 57 | uint32_t fileHandle = 0; |
| 58 | uint32_t offset = 0; |
| 59 | uint32_t length = 0; |
| 60 | uint64_t address = 0; |
| 61 | |
| 62 | // Request payload message is missing |
| 63 | auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length, |
| 64 | &address); |
| 65 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 66 | |
| 67 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 68 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 69 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 70 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 71 | // Address is NULL |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 72 | rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 73 | &fileHandle, &offset, &length, NULL); |
| 74 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 75 | |
| 76 | // Payload length is invalid |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 77 | rc = decode_rw_file_memory_req(request, 0, &fileHandle, &offset, &length, |
| 78 | &address); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 79 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 80 | } |
| 81 | |
| 82 | TEST(ReadWriteFileMemory, testGoodEncodeResponse) |
| 83 | { |
| 84 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES> |
| 85 | responseMsg{}; |
| 86 | uint32_t length = 0xFF00EE11; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 87 | uint32_t lengthLe = htole32(length); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 88 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 89 | |
| 90 | // ReadFileIntoMemory |
| 91 | auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY, |
| 92 | PLDM_SUCCESS, length, response); |
| 93 | |
| 94 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 95 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 96 | ASSERT_EQ(response->hdr.instance_id, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 97 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 98 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY); |
| 99 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 100 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 101 | &lengthLe, sizeof(lengthLe))); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 102 | |
| 103 | // WriteFileFromMemory |
| 104 | rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, |
| 105 | PLDM_SUCCESS, length, response); |
| 106 | |
| 107 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 108 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 109 | ASSERT_EQ(response->hdr.instance_id, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 110 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 111 | ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY); |
| 112 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 113 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 114 | &lengthLe, sizeof(lengthLe))); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | TEST(ReadWriteFileMemory, testBadEncodeResponse) |
| 118 | { |
| 119 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES> |
| 120 | responseMsg{}; |
| 121 | uint32_t length = 0; |
| 122 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 123 | |
| 124 | // ReadFileIntoMemory |
| 125 | auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY, |
| 126 | PLDM_ERROR, length, response); |
| 127 | |
| 128 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 129 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 130 | ASSERT_EQ(response->hdr.instance_id, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 131 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 132 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY); |
| 133 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 134 | |
| 135 | // WriteFileFromMemory |
| 136 | rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR, |
| 137 | length, response); |
| 138 | |
| 139 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 140 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 141 | ASSERT_EQ(response->hdr.instance_id, 0); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 142 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 143 | ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY); |
| 144 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 145 | } |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 146 | |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 147 | TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse) |
| 148 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 149 | std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES + hdrSize> responseMsg{}; |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 150 | // Random value for length |
| 151 | uint32_t length = 0xFF00EE12; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 152 | uint32_t lengthLe = htole32(length); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 153 | uint8_t completionCode = 0; |
| 154 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 155 | memcpy(responseMsg.data() + hdrSize, &completionCode, |
| 156 | sizeof(completionCode)); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 157 | memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &lengthLe, |
| 158 | sizeof(lengthLe)); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 159 | |
| 160 | uint8_t retCompletionCode = 0; |
| 161 | uint32_t retLength = 0; |
| 162 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 163 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 164 | |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 165 | // Invoke decode the read file memory response |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 166 | auto rc = decode_rw_file_memory_resp(response, responseMsg.size() - hdrSize, |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 167 | &retCompletionCode, &retLength); |
| 168 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 169 | ASSERT_EQ(completionCode, retCompletionCode); |
| 170 | ASSERT_EQ(length, retLength); |
| 171 | } |
| 172 | |
| 173 | TEST(ReadWriteFileIntoMemory, testBadDecodeResponse) |
| 174 | { |
| 175 | uint32_t length = 0; |
| 176 | uint8_t completionCode = 0; |
| 177 | |
| 178 | // Request payload message is missing |
| 179 | auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length); |
| 180 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 181 | |
| 182 | std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{}; |
| 183 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 184 | auto response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 185 | |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 186 | // Payload length is invalid |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 187 | rc = decode_rw_file_memory_resp(response, 0, &completionCode, &length); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 188 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 189 | } |
| 190 | |
| 191 | TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest) |
| 192 | { |
| 193 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 194 | requestMsg{}; |
| 195 | |
| 196 | uint32_t fileHandle = 0x12345678; |
| 197 | uint32_t offset = 0x87654321; |
| 198 | uint32_t length = 0x13245768; |
| 199 | uint64_t address = 0x124356879ACBDE0F; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 200 | uint32_t fileHandleLe = htole32(fileHandle); |
| 201 | uint32_t offsetLe = htole32(offset); |
| 202 | uint32_t lengthLe = htole32(length); |
| 203 | uint64_t addressLe = htole64(address); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 204 | |
| 205 | pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 206 | |
| 207 | auto rc = |
| 208 | encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle, |
| 209 | offset, length, address, request); |
| 210 | |
| 211 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 212 | ASSERT_EQ(request->hdr.request, PLDM_REQUEST); |
| 213 | ASSERT_EQ(request->hdr.instance_id, 0); |
| 214 | ASSERT_EQ(request->hdr.type, PLDM_OEM); |
| 215 | ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY); |
| 216 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 217 | ASSERT_EQ(0, memcmp(request->payload, &fileHandleLe, sizeof(fileHandleLe))); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 218 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 219 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe), &offsetLe, |
| 220 | sizeof(offsetLe))); |
| 221 | ASSERT_EQ(0, |
| 222 | memcmp(request->payload + sizeof(fileHandleLe) + sizeof(offsetLe), |
| 223 | &lengthLe, sizeof(lengthLe))); |
| 224 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe) + |
| 225 | sizeof(offsetLe) + sizeof(lengthLe), |
| 226 | &addressLe, sizeof(addressLe))); |
Priyanga | 8b97665 | 2019-06-27 11:30:33 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | TEST(ReadWriteFileIntoMemory, testBadEncodeRequest) |
| 230 | { |
| 231 | uint32_t fileHandle = 0; |
| 232 | uint32_t offset = 0; |
| 233 | uint32_t length = 0; |
| 234 | uint64_t address = 0; |
| 235 | |
| 236 | auto rc = |
| 237 | encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle, |
| 238 | offset, length, address, NULL); |
| 239 | |
| 240 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 241 | } |
| 242 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 243 | TEST(GetFileTable, GoodDecodeRequest) |
| 244 | { |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 245 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES + hdrSize> requestMsg{}; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 246 | |
| 247 | // Random value for DataTransferHandle, TransferOperationFlag, TableType |
| 248 | uint32_t transferHandle = 0x12345678; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 249 | uint32_t transferHandleLe = htole32(transferHandle); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 250 | uint8_t transferOpFlag = 1; |
| 251 | uint8_t tableType = 1; |
| 252 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 253 | memcpy(requestMsg.data() + hdrSize, &transferHandleLe, |
| 254 | sizeof(transferHandleLe)); |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 255 | memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize, |
| 256 | &transferOpFlag, sizeof(transferOpFlag)); |
| 257 | memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag) + |
| 258 | hdrSize, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 259 | &tableType, sizeof(tableType)); |
| 260 | |
| 261 | uint32_t retTransferHandle = 0; |
| 262 | uint8_t retTransferOpFlag = 0; |
| 263 | uint8_t retTableType = 0; |
| 264 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 265 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 266 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 267 | // Invoke decode get file table request |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 268 | auto rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 269 | &retTransferHandle, &retTransferOpFlag, |
| 270 | &retTableType); |
| 271 | |
| 272 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 273 | ASSERT_EQ(transferHandle, retTransferHandle); |
| 274 | ASSERT_EQ(transferOpFlag, retTransferOpFlag); |
| 275 | ASSERT_EQ(tableType, retTableType); |
| 276 | } |
| 277 | |
| 278 | TEST(GetFileTable, BadDecodeRequest) |
| 279 | { |
| 280 | uint32_t transferHandle = 0; |
| 281 | uint8_t transferOpFlag = 0; |
| 282 | uint8_t tableType = 0; |
| 283 | |
| 284 | // Request payload message is missing |
| 285 | auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle, |
| 286 | &transferOpFlag, &tableType); |
| 287 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 288 | |
| 289 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 290 | |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 291 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 292 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 293 | // TableType is NULL |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 294 | rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize, |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 295 | &transferHandle, &transferOpFlag, nullptr); |
| 296 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 297 | |
| 298 | // Payload length is invalid |
Zahed Hossain | 223a73d | 2019-07-04 12:46:18 -0500 | [diff] [blame] | 299 | rc = decode_get_file_table_req(request, 0, &transferHandle, &transferOpFlag, |
| 300 | &tableType); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 301 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 302 | } |
| 303 | |
| 304 | TEST(GetFileTable, GoodEncodeResponse) |
| 305 | { |
| 306 | // Random value for NextDataTransferHandle and TransferFlag |
| 307 | uint8_t completionCode = 0; |
| 308 | uint32_t nextTransferHandle = 0x87654321; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 309 | uint32_t nextTransferHandleLe = htole32(nextTransferHandle); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 310 | uint8_t transferFlag = 5; |
| 311 | // Mock file table contents of size 5 |
| 312 | std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5}; |
| 313 | constexpr size_t responseSize = sizeof(completionCode) + |
| 314 | sizeof(nextTransferHandle) + |
| 315 | sizeof(transferFlag) + fileTable.size(); |
| 316 | |
| 317 | std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{}; |
| 318 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 319 | |
| 320 | // GetFileTable |
| 321 | auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle, |
| 322 | transferFlag, fileTable.data(), |
| 323 | fileTable.size(), response); |
| 324 | |
| 325 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 326 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 327 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 328 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 329 | ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE); |
| 330 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 331 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 332 | &nextTransferHandleLe, sizeof(nextTransferHandle))); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 333 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 334 | sizeof(nextTransferHandle), |
| 335 | &transferFlag, sizeof(transferFlag))); |
| 336 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 337 | sizeof(nextTransferHandle), |
| 338 | &transferFlag, sizeof(transferFlag))); |
| 339 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 340 | sizeof(nextTransferHandle) + sizeof(transferFlag), |
| 341 | fileTable.data(), fileTable.size())); |
| 342 | } |
| 343 | |
| 344 | TEST(GetFileTable, BadEncodeResponse) |
| 345 | { |
| 346 | uint8_t completionCode = 0; |
| 347 | uint32_t nextTransferHandle = 0; |
| 348 | uint8_t transferFlag = 0; |
| 349 | constexpr size_t responseSize = sizeof(completionCode) + |
| 350 | sizeof(nextTransferHandle) + |
| 351 | sizeof(transferFlag); |
| 352 | |
| 353 | std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{}; |
| 354 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 355 | |
| 356 | // GetFileTable |
| 357 | auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle, |
| 358 | transferFlag, nullptr, 0, response); |
| 359 | |
| 360 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 361 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 362 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 363 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 364 | ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE); |
| 365 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 366 | } |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 367 | |
| 368 | TEST(ReadFile, testGoodDecodeRequest) |
| 369 | { |
| 370 | std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 371 | requestMsg{}; |
| 372 | |
| 373 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 374 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 375 | auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload); |
| 376 | |
| 377 | // Random value for fileHandle, offset and length |
| 378 | uint32_t fileHandle = 0x12345678; |
| 379 | uint32_t offset = 0x87654321; |
| 380 | uint32_t length = 0x13245768; |
| 381 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 382 | request->file_handle = htole32(fileHandle); |
| 383 | request->offset = htole32(offset); |
| 384 | request->length = htole32(length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 385 | |
| 386 | uint32_t retFileHandle = 0; |
| 387 | uint32_t retOffset = 0; |
| 388 | uint32_t retLength = 0; |
| 389 | |
| 390 | // Invoke decode the read file request |
| 391 | auto rc = decode_read_file_req(requestPtr, payload_length, &retFileHandle, |
| 392 | &retOffset, &retLength); |
| 393 | |
| 394 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 395 | ASSERT_EQ(fileHandle, retFileHandle); |
| 396 | ASSERT_EQ(offset, retOffset); |
| 397 | ASSERT_EQ(length, retLength); |
| 398 | } |
| 399 | |
| 400 | TEST(WriteFile, testGoodDecodeRequest) |
| 401 | { |
| 402 | // Random value for fileHandle, offset, length and file data |
| 403 | uint32_t fileHandle = 0x12345678; |
| 404 | uint32_t offset = 0x87654321; |
| 405 | uint32_t length = 0x467; |
| 406 | |
| 407 | std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES + |
| 408 | sizeof(pldm_msg_hdr) + length); |
| 409 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 410 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 411 | auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload); |
| 412 | |
| 413 | size_t fileDataOffset = |
| 414 | sizeof(fileHandle) + sizeof(offset) + sizeof(length); |
| 415 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 416 | request->file_handle = htole32(fileHandle); |
| 417 | request->offset = htole32(offset); |
| 418 | request->length = htole32(length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 419 | |
| 420 | uint32_t retFileHandle = 0; |
| 421 | uint32_t retOffset = 0; |
| 422 | uint32_t retLength = 0; |
| 423 | size_t retFileDataOffset = 0; |
| 424 | |
| 425 | // Invoke decode the write file request |
| 426 | auto rc = decode_write_file_req(requestPtr, payload_length, &retFileHandle, |
| 427 | &retOffset, &retLength, &retFileDataOffset); |
| 428 | |
| 429 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 430 | ASSERT_EQ(fileHandle, retFileHandle); |
| 431 | ASSERT_EQ(offset, retOffset); |
| 432 | ASSERT_EQ(length, retLength); |
| 433 | ASSERT_EQ(fileDataOffset, retFileDataOffset); |
| 434 | } |
| 435 | |
| 436 | TEST(ReadFile, testGoodDecodeResponse) |
| 437 | { |
| 438 | // Random value for length |
| 439 | uint32_t length = 0x10; |
| 440 | uint8_t completionCode = PLDM_SUCCESS; |
| 441 | |
| 442 | std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES + |
| 443 | sizeof(pldm_msg_hdr) + length); |
| 444 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 445 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 446 | auto response = |
| 447 | reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload); |
| 448 | |
| 449 | response->completion_code = completionCode; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 450 | response->length = htole32(length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 451 | |
| 452 | size_t fileDataOffset = sizeof(completionCode) + sizeof(length); |
| 453 | |
| 454 | uint32_t retLength = 0; |
| 455 | uint8_t retCompletionCode = 0; |
| 456 | size_t retFileDataOffset = 0; |
| 457 | |
| 458 | // Invoke decode the read file response |
| 459 | auto rc = |
| 460 | decode_read_file_resp(responsePtr, payload_length, &retCompletionCode, |
| 461 | &retLength, &retFileDataOffset); |
| 462 | |
| 463 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 464 | ASSERT_EQ(completionCode, retCompletionCode); |
| 465 | ASSERT_EQ(length, retLength); |
| 466 | ASSERT_EQ(fileDataOffset, retFileDataOffset); |
| 467 | } |
| 468 | |
| 469 | TEST(WriteFile, testGoodDecodeResponse) |
| 470 | { |
| 471 | std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 472 | responseMsg{}; |
| 473 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 474 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 475 | auto response = |
| 476 | reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload); |
| 477 | |
| 478 | uint8_t completionCode = PLDM_SUCCESS; |
| 479 | uint32_t length = 0x4678; |
| 480 | |
| 481 | response->completion_code = completionCode; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 482 | response->length = htole32(length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 483 | |
| 484 | uint32_t retLength = 0; |
| 485 | uint8_t retCompletionCode = 0; |
| 486 | |
| 487 | // Invoke decode the write file response |
| 488 | auto rc = decode_write_file_resp(responsePtr, payload_length, |
| 489 | &retCompletionCode, &retLength); |
| 490 | |
| 491 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 492 | ASSERT_EQ(completionCode, retCompletionCode); |
| 493 | ASSERT_EQ(length, retLength); |
| 494 | } |
| 495 | |
| 496 | TEST(ReadWriteFile, testBadDecodeResponse) |
| 497 | { |
| 498 | uint32_t length = 0; |
| 499 | uint8_t completionCode = 0; |
| 500 | size_t fileDataOffset = 0; |
| 501 | |
| 502 | // Bad decode response for read file |
| 503 | std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES + |
| 504 | sizeof(pldm_msg_hdr) + length); |
| 505 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 506 | |
| 507 | // Request payload message is missing |
| 508 | auto rc = decode_read_file_resp(NULL, 0, &completionCode, &length, |
| 509 | &fileDataOffset); |
| 510 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 511 | |
| 512 | // Payload length is invalid |
| 513 | rc = decode_read_file_resp(responsePtr, 0, &completionCode, &length, |
| 514 | &fileDataOffset); |
| 515 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 516 | |
| 517 | // Bad decode response for write file |
| 518 | std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 519 | responseMsgWr{}; |
| 520 | auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data()); |
| 521 | |
| 522 | // Request payload message is missing |
| 523 | rc = decode_write_file_resp(NULL, 0, &completionCode, &length); |
| 524 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 525 | |
| 526 | // Payload length is invalid |
| 527 | rc = decode_write_file_resp(responseWr, 0, &completionCode, &length); |
| 528 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 529 | } |
| 530 | |
| 531 | TEST(ReadWriteFile, testBadDecodeRequest) |
| 532 | { |
| 533 | uint32_t fileHandle = 0; |
| 534 | uint32_t offset = 0; |
| 535 | uint32_t length = 0; |
| 536 | |
| 537 | // Bad decode request for read file |
| 538 | std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 539 | requestMsg{}; |
| 540 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 541 | |
| 542 | // Request payload message is missing |
| 543 | auto rc = decode_read_file_req(NULL, 0, &fileHandle, &offset, &length); |
| 544 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 545 | |
| 546 | // Payload length is invalid |
| 547 | rc = decode_read_file_req(requestPtr, 0, &fileHandle, &offset, &length); |
| 548 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 549 | |
| 550 | // Bad decode request for write file |
| 551 | size_t fileDataOffset = 0; |
| 552 | std::array<uint8_t, PLDM_WRITE_FILE_REQ_BYTES> requestMsgWr{}; |
| 553 | auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data()); |
| 554 | |
| 555 | // Request payload message is missing |
| 556 | rc = decode_write_file_req(NULL, 0, &fileHandle, &offset, &length, |
| 557 | &fileDataOffset); |
| 558 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 559 | |
| 560 | // Payload length is invalid |
| 561 | rc = decode_write_file_req(requestWr, 0, &fileHandle, &offset, &length, |
| 562 | &fileDataOffset); |
| 563 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 564 | } |
| 565 | |
| 566 | TEST(ReadFile, testGoodEncodeResponse) |
| 567 | { |
| 568 | // Good encode response for read file |
| 569 | uint32_t length = 0x4; |
| 570 | |
| 571 | std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES + |
| 572 | sizeof(pldm_msg_hdr) + length); |
| 573 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 574 | auto response = |
| 575 | reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload); |
| 576 | |
| 577 | // ReadFile |
| 578 | auto rc = encode_read_file_resp(0, PLDM_SUCCESS, length, responsePtr); |
| 579 | |
| 580 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 581 | ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE); |
| 582 | ASSERT_EQ(responsePtr->hdr.instance_id, 0); |
| 583 | ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM); |
| 584 | ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE); |
| 585 | ASSERT_EQ(response->completion_code, PLDM_SUCCESS); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 586 | ASSERT_EQ(le32toh(response->length), length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | TEST(WriteFile, testGoodEncodeResponse) |
| 590 | { |
| 591 | uint32_t length = 0x467; |
| 592 | |
| 593 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES> |
| 594 | responseMsg{}; |
| 595 | |
| 596 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 597 | auto response = |
| 598 | reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload); |
| 599 | |
| 600 | // WriteFile |
| 601 | auto rc = encode_write_file_resp(0, PLDM_SUCCESS, length, responsePtr); |
| 602 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 603 | ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE); |
| 604 | ASSERT_EQ(responsePtr->hdr.instance_id, 0); |
| 605 | ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM); |
| 606 | ASSERT_EQ(responsePtr->hdr.command, PLDM_WRITE_FILE); |
| 607 | ASSERT_EQ(response->completion_code, PLDM_SUCCESS); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 608 | ASSERT_EQ(le32toh(response->length), length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | TEST(ReadFile, testGoodEncodeRequest) |
| 612 | { |
| 613 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES> |
| 614 | requestMsg{}; |
| 615 | |
| 616 | uint32_t fileHandle = 0x12345678; |
| 617 | uint32_t offset = 0x87654321; |
| 618 | uint32_t length = 0x13245768; |
| 619 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 620 | auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload); |
| 621 | |
| 622 | // ReadFile |
| 623 | auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr); |
| 624 | |
| 625 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 626 | ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST); |
| 627 | ASSERT_EQ(requestPtr->hdr.instance_id, 0); |
| 628 | ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM); |
| 629 | ASSERT_EQ(requestPtr->hdr.command, PLDM_READ_FILE); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 630 | ASSERT_EQ(le32toh(request->file_handle), fileHandle); |
| 631 | ASSERT_EQ(le32toh(request->offset), offset); |
| 632 | ASSERT_EQ(le32toh(request->length), length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | TEST(WriteFile, testGoodEncodeRequest) |
| 636 | { |
| 637 | uint32_t fileHandle = 0x12345678; |
| 638 | uint32_t offset = 0x87654321; |
| 639 | uint32_t length = 0x456; |
| 640 | |
| 641 | std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES + |
| 642 | sizeof(pldm_msg_hdr) + length); |
| 643 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 644 | auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload); |
| 645 | |
| 646 | // WriteFile |
| 647 | auto rc = encode_write_file_req(0, fileHandle, offset, length, requestPtr); |
| 648 | |
| 649 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 650 | ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST); |
| 651 | ASSERT_EQ(requestPtr->hdr.instance_id, 0); |
| 652 | ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM); |
| 653 | ASSERT_EQ(requestPtr->hdr.command, PLDM_WRITE_FILE); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 654 | ASSERT_EQ(le32toh(request->file_handle), fileHandle); |
| 655 | ASSERT_EQ(le32toh(request->offset), offset); |
| 656 | ASSERT_EQ(le32toh(request->length), length); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | TEST(ReadWriteFile, testBadEncodeRequest) |
| 660 | { |
| 661 | // Bad encode request for read file |
| 662 | uint32_t fileHandle = 0; |
| 663 | uint32_t offset = 0; |
| 664 | uint32_t length = 0; |
| 665 | |
| 666 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES> |
| 667 | requestMsg{}; |
| 668 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 669 | |
| 670 | // ReadFile check invalid file length |
| 671 | auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr); |
| 672 | |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 673 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 674 | |
| 675 | // Bad encode request for write file |
| 676 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_REQ_BYTES> |
| 677 | requestMsgWr{}; |
| 678 | auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data()); |
| 679 | |
| 680 | // WriteFile check for invalid file length |
| 681 | rc = encode_write_file_req(0, fileHandle, offset, length, requestWr); |
| 682 | |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 683 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
vkaverap | 2ffe329 | 2019-06-24 00:08:13 -0500 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | TEST(ReadWriteFile, testBadEncodeResponse) |
| 687 | { |
| 688 | // Bad encode response for read file |
| 689 | uint32_t length = 0; |
| 690 | |
| 691 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES> |
| 692 | responseMsg{}; |
| 693 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 694 | |
| 695 | // ReadFile |
| 696 | auto rc = encode_read_file_resp(0, PLDM_ERROR, length, responsePtr); |
| 697 | |
| 698 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 699 | ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE); |
| 700 | ASSERT_EQ(responsePtr->hdr.instance_id, 0); |
| 701 | ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM); |
| 702 | ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE); |
| 703 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR); |
| 704 | |
| 705 | // Bad encode response for write file |
| 706 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES> |
| 707 | responseMsgWr{}; |
| 708 | auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data()); |
| 709 | |
| 710 | // WriteFile |
| 711 | rc = encode_write_file_resp(0, PLDM_ERROR, length, responseWr); |
| 712 | |
| 713 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 714 | ASSERT_EQ(responseWr->hdr.request, PLDM_RESPONSE); |
| 715 | ASSERT_EQ(responseWr->hdr.instance_id, 0); |
| 716 | ASSERT_EQ(responseWr->hdr.type, PLDM_OEM); |
| 717 | ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE); |
| 718 | ASSERT_EQ(responseWr->payload[0], PLDM_ERROR); |
| 719 | } |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 720 | |
| 721 | TEST(ReadWriteFileByTypeMemory, testGoodDecodeRequest) |
| 722 | { |
| 723 | std::array<uint8_t, |
| 724 | PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 725 | requestMsg{}; |
| 726 | |
| 727 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 728 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 729 | auto request = reinterpret_cast<pldm_read_write_file_by_type_memory_req*>( |
| 730 | requestPtr->payload); |
| 731 | |
| 732 | // Random value for fileHandle, offset and length |
| 733 | uint16_t fileType = 0; |
| 734 | uint32_t fileHandle = 0x12345678; |
| 735 | uint32_t offset = 0x87654321; |
| 736 | uint32_t length = 0x13245768; |
| 737 | uint64_t address = 0x124356879ACBD456; |
| 738 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 739 | request->file_type = htole16(fileType); |
| 740 | request->file_handle = htole32(fileHandle); |
| 741 | request->offset = htole32(offset); |
| 742 | request->length = htole32(length); |
| 743 | request->address = htole64(address); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 744 | |
| 745 | uint16_t retFileType = 0x1; |
| 746 | uint32_t retFileHandle = 0; |
| 747 | uint32_t retOffset = 0; |
| 748 | uint32_t retLength = 0; |
| 749 | uint64_t retAddress = 0; |
| 750 | |
| 751 | // Invoke decode the read file request |
| 752 | auto rc = decode_rw_file_by_type_memory_req( |
| 753 | requestPtr, payload_length, &retFileType, &retFileHandle, &retOffset, |
| 754 | &retLength, &retAddress); |
| 755 | |
| 756 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 757 | ASSERT_EQ(fileType, retFileType); |
| 758 | ASSERT_EQ(fileHandle, retFileHandle); |
| 759 | ASSERT_EQ(offset, retOffset); |
| 760 | ASSERT_EQ(length, retLength); |
| 761 | ASSERT_EQ(address, retAddress); |
| 762 | } |
| 763 | |
| 764 | TEST(ReadWriteFileByTypeMemory, testGoodDecodeResponse) |
| 765 | { |
| 766 | std::array<uint8_t, |
| 767 | PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 768 | responseMsg{}; |
| 769 | |
| 770 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 771 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 772 | auto response = reinterpret_cast<pldm_read_write_file_by_type_memory_resp*>( |
| 773 | responsePtr->payload); |
| 774 | |
| 775 | // Random value for completion code and length |
| 776 | uint8_t completionCode = 0x0; |
| 777 | uint32_t length = 0x13245768; |
| 778 | |
| 779 | response->completion_code = completionCode; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 780 | response->length = htole32(length); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 781 | |
| 782 | uint8_t retCompletionCode = 0x1; |
| 783 | uint32_t retLength = 0; |
| 784 | |
| 785 | // Invoke decode the read/write file response |
| 786 | auto rc = decode_rw_file_by_type_memory_resp( |
| 787 | responsePtr, payload_length, &retCompletionCode, &retLength); |
| 788 | |
| 789 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 790 | ASSERT_EQ(completionCode, retCompletionCode); |
| 791 | ASSERT_EQ(length, retLength); |
| 792 | } |
| 793 | |
| 794 | TEST(ReadWriteFileByTypeMemory, testBadDecodeRequest) |
| 795 | { |
| 796 | uint16_t fileType = 0; |
| 797 | uint32_t fileHandle = 0; |
| 798 | uint32_t offset = 0; |
| 799 | uint32_t length = 0; |
| 800 | uint64_t address = 0; |
| 801 | |
| 802 | // Request payload message is missing |
| 803 | auto rc = decode_rw_file_by_type_memory_req(NULL, 0, &fileType, &fileHandle, |
| 804 | &offset, &length, &address); |
| 805 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 806 | |
| 807 | std::array<uint8_t, |
| 808 | PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 809 | requestMsg{}; |
| 810 | |
| 811 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 812 | |
| 813 | // Address is NULL |
| 814 | rc = decode_rw_file_by_type_memory_req( |
| 815 | requestPtr, requestMsg.size() - hdrSize, &fileType, &fileHandle, |
| 816 | &offset, &length, NULL); |
| 817 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 818 | |
| 819 | // Payload length is invalid |
| 820 | rc = decode_rw_file_by_type_memory_req( |
| 821 | requestPtr, 0, &fileType, &fileHandle, &offset, &length, &address); |
| 822 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 823 | } |
| 824 | |
| 825 | TEST(ReadWriteFileByTypeMemory, testBadDecodeResponse) |
| 826 | { |
| 827 | uint32_t length = 0; |
| 828 | uint8_t completionCode = 0; |
| 829 | |
| 830 | // Request payload message is missing |
| 831 | auto rc = |
| 832 | decode_rw_file_by_type_memory_resp(NULL, 0, &completionCode, &length); |
| 833 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 834 | |
| 835 | std::array<uint8_t, |
| 836 | PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 837 | responseMsg{}; |
| 838 | |
| 839 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 840 | |
| 841 | // Length is NULL |
| 842 | rc = decode_rw_file_by_type_memory_resp( |
| 843 | responsePtr, responseMsg.size() - hdrSize, &completionCode, NULL); |
| 844 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 845 | |
| 846 | // Payload length is invalid |
| 847 | rc = decode_rw_file_by_type_memory_resp(responsePtr, 0, &completionCode, |
| 848 | &length); |
| 849 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 850 | } |
| 851 | |
| 852 | TEST(ReadWriteFileByTypeMemory, testGoodEncodeRequest) |
| 853 | { |
| 854 | std::array<uint8_t, |
| 855 | sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES> |
| 856 | requestMsg{}; |
| 857 | |
| 858 | uint16_t fileType = 0; |
| 859 | uint32_t fileHandle = 0x12345678; |
| 860 | uint32_t offset = 0x87654321; |
| 861 | uint32_t length = 0x13245768; |
| 862 | uint64_t address = 0x124356879ACBDE0F; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 863 | uint16_t fileTypeLe = htole16(fileType); |
| 864 | uint32_t fileHandleLe = htole32(fileHandle); |
| 865 | uint32_t offsetLe = htole32(offset); |
| 866 | uint32_t lengthLe = htole32(length); |
| 867 | uint64_t addressLe = htole64(address); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 868 | |
| 869 | pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 870 | |
| 871 | auto rc = encode_rw_file_by_type_memory_req( |
| 872 | 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset, |
| 873 | length, address, request); |
| 874 | |
| 875 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 876 | ASSERT_EQ(request->hdr.request, PLDM_REQUEST); |
| 877 | ASSERT_EQ(request->hdr.instance_id, 0); |
| 878 | ASSERT_EQ(request->hdr.type, PLDM_OEM); |
| 879 | ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY); |
| 880 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 881 | ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe))); |
| 882 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe, |
| 883 | sizeof(fileHandleLe))); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 884 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 885 | ASSERT_EQ( |
| 886 | 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe), |
| 887 | &offsetLe, sizeof(offsetLe))); |
| 888 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) + |
| 889 | sizeof(fileHandleLe) + sizeof(offsetLe), |
| 890 | &lengthLe, sizeof(lengthLe))); |
| 891 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) + |
| 892 | sizeof(fileHandleLe) + sizeof(offsetLe) + |
| 893 | sizeof(lengthLe), |
| 894 | &addressLe, sizeof(addressLe))); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | TEST(ReadWriteFileByTypeMemory, testGoodEncodeResponse) |
| 898 | { |
| 899 | std::array<uint8_t, |
| 900 | sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES> |
| 901 | responseMsg{}; |
| 902 | |
| 903 | uint32_t length = 0x13245768; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 904 | uint32_t lengthLe = htole32(length); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 905 | uint8_t completionCode = 0x0; |
| 906 | |
| 907 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 908 | |
| 909 | auto rc = encode_rw_file_by_type_memory_resp( |
| 910 | 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, completionCode, length, |
| 911 | response); |
| 912 | |
| 913 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 914 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 915 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 916 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 917 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY); |
| 918 | |
| 919 | ASSERT_EQ( |
| 920 | 0, memcmp(response->payload, &completionCode, sizeof(completionCode))); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 921 | ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe, |
| 922 | sizeof(lengthLe))); |
vkaverap | 0740456 | 2019-08-05 22:57:11 -0500 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | TEST(ReadWriteFileByTypeMemory, testBadEncodeResponse) |
| 926 | { |
| 927 | std::array<uint8_t, |
| 928 | sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES> |
| 929 | responseMsg{}; |
| 930 | uint32_t length = 0; |
| 931 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 932 | |
| 933 | // completion code is PLDM_ERROR |
| 934 | auto rc = encode_rw_file_by_type_memory_resp( |
| 935 | 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_ERROR, length, response); |
| 936 | |
| 937 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 938 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 939 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 940 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 941 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY); |
| 942 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 943 | |
| 944 | // response is NULL pointer |
| 945 | rc = encode_rw_file_by_type_memory_resp( |
| 946 | 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_SUCCESS, length, NULL); |
| 947 | |
| 948 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 949 | } |
| 950 | |
| 951 | TEST(ReadWriteFileByTypeMemory, testBadEncodeRequest) |
| 952 | { |
| 953 | uint8_t fileType = 0; |
| 954 | uint32_t fileHandle = 0; |
| 955 | uint32_t offset = 0; |
| 956 | uint32_t length = 0; |
| 957 | uint64_t address = 0; |
| 958 | |
| 959 | // request is NULL pointer |
| 960 | auto rc = encode_rw_file_by_type_memory_req( |
| 961 | 0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset, |
| 962 | length, address, NULL); |
| 963 | |
| 964 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 965 | } |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 966 | |
| 967 | TEST(NewFile, testGoodDecodeRequest) |
| 968 | { |
| 969 | std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 970 | requestMsg{}; |
| 971 | |
| 972 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 973 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 974 | auto request = reinterpret_cast<pldm_new_file_req*>(requestPtr->payload); |
| 975 | |
| 976 | // Random value for fileHandle and length |
| 977 | uint16_t fileType = 0xFF; |
| 978 | uint32_t fileHandle = 0x12345678; |
Deepak Kodihalli | 8338876 | 2020-01-28 04:09:58 -0600 | [diff] [blame] | 979 | uint64_t length = 0x13245768; |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 980 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 981 | request->file_type = htole16(fileType); |
| 982 | request->file_handle = htole32(fileHandle); |
| 983 | request->length = htole64(length); |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 984 | |
| 985 | uint16_t retFileType = 0xFF; |
| 986 | uint32_t retFileHandle = 0; |
Deepak Kodihalli | 8338876 | 2020-01-28 04:09:58 -0600 | [diff] [blame] | 987 | uint64_t retLength = 0; |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 988 | |
| 989 | // Invoke decode the read file request |
| 990 | auto rc = decode_new_file_req(requestPtr, payload_length, &retFileType, |
| 991 | &retFileHandle, &retLength); |
| 992 | |
| 993 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 994 | ASSERT_EQ(fileType, retFileType); |
| 995 | ASSERT_EQ(fileHandle, retFileHandle); |
| 996 | ASSERT_EQ(length, retLength); |
| 997 | } |
| 998 | |
| 999 | TEST(NewFile, testGoodDecodeResponse) |
| 1000 | { |
| 1001 | std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1002 | responseMsg{}; |
| 1003 | |
| 1004 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1005 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 1006 | auto response = reinterpret_cast<pldm_new_file_resp*>(responsePtr->payload); |
| 1007 | |
| 1008 | // Random value for completion code |
| 1009 | uint8_t completionCode = 0x0; |
| 1010 | |
| 1011 | response->completion_code = completionCode; |
| 1012 | |
| 1013 | uint8_t retCompletionCode = PLDM_SUCCESS; |
| 1014 | |
| 1015 | // Invoke decode the read/write file response |
| 1016 | auto rc = |
| 1017 | decode_new_file_resp(responsePtr, payload_length, &retCompletionCode); |
| 1018 | |
| 1019 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1020 | ASSERT_EQ(completionCode, retCompletionCode); |
| 1021 | } |
| 1022 | |
| 1023 | TEST(NewFile, testBadDecodeRequest) |
| 1024 | { |
| 1025 | uint16_t fileType = 0; |
| 1026 | uint32_t fileHandle = 0; |
Deepak Kodihalli | 8338876 | 2020-01-28 04:09:58 -0600 | [diff] [blame] | 1027 | uint64_t length = 0; |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 1028 | |
| 1029 | // Request payload message is missing |
| 1030 | auto rc = decode_new_file_req(NULL, 0, &fileType, &fileHandle, &length); |
| 1031 | |
| 1032 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1033 | |
| 1034 | std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 1035 | requestMsg{}; |
| 1036 | |
| 1037 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1038 | |
| 1039 | // Payload length is invalid |
| 1040 | rc = decode_new_file_req(requestPtr, 0, &fileType, &fileHandle, &length); |
| 1041 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1042 | } |
| 1043 | |
| 1044 | TEST(NewFile, testBadDecodeResponse) |
| 1045 | { |
| 1046 | uint8_t completionCode = 0; |
| 1047 | |
| 1048 | // Request payload message is missing |
| 1049 | auto rc = decode_new_file_resp(NULL, 0, &completionCode); |
| 1050 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1051 | |
| 1052 | std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1053 | responseMsg{}; |
| 1054 | |
| 1055 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1056 | |
| 1057 | // Payload length is invalid |
| 1058 | rc = decode_new_file_resp(responsePtr, 0, &completionCode); |
| 1059 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1060 | } |
| 1061 | |
| 1062 | TEST(NewFile, testGoodEncodeRequest) |
| 1063 | { |
| 1064 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_REQ_BYTES> |
| 1065 | requestMsg{}; |
| 1066 | |
| 1067 | uint16_t fileType = 0xFF; |
| 1068 | uint32_t fileHandle = 0x12345678; |
| 1069 | uint32_t length = 0x13245768; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1070 | uint16_t fileTypeLe = htole16(fileType); |
| 1071 | uint32_t fileHandleLe = htole32(fileHandle); |
| 1072 | uint32_t lengthLe = htole32(length); |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 1073 | |
| 1074 | pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1075 | |
| 1076 | auto rc = encode_new_file_req(0, fileType, fileHandle, length, request); |
| 1077 | |
| 1078 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1079 | ASSERT_EQ(request->hdr.request, PLDM_REQUEST); |
| 1080 | ASSERT_EQ(request->hdr.instance_id, 0); |
| 1081 | ASSERT_EQ(request->hdr.type, PLDM_OEM); |
| 1082 | ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1083 | ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe))); |
| 1084 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe, |
| 1085 | sizeof(fileHandleLe))); |
| 1086 | ASSERT_EQ( |
| 1087 | 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe), |
| 1088 | &lengthLe, sizeof(lengthLe))); |
vkaverap | a9aac72 | 2019-08-22 02:10:15 -0500 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | TEST(NewFile, testGoodEncodeResponse) |
| 1092 | { |
| 1093 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES> |
| 1094 | responseMsg{}; |
| 1095 | |
| 1096 | uint8_t completionCode = 0x0; |
| 1097 | |
| 1098 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1099 | |
| 1100 | auto rc = encode_new_file_resp(0, completionCode, response); |
| 1101 | |
| 1102 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1103 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1104 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1105 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1106 | ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE); |
| 1107 | ASSERT_EQ( |
| 1108 | 0, memcmp(response->payload, &completionCode, sizeof(completionCode))); |
| 1109 | } |
| 1110 | |
| 1111 | TEST(NewFile, testBadEncodeResponse) |
| 1112 | { |
| 1113 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES> |
| 1114 | responseMsg{}; |
| 1115 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1116 | |
| 1117 | // completion code is PLDM_ERROR |
| 1118 | auto rc = encode_new_file_resp(0, PLDM_ERROR, response); |
| 1119 | |
| 1120 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1121 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1122 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1123 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1124 | ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE); |
| 1125 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 1126 | |
| 1127 | // response is NULL pointer |
| 1128 | rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL); |
| 1129 | |
| 1130 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1131 | } |
| 1132 | |
| 1133 | TEST(NewFile, testBadEncodeRequest) |
| 1134 | { |
| 1135 | uint8_t fileType = 0xFF; |
| 1136 | uint32_t fileHandle = 0; |
| 1137 | uint32_t length = 0; |
| 1138 | |
| 1139 | // request is NULL pointer |
| 1140 | auto rc = encode_new_file_req(0, fileType, fileHandle, length, NULL); |
| 1141 | |
| 1142 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1143 | } |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1144 | |
| 1145 | TEST(ReadWriteFileByType, testGoodDecodeRequest) |
| 1146 | { |
| 1147 | std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 1148 | requestMsg{}; |
| 1149 | |
| 1150 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1151 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 1152 | auto request = reinterpret_cast<pldm_read_write_file_by_type_req*>( |
| 1153 | requestPtr->payload); |
| 1154 | |
| 1155 | // Random value for fileHandle, offset and length |
| 1156 | uint16_t fileType = 0; |
| 1157 | uint32_t fileHandle = 0x12345678; |
| 1158 | uint32_t offset = 0x87654321; |
| 1159 | uint32_t length = 0x13245768; |
| 1160 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1161 | request->file_handle = htole32(fileHandle); |
| 1162 | request->offset = htole32(offset); |
| 1163 | request->length = htole32(length); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1164 | |
| 1165 | uint16_t retFileType = 0x1; |
| 1166 | uint32_t retFileHandle = 0; |
| 1167 | uint32_t retOffset = 0; |
| 1168 | uint32_t retLength = 0; |
| 1169 | |
| 1170 | // Invoke decode the read file request |
| 1171 | auto rc = |
| 1172 | decode_rw_file_by_type_req(requestPtr, payload_length, &retFileType, |
| 1173 | &retFileHandle, &retOffset, &retLength); |
| 1174 | |
| 1175 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1176 | ASSERT_EQ(fileType, retFileType); |
| 1177 | ASSERT_EQ(fileHandle, retFileHandle); |
| 1178 | ASSERT_EQ(offset, retOffset); |
| 1179 | ASSERT_EQ(length, retLength); |
| 1180 | } |
| 1181 | |
| 1182 | TEST(ReadWriteFileByType, testGoodDecodeResponse) |
| 1183 | { |
| 1184 | std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1185 | responseMsg{}; |
| 1186 | |
| 1187 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1188 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 1189 | auto response = reinterpret_cast<pldm_read_write_file_by_type_resp*>( |
| 1190 | responsePtr->payload); |
| 1191 | |
| 1192 | // Random value for completion code and length |
| 1193 | uint8_t completionCode = 0x0; |
| 1194 | uint32_t length = 0x13245768; |
| 1195 | |
| 1196 | response->completion_code = completionCode; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1197 | response->length = htole32(length); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1198 | |
| 1199 | uint8_t retCompletionCode = 0x1; |
| 1200 | uint32_t retLength = 0; |
| 1201 | |
| 1202 | // Invoke decode the read/write file response |
| 1203 | auto rc = decode_rw_file_by_type_resp(responsePtr, payload_length, |
| 1204 | &retCompletionCode, &retLength); |
| 1205 | |
| 1206 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1207 | ASSERT_EQ(completionCode, retCompletionCode); |
| 1208 | ASSERT_EQ(length, retLength); |
| 1209 | } |
| 1210 | |
| 1211 | TEST(ReadWriteFileByType, testBadDecodeRequest) |
| 1212 | { |
| 1213 | uint16_t fileType = 0; |
| 1214 | uint32_t fileHandle = 0; |
| 1215 | uint32_t offset = 0; |
| 1216 | uint32_t length = 0; |
| 1217 | |
| 1218 | // Request payload message is missing |
| 1219 | auto rc = decode_rw_file_by_type_req(NULL, 0, &fileType, &fileHandle, |
| 1220 | &offset, &length); |
| 1221 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1222 | |
| 1223 | std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 1224 | requestMsg{}; |
| 1225 | |
| 1226 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1227 | |
| 1228 | // Payload length is invalid |
| 1229 | rc = decode_rw_file_by_type_req(requestPtr, 0, &fileType, &fileHandle, |
| 1230 | &offset, &length); |
| 1231 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1232 | } |
| 1233 | |
| 1234 | TEST(ReadWriteFileByType, testBadDecodeResponse) |
| 1235 | { |
| 1236 | uint32_t length = 0; |
| 1237 | uint8_t completionCode = 0; |
| 1238 | |
| 1239 | // Request payload message is missing |
| 1240 | auto rc = decode_rw_file_by_type_resp(NULL, 0, &completionCode, &length); |
| 1241 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1242 | |
| 1243 | std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1244 | responseMsg{}; |
| 1245 | |
| 1246 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1247 | |
| 1248 | // Length is NULL |
| 1249 | rc = decode_rw_file_by_type_resp(responsePtr, responseMsg.size() - hdrSize, |
| 1250 | &completionCode, NULL); |
| 1251 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1252 | |
| 1253 | // Payload length is invalid |
| 1254 | rc = decode_rw_file_by_type_resp(responsePtr, 0, &completionCode, &length); |
| 1255 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1256 | } |
| 1257 | |
| 1258 | TEST(ReadWriteFileByType, testGoodEncodeRequest) |
| 1259 | { |
| 1260 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_REQ_BYTES> |
| 1261 | requestMsg{}; |
| 1262 | |
| 1263 | uint16_t fileType = 0; |
| 1264 | uint32_t fileHandle = 0x12345678; |
| 1265 | uint32_t offset = 0x87654321; |
| 1266 | uint32_t length = 0x13245768; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1267 | uint16_t fileTypeLe = htole16(fileType); |
| 1268 | uint32_t fileHandleLe = htole32(fileHandle); |
| 1269 | uint32_t offsetLe = htole32(offset); |
| 1270 | uint32_t lengthLe = htole32(length); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1271 | |
| 1272 | pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1273 | |
| 1274 | auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType, |
| 1275 | fileHandle, offset, length, request); |
| 1276 | |
| 1277 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1278 | ASSERT_EQ(request->hdr.request, PLDM_REQUEST); |
| 1279 | ASSERT_EQ(request->hdr.instance_id, 0); |
| 1280 | ASSERT_EQ(request->hdr.type, PLDM_OEM); |
| 1281 | ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE); |
| 1282 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1283 | ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe))); |
| 1284 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe, |
| 1285 | sizeof(fileHandleLe))); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1286 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1287 | ASSERT_EQ( |
| 1288 | 0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe), |
| 1289 | &offsetLe, sizeof(offsetLe))); |
| 1290 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) + |
| 1291 | sizeof(fileHandleLe) + sizeof(offsetLe), |
| 1292 | &lengthLe, sizeof(lengthLe))); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | TEST(ReadWriteFileByType, testGoodEncodeResponse) |
| 1296 | { |
| 1297 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES> |
| 1298 | responseMsg{}; |
| 1299 | |
| 1300 | uint32_t length = 0x13245768; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1301 | uint32_t lengthLe = htole32(length); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1302 | uint8_t completionCode = 0x0; |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1303 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1304 | |
| 1305 | auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, |
| 1306 | completionCode, length, response); |
| 1307 | |
| 1308 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1309 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1310 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1311 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1312 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE); |
| 1313 | |
| 1314 | ASSERT_EQ( |
| 1315 | 0, memcmp(response->payload, &completionCode, sizeof(completionCode))); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1316 | ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe, |
| 1317 | sizeof(lengthLe))); |
Deepak Kodihalli | dce1c99 | 2019-11-19 07:06:53 -0600 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | TEST(ReadWriteFileByType, testBadEncodeResponse) |
| 1321 | { |
| 1322 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES> |
| 1323 | responseMsg{}; |
| 1324 | uint32_t length = 0; |
| 1325 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1326 | |
| 1327 | // completion code is PLDM_ERROR |
| 1328 | auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_ERROR, |
| 1329 | length, response); |
| 1330 | |
| 1331 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1332 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1333 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1334 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1335 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE); |
| 1336 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 1337 | |
| 1338 | // response is NULL pointer |
| 1339 | rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_SUCCESS, |
| 1340 | length, NULL); |
| 1341 | |
| 1342 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1343 | } |
| 1344 | |
| 1345 | TEST(ReadWriteFileByType, testBadEncodeRequest) |
| 1346 | { |
| 1347 | uint8_t fileType = 0; |
| 1348 | uint32_t fileHandle = 0; |
| 1349 | uint32_t offset = 0; |
| 1350 | uint32_t length = 0; |
| 1351 | |
| 1352 | // request is NULL pointer |
| 1353 | auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType, |
| 1354 | fileHandle, offset, length, NULL); |
| 1355 | |
| 1356 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1357 | } |
vkaverap | f4e0a49 | 2019-11-19 01:47:35 -0600 | [diff] [blame] | 1358 | |
| 1359 | TEST(FileAck, testGoodDecodeRequest) |
| 1360 | { |
| 1361 | std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 1362 | requestMsg{}; |
| 1363 | |
| 1364 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1365 | size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 1366 | auto request = reinterpret_cast<pldm_file_ack_req*>(requestPtr->payload); |
| 1367 | |
| 1368 | // Random value for fileHandle |
| 1369 | uint16_t fileType = 0xFFFF; |
| 1370 | uint32_t fileHandle = 0x12345678; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1371 | uint8_t fileStatus = 0xFF; |
vkaverap | f4e0a49 | 2019-11-19 01:47:35 -0600 | [diff] [blame] | 1372 | |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1373 | request->file_type = htole16(fileType); |
| 1374 | request->file_handle = htole32(fileHandle); |
vkaverap | f4e0a49 | 2019-11-19 01:47:35 -0600 | [diff] [blame] | 1375 | request->file_status = fileStatus; |
| 1376 | |
| 1377 | uint16_t retFileType = 0xFF; |
| 1378 | uint32_t retFileHandle = 0; |
| 1379 | uint8_t retFileStatus = 0; |
| 1380 | |
| 1381 | // Invoke decode the read file request |
| 1382 | auto rc = decode_file_ack_req(requestPtr, payload_length, &retFileType, |
| 1383 | &retFileHandle, &retFileStatus); |
| 1384 | |
| 1385 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1386 | ASSERT_EQ(fileType, retFileType); |
| 1387 | ASSERT_EQ(fileHandle, retFileHandle); |
| 1388 | ASSERT_EQ(fileStatus, retFileStatus); |
| 1389 | } |
| 1390 | |
| 1391 | TEST(FileAck, testGoodDecodeResponse) |
| 1392 | { |
| 1393 | std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1394 | responseMsg{}; |
| 1395 | |
| 1396 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1397 | size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr); |
| 1398 | auto response = reinterpret_cast<pldm_file_ack_resp*>(responsePtr->payload); |
| 1399 | |
| 1400 | // Random value for completion code |
| 1401 | uint8_t completionCode = 0x0; |
| 1402 | |
| 1403 | response->completion_code = completionCode; |
| 1404 | |
| 1405 | uint8_t retCompletionCode = PLDM_SUCCESS; |
| 1406 | |
| 1407 | // Invoke decode the read/write file response |
| 1408 | auto rc = |
| 1409 | decode_file_ack_resp(responsePtr, payload_length, &retCompletionCode); |
| 1410 | |
| 1411 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1412 | ASSERT_EQ(completionCode, retCompletionCode); |
| 1413 | } |
| 1414 | |
| 1415 | TEST(FileAck, testBadDecodeRequest) |
| 1416 | { |
| 1417 | uint16_t fileType = 0; |
| 1418 | uint32_t fileHandle = 0; |
| 1419 | uint8_t fileStatus = 0; |
| 1420 | |
| 1421 | // Request payload message is missing |
| 1422 | auto rc = decode_file_ack_req(NULL, 0, &fileType, &fileHandle, &fileStatus); |
| 1423 | |
| 1424 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1425 | |
| 1426 | std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)> |
| 1427 | requestMsg{}; |
| 1428 | |
| 1429 | auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1430 | |
| 1431 | // Payload length is invalid |
| 1432 | rc = |
| 1433 | decode_file_ack_req(requestPtr, 0, &fileType, &fileHandle, &fileStatus); |
| 1434 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1435 | } |
| 1436 | |
| 1437 | TEST(FileAck, testBadDecodeResponse) |
| 1438 | { |
| 1439 | uint8_t completionCode = 0; |
| 1440 | |
| 1441 | // Request payload message is missing |
| 1442 | auto rc = decode_file_ack_resp(NULL, 0, &completionCode); |
| 1443 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1444 | |
| 1445 | std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)> |
| 1446 | responseMsg{}; |
| 1447 | |
| 1448 | auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1449 | |
| 1450 | // Payload length is invalid |
| 1451 | rc = decode_file_ack_resp(responsePtr, 0, &completionCode); |
| 1452 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 1453 | } |
| 1454 | |
| 1455 | TEST(FileAck, testGoodEncodeRequest) |
| 1456 | { |
| 1457 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_REQ_BYTES> |
| 1458 | requestMsg{}; |
| 1459 | |
| 1460 | uint16_t fileType = 0xFFFF; |
| 1461 | uint32_t fileHandle = 0x12345678; |
| 1462 | uint8_t fileStatus = 0xFF; |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1463 | uint16_t fileTypeLe = htole16(fileType); |
| 1464 | uint32_t fileHandleLe = htole32(fileHandle); |
vkaverap | f4e0a49 | 2019-11-19 01:47:35 -0600 | [diff] [blame] | 1465 | |
| 1466 | pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 1467 | |
| 1468 | auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, request); |
| 1469 | |
| 1470 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1471 | ASSERT_EQ(request->hdr.request, PLDM_REQUEST); |
| 1472 | ASSERT_EQ(request->hdr.instance_id, 0); |
| 1473 | ASSERT_EQ(request->hdr.type, PLDM_OEM); |
| 1474 | ASSERT_EQ(request->hdr.command, PLDM_FILE_ACK); |
Lei YU | 2d5c745 | 2020-03-03 14:43:45 +0800 | [diff] [blame] | 1475 | ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe))); |
| 1476 | ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe, |
| 1477 | sizeof(fileHandleLe))); |
vkaverap | f4e0a49 | 2019-11-19 01:47:35 -0600 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | TEST(FileAck, testGoodEncodeResponse) |
| 1481 | { |
| 1482 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES> |
| 1483 | responseMsg{}; |
| 1484 | |
| 1485 | uint8_t completionCode = 0x0; |
| 1486 | |
| 1487 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1488 | |
| 1489 | auto rc = encode_file_ack_resp(0, completionCode, response); |
| 1490 | |
| 1491 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1492 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1493 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1494 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1495 | ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK); |
| 1496 | ASSERT_EQ( |
| 1497 | 0, memcmp(response->payload, &completionCode, sizeof(completionCode))); |
| 1498 | } |
| 1499 | |
| 1500 | TEST(FileAck, testBadEncodeResponse) |
| 1501 | { |
| 1502 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES> |
| 1503 | responseMsg{}; |
| 1504 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 1505 | |
| 1506 | // completion code is PLDM_ERROR |
| 1507 | auto rc = encode_file_ack_resp(0, PLDM_ERROR, response); |
| 1508 | |
| 1509 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 1510 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 1511 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 1512 | ASSERT_EQ(response->hdr.type, PLDM_OEM); |
| 1513 | ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK); |
| 1514 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 1515 | |
| 1516 | // response is NULL pointer |
| 1517 | rc = encode_file_ack_resp(0, PLDM_SUCCESS, NULL); |
| 1518 | |
| 1519 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1520 | } |
| 1521 | |
| 1522 | TEST(FileAck, testBadEncodeRequest) |
| 1523 | { |
| 1524 | uint8_t fileType = 0xFF; |
| 1525 | uint32_t fileHandle = 0; |
| 1526 | uint8_t fileStatus = 0; |
| 1527 | |
| 1528 | // request is NULL pointer |
| 1529 | auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, nullptr); |
| 1530 | |
| 1531 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 1532 | } |