|
| 1 | +package com.helger.json.supplementary.issues; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import javax.annotation.Nonnegative; |
| 8 | +import javax.annotation.Nonnull; |
| 9 | + |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.helger.commons.wrapper.Wrapper; |
| 13 | +import com.helger.json.IJson; |
| 14 | +import com.helger.json.IJsonArray; |
| 15 | +import com.helger.json.IJsonObject; |
| 16 | +import com.helger.json.parser.JsonParseException; |
| 17 | +import com.helger.json.parser.JsonParser; |
| 18 | +import com.helger.json.serialize.JsonReader; |
| 19 | + |
| 20 | +public final class TestIssue34 |
| 21 | +{ |
| 22 | + @Nonnull |
| 23 | + public static String _createNestedDoc (@Nonnegative final int nNesting, |
| 24 | + @Nonnull final String sBeginning, |
| 25 | + @Nonnull final String sLevelOpen, |
| 26 | + @Nonnull final String sContent, |
| 27 | + @Nonnull final String sLevelClose, |
| 28 | + @Nonnull final String sEnd) |
| 29 | + { |
| 30 | + final StringBuilder ret = new StringBuilder (sBeginning.length () + |
| 31 | + nNesting * (sLevelOpen.length () + sLevelClose.length ()) + |
| 32 | + 1 + |
| 33 | + sContent.length () + |
| 34 | + 1 + |
| 35 | + (nNesting / 32) * 2 + |
| 36 | + sEnd.length ()); |
| 37 | + ret.append (sBeginning); |
| 38 | + for (int i = 0; i < nNesting; ++i) |
| 39 | + { |
| 40 | + ret.append (sLevelOpen); |
| 41 | + if ((i & 31) == 0) |
| 42 | + ret.append ('\n'); |
| 43 | + } |
| 44 | + ret.append ('\n').append (sContent).append ('\n'); |
| 45 | + for (int i = 0; i < nNesting; ++i) |
| 46 | + { |
| 47 | + ret.append (sLevelClose); |
| 48 | + if ((i & 31) == 0) |
| 49 | + ret.append ('\n'); |
| 50 | + } |
| 51 | + ret.append (sEnd); |
| 52 | + return ret.toString (); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testArrayMax () |
| 57 | + { |
| 58 | + // Stack overflow with JSON array with nesting 5176 |
| 59 | + final int nMax = JsonParser.DEFAULT_MAX_NESTING_DEPTH * 2; |
| 60 | + for (int nNesting = 1; nNesting < nMax; ++nNesting) |
| 61 | + { |
| 62 | + final String sNestedDoc = _createNestedDoc (nNesting, "", "[", "0", "]", ""); |
| 63 | + final Wrapper <JsonParseException> aWrapper = new Wrapper <> (); |
| 64 | + final IJson aJson = JsonReader.builder ().source (sNestedDoc).customExceptionCallback (aWrapper::set).read (); |
| 65 | + if (aWrapper.isSet ()) |
| 66 | + { |
| 67 | + assertNull (aJson); |
| 68 | + assertTrue ("Failed nesting is " + nNesting, nNesting > JsonParser.DEFAULT_MAX_NESTING_DEPTH); |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + assertNotNull (aJson); |
| 73 | + assertTrue (nNesting <= JsonParser.DEFAULT_MAX_NESTING_DEPTH); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testArrayMin () |
| 80 | + { |
| 81 | + // Default: nesting okay |
| 82 | + IJsonArray aJson = JsonReader.builder ().source ("[[0]]").readAsArray (); |
| 83 | + assertNotNull (aJson); |
| 84 | + // Nested too deep |
| 85 | + aJson = JsonReader.builder ().source ("[[0]]").customizeCallback (p -> p.setMaxNestingDepth (1)).readAsArray (); |
| 86 | + assertNull (aJson); |
| 87 | + // Nesting okay |
| 88 | + aJson = JsonReader.builder ().source ("[0]").customizeCallback (p -> p.setMaxNestingDepth (1)).readAsArray (); |
| 89 | + assertNotNull (aJson); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testObjectMax () |
| 94 | + { |
| 95 | + // Stack overflow with JSON object with nesting 5650 |
| 96 | + // Start with 2, because we always have an outer bracket |
| 97 | + final int nMax = JsonParser.DEFAULT_MAX_NESTING_DEPTH * 2; |
| 98 | + for (int nNesting = 2; nNesting < nMax; ++nNesting) |
| 99 | + { |
| 100 | + final String sNestedDoc = _createNestedDoc (nNesting - 1, "{", "'a':{ ", "'b':0", "} ", "}"); |
| 101 | + final Wrapper <JsonParseException> aWrapper = new Wrapper <> (); |
| 102 | + final IJson aJson = JsonReader.builder ().source (sNestedDoc).customExceptionCallback (aWrapper::set).read (); |
| 103 | + if (aWrapper.isSet ()) |
| 104 | + { |
| 105 | + assertNull (aJson); |
| 106 | + assertTrue ("Failed nesting is " + nNesting, nNesting > JsonParser.DEFAULT_MAX_NESTING_DEPTH); |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + assertNotNull (aJson); |
| 111 | + assertTrue (nNesting <= JsonParser.DEFAULT_MAX_NESTING_DEPTH); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testObjectMin () |
| 118 | + { |
| 119 | + // Default: nesting okay |
| 120 | + IJsonObject aJson = JsonReader.builder ().source ("{'a':{'a':0}}").readAsObject (); |
| 121 | + assertNotNull (aJson); |
| 122 | + // Nested too deep |
| 123 | + aJson = JsonReader.builder () |
| 124 | + .source ("{'a':{'a':0}}") |
| 125 | + .customizeCallback (p -> p.setMaxNestingDepth (1)) |
| 126 | + .readAsObject (); |
| 127 | + assertNull (aJson); |
| 128 | + // Nesting okay |
| 129 | + aJson = JsonReader.builder ().source ("{'a':0}").customizeCallback (p -> p.setMaxNestingDepth (1)).readAsObject (); |
| 130 | + assertNotNull (aJson); |
| 131 | + } |
| 132 | +} |
0 commit comments