From f112a091aadf080ff3f4e750ea33e7cb490a0213 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sat, 15 Feb 2025 12:03:03 -0800 Subject: [PATCH 1/2] fixed failing unit tests in strict mode, issue 940 --- .../org/json/JSONParserConfiguration.java | 1 + .../java/org/json/junit/JSONTokenerTest.java | 48 +++++++++++++++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index ae23a83..59b7e64 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration { public JSONParserConfiguration() { super(); this.overwriteDuplicateKey = false; + this.strictMode = true; } /** diff --git a/src/test/java/org/json/junit/JSONTokenerTest.java b/src/test/java/org/json/junit/JSONTokenerTest.java index c436d27..b0b45cb 100644 --- a/src/test/java/org/json/junit/JSONTokenerTest.java +++ b/src/test/java/org/json/junit/JSONTokenerTest.java @@ -16,10 +16,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.json.JSONTokener; +import org.json.*; import org.junit.Test; /** @@ -98,7 +95,17 @@ public class JSONTokenerTest { checkValid(" [] ",JSONArray.class); checkValid("[1,2]",JSONArray.class); checkValid("\n\n[1,2]\n\n",JSONArray.class); - checkValid("1 2", String.class); + + // Test should fail if default strictMode is true, pass if false + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); + if (jsonParserConfiguration.isStrictMode()) { + try { + checkValid("1 2", String.class); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } + } else { + checkValid("1 2", String.class); + } } @Test @@ -330,16 +337,37 @@ public class JSONTokenerTest { public void testInvalidInput_JSONObject_withoutStrictModel_shouldParseInput() { String input = "{\"invalidInput\": [],}"; JSONTokener tokener = new JSONTokener(input); - Object value = tokener.nextValue(); - assertEquals(new JSONObject(input).toString(), value.toString()); + + // Test should fail if default strictMode is true, pass if false + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); + if (jsonParserConfiguration.isStrictMode()) { + try { + Object value = tokener.nextValue(); + assertEquals(new JSONObject(input).toString(), value.toString()); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } + } else { + Object value = tokener.nextValue(); + assertEquals(new JSONObject(input).toString(), value.toString()); + } } @Test public void testInvalidInput_JSONArray_withoutStrictModel_shouldParseInput() { String input = "[\"invalidInput\",]"; JSONTokener tokener = new JSONTokener(input); - Object value = tokener.nextValue(); - assertEquals(new JSONArray(input).toString(), value.toString()); - } + // Test should fail if default strictMode is true, pass if false + JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); + if (jsonParserConfiguration.isStrictMode()) { + try { + Object value = tokener.nextValue(); + assertEquals(new JSONArray(input).toString(), value.toString()); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } + } else { + Object value = tokener.nextValue(); + assertEquals(new JSONArray(input).toString(), value.toString()); + } + } } From 3919abd69a785cdc83d9171b3365927cf52cca71 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sat, 15 Feb 2025 12:30:12 -0800 Subject: [PATCH 2/2] optimized unit tests to respond accurately to default strictMode --- .../org/json/JSONParserConfiguration.java | 2 +- .../java/org/json/junit/JSONArrayTest.java | 13 ++++-- .../java/org/json/junit/JSONObjectTest.java | 46 +++++++++++-------- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 59b7e64..d3420a1 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -15,7 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration { public JSONParserConfiguration() { super(); this.overwriteDuplicateKey = false; - this.strictMode = true; +// this.strictMode = true; } /** diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index 584e47c..580fe82 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -477,13 +477,18 @@ public class JSONArrayTest { */ @Test public void unquotedText() { + String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; + List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); + + // Test should fail if default strictMode is true, pass if false JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); if (jsonParserConfiguration.isStrictMode()) { - System.out.println("Skipping JSONArrayTest unquotedText() when strictMode default is true"); - } else { - String str = "[value1, something!, (parens), foo@bar.com, 23, 23+45]"; + try { JSONArray jsonArray = new JSONArray(str); - List expected = Arrays.asList("value1", "something!", "(parens)", "foo@bar.com", 23, "23+45"); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } + } else { + JSONArray jsonArray = new JSONArray(str); assertEquals(expected, jsonArray.toList()); } } diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 889113d..4c3413f 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -218,11 +218,16 @@ public class JSONObjectTest { */ @Test public void unquotedText() { + String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; + + // Test should fail if default strictMode is true, pass if false JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); if (jsonParserConfiguration.isStrictMode()) { - System.out.println("Skipping JSONObjectTest unquotedText() when strictMode default is true"); + try { + JSONObject jsonObject = new JSONObject(str); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } } else { - String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}"; JSONObject jsonObject = new JSONObject(str); String textStr = jsonObject.toString(); assertTrue("expected key1", textStr.contains("\"key1\"")); @@ -1074,24 +1079,29 @@ public class JSONObjectTest { */ @Test public void jsonInvalidNumberValues() { + // Number-notations supported by Java and invalid as JSON + String str = + "{" + + "\"hexNumber\":-0x123," + + "\"tooManyZeros\":00," + + "\"negativeInfinite\":-Infinity," + + "\"negativeNaN\":-NaN," + + "\"negativeFraction\":-.01," + + "\"tooManyZerosFraction\":00.001," + + "\"negativeHexFloat\":-0x1.fffp1," + + "\"hexFloat\":0x1.0P-1074," + + "\"floatIdentifier\":0.1f," + + "\"doubleIdentifier\":0.1d" + + "}"; + + // Test should fail if default strictMode is true, pass if false JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); if (jsonParserConfiguration.isStrictMode()) { - System.out.println("Skipping JSONObjectTest jsonInvalidNumberValues() when strictMode default is true"); + try { + JSONObject jsonObject = new JSONObject(str); + assertEquals("Expected to throw exception due to invalid string", true, false); + } catch (JSONException e) { } } else { - // Number-notations supported by Java and invalid as JSON - String str = - "{" + - "\"hexNumber\":-0x123," + - "\"tooManyZeros\":00," + - "\"negativeInfinite\":-Infinity," + - "\"negativeNaN\":-NaN," + - "\"negativeFraction\":-.01," + - "\"tooManyZerosFraction\":00.001," + - "\"negativeHexFloat\":-0x1.fffp1," + - "\"hexFloat\":0x1.0P-1074," + - "\"floatIdentifier\":0.1f," + - "\"doubleIdentifier\":0.1d" + - "}"; JSONObject jsonObject = new JSONObject(str); Object obj; obj = jsonObject.get("hexNumber"); @@ -2274,7 +2284,7 @@ public class JSONObjectTest { public void jsonObjectParsingErrors() { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); if (jsonParserConfiguration.isStrictMode()) { - System.out.println("Skipping JSONObjectTest jaonObjectParsingErrors() when strictMode default is true"); + System.out.println("Skipping JSONObjectTest jsonObjectParsingErrors() when strictMode default is true"); } else { try { // does not start with '{'