Skip to content

Commit 9ba4022

Browse files
committed
Create simple fuzz tests
1 parent a1d2f05 commit 9ba4022

File tree

134 files changed

+154
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+154
-0
lines changed

cbor/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cbor-fuzz.zip

cbor/cborFixtures_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"bytes"
55
"encoding/base64"
66
"fmt"
7+
"io/ioutil"
8+
"testing"
79

810
. "github.com/polydawn/refmt/tok"
911
"github.com/polydawn/refmt/tok/fixtures"
@@ -31,6 +33,21 @@ func deB64(s string) []byte {
3133
return bs
3234
}
3335

36+
func TestExportFixtures(t *testing.T) {
37+
t.SkipNow()
38+
for i, fix := range cborFixtures {
39+
if fix.encodeResult != nil || fix.decodeResult != nil {
40+
continue
41+
}
42+
43+
err := ioutil.WriteFile(fmt.Sprintf("fuzz-data/corpus/%d", i), fix.serial, 0666)
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
}
48+
49+
}
50+
3451
var inapplicable = fmt.Errorf("skipme: inapplicable")
3552

3653
var cborFixtures = []struct {

cbor/cbor_fuzz.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// +build gofuzz
2+
3+
package cbor
4+
5+
import (
6+
"bytes"
7+
"fmt"
8+
9+
"github.com/polydawn/refmt/shared"
10+
)
11+
12+
func Fuzz(data []byte) int {
13+
dec := NewDecoder(bytes.NewReader(data))
14+
15+
buf := &bytes.Buffer{}
16+
17+
pump := shared.TokenPump{dec, NewEncoder(buf)}
18+
err := pump.Run()
19+
if err != nil {
20+
return 0
21+
}
22+
out := buf.Bytes()
23+
if !bytes.Equal(out, data[:len(out)]) {
24+
fmt.Printf("input: %v, output: %v\n", data[:len(out)], out)
25+
panic("output not the same")
26+
}
27+
28+
if len(out) == len(data) {
29+
return 2
30+
}
31+
return 1
32+
}

cbor/fuzz-data/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*
2+
!.gitignore
3+
!corpus
4+
!corpus/*

cbor/fuzz-data/corpus/0

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
evalue
9 Bytes
Binary file not shown.
12 Bytes
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�3�004I
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
˕˕
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
y50

0 commit comments

Comments
 (0)