msgbuf: Add pldm_msgbuf_consumed()

pldm_msgbuf_consumed() only returns PLDM_SUCCESS if a message buffer has
been exactly exhausted - all bytes read and no overflow has occurred.

The decode_* functions have a mix of behaviour, some lenient like the
behaviour provided by pldm_msgbuf_validate() and others strict like
pldm_msgbuf_consumed().

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: If058ed3748dcc7cb366f4c31344c9bbfba1c5939
diff --git a/tests/msgbuf.cpp b/tests/msgbuf.cpp
index ad7fe25..1df195e 100644
--- a/tests/msgbuf.cpp
+++ b/tests/msgbuf.cpp
@@ -354,3 +354,38 @@
               PLDM_SUCCESS);
     ASSERT_EQ(pldm_msgbuf_destroy(ctx), PLDM_ERROR_INVALID_LENGTH);
 }
+
+TEST(msgbuf, consumed_under)
+{
+    struct pldm_msgbuf _ctx;
+    struct pldm_msgbuf* ctx = &_ctx;
+    uint8_t buf[1] = {};
+
+    ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
+}
+
+TEST(msgbuf, consumed_exact)
+{
+    struct pldm_msgbuf _ctx;
+    struct pldm_msgbuf* ctx = &_ctx;
+    uint8_t buf[1] = {};
+    uint8_t val;
+
+    ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, consumed_over)
+{
+    struct pldm_msgbuf _ctx;
+    struct pldm_msgbuf* ctx = &_ctx;
+    uint8_t buf[1] = {};
+    uint8_t val[2];
+
+    ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_extract_uint8(ctx, &val[0]), PLDM_SUCCESS);
+    EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
+    EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
+}