goldmark/fuzz/fuzz_test.go
Nathaniel Brough 5e78751e90 fuzz: Make goldmark compatible with OSS-Fuzz
This set of changes will make goldmark compatible with the fuzzing
requirements for google/oss-fuzz, a continuous fuzzing service.
To ensure compatibility this change adds;
- A seperate fuzzing target that doesn't use the builtin fuzz corpus
- A tool to convert the builtin fuzz corpus to a zip file corpus
  that is compatible with oss-fuzz
2023-01-19 12:16:00 -08:00

57 lines
1.1 KiB
Go

package fuzz
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer/html"
"github.com/yuin/goldmark/util"
)
func fuzz(f *testing.F) {
f.Fuzz(func(t *testing.T, orig string) {
markdown := goldmark.New(
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
),
goldmark.WithRendererOptions(
html.WithUnsafe(),
html.WithXHTML(),
),
goldmark.WithExtensions(
extension.DefinitionList,
extension.Footnote,
extension.GFM,
extension.Typographer,
extension.Linkify,
extension.Table,
extension.TaskList,
),
)
var b bytes.Buffer
if err := markdown.Convert(util.StringToReadOnlyBytes(orig), &b); err != nil {
panic(err)
}
})
}
func FuzzDefault(f *testing.F) {
bs, err := ioutil.ReadFile("../_test/spec.json")
if err != nil {
panic(err)
}
var testCases []map[string]interface{}
if err := json.Unmarshal(bs, &testCases); err != nil {
panic(err)
}
for _, c := range testCases {
f.Add(c["markdown"])
}
fuzz(f)
}