Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failing tests for #428 #430

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down