test: compression: add zstd uncompression test

Signed-off-by: Brandon Maier <brandon.maier@collins.com>
diff --git a/configs/sandbox_flattree_defconfig b/configs/sandbox_flattree_defconfig
index 41453e0..f30e806 100644
--- a/configs/sandbox_flattree_defconfig
+++ b/configs/sandbox_flattree_defconfig
@@ -211,6 +211,7 @@
 CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
+CONFIG_ZSTD=y
 CONFIG_ERRNO_STR=y
 # CONFIG_CMD_BOOTEFI_BOOTMGR is not set
 CONFIG_EFI_RUNTIME_UPDATE_CAPSULE=y
diff --git a/configs/sandbox_noinst_defconfig b/configs/sandbox_noinst_defconfig
index a122355..59e9a8d 100644
--- a/configs/sandbox_noinst_defconfig
+++ b/configs/sandbox_noinst_defconfig
@@ -236,6 +236,7 @@
 CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
+CONFIG_ZSTD=y
 CONFIG_ERRNO_STR=y
 CONFIG_UNIT_TEST=y
 CONFIG_SPL_UNIT_TEST=y
diff --git a/configs/sandbox_spl_defconfig b/configs/sandbox_spl_defconfig
index 4d70466..5316e0b 100644
--- a/configs/sandbox_spl_defconfig
+++ b/configs/sandbox_spl_defconfig
@@ -239,6 +239,7 @@
 CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
+CONFIG_ZSTD=y
 CONFIG_ERRNO_STR=y
 CONFIG_SPL_HEXDUMP=y
 CONFIG_UNIT_TEST=y
diff --git a/configs/sandbox_vpl_defconfig b/configs/sandbox_vpl_defconfig
index c94b059..1d434ec 100644
--- a/configs/sandbox_vpl_defconfig
+++ b/configs/sandbox_vpl_defconfig
@@ -254,6 +254,7 @@
 CONFIG_RSA_VERIFY_WITH_PKEY=y
 CONFIG_TPM=y
 CONFIG_LZ4=y
+CONFIG_ZSTD=y
 # CONFIG_VPL_LZMA is not set
 CONFIG_ERRNO_STR=y
 CONFIG_UNIT_TEST=y
diff --git a/test/Kconfig b/test/Kconfig
index 9f4641a..4650282 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -69,7 +69,7 @@
 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 de6dfbb..3df9081 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 @@
 	"\x9d\x12\x8c\x9d";
 static const unsigned long lz4_compressed_size = sizeof(lz4_compressed) - 1;
 
+/* 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 @@
 	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; \
@@ -443,6 +501,13 @@
 }
 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,
@@ -533,6 +598,12 @@
 }
 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);