msgbuf: Add insert and span APIs
pldm_msgbuf_insert() is generic APIs to insert data to a pointer.
It automatically calls the correct insertor API for the type of the
packing data.
pldm_msgbuf_span_required() spans the input number of bytes from
buffer to the input double pointer.
pldm_msgbuf_span_remaining() extracts the remaining number of bytes
from buffer to the input double pointer.
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: Ide7dbf735f69a8288e6f39a0b7b5c33aad38a98e
diff --git a/src/msgbuf.h b/src/msgbuf.h
index 662ba46..f5d4a54 100644
--- a/src/msgbuf.h
+++ b/src/msgbuf.h
@@ -15,8 +15,18 @@
#include <string.h>
#include <sys/types.h>
+/*
+ * Fix up C11's _Static_assert() vs C++'s static_assert().
+ *
+ * Can we please have nice things for once.
+ */
+#ifdef __cplusplus
+// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
+#define _Static_assert(...) static_assert(__VA_ARGS__)
+#endif
+
struct pldm_msgbuf {
- const uint8_t *cursor;
+ uint8_t *cursor;
ssize_t remaining;
};
@@ -366,6 +376,213 @@
_Generic((*(dst)), uint8_t \
: pldm_msgbuf_extract_array_uint8)(ctx, dst, count)
+static inline int pldm_msgbuf_insert_uint32(struct pldm_msgbuf *ctx,
+ const uint32_t src)
+{
+ uint32_t val = htole32(src);
+
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &val, sizeof(val));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_insert_uint16(struct pldm_msgbuf *ctx,
+ const uint16_t src)
+{
+ uint16_t val = htole16(src);
+
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &val, sizeof(val));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_insert_uint8(struct pldm_msgbuf *ctx,
+ const uint8_t src)
+{
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &src, sizeof(src));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_insert_int32(struct pldm_msgbuf *ctx,
+ const int32_t src)
+{
+ int32_t val = htole32(src);
+
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &val, sizeof(val));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_insert_int16(struct pldm_msgbuf *ctx,
+ const int16_t src)
+{
+ int16_t val = htole16(src);
+
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &val, sizeof(val));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_insert_int8(struct pldm_msgbuf *ctx,
+ const int8_t src)
+{
+ if (!ctx || !ctx->cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ ctx->remaining -= sizeof(src);
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, &src, sizeof(src));
+ ctx->cursor += sizeof(src);
+
+ return PLDM_SUCCESS;
+}
+
+#define pldm_msgbuf_insert(dst, src) \
+ _Generic((src), uint8_t \
+ : pldm_msgbuf_insert_uint8, int8_t \
+ : pldm_msgbuf_insert_int8, uint16_t \
+ : pldm_msgbuf_insert_uint16, int16_t \
+ : pldm_msgbuf_insert_int16, uint32_t \
+ : pldm_msgbuf_insert_uint32, int32_t \
+ : pldm_msgbuf_insert_int32)(dst, src)
+
+static inline int pldm_msgbuf_insert_array_uint8(struct pldm_msgbuf *ctx,
+ const uint8_t *src,
+ size_t count)
+{
+ size_t len;
+ if (!ctx || !ctx->cursor || !src) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (!count) {
+ return PLDM_SUCCESS;
+ }
+
+ len = sizeof(*src) * count;
+ if (len > SSIZE_MAX) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ ctx->remaining -= (ssize_t)len;
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ memcpy(ctx->cursor, src, len);
+ ctx->cursor += len;
+
+ return PLDM_SUCCESS;
+}
+
+#define pldm_msgbuf_insert_array(dst, src, count) \
+ _Generic((*(src)), uint8_t \
+ : pldm_msgbuf_insert_array_uint8)(dst, src, count)
+
+static inline int pldm_msgbuf_span_required(struct pldm_msgbuf *ctx,
+ size_t required, void **cursor)
+{
+ if (!ctx || !ctx->cursor || !cursor || *cursor) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ if (required > SSIZE_MAX) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ ctx->remaining -= (ssize_t)required;
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ *cursor = ctx->cursor;
+ ctx->cursor += required;
+
+ return PLDM_SUCCESS;
+}
+
+static inline int pldm_msgbuf_span_remaining(struct pldm_msgbuf *ctx,
+ void **cursor, size_t *len)
+{
+ if (!ctx || !ctx->cursor || !cursor || *cursor || !len) {
+ return PLDM_ERROR_INVALID_DATA;
+ }
+
+ assert(ctx->remaining >= 0);
+ if (ctx->remaining < 0) {
+ return PLDM_ERROR_INVALID_LENGTH;
+ }
+
+ *cursor = ctx->cursor;
+ ctx->cursor += ctx->remaining;
+ *len = ctx->remaining;
+ ctx->remaining = 0;
+
+ return PLDM_SUCCESS;
+}
#ifdef __cplusplus
}
#endif
diff --git a/tests/msgbuf.cpp b/tests/msgbuf.cpp
index 1df195e..6623fe0 100644
--- a/tests/msgbuf.cpp
+++ b/tests/msgbuf.cpp
@@ -9,16 +9,6 @@
#define NDEBUG 1
#endif
-/*
- * Fix up C11's _Static_assert() vs C++'s static_assert().
- *
- * Can we please have nice things for once.
- */
-#ifdef __cplusplus
-// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
-#define _Static_assert(...) static_assert(__VA_ARGS__)
-#endif
-
#include "msgbuf.h"
TEST(msgbuf, init_bad_ctx)
@@ -389,3 +379,345 @@
EXPECT_NE(pldm_msgbuf_extract_uint8(ctx, &val[1]), PLDM_SUCCESS);
EXPECT_EQ(pldm_msgbuf_destroy_consumed(ctx), PLDM_ERROR_INVALID_LENGTH);
}
+
+TEST(msgbuf, pldm_msgbuf_insert_int32_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int32_t src = -12345;
+ int32_t checkVal = 0;
+ uint8_t buf[sizeof(int32_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_int32(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_int32(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_int32_bad)
+{
+ int32_t src = -12345;
+
+ auto rc = pldm_msgbuf_insert_int32(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint32_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint32_t src = 0xf1223344;
+ uint32_t checkVal = 0;
+ uint8_t buf[sizeof(uint32_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_uint32(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint32(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint32_bad)
+{
+ uint32_t src = 0xf1223344;
+
+ auto rc = pldm_msgbuf_insert_uint32(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint16_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint16_t src = 0xf344;
+ uint16_t checkVal = 0;
+ uint8_t buf[sizeof(uint16_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_uint16(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint16_bad)
+{
+ uint16_t src = 0xf344;
+
+ auto rc = pldm_msgbuf_insert_uint16(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_int16_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int16_t src = -12;
+ int16_t checkVal = 0;
+ uint8_t buf[sizeof(int16_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_int16(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_int16(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_int16_bad)
+{
+ int16_t src = -12;
+
+ auto rc = pldm_msgbuf_insert_int16(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint8_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src = 0xf4;
+ uint8_t checkVal = 0;
+ uint8_t buf[sizeof(uint8_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_uint8(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint8(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_uint8_bad)
+{
+ uint8_t src = 0xf4;
+
+ auto rc = pldm_msgbuf_insert_uint8(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_int8_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int8_t src = -4;
+ int8_t checkVal = 0;
+ uint8_t buf[sizeof(int8_t)] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_int8(ctx, src), PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_int8(ctxExtract, &checkVal), PLDM_SUCCESS);
+
+ EXPECT_EQ(src, checkVal);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_int8_bad)
+{
+ int8_t src = -4;
+
+ auto rc = pldm_msgbuf_insert_int8(NULL, src);
+
+ EXPECT_EQ(rc, PLDM_ERROR_INVALID_DATA);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_array_uint8_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t buf[6] = {};
+ uint8_t retBuff[6] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
+ PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(
+ pldm_msgbuf_extract_array_uint8(ctxExtract, retBuff, sizeof(retBuff)),
+ PLDM_SUCCESS);
+
+ EXPECT_EQ(memcmp(src, retBuff, sizeof(retBuff)), 0);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_insert_array_uint8_bad)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t buf[6] = {};
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(NULL, src, sizeof(src)),
+ PLDM_ERROR_INVALID_DATA);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, NULL, sizeof(src)),
+ PLDM_ERROR_INVALID_DATA);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_span_required_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t buf[6] = {0};
+ const size_t required = 4;
+ uint8_t expectData[required] = {0x44, 0x55, 0x66, 0x77};
+ uint16_t testVal;
+ uint8_t* retBuff = NULL;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
+ PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, (void**)&retBuff),
+ PLDM_SUCCESS);
+
+ EXPECT_EQ(memcmp(expectData, retBuff, required), 0);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_span_required_bad)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t buf[6] = {0};
+ const size_t required = 4;
+ uint16_t testVal;
+ [[maybe_unused]] uint8_t* retBuff = NULL;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
+ PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_span_required(ctxExtract, required, NULL),
+ PLDM_ERROR_INVALID_DATA);
+ EXPECT_EQ(pldm_msgbuf_span_required(NULL, required, (void**)&retBuff),
+ PLDM_ERROR_INVALID_DATA);
+
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_span_remaining_good)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
+ uint8_t buf[8] = {0};
+ uint16_t testVal;
+ uint8_t expectData[6] = {0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
+ size_t remaining;
+ uint8_t* retBuff = NULL;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
+ PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
+ EXPECT_EQ(
+ pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, &remaining),
+ PLDM_SUCCESS);
+
+ EXPECT_EQ(remaining, sizeof(expectData));
+ EXPECT_EQ(memcmp(expectData, retBuff, remaining), 0);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
+
+TEST(msgbuf, pldm_msgbuf_span_remaining_bad)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[8] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99};
+ uint8_t buf[8] = {0};
+ uint16_t testVal;
+ size_t remaining;
+ uint8_t* retBuff = NULL;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_insert_array_uint8(ctx, src, sizeof(src)),
+ PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ ASSERT_EQ(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_extract_uint16(ctxExtract, &testVal), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_span_remaining(NULL, (void**)&retBuff, &remaining),
+ PLDM_ERROR_INVALID_DATA);
+ EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, NULL, &remaining),
+ PLDM_ERROR_INVALID_DATA);
+ EXPECT_EQ(pldm_msgbuf_span_remaining(ctxExtract, (void**)&retBuff, NULL),
+ PLDM_ERROR_INVALID_DATA);
+
+ EXPECT_EQ(pldm_msgbuf_destroy(ctxExtract), PLDM_SUCCESS);
+ EXPECT_EQ(pldm_msgbuf_destroy(ctx), PLDM_SUCCESS);
+}
\ No newline at end of file
diff --git a/tests/msgbuf_generic.c b/tests/msgbuf_generic.c
index d9d292a..d79d442 100644
--- a/tests/msgbuf_generic.c
+++ b/tests/msgbuf_generic.c
@@ -143,6 +143,161 @@
expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
}
+static void test_msgbuf_insert_generic_int32(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int32_t src = -12345;
+ int32_t checkVal = 0;
+ uint8_t buf[sizeof(int32_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_generic_uint32(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint32_t src = 0xf1223344;
+ uint32_t checkVal = 0;
+ uint8_t buf[sizeof(uint32_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_generic_uint16(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint16_t src = 0xf344;
+ uint16_t checkVal = 0;
+ uint8_t buf[sizeof(uint16_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_generic_int16(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int16_t src = -12;
+ int16_t checkVal = 0;
+ uint8_t buf[sizeof(int16_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(uint16_t)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_generic_uint8(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src = 0xf4;
+ uint8_t checkVal = 0;
+ uint8_t buf[sizeof(uint8_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_generic_int8(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ int8_t src = -4;
+ int8_t checkVal = 0;
+ uint8_t buf[sizeof(int8_t)] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert(ctx, src) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract(ctxExtract, &checkVal) == PLDM_SUCCESS);
+
+ expect(src == checkVal);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
+static void test_msgbuf_insert_array_generic_uint8(void)
+{
+ struct pldm_msgbuf _ctx;
+ struct pldm_msgbuf* ctx = &_ctx;
+ uint8_t src[6] = {0x11, 0x22, 0x44, 0x55, 0x66, 0x77};
+ uint8_t buf[6] = {0};
+ uint8_t retBuff[6] = {0};
+
+ expect(pldm_msgbuf_init(ctx, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_insert_array(ctx, src, sizeof(src)) == PLDM_SUCCESS);
+
+ struct pldm_msgbuf _ctxExtract;
+ struct pldm_msgbuf* ctxExtract = &_ctxExtract;
+
+ expect(pldm_msgbuf_init(ctxExtract, 0, buf, sizeof(buf)) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_extract_array(ctxExtract, retBuff, sizeof(retBuff)) ==
+ PLDM_SUCCESS);
+
+ expect(memcmp(src, retBuff, sizeof(retBuff)) == 0);
+ expect(pldm_msgbuf_destroy(ctxExtract) == PLDM_SUCCESS);
+ expect(pldm_msgbuf_destroy(ctx) == PLDM_SUCCESS);
+}
+
typedef void (*testfn)(void);
static const testfn tests[] = {test_msgbuf_extract_generic_uint8,
@@ -153,6 +308,13 @@
test_msgbuf_extract_generic_int32,
test_msgbuf_extract_generic_real32,
test_msgbuf_extract_array_generic_uint8,
+ test_msgbuf_insert_generic_uint8,
+ test_msgbuf_insert_generic_int8,
+ test_msgbuf_insert_generic_uint16,
+ test_msgbuf_insert_generic_int16,
+ test_msgbuf_insert_generic_uint32,
+ test_msgbuf_insert_generic_int32,
+ test_msgbuf_insert_array_generic_uint8,
NULL};
int main(void)