blob: 2a5dce1f6943f3e466de85ad64c49586cf200e19 [file] [log] [blame]
Tom Joseph7dae7772019-04-10 14:44:44 +05301#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 James3b02e272019-04-22 20:13:55 +000010TEST(ReadWriteFileMemory, testGoodDecodeRequest)
Tom Joseph7dae7772019-04-10 14:44:44 +053011{
Eddie James3b02e272019-04-22 20:13:55 +000012 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
Tom Joseph7dae7772019-04-10 14:44:44 +053013
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 James3b02e272019-04-22 20:13:55 +000034 auto rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(),
35 &retFileHandle, &retOffset, &retLength,
36 &retAddress);
Tom Joseph7dae7772019-04-10 14:44:44 +053037
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 James3b02e272019-04-22 20:13:55 +000045TEST(ReadWriteFileMemory, testBadDecodeRequest)
Tom Joseph7dae7772019-04-10 14:44:44 +053046{
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 James3b02e272019-04-22 20:13:55 +000053 auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length,
54 &address);
Tom Joseph7dae7772019-04-10 14:44:44 +053055 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
56
Eddie James3b02e272019-04-22 20:13:55 +000057 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
Tom Joseph7dae7772019-04-10 14:44:44 +053058
59 // Address is NULL
Eddie James3b02e272019-04-22 20:13:55 +000060 rc = decode_rw_file_memory_req(requestMsg.data(), requestMsg.size(),
61 &fileHandle, &offset, &length, NULL);
Tom Joseph7dae7772019-04-10 14:44:44 +053062 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
63
64 // Payload length is invalid
Eddie James3b02e272019-04-22 20:13:55 +000065 rc = decode_rw_file_memory_req(requestMsg.data(), 0, &fileHandle, &offset,
66 &length, &address);
Tom Joseph7dae7772019-04-10 14:44:44 +053067 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
68}
69
Eddie James3b02e272019-04-22 20:13:55 +000070TEST(ReadWriteFileMemory, testGoodEncodeResponse)
Tom Joseph7dae7772019-04-10 14:44:44 +053071{
Eddie James3b02e272019-04-22 20:13:55 +000072 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
Tom Joseph7dae7772019-04-10 14:44:44 +053073 responseMsg{};
74 uint32_t length = 0xFF00EE11;
75 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
76
Eddie James3b02e272019-04-22 20:13:55 +000077 // ReadFileIntoMemory
78 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
79 PLDM_SUCCESS, length, response);
Tom Joseph7dae7772019-04-10 14:44:44 +053080
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 James3b02e272019-04-22 20:13:55 +000089
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 Joseph7dae7772019-04-10 14:44:44 +0530102}
103
Eddie James3b02e272019-04-22 20:13:55 +0000104TEST(ReadWriteFileMemory, testBadEncodeResponse)
Tom Joseph7dae7772019-04-10 14:44:44 +0530105{
Eddie James3b02e272019-04-22 20:13:55 +0000106 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
Tom Joseph7dae7772019-04-10 14:44:44 +0530107 responseMsg{};
108 uint32_t length = 0;
109 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
110
Eddie James3b02e272019-04-22 20:13:55 +0000111 // ReadFileIntoMemory
112 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
113 PLDM_ERROR, length, response);
Tom Joseph7dae7772019-04-10 14:44:44 +0530114
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 James3b02e272019-04-22 20:13:55 +0000121
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 Joseph7dae7772019-04-10 14:44:44 +0530132}