George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 1 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 2 | #include "libpldmresponder/file_io.hpp" |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 3 | #include "libpldmresponder/file_io_by_type.hpp" |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 4 | #include "libpldmresponder/file_io_type_cert.hpp" |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 5 | #include "libpldmresponder/file_io_type_dump.hpp" |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 6 | #include "libpldmresponder/file_io_type_lid.hpp" |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 7 | #include "libpldmresponder/file_io_type_pel.hpp" |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 8 | #include "libpldmresponder/file_table.hpp" |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 9 | #include "xyz/openbmc_project/Common/error.hpp" |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 10 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 11 | #include <libpldm/base.h> |
| 12 | #include <libpldm/file_io.h> |
| 13 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 14 | #include <nlohmann/json.hpp> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 15 | |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 16 | #include <filesystem> |
| 17 | #include <fstream> |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 18 | |
| 19 | #include <gmock/gmock-matchers.h> |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 22 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 23 | namespace fs = std::filesystem; |
| 24 | using Json = nlohmann::json; |
| 25 | using namespace pldm::filetable; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 26 | using namespace pldm::responder; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 27 | |
| 28 | class TestFileTable : public testing::Test |
| 29 | { |
| 30 | public: |
| 31 | void SetUp() override |
| 32 | { |
| 33 | // Create a temporary directory to hold the config file and files to |
| 34 | // populate the file table. |
| 35 | char tmppldm[] = "/tmp/pldm_fileio_table.XXXXXX"; |
| 36 | dir = fs::path(mkdtemp(tmppldm)); |
| 37 | |
| 38 | // Copy the sample image files to the directory |
| 39 | fs::copy("./files", dir); |
| 40 | |
| 41 | imageFile = dir / "NVRAM-IMAGE"; |
| 42 | auto jsonObjects = Json::array(); |
| 43 | auto obj = Json::object(); |
| 44 | obj["path"] = imageFile.c_str(); |
| 45 | obj["file_traits"] = 1; |
| 46 | |
| 47 | jsonObjects.push_back(obj); |
| 48 | obj.clear(); |
| 49 | cksumFile = dir / "NVRAM-IMAGE-CKSUM"; |
| 50 | obj["path"] = cksumFile.c_str(); |
| 51 | obj["file_traits"] = 4; |
| 52 | jsonObjects.push_back(obj); |
| 53 | |
| 54 | fileTableConfig = dir / "configFile.json"; |
| 55 | std::ofstream file(fileTableConfig.c_str()); |
| 56 | file << std::setw(4) << jsonObjects << std::endl; |
| 57 | } |
| 58 | |
| 59 | void TearDown() override |
| 60 | { |
| 61 | fs::remove_all(dir); |
| 62 | } |
| 63 | |
| 64 | fs::path dir; |
| 65 | fs::path imageFile; |
| 66 | fs::path cksumFile; |
| 67 | fs::path fileTableConfig; |
| 68 | |
| 69 | // <4 bytes - File handle - 0 (0x00 0x00 0x00 0x00)>, |
| 70 | // <2 bytes - Filename length - 11 (0x0b 0x00> |
| 71 | // <11 bytes - Filename - ASCII for NVRAM-IMAGE> |
| 72 | // <4 bytes - File size - 1024 (0x00 0x04 0x00 0x00)> |
| 73 | // <4 bytes - File traits - 1 (0x01 0x00 0x00 0x00)> |
| 74 | // <4 bytes - File handle - 1 (0x01 0x00 0x00 0x00)>, |
| 75 | // <2 bytes - Filename length - 17 (0x11 0x00> |
| 76 | // <17 bytes - Filename - ASCII for NVRAM-IMAGE-CKSUM> |
| 77 | // <4 bytes - File size - 16 (0x0f 0x00 0x00 0x00)> |
| 78 | // <4 bytes - File traits - 4 (0x04 0x00 0x00 0x00)> |
| 79 | // No pad bytes added since the length for both the file entries in the |
| 80 | // table is 56, which is a multiple of 4. |
| 81 | // <4 bytes - Checksum - 2088303182(0x4e 0xfa 0x78 0x7c)> |
| 82 | Table attrTable = { |
| 83 | 0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x4e, 0x56, 0x52, 0x41, 0x4d, 0x2d, |
| 84 | 0x49, 0x4d, 0x41, 0x47, 0x45, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, |
| 85 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x4e, 0x56, 0x52, 0x41, 0x4d, |
| 86 | 0x2d, 0x49, 0x4d, 0x41, 0x47, 0x45, 0x2d, 0x43, 0x4b, 0x53, 0x55, 0x4d, |
| 87 | 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x4e, 0xfa, 0x78, 0x7c}; |
| 88 | }; |
| 89 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 90 | namespace pldm |
| 91 | { |
| 92 | |
| 93 | namespace responder |
| 94 | { |
| 95 | |
| 96 | namespace dma |
| 97 | { |
| 98 | |
| 99 | class MockDMA |
| 100 | { |
| 101 | public: |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 102 | MOCK_METHOD5(transferDataHost, int(int fd, uint32_t offset, uint32_t length, |
| 103 | uint64_t address, bool upstream)); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace dma |
| 107 | } // namespace responder |
| 108 | } // namespace pldm |
| 109 | using namespace pldm::responder; |
| 110 | using ::testing::_; |
| 111 | using ::testing::Return; |
| 112 | |
| 113 | TEST(TransferDataHost, GoodPath) |
| 114 | { |
| 115 | using namespace pldm::responder::dma; |
| 116 | |
| 117 | MockDMA dmaObj; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 118 | char tmpfile[] = "/tmp/pldm_fileio_table.XXXXXX"; |
| 119 | int fd = mkstemp(tmpfile); |
| 120 | close(fd); |
| 121 | fs::path path(tmpfile); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 122 | |
| 123 | // Minimum length of 16 and expect transferDataHost to be called once |
| 124 | // returns the default value of 0 (the return type of transferDataHost is |
| 125 | // int, the default value for int is 0) |
| 126 | uint32_t length = minSize; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 127 | EXPECT_CALL(dmaObj, transferDataHost(_, 0, length, 0, true)).Times(1); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 128 | auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 129 | path, 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 130 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 131 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 132 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 133 | &length, sizeof(length))); |
| 134 | |
| 135 | // maxsize of DMA |
| 136 | length = maxSize; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 137 | EXPECT_CALL(dmaObj, transferDataHost(_, 0, length, 0, true)).Times(1); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 138 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 139 | 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 140 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 141 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 142 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 143 | &length, sizeof(length))); |
| 144 | |
| 145 | // length greater than maxsize of DMA |
| 146 | length = maxSize + minSize; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 147 | EXPECT_CALL(dmaObj, transferDataHost(_, 0, maxSize, 0, true)).Times(1); |
| 148 | EXPECT_CALL(dmaObj, transferDataHost(_, maxSize, minSize, maxSize, true)) |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 149 | .Times(1); |
| 150 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 151 | 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 152 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 153 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 154 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 155 | &length, sizeof(length))); |
| 156 | |
| 157 | // length greater than 2*maxsize of DMA |
| 158 | length = 3 * maxSize; |
| 159 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, true)).Times(3); |
| 160 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 161 | 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 162 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 163 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 164 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 165 | &length, sizeof(length))); |
| 166 | |
| 167 | // check for downstream(copy data from host to BMC) parameter |
| 168 | length = minSize; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 169 | EXPECT_CALL(dmaObj, transferDataHost(_, 0, length, 0, false)).Times(1); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 170 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 171 | 0, length, 0, false, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 172 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 173 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 174 | ASSERT_EQ(0, memcmp(responsePtr->payload + sizeof(responsePtr->payload[0]), |
| 175 | &length, sizeof(length))); |
| 176 | } |
| 177 | |
| 178 | TEST(TransferDataHost, BadPath) |
| 179 | { |
| 180 | using namespace pldm::responder::dma; |
| 181 | |
| 182 | MockDMA dmaObj; |
Deepak Kodihalli | 15211b4 | 2019-12-14 02:24:49 -0600 | [diff] [blame] | 183 | char tmpfile[] = "/tmp/pldm_fileio_table.XXXXXX"; |
| 184 | int fd = mkstemp(tmpfile); |
| 185 | close(fd); |
| 186 | fs::path path(tmpfile); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 187 | |
| 188 | // Minimum length of 16 and transferDataHost returning a negative errno |
| 189 | uint32_t length = minSize; |
| 190 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1)); |
| 191 | auto response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 192 | path, 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 193 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 194 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR); |
| 195 | |
| 196 | // length greater than maxsize of DMA and transferDataHost returning a |
| 197 | // negative errno |
| 198 | length = maxSize + minSize; |
| 199 | EXPECT_CALL(dmaObj, transferDataHost(_, _, _, _, _)).WillOnce(Return(-1)); |
| 200 | response = transferAll<MockDMA>(&dmaObj, PLDM_READ_FILE_INTO_MEMORY, path, |
Jinu Joy Thomas | 33705fd | 2019-07-02 16:03:05 +0530 | [diff] [blame] | 201 | 0, length, 0, true, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 202 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 203 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR); |
| 204 | } |
| 205 | |
| 206 | TEST(ReadFileIntoMemory, BadPath) |
| 207 | { |
| 208 | uint32_t fileHandle = 0; |
| 209 | uint32_t offset = 0; |
| 210 | uint32_t length = 10; |
| 211 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 212 | uint8_t host_eid = 0; |
| 213 | int hostSocketFd = 0; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 214 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 215 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 216 | requestMsg{}; |
| 217 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 218 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 219 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 220 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 221 | sizeof(length)); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 222 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 223 | sizeof(length), |
| 224 | &address, sizeof(address)); |
| 225 | |
| 226 | // Pass invalid payload length |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 227 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 228 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 229 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 230 | auto response = handler.readFileIntoMemory(request, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 231 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 232 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 233 | } |
| 234 | |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 235 | TEST_F(TestFileTable, ReadFileInvalidFileHandle) |
| 236 | { |
| 237 | // Invalid file handle in the file table |
| 238 | uint32_t fileHandle = 2; |
| 239 | uint32_t offset = 0; |
| 240 | uint32_t length = 0; |
| 241 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 242 | uint8_t host_eid = 0; |
| 243 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 244 | |
| 245 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 246 | requestMsg{}; |
| 247 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 248 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 249 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 250 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 251 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 252 | sizeof(length)); |
| 253 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 254 | sizeof(length), |
| 255 | &address, sizeof(address)); |
| 256 | |
| 257 | using namespace pldm::filetable; |
| 258 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 259 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 260 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 261 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 262 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 263 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 264 | auto response = handler.readFileIntoMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 265 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 266 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 267 | // Clear the file table contents. |
| 268 | table.clear(); |
| 269 | } |
| 270 | |
| 271 | TEST_F(TestFileTable, ReadFileInvalidOffset) |
| 272 | { |
| 273 | uint32_t fileHandle = 0; |
| 274 | // The file size is 1024, so the offset is invalid |
| 275 | uint32_t offset = 1024; |
| 276 | uint32_t length = 0; |
| 277 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 278 | uint8_t host_eid = 0; |
| 279 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 280 | |
| 281 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 282 | requestMsg{}; |
| 283 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 284 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 285 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 286 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 287 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 288 | sizeof(length)); |
| 289 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 290 | sizeof(length), |
| 291 | &address, sizeof(address)); |
| 292 | |
| 293 | using namespace pldm::filetable; |
| 294 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 295 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 296 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 297 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 298 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 299 | auto response = handler.readFileIntoMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 300 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 301 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 302 | // Clear the file table contents. |
| 303 | table.clear(); |
| 304 | } |
| 305 | |
| 306 | TEST_F(TestFileTable, ReadFileInvalidLength) |
| 307 | { |
| 308 | uint32_t fileHandle = 0; |
| 309 | uint32_t offset = 100; |
| 310 | // Length should be a multiple of dma min size(16) |
| 311 | uint32_t length = 10; |
| 312 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 313 | uint8_t host_eid = 0; |
| 314 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 315 | |
| 316 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 317 | requestMsg{}; |
| 318 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 319 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 320 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 321 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 322 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 323 | sizeof(length)); |
| 324 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 325 | sizeof(length), |
| 326 | &address, sizeof(address)); |
| 327 | |
| 328 | using namespace pldm::filetable; |
| 329 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 330 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 331 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 332 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 333 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 334 | auto response = handler.readFileIntoMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 335 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 336 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 337 | // Clear the file table contents. |
| 338 | table.clear(); |
| 339 | } |
| 340 | |
| 341 | TEST_F(TestFileTable, ReadFileInvalidEffectiveLength) |
| 342 | { |
| 343 | uint32_t fileHandle = 0; |
| 344 | // valid offset |
| 345 | uint32_t offset = 100; |
| 346 | // length + offset exceeds the size, so effective length is |
| 347 | // filesize(1024) - offset(100). The effective length is not a multiple of |
| 348 | // DMA min size(16) |
| 349 | uint32_t length = 1024; |
| 350 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 351 | uint8_t host_eid = 0; |
| 352 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 353 | |
| 354 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 355 | requestMsg{}; |
| 356 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 357 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 358 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 359 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 360 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 361 | sizeof(length)); |
| 362 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 363 | sizeof(length), |
| 364 | &address, sizeof(address)); |
| 365 | |
| 366 | using namespace pldm::filetable; |
| 367 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 368 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 369 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 370 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 371 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 372 | auto response = handler.readFileIntoMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 373 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 374 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 375 | // Clear the file table contents. |
| 376 | table.clear(); |
| 377 | } |
| 378 | |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 379 | TEST(WriteFileFromMemory, BadPath) |
| 380 | { |
| 381 | uint32_t fileHandle = 0; |
| 382 | uint32_t offset = 0; |
| 383 | uint32_t length = 10; |
| 384 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 385 | uint8_t host_eid = 0; |
| 386 | int hostSocketFd = 0; |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 387 | |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 388 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 389 | requestMsg{}; |
| 390 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 391 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 392 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 393 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 394 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 395 | sizeof(length)); |
Jinu Joy Thomas | f666db1 | 2019-05-29 05:22:31 -0500 | [diff] [blame] | 396 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 397 | sizeof(length), |
| 398 | &address, sizeof(address)); |
| 399 | |
| 400 | // Pass invalid payload length |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 401 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 402 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 403 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 404 | auto response = handler.writeFileFromMemory(request, 0); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 405 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 406 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 407 | |
| 408 | // The length field is not a multiple of DMA minsize |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 409 | response = handler.writeFileFromMemory(request, requestPayloadLength); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 410 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 411 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
Jinu Joy Thomas | 7f57f44 | 2019-06-13 20:38:49 +0530 | [diff] [blame] | 412 | } |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 413 | |
| 414 | TEST_F(TestFileTable, WriteFileInvalidFileHandle) |
| 415 | { |
| 416 | // Invalid file handle in the file table |
| 417 | uint32_t fileHandle = 2; |
| 418 | uint32_t offset = 0; |
| 419 | uint32_t length = 16; |
| 420 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 421 | uint8_t host_eid = 0; |
| 422 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 423 | |
| 424 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 425 | requestMsg{}; |
| 426 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 427 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 428 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 429 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 430 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 431 | sizeof(length)); |
| 432 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 433 | sizeof(length), |
| 434 | &address, sizeof(address)); |
| 435 | |
| 436 | using namespace pldm::filetable; |
| 437 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 438 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 439 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 440 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 441 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 442 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 443 | auto response = handler.writeFileFromMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 444 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 445 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 446 | // Clear the file table contents. |
| 447 | table.clear(); |
| 448 | } |
| 449 | |
| 450 | TEST_F(TestFileTable, WriteFileInvalidOffset) |
| 451 | { |
| 452 | uint32_t fileHandle = 0; |
| 453 | // The file size is 1024, so the offset is invalid |
| 454 | uint32_t offset = 1024; |
| 455 | uint32_t length = 16; |
| 456 | uint64_t address = 0; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 457 | uint8_t host_eid = 0; |
| 458 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 459 | |
| 460 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES> |
| 461 | requestMsg{}; |
| 462 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 463 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 464 | memcpy(request->payload, &fileHandle, sizeof(fileHandle)); |
| 465 | memcpy(request->payload + sizeof(fileHandle), &offset, sizeof(offset)); |
| 466 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset), &length, |
| 467 | sizeof(length)); |
| 468 | memcpy(request->payload + sizeof(fileHandle) + sizeof(offset) + |
| 469 | sizeof(length), |
| 470 | &address, sizeof(address)); |
| 471 | |
| 472 | using namespace pldm::filetable; |
| 473 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 474 | auto& table = buildFileTable(TestFileTable::fileTableConfig.c_str()); |
| 475 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 476 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 477 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 478 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 479 | auto response = handler.writeFileFromMemory(request, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 480 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 481 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 482 | // Clear the file table contents. |
| 483 | table.clear(); |
| 484 | } |
| 485 | |
| 486 | TEST(FileTable, ConfigNotExist) |
| 487 | { |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 488 | FileTable tableObj(""); |
John Wang | 881cde1 | 2019-10-24 15:08:48 +0800 | [diff] [blame] | 489 | EXPECT_EQ(tableObj.isEmpty(), true); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | TEST_F(TestFileTable, ValidateFileEntry) |
| 493 | { |
| 494 | FileTable tableObj(fileTableConfig.c_str()); |
| 495 | |
| 496 | // Test file handle 0, the file size is 1K bytes. |
| 497 | auto value = tableObj.at(0); |
| 498 | ASSERT_EQ(value.handle, 0); |
| 499 | ASSERT_EQ(strcmp(value.fsPath.c_str(), imageFile.c_str()), 0); |
| 500 | ASSERT_EQ(static_cast<uint32_t>(fs::file_size(value.fsPath)), 1024); |
| 501 | ASSERT_EQ(value.traits.value, 1); |
| 502 | ASSERT_EQ(true, fs::exists(value.fsPath)); |
| 503 | |
| 504 | // Test file handle 1, the file size is 16 bytes |
| 505 | auto value1 = tableObj.at(1); |
| 506 | ASSERT_EQ(value1.handle, 1); |
| 507 | ASSERT_EQ(strcmp(value1.fsPath.c_str(), cksumFile.c_str()), 0); |
| 508 | ASSERT_EQ(static_cast<uint32_t>(fs::file_size(value1.fsPath)), 16); |
| 509 | ASSERT_EQ(value1.traits.value, 4); |
| 510 | ASSERT_EQ(true, fs::exists(value1.fsPath)); |
| 511 | |
| 512 | // Test invalid file handle |
| 513 | ASSERT_THROW(tableObj.at(2), std::out_of_range); |
| 514 | } |
| 515 | |
| 516 | TEST_F(TestFileTable, ValidateFileTable) |
| 517 | { |
| 518 | FileTable tableObj(fileTableConfig.c_str()); |
| 519 | |
| 520 | // Validate file attribute table |
| 521 | auto table = tableObj(); |
| 522 | ASSERT_EQ(true, |
| 523 | std::equal(attrTable.begin(), attrTable.end(), table.begin())); |
| 524 | } |
| 525 | |
| 526 | TEST_F(TestFileTable, GetFileTableCommand) |
| 527 | { |
| 528 | // Initialise the file table with a valid handle of 0 & 1 |
| 529 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 530 | |
| 531 | uint32_t transferHandle = 0; |
| 532 | uint8_t opFlag = 0; |
| 533 | uint8_t type = PLDM_FILE_ATTRIBUTE_TABLE; |
| 534 | uint32_t nextTransferHandle = 0; |
| 535 | uint8_t transferFlag = PLDM_START_AND_END; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 536 | uint8_t host_eid = 0; |
| 537 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 538 | |
| 539 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES> |
| 540 | requestMsg{}; |
| 541 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 542 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 543 | auto request = reinterpret_cast<pldm_get_file_table_req*>( |
| 544 | requestMsg.data() + sizeof(pldm_msg_hdr)); |
| 545 | request->transfer_handle = transferHandle; |
| 546 | request->operation_flag = opFlag; |
| 547 | request->table_type = type; |
| 548 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 549 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 550 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 551 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 552 | auto response = handler.getFileTable(requestMsgPtr, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 553 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 554 | ASSERT_EQ(responsePtr->payload[0], PLDM_SUCCESS); |
| 555 | size_t offsetSize = sizeof(responsePtr->payload[0]); |
| 556 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, &nextTransferHandle, |
| 557 | sizeof(nextTransferHandle))); |
| 558 | offsetSize += sizeof(nextTransferHandle); |
| 559 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, &transferFlag, |
| 560 | sizeof(transferFlag))); |
| 561 | offsetSize += sizeof(transferFlag); |
| 562 | ASSERT_EQ(0, memcmp(responsePtr->payload + offsetSize, attrTable.data(), |
| 563 | attrTable.size())); |
| 564 | table.clear(); |
| 565 | } |
| 566 | |
| 567 | TEST_F(TestFileTable, GetFileTableCommandReqLengthMismatch) |
| 568 | { |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 569 | uint8_t host_eid = 0; |
| 570 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 571 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES> |
| 572 | requestMsg{}; |
| 573 | auto request = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 574 | |
| 575 | // Pass invalid command payload length |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 576 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 577 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 578 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 579 | auto response = handler.getFileTable(request, 0); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 580 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 581 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 582 | } |
| 583 | |
| 584 | TEST_F(TestFileTable, GetFileTableCommandOEMAttrTable) |
| 585 | { |
| 586 | uint32_t transferHandle = 0; |
| 587 | uint8_t opFlag = 0; |
| 588 | uint8_t type = PLDM_OEM_FILE_ATTRIBUTE_TABLE; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 589 | uint8_t host_eid = 0; |
| 590 | int hostSocketFd = 0; |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 591 | |
| 592 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES> |
| 593 | requestMsg{}; |
| 594 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 595 | size_t requestPayloadLength = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 596 | auto request = reinterpret_cast<pldm_get_file_table_req*>( |
| 597 | requestMsg.data() + sizeof(pldm_msg_hdr)); |
| 598 | request->transfer_handle = transferHandle; |
| 599 | request->operation_flag = opFlag; |
| 600 | request->table_type = type; |
| 601 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 602 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 603 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 604 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 605 | auto response = handler.getFileTable(requestMsgPtr, requestPayloadLength); |
Tom Joseph | 0c6d22c | 2019-06-26 09:58:41 +0530 | [diff] [blame] | 606 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 607 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_TABLE_TYPE); |
| 608 | } |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 609 | |
| 610 | TEST_F(TestFileTable, ReadFileBadPath) |
| 611 | { |
| 612 | uint32_t fileHandle = 1; |
| 613 | uint32_t offset = 0; |
| 614 | uint32_t length = 0x4; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 615 | uint8_t host_eid = 0; |
| 616 | int hostSocketFd = 0; |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 617 | |
| 618 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES> |
| 619 | requestMsg{}; |
| 620 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 621 | auto payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 622 | auto request = reinterpret_cast<pldm_read_file_req*>(requestMsg.data() + |
| 623 | sizeof(pldm_msg_hdr)); |
| 624 | |
| 625 | request->file_handle = fileHandle; |
| 626 | request->offset = offset; |
| 627 | request->length = length; |
| 628 | |
| 629 | using namespace pldm::filetable; |
| 630 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 631 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 632 | |
| 633 | // Invalid payload length |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 634 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 635 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 636 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 637 | auto response = handler.readFile(requestMsgPtr, 0); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 638 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 639 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 640 | |
| 641 | // Data out of range. File size is 1024, offset = 1024 is invalid. |
| 642 | request->offset = 1024; |
| 643 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 644 | response = handler.readFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 645 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 646 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 647 | |
| 648 | // Invalid file handle |
| 649 | request->file_handle = 2; |
| 650 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 651 | response = handler.readFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 652 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 653 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 654 | |
| 655 | table.clear(); |
| 656 | } |
| 657 | |
| 658 | TEST_F(TestFileTable, ReadFileGoodPath) |
| 659 | { |
| 660 | uint32_t fileHandle = 0; |
| 661 | uint32_t offset = 0; |
| 662 | uint32_t length = 0x4; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 663 | uint8_t host_eid = 0; |
| 664 | int hostSocketFd = 0; |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 665 | |
| 666 | std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES> |
| 667 | requestMsg{}; |
| 668 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 669 | auto payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 670 | auto request = reinterpret_cast<pldm_read_file_req*>(requestMsg.data() + |
| 671 | sizeof(pldm_msg_hdr)); |
| 672 | |
| 673 | request->file_handle = fileHandle; |
| 674 | request->offset = offset; |
| 675 | request->length = length; |
| 676 | |
| 677 | using namespace pldm::filetable; |
| 678 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 679 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 680 | FileEntry value{}; |
| 681 | value = table.at(fileHandle); |
| 682 | |
| 683 | std::ifstream stream(value.fsPath, std::ios::in | std::ios::binary); |
| 684 | stream.seekg(offset); |
| 685 | std::vector<char> buffer(length); |
| 686 | stream.read(buffer.data(), length); |
| 687 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 688 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 689 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 690 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 691 | auto responseMsg = handler.readFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 692 | auto response = reinterpret_cast<pldm_read_file_resp*>( |
| 693 | responseMsg.data() + sizeof(pldm_msg_hdr)); |
| 694 | ASSERT_EQ(response->completion_code, PLDM_SUCCESS); |
| 695 | ASSERT_EQ(response->length, length); |
| 696 | ASSERT_EQ(0, memcmp(response->file_data, buffer.data(), length)); |
| 697 | |
| 698 | // Test condition offset + length > fileSize; |
| 699 | size_t fileSize = 1024; |
| 700 | request->offset = 1023; |
| 701 | request->length = 10; |
| 702 | |
| 703 | stream.seekg(request->offset); |
| 704 | buffer.resize(fileSize - request->offset); |
| 705 | stream.read(buffer.data(), (fileSize - request->offset)); |
| 706 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 707 | responseMsg = handler.readFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 708 | response = reinterpret_cast<pldm_read_file_resp*>(responseMsg.data() + |
| 709 | sizeof(pldm_msg_hdr)); |
| 710 | ASSERT_EQ(response->completion_code, PLDM_SUCCESS); |
| 711 | ASSERT_EQ(response->length, (fileSize - request->offset)); |
| 712 | ASSERT_EQ(0, memcmp(response->file_data, buffer.data(), |
| 713 | (fileSize - request->offset))); |
| 714 | |
| 715 | table.clear(); |
| 716 | } |
| 717 | |
| 718 | TEST_F(TestFileTable, WriteFileBadPath) |
| 719 | { |
| 720 | uint32_t fileHandle = 0; |
| 721 | uint32_t offset = 0; |
| 722 | uint32_t length = 0x10; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 723 | uint8_t host_eid = 0; |
| 724 | int hostSocketFd = 0; |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 725 | |
| 726 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 727 | PLDM_WRITE_FILE_REQ_BYTES + length); |
| 728 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 729 | auto payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 730 | auto request = reinterpret_cast<pldm_write_file_req*>(requestMsg.data() + |
| 731 | sizeof(pldm_msg_hdr)); |
| 732 | |
| 733 | using namespace pldm::filetable; |
| 734 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 735 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 736 | |
| 737 | request->file_handle = fileHandle; |
| 738 | request->offset = offset; |
| 739 | request->length = length; |
| 740 | |
| 741 | // Invalid payload length |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 742 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 743 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 744 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 745 | auto response = handler.writeFile(requestMsgPtr, 0); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 746 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 747 | ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR_INVALID_LENGTH); |
| 748 | |
| 749 | // Data out of range. File size is 1024, offset = 1024 is invalid. |
| 750 | request->offset = 1024; |
| 751 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 752 | response = handler.writeFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 753 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 754 | ASSERT_EQ(responsePtr->payload[0], PLDM_DATA_OUT_OF_RANGE); |
| 755 | |
| 756 | // Invalid file handle |
| 757 | request->file_handle = 2; |
| 758 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 759 | response = handler.writeFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 760 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 761 | ASSERT_EQ(responsePtr->payload[0], PLDM_INVALID_FILE_HANDLE); |
| 762 | |
| 763 | table.clear(); |
| 764 | } |
| 765 | |
| 766 | TEST_F(TestFileTable, WriteFileGoodPath) |
| 767 | { |
| 768 | uint32_t fileHandle = 1; |
| 769 | uint32_t offset = 0; |
| 770 | std::array<uint8_t, 4> fileData = {0x41, 0x42, 0x43, 0x44}; |
| 771 | uint32_t length = fileData.size(); |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 772 | uint8_t host_eid = 0; |
| 773 | int hostSocketFd = 0; |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 774 | |
| 775 | std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) + |
| 776 | PLDM_WRITE_FILE_REQ_BYTES + length); |
| 777 | auto requestMsgPtr = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 778 | auto payload_length = requestMsg.size() - sizeof(pldm_msg_hdr); |
| 779 | auto request = reinterpret_cast<pldm_write_file_req*>(requestMsg.data() + |
| 780 | sizeof(pldm_msg_hdr)); |
| 781 | |
| 782 | using namespace pldm::filetable; |
| 783 | // Initialise the file table with 2 valid file handles 0 & 1. |
| 784 | auto& table = buildFileTable(fileTableConfig.c_str()); |
| 785 | FileEntry value{}; |
| 786 | value = table.at(fileHandle); |
| 787 | |
| 788 | request->file_handle = fileHandle; |
| 789 | request->offset = offset; |
| 790 | request->length = length; |
| 791 | memcpy(request->file_data, fileData.data(), fileData.size()); |
| 792 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 793 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 794 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 795 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 796 | auto responseMsg = handler.writeFile(requestMsgPtr, payload_length); |
vkaverap | 5b914c3 | 2019-06-30 22:23:54 -0500 | [diff] [blame] | 797 | auto response = reinterpret_cast<pldm_read_file_resp*>( |
| 798 | responseMsg.data() + sizeof(pldm_msg_hdr)); |
| 799 | |
| 800 | std::ifstream stream(value.fsPath, std::ios::in | std::ios::binary); |
| 801 | stream.seekg(offset); |
| 802 | std::vector<char> buffer(length); |
| 803 | stream.read(buffer.data(), length); |
| 804 | |
| 805 | ASSERT_EQ(response->completion_code, PLDM_SUCCESS); |
| 806 | ASSERT_EQ(response->length, length); |
| 807 | ASSERT_EQ(0, memcmp(fileData.data(), buffer.data(), length)); |
| 808 | |
| 809 | table.clear(); |
| 810 | } |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 811 | |
| 812 | TEST(writeFileByTypeFromMemory, testBadPath) |
| 813 | { |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 814 | uint8_t host_eid = 0; |
| 815 | int hostSocketFd = 0; |
| 816 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 817 | const auto hdr_size = sizeof(pldm_msg_hdr); |
| 818 | std::array<uint8_t, hdr_size + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES> |
| 819 | requestMsg{}; |
| 820 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 821 | size_t requestPayloadLength = requestMsg.size() - hdr_size; |
| 822 | struct pldm_read_write_file_by_type_memory_req* request = |
| 823 | reinterpret_cast<struct pldm_read_write_file_by_type_memory_req*>( |
| 824 | req->payload); |
| 825 | request->file_type = PLDM_FILE_TYPE_PEL; |
| 826 | request->file_handle = 0xFFFFFFFF; |
| 827 | request->offset = 0; |
| 828 | request->length = 17; |
| 829 | request->address = 0; |
| 830 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 831 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 832 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 833 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 834 | auto response = handler.writeFileByTypeFromMemory(req, 0); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 835 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 836 | |
| 837 | struct pldm_read_write_file_by_type_memory_resp* resp = |
| 838 | reinterpret_cast<struct pldm_read_write_file_by_type_memory_resp*>( |
| 839 | responsePtr->payload); |
| 840 | ASSERT_EQ(PLDM_ERROR_INVALID_LENGTH, resp->completion_code); |
| 841 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 842 | response = handler.writeFileByTypeFromMemory(req, requestPayloadLength); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 843 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 844 | |
| 845 | resp = reinterpret_cast<struct pldm_read_write_file_by_type_memory_resp*>( |
| 846 | responsePtr->payload); |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 847 | ASSERT_EQ(PLDM_ERROR_INVALID_LENGTH, resp->completion_code); |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | TEST(getHandlerByType, allPaths) |
| 851 | { |
| 852 | uint32_t fileHandle{}; |
| 853 | auto handler = getHandlerByType(PLDM_FILE_TYPE_PEL, fileHandle); |
| 854 | auto pelType = dynamic_cast<PelHandler*>(handler.get()); |
| 855 | ASSERT_TRUE(pelType != nullptr); |
| 856 | |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 857 | handler = getHandlerByType(PLDM_FILE_TYPE_LID_PERM, fileHandle); |
| 858 | auto lidType = dynamic_cast<LidHandler*>(handler.get()); |
| 859 | ASSERT_TRUE(lidType != nullptr); |
| 860 | pelType = dynamic_cast<PelHandler*>(handler.get()); |
| 861 | ASSERT_TRUE(pelType == nullptr); |
| 862 | handler = getHandlerByType(PLDM_FILE_TYPE_LID_TEMP, fileHandle); |
| 863 | lidType = dynamic_cast<LidHandler*>(handler.get()); |
| 864 | ASSERT_TRUE(lidType != nullptr); |
| 865 | |
Sampa Misra | 1896716 | 2020-01-14 02:31:41 -0600 | [diff] [blame] | 866 | handler = getHandlerByType(PLDM_FILE_TYPE_DUMP, fileHandle); |
| 867 | auto dumpType = dynamic_cast<DumpHandler*>(handler.get()); |
| 868 | ASSERT_TRUE(dumpType != nullptr); |
| 869 | |
Jayashankar Padath | db12436 | 2021-01-28 21:12:34 -0600 | [diff] [blame] | 870 | handler = getHandlerByType(PLDM_FILE_TYPE_RESOURCE_DUMP_PARMS, fileHandle); |
| 871 | dumpType = dynamic_cast<DumpHandler*>(handler.get()); |
| 872 | ASSERT_TRUE(dumpType != nullptr); |
| 873 | |
| 874 | handler = getHandlerByType(PLDM_FILE_TYPE_RESOURCE_DUMP, fileHandle); |
| 875 | dumpType = dynamic_cast<DumpHandler*>(handler.get()); |
| 876 | ASSERT_TRUE(dumpType != nullptr); |
| 877 | |
Sampa Misra | d823cc0 | 2020-03-24 04:53:20 -0500 | [diff] [blame] | 878 | handler = getHandlerByType(PLDM_FILE_TYPE_CERT_SIGNING_REQUEST, fileHandle); |
| 879 | auto certType = dynamic_cast<CertHandler*>(handler.get()); |
| 880 | ASSERT_TRUE(certType != nullptr); |
| 881 | |
| 882 | handler = getHandlerByType(PLDM_FILE_TYPE_SIGNED_CERT, fileHandle); |
| 883 | certType = dynamic_cast<CertHandler*>(handler.get()); |
| 884 | ASSERT_TRUE(certType != nullptr); |
| 885 | |
| 886 | handler = getHandlerByType(PLDM_FILE_TYPE_ROOT_CERT, fileHandle); |
| 887 | certType = dynamic_cast<CertHandler*>(handler.get()); |
| 888 | ASSERT_TRUE(certType != nullptr); |
| 889 | |
Sampa Misra | 854e61f | 2019-08-22 04:36:47 -0500 | [diff] [blame] | 890 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 891 | ASSERT_THROW(getHandlerByType(0xFFFF, fileHandle), InternalFailure); |
| 892 | } |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 893 | |
| 894 | TEST(readFileByTypeIntoMemory, testBadPath) |
| 895 | { |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 896 | uint8_t host_eid = 0; |
| 897 | int hostSocketFd = 0; |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 898 | const auto hdr_size = sizeof(pldm_msg_hdr); |
| 899 | std::array<uint8_t, hdr_size + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES> |
| 900 | requestMsg{}; |
| 901 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 902 | struct pldm_read_write_file_by_type_memory_req* request = |
| 903 | reinterpret_cast<struct pldm_read_write_file_by_type_memory_req*>( |
| 904 | req->payload); |
| 905 | request->file_type = 0xFFFF; |
| 906 | request->file_handle = 0; |
| 907 | request->offset = 0; |
| 908 | request->length = 17; |
| 909 | request->address = 0; |
| 910 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 911 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 912 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 913 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 914 | auto response = handler.readFileByTypeIntoMemory(req, 0); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 915 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 916 | struct pldm_read_write_file_by_type_memory_resp* resp = |
| 917 | reinterpret_cast<struct pldm_read_write_file_by_type_memory_resp*>( |
| 918 | responsePtr->payload); |
| 919 | ASSERT_EQ(PLDM_ERROR_INVALID_LENGTH, resp->completion_code); |
| 920 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 921 | response = handler.readFileByTypeIntoMemory( |
| 922 | req, PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 923 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 924 | resp = reinterpret_cast<struct pldm_read_write_file_by_type_memory_resp*>( |
| 925 | responsePtr->payload); |
Deepak Kodihalli | 3bf5c55 | 2020-04-20 06:16:01 -0500 | [diff] [blame] | 926 | ASSERT_EQ(PLDM_ERROR_INVALID_LENGTH, resp->completion_code); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 927 | |
| 928 | request->length = 16; |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 929 | response = handler.readFileByTypeIntoMemory( |
| 930 | req, PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 931 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 932 | resp = reinterpret_cast<struct pldm_read_write_file_by_type_memory_resp*>( |
| 933 | responsePtr->payload); |
| 934 | ASSERT_EQ(PLDM_INVALID_FILE_TYPE, resp->completion_code); |
| 935 | } |
| 936 | |
| 937 | TEST(readFileByType, testBadPath) |
| 938 | { |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 939 | uint8_t host_eid = 0; |
| 940 | int hostSocketFd = 0; |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 941 | const auto hdr_size = sizeof(pldm_msg_hdr); |
| 942 | std::array<uint8_t, hdr_size + PLDM_RW_FILE_BY_TYPE_REQ_BYTES> requestMsg{}; |
| 943 | auto payloadLength = requestMsg.size() - hdr_size; |
| 944 | auto req = reinterpret_cast<pldm_msg*>(requestMsg.data()); |
| 945 | struct pldm_read_write_file_by_type_req* request = |
| 946 | reinterpret_cast<struct pldm_read_write_file_by_type_req*>( |
| 947 | req->payload); |
| 948 | request->file_type = 0xFFFF; |
| 949 | request->file_handle = 0; |
| 950 | request->offset = 0; |
| 951 | request->length = 13; |
| 952 | |
Sampa Misra | aea5dde | 2020-08-31 08:33:47 -0500 | [diff] [blame] | 953 | std::unique_ptr<oem_platform::Handler> oemPlatformHandler{}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 954 | oem_ibm::Handler handler(oemPlatformHandler.get(), hostSocketFd, host_eid, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 955 | nullptr, nullptr); |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 956 | auto response = handler.readFileByType(req, 0); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 957 | auto responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 958 | struct pldm_read_write_file_by_type_resp* resp = |
| 959 | reinterpret_cast<struct pldm_read_write_file_by_type_resp*>( |
| 960 | responsePtr->payload); |
| 961 | ASSERT_EQ(PLDM_ERROR_INVALID_LENGTH, resp->completion_code); |
| 962 | |
Deepak Kodihalli | bc669f1 | 2019-11-28 08:52:07 -0600 | [diff] [blame] | 963 | response = handler.readFileByType(req, payloadLength); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 964 | responsePtr = reinterpret_cast<pldm_msg*>(response.data()); |
| 965 | resp = reinterpret_cast<struct pldm_read_write_file_by_type_resp*>( |
| 966 | responsePtr->payload); |
| 967 | ASSERT_EQ(PLDM_INVALID_FILE_TYPE, resp->completion_code); |
| 968 | } |
| 969 | |
| 970 | TEST(readFileByType, testReadFile) |
| 971 | { |
| 972 | LidHandler handler(0, true); |
| 973 | Response response; |
| 974 | uint32_t length{}; |
| 975 | |
| 976 | auto rc = handler.readFile({}, 0, length, response); |
| 977 | ASSERT_EQ(PLDM_INVALID_FILE_HANDLE, rc); |
| 978 | |
| 979 | char tmplt[] = "/tmp/lid.XXXXXX"; |
| 980 | auto fd = mkstemp(tmplt); |
| 981 | std::vector<uint8_t> in = {100, 10, 56, 78, 34, 56, 79, 235, 111}; |
Manojkiran Eda | 6b1d883 | 2021-03-27 11:25:06 +0530 | [diff] [blame] | 982 | rc = write(fd, in.data(), in.size()); |
Deepak Kodihalli | 75e02f8 | 2019-11-20 02:51:05 -0600 | [diff] [blame] | 983 | close(fd); |
| 984 | length = in.size() + 1000; |
| 985 | rc = handler.readFile(tmplt, 0, length, response); |
| 986 | ASSERT_EQ(rc, PLDM_SUCCESS); |
| 987 | ASSERT_EQ(length, in.size()); |
| 988 | ASSERT_EQ(response.size(), in.size()); |
| 989 | ASSERT_EQ(std::equal(in.begin(), in.end(), response.begin()), true); |
| 990 | } |