/
fuzz_test.go
62 lines (50 loc) · 1.06 KB
/
fuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// chris 080715
package main
import (
"bytes"
"os"
"path"
"testing"
"time"
"io/ioutil"
"math/rand"
"github.com/crunchyroll/rebnf"
"golang.org/x/exp/ebnf"
)
func testFuzz(t *testing.T, srcname, start string) {
var err error
var grammar ebnf.Grammar
var tempfile *os.File
grammar, err = rebnf.Parse(srcname, nil)
if err != nil {
t.Error(err)
}
dst := new(bytes.Buffer)
ctx := rebnf.NewCtx(20, 20, " ", false)
err = ctx.Random(dst, grammar, start)
if err != nil {
t.Error(err)
}
dstbytes := dst.Bytes()
// Test gofmt.
testFmt(t, "-", bytes.NewBuffer(dstbytes))
// Test go build.
tempfile, err = ioutil.TempFile("test", "tmp_")
if err != nil {
t.Error(err)
}
tempfile.Close()
testBuild(t, tempfile.Name(), "-", bytes.NewBuffer(dstbytes))
}
func TestFuzz(t *testing.T) {
if testing.Short() {
t.Skip("skipping fuzz tests in short mode")
}
seed := time.Now().UTC().UnixNano()
rand.Seed(seed)
t.Logf("chose random seed %d\n", seed)
srcname := path.Join("test", "struct.ebnf")
for i := 0; i < 100; i++ {
testFuzz(t, srcname, "Start")
}
}