Skip to content

Commit 976f963

Browse files
kartbendkalowsk
authored andcommitted
tests: unit: add unit tests for hex utilities
Ensure 100% code coverage of lib/utils/hex.c Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 455280e commit 976f963

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

tests/unit/hex/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr COMPONENTS unittest REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
7+
project(hex)
8+
9+
target_sources(testbinary PRIVATE main.c)

tests/unit/hex/main.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
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+
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+
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+
53+
ZTEST(hex, test_hex2char_invalid)
54+
{
55+
char c;
56+
57+
zassert_equal(hex2char(16, &c), -EINVAL);
58+
}
59+
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+
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+
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+
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+
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+
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);

tests/unit/hex/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_ZTEST=y

tests/unit/hex/testcase.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
utilities.hex:
3+
tags: hex
4+
type: unit

0 commit comments

Comments
 (0)