-
Notifications
You must be signed in to change notification settings - Fork 30
Add fuzzer: FuzzEncodeFromJSON which signals roundtripping issues #35
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,73 @@ | ||||||||||||||||||
| //go:build go1.18 | ||||||||||||||||||
| // +build go1.18 | ||||||||||||||||||
|
|
||||||||||||||||||
| package yaml_test | ||||||||||||||||||
|
|
||||||||||||||||||
| import ( | ||||||||||||||||||
| "bytes" | ||||||||||||||||||
| "encoding/json" | ||||||||||||||||||
| "testing" | ||||||||||||||||||
|
|
||||||||||||||||||
| "go.yaml.in/yaml/v3" | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| // 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":[]}`) | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this also
Suggested change
|
||||||||||||||||||
| f.Add(`-0`) | ||||||||||||||||||
| f.Add(`-0.000`) | ||||||||||||||||||
| f.Add(`"\n"`) | ||||||||||||||||||
| f.Add(`"\t"`) | ||||||||||||||||||
|
|
||||||||||||||||||
| 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) | ||||||||||||||||||
|
Comment on lines
+34
to
+37
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This maybe
Suggested change
|
||||||||||||||||||
| 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) | ||||||||||||||||||
| } | ||||||||||||||||||
| */ | ||||||||||||||||||
|
Comment on lines
+55
to
+60
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm always suspicious when I see commented out code. What is the need here? Can't it be simply removed? Is it a left behind debug or something that was planned but that was abandoned? |
||||||||||||||||||
|
|
||||||||||||||||||
| 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) | ||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dolmen , what do you think about changing this to If we error, we can't really merge this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now it stops on the first error. Changing to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened #45 to address the With that fix I was able to run a 1m fuzzing test successfully:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've opened #46 to address another issue highlighted by the fuzzing test. With both fixes applied:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fuzzing only happens when you explicitly run it with
This looks fine to merge. |
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| }) | ||||||||||||||||||
| } | ||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please remove the go version from here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we now have "go 1.18" in go.mod this build guard is redundant.
So 👍