Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 1 | #include "libpldmresponder/file_io.hpp" |
Tom Joseph | b4ecdf2 | 2019-05-15 12:16:40 +0530 | [diff] [blame] | 2 | #include "libpldmresponder/file_table.hpp" |
| 3 | |
| 4 | #include <filesystem> |
| 5 | #include <fstream> |
| 6 | #include <nlohmann/json.hpp> |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 7 | |
| 8 | #include "libpldm/base.h" |
| 9 | #include "libpldm/file_io.h" |
| 10 | |
| 11 | #include <gmock/gmock-matchers.h> |
| 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
Tom Joseph | b4ecdf2 | 2019-05-15 12:16:40 +0530 | [diff] [blame] | 14 | |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 15 | #define SD_JOURNAL_SUPPRESS_LOCATION |
| 16 | |
| 17 | #include <systemd/sd-journal.h> |
| 18 | |
| 19 | std::vector<std::string> logs; |
| 20 | |
| 21 | extern "C" { |
| 22 | |
| 23 | int sd_journal_send(const char* format, ...) |
| 24 | { |
| 25 | logs.push_back(format); |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | int sd_journal_send_with_location(const char* file, const char* line, |
| 30 | const char* func, const char* format, ...) |
| 31 | { |
| 32 | logs.push_back(format); |
| 33 | return 0; |
| 34 | } |
| 35 | } |
| 36 | |
Tom Joseph | b4ecdf2 | 2019-05-15 12:16:40 +0530 | [diff] [blame] | 37 | namespace fs = std::filesystem; |
| 38 | using Json = nlohmann::json; |
| 39 | using namespace pldm::filetable; |
| 40 | |
| 41 | class TestFileTable : public testing::Test |
| 42 | { |
| 43 | public: |
| 44 | void SetUp() override |
| 45 | { |
| 46 | // Create a temporary directory to hold the config file and files to |
| 47 | // populate the file table. |
| 48 | char tmppldm[] = "/tmp/pldm_fileio_table.XXXXXX"; |
| 49 | dir = fs::path(mkdtemp(tmppldm)); |
| 50 | |
| 51 | // Copy the sample image files to the directory |
| 52 | fs::copy("./files", dir); |
| 53 | |
| 54 | imageFile = dir / "NVRAM-IMAGE"; |
| 55 | auto jsonObjects = Json::array(); |
| 56 | auto obj = Json::object(); |
| 57 | obj["path"] = imageFile.c_str(); |
| 58 | obj["file_traits"] = 1; |
| 59 | |
| 60 | jsonObjects.push_back(obj); |
| 61 | obj.clear(); |
| 62 | cksumFile = dir / "NVRAM-IMAGE-CKSUM"; |
| 63 | obj["path"] = cksumFile.c_str(); |
| 64 | obj["file_traits"] = 4; |
| 65 | jsonObjects.push_back(obj); |
| 66 | |
| 67 | fileTableConfig = dir / "configFile.json"; |
| 68 | std::ofstream file(fileTableConfig.c_str()); |
| 69 | file << std::setw(4) << jsonObjects << std::endl; |
| 70 | } |
| 71 | |
| 72 | void TearDown() override |
| 73 | { |
| 74 | fs::remove_all(dir); |
| 75 | } |
| 76 | |
| 77 | fs::path dir; |
| 78 | fs::path imageFile; |
| 79 | fs::path cksumFile; |
| 80 | fs::path fileTableConfig; |
| 81 | |
| 82 | // <4 bytes - File handle - 0 (0x00 0x00 0x00 0x00)>, |
| 83 | // <2 bytes - Filename length - 11 (0x0b 0x00> |
| 84 | // <11 bytes - Filename - ASCII for NVRAM-IMAGE> |
| 85 | // <4 bytes - File size - 1024 (0x00 0x04 0x00 0x00)> |
| 86 | // <4 bytes - File traits - 1 (0x01 0x00 0x00 0x00)> |
| 87 | // <4 bytes - File handle - 1 (0x01 0x00 0x00 0x00)>, |
| 88 | // <2 bytes - Filename length - 17 (0x11 0x00> |
| 89 | // <17 bytes - Filename - ASCII for NVRAM-IMAGE-CKSUM> |
| 90 | // <4 bytes - File size - 16 (0x0f 0x00 0x00 0x00)> |
| 91 | // <4 bytes - File traits - 4 (0x04 0x00 0x00 0x00)> |
| 92 | // No pad bytes added since the length for both the file entries in the |
| 93 | // table is 56, which is a multiple of 4. |
| 94 | // <4 bytes - Checksum - 2088303182(0x4e 0xfa 0x78 0x7c)> |
| 95 | Table attrTable = { |
| 96 | 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x4e, 0x56, 0x52, 0x41, 0x4d, 0x2d, |
| 97 | 0x49, 0x4d, 0x41, 0x47, 0x45, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 98 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x4e, 0x56, 0x52, 0x41, 0x4d, |
| 99 | 0x2d, 0x49, 0x4d, 0x41, 0x47, 0x45, 0x2d, 0x43, 0x4b, 0x53, 0x55, 0x4d, |
| 100 | 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x4e, 0xfa, 0x78, 0x7c}; |
| 101 | }; |
| 102 | |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 103 | namespace pldm |
| 104 | { |
| 105 | |
| 106 | namespace responder |
| 107 | { |
| 108 | |
| 109 | namespace dma |
| 110 | { |
| 111 | |
| 112 | class MockDMA |
| 113 | { |
| 114 | public: |
| 115 | MOCK_METHOD5(transferDataHost, |
| 116 | int(const fs::path& file, uint32_t offset, uint32_t length, |
| 117 | uint64_t address, bool upstream)); |
| 118 | }; |
| 119 | |
| 120 | } // namespace dma |
| 121 | } // namespace responder |
| 122 | } // namespace pldm |
| 123 | using namespace pldm::responder; |
| 124 | using ::testing::_; |
| 125 | using ::testing::Return; |
| 126 | |
| 127 | TEST(TransferDataHost, GoodPath) |
| 128 | { |
| 129 | using namespace pldm::responder::dma; |
| 130 | |
| 131 | MockDMA dmaObj; |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 132 | fs::path path(""); |
| 133 | |
| 134 | // Minimum length of 16 and expect transferDataHost to be called once |
| 135 | // returns the default value of 0 (the return type of transferDataHost is |
| 136 | // int, the default value for int is 0) |
| 137 | uint32_t length = minSize; |
| 138 | EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, true)).Times(1); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 139 | auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, |
| 140 | path, 0, length, 0, true); |
| 141 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 142 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 143 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 144 | &length, sizeof(length))); |
| 145 | |
| 146 | // maxsize of DMA |
| 147 | length = maxSize; |
| 148 | EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, true)).Times(1); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 149 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
| 150 | 0, length, 0, true); |
| 151 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 152 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 153 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 154 | &length, sizeof(length))); |
| 155 | |
| 156 | // length greater than maxsize of DMA |
| 157 | length = maxSize + minSize; |
| 158 | EXPECT_CALL(dmaObj, transferDataHost(path, 0, maxSize, 0, true)).Times(1); |
| 159 | EXPECT_CALL(dmaObj, transferDataHost(path, maxSize, minSize, maxSize, true)) |
| 160 | .Times(1); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 161 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
| 162 | 0, length, 0, true); |
| 163 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 164 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 165 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 166 | &length, sizeof(length))); |
| 167 | |
| 168 | // length greater than 2*maxsize of DMA |
| 169 | length = 3 * maxSize; |
| 170 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, true)).Times(3); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 171 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
| 172 | 0, length, 0, true); |
| 173 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 174 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 175 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 176 | &length, sizeof(length))); |
| 177 | |
| 178 | // check for downstream(copy data from host to BMC) parameter |
| 179 | length = minSize; |
| 180 | EXPECT_CALL(dmaObj, transferDataHost(path, 0, length, 0, false)).Times(1); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 181 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
| 182 | 0, length, 0, false); |
| 183 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 184 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 185 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 186 | &length, sizeof(length))); |
| 187 | } |
| 188 | |
| 189 | TEST(TransferDataHost, BadPath) |
| 190 | { |
| 191 | using namespace pldm::responder::dma; |
| 192 | |
| 193 | MockDMA dmaObj; |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 194 | fs::path path(""); |
| 195 | |
| 196 | // Minimum length of 16 and transferDataHost returning a negative errno |
| 197 | uint32_t length = minSize; |
| 198 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1)); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 199 | auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, |
| 200 | path, 0, length, 0, true); |
| 201 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 202 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR); |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 203 | |
| 204 | // length greater than maxsize of DMA and transferDataHost returning a |
| 205 | // negative errno |
| 206 | length = maxSize + minSize; |
| 207 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1)); |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 208 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
| 209 | 0, length, 0, true); |
| 210 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 211 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR); |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | TEST(ReadFileIntoMemory, BadPath) |
| 215 | { |
| 216 | uint32_t fileHandle = 0; |
| 217 | uint32_t offset = 0; |
| 218 | uint32_t length = 10; |
| 219 | uint64_t address = 0; |
| 220 | |
| 221 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 222 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 223 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 224 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 225 | sizeof(length)); |
| 226 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 227 | sizeof(length), |
| 228 | &address, sizeof(address)); |
| 229 | |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 230 | // Pass invalid payload length |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 231 | auto response = readFileIntoMemory(requestMsg.data(), 0); |
| 232 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 233 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 234 | } |
| 235 | |
Tom Joseph | 0722040 | 2019-05-15 16:30:41 +0530 | [diff] [blame] | 236 | TEST_F(TestFileTable, ReadFileInvalidFileHandle) |
| 237 | { |
| 238 | // Invalid file handle in the file table |
| 239 | uint32_t fileHandle = 2; |
| 240 | uint32_t offset = 0; |
| 241 | uint32_t length = 0; |
| 242 | uint64_t address = 0; |
| 243 | |
| 244 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 245 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 246 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 247 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 248 | sizeof(length)); |
| 249 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 250 | sizeof(length), |
| 251 | &address, sizeof(address)); |
| 252 | |
| 253 | using namespace pldm::filetable; |
| 254 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 255 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 256 | |
| 257 | auto response = readFileIntoMemory(requestMsg.data(), requestMsg.size()); |
| 258 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 259 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 260 | // Clear the file table contents. |
| 261 | table.clear(); |
| 262 | } |
| 263 | |
| 264 | TEST_F(TestFileTable, ReadFileInvalidOffset) |
| 265 | { |
| 266 | uint32_t fileHandle = 0; |
| 267 | // The file size is 1024, so the offset is invalid |
| 268 | uint32_t offset = 1024; |
| 269 | uint32_t length = 0; |
| 270 | uint64_t address = 0; |
| 271 | |
| 272 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 273 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 274 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 275 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 276 | sizeof(length)); |
| 277 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 278 | sizeof(length), |
| 279 | &address, sizeof(address)); |
| 280 | |
| 281 | using namespace pldm::filetable; |
| 282 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 283 | |
| 284 | auto response = readFileIntoMemory(requestMsg.data(), requestMsg.size()); |
| 285 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 286 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 287 | // Clear the file table contents. |
| 288 | table.clear(); |
| 289 | } |
| 290 | |
| 291 | TEST_F(TestFileTable, ReadFileInvalidLength) |
| 292 | { |
| 293 | uint32_t fileHandle = 0; |
| 294 | uint32_t offset = 100; |
| 295 | // Length should be a multiple of dma min size(16) |
| 296 | uint32_t length = 10; |
| 297 | uint64_t address = 0; |
| 298 | |
| 299 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 300 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 301 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 302 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 303 | sizeof(length)); |
| 304 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 305 | sizeof(length), |
| 306 | &address, sizeof(address)); |
| 307 | |
| 308 | using namespace pldm::filetable; |
| 309 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 310 | |
| 311 | auto response = readFileIntoMemory(requestMsg.data(), requestMsg.size()); |
| 312 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 313 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_READ_LENGTH); |
| 314 | // Clear the file table contents. |
| 315 | table.clear(); |
| 316 | } |
| 317 | |
| 318 | TEST_F(TestFileTable, ReadFileInvalidEffectiveLength) |
| 319 | { |
| 320 | uint32_t fileHandle = 0; |
| 321 | // valid offset |
| 322 | uint32_t offset = 100; |
| 323 | // length + offset exceeds the size, so effective length is |
| 324 | // filesize(1024) - offset(100). The effective length is not a multiple of |
| 325 | // DMA min size(16) |
| 326 | uint32_t length = 1024; |
| 327 | uint64_t address = 0; |
| 328 | |
| 329 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 330 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 331 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 332 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 333 | sizeof(length)); |
| 334 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 335 | sizeof(length), |
| 336 | &address, sizeof(address)); |
| 337 | |
| 338 | using namespace pldm::filetable; |
| 339 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 340 | |
| 341 | auto response = readFileIntoMemory(requestMsg.data(), requestMsg.size()); |
| 342 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 343 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_READ_LENGTH); |
| 344 | // Clear the file table contents. |
| 345 | table.clear(); |
| 346 | } |
| 347 | |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 348 | TEST(WriteFileFromMemory, BadPath) |
| 349 | { |
| 350 | uint32_t fileHandle = 0; |
| 351 | uint32_t offset = 0; |
| 352 | uint32_t length = 10; |
| 353 | uint64_t address = 0; |
| 354 | |
| 355 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 356 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 357 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 358 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 359 | sizeof(length)); |
| 360 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 361 | sizeof(length), |
| 362 | &address, sizeof(address)); |
| 363 | |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 364 | // Pass invalid payload length |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 365 | auto response = writeFileFromMemory(requestMsg.data(), 0); |
| 366 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 367 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 368 | |
| 369 | // The length field is not a multiple of DMA minsize |
Tom Joseph | 4e48b44 | 2019-06-04 09:23:44 +0530 | [diff] [blame] | 370 | response = writeFileFromMemory(requestMsg.data(), requestMsg.size()); |
| 371 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 372 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_WRITE_LENGTH); |
Tom Joseph | 5530676 | 2019-05-02 09:11:26 +0530 | [diff] [blame] | 373 | } |
Tom Joseph | b4ecdf2 | 2019-05-15 12:16:40 +0530 | [diff] [blame] | 374 | |
Tom Joseph | 0722040 | 2019-05-15 16:30:41 +0530 | [diff] [blame] | 375 | TEST_F(TestFileTable, WriteFileInvalidFileHandle) |
| 376 | { |
| 377 | // Invalid file handle in the file table |
| 378 | uint32_t fileHandle = 2; |
| 379 | uint32_t offset = 0; |
| 380 | uint32_t length = 16; |
| 381 | uint64_t address = 0; |
| 382 | |
| 383 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 384 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 385 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 386 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 387 | sizeof(length)); |
| 388 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 389 | sizeof(length), |
| 390 | &address, sizeof(address)); |
| 391 | |
| 392 | using namespace pldm::filetable; |
| 393 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 394 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 395 | |
| 396 | auto response = writeFileFromMemory(requestMsg.data(), requestMsg.size()); |
| 397 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 398 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 399 | // Clear the file table contents. |
| 400 | table.clear(); |
| 401 | } |
| 402 | |
| 403 | TEST_F(TestFileTable, WriteFileInvalidOffset) |
| 404 | { |
| 405 | uint32_t fileHandle = 0; |
| 406 | // The file size is 1024, so the offset is invalid |
| 407 | uint32_t offset = 1024; |
| 408 | uint32_t length = 16; |
| 409 | uint64_t address = 0; |
| 410 | |
| 411 | std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{}; |
| 412 | memcpy(requestMsg.data(), &fileHandle, sizeof(fileHandle)); |
| 413 | memcpy(requestMsg.data() + sizeof(fileHandle), &offset, sizeof(offset)); |
| 414 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset), &length, |
| 415 | sizeof(length)); |
| 416 | memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + |
| 417 | sizeof(length), |
| 418 | &address, sizeof(address)); |
| 419 | |
| 420 | using namespace pldm::filetable; |
| 421 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 422 | auto& table = buildFileTable(TestFileTable::fileTableConfig.c_str()); |
| 423 | |
| 424 | auto response = writeFileFromMemory(requestMsg.data(), requestMsg.size()); |
| 425 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 426 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 427 | // Clear the file table contents. |
| 428 | table.clear(); |
| 429 | } |
| 430 | |
Tom Joseph | b4ecdf2 | 2019-05-15 12:16:40 +0530 | [diff] [blame] | 431 | TEST(FileTable, ConfigNotExist) |
| 432 | { |
| 433 | logs.clear(); |
| 434 | FileTable tableObj(""); |
| 435 | EXPECT_EQ(logs.size(), 1); |
| 436 | } |
| 437 | |
| 438 | TEST_F(TestFileTable, ValidateFileEntry) |
| 439 | { |
| 440 | FileTable tableObj(fileTableConfig.c_str()); |
| 441 | |
| 442 | // Test file handle 0, the file size is 1K bytes. |
| 443 | auto value = tableObj.at(0); |
| 444 | ASSERT_EQ(value.handle, 0); |
| 445 | ASSERT_EQ(strcmp(value.fsPath.c_str(), imageFile.c_str()), 0); |
| 446 | ASSERT_EQ(static_cast<uint32_t>(fs::file_size(value.fsPath)), 1024); |
| 447 | ASSERT_EQ(value.traits.value, 1); |
| 448 | ASSERT_EQ(true, fs::exists(value.fsPath)); |
| 449 | |
| 450 | // Test file handle 1, the file size is 16 bytes |
| 451 | auto value1 = tableObj.at(1); |
| 452 | ASSERT_EQ(value1.handle, 1); |
| 453 | ASSERT_EQ(strcmp(value1.fsPath.c_str(), cksumFile.c_str()), 0); |
| 454 | ASSERT_EQ(static_cast<uint32_t>(fs::file_size(value1.fsPath)), 16); |
| 455 | ASSERT_EQ(value1.traits.value, 4); |
| 456 | ASSERT_EQ(true, fs::exists(value1.fsPath)); |
| 457 | |
| 458 | // Test invalid file handle |
| 459 | ASSERT_THROW(tableObj.at(2), std::out_of_range); |
| 460 | } |
| 461 | |
| 462 | TEST_F(TestFileTable, ValidateFileTable) |
| 463 | { |
| 464 | FileTable tableObj(fileTableConfig.c_str()); |
| 465 | |
| 466 | // Validate file attribute table |
| 467 | auto table = tableObj(); |
| 468 | ASSERT_EQ(true, |
| 469 | std::equal(attrTable.begin(), attrTable.end(), table.begin())); |
| 470 | } |
Tom Joseph | 1985662 | 2019-06-07 10:18:21 +0530 | [diff] [blame^] | 471 | |
| 472 | TEST_F(TestFileTable, GetFileTableCommand) |
| 473 | { |
| 474 | // Initialise the file table with a valid handle of 0 & 1 |
| 475 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 476 | |
| 477 | uint32_t transferHandle = 0; |
| 478 | uint8_t opFlag = 0; |
| 479 | uint8_t type = PLDM_FILE_ATTRIBUTE_TABLE; |
| 480 | uint32_t nextTransferHandle = 0; |
| 481 | uint8_t transferFlag = PLDM_START_AND_END; |
| 482 | |
| 483 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 484 | auto request = |
| 485 | reinterpret_cast<pldm_get_file_table_req*>(requestMsg.data()); |
| 486 | request->transfer_handle = transferHandle; |
| 487 | request->operation_flag = opFlag; |
| 488 | request->table_type = type; |
| 489 | |
| 490 | auto response = getFileTable(requestMsg.data(), requestMsg.size()); |
| 491 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 492 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 493 | size_t offsetSize = sizeof(responsePtr->payload[0]); |
| 494 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, &nextTransferHandle, |
| 495 | sizeof(nextTransferHandle))); |
| 496 | offsetSize += sizeof(nextTransferHandle); |
| 497 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, &transferFlag, |
| 498 | sizeof(transferFlag))); |
| 499 | offsetSize += sizeof(transferFlag); |
| 500 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, attrTable.data(), |
| 501 | attrTable.size())); |
| 502 | table.clear(); |
| 503 | } |
| 504 | |
| 505 | TEST_F(TestFileTable, GetFileTableCommandReqLengthMismatch) |
| 506 | { |
| 507 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 508 | |
| 509 | // Pass invalid command payload length |
| 510 | auto response = getFileTable(requestMsg.data(), 0); |
| 511 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 512 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 513 | } |
| 514 | |
| 515 | TEST_F(TestFileTable, GetFileTableCommandBufferNull) |
| 516 | { |
| 517 | // Pass null buffer for payload |
| 518 | auto response = getFileTable(nullptr, PLDM_GET_FILE_TABLE_REQ_BYTES); |
| 519 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 520 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_DATA); |
| 521 | } |
| 522 | |
| 523 | TEST_F(TestFileTable, GetFileTableCommandOEMAttrTable) |
| 524 | { |
| 525 | uint32_t transferHandle = 0; |
| 526 | uint8_t opFlag = 0; |
| 527 | uint8_t type = PLDM_OEM_FILE_ATTRIBUTE_TABLE; |
| 528 | |
| 529 | std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{}; |
| 530 | auto request = |
| 531 | reinterpret_cast<pldm_get_file_table_req*>(requestMsg.data()); |
| 532 | request->transfer_handle = transferHandle; |
| 533 | request->operation_flag = opFlag; |
| 534 | request->table_type = type; |
| 535 | |
| 536 | auto response = getFileTable(requestMsg.data(), requestMsg.size()); |
| 537 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 538 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_TABLE_TYPE); |
| 539 | } |