From 841c1a8ce80cdb24f7763e3520a590aa383f3538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 11 Mar 2024 20:25:41 +0100 Subject: [PATCH] Add fuzzer: FuzzEncodeFromJSON Add fuzzing for roundtripping by using JSON documents as input --- fuzz_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 fuzz_test.go diff --git a/fuzz_test.go b/fuzz_test.go new file mode 100644 index 00000000..89975a4d --- /dev/null +++ b/fuzz_test.go @@ -0,0 +1,74 @@ +//go:build go1.18 +// +build go1.18 + +package yaml_test + +import ( + "bytes" + "encoding/json" + "testing" + + "gopkg.in/yaml.v3" +) + +// FuzzEncodeFromJSON checks that any JSON encoded value can also be encoded as YAML... and decoded. +func FuzzEncodeFromJSON(f *testing.F) { + f.Add(`null`) + f.Add(`""`) + f.Add(`0`) + f.Add(`true`) + f.Add(`false`) + f.Add(`{}`) + f.Add(`[]`) + f.Add(`[[]]`) + f.Add(`{"a":[]}`) + + f.Fuzz(func(t *testing.T, s string) { + + var v interface{} + if err := json.Unmarshal([]byte(s), &v); err != nil { + t.Skip("not valid JSON") + } + + t.Logf("JSON %q", s) + t.Logf("Go %q <%[1]x>", v) + + // Encode as YAML + b, err := yaml.Marshal(v) + if err != nil { + t.Error(err) + } + t.Logf("YAML %q <%[1]x>", b) + + // Decode as YAML + var v2 interface{} + if err := yaml.Unmarshal(b, &v2); err != nil { + t.Error(err) + } + + t.Logf("Go %q <%[1]x>", v2) + + /* + // Handling of number is different, so we can't have universal exact matching + if !reflect.DeepEqual(v2, v) { + t.Errorf("mismatch:\n- got: %#v\n- expected: %#v", v2, v) + } + */ + + b2, err := yaml.Marshal(v2) + if err != nil { + t.Error(err) + } + t.Logf("YAML %q <%[1]x>", b2) + + if !bytes.Equal(b, b2) { + t.Errorf("Marshal->Unmarshal->Marshal mismatch:\n- expected: %q\n- got: %q", b, b2) + } + + }) +} + +func TestEncodeString(t *testing.T) { + b, _ := yaml.Marshal(`\n`) + t.Logf("%q <%[1]x>", string(b)) +}