Skip to content

Commit

Permalink
tests: net: buf: Improve testing coverage
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rettichschnidi authored and dleach02 committed Mar 23, 2024
1 parent 09abd85 commit 6c300c9
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion tests/net/buf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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;
Expand Down

0 comments on commit 6c300c9

Please sign in to comment.