From 8c1c6f8a58a98ab45d08b8c6391ffeef3cefc5eb Mon Sep 17 00:00:00 2001 From: Joshua Barr Date: Fri, 26 Jan 2024 12:34:40 -0800 Subject: [PATCH] Add test coverage for maximum buffer size --- .../ion/impl/ResizingPipedInputStream.java | 2 +- .../amazon/ion/impl/_Private_IonConstants.java | 3 ++- .../ion/impl/ResizingPipedInputStreamTest.java | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/amazon/ion/impl/ResizingPipedInputStream.java b/src/main/java/com/amazon/ion/impl/ResizingPipedInputStream.java index dd09fd1715..16826d753d 100644 --- a/src/main/java/com/amazon/ion/impl/ResizingPipedInputStream.java +++ b/src/main/java/com/amazon/ion/impl/ResizingPipedInputStream.java @@ -152,7 +152,7 @@ public ResizingPipedInputStream(final int initialBufferSize) { if (initialBufferSize < 1) { throw new IllegalArgumentException("Initial buffer size must be at least 1."); } - if (initialBufferSize > _Private_IonConstants.ARRAY_MAXIMUM_SIZE) { + if (maximumBufferSize > _Private_IonConstants.ARRAY_MAXIMUM_SIZE) { throw new IllegalArgumentException("Initial buffer size must be at most " + _Private_IonConstants.ARRAY_MAXIMUM_SIZE + "."); } if (maximumBufferSize < initialBufferSize) { diff --git a/src/main/java/com/amazon/ion/impl/_Private_IonConstants.java b/src/main/java/com/amazon/ion/impl/_Private_IonConstants.java index e0fe8cc0c3..d7c706b36d 100644 --- a/src/main/java/com/amazon/ion/impl/_Private_IonConstants.java +++ b/src/main/java/com/amazon/ion/impl/_Private_IonConstants.java @@ -26,7 +26,8 @@ public final class _Private_IonConstants // Integer.MAX_VALUE. True maximum array size is a JVM implementation detail, and varies by type and by JVM. // We have this pinned at Integer.MAX_VALUE - 8 because that's a fairly common value in the JDK itself, in // classes such as ConcurrentHashMap, InputStream, Hashtable, ByteArrayChannel, etc. - // In testing against a variety of JVMs and types the smallest maximum size I've seen is Integer.MAX_VALUE - 6 + // In testing against a variety of JVMs and types the smallest maximum size I've seen is Integer.MAX_VALUE - 2, + // and the smallest maximum size I've seen reported is Integer.MAX_VALUE - 6. public static final int ARRAY_MAXIMUM_SIZE = Integer.MAX_VALUE - 8; private _Private_IonConstants() { } diff --git a/src/test/java/com/amazon/ion/impl/ResizingPipedInputStreamTest.java b/src/test/java/com/amazon/ion/impl/ResizingPipedInputStreamTest.java index d3d12d4ebb..00acac82b7 100644 --- a/src/test/java/com/amazon/ion/impl/ResizingPipedInputStreamTest.java +++ b/src/test/java/com/amazon/ion/impl/ResizingPipedInputStreamTest.java @@ -21,11 +21,9 @@ import java.util.zip.GZIPOutputStream; import static com.amazon.ion.BitUtils.bytes; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; import static org.junit.runners.Parameterized.Parameter; import static org.junit.runners.Parameterized.Parameters; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; @RunWith(Parameterized.class) public class ResizingPipedInputStreamTest { @@ -469,16 +467,20 @@ public void truncate() throws Exception { assertEquals(0, input.available()); } - @Test(expected = IllegalArgumentException.class) + @Test public void errorsOnInvalidInitialBufferSize() { - new ResizingPipedInputStream(0); + assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(0)); } - @Test(expected = IllegalArgumentException.class) - public void errorsOnInvalidMaximumBufferSize() { - new ResizingPipedInputStream(10, 9, useBoundary); + @Test + public void errorsOnMaximumBufferSizeSmallerThanInitialSize() { + assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(10, 9, useBoundary)); } + @Test + public void errorsOnInvalidMaximumBufferSize() { + assertThrows(IllegalArgumentException.class, () -> new ResizingPipedInputStream(10, Integer.MAX_VALUE, useBoundary)); + } @Test public void initialBufferSizeAndCapacity() { assertEquals(bufferSize, input.getInitialBufferSize());