forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check number lengths (FasterXML#3687)
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/test/java/com/fasterxml/jackson/databind/deser/TestBigNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.StreamReadConstraints; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
||
public class TestBigNumbers extends BaseMapTest | ||
{ | ||
static class BigDecimalWrapper { | ||
BigDecimal number; | ||
|
||
public BigDecimalWrapper() {} | ||
|
||
public BigDecimalWrapper(BigDecimal number) { | ||
this.number = number; | ||
} | ||
|
||
public void setNumber(BigDecimal number) { | ||
this.number = number; | ||
} | ||
} | ||
|
||
static class BigIntegerWrapper { | ||
BigInteger number; | ||
|
||
public BigIntegerWrapper() {} | ||
|
||
public BigIntegerWrapper(BigInteger number) { | ||
this.number = number; | ||
} | ||
|
||
public void setNumber(BigInteger number) { | ||
this.number = number; | ||
} | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Tests | ||
/********************************************************** | ||
*/ | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
private final ObjectMapper newJsonMapperWithUnlimitedNumberSizeSupport() { | ||
JsonFactory jsonFactory = JsonFactory.builder() | ||
.streamReadConstraints(StreamReadConstraints.builder().maxNumberLength(Integer.MAX_VALUE).build()) | ||
.build(); | ||
return JsonMapper.builder(jsonFactory).build(); | ||
} | ||
|
||
public void testDouble() throws Exception | ||
{ | ||
try { | ||
MAPPER.readValue(generateJson("d"), DoubleWrapper.class); | ||
} catch (JsonMappingException jsonMappingException) { | ||
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), | ||
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); | ||
} | ||
} | ||
|
||
public void testDoubleUnlimited() throws Exception | ||
{ | ||
DoubleWrapper dw = | ||
newJsonMapperWithUnlimitedNumberSizeSupport().readValue(generateJson("d"), DoubleWrapper.class); | ||
assertNotNull(dw); | ||
} | ||
|
||
public void testBigDecimal() throws Exception | ||
{ | ||
try { | ||
MAPPER.readValue(generateJson("number"), BigDecimalWrapper.class); | ||
} catch (JsonMappingException jsonMappingException) { | ||
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), | ||
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); | ||
} | ||
} | ||
|
||
public void testBigDecimalUnlimited() throws Exception | ||
{ | ||
BigDecimalWrapper bdw = | ||
newJsonMapperWithUnlimitedNumberSizeSupport() | ||
.readValue(generateJson("number"), BigDecimalWrapper.class); | ||
assertNotNull(bdw); | ||
} | ||
|
||
public void testBigInteger() throws Exception | ||
{ | ||
try { | ||
MAPPER.readValue(generateJson("number"), BigIntegerWrapper.class); | ||
} catch (JsonMappingException jsonMappingException) { | ||
assertTrue("unexpected exception message: " + jsonMappingException.getMessage(), | ||
jsonMappingException.getMessage().startsWith("Malformed numeric value ([number with 1200 characters])")); | ||
} | ||
} | ||
|
||
public void testBigIntegerUnlimited() throws Exception | ||
{ | ||
BigIntegerWrapper bdw = | ||
newJsonMapperWithUnlimitedNumberSizeSupport() | ||
.readValue(generateJson("number"), BigIntegerWrapper.class); | ||
assertNotNull(bdw); | ||
} | ||
|
||
private String generateJson(final String fieldName) { | ||
final int len = 1200; | ||
final StringBuilder sb = new StringBuilder(); | ||
sb.append("{\"") | ||
.append(fieldName) | ||
.append("\": "); | ||
for (int i = 0; i < len; i++) { | ||
sb.append(1); | ||
} | ||
sb.append("}"); | ||
return sb.toString(); | ||
} | ||
} |