libpldm: Add encode API for TransferComplete response

The FD sends TransferComplete command to the UA once the FD has
transferred all the data for the component image or determines the
transfer has failed. If the TransferResult of the request message
indicates the transfer completed without error then, upon the
successful completion of this command, the FD proceeds to the next
step that verifies the firmware. This implementation works with
DSP0267_1.1.0, DSP0267_1.0.1 and DSP0267_1.0.0.

Tested: Unit tests passed

Signed-off-by: gokulsanker <gokul.sanker.v.g@intel.com>
Change-Id: Ief084641400d5b2b23e346a0e9916091e48f4a37
diff --git a/libpldm/tests/libpldm_firmware_update_test.cpp b/libpldm/tests/libpldm_firmware_update_test.cpp
index 0aacb7b..a276e24 100644
--- a/libpldm/tests/libpldm_firmware_update_test.cpp
+++ b/libpldm/tests/libpldm_firmware_update_test.cpp
@@ -2122,3 +2122,43 @@
     rc = decode_transfer_complete_req(requestMsg, 0, &outTransferResult);

     EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);

 }

+

+TEST(TransferComplete, goodPathEncodeResponse)

+{

+    constexpr uint8_t instanceId = 4;

+    constexpr uint8_t completionCode = PLDM_SUCCESS;

+    constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>

+        outTransferCompleteResponse1{0x04, 0x05, 0x16, 0x00};

+    std::array<uint8_t, hdrSize + sizeof(completionCode)>

+        transferCompleteResponse1{0x00, 0x00, 0x00, 0x00};

+    auto responseMsg1 =

+        reinterpret_cast<pldm_msg*>(transferCompleteResponse1.data());

+    auto rc = encode_transfer_complete_resp(

+        instanceId, completionCode, responseMsg1, sizeof(completionCode));

+    EXPECT_EQ(rc, PLDM_SUCCESS);

+    EXPECT_EQ(transferCompleteResponse1, outTransferCompleteResponse1);

+

+    constexpr std::array<uint8_t, hdrSize + sizeof(completionCode)>

+        outTransferCompleteResponse2{0x04, 0x05, 0x16, 0x87};

+    std::array<uint8_t, hdrSize + sizeof(completionCode)>

+        transferCompleteResponse2{0x00, 0x00, 0x00, 0x00};

+    auto responseMsg2 =

+        reinterpret_cast<pldm_msg*>(transferCompleteResponse2.data());

+    rc = encode_transfer_complete_resp(instanceId,

+                                       PLDM_FWUP_COMMAND_NOT_EXPECTED,

+                                       responseMsg2, sizeof(completionCode));

+    EXPECT_EQ(rc, PLDM_SUCCESS);

+    EXPECT_EQ(transferCompleteResponse2, outTransferCompleteResponse2);

+}

+

+TEST(TransferComplete, errorPathEncodeResponse)

+{

+    std::array<uint8_t, hdrSize> transferCompleteResponse{0x00, 0x00, 0x00};

+    auto responseMsg =

+        reinterpret_cast<pldm_msg*>(transferCompleteResponse.data());

+    auto rc = encode_transfer_complete_resp(0, PLDM_SUCCESS, nullptr, 0);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);

+

+    rc = encode_transfer_complete_resp(0, PLDM_SUCCESS, responseMsg, 0);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);

+}