1 /*
2 * Copyright The Zephyr Project Contributors
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/sys/util.h>
9
10 #include "../../../lib/utils/hex.c"
11
ZTEST(hex,test_char2hex_valid)12 ZTEST(hex, test_char2hex_valid)
13 {
14 uint8_t x;
15
16 zassert_ok(char2hex('0', &x));
17 zassert_equal(x, 0);
18 zassert_ok(char2hex('9', &x));
19 zassert_equal(x, 9);
20 zassert_ok(char2hex('a', &x));
21 zassert_equal(x, 10);
22 zassert_ok(char2hex('f', &x));
23 zassert_equal(x, 15);
24 zassert_ok(char2hex('A', &x));
25 zassert_equal(x, 10);
26 zassert_ok(char2hex('F', &x));
27 zassert_equal(x, 15);
28 }
29
ZTEST(hex,test_char2hex_invalid)30 ZTEST(hex, test_char2hex_invalid)
31 {
32 uint8_t x;
33
34 zassert_equal(char2hex('g', &x), -EINVAL);
35 zassert_equal(char2hex('G', &x), -EINVAL);
36 zassert_equal(char2hex('!', &x), -EINVAL);
37 }
38
ZTEST(hex,test_hex2char_valid)39 ZTEST(hex, test_hex2char_valid)
40 {
41 char c;
42
43 zassert_ok(hex2char(0, &c));
44 zassert_equal(c, '0');
45 zassert_ok(hex2char(9, &c));
46 zassert_equal(c, '9');
47 zassert_ok(hex2char(10, &c));
48 zassert_equal(c, 'a');
49 zassert_ok(hex2char(15, &c));
50 zassert_equal(c, 'f');
51 }
52
ZTEST(hex,test_hex2char_invalid)53 ZTEST(hex, test_hex2char_invalid)
54 {
55 char c;
56
57 zassert_equal(hex2char(16, &c), -EINVAL);
58 }
59
ZTEST(hex,test_bin2hex)60 ZTEST(hex, test_bin2hex)
61 {
62 uint8_t buf[] = {0x00, 0x10, 0xFF, 0x3A};
63 char hexstr[9];
64 size_t len;
65
66 len = bin2hex(buf, sizeof(buf), hexstr, sizeof(hexstr));
67 zassert_equal(len, 8);
68 zassert_mem_equal(hexstr, "0010ff3a", 9);
69 }
70
ZTEST(hex,test_bin2hex_too_small)71 ZTEST(hex, test_bin2hex_too_small)
72 {
73 uint8_t buf[] = {0xAA};
74 char hexstr[2];
75
76 zassert_equal(bin2hex(buf, sizeof(buf), hexstr, sizeof(hexstr)), 0);
77 }
78
ZTEST(hex,test_hex2bin_even)79 ZTEST(hex, test_hex2bin_even)
80 {
81 const char *hexstr = "0010ff3a";
82 uint8_t expected[] = {0x00, 0x10, 0xFF, 0x3A};
83 uint8_t buf[4];
84 size_t len;
85
86 len = hex2bin(hexstr, strlen(hexstr), buf, sizeof(buf));
87 zassert_equal(len, 4);
88 zassert_mem_equal(buf, expected, sizeof(expected));
89 }
90
ZTEST(hex,test_hex2bin_odd)91 ZTEST(hex, test_hex2bin_odd)
92 {
93 const char *hexstr = "abc";
94 uint8_t expected[] = {0x0a, 0xbc};
95 uint8_t buf[2];
96 size_t len;
97
98 len = hex2bin(hexstr, strlen(hexstr), buf, sizeof(buf));
99 zassert_equal(len, 2);
100 zassert_mem_equal(buf, expected, sizeof(expected));
101 }
102
ZTEST(hex,test_hex2bin_invalid)103 ZTEST(hex, test_hex2bin_invalid)
104 {
105 const char *hexstr1 = "g04";
106 const char *hexstr2 = "01g4";
107 const char *hexstr3 = "014g";
108 uint8_t buf[2];
109
110 zassert_equal(hex2bin(hexstr1, strlen(hexstr1), buf, sizeof(buf)), 0);
111 zassert_equal(hex2bin(hexstr2, strlen(hexstr2), buf, sizeof(buf)), 0);
112 zassert_equal(hex2bin(hexstr3, strlen(hexstr3), buf, sizeof(buf)), 0);
113 }
114
ZTEST(hex,test_hex2bin_buf_too_small)115 ZTEST(hex, test_hex2bin_buf_too_small)
116 {
117 const char *hexstr = "abc";
118 uint8_t buf[1];
119
120 zassert_equal(hex2bin(hexstr, strlen(hexstr), buf, sizeof(buf)), 0);
121 }
122
123 ZTEST_SUITE(hex, NULL, NULL, NULL, NULL, NULL);
124