[PATCH v3 2/2] test: compression: add zstd uncompression test

Brandon Maier brandon.maier at collins.com
Mon Jan 9 19:55:30 CET 2023


Signed-off-by: Brandon Maier <brandon.maier at collins.com>
---
v2:
- added zstd uncompression test
v3:
- use sizeof() instead of magic number for length
---
 test/Kconfig       |  2 +-
 test/compression.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/test/Kconfig b/test/Kconfig
index a6b463e4d06..dd2f082ce63 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -64,7 +64,7 @@ endif
 config UT_COMPRESSION
 	bool "Unit test for compression"
 	depends on UNIT_TEST
-	depends on CMDLINE && GZIP_COMPRESSED && BZIP2 && LZMA && LZO && LZ4
+	depends on CMDLINE && GZIP_COMPRESSED && BZIP2 && LZMA && LZO && LZ4 && ZSTD
 	default y
 	help
 	  Enables tests for compression and decompression routines for simple
diff --git a/test/compression.c b/test/compression.c
index 82e29c9b86b..eae1884938f 100644
--- a/test/compression.c
+++ b/test/compression.c
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <abuf.h>
 #include <bootm.h>
 #include <command.h>
 #include <gzip.h>
@@ -22,6 +23,7 @@
 #include <lzma/LzmaTools.h>
 
 #include <linux/lzo.h>
+#include <linux/zstd.h>
 #include <test/compression.h>
 #include <test/suites.h>
 #include <test/ut.h>
@@ -121,6 +123,23 @@ static const char lz4_compressed[] =
 	"\x9d\x12\x8c\x9d";
 static const unsigned long lz4_compressed_size = 276;
 
+/* zstd -19 -c /tmp/plain.txt > /tmp/plain.zst */
+static const char zstd_compressed[] =
+	"\x28\xb5\x2f\xfd\x64\x5e\x00\xbd\x05\x00\x02\x0e\x26\x1a\x70\x17"
+	"\xb8\x0d\x0c\x53\x5c\x9d\x97\xee\xa0\x5d\x84\x89\x3f\x5c\x7a\x78"
+	"\x00\x80\x80\x0f\xe8\xdf\xaf\x06\x66\xd0\x23\xa6\x7a\x64\x8e\xf4"
+	"\x0d\x5b\x47\x65\x26\x7e\x81\xdd\x0b\xe7\x5a\x95\x3d\x49\xcc\x67"
+	"\xe0\x2d\x46\x58\xb6\xac\x64\x16\xf2\xe0\xf8\x16\x17\xaf\xda\x8f"
+	"\x37\xc0\xc3\x0d\x3b\x89\x57\x15\x1e\x46\x46\x12\x9a\x84\xbe\xa6"
+	"\xab\xcf\x50\x90\x5f\x78\x01\xd2\xc0\x51\x72\x59\x0b\xea\xab\xf2"
+	"\xd4\x2b\x2d\x26\x7c\x10\x66\x78\x42\x64\x45\x3f\xa5\x15\x6f\xbd"
+	"\x4a\x61\xe1\xc8\x27\xc0\xe3\x95\x0c\xf9\xca\x7c\xf5\x13\x30\xc3"
+	"\x1a\x7c\x7d\xa4\x17\x0b\xff\x14\xa6\x7a\x95\xa0\x34\xbc\xce\x21"
+	"\x78\x36\x23\x33\x11\x09\x00\x60\x13\x00\x63\xa3\x8e\x28\x94\x55"
+	"\x15\xb6\x26\x68\x05\x4f\x23\x12\xee\x53\x55\x2d\x44\x2f\x54\x95"
+	"\x01\xe4\xf4\x6e\xfa";
+static const unsigned long zstd_compressed_size = sizeof(zstd_compressed) - 1;
+
 
 #define TEST_BUFFER_SIZE	512
 
@@ -296,6 +315,45 @@ static int uncompress_using_lz4(struct unit_test_state *uts,
 	return (ret != 0);
 }
 
+static int compress_using_zstd(struct unit_test_state *uts,
+			       void *in, unsigned long in_size,
+			       void *out, unsigned long out_max,
+			       unsigned long *out_size)
+{
+	/* There is no zstd compression in u-boot, so fake it. */
+	ut_asserteq(in_size, strlen(plain));
+	ut_asserteq_mem(plain, in, in_size);
+
+	if (zstd_compressed_size > out_max)
+		return -1;
+
+	memcpy(out, zstd_compressed, zstd_compressed_size);
+	if (out_size)
+		*out_size = zstd_compressed_size;
+
+	return 0;
+}
+
+static int uncompress_using_zstd(struct unit_test_state *uts,
+				 void *in, unsigned long in_size,
+				 void *out, unsigned long out_max,
+				 unsigned long *out_size)
+{
+	struct abuf in_buf, out_buf;
+	int ret;
+
+	abuf_init_set(&in_buf, in, in_size);
+	abuf_init_set(&out_buf, out, out_max);
+
+	ret = zstd_decompress(&in_buf, &out_buf);
+	if (ret >= 0) {
+		*out_size = ret;
+		ret = 0;
+	}
+
+	return ret;
+}
+
 #define errcheck(statement) if (!(statement)) { \
 	fprintf(stderr, "\tFailed: %s\n", #statement); \
 	ret = 1; \
@@ -440,6 +498,13 @@ static int compression_test_lz4(struct unit_test_state *uts)
 }
 COMPRESSION_TEST(compression_test_lz4, 0);
 
+static int compression_test_zstd(struct unit_test_state *uts)
+{
+	return run_test(uts, "zstd", compress_using_zstd,
+			uncompress_using_zstd);
+}
+COMPRESSION_TEST(compression_test_zstd, 0);
+
 static int compress_using_none(struct unit_test_state *uts,
 			       void *in, unsigned long in_size,
 			       void *out, unsigned long out_max,
@@ -530,6 +595,12 @@ static int compression_test_bootm_lz4(struct unit_test_state *uts)
 }
 COMPRESSION_TEST(compression_test_bootm_lz4, 0);
 
+static int compression_test_bootm_zstd(struct unit_test_state *uts)
+{
+	return run_bootm_test(uts, IH_COMP_ZSTD, compress_using_zstd);
+}
+COMPRESSION_TEST(compression_test_bootm_zstd, 0);
+
 static int compression_test_bootm_none(struct unit_test_state *uts)
 {
 	return run_bootm_test(uts, IH_COMP_NONE, compress_using_none);
-- 
2.39.0



More information about the U-Boot mailing list