Skip to content

Commit 982fc41

Browse files
kderdaMaureenHelm
authored andcommitted
tests: net: buf: add tests for data matching
Add new test case for veryfing matching between net_bufs' contents and a given data buffer. Signed-off-by: Konrad Derda <[email protected]>
1 parent a5b868d commit 982fc41

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

tests/net/buf/src/main.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define USER_DATA_HEAP 4
2121
#define USER_DATA_FIXED 0
2222
#define USER_DATA_VAR 63
23+
#define FIXED_BUFFER_SIZE 128
2324

2425
struct bt_data {
2526
void *hci_sync;
@@ -68,7 +69,7 @@ static void fixed_destroy(struct net_buf *buf);
6869
static void var_destroy(struct net_buf *buf);
6970

7071
NET_BUF_POOL_HEAP_DEFINE(bufs_pool, 10, USER_DATA_HEAP, buf_destroy);
71-
NET_BUF_POOL_FIXED_DEFINE(fixed_pool, 10, 128, USER_DATA_FIXED, fixed_destroy);
72+
NET_BUF_POOL_FIXED_DEFINE(fixed_pool, 10, FIXED_BUFFER_SIZE, USER_DATA_FIXED, fixed_destroy);
7273
NET_BUF_POOL_VAR_DEFINE(var_pool, 10, 1024, USER_DATA_VAR, var_destroy);
7374

7475
static void buf_destroy(struct net_buf *buf)
@@ -724,4 +725,76 @@ ZTEST(net_buf_tests, test_net_buf_user_data)
724725
net_buf_unref(buf);
725726
}
726727

728+
ZTEST(net_buf_tests, test_net_buf_comparison)
729+
{
730+
struct net_buf *buf;
731+
size_t written;
732+
size_t offset;
733+
size_t to_compare;
734+
size_t res;
735+
uint8_t data[FIXED_BUFFER_SIZE * 2];
736+
737+
/* Fill data buffer */
738+
for (int i = 0; i < sizeof(data); ++i) {
739+
data[i] = (uint8_t)i;
740+
}
741+
742+
/* Allocate a single net_buf */
743+
buf = net_buf_alloc(&fixed_pool, K_NO_WAIT);
744+
zassert_not_null(buf, "Failed to get buffer");
745+
746+
written = net_buf_append_bytes(buf, buf->size, data, K_NO_WAIT, NULL, NULL);
747+
zassert_equal(written, buf->size, "Failed to fill the buffer");
748+
zassert_equal(buf->frags, NULL, "Additional buffer allocated");
749+
750+
/* Compare the whole buffer */
751+
res = net_buf_data_match(buf, 0, data, buf->size);
752+
zassert_equal(res, buf->size, "Whole net_buf comparison failed");
753+
754+
/* Compare from the offset */
755+
offset = buf->size / 2;
756+
to_compare = written - offset;
757+
758+
res = net_buf_data_match(buf, offset, &data[offset], to_compare);
759+
zassert_equal(res, to_compare, "Comparison with offset failed");
760+
761+
/* Write more data (it allocates more buffers) */
762+
written = net_buf_append_bytes(buf, sizeof(data) - written, &data[buf->size], K_NO_WAIT,
763+
NULL, NULL);
764+
zassert_true(buf->frags, "Failed to allocate an additional net_buf");
765+
766+
/* Compare whole data with buffers' content */
767+
res = net_buf_data_match(buf, 0, data, sizeof(data));
768+
zassert_equal(res, sizeof(data), "Failed to compare data with multiple buffers");
769+
770+
/* Compare data with offset at the edge between two fragments */
771+
offset = buf->size - (buf->size / 2);
772+
res = net_buf_data_match(buf, offset, &data[offset], buf->size);
773+
zassert_equal(res, buf->size, "Failed to compare bytes within two buffers with offset");
774+
775+
/* Compare data with partial matching - change the data in the middle */
776+
data[sizeof(data) / 2] += 1;
777+
res = net_buf_data_match(buf, 0, data, sizeof(data));
778+
zassert_equal(res, sizeof(data) / 2, "Partial matching failed");
779+
780+
/* No buffer - expect 0 matching bytes */
781+
res = net_buf_data_match(NULL, 0, data, sizeof(data));
782+
zassert_equal(res, 0, "Matching without a buffer must fail");
783+
784+
/* No data - expect 0 matching bytes */
785+
res = net_buf_data_match(buf, 0, NULL, sizeof(data));
786+
zassert_equal(res, 0, "Matching without data must fail");
787+
788+
/* Too high offset - expect 0 matching bytes */
789+
res = net_buf_data_match(buf, FIXED_BUFFER_SIZE * 2, data, sizeof(data));
790+
zassert_equal(res, 0, "Matching with too high offset must fail");
791+
792+
/* Try to match more bytes than are in buffers - expect only partial match */
793+
offset = (FIXED_BUFFER_SIZE * 2) - 8;
794+
res = net_buf_data_match(buf, offset, &data[offset], 16);
795+
zassert_equal(res, 8, "Reaching out of bounds must return a partial match");
796+
797+
net_buf_unref(buf);
798+
}
799+
727800
ZTEST_SUITE(net_buf_tests, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)