1 /*
2  * Copyright (c) 2015 Intel Corporation
3  * Copyright (c) 2023 Codecoup
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/fff.h>
9 #include <zephyr/kernel.h>
10 #include <zephyr/net/buf.h>
11 
12 DEFINE_FFF_GLOBALS;
13 
14 NET_BUF_SIMPLE_DEFINE_STATIC(buf, 16);
15 static const uint8_t le16[2] = { 0x02, 0x01 };
16 static const uint8_t be16[2] = { 0x01, 0x02 };
17 static const uint8_t le24[3] = { 0x03, 0x02, 0x01 };
18 static const uint8_t be24[3] = { 0x01, 0x02, 0x03 };
19 static const uint8_t le32[4] = { 0x04, 0x03, 0x02, 0x01 };
20 static const uint8_t be32[4] = { 0x01, 0x02, 0x03, 0x04 };
21 static const uint8_t le48[6] = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
22 static const uint8_t be48[6] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
23 static const uint8_t le64[8] = { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
24 static const uint8_t be64[8] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
25 static const uint16_t u16 = 0x0102;
26 static const uint32_t u24 = 0x010203;
27 static const uint32_t u32 = 0x01020304;
28 static const uint64_t u48 = 0x010203040506;
29 static const uint64_t u64 = 0x0102030405060708;
30 
net_buf_simple_test_suite_before(void * f)31 static void net_buf_simple_test_suite_before(void *f)
32 {
33 	net_buf_simple_reset(&buf);
34 }
35 
36 ZTEST_SUITE(net_buf_simple_test_suite, NULL, NULL,
37 	    net_buf_simple_test_suite_before, NULL, NULL);
38 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_clone)39 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_clone)
40 {
41 	struct net_buf_simple clone;
42 
43 	net_buf_simple_clone(&buf, &clone);
44 
45 	zassert_equal(buf.data, clone.data, "Incorrect clone data pointer");
46 }
47 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_le16)48 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le16)
49 {
50 	net_buf_simple_add_mem(&buf, &le16, sizeof(le16));
51 
52 	zassert_equal(u16, net_buf_simple_pull_le16(&buf),
53 		      "Invalid 16 bits byte order");
54 }
55 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_be16)56 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be16)
57 {
58 	net_buf_simple_add_mem(&buf, &be16, sizeof(be16));
59 
60 	zassert_equal(u16, net_buf_simple_pull_be16(&buf),
61 		      "Invalid 16 bits byte order");
62 }
63 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_le16)64 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le16)
65 {
66 	net_buf_simple_add_le16(&buf, u16);
67 
68 	zassert_mem_equal(le16, net_buf_simple_pull_mem(&buf, sizeof(le16)),
69 			  sizeof(le16), "Invalid 16 bits byte order");
70 }
71 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_be16)72 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be16)
73 {
74 	net_buf_simple_add_be16(&buf, u16);
75 
76 	zassert_mem_equal(be16, net_buf_simple_pull_mem(&buf, sizeof(be16)),
77 			  sizeof(be16), "Invalid 16 bits byte order");
78 }
79 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_le24)80 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le24)
81 {
82 	net_buf_simple_add_mem(&buf, &le24, sizeof(le24));
83 
84 	zassert_equal(u24, net_buf_simple_pull_le24(&buf),
85 		      "Invalid 24 bits byte order");
86 }
87 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_be24)88 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be24)
89 {
90 	net_buf_simple_add_mem(&buf, &be24, sizeof(be24));
91 
92 	zassert_equal(u24, net_buf_simple_pull_be24(&buf),
93 		      "Invalid 24 bits byte order");
94 }
95 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_le24)96 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le24)
97 {
98 	net_buf_simple_add_le24(&buf, u24);
99 
100 	zassert_mem_equal(le24, net_buf_simple_pull_mem(&buf, sizeof(le24)),
101 			  sizeof(le24), "Invalid 24 bits byte order");
102 }
103 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_be24)104 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be24)
105 {
106 	net_buf_simple_add_be24(&buf, u24);
107 
108 	zassert_mem_equal(be24, net_buf_simple_pull_mem(&buf, sizeof(be24)),
109 			  sizeof(be24), "Invalid 24 bits byte order");
110 }
111 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_le32)112 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le32)
113 {
114 	net_buf_simple_add_mem(&buf, &le32, sizeof(le32));
115 
116 	zassert_equal(u32, net_buf_simple_pull_le32(&buf),
117 		      "Invalid 32 bits byte order");
118 }
119 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_be32)120 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be32)
121 {
122 	net_buf_simple_add_mem(&buf, &be32, sizeof(be32));
123 
124 	zassert_equal(u32, net_buf_simple_pull_be32(&buf),
125 		      "Invalid 32 bits byte order");
126 }
127 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_le32)128 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le32)
129 {
130 	net_buf_simple_add_le32(&buf, u32);
131 
132 	zassert_mem_equal(le32, net_buf_simple_pull_mem(&buf, sizeof(le32)),
133 			  sizeof(le32), "Invalid 32 bits byte order");
134 }
135 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_be32)136 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be32)
137 {
138 	net_buf_simple_add_be32(&buf, u32);
139 
140 	zassert_mem_equal(be32, net_buf_simple_pull_mem(&buf, sizeof(be32)),
141 			  sizeof(be32), "Invalid 32 bits byte order");
142 }
143 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_le48)144 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le48)
145 {
146 	net_buf_simple_add_mem(&buf, &le48, sizeof(le48));
147 
148 	zassert_equal(u48, net_buf_simple_pull_le48(&buf),
149 		      "Invalid 48 bits byte order");
150 }
151 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_be48)152 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be48)
153 {
154 	net_buf_simple_add_mem(&buf, &be48, sizeof(be48));
155 
156 	zassert_equal(u48, net_buf_simple_pull_be48(&buf),
157 		      "Invalid 48 bits byte order");
158 }
159 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_le48)160 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le48)
161 {
162 	net_buf_simple_add_le48(&buf, u48);
163 
164 	zassert_mem_equal(le48, net_buf_simple_pull_mem(&buf, sizeof(le48)),
165 			  sizeof(le48), "Invalid 48 bits byte order");
166 }
167 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_be48)168 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be48)
169 {
170 	net_buf_simple_add_be48(&buf, u48);
171 
172 	zassert_mem_equal(be48, net_buf_simple_pull_mem(&buf, sizeof(be48)),
173 			  sizeof(be48), "Invalid 48 bits byte order");
174 }
175 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_le64)176 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_le64)
177 {
178 	net_buf_simple_add_mem(&buf, &le64, sizeof(le64));
179 
180 	zassert_equal(u64, net_buf_simple_pull_le64(&buf),
181 		      "Invalid 64 bits byte order");
182 }
183 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_pull_be64)184 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_pull_be64)
185 {
186 	net_buf_simple_add_mem(&buf, &be64, sizeof(be64));
187 
188 	zassert_equal(u64, net_buf_simple_pull_be64(&buf),
189 		      "Invalid 64 bits byte order");
190 }
191 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_le64)192 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_le64)
193 {
194 	net_buf_simple_add_le64(&buf, u64);
195 
196 	zassert_mem_equal(le64, net_buf_simple_pull_mem(&buf, sizeof(le64)),
197 			  sizeof(le64), "Invalid 64 bits byte order");
198 }
199 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_add_be64)200 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_add_be64)
201 {
202 	net_buf_simple_add_be64(&buf, u64);
203 
204 	zassert_mem_equal(be64, net_buf_simple_pull_mem(&buf, sizeof(be64)),
205 			  sizeof(be64), "Invalid 64 bits byte order");
206 }
207 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_le16)208 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le16)
209 {
210 	net_buf_simple_reserve(&buf, 16);
211 
212 	net_buf_simple_push_mem(&buf, &le16, sizeof(le16));
213 
214 	zassert_equal(u16, net_buf_simple_remove_le16(&buf),
215 		      "Invalid 16 bits byte order");
216 }
217 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_be16)218 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be16)
219 {
220 	net_buf_simple_reserve(&buf, 16);
221 
222 	net_buf_simple_push_mem(&buf, &be16, sizeof(be16));
223 
224 	zassert_equal(u16, net_buf_simple_remove_be16(&buf),
225 		      "Invalid 16 bits byte order");
226 }
227 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_le16)228 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le16)
229 {
230 	net_buf_simple_reserve(&buf, 16);
231 
232 	net_buf_simple_push_le16(&buf, u16);
233 
234 	zassert_mem_equal(le16, net_buf_simple_remove_mem(&buf, sizeof(le16)),
235 			  sizeof(le16),  "Invalid 16 bits byte order");
236 }
237 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_be16)238 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be16)
239 {
240 	net_buf_simple_reserve(&buf, 16);
241 
242 	net_buf_simple_push_be16(&buf, u16);
243 
244 	zassert_mem_equal(be16, net_buf_simple_remove_mem(&buf, sizeof(be16)),
245 			  sizeof(be16),  "Invalid 16 bits byte order");
246 }
247 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_le24)248 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le24)
249 {
250 	net_buf_simple_reserve(&buf, 16);
251 
252 	net_buf_simple_push_mem(&buf, &le24, sizeof(le24));
253 
254 	zassert_equal(u24, net_buf_simple_remove_le24(&buf),
255 		      "Invalid 24 bits byte order");
256 }
257 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_be24)258 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be24)
259 {
260 	net_buf_simple_reserve(&buf, 16);
261 
262 	net_buf_simple_push_mem(&buf, &be24, sizeof(be24));
263 
264 	zassert_equal(u24, net_buf_simple_remove_be24(&buf),
265 		      "Invalid 24 bits byte order");
266 }
267 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_le24)268 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le24)
269 {
270 	net_buf_simple_reserve(&buf, 16);
271 
272 	net_buf_simple_push_le24(&buf, u24);
273 
274 	zassert_mem_equal(le24, net_buf_simple_remove_mem(&buf, sizeof(le24)),
275 			  sizeof(le24),  "Invalid 24 bits byte order");
276 }
277 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_be24)278 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be24)
279 {
280 	net_buf_simple_reserve(&buf, 16);
281 
282 	net_buf_simple_push_be24(&buf, u24);
283 
284 	zassert_mem_equal(be24, net_buf_simple_remove_mem(&buf, sizeof(be24)),
285 			  sizeof(be24),  "Invalid 24 bits byte order");
286 }
287 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_le32)288 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le32)
289 {
290 	net_buf_simple_reserve(&buf, 16);
291 
292 	net_buf_simple_push_mem(&buf, &le32, sizeof(le32));
293 
294 	zassert_equal(u32, net_buf_simple_remove_le32(&buf),
295 		      "Invalid 32 bits byte order");
296 }
297 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_be32)298 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be32)
299 {
300 	net_buf_simple_reserve(&buf, 16);
301 
302 	net_buf_simple_push_mem(&buf, &be32, sizeof(be32));
303 
304 	zassert_equal(u32, net_buf_simple_remove_be32(&buf),
305 		      "Invalid 32 bits byte order");
306 }
307 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_le32)308 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le32)
309 {
310 	net_buf_simple_reserve(&buf, 16);
311 
312 	net_buf_simple_push_le32(&buf, u32);
313 
314 	zassert_mem_equal(le32, net_buf_simple_remove_mem(&buf, sizeof(le32)),
315 			  sizeof(le32), "Invalid 32 bits byte order");
316 }
317 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_be32)318 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be32)
319 {
320 	net_buf_simple_reserve(&buf, 16);
321 
322 	net_buf_simple_push_be32(&buf, u32);
323 
324 	zassert_mem_equal(be32, net_buf_simple_remove_mem(&buf, sizeof(be32)),
325 			  sizeof(be32), "Invalid 32 bits byte order");
326 }
327 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_le48)328 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le48)
329 {
330 	net_buf_simple_reserve(&buf, 16);
331 
332 	net_buf_simple_push_mem(&buf, &le48, sizeof(le48));
333 
334 	zassert_equal(u48, net_buf_simple_remove_le48(&buf),
335 		      "Invalid 48 bits byte order");
336 }
337 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_be48)338 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be48)
339 {
340 	net_buf_simple_reserve(&buf, 16);
341 
342 	net_buf_simple_push_mem(&buf, &be48, sizeof(be48));
343 
344 	zassert_equal(u48, net_buf_simple_remove_be48(&buf),
345 		      "Invalid 48 bits byte order");
346 }
347 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_le48)348 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le48)
349 {
350 	net_buf_simple_reserve(&buf, 16);
351 
352 	net_buf_simple_push_le48(&buf, u48);
353 
354 	zassert_mem_equal(le48, net_buf_simple_remove_mem(&buf, sizeof(le48)),
355 			  sizeof(le48),  "Invalid 48 bits byte order");
356 }
357 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_be48)358 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be48)
359 {
360 	net_buf_simple_reserve(&buf, 16);
361 
362 	net_buf_simple_push_be48(&buf, u48);
363 
364 	zassert_mem_equal(be48, net_buf_simple_remove_mem(&buf, sizeof(be48)),
365 			  sizeof(be48),  "Invalid 48 bits byte order");
366 }
367 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_le64)368 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_le64)
369 {
370 	net_buf_simple_reserve(&buf, 16);
371 
372 	net_buf_simple_push_mem(&buf, &le64, sizeof(le64));
373 
374 	zassert_equal(u64, net_buf_simple_remove_le64(&buf),
375 		      "Invalid 64 bits byte order");
376 }
377 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_remove_be64)378 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_remove_be64)
379 {
380 	net_buf_simple_reserve(&buf, 16);
381 
382 	net_buf_simple_push_mem(&buf, &be64, sizeof(be64));
383 
384 	zassert_equal(u64, net_buf_simple_remove_be64(&buf),
385 		      "Invalid 64 bits byte order");
386 }
387 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_le64)388 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_le64)
389 {
390 	net_buf_simple_reserve(&buf, 16);
391 
392 	net_buf_simple_push_le64(&buf, u64);
393 
394 	zassert_mem_equal(le64, net_buf_simple_remove_mem(&buf, sizeof(le64)),
395 			  sizeof(le64), "Invalid 64 bits byte order");
396 }
397 
ZTEST(net_buf_simple_test_suite,test_net_buf_simple_push_be64)398 ZTEST(net_buf_simple_test_suite, test_net_buf_simple_push_be64)
399 {
400 	net_buf_simple_reserve(&buf, 16);
401 
402 	net_buf_simple_push_be64(&buf, u64);
403 
404 	zassert_mem_equal(be64, net_buf_simple_remove_mem(&buf, sizeof(be64)),
405 			  sizeof(be64), "Invalid 64 bits byte order");
406 }
407