From c958b3182ff2c57b44fb34069e4c519b68e81707 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Sun, 7 Jul 2024 18:13:43 -0400 Subject: [PATCH] ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast on null Checksum input More available() and read() tests --- src/changes/changes.xml | 1 + .../commons/io/input/ChecksumInputStream.java | 3 +- .../io/input/ChecksumInputStreamTest.java | 29 ++++++++++++++-- .../io/input/CircularInputStreamTest.java | 33 ++++++++++++------- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b735c6121e6..fa5d8307d7c 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -68,6 +68,7 @@ The type attribute can be add,update,fix,remove. BoundedInputStream.available() should return 0 when the stream is closed. CircularInputStream.available() should return 0 when the stream is closed. InfiniteCircularInputStream.available() should return 0 when the stream is closed. + ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast on null Checksum input. Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635. Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0. diff --git a/src/main/java/org/apache/commons/io/input/ChecksumInputStream.java b/src/main/java/org/apache/commons/io/input/ChecksumInputStream.java index d530edb9649..8af6c80ac56 100644 --- a/src/main/java/org/apache/commons/io/input/ChecksumInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ChecksumInputStream.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Objects; import java.util.zip.CheckedInputStream; import java.util.zip.Checksum; @@ -218,7 +219,7 @@ public static Builder builder() { */ private ChecksumInputStream(final InputStream in, final Checksum checksum, final long expectedChecksumValue, final long countThreshold) { - super(new CheckedInputStream(in, checksum)); + super(new CheckedInputStream(in, Objects.requireNonNull(checksum, "checksum"))); this.countThreshold = countThreshold; this.expectedChecksumValue = expectedChecksumValue; } diff --git a/src/test/java/org/apache/commons/io/input/ChecksumInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ChecksumInputStreamTest.java index f01eb0cb601..ec1dcfeb884 100644 --- a/src/test/java/org/apache/commons/io/input/ChecksumInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/ChecksumInputStreamTest.java @@ -27,6 +27,7 @@ import java.util.zip.Adler32; import java.util.zip.CRC32; +import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; /** @@ -34,17 +35,30 @@ */ public class ChecksumInputStreamTest { + private ChecksumInputStream createInputStream() throws IOException { + return ChecksumInputStream.builder().setCharSequence("Hi").setChecksum(new CRC32()).get(); + } + @SuppressWarnings("resource") @Test - public void testAvailable() throws Exception { + public void testAvailableAfterClose() throws Exception { final InputStream shadow; - try (InputStream in = ChecksumInputStream.builder().setCharSequence("Hi").get()) { + try (InputStream in = createInputStream()) { assertTrue(in.available() > 0); shadow = in; } assertEquals(0, shadow.available()); } + @Test + public void testAvailableAfterOpen() throws Exception { + try (InputStream in = createInputStream()) { + assertTrue(in.available() > 0); + assertEquals('H', in.read()); + assertTrue(in.available() > 0); + } + } + @Test public void testDefaultThresholdFailure() throws IOException { final byte[] byteArray = new byte[3]; @@ -94,6 +108,17 @@ public void testDefaultThresholdSuccess() throws IOException { } } + @SuppressWarnings("resource") + @Test + public void testReadAfterClose() throws Exception { + final InputStream shadow; + try (InputStream in = createInputStream()) { + assertTrue(in.available() > 0); + shadow = in; + } + assertEquals(IOUtils.EOF, shadow.read()); + } + @Test public void testReadTakingByteArrayThrowsException() throws IOException { final Adler32 adler32 = new Adler32(); diff --git a/src/test/java/org/apache/commons/io/input/CircularInputStreamTest.java b/src/test/java/org/apache/commons/io/input/CircularInputStreamTest.java index 282ad06ea15..c3a98f95d92 100644 --- a/src/test/java/org/apache/commons/io/input/CircularInputStreamTest.java +++ b/src/test/java/org/apache/commons/io/input/CircularInputStreamTest.java @@ -49,28 +49,25 @@ private InputStream createInputStream(final byte[] repeatContent, final long tar @SuppressWarnings("resource") @Test - public void testAvailable() throws Exception { + public void testAvailableAfterClose() throws Exception { final InputStream shadow; - try (InputStream in = createInputStream(new byte[] { 1, 2 }, 1)) { + try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) { assertTrue(in.available() > 0); + assertEquals(1, in.read()); + assertEquals(2, in.read()); + assertEquals(1, in.read()); shadow = in; } assertEquals(0, shadow.available()); } - @SuppressWarnings("resource") @Test - public void testAvailableAfterClose() throws Exception { - final InputStream shadow; - try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) { + public void testAvailableAfterOpen() throws Exception { + try (InputStream in = createInputStream(new byte[] { 1, 2 }, 1)) { assertTrue(in.available() > 0); assertEquals(1, in.read()); - assertEquals(2, in.read()); - assertEquals(1, in.read()); - shadow = in; + assertTrue(in.available() > 0); } - assertEquals(0, shadow.available()); - assertEquals(IOUtils.EOF, shadow.read()); } @Test @@ -117,6 +114,20 @@ public void testNullInputSize0() { assertThrows(NullPointerException.class, () -> createInputStream(null, 0)); } + @SuppressWarnings("resource") + @Test + public void testReaderAfterClose() throws Exception { + final InputStream shadow; + try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) { + assertTrue(in.available() > 0); + assertEquals(1, in.read()); + assertEquals(2, in.read()); + assertEquals(1, in.read()); + shadow = in; + } + assertEquals(IOUtils.EOF, shadow.read()); + } + @Test public void testWholeRangeOfBytes() throws IOException { final int size = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;