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
63
64
65
66
67
68
|
package caesar
import (
"bytes"
"flag"
"io"
"io/ioutil"
"os"
"testing"
)
var (
runFuzz bool
fuzzInput string
)
func TestMain(m *testing.M) {
flag.BoolVar(&runFuzz, "fuzz", false, "run the fuzz tests")
flag.StringVar(&fuzzInput, "fuzz.input", "", "input file for fuzz test")
flag.Parse()
os.Exit(m.Run())
}
func TestFuzz(t *testing.T) {
if !runFuzz {
t.Skip("skipping fuzz test")
}
f, err := os.Open(fuzzInput)
if err != nil {
t.Fatalf("could not open fuzz input: %v", err)
}
defer f.Close()
data, err := ioutil.ReadAll(f)
if err != nil {
t.Fatalf("could not read fuzz input: %v", err)
}
buf := bytes.NewBuffer(data)
for {
// read input
inp, err := buf.ReadBytes('\t')
if err == io.EOF {
return
} else if err != nil {
t.Fatalf("read error: %v", err)
}
// read expected output
exp, err := buf.ReadBytes('\n')
if err == io.EOF {
t.Fatalf("found input with no matching output")
} else if err != nil {
t.Fatalf("read error: %v", err)
}
if len(inp) < 2 || len(exp) < 2 {
t.Fatalf("malformed input")
}
result := Encode(string(inp[:len(inp)-2]))
if result != string(exp[:len(exp)-2]) {
t.Logf("input: %s", inp)
t.Logf("output: %s", result)
t.Logf("expected: %s", exp)
t.Fail()
}
}
Encode(string(data))
}
|