Skip to content

Commit c958b31

Browse files
committed
ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast
on null Checksum input More available() and read() tests
1 parent a3d1c98 commit c958b31

4 files changed

Lines changed: 52 additions & 14 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
6868
<action dev="ggregory" type="add" due-to="Gary Gregory">BoundedInputStream.available() should return 0 when the stream is closed.</action>
6969
<action dev="ggregory" type="add" due-to="Gary Gregory">CircularInputStream.available() should return 0 when the stream is closed.</action>
7070
<action dev="ggregory" type="add" due-to="Gary Gregory">InfiniteCircularInputStream.available() should return 0 when the stream is closed.</action>
71+
<action dev="ggregory" type="add" due-to="Gary Gregory">ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast on null Checksum input.</action>
7172
<!-- UPDATE -->
7273
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons.bytebuddy.version from 1.14.13 to 1.14.17 #615, #621, #631, #635.</action>
7374
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>

src/main/java/org/apache/commons/io/input/ChecksumInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.io.InputStream;
23+
import java.util.Objects;
2324
import java.util.zip.CheckedInputStream;
2425
import java.util.zip.Checksum;
2526

@@ -218,7 +219,7 @@ public static Builder builder() {
218219
*/
219220
private ChecksumInputStream(final InputStream in, final Checksum checksum, final long expectedChecksumValue,
220221
final long countThreshold) {
221-
super(new CheckedInputStream(in, checksum));
222+
super(new CheckedInputStream(in, Objects.requireNonNull(checksum, "checksum")));
222223
this.countThreshold = countThreshold;
223224
this.expectedChecksumValue = expectedChecksumValue;
224225
}

src/test/java/org/apache/commons/io/input/ChecksumInputStreamTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,38 @@
2727
import java.util.zip.Adler32;
2828
import java.util.zip.CRC32;
2929

30+
import org.apache.commons.io.IOUtils;
3031
import org.junit.jupiter.api.Test;
3132

3233
/**
3334
* Tests {@link ChecksumInputStream}.
3435
*/
3536
public class ChecksumInputStreamTest {
3637

38+
private ChecksumInputStream createInputStream() throws IOException {
39+
return ChecksumInputStream.builder().setCharSequence("Hi").setChecksum(new CRC32()).get();
40+
}
41+
3742
@SuppressWarnings("resource")
3843
@Test
39-
public void testAvailable() throws Exception {
44+
public void testAvailableAfterClose() throws Exception {
4045
final InputStream shadow;
41-
try (InputStream in = ChecksumInputStream.builder().setCharSequence("Hi").get()) {
46+
try (InputStream in = createInputStream()) {
4247
assertTrue(in.available() > 0);
4348
shadow = in;
4449
}
4550
assertEquals(0, shadow.available());
4651
}
4752

53+
@Test
54+
public void testAvailableAfterOpen() throws Exception {
55+
try (InputStream in = createInputStream()) {
56+
assertTrue(in.available() > 0);
57+
assertEquals('H', in.read());
58+
assertTrue(in.available() > 0);
59+
}
60+
}
61+
4862
@Test
4963
public void testDefaultThresholdFailure() throws IOException {
5064
final byte[] byteArray = new byte[3];
@@ -94,6 +108,17 @@ public void testDefaultThresholdSuccess() throws IOException {
94108
}
95109
}
96110

111+
@SuppressWarnings("resource")
112+
@Test
113+
public void testReadAfterClose() throws Exception {
114+
final InputStream shadow;
115+
try (InputStream in = createInputStream()) {
116+
assertTrue(in.available() > 0);
117+
shadow = in;
118+
}
119+
assertEquals(IOUtils.EOF, shadow.read());
120+
}
121+
97122
@Test
98123
public void testReadTakingByteArrayThrowsException() throws IOException {
99124
final Adler32 adler32 = new Adler32();

src/test/java/org/apache/commons/io/input/CircularInputStreamTest.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,25 @@ private InputStream createInputStream(final byte[] repeatContent, final long tar
4949

5050
@SuppressWarnings("resource")
5151
@Test
52-
public void testAvailable() throws Exception {
52+
public void testAvailableAfterClose() throws Exception {
5353
final InputStream shadow;
54-
try (InputStream in = createInputStream(new byte[] { 1, 2 }, 1)) {
54+
try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) {
5555
assertTrue(in.available() > 0);
56+
assertEquals(1, in.read());
57+
assertEquals(2, in.read());
58+
assertEquals(1, in.read());
5659
shadow = in;
5760
}
5861
assertEquals(0, shadow.available());
5962
}
6063

61-
@SuppressWarnings("resource")
6264
@Test
63-
public void testAvailableAfterClose() throws Exception {
64-
final InputStream shadow;
65-
try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) {
65+
public void testAvailableAfterOpen() throws Exception {
66+
try (InputStream in = createInputStream(new byte[] { 1, 2 }, 1)) {
6667
assertTrue(in.available() > 0);
6768
assertEquals(1, in.read());
68-
assertEquals(2, in.read());
69-
assertEquals(1, in.read());
70-
shadow = in;
69+
assertTrue(in.available() > 0);
7170
}
72-
assertEquals(0, shadow.available());
73-
assertEquals(IOUtils.EOF, shadow.read());
7471
}
7572

7673
@Test
@@ -117,6 +114,20 @@ public void testNullInputSize0() {
117114
assertThrows(NullPointerException.class, () -> createInputStream(null, 0));
118115
}
119116

117+
@SuppressWarnings("resource")
118+
@Test
119+
public void testReaderAfterClose() throws Exception {
120+
final InputStream shadow;
121+
try (InputStream in = createInputStream(new byte[] { 1, 2 }, 4)) {
122+
assertTrue(in.available() > 0);
123+
assertEquals(1, in.read());
124+
assertEquals(2, in.read());
125+
assertEquals(1, in.read());
126+
shadow = in;
127+
}
128+
assertEquals(IOUtils.EOF, shadow.read());
129+
}
130+
120131
@Test
121132
public void testWholeRangeOfBytes() throws IOException {
122133
final int size = Byte.MAX_VALUE - Byte.MIN_VALUE + 1;

0 commit comments

Comments
 (0)