Add UT for CellularUtil::hex_to_char and ::hex_str_to_char_str

Also added checks for pointer validity.
pull/11495/head
Kimmo Vaisanen 2019-09-13 08:45:04 +03:00
parent 0b9e80f76e
commit e824714cd8
2 changed files with 53 additions and 8 deletions

View File

@ -53,6 +53,47 @@ TEST_F(Testutil, test_util_binary_str_to_uint)
EXPECT_TRUE(0 == binary_str_to_uint(binary_str, 0));
}
TEST_F(Testutil, hex_to_char)
{
char output;
// 0
hex_to_char("00", output);
EXPECT_EQ((char)0x00, output);
// <128
hex_to_char("10", output);
EXPECT_EQ((char)0x10, output);
// =128
hex_to_char("80", output);
EXPECT_EQ((char)0x80, output);
// >128
hex_to_char("FF", output);
EXPECT_EQ((char)0xFF, output);
// Null -> output is not modified
hex_to_char(NULL, output);
EXPECT_EQ((char)0xFF, output);
}
TEST_F(Testutil, hex_str_to_char_str)
{
char input[] = "0165AABBCC";
char output[32];
EXPECT_EQ(5, hex_str_to_char_str(input, strlen(input), output));
EXPECT_EQ((char)0x01, output[0]);
EXPECT_EQ((char)0x65, output[1]);
EXPECT_EQ((char)0xAA, output[2]);
EXPECT_EQ((char)0xBB, output[3]);
EXPECT_EQ((char)0xCC, output[4]);
// NULL params
EXPECT_EQ(0, hex_str_to_char_str(NULL, 2, output));
EXPECT_EQ(0, hex_str_to_char_str(input, strlen(input), NULL));
}
TEST_F(Testutil, test_util_uint_to_binary_string)
{
char str[33];

View File

@ -278,11 +278,13 @@ int hex_str_to_int(const char *hex_string, int hex_string_length)
int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
{
int strcount = 0;
for (int i = 0; i + 1 < len; i += 2) {
char tmp;
hex_to_char(str + i, tmp);
buf[strcount] = tmp;
strcount++;
if (str && buf) {
for (int i = 0; i + 1 < len; i += 2) {
char tmp;
hex_to_char(str + i, tmp);
buf[strcount] = tmp;
strcount++;
}
}
return strcount;
@ -290,9 +292,11 @@ int hex_str_to_char_str(const char *str, uint16_t len, char *buf)
void hex_to_char(const char *hex, char &buf)
{
int upper = hex_str_to_int(hex, 1);
int lower = hex_str_to_int(hex + 1, 1);
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
if (hex) {
int upper = hex_str_to_int(hex, 1);
int lower = hex_str_to_int(hex + 1, 1);
buf = ((upper << 4) & 0xF0) | (lower & 0x0F);
}
}
void uint_to_binary_str(uint32_t num, char *str, int str_size, int bit_cnt)