]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: cmd: Add test for zip/unzip/gzwrite commands
authorMarek Vasut <marek.vasut+renesas@mailbox.org>
Thu, 5 Feb 2026 01:40:45 +0000 (02:40 +0100)
committerTom Rini <trini@konsulko.com>
Wed, 18 Feb 2026 21:02:58 +0000 (15:02 -0600)
Add simple test for zip/unzip/gzwrite commands. The test works as
follows. First, create three buffers with a bit of space between
each of them, fill them with random data, then compress data in
buffer 1 into buffer 2, decompress data in buffer 2 either directly
into buffer 3 or into MMC 1 and then read them back into buffer 3,
and finally compare buffer 1 and buffer 3, they have to be identical.

The buffers are filled with random data to detect out of bounds writes.
Test for various sizes, both small and large and unaligned.

The test uses ut_assert_skip_to_line() to skip over gzwrite progress
bar. Since the progress bar updates fill up the console record buffer,
increase the size of it to compensate.

Reviewed-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
Tested-by: Mattijs Korpershoek <mkorpershoek@kernel.org>
Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
arch/sandbox/dts/test.dts
common/Kconfig
test/cmd/Makefile
test/cmd/unzip.c [new file with mode: 0644]
test/py/tests/test_ut.py

index b7402d7042a55801f5ce912642233b1b03b691d6..762c1d9bbe29c5d5d7ded56b33334a4067cd8627 100644 (file)
                filename = "mmc8.img";
        };
 
+       /* This is used for zip/unzip/gzwrite tests. */
+       mmc9 {
+               status = "disabled";
+               compatible = "sandbox,mmc";
+               filename = "mmc9.img";
+       };
+
        pch {
                compatible = "sandbox,pch";
        };
index 47d17f4e7c60e54df40103f79d1ad5930cba1fc7..63448bdc13bfe15cc1c42d1240a7e31c204c5e45 100644 (file)
@@ -26,7 +26,7 @@ config CONSOLE_RECORD_INIT_F
 config CONSOLE_RECORD_OUT_SIZE
        hex "Output buffer size"
        depends on CONSOLE_RECORD
-       default 0x6000
+       default 0x20000
        help
          Set the size of the console recording output buffer. When this fills
          up, no more data will be recorded until some is removed. The buffer
index 2476068aee6fa2eeb438f94ad5c9919ead9a99ff..8c9f112782d0ba9839adf5c3618abd8a01aa586a 100644 (file)
@@ -45,3 +45,6 @@ endif
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o
 endif
 obj-$(CONFIG_CMD_SPAWN) += spawn.o
+ifdef CONFIG_CMD_ZIP
+obj-$(CONFIG_CMD_UNZIP) += unzip.o
+endif
diff --git a/test/cmd/unzip.c b/test/cmd/unzip.c
new file mode 100644 (file)
index 0000000..b67c5ba
--- /dev/null
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Tests for zip/unzip/gzwrite commands
+ *
+ * Copyright 2026, Marek Vasut <marek.vasut+renesas@mailbox.org>
+ */
+
+#include <command.h>
+#include <env.h>
+#include <dm.h>
+#include <dm/lists.h>
+#include <dm/test.h>
+#include <linux/sizes.h>
+#include <mapmem.h>
+#include <part.h>
+#include <test/cmd.h>
+#include <test/test.h>
+#include <test/ut.h>
+#include <u-boot/crc.h>
+
+/* sys/random.h is not accessible */
+extern ssize_t getrandom(void *buf, size_t size, unsigned int flags);
+
+static const ssize_t sizes[] = { 32, SZ_1K, SZ_4K, SZ_1M, SZ_16M, SZ_1M - 1, SZ_1M + 1, 6758401 };
+
+static int do_test_cmd_zip_unzip(struct unit_test_state *uts, ssize_t size,
+                                const bool gzwrite)
+{
+       unsigned long loadaddr = env_get_ulong("loadaddr", 16, 0);
+       unsigned long encaddr = loadaddr + size + 0x10000;
+       unsigned long decaddr = encaddr + size + 0x10000;
+       unsigned char *loadmap = map_sysmem(loadaddr, size);
+       unsigned char *decmap = map_sysmem(decaddr, size);
+       unsigned char *encmap = map_sysmem(encaddr, size);
+
+       /*
+        * Prepare three buffers, $loadadd, $encaddr, $decaddr, and
+        * fill them all with random data. Add slight space between
+        * the compressed buffer 'encaddr' and uncompressed buffer
+        * 'decaddr', because the compressed data with gzip header
+        * might be longer than uncompressed source data 'loadaddr',
+        * and if the uncompressed data buffer 'decaddr' followed
+        * 'encaddr', the decompression could corrupt end of 'encaddr'
+        * buffer.
+        */
+
+       ut_assert(getrandom(loadmap, size, 0) == size);
+       ut_assert(getrandom(decmap, size, 0) == size);
+       ut_assert(getrandom(encmap, size, 0) == size);
+
+       /* Compress data in $loadaddr into $encaddr */
+       ut_assertok(run_commandf("zip $loadaddr %zx %zx", size, encaddr));
+       console_record_readline(uts->actual_str, sizeof(uts->actual_str));
+       ut_assert(strstr(uts->actual_str, "Compressed size: "));
+
+       if (gzwrite) {
+               unsigned int sectsize = DIV_ROUND_UP(size, 512);
+               u32 crc = crc32(0, loadmap, size);
+               struct blk_desc *mmc_dev_desc;
+
+               ut_assertok(run_commandf("gzwrite mmc 9 %zx $filesize", encaddr));
+               ut_assert_skip_to_line("\t%zu bytes, crc 0x%08x", size, crc);
+
+               ut_asserteq(9, blk_get_device_by_str("mmc", "9", &mmc_dev_desc));
+               ut_assertok(run_commandf("mmc dev 9"));
+               ut_assert_nextline("switch to partitions #0, OK");
+               ut_assert_nextline("mmc9 is current device");
+
+               ut_assertok(run_commandf("mmc read %zx 0 %x", decaddr, sectsize));
+               ut_assert_nextline("MMC read: dev # 9, block # 0, count %u ... %u blocks read: OK",
+                                  sectsize, sectsize);
+       } else {
+               /* Decompress data in $encaddr into $decaddr */
+               ut_assertok(run_commandf("unzip %zx %zx $filesize", encaddr, decaddr));
+               ut_assert_nextline("Uncompressed size: %zu = 0x%zX", size, size);
+       }
+
+       /* Input data and compressed-decompressed data */
+       ut_asserteq_mem(loadmap, decmap, size);
+
+       ut_assert_console_end();
+
+       unmap_sysmem(loadmap);
+       unmap_sysmem(decmap);
+       unmap_sysmem(encmap);
+
+       return 0;
+}
+
+static int dm_test_cmd_zip_unzip(struct unit_test_state *uts)
+{
+       int i, ret;
+
+       for (i = 0; i < ARRAY_SIZE(sizes); i++) {
+               ret = do_test_cmd_zip_unzip(uts, sizes[i], false);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_cmd_zip_unzip, UTF_CONSOLE);
+
+static int dm_test_cmd_zip_gzwrite(struct unit_test_state *uts)
+{
+       struct udevice *dev;
+       ofnode root, node;
+       int i, ret;
+
+       /* Enable the mmc9 node for this test */
+       root = oftree_root(oftree_default());
+       node = ofnode_find_subnode(root, "mmc9");
+       ut_assert(ofnode_valid(node));
+       ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false));
+
+       for (i = 0; i < ARRAY_SIZE(sizes); i++) {
+               ret = do_test_cmd_zip_unzip(uts, sizes[i], true);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+DM_TEST(dm_test_cmd_zip_gzwrite, UTF_CONSOLE);
index 6d535b5206dda9655a3c08e7353fcecf3def0a99..b7166d59943b9635a422a3c04a1b033ef69d27b0 100644 (file)
@@ -522,6 +522,12 @@ def test_ut_dm_init(ubman):
     with open(fn, 'wb') as fh:
         fh.write(data)
 
+    mmc_dev = 9
+    fn = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img')
+    data = b'\x00' * (32 * 1024 * 1024)
+    with open(fn, 'wb') as fh:
+        fh.write(data)
+
 
 def setup_efi_image(ubman):
     """Create a 20MB disk image with an EFI app on it"""