From ec572f1cbd2a9fc954c383ee623eeb2522aea4a5 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Thu, 21 Dec 2023 18:28:28 -0800 Subject: [PATCH] Add failing tests for #428 (#430) --- .../ion/failing/IonNumberOverflow428Test.java | 76 +++++++++++++++++++ .../ion/fuzz/Fuzz_65062_VarintTest.java | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonNumberOverflow428Test.java diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonNumberOverflow428Test.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonNumberOverflow428Test.java new file mode 100644 index 000000000..128353045 --- /dev/null +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/failing/IonNumberOverflow428Test.java @@ -0,0 +1,76 @@ +package com.fasterxml.jackson.dataformat.ion.failing; + +import org.hamcrest.Matchers; +import org.junit.Test; + +import com.fasterxml.jackson.core.exc.InputCoercionException; +import com.fasterxml.jackson.dataformat.ion.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.math.BigInteger; + +// for [dataformats-ion#428] +public class IonNumberOverflow428Test +{ + private final IonObjectMapper MAPPER = IonObjectMapper + .builderForBinaryWriters() + .build(); + + // Test to ensure we handle value overflow (long->int) correctly + @Test + public void testIntCoercionOverflow() throws Exception { + _testIntCoercionFail(Long.valueOf(3L * Integer.MAX_VALUE / 2L)); + } + + @Test + public void testIntCoercionUnderflow() throws Exception { + _testIntCoercionFail(Long.valueOf(3L * Integer.MIN_VALUE / 2L)); + } + + private void _testIntCoercionFail(Long input) throws Exception + { + final byte[] doc = MAPPER.writeValueAsBytes(input); + + // First, verify correct value decoding + assertEquals(input, MAPPER.readValue(doc, Long.class)); + + // And then over/underflow + try { + Integer result = MAPPER.readValue(doc, Integer.class); + fail("Should not pass; got: "+result+" (from "+input+")"); + } catch (InputCoercionException e) { + assertThat(e.getMessage(), Matchers.containsString("out of range of int")); + } + } + + // Test to ensure we handle value overflow (BigInteger->long) correctly + @Test + public void testLongCoercionOverflow() throws Exception { + _testLongCoercionFail(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.TEN)); + } + + @Test + public void testLongCoercionUnderflow() throws Exception { + _testLongCoercionFail(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.TEN)); + } + + private void _testLongCoercionFail(BigInteger input) throws Exception + { + final byte[] doc = MAPPER.writeValueAsBytes(input); + + // First, verify correct value decoding + assertEquals(input, MAPPER.readValue(doc, BigInteger.class)); + + // And then over/underflow + try { + Long result = MAPPER.readValue(doc, Long.class); + fail("Should not pass; got: "+result+" (from "+input+")"); + } catch (InputCoercionException e) { + assertThat(e.getMessage(), Matchers.containsString("out of range of long")); + } + } + +} diff --git a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java index b9523acff..8ecc5863f 100644 --- a/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java +++ b/ion/src/test/java/com/fasterxml/jackson/dataformat/ion/fuzz/Fuzz_65062_VarintTest.java @@ -17,7 +17,7 @@ // https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=65062 public class Fuzz_65062_VarintTest { - final IonObjectMapper MAPPER = IonObjectMapper.builder().build(); + private final IonObjectMapper MAPPER = IonObjectMapper.builder().build(); @Test public void testFuzz65062_Varint() throws Exception {