|
20 | 20 | #define USER_DATA_HEAP 4
|
21 | 21 | #define USER_DATA_FIXED 0
|
22 | 22 | #define USER_DATA_VAR 63
|
| 23 | +#define FIXED_BUFFER_SIZE 128 |
23 | 24 |
|
24 | 25 | struct bt_data {
|
25 | 26 | void *hci_sync;
|
@@ -68,7 +69,7 @@ static void fixed_destroy(struct net_buf *buf);
|
68 | 69 | static void var_destroy(struct net_buf *buf);
|
69 | 70 |
|
70 | 71 | 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); |
72 | 73 | NET_BUF_POOL_VAR_DEFINE(var_pool, 10, 1024, USER_DATA_VAR, var_destroy);
|
73 | 74 |
|
74 | 75 | static void buf_destroy(struct net_buf *buf)
|
@@ -724,4 +725,76 @@ ZTEST(net_buf_tests, test_net_buf_user_data)
|
724 | 725 | net_buf_unref(buf);
|
725 | 726 | }
|
726 | 727 |
|
| 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 | + |
727 | 800 | ZTEST_SUITE(net_buf_tests, NULL, NULL, NULL, NULL, NULL);
|
0 commit comments