blob: de766b1b4f429b8bc22139c50e9ed15328547139 [file] [log] [blame]
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +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
Zahed Hossain223a73d2019-07-04 12:46:18 -050010constexpr auto hdrSize = sizeof(pldm_msg_hdr);
11
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053012TEST(ReadWriteFileMemory, testGoodDecodeRequest)
13{
Zahed Hossain223a73d2019-07-04 12:46:18 -050014 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES + hdrSize> requestMsg{};
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053015
16 // Random value for fileHandle, offset, length, address
17 uint32_t fileHandle = 0x12345678;
18 uint32_t offset = 0x87654321;
19 uint32_t length = 0x13245768;
20 uint64_t address = 0x124356879ACBDE0F;
21
Zahed Hossain223a73d2019-07-04 12:46:18 -050022 memcpy(requestMsg.data() + hdrSize, &fileHandle, sizeof(fileHandle));
23 memcpy(requestMsg.data() + sizeof(fileHandle) + hdrSize, &offset,
24 sizeof(offset));
25 memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) + hdrSize,
26 &length, sizeof(length));
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053027 memcpy(requestMsg.data() + sizeof(fileHandle) + sizeof(offset) +
Zahed Hossain223a73d2019-07-04 12:46:18 -050028 sizeof(length) + hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053029 &address, sizeof(address));
30
31 uint32_t retFileHandle = 0;
32 uint32_t retOffset = 0;
33 uint32_t retLength = 0;
34 uint64_t retAddress = 0;
35
Zahed Hossain223a73d2019-07-04 12:46:18 -050036 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
37
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053038 // Invoke decode the read file memory request
Zahed Hossain223a73d2019-07-04 12:46:18 -050039 auto rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053040 &retFileHandle, &retOffset, &retLength,
41 &retAddress);
42
43 ASSERT_EQ(rc, PLDM_SUCCESS);
44 ASSERT_EQ(fileHandle, retFileHandle);
45 ASSERT_EQ(offset, retOffset);
46 ASSERT_EQ(length, retLength);
47 ASSERT_EQ(address, retAddress);
48}
49
50TEST(ReadWriteFileMemory, testBadDecodeRequest)
51{
52 uint32_t fileHandle = 0;
53 uint32_t offset = 0;
54 uint32_t length = 0;
55 uint64_t address = 0;
56
57 // Request payload message is missing
58 auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length,
59 &address);
60 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
61
62 std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
63
Zahed Hossain223a73d2019-07-04 12:46:18 -050064 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
65
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053066 // Address is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -050067 rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053068 &fileHandle, &offset, &length, NULL);
69 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
70
71 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -050072 rc = decode_rw_file_memory_req(request, 0, &fileHandle, &offset, &length,
73 &address);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053074 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
75}
76
77TEST(ReadWriteFileMemory, testGoodEncodeResponse)
78{
79 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
80 responseMsg{};
81 uint32_t length = 0xFF00EE11;
82 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
83
84 // ReadFileIntoMemory
85 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
86 PLDM_SUCCESS, length, response);
87
88 ASSERT_EQ(rc, PLDM_SUCCESS);
89 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
90 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -050091 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +053092 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
93 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
94 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
95 &length, sizeof(length)));
96
97 // WriteFileFromMemory
98 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
99 PLDM_SUCCESS, length, response);
100
101 ASSERT_EQ(rc, PLDM_SUCCESS);
102 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
103 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500104 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530105 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
106 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
107 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
108 &length, sizeof(length)));
109}
110
111TEST(ReadWriteFileMemory, testBadEncodeResponse)
112{
113 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
114 responseMsg{};
115 uint32_t length = 0;
116 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
117
118 // ReadFileIntoMemory
119 auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
120 PLDM_ERROR, length, response);
121
122 ASSERT_EQ(rc, PLDM_SUCCESS);
123 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
124 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500125 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530126 ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
127 ASSERT_EQ(response->payload[0], PLDM_ERROR);
128
129 // WriteFileFromMemory
130 rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR,
131 length, response);
132
133 ASSERT_EQ(rc, PLDM_SUCCESS);
134 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
135 ASSERT_EQ(response->hdr.instance_id, 0);
Jinu Joy Thomasf666db12019-05-29 05:22:31 -0500136 ASSERT_EQ(response->hdr.type, PLDM_OEM);
Jinu Joy Thomas7f57f442019-06-13 20:38:49 +0530137 ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
138 ASSERT_EQ(response->payload[0], PLDM_ERROR);
139}
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530140
Priyanga8b976652019-06-27 11:30:33 -0500141TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse)
142{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500143 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES + hdrSize> responseMsg{};
Priyanga8b976652019-06-27 11:30:33 -0500144 // Random value for length
145 uint32_t length = 0xFF00EE12;
146 uint8_t completionCode = 0;
147
Zahed Hossain223a73d2019-07-04 12:46:18 -0500148 memcpy(responseMsg.data() + hdrSize, &completionCode,
149 sizeof(completionCode));
150 memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &length,
Priyanga8b976652019-06-27 11:30:33 -0500151 sizeof(length));
152
153 uint8_t retCompletionCode = 0;
154 uint32_t retLength = 0;
155
Zahed Hossain223a73d2019-07-04 12:46:18 -0500156 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
157
Priyanga8b976652019-06-27 11:30:33 -0500158 // Invoke decode the read file memory response
Zahed Hossain223a73d2019-07-04 12:46:18 -0500159 auto rc = decode_rw_file_memory_resp(response, responseMsg.size() - hdrSize,
Priyanga8b976652019-06-27 11:30:33 -0500160 &retCompletionCode, &retLength);
161 ASSERT_EQ(rc, PLDM_SUCCESS);
162 ASSERT_EQ(completionCode, retCompletionCode);
163 ASSERT_EQ(length, retLength);
164}
165
166TEST(ReadWriteFileIntoMemory, testBadDecodeResponse)
167{
168 uint32_t length = 0;
169 uint8_t completionCode = 0;
170
171 // Request payload message is missing
172 auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length);
173 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
174
175 std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
176
Zahed Hossain223a73d2019-07-04 12:46:18 -0500177 auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
178
Priyanga8b976652019-06-27 11:30:33 -0500179 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500180 rc = decode_rw_file_memory_resp(response, 0, &completionCode, &length);
Priyanga8b976652019-06-27 11:30:33 -0500181 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
182}
183
184TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest)
185{
186 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES>
187 requestMsg{};
188
189 uint32_t fileHandle = 0x12345678;
190 uint32_t offset = 0x87654321;
191 uint32_t length = 0x13245768;
192 uint64_t address = 0x124356879ACBDE0F;
193
194 pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
195
196 auto rc =
197 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
198 offset, length, address, request);
199
200 ASSERT_EQ(rc, PLDM_SUCCESS);
201 ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
202 ASSERT_EQ(request->hdr.instance_id, 0);
203 ASSERT_EQ(request->hdr.type, PLDM_OEM);
204 ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
205
206 ASSERT_EQ(0, memcmp(request->payload, &fileHandle, sizeof(fileHandle)));
207
208 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle), &offset,
209 sizeof(offset)));
210 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset),
211 &length, sizeof(length)));
212 ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandle) + sizeof(offset) +
213 sizeof(length),
214 &address, sizeof(address)));
215}
216
217TEST(ReadWriteFileIntoMemory, testBadEncodeRequest)
218{
219 uint32_t fileHandle = 0;
220 uint32_t offset = 0;
221 uint32_t length = 0;
222 uint64_t address = 0;
223
224 auto rc =
225 encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
226 offset, length, address, NULL);
227
228 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
229}
230
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530231TEST(GetFileTable, GoodDecodeRequest)
232{
Zahed Hossain223a73d2019-07-04 12:46:18 -0500233 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES + hdrSize> requestMsg{};
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530234
235 // Random value for DataTransferHandle, TransferOperationFlag, TableType
236 uint32_t transferHandle = 0x12345678;
237 uint8_t transferOpFlag = 1;
238 uint8_t tableType = 1;
239
Zahed Hossain223a73d2019-07-04 12:46:18 -0500240 memcpy(requestMsg.data() + hdrSize, &transferHandle,
241 sizeof(transferHandle));
242 memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize,
243 &transferOpFlag, sizeof(transferOpFlag));
244 memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag) +
245 hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530246 &tableType, sizeof(tableType));
247
248 uint32_t retTransferHandle = 0;
249 uint8_t retTransferOpFlag = 0;
250 uint8_t retTableType = 0;
251
Zahed Hossain223a73d2019-07-04 12:46:18 -0500252 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
253
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530254 // Invoke decode get file table request
Zahed Hossain223a73d2019-07-04 12:46:18 -0500255 auto rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530256 &retTransferHandle, &retTransferOpFlag,
257 &retTableType);
258
259 ASSERT_EQ(rc, PLDM_SUCCESS);
260 ASSERT_EQ(transferHandle, retTransferHandle);
261 ASSERT_EQ(transferOpFlag, retTransferOpFlag);
262 ASSERT_EQ(tableType, retTableType);
263}
264
265TEST(GetFileTable, BadDecodeRequest)
266{
267 uint32_t transferHandle = 0;
268 uint8_t transferOpFlag = 0;
269 uint8_t tableType = 0;
270
271 // Request payload message is missing
272 auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle,
273 &transferOpFlag, &tableType);
274 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
275
276 std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
277
Zahed Hossain223a73d2019-07-04 12:46:18 -0500278 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
279
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530280 // TableType is NULL
Zahed Hossain223a73d2019-07-04 12:46:18 -0500281 rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530282 &transferHandle, &transferOpFlag, nullptr);
283 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
284
285 // Payload length is invalid
Zahed Hossain223a73d2019-07-04 12:46:18 -0500286 rc = decode_get_file_table_req(request, 0, &transferHandle, &transferOpFlag,
287 &tableType);
Tom Joseph0c6d22c2019-06-26 09:58:41 +0530288 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
289}
290
291TEST(GetFileTable, GoodEncodeResponse)
292{
293 // Random value for NextDataTransferHandle and TransferFlag
294 uint8_t completionCode = 0;
295 uint32_t nextTransferHandle = 0x87654321;
296 uint8_t transferFlag = 5;
297 // Mock file table contents of size 5
298 std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5};
299 constexpr size_t responseSize = sizeof(completionCode) +
300 sizeof(nextTransferHandle) +
301 sizeof(transferFlag) + fileTable.size();
302
303 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
304 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
305
306 // GetFileTable
307 auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle,
308 transferFlag, fileTable.data(),
309 fileTable.size(), response);
310
311 ASSERT_EQ(rc, PLDM_SUCCESS);
312 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
313 ASSERT_EQ(response->hdr.instance_id, 0);
314 ASSERT_EQ(response->hdr.type, PLDM_OEM);
315 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
316 ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
317 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
318 &nextTransferHandle, sizeof(nextTransferHandle)));
319 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
320 sizeof(nextTransferHandle),
321 &transferFlag, sizeof(transferFlag)));
322 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
323 sizeof(nextTransferHandle),
324 &transferFlag, sizeof(transferFlag)));
325 ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
326 sizeof(nextTransferHandle) + sizeof(transferFlag),
327 fileTable.data(), fileTable.size()));
328}
329
330TEST(GetFileTable, BadEncodeResponse)
331{
332 uint8_t completionCode = 0;
333 uint32_t nextTransferHandle = 0;
334 uint8_t transferFlag = 0;
335 constexpr size_t responseSize = sizeof(completionCode) +
336 sizeof(nextTransferHandle) +
337 sizeof(transferFlag);
338
339 std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
340 pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
341
342 // GetFileTable
343 auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle,
344 transferFlag, nullptr, 0, response);
345
346 ASSERT_EQ(rc, PLDM_SUCCESS);
347 ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
348 ASSERT_EQ(response->hdr.instance_id, 0);
349 ASSERT_EQ(response->hdr.type, PLDM_OEM);
350 ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
351 ASSERT_EQ(response->payload[0], PLDM_ERROR);
352}