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 | } |