Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create simple fuzz tests for CBOR #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cbor/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cbor-fuzz.zip
18 changes: 15 additions & 3 deletions cbor/cborDecoderTerminals.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ package cbor

import (
"encoding/binary"
"errors"
"fmt"
"math"
)

const (
maxUint = ^uint(0)
maxInt = int(maxUint >> 1)
)

func (d *Decoder) decodeFloat(majorByte byte) (f float64, err error) {
var bs []byte
switch majorByte {
Expand Down Expand Up @@ -66,8 +72,12 @@ func (d *Decoder) decodeNegInt(majorByte byte) (i int64, err error) {
if err != nil {
return 0, err
}
// TODO needs overflow check
return -1 - int64(ui), nil
pos := ui + 1
if pos > uint64(-math.MinInt64) {
return -1, errors.New("cbor: negative integer out of rage of int64 type")
}

return -int64(pos), nil
}

// Decode expecting a positive integer.
Expand All @@ -80,7 +90,9 @@ func (d *Decoder) decodeLen(majorByte byte) (i int, err error) {
if err != nil {
return 0, err
}
// TODO needs overflow check
if ui > uint64(maxInt) {
return 0, errors.New("cbor: positive integer is out of length")
}
return int(ui), nil
}

Expand Down
17 changes: 17 additions & 0 deletions cbor/cborFixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"testing"

. "github.com/polydawn/refmt/tok"
"github.com/polydawn/refmt/tok/fixtures"
Expand Down Expand Up @@ -31,6 +33,21 @@ func deB64(s string) []byte {
return bs
}

func TestExportFixtures(t *testing.T) {
t.SkipNow()
for i, fix := range cborFixtures {
if fix.encodeResult != nil || fix.decodeResult != nil {
continue
}

err := ioutil.WriteFile(fmt.Sprintf("fuzz-data/corpus/%d", i), fix.serial, 0666)
if err != nil {
t.Fatal(err)
}
}

}

var inapplicable = fmt.Errorf("skipme: inapplicable")

var cborFixtures = []struct {
Expand Down
44 changes: 44 additions & 0 deletions cbor/cbor_fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build gofuzz

package cbor

import (
"bytes"
"fmt"

"github.com/polydawn/refmt/shared"
)

func Fuzz(data []byte) int {
dec := NewDecoder(bytes.NewReader(data))
buf := &bytes.Buffer{}

// Run it once to sanitize
pump := shared.TokenPump{dec, NewEncoder(buf)}
err := pump.Run()
if err != nil {
return 0
}

// Run second loop to check stability
sanitized := buf.Bytes()
dec = NewDecoder(bytes.NewReader(sanitized))
buf = &bytes.Buffer{}
pump = shared.TokenPump{dec, NewEncoder(buf)}
err = pump.Run()
if err != nil {
fmt.Printf("input: %v, sanitized: %v", data, sanitized)
panic("sanitised failed to prase: " + err.Error())
}

out := buf.Bytes()
if !bytes.Equal(out, sanitized) {
fmt.Printf("santized: %v, output: %v\n", sanitized, out)
panic("looping data failed")
}

if len(out) == len(data) {
return 2
}
return 1
}
4 changes: 4 additions & 0 deletions cbor/fuzz-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!.gitignore
!corpus
!corpus/*
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
evalue
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ebk2bv2�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ak�ak�-�ak�-�ak�ak�-�ak�ak�-�ak�ak�ak�ak�-�ak�ak�ak�-�ak�ak�-�ak�ak�ak�ak�ak�ak�-�ak�ak�ak�56
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�b���5�5�5�5�5�5�5�5�5�5�5�5�5�5�5�5���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��_��_��_��_��_��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�5v2151512�5uebv2155111512312572702%:15312572702%:
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����`�``�`���`�`���`���`����`���`��`����
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�6134751:8882348962738v-06131347$>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
� ��� ����
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
؀�؀����؀�����؀�������؀���������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������������������������������������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����`�``�`���`�`������������������������������������������������������
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�eluebv346944695195361418882384896278381347656252�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/10
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/11
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/12
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�evalue
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/13
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�evalue�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�489627834796278347653252�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/15
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�evaluebv2
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�3L300472H333611R4410150625�evalue���
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/16
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�evaluebv2�
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/17
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�bk1bv1bke�bohdwheecwow��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��(���(��2�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/18
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�bke�bohdwheecwow�bk1bv1�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������������
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/19
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��akavdwhee�bk1bv1
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ρak�2�a�Ρakava
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���2�
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
� ��� �� ����� �� ������
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/20
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��akav�dwhee�bk1bv1��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
B@
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/21
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�4896278347656252�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/22
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
������
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/23
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ak�bk2bv2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������|������|���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Y
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/24
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ak�bk2bv2��
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/25
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��(��2�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/26
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
� ��� �� �����
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/28
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ak�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/29
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�ak��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�b�e2220446025031300472613533361107144567552939551135250625333611640625B@�evalue���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�68081828+8485868788898888528384%58688888
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��_��_��_��_��_�_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�;��������;��������;��������;��������;��������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������������������
2 changes: 2 additions & 0 deletions cbor/fuzz-data/corpus/3
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
qstr
broken tabbed
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/30
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����������
Binary file added cbor/fuzz-data/corpus/33
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�bke���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/35
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/36
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/37
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�r�)��b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
؀�؀�؀����
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�b���5���
Binary file added cbor/fuzz-data/corpus/39
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��_��_��_��_��_��_��_��_�_
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�22044602503130047223415101562561353336110714456755293953613525062533361164062���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��@@@@@@@@@�@@@@@@@@�
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�o hex data for %x ringb�e22204460250313004726333611640625�evalue���
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
������������������
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Binary file added cbor/fuzz-data/corpus/40
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
������������������������������������
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/41
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Evalue
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
����
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������������������,�����������,�����������,��,��
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�b���5�5�5�5�5�5�5�5���
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�`����`����
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
���������������������
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��|���
Binary file added cbor/fuzz-data/corpus/44
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/45
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�2�akav
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ؐ�
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/46
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�2ewahoo
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/47
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��(��2c500
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/48
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�bk1�2c500bk2huntaggedbk3�<c600bk4��2dasdf�2dqwerbk5�2c505
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�������������������������������������
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�`�`���`�`_����`���`��`���`����
1 change: 1 addition & 0 deletions cbor/fuzz-data/corpus/5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��
Loading