From 65c32810cb7f46b63b0bfbc7ad38257f399e3360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 2 Jul 2013 21:22:38 +0200 Subject: [PATCH] Add test for uc_mbuf-copy_data --- include/ucore/mbuf.h | 5 +---- src/mbuf.c | 22 +++++++++++++++++++++- test/test_mbuf.c | 25 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/include/ucore/mbuf.h b/include/ucore/mbuf.h index ebfc8aa..406f951 100644 --- a/include/ucore/mbuf.h +++ b/include/ucore/mbuf.h @@ -43,7 +43,7 @@ struct MBuf *uc_mbuf_new_inline_hr(uint32_t len, uint32_t headroom); #define uc_mbuf_new_inline(len) uc_mbuf_new_inline_hr((len), 0) void uc_mbuf_free(struct MBuf *mbuf); -struct MBuf *uc_mbuf_copy(struct MBuf *mbuf); +struct MBuf *uc_mbuf_copy_data(struct MBuf *mbuf); void uc_mbuf_init(struct MBuf *mbuf, uint8_t *buf, uint32_t len, uint32_t headroom, uint32_t flags); void uc_mbuf_zero(struct MBuf *mbuf); @@ -70,9 +70,6 @@ uint8_t *uc_mbuf_push(struct MBuf *mbuf, uint32_t size); uint8_t *uc_mbuf_pull(struct MBuf *mbuf, uint32_t size); -struct MBuf *uc_uc_mbuf_new_hr(uint32_t len, uint32_t headroom); -struct MBuf *uc_uc_mbuf_new_inline_hr(uint32_t len, uint32_t headroom); - #undef UC_INLINE #endif diff --git a/src/mbuf.c b/src/mbuf.c index 80a551e..97c1c6e 100644 --- a/src/mbuf.c +++ b/src/mbuf.c @@ -5,6 +5,7 @@ uint8_t *uc_mbuf_put(struct MBuf *mbuf, uint32_t size) { uint8_t *data = mbuf->tail; + if (uc_mbuf_tailroom(mbuf) < size) { return NULL; } @@ -28,6 +29,7 @@ uint8_t *uc_mbuf_push(struct MBuf *mbuf, uint32_t size) uint8_t *uc_mbuf_pull(struct MBuf *mbuf, uint32_t size) { uint8_t *data = mbuf->head; + if (uc_mbuf_len(mbuf) < size) { return NULL; } @@ -37,7 +39,6 @@ uint8_t *uc_mbuf_pull(struct MBuf *mbuf, uint32_t size) return data; } - void uc_mbuf_init(struct MBuf *mbuf, uint8_t *buf, uint32_t len, uint32_t headroom, uint32_t flags) { mbuf->bufstart = buf; @@ -97,5 +98,24 @@ void uc_mbuf_zero(struct MBuf *mbuf) memset(mbuf->bufstart, 0, mbuf->allocated); } +struct MBuf *uc_mbuf_copy_data(struct MBuf *mbuf) +{ + uint32_t len; + struct MBuf *copy; + uint8_t *data; + + len = uc_mbuf_len(mbuf); + + copy = uc_mbuf_new_hr(len, uc_mbuf_headroom(mbuf)); + if (copy == NULL) { + return NULL; + } + + data = uc_mbuf_put(copy, len); + memcpy(data, mbuf->head, len); + + return copy; +} + #undef UC_INLINE diff --git a/test/test_mbuf.c b/test/test_mbuf.c index b465110..98b5bdd 100644 --- a/test/test_mbuf.c +++ b/test/test_mbuf.c @@ -174,6 +174,30 @@ START_TEST (test_mbuf_pull_fail) END_TEST +START_TEST (test_mbuf_copy) + struct MBuf *mbuf; + uint8_t *data1; + struct MBuf *copy; + + mbuf = uc_mbuf_new_inline_hr(10,5); + fail_if(mbuf == NULL); + + data1 = uc_mbuf_put(mbuf, 10); + fail_if(data1 == NULL); + + strcpy((char*)data1, "123456789"); + + copy = uc_mbuf_copy_data(mbuf); + fail_if(copy == NULL); + fail_if(uc_mbuf_len(copy) != 10); + fail_if(uc_mbuf_headroom(copy) != 5); + fail_if(memcmp(mbuf->head, copy->head, 10) != 0); + + uc_mbuf_free(mbuf); + uc_mbuf_free(copy); + +END_TEST + Suite *mbuf_suite(void) @@ -189,6 +213,7 @@ Suite *mbuf_suite(void) tcase_add_test(tc, test_mbuf_push_fail); tcase_add_test(tc, test_mbuf_pull); tcase_add_test(tc, test_mbuf_pull_fail); + tcase_add_test(tc, test_mbuf_copy); suite_add_tcase(s, tc);