From da6eadc840716eb29b0175f39b2790bba166db4a Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 16 Nov 2022 15:41:57 +0100 Subject: Add a single, central hex blob decoder Since we need it twice (once for tar, once for the filemap xattr parser), add a single, central implementation to libutil, add a unit test for that implementation and then use it in both libtar and gensquashfs. Signed-off-by: David Oberhollenzer --- tests/libutil/hex_decode.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tests/libutil/hex_decode.c (limited to 'tests/libutil/hex_decode.c') diff --git a/tests/libutil/hex_decode.c b/tests/libutil/hex_decode.c new file mode 100644 index 0000000..21ac4e7 --- /dev/null +++ b/tests/libutil/hex_decode.c @@ -0,0 +1,66 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * hex_decode.c + * + * Copyright (C) 2022 David Oberhollenzer + */ +#include "config.h" +#include "util/util.h" +#include "util/test.h" + +static const struct { + int result; + const char *in; + const char *out; +} test_vec[] = { + { 0, "", NULL }, + { -1, "A", NULL }, + { 0, "AA", "\xAA" }, + { 0, "0A", "\x0A" }, + { 0, "A0", "\xA0" }, + { -1, "A0B", NULL }, + { 0, "A0BC", "\xA0\xBC" }, + { 0, "0123456789ABCDEF", "\x01\x23\x45\x67\x89\xAB\xCD\xEF" }, + { 0, "0123456789abcdef", "\x01\x23\x45\x67\x89\xAB\xCD\xEF" }, + { -1, "0123456789ABCDEFGH", NULL }, + { -1, "0123456789abcdefgh", NULL }, +}; + +int main(int argc, char **argv) +{ + sqfs_u8 buffer[256]; + size_t i, j; + (void)argc; (void)argv; + + for (i = 0; i < sizeof(test_vec) / sizeof(test_vec[0]); ++i) { + size_t in_len = strlen(test_vec[i].in); + size_t out_len = in_len / 2; + int ret; + + /* initialize the buffer */ + for (j = 0; j < sizeof(buffer); ++j) { + buffer[j] = (j % 2) ? 0xAA : 0x55; + } + + /* convert */ + ret = hex_decode(test_vec[i].in, in_len, + buffer, sizeof(buffer)); + + /* make sure pattern is un-touched after expected offset */ + for (j = out_len; j < sizeof(buffer); ++j) { + TEST_ASSERT(buffer[j] == ((j % 2) ? 0xAA : 0x55)); + } + + /* check result */ + if (test_vec[i].result == 0) { + TEST_ASSERT(ret == 0); + ret = memcmp(buffer, test_vec[i].out, out_len); + TEST_ASSERT(ret == 0); + } else { + TEST_ASSERT(ret != 0); + } + } + + return EXIT_SUCCESS; +} + -- cgit v1.2.3