Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 1 | #include <string.h> |
| 2 | |
| 3 | #include <array> |
| 4 | |
| 5 | #include "libpldm/base.h" |
| 6 | #include "libpldm/file_io.h" |
| 7 | |
| 8 | #include <gtest/gtest.h> |
| 9 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 10 | TEST(ReadWriteFileMemory, testGoodDecodeRequest) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 11 | { |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 12 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 13 | |
| 14 | // Random value for fileHandle, offset, length, address |
| 15 | uint32_t fileHandle = 0x12345678; |
| 16 | uint32_t offset = 0x87654321; |
| 17 | uint32_t length = 0x13245768; |
| 18 | uint64_t address = 0x124356879ACBDE0F; |
| 19 | |
| 20 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 21 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 22 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 23 | sizeof(length)); |
| 24 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 25 | sizeof(length), |
| 26 | &address, sizeof(address)); |
| 27 | |
| 28 | uint32_t retFileHandle = 0; |
| 29 | uint32_t retOffset = 0; |
| 30 | uint32_t retLength = 0; |
| 31 | uint64_t retAddress = 0; |
| 32 | |
| 33 | // Invoke decode the read file memory request |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 34 | auto rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(), |
| 35 | &retFileHandle, &retOffset, &retLength, |
| 36 | &retAddress); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 37 | |
| 38 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 39 | ASSERT_EQ(fileHandle, retFileHandle); |
| 40 | ASSERT_EQ(offset, retOffset); |
| 41 | ASSERT_EQ(length, retLength); |
| 42 | ASSERT_EQ(address, retAddress); |
| 43 | } |
| 44 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 45 | TEST(ReadWriteFileMemory, testBadDecodeRequest) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 46 | { |
| 47 | uint32_t fileHandle = 0; |
| 48 | uint32_t offset = 0; |
| 49 | uint32_t length = 0; |
| 50 | uint64_t address = 0; |
| 51 | |
| 52 | // Request payload message is missing |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 53 | auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length, |
| 54 | &address); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 55 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 56 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 57 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 58 | |
| 59 | // Address is NULL |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 60 | rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(), |
| 61 | &fileHandle, &offset, &length, NULL); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 62 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 63 | |
| 64 | // Payload length is invalid |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 65 | rc = decode_rw_file_memory_req(requestMsg.data(), 0, &fileHandle, &offset, |
| 66 | &length, &address); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 67 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 68 | } |
| 69 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 70 | TEST(ReadWriteFileMemory, testGoodEncodeResponse) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 71 | { |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 72 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES> |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 73 | responseMsg{}; |
| 74 | uint32_t length = 0xFF00EE11; |
| 75 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 76 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 77 | // ReadFileIntoMemory |
| 78 | auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY, |
| 79 | PLDM_SUCCESS, length, response); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 80 | |
| 81 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 82 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 83 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 84 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 85 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY); |
| 86 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 87 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
| 88 | &length, sizeof(length))); |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 89 | |
| 90 | // WriteFileFromMemory |
| 91 | rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_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); |
| 97 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 98 | ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY); |
| 99 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 100 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
| 101 | &length, sizeof(length))); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 104 | TEST(ReadWriteFileMemory, testBadEncodeResponse) |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 105 | { |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 106 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES> |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 107 | responseMsg{}; |
| 108 | uint32_t length = 0; |
| 109 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 110 | |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 111 | // ReadFileIntoMemory |
| 112 | auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY, |
| 113 | PLDM_ERROR, length, response); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 114 | |
| 115 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 116 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 117 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 118 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 119 | ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY); |
| 120 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
Eddie James | 3b02e27 | 2019-04-22 20:13:55 +0000 | [diff] [blame] | 121 | |
| 122 | // WriteFileFromMemory |
| 123 | rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR, |
| 124 | length, response); |
| 125 | |
| 126 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 127 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 128 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 129 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 130 | ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY); |
| 131 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
Tom Joseph | 7dae777 | 2019-04-10 14:44:44 +0530 | [diff] [blame] | 132 | } |
Tom Joseph | 61325fa | 2019-06-04 15:22:22 +0530 | [diff] [blame] | 133 | |
| 134 | TEST(GetFileTable, GoodDecodeRequest) |
| 135 | { |
| 136 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 137 | |
| 138 | // Random value for DataTransferHandle, TransferOperationFlag, TableType |
| 139 | uint32_t transferHandle = 0x12345678; |
| 140 | uint8_t transferOpFlag = 1; |
| 141 | uint8_t tableType = 1; |
| 142 | |
| 143 | memcpy(requestMsg.data(), &transferHandle, sizeof(transferHandle)); |
| 144 | memcpy(requestMsg.data() + sizeof(transferHandle), &transferOpFlag, |
| 145 | sizeof(transferOpFlag)); |
| 146 | memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag), |
| 147 | &tableType, sizeof(tableType)); |
| 148 | |
| 149 | uint32_t retTransferHandle = 0; |
| 150 | uint8_t retTransferOpFlag = 0; |
| 151 | uint8_t retTableType = 0; |
| 152 | |
| 153 | // Invoke decode get file table request |
| 154 | auto rc = decode_get_file_table_req(requestMsg.data(), requestMsg.size(), |
| 155 | &retTransferHandle, &retTransferOpFlag, |
| 156 | &retTableType); |
| 157 | |
| 158 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 159 | ASSERT_EQ(transferHandle, retTransferHandle); |
| 160 | ASSERT_EQ(transferOpFlag, retTransferOpFlag); |
| 161 | ASSERT_EQ(tableType, retTableType); |
| 162 | } |
| 163 | |
| 164 | TEST(GetFileTable, BadDecodeRequest) |
| 165 | { |
| 166 | uint32_t transferHandle = 0; |
| 167 | uint8_t transferOpFlag = 0; |
| 168 | uint8_t tableType = 0; |
| 169 | |
| 170 | // Request payload message is missing |
| 171 | auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle, |
| 172 | &transferOpFlag, &tableType); |
| 173 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 174 | |
| 175 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 176 | |
| 177 | // TableType is NULL |
| 178 | rc = decode_get_file_table_req(requestMsg.data(), requestMsg.size(), |
| 179 | &transferHandle, &transferOpFlag, nullptr); |
| 180 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA); |
| 181 | |
| 182 | // Payload length is invalid |
| 183 | rc = decode_get_file_table_req(requestMsg.data(), 0, &transferHandle, |
| 184 | &transferOpFlag, &tableType); |
| 185 | ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH); |
| 186 | } |
| 187 | |
| 188 | TEST(GetFileTable, GoodEncodeResponse) |
| 189 | { |
| 190 | // Random value for NextDataTransferHandle and TransferFlag |
| 191 | uint8_t completionCode = 0; |
| 192 | uint32_t nextTransferHandle = 0x87654321; |
| 193 | uint8_t transferFlag = 5; |
| 194 | // Mock file table contents of size 5 |
| 195 | std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5}; |
| 196 | constexpr size_t responseSize = sizeof(completionCode) + |
| 197 | sizeof(nextTransferHandle) + |
| 198 | sizeof(transferFlag) + fileTable.size(); |
| 199 | |
| 200 | std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{}; |
| 201 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 202 | |
| 203 | // GetFileTable |
| 204 | auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle, |
| 205 | transferFlag, fileTable.data(), |
| 206 | fileTable.size(), response); |
| 207 | |
| 208 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 209 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 210 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 211 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 212 | ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE); |
| 213 | ASSERT_EQ(response->payload[0], PLDM_SUCCESS); |
| 214 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]), |
| 215 | &nextTransferHandle, sizeof(nextTransferHandle))); |
| 216 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 217 | sizeof(nextTransferHandle), |
| 218 | &transferFlag, sizeof(transferFlag))); |
| 219 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 220 | sizeof(nextTransferHandle), |
| 221 | &transferFlag, sizeof(transferFlag))); |
| 222 | ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) + |
| 223 | sizeof(nextTransferHandle) + sizeof(transferFlag), |
| 224 | fileTable.data(), fileTable.size())); |
| 225 | } |
| 226 | |
| 227 | TEST(GetFileTable, BadEncodeResponse) |
| 228 | { |
| 229 | uint8_t completionCode = 0; |
| 230 | uint32_t nextTransferHandle = 0; |
| 231 | uint8_t transferFlag = 0; |
| 232 | constexpr size_t responseSize = sizeof(completionCode) + |
| 233 | sizeof(nextTransferHandle) + |
| 234 | sizeof(transferFlag); |
| 235 | |
| 236 | std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{}; |
| 237 | pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data()); |
| 238 | |
| 239 | // GetFileTable |
| 240 | auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle, |
| 241 | transferFlag, nullptr, 0, response); |
| 242 | |
| 243 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 244 | ASSERT_EQ(response->hdr.request, PLDM_RESPONSE); |
| 245 | ASSERT_EQ(response->hdr.instance_id, 0); |
| 246 | ASSERT_EQ(response->hdr.type, PLDM_IBM_OEM_TYPE); |
| 247 | ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE); |
| 248 | ASSERT_EQ(response->payload[0], PLDM_ERROR); |
| 249 | } |