Skip to content

Commit a831f19

Browse files
committed
Bit more smile/binary testing
1 parent 03daa88 commit a831f19

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

smile/src/test/java/com/fasterxml/jackson/dataformat/smile/mapper/BinaryReadTest.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import com.fasterxml.jackson.core.JsonParser;
1212
import com.fasterxml.jackson.core.JsonToken;
13-
13+
import com.fasterxml.jackson.core.exc.StreamReadException;
1414
import com.fasterxml.jackson.databind.JsonNode;
1515
import com.fasterxml.jackson.databind.ObjectMapper;
1616
import com.fasterxml.jackson.databind.node.BinaryNode;
@@ -116,7 +116,36 @@ public void _testMultipleBinaryFields(ObjectMapper mapper) throws Exception
116116
Assert.assertArrayEquals(input.bytes3, result.bytes3);
117117
}
118118

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
120149
{
121150
byte[] input = _bytes(size, 0);
122151

0 commit comments

Comments
 (0)