generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
rjson_test.go
371 lines (321 loc) · 7.43 KB
/
rjson_test.go
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package rjson
import (
"compress/gzip"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// These are the files in testdata.
var jsonTestFiles = []string{
// from querying github's REST api
"github_user.json",
"github_repo.json",
// from json checker test suite
"jsonchecker_pass1.json",
// from https://github.com/miloyip/nativejson-benchmark
"canada.json",
"citm_catalog.json",
"twitter.json",
"code.json",
// from https://raw.githubusercontent.com/mailru/easyjson/master/benchmark/example.json
"example.json",
// from https://github.com/ultrajson/ultrajson/blob/master/tests/sample.json
"sample.json",
}
func corpusFiles(t *testing.T) []string {
t.Helper()
var result []string
corpusDir := filepath.FromSlash(`testdata/fuzz/corpus`)
dir, err := ioutil.ReadDir(corpusDir)
require.NoError(t, err)
for _, info := range dir {
result = append(result, filepath.Join(corpusDir, info.Name()))
}
return result
}
func testFuzzerFunc(t *testing.T, fn func([]byte) (int, error)) {
t.Helper()
require.NotNil(t, fn)
for _, filename := range corpusFiles(t) {
data, err := ioutil.ReadFile(filename)
require.NoError(t, err)
_, err = fn(data)
assert.NoErrorf(t, err, "error from file: %s\n with data:\n%s", filename, string(data))
}
}
//nolint:unused,deadcode // this is a convenience method for ad-hoc testing
func testFuzzerWithInput(t *testing.T, fuzzer func([]byte) (int, error), inputs ...string) {
t.Helper()
for _, input := range inputs {
data := []byte(input)
_, err := fuzzer(data)
assert.NoError(t, err, "error on input: %s", input)
}
}
func Test_fuzzReadFloat64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadFloat64)
}
func Test_fuzzReadUint64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadUint64)
}
func Test_fuzzReadUint32(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadUint32)
}
func Test_fuzzReadUint(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadUint)
}
func Test_fuzzReadInt64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadInt64)
}
func Test_fuzzReadInt32(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadInt32)
}
func Test_fuzzReadInt(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadInt)
}
func Test_fuzzReadString(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadString)
}
func Test_fuzzReadStringBytes(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadStringBytes)
}
func Test_fuzzReadBool(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadBool)
}
func Test_fuzzReadNull(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadNull)
}
func Test_fuzzSkipValue(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzSkipValue)
}
func Test_fuzzValid(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzValid)
}
func Test_fuzzNextToken(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzNextToken)
}
func Test_fuzzNextTokenType(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzNextTokenType)
}
func Test_fuzzHandleArrayValues(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzHandleArrayValues)
}
func Test_fuzzHandleObjectValues(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzHandleObjectValues)
}
func Test_fuzzReadArray(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadArray)
}
func Test_fuzzReadObject(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadObject)
}
func Test_fuzzReadValue(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzReadValue)
}
func Test_fuzzDecodeFloat64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeFloat64)
}
func Test_fuzzDecodeUint64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeUint64)
}
func Test_fuzzDecodeUint32(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeUint32)
}
func Test_fuzzDecodeUint(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeUint)
}
func Test_fuzzDecodeInt64(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeInt64)
}
func Test_fuzzDecodeInt32(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeInt32)
}
func Test_fuzzDecodeInt(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeInt)
}
func Test_fuzzDecodeString(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeString)
}
func Test_fuzzDecodeBool(t *testing.T) {
t.Parallel()
testFuzzerFunc(t, fuzzDecodeBool)
}
// various values that fuzz has crashed on in the past
var oldCrashers = []string{
`"浱up蔽Cr"`,
"{\"\":\"0\xc9\"}",
"\"\x90\"",
"{\"\x14\":0}",
"0\xff",
"{\"a\": \"b\", \"\xbf\":\"\"}",
"\"\x1b\"",
"{\"a\":\"\x1b\"}",
"{\"\x1b\":true}",
"\"\x1f\"",
"\"\\'\"",
"80000000000000000000",
"999999999999999999",
"{\"�\":\"0\",\"\xbd\":\"\"}",
"[[0e],0e0]",
"[0.,0e0]",
"[0E,0E0]",
"[0.,0.0]",
"[0e,0e0]",
"{\"\":0.,\"\":0.0}",
"[0E,0.0]",
}
func jsontestsuiteFiles(t testing.TB) []string {
t.Helper()
var files []string
testdir := filepath.FromSlash("testdata/jsontestsuite")
dir, err := ioutil.ReadDir(testdir)
require.NoError(t, err)
for _, info := range dir {
name := info.Name()
if info.IsDir() || !strings.HasSuffix(name, ".json") {
continue
}
files = append(files, filepath.Join(testdir, name))
}
return files
}
func TestValid(t *testing.T) {
t.Run("invalidJSON", func(t *testing.T) {
for _, s := range invalidJSON {
assert.Equalf(t,
json.Valid([]byte(s)),
Valid([]byte(s), nil),
"failed on input: %s", s,
)
}
})
for _, name := range jsontestsuiteFiles(t) {
data, err := ioutil.ReadFile(name)
require.NoError(t, err)
t.Run(name, func(t *testing.T) {
var want bool
switch filepath.Base(name)[0] {
case 'y':
want = true
case 'n':
want = false
default:
want = json.Valid(data)
}
got := Valid(data, nil)
require.Equal(t, want, got, string(data))
})
}
t.Run("oldCrashers", func(t *testing.T) {
for _, s := range oldCrashers {
assert.Equalf(t,
json.Valid([]byte(s)),
Valid([]byte(s), nil),
"failed on input: %s", s,
)
}
})
for _, s := range jsonTestFiles {
t.Run(s, func(t *testing.T) {
data := getTestdataJSONGz(t, s)
assert.Equal(t, json.Valid(data), Valid(data, nil))
})
}
}
func gunzipTestJSON(t testing.TB, filename string) string {
t.Helper()
targetDir := filepath.Join("testdata", "tmp")
err := os.MkdirAll(targetDir, 0o700)
require.NoError(t, err)
target := filepath.Join(targetDir, filename)
if fileExists(t, target) {
return target
}
src := filepath.Join("testdata", filename+".gz")
f, err := os.Open(src)
require.NoError(t, err)
defer func() {
require.NoError(t, f.Close())
}()
gz, err := gzip.NewReader(f)
require.NoError(t, err)
buf, err := ioutil.ReadAll(gz)
require.NoError(t, err)
err = ioutil.WriteFile(target, buf, 0o600)
require.NoError(t, err)
return target
}
func getTestdataJSONGz(t testing.TB, path string) []byte {
t.Helper()
filename := gunzipTestJSON(t, path)
got, err := ioutil.ReadFile(filename)
require.NoError(t, err)
return got
}
func fileExists(t testing.TB, filename string) bool {
t.Helper()
_, err := os.Stat(filename)
if errors.Is(err, os.ErrNotExist) {
return false
}
require.NoError(t, err)
return true
}
// examples taken from github.com/pkg/json
var invalidJSON = []string{
`[`,
`{"":2`,
`[[[[]]]`,
`{"`,
`{"":` + "\n" + `}`,
`{{"key": 1}: 2}}`,
`{1: 1}`,
`[[],[], [[]],�[[]]]`,
`+`,
`,`,
`1.e1`,
`{"a":"b":"c"}`,
`{"test"::"input"}`,
`e1`,
`-.1e-1`,
`123.`,
`--123`,
`.1`,
`0.1e`,
"{\"foo\": \"\x14\"}",
"{\"\x14\":0}",
}