1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 
8 #include <zephyr/ztest.h>
9 
10 #include <zephyr/sys/byteorder.h>
11 
12 /**
13  * @addtogroup kernel_common_tests
14  * @{
15  */
16 /**
17  * @brief Test swapping for memory contents
18  *
19  * @details Verify the functionality provided by
20  * sys_memcpy_swap()
21  *
22  * @see sys_memcpy_swap()
23  */
ZTEST(byteorder,test_byteorder_memcpy_swap)24 ZTEST(byteorder, test_byteorder_memcpy_swap)
25 {
26 	uint8_t buf_orig[8] = { 0x00, 0x01, 0x02, 0x03,
27 			     0x04, 0x05, 0x06, 0x07 };
28 	uint8_t buf_chk[8] = { 0x07, 0x06, 0x05, 0x04,
29 			    0x03, 0x02, 0x01, 0x00 };
30 	uint8_t buf_dst[8] = { 0 };
31 
32 	sys_memcpy_swap(buf_dst, buf_orig, 8);
33 	zassert_true((memcmp(buf_dst, buf_chk, 8) == 0),
34 		     "Swap memcpy failed");
35 
36 	sys_memcpy_swap(buf_dst, buf_chk, 8);
37 	zassert_true((memcmp(buf_dst, buf_orig, 8) == 0),
38 		     "Swap memcpy failed");
39 }
40 
41 /**
42  * @brief Test sys_mem_swap() functionality
43  *
44  * @details Test if sys_mem_swap() reverses the contents
45  *
46  * @see sys_mem_swap()
47  */
ZTEST(byteorder,test_byteorder_mem_swap)48 ZTEST(byteorder, test_byteorder_mem_swap)
49 {
50 	uint8_t buf_orig_1[8] = { 0x00, 0x01, 0x02, 0x03,
51 			       0x04, 0x05, 0x06, 0x07 };
52 	uint8_t buf_orig_2[11] = { 0x00, 0x01, 0x02, 0x03,
53 				0x04, 0x05, 0x06, 0x07,
54 				0x08, 0x09, 0xa0 };
55 	uint8_t buf_chk_1[8] = { 0x07, 0x06, 0x05, 0x04,
56 			      0x03, 0x02, 0x01, 0x00 };
57 	uint8_t buf_chk_2[11] = { 0xa0, 0x09, 0x08, 0x07,
58 			       0x06, 0x05, 0x04, 0x03,
59 			       0x02, 0x01, 0x00 };
60 
61 	sys_mem_swap(buf_orig_1, 8);
62 	zassert_true((memcmp(buf_orig_1, buf_chk_1, 8) == 0),
63 		     "Swapping buffer failed");
64 
65 	sys_mem_swap(buf_orig_2, 11);
66 	zassert_true((memcmp(buf_orig_2, buf_chk_2, 11) == 0),
67 		     "Swapping buffer failed");
68 }
69 
70 /**
71  * @brief Test sys_get_be64() functionality
72  *
73  * @details Test if sys_get_be64() correctly handles endianness.
74  *
75  * @see sys_get_be64()
76  */
ZTEST(byteorder,test_sys_get_be64)77 ZTEST(byteorder, test_sys_get_be64)
78 {
79 	uint64_t val = 0xf0e1d2c3b4a59687, tmp;
80 	uint8_t buf[] = {
81 		0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87
82 	};
83 
84 	tmp = sys_get_be64(buf);
85 
86 	zassert_equal(tmp, val, "sys_get_be64() failed");
87 }
88 
89 /**
90  * @brief Test sys_put_be64() functionality
91  *
92  * @details Test if sys_put_be64() correctly handles endianness.
93  *
94  * @see sys_put_be64()
95  */
ZTEST(byteorder,test_sys_put_be64)96 ZTEST(byteorder, test_sys_put_be64)
97 {
98 	uint64_t val = 0xf0e1d2c3b4a59687;
99 	uint8_t buf[] = {
100 		0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87
101 	};
102 	uint8_t tmp[sizeof(uint64_t)];
103 
104 	sys_put_be64(val, tmp);
105 
106 	zassert_mem_equal(tmp, buf, sizeof(uint64_t), "sys_put_be64() failed");
107 }
108 
109 /**
110  * @brief Test sys_get_be40() functionality
111  *
112  * @details Test if sys_get_be40() correctly handles endianness.
113  *
114  * @see sys_get_be40()
115  */
ZTEST(byteorder,test_sys_get_be40)116 ZTEST(byteorder, test_sys_get_be40)
117 {
118 	uint64_t val = 0xf0e1d2c3b4, tmp;
119 	uint8_t buf[] = {0xf0, 0xe1, 0xd2, 0xc3, 0xb4};
120 
121 	tmp = sys_get_be40(buf);
122 
123 	zassert_equal(tmp, val, "sys_get_be64() failed");
124 }
125 
126 /**
127  * @brief Test sys_put_be40() functionality
128  *
129  * @details Test if sys_put_be40() correctly handles endianness.
130  *
131  * @see sys_put_be40()
132  */
ZTEST(byteorder,test_sys_put_be40)133 ZTEST(byteorder, test_sys_put_be40)
134 {
135 	uint64_t val = 0xf0e1d2c3b4;
136 	uint8_t buf[] = {0xf0, 0xe1, 0xd2, 0xc3, 0xb4};
137 	uint8_t tmp[sizeof(buf)];
138 
139 	sys_put_be40(val, tmp);
140 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_be40() failed");
141 }
142 
143 /**
144  * @brief Test sys_get_be48() functionality
145  *
146  * @details Test if sys_get_be48() correctly handles endianness.
147  *
148  * @see sys_get_be48()
149  */
ZTEST(byteorder,test_sys_get_be48)150 ZTEST(byteorder, test_sys_get_be48)
151 {
152 	uint64_t val = 0xf0e1d2c3b4a5, tmp;
153 	uint8_t buf[] = {
154 		0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5
155 	};
156 
157 	tmp = sys_get_be48(buf);
158 
159 	zassert_equal(tmp, val, "sys_get_be64() failed");
160 }
161 
162 /**
163  * @brief Test sys_put_be48() functionality
164  *
165  * @details Test if sys_put_be48() correctly handles endianness.
166  *
167  * @see sys_put_be48()
168  */
ZTEST(byteorder,test_sys_put_be48)169 ZTEST(byteorder, test_sys_put_be48)
170 {
171 	uint64_t val = 0xf0e1d2c3b4a5;
172 	uint8_t buf[] = {
173 		0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5
174 	};
175 	uint8_t tmp[sizeof(buf)];
176 
177 	sys_put_be48(val, tmp);
178 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_be48() failed");
179 }
180 
181 /**
182  * @brief Test sys_get_be32() functionality
183  *
184  * @details Test if sys_get_be32() correctly handles endianness.
185  *
186  * @see sys_get_be32()
187  */
ZTEST(byteorder,test_sys_get_be32)188 ZTEST(byteorder, test_sys_get_be32)
189 {
190 	uint32_t val = 0xf0e1d2c3, tmp;
191 	uint8_t buf[] = {
192 		0xf0, 0xe1, 0xd2, 0xc3
193 	};
194 
195 	tmp = sys_get_be32(buf);
196 
197 	zassert_equal(tmp, val, "sys_get_be32() failed");
198 }
199 
200 /**
201  * @brief Test sys_put_be32() functionality
202  *
203  * @details Test if sys_put_be32() correctly handles endianness.
204  *
205  * @see sys_put_be32()
206  */
ZTEST(byteorder,test_sys_put_be32)207 ZTEST(byteorder, test_sys_put_be32)
208 {
209 	uint64_t val = 0xf0e1d2c3;
210 	uint8_t buf[] = {
211 		0xf0, 0xe1, 0xd2, 0xc3
212 	};
213 	uint8_t tmp[sizeof(uint32_t)];
214 
215 	sys_put_be32(val, tmp);
216 
217 	zassert_mem_equal(tmp, buf, sizeof(uint32_t), "sys_put_be32() failed");
218 }
219 
220 /**
221  * @brief Test sys_get_be24() functionality
222  *
223  * @details Test if sys_get_be24() correctly handles endianness.
224  *
225  * @see sys_get_be24()
226  */
ZTEST(byteorder,test_sys_get_be24)227 ZTEST(byteorder, test_sys_get_be24)
228 {
229 	uint32_t val = 0xf0e1d2, tmp;
230 	uint8_t buf[] = {
231 		0xf0, 0xe1, 0xd2
232 	};
233 
234 	tmp = sys_get_be24(buf);
235 
236 	zassert_equal(tmp, val, "sys_get_be24() failed");
237 }
238 
239 /**
240  * @brief Test sys_put_be24() functionality
241  *
242  * @details Test if sys_put_be24() correctly handles endianness.
243  *
244  * @see sys_put_be24()
245  */
ZTEST(byteorder,test_sys_put_be24)246 ZTEST(byteorder, test_sys_put_be24)
247 {
248 	uint64_t val = 0xf0e1d2;
249 	uint8_t buf[] = {
250 		0xf0, 0xe1, 0xd2
251 	};
252 	uint8_t tmp[sizeof(buf)];
253 
254 	sys_put_be24(val, tmp);
255 
256 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_be24() failed");
257 }
258 
259 /**
260  * @brief Test sys_get_be16() functionality
261  *
262  * @details Test if sys_get_be16() correctly handles endianness.
263  *
264  * @see sys_get_be16()
265  */
ZTEST(byteorder,test_sys_get_be16)266 ZTEST(byteorder, test_sys_get_be16)
267 {
268 	uint32_t val = 0xf0e1, tmp;
269 	uint8_t buf[] = {
270 		0xf0, 0xe1
271 	};
272 
273 	tmp = sys_get_be16(buf);
274 
275 	zassert_equal(tmp, val, "sys_get_be16() failed");
276 }
277 
278 /**
279  * @brief Test sys_put_be16() functionality
280  *
281  * @details Test if sys_put_be16() correctly handles endianness.
282  *
283  * @see sys_put_be16()
284  */
ZTEST(byteorder,test_sys_put_be16)285 ZTEST(byteorder, test_sys_put_be16)
286 {
287 	uint64_t val = 0xf0e1;
288 	uint8_t buf[] = {
289 		0xf0, 0xe1
290 	};
291 	uint8_t tmp[sizeof(uint16_t)];
292 
293 	sys_put_be16(val, tmp);
294 
295 	zassert_mem_equal(tmp, buf, sizeof(uint16_t), "sys_put_be16() failed");
296 }
297 
298 /**
299  * @brief Test sys_get_le16() functionality
300  *
301  * @details Test if sys_get_le16() correctly handles endianness.
302  *
303  * @see sys_get_le16()
304  */
ZTEST(byteorder,test_sys_get_le16)305 ZTEST(byteorder, test_sys_get_le16)
306 {
307 	uint32_t val = 0xf0e1, tmp;
308 	uint8_t buf[] = {
309 		0xe1, 0xf0
310 	};
311 
312 	tmp = sys_get_le16(buf);
313 
314 	zassert_equal(tmp, val, "sys_get_le16() failed");
315 }
316 
317 /**
318  * @brief Test sys_put_le16() functionality
319  *
320  * @details Test if sys_put_le16() correctly handles endianness.
321  *
322  * @see sys_put_le16()
323  */
ZTEST(byteorder,test_sys_put_le16)324 ZTEST(byteorder, test_sys_put_le16)
325 {
326 	uint64_t val = 0xf0e1;
327 	uint8_t buf[] = {
328 		0xe1, 0xf0
329 	};
330 	uint8_t tmp[sizeof(uint16_t)];
331 
332 	sys_put_le16(val, tmp);
333 
334 	zassert_mem_equal(tmp, buf, sizeof(uint16_t), "sys_put_le16() failed");
335 }
336 
337 /**
338  * @brief Test sys_get_le24() functionality
339  *
340  * @details Test if sys_get_le24() correctly handles endianness.
341  *
342  * @see sys_get_le24()
343  */
ZTEST(byteorder,test_sys_get_le24)344 ZTEST(byteorder, test_sys_get_le24)
345 {
346 	uint32_t val = 0xf0e1d2, tmp;
347 	uint8_t buf[] = {
348 		0xd2, 0xe1, 0xf0
349 	};
350 
351 	tmp = sys_get_le24(buf);
352 
353 	zassert_equal(tmp, val, "sys_get_le24() failed");
354 }
355 
356 /**
357  * @brief Test sys_put_le24() functionality
358  *
359  * @details Test if sys_put_le24() correctly handles endianness.
360  *
361  * @see sys_put_le24()
362  */
ZTEST(byteorder,test_sys_put_le24)363 ZTEST(byteorder, test_sys_put_le24)
364 {
365 	uint64_t val = 0xf0e1d2;
366 	uint8_t buf[] = {
367 		0xd2, 0xe1, 0xf0
368 	};
369 	uint8_t tmp[sizeof(uint32_t)];
370 
371 	sys_put_le24(val, tmp);
372 
373 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_le24() failed");
374 }
375 
376 /**
377  * @brief Test sys_get_le32() functionality
378  *
379  * @details Test if sys_get_le32() correctly handles endianness.
380  *
381  * @see sys_get_le32()
382  */
ZTEST(byteorder,test_sys_get_le32)383 ZTEST(byteorder, test_sys_get_le32)
384 {
385 	uint32_t val = 0xf0e1d2c3, tmp;
386 	uint8_t buf[] = {
387 		0xc3, 0xd2, 0xe1, 0xf0
388 	};
389 
390 	tmp = sys_get_le32(buf);
391 
392 	zassert_equal(tmp, val, "sys_get_le32() failed");
393 }
394 
395 /**
396  * @brief Test sys_put_le32() functionality
397  *
398  * @details Test if sys_put_le32() correctly handles endianness.
399  *
400  * @see sys_put_le32()
401  */
ZTEST(byteorder,test_sys_put_le32)402 ZTEST(byteorder, test_sys_put_le32)
403 {
404 	uint64_t val = 0xf0e1d2c3;
405 	uint8_t buf[] = {
406 		0xc3, 0xd2, 0xe1, 0xf0
407 	};
408 	uint8_t tmp[sizeof(uint32_t)];
409 
410 	sys_put_le32(val, tmp);
411 
412 	zassert_mem_equal(tmp, buf, sizeof(uint32_t), "sys_put_le32() failed");
413 }
414 
415 /**
416  * @brief Test sys_get_le40() functionality
417  *
418  * @details Test if sys_get_le40() correctly handles endianness.
419  *
420  * @see sys_get_le40()
421  */
ZTEST(byteorder,test_sys_get_le40)422 ZTEST(byteorder, test_sys_get_le40)
423 {
424 	uint64_t val = 0xf0e1d2c3b4, tmp;
425 	uint8_t buf[] = {0xb4, 0xc3, 0xd2, 0xe1, 0xf0};
426 
427 	tmp = sys_get_le40(buf);
428 
429 	zassert_equal(tmp, val, "sys_get_le40() failed");
430 }
431 
432 /**
433  * @brief Test sys_put_le40() functionality
434  *
435  * @details Test if sys_put_le40() correctly handles endianness.
436  *
437  * @see sys_put_le40()
438  */
ZTEST(byteorder,test_sys_put_le40)439 ZTEST(byteorder, test_sys_put_le40)
440 {
441 	uint64_t val = 0xf0e1d2c3b4;
442 	uint8_t buf[] = {0xb4, 0xc3, 0xd2, 0xe1, 0xf0};
443 	uint8_t tmp[sizeof(uint64_t)];
444 
445 	sys_put_le40(val, tmp);
446 
447 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_le40() failed");
448 }
449 
450 /**
451  * @brief Test sys_get_le48() functionality
452  *
453  * @details Test if sys_get_le48() correctly handles endianness.
454  *
455  * @see sys_get_le48()
456  */
ZTEST(byteorder,test_sys_get_le48)457 ZTEST(byteorder, test_sys_get_le48)
458 {
459 	uint64_t val = 0xf0e1d2c3b4a5, tmp;
460 	uint8_t buf[] = {
461 		0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0
462 	};
463 
464 	tmp = sys_get_le48(buf);
465 
466 	zassert_equal(tmp, val, "sys_get_le48() failed");
467 }
468 
469 /**
470  * @brief Test sys_put_le48() functionality
471  *
472  * @details Test if sys_put_le48() correctly handles endianness.
473  *
474  * @see sys_put_le48()
475  */
ZTEST(byteorder,test_sys_put_le48)476 ZTEST(byteorder, test_sys_put_le48)
477 {
478 	uint64_t val = 0xf0e1d2c3b4a5;
479 	uint8_t buf[] = {
480 		0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0
481 	};
482 	uint8_t tmp[sizeof(uint64_t)];
483 
484 	sys_put_le48(val, tmp);
485 
486 	zassert_mem_equal(tmp, buf, sizeof(buf), "sys_put_le48() failed");
487 }
488 
489 /**
490  * @brief Test sys_get_le64() functionality
491  *
492  * @details Test if sys_get_le64() correctly handles endianness.
493  *
494  * @see sys_get_le64()
495  */
ZTEST(byteorder,test_sys_get_le64)496 ZTEST(byteorder, test_sys_get_le64)
497 {
498 	uint64_t val = 0xf0e1d2c3b4a59687, tmp;
499 	uint8_t buf[] = {
500 		0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0
501 	};
502 
503 	tmp = sys_get_le64(buf);
504 
505 	zassert_equal(tmp, val, "sys_get_le64() failed");
506 }
507 
508 /**
509  * @brief Test sys_put_le64() functionality
510  *
511  * @details Test if sys_put_le64() correctly handles endianness.
512  *
513  * @see sys_put_le64()
514  */
ZTEST(byteorder,test_sys_put_le64)515 ZTEST(byteorder, test_sys_put_le64)
516 {
517 	uint64_t val = 0xf0e1d2c3b4a59687;
518 	uint8_t buf[] = {
519 		0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0
520 	};
521 	uint8_t tmp[sizeof(uint64_t)];
522 
523 	sys_put_le64(val, tmp);
524 
525 	zassert_mem_equal(tmp, buf, sizeof(uint64_t), "sys_put_le64() failed");
526 }
527 
528 /**
529  * @brief Test sys_uint16_to_array() functionality
530  *
531  * @details Test if sys_uint16_to_array() correctly handles endianness.
532  *
533  * @see sys_uint16_to_array()
534  */
ZTEST(byteorder,test_sys_uint16_to_array)535 ZTEST(byteorder, test_sys_uint16_to_array)
536 {
537 	#define VAL 0xf0e1
538 	uint8_t tmp[sizeof(uint16_t)] = sys_uint16_to_array(VAL);
539 	uint8_t buf[] = {
540 		COND_CODE_1(CONFIG_LITTLE_ENDIAN,
541 		(0xe1, 0xf0),
542 		(0xf0, 0xe1))
543 	};
544 
545 	zassert_mem_equal(tmp, buf, sizeof(uint16_t), "sys_uint16_to_array() failed");
546 	#undef VAL
547 }
548 
549 /**
550  * @brief Test sys_uint32_to_array() functionality
551  *
552  * @details Test if sys_uint32_to_array() correctly handles endianness.
553  *
554  * @see sys_uint32_to_array()
555  */
ZTEST(byteorder,test_sys_uint32_to_array)556 ZTEST(byteorder, test_sys_uint32_to_array)
557 {
558 	#define VAL 0xf0e1d2c3
559 	uint8_t tmp[sizeof(uint32_t)] = sys_uint32_to_array(VAL);
560 	uint8_t buf[] = {
561 		COND_CODE_1(CONFIG_LITTLE_ENDIAN,
562 		(0xc3, 0xd2, 0xe1, 0xf0),
563 		(0xf0, 0xe1, 0xd2, 0xc3))
564 	};
565 
566 	zassert_mem_equal(tmp, buf, sizeof(uint32_t), "sys_uint32_to_array() failed");
567 	#undef VAL
568 }
569 
570 /**
571  * @brief Test sys_uint64_to_array() functionality
572  *
573  * @details Test if sys_uint64_to_array() correctly handles endianness.
574  *
575  * @see sys_uint64_to_array()
576  */
ZTEST(byteorder,test_sys_uint64_to_array)577 ZTEST(byteorder, test_sys_uint64_to_array)
578 {
579 	#define VAL 0xf0e1d2c3b4a59687
580 	uint8_t tmp[sizeof(uint64_t)] = sys_uint64_to_array(VAL);
581 	uint8_t buf[] = {
582 		COND_CODE_1(CONFIG_LITTLE_ENDIAN,
583 		(0x87, 0x96, 0xa5, 0xb4, 0xc3, 0xd2, 0xe1, 0xf0),
584 		(0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87))
585 	};
586 
587 	zassert_mem_equal(tmp, buf, sizeof(uint64_t), "sys_uint64_to_array() failed");
588 	#undef VAL
589 }
590 
591 extern void *common_setup(void);
592 ZTEST_SUITE(byteorder, NULL, common_setup, NULL, NULL, NULL);
593 
594 /**
595  * @}
596  */
597