Skip to contentSkip to conversation

test: compression: check with trailing garbage in input

PATCH In review Brandon Maier 3 participants 6 messages Dec 28, 2022 – Jan 12, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 test: compression: check with trailing garbage in input

The Linux kernel appends 4 bytes to the end of compressed kernel Images containing the uncompressed image size. They are used to make self-decompressing Images easier. However for archs that don't support self-decompression, like ARM64, U-Boot must be able to decompress the image with the garbage data.

The existing decompressors already support this. This unit test was added while working on zstd support as upstream zstd will error if there is garbage data in the input buffer, and special care was needed to support this.

---
 test/compression.c | 9 +++++++++
 1 file changed, 9 insertions(+)
test/compression.c +9 -0
index 82e29c9b86b..6c2a43fbed7 100644
@@ -350,6 +350,15 @@ static int run_test_internal(struct unit_test_state *uts, char *name,
 			buf->orig_size) == 0);
 	errcheck(((char *)buf->uncompressed_buf)[buf->orig_size] == 'A');
 
+	/* Uncompresses with trailing garbage in input buffer. */
+	memset(buf->uncompressed_buf, 'A', TEST_BUFFER_SIZE);
+	errcheck(uncompress(uts, buf->compressed_buf, buf->compressed_size + 4,
+			    buf->uncompressed_buf, buf->uncompressed_size,
+			    &buf->uncompressed_size) == 0);
+	errcheck(buf->uncompressed_size == buf->orig_size);
+	errcheck(memcmp(buf->orig_buf, buf->uncompressed_buf,
+			buf->orig_size) == 0);
+
 	/* Make sure compression does not over-run. */
 	memset(buf->compare_buf, 'A', TEST_BUFFER_SIZE);
 	ret = compress(uts, buf->orig_buf, buf->orig_size,
Signature · 1 line
2.39.0
#1 lore raw

PATCH 2/2 test: compression: use ut_assert for errors

Use the ut_assert macros for more useful error messages.

---
 test/compression.c | 56 +++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 31 deletions(-)
test/compression.c +25 -31
index 6c2a43fbed7..ba98d21802c 100644
@@ -321,51 +321,48 @@ static int run_test_internal(struct unit_test_state *uts, char *name,
 	/* Compress works as expected. */
 	printf("\torig_size:%lu\n", buf->orig_size);
 	memset(buf->compressed_buf, 'A', TEST_BUFFER_SIZE);
-	errcheck(compress(uts, buf->orig_buf, buf->orig_size,
+	ut_assertok(compress(uts, buf->orig_buf, buf->orig_size,
 			  buf->compressed_buf, buf->compressed_size,
-			  &buf->compressed_size) == 0);
+			  &buf->compressed_size));
 	printf("\tcompressed_size:%lu\n", buf->compressed_size);
-	errcheck(buf->compressed_size > 0);
-	errcheck(buf->compressed_size < buf->orig_size);
-	errcheck(((char *)buf->compressed_buf)[buf->compressed_size - 1] !=
-			'A');
-	errcheck(((char *)buf->compressed_buf)[buf->compressed_size] == 'A');
+	ut_assert(buf->compressed_size > 0);
+	ut_assert(buf->compressed_size < buf->orig_size);
+	ut_assert(((char *)buf->compressed_buf)[buf->compressed_size - 1]
+			!= 'A');
+	ut_asserteq(((char *)buf->compressed_buf)[buf->compressed_size], 'A');
 
 	/* Uncompresses with space remaining. */
-	errcheck(uncompress(uts, buf->compressed_buf, buf->compressed_size,
+	ut_assertok(uncompress(uts, buf->compressed_buf, buf->compressed_size,
 			    buf->uncompressed_buf, buf->uncompressed_size,
-			    &buf->uncompressed_size) == 0);
+			    &buf->uncompressed_size));
 	printf("\tuncompressed_size:%lu\n", buf->uncompressed_size);
-	errcheck(buf->uncompressed_size == buf->orig_size);
-	errcheck(memcmp(buf->orig_buf, buf->uncompressed_buf,
-			buf->orig_size) == 0);
+	ut_asserteq(buf->uncompressed_size, buf->orig_size);
+	ut_asserteq_mem(buf->orig_buf, buf->uncompressed_buf, buf->orig_size);
 
 	/* Uncompresses with exactly the right size output buffer. */
 	memset(buf->uncompressed_buf, 'A', TEST_BUFFER_SIZE);
-	errcheck(uncompress(uts, buf->compressed_buf, buf->compressed_size,
+	ut_assertok(uncompress(uts, buf->compressed_buf, buf->compressed_size,
 			    buf->uncompressed_buf, buf->orig_size,
-			    &buf->uncompressed_size) == 0);
-	errcheck(buf->uncompressed_size == buf->orig_size);
-	errcheck(memcmp(buf->orig_buf, buf->uncompressed_buf,
-			buf->orig_size) == 0);
-	errcheck(((char *)buf->uncompressed_buf)[buf->orig_size] == 'A');
+			    &buf->uncompressed_size));
+	ut_asserteq(buf->uncompressed_size, buf->orig_size);
+	ut_asserteq_mem(buf->orig_buf, buf->uncompressed_buf, buf->orig_size);
+	ut_asserteq(((char *)buf->uncompressed_buf)[buf->orig_size], 'A');
 
 	/* Uncompresses with trailing garbage in input buffer. */
 	memset(buf->uncompressed_buf, 'A', TEST_BUFFER_SIZE);
-	errcheck(uncompress(uts, buf->compressed_buf, buf->compressed_size + 4,
+	ut_assertok(uncompress(uts, buf->compressed_buf, buf->compressed_size + 4,
 			    buf->uncompressed_buf, buf->uncompressed_size,
-			    &buf->uncompressed_size) == 0);
-	errcheck(buf->uncompressed_size == buf->orig_size);
-	errcheck(memcmp(buf->orig_buf, buf->uncompressed_buf,
-			buf->orig_size) == 0);
+			    &buf->uncompressed_size));
+	ut_asserteq(buf->uncompressed_size, buf->orig_size);
+	ut_asserteq_mem(buf->orig_buf, buf->uncompressed_buf, buf->orig_size);
 
 	/* Make sure compression does not over-run. */
 	memset(buf->compare_buf, 'A', TEST_BUFFER_SIZE);
 	ret = compress(uts, buf->orig_buf, buf->orig_size,
 		       buf->compare_buf, buf->compressed_size - 1,
 		       NULL);
-	errcheck(((char *)buf->compare_buf)[buf->compressed_size] == 'A');
-	errcheck(ret != 0);
+	ut_asserteq(((char *)buf->compare_buf)[buf->compressed_size], 'A');
+	ut_assert(ret != 0);
 	printf("\tcompress does not overrun\n");
 
 	/* Make sure decompression does not over-run. */
@@ -373,15 +370,12 @@ static int run_test_internal(struct unit_test_state *uts, char *name,
 	ret = uncompress(uts, buf->compressed_buf, buf->compressed_size,
 			 buf->compare_buf, buf->uncompressed_size - 1,
 			 NULL);
-	errcheck(((char *)buf->compare_buf)[buf->uncompressed_size - 1] == 'A');
-	errcheck(ret != 0);
+	ut_asserteq(((char *)buf->compare_buf)[buf->uncompressed_size - 1], 'A');
+	ut_assert(ret != 0);
 	printf("\tuncompress does not overrun\n");
 
 	/* Got here, everything is fine. */
-	ret = 0;
-
-out:
-	return ret;
+	return 0;
 }
 
 static int run_test(struct unit_test_state *uts, char *name,
Signature · 1 line
2.39.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] test: compression: use ut_assert for errors

Quoted 8 lines · Brandon Maier

Use the ut_assert macros for more useful error messages.

---
 test/compression.c | 56 +++++++++++++++++++++-------------------------
 1 file changed, 25 insertions(+), 31 deletions(-)
Reviewed-by on v1 1/2
#4 lore raw
In reply to Brandon Maier
Quoted 12 lines · Brandon Maier

The Linux kernel appends 4 bytes to the end of compressed kernel Images containing the uncompressed image size. They are used to make self-decompressing Images easier. However for archs that don't support self-decompression, like ARM64, U-Boot must be able to decompress the image with the garbage data.

The existing decompressors already support this. This unit test was added while working on zstd support as upstream zstd will error if there is garbage data in the input buffer, and special care was needed to support this.

Quoted 4 lines
---
 test/compression.c | 9 +++++++++
 1 file changed, 9 insertions(+)
#5 lore raw
In reply to Brandon Maier
Quoted 6 lines · Brandon Maier

The Linux kernel appends 4 bytes to the end of compressed kernel Images containing the uncompressed image size. They are used to make self-decompressing Images easier. However for archs that don't support self-decompression, like ARM64, U-Boot must be able to decompress the image with the garbage data.

Quoted 7 lines

The existing decompressors already support this. This unit test was added while working on zstd support as upstream zstd will error if there is garbage data in the input buffer, and special care was needed to support this.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom