|
| 1 | +package com.fasterxml.jackson.dataformat.smile; |
| 2 | + |
| 3 | +import java.io.InputStream; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.core.exc.StreamReadException; |
| 6 | +import com.fasterxml.jackson.core.io.IOContext; |
| 7 | +import com.fasterxml.jackson.core.sym.ByteQuadsCanonicalizer; |
| 8 | +import com.fasterxml.jackson.dataformat.smile.testutil.ThrottledInputStream; |
| 9 | + |
| 10 | +public class ParserInternalsTest extends BaseTestForSmile |
| 11 | +{ |
| 12 | + private final ByteQuadsCanonicalizer ROOT_SYMBOLS = ByteQuadsCanonicalizer.createRoot(); |
| 13 | + |
| 14 | + public void testPositiveVIntGoodCases() throws Exception |
| 15 | + { |
| 16 | + // First, couple of known good cases |
| 17 | + _verifyVIntOk(0, new byte[] { (byte) 0x80 }); |
| 18 | + _verifyVIntOk(3, new byte[] { (byte) 0x83 }); |
| 19 | + _verifyVIntOk(Integer.MAX_VALUE, new byte[] { |
| 20 | + 0x0F, 0x7F, 0x7F, 0x7F, (byte) 0xBF |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + public void testPositiveVIntOverflows() throws Exception |
| 25 | + { |
| 26 | + // Bad: ends at 5th, but overflows |
| 27 | + _verifyVIntBad(new byte[] { |
| 28 | + (byte) 0x7F, 0x7F, 0x7F, 0x7F, (byte) 0xBF |
| 29 | + }, "Overflow in VInt", "1st byte (0x7F)" |
| 30 | + ); |
| 31 | + // Bad: does not end at 5th (and overflows that way) |
| 32 | + _verifyVIntBad(new byte[] { |
| 33 | + (byte) 0x7F, 0x7F, 0x7F, 0x7F, (byte) 0x7F |
| 34 | + }, "Overflow in VInt", "5th byte (0x7F)" |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + private void _verifyVIntOk(int expValue, byte[] doc) throws Exception |
| 39 | + { |
| 40 | + // First, read with no boundaries |
| 41 | + try (SmileParser p = _minimalParser(doc)) { |
| 42 | + assertEquals(expValue, p._readUnsignedVInt()); |
| 43 | + } |
| 44 | + |
| 45 | + // Then incremental read |
| 46 | + try (SmileParser p = _minimalParser(new ThrottledInputStream(doc, 1))) { |
| 47 | + assertEquals(expValue, p._readUnsignedVInt()); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + private void _verifyVIntBad(byte[] doc, String... msgs) throws Exception |
| 52 | + { |
| 53 | + // First, read with no boundaries |
| 54 | + try (SmileParser p = _minimalParser(doc)) { |
| 55 | + try { |
| 56 | + int val = p._readUnsignedVInt(); |
| 57 | + fail("Should not pass, got 0x"+Integer.toHexString(val)); |
| 58 | + } catch (StreamReadException e) { |
| 59 | + verifyException(e, msgs); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Then incremental read |
| 64 | + try (SmileParser p = _minimalParser(new ThrottledInputStream(doc, 1))) { |
| 65 | + try { |
| 66 | + int val = p._readUnsignedVInt(); |
| 67 | + fail("Should not pass, got 0x"+Integer.toHexString(val)); |
| 68 | + } catch (StreamReadException e) { |
| 69 | + verifyException(e, msgs); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private SmileParser _minimalParser(byte[] doc) { |
| 75 | + IOContext ctxt = new IOContext(null, doc, false); |
| 76 | + return new SmileParser(ctxt, // IOContext |
| 77 | + 0, 0, // flags |
| 78 | + null, // (codec) |
| 79 | + ROOT_SYMBOLS.makeChild(0), // ByteQuadsCanonicalizer |
| 80 | + null, // InputStream |
| 81 | + doc, 0, doc.length, false); |
| 82 | + } |
| 83 | + |
| 84 | + private SmileParser _minimalParser(InputStream in) { |
| 85 | + IOContext ctxt = new IOContext(null, in, false); |
| 86 | + return new SmileParser(ctxt, // IOContext |
| 87 | + 0, 0, // flags |
| 88 | + null, // (codec) |
| 89 | + ROOT_SYMBOLS.makeChild(0), // ByteQuadsCanonicalizer |
| 90 | + in, // InputStream |
| 91 | + new byte[1], 0, 0, false); |
| 92 | + } |
| 93 | +} |
0 commit comments