Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(fuzz): add testdata #19

Merged
merged 6 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Never modify line endings of corpus.
testdata/fuzz/** text eol=lf
44 changes: 42 additions & 2 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package yaml_test

import (
"os"
"strconv"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -34,6 +36,10 @@ func addFuzzingCorpus(add func(data []byte)) {
"scalar: >\n next\n line\n * one\n",
// https://github.com/go-faster/yamlx/issues/8
"0:\n #00\n - |1 \n 00",
// https://github.com/go-faster/yamlx/pull/19#issuecomment-1221479649
"|+\n\n#00000",
// https://github.com/go-faster/yamlx/pull/19#issuecomment-1229998571
"&x\nc: *x",
}

for _, data := range cases {
Expand Down Expand Up @@ -72,6 +78,7 @@ func FuzzDecodeEncodeDecode(f *testing.F) {
add(tt.Data)
}
}
compareTags, _ := strconv.ParseBool(os.Getenv("YAMLX_FUZZ_COMPARE_TAGS"))

f.Fuzz(func(t *testing.T, input []byte) {
var (
Expand All @@ -93,6 +100,12 @@ func FuzzDecodeEncodeDecode(f *testing.F) {
t.Skipf("Error: %+v", err)
return
}
if v.Kind == yaml.DocumentNode {
// FIXME(tdakkota): parser/scanner thinks that comments are part of children nodes.
v.HeadComment = ""
v.LineComment = ""
v.FootComment = ""
}

a := require.New(t)
data, err = yaml.Marshal(&v)
Expand All @@ -106,10 +119,37 @@ func FuzzDecodeEncodeDecode(f *testing.F) {
return
}

var compareNodes func(n1, n2 *yaml.Node)
var (
compareNodes func(n1, n2 *yaml.Node)
seen map[*yaml.Node]struct{}
)
compareNodes = func(n1, n2 *yaml.Node) {
a.Equal(n1.ShortTag(), n2.ShortTag())
_, seen1 := seen[n1]
_, seen2 := seen[n2]
a.Equal(seen1, seen2)
// Don't compare nodes twice.
if seen1 {
return
}
seen[n1], seen[n2] = struct{}{}, struct{}{}

a.Equal(n1.Kind, n2.Kind)
if compareTags {
a.Equal(n1.ShortTag(), n2.ShortTag())
}
a.Equal(n1.Value, n2.Value)

// Compare aliases and anchors.
a.Equal(n1.Anchor, n2.Anchor)
if n1.Alias == nil {
// Ensure that n2.Alias is nil as well.
a.Nil(n2.Alias)
} else {
a.NotNil(n2.Alias)
compareNodes(n1.Alias, n2.Alias)
}

// Compare children.
a.Equal(len(n1.Content), len(n2.Content))
for i := range n1.Content {
compareNodes(n1.Content[i], n2.Content[i])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("{0}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("?")