From d63b7747536394fe76607b91a781d626620dc2d9 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Fri, 5 Aug 2022 11:25:24 +0300 Subject: [PATCH 1/6] test(fuzz): add testdata Updates #10 --- ...3016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001 | 2 ++ ...22695dec4f600bb0200536e2084f9f7e5ca10ec94d99bbf541b15f0de9e0 | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001 create mode 100644 testdata/fuzz/FuzzDecodeEncodeDecode/c18522695dec4f600bb0200536e2084f9f7e5ca10ec94d99bbf541b15f0de9e0 diff --git a/testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001 b/testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001 new file mode 100644 index 0000000..f5d39b2 --- /dev/null +++ b/testdata/fuzz/FuzzDecodeEncodeDecode/90513016cb18725627689d6b25adb2c440d71f64d72e23ef201ed3ac51443001 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("{0}") diff --git a/testdata/fuzz/FuzzDecodeEncodeDecode/c18522695dec4f600bb0200536e2084f9f7e5ca10ec94d99bbf541b15f0de9e0 b/testdata/fuzz/FuzzDecodeEncodeDecode/c18522695dec4f600bb0200536e2084f9f7e5ca10ec94d99bbf541b15f0de9e0 new file mode 100644 index 0000000..29ee1be --- /dev/null +++ b/testdata/fuzz/FuzzDecodeEncodeDecode/c18522695dec4f600bb0200536e2084f9f7e5ca10ec94d99bbf541b15f0de9e0 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("?") From e9df86f6d38eb9e5f14386f5f3e42452b02c51ec Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sat, 20 Aug 2022 23:16:54 +0300 Subject: [PATCH 2/6] test: move tag comparison under flag, compare kinds instead --- fuzz_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/fuzz_test.go b/fuzz_test.go index efa1708..9483855 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -1,6 +1,8 @@ package yaml_test import ( + "os" + "strconv" "testing" "github.com/stretchr/testify/require" @@ -72,6 +74,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 ( @@ -108,7 +111,10 @@ func FuzzDecodeEncodeDecode(f *testing.F) { var compareNodes func(n1, n2 *yaml.Node) compareNodes = func(n1, n2 *yaml.Node) { - a.Equal(n1.ShortTag(), n2.ShortTag()) + a.Equal(n1.Kind, n2.Kind) + if compareTags { + a.Equal(n1.ShortTag(), n2.ShortTag()) + } a.Equal(n1.Value, n2.Value) a.Equal(len(n1.Content), len(n2.Content)) for i := range n1.Content { From 0204d896a5b3ccae6a3b787c347b73eed74156df Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sat, 20 Aug 2022 23:17:10 +0300 Subject: [PATCH 3/6] test: compare anchors and aliases as well --- fuzz_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/fuzz_test.go b/fuzz_test.go index 9483855..4d41213 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -116,6 +116,18 @@ func FuzzDecodeEncodeDecode(f *testing.F) { 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]) From e801f58a9d7e2ac4f9ada96ea0b0e1c263675f55 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sat, 20 Aug 2022 23:20:33 +0300 Subject: [PATCH 4/6] chore: add .gitattributes for fuzz corpus --- .gitattributes | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d70d3d6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Never modify line endings of corpus. +testdata/fuzz/** text eol=lf From 17cf214227f9c35579651e90cbccf156bc447c9c Mon Sep 17 00:00:00 2001 From: tdakkota Date: Tue, 23 Aug 2022 04:45:36 +0300 Subject: [PATCH 5/6] chore: reset comments of the root node --- fuzz_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/fuzz_test.go b/fuzz_test.go index 4d41213..fafe872 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -36,6 +36,8 @@ 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", } for _, data := range cases { @@ -96,6 +98,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) From 55e8d716f5070528de1c60a79d60c4ec732eb9a7 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Mon, 29 Aug 2022 12:28:57 +0300 Subject: [PATCH 6/6] test: do not compare same node twice Alias node can refer to itself. --- fuzz_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/fuzz_test.go b/fuzz_test.go index fafe872..0cecba8 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -38,6 +38,8 @@ func addFuzzingCorpus(add func(data []byte)) { "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 { @@ -117,8 +119,20 @@ 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) { + _, 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())