From 7fa023f6b2598c61adcd9fe6b538cae204d14bf0 Mon Sep 17 00:00:00 2001 From: Ilia Motornyi Date: Mon, 10 Apr 2017 14:42:16 +0100 Subject: [PATCH] Explicitly throw JsonException for unparseable input + some tests Change-Id: I36f456a05111f5d90a7d471a3927497c792faf79 --- .../elemental/json/impl/JsonTokenizer.java | 2 + .../tests/elemental/json/JsonUtilTest.java | 66 +++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/elemental/src/elemental/json/impl/JsonTokenizer.java b/elemental/src/elemental/json/impl/JsonTokenizer.java index 16206071dad..b6cfbd5ea02 100644 --- a/elemental/src/elemental/json/impl/JsonTokenizer.java +++ b/elemental/src/elemental/json/impl/JsonTokenizer.java @@ -110,6 +110,8 @@ String nextString(int startChar) throws JsonException { case '\r': case '\n': throw new JsonException(""); + case INVALID_CHAR: + throw new JsonException("Invalid string: closing " + startChar + " is not found"); case '\\': c = next(); switch (c) { diff --git a/elemental/tests/elemental/json/JsonUtilTest.java b/elemental/tests/elemental/json/JsonUtilTest.java index 1dbaa8896dc..ac887a54b34 100755 --- a/elemental/tests/elemental/json/JsonUtilTest.java +++ b/elemental/tests/elemental/json/JsonUtilTest.java @@ -124,6 +124,72 @@ public void testIllegalParse() { } } + public void testUnclosedString() { + try { + JsonUtil.parse("\"a"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + + public void testUnclosedEmptyString() { + try { + JsonUtil.parse("\""); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + + public void testUnclosedArray() { + try { + JsonUtil.parse("[1"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + + public void testUnclosedEmptyArray() { + try { + JsonUtil.parse("["); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + + public void testUnclosedObject() { + try { + JsonUtil.parse("{'a"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + try { + JsonUtil.parse("{'a'"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + try { + JsonUtil.parse("{'a':"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + + public void testUnclosedEmptyObject() { + try { + JsonUtil.parse("{"); + fail("Expected JsonException to be thrown"); + } catch (JsonException je) { + // Expected + } + } + public void testLegalParse() { JsonValue obj = JsonUtil.parse( "{ \"a\":1, \"b\":\"hello\", \"c\": true,"