transport: free un-wanted responses in pldm_transport_send_recv_msg()

mctp-demux broadcasts the PLDM response messages to all of mctp
socket instances. That means the GetSensorReading response from one
instance (pldmd) can be received by the other instance (pldmtool)
which is waiting for the response of GetPDR request message. When the
gap time between the two requests of pldmtool is long enough the
number of GetSensorReading response messages can be more than 32. That
means the instance ID of new GetPDR command in pldmtool can equal with
the used and free one in GetSensorReading. The
`pldm_transport_send_recv_msg()` will detect the reponse of
GetSensorReading as the response of the GetPDR request.

To prevent this unexpected behavior the `pldm_transport_send_recv_msg()`
should free the un-wanted responses before sending new PLDM request. The
serialised message to be sent must already encode an allocated instance
ID, which by the specification must not be reused before expiry.
Therefore after the socket is drained, any subsequent response
containing the request's instance ID must be the response matching the
request.

Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
[AJ: Add a test, massage the commit message]
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I6c684bdaea2ba5d96b24ecd3c72e846c644fb16d
diff --git a/tests/transport.cpp b/tests/transport.cpp
index 9f00986..fcf2d0a 100644
--- a/tests/transport.cpp
+++ b/tests/transport.cpp
@@ -216,3 +216,55 @@
     pldm_transport_test_destroy(test);
 }
 #endif
+
+#ifdef LIBPLDM_API_TESTING
+TEST(Transport, send_recv_drain_one_unwanted)
+{
+    uint8_t unwanted[] = {0x01, 0x00, 0x01, 0x01};
+    uint8_t req[] = {0x81, 0x00, 0x01, 0x01};
+    uint8_t resp[] = {0x01, 0x00, 0x01, 0x00};
+    const struct pldm_transport_test_descriptor seq[] = {
+        {
+            .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV,
+            .recv_msg =
+                {
+                    .src = 2,
+                    .msg = unwanted,
+                    .len = sizeof(unwanted),
+                },
+        },
+        {
+            .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND,
+            .send_msg =
+                {
+                    .dst = 1,
+                    .msg = req,
+                    .len = sizeof(req),
+                },
+        },
+        {
+            .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV,
+            .recv_msg =
+                {
+                    .src = 1,
+                    .msg = resp,
+                    .len = sizeof(resp),
+                },
+        },
+    };
+    struct pldm_transport_test* test = NULL;
+    struct pldm_transport* ctx;
+    size_t len;
+    void* msg;
+    int rc;
+
+    EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0);
+    ctx = pldm_transport_test_core(test);
+    rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len);
+    EXPECT_EQ(rc, PLDM_SUCCESS);
+    EXPECT_NE(memcmp(msg, unwanted, len), 0);
+    EXPECT_EQ(memcmp(msg, resp, len), 0);
+    free(msg);
+    pldm_transport_test_destroy(test);
+}
+#endif