|
| 1 | +package com.google.gson.functional; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.junit.Assert.assertThrows; |
| 5 | + |
| 6 | +import com.google.gson.Gson; |
| 7 | +import com.google.gson.JsonParseException; |
| 8 | +import com.google.gson.JsonPrimitive; |
| 9 | +import com.google.gson.JsonSyntaxException; |
| 10 | +import com.google.gson.ToNumberPolicy; |
| 11 | +import com.google.gson.ToNumberStrategy; |
| 12 | +import com.google.gson.TypeAdapter; |
| 13 | +import com.google.gson.internal.LazilyParsedNumber; |
| 14 | +import com.google.gson.stream.JsonReader; |
| 15 | +import com.google.gson.stream.JsonToken; |
| 16 | +import com.google.gson.stream.MalformedJsonException; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.ObjectOutputStream; |
| 19 | +import java.io.OutputStream; |
| 20 | +import java.io.StringReader; |
| 21 | +import java.math.BigDecimal; |
| 22 | +import java.math.BigInteger; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class NumberLimitsTest { |
| 26 | + private static final int MAX_LENGTH = 10_000; |
| 27 | + |
| 28 | + private static JsonReader jsonReader(String json) { |
| 29 | + return new JsonReader(new StringReader(json)); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Tests how {@link JsonReader} behaves for large numbers. |
| 34 | + * |
| 35 | + * <p>Currently {@link JsonReader} itself does not enforce any limits. |
| 36 | + * The reasons for this are: |
| 37 | + * <ul> |
| 38 | + * <li>Methods such as {@link JsonReader#nextDouble()} seem to have no problem |
| 39 | + * parsing extremely large or small numbers (it rounds to 0 or Infinity) |
| 40 | + * (to be verified?; if it had performance problems with certain numbers, then |
| 41 | + * it would affect other parts of Gson which parse as float or double as well) |
| 42 | + * <li>Enforcing limits only when a JSON number is encountered would be ineffective |
| 43 | + * unless users explicitly call {@link JsonReader#peek()} and check if the value |
| 44 | + * is a JSON number. Otherwise the limits could be circumvented because |
| 45 | + * {@link JsonReader#nextString()} reads both strings and numbers, and for |
| 46 | + * JSON strings no restrictions are enforced. |
| 47 | + * </ul> |
| 48 | + */ |
| 49 | + @Test |
| 50 | + public void testJsonReader() throws IOException { |
| 51 | + JsonReader reader = jsonReader("1".repeat(1000)); |
| 52 | + assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER); |
| 53 | + assertThat(reader.nextString()).isEqualTo("1".repeat(1000)); |
| 54 | + |
| 55 | + JsonReader reader2 = jsonReader("1".repeat(MAX_LENGTH + 1)); |
| 56 | + // Currently JsonReader does not recognize large JSON numbers as numbers but treats them |
| 57 | + // as unquoted string |
| 58 | + MalformedJsonException e = assertThrows(MalformedJsonException.class, () -> reader2.peek()); |
| 59 | + assertThat(e).hasMessageThat().startsWith("Use JsonReader.setStrictness(Strictness.LENIENT) to accept malformed JSON"); |
| 60 | + |
| 61 | + reader = jsonReader("1e9999"); |
| 62 | + assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER); |
| 63 | + assertThat(reader.nextString()).isEqualTo("1e9999"); |
| 64 | + |
| 65 | + reader = jsonReader("1e+9999"); |
| 66 | + assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER); |
| 67 | + assertThat(reader.nextString()).isEqualTo("1e+9999"); |
| 68 | + |
| 69 | + reader = jsonReader("1e10000"); |
| 70 | + assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER); |
| 71 | + assertThat(reader.nextString()).isEqualTo("1e10000"); |
| 72 | + |
| 73 | + reader = jsonReader("1e00001"); |
| 74 | + assertThat(reader.peek()).isEqualTo(JsonToken.NUMBER); |
| 75 | + assertThat(reader.nextString()).isEqualTo("1e00001"); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testJsonPrimitive() { |
| 80 | + assertThat(new JsonPrimitive("1".repeat(MAX_LENGTH)).getAsBigDecimal()) |
| 81 | + .isEqualTo(new BigDecimal("1".repeat(MAX_LENGTH))); |
| 82 | + assertThat(new JsonPrimitive("1e9999").getAsBigDecimal()) |
| 83 | + .isEqualTo(new BigDecimal("1e9999")); |
| 84 | + assertThat(new JsonPrimitive("1e-9999").getAsBigDecimal()) |
| 85 | + .isEqualTo(new BigDecimal("1e-9999")); |
| 86 | + |
| 87 | + NumberFormatException e = assertThrows(NumberFormatException.class, |
| 88 | + () -> new JsonPrimitive("1".repeat(MAX_LENGTH + 1)).getAsBigDecimal()); |
| 89 | + assertThat(e).hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 90 | + |
| 91 | + e = assertThrows(NumberFormatException.class, |
| 92 | + () -> new JsonPrimitive("1e10000").getAsBigDecimal()); |
| 93 | + assertThat(e).hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 94 | + |
| 95 | + e = assertThrows(NumberFormatException.class, |
| 96 | + () -> new JsonPrimitive("1e-10000").getAsBigDecimal()); |
| 97 | + assertThat(e).hasMessageThat().isEqualTo("Number has unsupported scale: 1e-10000"); |
| 98 | + |
| 99 | + |
| 100 | + assertThat(new JsonPrimitive("1".repeat(MAX_LENGTH)).getAsBigInteger()) |
| 101 | + .isEqualTo(new BigInteger("1".repeat(MAX_LENGTH))); |
| 102 | + |
| 103 | + e = assertThrows(NumberFormatException.class, |
| 104 | + () -> new JsonPrimitive("1".repeat(MAX_LENGTH + 1)).getAsBigInteger()); |
| 105 | + assertThat(e).hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testToNumberPolicy() throws IOException { |
| 110 | + ToNumberStrategy strategy = ToNumberPolicy.BIG_DECIMAL; |
| 111 | + |
| 112 | + assertThat(strategy.readNumber(jsonReader("\"" + "1".repeat(MAX_LENGTH) + "\""))) |
| 113 | + .isEqualTo(new BigDecimal("1".repeat(MAX_LENGTH))); |
| 114 | + assertThat(strategy.readNumber(jsonReader("1e9999"))) |
| 115 | + .isEqualTo(new BigDecimal("1e9999")); |
| 116 | + |
| 117 | + |
| 118 | + JsonParseException e = assertThrows(JsonParseException.class, |
| 119 | + () -> strategy.readNumber(jsonReader("\"" + "1".repeat(MAX_LENGTH + 1) + "\""))); |
| 120 | + assertThat(e).hasMessageThat().isEqualTo("Cannot parse " + "1".repeat(MAX_LENGTH + 1) + "; at path $"); |
| 121 | + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 122 | + |
| 123 | + e = assertThrows(JsonParseException.class, () -> strategy.readNumber(jsonReader("\"1e10000\""))); |
| 124 | + assertThat(e).hasMessageThat().isEqualTo("Cannot parse 1e10000; at path $"); |
| 125 | + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testLazilyParsedNumber() throws IOException { |
| 130 | + assertThat(new LazilyParsedNumber("1".repeat(MAX_LENGTH)).intValue()) |
| 131 | + .isEqualTo(new BigDecimal("1".repeat(MAX_LENGTH)).intValue()); |
| 132 | + assertThat(new LazilyParsedNumber("1e9999").intValue()) |
| 133 | + .isEqualTo(new BigDecimal("1e9999").intValue()); |
| 134 | + |
| 135 | + NumberFormatException e = assertThrows(NumberFormatException.class, |
| 136 | + () -> new LazilyParsedNumber("1".repeat(MAX_LENGTH + 1)).intValue()); |
| 137 | + assertThat(e).hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 138 | + |
| 139 | + e = assertThrows(NumberFormatException.class, |
| 140 | + () -> new LazilyParsedNumber("1e10000").intValue()); |
| 141 | + assertThat(e).hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 142 | + |
| 143 | + e = assertThrows(NumberFormatException.class, |
| 144 | + () -> new LazilyParsedNumber("1e10000").longValue()); |
| 145 | + assertThat(e).hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 146 | + |
| 147 | + ObjectOutputStream objOut = new ObjectOutputStream(OutputStream.nullOutputStream()); |
| 148 | + // Number is serialized as BigDecimal; should also enforce limits during this conversion |
| 149 | + e = assertThrows(NumberFormatException.class, () -> objOut.writeObject(new LazilyParsedNumber("1e10000"))); |
| 150 | + assertThat(e).hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testBigDecimalAdapter() throws IOException { |
| 155 | + TypeAdapter<BigDecimal> adapter = new Gson().getAdapter(BigDecimal.class); |
| 156 | + |
| 157 | + assertThat(adapter.fromJson("\"" + "1".repeat(MAX_LENGTH) + "\"")) |
| 158 | + .isEqualTo(new BigDecimal("1".repeat(MAX_LENGTH))); |
| 159 | + assertThat(adapter.fromJson("\"1e9999\"")) |
| 160 | + .isEqualTo(new BigDecimal("1e9999")); |
| 161 | + |
| 162 | + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, |
| 163 | + () -> adapter.fromJson("\"" + "1".repeat(MAX_LENGTH + 1) + "\"")); |
| 164 | + assertThat(e).hasMessageThat().isEqualTo("Failed parsing '" + "1".repeat(MAX_LENGTH + 1) + "' as BigDecimal; at path $"); |
| 165 | + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 166 | + |
| 167 | + e = assertThrows(JsonSyntaxException.class, |
| 168 | + () -> adapter.fromJson("\"1e10000\"")); |
| 169 | + assertThat(e).hasMessageThat().isEqualTo("Failed parsing '1e10000' as BigDecimal; at path $"); |
| 170 | + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Number has unsupported scale: 1e10000"); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void testBigIntegerAdapter() throws IOException { |
| 175 | + TypeAdapter<BigInteger> adapter = new Gson().getAdapter(BigInteger.class); |
| 176 | + |
| 177 | + assertThat(adapter.fromJson("\"" + "1".repeat(MAX_LENGTH) + "\"")) |
| 178 | + .isEqualTo(new BigInteger("1".repeat(MAX_LENGTH))); |
| 179 | + |
| 180 | + JsonSyntaxException e = assertThrows(JsonSyntaxException.class, |
| 181 | + () -> adapter.fromJson("\"" + "1".repeat(MAX_LENGTH + 1) + "\"")); |
| 182 | + assertThat(e).hasMessageThat().isEqualTo("Failed parsing '" + "1".repeat(MAX_LENGTH + 1) + "' as BigInteger; at path $"); |
| 183 | + assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("Number string too large: 111111111111111111111111111111..."); |
| 184 | + } |
| 185 | +} |
0 commit comments