|
10 | 10 |
|
11 | 11 | import com.fasterxml.jackson.core.JsonParser;
|
12 | 12 | import com.fasterxml.jackson.core.JsonToken;
|
13 |
| - |
| 13 | +import com.fasterxml.jackson.core.exc.StreamReadException; |
14 | 14 | import com.fasterxml.jackson.databind.JsonNode;
|
15 | 15 | import com.fasterxml.jackson.databind.ObjectMapper;
|
16 | 16 | import com.fasterxml.jackson.databind.node.BinaryNode;
|
@@ -116,7 +116,36 @@ public void _testMultipleBinaryFields(ObjectMapper mapper) throws Exception
|
116 | 116 | Assert.assertArrayEquals(input.bytes3, result.bytes3);
|
117 | 117 | }
|
118 | 118 |
|
119 |
| - public void _testBinary(ObjectMapper mapper, int size) throws Exception |
| 119 | + // Let's also verify handling of truncated (invalid) binary values |
| 120 | + public void testTruncatedBinaryValues() throws Exception { |
| 121 | + _testTruncatedBinaryValues(MAPPER_7BITS); |
| 122 | + _testTruncatedBinaryValues(MAPPER_RAW); |
| 123 | + } |
| 124 | + |
| 125 | + private void _testTruncatedBinaryValues(ObjectMapper mapper) throws Exception { |
| 126 | + // Just need two, really; non-huge (below ~250k) and huge (250kB+) |
| 127 | + _testTruncatedBinaryValues(mapper, 99000); |
| 128 | + _testTruncatedBinaryValues(mapper, 299003); |
| 129 | + } |
| 130 | + |
| 131 | + private void _testTruncatedBinaryValues(ObjectMapper mapper, int size) throws Exception |
| 132 | + { |
| 133 | + byte[] payload = _bytes(size, 0); |
| 134 | + byte[] doc = mapper.writeValueAsBytes(payload); |
| 135 | + |
| 136 | + // and then lop off like last 4k |
| 137 | + byte[] truncDoc = Arrays.copyOfRange(doc, 0, doc.length - 4000); |
| 138 | + |
| 139 | + try { |
| 140 | + mapper.readValue(truncDoc, Object.class); |
| 141 | + fail("Should not pass"); |
| 142 | + } catch (StreamReadException e) { |
| 143 | + verifyException(e, "Unexpected end-of-input for Binary value"); |
| 144 | + verifyException(e, "expected "+size+" "); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void _testBinary(ObjectMapper mapper, int size) throws Exception |
120 | 149 | {
|
121 | 150 | byte[] input = _bytes(size, 0);
|
122 | 151 |
|
|
0 commit comments