"Das U-Boot" Source Tree
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

test: cmd: Add test for zip/unzip/gzwrite commands

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>

authored by

Marek Vasut and committed by
Tom Rini
6be3db6c 8a056a10

+141 -1
+7
arch/sandbox/dts/test.dts
··· 1272 1272 filename = "mmc8.img"; 1273 1273 }; 1274 1274 1275 + /* This is used for zip/unzip/gzwrite tests. */ 1276 + mmc9 { 1277 + status = "disabled"; 1278 + compatible = "sandbox,mmc"; 1279 + filename = "mmc9.img"; 1280 + }; 1281 + 1275 1282 pch { 1276 1283 compatible = "sandbox,pch"; 1277 1284 };
+1 -1
common/Kconfig
··· 26 26 config CONSOLE_RECORD_OUT_SIZE 27 27 hex "Output buffer size" 28 28 depends on CONSOLE_RECORD 29 - default 0x6000 29 + default 0x20000 30 30 help 31 31 Set the size of the console recording output buffer. When this fills 32 32 up, no more data will be recorded until some is removed. The buffer
+3
test/cmd/Makefile
··· 45 45 obj-$(CONFIG_ARM_FFA_TRANSPORT) += armffa.o 46 46 endif 47 47 obj-$(CONFIG_CMD_SPAWN) += spawn.o 48 + ifdef CONFIG_CMD_ZIP 49 + obj-$(CONFIG_CMD_UNZIP) += unzip.o 50 + endif
+124
test/cmd/unzip.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + /* 3 + * Tests for zip/unzip/gzwrite commands 4 + * 5 + * Copyright 2026, Marek Vasut <marek.vasut+renesas@mailbox.org> 6 + */ 7 + 8 + #include <command.h> 9 + #include <env.h> 10 + #include <dm.h> 11 + #include <dm/lists.h> 12 + #include <dm/test.h> 13 + #include <linux/sizes.h> 14 + #include <mapmem.h> 15 + #include <part.h> 16 + #include <test/cmd.h> 17 + #include <test/test.h> 18 + #include <test/ut.h> 19 + #include <u-boot/crc.h> 20 + 21 + /* sys/random.h is not accessible */ 22 + extern ssize_t getrandom(void *buf, size_t size, unsigned int flags); 23 + 24 + static const ssize_t sizes[] = { 32, SZ_1K, SZ_4K, SZ_1M, SZ_16M, SZ_1M - 1, SZ_1M + 1, 6758401 }; 25 + 26 + static int do_test_cmd_zip_unzip(struct unit_test_state *uts, ssize_t size, 27 + const bool gzwrite) 28 + { 29 + unsigned long loadaddr = env_get_ulong("loadaddr", 16, 0); 30 + unsigned long encaddr = loadaddr + size + 0x10000; 31 + unsigned long decaddr = encaddr + size + 0x10000; 32 + unsigned char *loadmap = map_sysmem(loadaddr, size); 33 + unsigned char *decmap = map_sysmem(decaddr, size); 34 + unsigned char *encmap = map_sysmem(encaddr, size); 35 + 36 + /* 37 + * Prepare three buffers, $loadadd, $encaddr, $decaddr, and 38 + * fill them all with random data. Add slight space between 39 + * the compressed buffer 'encaddr' and uncompressed buffer 40 + * 'decaddr', because the compressed data with gzip header 41 + * might be longer than uncompressed source data 'loadaddr', 42 + * and if the uncompressed data buffer 'decaddr' followed 43 + * 'encaddr', the decompression could corrupt end of 'encaddr' 44 + * buffer. 45 + */ 46 + 47 + ut_assert(getrandom(loadmap, size, 0) == size); 48 + ut_assert(getrandom(decmap, size, 0) == size); 49 + ut_assert(getrandom(encmap, size, 0) == size); 50 + 51 + /* Compress data in $loadaddr into $encaddr */ 52 + ut_assertok(run_commandf("zip $loadaddr %zx %zx", size, encaddr)); 53 + console_record_readline(uts->actual_str, sizeof(uts->actual_str)); 54 + ut_assert(strstr(uts->actual_str, "Compressed size: ")); 55 + 56 + if (gzwrite) { 57 + unsigned int sectsize = DIV_ROUND_UP(size, 512); 58 + u32 crc = crc32(0, loadmap, size); 59 + struct blk_desc *mmc_dev_desc; 60 + 61 + ut_assertok(run_commandf("gzwrite mmc 9 %zx $filesize", encaddr)); 62 + ut_assert_skip_to_line("\t%zu bytes, crc 0x%08x", size, crc); 63 + 64 + ut_asserteq(9, blk_get_device_by_str("mmc", "9", &mmc_dev_desc)); 65 + ut_assertok(run_commandf("mmc dev 9")); 66 + ut_assert_nextline("switch to partitions #0, OK"); 67 + ut_assert_nextline("mmc9 is current device"); 68 + 69 + ut_assertok(run_commandf("mmc read %zx 0 %x", decaddr, sectsize)); 70 + ut_assert_nextline("MMC read: dev # 9, block # 0, count %u ... %u blocks read: OK", 71 + sectsize, sectsize); 72 + } else { 73 + /* Decompress data in $encaddr into $decaddr */ 74 + ut_assertok(run_commandf("unzip %zx %zx $filesize", encaddr, decaddr)); 75 + ut_assert_nextline("Uncompressed size: %zu = 0x%zX", size, size); 76 + } 77 + 78 + /* Input data and compressed-decompressed data */ 79 + ut_asserteq_mem(loadmap, decmap, size); 80 + 81 + ut_assert_console_end(); 82 + 83 + unmap_sysmem(loadmap); 84 + unmap_sysmem(decmap); 85 + unmap_sysmem(encmap); 86 + 87 + return 0; 88 + } 89 + 90 + static int dm_test_cmd_zip_unzip(struct unit_test_state *uts) 91 + { 92 + int i, ret; 93 + 94 + for (i = 0; i < ARRAY_SIZE(sizes); i++) { 95 + ret = do_test_cmd_zip_unzip(uts, sizes[i], false); 96 + if (ret) 97 + return ret; 98 + } 99 + 100 + return 0; 101 + } 102 + DM_TEST(dm_test_cmd_zip_unzip, UTF_CONSOLE); 103 + 104 + static int dm_test_cmd_zip_gzwrite(struct unit_test_state *uts) 105 + { 106 + struct udevice *dev; 107 + ofnode root, node; 108 + int i, ret; 109 + 110 + /* Enable the mmc9 node for this test */ 111 + root = oftree_root(oftree_default()); 112 + node = ofnode_find_subnode(root, "mmc9"); 113 + ut_assert(ofnode_valid(node)); 114 + ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false)); 115 + 116 + for (i = 0; i < ARRAY_SIZE(sizes); i++) { 117 + ret = do_test_cmd_zip_unzip(uts, sizes[i], true); 118 + if (ret) 119 + return ret; 120 + } 121 + 122 + return 0; 123 + } 124 + DM_TEST(dm_test_cmd_zip_gzwrite, UTF_CONSOLE);
+6
test/py/tests/test_ut.py
··· 522 522 with open(fn, 'wb') as fh: 523 523 fh.write(data) 524 524 525 + mmc_dev = 9 526 + fn = os.path.join(ubman.config.source_dir, f'mmc{mmc_dev}.img') 527 + data = b'\x00' * (32 * 1024 * 1024) 528 + with open(fn, 'wb') as fh: 529 + fh.write(data) 530 + 525 531 526 532 def setup_efi_image(ubman): 527 533 """Create a 20MB disk image with an EFI app on it"""