...

Source file src/github.com/yuin/goldmark/fuzz/fuzz_test.go

Documentation: github.com/yuin/goldmark/fuzz

     1  package fuzz
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io/ioutil"
     7  	"testing"
     8  
     9  	"github.com/yuin/goldmark"
    10  	"github.com/yuin/goldmark/extension"
    11  	"github.com/yuin/goldmark/parser"
    12  	"github.com/yuin/goldmark/renderer/html"
    13  	"github.com/yuin/goldmark/util"
    14  )
    15  
    16  func fuzz(f *testing.F) {
    17  	f.Fuzz(func(t *testing.T, orig string) {
    18  		markdown := goldmark.New(
    19  			goldmark.WithParserOptions(
    20  				parser.WithAutoHeadingID(),
    21  				parser.WithAttribute(),
    22  			),
    23  			goldmark.WithRendererOptions(
    24  				html.WithUnsafe(),
    25  				html.WithXHTML(),
    26  			),
    27  			goldmark.WithExtensions(
    28  				extension.DefinitionList,
    29  				extension.Footnote,
    30  				extension.GFM,
    31  				extension.Typographer,
    32  				extension.Linkify,
    33  				extension.Table,
    34  				extension.TaskList,
    35  			),
    36  		)
    37  		var b bytes.Buffer
    38  		if err := markdown.Convert(util.StringToReadOnlyBytes(orig), &b); err != nil {
    39  			panic(err)
    40  		}
    41  	})
    42  }
    43  
    44  func FuzzDefault(f *testing.F) {
    45  	bs, err := ioutil.ReadFile("../_test/spec.json")
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	var testCases []map[string]interface{}
    50  	if err := json.Unmarshal(bs, &testCases); err != nil {
    51  		panic(err)
    52  	}
    53  	for _, c := range testCases {
    54  		f.Add(c["markdown"])
    55  	}
    56  	fuzz(f)
    57  }
    58  

View as plain text