|
| 1 | +package com.fasterxml.jackson.core.read; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Disabled; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.api.Timeout; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.core.JUnit5TestBase; |
| 8 | +import com.fasterxml.jackson.core.JsonFactory; |
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.core.JsonToken; |
| 11 | +import com.fasterxml.jackson.core.StreamReadConstraints; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 15 | + |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +// https://github.com/FasterXML/jackson-core/pull/1352 |
| 20 | +class TestReadHumongousString extends JUnit5TestBase |
| 21 | +{ |
| 22 | + // disabled because it takes too much memory to run |
| 23 | + @Disabled |
| 24 | + // Since we might get infinite loop: |
| 25 | + @Timeout(value = 10, unit = TimeUnit.SECONDS, threadMode = Timeout.ThreadMode.SEPARATE_THREAD) |
| 26 | + @Test |
| 27 | + void testLargeStringDeserialization() throws Exception { |
| 28 | + final int len = Integer.MAX_VALUE - 1024; |
| 29 | + final byte[] largeByteString = makeLongByteString(len); |
| 30 | + final JsonFactory f = JsonFactory.builder() |
| 31 | + .streamReadConstraints(StreamReadConstraints.builder() |
| 32 | + .maxStringLength(Integer.MAX_VALUE) |
| 33 | + .build()) |
| 34 | + .build(); |
| 35 | + |
| 36 | + try (JsonParser parser = f.createParser(largeByteString)) { |
| 37 | + assertToken(JsonToken.VALUE_STRING, parser.nextToken()); |
| 38 | + // Let's not construct String but just check that length is |
| 39 | + // expected: this avoids having to allocate 4 gig more of heap |
| 40 | + // for test -- should still trigger problem if fix not valid |
| 41 | + assertEquals(len, parser.getTextLength()); |
| 42 | + // TODO: could use streaming accessor (`JsonParser.getText(Writer)`) |
| 43 | + assertNull(parser.nextToken()); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + private byte[] makeLongByteString(int length) { |
| 49 | + final byte[] result = new byte[length + 2]; |
| 50 | + Arrays.fill(result, (byte) 'a'); |
| 51 | + result[0] = '\"'; |
| 52 | + result[length + 1] = '\"'; |
| 53 | + return result; |
| 54 | + } |
| 55 | +} |
0 commit comments