From 6c300c9c66e7a73bf46c1d9aec166e09ba410383 Mon Sep 17 00:00:00 2001 From: Reto Schneider Date: Wed, 20 Mar 2024 17:18:17 +0100 Subject: [PATCH] tests: net: buf: Improve testing coverage Before this commit, cloning a buffer was tested only for a buffer belonging to a reference counted pool. By using a buffer from a pool that does not support reference counting, the added test ensures that the non-ref-counting code path also works. Signed-off-by: Reto Schneider --- tests/net/buf/src/main.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/net/buf/src/main.c b/tests/net/buf/src/main.c index 1cc8ecc8cfbaad..d9a54f3a28e8bf 100644 --- a/tests/net/buf/src/main.c +++ b/tests/net/buf/src/main.c @@ -408,12 +408,13 @@ ZTEST(net_buf_tests, test_net_buf_multi_frags) "Incorrect frag destroy callback count"); } -ZTEST(net_buf_tests, test_net_buf_clone) +ZTEST(net_buf_tests, test_net_buf_clone_ref_count) { struct net_buf *buf, *clone; destroy_called = 0; + /* Heap pool supports reference counting */ buf = net_buf_alloc_len(&bufs_pool, 74, K_NO_WAIT); zassert_not_null(buf, "Failed to get buffer"); @@ -427,6 +428,30 @@ ZTEST(net_buf_tests, test_net_buf_clone) zassert_equal(destroy_called, 2, "Incorrect destroy callback count"); } +ZTEST(net_buf_tests, test_net_buf_clone_no_ref_count) +{ + struct net_buf *buf, *clone; + const uint8_t data[3] = {0x11, 0x22, 0x33}; + + destroy_called = 0; + + /* Fixed pool does not support reference counting */ + buf = net_buf_alloc_len(&fixed_pool, 3, K_NO_WAIT); + zassert_not_null(buf, "Failed to get buffer"); + net_buf_add_mem(buf, data, sizeof(data)); + + clone = net_buf_clone(buf, K_NO_WAIT); + zassert_not_null(clone, "Failed to get clone buffer"); + zassert_not_equal(buf->data, clone->data, + "No reference counting support, different pointers expected"); + zassert_mem_equal(clone->data, data, sizeof(data)); + + net_buf_unref(buf); + net_buf_unref(clone); + + zassert_equal(destroy_called, 2, "Incorrect destroy callback count"); +} + ZTEST(net_buf_tests, test_net_buf_fixed_pool) { struct net_buf *buf;