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