libpldm: Add encode API for CancelUpdateComponent request

Update agent send this command to FD/FDP during firmware component
transfer process. The FD/FDP, upon receiving this command shall stop
sending RequestFirmwareData commands to the UA, and cancel the
current component update procedure. The FD/FDP controller shall
transition to the READY XFER state of update mode and be ready to
accept another UpdateComponent command. 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: I95563734c9d22c435a59b697fb784b7acfa897a7
diff --git a/libpldm/tests/libpldm_firmware_update_test.cpp b/libpldm/tests/libpldm_firmware_update_test.cpp
index c8e2954..22ee122 100644
--- a/libpldm/tests/libpldm_firmware_update_test.cpp
+++ b/libpldm/tests/libpldm_firmware_update_test.cpp
@@ -2714,3 +2714,31 @@
         &progressPercent, &reasonCode, &updateOptionFlagsEnabled);

     EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);

 }

+

+TEST(CancelUpdateComponent, goodPathEncodeRequest)

+{

+    constexpr uint8_t instanceId = 9;

+    std::array<uint8_t, hdrSize> request{};

+    auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());

+

+    auto rc = encode_cancel_update_component_req(

+        instanceId, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);

+    EXPECT_EQ(rc, PLDM_SUCCESS);

+

+    constexpr std::array<uint8_t, hdrSize> outRequest{0x89, 0x05, 0x1C};

+    EXPECT_EQ(request, outRequest);

+}

+

+TEST(CancelUpdateComponent, errorPathEncodeRequest)

+{

+    std::array<uint8_t, hdrSize + sizeof(uint8_t)> request{};

+    auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());

+

+    auto rc = encode_cancel_update_component_req(

+        0, nullptr, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);

+

+    rc = encode_cancel_update_component_req(

+        0, requestMsg, PLDM_CANCEL_UPDATE_COMPONENT_REQ_BYTES + 1);

+    EXPECT_EQ(rc, PLDM_ERROR_INVALID_LENGTH);

+}