Skip to content

Commit

Permalink
test: add json corpus
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Jan 16, 2022
1 parent ed2c257 commit e00d36c
Show file tree
Hide file tree
Showing 321 changed files with 424 additions and 0 deletions.
22 changes: 22 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@ package jx
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

"github.com/go-faster/errors"
"github.com/stretchr/testify/require"
)

func addCorpus(f *testing.F) {
f.Helper()
dir := filepath.Join("testdata", "test_parsing")
files, err := os.ReadDir(dir)
require.NoError(f, err)
for _, d := range files {
if d.IsDir() || !strings.HasSuffix(d.Name(), ".json") {
continue
}
name := filepath.Join(dir, d.Name())
data, err := os.ReadFile(name)
require.NoError(f, err)
f.Add(data)
}
}

func FuzzValid(f *testing.F) {
for _, s := range []string{
"{}",
Expand All @@ -22,6 +42,7 @@ func FuzzValid(f *testing.F) {
} {
f.Add([]byte(s))
}
addCorpus(f)
f.Fuzz(func(t *testing.T, data []byte) {
var (
std = json.Valid(data)
Expand All @@ -43,6 +64,7 @@ func FuzzDecEnc(f *testing.F) {
f.Add([]byte(`"a\ufffdz"`))
f.Add([]byte(`"\\nH\\tel\\tl\\ro\\\\World\\r\n\rHello\r\tHi"`))
f.Add([]byte(`"key:\"/registry/runtimeclasses/\" range_end:\"/registry/runtimeclasses0\" count_only:true "`))
addCorpus(f)
f.Fuzz(func(t *testing.T, data []byte) {
r := GetDecoder()
r.ResetBytes(data)
Expand Down
63 changes: 63 additions & 0 deletions suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package jx

import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSuite(t *testing.T) {
// https://github.com/nst/JSONTestSuite
// By Nicolas Seriot (https://github.com/nst)
dir := filepath.Join("testdata", "test_parsing")
files, err := os.ReadDir(dir)
require.NoError(t, err)

const (
Accept = "y_"
Reject = "n_"
Undefined = "i_"
)
for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") {
continue
}

name := filepath.Join(dir, f.Name())
action := f.Name()[:2]

t.Run(f.Name(), func(t *testing.T) {
data, err := os.ReadFile(name)
require.NoError(t, err)

r := GetDecoder()
r.ResetBytes(data)
defer PutDecoder(r)

_, decodeErr := r.Any()
switch action {
case Accept:
assert.True(t, Valid(data), "validate")
assert.True(t, json.Valid(data), "std")
assert.NoError(t, decodeErr, "%#v", string(data))
case Reject:
assert.False(t, Valid(data), "validate")
assert.False(t, json.Valid(data), "std")
// TODO: assert decodeErr + buffer drain?
case Undefined:
if decodeErr == nil {
t.Log("Accept")
} else {
t.Logf("Reject: %v", decodeErr)
}
default:
t.Fatalf("Unkown prefix %q", action)
}
})
}
}
21 changes: 21 additions & 0 deletions testdata/test_parsing/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Nicolas Seriot

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_double_huge_neg_exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[123.456e-789]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_huge_exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0.4e00669999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999969999999006]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_neg_int_huge_exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-1e+9999]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_pos_double_huge_exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1.5e+9999]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_real_neg_overflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-123123e100000]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_real_pos_overflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[123123e100000]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_real_underflow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[123e-10000000]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_too_big_neg_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-123123123123123123123123123123]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_too_big_pos_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[100000000000000000000]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_number_very_big_negative_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-237462374673276894279832749832423479823246327846]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_object_key_lone_2nd_surrogate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"\uDFAA":0}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uDADA"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uD888\u1234"]
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_UTF-8_invalid_sequence.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["日ш�"]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_UTF8_surrogate_U+D800.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["���"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uD800\n"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uDd1ea"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uD800\uD800\n"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\ud800"]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_invalid_surrogate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\ud800abc"]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_invalid_utf-8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[""]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uDd1e\uD834"]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_iso_latin_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[""]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_lone_second_surrogate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["\uDFAA"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[""]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_not_in_unicode_range.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["����"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["��"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["������"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["������"]
1 change: 1 addition & 0 deletions testdata/test_parsing/i_string_truncated-utf-8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["��"]
Binary file added testdata/test_parsing/i_string_utf16BE_no_BOM.json
Binary file not shown.
Binary file added testdata/test_parsing/i_string_utf16LE_no_BOM.json
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/test_parsing/i_structure_500_nested_arrays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_1_true_without_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1 true]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_a_invalid_utf8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[a�]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_colon_instead_of_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["": 1]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_comma_after_close.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[""],
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_comma_and_number.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[,1]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_double_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,,2]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_double_extra_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["x",,]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_extra_close.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["x"]]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_extra_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["",]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_incomplete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["x"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[x
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_inner_array_no_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[3[4]]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_invalid_utf8.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1:2]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_just_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[,]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_just_minus.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_missing_value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ , ""]
3 changes: 3 additions & 0 deletions testdata/test_parsing/n_array_newlines_unclosed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
["a",
4
,1,
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_number_and_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,,]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[" a"\f]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_star_inside.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[*]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_unclosed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[""
1 change: 1 addition & 0 deletions testdata/test_parsing/n_array_unclosed_trailing_comma.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1,
3 changes: 3 additions & 0 deletions testdata/test_parsing/n_array_unclosed_with_new_lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[1,
1
,1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{}
1 change: 1 addition & 0 deletions testdata/test_parsing/n_incomplete_false.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[fals]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_incomplete_null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[nul]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_incomplete_true.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[tru]
Binary file not shown.
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_++.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[++1234]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_+1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[+1]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_+Inf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[+Inf]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_-01.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-01]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_-1.0..json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-1.0.]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_-2..json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-2.]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_-NaN.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[-NaN]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_.-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[.-1]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_.2e-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[.2e-3]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0.1.2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0.1.2]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0.3e+.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0.3e+]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0.3e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0.3e]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0.e1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0.e1]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0_capital_E+.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0E+]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0_capital_E.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0E]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0e+.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0e+]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_0e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[0e]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_1.0e+.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1.0e+]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_1.0e-.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1.0e-]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_1.0e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1.0e]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_1_000.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1 000.0]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_1eE2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1eE2]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_2.e+3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2.e+3]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_2.e-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2.e-3]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_2.e3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[2.e3]
1 change: 1 addition & 0 deletions testdata/test_parsing/n_number_9.e+.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[9.e+]
Loading

0 comments on commit e00d36c

Please sign in to comment.