-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
207 lines (185 loc) · 5.59 KB
/
Copy pathjson_test.go
File metadata and controls
207 lines (185 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package magic
import (
"strings"
"testing"
)
func TestJSONDetection(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
}{
{name: "object", input: `{"schemaVersion": 2}`},
{name: "array", input: `[1, "two", false, null]`},
{name: "surrounding whitespace", input: " \t\r\n{\"key\": \"value\"}\n"},
{name: "Unicode", input: `{"message":"héllo, 世界","escaped":"\uD834\uDD1E"}`},
{name: "escaped characters", input: `["\b\f\n\r\t\/\\\""]`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assertResult(t, Detect([]byte(test.input)), Result{
Kind: KindText,
MIME: mimeJSON,
Format: FormatJSON,
Encoding: encodingUTF8,
})
})
}
}
func TestInvalidJSONRetainsExistingClassification(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
}{
{name: "bare string", input: `"hello"`},
{name: "bare number", input: `-12.5e+2`},
{name: "bare integer", input: `12345`},
{name: "bare true", input: `true`},
{name: "bare false", input: `false`},
{name: "bare null", input: `null`},
{name: "mismatched delimiters", input: `{]`},
{name: "whitespace only", input: " \t\r\n"},
{name: "missing value", input: `{"key":}`},
{name: "single quoted key", input: `{'key': 1}`},
{name: "trailing comma", input: `[1,]`},
{name: "unterminated string", input: `"value`},
{name: "truncated literal", input: `tru`},
{name: "leading zero", input: `01`},
{name: "truncated fraction", input: `1.`},
{name: "truncated exponent", input: `1e+`},
{name: "invalid escape", input: `"\x"`},
{name: "unescaped control", input: "\"value\tvalue\""},
{name: "leading form feed", input: "\f{}"},
{name: "trailing form feed", input: "{}\f"},
{name: "trailing content", input: `{}x`},
{name: "second value", input: `true false`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assertResult(t, Detect([]byte(test.input)), Result{
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
})
})
}
}
func TestInvalidUTF8JSONRetainsUnknownClassification(t *testing.T) {
t.Parallel()
tests := [][]byte{
{'"', 0xff, '"'},
{'{', '}', 0xff},
}
for _, input := range tests {
assertResult(t, Detect(input), Result{
Kind: KindUnknown,
Reason: ReasonInvalidText,
})
}
}
func TestJSONNestingDepth(t *testing.T) {
t.Parallel()
tests := []struct {
name string
open string
close string
}{
{name: "arrays", open: "[", close: "]"},
{name: "objects", open: `{"value":`, close: "}"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
input := strings.Repeat(test.open, jsonMaximumDepth) + "0" +
strings.Repeat(test.close, jsonMaximumDepth)
if got := Detect([]byte(input)); got.Format != FormatJSON {
t.Fatalf("Detect() = %#v, want JSON at maximum nesting depth", got)
}
input = test.open + input + test.close
if got := Detect([]byte(input)); got.Format == FormatJSON {
t.Fatalf("Detect() = %#v, want nesting depth limit to reject JSON", got)
}
})
}
}
func TestJSONInlineNestingDoesNotAllocate(t *testing.T) {
input := []byte(strings.Repeat("[", jsonInlineDepth) + "0" +
strings.Repeat("]", jsonInlineDepth))
if allocations := testing.AllocsPerRun(1000, func() {
Detect(input)
}); allocations != 0 {
t.Fatalf("Detect allocated %.2f times for inline JSON nesting", allocations)
}
}
func TestJSONPrefixDetection(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
}{
{name: "complete object", input: `{}`},
{name: "truncated object", input: `{`},
{name: "truncated object key", input: `{"key`},
{name: "truncated object value", input: `{"key":`},
{name: "truncated array", input: `[`},
{name: "truncated array value", input: `[1,`},
{name: "truncated array string", input: `["value`},
{name: "truncated array escape", input: `["value\`},
{name: "truncated array Unicode escape", input: `["value\u12`},
{name: "truncated array number", input: `[-`},
{name: "truncated array fraction", input: `[1.`},
{name: "truncated array exponent", input: `[1e`},
{name: "truncated array signed exponent", input: `[1e+`},
{name: "truncated array true", input: `[tru`},
{name: "truncated array false", input: `[fals`},
{name: "truncated array null", input: `[nul`},
{name: "leading whitespace", input: " \n{"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assertResult(t, DetectPrefix([]byte(test.input)), Result{
Kind: KindText,
MIME: mimeJSON,
Format: FormatJSON,
Encoding: encodingUTF8,
Reason: ReasonNeedMore,
})
})
}
}
func TestInvalidJSONPrefixRetainsExistingClassification(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
}{
{name: "plain text", input: `package`},
{name: "bare number", input: `123`},
{name: "bare string", input: `"value`},
{name: "bare literal", input: `tru`},
{name: "whitespace only", input: " \t\r\n"},
{name: "mismatched delimiters", input: `{]`},
{name: "trailing comma", input: `[1,]`},
{name: "leading zero", input: `01`},
{name: "invalid literal", input: `truex`},
{name: "invalid escape", input: `"\x"`},
{name: "trailing content", input: `{}x`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
assertResult(t, DetectPrefix([]byte(test.input)), Result{
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Reason: ReasonNeedMore,
})
})
}
}