blob: 665ceb10b588049af62eb9c741c09e041bfe1c90 [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}
vkaverap2ffe3292019-06-24 00:08:13 -0500353
354TEST(ReadFile, testGoodDecodeRequest)
355{
356 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
357 requestMsg{};
358
359 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
360 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
361 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
362
363 // Random value for fileHandle, offset and length
364 uint32_t fileHandle = 0x12345678;
365 uint32_t offset = 0x87654321;
366 uint32_t length = 0x13245768;
367
368 request->file_handle = fileHandle;
369 request->offset = offset;
370 request->length = length;
371
372 uint32_t retFileHandle = 0;
373 uint32_t retOffset = 0;
374 uint32_t retLength = 0;
375
376 // Invoke decode the read file request
377 auto rc = decode_read_file_req(requestPtr, payload_length, &retFileHandle,
378 &retOffset, &retLength);
379
380 ASSERT_EQ(rc, PLDM_SUCCESS);
381 ASSERT_EQ(fileHandle, retFileHandle);
382 ASSERT_EQ(offset, retOffset);
383 ASSERT_EQ(length, retLength);
384}
385
386TEST(WriteFile, testGoodDecodeRequest)
387{
388 // Random value for fileHandle, offset, length and file data
389 uint32_t fileHandle = 0x12345678;
390 uint32_t offset = 0x87654321;
391 uint32_t length = 0x467;
392
393 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
394 sizeof(pldm_msg_hdr) + length);
395 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
396 size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
397 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
398
399 size_t fileDataOffset =
400 sizeof(fileHandle) + sizeof(offset) + sizeof(length);
401
402 request->file_handle = fileHandle;
403 request->offset = offset;
404 request->length = length;
405
406 uint32_t retFileHandle = 0;
407 uint32_t retOffset = 0;
408 uint32_t retLength = 0;
409 size_t retFileDataOffset = 0;
410
411 // Invoke decode the write file request
412 auto rc = decode_write_file_req(requestPtr, payload_length, &retFileHandle,
413 &retOffset, &retLength, &retFileDataOffset);
414
415 ASSERT_EQ(rc, PLDM_SUCCESS);
416 ASSERT_EQ(fileHandle, retFileHandle);
417 ASSERT_EQ(offset, retOffset);
418 ASSERT_EQ(length, retLength);
419 ASSERT_EQ(fileDataOffset, retFileDataOffset);
420}
421
422TEST(ReadFile, testGoodDecodeResponse)
423{
424 // Random value for length
425 uint32_t length = 0x10;
426 uint8_t completionCode = PLDM_SUCCESS;
427
428 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
429 sizeof(pldm_msg_hdr) + length);
430 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
431 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
432 auto response =
433 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
434
435 response->completion_code = completionCode;
436 response->length = length;
437
438 size_t fileDataOffset = sizeof(completionCode) + sizeof(length);
439
440 uint32_t retLength = 0;
441 uint8_t retCompletionCode = 0;
442 size_t retFileDataOffset = 0;
443
444 // Invoke decode the read file response
445 auto rc =
446 decode_read_file_resp(responsePtr, payload_length, &retCompletionCode,
447 &retLength, &retFileDataOffset);
448
449 ASSERT_EQ(rc, PLDM_SUCCESS);
450 ASSERT_EQ(completionCode, retCompletionCode);
451 ASSERT_EQ(length, retLength);
452 ASSERT_EQ(fileDataOffset, retFileDataOffset);
453}
454
455TEST(WriteFile, testGoodDecodeResponse)
456{
457 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
458 responseMsg{};
459 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
460 size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
461 auto response =
462 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
463
464 uint8_t completionCode = PLDM_SUCCESS;
465 uint32_t length = 0x4678;
466
467 response->completion_code = completionCode;
468 response->length = length;
469
470 uint32_t retLength = 0;
471 uint8_t retCompletionCode = 0;
472
473 // Invoke decode the write file response
474 auto rc = decode_write_file_resp(responsePtr, payload_length,
475 &retCompletionCode, &retLength);
476
477 ASSERT_EQ(rc, PLDM_SUCCESS);
478 ASSERT_EQ(completionCode, retCompletionCode);
479 ASSERT_EQ(length, retLength);
480}
481
482TEST(ReadWriteFile, testBadDecodeResponse)
483{
484 uint32_t length = 0;
485 uint8_t completionCode = 0;
486 size_t fileDataOffset = 0;
487
488 // Bad decode response for read file
489 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
490 sizeof(pldm_msg_hdr) + length);
491 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
492
493 // Request payload message is missing
494 auto rc = decode_read_file_resp(NULL, 0, &completionCode, &length,
495 &fileDataOffset);
496 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
497
498 // Payload length is invalid
499 rc = decode_read_file_resp(responsePtr, 0, &completionCode, &length,
500 &fileDataOffset);
501 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
502
503 // Bad decode response for write file
504 std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
505 responseMsgWr{};
506 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
507
508 // Request payload message is missing
509 rc = decode_write_file_resp(NULL, 0, &completionCode, &length);
510 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
511
512 // Payload length is invalid
513 rc = decode_write_file_resp(responseWr, 0, &completionCode, &length);
514 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
515}
516
517TEST(ReadWriteFile, testBadDecodeRequest)
518{
519 uint32_t fileHandle = 0;
520 uint32_t offset = 0;
521 uint32_t length = 0;
522
523 // Bad decode request for read file
524 std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
525 requestMsg{};
526 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
527
528 // Request payload message is missing
529 auto rc = decode_read_file_req(NULL, 0, &fileHandle, &offset, &length);
530 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
531
532 // Payload length is invalid
533 rc = decode_read_file_req(requestPtr, 0, &fileHandle, &offset, &length);
534 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
535
536 // Bad decode request for write file
537 size_t fileDataOffset = 0;
538 std::array<uint8_t, PLDM_WRITE_FILE_REQ_BYTES> requestMsgWr{};
539 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
540
541 // Request payload message is missing
542 rc = decode_write_file_req(NULL, 0, &fileHandle, &offset, &length,
543 &fileDataOffset);
544 ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
545
546 // Payload length is invalid
547 rc = decode_write_file_req(requestWr, 0, &fileHandle, &offset, &length,
548 &fileDataOffset);
549 ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
550}
551
552TEST(ReadFile, testGoodEncodeResponse)
553{
554 // Good encode response for read file
555 uint32_t length = 0x4;
556
557 std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
558 sizeof(pldm_msg_hdr) + length);
559 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
560 auto response =
561 reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
562
563 // ReadFile
564 auto rc = encode_read_file_resp(0, PLDM_SUCCESS, length, responsePtr);
565
566 ASSERT_EQ(rc, PLDM_SUCCESS);
567 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
568 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
569 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
570 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
571 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
572 ASSERT_EQ(response->length, length);
573}
574
575TEST(WriteFile, testGoodEncodeResponse)
576{
577 uint32_t length = 0x467;
578
579 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
580 responseMsg{};
581
582 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
583 auto response =
584 reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
585
586 // WriteFile
587 auto rc = encode_write_file_resp(0, PLDM_SUCCESS, length, responsePtr);
588 ASSERT_EQ(rc, PLDM_SUCCESS);
589 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
590 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
591 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
592 ASSERT_EQ(responsePtr->hdr.command, PLDM_WRITE_FILE);
593 ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
594 ASSERT_EQ(response->length, length);
595}
596
597TEST(ReadFile, testGoodEncodeRequest)
598{
599 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
600 requestMsg{};
601
602 uint32_t fileHandle = 0x12345678;
603 uint32_t offset = 0x87654321;
604 uint32_t length = 0x13245768;
605 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
606 auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
607
608 // ReadFile
609 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
610
611 ASSERT_EQ(rc, PLDM_SUCCESS);
612 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
613 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
614 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
615 ASSERT_EQ(requestPtr->hdr.command, PLDM_READ_FILE);
616 ASSERT_EQ(request->file_handle, fileHandle);
617 ASSERT_EQ(request->offset, offset);
618 ASSERT_EQ(request->length, length);
619}
620
621TEST(WriteFile, testGoodEncodeRequest)
622{
623 uint32_t fileHandle = 0x12345678;
624 uint32_t offset = 0x87654321;
625 uint32_t length = 0x456;
626
627 std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
628 sizeof(pldm_msg_hdr) + length);
629 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
630 auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
631
632 // WriteFile
633 auto rc = encode_write_file_req(0, fileHandle, offset, length, requestPtr);
634
635 ASSERT_EQ(rc, PLDM_SUCCESS);
636 ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
637 ASSERT_EQ(requestPtr->hdr.instance_id, 0);
638 ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
639 ASSERT_EQ(requestPtr->hdr.command, PLDM_WRITE_FILE);
640 ASSERT_EQ(request->file_handle, fileHandle);
641 ASSERT_EQ(request->offset, offset);
642 ASSERT_EQ(request->length, length);
643}
644
645TEST(ReadWriteFile, testBadEncodeRequest)
646{
647 // Bad encode request for read file
648 uint32_t fileHandle = 0;
649 uint32_t offset = 0;
650 uint32_t length = 0;
651
652 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
653 requestMsg{};
654 auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
655
656 // ReadFile check invalid file length
657 auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
658
659 ASSERT_EQ(rc, PLDM_INVALID_READ_LENGTH);
660
661 // Bad encode request for write file
662 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_REQ_BYTES>
663 requestMsgWr{};
664 auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
665
666 // WriteFile check for invalid file length
667 rc = encode_write_file_req(0, fileHandle, offset, length, requestWr);
668
669 ASSERT_EQ(rc, PLDM_INVALID_WRITE_LENGTH);
670}
671
672TEST(ReadWriteFile, testBadEncodeResponse)
673{
674 // Bad encode response for read file
675 uint32_t length = 0;
676
677 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES>
678 responseMsg{};
679 auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
680
681 // ReadFile
682 auto rc = encode_read_file_resp(0, PLDM_ERROR, length, responsePtr);
683
684 ASSERT_EQ(rc, PLDM_SUCCESS);
685 ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
686 ASSERT_EQ(responsePtr->hdr.instance_id, 0);
687 ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
688 ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
689 ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
690
691 // Bad encode response for write file
692 std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
693 responseMsgWr{};
694 auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
695
696 // WriteFile
697 rc = encode_write_file_resp(0, PLDM_ERROR, length, responseWr);
698
699 ASSERT_EQ(rc, PLDM_SUCCESS);
700 ASSERT_EQ(responseWr->hdr.request, PLDM_RESPONSE);
701 ASSERT_EQ(responseWr->hdr.instance_id, 0);
702 ASSERT_EQ(responseWr->hdr.type, PLDM_OEM);
703 ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE);
704 ASSERT_EQ(responseWr->payload[0], PLDM_ERROR);
705}