xref: /openbmc/libpldm/tests/oem/ibm/fileio.cpp (revision d0ba43af)
1 #include <endian.h>
2 #include <libpldm/base.h>
3 #include <libpldm/oem/ibm/file_io.h>
4 
5 #include <array>
6 #include <cstdint>
7 #include <cstring>
8 #include <vector>
9 
10 #include <gtest/gtest.h>
11 
12 constexpr auto hdrSize = sizeof(pldm_msg_hdr);
13 
TEST(ReadWriteFileMemory,testGoodDecodeRequest)14 TEST(ReadWriteFileMemory, testGoodDecodeRequest)
15 {
16     std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES + hdrSize> requestMsg{};
17 
18     // Random value for fileHandle, offset, length, address
19     uint32_t fileHandle = 0x12345678;
20     uint32_t offset = 0x87654321;
21     uint32_t length = 0x13245768;
22     uint64_t address = 0x124356879acbde0f;
23     uint32_t fileHandleLe = htole32(fileHandle);
24     uint32_t offsetLe = htole32(offset);
25     uint32_t lengthLe = htole32(length);
26     uint64_t addressLe = htole64(address);
27 
28     memcpy(requestMsg.data() + hdrSize, &fileHandleLe, sizeof(fileHandleLe));
29     memcpy(requestMsg.data() + sizeof(fileHandleLe) + hdrSize, &offsetLe,
30            sizeof(offsetLe));
31     memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) +
32                hdrSize,
33            &lengthLe, sizeof(lengthLe));
34     memcpy(requestMsg.data() + sizeof(fileHandleLe) + sizeof(offsetLe) +
35                sizeof(lengthLe) + hdrSize,
36            &addressLe, sizeof(addressLe));
37 
38     uint32_t retFileHandle = 0;
39     uint32_t retOffset = 0;
40     uint32_t retLength = 0;
41     uint64_t retAddress = 0;
42 
43     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
44     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
45 
46     // Invoke decode the read file memory request
47     auto rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
48                                         &retFileHandle, &retOffset, &retLength,
49                                         &retAddress);
50 
51     ASSERT_EQ(rc, PLDM_SUCCESS);
52     ASSERT_EQ(fileHandle, retFileHandle);
53     ASSERT_EQ(offset, retOffset);
54     ASSERT_EQ(length, retLength);
55     ASSERT_EQ(address, retAddress);
56 }
57 
TEST(ReadWriteFileMemory,testBadDecodeRequest)58 TEST(ReadWriteFileMemory, testBadDecodeRequest)
59 {
60     uint32_t fileHandle = 0;
61     uint32_t offset = 0;
62     uint32_t length = 0;
63     uint64_t address = 0;
64 
65     // Request payload message is missing
66     auto rc = decode_rw_file_memory_req(NULL, 0, &fileHandle, &offset, &length,
67                                         &address);
68     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
69 
70     std::array<uint8_t, PLDM_RW_FILE_MEM_REQ_BYTES> requestMsg{};
71 
72     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
73     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
74 
75     // Address is NULL
76     rc = decode_rw_file_memory_req(request, requestMsg.size() - hdrSize,
77                                    &fileHandle, &offset, &length, NULL);
78     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
79 
80     // Payload length is invalid
81     rc = decode_rw_file_memory_req(request, 0, &fileHandle, &offset, &length,
82                                    &address);
83     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
84 }
85 
TEST(ReadWriteFileMemory,testGoodEncodeResponse)86 TEST(ReadWriteFileMemory, testGoodEncodeResponse)
87 {
88     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
89         responseMsg{};
90     uint32_t length = 0xff00ee11;
91     uint32_t lengthLe = htole32(length);
92     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
93     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
94 
95     // ReadFileIntoMemory
96     auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
97                                          PLDM_SUCCESS, length, response);
98 
99     ASSERT_EQ(rc, PLDM_SUCCESS);
100     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
101     ASSERT_EQ(response->hdr.instance_id, 0);
102     ASSERT_EQ(response->hdr.type, PLDM_OEM);
103     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
104     ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
105     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
106                         &lengthLe, sizeof(lengthLe)));
107 
108     // WriteFileFromMemory
109     rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY,
110                                     PLDM_SUCCESS, length, response);
111 
112     ASSERT_EQ(rc, PLDM_SUCCESS);
113     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
114     ASSERT_EQ(response->hdr.instance_id, 0);
115     ASSERT_EQ(response->hdr.type, PLDM_OEM);
116     ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
117     ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
118     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
119                         &lengthLe, sizeof(lengthLe)));
120 }
121 
TEST(ReadWriteFileMemory,testBadEncodeResponse)122 TEST(ReadWriteFileMemory, testBadEncodeResponse)
123 {
124     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_RESP_BYTES>
125         responseMsg{};
126     uint32_t length = 0;
127     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
128     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
129 
130     // ReadFileIntoMemory
131     auto rc = encode_rw_file_memory_resp(0, PLDM_READ_FILE_INTO_MEMORY,
132                                          PLDM_ERROR, length, response);
133 
134     ASSERT_EQ(rc, PLDM_SUCCESS);
135     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
136     ASSERT_EQ(response->hdr.instance_id, 0);
137     ASSERT_EQ(response->hdr.type, PLDM_OEM);
138     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
139     ASSERT_EQ(response->payload[0], PLDM_ERROR);
140 
141     // WriteFileFromMemory
142     rc = encode_rw_file_memory_resp(0, PLDM_WRITE_FILE_FROM_MEMORY, PLDM_ERROR,
143                                     length, response);
144 
145     ASSERT_EQ(rc, PLDM_SUCCESS);
146     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
147     ASSERT_EQ(response->hdr.instance_id, 0);
148     ASSERT_EQ(response->hdr.type, PLDM_OEM);
149     ASSERT_EQ(response->hdr.command, PLDM_WRITE_FILE_FROM_MEMORY);
150     ASSERT_EQ(response->payload[0], PLDM_ERROR);
151 }
152 
TEST(ReadWriteFileIntoMemory,testGoodDecodeResponse)153 TEST(ReadWriteFileIntoMemory, testGoodDecodeResponse)
154 {
155     std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES + hdrSize> responseMsg{};
156     // Random value for length
157     uint32_t length = 0xff00ee12;
158     uint32_t lengthLe = htole32(length);
159     uint8_t completionCode = 0;
160 
161     memcpy(responseMsg.data() + hdrSize, &completionCode,
162            sizeof(completionCode));
163     memcpy(responseMsg.data() + sizeof(completionCode) + hdrSize, &lengthLe,
164            sizeof(lengthLe));
165 
166     uint8_t retCompletionCode = 0;
167     uint32_t retLength = 0;
168 
169     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
170     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
171 
172     // Invoke decode the read file memory response
173     auto rc = decode_rw_file_memory_resp(response, responseMsg.size() - hdrSize,
174                                          &retCompletionCode, &retLength);
175     ASSERT_EQ(rc, PLDM_SUCCESS);
176     ASSERT_EQ(completionCode, retCompletionCode);
177     ASSERT_EQ(length, retLength);
178 }
179 
TEST(ReadWriteFileIntoMemory,testBadDecodeResponse)180 TEST(ReadWriteFileIntoMemory, testBadDecodeResponse)
181 {
182     uint32_t length = 0;
183     uint8_t completionCode = 0;
184 
185     // Request payload message is missing
186     auto rc = decode_rw_file_memory_resp(NULL, 0, &completionCode, &length);
187     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
188 
189     std::array<uint8_t, PLDM_RW_FILE_MEM_RESP_BYTES> responseMsg{};
190 
191     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
192     auto response = reinterpret_cast<pldm_msg*>(responseMsg.data());
193 
194     // Payload length is invalid
195     rc = decode_rw_file_memory_resp(response, 0, &completionCode, &length);
196     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
197 }
198 
TEST(ReadWriteFileIntoMemory,testGoodEncodeRequest)199 TEST(ReadWriteFileIntoMemory, testGoodEncodeRequest)
200 {
201     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_MEM_REQ_BYTES>
202         requestMsg{};
203 
204     uint32_t fileHandle = 0x12345678;
205     uint32_t offset = 0x87654321;
206     uint32_t length = 0x13245768;
207     uint64_t address = 0x124356879acbde0f;
208     uint32_t fileHandleLe = htole32(fileHandle);
209     uint32_t offsetLe = htole32(offset);
210     uint32_t lengthLe = htole32(length);
211     uint64_t addressLe = htole64(address);
212 
213     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
214     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
215 
216     auto rc =
217         encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
218                                   offset, length, address, request);
219 
220     ASSERT_EQ(rc, PLDM_SUCCESS);
221     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
222     ASSERT_EQ(request->hdr.instance_id, 0);
223     ASSERT_EQ(request->hdr.type, PLDM_OEM);
224     ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_INTO_MEMORY);
225 
226     ASSERT_EQ(0, memcmp(request->payload, &fileHandleLe, sizeof(fileHandleLe)));
227 
228     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe), &offsetLe,
229                         sizeof(offsetLe)));
230     ASSERT_EQ(0,
231               memcmp(request->payload + sizeof(fileHandleLe) + sizeof(offsetLe),
232                      &lengthLe, sizeof(lengthLe)));
233     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileHandleLe) +
234                             sizeof(offsetLe) + sizeof(lengthLe),
235                         &addressLe, sizeof(addressLe)));
236 }
237 
TEST(ReadWriteFileIntoMemory,testBadEncodeRequest)238 TEST(ReadWriteFileIntoMemory, testBadEncodeRequest)
239 {
240     uint32_t fileHandle = 0;
241     uint32_t offset = 0;
242     uint32_t length = 0;
243     uint64_t address = 0;
244 
245     auto rc =
246         encode_rw_file_memory_req(0, PLDM_READ_FILE_INTO_MEMORY, fileHandle,
247                                   offset, length, address, NULL);
248 
249     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
250 }
251 
TEST(GetFileTable,GoodDecodeRequest)252 TEST(GetFileTable, GoodDecodeRequest)
253 {
254     std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES + hdrSize> requestMsg{};
255 
256     // Random value for DataTransferHandle, TransferOperationFlag, TableType
257     uint32_t transferHandle = 0x12345678;
258     uint32_t transferHandleLe = htole32(transferHandle);
259     uint8_t transferOpFlag = 1;
260     uint8_t tableType = 1;
261 
262     memcpy(requestMsg.data() + hdrSize, &transferHandleLe,
263            sizeof(transferHandleLe));
264     memcpy(requestMsg.data() + sizeof(transferHandle) + hdrSize,
265            &transferOpFlag, sizeof(transferOpFlag));
266     memcpy(requestMsg.data() + sizeof(transferHandle) + sizeof(transferOpFlag) +
267                hdrSize,
268            &tableType, sizeof(tableType));
269 
270     uint32_t retTransferHandle = 0;
271     uint8_t retTransferOpFlag = 0;
272     uint8_t retTableType = 0;
273 
274     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
275     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
276 
277     // Invoke decode get file table request
278     auto rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
279                                         &retTransferHandle, &retTransferOpFlag,
280                                         &retTableType);
281 
282     ASSERT_EQ(rc, PLDM_SUCCESS);
283     ASSERT_EQ(transferHandle, retTransferHandle);
284     ASSERT_EQ(transferOpFlag, retTransferOpFlag);
285     ASSERT_EQ(tableType, retTableType);
286 }
287 
TEST(GetFileTable,BadDecodeRequest)288 TEST(GetFileTable, BadDecodeRequest)
289 {
290     uint32_t transferHandle = 0;
291     uint8_t transferOpFlag = 0;
292     uint8_t tableType = 0;
293 
294     // Request payload message is missing
295     auto rc = decode_get_file_table_req(nullptr, 0, &transferHandle,
296                                         &transferOpFlag, &tableType);
297     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
298 
299     std::array<uint8_t, PLDM_GET_FILE_TABLE_REQ_BYTES> requestMsg{};
300 
301     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
302     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
303 
304     // TableType is NULL
305     rc = decode_get_file_table_req(request, requestMsg.size() - hdrSize,
306                                    &transferHandle, &transferOpFlag, nullptr);
307     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
308 
309     // Payload length is invalid
310     rc = decode_get_file_table_req(request, 0, &transferHandle, &transferOpFlag,
311                                    &tableType);
312     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
313 }
314 
TEST(GetFileTable,GoodEncodeResponse)315 TEST(GetFileTable, GoodEncodeResponse)
316 {
317     // Random value for NextDataTransferHandle and TransferFlag
318     uint8_t completionCode = 0;
319     uint32_t nextTransferHandle = 0x87654321;
320     uint32_t nextTransferHandleLe = htole32(nextTransferHandle);
321     uint8_t transferFlag = 5;
322     // Mock file table contents of size 5
323     std::array<uint8_t, 5> fileTable = {1, 2, 3, 4, 5};
324     constexpr size_t responseSize = sizeof(completionCode) +
325                                     sizeof(nextTransferHandle) +
326                                     sizeof(transferFlag) + fileTable.size();
327 
328     std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
329     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
330     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
331 
332     // GetFileTable
333     auto rc = encode_get_file_table_resp(0, PLDM_SUCCESS, nextTransferHandle,
334                                          transferFlag, fileTable.data(),
335                                          fileTable.size(), response);
336 
337     ASSERT_EQ(rc, PLDM_SUCCESS);
338     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
339     ASSERT_EQ(response->hdr.instance_id, 0);
340     ASSERT_EQ(response->hdr.type, PLDM_OEM);
341     ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
342     ASSERT_EQ(response->payload[0], PLDM_SUCCESS);
343     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]),
344                         &nextTransferHandleLe, sizeof(nextTransferHandle)));
345     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
346                             sizeof(nextTransferHandle),
347                         &transferFlag, sizeof(transferFlag)));
348     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
349                             sizeof(nextTransferHandle),
350                         &transferFlag, sizeof(transferFlag)));
351     ASSERT_EQ(0, memcmp(response->payload + sizeof(response->payload[0]) +
352                             sizeof(nextTransferHandle) + sizeof(transferFlag),
353                         fileTable.data(), fileTable.size()));
354 }
355 
TEST(GetFileTable,BadEncodeResponse)356 TEST(GetFileTable, BadEncodeResponse)
357 {
358     uint8_t completionCode = 0;
359     uint32_t nextTransferHandle = 0;
360     uint8_t transferFlag = 0;
361     constexpr size_t responseSize = sizeof(completionCode) +
362                                     sizeof(nextTransferHandle) +
363                                     sizeof(transferFlag);
364 
365     std::array<uint8_t, sizeof(pldm_msg_hdr) + responseSize> responseMsg{};
366     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
367     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
368 
369     // GetFileTable
370     auto rc = encode_get_file_table_resp(0, PLDM_ERROR, nextTransferHandle,
371                                          transferFlag, nullptr, 0, response);
372 
373     ASSERT_EQ(rc, PLDM_SUCCESS);
374     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
375     ASSERT_EQ(response->hdr.instance_id, 0);
376     ASSERT_EQ(response->hdr.type, PLDM_OEM);
377     ASSERT_EQ(response->hdr.command, PLDM_GET_FILE_TABLE);
378     ASSERT_EQ(response->payload[0], PLDM_ERROR);
379 }
380 
TEST(GetFileTable,GoodEncodeRequest)381 TEST(GetFileTable, GoodEncodeRequest)
382 {
383     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_FILE_TABLE_REQ_BYTES>
384         requestMsg{};
385     uint32_t transferHandle = 0x0;
386     uint8_t transferOpFlag = 0x01;
387     uint8_t tableType = PLDM_FILE_ATTRIBUTE_TABLE;
388 
389     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
390     auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
391     auto rc = encode_get_file_table_req(0, transferHandle, transferOpFlag,
392                                         tableType, request);
393     EXPECT_EQ(rc, PLDM_SUCCESS);
394 
395     struct pldm_get_file_table_req* req =
396         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
397         reinterpret_cast<struct pldm_get_file_table_req*>(request->payload);
398     EXPECT_EQ(transferHandle, le32toh(req->transfer_handle));
399     EXPECT_EQ(transferOpFlag, req->operation_flag);
400     EXPECT_EQ(tableType, req->table_type);
401 }
402 
TEST(GetFileTable,BadEncodeRequest)403 TEST(GetFileTable, BadEncodeRequest)
404 {
405     uint32_t transferHandle = 0x0;
406     uint8_t transferOpFlag = 0x01;
407     uint8_t tableType = PLDM_FILE_ATTRIBUTE_TABLE;
408 
409     auto rc = encode_get_file_table_req(0, transferHandle, transferOpFlag,
410                                         tableType, nullptr);
411 
412     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
413 }
414 
TEST(GetFileTable,GoodDecodeResponse)415 TEST(GetFileTable, GoodDecodeResponse)
416 {
417     uint32_t nextTransferHandle = 32;
418     uint8_t completionCode = PLDM_SUCCESS;
419     uint8_t transferFlag = PLDM_START_AND_END;
420     std::vector<uint8_t> fileTableData = {1, 2, 3, 4, 5, 6, 7, 8, 9};
421 
422     std::vector<uint8_t> responseMsg(
423         hdrSize + PLDM_GET_FILE_TABLE_MIN_RESP_BYTES + fileTableData.size());
424 
425     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
426     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
427     size_t payload_length = responseMsg.size() - hdrSize;
428 
429     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
430     auto resp = reinterpret_cast<struct pldm_get_file_table_resp*>(
431         responsePtr->payload);
432 
433     resp->completion_code = completionCode;
434     resp->next_transfer_handle = htole32(nextTransferHandle);
435     resp->transfer_flag = transferFlag;
436     memcpy(resp->table_data, fileTableData.data(), fileTableData.size());
437 
438     uint8_t retCompletionCode;
439     uint32_t retNextTransferHandle;
440     uint8_t retTransferFlag;
441     std::vector<uint8_t> retFileTableData(9, 0);
442     size_t retFileTableDataLength = 0;
443 
444     auto rc = decode_get_file_table_resp(
445         responsePtr, payload_length, &retCompletionCode, &retNextTransferHandle,
446         &retTransferFlag, retFileTableData.data(), &retFileTableDataLength);
447 
448     ASSERT_EQ(rc, PLDM_SUCCESS);
449     ASSERT_EQ(completionCode, retCompletionCode);
450     ASSERT_EQ(nextTransferHandle, retNextTransferHandle);
451     ASSERT_EQ(transferFlag, retTransferFlag);
452     ASSERT_EQ(0, memcmp(fileTableData.data(), resp->table_data,
453                         retFileTableDataLength));
454     ASSERT_EQ(fileTableData.size(), retFileTableDataLength);
455 }
456 
TEST(GetFileTable,BadDecodeResponse)457 TEST(GetFileTable, BadDecodeResponse)
458 {
459     uint32_t nextTransferHandle = 32;
460     uint8_t completionCode = PLDM_SUCCESS;
461     uint8_t transferFlag = PLDM_START_AND_END;
462     std::vector<uint8_t> fileTableData(9, 0);
463     size_t file_table_data_length = 0;
464 
465     std::vector<uint8_t> responseMsg(
466         hdrSize + PLDM_GET_FILE_TABLE_MIN_RESP_BYTES + fileTableData.size());
467 
468     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
469     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
470 
471     auto rc = decode_get_file_table_resp(
472         nullptr, 0, &completionCode, &nextTransferHandle, &transferFlag,
473         fileTableData.data(), &file_table_data_length);
474 
475     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
476 
477     rc = decode_get_file_table_resp(
478         responsePtr, 0, &completionCode, &nextTransferHandle, &transferFlag,
479         fileTableData.data(), &file_table_data_length);
480 
481     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
482 }
483 
TEST(ReadFile,testGoodDecodeRequest)484 TEST(ReadFile, testGoodDecodeRequest)
485 {
486     std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
487         requestMsg{};
488 
489     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
490     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
491     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
492     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
493     auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
494 
495     // Random value for fileHandle, offset and length
496     uint32_t fileHandle = 0x12345678;
497     uint32_t offset = 0x87654321;
498     uint32_t length = 0x13245768;
499 
500     request->file_handle = htole32(fileHandle);
501     request->offset = htole32(offset);
502     request->length = htole32(length);
503 
504     uint32_t retFileHandle = 0;
505     uint32_t retOffset = 0;
506     uint32_t retLength = 0;
507 
508     // Invoke decode the read file request
509     auto rc = decode_read_file_req(requestPtr, payload_length, &retFileHandle,
510                                    &retOffset, &retLength);
511 
512     ASSERT_EQ(rc, PLDM_SUCCESS);
513     ASSERT_EQ(fileHandle, retFileHandle);
514     ASSERT_EQ(offset, retOffset);
515     ASSERT_EQ(length, retLength);
516 }
517 
TEST(WriteFile,testGoodDecodeRequest)518 TEST(WriteFile, testGoodDecodeRequest)
519 {
520     // Random value for fileHandle, offset, length and file data
521     uint32_t fileHandle = 0x12345678;
522     uint32_t offset = 0x87654321;
523     uint32_t length = 0x467;
524 
525     std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
526                                     sizeof(pldm_msg_hdr) + length);
527     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
528     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
529     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
530     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
531     auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
532 
533     size_t fileDataOffset =
534         sizeof(fileHandle) + sizeof(offset) + sizeof(length);
535 
536     request->file_handle = htole32(fileHandle);
537     request->offset = htole32(offset);
538     request->length = htole32(length);
539 
540     uint32_t retFileHandle = 0;
541     uint32_t retOffset = 0;
542     uint32_t retLength = 0;
543     size_t retFileDataOffset = 0;
544 
545     // Invoke decode the write file request
546     auto rc = decode_write_file_req(requestPtr, payload_length, &retFileHandle,
547                                     &retOffset, &retLength, &retFileDataOffset);
548 
549     ASSERT_EQ(rc, PLDM_SUCCESS);
550     ASSERT_EQ(fileHandle, retFileHandle);
551     ASSERT_EQ(offset, retOffset);
552     ASSERT_EQ(length, retLength);
553     ASSERT_EQ(fileDataOffset, retFileDataOffset);
554 }
555 
TEST(ReadFile,testGoodDecodeResponse)556 TEST(ReadFile, testGoodDecodeResponse)
557 {
558     // Random value for length
559     uint32_t length = 0x10;
560     uint8_t completionCode = PLDM_SUCCESS;
561 
562     std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
563                                      sizeof(pldm_msg_hdr) + length);
564     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
565     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
566     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
567     auto response =
568         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
569         reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
570 
571     response->completion_code = completionCode;
572     response->length = htole32(length);
573 
574     size_t fileDataOffset = sizeof(completionCode) + sizeof(length);
575 
576     uint32_t retLength = 0;
577     uint8_t retCompletionCode = 0;
578     size_t retFileDataOffset = 0;
579 
580     // Invoke decode the read file response
581     auto rc =
582         decode_read_file_resp(responsePtr, payload_length, &retCompletionCode,
583                               &retLength, &retFileDataOffset);
584 
585     ASSERT_EQ(rc, PLDM_SUCCESS);
586     ASSERT_EQ(completionCode, retCompletionCode);
587     ASSERT_EQ(length, retLength);
588     ASSERT_EQ(fileDataOffset, retFileDataOffset);
589 }
590 
TEST(WriteFile,testGoodDecodeResponse)591 TEST(WriteFile, testGoodDecodeResponse)
592 {
593     std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
594         responseMsg{};
595     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
596     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
597     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
598     auto response =
599         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
600         reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
601 
602     uint8_t completionCode = PLDM_SUCCESS;
603     uint32_t length = 0x4678;
604 
605     response->completion_code = completionCode;
606     response->length = htole32(length);
607 
608     uint32_t retLength = 0;
609     uint8_t retCompletionCode = 0;
610 
611     // Invoke decode the write file response
612     auto rc = decode_write_file_resp(responsePtr, payload_length,
613                                      &retCompletionCode, &retLength);
614 
615     ASSERT_EQ(rc, PLDM_SUCCESS);
616     ASSERT_EQ(completionCode, retCompletionCode);
617     ASSERT_EQ(length, retLength);
618 }
619 
TEST(ReadWriteFile,testBadDecodeResponse)620 TEST(ReadWriteFile, testBadDecodeResponse)
621 {
622     uint32_t length = 0;
623     uint8_t completionCode = 0;
624     size_t fileDataOffset = 0;
625 
626     // Bad decode response for read file
627     std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
628                                      sizeof(pldm_msg_hdr) + length);
629     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
630     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
631 
632     // Request payload message is missing
633     auto rc = decode_read_file_resp(NULL, 0, &completionCode, &length,
634                                     &fileDataOffset);
635     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
636 
637     // Payload length is invalid
638     rc = decode_read_file_resp(responsePtr, 0, &completionCode, &length,
639                                &fileDataOffset);
640     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
641 
642     // Bad decode response for write file
643     std::array<uint8_t, PLDM_WRITE_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
644         responseMsgWr{};
645     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
646     auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
647 
648     // Request payload message is missing
649     rc = decode_write_file_resp(NULL, 0, &completionCode, &length);
650     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
651 
652     // Payload length is invalid
653     rc = decode_write_file_resp(responseWr, 0, &completionCode, &length);
654     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
655 }
656 
TEST(ReadWriteFile,testBadDecodeRequest)657 TEST(ReadWriteFile, testBadDecodeRequest)
658 {
659     uint32_t fileHandle = 0;
660     uint32_t offset = 0;
661     uint32_t length = 0;
662 
663     // Bad decode request for read file
664     std::array<uint8_t, PLDM_READ_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
665         requestMsg{};
666     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
667     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
668 
669     // Request payload message is missing
670     auto rc = decode_read_file_req(NULL, 0, &fileHandle, &offset, &length);
671     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
672 
673     // Payload length is invalid
674     rc = decode_read_file_req(requestPtr, 0, &fileHandle, &offset, &length);
675     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
676 
677     // Bad decode request for write file
678     size_t fileDataOffset = 0;
679     std::array<uint8_t, PLDM_WRITE_FILE_REQ_BYTES> requestMsgWr{};
680     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
681     auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
682 
683     // Request payload message is missing
684     rc = decode_write_file_req(NULL, 0, &fileHandle, &offset, &length,
685                                &fileDataOffset);
686     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
687 
688     // Payload length is invalid
689     rc = decode_write_file_req(requestWr, 0, &fileHandle, &offset, &length,
690                                &fileDataOffset);
691     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
692 }
693 
TEST(ReadFile,testGoodEncodeResponse)694 TEST(ReadFile, testGoodEncodeResponse)
695 {
696     // Good encode response for read file
697     uint32_t length = 0x4;
698 
699     std::vector<uint8_t> responseMsg(PLDM_READ_FILE_RESP_BYTES +
700                                      sizeof(pldm_msg_hdr) + length);
701     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
702     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
703     auto response =
704         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
705         reinterpret_cast<pldm_read_file_resp*>(responsePtr->payload);
706 
707     // ReadFile
708     auto rc = encode_read_file_resp(0, PLDM_SUCCESS, length, responsePtr);
709 
710     ASSERT_EQ(rc, PLDM_SUCCESS);
711     ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
712     ASSERT_EQ(responsePtr->hdr.instance_id, 0);
713     ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
714     ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
715     ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
716     ASSERT_EQ(le32toh(response->length), length);
717 }
718 
TEST(WriteFile,testGoodEncodeResponse)719 TEST(WriteFile, testGoodEncodeResponse)
720 {
721     uint32_t length = 0x467;
722 
723     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
724         responseMsg{};
725 
726     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
727     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
728     auto response =
729         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
730         reinterpret_cast<pldm_write_file_resp*>(responsePtr->payload);
731 
732     // WriteFile
733     auto rc = encode_write_file_resp(0, PLDM_SUCCESS, length, responsePtr);
734     ASSERT_EQ(rc, PLDM_SUCCESS);
735     ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
736     ASSERT_EQ(responsePtr->hdr.instance_id, 0);
737     ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
738     ASSERT_EQ(responsePtr->hdr.command, PLDM_WRITE_FILE);
739     ASSERT_EQ(response->completion_code, PLDM_SUCCESS);
740     ASSERT_EQ(le32toh(response->length), length);
741 }
742 
TEST(ReadFile,testGoodEncodeRequest)743 TEST(ReadFile, testGoodEncodeRequest)
744 {
745     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
746         requestMsg{};
747 
748     uint32_t fileHandle = 0x12345678;
749     uint32_t offset = 0x87654321;
750     uint32_t length = 0x13245768;
751     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
752     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
753     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
754     auto request = reinterpret_cast<pldm_read_file_req*>(requestPtr->payload);
755 
756     // ReadFile
757     auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
758 
759     ASSERT_EQ(rc, PLDM_SUCCESS);
760     ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
761     ASSERT_EQ(requestPtr->hdr.instance_id, 0);
762     ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
763     ASSERT_EQ(requestPtr->hdr.command, PLDM_READ_FILE);
764     ASSERT_EQ(le32toh(request->file_handle), fileHandle);
765     ASSERT_EQ(le32toh(request->offset), offset);
766     ASSERT_EQ(le32toh(request->length), length);
767 }
768 
TEST(WriteFile,testGoodEncodeRequest)769 TEST(WriteFile, testGoodEncodeRequest)
770 {
771     uint32_t fileHandle = 0x12345678;
772     uint32_t offset = 0x87654321;
773     uint32_t length = 0x456;
774 
775     std::vector<uint8_t> requestMsg(PLDM_WRITE_FILE_REQ_BYTES +
776                                     sizeof(pldm_msg_hdr) + length);
777     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
778     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
779     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
780     auto request = reinterpret_cast<pldm_write_file_req*>(requestPtr->payload);
781 
782     // WriteFile
783     auto rc = encode_write_file_req(0, fileHandle, offset, length, requestPtr);
784 
785     ASSERT_EQ(rc, PLDM_SUCCESS);
786     ASSERT_EQ(requestPtr->hdr.request, PLDM_REQUEST);
787     ASSERT_EQ(requestPtr->hdr.instance_id, 0);
788     ASSERT_EQ(requestPtr->hdr.type, PLDM_OEM);
789     ASSERT_EQ(requestPtr->hdr.command, PLDM_WRITE_FILE);
790     ASSERT_EQ(le32toh(request->file_handle), fileHandle);
791     ASSERT_EQ(le32toh(request->offset), offset);
792     ASSERT_EQ(le32toh(request->length), length);
793 }
794 
TEST(ReadWriteFile,testBadEncodeRequest)795 TEST(ReadWriteFile, testBadEncodeRequest)
796 {
797     // Bad encode request for read file
798     uint32_t fileHandle = 0;
799     uint32_t offset = 0;
800     uint32_t length = 0;
801 
802     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_REQ_BYTES>
803         requestMsg{};
804     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
805     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
806 
807     // ReadFile check invalid file length
808     auto rc = encode_read_file_req(0, fileHandle, offset, length, requestPtr);
809 
810     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
811 
812     // Bad encode request for write file
813     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_REQ_BYTES>
814         requestMsgWr{};
815     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
816     auto requestWr = reinterpret_cast<pldm_msg*>(requestMsgWr.data());
817 
818     // WriteFile check for invalid file length
819     rc = encode_write_file_req(0, fileHandle, offset, length, requestWr);
820 
821     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
822 }
823 
TEST(ReadWriteFile,testBadEncodeResponse)824 TEST(ReadWriteFile, testBadEncodeResponse)
825 {
826     // Bad encode response for read file
827     uint32_t length = 0;
828 
829     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_READ_FILE_RESP_BYTES>
830         responseMsg{};
831     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
832     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
833 
834     // ReadFile
835     auto rc = encode_read_file_resp(0, PLDM_ERROR, length, responsePtr);
836 
837     ASSERT_EQ(rc, PLDM_SUCCESS);
838     ASSERT_EQ(responsePtr->hdr.request, PLDM_RESPONSE);
839     ASSERT_EQ(responsePtr->hdr.instance_id, 0);
840     ASSERT_EQ(responsePtr->hdr.type, PLDM_OEM);
841     ASSERT_EQ(responsePtr->hdr.command, PLDM_READ_FILE);
842     ASSERT_EQ(responsePtr->payload[0], PLDM_ERROR);
843 
844     // Bad encode response for write file
845     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_WRITE_FILE_RESP_BYTES>
846         responseMsgWr{};
847     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
848     auto responseWr = reinterpret_cast<pldm_msg*>(responseMsgWr.data());
849 
850     // WriteFile
851     rc = encode_write_file_resp(0, PLDM_ERROR, length, responseWr);
852 
853     ASSERT_EQ(rc, PLDM_SUCCESS);
854     ASSERT_EQ(responseWr->hdr.request, PLDM_RESPONSE);
855     ASSERT_EQ(responseWr->hdr.instance_id, 0);
856     ASSERT_EQ(responseWr->hdr.type, PLDM_OEM);
857     ASSERT_EQ(responseWr->hdr.command, PLDM_WRITE_FILE);
858     ASSERT_EQ(responseWr->payload[0], PLDM_ERROR);
859 }
860 
TEST(ReadWriteFileByTypeMemory,testGoodDecodeRequest)861 TEST(ReadWriteFileByTypeMemory, testGoodDecodeRequest)
862 {
863     std::array<uint8_t,
864                PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
865         requestMsg{};
866 
867     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
868     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
869     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
870     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
871     auto request = reinterpret_cast<pldm_read_write_file_by_type_memory_req*>(
872         requestPtr->payload);
873 
874     // Random value for fileHandle, offset and length
875     uint16_t fileType = 0;
876     uint32_t fileHandle = 0x12345678;
877     uint32_t offset = 0x87654321;
878     uint32_t length = 0x13245768;
879     uint64_t address = 0x124356879acbd456;
880 
881     request->file_type = htole16(fileType);
882     request->file_handle = htole32(fileHandle);
883     request->offset = htole32(offset);
884     request->length = htole32(length);
885     request->address = htole64(address);
886 
887     uint16_t retFileType = 0x1;
888     uint32_t retFileHandle = 0;
889     uint32_t retOffset = 0;
890     uint32_t retLength = 0;
891     uint64_t retAddress = 0;
892 
893     // Invoke decode the read file request
894     auto rc = decode_rw_file_by_type_memory_req(
895         requestPtr, payload_length, &retFileType, &retFileHandle, &retOffset,
896         &retLength, &retAddress);
897 
898     ASSERT_EQ(rc, PLDM_SUCCESS);
899     ASSERT_EQ(fileType, retFileType);
900     ASSERT_EQ(fileHandle, retFileHandle);
901     ASSERT_EQ(offset, retOffset);
902     ASSERT_EQ(length, retLength);
903     ASSERT_EQ(address, retAddress);
904 }
905 
TEST(ReadWriteFileByTypeMemory,testGoodDecodeResponse)906 TEST(ReadWriteFileByTypeMemory, testGoodDecodeResponse)
907 {
908     std::array<uint8_t,
909                PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
910         responseMsg{};
911 
912     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
913     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
914     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
915     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
916     auto response = reinterpret_cast<pldm_read_write_file_by_type_memory_resp*>(
917         responsePtr->payload);
918 
919     // Random value for completion code and length
920     uint8_t completionCode = 0x0;
921     uint32_t length = 0x13245768;
922 
923     response->completion_code = completionCode;
924     response->length = htole32(length);
925 
926     uint8_t retCompletionCode = 0x1;
927     uint32_t retLength = 0;
928 
929     // Invoke decode the read/write file response
930     auto rc = decode_rw_file_by_type_memory_resp(
931         responsePtr, payload_length, &retCompletionCode, &retLength);
932 
933     ASSERT_EQ(rc, PLDM_SUCCESS);
934     ASSERT_EQ(completionCode, retCompletionCode);
935     ASSERT_EQ(length, retLength);
936 }
937 
TEST(ReadWriteFileByTypeMemory,testBadDecodeRequest)938 TEST(ReadWriteFileByTypeMemory, testBadDecodeRequest)
939 {
940     uint16_t fileType = 0;
941     uint32_t fileHandle = 0;
942     uint32_t offset = 0;
943     uint32_t length = 0;
944     uint64_t address = 0;
945 
946     // Request payload message is missing
947     auto rc = decode_rw_file_by_type_memory_req(NULL, 0, &fileType, &fileHandle,
948                                                 &offset, &length, &address);
949     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
950 
951     std::array<uint8_t,
952                PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES + sizeof(pldm_msg_hdr)>
953         requestMsg{};
954 
955     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
956     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
957 
958     // Address is NULL
959     rc = decode_rw_file_by_type_memory_req(
960         requestPtr, requestMsg.size() - hdrSize, &fileType, &fileHandle,
961         &offset, &length, NULL);
962     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
963 
964     // Payload length is invalid
965     rc = decode_rw_file_by_type_memory_req(
966         requestPtr, 0, &fileType, &fileHandle, &offset, &length, &address);
967     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
968 }
969 
TEST(ReadWriteFileByTypeMemory,testBadDecodeResponse)970 TEST(ReadWriteFileByTypeMemory, testBadDecodeResponse)
971 {
972     uint32_t length = 0;
973     uint8_t completionCode = 0;
974 
975     // Request payload message is missing
976     auto rc =
977         decode_rw_file_by_type_memory_resp(NULL, 0, &completionCode, &length);
978     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
979 
980     std::array<uint8_t,
981                PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES + sizeof(pldm_msg_hdr)>
982         responseMsg{};
983 
984     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
985     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
986 
987     // Length is NULL
988     rc = decode_rw_file_by_type_memory_resp(
989         responsePtr, responseMsg.size() - hdrSize, &completionCode, NULL);
990     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
991 
992     // Payload length is invalid
993     rc = decode_rw_file_by_type_memory_resp(responsePtr, 0, &completionCode,
994                                             &length);
995     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
996 }
997 
TEST(ReadWriteFileByTypeMemory,testGoodEncodeRequest)998 TEST(ReadWriteFileByTypeMemory, testGoodEncodeRequest)
999 {
1000     std::array<uint8_t,
1001                sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_REQ_BYTES>
1002         requestMsg{};
1003 
1004     uint16_t fileType = 0;
1005     uint32_t fileHandle = 0x12345678;
1006     uint32_t offset = 0x87654321;
1007     uint32_t length = 0x13245768;
1008     uint64_t address = 0x124356879acbde0f;
1009     uint16_t fileTypeLe = htole16(fileType);
1010     uint32_t fileHandleLe = htole32(fileHandle);
1011     uint32_t offsetLe = htole32(offset);
1012     uint32_t lengthLe = htole32(length);
1013     uint64_t addressLe = htole64(address);
1014 
1015     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1016     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1017 
1018     auto rc = encode_rw_file_by_type_memory_req(
1019         0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
1020         length, address, request);
1021 
1022     ASSERT_EQ(rc, PLDM_SUCCESS);
1023     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1024     ASSERT_EQ(request->hdr.instance_id, 0);
1025     ASSERT_EQ(request->hdr.type, PLDM_OEM);
1026     ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
1027 
1028     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1029     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1030                         sizeof(fileHandleLe)));
1031 
1032     ASSERT_EQ(
1033         0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1034                   &offsetLe, sizeof(offsetLe)));
1035     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1036                             sizeof(fileHandleLe) + sizeof(offsetLe),
1037                         &lengthLe, sizeof(lengthLe)));
1038     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1039                             sizeof(fileHandleLe) + sizeof(offsetLe) +
1040                             sizeof(lengthLe),
1041                         &addressLe, sizeof(addressLe)));
1042 }
1043 
TEST(ReadWriteFileByTypeMemory,testGoodEncodeResponse)1044 TEST(ReadWriteFileByTypeMemory, testGoodEncodeResponse)
1045 {
1046     std::array<uint8_t,
1047                sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
1048         responseMsg{};
1049 
1050     uint32_t length = 0x13245768;
1051     uint32_t lengthLe = htole32(length);
1052     uint8_t completionCode = 0x0;
1053 
1054     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1055     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1056 
1057     auto rc = encode_rw_file_by_type_memory_resp(
1058         0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, completionCode, length,
1059         response);
1060 
1061     ASSERT_EQ(rc, PLDM_SUCCESS);
1062     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1063     ASSERT_EQ(response->hdr.instance_id, 0);
1064     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1065     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
1066 
1067     ASSERT_EQ(
1068         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1069     ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe,
1070                         sizeof(lengthLe)));
1071 }
1072 
TEST(ReadWriteFileByTypeMemory,testBadEncodeResponse)1073 TEST(ReadWriteFileByTypeMemory, testBadEncodeResponse)
1074 {
1075     std::array<uint8_t,
1076                sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_MEM_RESP_BYTES>
1077         responseMsg{};
1078     uint32_t length = 0;
1079     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1080     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1081 
1082     // completion code is PLDM_ERROR
1083     auto rc = encode_rw_file_by_type_memory_resp(
1084         0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_ERROR, length, response);
1085 
1086     ASSERT_EQ(rc, PLDM_SUCCESS);
1087     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1088     ASSERT_EQ(response->hdr.instance_id, 0);
1089     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1090     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY);
1091     ASSERT_EQ(response->payload[0], PLDM_ERROR);
1092 
1093     // response is NULL pointer
1094     rc = encode_rw_file_by_type_memory_resp(
1095         0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, PLDM_SUCCESS, length, NULL);
1096 
1097     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1098 }
1099 
TEST(ReadWriteFileByTypeMemory,testBadEncodeRequest)1100 TEST(ReadWriteFileByTypeMemory, testBadEncodeRequest)
1101 {
1102     uint8_t fileType = 0;
1103     uint32_t fileHandle = 0;
1104     uint32_t offset = 0;
1105     uint32_t length = 0;
1106     uint64_t address = 0;
1107 
1108     // request is NULL pointer
1109     auto rc = encode_rw_file_by_type_memory_req(
1110         0, PLDM_READ_FILE_BY_TYPE_INTO_MEMORY, fileType, fileHandle, offset,
1111         length, address, NULL);
1112 
1113     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1114 }
1115 
TEST(NewFile,testGoodDecodeRequest)1116 TEST(NewFile, testGoodDecodeRequest)
1117 {
1118     std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1119         requestMsg{};
1120 
1121     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1122     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1123     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1124     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1125     auto request = reinterpret_cast<pldm_new_file_req*>(requestPtr->payload);
1126 
1127     // Random value for fileHandle and length
1128     uint16_t fileType = 0xff;
1129     uint32_t fileHandle = 0x12345678;
1130     uint64_t length = 0x13245768;
1131 
1132     request->file_type = htole16(fileType);
1133     request->file_handle = htole32(fileHandle);
1134     request->length = htole64(length);
1135 
1136     uint16_t retFileType = 0xff;
1137     uint32_t retFileHandle = 0;
1138     uint64_t retLength = 0;
1139 
1140     // Invoke decode the read file request
1141     auto rc = decode_new_file_req(requestPtr, payload_length, &retFileType,
1142                                   &retFileHandle, &retLength);
1143 
1144     ASSERT_EQ(rc, PLDM_SUCCESS);
1145     ASSERT_EQ(fileType, retFileType);
1146     ASSERT_EQ(fileHandle, retFileHandle);
1147     ASSERT_EQ(length, retLength);
1148 }
1149 
TEST(NewFile,testGoodDecodeResponse)1150 TEST(NewFile, testGoodDecodeResponse)
1151 {
1152     std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1153         responseMsg{};
1154 
1155     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1156     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1157     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1158     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1159     auto response = reinterpret_cast<pldm_new_file_resp*>(responsePtr->payload);
1160 
1161     // Random value for completion code
1162     uint8_t completionCode = 0x0;
1163 
1164     response->completion_code = completionCode;
1165 
1166     uint8_t retCompletionCode = PLDM_SUCCESS;
1167 
1168     // Invoke decode the read/write file response
1169     auto rc =
1170         decode_new_file_resp(responsePtr, payload_length, &retCompletionCode);
1171 
1172     ASSERT_EQ(rc, PLDM_SUCCESS);
1173     ASSERT_EQ(completionCode, retCompletionCode);
1174 }
1175 
TEST(NewFile,testBadDecodeRequest)1176 TEST(NewFile, testBadDecodeRequest)
1177 {
1178     uint16_t fileType = 0;
1179     uint32_t fileHandle = 0;
1180     uint64_t length = 0;
1181 
1182     // Request payload message is missing
1183     auto rc = decode_new_file_req(NULL, 0, &fileType, &fileHandle, &length);
1184 
1185     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1186 
1187     std::array<uint8_t, PLDM_NEW_FILE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1188         requestMsg{};
1189 
1190     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1191     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1192 
1193     // Payload length is invalid
1194     rc = decode_new_file_req(requestPtr, 0, &fileType, &fileHandle, &length);
1195     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1196 }
1197 
TEST(NewFile,testBadDecodeResponse)1198 TEST(NewFile, testBadDecodeResponse)
1199 {
1200     uint8_t completionCode = 0;
1201 
1202     // Request payload message is missing
1203     auto rc = decode_new_file_resp(NULL, 0, &completionCode);
1204     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1205 
1206     std::array<uint8_t, PLDM_NEW_FILE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1207         responseMsg{};
1208 
1209     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1210     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1211 
1212     // Payload length is invalid
1213     rc = decode_new_file_resp(responsePtr, 0, &completionCode);
1214     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1215 }
1216 
TEST(NewFile,testGoodEncodeRequest)1217 TEST(NewFile, testGoodEncodeRequest)
1218 {
1219     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_REQ_BYTES>
1220         requestMsg{};
1221 
1222     uint16_t fileType = 0xff;
1223     uint32_t fileHandle = 0x12345678;
1224     uint32_t length = 0x13245768;
1225     uint16_t fileTypeLe = htole16(fileType);
1226     uint32_t fileHandleLe = htole32(fileHandle);
1227     uint32_t lengthLe = htole32(length);
1228 
1229     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1230     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1231 
1232     auto rc = encode_new_file_req(0, fileType, fileHandle, length, request);
1233 
1234     ASSERT_EQ(rc, PLDM_SUCCESS);
1235     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1236     ASSERT_EQ(request->hdr.instance_id, 0);
1237     ASSERT_EQ(request->hdr.type, PLDM_OEM);
1238     ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1239     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1240     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1241                         sizeof(fileHandleLe)));
1242     ASSERT_EQ(
1243         0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1244                   &lengthLe, sizeof(lengthLe)));
1245 }
1246 
TEST(NewFile,testGoodEncodeResponse)1247 TEST(NewFile, testGoodEncodeResponse)
1248 {
1249     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1250         responseMsg{};
1251 
1252     uint8_t completionCode = 0x0;
1253 
1254     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1255     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1256 
1257     auto rc = encode_new_file_resp(0, completionCode, response);
1258 
1259     ASSERT_EQ(rc, PLDM_SUCCESS);
1260     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1261     ASSERT_EQ(response->hdr.instance_id, 0);
1262     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1263     ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1264     ASSERT_EQ(
1265         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1266 }
1267 
TEST(NewFile,testBadEncodeResponse)1268 TEST(NewFile, testBadEncodeResponse)
1269 {
1270     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_NEW_FILE_RESP_BYTES>
1271         responseMsg{};
1272     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1273     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1274 
1275     // completion code is PLDM_ERROR
1276     auto rc = encode_new_file_resp(0, PLDM_ERROR, response);
1277 
1278     ASSERT_EQ(rc, PLDM_SUCCESS);
1279     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1280     ASSERT_EQ(response->hdr.instance_id, 0);
1281     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1282     ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE);
1283     ASSERT_EQ(response->payload[0], PLDM_ERROR);
1284 
1285     // response is NULL pointer
1286     rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL);
1287 
1288     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1289 }
1290 
TEST(NewFile,testBadEncodeRequest)1291 TEST(NewFile, testBadEncodeRequest)
1292 {
1293     uint8_t fileType = 0xff;
1294     uint32_t fileHandle = 0;
1295     uint32_t length = 0;
1296 
1297     // request is NULL pointer
1298     auto rc = encode_new_file_req(0, fileType, fileHandle, length, NULL);
1299 
1300     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1301 }
1302 
TEST(ReadWriteFileByType,testGoodDecodeRequest)1303 TEST(ReadWriteFileByType, testGoodDecodeRequest)
1304 {
1305     std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1306         requestMsg{};
1307 
1308     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1309     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1310     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1311     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1312     auto request = reinterpret_cast<pldm_read_write_file_by_type_req*>(
1313         requestPtr->payload);
1314 
1315     // Random value for fileHandle, offset and length
1316     uint16_t fileType = 0;
1317     uint32_t fileHandle = 0x12345678;
1318     uint32_t offset = 0x87654321;
1319     uint32_t length = 0x13245768;
1320 
1321     request->file_handle = htole32(fileHandle);
1322     request->offset = htole32(offset);
1323     request->length = htole32(length);
1324 
1325     uint16_t retFileType = 0x1;
1326     uint32_t retFileHandle = 0;
1327     uint32_t retOffset = 0;
1328     uint32_t retLength = 0;
1329 
1330     // Invoke decode the read file request
1331     auto rc =
1332         decode_rw_file_by_type_req(requestPtr, payload_length, &retFileType,
1333                                    &retFileHandle, &retOffset, &retLength);
1334 
1335     ASSERT_EQ(rc, PLDM_SUCCESS);
1336     ASSERT_EQ(fileType, retFileType);
1337     ASSERT_EQ(fileHandle, retFileHandle);
1338     ASSERT_EQ(offset, retOffset);
1339     ASSERT_EQ(length, retLength);
1340 }
1341 
TEST(ReadWriteFileByType,testGoodDecodeResponse)1342 TEST(ReadWriteFileByType, testGoodDecodeResponse)
1343 {
1344     std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1345         responseMsg{};
1346 
1347     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1348     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1349     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1350     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1351     auto response = reinterpret_cast<pldm_read_write_file_by_type_resp*>(
1352         responsePtr->payload);
1353 
1354     // Random value for completion code and length
1355     uint8_t completionCode = 0x0;
1356     uint32_t length = 0x13245768;
1357 
1358     response->completion_code = completionCode;
1359     response->length = htole32(length);
1360 
1361     uint8_t retCompletionCode = 0x1;
1362     uint32_t retLength = 0;
1363 
1364     // Invoke decode the read/write file response
1365     auto rc = decode_rw_file_by_type_resp(responsePtr, payload_length,
1366                                           &retCompletionCode, &retLength);
1367 
1368     ASSERT_EQ(rc, PLDM_SUCCESS);
1369     ASSERT_EQ(completionCode, retCompletionCode);
1370     ASSERT_EQ(length, retLength);
1371 }
1372 
TEST(ReadWriteFileByType,testBadDecodeRequest)1373 TEST(ReadWriteFileByType, testBadDecodeRequest)
1374 {
1375     uint16_t fileType = 0;
1376     uint32_t fileHandle = 0;
1377     uint32_t offset = 0;
1378     uint32_t length = 0;
1379 
1380     // Request payload message is missing
1381     auto rc = decode_rw_file_by_type_req(NULL, 0, &fileType, &fileHandle,
1382                                          &offset, &length);
1383     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1384 
1385     std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_REQ_BYTES + sizeof(pldm_msg_hdr)>
1386         requestMsg{};
1387 
1388     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1389     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1390 
1391     // Payload length is invalid
1392     rc = decode_rw_file_by_type_req(requestPtr, 0, &fileType, &fileHandle,
1393                                     &offset, &length);
1394     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1395 }
1396 
TEST(ReadWriteFileByType,testBadDecodeResponse)1397 TEST(ReadWriteFileByType, testBadDecodeResponse)
1398 {
1399     uint32_t length = 0;
1400     uint8_t completionCode = 0;
1401 
1402     // Request payload message is missing
1403     auto rc = decode_rw_file_by_type_resp(NULL, 0, &completionCode, &length);
1404     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1405 
1406     std::array<uint8_t, PLDM_RW_FILE_BY_TYPE_RESP_BYTES + sizeof(pldm_msg_hdr)>
1407         responseMsg{};
1408 
1409     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1410     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1411 
1412     // Length is NULL
1413     rc = decode_rw_file_by_type_resp(responsePtr, responseMsg.size() - hdrSize,
1414                                      &completionCode, NULL);
1415     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1416 
1417     // Payload length is invalid
1418     rc = decode_rw_file_by_type_resp(responsePtr, 0, &completionCode, &length);
1419     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1420 }
1421 
TEST(ReadWriteFileByType,testGoodEncodeRequest)1422 TEST(ReadWriteFileByType, testGoodEncodeRequest)
1423 {
1424     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_REQ_BYTES>
1425         requestMsg{};
1426 
1427     uint16_t fileType = 0;
1428     uint32_t fileHandle = 0x12345678;
1429     uint32_t offset = 0x87654321;
1430     uint32_t length = 0x13245768;
1431     uint16_t fileTypeLe = htole16(fileType);
1432     uint32_t fileHandleLe = htole32(fileHandle);
1433     uint32_t offsetLe = htole32(offset);
1434     uint32_t lengthLe = htole32(length);
1435 
1436     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1437     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1438 
1439     auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1440                                          fileHandle, offset, length, request);
1441 
1442     ASSERT_EQ(rc, PLDM_SUCCESS);
1443     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1444     ASSERT_EQ(request->hdr.instance_id, 0);
1445     ASSERT_EQ(request->hdr.type, PLDM_OEM);
1446     ASSERT_EQ(request->hdr.command, PLDM_READ_FILE_BY_TYPE);
1447 
1448     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1449     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1450                         sizeof(fileHandleLe)));
1451 
1452     ASSERT_EQ(
1453         0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1454                   &offsetLe, sizeof(offsetLe)));
1455     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1456                             sizeof(fileHandleLe) + sizeof(offsetLe),
1457                         &lengthLe, sizeof(lengthLe)));
1458 }
1459 
TEST(ReadWriteFileByType,testGoodEncodeResponse)1460 TEST(ReadWriteFileByType, testGoodEncodeResponse)
1461 {
1462     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1463         responseMsg{};
1464 
1465     uint32_t length = 0x13245768;
1466     uint32_t lengthLe = htole32(length);
1467     uint8_t completionCode = 0x0;
1468     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1469     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1470 
1471     auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE,
1472                                           completionCode, length, response);
1473 
1474     ASSERT_EQ(rc, PLDM_SUCCESS);
1475     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1476     ASSERT_EQ(response->hdr.instance_id, 0);
1477     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1478     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1479 
1480     ASSERT_EQ(
1481         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1482     ASSERT_EQ(0, memcmp(response->payload + sizeof(completionCode), &lengthLe,
1483                         sizeof(lengthLe)));
1484 }
1485 
TEST(ReadWriteFileByType,testBadEncodeResponse)1486 TEST(ReadWriteFileByType, testBadEncodeResponse)
1487 {
1488     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_RW_FILE_BY_TYPE_RESP_BYTES>
1489         responseMsg{};
1490     uint32_t length = 0;
1491     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1492     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1493 
1494     // completion code is PLDM_ERROR
1495     auto rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_ERROR,
1496                                           length, response);
1497 
1498     ASSERT_EQ(rc, PLDM_SUCCESS);
1499     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1500     ASSERT_EQ(response->hdr.instance_id, 0);
1501     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1502     ASSERT_EQ(response->hdr.command, PLDM_READ_FILE_BY_TYPE);
1503     ASSERT_EQ(response->payload[0], PLDM_ERROR);
1504 
1505     // response is NULL pointer
1506     rc = encode_rw_file_by_type_resp(0, PLDM_READ_FILE_BY_TYPE, PLDM_SUCCESS,
1507                                      length, NULL);
1508 
1509     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1510 }
1511 
TEST(ReadWriteFileByType,testBadEncodeRequest)1512 TEST(ReadWriteFileByType, testBadEncodeRequest)
1513 {
1514     uint8_t fileType = 0;
1515     uint32_t fileHandle = 0;
1516     uint32_t offset = 0;
1517     uint32_t length = 0;
1518 
1519     // request is NULL pointer
1520     auto rc = encode_rw_file_by_type_req(0, PLDM_READ_FILE_BY_TYPE, fileType,
1521                                          fileHandle, offset, length, NULL);
1522 
1523     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1524 }
1525 
TEST(FileAck,testGoodDecodeRequest)1526 TEST(FileAck, testGoodDecodeRequest)
1527 {
1528     std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1529         requestMsg{};
1530 
1531     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1532     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1533     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1534     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1535     auto request = reinterpret_cast<pldm_file_ack_req*>(requestPtr->payload);
1536 
1537     // Random value for fileHandle
1538     uint16_t fileType = 0xffff;
1539     uint32_t fileHandle = 0x12345678;
1540     uint8_t fileStatus = 0xff;
1541 
1542     request->file_type = htole16(fileType);
1543     request->file_handle = htole32(fileHandle);
1544     request->file_status = fileStatus;
1545 
1546     uint16_t retFileType = 0xff;
1547     uint32_t retFileHandle = 0;
1548     uint8_t retFileStatus = 0;
1549 
1550     // Invoke decode the read file request
1551     auto rc = decode_file_ack_req(requestPtr, payload_length, &retFileType,
1552                                   &retFileHandle, &retFileStatus);
1553 
1554     ASSERT_EQ(rc, PLDM_SUCCESS);
1555     ASSERT_EQ(fileType, retFileType);
1556     ASSERT_EQ(fileHandle, retFileHandle);
1557     ASSERT_EQ(fileStatus, retFileStatus);
1558 }
1559 
TEST(FileAck,testGoodDecodeResponse)1560 TEST(FileAck, testGoodDecodeResponse)
1561 {
1562     std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1563         responseMsg{};
1564 
1565     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1566     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1567     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1568     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1569     auto response = reinterpret_cast<pldm_file_ack_resp*>(responsePtr->payload);
1570 
1571     // Random value for completion code
1572     uint8_t completionCode = 0x0;
1573 
1574     response->completion_code = completionCode;
1575 
1576     uint8_t retCompletionCode = PLDM_SUCCESS;
1577 
1578     // Invoke decode the read/write file response
1579     auto rc =
1580         decode_file_ack_resp(responsePtr, payload_length, &retCompletionCode);
1581 
1582     ASSERT_EQ(rc, PLDM_SUCCESS);
1583     ASSERT_EQ(completionCode, retCompletionCode);
1584 }
1585 
TEST(FileAck,testBadDecodeRequest)1586 TEST(FileAck, testBadDecodeRequest)
1587 {
1588     uint16_t fileType = 0;
1589     uint32_t fileHandle = 0;
1590     uint8_t fileStatus = 0;
1591 
1592     // Request payload message is missing
1593     auto rc = decode_file_ack_req(NULL, 0, &fileType, &fileHandle, &fileStatus);
1594 
1595     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1596 
1597     std::array<uint8_t, PLDM_FILE_ACK_REQ_BYTES + sizeof(pldm_msg_hdr)>
1598         requestMsg{};
1599 
1600     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1601     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1602 
1603     // Payload length is invalid
1604     rc =
1605         decode_file_ack_req(requestPtr, 0, &fileType, &fileHandle, &fileStatus);
1606     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1607 }
1608 
TEST(FileAck,testBadDecodeResponse)1609 TEST(FileAck, testBadDecodeResponse)
1610 {
1611     uint8_t completionCode = 0;
1612 
1613     // Request payload message is missing
1614     auto rc = decode_file_ack_resp(NULL, 0, &completionCode);
1615     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1616 
1617     std::array<uint8_t, PLDM_FILE_ACK_RESP_BYTES + sizeof(pldm_msg_hdr)>
1618         responseMsg{};
1619 
1620     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1621     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1622 
1623     // Payload length is invalid
1624     rc = decode_file_ack_resp(responsePtr, 0, &completionCode);
1625     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1626 }
1627 
TEST(FileAck,testGoodEncodeRequest)1628 TEST(FileAck, testGoodEncodeRequest)
1629 {
1630     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_REQ_BYTES>
1631         requestMsg{};
1632 
1633     uint16_t fileType = 0xffff;
1634     uint32_t fileHandle = 0x12345678;
1635     uint8_t fileStatus = 0xff;
1636     uint16_t fileTypeLe = htole16(fileType);
1637     uint32_t fileHandleLe = htole32(fileHandle);
1638 
1639     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1640     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1641 
1642     auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, request);
1643 
1644     ASSERT_EQ(rc, PLDM_SUCCESS);
1645     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1646     ASSERT_EQ(request->hdr.instance_id, 0);
1647     ASSERT_EQ(request->hdr.type, PLDM_OEM);
1648     ASSERT_EQ(request->hdr.command, PLDM_FILE_ACK);
1649     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1650     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1651                         sizeof(fileHandleLe)));
1652 }
1653 
TEST(FileAck,testGoodEncodeResponse)1654 TEST(FileAck, testGoodEncodeResponse)
1655 {
1656     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1657         responseMsg{};
1658 
1659     uint8_t completionCode = 0x0;
1660 
1661     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1662     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1663 
1664     auto rc = encode_file_ack_resp(0, completionCode, response);
1665 
1666     ASSERT_EQ(rc, PLDM_SUCCESS);
1667     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1668     ASSERT_EQ(response->hdr.instance_id, 0);
1669     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1670     ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1671     ASSERT_EQ(
1672         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1673 }
1674 
TEST(FileAck,testBadEncodeResponse)1675 TEST(FileAck, testBadEncodeResponse)
1676 {
1677     std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_RESP_BYTES>
1678         responseMsg{};
1679     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1680     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1681 
1682     // completion code is PLDM_ERROR
1683     auto rc = encode_file_ack_resp(0, PLDM_ERROR, response);
1684 
1685     ASSERT_EQ(rc, PLDM_SUCCESS);
1686     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1687     ASSERT_EQ(response->hdr.instance_id, 0);
1688     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1689     ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK);
1690     ASSERT_EQ(response->payload[0], PLDM_ERROR);
1691 
1692     // response is NULL pointer
1693     rc = encode_file_ack_resp(0, PLDM_SUCCESS, NULL);
1694 
1695     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1696 }
1697 
TEST(FileAck,testBadEncodeRequest)1698 TEST(FileAck, testBadEncodeRequest)
1699 {
1700     uint8_t fileType = 0xff;
1701     uint32_t fileHandle = 0;
1702     uint8_t fileStatus = 0;
1703 
1704     // request is NULL pointer
1705     auto rc = encode_file_ack_req(0, fileType, fileHandle, fileStatus, nullptr);
1706 
1707     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1708 }
1709 
TEST(FileAckWithMetadata,testGoodEncodeResponse)1710 TEST(FileAckWithMetadata, testGoodEncodeResponse)
1711 {
1712     std::array<uint8_t,
1713                sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES>
1714         responseMsg{};
1715 
1716     uint8_t completionCode = 0x0;
1717 
1718     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1719     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1720 
1721     auto rc = encode_file_ack_with_meta_data_resp(0, completionCode, response);
1722 
1723     ASSERT_EQ(rc, PLDM_SUCCESS);
1724     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1725     ASSERT_EQ(response->hdr.instance_id, 0);
1726     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1727     ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK_WITH_META_DATA);
1728     ASSERT_EQ(
1729         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1730 }
1731 
TEST(FileAckWithMetadata,testBadEncodeResponse)1732 TEST(FileAckWithMetadata, testBadEncodeResponse)
1733 {
1734     std::array<uint8_t,
1735                sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES>
1736         responseMsg{};
1737     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1738     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1739 
1740     // completion code is PLDM_ERROR
1741     auto rc = encode_file_ack_with_meta_data_resp(0, PLDM_ERROR, response);
1742 
1743     ASSERT_EQ(rc, PLDM_SUCCESS);
1744     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1745     ASSERT_EQ(response->hdr.instance_id, 0);
1746     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1747     ASSERT_EQ(response->hdr.command, PLDM_FILE_ACK_WITH_META_DATA);
1748     ASSERT_EQ(response->payload[0], PLDM_ERROR);
1749 
1750     // response is NULL pointer
1751     rc = encode_file_ack_with_meta_data_resp(0, PLDM_SUCCESS, NULL);
1752 
1753     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1754 }
1755 
TEST(FileAckWithMetadata,testGoodDecodeResponse)1756 TEST(FileAckWithMetadata, testGoodDecodeResponse)
1757 {
1758     std::array<uint8_t,
1759                PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES + sizeof(pldm_msg_hdr)>
1760         responseMsg{};
1761 
1762     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1763     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1764     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
1765     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1766     auto response = reinterpret_cast<pldm_file_ack_with_meta_data_resp*>(
1767         responsePtr->payload);
1768 
1769     // Random value for completion code
1770     uint8_t completionCode = 0x0;
1771 
1772     response->completion_code = completionCode;
1773 
1774     uint8_t retCompletionCode = PLDM_SUCCESS;
1775 
1776     // Invoke decode the read/write file response
1777     auto rc = decode_file_ack_with_meta_data_resp(responsePtr, payload_length,
1778                                                   &retCompletionCode);
1779 
1780     ASSERT_EQ(rc, PLDM_SUCCESS);
1781     ASSERT_EQ(completionCode, retCompletionCode);
1782 }
1783 
TEST(FileAckWithMetadata,testBadDecodeResponse)1784 TEST(FileAckWithMetadata, testBadDecodeResponse)
1785 {
1786     uint8_t completionCode = 0;
1787 
1788     // Request payload message is missing
1789     auto rc = decode_file_ack_with_meta_data_resp(NULL, 0, &completionCode);
1790     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1791 
1792     std::array<uint8_t,
1793                PLDM_FILE_ACK_WITH_META_DATA_RESP_BYTES + sizeof(pldm_msg_hdr)>
1794         responseMsg{};
1795 
1796     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1797     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
1798 
1799     // Payload length is invalid
1800     rc = decode_file_ack_with_meta_data_resp(responsePtr, 0, &completionCode);
1801     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1802 }
1803 
TEST(FileAckWithMetadata,testGoodEncodeRequest)1804 TEST(FileAckWithMetadata, testGoodEncodeRequest)
1805 {
1806     std::array<uint8_t,
1807                sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES>
1808         requestMsg{};
1809 
1810     uint16_t fileType = 0xffff;
1811     uint32_t fileHandle = 0x12345678;
1812     uint8_t fileStatus = 0xff;
1813     uint32_t fileMetaData1 = 0xffffffff;
1814     uint32_t fileMetaData2 = 0xffffffff;
1815     uint32_t fileMetaData3 = 0xffffffff;
1816     uint32_t fileMetaData4 = 0xffffffff;
1817 
1818     uint16_t fileTypeLe = htole16(fileType);
1819     uint32_t fileHandleLe = htole32(fileHandle);
1820     uint32_t fileMetaData1Le = htole32(fileMetaData1);
1821     uint32_t fileMetaData2Le = htole32(fileMetaData2);
1822     uint32_t fileMetaData3Le = htole32(fileMetaData3);
1823     uint32_t fileMetaData4Le = htole32(fileMetaData4);
1824 
1825     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1826     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1827 
1828     auto rc = encode_file_ack_with_meta_data_req(
1829         0, fileType, fileHandle, fileStatus, fileMetaData1, fileMetaData2,
1830         fileMetaData3, fileMetaData4, request);
1831 
1832     ASSERT_EQ(rc, PLDM_SUCCESS);
1833     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
1834     ASSERT_EQ(request->hdr.instance_id, 0);
1835     ASSERT_EQ(request->hdr.type, PLDM_OEM);
1836     ASSERT_EQ(request->hdr.command, PLDM_FILE_ACK_WITH_META_DATA);
1837     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
1838     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
1839                         sizeof(fileHandleLe)));
1840     ASSERT_EQ(
1841         0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
1842                   &fileStatus, sizeof(fileStatus)));
1843     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1844                             sizeof(fileHandleLe) + sizeof(fileStatus),
1845                         &fileMetaData1Le, sizeof(fileMetaData1Le)));
1846     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1847                             sizeof(fileHandleLe) + sizeof(fileStatus) +
1848                             sizeof(fileMetaData1Le),
1849                         &fileMetaData2Le, sizeof(fileMetaData2Le)));
1850 
1851     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1852                             sizeof(fileHandleLe) + sizeof(fileStatus) +
1853                             sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le),
1854                         &fileMetaData3Le, sizeof(fileMetaData3Le)));
1855 
1856     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
1857                             sizeof(fileHandleLe) + sizeof(fileStatus) +
1858                             sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le) +
1859                             sizeof(fileMetaData3Le),
1860                         &fileMetaData4Le, sizeof(fileMetaData4Le)));
1861 }
1862 
TEST(FileAckWithMetadata,testBadEncodeRequest)1863 TEST(FileAckWithMetadata, testBadEncodeRequest)
1864 {
1865     uint8_t fileType = 0xff;
1866     uint32_t fileHandle = 0;
1867     uint8_t fileStatus = 0;
1868     uint32_t fileMetaData1 = 0;
1869     uint32_t fileMetaData2 = 0;
1870     uint32_t fileMetaData3 = 0;
1871     uint32_t fileMetaData4 = 0;
1872 
1873     // request is NULL pointer
1874     auto rc = encode_file_ack_with_meta_data_req(
1875         0, fileType, fileHandle, fileStatus, fileMetaData1, fileMetaData2,
1876         fileMetaData3, fileMetaData4, nullptr);
1877 
1878     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1879 }
1880 
TEST(FileAckWithMetadata,testGoodDecodeRequest)1881 TEST(FileAckWithMetadata, testGoodDecodeRequest)
1882 {
1883     std::array<uint8_t,
1884                sizeof(pldm_msg_hdr) + PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES>
1885         requestMsg{};
1886 
1887     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1888     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1889     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
1890     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1891     auto request = reinterpret_cast<pldm_file_ack_with_meta_data_req*>(
1892         requestPtr->payload);
1893 
1894     uint16_t fileType = 0xffff;
1895     uint32_t fileHandle = 0x12345678;
1896     uint8_t fileStatus = 0xff;
1897     uint32_t fileMetaData1 = 0x12345678;
1898     uint32_t fileMetaData2 = 0x87654321;
1899     uint32_t fileMetaData3 = 0x22121117;
1900     uint32_t fileMetaData4 = 0x12334345;
1901 
1902     request->file_type = htole16(fileType);
1903     request->file_handle = htole32(fileHandle);
1904     request->file_status = fileStatus;
1905     request->file_meta_data_1 = htole32(fileMetaData1);
1906     request->file_meta_data_2 = htole32(fileMetaData2);
1907     request->file_meta_data_3 = htole32(fileMetaData3);
1908     request->file_meta_data_4 = htole32(fileMetaData4);
1909 
1910     uint16_t retFileType = 0xffff;
1911     uint32_t retFileHandle = 0;
1912     uint8_t retFileStatus = 0;
1913     uint32_t retFileMetaData1 = 0;
1914     uint32_t retFileMetaData2 = 0;
1915     uint32_t retFileMetaData3 = 0;
1916     uint32_t retFileMetaData4 = 0;
1917 
1918     auto rc = decode_file_ack_with_meta_data_req(
1919         requestPtr, payload_length, &retFileType, &retFileHandle,
1920         &retFileStatus, &retFileMetaData1, &retFileMetaData2, &retFileMetaData3,
1921         &retFileMetaData4);
1922     ASSERT_EQ(rc, PLDM_SUCCESS);
1923     ASSERT_EQ(fileType, retFileType);
1924     ASSERT_EQ(fileHandle, retFileHandle);
1925     ASSERT_EQ(fileStatus, retFileStatus);
1926     ASSERT_EQ(fileMetaData1, retFileMetaData1);
1927     ASSERT_EQ(fileMetaData2, retFileMetaData2);
1928     ASSERT_EQ(fileMetaData3, retFileMetaData3);
1929     ASSERT_EQ(fileMetaData4, retFileMetaData4);
1930 }
1931 
TEST(FileAckWithMetadata,testBadDecodeRequest)1932 TEST(FileAckWithMetadata, testBadDecodeRequest)
1933 {
1934     uint16_t fileType = 0;
1935     uint32_t fileHandle = 0;
1936     uint8_t fileStatus = 0;
1937     uint32_t fileMetaData1 = 0;
1938     uint32_t fileMetaData2 = 0;
1939     uint32_t fileMetaData3 = 0;
1940     uint32_t fileMetaData4 = 0;
1941 
1942     // Request payload message is missing
1943     auto rc = decode_file_ack_with_meta_data_req(
1944         NULL, 0, &fileType, &fileHandle, &fileStatus, &fileMetaData1,
1945         &fileMetaData2, &fileMetaData3, &fileMetaData4);
1946 
1947     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
1948 
1949     std::array<uint8_t,
1950                PLDM_FILE_ACK_WITH_META_DATA_REQ_BYTES + sizeof(pldm_msg_hdr)>
1951         requestMsg{};
1952 
1953     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1954     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
1955 
1956     // Payload length is invalid
1957     rc = decode_file_ack_with_meta_data_req(
1958         requestPtr, 0, &fileType, &fileHandle, &fileStatus, &fileMetaData1,
1959         &fileMetaData2, &fileMetaData3, &fileMetaData4);
1960     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
1961 }
1962 
TEST(NewFileAvailableWithMetaData,testGoodEncodeResponse)1963 TEST(NewFileAvailableWithMetaData, testGoodEncodeResponse)
1964 {
1965     std::array<uint8_t, sizeof(pldm_msg_hdr) +
1966                             PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES>
1967         responseMsg{};
1968 
1969     uint8_t completionCode = 0x0;
1970 
1971     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1972     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1973 
1974     auto rc = encode_new_file_with_metadata_resp(0, completionCode, response);
1975 
1976     ASSERT_EQ(rc, PLDM_SUCCESS);
1977     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1978     ASSERT_EQ(response->hdr.instance_id, 0);
1979     ASSERT_EQ(response->hdr.type, PLDM_OEM);
1980     ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
1981     ASSERT_EQ(
1982         0, memcmp(response->payload, &completionCode, sizeof(completionCode)));
1983 }
1984 
TEST(NewFileAvailableWithMetaData,testBadEncodeResponse)1985 TEST(NewFileAvailableWithMetaData, testBadEncodeResponse)
1986 {
1987     std::array<uint8_t, sizeof(pldm_msg_hdr) +
1988                             PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES>
1989         responseMsg{};
1990     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1991     pldm_msg* response = reinterpret_cast<pldm_msg*>(responseMsg.data());
1992 
1993     // completion code is PLDM_ERROR
1994     auto rc = encode_new_file_with_metadata_resp(0, PLDM_ERROR, response);
1995 
1996     ASSERT_EQ(rc, PLDM_SUCCESS);
1997     ASSERT_EQ(response->hdr.request, PLDM_RESPONSE);
1998     ASSERT_EQ(response->hdr.instance_id, 0);
1999     ASSERT_EQ(response->hdr.type, PLDM_OEM);
2000     ASSERT_EQ(response->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
2001     ASSERT_EQ(response->payload[0], PLDM_ERROR);
2002 
2003     // response is NULL pointer
2004     rc = encode_new_file_resp(0, PLDM_SUCCESS, NULL);
2005 
2006     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2007 }
2008 
TEST(NewFileAvailableWithMetaData,testGoodDecodeResponse)2009 TEST(NewFileAvailableWithMetaData, testGoodDecodeResponse)
2010 {
2011     std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES +
2012                             sizeof(pldm_msg_hdr)>
2013         responseMsg{};
2014 
2015     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2016     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
2017     size_t payload_length = responseMsg.size() - sizeof(pldm_msg_hdr);
2018     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2019     auto response = reinterpret_cast<pldm_file_ack_with_meta_data_resp*>(
2020         responsePtr->payload);
2021 
2022     // Random value for completion code
2023     uint8_t completionCode = 0x0;
2024 
2025     response->completion_code = completionCode;
2026 
2027     uint8_t retCompletionCode = PLDM_SUCCESS;
2028 
2029     // Invoke decode the read/write file response
2030     auto rc = decode_new_file_with_metadata_resp(responsePtr, payload_length,
2031                                                  &retCompletionCode);
2032 
2033     ASSERT_EQ(rc, PLDM_SUCCESS);
2034     ASSERT_EQ(completionCode, retCompletionCode);
2035 }
2036 
TEST(NewFileAvailableWithMetaData,testBadDecodeResponse)2037 TEST(NewFileAvailableWithMetaData, testBadDecodeResponse)
2038 {
2039     uint8_t completionCode = 0;
2040 
2041     // Request payload message is missing
2042     auto rc = decode_new_file_with_metadata_resp(NULL, 0, &completionCode);
2043     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2044 
2045     std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_RESP_BYTES +
2046                             sizeof(pldm_msg_hdr)>
2047         responseMsg{};
2048 
2049     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2050     auto responsePtr = reinterpret_cast<pldm_msg*>(responseMsg.data());
2051 
2052     // Payload length is invalid
2053     rc = decode_new_file_with_metadata_resp(responsePtr, 0, &completionCode);
2054     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2055 }
2056 
TEST(NewFileAvailableWithMetaData,testGoodEncodeRequest)2057 TEST(NewFileAvailableWithMetaData, testGoodEncodeRequest)
2058 {
2059     std::array<uint8_t, sizeof(pldm_msg_hdr) +
2060                             PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES>
2061         requestMsg{};
2062 
2063     uint16_t fileType = 0xffff;
2064     uint32_t fileHandle = 0x12345678;
2065     uint32_t length = 0x13245768;
2066     uint64_t fileMetaData1 = 0xffffffff;
2067     uint32_t fileMetaData2 = 0xffffffff;
2068     uint32_t fileMetaData3 = 0xffffffff;
2069     uint32_t fileMetaData4 = 0xffffffff;
2070 
2071     uint16_t fileTypeLe = htole16(fileType);
2072     uint32_t fileHandleLe = htole32(fileHandle);
2073     uint64_t lengthLe = htole64(length);
2074     uint32_t fileMetaData1Le = htole32(fileMetaData1);
2075     uint32_t fileMetaData2Le = htole32(fileMetaData2);
2076     uint32_t fileMetaData3Le = htole32(fileMetaData3);
2077     uint32_t fileMetaData4Le = htole32(fileMetaData4);
2078 
2079     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2080     pldm_msg* request = reinterpret_cast<pldm_msg*>(requestMsg.data());
2081 
2082     auto rc = encode_new_file_with_metadata_req(
2083         0, fileType, fileHandle, length, fileMetaData1, fileMetaData2,
2084         fileMetaData3, fileMetaData4, request);
2085 
2086     ASSERT_EQ(rc, PLDM_SUCCESS);
2087     ASSERT_EQ(request->hdr.request, PLDM_REQUEST);
2088     ASSERT_EQ(request->hdr.instance_id, 0);
2089     ASSERT_EQ(request->hdr.type, PLDM_OEM);
2090     ASSERT_EQ(request->hdr.command, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA);
2091     ASSERT_EQ(0, memcmp(request->payload, &fileTypeLe, sizeof(fileTypeLe)));
2092     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe), &fileHandleLe,
2093                         sizeof(fileHandleLe)));
2094     ASSERT_EQ(
2095         0, memcmp(request->payload + sizeof(fileTypeLe) + sizeof(fileHandleLe),
2096                   &lengthLe, sizeof(lengthLe)));
2097 
2098     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
2099                             sizeof(fileHandleLe) + sizeof(lengthLe),
2100                         &fileMetaData1Le, sizeof(fileMetaData1Le)));
2101 
2102     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
2103                             sizeof(fileHandleLe) + sizeof(lengthLe) +
2104                             sizeof(fileMetaData1Le),
2105                         &fileMetaData2Le, sizeof(fileMetaData2Le)));
2106 
2107     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
2108                             sizeof(fileHandleLe) + sizeof(lengthLe) +
2109                             sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le),
2110                         &fileMetaData3Le, sizeof(fileMetaData3Le)));
2111 
2112     ASSERT_EQ(0, memcmp(request->payload + sizeof(fileTypeLe) +
2113                             sizeof(fileHandleLe) + sizeof(lengthLe) +
2114                             sizeof(fileMetaData1Le) + sizeof(fileMetaData2Le) +
2115                             sizeof(fileMetaData3Le),
2116                         &fileMetaData4Le, sizeof(fileMetaData4Le)));
2117 }
2118 
TEST(NewFileAvailableWithMetaData,testBadEncodeRequest)2119 TEST(NewFileAvailableWithMetaData, testBadEncodeRequest)
2120 {
2121     uint8_t fileType = 0xff;
2122     uint32_t fileHandle = 0;
2123     uint32_t length = 0;
2124     uint32_t fileMetaData1 = 0;
2125     uint32_t fileMetaData2 = 0;
2126     uint32_t fileMetaData3 = 0;
2127     uint32_t fileMetaData4 = 0;
2128 
2129     // request is NULL pointer
2130     auto rc = encode_new_file_with_metadata_req(
2131         0, fileType, fileHandle, length, fileMetaData1, fileMetaData2,
2132         fileMetaData3, fileMetaData4, NULL);
2133 
2134     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2135 }
2136 
TEST(NewFileAvailableWithMetaData,testGoodDecodeRequest)2137 TEST(NewFileAvailableWithMetaData, testGoodDecodeRequest)
2138 {
2139     std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES +
2140                             sizeof(pldm_msg_hdr)>
2141         requestMsg{};
2142 
2143     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2144     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
2145     size_t payload_length = requestMsg.size() - sizeof(pldm_msg_hdr);
2146     auto request =
2147         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2148         reinterpret_cast<pldm_new_file_with_metadata_req*>(requestPtr->payload);
2149 
2150     // Random value for fileHandle and length
2151     uint16_t fileType = 0xffff;
2152     uint32_t fileHandle = 0x12345678;
2153     uint64_t length = 0x13245768;
2154     uint32_t fileMetaData1 = 0x12345678;
2155     uint32_t fileMetaData2 = 0x87654321;
2156     uint32_t fileMetaData3 = 0x22121117;
2157     uint32_t fileMetaData4 = 0x12334345;
2158 
2159     request->file_type = htole16(fileType);
2160     request->file_handle = htole32(fileHandle);
2161     request->length = htole64(length);
2162     request->file_meta_data_1 = htole32(fileMetaData1);
2163     request->file_meta_data_2 = htole32(fileMetaData2);
2164     request->file_meta_data_3 = htole32(fileMetaData3);
2165     request->file_meta_data_4 = htole32(fileMetaData4);
2166 
2167     uint16_t retFileType = 0xffff;
2168     uint32_t retFileHandle = 0;
2169     uint64_t retLength = 0;
2170     uint32_t retFileMetaData1 = 0;
2171     uint32_t retFileMetaData2 = 0;
2172     uint32_t retFileMetaData3 = 0;
2173     uint32_t retFileMetaData4 = 0;
2174 
2175     // Invoke decode the read file request
2176     auto rc = decode_new_file_with_metadata_req(
2177         requestPtr, payload_length, &retFileType, &retFileHandle, &retLength,
2178         &retFileMetaData1, &retFileMetaData2, &retFileMetaData3,
2179         &retFileMetaData4);
2180 
2181     ASSERT_EQ(rc, PLDM_SUCCESS);
2182     ASSERT_EQ(fileType, retFileType);
2183     ASSERT_EQ(fileHandle, retFileHandle);
2184     ASSERT_EQ(length, retLength);
2185     ASSERT_EQ(fileMetaData1, retFileMetaData1);
2186     ASSERT_EQ(fileMetaData2, retFileMetaData2);
2187     ASSERT_EQ(fileMetaData3, retFileMetaData3);
2188     ASSERT_EQ(fileMetaData4, retFileMetaData4);
2189 }
2190 
TEST(NewFileAvailableWithMetaData,testBadDecodeRequest)2191 TEST(NewFileAvailableWithMetaData, testBadDecodeRequest)
2192 {
2193     uint16_t fileType = 0;
2194     uint32_t fileHandle = 0;
2195     uint64_t length = 0;
2196     uint32_t fileMetaData1 = 0;
2197     uint32_t fileMetaData2 = 0;
2198     uint32_t fileMetaData3 = 0;
2199     uint32_t fileMetaData4 = 0;
2200 
2201     // Request payload message is missing
2202     auto rc = decode_new_file_with_metadata_req(
2203         NULL, 0, &fileType, &fileHandle, &length, &fileMetaData1,
2204         &fileMetaData2, &fileMetaData3, &fileMetaData4);
2205 
2206     ASSERT_EQ(rc, PLDM_ERROR_INVALID_DATA);
2207 
2208     std::array<uint8_t, PLDM_NEW_FILE_AVAILABLE_WITH_META_DATA_REQ_BYTES +
2209                             sizeof(pldm_msg_hdr)>
2210         requestMsg{};
2211 
2212     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
2213     auto requestPtr = reinterpret_cast<pldm_msg*>(requestMsg.data());
2214 
2215     // Payload length is invalid
2216     rc = decode_new_file_with_metadata_req(
2217         requestPtr, 0, &fileType, &fileHandle, &length, &fileMetaData1,
2218         &fileMetaData2, &fileMetaData3, &fileMetaData4);
2219     ASSERT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);
2220 }
2221