Skip to content

Commit 5706d9f

Browse files
committed
cbor: create simple fuzz tests and add intial corpus
1 parent 2e9ba99 commit 5706d9f

File tree

43 files changed

+102
-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.

43 files changed

+102
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
buf := &bytes.Buffer{}
15+
16+
// Run it once to sanitize
17+
pump := shared.TokenPump{dec, NewEncoder(buf)}
18+
err := pump.Run()
19+
if err != nil {
20+
return 0
21+
}
22+
23+
// Run second loop to check stability
24+
sanitized := buf.Bytes()
25+
dec = NewDecoder(bytes.NewReader(sanitized))
26+
buf = &bytes.Buffer{}
27+
pump = shared.TokenPump{dec, NewEncoder(buf)}
28+
err = pump.Run()
29+
if err != nil {
30+
fmt.Printf("input: %v, sanitized: %v", data, sanitized)
31+
panic("sanitised failed to prase: " + err.Error())
32+
}
33+
34+
out := buf.Bytes()
35+
if !bytes.Equal(out, sanitized) {
36+
fmt.Printf("santized: %v, output: %v\n", sanitized, out)
37+
panic("looping data failed")
38+
}
39+
40+
if len(out) == len(data) {
41+
return 2
42+
}
43+
return 1
44+
}

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

cbor/fuzz-data/corpus/10

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

cbor/fuzz-data/corpus/11

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
��

cbor/fuzz-data/corpus/12

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�evalue

cbor/fuzz-data/corpus/13

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�evalue�

cbor/fuzz-data/corpus/15

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
�evaluebv2

0 commit comments

Comments
 (0)