|
| 1 | +package com.fasterxml.jackson.core.read; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.fail; |
| 5 | + |
| 6 | +import java.io.StringReader; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.core.*; |
| 12 | +import com.fasterxml.jackson.core.exc.StreamReadException; |
| 13 | +import com.fasterxml.jackson.core.json.JsonReadFeature; |
| 14 | +import com.fasterxml.jackson.core.json.async.NonBlockingJsonParser; |
| 15 | + |
| 16 | +// for [core#633]: optionally allow Record-Separator ctrl char |
| 17 | +class NonStandardAllowRSTest |
| 18 | + extends JUnit5TestBase |
| 19 | +{ |
| 20 | + @Test |
| 21 | + void recordSeparatorEnabled() throws Exception { |
| 22 | + doRecordSeparationTest(true); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void recordSeparatorDisabled() throws Exception { |
| 27 | + doRecordSeparationTest(false); |
| 28 | + } |
| 29 | + |
| 30 | + // Testing record separation for all parser implementations |
| 31 | + private void doRecordSeparationTest(boolean recordSeparation) throws Exception { |
| 32 | + String contents = "{\"key\":true}\u001E"; |
| 33 | + JsonFactory factory = JsonFactory.builder() |
| 34 | + .configure(JsonReadFeature.ALLOW_RS_CONTROL_CHAR, recordSeparation) |
| 35 | + .build(); |
| 36 | + try (JsonParser parser = factory.createParser(contents)) { |
| 37 | + verifyRecordSeparation(parser, recordSeparation); |
| 38 | + } |
| 39 | + try (JsonParser parser = factory.createParser(new StringReader(contents))) { |
| 40 | + verifyRecordSeparation(parser, recordSeparation); |
| 41 | + } |
| 42 | + try (JsonParser parser = factory.createParser(contents.getBytes(StandardCharsets.UTF_8))) { |
| 43 | + verifyRecordSeparation(parser, recordSeparation); |
| 44 | + } |
| 45 | + try (NonBlockingJsonParser parser = (NonBlockingJsonParser) factory.createNonBlockingByteArrayParser()) { |
| 46 | + byte[] data = contents.getBytes(StandardCharsets.UTF_8); |
| 47 | + parser.feedInput(data, 0, data.length); |
| 48 | + parser.endOfInput(); |
| 49 | + verifyRecordSeparation(parser, recordSeparation); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private void verifyRecordSeparation(JsonParser parser, boolean recordSeparation) throws Exception { |
| 54 | + try { |
| 55 | + assertToken(JsonToken.START_OBJECT, parser.nextToken()); |
| 56 | + String field1 = parser.nextFieldName(); |
| 57 | + assertEquals("key", field1); |
| 58 | + assertToken(JsonToken.VALUE_TRUE, parser.nextToken()); |
| 59 | + assertToken(JsonToken.END_OBJECT, parser.nextToken()); |
| 60 | + parser.nextToken(); // RS token |
| 61 | + if (!recordSeparation) { |
| 62 | + fail("Should have thrown an exception"); |
| 63 | + } |
| 64 | + } catch (StreamReadException e) { |
| 65 | + if (!recordSeparation) { |
| 66 | + verifyException(e, "Illegal character ((CTRL-CHAR"); |
| 67 | + verifyException(e, "consider enabling `JsonReadFeature.ALLOW_RS_CONTROL_CHAR`"); |
| 68 | + } else { |
| 69 | + throw e; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments