91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func FuzzPathKeyRoundTrip(f *testing.F) {
|
|
for _, seed := range []string{"name", "", "a.b", "x[y]", "日本語", " leading ", "quote\"slash\\"} {
|
|
f.Add(seed)
|
|
}
|
|
f.Fuzz(func(t *testing.T, key string) {
|
|
if !utf8.ValidString(key) {
|
|
t.Skip()
|
|
}
|
|
for _, prefix := range []string{"", "root"} {
|
|
path := appendPathKey(prefix, key)
|
|
tokens, err := parsePath(path)
|
|
if err != nil {
|
|
t.Fatalf("parse generated path %q: %v", path, err)
|
|
}
|
|
last := tokens[len(tokens)-1]
|
|
if last.isIndex || last.key != key {
|
|
t.Fatalf("path %q decoded final token %#v, want key %q", path, last, key)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func FuzzParseJSON(f *testing.F) {
|
|
for _, seed := range [][]byte{
|
|
[]byte(`{"name":"Alice","items":[1,2,null]}`),
|
|
[]byte(`{"duplicate":1,"duplicate":2}`),
|
|
[]byte(`[]`),
|
|
[]byte(`{"a":1} {"b":2}`),
|
|
{0xff},
|
|
} {
|
|
f.Add(seed)
|
|
}
|
|
f.Fuzz(func(t *testing.T, input []byte) {
|
|
if len(input) > 64*1024 {
|
|
t.Skip()
|
|
}
|
|
value, err := parseStructured(input, "json")
|
|
if err != nil {
|
|
return
|
|
}
|
|
encoded, err := json.Marshal(value)
|
|
if err != nil {
|
|
t.Fatalf("marshal accepted value: %v", err)
|
|
}
|
|
if !json.Valid(encoded) {
|
|
t.Fatalf("parser returned value that encoded as invalid JSON")
|
|
}
|
|
})
|
|
}
|
|
|
|
func FuzzDelimitedTableRoundTrip(f *testing.F) {
|
|
for _, seed := range []string{
|
|
"plain",
|
|
" surrounding spaces ",
|
|
"comma, tab\t quote\"",
|
|
"first line\nsecond line",
|
|
"日本語と絵文字🌙",
|
|
} {
|
|
f.Add(seed)
|
|
}
|
|
f.Fuzz(func(t *testing.T, value string) {
|
|
if !utf8.ValidString(value) || strings.Contains(value, "\r\n") {
|
|
t.Skip()
|
|
}
|
|
want := table{Header: []string{"value"}, Rows: [][]string{{value}}}
|
|
for _, format := range []string{"csv", "tsv"} {
|
|
encoded, err := encodeTable(want, format, "Sheet1")
|
|
if err != nil {
|
|
t.Fatalf("encode %s: %v", format, err)
|
|
}
|
|
got, err := parseTable(encoded, format, "Sheet1")
|
|
if err != nil {
|
|
t.Fatalf("parse %s: %v", format, err)
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("%s round trip = %#v, want %#v", format, got, want)
|
|
}
|
|
}
|
|
})
|
|
}
|