| 1 | package jsonparser |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | var testPaths = [][]string{ |
| 10 | []string{"test"}, |
| 11 | []string{"these"}, |
| 12 | []string{"keys"}, |
| 13 | []string{"please"}, |
| 14 | } |
| 15 | |
| 16 | // Test helper for SYS-REQ-008. |
| 17 | func testIter(data []byte) (err error) { |
| 18 | EachKey(data, func(idx int, value []byte, vt ValueType, iterErr error) { |
| 19 | if iterErr != nil { |
| 20 | err = fmt.Errorf("Error parsing json: %s", iterErr.Error()) |
| 21 | } |
| 22 | }, testPaths...) |
| 23 | return err |
| 24 | } |
| 25 | |
| 26 | // Verifies: SYS-REQ-001 [malformed] |
| 27 | // MCDC SYS-REQ-001: N/A |
| 28 | // Verifies: SYS-REQ-008 [malformed] |
| 29 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=F, eachkey_completes_requested_scan=F, eachkey_malformed_input_returns_error=T, missing_multipath_request_does_not_emit_callback=F, multipath_requests_are_provided=T => TRUE |
| 30 | func TestPanickingErrors(t *testing.T) { |
| 31 | if err := testIter([]byte(`{"test":`)); err == nil { |
| 32 | t.Error("Expected error...") |
| 33 | } |
| 34 | |
| 35 | if err := testIter([]byte(`{"test":0}some":[{"these":[{"keys":"some"}]}]}some"}]}],"please":"some"}`)); err == nil { |
| 36 | t.Error("Expected error...") |
| 37 | } |
| 38 | |
| 39 | if _, _, _, err := Get([]byte(`{"test":`), "test"); err == nil { |
| 40 | t.Error("Expected error...") |
| 41 | } |
| 42 | |
| 43 | if _, _, _, err := Get([]byte(`{"some":0}some":[{"some":[{"some":"some"}]}]}some"}]}],"some":"some"}`), "x"); err == nil { |
| 44 | t.Error("Expected error...") |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Verifies: SYS-REQ-008 [boundary] |
| 49 | // MCDC SYS-REQ-008: eachkey_callback_receives_found_values=F, eachkey_completes_requested_scan=F, eachkey_malformed_input_returns_error=F, missing_multipath_request_does_not_emit_callback=F, multipath_requests_are_provided=F => TRUE |
| 50 | func TestEachKeyNoRequests(t *testing.T) { |
| 51 | called := false |
| 52 | EachKey([]byte(`{"a":1}`), func(idx int, value []byte, vt ValueType, err error) { |
| 53 | called = true |
| 54 | }) |
| 55 | if called { |
| 56 | t.Fatal("EachKey should not invoke the callback when no paths are requested") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // check having a very deep key depth |
| 61 | // Verifies: SYS-REQ-008 [boundary] |
| 62 | // MCDC SYS-REQ-008: N/A |
| 63 | func TestKeyDepth(t *testing.T) { |
| 64 | var sb strings.Builder |
| 65 | var keys []string |
| 66 | //build data |
| 67 | sb.WriteString("{") |
| 68 | for i := 0; i < 128; i++ { |
| 69 | fmt.Fprintf(&sb, `"key%d": %dx,`, i, i) |
| 70 | keys = append(keys, fmt.Sprintf("key%d", i)) |
| 71 | } |
| 72 | sb.WriteString("}") |
| 73 | |
| 74 | data := []byte(sb.String()) |
| 75 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 76 | return |
| 77 | }, keys) |
| 78 | } |
| 79 | |
| 80 | // check having a bunch of keys in a call to EachKey |
| 81 | // Verifies: SYS-REQ-008 [boundary] |
| 82 | // MCDC SYS-REQ-008: N/A |
| 83 | func TestKeyCount(t *testing.T) { |
| 84 | var sb strings.Builder |
| 85 | var keys [][]string |
| 86 | //build data |
| 87 | sb.WriteString("{") |
| 88 | for i := 0; i < 128; i++ { |
| 89 | fmt.Fprintf(&sb, `"key%d":"%d"`, i, i) |
| 90 | if i < 127 { |
| 91 | sb.WriteString(",") |
| 92 | } |
| 93 | keys = append(keys, []string{fmt.Sprintf("key%d", i)}) |
| 94 | } |
| 95 | sb.WriteString("}") |
| 96 | |
| 97 | data := []byte(sb.String()) |
| 98 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 99 | return |
| 100 | }, keys...) |
| 101 | } |
| 102 | |
| 103 | // try pulling lots of keys out of a big array |
| 104 | // Verifies: SYS-REQ-008 [boundary] |
| 105 | // MCDC SYS-REQ-008: N/A |
| 106 | func TestKeyDepthArray(t *testing.T) { |
| 107 | var sb strings.Builder |
| 108 | var keys []string |
| 109 | //build data |
| 110 | sb.WriteString("[") |
| 111 | for i := 0; i < 128; i++ { |
| 112 | fmt.Fprintf(&sb, `{"key": %d},`, i) |
| 113 | keys = append(keys, fmt.Sprintf("[%d].key", i)) |
| 114 | } |
| 115 | sb.WriteString("]") |
| 116 | |
| 117 | data := []byte(sb.String()) |
| 118 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 119 | return |
| 120 | }, keys) |
| 121 | } |
| 122 | |
| 123 | // check having a bunch of keys |
| 124 | // Verifies: SYS-REQ-008 [boundary] |
| 125 | // MCDC SYS-REQ-008: N/A |
| 126 | func TestKeyCountArray(t *testing.T) { |
| 127 | var sb strings.Builder |
| 128 | var keys [][]string |
| 129 | //build data |
| 130 | sb.WriteString("[") |
| 131 | for i := 0; i < 128; i++ { |
| 132 | fmt.Fprintf(&sb, `{"key":"%d"}`, i) |
| 133 | if i < 127 { |
| 134 | sb.WriteString(",") |
| 135 | } |
| 136 | keys = append(keys, []string{fmt.Sprintf("[%d].key", i)}) |
| 137 | } |
| 138 | sb.WriteString("]") |
| 139 | |
| 140 | data := []byte(sb.String()) |
| 141 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 142 | return |
| 143 | }, keys...) |
| 144 | } |
| 145 | |
| 146 | // check having a bunch of keys in a super deep array |
| 147 | // Verifies: SYS-REQ-008 [boundary] |
| 148 | // MCDC SYS-REQ-008: N/A |
| 149 | func TestEachKeyArray(t *testing.T) { |
| 150 | var sb strings.Builder |
| 151 | var keys [][]string |
| 152 | //build data |
| 153 | sb.WriteString(`[`) |
| 154 | for i := 0; i < 127; i++ { |
| 155 | fmt.Fprintf(&sb, `%d`, i) |
| 156 | if i < 127 { |
| 157 | sb.WriteString(",") |
| 158 | } |
| 159 | if i < 32 { |
| 160 | keys = append(keys, []string{fmt.Sprintf("[%d]", 128+i)}) |
| 161 | } |
| 162 | } |
| 163 | sb.WriteString(`]`) |
| 164 | |
| 165 | data := []byte(sb.String()) |
| 166 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 167 | return |
| 168 | }, keys...) |
| 169 | } |
| 170 | |
| 171 | // Verifies: SYS-REQ-008 [boundary] |
| 172 | // MCDC SYS-REQ-008: N/A |
| 173 | func TestLargeArray(t *testing.T) { |
| 174 | var sb strings.Builder |
| 175 | //build data |
| 176 | sb.WriteString(`[`) |
| 177 | for i := 0; i < 127; i++ { |
| 178 | fmt.Fprintf(&sb, `%d`, i) |
| 179 | if i < 127 { |
| 180 | sb.WriteString(",") |
| 181 | } |
| 182 | } |
| 183 | sb.WriteString(`]`) |
| 184 | keys := [][]string{[]string{`[1]`}} |
| 185 | |
| 186 | data := []byte(sb.String()) |
| 187 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 188 | return |
| 189 | }, keys...) |
| 190 | } |
| 191 | |
| 192 | // Verifies: SYS-REQ-008 [boundary] |
| 193 | // MCDC SYS-REQ-008: N/A |
| 194 | func TestArrayOutOfBounds(t *testing.T) { |
| 195 | var sb strings.Builder |
| 196 | //build data |
| 197 | sb.WriteString(`[`) |
| 198 | for i := 0; i < 61; i++ { |
| 199 | fmt.Fprintf(&sb, `%d`, i) |
| 200 | if i < 61 { |
| 201 | sb.WriteString(",") |
| 202 | } |
| 203 | } |
| 204 | sb.WriteString(`]`) |
| 205 | keys := [][]string{[]string{`[128]`}} |
| 206 | |
| 207 | data := []byte(sb.String()) |
| 208 | EachKey(data, func(offset int, value []byte, dt ValueType, err error) { |
| 209 | return |
| 210 | }, keys...) |
| 211 | } |