aboutsummaryrefslogtreecommitdiff
path: root/tests/libutil/hex_decode.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-16 15:41:57 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2022-11-18 00:58:07 +0100
commitda6eadc840716eb29b0175f39b2790bba166db4a (patch)
tree2da0a94126432e7af409ed809ea3476a1e44597c /tests/libutil/hex_decode.c
parent0f68ca18f491b4e53cec788b327a752cbeb43377 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'tests/libutil/hex_decode.c')
-rw-r--r--tests/libutil/hex_decode.c66
1 files changed, 66 insertions, 0 deletions
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 <goliath@infraroot.at>
+ */
+#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;
+}
+