Skip to content

Commit 3849e0f

Browse files
committed
Add failing tests for #428
1 parent 04c3d35 commit 3849e0f

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fasterxml.jackson.dataformat.ion.failing;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Test;
5+
6+
import com.fasterxml.jackson.core.exc.InputCoercionException;
7+
import com.fasterxml.jackson.dataformat.ion.*;
8+
9+
import static org.hamcrest.MatcherAssert.assertThat;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.fail;
12+
13+
import java.math.BigInteger;
14+
15+
// for [dataformats-ion#428]
16+
public class IonNumberOverflow428Test
17+
{
18+
private final IonObjectMapper MAPPER = IonObjectMapper
19+
.builderForBinaryWriters()
20+
.build();
21+
22+
// Test to ensure we handle value overflow (long->int) correctly
23+
@Test
24+
public void testIntCoercionOverflow() throws Exception {
25+
_testIntCoercionFail(Long.valueOf(3L * Integer.MAX_VALUE / 2L));
26+
}
27+
28+
@Test
29+
public void testIntCoercionUnderflow() throws Exception {
30+
_testIntCoercionFail(Long.valueOf(3L * Integer.MIN_VALUE / 2L));
31+
}
32+
33+
private void _testIntCoercionFail(Long input) throws Exception
34+
{
35+
final byte[] doc = MAPPER.writeValueAsBytes(input);
36+
37+
// First, verify correct value decoding
38+
assertEquals(input, MAPPER.readValue(doc, Long.class));
39+
40+
// And then over/underflow
41+
try {
42+
Integer result = MAPPER.readValue(doc, Integer.class);
43+
fail("Should not pass; got: "+result+" (from "+input+")");
44+
} catch (InputCoercionException e) {
45+
assertThat(e.getMessage(), Matchers.containsString("out of range of int"));
46+
}
47+
}
48+
49+
// Test to ensure we handle value overflow (BigInteger->long) correctly
50+
@Test
51+
public void testLongCoercionOverflow() throws Exception {
52+
_testLongCoercionFail(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN));
53+
}
54+
55+
@Test
56+
public void testLongCoercionUnderflow() throws Exception {
57+
_testLongCoercionFail(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN));
58+
}
59+
60+
private void _testLongCoercionFail(BigInteger input) throws Exception
61+
{
62+
final byte[] doc = MAPPER.writeValueAsBytes(input);
63+
64+
// First, verify correct value decoding
65+
assertEquals(input, MAPPER.readValue(doc, BigInteger.class));
66+
67+
// And then over/underflow
68+
try {
69+
Long result = MAPPER.readValue(doc, Long.class);
70+
fail("Should not pass; got: "+result+" (from "+input+")");
71+
} catch (InputCoercionException e) {
72+
assertThat(e.getMessage(), Matchers.containsString("out of range of long"));
73+
}
74+
}
75+
76+
}

ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
// https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=65062
1818
public class Fuzz_65062_VarintTest
1919
{
20-
final IonObjectMapper MAPPER = IonObjectMapper.builder().build();
20+
private final IonObjectMapper MAPPER = IonObjectMapper.builder().build();
2121

2222
@Test
2323
public void testFuzz65062_Varint() throws Exception {

0 commit comments

Comments
 (0)