Skip to content

Commit

Permalink
ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast
Browse files Browse the repository at this point in the history
on null Checksum input

More available() and read() tests
  • Loading branch information
garydgregory committed Jul 7, 2024
1 parent a3d1c98 commit c958b31
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Gary Gregory">BoundedInputStream.available() should return 0 when the stream is closed.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">CircularInputStream.available() should return 0 when the stream is closed.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">InfiniteCircularInputStream.available() should return 0 when the stream is closed.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">ChecksumInputStream(InputStream, Checksum, long, long) should fail-fast on null Checksum input.</action>
<!-- UPDATE -->
<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>
<action dev="ggregory" type="update" due-to="Dependabot">Bump tests commons-codec:commons-codec from 1.16.1 to 1.17.0.</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,38 @@
import java.util.zip.Adler32;
import java.util.zip.CRC32;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

/**
* Tests {@link ChecksumInputStream}.
*/
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];
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c958b31

Please sign in to comment.