ByteSourceBootstrapperSliceTest.java

package tools.jackson.core.unittest.json;

import java.io.ByteArrayOutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.junit.jupiter.api.Test;

import tools.jackson.core.JsonParser;
import tools.jackson.core.JsonToken;
import tools.jackson.core.ObjectReadContext;
import tools.jackson.core.exc.StreamReadException;
import tools.jackson.core.json.JsonFactory;
import tools.jackson.core.unittest.JacksonCoreTestBase;

import static org.junit.jupiter.api.Assertions.*;

// Input decoded via Reader and given as a byte[] slice (over 8 KiB, so not
// decoded via StringReader) must not be read past offset + length
class ByteSourceBootstrapperSliceTest extends JacksonCoreTestBase
{
    private final static String BOM = "\uFEFF";

    private final JsonFactory JSON_F = newStreamFactory();

    @Test
    void utf16BESliceNotReadPastEnd() throws Exception
    {
        _testSliceNotReadPastEnd(JSON_F, StandardCharsets.UTF_16BE, "");
    }

    @Test
    void utf16BEWithBOMSliceNotReadPastEnd() throws Exception
    {
        _testSliceNotReadPastEnd(JSON_F, StandardCharsets.UTF_16BE, BOM);
    }

    @Test
    void utf16LESliceNotReadPastEnd() throws Exception
    {
        _testSliceNotReadPastEnd(JSON_F, StandardCharsets.UTF_16LE, "");
    }

    @Test
    void utf16LEWithBOMSliceNotReadPastEnd() throws Exception
    {
        _testSliceNotReadPastEnd(JSON_F, StandardCharsets.UTF_16LE, BOM);
    }

    // UTF-8 only goes through Reader if canonicalization is disabled
    @Test
    void utf8NoCanonicalizationSliceNotReadPastEnd() throws Exception
    {
        final JsonFactory f = JsonFactory.builder()
                .disable(JsonFactory.Feature.CANONICALIZE_PROPERTY_NAMES)
                .build();
        _testSliceNotReadPastEnd(f, StandardCharsets.UTF_8, "");
    }

    private void _testSliceNotReadPastEnd(JsonFactory f, Charset cs, String prefix) throws Exception
    {
        // Incomplete document in slice; remainder is outside of it
        final char[] padding = new char[9000];
        Arrays.fill(padding, ' ');
        final byte[] pre = "[0,".getBytes(cs);
        final byte[] slice = (prefix + "[" + new String(padding) + "1").getBytes(cs);
        final ByteArrayOutputStream buf = new ByteArrayOutputStream();
        buf.write(pre);
        buf.write(slice);
        buf.write(",2]".getBytes(cs));

        try (JsonParser p = f.createParser(ObjectReadContext.empty(),
                buf.toByteArray(), pre.length, slice.length)) {
            assertToken(JsonToken.START_ARRAY, p.nextToken());
            assertToken(JsonToken.VALUE_NUMBER_INT, p.nextToken());
            assertEquals(1, p.getIntValue());
            StreamReadException e = assertThrows(StreamReadException.class, p::nextToken);
            verifyException(e, "Unexpected end-of-input");
        }
    }
}