Skip to content

Commit

Permalink
add fuzz tests and fix several bugs found by them
Browse files Browse the repository at this point in the history
  • Loading branch information
sivukhin committed Feb 18, 2024
1 parent 09feeec commit 6c6256e
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 6 deletions.
2 changes: 1 addition & 1 deletion djot_parser/djot_ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ func buildDjotAst(
Children: []TreeNode[DjotNode]{{Type: TextNode, Text: []byte("↩︎︎")}},
Attributes: attributes,
}
if children[len(children)-1].Type == ParagraphNode {
if len(children) > 0 && children[len(children)-1].Type == ParagraphNode {
children[len(children)-1].Children = append(children[len(children)-1].Children, backrefLinkNode)
} else {
children = append(children, TreeNode[DjotNode]{Type: ParagraphNode, Children: []TreeNode[DjotNode]{backrefLinkNode}})
Expand Down
42 changes: 42 additions & 0 deletions djot_parser/djot_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package djot_parser

import (
"fmt"
"os"
"path"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/sivukhin/godjot/djot_tokenizer"
)

func seedFuzz(f *testing.F) {
dir, err := os.ReadDir(examplesDir)
require.Nil(f, err)
for _, entry := range dir {
name := entry.Name()
example, ok := strings.CutSuffix(name, ".html")
if !ok {
continue
}
djotExample, err := os.ReadFile(path.Join(examplesDir, fmt.Sprintf("%v.djot", example)))
require.Nil(f, err)
f.Add(string(djotExample))
}
}

func FuzzDjotE2E(f *testing.F) {
seedFuzz(f)
f.Fuzz(func(t *testing.T, input string) { _ = printDjot(input) })
}

func FuzzDjotTokenizer(f *testing.F) {
seedFuzz(f)
f.Fuzz(func(t *testing.T, input string) { _ = djot_tokenizer.BuildDjotTokens([]byte(input)) })
}

func TestName(t *testing.T) {
t.Log(djot_tokenizer.BuildDjotTokens([]byte("[^]:")))
}
3 changes: 3 additions & 0 deletions djot_parser/examples/empty-footnote.djot
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[^]

[^]:
9 changes: 9 additions & 0 deletions djot_parser/examples/empty-footnote.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p><a id="fnref1" href="#fn1" role="doc-noteref"><sup>1</sup></a></p>
<section role="doc-endnotes">
<hr>
<ol>
<li id="fn1">
<p><a href="#fnref1" role="doc-backlink">↩︎︎</a></p>
</li>
</ol>
</section>
2 changes: 2 additions & 0 deletions djot_parser/testdata/fuzz/FuzzDjotE2E/505457fa1e917292
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("[^]:")
2 changes: 2 additions & 0 deletions djot_parser/testdata/fuzz/FuzzDjotE2E/55c570d46dd4cf42
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
string("```\r0")
13 changes: 8 additions & 5 deletions djot_tokenizer/djot_block_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
)

var (
NotSpaceByteMask = tokenizer.SpaceNewLineByteMask.Negate()
NotBracketByteMask = tokenizer.NewByteMask([]byte("]")).Negate()
ThematicBreakByteMask = tokenizer.NewByteMask([]byte(" \t\n*-"))
NotSpaceNewLineByteMask = tokenizer.SpaceNewLineByteMask.Negate()
NotBracketByteMask = tokenizer.NewByteMask([]byte("]")).Negate()
ThematicBreakByteMask = tokenizer.NewByteMask([]byte(" \t\n*-"))

DigitByteMask = tokenizer.NewByteMask([]byte("0123456789"))
LowerAlphaByteMask = tokenizer.NewByteMask([]byte("abcdefghijklmnopqrstuvwxyz"))
Expand Down Expand Up @@ -79,8 +79,11 @@ func MatchBlockToken(
}

metaStart := next
next, ok = r.MaskRepeat(next, NotSpaceByteMask, 1)
tokenizer.Assertf(ok, "MaskRepeat must match because !r.IsEmpty(next) and next symbol is not in SpaceByteMask")
next, ok = r.MaskRepeat(next, NotSpaceNewLineByteMask, 1)
// usually MaskRepeat must match because !r.IsEmpty(next) and next symbol is not in SpaceByteMask but in some broken cases this can fail (see panic1 test)
if !ok {
return fail()
}
metaEnd := next

if next, ok = r.EmptyOrWhiteSpace(next); !ok {
Expand Down

0 comments on commit 6c6256e

Please sign in to comment.