Skip to content

Commit

Permalink
test: add tag emitter fuzz target
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Apr 11, 2023
1 parent bb7e016 commit 3876d30
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,70 @@ func FuzzDecodeEncodeDecode(f *testing.F) {
compareNodes(&v, &v2)
})
}

func FuzzTag(f *testing.F) {
for _, tag := range []string{
// Special characters.
"\x00",
"\x20", // Space
"\t",
"\r",
"\n",

// Invalid UTF-8.
"\xff",
"\xca",
"\xf0\x9f\xa4",

// Tag characters.
"!",
"!!",
"!<tag>",

// Comment characters.
"#",
"#\n#",

// Flow collection characters.
"[]",
"{}",
",",

// Just text.
"a",
"foo",

// Unicode.
"тег",
"🤷",

// Existing tags.
"str",
"tag:yaml.org,2002:str",
} {
f.Add(tag)
}

f.Fuzz(func(t *testing.T, tag string) {
n := &yaml.Node{
Kind: yaml.ScalarNode,
Tag: tag,
Value: "foo",
}

data, err := yaml.Marshal(n)
if err != nil {
require.ErrorContains(t, err, "invalid UTF-8 in tag")
return
}

var n2 *yaml.Node
require.NoError(t, yaml.Unmarshal(data, &n2))
if n2.Kind == yaml.DocumentNode {
n2 = n2.Content[0]
}

require.Equal(t, n.LongTag(), n2.LongTag())
require.Equal(t, n.Value, n2.Value)
})
}

0 comments on commit 3876d30

Please sign in to comment.