|
| 1 | +package jdk.incubator.java.util.json; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | + |
| 9 | +/// Boundary hardening for JSON number math, added by the final-defense audit |
| 10 | +/// of issue #145. Complements the ported upstream {@code TestJsonNumber} and |
| 11 | +/// the issue #118 {@code JsonNumberOfDoubleMatrixTest} with the extremes that |
| 12 | +/// exercise {@code JsonNumberImpl}'s fast path, overflow guards |
| 13 | +/// ({@code Math.addExact}/{@code subtractExact}/{@code negateExact} and |
| 14 | +/// {@code Utils.powExact}) and the {@code JsonNumber.of(String)} whitespace |
| 15 | +/// contract. Expected behaviour is taken from the {@link JsonValue} javadoc |
| 16 | +/// ranges ("MIN_VALUE to MAX_VALUE, inclusive"), the upstream implementation |
| 17 | +/// and RFC 8259 section 6. |
| 18 | +public class JsonNumberBoundaryTest extends JsonTestLoggingConfig { |
| 19 | + |
| 20 | + @Test |
| 21 | + void integerMinValueBoundaryIsRepresentable() { |
| 22 | + var jn = Json.parse("-2147483648"); |
| 23 | + assertEquals(Integer.MIN_VALUE, jn.asInt(), "asInt at Integer.MIN_VALUE"); |
| 24 | + assertEquals(-2147483648L, jn.asLong(), "asLong at Integer.MIN_VALUE"); |
| 25 | + assertEquals(-2147483648.0d, jn.asDouble(), "asDouble at Integer.MIN_VALUE"); |
| 26 | + assertEquals("-2147483648", jn.toString(), "toString preservation"); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void longMinValueBoundaryIsRepresentable() { |
| 31 | + var jn = Json.parse("-9223372036854775808"); |
| 32 | + assertEquals(Long.MIN_VALUE, jn.asLong(), "asLong at Long.MIN_VALUE"); |
| 33 | + assertThrows(JsonValueException.class, jn::asInt, "asInt beyond Integer range"); |
| 34 | + assertEquals(-9.223372036854776E18d, jn.asDouble(), "asDouble at Long.MIN_VALUE"); |
| 35 | + assertEquals("-9223372036854775808", jn.toString(), "toString preservation"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void exponentAtIntMaxOverflowsAllConversions() { |
| 40 | + // 1e2147483647: Integer.parseInt of the exponent succeeds, but |
| 41 | + // Utils.powExact(10, 2147483647) overflows long -> ArithmeticException |
| 42 | + // -> Optional.empty -> JsonValueException; asDouble is Infinity. |
| 43 | + var jn = Json.parse("1e2147483647"); |
| 44 | + assertThrows(JsonValueException.class, jn::asInt, "asInt for 1e2147483647"); |
| 45 | + assertThrows(JsonValueException.class, jn::asLong, "asLong for 1e2147483647"); |
| 46 | + assertThrows(JsonValueException.class, jn::asDouble, "asDouble for 1e2147483647"); |
| 47 | + assertEquals("1e2147483647", jn.toString(), "toString preservation"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void exponentAboveIntMaxOverflowsAllConversions() { |
| 52 | + // 1e2147483648: Integer.parseInt of the exponent itself overflows. |
| 53 | + var jn = Json.parse("1e2147483648"); |
| 54 | + assertThrows(JsonValueException.class, jn::asInt, "asInt for 1e2147483648"); |
| 55 | + assertThrows(JsonValueException.class, jn::asLong, "asLong for 1e2147483648"); |
| 56 | + assertThrows(JsonValueException.class, jn::asDouble, "asDouble for 1e2147483648"); |
| 57 | + assertEquals("1e2147483648", jn.toString(), "toString preservation"); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void exponentAtIntMinOverflowsIntegralOnly() { |
| 62 | + // 1e-2147483648: Math.negateExact(Integer.MIN_VALUE) overflows during |
| 63 | + // the 10^power division path -> JsonValueException for asInt/asLong; |
| 64 | + // asDouble underflows to a finite 0.0 via Double.parseDouble. |
| 65 | + var jn = Json.parse("1e-2147483648"); |
| 66 | + assertThrows(JsonValueException.class, jn::asInt, "asInt for 1e-2147483648"); |
| 67 | + assertThrows(JsonValueException.class, jn::asLong, "asLong for 1e-2147483648"); |
| 68 | + assertEquals(0.0d, jn.asDouble(), "asDouble for 1e-2147483648"); |
| 69 | + assertEquals("1e-2147483648", jn.toString(), "toString preservation"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void exponentBelowIntMinOverflowsIntegralOnly() { |
| 74 | + // 1e-2147483649: Integer.parseInt of the exponent fails; asDouble |
| 75 | + // still underflows to a finite 0.0. |
| 76 | + var jn = Json.parse("1e-2147483649"); |
| 77 | + assertThrows(JsonValueException.class, jn::asInt, "asInt for 1e-2147483649"); |
| 78 | + assertThrows(JsonValueException.class, jn::asLong, "asLong for 1e-2147483649"); |
| 79 | + assertEquals(0.0d, jn.asDouble(), "asDouble for 1e-2147483649"); |
| 80 | + assertEquals("1e-2147483649", jn.toString(), "toString preservation"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void parsedNegativeZeroKeepsTextAndValue() { |
| 85 | + var jn = Json.parse("-0"); |
| 86 | + assertEquals("-0", jn.toString(), "toString preservation"); |
| 87 | + assertEquals(0, jn.asInt(), "asInt of -0"); |
| 88 | + assertEquals(0L, jn.asLong(), "asLong of -0"); |
| 89 | + // Double.compare distinguishes -0.0 from 0.0; RFC 8259 permits "-0" |
| 90 | + // and asDouble is specified to properly return negative zero. |
| 91 | + assertEquals(0, Double.compare(jn.asDouble(), -0.0d), "asDouble of -0 is negative zero"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void ofStringStripsInsignificantWhitespace() { |
| 96 | + // JsonNumber.of(String) javadoc: the representation is equivalent to |
| 97 | + // num with any leading or trailing JSON insignificant whitespaces removed. |
| 98 | + var jn = JsonNumber.of(" 3 "); |
| 99 | + assertEquals("3", jn.toString(), "toString of of(\" 3 \")"); |
| 100 | + assertEquals(3, jn.asInt(), "asInt of of(\" 3 \")"); |
| 101 | + var neg = JsonNumber.of("\t-0.5\n"); |
| 102 | + assertEquals("-0.5", neg.toString(), "toString of of(\"\\t-0.5\\n\")"); |
| 103 | + assertEquals(-0.5d, neg.asDouble(), "asDouble of of(\"\\t-0.5\\n\")"); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void bareMinusAndEmptyTextAreNotJsonNumbers() { |
| 108 | + // RFC 8259 section 6: a number must contain at least one integer digit; |
| 109 | + // an empty text is not a JSON value (RFC 8259 section 2). |
| 110 | + assertThrows(JsonParseException.class, () -> Json.parse("-"), "parse(\"-\")"); |
| 111 | + assertThrows(JsonParseException.class, () -> Json.parse(""), "parse(\"\")"); |
| 112 | + assertThrows(IllegalArgumentException.class, () -> JsonNumber.of("-"), "of(\"-\")"); |
| 113 | + assertThrows(IllegalArgumentException.class, () -> JsonNumber.of(""), "of(\"\")"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + void intMinValueViaFactoryStringContract() { |
| 118 | + // of(String) must accept the same boundary text parse accepts and be |
| 119 | + // behaviourally identical to the parsed value. |
| 120 | + var viaFactory = JsonNumber.of("-2147483648"); |
| 121 | + var viaParse = Json.parse("-2147483648"); |
| 122 | + assertEquals(viaParse.toString(), viaFactory.toString(), "toString parity"); |
| 123 | + assertEquals(Integer.MIN_VALUE, viaFactory.asInt(), "asInt parity"); |
| 124 | + assertEquals(Long.MIN_VALUE, JsonNumber.of("-9223372036854775808").asLong(), |
| 125 | + "asLong of of(\"-9223372036854775808\")"); |
| 126 | + assertTrue(viaFactory instanceof JsonNumber, "factory produces JsonNumber"); |
| 127 | + } |
| 128 | +} |
0 commit comments