diff options
author | Dominik Stadler <centic@apache.org> | 2019-12-14 13:10:17 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2019-12-14 13:10:17 +0000 |
commit | befd31663f10a2059e2a1f6c7111955f4312e17b (patch) | |
tree | b782a5b0fe8334495c33d8e5a106faae0cc6513f /src/testcases | |
parent | 57b0576f63fffcbc55afc58e3e9d186d2d8c4e72 (diff) | |
download | poi-befd31663f10a2059e2a1f6c7111955f4312e17b.tar.gz poi-befd31663f10a2059e2a1f6c7111955f4312e17b.zip |
Bug 63569: Adjust handling of check for max allocation of byte array
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871506 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases')
-rw-r--r-- | src/testcases/org/apache/poi/util/TestIOUtils.java | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/testcases/org/apache/poi/util/TestIOUtils.java b/src/testcases/org/apache/poi/util/TestIOUtils.java index 1445cb9978..73faa9a57e 100644 --- a/src/testcases/org/apache/poi/util/TestIOUtils.java +++ b/src/testcases/org/apache/poi/util/TestIOUtils.java @@ -19,7 +19,9 @@ package org.apache.poi.util; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -202,6 +204,62 @@ public final class TestIOUtils { assertEquals("length: "+LENGTH, 10000, skipped); } + @Test + public void testSetMaxOverride() throws IOException { + ByteArrayInputStream stream = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); + byte[] bytes = IOUtils.toByteArray(stream); + assertNotNull(bytes); + assertEquals("abc", new String(bytes, StandardCharsets.UTF_8)); + } + + @Test + public void testSetMaxOverrideLimit() throws IOException { + IOUtils.setByteArrayMaxOverride(30 * 1024 * 1024); + try { + ByteArrayInputStream stream = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); + byte[] bytes = IOUtils.toByteArray(stream); + assertNotNull(bytes); + assertEquals("abc", new String(bytes, StandardCharsets.UTF_8)); + } finally { + IOUtils.setByteArrayMaxOverride(-1); + } + } + + @Test + public void testSetMaxOverrideOverLimit() throws IOException { + IOUtils.setByteArrayMaxOverride(2); + try { + ByteArrayInputStream stream = new ByteArrayInputStream("abc".getBytes(StandardCharsets.UTF_8)); + try { + IOUtils.toByteArray(stream); + fail("Should have caught an Exception here"); + } catch (RecordFormatException e) { + // expected + } + } finally { + IOUtils.setByteArrayMaxOverride(-1); + } + } + + @Test + public void testSafelyAllocate() { + byte[] bytes = IOUtils.safelyAllocate(30, 200); + assertNotNull(bytes); + assertEquals(30, bytes.length); + } + + @Test + public void testSafelyAllocateLimit() { + IOUtils.setByteArrayMaxOverride(40); + try { + byte[] bytes = IOUtils.safelyAllocate(30, 200); + assertNotNull(bytes); + assertEquals(30, bytes.length); + } finally { + IOUtils.setByteArrayMaxOverride(-1); + } + } + /** * This returns 0 for the first call to skip and then reads * as requested. This tests that the fallback to read() works. |