From 134a7177ba7242f0d11865f6b2a08215b2c3961c Mon Sep 17 00:00:00 2001 From: dragonfsky Date: Mon, 29 Jun 2026 05:20:50 +0800 Subject: [PATCH] test: consolidate frame garbage rejection checks --- .../io/github/dfa1/zstd/ZstdFrameTest.java | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java b/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java index 87a95fe..17fb64e 100644 --- a/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java +++ b/zstd/src/test/java/io/github/dfa1/zstd/ZstdFrameTest.java @@ -19,6 +19,7 @@ class ZstdFrameTest { private static final byte[] PAYLOAD = "frame inspection payload ".repeat(500).getBytes(StandardCharsets.UTF_8); + private static final byte[] GARBAGE = "xx".getBytes(StandardCharsets.UTF_8); @Nested class IsFrame { @@ -30,7 +31,13 @@ void recognizesAZstdFrame() { @Test void rejectsGarbage() { - assertThat(ZstdFrame.isZstdFrame("not a frame".getBytes(StandardCharsets.UTF_8))).isFalse(); + // Given bytes that are not a zstd frame + + // When checking whether they are a frame + boolean result = ZstdFrame.isZstdFrame(ZstdFrameTest.GARBAGE); + + // Then it reports false rather than throwing + assertThat(result).isFalse(); } } @@ -62,13 +69,11 @@ void locatesFirstFrameBoundaryWhenConcatenated() throws Exception { @Test void rejectsGarbage() { // Given bytes that are not a zstd frame - byte[] garbage = "xxxx".getBytes(StandardCharsets.UTF_8); - // When asking for the first frame's compressed size - ThrowingCallable result = () -> ZstdFrame.compressedSize(garbage); + ThrowingCallable result = () -> ZstdFrame.compressedSize(ZstdFrameTest.GARBAGE); // Then it fails - assertThatThrownBy(result).isInstanceOf(ZstdException.class); + ZstdFrameTest.rejectsGarbage(result); } } @@ -84,13 +89,11 @@ void boundsTheDecompressedSize() { @Test void rejectsGarbage() { // Given bytes that are not valid zstd data - byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8); - // When asking for the decompressed bound - ThrowingCallable result = () -> ZstdFrame.decompressedBound(garbage); + ThrowingCallable result = () -> ZstdFrame.decompressedBound(ZstdFrameTest.GARBAGE); // Then it fails - assertThatThrownBy(result).isInstanceOf(ZstdException.class); + ZstdFrameTest.rejectsGarbage(result); } } @@ -152,13 +155,11 @@ void failsWhenAFrameDoesNotRecordItsSize() throws Exception { @Test void rejectsGarbage() { // Given bytes that are not valid zstd data - byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8); - // When the exact total is requested - ThrowingCallable result = () -> ZstdFrame.decompressedSize(garbage); + ThrowingCallable result = () -> ZstdFrame.decompressedSize(ZstdFrameTest.GARBAGE); // Then it fails - assertThatThrownBy(result).isInstanceOf(ZstdException.class); + ZstdFrameTest.rejectsGarbage(result); } } @@ -256,13 +257,11 @@ void enablesInPlaceDecompression() { @Test void rejectsGarbage() { // Given bytes that are not valid zstd data - byte[] garbage = "xx".getBytes(StandardCharsets.UTF_8); - // When the margin is requested - ThrowingCallable result = () -> ZstdFrame.decompressionMargin(garbage); + ThrowingCallable result = () -> ZstdFrame.decompressionMargin(ZstdFrameTest.GARBAGE); // Then it fails - assertThatThrownBy(result).isInstanceOf(ZstdException.class); + ZstdFrameTest.rejectsGarbage(result); } } @@ -475,4 +474,8 @@ void standardNativeFrameIsNotSkippable() { } } } + + private static void rejectsGarbage(ThrowingCallable result) { + assertThatThrownBy(result).isInstanceOf(ZstdException.class); + } }