Skip to content

Commit fd0b148

Browse files
jukkarnashif
authored andcommitted
tests: net_buf: Add alignment tests
Make sure that net_buf pools created by NET_BUF_POOL_VAR_ALIGN_DEFINE are aligned according to user needs. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
1 parent 45a0a49 commit fd0b148

File tree

1 file changed

+75
-0
lines changed
  • tests/lib/net_buf/buf/src

1 file changed

+75
-0
lines changed

tests/lib/net_buf/buf/src/main.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,29 @@ static int destroy_called;
6767
static void buf_destroy(struct net_buf *buf);
6868
static void fixed_destroy(struct net_buf *buf);
6969
static void var_destroy(struct net_buf *buf);
70+
static void var_destroy_aligned(struct net_buf *buf);
71+
static void var_destroy_aligned_small(struct net_buf *buf);
72+
73+
#define VAR_POOL_ALIGN 8
74+
#define VAR_POOL_ALIGN_SMALL 4
75+
#define VAR_POOL_DATA_COUNT 4
76+
#define VAR_POOL_DATA_SIZE (VAR_POOL_DATA_COUNT * 64)
7077

7178
NET_BUF_POOL_HEAP_DEFINE(bufs_pool, 10, USER_DATA_HEAP, buf_destroy);
7279
NET_BUF_POOL_FIXED_DEFINE(fixed_pool, 10, FIXED_BUFFER_SIZE, USER_DATA_FIXED, fixed_destroy);
7380
NET_BUF_POOL_VAR_DEFINE(var_pool, 10, 1024, USER_DATA_VAR, var_destroy);
7481

82+
/* Two pools, one with aligned to 8 bytes and one with aligned to 4 bytes
83+
* buffers. The aligned pools are used to test that the alignment works
84+
* correctly.
85+
*/
86+
NET_BUF_POOL_VAR_ALIGN_DEFINE(var_pool_aligned, VAR_POOL_DATA_COUNT,
87+
VAR_POOL_DATA_SIZE, USER_DATA_VAR,
88+
var_destroy_aligned, VAR_POOL_ALIGN);
89+
NET_BUF_POOL_VAR_ALIGN_DEFINE(var_pool_aligned_small, VAR_POOL_DATA_COUNT,
90+
VAR_POOL_DATA_SIZE, USER_DATA_VAR,
91+
var_destroy_aligned_small, VAR_POOL_ALIGN_SMALL);
92+
7593
static void buf_destroy(struct net_buf *buf)
7694
{
7795
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
@@ -99,6 +117,24 @@ static void var_destroy(struct net_buf *buf)
99117
net_buf_destroy(buf);
100118
}
101119

120+
static void var_destroy_aligned(struct net_buf *buf)
121+
{
122+
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
123+
124+
destroy_called++;
125+
zassert_equal(pool, &var_pool_aligned, "Invalid free pointer in buffer");
126+
net_buf_destroy(buf);
127+
}
128+
129+
static void var_destroy_aligned_small(struct net_buf *buf)
130+
{
131+
struct net_buf_pool *pool = net_buf_pool_get(buf->pool_id);
132+
133+
destroy_called++;
134+
zassert_equal(pool, &var_pool_aligned_small, "Invalid free pointer in buffer");
135+
net_buf_destroy(buf);
136+
}
137+
102138
static const char example_data[] = "0123456789"
103139
"abcdefghijklmnopqrstuvxyz"
104140
"!#¤%&/()=?";
@@ -1084,4 +1120,43 @@ ZTEST(net_buf_tests, test_net_buf_linearize)
10841120
zassert_equal(destroy_called, 4, "Incorrect destroy callback count");
10851121
}
10861122

1123+
ZTEST(net_buf_tests, test_net_buf_var_pool_aligned)
1124+
{
1125+
struct net_buf *buf1, *buf2, *buf3;
1126+
1127+
destroy_called = 0;
1128+
1129+
zassert_equal(var_pool_aligned.alloc->alignment, VAR_POOL_ALIGN,
1130+
"Expected %d-byte alignment for variable pool",
1131+
VAR_POOL_ALIGN);
1132+
1133+
buf1 = net_buf_alloc_len(&var_pool_aligned, 20, K_NO_WAIT);
1134+
zassert_not_null(buf1, "Failed to get buffer");
1135+
1136+
zassert_true(IS_ALIGNED((uintptr_t)buf1->data, VAR_POOL_ALIGN),
1137+
"Buffer data pointer is not aligned to %d bytes",
1138+
VAR_POOL_ALIGN);
1139+
1140+
buf2 = net_buf_alloc_len(&var_pool_aligned_small, 29, K_NO_WAIT);
1141+
zassert_not_null(buf2, "Failed to get buffer");
1142+
1143+
zassert_true(IS_ALIGNED((uintptr_t)buf2->data, VAR_POOL_ALIGN_SMALL),
1144+
"Buffer data pointer is not aligned to %d bytes",
1145+
VAR_POOL_ALIGN_SMALL);
1146+
1147+
buf3 = net_buf_alloc_len(&var_pool_aligned, VAR_POOL_ALIGN_SMALL, K_NO_WAIT);
1148+
zassert_is_null(buf3,
1149+
"Managed to get buffer even if alignment %d is larger than size %d",
1150+
VAR_POOL_ALIGN, VAR_POOL_ALIGN_SMALL);
1151+
1152+
buf3 = net_buf_alloc_len(&var_pool_aligned, VAR_POOL_ALIGN, K_NO_WAIT);
1153+
zassert_not_null(buf3, "Failed to get buffer");
1154+
1155+
net_buf_unref(buf1);
1156+
net_buf_unref(buf2);
1157+
net_buf_unref(buf3);
1158+
1159+
zassert_equal(destroy_called, 3, "Incorrect destroy callback count");
1160+
}
1161+
10871162
ZTEST_SUITE(net_buf_tests, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)