Skip to content

Commit e824714

Browse files
author
Kimmo Vaisanen
committed
Add UT for CellularUtil::hex_to_char and ::hex_str_to_char_str
Also added checks for pointer validity.
1 parent 0b9e80f commit e824714

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

UNITTESTS/features/cellular/framework/common/util/utiltest.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,47 @@ TEST_F(Testutil, test_util_binary_str_to_uint)
5353
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
5454
}
5555

56+
TEST_F(Testutil, hex_to_char)
57+
{
58+
char output;
59+
60+
// 0
61+
hex_to_char("00", output);
62+
EXPECT_EQ((char)0x00, output);
63+
64+
// <128
65+
hex_to_char("10", output);
66+
EXPECT_EQ((char)0x10, output);
67+
68+
// =128
69+
hex_to_char("80", output);
70+
EXPECT_EQ((char)0x80, output);
71+
72+
// >128
73+
hex_to_char("FF", output);
74+
EXPECT_EQ((char)0xFF, output);
75+
76+
// Null -> output is not modified
77+
hex_to_char(NULL, output);
78+
EXPECT_EQ((char)0xFF, output);
79+
}
80+
81+
TEST_F(Testutil, hex_str_to_char_str)
82+
{
83+
char input[] = "0165AABBCC";
84+
char output[32];
85+
EXPECT_EQ(5, hex_str_to_char_str(input, strlen(input), output));
86+
EXPECT_EQ((char)0x01, output[0]);
87+
EXPECT_EQ((char)0x65, output[1]);
88+
EXPECT_EQ((char)0xAA, output[2]);
89+
EXPECT_EQ((char)0xBB, output[3]);
90+
EXPECT_EQ((char)0xCC, output[4]);
91+
92+
// NULL params
93+
EXPECT_EQ(0, hex_str_to_char_str(NULL, 2, output));
94+
EXPECT_EQ(0, hex_str_to_char_str(input, strlen(input), NULL));
95+
}
96+
5697
TEST_F(Testutil, test_util_uint_to_binary_string)
5798
{
5899
char str[33];

features/cellular/framework/common/CellularUtil.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,21 +278,25 @@ int hex_str_to_int(const char *hex_string, int hex_string_length)
278278
int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
279279
{
280280
int strcount = 0;
281-
for (int i = 0; i + 1 < len; i += 2) {
282-
char tmp;
283-
hex_to_char(str + i, tmp);
284-
buf[strcount] = tmp;
285-
strcount++;
281+
if (str && buf) {
282+
for (int i = 0; i + 1 < len; i += 2) {
283+
char tmp;
284+
hex_to_char(str + i, tmp);
285+
buf[strcount] = tmp;
286+
strcount++;
287+
}
286288
}
287289

288290
return strcount;
289291
}
290292

291293
void hex_to_char(const char *hex, char &buf)
292294
{
293-
int upper = hex_str_to_int(hex, 1);
294-
int lower = hex_str_to_int(hex + 1, 1);
295-
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
295+
if (hex) {
296+
int upper = hex_str_to_int(hex, 1);
297+
int lower = hex_str_to_int(hex + 1, 1);
298+
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
299+
}
296300
}
297301

298302
void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt)

0 commit comments

Comments
 (0)