87 lines
1.8 KiB
C++
87 lines
1.8 KiB
C++
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <di/misc/hexstr.h>
|
|
extern "C" {
|
|
#include "misc/hexstr.c"
|
|
}
|
|
|
|
TEST(hexstr, to_from) {
|
|
const char in_str[] = "0123456789_`abcdefg@ABCDEFG";
|
|
const char exp_str[] = "0123456789abcdefabcdef0000";
|
|
uint8_t bin[((sizeof(in_str) - 1) / 2)];
|
|
const uint8_t bin_exp[((sizeof(in_str) - 1) / 2)] = {
|
|
0x01, 0x23, 0x45, 0x67,
|
|
0x89, 0xab, 0xcd, 0xef,
|
|
0xab, 0xcd, 0xef
|
|
};
|
|
char out_str[64];
|
|
|
|
memset(&bin, 0, sizeof(bin));
|
|
memset(&out_str, 0, sizeof(out_str));
|
|
|
|
di_hexstr_to_bin(
|
|
bin, sizeof(bin),
|
|
in_str, strlen(in_str));
|
|
|
|
EXPECT_EQ(0, memcmp(bin, bin_exp, sizeof(bin_exp)));
|
|
|
|
di_hexstr_from_bin(
|
|
out_str, sizeof(out_str),
|
|
bin, sizeof(bin));
|
|
|
|
EXPECT_EQ(0, strcmp(exp_str, out_str));
|
|
}
|
|
|
|
TEST(hexstr, hexstr_to_bin_nonPrintables) {
|
|
const char *in_str = "\x01\x02\x03\xfd\xfe\xff";
|
|
uint8_t bin[10];
|
|
uint8_t bin_exp[10];
|
|
|
|
memset(&bin, 0, sizeof(bin));
|
|
memset(&bin_exp, 0, sizeof(bin_exp));
|
|
|
|
di_hexstr_to_bin(
|
|
bin, sizeof(bin),
|
|
in_str, 3);
|
|
|
|
EXPECT_EQ(0, memcmp(bin, bin_exp, sizeof(bin_exp)));
|
|
}
|
|
|
|
TEST(hexstr, char_to_bin) {
|
|
const char in_str[] = "_`@G";
|
|
for (size_t n = 0; n < sizeof(in_str) - 1; n++)
|
|
EXPECT_EQ(0, di_hexstr_char_to_bin(in_str[n]));
|
|
}
|
|
|
|
TEST(hexstr, hexstr_to_bin_smaller_dst) {
|
|
const char *in_str = "012345"; // 3 bytes
|
|
uint8_t bin[2]; // 2 bytes
|
|
uint8_t bin_exp[2] = { 0x01, 0x23 };
|
|
|
|
di_hexstr_to_bin(
|
|
bin, sizeof(bin),
|
|
in_str, strlen(in_str));
|
|
|
|
EXPECT_EQ(0, memcmp(bin, bin_exp, sizeof(bin_exp)));
|
|
}
|
|
|
|
TEST(hexstr, hexstr_from_bin_smaller_dst) {
|
|
const uint8_t bin[3] = {
|
|
0x45, 0x01, 0x23 // 3 bytes -> 6 char + 1 null
|
|
};
|
|
char out_str[5];
|
|
char out_str_exp[5] = "4501";
|
|
|
|
memset(out_str, 0, sizeof(out_str));
|
|
|
|
di_hexstr_from_bin(
|
|
out_str, sizeof(out_str) - 1,
|
|
bin, sizeof(bin));
|
|
|
|
EXPECT_STREQ(out_str_exp, out_str);
|
|
}
|